]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-realview/localtimer.c
4f87b4d09c7d207b1649f6e7913d7b28b14a06da
[linux-2.6-omap-h63xx.git] / 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 #include <linux/irq.h>
20
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 static void local_timer_set_mode(enum clock_event_mode mode,
47                                  struct clock_event_device *clk)
48 {
49         void __iomem *base = TWD_BASE(smp_processor_id());
50         unsigned long ctrl;
51
52         switch(mode) {
53         case CLOCK_EVT_MODE_PERIODIC:
54                 /* timer load already set up */
55                 ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
56                         | TWD_TIMER_CONTROL_PERIODIC;
57                 break;
58         case CLOCK_EVT_MODE_ONESHOT:
59                 /* period set, and timer enabled in 'next_event' hook */
60                 ctrl = TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT;
61                 break;
62         case CLOCK_EVT_MODE_UNUSED:
63         case CLOCK_EVT_MODE_SHUTDOWN:
64         default:
65                 ctrl = 0;
66         }
67
68         __raw_writel(ctrl, base + TWD_TIMER_CONTROL);
69 }
70
71 static int local_timer_set_next_event(unsigned long evt,
72                                       struct clock_event_device *unused)
73 {
74         void __iomem *base = TWD_BASE(smp_processor_id());
75         unsigned long ctrl = __raw_readl(base + TWD_TIMER_CONTROL);
76
77         __raw_writel(evt, base + TWD_TIMER_COUNTER);
78         __raw_writel(ctrl | TWD_TIMER_CONTROL_ENABLE, base + TWD_TIMER_CONTROL);
79
80         return 0;
81 }
82
83 /*
84  * local_timer_ack: checks for a local timer interrupt.
85  *
86  * If a local timer interrupt has occurred, acknowledge and return 1.
87  * Otherwise, return 0.
88  */
89 int local_timer_ack(void)
90 {
91         void __iomem *base = TWD_BASE(smp_processor_id());
92
93         if (__raw_readl(base + TWD_TIMER_INTSTAT)) {
94                 __raw_writel(1, base + TWD_TIMER_INTSTAT);
95                 return 1;
96         }
97
98         return 0;
99 }
100
101 static void __cpuinit twd_calibrate_rate(unsigned int cpu)
102 {
103         void __iomem *base = TWD_BASE(cpu);
104         unsigned long load, count;
105         u64 waitjiffies;
106
107         /*
108          * If this is the first time round, we need to work out how fast
109          * the timer ticks
110          */
111         if (mpcore_timer_rate == 0) {
112                 printk("Calibrating local timer... ");
113
114                 /* Wait for a tick to start */
115                 waitjiffies = get_jiffies_64() + 1;
116
117                 while (get_jiffies_64() < waitjiffies)
118                         udelay(10);
119
120                 /* OK, now the tick has started, let's get the timer going */
121                 waitjiffies += 5;
122
123                                  /* enable, no interrupt or reload */
124                 __raw_writel(0x1, base + TWD_TIMER_CONTROL);
125
126                                  /* maximum value */
127                 __raw_writel(0xFFFFFFFFU, base + TWD_TIMER_COUNTER);
128
129                 while (get_jiffies_64() < waitjiffies)
130                         udelay(10);
131
132                 count = __raw_readl(base + TWD_TIMER_COUNTER);
133
134                 mpcore_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
135
136                 printk("%lu.%02luMHz.\n", mpcore_timer_rate / 1000000,
137                         (mpcore_timer_rate / 100000) % 100);
138         }
139
140         load = mpcore_timer_rate / HZ;
141
142         __raw_writel(load, base + TWD_TIMER_LOAD);
143 }
144
145 /*
146  * Setup the local clock events for a CPU.
147  */
148 void __cpuinit local_timer_setup(unsigned int cpu)
149 {
150         struct clock_event_device *clk = &per_cpu(local_clockevent, cpu);
151         unsigned long flags;
152
153         twd_calibrate_rate(cpu);
154
155         clk->name               = "local_timer";
156         clk->features           = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
157         clk->rating             = 350;
158         clk->set_mode           = local_timer_set_mode;
159         clk->set_next_event     = local_timer_set_next_event;
160         clk->irq                = IRQ_LOCALTIMER;
161         clk->cpumask            = cpumask_of_cpu(cpu);
162         clk->shift              = 20;
163         clk->mult               = div_sc(mpcore_timer_rate, NSEC_PER_SEC, clk->shift);
164         clk->max_delta_ns       = clockevent_delta2ns(0xffffffff, clk);
165         clk->min_delta_ns       = clockevent_delta2ns(0xf, clk);
166
167         /* Make sure our local interrupt controller has this enabled */
168         local_irq_save(flags);
169         get_irq_chip(IRQ_LOCALTIMER)->unmask(IRQ_LOCALTIMER);
170         local_irq_restore(flags);
171
172         clockevents_register_device(clk);
173 }
174
175 /*
176  * take a local timer down
177  */
178 void __cpuexit local_timer_stop(unsigned int cpu)
179 {
180         __raw_writel(0, TWD_BASE(cpu) + TWD_TIMER_CONTROL);
181 }
182
183 #else   /* CONFIG_LOCAL_TIMERS */
184
185 static void dummy_timer_set_mode(enum clock_event_mode mode,
186                                  struct clock_event_device *clk)
187 {
188 }
189
190 void __cpuinit local_timer_setup(unsigned int cpu)
191 {
192         struct clock_event_device *clk = &per_cpu(local_clockevent, cpu);
193
194         clk->name               = "dummy_timer";
195         clk->features           = CLOCK_EVT_FEAT_DUMMY;
196         clk->rating             = 200;
197         clk->set_mode           = dummy_timer_set_mode;
198         clk->broadcast          = smp_timer_broadcast;
199         clk->cpumask            = cpumask_of_cpu(cpu);
200
201         clockevents_register_device(clk);
202 }
203
204 #endif  /* !CONFIG_LOCAL_TIMERS */