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/bitops.h>
14 #include <linux/irq.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_address.h>
21 #include <linux/sched_clock.h>
22 #include <asm/mach/time.h>
24 #define PRIMA2_CLOCK_FREQ 1000000
26 #define SIRFSOC_TIMER_COUNTER_LO 0x0000
27 #define SIRFSOC_TIMER_COUNTER_HI 0x0004
28 #define SIRFSOC_TIMER_MATCH_0 0x0008
29 #define SIRFSOC_TIMER_MATCH_1 0x000C
30 #define SIRFSOC_TIMER_MATCH_2 0x0010
31 #define SIRFSOC_TIMER_MATCH_3 0x0014
32 #define SIRFSOC_TIMER_MATCH_4 0x0018
33 #define SIRFSOC_TIMER_MATCH_5 0x001C
34 #define SIRFSOC_TIMER_STATUS 0x0020
35 #define SIRFSOC_TIMER_INT_EN 0x0024
36 #define SIRFSOC_TIMER_WATCHDOG_EN 0x0028
37 #define SIRFSOC_TIMER_DIV 0x002C
38 #define SIRFSOC_TIMER_LATCH 0x0030
39 #define SIRFSOC_TIMER_LATCHED_LO 0x0034
40 #define SIRFSOC_TIMER_LATCHED_HI 0x0038
42 #define SIRFSOC_TIMER_WDT_INDEX 5
44 #define SIRFSOC_TIMER_LATCH_BIT BIT(0)
46 #define SIRFSOC_TIMER_REG_CNT 11
48 static const u32 sirfsoc_timer_reg_list
[SIRFSOC_TIMER_REG_CNT
] = {
49 SIRFSOC_TIMER_MATCH_0
, SIRFSOC_TIMER_MATCH_1
, SIRFSOC_TIMER_MATCH_2
,
50 SIRFSOC_TIMER_MATCH_3
, SIRFSOC_TIMER_MATCH_4
, SIRFSOC_TIMER_MATCH_5
,
51 SIRFSOC_TIMER_INT_EN
, SIRFSOC_TIMER_WATCHDOG_EN
, SIRFSOC_TIMER_DIV
,
52 SIRFSOC_TIMER_LATCHED_LO
, SIRFSOC_TIMER_LATCHED_HI
,
55 static u32 sirfsoc_timer_reg_val
[SIRFSOC_TIMER_REG_CNT
];
57 static void __iomem
*sirfsoc_timer_base
;
59 /* timer0 interrupt handler */
60 static irqreturn_t
sirfsoc_timer_interrupt(int irq
, void *dev_id
)
62 struct clock_event_device
*ce
= dev_id
;
64 WARN_ON(!(readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_STATUS
) &
67 /* clear timer0 interrupt */
68 writel_relaxed(BIT(0), sirfsoc_timer_base
+ SIRFSOC_TIMER_STATUS
);
70 ce
->event_handler(ce
);
75 /* read 64-bit timer counter */
76 static cycle_t notrace
sirfsoc_timer_read(struct clocksource
*cs
)
80 /* latch the 64-bit timer counter */
81 writel_relaxed(SIRFSOC_TIMER_LATCH_BIT
,
82 sirfsoc_timer_base
+ SIRFSOC_TIMER_LATCH
);
83 cycles
= readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_LATCHED_HI
);
84 cycles
= (cycles
<< 32) |
85 readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_LATCHED_LO
);
90 static int sirfsoc_timer_set_next_event(unsigned long delta
,
91 struct clock_event_device
*ce
)
93 unsigned long now
, next
;
95 writel_relaxed(SIRFSOC_TIMER_LATCH_BIT
,
96 sirfsoc_timer_base
+ SIRFSOC_TIMER_LATCH
);
97 now
= readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_LATCHED_LO
);
99 writel_relaxed(next
, sirfsoc_timer_base
+ SIRFSOC_TIMER_MATCH_0
);
100 writel_relaxed(SIRFSOC_TIMER_LATCH_BIT
,
101 sirfsoc_timer_base
+ SIRFSOC_TIMER_LATCH
);
102 now
= readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_LATCHED_LO
);
104 return next
- now
> delta
? -ETIME
: 0;
107 static int sirfsoc_timer_shutdown(struct clock_event_device
*evt
)
109 u32 val
= readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_INT_EN
);
111 writel_relaxed(val
& ~BIT(0),
112 sirfsoc_timer_base
+ SIRFSOC_TIMER_INT_EN
);
116 static int sirfsoc_timer_set_oneshot(struct clock_event_device
*evt
)
118 u32 val
= readl_relaxed(sirfsoc_timer_base
+ SIRFSOC_TIMER_INT_EN
);
120 writel_relaxed(val
| BIT(0), sirfsoc_timer_base
+ SIRFSOC_TIMER_INT_EN
);
124 static void sirfsoc_clocksource_suspend(struct clocksource
*cs
)
128 writel_relaxed(SIRFSOC_TIMER_LATCH_BIT
,
129 sirfsoc_timer_base
+ SIRFSOC_TIMER_LATCH
);
131 for (i
= 0; i
< SIRFSOC_TIMER_REG_CNT
; i
++)
132 sirfsoc_timer_reg_val
[i
] =
133 readl_relaxed(sirfsoc_timer_base
+
134 sirfsoc_timer_reg_list
[i
]);
137 static void sirfsoc_clocksource_resume(struct clocksource
*cs
)
141 for (i
= 0; i
< SIRFSOC_TIMER_REG_CNT
- 2; i
++)
142 writel_relaxed(sirfsoc_timer_reg_val
[i
],
143 sirfsoc_timer_base
+ sirfsoc_timer_reg_list
[i
]);
145 writel_relaxed(sirfsoc_timer_reg_val
[SIRFSOC_TIMER_REG_CNT
- 2],
146 sirfsoc_timer_base
+ SIRFSOC_TIMER_COUNTER_LO
);
147 writel_relaxed(sirfsoc_timer_reg_val
[SIRFSOC_TIMER_REG_CNT
- 1],
148 sirfsoc_timer_base
+ SIRFSOC_TIMER_COUNTER_HI
);
151 static struct clock_event_device sirfsoc_clockevent
= {
152 .name
= "sirfsoc_clockevent",
154 .features
= CLOCK_EVT_FEAT_ONESHOT
,
155 .set_state_shutdown
= sirfsoc_timer_shutdown
,
156 .set_state_oneshot
= sirfsoc_timer_set_oneshot
,
157 .set_next_event
= sirfsoc_timer_set_next_event
,
160 static struct clocksource sirfsoc_clocksource
= {
161 .name
= "sirfsoc_clocksource",
163 .mask
= CLOCKSOURCE_MASK(64),
164 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
165 .read
= sirfsoc_timer_read
,
166 .suspend
= sirfsoc_clocksource_suspend
,
167 .resume
= sirfsoc_clocksource_resume
,
170 static struct irqaction sirfsoc_timer_irq
= {
171 .name
= "sirfsoc_timer0",
174 .handler
= sirfsoc_timer_interrupt
,
175 .dev_id
= &sirfsoc_clockevent
,
178 /* Overwrite weak default sched_clock with more precise one */
179 static u64 notrace
sirfsoc_read_sched_clock(void)
181 return sirfsoc_timer_read(NULL
);
184 static void __init
sirfsoc_clockevent_init(void)
186 sirfsoc_clockevent
.cpumask
= cpumask_of(0);
187 clockevents_config_and_register(&sirfsoc_clockevent
, PRIMA2_CLOCK_FREQ
,
191 /* initialize the kernel jiffy timer source */
192 static void __init
sirfsoc_prima2_timer_init(struct device_node
*np
)
197 clk
= of_clk_get(np
, 0);
200 BUG_ON(clk_prepare_enable(clk
));
202 rate
= clk_get_rate(clk
);
204 BUG_ON(rate
< PRIMA2_CLOCK_FREQ
);
205 BUG_ON(rate
% PRIMA2_CLOCK_FREQ
);
207 sirfsoc_timer_base
= of_iomap(np
, 0);
208 if (!sirfsoc_timer_base
)
209 panic("unable to map timer cpu registers\n");
211 sirfsoc_timer_irq
.irq
= irq_of_parse_and_map(np
, 0);
213 writel_relaxed(rate
/ PRIMA2_CLOCK_FREQ
/ 2 - 1,
214 sirfsoc_timer_base
+ SIRFSOC_TIMER_DIV
);
215 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_COUNTER_LO
);
216 writel_relaxed(0, sirfsoc_timer_base
+ SIRFSOC_TIMER_COUNTER_HI
);
217 writel_relaxed(BIT(0), sirfsoc_timer_base
+ SIRFSOC_TIMER_STATUS
);
219 BUG_ON(clocksource_register_hz(&sirfsoc_clocksource
,
222 sched_clock_register(sirfsoc_read_sched_clock
, 64, PRIMA2_CLOCK_FREQ
);
224 BUG_ON(setup_irq(sirfsoc_timer_irq
.irq
, &sirfsoc_timer_irq
));
226 sirfsoc_clockevent_init();
228 CLOCKSOURCE_OF_DECLARE(sirfsoc_prima2_timer
,
229 "sirf,prima2-tick", sirfsoc_prima2_timer_init
);