[ARM] 4814/1: RealView: Add broadcasting clockevents support for ARM11MPCore
[safe/jmp/linux-2.6] / arch / arm / mach-realview / localtimer.c
1 /*
2  *  linux/arch/arm/mach-realview/localtimer.c
3  *
4  *  Copyright (C) 2002 ARM Ltd.
5  *  All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/smp.h>
16 #include <linux/jiffies.h>
17 #include <linux/percpu.h>
18 #include <linux/clockchips.h>
19
20 #include <asm/mach/time.h>
21 #include <asm/hardware/arm_twd.h>
22 #include <asm/hardware/gic.h>
23 #include <asm/hardware.h>
24 #include <asm/io.h>
25 #include <asm/irq.h>
26
27 #define TWD_BASE(cpu)   (__io_address(REALVIEW_TWD_BASE) + \
28                          ((cpu) * REALVIEW_TWD_SIZE))
29
30 static DEFINE_PER_CPU(struct clock_event_device, local_clockevent);
31
32 /*
33  * Used on SMP for either the local timer or IPI_TIMER
34  */
35 void local_timer_interrupt(void)
36 {
37         struct clock_event_device *clk = &__get_cpu_var(local_clockevent);
38
39         clk->event_handler(clk);
40 }
41
42 #ifdef CONFIG_LOCAL_TIMERS
43
44 static unsigned long mpcore_timer_rate;
45
46 /*
47  * local_timer_ack: checks for a local timer interrupt.
48  *
49  * If a local timer interrupt has occurred, acknowledge and return 1.
50  * Otherwise, return 0.
51  */
52 int local_timer_ack(void)
53 {
54         void __iomem *base = TWD_BASE(smp_processor_id());
55
56         if (__raw_readl(base + TWD_TIMER_INTSTAT)) {
57                 __raw_writel(1, base + TWD_TIMER_INTSTAT);
58                 return 1;
59         }
60
61         return 0;
62 }
63
64 void __cpuinit local_timer_setup(unsigned int cpu)
65 {
66         void __iomem *base = TWD_BASE(cpu);
67         unsigned int load, offset;
68         u64 waitjiffies;
69         unsigned int count;
70
71         /*
72          * If this is the first time round, we need to work out how fast
73          * the timer ticks
74          */
75         if (mpcore_timer_rate == 0) {
76                 printk("Calibrating local timer... ");
77
78                 /* Wait for a tick to start */
79                 waitjiffies = get_jiffies_64() + 1;
80
81                 while (get_jiffies_64() < waitjiffies)
82                         udelay(10);
83
84                 /* OK, now the tick has started, let's get the timer going */
85                 waitjiffies += 5;
86
87                                  /* enable, no interrupt or reload */
88                 __raw_writel(0x1, base + TWD_TIMER_CONTROL);
89
90                                  /* maximum value */
91                 __raw_writel(0xFFFFFFFFU, base + TWD_TIMER_COUNTER);
92
93                 while (get_jiffies_64() < waitjiffies)
94                         udelay(10);
95
96                 count = __raw_readl(base + TWD_TIMER_COUNTER);
97
98                 mpcore_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
99
100                 printk("%lu.%02luMHz.\n", mpcore_timer_rate / 1000000,
101                         (mpcore_timer_rate / 100000) % 100);
102         }
103
104         load = mpcore_timer_rate / HZ;
105
106         __raw_writel(load, base + TWD_TIMER_LOAD);
107         __raw_writel(0x7,  base + TWD_TIMER_CONTROL);
108
109         /*
110          * Now maneuver our local tick into the right part of the jiffy.
111          * Start by working out where within the tick our local timer
112          * interrupt should go.
113          */
114         offset = ((mpcore_timer_rate / HZ) / (NR_CPUS + 1)) * (cpu + 1);
115
116         /*
117          * gettimeoffset() will return a number of us since the last tick.
118          * Convert this number of us to a local timer tick count.
119          * Be careful of integer overflow whilst keeping maximum precision.
120          *
121          * with HZ=100 and 1MHz (fpga) ~ 1GHz processor:
122          * load = 1 ~ 10,000
123          * mpcore_timer_rate/10000 = 100 ~ 100,000
124          *
125          * so the multiply value will be less than 10^9 always.
126          */
127         load = (system_timer->offset() * (mpcore_timer_rate / 10000)) / 100;
128
129         /* Add on our offset to get the load value */
130         load = (load + offset) % (mpcore_timer_rate / HZ);
131
132         __raw_writel(load, base + TWD_TIMER_COUNTER);
133
134         /* Make sure our local interrupt controller has this enabled */
135         __raw_writel(1 << IRQ_LOCALTIMER,
136                      __io_address(REALVIEW_GIC_DIST_BASE) + GIC_DIST_ENABLE_SET);
137 }
138
139 /*
140  * take a local timer down
141  */
142 void __cpuexit local_timer_stop(unsigned int cpu)
143 {
144         __raw_writel(0, TWD_BASE(cpu) + TWD_TIMER_CONTROL);
145 }
146
147 #else   /* CONFIG_LOCAL_TIMERS */
148
149 static void dummy_timer_set_mode(enum clock_event_mode mode,
150                                  struct clock_event_device *clk)
151 {
152 }
153
154 void __cpuinit local_timer_setup(unsigned int cpu)
155 {
156         struct clock_event_device *clk = &per_cpu(local_clockevent, cpu);
157
158         clk->name               = "dummy_timer";
159         clk->features           = CLOCK_EVT_FEAT_DUMMY;
160         clk->rating             = 200;
161         clk->set_mode           = dummy_timer_set_mode;
162         clk->broadcast          = smp_timer_broadcast;
163         clk->cpumask            = cpumask_of_cpu(cpu);
164
165         clockevents_register_device(clk);
166 }
167
168 #endif  /* !CONFIG_LOCAL_TIMERS */