blob: 5e09e08de1420498ffd6e9970348df61ea27e7ca [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * linux/arch/arm/mach-zx297510/timer.c
3 *
4 * Copyright (C) 2013 ZTE-TSP
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/kernel.h>
18#include <linux/interrupt.h>
19#include <linux/irq.h>
20#include <linux/clockchips.h>
21#include <linux/clk.h>
22#include <linux/module.h>
23#include <linux/err.h>
24#include <linux/syscore_ops.h>
25
26#include <asm/mach/time.h>
27#include <asm/sched_clock.h>
28
29#include <mach/timex.h>
30#include <mach/clock.h>
31#include <mach/iomap.h>
32#include <mach/debug.h>
33#include <mach/irqs.h>
34#include <mach/pcu.h>
35#include <mach/spinlock.h>
36
37
38#ifdef CONFIG_ARCH_ZX297510FPGA
39#define SOURCE_CLOCK_RATE (32768) /*timer 1 32.768K*/
40#else /*evb, ufi, dc, cpe*/
41#define SOURCE_CLOCK_RATE (1000000) /*timer 1 26M/13/2*/
42#endif
43
44#define EVENT_CLOCK_RATE (32768) /*timer 3 32.768K*/
45/*#define EVENT_CLOCK_RATE 1000000*/ /*timer from 104M*/
46
47#define CLOCKSOURCE_NAME "zx297510_timer1"
48#define CLOCKEVENT_NAME "zx297510_timer3"
49
50/* timer register offset */
51
52#define CONFIG_REG (0x04)
53#define LOAD_REG (0x08)
54#define START_REG (0x0C)
55#define REFRESH_REG (0x10)
56#define CUR_VALUE (0x18)
57
58
59#define ZX29_TIMER1_VA (ZX297510_TIMER1_BASE - ZX297510_A2LSP_BASE + ZX29_A2LSP_VA)
60#define ZX29_TIMER3_VA (ZX297510_TIMER3_BASE - ZX297510_A1_BASE + ZX29_A1_VA)
61
62#define SOURCE_BASE_VA ZX29_TIMER1_VA
63#define EVENT_BASE_VA ZX29_TIMER3_VA
64
65
66//#define DEBUG_SYS_TIMER
67
68#ifdef DEBUG_SYS_TIMER
69#pragma GCC optimize("O0")
70void zx29_gpio1v8_set_direction(unsigned int pin_num, unsigned int value);
71void zx29_gpio1v8_output_data(unsigned int pin_num, unsigned int value);
72void zx29_gpio1v8_function_sel(unsigned int pin_num, unsigned int value);
73
74typedef struct
75{
76 unsigned int trace_t;
77 unsigned int count_reg;
78 unsigned int load_reg;
79 unsigned int refresh_reg;
80 unsigned int time_t;
81}test_trace;
82
83volatile test_trace oneshot_count_view[1000];
84volatile unsigned int oneshot_count_index=0;
85
86volatile unsigned int timer_int_stamp[1000];
87volatile unsigned int timer_int_index=0;
88#endif
89
90static struct clock_event_device timer_clkevt;
91
92volatile static unsigned int sys_time_cnt=0;
93volatile static unsigned int periodic_count_value=0;
94volatile static unsigned int sys_time_count_value=0;
95volatile static unsigned int event_cycle=0; /*it is for translating presistent time*/
96static struct timespec persistent_ts;
97
98
99/*
100 * this function can only just be used for debug
101 */
102unsigned int test_timer_read(void)
103{
104 unsigned int t=0;
105
106 t = ioread32(SOURCE_BASE_VA+CUR_VALUE);
107 sys_time_count_value=0x7fffffff-t;
108 return sys_time_count_value;
109}
110EXPORT_SYMBOL(test_timer_read);
111
112/*
113 *refresh configure register
114 */
115inline void refresh_config_reg(const void __iomem *addr)
116{
117 unsigned int tmp=0;
118
119 tmp=ioread32(addr+REFRESH_REG);
120 tmp ^=0xC;
121 iowrite32(tmp,addr+REFRESH_REG);
122}
123
124/*
125 *refresh load register
126 */
127inline void refresh_load_reg(const void __iomem *addr)
128{
129 unsigned int tmp=0;
130
131 tmp=ioread32(addr+REFRESH_REG);
132 tmp ^= 0x3;
133 iowrite32(tmp,addr+REFRESH_REG);
134}
135
136/*
137 *refresh load register and configure register
138 */
139inline void refresh_config_load_reg(const void __iomem *addr)
140{
141 unsigned int tmp=0;
142
143 tmp=ioread32(addr+REFRESH_REG);
144 tmp ^= 0xf;
145 iowrite32(tmp,addr+REFRESH_REG);
146
147}
148
149/*
150 * read_persistent_clock - Return time from a persistent clock.
151 *
152 * Reads the time from a source which isn't disabled during PM, the
153 * 32k sync timer. Convert the cycles elapsed since last read into
154 * nsecs and adds to a monotonically increasing timespec.
155 *
156 * Generally, RTC is used to get this time,but in zx297510 platform,
157 * precision of RTC is not enough, so I use timer3(clockevent) as
158 * persistent clock.Although timer3 can not represent the natural time,
159 * but it doesn't matter, linux just needs a relative time.
160 */
161void read_persistent_clock(struct timespec *ts)
162{
163 unsigned int current_cycle1 = 0;
164 unsigned int current_cycle2 = 0;
165 unsigned long long delta;
166 unsigned long long ns;
167 struct timespec *tsp = &persistent_ts;
168
169 /*event clock is to slow, so we need to get the newest time*/
170 current_cycle1 = ioread32(EVENT_BASE_VA + CUR_VALUE);
171
172 if (current_cycle1 == 0xffffffff){ /*this function can be invoked before event timer initiated*/
173 ts->tv_nsec = 0;
174 ts->tv_sec = 0;
175 return;
176 }
177
178 do{
179 current_cycle2 = ioread32(EVENT_BASE_VA + CUR_VALUE);
180 if (current_cycle1 == current_cycle2)
181 break;
182 current_cycle1 = current_cycle2;
183 }while(1);
184
185 delta = event_cycle - current_cycle1;
186 ns = delta * (u64)1000000000; /*delta * 10^9 / 32768*/
187 ns >>= 15;
188
189 tsp->tv_sec = 0;
190 tsp->tv_nsec = 0;
191 timespec_add_ns(tsp, ns); /*convert ns to struct timespec*/
192
193 *ts = *tsp;
194
195}
196
197
198/*
199 * IRQ handler for the timer.
200 */
201static irqreturn_t zx297510_timer_interrupt(int irq, void *dev_id)
202{
203 unsigned int tmp=0;
204
205 WARN_ON_ONCE(!irqs_disabled());
206
207 reg_spin_lock();
208 tmp=ioread32(TIMER_INT_DDR_SW_CLEAR_REG); /*clear clock event timer status in pcu*/
209 tmp |=0x2;
210 iowrite32(tmp,TIMER_INT_DDR_SW_CLEAR_REG);
211 reg_spin_unlock();
212
213#ifdef DEBUG_SYS_TIMER
214 zx29_gpio1v8_output_data(16,(sys_time_cnt & 0x01)); /*light a led to test systimer*/
215 timer_int_stamp[timer_int_index]= test_timer_read();
216 timer_int_index++;
217 if(timer_int_index==1000)
218 timer_int_index=0;
219#endif
220
221 sys_time_cnt++;
222
223 timer_clkevt.event_handler(&timer_clkevt);
224
225 return IRQ_HANDLED;
226}
227
228static struct irqaction zx297510_timer_irq = {
229 .name = "zx297510_tick",
230 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
231 .handler = zx297510_timer_interrupt,
232};
233
234
235/*
236 * Clockevent device: interrupts every 1/HZ
237 */
238static void timer_set_mode(enum clock_event_mode mode, struct clock_event_device *dev)
239{
240 unsigned int tmp=0;
241
242 switch (mode) {
243 case CLOCK_EVT_MODE_PERIODIC:
244 iowrite32(periodic_count_value-2, EVENT_BASE_VA+LOAD_REG); /*set cycle value*/
245 tmp=ioread32(EVENT_BASE_VA+CONFIG_REG);
246 tmp|=0x2;
247 iowrite32(tmp, EVENT_BASE_VA+CONFIG_REG); /* auto reload*/
248 refresh_config_load_reg(EVENT_BASE_VA);
249 //iowrite32(0x1,EVENT_BASE_VA+START_REG); /*start timer */
250 break;
251
252 case CLOCK_EVT_MODE_ONESHOT:
253 //iowrite32(0x0,EVENT_BASE_VA+START_REG); /*stop timer */
254 tmp=ioread32(EVENT_BASE_VA+CONFIG_REG);
255 tmp &= ~0x2; /*disable auto reload*/
256 iowrite32(tmp, EVENT_BASE_VA+CONFIG_REG);
257 refresh_config_reg(EVENT_BASE_VA);
258 break;
259
260 case CLOCK_EVT_MODE_SHUTDOWN:
261 case CLOCK_EVT_MODE_UNUSED:
262 case CLOCK_EVT_MODE_RESUME:
263 break;
264 }
265}
266
267/*
268 * only for oneshot mode
269 */
270static int timer_set_next_event(unsigned long cycles,
271 struct clock_event_device *evt)
272{
273 unsigned int t0;
274
275 event_cycle = cycles-2; /*used by read_persistent_clock()*/
276
277 iowrite32(cycles-2, EVENT_BASE_VA+LOAD_REG);
278
279 refresh_load_reg(EVENT_BASE_VA);
280 t0=test_timer_read();
281 while( (test_timer_read()-t0) < 31);
282 /*
283 *wait for 2-3 work clock cycles to finish refresh
284 *when work clock is 32K, refresh time is too long
285 * don't stop timer , change load_reg and refresh directly
286 * start timer don't need too, in order to save time
287 */
288 //while(ioread32(EVENT_BASE_VA+CUR_VALUE) != (cycles-2));
289
290 //iowrite32(0x1,EVENT_BASE_VA+START_REG); /*start timer */
291
292#ifdef DEBUG_SYS_TIMER
293 oneshot_count_view[oneshot_count_index].trace_t = cycles;
294 oneshot_count_view[oneshot_count_index].count_reg = ioread32(EVENT_BASE_VA+CUR_VALUE);
295 oneshot_count_view[oneshot_count_index].load_reg = ioread32(EVENT_BASE_VA+LOAD_REG);
296 oneshot_count_view[oneshot_count_index].refresh_reg = ioread32(EVENT_BASE_VA+REFRESH_REG);
297 oneshot_count_view[oneshot_count_index].time_t = test_timer_read();
298 oneshot_count_index++;
299 if(oneshot_count_index==1000)
300 oneshot_count_index=0;
301#endif
302
303 return 0;
304}
305
306#ifdef CONFIG_ARCH_ZX297510FPGA
307static cycle_t read_timer_clk(struct clocksource *cs)
308{
309 u32 elapsed=0;
310 u32 t1=0,t2=0;
311
312 t1 = ioread32(SOURCE_BASE_VA+CUR_VALUE);
313 do{
314 t2 = ioread32(SOURCE_BASE_VA+CUR_VALUE);
315 if(t1==t2)
316 break;
317 t1=t2;
318 }while(1);
319
320 elapsed=0x7fffffff-t1;
321
322 return elapsed;
323}
324
325#else
326static cycle_t read_timer_clk(struct clocksource *cs)
327{
328 u32 elapsed=0;
329 u32 t=0;
330 t = ioread32(SOURCE_BASE_VA+CUR_VALUE);
331 elapsed=0x7fffffff-t;
332
333 return elapsed;
334}
335#endif
336
337#ifdef CONFIG_PM
338int zx29_clocksource_suspend(void)
339{
340 /*
341 *nothing should be done here.
342 *maybe clocksource should be stoped, but I don't do this
343 *there is no effect on system running, even if clocksource is not suspended actually.
344 *I can use clocksource in power off processing for debug.
345 */
346 return 0;
347}
348
349void zx29_clocksource_resume(void)
350{
351 /*re-initiate the clocksource*/
352 iowrite32(0x7fffffff,SOURCE_BASE_VA+LOAD_REG); // 2^31-1
353#ifdef CONFIG_ARCH_ZX297510FPGA
354 iowrite32(0x02,SOURCE_BASE_VA+CONFIG_REG); // auto load
355#else
356 iowrite32(0x22,SOURCE_BASE_VA+CONFIG_REG); // main clock/13/2 , auto load
357 refresh_config_load_reg(SOURCE_BASE_VA); // refresh load reg and config reg
358 iowrite32(0x1,SOURCE_BASE_VA+START_REG); // start timer
359#endif
360}
361
362static struct syscore_ops zx29_clocksource_syscore_ops = {
363 .suspend = zx29_clocksource_suspend,
364 .resume = zx29_clocksource_resume,
365};
366
367#endif
368
369
370static struct clocksource timer_clksrc = {
371 .name = CLOCKSOURCE_NAME,
372 .rating = 300,
373 .read = read_timer_clk,
374 .mask = CLOCKSOURCE_MASK(31),
375 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
376};
377
378static struct clock_event_device timer_clkevt = {
379 .name = CLOCKEVENT_NAME,
380#ifdef CONFIG_ARCH_ZX297510FPGA
381 .features = CLOCK_EVT_FEAT_PERIODIC, /*FPGA is too slow to process hrtimer*/
382#else
383 .features = CLOCK_EVT_FEAT_PERIODIC |CLOCK_EVT_FEAT_ONESHOT,
384#endif
385 .shift = 32,
386 .rating = 200,
387 .set_mode = timer_set_mode,
388 .set_next_event = timer_set_next_event,
389};
390
391static notrace u32 zx29_sched_clock_read(void)
392{
393 return (u32)timer_clksrc.read(&timer_clksrc);
394}
395
396/*
397 * Set up both clocksource and clockevent support.
398 */
399static void __init zx297510_timer_init(void)
400{
401 struct clk *pclk;
402 int ret=0;
403 unsigned int tmp=0;
404
405#ifdef DEBUG_SYS_TIMER
406 zx29_gpio1v8_function_sel(16,0); /*GPIO*/
407 zx29_gpio1v8_set_direction(16,0); /*output*/
408#endif
409
410
411 /************set clock source ***************/
412#if 0
413 tmp=ioread32(Zx29_LSPCPRM_VA+0x24);
414 tmp &=~0xf00000;
415 tmp |= 12<<20;
416 iowrite32(tmp,Zx29_LSPCPRM_VA+0x24); //main clock/13
417
418 tmp=ioread32(Zx29_LSPCPRM_VA);
419 tmp &= ~(0x1<<5);
420 iowrite32(tmp,Zx29_LSPCPRM_VA); //select main lock
421
422 tmp=ioread32(Zx29_LSPCPRM_VA+0x2c);
423 tmp &= ~(0x1<<14);
424 iowrite32(tmp,Zx29_LSPCPRM_VA+0x2c); //enbale timer1 clock
425#else
426 pclk=clk_get_sys(CLOCKSOURCE_NAME, "work_clk");
427 if (IS_ERR(pclk))
428 ZDRV_ASSERT(0);
429 ret=clk_set_rate(pclk,2000000); /*main clock/13*/
430 if(ret)
431 ZDRV_ASSERT(0);
432 clk_enable(pclk);
433#endif
434 iowrite32(0x7fffffff,SOURCE_BASE_VA+LOAD_REG); // 2^31-1
435
436#ifdef CONFIG_ARCH_ZX297510FPGA
437 iowrite32(0x02,SOURCE_BASE_VA+CONFIG_REG); // auto load
438#else
439 iowrite32(0x22,SOURCE_BASE_VA+CONFIG_REG); //main clock/13/2 , auto load
440#endif
441 refresh_config_load_reg(SOURCE_BASE_VA); // refresh load reg and config reg
442
443 iowrite32(0x1,SOURCE_BASE_VA+START_REG); //start timer
444
445 /* Register clocksource. */
446 ret = clocksource_register_hz(&timer_clksrc, SOURCE_CLOCK_RATE);
447 if (ret){
448 printk(KERN_INFO"clocksource_register failed\n");
449 ZDRV_ASSERT(0);
450 }
451
452 /*register hardware time stamp*/
453#ifdef CONFIG_PM
454 setup_sched_clock_needs_suspend(zx29_sched_clock_read, 31, SOURCE_CLOCK_RATE);
455#else
456 setup_sched_clock(zx29_sched_clock_read, 31, SOURCE_CLOCK_RATE);
457#endif
458
459 /************** set clock event ***************/
460#if 0
461 tmp=ioread32(ZX29_A1CRM_VA);
462 tmp &= ~((0x1<<15)|(0xf<<8));
463 tmp |= (0x1<<15)|(0x1<<8);
464 iowrite32(tmp,ZX29_A1CRM_VA); //select 32K, div 2 32K/2
465
466 tmp=ioread32(ZX29_A1CRM_VA+0x0c);
467 tmp |= (0x1<<7);
468 iowrite32(tmp,ZX29_A1CRM_VA+0xc); //timer1 pclk ungate
469
470 tmp=ioread32(ZX29_A1CRM_VA+0x10);
471 tmp |= (0x1<<7);
472 iowrite32(tmp,ZX29_A1CRM_VA+0x10); //timer1 wclk ungate
473#else
474 pclk=clk_get_sys(CLOCKEVENT_NAME, "apb_clk");
475 if (IS_ERR(pclk))
476 ZDRV_ASSERT(0);
477 clk_enable(pclk);
478
479 pclk=clk_get_sys(CLOCKEVENT_NAME, "work_clk");
480 if (IS_ERR(pclk))
481 ZDRV_ASSERT(0);
482
483 /* select timer work clocksource at 32KHz */
484 ret=clk_set_rate(pclk,32768);
485 //ret=clk_set_rate(pclk,8000000); //for 104M
486 if(ret)
487 ZDRV_ASSERT(0);
488
489 /* enable timer work clock */
490 clk_enable(pclk);
491 //iowrite32(0x60,EVENT_BASE_VA+CONFIG_REG); // wclk/13 for 104M
492 //refresh_config_reg(EVENT_BASE_VA);
493#endif
494 /* set count value */
495 periodic_count_value=(EVENT_CLOCK_RATE+HZ/2)/HZ;
496
497 /*
498 *don't need do below here, timer_set_mode() will do these
499 */
500
501 /*iowrite32(periodic_count_value-1, EVENT_BASE_VA+LOAD_REG);
502 iowrite32(0x02,EVENT_BASE_VA+CONFIG_REG); // 32K/1 auto load
503 refresh_config_load_reg(EVENT_BASE_VA); // refresh load reg and config reg
504 */
505 reg_spin_lock();
506 tmp=ioread32(TIMER_INT_TYPE_REG); /*indicate timer3 level type for pcu: pulse*/
507 tmp &=~0x2;
508 iowrite32(tmp,TIMER_INT_TYPE_REG);
509
510 tmp=ioread32(TIMER_INT_DDR_SW_CLEAR_REG); /*clear clock event timer status in pcu*/
511 tmp |=0x2;
512 iowrite32(tmp,TIMER_INT_DDR_SW_CLEAR_REG);
513 reg_spin_unlock();
514
515 /* Set up irq handler */
516 ret=setup_irq(TIMER3_INT, &zx297510_timer_irq);
517 if(ret<0)
518 ZDRV_ASSERT(0);
519
520 /* Set up and register clockevents */
521 timer_clkevt.cpumask = cpumask_of(0);
522 clockevents_config_and_register(&timer_clkevt,EVENT_CLOCK_RATE,32,0x7fffffff);
523
524 iowrite32(0x1,EVENT_BASE_VA+START_REG); /*start timer */
525
526#ifdef CONFIG_PM
527 register_syscore_ops(&zx29_clocksource_syscore_ops);
528#endif
529
530 printk(KERN_INFO "ZTE-TSP zx297510 system timer initialized\n");
531}
532
533
534struct sys_timer zx297510_timer = {
535 .init = zx297510_timer_init,
536};