blob: f3efef0c5dbc1bee14af9c967003ef8952842a63 [file] [log] [blame]
Linus Walleijbb3cee22009-04-23 10:22:13 +01001/*
2 *
3 * arch/arm/mach-u300/timer.c
4 *
5 *
6 * Copyright (C) 2007-2009 ST-Ericsson AB
7 * License terms: GNU General Public License (GPL) version 2
8 * Timer COH 901 328, runs the OS timer interrupt.
9 * Author: Linus Walleij <linus.walleij@stericsson.com>
10 */
11#include <linux/interrupt.h>
12#include <linux/time.h>
13#include <linux/timex.h>
14#include <linux/clockchips.h>
15#include <linux/clocksource.h>
16#include <linux/types.h>
17#include <linux/io.h>
Linus Walleijb7276b22010-08-05 07:58:58 +010018#include <linux/clk.h>
19#include <linux/err.h>
Linus Walleija4fe2922012-08-13 13:49:45 +020020#include <linux/irq.h>
Linus Walleij3c96d8e2013-04-05 22:45:11 +020021#include <linux/delay.h>
Linus Walleij5a5056c2013-04-08 10:50:11 +020022#include <linux/of_address.h>
23#include <linux/of_irq.h>
Linus Walleijbb3cee22009-04-23 10:22:13 +010024
25#include <mach/hardware.h>
Linus Walleija4fe2922012-08-13 13:49:45 +020026#include <mach/irqs.h>
Linus Walleijbb3cee22009-04-23 10:22:13 +010027
28/* Generic stuff */
Russell King5c21b7c2010-12-15 21:50:14 +000029#include <asm/sched_clock.h>
Linus Walleijbb3cee22009-04-23 10:22:13 +010030#include <asm/mach/map.h>
31#include <asm/mach/time.h>
Linus Walleijbb3cee22009-04-23 10:22:13 +010032
Linus Walleijbb3cee22009-04-23 10:22:13 +010033/*
34 * APP side special timer registers
35 * This timer contains four timers which can fire an interrupt each.
36 * OS (operating system) timer @ 32768 Hz
37 * DD (device driver) timer @ 1 kHz
38 * GP1 (general purpose 1) timer @ 1MHz
39 * GP2 (general purpose 2) timer @ 1MHz
40 */
41
42/* Reset OS Timer 32bit (-/W) */
43#define U300_TIMER_APP_ROST (0x0000)
44#define U300_TIMER_APP_ROST_TIMER_RESET (0x00000000)
45/* Enable OS Timer 32bit (-/W) */
46#define U300_TIMER_APP_EOST (0x0004)
47#define U300_TIMER_APP_EOST_TIMER_ENABLE (0x00000000)
48/* Disable OS Timer 32bit (-/W) */
49#define U300_TIMER_APP_DOST (0x0008)
50#define U300_TIMER_APP_DOST_TIMER_DISABLE (0x00000000)
51/* OS Timer Mode Register 32bit (-/W) */
52#define U300_TIMER_APP_SOSTM (0x000c)
53#define U300_TIMER_APP_SOSTM_MODE_CONTINUOUS (0x00000000)
54#define U300_TIMER_APP_SOSTM_MODE_ONE_SHOT (0x00000001)
55/* OS Timer Status Register 32bit (R/-) */
56#define U300_TIMER_APP_OSTS (0x0010)
57#define U300_TIMER_APP_OSTS_TIMER_STATE_MASK (0x0000000F)
58#define U300_TIMER_APP_OSTS_TIMER_STATE_IDLE (0x00000001)
59#define U300_TIMER_APP_OSTS_TIMER_STATE_ACTIVE (0x00000002)
60#define U300_TIMER_APP_OSTS_ENABLE_IND (0x00000010)
61#define U300_TIMER_APP_OSTS_MODE_MASK (0x00000020)
62#define U300_TIMER_APP_OSTS_MODE_CONTINUOUS (0x00000000)
63#define U300_TIMER_APP_OSTS_MODE_ONE_SHOT (0x00000020)
64#define U300_TIMER_APP_OSTS_IRQ_ENABLED_IND (0x00000040)
65#define U300_TIMER_APP_OSTS_IRQ_PENDING_IND (0x00000080)
66/* OS Timer Current Count Register 32bit (R/-) */
67#define U300_TIMER_APP_OSTCC (0x0014)
68/* OS Timer Terminal Count Register 32bit (R/W) */
69#define U300_TIMER_APP_OSTTC (0x0018)
70/* OS Timer Interrupt Enable Register 32bit (-/W) */
71#define U300_TIMER_APP_OSTIE (0x001c)
72#define U300_TIMER_APP_OSTIE_IRQ_DISABLE (0x00000000)
73#define U300_TIMER_APP_OSTIE_IRQ_ENABLE (0x00000001)
74/* OS Timer Interrupt Acknowledge Register 32bit (-/W) */
75#define U300_TIMER_APP_OSTIA (0x0020)
76#define U300_TIMER_APP_OSTIA_IRQ_ACK (0x00000080)
77
78/* Reset DD Timer 32bit (-/W) */
79#define U300_TIMER_APP_RDDT (0x0040)
80#define U300_TIMER_APP_RDDT_TIMER_RESET (0x00000000)
81/* Enable DD Timer 32bit (-/W) */
82#define U300_TIMER_APP_EDDT (0x0044)
83#define U300_TIMER_APP_EDDT_TIMER_ENABLE (0x00000000)
84/* Disable DD Timer 32bit (-/W) */
85#define U300_TIMER_APP_DDDT (0x0048)
86#define U300_TIMER_APP_DDDT_TIMER_DISABLE (0x00000000)
87/* DD Timer Mode Register 32bit (-/W) */
88#define U300_TIMER_APP_SDDTM (0x004c)
89#define U300_TIMER_APP_SDDTM_MODE_CONTINUOUS (0x00000000)
90#define U300_TIMER_APP_SDDTM_MODE_ONE_SHOT (0x00000001)
91/* DD Timer Status Register 32bit (R/-) */
92#define U300_TIMER_APP_DDTS (0x0050)
93#define U300_TIMER_APP_DDTS_TIMER_STATE_MASK (0x0000000F)
94#define U300_TIMER_APP_DDTS_TIMER_STATE_IDLE (0x00000001)
95#define U300_TIMER_APP_DDTS_TIMER_STATE_ACTIVE (0x00000002)
96#define U300_TIMER_APP_DDTS_ENABLE_IND (0x00000010)
97#define U300_TIMER_APP_DDTS_MODE_MASK (0x00000020)
98#define U300_TIMER_APP_DDTS_MODE_CONTINUOUS (0x00000000)
99#define U300_TIMER_APP_DDTS_MODE_ONE_SHOT (0x00000020)
100#define U300_TIMER_APP_DDTS_IRQ_ENABLED_IND (0x00000040)
101#define U300_TIMER_APP_DDTS_IRQ_PENDING_IND (0x00000080)
102/* DD Timer Current Count Register 32bit (R/-) */
103#define U300_TIMER_APP_DDTCC (0x0054)
104/* DD Timer Terminal Count Register 32bit (R/W) */
105#define U300_TIMER_APP_DDTTC (0x0058)
106/* DD Timer Interrupt Enable Register 32bit (-/W) */
107#define U300_TIMER_APP_DDTIE (0x005c)
108#define U300_TIMER_APP_DDTIE_IRQ_DISABLE (0x00000000)
109#define U300_TIMER_APP_DDTIE_IRQ_ENABLE (0x00000001)
110/* DD Timer Interrupt Acknowledge Register 32bit (-/W) */
111#define U300_TIMER_APP_DDTIA (0x0060)
112#define U300_TIMER_APP_DDTIA_IRQ_ACK (0x00000080)
113
114/* Reset GP1 Timer 32bit (-/W) */
115#define U300_TIMER_APP_RGPT1 (0x0080)
116#define U300_TIMER_APP_RGPT1_TIMER_RESET (0x00000000)
117/* Enable GP1 Timer 32bit (-/W) */
118#define U300_TIMER_APP_EGPT1 (0x0084)
119#define U300_TIMER_APP_EGPT1_TIMER_ENABLE (0x00000000)
120/* Disable GP1 Timer 32bit (-/W) */
121#define U300_TIMER_APP_DGPT1 (0x0088)
122#define U300_TIMER_APP_DGPT1_TIMER_DISABLE (0x00000000)
123/* GP1 Timer Mode Register 32bit (-/W) */
124#define U300_TIMER_APP_SGPT1M (0x008c)
125#define U300_TIMER_APP_SGPT1M_MODE_CONTINUOUS (0x00000000)
126#define U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT (0x00000001)
127/* GP1 Timer Status Register 32bit (R/-) */
128#define U300_TIMER_APP_GPT1S (0x0090)
129#define U300_TIMER_APP_GPT1S_TIMER_STATE_MASK (0x0000000F)
130#define U300_TIMER_APP_GPT1S_TIMER_STATE_IDLE (0x00000001)
131#define U300_TIMER_APP_GPT1S_TIMER_STATE_ACTIVE (0x00000002)
132#define U300_TIMER_APP_GPT1S_ENABLE_IND (0x00000010)
133#define U300_TIMER_APP_GPT1S_MODE_MASK (0x00000020)
134#define U300_TIMER_APP_GPT1S_MODE_CONTINUOUS (0x00000000)
135#define U300_TIMER_APP_GPT1S_MODE_ONE_SHOT (0x00000020)
136#define U300_TIMER_APP_GPT1S_IRQ_ENABLED_IND (0x00000040)
137#define U300_TIMER_APP_GPT1S_IRQ_PENDING_IND (0x00000080)
138/* GP1 Timer Current Count Register 32bit (R/-) */
139#define U300_TIMER_APP_GPT1CC (0x0094)
140/* GP1 Timer Terminal Count Register 32bit (R/W) */
141#define U300_TIMER_APP_GPT1TC (0x0098)
142/* GP1 Timer Interrupt Enable Register 32bit (-/W) */
143#define U300_TIMER_APP_GPT1IE (0x009c)
144#define U300_TIMER_APP_GPT1IE_IRQ_DISABLE (0x00000000)
145#define U300_TIMER_APP_GPT1IE_IRQ_ENABLE (0x00000001)
146/* GP1 Timer Interrupt Acknowledge Register 32bit (-/W) */
147#define U300_TIMER_APP_GPT1IA (0x00a0)
148#define U300_TIMER_APP_GPT1IA_IRQ_ACK (0x00000080)
149
150/* Reset GP2 Timer 32bit (-/W) */
151#define U300_TIMER_APP_RGPT2 (0x00c0)
152#define U300_TIMER_APP_RGPT2_TIMER_RESET (0x00000000)
153/* Enable GP2 Timer 32bit (-/W) */
154#define U300_TIMER_APP_EGPT2 (0x00c4)
155#define U300_TIMER_APP_EGPT2_TIMER_ENABLE (0x00000000)
156/* Disable GP2 Timer 32bit (-/W) */
157#define U300_TIMER_APP_DGPT2 (0x00c8)
158#define U300_TIMER_APP_DGPT2_TIMER_DISABLE (0x00000000)
159/* GP2 Timer Mode Register 32bit (-/W) */
160#define U300_TIMER_APP_SGPT2M (0x00cc)
161#define U300_TIMER_APP_SGPT2M_MODE_CONTINUOUS (0x00000000)
162#define U300_TIMER_APP_SGPT2M_MODE_ONE_SHOT (0x00000001)
163/* GP2 Timer Status Register 32bit (R/-) */
164#define U300_TIMER_APP_GPT2S (0x00d0)
165#define U300_TIMER_APP_GPT2S_TIMER_STATE_MASK (0x0000000F)
166#define U300_TIMER_APP_GPT2S_TIMER_STATE_IDLE (0x00000001)
167#define U300_TIMER_APP_GPT2S_TIMER_STATE_ACTIVE (0x00000002)
168#define U300_TIMER_APP_GPT2S_ENABLE_IND (0x00000010)
169#define U300_TIMER_APP_GPT2S_MODE_MASK (0x00000020)
170#define U300_TIMER_APP_GPT2S_MODE_CONTINUOUS (0x00000000)
171#define U300_TIMER_APP_GPT2S_MODE_ONE_SHOT (0x00000020)
172#define U300_TIMER_APP_GPT2S_IRQ_ENABLED_IND (0x00000040)
173#define U300_TIMER_APP_GPT2S_IRQ_PENDING_IND (0x00000080)
174/* GP2 Timer Current Count Register 32bit (R/-) */
175#define U300_TIMER_APP_GPT2CC (0x00d4)
176/* GP2 Timer Terminal Count Register 32bit (R/W) */
177#define U300_TIMER_APP_GPT2TC (0x00d8)
178/* GP2 Timer Interrupt Enable Register 32bit (-/W) */
179#define U300_TIMER_APP_GPT2IE (0x00dc)
180#define U300_TIMER_APP_GPT2IE_IRQ_DISABLE (0x00000000)
181#define U300_TIMER_APP_GPT2IE_IRQ_ENABLE (0x00000001)
182/* GP2 Timer Interrupt Acknowledge Register 32bit (-/W) */
183#define U300_TIMER_APP_GPT2IA (0x00e0)
184#define U300_TIMER_APP_GPT2IA_IRQ_ACK (0x00000080)
185
186/* Clock request control register - all four timers */
187#define U300_TIMER_APP_CRC (0x100)
188#define U300_TIMER_APP_CRC_CLOCK_REQUEST_ENABLE (0x00000001)
189
190#define TICKS_PER_JIFFY ((CLOCK_TICK_RATE + (HZ/2)) / HZ)
191#define US_PER_TICK ((1000000 + (HZ/2)) / HZ)
192
Linus Walleij5a5056c2013-04-08 10:50:11 +0200193static void __iomem *u300_timer_base;
194
Linus Walleijbb3cee22009-04-23 10:22:13 +0100195/*
196 * The u300_set_mode() function is always called first, if we
197 * have oneshot timer active, the oneshot scheduling function
198 * u300_set_next_event() is called immediately after.
199 */
200static void u300_set_mode(enum clock_event_mode mode,
201 struct clock_event_device *evt)
202{
203 switch (mode) {
204 case CLOCK_EVT_MODE_PERIODIC:
205 /* Disable interrupts on GPT1 */
206 writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200207 u300_timer_base + U300_TIMER_APP_GPT1IE);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100208 /* Disable GP1 while we're reprogramming it. */
209 writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200210 u300_timer_base + U300_TIMER_APP_DGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100211 /*
212 * Set the periodic mode to a certain number of ticks per
213 * jiffy.
214 */
215 writel(TICKS_PER_JIFFY,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200216 u300_timer_base + U300_TIMER_APP_GPT1TC);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100217 /*
218 * Set continuous mode, so the timer keeps triggering
219 * interrupts.
220 */
221 writel(U300_TIMER_APP_SGPT1M_MODE_CONTINUOUS,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200222 u300_timer_base + U300_TIMER_APP_SGPT1M);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100223 /* Enable timer interrupts */
224 writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200225 u300_timer_base + U300_TIMER_APP_GPT1IE);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100226 /* Then enable the OS timer again */
227 writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200228 u300_timer_base + U300_TIMER_APP_EGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100229 break;
230 case CLOCK_EVT_MODE_ONESHOT:
231 /* Just break; here? */
232 /*
233 * The actual event will be programmed by the next event hook,
234 * so we just set a dummy value somewhere at the end of the
235 * universe here.
236 */
237 /* Disable interrupts on GPT1 */
238 writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200239 u300_timer_base + U300_TIMER_APP_GPT1IE);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100240 /* Disable GP1 while we're reprogramming it. */
241 writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200242 u300_timer_base + U300_TIMER_APP_DGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100243 /*
244 * Expire far in the future, u300_set_next_event() will be
245 * called soon...
246 */
Linus Walleij5a5056c2013-04-08 10:50:11 +0200247 writel(0xFFFFFFFF, u300_timer_base + U300_TIMER_APP_GPT1TC);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100248 /* We run one shot per tick here! */
249 writel(U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200250 u300_timer_base + U300_TIMER_APP_SGPT1M);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100251 /* Enable interrupts for this timer */
252 writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200253 u300_timer_base + U300_TIMER_APP_GPT1IE);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100254 /* Enable timer */
255 writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200256 u300_timer_base + U300_TIMER_APP_EGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100257 break;
258 case CLOCK_EVT_MODE_UNUSED:
259 case CLOCK_EVT_MODE_SHUTDOWN:
260 /* Disable interrupts on GP1 */
261 writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200262 u300_timer_base + U300_TIMER_APP_GPT1IE);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100263 /* Disable GP1 */
264 writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200265 u300_timer_base + U300_TIMER_APP_DGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100266 break;
267 case CLOCK_EVT_MODE_RESUME:
268 /* Ignore this call */
269 break;
270 }
271}
272
273/*
274 * The app timer in one shot mode obviously has to be reprogrammed
275 * in EXACTLY this sequence to work properly. Do NOT try to e.g. replace
276 * the interrupt disable + timer disable commands with a reset command,
277 * it will fail miserably. Apparently (and I found this the hard way)
278 * the timer is very sensitive to the instruction order, though you don't
279 * get that impression from the data sheet.
280 */
281static int u300_set_next_event(unsigned long cycles,
282 struct clock_event_device *evt)
283
284{
285 /* Disable interrupts on GPT1 */
286 writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200287 u300_timer_base + U300_TIMER_APP_GPT1IE);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100288 /* Disable GP1 while we're reprogramming it. */
289 writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200290 u300_timer_base + U300_TIMER_APP_DGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100291 /* Reset the General Purpose timer 1. */
292 writel(U300_TIMER_APP_RGPT1_TIMER_RESET,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200293 u300_timer_base + U300_TIMER_APP_RGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100294 /* IRQ in n * cycles */
Linus Walleij5a5056c2013-04-08 10:50:11 +0200295 writel(cycles, u300_timer_base + U300_TIMER_APP_GPT1TC);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100296 /*
297 * We run one shot per tick here! (This is necessary to reconfigure,
298 * the timer will tilt if you don't!)
299 */
300 writel(U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200301 u300_timer_base + U300_TIMER_APP_SGPT1M);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100302 /* Enable timer interrupts */
303 writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200304 u300_timer_base + U300_TIMER_APP_GPT1IE);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100305 /* Then enable the OS timer again */
306 writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200307 u300_timer_base + U300_TIMER_APP_EGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100308 return 0;
309}
310
311
312/* Use general purpose timer 1 as clock event */
313static struct clock_event_device clockevent_u300_1mhz = {
Linus Walleijcde21de2011-05-30 15:51:47 +0200314 .name = "GPT1",
315 .rating = 300, /* Reasonably fast and accurate clock event */
316 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
317 .set_next_event = u300_set_next_event,
318 .set_mode = u300_set_mode,
Linus Walleijbb3cee22009-04-23 10:22:13 +0100319};
320
321/* Clock event timer interrupt handler */
322static irqreturn_t u300_timer_interrupt(int irq, void *dev_id)
323{
324 struct clock_event_device *evt = &clockevent_u300_1mhz;
325 /* ACK/Clear timer IRQ for the APP GPT1 Timer */
Linus Walleij5a5056c2013-04-08 10:50:11 +0200326
Linus Walleijbb3cee22009-04-23 10:22:13 +0100327 writel(U300_TIMER_APP_GPT1IA_IRQ_ACK,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200328 u300_timer_base + U300_TIMER_APP_GPT1IA);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100329 evt->event_handler(evt);
330 return IRQ_HANDLED;
331}
332
333static struct irqaction u300_timer_irq = {
Linus Walleijcde21de2011-05-30 15:51:47 +0200334 .name = "U300 Timer Tick",
335 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
336 .handler = u300_timer_interrupt,
Linus Walleijbb3cee22009-04-23 10:22:13 +0100337};
338
Linus Walleija2ca00e2009-09-17 13:10:50 +0100339/*
340 * Override the global weak sched_clock symbol with this
341 * local implementation which uses the clocksource to get some
342 * better resolution when scheduling the kernel. We accept that
343 * this wraps around for now, since it is just a relative time
344 * stamp. (Inspired by OMAP implementation.)
345 */
Russell King5c21b7c2010-12-15 21:50:14 +0000346
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100347static u32 notrace u300_read_sched_clock(void)
Linus Walleija2ca00e2009-09-17 13:10:50 +0100348{
Linus Walleij5a5056c2013-04-08 10:50:11 +0200349 return readl(u300_timer_base + U300_TIMER_APP_GPT2CC);
Linus Walleija2ca00e2009-09-17 13:10:50 +0100350}
351
Linus Walleij3c96d8e2013-04-05 22:45:11 +0200352static unsigned long u300_read_current_timer(void)
353{
Linus Walleij5a5056c2013-04-08 10:50:11 +0200354 return readl(u300_timer_base + U300_TIMER_APP_GPT2CC);
Linus Walleij3c96d8e2013-04-05 22:45:11 +0200355}
356
357static struct delay_timer u300_delay_timer;
Linus Walleijbb3cee22009-04-23 10:22:13 +0100358
359/*
360 * This sets up the system timers, clock source and clock event.
361 */
Linus Walleij75a7f3f2013-04-22 11:29:30 +0200362static void __init u300_timer_init_of(struct device_node *np)
Linus Walleijbb3cee22009-04-23 10:22:13 +0100363{
Linus Walleij75a7f3f2013-04-22 11:29:30 +0200364 struct resource irq_res;
365 int irq;
Linus Walleijb7276b22010-08-05 07:58:58 +0100366 struct clk *clk;
Linus Walleij3af8a8d2010-08-05 07:59:54 +0100367 unsigned long rate;
Linus Walleijb7276b22010-08-05 07:58:58 +0100368
Linus Walleij75a7f3f2013-04-22 11:29:30 +0200369 u300_timer_base = of_iomap(np, 0);
370 if (!u300_timer_base)
371 panic("could not ioremap system timer\n");
372
373 /* Get the IRQ for the GP1 timer */
374 irq = of_irq_to_resource(np, 2, &irq_res);
375 if (irq <= 0)
376 panic("no IRQ for system timer\n");
377
Linus Walleij5a5056c2013-04-08 10:50:11 +0200378 pr_info("U300 GP1 timer @ base: %p, IRQ: %d\n", u300_timer_base, irq);
379
Linus Walleijb7276b22010-08-05 07:58:58 +0100380 /* Clock the interrupt controller */
381 clk = clk_get_sys("apptimer", NULL);
382 BUG_ON(IS_ERR(clk));
Linus Walleij50667d62012-06-19 23:44:25 +0200383 clk_prepare_enable(clk);
Linus Walleij3af8a8d2010-08-05 07:59:54 +0100384 rate = clk_get_rate(clk);
Linus Walleijb7276b22010-08-05 07:58:58 +0100385
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100386 setup_sched_clock(u300_read_sched_clock, 32, rate);
Russell King5c21b7c2010-12-15 21:50:14 +0000387
Linus Walleij3c96d8e2013-04-05 22:45:11 +0200388 u300_delay_timer.read_current_timer = &u300_read_current_timer;
389 u300_delay_timer.freq = rate;
390 register_current_timer_delay(&u300_delay_timer);
391
Linus Walleijbb3cee22009-04-23 10:22:13 +0100392 /*
393 * Disable the "OS" and "DD" timers - these are designed for Symbian!
394 * Example usage in cnh1601578 cpu subsystem pd_timer_app.c
395 */
396 writel(U300_TIMER_APP_CRC_CLOCK_REQUEST_ENABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200397 u300_timer_base + U300_TIMER_APP_CRC);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100398 writel(U300_TIMER_APP_ROST_TIMER_RESET,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200399 u300_timer_base + U300_TIMER_APP_ROST);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100400 writel(U300_TIMER_APP_DOST_TIMER_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200401 u300_timer_base + U300_TIMER_APP_DOST);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100402 writel(U300_TIMER_APP_RDDT_TIMER_RESET,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200403 u300_timer_base + U300_TIMER_APP_RDDT);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100404 writel(U300_TIMER_APP_DDDT_TIMER_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200405 u300_timer_base + U300_TIMER_APP_DDDT);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100406
407 /* Reset the General Purpose timer 1. */
408 writel(U300_TIMER_APP_RGPT1_TIMER_RESET,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200409 u300_timer_base + U300_TIMER_APP_RGPT1);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100410
411 /* Set up the IRQ handler */
Linus Walleij5a5056c2013-04-08 10:50:11 +0200412 setup_irq(irq, &u300_timer_irq);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100413
414 /* Reset the General Purpose timer 2 */
415 writel(U300_TIMER_APP_RGPT2_TIMER_RESET,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200416 u300_timer_base + U300_TIMER_APP_RGPT2);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100417 /* Set this timer to run around forever */
Linus Walleij5a5056c2013-04-08 10:50:11 +0200418 writel(0xFFFFFFFFU, u300_timer_base + U300_TIMER_APP_GPT2TC);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100419 /* Set continuous mode so it wraps around */
420 writel(U300_TIMER_APP_SGPT2M_MODE_CONTINUOUS,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200421 u300_timer_base + U300_TIMER_APP_SGPT2M);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100422 /* Disable timer interrupts */
423 writel(U300_TIMER_APP_GPT2IE_IRQ_DISABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200424 u300_timer_base + U300_TIMER_APP_GPT2IE);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100425 /* Then enable the GP2 timer to use as a free running us counter */
426 writel(U300_TIMER_APP_EGPT2_TIMER_ENABLE,
Linus Walleij5a5056c2013-04-08 10:50:11 +0200427 u300_timer_base + U300_TIMER_APP_EGPT2);
Linus Walleijbb3cee22009-04-23 10:22:13 +0100428
Russell King234b6ced2011-05-08 14:09:47 +0100429 /* Use general purpose timer 2 as clock source */
Linus Walleij5a5056c2013-04-08 10:50:11 +0200430 if (clocksource_mmio_init(u300_timer_base + U300_TIMER_APP_GPT2CC,
Russell King234b6ced2011-05-08 14:09:47 +0100431 "GPT2", rate, 300, 32, clocksource_mmio_readl_up))
Linus Walleij94250322011-05-31 22:10:03 +0100432 pr_err("timer: failed to initialize U300 clock source\n");
Linus Walleijbb3cee22009-04-23 10:22:13 +0100433
Linus Walleijcde21de2011-05-30 15:51:47 +0200434 /* Configure and register the clockevent */
435 clockevents_config_and_register(&clockevent_u300_1mhz, rate,
436 1, 0xffffffff);
437
Linus Walleijbb3cee22009-04-23 10:22:13 +0100438 /*
439 * TODO: init and register the rest of the timers too, they can be
440 * used by hrtimers!
441 */
442}
Linus Walleij5a5056c2013-04-08 10:50:11 +0200443
Linus Walleij5a5056c2013-04-08 10:50:11 +0200444CLOCKSOURCE_OF_DECLARE(u300_timer, "stericsson,u300-apptimer",
445 u300_timer_init_of);