blob: dae8a66301d7d6d8d812e79552846c199fe2d17d [file] [log] [blame]
Binghua Duan02c981c2011-07-08 17:40:12 +08001/*
2 * System timer for CSR SiRFprimaII
3 *
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
5 *
6 * Licensed under GPLv2 or later.
7 */
8
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>
18#include <linux/of.h>
Arnd Bergmann67d71342013-03-19 15:31:08 +010019#include <linux/of_irq.h>
Binghua Duan02c981c2011-07-08 17:40:12 +080020#include <linux/of_address.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070021#include <linux/sched_clock.h>
Binghua Duan02c981c2011-07-08 17:40:12 +080022#include <asm/mach/time.h>
23
Uwe Kleine-König980c51a2013-11-11 21:06:11 +010024#define PRIMA2_CLOCK_FREQ 1000000
25
Binghua Duan02c981c2011-07-08 17:40:12 +080026#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
41
42#define SIRFSOC_TIMER_WDT_INDEX 5
43
44#define SIRFSOC_TIMER_LATCH_BIT BIT(0)
45
Barry Songe5598a82011-09-21 20:56:33 +080046#define SIRFSOC_TIMER_REG_CNT 11
47
48static 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,
53};
54
55static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
56
Binghua Duan02c981c2011-07-08 17:40:12 +080057static void __iomem *sirfsoc_timer_base;
Binghua Duan02c981c2011-07-08 17:40:12 +080058
59/* timer0 interrupt handler */
60static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
61{
62 struct clock_event_device *ce = dev_id;
63
Bin Shi4c1ad702014-05-06 22:42:29 +080064 WARN_ON(!(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_STATUS) &
65 BIT(0)));
Binghua Duan02c981c2011-07-08 17:40:12 +080066
67 /* clear timer0 interrupt */
68 writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
69
70 ce->event_handler(ce);
71
72 return IRQ_HANDLED;
73}
74
75/* read 64-bit timer counter */
Jisheng Zhangcdc68ec2015-10-20 16:02:37 +080076static cycle_t notrace sirfsoc_timer_read(struct clocksource *cs)
Binghua Duan02c981c2011-07-08 17:40:12 +080077{
78 u64 cycles;
79
80 /* latch the 64-bit timer counter */
Bin Shi4c1ad702014-05-06 22:42:29 +080081 writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
82 sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
Binghua Duan02c981c2011-07-08 17:40:12 +080083 cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_HI);
Bin Shi4c1ad702014-05-06 22:42:29 +080084 cycles = (cycles << 32) |
85 readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
Binghua Duan02c981c2011-07-08 17:40:12 +080086
87 return cycles;
88}
89
90static int sirfsoc_timer_set_next_event(unsigned long delta,
91 struct clock_event_device *ce)
92{
93 unsigned long now, next;
94
Bin Shi4c1ad702014-05-06 22:42:29 +080095 writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
96 sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
Binghua Duan02c981c2011-07-08 17:40:12 +080097 now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
98 next = now + delta;
99 writel_relaxed(next, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0);
Bin Shi4c1ad702014-05-06 22:42:29 +0800100 writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
101 sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
Binghua Duan02c981c2011-07-08 17:40:12 +0800102 now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO);
103
104 return next - now > delta ? -ETIME : 0;
105}
106
Viresh Kumar53cba062015-06-18 16:24:49 +0530107static int sirfsoc_timer_shutdown(struct clock_event_device *evt)
Binghua Duan02c981c2011-07-08 17:40:12 +0800108{
109 u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
Viresh Kumar53cba062015-06-18 16:24:49 +0530110
111 writel_relaxed(val & ~BIT(0),
112 sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
113 return 0;
114}
115
116static int sirfsoc_timer_set_oneshot(struct clock_event_device *evt)
117{
118 u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
119
120 writel_relaxed(val | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN);
121 return 0;
Binghua Duan02c981c2011-07-08 17:40:12 +0800122}
123
Barry Songe5598a82011-09-21 20:56:33 +0800124static void sirfsoc_clocksource_suspend(struct clocksource *cs)
125{
126 int i;
127
Bin Shi4c1ad702014-05-06 22:42:29 +0800128 writel_relaxed(SIRFSOC_TIMER_LATCH_BIT,
129 sirfsoc_timer_base + SIRFSOC_TIMER_LATCH);
Barry Songe5598a82011-09-21 20:56:33 +0800130
131 for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
Bin Shi4c1ad702014-05-06 22:42:29 +0800132 sirfsoc_timer_reg_val[i] =
133 readl_relaxed(sirfsoc_timer_base +
134 sirfsoc_timer_reg_list[i]);
Barry Songe5598a82011-09-21 20:56:33 +0800135}
136
137static void sirfsoc_clocksource_resume(struct clocksource *cs)
138{
139 int i;
140
Barry Songdebeaf62012-07-30 13:29:30 +0800141 for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++)
Bin Shi4c1ad702014-05-06 22:42:29 +0800142 writel_relaxed(sirfsoc_timer_reg_val[i],
143 sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
Barry Songe5598a82011-09-21 20:56:33 +0800144
Bin Shi4c1ad702014-05-06 22:42:29 +0800145 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);
Barry Songe5598a82011-09-21 20:56:33 +0800149}
150
Binghua Duan02c981c2011-07-08 17:40:12 +0800151static struct clock_event_device sirfsoc_clockevent = {
152 .name = "sirfsoc_clockevent",
153 .rating = 200,
154 .features = CLOCK_EVT_FEAT_ONESHOT,
Viresh Kumar53cba062015-06-18 16:24:49 +0530155 .set_state_shutdown = sirfsoc_timer_shutdown,
156 .set_state_oneshot = sirfsoc_timer_set_oneshot,
Binghua Duan02c981c2011-07-08 17:40:12 +0800157 .set_next_event = sirfsoc_timer_set_next_event,
158};
159
160static struct clocksource sirfsoc_clocksource = {
161 .name = "sirfsoc_clocksource",
162 .rating = 200,
163 .mask = CLOCKSOURCE_MASK(64),
164 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
165 .read = sirfsoc_timer_read,
Barry Songe5598a82011-09-21 20:56:33 +0800166 .suspend = sirfsoc_clocksource_suspend,
167 .resume = sirfsoc_clocksource_resume,
Binghua Duan02c981c2011-07-08 17:40:12 +0800168};
169
170static struct irqaction sirfsoc_timer_irq = {
171 .name = "sirfsoc_timer0",
172 .flags = IRQF_TIMER,
173 .irq = 0,
174 .handler = sirfsoc_timer_interrupt,
175 .dev_id = &sirfsoc_clockevent,
176};
177
178/* Overwrite weak default sched_clock with more precise one */
Stephen Boyd130e6b252013-07-18 16:21:28 -0700179static u64 notrace sirfsoc_read_sched_clock(void)
Binghua Duan02c981c2011-07-08 17:40:12 +0800180{
Stephen Boyd130e6b252013-07-18 16:21:28 -0700181 return sirfsoc_timer_read(NULL);
Binghua Duan02c981c2011-07-08 17:40:12 +0800182}
183
184static void __init sirfsoc_clockevent_init(void)
185{
Binghua Duan02c981c2011-07-08 17:40:12 +0800186 sirfsoc_clockevent.cpumask = cpumask_of(0);
Uwe Kleine-König980c51a2013-11-11 21:06:11 +0100187 clockevents_config_and_register(&sirfsoc_clockevent, PRIMA2_CLOCK_FREQ,
Shawn Guo838a2ae2013-01-12 11:50:05 +0000188 2, -2);
Binghua Duan02c981c2011-07-08 17:40:12 +0800189}
190
191/* initialize the kernel jiffy timer source */
Daniel Lezcanode234842016-06-06 23:02:59 +0200192static int __init sirfsoc_prima2_timer_init(struct device_node *np)
Binghua Duan02c981c2011-07-08 17:40:12 +0800193{
194 unsigned long rate;
Binghua Duan198678b2012-08-20 06:42:36 +0000195 struct clk *clk;
Daniel Lezcanode234842016-06-06 23:02:59 +0200196 int ret;
Binghua Duan198678b2012-08-20 06:42:36 +0000197
Zhiwu Songc7cff542014-05-05 19:30:04 +0800198 clk = of_clk_get(np, 0);
Daniel Lezcanode234842016-06-06 23:02:59 +0200199 if (IS_ERR(clk)) {
200 pr_err("Failed to get clock");
201 return PTR_ERR(clk);
202 }
Zhiwu Song38941522014-07-03 20:52:51 +0800203
Daniel Lezcanode234842016-06-06 23:02:59 +0200204 ret = clk_prepare_enable(clk);
205 if (ret) {
206 pr_err("Failed to enable clock");
207 return ret;
208 }
Zhiwu Song38941522014-07-03 20:52:51 +0800209
Binghua Duan02c981c2011-07-08 17:40:12 +0800210 rate = clk_get_rate(clk);
211
Daniel Lezcanode234842016-06-06 23:02:59 +0200212 if (rate < PRIMA2_CLOCK_FREQ || rate % PRIMA2_CLOCK_FREQ) {
213 pr_err("Invalid clock rate");
214 return -EINVAL;
215 }
Binghua Duan02c981c2011-07-08 17:40:12 +0800216
Arnd Bergmann275786b2013-03-19 15:27:22 +0100217 sirfsoc_timer_base = of_iomap(np, 0);
Daniel Lezcanode234842016-06-06 23:02:59 +0200218 if (!sirfsoc_timer_base) {
219 pr_err("unable to map timer cpu registers\n");
220 return -ENXIO;
221 }
Arnd Bergmann275786b2013-03-19 15:27:22 +0100222
223 sirfsoc_timer_irq.irq = irq_of_parse_and_map(np, 0);
Marc Zyngierbc8d8492012-01-16 11:44:12 +0000224
Uwe Kleine-König980c51a2013-11-11 21:06:11 +0100225 writel_relaxed(rate / PRIMA2_CLOCK_FREQ / 2 - 1,
Bin Shi4c1ad702014-05-06 22:42:29 +0800226 sirfsoc_timer_base + SIRFSOC_TIMER_DIV);
Binghua Duan02c981c2011-07-08 17:40:12 +0800227 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO);
228 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI);
229 writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS);
230
Daniel Lezcanode234842016-06-06 23:02:59 +0200231 ret = clocksource_register_hz(&sirfsoc_clocksource, PRIMA2_CLOCK_FREQ);
232 if (ret) {
233 pr_err("Failed to register clocksource");
234 return ret;
235 }
Binghua Duan02c981c2011-07-08 17:40:12 +0800236
Uwe Kleine-König980c51a2013-11-11 21:06:11 +0100237 sched_clock_register(sirfsoc_read_sched_clock, 64, PRIMA2_CLOCK_FREQ);
Marc Zyngierbc8d8492012-01-16 11:44:12 +0000238
Daniel Lezcanode234842016-06-06 23:02:59 +0200239 ret = setup_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq);
240 if (ret) {
241 pr_err("Failed to setup irq");
242 return ret;
243 }
Binghua Duan02c981c2011-07-08 17:40:12 +0800244
245 sirfsoc_clockevent_init();
Daniel Lezcanode234842016-06-06 23:02:59 +0200246
247 return 0;
Binghua Duan02c981c2011-07-08 17:40:12 +0800248}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200249CLOCKSOURCE_OF_DECLARE(sirfsoc_prima2_timer,
Bin Shi4c1ad702014-05-06 22:42:29 +0800250 "sirf,prima2-tick", sirfsoc_prima2_timer_init);