2 * System timer for CSR SiRFprimaII
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
6 * Licensed under GPLv2 or later.
9 #include <linux/kernel.h>
10 #include <linux/interrupt.h>
11 #include <linux/clockchips.h>
12 #include <linux/clocksource.h>
13 #include <linux/cpu.h>
14 #include <linux/bitops.h>
15 #include <linux/irq.h>
16 #include <linux/clk.h>
17 #include <linux/slab.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_address.h>
21 #include <linux/sched_clock.h>
23 #define SIRFSOC_TIMER_32COUNTER_0_CTRL 0x0000
24 #define SIRFSOC_TIMER_32COUNTER_1_CTRL 0x0004
25 #define SIRFSOC_TIMER_MATCH_0 0x0018
26 #define SIRFSOC_TIMER_MATCH_1 0x001c
27 #define SIRFSOC_TIMER_COUNTER_0 0x0048
28 #define SIRFSOC_TIMER_COUNTER_1 0x004c
29 #define SIRFSOC_TIMER_INTR_STATUS 0x0060
30 #define SIRFSOC_TIMER_WATCHDOG_EN 0x0064
31 #define SIRFSOC_TIMER_64COUNTER_CTRL 0x0068
32 #define SIRFSOC_TIMER_64COUNTER_LO 0x006c
33 #define SIRFSOC_TIMER_64COUNTER_HI 0x0070
34 #define SIRFSOC_TIMER_64COUNTER_LOAD_LO 0x0074
35 #define SIRFSOC_TIMER_64COUNTER_LOAD_HI 0x0078
36 #define SIRFSOC_TIMER_64COUNTER_RLATCHED_LO 0x007c
37 #define SIRFSOC_TIMER_64COUNTER_RLATCHED_HI 0x0080
39 #define SIRFSOC_TIMER_REG_CNT 6
41 static unsigned long atlas7_timer_rate
;
43 static const u32 sirfsoc_timer_reg_list
[SIRFSOC_TIMER_REG_CNT
] = {
44 SIRFSOC_TIMER_WATCHDOG_EN
,
45 SIRFSOC_TIMER_32COUNTER_0_CTRL
,
46 SIRFSOC_TIMER_32COUNTER_1_CTRL
,
47 SIRFSOC_TIMER_64COUNTER_CTRL
,
48 SIRFSOC_TIMER_64COUNTER_RLATCHED_LO
,
49 SIRFSOC_TIMER_64COUNTER_RLATCHED_HI
,
52 static u32 sirfsoc_timer_reg_val
[SIRFSOC_TIMER_REG_CNT
];
54 static void __iomem
*sirfsoc_timer_base
;
56 /* disable count and interrupt */
57 static inline void sirfsoc_timer_count_disable(int idx
)
59 writel_relaxed(readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_32COUNTER_0_CTRL
+ 4 * idx
) & ~0x7,
60 sirfsoc_timer_base
+ SIRFSOC_TIMER_32COUNTER_0_CTRL
+ 4 * idx
);
63 /* enable count and interrupt */
64 static inline void sirfsoc_timer_count_enable(int idx
)
66 writel_relaxed(readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_32COUNTER_0_CTRL
+ 4 * idx
) | 0x3,
67 sirfsoc_timer_base
+ SIRFSOC_TIMER_32COUNTER_0_CTRL
+ 4 * idx
);
70 /* timer interrupt handler */
71 static irqreturn_t
sirfsoc_timer_interrupt(int irq
, void *dev_id
)
73 struct clock_event_device
*ce
= dev_id
;
74 int cpu
= smp_processor_id();
76 /* clear timer interrupt */
77 writel_relaxed(BIT(cpu
), sirfsoc_timer_base
+ SIRFSOC_TIMER_INTR_STATUS
);
79 if (clockevent_state_oneshot(ce
))
80 sirfsoc_timer_count_disable(cpu
);
82 ce
->event_handler(ce
);
87 /* read 64-bit timer counter */
88 static cycle_t
sirfsoc_timer_read(struct clocksource
*cs
)
92 writel_relaxed((readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_CTRL
) |
93 BIT(0)) & ~BIT(1), sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_CTRL
);
95 cycles
= readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_RLATCHED_HI
);
96 cycles
= (cycles
<< 32) | readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_RLATCHED_LO
);
101 static int sirfsoc_timer_set_next_event(unsigned long delta
,
102 struct clock_event_device
*ce
)
104 int cpu
= smp_processor_id();
106 /* disable timer first, then modify the related registers */
107 sirfsoc_timer_count_disable(cpu
);
109 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_COUNTER_0
+
111 writel_relaxed(delta
, sirfsoc_timer_base
+ SIRFSOC_TIMER_MATCH_0
+
114 /* enable the tick */
115 sirfsoc_timer_count_enable(cpu
);
120 /* Oneshot is enabled in set_next_event */
121 static int sirfsoc_timer_shutdown(struct clock_event_device
*evt
)
123 sirfsoc_timer_count_disable(smp_processor_id());
127 static void sirfsoc_clocksource_suspend(struct clocksource
*cs
)
131 for (i
= 0; i
< SIRFSOC_TIMER_REG_CNT
; i
++)
132 sirfsoc_timer_reg_val
[i
] = readl_relaxed(sirfsoc_timer_base
+ sirfsoc_timer_reg_list
[i
]);
135 static void sirfsoc_clocksource_resume(struct clocksource
*cs
)
139 for (i
= 0; i
< SIRFSOC_TIMER_REG_CNT
- 2; i
++)
140 writel_relaxed(sirfsoc_timer_reg_val
[i
], sirfsoc_timer_base
+ sirfsoc_timer_reg_list
[i
]);
142 writel_relaxed(sirfsoc_timer_reg_val
[SIRFSOC_TIMER_REG_CNT
- 2],
143 sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_LOAD_LO
);
144 writel_relaxed(sirfsoc_timer_reg_val
[SIRFSOC_TIMER_REG_CNT
- 1],
145 sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_LOAD_HI
);
147 writel_relaxed(readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_CTRL
) |
148 BIT(1) | BIT(0), sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_CTRL
);
151 static struct clock_event_device __percpu
*sirfsoc_clockevent
;
153 static struct clocksource sirfsoc_clocksource
= {
154 .name
= "sirfsoc_clocksource",
156 .mask
= CLOCKSOURCE_MASK(64),
157 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
158 .read
= sirfsoc_timer_read
,
159 .suspend
= sirfsoc_clocksource_suspend
,
160 .resume
= sirfsoc_clocksource_resume
,
163 static struct irqaction sirfsoc_timer_irq
= {
164 .name
= "sirfsoc_timer0",
165 .flags
= IRQF_TIMER
| IRQF_NOBALANCING
,
166 .handler
= sirfsoc_timer_interrupt
,
169 static struct irqaction sirfsoc_timer1_irq
= {
170 .name
= "sirfsoc_timer1",
171 .flags
= IRQF_TIMER
| IRQF_NOBALANCING
,
172 .handler
= sirfsoc_timer_interrupt
,
175 static int sirfsoc_local_timer_setup(struct clock_event_device
*ce
)
177 int cpu
= smp_processor_id();
178 struct irqaction
*action
;
181 action
= &sirfsoc_timer_irq
;
183 action
= &sirfsoc_timer1_irq
;
185 ce
->irq
= action
->irq
;
186 ce
->name
= "local_timer";
187 ce
->features
= CLOCK_EVT_FEAT_ONESHOT
;
189 ce
->set_state_shutdown
= sirfsoc_timer_shutdown
;
190 ce
->set_state_oneshot
= sirfsoc_timer_shutdown
;
191 ce
->tick_resume
= sirfsoc_timer_shutdown
;
192 ce
->set_next_event
= sirfsoc_timer_set_next_event
;
193 clockevents_calc_mult_shift(ce
, atlas7_timer_rate
, 60);
194 ce
->max_delta_ns
= clockevent_delta2ns(-2, ce
);
195 ce
->min_delta_ns
= clockevent_delta2ns(2, ce
);
196 ce
->cpumask
= cpumask_of(cpu
);
199 BUG_ON(setup_irq(ce
->irq
, action
));
200 irq_force_affinity(action
->irq
, cpumask_of(cpu
));
202 clockevents_register_device(ce
);
206 static void sirfsoc_local_timer_stop(struct clock_event_device
*ce
)
208 int cpu
= smp_processor_id();
210 sirfsoc_timer_count_disable(1);
213 remove_irq(sirfsoc_timer_irq
.irq
, &sirfsoc_timer_irq
);
215 remove_irq(sirfsoc_timer1_irq
.irq
, &sirfsoc_timer1_irq
);
218 static int sirfsoc_cpu_notify(struct notifier_block
*self
,
219 unsigned long action
, void *hcpu
)
222 * Grab cpu pointer in each case to avoid spurious
223 * preemptible warnings
225 switch (action
& ~CPU_TASKS_FROZEN
) {
227 sirfsoc_local_timer_setup(this_cpu_ptr(sirfsoc_clockevent
));
230 sirfsoc_local_timer_stop(this_cpu_ptr(sirfsoc_clockevent
));
237 static struct notifier_block sirfsoc_cpu_nb
= {
238 .notifier_call
= sirfsoc_cpu_notify
,
241 static void __init
sirfsoc_clockevent_init(void)
243 sirfsoc_clockevent
= alloc_percpu(struct clock_event_device
);
244 BUG_ON(!sirfsoc_clockevent
);
246 BUG_ON(register_cpu_notifier(&sirfsoc_cpu_nb
));
248 /* Immediately configure the timer on the boot CPU */
249 sirfsoc_local_timer_setup(this_cpu_ptr(sirfsoc_clockevent
));
252 /* initialize the kernel jiffy timer source */
253 static void __init
sirfsoc_atlas7_timer_init(struct device_node
*np
)
257 clk
= of_clk_get(np
, 0);
260 BUG_ON(clk_prepare_enable(clk
));
262 atlas7_timer_rate
= clk_get_rate(clk
);
264 /* timer dividers: 0, not divided */
265 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_CTRL
);
266 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_32COUNTER_0_CTRL
);
267 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_32COUNTER_1_CTRL
);
269 /* Initialize timer counters to 0 */
270 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_LOAD_LO
);
271 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_LOAD_HI
);
272 writel_relaxed(readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_CTRL
) |
273 BIT(1) | BIT(0), sirfsoc_timer_base
+ SIRFSOC_TIMER_64COUNTER_CTRL
);
274 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_COUNTER_0
);
275 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_COUNTER_1
);
277 /* Clear all interrupts */
278 writel_relaxed(0xFFFF, sirfsoc_timer_base
+ SIRFSOC_TIMER_INTR_STATUS
);
280 BUG_ON(clocksource_register_hz(&sirfsoc_clocksource
, atlas7_timer_rate
));
282 sirfsoc_clockevent_init();
285 static void __init
sirfsoc_of_timer_init(struct device_node
*np
)
287 sirfsoc_timer_base
= of_iomap(np
, 0);
288 if (!sirfsoc_timer_base
)
289 panic("unable to map timer cpu registers\n");
291 sirfsoc_timer_irq
.irq
= irq_of_parse_and_map(np
, 0);
292 if (!sirfsoc_timer_irq
.irq
)
293 panic("No irq passed for timer0 via DT\n");
295 sirfsoc_timer1_irq
.irq
= irq_of_parse_and_map(np
, 1);
296 if (!sirfsoc_timer1_irq
.irq
)
297 panic("No irq passed for timer1 via DT\n");
299 sirfsoc_atlas7_timer_init(np
);
301 CLOCKSOURCE_OF_DECLARE(sirfsoc_atlas7_timer
, "sirf,atlas7-tick", sirfsoc_of_timer_init
);