blob: a351f97c87a3d24ef6553c4dd4af0d6784a60622 [file] [log] [blame]
Vitja Makarov8b5f79f2008-02-29 12:24:23 +08001/*
Vitja Makarov8b5f79f2008-02-29 12:24:23 +08002 * Based on arm clockevents implementation and old bfin time tick.
3 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Copyright 2008-2009 Analog Devics Inc.
5 * 2008 GeoTechnologies
6 * Vitja Makarov
Vitja Makarov8b5f79f2008-02-29 12:24:23 +08007 *
Robin Getz96f10502009-09-24 14:11:24 +00008 * Licensed under the GPL-2
Vitja Makarov8b5f79f2008-02-29 12:24:23 +08009 */
Robin Getz96f10502009-09-24 14:11:24 +000010
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080011#include <linux/module.h>
12#include <linux/profile.h>
13#include <linux/interrupt.h>
14#include <linux/time.h>
Mike Frysinger764cb812008-04-24 05:07:29 +080015#include <linux/timex.h>
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080016#include <linux/irq.h>
17#include <linux/clocksource.h>
18#include <linux/clockchips.h>
Michael Henneriche6c91b62008-04-25 04:58:29 +080019#include <linux/cpufreq.h>
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080020
21#include <asm/blackfin.h>
Michael Henneriche6c91b62008-04-25 04:58:29 +080022#include <asm/time.h>
Graf Yang1fa9be72009-05-15 11:01:59 +000023#include <asm/gptimers.h>
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080024
Michael Henneriche6c91b62008-04-25 04:58:29 +080025/* Accelerators for sched_clock()
26 * convert from cycles(64bits) => nanoseconds (64bits)
27 * basic equation:
28 * ns = cycles / (freq / ns_per_sec)
29 * ns = cycles * (ns_per_sec / freq)
30 * ns = cycles * (10^9 / (cpu_khz * 10^3))
31 * ns = cycles * (10^6 / cpu_khz)
32 *
33 * Then we use scaling math (suggested by george@mvista.com) to get:
34 * ns = cycles * (10^6 * SC / cpu_khz) / SC
35 * ns = cycles * cyc2ns_scale / SC
36 *
37 * And since SC is a constant power of two, we can convert the div
38 * into a shift.
39 *
40 * We can use khz divisor instead of mhz to keep a better precision, since
41 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
42 * (mathieu.desnoyers@polymtl.ca)
43 *
44 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
45 */
46
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080047#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
48
Yi Liceb33be2009-09-15 06:50:51 +000049#if defined(CONFIG_CYCLES_CLOCKSOURCE)
50
Yi Liceb33be2009-09-15 06:50:51 +000051static notrace cycle_t bfin_read_cycles(struct clocksource *cs)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080052{
Vitja Makarov1bfb4b22008-05-07 11:41:26 +080053 return __bfin_cycles_off + (get_cycles() << __bfin_cycles_mod);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080054}
55
Graf Yang1fa9be72009-05-15 11:01:59 +000056static struct clocksource bfin_cs_cycles = {
57 .name = "bfin_cs_cycles",
Graf Yange78feaa2009-09-14 04:41:00 +000058 .rating = 400,
Graf Yang1fa9be72009-05-15 11:01:59 +000059 .read = bfin_read_cycles,
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080060 .mask = CLOCKSOURCE_MASK(64),
Yi Li29857122009-09-15 08:55:47 +000061 .shift = CYC2NS_SCALE_FACTOR,
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080062 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
63};
64
Yi Liceb33be2009-09-15 06:50:51 +000065static inline unsigned long long bfin_cs_cycles_sched_clock(void)
Magnus Damm8e196082009-04-21 12:24:00 -070066{
Mike Frysingerc768a942009-12-04 03:32:11 +000067 return clocksource_cyc2ns(bfin_read_cycles(&bfin_cs_cycles),
68 bfin_cs_cycles.mult, bfin_cs_cycles.shift);
Magnus Damm8e196082009-04-21 12:24:00 -070069}
70
Graf Yang1fa9be72009-05-15 11:01:59 +000071static int __init bfin_cs_cycles_init(void)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080072{
Graf Yang1fa9be72009-05-15 11:01:59 +000073 bfin_cs_cycles.mult = \
74 clocksource_hz2mult(get_cclk(), bfin_cs_cycles.shift);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080075
Graf Yang1fa9be72009-05-15 11:01:59 +000076 if (clocksource_register(&bfin_cs_cycles))
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080077 panic("failed to register clocksource");
78
79 return 0;
80}
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080081#else
Graf Yang1fa9be72009-05-15 11:01:59 +000082# define bfin_cs_cycles_init()
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080083#endif
84
Graf Yang1fa9be72009-05-15 11:01:59 +000085#ifdef CONFIG_GPTMR0_CLOCKSOURCE
86
87void __init setup_gptimer0(void)
88{
89 disable_gptimers(TIMER0bit);
90
91 set_gptimer_config(TIMER0_id, \
92 TIMER_OUT_DIS | TIMER_PERIOD_CNT | TIMER_MODE_PWM);
93 set_gptimer_period(TIMER0_id, -1);
94 set_gptimer_pwidth(TIMER0_id, -2);
95 SSYNC();
96 enable_gptimers(TIMER0bit);
97}
98
Yi Lif7036d62009-09-15 02:08:50 +000099static cycle_t bfin_read_gptimer0(struct clocksource *cs)
Graf Yang1fa9be72009-05-15 11:01:59 +0000100{
101 return bfin_read_TIMER0_COUNTER();
102}
103
104static struct clocksource bfin_cs_gptimer0 = {
105 .name = "bfin_cs_gptimer0",
Graf Yange78feaa2009-09-14 04:41:00 +0000106 .rating = 350,
Graf Yang1fa9be72009-05-15 11:01:59 +0000107 .read = bfin_read_gptimer0,
108 .mask = CLOCKSOURCE_MASK(32),
Yi Li29857122009-09-15 08:55:47 +0000109 .shift = CYC2NS_SCALE_FACTOR,
Graf Yang1fa9be72009-05-15 11:01:59 +0000110 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
111};
112
Yi Liceb33be2009-09-15 06:50:51 +0000113static inline unsigned long long bfin_cs_gptimer0_sched_clock(void)
114{
Mike Frysingerc768a942009-12-04 03:32:11 +0000115 return clocksource_cyc2ns(bfin_read_TIMER0_COUNTER(),
116 bfin_cs_gptimer0.mult, bfin_cs_gptimer0.shift);
Yi Liceb33be2009-09-15 06:50:51 +0000117}
118
Graf Yang1fa9be72009-05-15 11:01:59 +0000119static int __init bfin_cs_gptimer0_init(void)
120{
121 setup_gptimer0();
122
123 bfin_cs_gptimer0.mult = \
124 clocksource_hz2mult(get_sclk(), bfin_cs_gptimer0.shift);
125
126 if (clocksource_register(&bfin_cs_gptimer0))
127 panic("failed to register clocksource");
128
129 return 0;
130}
131#else
132# define bfin_cs_gptimer0_init()
133#endif
134
Yi Liceb33be2009-09-15 06:50:51 +0000135#if defined(CONFIG_GPTMR0_CLOCKSOURCE) || defined(CONFIG_CYCLES_CLOCKSOURCE)
136/* prefer to use cycles since it has higher rating */
137notrace unsigned long long sched_clock(void)
138{
139#if defined(CONFIG_CYCLES_CLOCKSOURCE)
140 return bfin_cs_cycles_sched_clock();
141#else
142 return bfin_cs_gptimer0_sched_clock();
143#endif
144}
145#endif
146
Graf Yang1fa9be72009-05-15 11:01:59 +0000147#if defined(CONFIG_TICKSOURCE_GPTMR0)
Yi Li0d152c22009-12-28 10:21:49 +0000148static int bfin_gptmr0_set_next_event(unsigned long cycles,
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800149 struct clock_event_device *evt)
150{
Graf Yang1fa9be72009-05-15 11:01:59 +0000151 disable_gptimers(TIMER0bit);
152
153 /* it starts counting three SCLK cycles after the TIMENx bit is set */
154 set_gptimer_pwidth(TIMER0_id, cycles - 3);
155 enable_gptimers(TIMER0bit);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800156 return 0;
157}
158
Yi Li0d152c22009-12-28 10:21:49 +0000159static void bfin_gptmr0_set_mode(enum clock_event_mode mode,
Graf Yang1fa9be72009-05-15 11:01:59 +0000160 struct clock_event_device *evt)
161{
162 switch (mode) {
163 case CLOCK_EVT_MODE_PERIODIC: {
164 set_gptimer_config(TIMER0_id, \
165 TIMER_OUT_DIS | TIMER_IRQ_ENA | \
166 TIMER_PERIOD_CNT | TIMER_MODE_PWM);
167 set_gptimer_period(TIMER0_id, get_sclk() / HZ);
168 set_gptimer_pwidth(TIMER0_id, get_sclk() / HZ - 1);
169 enable_gptimers(TIMER0bit);
170 break;
171 }
172 case CLOCK_EVT_MODE_ONESHOT:
173 disable_gptimers(TIMER0bit);
174 set_gptimer_config(TIMER0_id, \
175 TIMER_OUT_DIS | TIMER_IRQ_ENA | TIMER_MODE_PWM);
176 set_gptimer_period(TIMER0_id, 0);
177 break;
178 case CLOCK_EVT_MODE_UNUSED:
179 case CLOCK_EVT_MODE_SHUTDOWN:
180 disable_gptimers(TIMER0bit);
181 break;
182 case CLOCK_EVT_MODE_RESUME:
183 break;
184 }
185}
186
Yi Li0d152c22009-12-28 10:21:49 +0000187static void bfin_gptmr0_ack(void)
Graf Yang1fa9be72009-05-15 11:01:59 +0000188{
189 set_gptimer_status(TIMER_GROUP1, TIMER_STATUS_TIMIL0);
190}
191
Yi Li0d152c22009-12-28 10:21:49 +0000192static void __init bfin_gptmr0_init(void)
Graf Yang1fa9be72009-05-15 11:01:59 +0000193{
194 disable_gptimers(TIMER0bit);
195}
196
Yi Li0d152c22009-12-28 10:21:49 +0000197#ifdef CONFIG_CORE_TIMER_IRQ_L1
198__attribute__((l1_text))
199#endif
200irqreturn_t bfin_gptmr0_interrupt(int irq, void *dev_id)
Graf Yang1fa9be72009-05-15 11:01:59 +0000201{
Yi Li0d152c22009-12-28 10:21:49 +0000202 struct clock_event_device *evt = dev_id;
203 smp_mb();
204 evt->event_handler(evt);
205 bfin_gptmr0_ack();
206 return IRQ_HANDLED;
Graf Yang1fa9be72009-05-15 11:01:59 +0000207}
208
Yi Li0d152c22009-12-28 10:21:49 +0000209static struct irqaction gptmr0_irq = {
210 .name = "Blackfin GPTimer0",
211 .flags = IRQF_DISABLED | IRQF_TIMER | \
212 IRQF_IRQPOLL | IRQF_PERCPU,
213 .handler = bfin_gptmr0_interrupt,
214};
Graf Yang1fa9be72009-05-15 11:01:59 +0000215
Yi Li0d152c22009-12-28 10:21:49 +0000216static struct clock_event_device clockevent_gptmr0 = {
217 .name = "bfin_gptimer0",
218 .rating = 300,
219 .irq = IRQ_TIMER0,
220 .shift = 32,
221 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
222 .set_next_event = bfin_gptmr0_set_next_event,
223 .set_mode = bfin_gptmr0_set_mode,
224};
225
226static void __init bfin_gptmr0_clockevent_init(struct clock_event_device *evt)
227{
228 unsigned long clock_tick;
229
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->min_delta_ns = clockevent_delta2ns(100, evt);
234
235 evt->cpumask = cpumask_of(0);
236
237 clockevents_register_device(evt);
238}
239#endif /* CONFIG_TICKSOURCE_GPTMR0 */
240
241#if defined(CONFIG_TICKSOURCE_CORETMR)
242/* per-cpu local core timer */
243static DEFINE_PER_CPU(struct clock_event_device, coretmr_events);
244
245static int bfin_coretmr_set_next_event(unsigned long cycles,
Graf Yang1fa9be72009-05-15 11:01:59 +0000246 struct clock_event_device *evt)
247{
248 bfin_write_TCNTL(TMPWR);
249 CSYNC();
250 bfin_write_TCOUNT(cycles);
251 CSYNC();
252 bfin_write_TCNTL(TMPWR | TMREN);
253 return 0;
254}
255
Yi Li0d152c22009-12-28 10:21:49 +0000256static void bfin_coretmr_set_mode(enum clock_event_mode mode,
Graf Yang1fa9be72009-05-15 11:01:59 +0000257 struct clock_event_device *evt)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800258{
259 switch (mode) {
260 case CLOCK_EVT_MODE_PERIODIC: {
Michael Henneriche6c91b62008-04-25 04:58:29 +0800261 unsigned long tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800262 bfin_write_TCNTL(TMPWR);
263 CSYNC();
Graf Yang1fa9be72009-05-15 11:01:59 +0000264 bfin_write_TSCALE(TIME_SCALE - 1);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800265 bfin_write_TPERIOD(tcount);
266 bfin_write_TCOUNT(tcount);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800267 CSYNC();
Graf Yang1fa9be72009-05-15 11:01:59 +0000268 bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800269 break;
270 }
271 case CLOCK_EVT_MODE_ONESHOT:
Graf Yang1fa9be72009-05-15 11:01:59 +0000272 bfin_write_TCNTL(TMPWR);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800273 CSYNC();
Graf Yang1fa9be72009-05-15 11:01:59 +0000274 bfin_write_TSCALE(TIME_SCALE - 1);
275 bfin_write_TPERIOD(0);
276 bfin_write_TCOUNT(0);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800277 break;
278 case CLOCK_EVT_MODE_UNUSED:
279 case CLOCK_EVT_MODE_SHUTDOWN:
280 bfin_write_TCNTL(0);
281 CSYNC();
282 break;
283 case CLOCK_EVT_MODE_RESUME:
284 break;
285 }
286}
287
Yi Li0d152c22009-12-28 10:21:49 +0000288void bfin_coretmr_init(void)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800289{
290 /* power up the timer, but don't enable it just yet */
291 bfin_write_TCNTL(TMPWR);
292 CSYNC();
293
Yi Li0d152c22009-12-28 10:21:49 +0000294 /* the TSCALE prescaler counter. */
Michael Henneriche6c91b62008-04-25 04:58:29 +0800295 bfin_write_TSCALE(TIME_SCALE - 1);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800296 bfin_write_TPERIOD(0);
297 bfin_write_TCOUNT(0);
298
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800299 CSYNC();
300}
301
Yi Li0d152c22009-12-28 10:21:49 +0000302#ifdef CONFIG_CORE_TIMER_IRQ_L1
303__attribute__((l1_text))
304#endif
305irqreturn_t bfin_coretmr_interrupt(int irq, void *dev_id)
Graf Yang1fa9be72009-05-15 11:01:59 +0000306{
Yi Li0d152c22009-12-28 10:21:49 +0000307 int cpu = smp_processor_id();
308 struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
Graf Yang1fa9be72009-05-15 11:01:59 +0000309
Graf Yang1fa9be72009-05-15 11:01:59 +0000310 smp_mb();
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800311 evt->event_handler(evt);
312 return IRQ_HANDLED;
313}
314
Yi Li0d152c22009-12-28 10:21:49 +0000315static struct irqaction coretmr_irq = {
316 .name = "Blackfin CoreTimer",
317 .flags = IRQF_DISABLED | IRQF_TIMER | \
318 IRQF_IRQPOLL | IRQF_PERCPU,
319 .handler = bfin_coretmr_interrupt,
320};
321
322void bfin_coretmr_clockevent_init(void)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800323{
Yi Li0d152c22009-12-28 10:21:49 +0000324 unsigned long clock_tick;
325 unsigned int cpu = smp_processor_id();
326 struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
Vitja Makarov1bfb4b22008-05-07 11:41:26 +0800327
Yi Li0d152c22009-12-28 10:21:49 +0000328 evt->name = "bfin_core_timer";
329 evt->rating = 350;
330 evt->irq = -1;
331 evt->shift = 32;
332 evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
333 evt->set_next_event = bfin_coretmr_set_next_event;
334 evt->set_mode = bfin_coretmr_set_mode;
Vitja Makarov1bfb4b22008-05-07 11:41:26 +0800335
Yi Li0d152c22009-12-28 10:21:49 +0000336 clock_tick = get_cclk() / TIME_SCALE;
337 evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
338 evt->max_delta_ns = clockevent_delta2ns(-1, evt);
339 evt->min_delta_ns = clockevent_delta2ns(100, evt);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800340
Yi Li0d152c22009-12-28 10:21:49 +0000341 evt->cpumask = cpumask_of(cpu);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800342
Yi Li0d152c22009-12-28 10:21:49 +0000343 clockevents_register_device(evt);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800344}
Yi Li0d152c22009-12-28 10:21:49 +0000345#endif /* CONFIG_TICKSOURCE_CORETMR */
346
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800347
348void __init time_init(void)
349{
350 time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
351
352#ifdef CONFIG_RTC_DRV_BFIN
353 /* [#2663] hack to filter junk RTC values that would cause
354 * userspace to have to deal with time values greater than
355 * 2^31 seconds (which uClibc cannot cope with yet)
356 */
357 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
358 printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
359 bfin_write_RTC_STAT(0);
360 }
361#endif
362
363 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
364 xtime.tv_sec = secs_since_1970;
365 xtime.tv_nsec = 0;
366 set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
367
Graf Yang1fa9be72009-05-15 11:01:59 +0000368 bfin_cs_cycles_init();
369 bfin_cs_gptimer0_init();
Yi Li0d152c22009-12-28 10:21:49 +0000370
371#if defined(CONFIG_TICKSOURCE_CORETMR)
372 bfin_coretmr_init();
373 setup_irq(IRQ_CORETMR, &coretmr_irq);
374 bfin_coretmr_clockevent_init();
375#endif
376
377#if defined(CONFIG_TICKSOURCE_GPTMR0)
378 bfin_gptmr0_init();
379 setup_irq(IRQ_TIMER0, &gptmr0_irq);
380 gptmr0_irq.dev_id = &clockevent_gptmr0;
381 bfin_gptmr0_clockevent_init(&clockevent_gptmr0);
382#endif
383
384#if !defined(CONFIG_TICKSOURCE_CORETMR) && !defined(CONFIG_TICKSOURCE_GPTMR0)
385# error at least one clock event device is required
386#endif
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800387}