2 * Based on arm clockevents implementation and old bfin time tick.
4 * Copyright 2008-2009 Analog Devics Inc.
8 * Licensed under the GPL-2
11 #include <linux/module.h>
12 #include <linux/profile.h>
13 #include <linux/interrupt.h>
14 #include <linux/time.h>
15 #include <linux/timex.h>
16 #include <linux/irq.h>
17 #include <linux/clocksource.h>
18 #include <linux/clockchips.h>
19 #include <linux/cpufreq.h>
21 #include <asm/blackfin.h>
23 #include <asm/gptimers.h>
27 #if defined(CONFIG_CYCLES_CLOCKSOURCE)
29 static notrace u64
bfin_read_cycles(struct clocksource
*cs
)
31 #ifdef CONFIG_CPU_FREQ
32 return __bfin_cycles_off
+ (get_cycles() << __bfin_cycles_mod
);
38 static struct clocksource bfin_cs_cycles
= {
39 .name
= "bfin_cs_cycles",
41 .read
= bfin_read_cycles
,
42 .mask
= CLOCKSOURCE_MASK(64),
43 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
46 static inline unsigned long long bfin_cs_cycles_sched_clock(void)
48 return clocksource_cyc2ns(bfin_read_cycles(&bfin_cs_cycles
),
49 bfin_cs_cycles
.mult
, bfin_cs_cycles
.shift
);
52 static int __init
bfin_cs_cycles_init(void)
54 if (clocksource_register_hz(&bfin_cs_cycles
, get_cclk()))
55 panic("failed to register clocksource");
60 # define bfin_cs_cycles_init()
63 #ifdef CONFIG_GPTMR0_CLOCKSOURCE
65 void __init
setup_gptimer0(void)
67 disable_gptimers(TIMER0bit
);
70 bfin_write16(TIMER_DATA_IMSK
, 0);
71 set_gptimer_config(TIMER0_id
, TIMER_OUT_DIS
72 | TIMER_MODE_PWM_CONT
| TIMER_PULSE_HI
| TIMER_IRQ_PER
);
74 set_gptimer_config(TIMER0_id
, \
75 TIMER_OUT_DIS
| TIMER_PERIOD_CNT
| TIMER_MODE_PWM
);
77 set_gptimer_period(TIMER0_id
, -1);
78 set_gptimer_pwidth(TIMER0_id
, -2);
80 enable_gptimers(TIMER0bit
);
83 static u64
bfin_read_gptimer0(struct clocksource
*cs
)
85 return bfin_read_TIMER0_COUNTER();
88 static struct clocksource bfin_cs_gptimer0
= {
89 .name
= "bfin_cs_gptimer0",
91 .read
= bfin_read_gptimer0
,
92 .mask
= CLOCKSOURCE_MASK(32),
93 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
96 static inline unsigned long long bfin_cs_gptimer0_sched_clock(void)
98 return clocksource_cyc2ns(bfin_read_TIMER0_COUNTER(),
99 bfin_cs_gptimer0
.mult
, bfin_cs_gptimer0
.shift
);
102 static int __init
bfin_cs_gptimer0_init(void)
106 if (clocksource_register_hz(&bfin_cs_gptimer0
, get_sclk()))
107 panic("failed to register clocksource");
112 # define bfin_cs_gptimer0_init()
115 #if defined(CONFIG_GPTMR0_CLOCKSOURCE) || defined(CONFIG_CYCLES_CLOCKSOURCE)
116 /* prefer to use cycles since it has higher rating */
117 notrace
unsigned long long sched_clock(void)
119 #if defined(CONFIG_CYCLES_CLOCKSOURCE)
120 return bfin_cs_cycles_sched_clock();
122 return bfin_cs_gptimer0_sched_clock();
127 #if defined(CONFIG_TICKSOURCE_GPTMR0)
128 static int bfin_gptmr0_set_next_event(unsigned long cycles
,
129 struct clock_event_device
*evt
)
131 disable_gptimers(TIMER0bit
);
133 /* it starts counting three SCLK cycles after the TIMENx bit is set */
134 set_gptimer_pwidth(TIMER0_id
, cycles
- 3);
135 enable_gptimers(TIMER0bit
);
139 static int bfin_gptmr0_set_periodic(struct clock_event_device
*evt
)
142 set_gptimer_config(TIMER0_id
,
143 TIMER_OUT_DIS
| TIMER_IRQ_ENA
|
144 TIMER_PERIOD_CNT
| TIMER_MODE_PWM
);
146 set_gptimer_config(TIMER0_id
,
147 TIMER_OUT_DIS
| TIMER_MODE_PWM_CONT
|
148 TIMER_PULSE_HI
| TIMER_IRQ_PER
);
151 set_gptimer_period(TIMER0_id
, get_sclk() / HZ
);
152 set_gptimer_pwidth(TIMER0_id
, get_sclk() / HZ
- 1);
153 enable_gptimers(TIMER0bit
);
157 static int bfin_gptmr0_set_oneshot(struct clock_event_device
*evt
)
159 disable_gptimers(TIMER0bit
);
161 set_gptimer_config(TIMER0_id
,
162 TIMER_OUT_DIS
| TIMER_IRQ_ENA
| TIMER_MODE_PWM
);
164 set_gptimer_config(TIMER0_id
,
165 TIMER_OUT_DIS
| TIMER_MODE_PWM
| TIMER_PULSE_HI
|
169 set_gptimer_period(TIMER0_id
, 0);
173 static int bfin_gptmr0_shutdown(struct clock_event_device
*evt
)
175 disable_gptimers(TIMER0bit
);
179 static void bfin_gptmr0_ack(void)
181 clear_gptimer_intr(TIMER0_id
);
184 static void __init
bfin_gptmr0_init(void)
186 disable_gptimers(TIMER0bit
);
189 #ifdef CONFIG_CORE_TIMER_IRQ_L1
190 __attribute__((l1_text
))
192 irqreturn_t
bfin_gptmr0_interrupt(int irq
, void *dev_id
)
194 struct clock_event_device
*evt
= dev_id
;
197 * We want to ACK before we handle so that we can handle smaller timer
198 * intervals. This way if the timer expires again while we're handling
199 * things, we're more likely to see that 2nd int rather than swallowing
200 * it by ACKing the int at the end of this handler.
203 evt
->event_handler(evt
);
207 static struct irqaction gptmr0_irq
= {
208 .name
= "Blackfin GPTimer0",
209 .flags
= IRQF_TIMER
| IRQF_IRQPOLL
| IRQF_PERCPU
,
210 .handler
= bfin_gptmr0_interrupt
,
213 static struct clock_event_device clockevent_gptmr0
= {
214 .name
= "bfin_gptimer0",
218 .features
= CLOCK_EVT_FEAT_PERIODIC
|
219 CLOCK_EVT_FEAT_ONESHOT
,
220 .set_next_event
= bfin_gptmr0_set_next_event
,
221 .set_state_shutdown
= bfin_gptmr0_shutdown
,
222 .set_state_periodic
= bfin_gptmr0_set_periodic
,
223 .set_state_oneshot
= bfin_gptmr0_set_oneshot
,
226 static void __init
bfin_gptmr0_clockevent_init(struct clock_event_device
*evt
)
228 unsigned long clock_tick
;
230 clock_tick
= get_sclk();
231 evt
->mult
= div_sc(clock_tick
, NSEC_PER_SEC
, evt
->shift
);
232 evt
->max_delta_ns
= clockevent_delta2ns(-1, evt
);
233 evt
->max_delta_ticks
= (unsigned long)-1;
234 evt
->min_delta_ns
= clockevent_delta2ns(100, evt
);
235 evt
->min_delta_ticks
= 100;
237 evt
->cpumask
= cpumask_of(0);
239 clockevents_register_device(evt
);
241 #endif /* CONFIG_TICKSOURCE_GPTMR0 */
243 #if defined(CONFIG_TICKSOURCE_CORETMR)
244 /* per-cpu local core timer */
245 DEFINE_PER_CPU(struct clock_event_device
, coretmr_events
);
247 static int bfin_coretmr_set_next_event(unsigned long cycles
,
248 struct clock_event_device
*evt
)
250 bfin_write_TCNTL(TMPWR
);
252 bfin_write_TCOUNT(cycles
);
254 bfin_write_TCNTL(TMPWR
| TMREN
);
258 static int bfin_coretmr_set_periodic(struct clock_event_device
*evt
)
260 unsigned long tcount
= ((get_cclk() / (HZ
* TIME_SCALE
)) - 1);
262 bfin_write_TCNTL(TMPWR
);
264 bfin_write_TSCALE(TIME_SCALE
- 1);
265 bfin_write_TPERIOD(tcount
);
266 bfin_write_TCOUNT(tcount
);
268 bfin_write_TCNTL(TMPWR
| TMREN
| TAUTORLD
);
272 static int bfin_coretmr_set_oneshot(struct clock_event_device
*evt
)
274 bfin_write_TCNTL(TMPWR
);
276 bfin_write_TSCALE(TIME_SCALE
- 1);
277 bfin_write_TPERIOD(0);
278 bfin_write_TCOUNT(0);
282 static int bfin_coretmr_shutdown(struct clock_event_device
*evt
)
289 void bfin_coretmr_init(void)
291 /* power up the timer, but don't enable it just yet */
292 bfin_write_TCNTL(TMPWR
);
295 /* the TSCALE prescaler counter. */
296 bfin_write_TSCALE(TIME_SCALE
- 1);
297 bfin_write_TPERIOD(0);
298 bfin_write_TCOUNT(0);
303 #ifdef CONFIG_CORE_TIMER_IRQ_L1
304 __attribute__((l1_text
))
307 irqreturn_t
bfin_coretmr_interrupt(int irq
, void *dev_id
)
309 int cpu
= smp_processor_id();
310 struct clock_event_device
*evt
= &per_cpu(coretmr_events
, cpu
);
313 evt
->event_handler(evt
);
315 touch_nmi_watchdog();
320 static struct irqaction coretmr_irq
= {
321 .name
= "Blackfin CoreTimer",
322 .flags
= IRQF_TIMER
| IRQF_IRQPOLL
| IRQF_PERCPU
,
323 .handler
= bfin_coretmr_interrupt
,
326 void bfin_coretmr_clockevent_init(void)
328 unsigned long clock_tick
;
329 unsigned int cpu
= smp_processor_id();
330 struct clock_event_device
*evt
= &per_cpu(coretmr_events
, cpu
);
333 evt
->broadcast
= smp_timer_broadcast
;
336 evt
->name
= "bfin_core_timer";
340 evt
->features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
341 evt
->set_next_event
= bfin_coretmr_set_next_event
;
342 evt
->set_state_shutdown
= bfin_coretmr_shutdown
;
343 evt
->set_state_periodic
= bfin_coretmr_set_periodic
;
344 evt
->set_state_oneshot
= bfin_coretmr_set_oneshot
;
346 clock_tick
= get_cclk() / TIME_SCALE
;
347 evt
->mult
= div_sc(clock_tick
, NSEC_PER_SEC
, evt
->shift
);
348 evt
->max_delta_ns
= clockevent_delta2ns(-1, evt
);
349 evt
->max_delta_ticks
= (unsigned long)-1;
350 evt
->min_delta_ns
= clockevent_delta2ns(100, evt
);
351 evt
->min_delta_ticks
= 100;
353 evt
->cpumask
= cpumask_of(cpu
);
355 clockevents_register_device(evt
);
357 #endif /* CONFIG_TICKSOURCE_CORETMR */
360 void read_persistent_clock(struct timespec
*ts
)
362 time_t secs_since_1970
= (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
363 ts
->tv_sec
= secs_since_1970
;
367 void __init
time_init(void)
370 #ifdef CONFIG_RTC_DRV_BFIN
371 /* [#2663] hack to filter junk RTC values that would cause
372 * userspace to have to deal with time values greater than
373 * 2^31 seconds (which uClibc cannot cope with yet)
375 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
376 printk(KERN_NOTICE
"bfin-rtc: invalid date; resetting\n");
377 bfin_write_RTC_STAT(0);
381 bfin_cs_cycles_init();
382 bfin_cs_gptimer0_init();
384 #if defined(CONFIG_TICKSOURCE_CORETMR)
386 setup_irq(IRQ_CORETMR
, &coretmr_irq
);
387 bfin_coretmr_clockevent_init();
390 #if defined(CONFIG_TICKSOURCE_GPTMR0)
392 setup_irq(IRQ_TIMER0
, &gptmr0_irq
);
393 gptmr0_irq
.dev_id
= &clockevent_gptmr0
;
394 bfin_gptmr0_clockevent_init(&clockevent_gptmr0
);
397 #if !defined(CONFIG_TICKSOURCE_CORETMR) && !defined(CONFIG_TICKSOURCE_GPTMR0)
398 # error at least one clock event device is required