2 * linux/arch/arm/kernel/smp_twd.c
4 * Copyright (C) 2002 ARM Ltd.
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.
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/smp.h>
18 #include <linux/jiffies.h>
19 #include <linux/clockchips.h>
20 #include <linux/irq.h>
23 #include <asm/smp_twd.h>
24 #include <asm/localtimer.h>
25 #include <asm/hardware/gic.h>
27 /* set up by the platform code */
28 void __iomem
*twd_base
;
30 static struct clk
*twd_clk
;
31 static unsigned long twd_timer_rate
;
33 static struct clock_event_device __percpu
**twd_evt
;
35 static void twd_set_mode(enum clock_event_mode mode
,
36 struct clock_event_device
*clk
)
41 case CLOCK_EVT_MODE_PERIODIC
:
42 /* timer load already set up */
43 ctrl
= TWD_TIMER_CONTROL_ENABLE
| TWD_TIMER_CONTROL_IT_ENABLE
44 | TWD_TIMER_CONTROL_PERIODIC
;
45 __raw_writel(twd_timer_rate
/ HZ
, twd_base
+ TWD_TIMER_LOAD
);
47 case CLOCK_EVT_MODE_ONESHOT
:
48 /* period set, and timer enabled in 'next_event' hook */
49 ctrl
= TWD_TIMER_CONTROL_IT_ENABLE
| TWD_TIMER_CONTROL_ONESHOT
;
51 case CLOCK_EVT_MODE_UNUSED
:
52 case CLOCK_EVT_MODE_SHUTDOWN
:
57 __raw_writel(ctrl
, twd_base
+ TWD_TIMER_CONTROL
);
60 static int twd_set_next_event(unsigned long evt
,
61 struct clock_event_device
*unused
)
63 unsigned long ctrl
= __raw_readl(twd_base
+ TWD_TIMER_CONTROL
);
65 ctrl
|= TWD_TIMER_CONTROL_ENABLE
;
67 __raw_writel(evt
, twd_base
+ TWD_TIMER_COUNTER
);
68 __raw_writel(ctrl
, twd_base
+ TWD_TIMER_CONTROL
);
74 * local_timer_ack: checks for a local timer interrupt.
76 * If a local timer interrupt has occurred, acknowledge and return 1.
77 * Otherwise, return 0.
79 int twd_timer_ack(void)
81 if (__raw_readl(twd_base
+ TWD_TIMER_INTSTAT
)) {
82 __raw_writel(1, twd_base
+ TWD_TIMER_INTSTAT
);
89 void twd_timer_stop(struct clock_event_device
*clk
)
91 twd_set_mode(CLOCK_EVT_MODE_UNUSED
, clk
);
92 disable_percpu_irq(clk
->irq
);
95 static void __cpuinit
twd_calibrate_rate(void)
101 * If this is the first time round, we need to work out how fast
104 if (twd_timer_rate
== 0) {
105 printk(KERN_INFO
"Calibrating local timer... ");
107 /* Wait for a tick to start */
108 waitjiffies
= get_jiffies_64() + 1;
110 while (get_jiffies_64() < waitjiffies
)
113 /* OK, now the tick has started, let's get the timer going */
116 /* enable, no interrupt or reload */
117 __raw_writel(0x1, twd_base
+ TWD_TIMER_CONTROL
);
120 __raw_writel(0xFFFFFFFFU
, twd_base
+ TWD_TIMER_COUNTER
);
122 while (get_jiffies_64() < waitjiffies
)
125 count
= __raw_readl(twd_base
+ TWD_TIMER_COUNTER
);
127 twd_timer_rate
= (0xFFFFFFFFU
- count
) * (HZ
/ 5);
129 printk("%lu.%02luMHz.\n", twd_timer_rate
/ 1000000,
130 (twd_timer_rate
/ 10000) % 100);
134 static irqreturn_t
twd_handler(int irq
, void *dev_id
)
136 struct clock_event_device
*evt
= *(struct clock_event_device
**)dev_id
;
138 if (twd_timer_ack()) {
139 evt
->event_handler(evt
);
146 static struct clk
*twd_get_clock(void)
151 clk
= clk_get_sys("smp_twd", NULL
);
153 pr_err("smp_twd: clock not found: %d\n", (int)PTR_ERR(clk
));
157 err
= clk_prepare(clk
);
159 pr_err("smp_twd: clock failed to prepare: %d\n", err
);
164 err
= clk_enable(clk
);
166 pr_err("smp_twd: clock failed to enable: %d\n", err
);
176 * Setup the local clock events for a CPU.
178 void __cpuinit
twd_timer_setup(struct clock_event_device
*clk
)
180 struct clock_event_device
**this_cpu_clk
;
185 twd_evt
= alloc_percpu(struct clock_event_device
*);
187 pr_err("twd: can't allocate memory\n");
191 err
= request_percpu_irq(clk
->irq
, twd_handler
,
194 pr_err("twd: can't register interrupt %d (%d)\n",
201 twd_clk
= twd_get_clock();
203 if (!IS_ERR_OR_NULL(twd_clk
))
204 twd_timer_rate
= clk_get_rate(twd_clk
);
206 twd_calibrate_rate();
208 clk
->name
= "local_timer";
209 clk
->features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
|
210 CLOCK_EVT_FEAT_C3STOP
;
212 clk
->set_mode
= twd_set_mode
;
213 clk
->set_next_event
= twd_set_next_event
;
215 this_cpu_clk
= __this_cpu_ptr(twd_evt
);
218 clockevents_config_and_register(clk
, twd_timer_rate
,
220 enable_percpu_irq(clk
->irq
, 0);