1
2
3
4
5
6
7
8
9
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>
18#include <linux/clk.h>
19#include <linux/err.h>
20#include <linux/irq.h>
21#include <linux/delay.h>
22#include <linux/of_address.h>
23#include <linux/of_irq.h>
24#include <linux/sched_clock.h>
25
26
27#include <asm/mach/map.h>
28#include <asm/mach/time.h>
29
30
31
32
33
34
35
36
37
38
39
40#define U300_TIMER_APP_ROST (0x0000)
41#define U300_TIMER_APP_ROST_TIMER_RESET (0x00000000)
42
43#define U300_TIMER_APP_EOST (0x0004)
44#define U300_TIMER_APP_EOST_TIMER_ENABLE (0x00000000)
45
46#define U300_TIMER_APP_DOST (0x0008)
47#define U300_TIMER_APP_DOST_TIMER_DISABLE (0x00000000)
48
49#define U300_TIMER_APP_SOSTM (0x000c)
50#define U300_TIMER_APP_SOSTM_MODE_CONTINUOUS (0x00000000)
51#define U300_TIMER_APP_SOSTM_MODE_ONE_SHOT (0x00000001)
52
53#define U300_TIMER_APP_OSTS (0x0010)
54#define U300_TIMER_APP_OSTS_TIMER_STATE_MASK (0x0000000F)
55#define U300_TIMER_APP_OSTS_TIMER_STATE_IDLE (0x00000001)
56#define U300_TIMER_APP_OSTS_TIMER_STATE_ACTIVE (0x00000002)
57#define U300_TIMER_APP_OSTS_ENABLE_IND (0x00000010)
58#define U300_TIMER_APP_OSTS_MODE_MASK (0x00000020)
59#define U300_TIMER_APP_OSTS_MODE_CONTINUOUS (0x00000000)
60#define U300_TIMER_APP_OSTS_MODE_ONE_SHOT (0x00000020)
61#define U300_TIMER_APP_OSTS_IRQ_ENABLED_IND (0x00000040)
62#define U300_TIMER_APP_OSTS_IRQ_PENDING_IND (0x00000080)
63
64#define U300_TIMER_APP_OSTCC (0x0014)
65
66#define U300_TIMER_APP_OSTTC (0x0018)
67
68#define U300_TIMER_APP_OSTIE (0x001c)
69#define U300_TIMER_APP_OSTIE_IRQ_DISABLE (0x00000000)
70#define U300_TIMER_APP_OSTIE_IRQ_ENABLE (0x00000001)
71
72#define U300_TIMER_APP_OSTIA (0x0020)
73#define U300_TIMER_APP_OSTIA_IRQ_ACK (0x00000080)
74
75
76#define U300_TIMER_APP_RDDT (0x0040)
77#define U300_TIMER_APP_RDDT_TIMER_RESET (0x00000000)
78
79#define U300_TIMER_APP_EDDT (0x0044)
80#define U300_TIMER_APP_EDDT_TIMER_ENABLE (0x00000000)
81
82#define U300_TIMER_APP_DDDT (0x0048)
83#define U300_TIMER_APP_DDDT_TIMER_DISABLE (0x00000000)
84
85#define U300_TIMER_APP_SDDTM (0x004c)
86#define U300_TIMER_APP_SDDTM_MODE_CONTINUOUS (0x00000000)
87#define U300_TIMER_APP_SDDTM_MODE_ONE_SHOT (0x00000001)
88
89#define U300_TIMER_APP_DDTS (0x0050)
90#define U300_TIMER_APP_DDTS_TIMER_STATE_MASK (0x0000000F)
91#define U300_TIMER_APP_DDTS_TIMER_STATE_IDLE (0x00000001)
92#define U300_TIMER_APP_DDTS_TIMER_STATE_ACTIVE (0x00000002)
93#define U300_TIMER_APP_DDTS_ENABLE_IND (0x00000010)
94#define U300_TIMER_APP_DDTS_MODE_MASK (0x00000020)
95#define U300_TIMER_APP_DDTS_MODE_CONTINUOUS (0x00000000)
96#define U300_TIMER_APP_DDTS_MODE_ONE_SHOT (0x00000020)
97#define U300_TIMER_APP_DDTS_IRQ_ENABLED_IND (0x00000040)
98#define U300_TIMER_APP_DDTS_IRQ_PENDING_IND (0x00000080)
99
100#define U300_TIMER_APP_DDTCC (0x0054)
101
102#define U300_TIMER_APP_DDTTC (0x0058)
103
104#define U300_TIMER_APP_DDTIE (0x005c)
105#define U300_TIMER_APP_DDTIE_IRQ_DISABLE (0x00000000)
106#define U300_TIMER_APP_DDTIE_IRQ_ENABLE (0x00000001)
107
108#define U300_TIMER_APP_DDTIA (0x0060)
109#define U300_TIMER_APP_DDTIA_IRQ_ACK (0x00000080)
110
111
112#define U300_TIMER_APP_RGPT1 (0x0080)
113#define U300_TIMER_APP_RGPT1_TIMER_RESET (0x00000000)
114
115#define U300_TIMER_APP_EGPT1 (0x0084)
116#define U300_TIMER_APP_EGPT1_TIMER_ENABLE (0x00000000)
117
118#define U300_TIMER_APP_DGPT1 (0x0088)
119#define U300_TIMER_APP_DGPT1_TIMER_DISABLE (0x00000000)
120
121#define U300_TIMER_APP_SGPT1M (0x008c)
122#define U300_TIMER_APP_SGPT1M_MODE_CONTINUOUS (0x00000000)
123#define U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT (0x00000001)
124
125#define U300_TIMER_APP_GPT1S (0x0090)
126#define U300_TIMER_APP_GPT1S_TIMER_STATE_MASK (0x0000000F)
127#define U300_TIMER_APP_GPT1S_TIMER_STATE_IDLE (0x00000001)
128#define U300_TIMER_APP_GPT1S_TIMER_STATE_ACTIVE (0x00000002)
129#define U300_TIMER_APP_GPT1S_ENABLE_IND (0x00000010)
130#define U300_TIMER_APP_GPT1S_MODE_MASK (0x00000020)
131#define U300_TIMER_APP_GPT1S_MODE_CONTINUOUS (0x00000000)
132#define U300_TIMER_APP_GPT1S_MODE_ONE_SHOT (0x00000020)
133#define U300_TIMER_APP_GPT1S_IRQ_ENABLED_IND (0x00000040)
134#define U300_TIMER_APP_GPT1S_IRQ_PENDING_IND (0x00000080)
135
136#define U300_TIMER_APP_GPT1CC (0x0094)
137
138#define U300_TIMER_APP_GPT1TC (0x0098)
139
140#define U300_TIMER_APP_GPT1IE (0x009c)
141#define U300_TIMER_APP_GPT1IE_IRQ_DISABLE (0x00000000)
142#define U300_TIMER_APP_GPT1IE_IRQ_ENABLE (0x00000001)
143
144#define U300_TIMER_APP_GPT1IA (0x00a0)
145#define U300_TIMER_APP_GPT1IA_IRQ_ACK (0x00000080)
146
147
148#define U300_TIMER_APP_RGPT2 (0x00c0)
149#define U300_TIMER_APP_RGPT2_TIMER_RESET (0x00000000)
150
151#define U300_TIMER_APP_EGPT2 (0x00c4)
152#define U300_TIMER_APP_EGPT2_TIMER_ENABLE (0x00000000)
153
154#define U300_TIMER_APP_DGPT2 (0x00c8)
155#define U300_TIMER_APP_DGPT2_TIMER_DISABLE (0x00000000)
156
157#define U300_TIMER_APP_SGPT2M (0x00cc)
158#define U300_TIMER_APP_SGPT2M_MODE_CONTINUOUS (0x00000000)
159#define U300_TIMER_APP_SGPT2M_MODE_ONE_SHOT (0x00000001)
160
161#define U300_TIMER_APP_GPT2S (0x00d0)
162#define U300_TIMER_APP_GPT2S_TIMER_STATE_MASK (0x0000000F)
163#define U300_TIMER_APP_GPT2S_TIMER_STATE_IDLE (0x00000001)
164#define U300_TIMER_APP_GPT2S_TIMER_STATE_ACTIVE (0x00000002)
165#define U300_TIMER_APP_GPT2S_ENABLE_IND (0x00000010)
166#define U300_TIMER_APP_GPT2S_MODE_MASK (0x00000020)
167#define U300_TIMER_APP_GPT2S_MODE_CONTINUOUS (0x00000000)
168#define U300_TIMER_APP_GPT2S_MODE_ONE_SHOT (0x00000020)
169#define U300_TIMER_APP_GPT2S_IRQ_ENABLED_IND (0x00000040)
170#define U300_TIMER_APP_GPT2S_IRQ_PENDING_IND (0x00000080)
171
172#define U300_TIMER_APP_GPT2CC (0x00d4)
173
174#define U300_TIMER_APP_GPT2TC (0x00d8)
175
176#define U300_TIMER_APP_GPT2IE (0x00dc)
177#define U300_TIMER_APP_GPT2IE_IRQ_DISABLE (0x00000000)
178#define U300_TIMER_APP_GPT2IE_IRQ_ENABLE (0x00000001)
179
180#define U300_TIMER_APP_GPT2IA (0x00e0)
181#define U300_TIMER_APP_GPT2IA_IRQ_ACK (0x00000080)
182
183
184#define U300_TIMER_APP_CRC (0x100)
185#define U300_TIMER_APP_CRC_CLOCK_REQUEST_ENABLE (0x00000001)
186
187#define TICKS_PER_JIFFY ((CLOCK_TICK_RATE + (HZ/2)) / HZ)
188#define US_PER_TICK ((1000000 + (HZ/2)) / HZ)
189
190static void __iomem *u300_timer_base;
191
192
193
194
195
196
197static void u300_set_mode(enum clock_event_mode mode,
198 struct clock_event_device *evt)
199{
200 switch (mode) {
201 case CLOCK_EVT_MODE_PERIODIC:
202
203 writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
204 u300_timer_base + U300_TIMER_APP_GPT1IE);
205
206 writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
207 u300_timer_base + U300_TIMER_APP_DGPT1);
208
209
210
211
212 writel(TICKS_PER_JIFFY,
213 u300_timer_base + U300_TIMER_APP_GPT1TC);
214
215
216
217
218 writel(U300_TIMER_APP_SGPT1M_MODE_CONTINUOUS,
219 u300_timer_base + U300_TIMER_APP_SGPT1M);
220
221 writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
222 u300_timer_base + U300_TIMER_APP_GPT1IE);
223
224 writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
225 u300_timer_base + U300_TIMER_APP_EGPT1);
226 break;
227 case CLOCK_EVT_MODE_ONESHOT:
228
229
230
231
232
233
234
235 writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
236 u300_timer_base + U300_TIMER_APP_GPT1IE);
237
238 writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
239 u300_timer_base + U300_TIMER_APP_DGPT1);
240
241
242
243
244 writel(0xFFFFFFFF, u300_timer_base + U300_TIMER_APP_GPT1TC);
245
246 writel(U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT,
247 u300_timer_base + U300_TIMER_APP_SGPT1M);
248
249 writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
250 u300_timer_base + U300_TIMER_APP_GPT1IE);
251
252 writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
253 u300_timer_base + U300_TIMER_APP_EGPT1);
254 break;
255 case CLOCK_EVT_MODE_UNUSED:
256 case CLOCK_EVT_MODE_SHUTDOWN:
257
258 writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
259 u300_timer_base + U300_TIMER_APP_GPT1IE);
260
261 writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
262 u300_timer_base + U300_TIMER_APP_DGPT1);
263 break;
264 case CLOCK_EVT_MODE_RESUME:
265
266 break;
267 }
268}
269
270
271
272
273
274
275
276
277
278static int u300_set_next_event(unsigned long cycles,
279 struct clock_event_device *evt)
280
281{
282
283 writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
284 u300_timer_base + U300_TIMER_APP_GPT1IE);
285
286 writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
287 u300_timer_base + U300_TIMER_APP_DGPT1);
288
289 writel(U300_TIMER_APP_RGPT1_TIMER_RESET,
290 u300_timer_base + U300_TIMER_APP_RGPT1);
291
292 writel(cycles, u300_timer_base + U300_TIMER_APP_GPT1TC);
293
294
295
296
297 writel(U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT,
298 u300_timer_base + U300_TIMER_APP_SGPT1M);
299
300 writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
301 u300_timer_base + U300_TIMER_APP_GPT1IE);
302
303 writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
304 u300_timer_base + U300_TIMER_APP_EGPT1);
305 return 0;
306}
307
308
309
310static struct clock_event_device clockevent_u300_1mhz = {
311 .name = "GPT1",
312 .rating = 300,
313 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
314 .set_next_event = u300_set_next_event,
315 .set_mode = u300_set_mode,
316};
317
318
319static irqreturn_t u300_timer_interrupt(int irq, void *dev_id)
320{
321 struct clock_event_device *evt = &clockevent_u300_1mhz;
322
323
324 writel(U300_TIMER_APP_GPT1IA_IRQ_ACK,
325 u300_timer_base + U300_TIMER_APP_GPT1IA);
326 evt->event_handler(evt);
327 return IRQ_HANDLED;
328}
329
330static struct irqaction u300_timer_irq = {
331 .name = "U300 Timer Tick",
332 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
333 .handler = u300_timer_interrupt,
334};
335
336
337
338
339
340
341
342
343
344static u32 notrace u300_read_sched_clock(void)
345{
346 return readl(u300_timer_base + U300_TIMER_APP_GPT2CC);
347}
348
349static unsigned long u300_read_current_timer(void)
350{
351 return readl(u300_timer_base + U300_TIMER_APP_GPT2CC);
352}
353
354static struct delay_timer u300_delay_timer;
355
356
357
358
359static void __init u300_timer_init_of(struct device_node *np)
360{
361 struct resource irq_res;
362 int irq;
363 struct clk *clk;
364 unsigned long rate;
365
366 u300_timer_base = of_iomap(np, 0);
367 if (!u300_timer_base)
368 panic("could not ioremap system timer\n");
369
370
371 irq = of_irq_to_resource(np, 2, &irq_res);
372 if (irq <= 0)
373 panic("no IRQ for system timer\n");
374
375 pr_info("U300 GP1 timer @ base: %p, IRQ: %d\n", u300_timer_base, irq);
376
377
378 clk = of_clk_get(np, 0);
379 BUG_ON(IS_ERR(clk));
380 clk_prepare_enable(clk);
381 rate = clk_get_rate(clk);
382
383 setup_sched_clock(u300_read_sched_clock, 32, rate);
384
385 u300_delay_timer.read_current_timer = &u300_read_current_timer;
386 u300_delay_timer.freq = rate;
387 register_current_timer_delay(&u300_delay_timer);
388
389
390
391
392
393 writel(U300_TIMER_APP_CRC_CLOCK_REQUEST_ENABLE,
394 u300_timer_base + U300_TIMER_APP_CRC);
395 writel(U300_TIMER_APP_ROST_TIMER_RESET,
396 u300_timer_base + U300_TIMER_APP_ROST);
397 writel(U300_TIMER_APP_DOST_TIMER_DISABLE,
398 u300_timer_base + U300_TIMER_APP_DOST);
399 writel(U300_TIMER_APP_RDDT_TIMER_RESET,
400 u300_timer_base + U300_TIMER_APP_RDDT);
401 writel(U300_TIMER_APP_DDDT_TIMER_DISABLE,
402 u300_timer_base + U300_TIMER_APP_DDDT);
403
404
405 writel(U300_TIMER_APP_RGPT1_TIMER_RESET,
406 u300_timer_base + U300_TIMER_APP_RGPT1);
407
408
409 setup_irq(irq, &u300_timer_irq);
410
411
412 writel(U300_TIMER_APP_RGPT2_TIMER_RESET,
413 u300_timer_base + U300_TIMER_APP_RGPT2);
414
415 writel(0xFFFFFFFFU, u300_timer_base + U300_TIMER_APP_GPT2TC);
416
417 writel(U300_TIMER_APP_SGPT2M_MODE_CONTINUOUS,
418 u300_timer_base + U300_TIMER_APP_SGPT2M);
419
420 writel(U300_TIMER_APP_GPT2IE_IRQ_DISABLE,
421 u300_timer_base + U300_TIMER_APP_GPT2IE);
422
423 writel(U300_TIMER_APP_EGPT2_TIMER_ENABLE,
424 u300_timer_base + U300_TIMER_APP_EGPT2);
425
426
427 if (clocksource_mmio_init(u300_timer_base + U300_TIMER_APP_GPT2CC,
428 "GPT2", rate, 300, 32, clocksource_mmio_readl_up))
429 pr_err("timer: failed to initialize U300 clock source\n");
430
431
432 clockevents_config_and_register(&clockevent_u300_1mhz, rate,
433 1, 0xffffffff);
434
435
436
437
438
439}
440
441CLOCKSOURCE_OF_DECLARE(u300_timer, "stericsson,u300-apptimer",
442 u300_timer_init_of);
443