1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2007-2009 ST-Ericsson AB
4*4882a593Smuzhiyun * Timer COH 901 328, runs the OS timer interrupt.
5*4882a593Smuzhiyun * Author: Linus Walleij <linus.walleij@stericsson.com>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun #include <linux/interrupt.h>
8*4882a593Smuzhiyun #include <linux/time.h>
9*4882a593Smuzhiyun #include <linux/timex.h>
10*4882a593Smuzhiyun #include <linux/clockchips.h>
11*4882a593Smuzhiyun #include <linux/clocksource.h>
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/io.h>
14*4882a593Smuzhiyun #include <linux/clk.h>
15*4882a593Smuzhiyun #include <linux/err.h>
16*4882a593Smuzhiyun #include <linux/irq.h>
17*4882a593Smuzhiyun #include <linux/delay.h>
18*4882a593Smuzhiyun #include <linux/of_address.h>
19*4882a593Smuzhiyun #include <linux/of_irq.h>
20*4882a593Smuzhiyun #include <linux/sched_clock.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /* Generic stuff */
23*4882a593Smuzhiyun #include <asm/mach/map.h>
24*4882a593Smuzhiyun #include <asm/mach/time.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun * APP side special timer registers
28*4882a593Smuzhiyun * This timer contains four timers which can fire an interrupt each.
29*4882a593Smuzhiyun * OS (operating system) timer @ 32768 Hz
30*4882a593Smuzhiyun * DD (device driver) timer @ 1 kHz
31*4882a593Smuzhiyun * GP1 (general purpose 1) timer @ 1MHz
32*4882a593Smuzhiyun * GP2 (general purpose 2) timer @ 1MHz
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /* Reset OS Timer 32bit (-/W) */
36*4882a593Smuzhiyun #define U300_TIMER_APP_ROST (0x0000)
37*4882a593Smuzhiyun #define U300_TIMER_APP_ROST_TIMER_RESET (0x00000000)
38*4882a593Smuzhiyun /* Enable OS Timer 32bit (-/W) */
39*4882a593Smuzhiyun #define U300_TIMER_APP_EOST (0x0004)
40*4882a593Smuzhiyun #define U300_TIMER_APP_EOST_TIMER_ENABLE (0x00000000)
41*4882a593Smuzhiyun /* Disable OS Timer 32bit (-/W) */
42*4882a593Smuzhiyun #define U300_TIMER_APP_DOST (0x0008)
43*4882a593Smuzhiyun #define U300_TIMER_APP_DOST_TIMER_DISABLE (0x00000000)
44*4882a593Smuzhiyun /* OS Timer Mode Register 32bit (-/W) */
45*4882a593Smuzhiyun #define U300_TIMER_APP_SOSTM (0x000c)
46*4882a593Smuzhiyun #define U300_TIMER_APP_SOSTM_MODE_CONTINUOUS (0x00000000)
47*4882a593Smuzhiyun #define U300_TIMER_APP_SOSTM_MODE_ONE_SHOT (0x00000001)
48*4882a593Smuzhiyun /* OS Timer Status Register 32bit (R/-) */
49*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS (0x0010)
50*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_TIMER_STATE_MASK (0x0000000F)
51*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_TIMER_STATE_IDLE (0x00000001)
52*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_TIMER_STATE_ACTIVE (0x00000002)
53*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_ENABLE_IND (0x00000010)
54*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_MODE_MASK (0x00000020)
55*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_MODE_CONTINUOUS (0x00000000)
56*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_MODE_ONE_SHOT (0x00000020)
57*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_IRQ_ENABLED_IND (0x00000040)
58*4882a593Smuzhiyun #define U300_TIMER_APP_OSTS_IRQ_PENDING_IND (0x00000080)
59*4882a593Smuzhiyun /* OS Timer Current Count Register 32bit (R/-) */
60*4882a593Smuzhiyun #define U300_TIMER_APP_OSTCC (0x0014)
61*4882a593Smuzhiyun /* OS Timer Terminal Count Register 32bit (R/W) */
62*4882a593Smuzhiyun #define U300_TIMER_APP_OSTTC (0x0018)
63*4882a593Smuzhiyun /* OS Timer Interrupt Enable Register 32bit (-/W) */
64*4882a593Smuzhiyun #define U300_TIMER_APP_OSTIE (0x001c)
65*4882a593Smuzhiyun #define U300_TIMER_APP_OSTIE_IRQ_DISABLE (0x00000000)
66*4882a593Smuzhiyun #define U300_TIMER_APP_OSTIE_IRQ_ENABLE (0x00000001)
67*4882a593Smuzhiyun /* OS Timer Interrupt Acknowledge Register 32bit (-/W) */
68*4882a593Smuzhiyun #define U300_TIMER_APP_OSTIA (0x0020)
69*4882a593Smuzhiyun #define U300_TIMER_APP_OSTIA_IRQ_ACK (0x00000080)
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* Reset DD Timer 32bit (-/W) */
72*4882a593Smuzhiyun #define U300_TIMER_APP_RDDT (0x0040)
73*4882a593Smuzhiyun #define U300_TIMER_APP_RDDT_TIMER_RESET (0x00000000)
74*4882a593Smuzhiyun /* Enable DD Timer 32bit (-/W) */
75*4882a593Smuzhiyun #define U300_TIMER_APP_EDDT (0x0044)
76*4882a593Smuzhiyun #define U300_TIMER_APP_EDDT_TIMER_ENABLE (0x00000000)
77*4882a593Smuzhiyun /* Disable DD Timer 32bit (-/W) */
78*4882a593Smuzhiyun #define U300_TIMER_APP_DDDT (0x0048)
79*4882a593Smuzhiyun #define U300_TIMER_APP_DDDT_TIMER_DISABLE (0x00000000)
80*4882a593Smuzhiyun /* DD Timer Mode Register 32bit (-/W) */
81*4882a593Smuzhiyun #define U300_TIMER_APP_SDDTM (0x004c)
82*4882a593Smuzhiyun #define U300_TIMER_APP_SDDTM_MODE_CONTINUOUS (0x00000000)
83*4882a593Smuzhiyun #define U300_TIMER_APP_SDDTM_MODE_ONE_SHOT (0x00000001)
84*4882a593Smuzhiyun /* DD Timer Status Register 32bit (R/-) */
85*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS (0x0050)
86*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_TIMER_STATE_MASK (0x0000000F)
87*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_TIMER_STATE_IDLE (0x00000001)
88*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_TIMER_STATE_ACTIVE (0x00000002)
89*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_ENABLE_IND (0x00000010)
90*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_MODE_MASK (0x00000020)
91*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_MODE_CONTINUOUS (0x00000000)
92*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_MODE_ONE_SHOT (0x00000020)
93*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_IRQ_ENABLED_IND (0x00000040)
94*4882a593Smuzhiyun #define U300_TIMER_APP_DDTS_IRQ_PENDING_IND (0x00000080)
95*4882a593Smuzhiyun /* DD Timer Current Count Register 32bit (R/-) */
96*4882a593Smuzhiyun #define U300_TIMER_APP_DDTCC (0x0054)
97*4882a593Smuzhiyun /* DD Timer Terminal Count Register 32bit (R/W) */
98*4882a593Smuzhiyun #define U300_TIMER_APP_DDTTC (0x0058)
99*4882a593Smuzhiyun /* DD Timer Interrupt Enable Register 32bit (-/W) */
100*4882a593Smuzhiyun #define U300_TIMER_APP_DDTIE (0x005c)
101*4882a593Smuzhiyun #define U300_TIMER_APP_DDTIE_IRQ_DISABLE (0x00000000)
102*4882a593Smuzhiyun #define U300_TIMER_APP_DDTIE_IRQ_ENABLE (0x00000001)
103*4882a593Smuzhiyun /* DD Timer Interrupt Acknowledge Register 32bit (-/W) */
104*4882a593Smuzhiyun #define U300_TIMER_APP_DDTIA (0x0060)
105*4882a593Smuzhiyun #define U300_TIMER_APP_DDTIA_IRQ_ACK (0x00000080)
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /* Reset GP1 Timer 32bit (-/W) */
108*4882a593Smuzhiyun #define U300_TIMER_APP_RGPT1 (0x0080)
109*4882a593Smuzhiyun #define U300_TIMER_APP_RGPT1_TIMER_RESET (0x00000000)
110*4882a593Smuzhiyun /* Enable GP1 Timer 32bit (-/W) */
111*4882a593Smuzhiyun #define U300_TIMER_APP_EGPT1 (0x0084)
112*4882a593Smuzhiyun #define U300_TIMER_APP_EGPT1_TIMER_ENABLE (0x00000000)
113*4882a593Smuzhiyun /* Disable GP1 Timer 32bit (-/W) */
114*4882a593Smuzhiyun #define U300_TIMER_APP_DGPT1 (0x0088)
115*4882a593Smuzhiyun #define U300_TIMER_APP_DGPT1_TIMER_DISABLE (0x00000000)
116*4882a593Smuzhiyun /* GP1 Timer Mode Register 32bit (-/W) */
117*4882a593Smuzhiyun #define U300_TIMER_APP_SGPT1M (0x008c)
118*4882a593Smuzhiyun #define U300_TIMER_APP_SGPT1M_MODE_CONTINUOUS (0x00000000)
119*4882a593Smuzhiyun #define U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT (0x00000001)
120*4882a593Smuzhiyun /* GP1 Timer Status Register 32bit (R/-) */
121*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S (0x0090)
122*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_TIMER_STATE_MASK (0x0000000F)
123*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_TIMER_STATE_IDLE (0x00000001)
124*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_TIMER_STATE_ACTIVE (0x00000002)
125*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_ENABLE_IND (0x00000010)
126*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_MODE_MASK (0x00000020)
127*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_MODE_CONTINUOUS (0x00000000)
128*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_MODE_ONE_SHOT (0x00000020)
129*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_IRQ_ENABLED_IND (0x00000040)
130*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1S_IRQ_PENDING_IND (0x00000080)
131*4882a593Smuzhiyun /* GP1 Timer Current Count Register 32bit (R/-) */
132*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1CC (0x0094)
133*4882a593Smuzhiyun /* GP1 Timer Terminal Count Register 32bit (R/W) */
134*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1TC (0x0098)
135*4882a593Smuzhiyun /* GP1 Timer Interrupt Enable Register 32bit (-/W) */
136*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1IE (0x009c)
137*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1IE_IRQ_DISABLE (0x00000000)
138*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1IE_IRQ_ENABLE (0x00000001)
139*4882a593Smuzhiyun /* GP1 Timer Interrupt Acknowledge Register 32bit (-/W) */
140*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1IA (0x00a0)
141*4882a593Smuzhiyun #define U300_TIMER_APP_GPT1IA_IRQ_ACK (0x00000080)
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* Reset GP2 Timer 32bit (-/W) */
144*4882a593Smuzhiyun #define U300_TIMER_APP_RGPT2 (0x00c0)
145*4882a593Smuzhiyun #define U300_TIMER_APP_RGPT2_TIMER_RESET (0x00000000)
146*4882a593Smuzhiyun /* Enable GP2 Timer 32bit (-/W) */
147*4882a593Smuzhiyun #define U300_TIMER_APP_EGPT2 (0x00c4)
148*4882a593Smuzhiyun #define U300_TIMER_APP_EGPT2_TIMER_ENABLE (0x00000000)
149*4882a593Smuzhiyun /* Disable GP2 Timer 32bit (-/W) */
150*4882a593Smuzhiyun #define U300_TIMER_APP_DGPT2 (0x00c8)
151*4882a593Smuzhiyun #define U300_TIMER_APP_DGPT2_TIMER_DISABLE (0x00000000)
152*4882a593Smuzhiyun /* GP2 Timer Mode Register 32bit (-/W) */
153*4882a593Smuzhiyun #define U300_TIMER_APP_SGPT2M (0x00cc)
154*4882a593Smuzhiyun #define U300_TIMER_APP_SGPT2M_MODE_CONTINUOUS (0x00000000)
155*4882a593Smuzhiyun #define U300_TIMER_APP_SGPT2M_MODE_ONE_SHOT (0x00000001)
156*4882a593Smuzhiyun /* GP2 Timer Status Register 32bit (R/-) */
157*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S (0x00d0)
158*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_TIMER_STATE_MASK (0x0000000F)
159*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_TIMER_STATE_IDLE (0x00000001)
160*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_TIMER_STATE_ACTIVE (0x00000002)
161*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_ENABLE_IND (0x00000010)
162*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_MODE_MASK (0x00000020)
163*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_MODE_CONTINUOUS (0x00000000)
164*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_MODE_ONE_SHOT (0x00000020)
165*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_IRQ_ENABLED_IND (0x00000040)
166*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2S_IRQ_PENDING_IND (0x00000080)
167*4882a593Smuzhiyun /* GP2 Timer Current Count Register 32bit (R/-) */
168*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2CC (0x00d4)
169*4882a593Smuzhiyun /* GP2 Timer Terminal Count Register 32bit (R/W) */
170*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2TC (0x00d8)
171*4882a593Smuzhiyun /* GP2 Timer Interrupt Enable Register 32bit (-/W) */
172*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2IE (0x00dc)
173*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2IE_IRQ_DISABLE (0x00000000)
174*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2IE_IRQ_ENABLE (0x00000001)
175*4882a593Smuzhiyun /* GP2 Timer Interrupt Acknowledge Register 32bit (-/W) */
176*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2IA (0x00e0)
177*4882a593Smuzhiyun #define U300_TIMER_APP_GPT2IA_IRQ_ACK (0x00000080)
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /* Clock request control register - all four timers */
180*4882a593Smuzhiyun #define U300_TIMER_APP_CRC (0x100)
181*4882a593Smuzhiyun #define U300_TIMER_APP_CRC_CLOCK_REQUEST_ENABLE (0x00000001)
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun static void __iomem *u300_timer_base;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun struct u300_clockevent_data {
186*4882a593Smuzhiyun struct clock_event_device cevd;
187*4882a593Smuzhiyun unsigned ticks_per_jiffy;
188*4882a593Smuzhiyun };
189*4882a593Smuzhiyun
u300_shutdown(struct clock_event_device * evt)190*4882a593Smuzhiyun static int u300_shutdown(struct clock_event_device *evt)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun /* Disable interrupts on GP1 */
193*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
194*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1IE);
195*4882a593Smuzhiyun /* Disable GP1 */
196*4882a593Smuzhiyun writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
197*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_DGPT1);
198*4882a593Smuzhiyun return 0;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /*
202*4882a593Smuzhiyun * If we have oneshot timer active, the oneshot scheduling function
203*4882a593Smuzhiyun * u300_set_next_event() is called immediately after.
204*4882a593Smuzhiyun */
u300_set_oneshot(struct clock_event_device * evt)205*4882a593Smuzhiyun static int u300_set_oneshot(struct clock_event_device *evt)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun /* Just return; here? */
208*4882a593Smuzhiyun /*
209*4882a593Smuzhiyun * The actual event will be programmed by the next event hook,
210*4882a593Smuzhiyun * so we just set a dummy value somewhere at the end of the
211*4882a593Smuzhiyun * universe here.
212*4882a593Smuzhiyun */
213*4882a593Smuzhiyun /* Disable interrupts on GPT1 */
214*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
215*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1IE);
216*4882a593Smuzhiyun /* Disable GP1 while we're reprogramming it. */
217*4882a593Smuzhiyun writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
218*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_DGPT1);
219*4882a593Smuzhiyun /*
220*4882a593Smuzhiyun * Expire far in the future, u300_set_next_event() will be
221*4882a593Smuzhiyun * called soon...
222*4882a593Smuzhiyun */
223*4882a593Smuzhiyun writel(0xFFFFFFFF, u300_timer_base + U300_TIMER_APP_GPT1TC);
224*4882a593Smuzhiyun /* We run one shot per tick here! */
225*4882a593Smuzhiyun writel(U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT,
226*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_SGPT1M);
227*4882a593Smuzhiyun /* Enable interrupts for this timer */
228*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
229*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1IE);
230*4882a593Smuzhiyun /* Enable timer */
231*4882a593Smuzhiyun writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
232*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_EGPT1);
233*4882a593Smuzhiyun return 0;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
u300_set_periodic(struct clock_event_device * evt)236*4882a593Smuzhiyun static int u300_set_periodic(struct clock_event_device *evt)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun struct u300_clockevent_data *cevdata =
239*4882a593Smuzhiyun container_of(evt, struct u300_clockevent_data, cevd);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /* Disable interrupts on GPT1 */
242*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
243*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1IE);
244*4882a593Smuzhiyun /* Disable GP1 while we're reprogramming it. */
245*4882a593Smuzhiyun writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
246*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_DGPT1);
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun * Set the periodic mode to a certain number of ticks per
249*4882a593Smuzhiyun * jiffy.
250*4882a593Smuzhiyun */
251*4882a593Smuzhiyun writel(cevdata->ticks_per_jiffy,
252*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1TC);
253*4882a593Smuzhiyun /*
254*4882a593Smuzhiyun * Set continuous mode, so the timer keeps triggering
255*4882a593Smuzhiyun * interrupts.
256*4882a593Smuzhiyun */
257*4882a593Smuzhiyun writel(U300_TIMER_APP_SGPT1M_MODE_CONTINUOUS,
258*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_SGPT1M);
259*4882a593Smuzhiyun /* Enable timer interrupts */
260*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
261*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1IE);
262*4882a593Smuzhiyun /* Then enable the OS timer again */
263*4882a593Smuzhiyun writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
264*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_EGPT1);
265*4882a593Smuzhiyun return 0;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /*
269*4882a593Smuzhiyun * The app timer in one shot mode obviously has to be reprogrammed
270*4882a593Smuzhiyun * in EXACTLY this sequence to work properly. Do NOT try to e.g. replace
271*4882a593Smuzhiyun * the interrupt disable + timer disable commands with a reset command,
272*4882a593Smuzhiyun * it will fail miserably. Apparently (and I found this the hard way)
273*4882a593Smuzhiyun * the timer is very sensitive to the instruction order, though you don't
274*4882a593Smuzhiyun * get that impression from the data sheet.
275*4882a593Smuzhiyun */
u300_set_next_event(unsigned long cycles,struct clock_event_device * evt)276*4882a593Smuzhiyun static int u300_set_next_event(unsigned long cycles,
277*4882a593Smuzhiyun struct clock_event_device *evt)
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun /* Disable interrupts on GPT1 */
281*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
282*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1IE);
283*4882a593Smuzhiyun /* Disable GP1 while we're reprogramming it. */
284*4882a593Smuzhiyun writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
285*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_DGPT1);
286*4882a593Smuzhiyun /* Reset the General Purpose timer 1. */
287*4882a593Smuzhiyun writel(U300_TIMER_APP_RGPT1_TIMER_RESET,
288*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_RGPT1);
289*4882a593Smuzhiyun /* IRQ in n * cycles */
290*4882a593Smuzhiyun writel(cycles, u300_timer_base + U300_TIMER_APP_GPT1TC);
291*4882a593Smuzhiyun /*
292*4882a593Smuzhiyun * We run one shot per tick here! (This is necessary to reconfigure,
293*4882a593Smuzhiyun * the timer will tilt if you don't!)
294*4882a593Smuzhiyun */
295*4882a593Smuzhiyun writel(U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT,
296*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_SGPT1M);
297*4882a593Smuzhiyun /* Enable timer interrupts */
298*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
299*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1IE);
300*4882a593Smuzhiyun /* Then enable the OS timer again */
301*4882a593Smuzhiyun writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
302*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_EGPT1);
303*4882a593Smuzhiyun return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun static struct u300_clockevent_data u300_clockevent_data = {
307*4882a593Smuzhiyun /* Use general purpose timer 1 as clock event */
308*4882a593Smuzhiyun .cevd = {
309*4882a593Smuzhiyun .name = "GPT1",
310*4882a593Smuzhiyun /* Reasonably fast and accurate clock event */
311*4882a593Smuzhiyun .rating = 300,
312*4882a593Smuzhiyun .features = CLOCK_EVT_FEAT_PERIODIC |
313*4882a593Smuzhiyun CLOCK_EVT_FEAT_ONESHOT,
314*4882a593Smuzhiyun .set_next_event = u300_set_next_event,
315*4882a593Smuzhiyun .set_state_shutdown = u300_shutdown,
316*4882a593Smuzhiyun .set_state_periodic = u300_set_periodic,
317*4882a593Smuzhiyun .set_state_oneshot = u300_set_oneshot,
318*4882a593Smuzhiyun },
319*4882a593Smuzhiyun };
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /* Clock event timer interrupt handler */
u300_timer_interrupt(int irq,void * dev_id)322*4882a593Smuzhiyun static irqreturn_t u300_timer_interrupt(int irq, void *dev_id)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun struct clock_event_device *evt = &u300_clockevent_data.cevd;
325*4882a593Smuzhiyun /* ACK/Clear timer IRQ for the APP GPT1 Timer */
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT1IA_IRQ_ACK,
328*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT1IA);
329*4882a593Smuzhiyun evt->event_handler(evt);
330*4882a593Smuzhiyun return IRQ_HANDLED;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /*
334*4882a593Smuzhiyun * Override the global weak sched_clock symbol with this
335*4882a593Smuzhiyun * local implementation which uses the clocksource to get some
336*4882a593Smuzhiyun * better resolution when scheduling the kernel. We accept that
337*4882a593Smuzhiyun * this wraps around for now, since it is just a relative time
338*4882a593Smuzhiyun * stamp. (Inspired by OMAP implementation.)
339*4882a593Smuzhiyun */
340*4882a593Smuzhiyun
u300_read_sched_clock(void)341*4882a593Smuzhiyun static u64 notrace u300_read_sched_clock(void)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun return readl(u300_timer_base + U300_TIMER_APP_GPT2CC);
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
u300_read_current_timer(void)346*4882a593Smuzhiyun static unsigned long u300_read_current_timer(void)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun return readl(u300_timer_base + U300_TIMER_APP_GPT2CC);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun static struct delay_timer u300_delay_timer;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /*
354*4882a593Smuzhiyun * This sets up the system timers, clock source and clock event.
355*4882a593Smuzhiyun */
u300_timer_init_of(struct device_node * np)356*4882a593Smuzhiyun static int __init u300_timer_init_of(struct device_node *np)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun unsigned int irq;
359*4882a593Smuzhiyun struct clk *clk;
360*4882a593Smuzhiyun unsigned long rate;
361*4882a593Smuzhiyun int ret;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun u300_timer_base = of_iomap(np, 0);
364*4882a593Smuzhiyun if (!u300_timer_base) {
365*4882a593Smuzhiyun pr_err("could not ioremap system timer\n");
366*4882a593Smuzhiyun return -ENXIO;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /* Get the IRQ for the GP1 timer */
370*4882a593Smuzhiyun irq = irq_of_parse_and_map(np, 2);
371*4882a593Smuzhiyun if (!irq) {
372*4882a593Smuzhiyun pr_err("no IRQ for system timer\n");
373*4882a593Smuzhiyun return -EINVAL;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun pr_info("U300 GP1 timer @ base: %p, IRQ: %u\n", u300_timer_base, irq);
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun /* Clock the interrupt controller */
379*4882a593Smuzhiyun clk = of_clk_get(np, 0);
380*4882a593Smuzhiyun if (IS_ERR(clk))
381*4882a593Smuzhiyun return PTR_ERR(clk);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun ret = clk_prepare_enable(clk);
384*4882a593Smuzhiyun if (ret)
385*4882a593Smuzhiyun return ret;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun rate = clk_get_rate(clk);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun u300_clockevent_data.ticks_per_jiffy = DIV_ROUND_CLOSEST(rate, HZ);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun sched_clock_register(u300_read_sched_clock, 32, rate);
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun u300_delay_timer.read_current_timer = &u300_read_current_timer;
394*4882a593Smuzhiyun u300_delay_timer.freq = rate;
395*4882a593Smuzhiyun register_current_timer_delay(&u300_delay_timer);
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /*
398*4882a593Smuzhiyun * Disable the "OS" and "DD" timers - these are designed for Symbian!
399*4882a593Smuzhiyun * Example usage in cnh1601578 cpu subsystem pd_timer_app.c
400*4882a593Smuzhiyun */
401*4882a593Smuzhiyun writel(U300_TIMER_APP_CRC_CLOCK_REQUEST_ENABLE,
402*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_CRC);
403*4882a593Smuzhiyun writel(U300_TIMER_APP_ROST_TIMER_RESET,
404*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_ROST);
405*4882a593Smuzhiyun writel(U300_TIMER_APP_DOST_TIMER_DISABLE,
406*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_DOST);
407*4882a593Smuzhiyun writel(U300_TIMER_APP_RDDT_TIMER_RESET,
408*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_RDDT);
409*4882a593Smuzhiyun writel(U300_TIMER_APP_DDDT_TIMER_DISABLE,
410*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_DDDT);
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun /* Reset the General Purpose timer 1. */
413*4882a593Smuzhiyun writel(U300_TIMER_APP_RGPT1_TIMER_RESET,
414*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_RGPT1);
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun /* Set up the IRQ handler */
417*4882a593Smuzhiyun ret = request_irq(irq, u300_timer_interrupt,
418*4882a593Smuzhiyun IRQF_TIMER | IRQF_IRQPOLL, "U300 Timer Tick", NULL);
419*4882a593Smuzhiyun if (ret)
420*4882a593Smuzhiyun return ret;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /* Reset the General Purpose timer 2 */
423*4882a593Smuzhiyun writel(U300_TIMER_APP_RGPT2_TIMER_RESET,
424*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_RGPT2);
425*4882a593Smuzhiyun /* Set this timer to run around forever */
426*4882a593Smuzhiyun writel(0xFFFFFFFFU, u300_timer_base + U300_TIMER_APP_GPT2TC);
427*4882a593Smuzhiyun /* Set continuous mode so it wraps around */
428*4882a593Smuzhiyun writel(U300_TIMER_APP_SGPT2M_MODE_CONTINUOUS,
429*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_SGPT2M);
430*4882a593Smuzhiyun /* Disable timer interrupts */
431*4882a593Smuzhiyun writel(U300_TIMER_APP_GPT2IE_IRQ_DISABLE,
432*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_GPT2IE);
433*4882a593Smuzhiyun /* Then enable the GP2 timer to use as a free running us counter */
434*4882a593Smuzhiyun writel(U300_TIMER_APP_EGPT2_TIMER_ENABLE,
435*4882a593Smuzhiyun u300_timer_base + U300_TIMER_APP_EGPT2);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /* Use general purpose timer 2 as clock source */
438*4882a593Smuzhiyun ret = clocksource_mmio_init(u300_timer_base + U300_TIMER_APP_GPT2CC,
439*4882a593Smuzhiyun "GPT2", rate, 300, 32, clocksource_mmio_readl_up);
440*4882a593Smuzhiyun if (ret) {
441*4882a593Smuzhiyun pr_err("timer: failed to initialize U300 clock source\n");
442*4882a593Smuzhiyun return ret;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun /* Configure and register the clockevent */
446*4882a593Smuzhiyun clockevents_config_and_register(&u300_clockevent_data.cevd, rate,
447*4882a593Smuzhiyun 1, 0xffffffff);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /*
450*4882a593Smuzhiyun * TODO: init and register the rest of the timers too, they can be
451*4882a593Smuzhiyun * used by hrtimers!
452*4882a593Smuzhiyun */
453*4882a593Smuzhiyun return 0;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun TIMER_OF_DECLARE(u300_timer, "stericsson,u300-apptimer",
457*4882a593Smuzhiyun u300_timer_init_of);
458