blob: 41a907596c70dbbd6999d193bd1aaba91ca17996 [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>
Graf Yang60ffdb32010-01-20 10:56:24 +000024#include <asm/nmi.h>
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080025
Michael Henneriche6c91b62008-04-25 04:58:29 +080026/* Accelerators for sched_clock()
27 * convert from cycles(64bits) => nanoseconds (64bits)
28 * basic equation:
29 * ns = cycles / (freq / ns_per_sec)
30 * ns = cycles * (ns_per_sec / freq)
31 * ns = cycles * (10^9 / (cpu_khz * 10^3))
32 * ns = cycles * (10^6 / cpu_khz)
33 *
34 * Then we use scaling math (suggested by george@mvista.com) to get:
35 * ns = cycles * (10^6 * SC / cpu_khz) / SC
36 * ns = cycles * cyc2ns_scale / SC
37 *
38 * And since SC is a constant power of two, we can convert the div
39 * into a shift.
40 *
41 * We can use khz divisor instead of mhz to keep a better precision, since
42 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
43 * (mathieu.desnoyers@polymtl.ca)
44 *
45 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
46 */
47
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080048#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
49
Yi Liceb33be2009-09-15 06:50:51 +000050#if defined(CONFIG_CYCLES_CLOCKSOURCE)
51
Yi Liceb33be2009-09-15 06:50:51 +000052static notrace cycle_t bfin_read_cycles(struct clocksource *cs)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080053{
Vitja Makarov1bfb4b22008-05-07 11:41:26 +080054 return __bfin_cycles_off + (get_cycles() << __bfin_cycles_mod);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080055}
56
Graf Yang1fa9be72009-05-15 11:01:59 +000057static struct clocksource bfin_cs_cycles = {
58 .name = "bfin_cs_cycles",
Graf Yange78feaa2009-09-14 04:41:00 +000059 .rating = 400,
Graf Yang1fa9be72009-05-15 11:01:59 +000060 .read = bfin_read_cycles,
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080061 .mask = CLOCKSOURCE_MASK(64),
Yi Li29857122009-09-15 08:55:47 +000062 .shift = CYC2NS_SCALE_FACTOR,
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080063 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
64};
65
Yi Liceb33be2009-09-15 06:50:51 +000066static inline unsigned long long bfin_cs_cycles_sched_clock(void)
Magnus Damm8e196082009-04-21 12:24:00 -070067{
Mike Frysingerc768a942009-12-04 03:32:11 +000068 return clocksource_cyc2ns(bfin_read_cycles(&bfin_cs_cycles),
69 bfin_cs_cycles.mult, bfin_cs_cycles.shift);
Magnus Damm8e196082009-04-21 12:24:00 -070070}
71
Graf Yang1fa9be72009-05-15 11:01:59 +000072static int __init bfin_cs_cycles_init(void)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080073{
Graf Yang1fa9be72009-05-15 11:01:59 +000074 bfin_cs_cycles.mult = \
75 clocksource_hz2mult(get_cclk(), bfin_cs_cycles.shift);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080076
Graf Yang1fa9be72009-05-15 11:01:59 +000077 if (clocksource_register(&bfin_cs_cycles))
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080078 panic("failed to register clocksource");
79
80 return 0;
81}
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080082#else
Graf Yang1fa9be72009-05-15 11:01:59 +000083# define bfin_cs_cycles_init()
Vitja Makarov8b5f79f2008-02-29 12:24:23 +080084#endif
85
Graf Yang1fa9be72009-05-15 11:01:59 +000086#ifdef CONFIG_GPTMR0_CLOCKSOURCE
87
88void __init setup_gptimer0(void)
89{
90 disable_gptimers(TIMER0bit);
91
92 set_gptimer_config(TIMER0_id, \
93 TIMER_OUT_DIS | TIMER_PERIOD_CNT | TIMER_MODE_PWM);
94 set_gptimer_period(TIMER0_id, -1);
95 set_gptimer_pwidth(TIMER0_id, -2);
96 SSYNC();
97 enable_gptimers(TIMER0bit);
98}
99
Yi Lif7036d62009-09-15 02:08:50 +0000100static cycle_t bfin_read_gptimer0(struct clocksource *cs)
Graf Yang1fa9be72009-05-15 11:01:59 +0000101{
102 return bfin_read_TIMER0_COUNTER();
103}
104
105static struct clocksource bfin_cs_gptimer0 = {
106 .name = "bfin_cs_gptimer0",
Graf Yange78feaa2009-09-14 04:41:00 +0000107 .rating = 350,
Graf Yang1fa9be72009-05-15 11:01:59 +0000108 .read = bfin_read_gptimer0,
109 .mask = CLOCKSOURCE_MASK(32),
Yi Li29857122009-09-15 08:55:47 +0000110 .shift = CYC2NS_SCALE_FACTOR,
Graf Yang1fa9be72009-05-15 11:01:59 +0000111 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
112};
113
Yi Liceb33be2009-09-15 06:50:51 +0000114static inline unsigned long long bfin_cs_gptimer0_sched_clock(void)
115{
Mike Frysingerc768a942009-12-04 03:32:11 +0000116 return clocksource_cyc2ns(bfin_read_TIMER0_COUNTER(),
117 bfin_cs_gptimer0.mult, bfin_cs_gptimer0.shift);
Yi Liceb33be2009-09-15 06:50:51 +0000118}
119
Graf Yang1fa9be72009-05-15 11:01:59 +0000120static int __init bfin_cs_gptimer0_init(void)
121{
122 setup_gptimer0();
123
124 bfin_cs_gptimer0.mult = \
125 clocksource_hz2mult(get_sclk(), bfin_cs_gptimer0.shift);
126
127 if (clocksource_register(&bfin_cs_gptimer0))
128 panic("failed to register clocksource");
129
130 return 0;
131}
132#else
133# define bfin_cs_gptimer0_init()
134#endif
135
Yi Liceb33be2009-09-15 06:50:51 +0000136#if defined(CONFIG_GPTMR0_CLOCKSOURCE) || defined(CONFIG_CYCLES_CLOCKSOURCE)
137/* prefer to use cycles since it has higher rating */
138notrace unsigned long long sched_clock(void)
139{
140#if defined(CONFIG_CYCLES_CLOCKSOURCE)
141 return bfin_cs_cycles_sched_clock();
142#else
143 return bfin_cs_gptimer0_sched_clock();
144#endif
145}
146#endif
147
Graf Yang1fa9be72009-05-15 11:01:59 +0000148#if defined(CONFIG_TICKSOURCE_GPTMR0)
Yi Li0d152c22009-12-28 10:21:49 +0000149static int bfin_gptmr0_set_next_event(unsigned long cycles,
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800150 struct clock_event_device *evt)
151{
Graf Yang1fa9be72009-05-15 11:01:59 +0000152 disable_gptimers(TIMER0bit);
153
154 /* it starts counting three SCLK cycles after the TIMENx bit is set */
155 set_gptimer_pwidth(TIMER0_id, cycles - 3);
156 enable_gptimers(TIMER0bit);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800157 return 0;
158}
159
Yi Li0d152c22009-12-28 10:21:49 +0000160static void bfin_gptmr0_set_mode(enum clock_event_mode mode,
Graf Yang1fa9be72009-05-15 11:01:59 +0000161 struct clock_event_device *evt)
162{
163 switch (mode) {
164 case CLOCK_EVT_MODE_PERIODIC: {
165 set_gptimer_config(TIMER0_id, \
166 TIMER_OUT_DIS | TIMER_IRQ_ENA | \
167 TIMER_PERIOD_CNT | TIMER_MODE_PWM);
168 set_gptimer_period(TIMER0_id, get_sclk() / HZ);
169 set_gptimer_pwidth(TIMER0_id, get_sclk() / HZ - 1);
170 enable_gptimers(TIMER0bit);
171 break;
172 }
173 case CLOCK_EVT_MODE_ONESHOT:
174 disable_gptimers(TIMER0bit);
175 set_gptimer_config(TIMER0_id, \
176 TIMER_OUT_DIS | TIMER_IRQ_ENA | TIMER_MODE_PWM);
177 set_gptimer_period(TIMER0_id, 0);
178 break;
179 case CLOCK_EVT_MODE_UNUSED:
180 case CLOCK_EVT_MODE_SHUTDOWN:
181 disable_gptimers(TIMER0bit);
182 break;
183 case CLOCK_EVT_MODE_RESUME:
184 break;
185 }
186}
187
Yi Li0d152c22009-12-28 10:21:49 +0000188static void bfin_gptmr0_ack(void)
Graf Yang1fa9be72009-05-15 11:01:59 +0000189{
190 set_gptimer_status(TIMER_GROUP1, TIMER_STATUS_TIMIL0);
191}
192
Yi Li0d152c22009-12-28 10:21:49 +0000193static void __init bfin_gptmr0_init(void)
Graf Yang1fa9be72009-05-15 11:01:59 +0000194{
195 disable_gptimers(TIMER0bit);
196}
197
Yi Li0d152c22009-12-28 10:21:49 +0000198#ifdef CONFIG_CORE_TIMER_IRQ_L1
199__attribute__((l1_text))
200#endif
201irqreturn_t bfin_gptmr0_interrupt(int irq, void *dev_id)
Graf Yang1fa9be72009-05-15 11:01:59 +0000202{
Yi Li0d152c22009-12-28 10:21:49 +0000203 struct clock_event_device *evt = dev_id;
204 smp_mb();
205 evt->event_handler(evt);
206 bfin_gptmr0_ack();
207 return IRQ_HANDLED;
Graf Yang1fa9be72009-05-15 11:01:59 +0000208}
209
Yi Li0d152c22009-12-28 10:21:49 +0000210static struct irqaction gptmr0_irq = {
211 .name = "Blackfin GPTimer0",
212 .flags = IRQF_DISABLED | IRQF_TIMER | \
213 IRQF_IRQPOLL | IRQF_PERCPU,
214 .handler = bfin_gptmr0_interrupt,
215};
Graf Yang1fa9be72009-05-15 11:01:59 +0000216
Yi Li0d152c22009-12-28 10:21:49 +0000217static struct clock_event_device clockevent_gptmr0 = {
218 .name = "bfin_gptimer0",
219 .rating = 300,
220 .irq = IRQ_TIMER0,
221 .shift = 32,
222 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
223 .set_next_event = bfin_gptmr0_set_next_event,
224 .set_mode = bfin_gptmr0_set_mode,
225};
226
227static void __init bfin_gptmr0_clockevent_init(struct clock_event_device *evt)
228{
229 unsigned long clock_tick;
230
231 clock_tick = get_sclk();
232 evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
233 evt->max_delta_ns = clockevent_delta2ns(-1, evt);
234 evt->min_delta_ns = clockevent_delta2ns(100, evt);
235
236 evt->cpumask = cpumask_of(0);
237
238 clockevents_register_device(evt);
239}
240#endif /* CONFIG_TICKSOURCE_GPTMR0 */
241
242#if defined(CONFIG_TICKSOURCE_CORETMR)
243/* per-cpu local core timer */
244static DEFINE_PER_CPU(struct clock_event_device, coretmr_events);
245
246static int bfin_coretmr_set_next_event(unsigned long cycles,
Graf Yang1fa9be72009-05-15 11:01:59 +0000247 struct clock_event_device *evt)
248{
249 bfin_write_TCNTL(TMPWR);
250 CSYNC();
251 bfin_write_TCOUNT(cycles);
252 CSYNC();
253 bfin_write_TCNTL(TMPWR | TMREN);
254 return 0;
255}
256
Yi Li0d152c22009-12-28 10:21:49 +0000257static void bfin_coretmr_set_mode(enum clock_event_mode mode,
Graf Yang1fa9be72009-05-15 11:01:59 +0000258 struct clock_event_device *evt)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800259{
260 switch (mode) {
261 case CLOCK_EVT_MODE_PERIODIC: {
Michael Henneriche6c91b62008-04-25 04:58:29 +0800262 unsigned long tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800263 bfin_write_TCNTL(TMPWR);
264 CSYNC();
Graf Yang1fa9be72009-05-15 11:01:59 +0000265 bfin_write_TSCALE(TIME_SCALE - 1);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800266 bfin_write_TPERIOD(tcount);
267 bfin_write_TCOUNT(tcount);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800268 CSYNC();
Graf Yang1fa9be72009-05-15 11:01:59 +0000269 bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800270 break;
271 }
272 case CLOCK_EVT_MODE_ONESHOT:
Graf Yang1fa9be72009-05-15 11:01:59 +0000273 bfin_write_TCNTL(TMPWR);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800274 CSYNC();
Graf Yang1fa9be72009-05-15 11:01:59 +0000275 bfin_write_TSCALE(TIME_SCALE - 1);
276 bfin_write_TPERIOD(0);
277 bfin_write_TCOUNT(0);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800278 break;
279 case CLOCK_EVT_MODE_UNUSED:
280 case CLOCK_EVT_MODE_SHUTDOWN:
281 bfin_write_TCNTL(0);
282 CSYNC();
283 break;
284 case CLOCK_EVT_MODE_RESUME:
285 break;
286 }
287}
288
Yi Li0d152c22009-12-28 10:21:49 +0000289void bfin_coretmr_init(void)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800290{
291 /* power up the timer, but don't enable it just yet */
292 bfin_write_TCNTL(TMPWR);
293 CSYNC();
294
Yi Li0d152c22009-12-28 10:21:49 +0000295 /* the TSCALE prescaler counter. */
Michael Henneriche6c91b62008-04-25 04:58:29 +0800296 bfin_write_TSCALE(TIME_SCALE - 1);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800297 bfin_write_TPERIOD(0);
298 bfin_write_TCOUNT(0);
299
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800300 CSYNC();
301}
302
Yi Li0d152c22009-12-28 10:21:49 +0000303#ifdef CONFIG_CORE_TIMER_IRQ_L1
304__attribute__((l1_text))
305#endif
306irqreturn_t bfin_coretmr_interrupt(int irq, void *dev_id)
Graf Yang1fa9be72009-05-15 11:01:59 +0000307{
Yi Li0d152c22009-12-28 10:21:49 +0000308 int cpu = smp_processor_id();
309 struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
Graf Yang1fa9be72009-05-15 11:01:59 +0000310
Graf Yang1fa9be72009-05-15 11:01:59 +0000311 smp_mb();
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800312 evt->event_handler(evt);
Graf Yang60ffdb32010-01-20 10:56:24 +0000313
314 touch_nmi_watchdog();
315
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800316 return IRQ_HANDLED;
317}
318
Yi Li0d152c22009-12-28 10:21:49 +0000319static struct irqaction coretmr_irq = {
320 .name = "Blackfin CoreTimer",
321 .flags = IRQF_DISABLED | IRQF_TIMER | \
322 IRQF_IRQPOLL | IRQF_PERCPU,
323 .handler = bfin_coretmr_interrupt,
324};
325
326void bfin_coretmr_clockevent_init(void)
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800327{
Yi Li0d152c22009-12-28 10:21:49 +0000328 unsigned long clock_tick;
329 unsigned int cpu = smp_processor_id();
330 struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
Vitja Makarov1bfb4b22008-05-07 11:41:26 +0800331
Yi Li0d152c22009-12-28 10:21:49 +0000332 evt->name = "bfin_core_timer";
333 evt->rating = 350;
334 evt->irq = -1;
335 evt->shift = 32;
336 evt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
337 evt->set_next_event = bfin_coretmr_set_next_event;
338 evt->set_mode = bfin_coretmr_set_mode;
Vitja Makarov1bfb4b22008-05-07 11:41:26 +0800339
Yi Li0d152c22009-12-28 10:21:49 +0000340 clock_tick = get_cclk() / TIME_SCALE;
341 evt->mult = div_sc(clock_tick, NSEC_PER_SEC, evt->shift);
342 evt->max_delta_ns = clockevent_delta2ns(-1, evt);
343 evt->min_delta_ns = clockevent_delta2ns(100, evt);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800344
Yi Li0d152c22009-12-28 10:21:49 +0000345 evt->cpumask = cpumask_of(cpu);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800346
Yi Li0d152c22009-12-28 10:21:49 +0000347 clockevents_register_device(evt);
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800348}
Yi Li0d152c22009-12-28 10:21:49 +0000349#endif /* CONFIG_TICKSOURCE_CORETMR */
350
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800351
352void __init time_init(void)
353{
354 time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
355
356#ifdef CONFIG_RTC_DRV_BFIN
357 /* [#2663] hack to filter junk RTC values that would cause
358 * userspace to have to deal with time values greater than
359 * 2^31 seconds (which uClibc cannot cope with yet)
360 */
361 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
362 printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
363 bfin_write_RTC_STAT(0);
364 }
365#endif
366
367 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
368 xtime.tv_sec = secs_since_1970;
369 xtime.tv_nsec = 0;
370 set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
371
Graf Yang1fa9be72009-05-15 11:01:59 +0000372 bfin_cs_cycles_init();
373 bfin_cs_gptimer0_init();
Yi Li0d152c22009-12-28 10:21:49 +0000374
375#if defined(CONFIG_TICKSOURCE_CORETMR)
376 bfin_coretmr_init();
377 setup_irq(IRQ_CORETMR, &coretmr_irq);
378 bfin_coretmr_clockevent_init();
379#endif
380
381#if defined(CONFIG_TICKSOURCE_GPTMR0)
382 bfin_gptmr0_init();
383 setup_irq(IRQ_TIMER0, &gptmr0_irq);
384 gptmr0_irq.dev_id = &clockevent_gptmr0;
385 bfin_gptmr0_clockevent_init(&clockevent_gptmr0);
386#endif
387
388#if !defined(CONFIG_TICKSOURCE_CORETMR) && !defined(CONFIG_TICKSOURCE_GPTMR0)
389# error at least one clock event device is required
390#endif
Vitja Makarov8b5f79f2008-02-29 12:24:23 +0800391}