2 * linux/arch/kernel/time-ts.c
4 * Based on arm clockevents implementation and old bfin time tick.
6 * Copyright(C) 2008, GeoTechnologies, Vitja Makarov
8 * This code is licenced under the GPL version 2. For details see
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>
25 #if defined(CONFIG_CYCLES_CLOCKSOURCE)
27 /* Accelerators for sched_clock()
28 * convert from cycles(64bits) => nanoseconds (64bits)
30 * ns = cycles / (freq / ns_per_sec)
31 * ns = cycles * (ns_per_sec / freq)
32 * ns = cycles * (10^9 / (cpu_khz * 10^3))
33 * ns = cycles * (10^6 / cpu_khz)
35 * Then we use scaling math (suggested by george@mvista.com) to get:
36 * ns = cycles * (10^6 * SC / cpu_khz) / SC
37 * ns = cycles * cyc2ns_scale / SC
39 * And since SC is a constant power of two, we can convert the div
42 * We can use khz divisor instead of mhz to keep a better precision, since
43 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
44 * (mathieu.desnoyers@polymtl.ca)
46 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
49 static unsigned long cyc2ns_scale
;
50 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
52 static inline void set_cyc2ns_scale(unsigned long cpu_khz
)
54 cyc2ns_scale
= (1000000 << CYC2NS_SCALE_FACTOR
) / cpu_khz
;
57 static inline unsigned long long cycles_2_ns(cycle_t cyc
)
59 return (cyc
* cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
62 static cycle_t
bfin_read_cycles(struct clocksource
*cs
)
64 return __bfin_cycles_off
+ (get_cycles() << __bfin_cycles_mod
);
67 static struct clocksource bfin_cs_cycles
= {
68 .name
= "bfin_cs_cycles",
70 .read
= bfin_read_cycles
,
71 .mask
= CLOCKSOURCE_MASK(64),
73 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
76 unsigned long long sched_clock(void)
78 return cycles_2_ns(bfin_read_cycles(&bfin_cs_cycles
));
81 static int __init
bfin_cs_cycles_init(void)
83 set_cyc2ns_scale(get_cclk() / 1000);
85 bfin_cs_cycles
.mult
= \
86 clocksource_hz2mult(get_cclk(), bfin_cs_cycles
.shift
);
88 if (clocksource_register(&bfin_cs_cycles
))
89 panic("failed to register clocksource");
94 # define bfin_cs_cycles_init()
97 #ifdef CONFIG_GPTMR0_CLOCKSOURCE
99 void __init
setup_gptimer0(void)
101 disable_gptimers(TIMER0bit
);
103 set_gptimer_config(TIMER0_id
, \
104 TIMER_OUT_DIS
| TIMER_PERIOD_CNT
| TIMER_MODE_PWM
);
105 set_gptimer_period(TIMER0_id
, -1);
106 set_gptimer_pwidth(TIMER0_id
, -2);
108 enable_gptimers(TIMER0bit
);
111 static cycle_t
bfin_read_gptimer0(void)
113 return bfin_read_TIMER0_COUNTER();
116 static struct clocksource bfin_cs_gptimer0
= {
117 .name
= "bfin_cs_gptimer0",
119 .read
= bfin_read_gptimer0
,
120 .mask
= CLOCKSOURCE_MASK(32),
122 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
125 static int __init
bfin_cs_gptimer0_init(void)
129 bfin_cs_gptimer0
.mult
= \
130 clocksource_hz2mult(get_sclk(), bfin_cs_gptimer0
.shift
);
132 if (clocksource_register(&bfin_cs_gptimer0
))
133 panic("failed to register clocksource");
138 # define bfin_cs_gptimer0_init()
141 #ifdef CONFIG_CORE_TIMER_IRQ_L1
142 __attribute__((l1_text
))
144 irqreturn_t
timer_interrupt(int irq
, void *dev_id
);
146 static int bfin_timer_set_next_event(unsigned long, \
147 struct clock_event_device
*);
149 static void bfin_timer_set_mode(enum clock_event_mode
, \
150 struct clock_event_device
*);
152 static struct clock_event_device clockevent_bfin
= {
153 #if defined(CONFIG_TICKSOURCE_GPTMR0)
154 .name
= "bfin_gptimer0",
158 .name
= "bfin_core_timer",
163 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
164 .set_next_event
= bfin_timer_set_next_event
,
165 .set_mode
= bfin_timer_set_mode
,
168 static struct irqaction bfin_timer_irq
= {
169 #if defined(CONFIG_TICKSOURCE_GPTMR0)
170 .name
= "Blackfin GPTimer0",
172 .name
= "Blackfin CoreTimer",
174 .flags
= IRQF_DISABLED
| IRQF_TIMER
| \
175 IRQF_IRQPOLL
| IRQF_PERCPU
,
176 .handler
= timer_interrupt
,
177 .dev_id
= &clockevent_bfin
,
180 #if defined(CONFIG_TICKSOURCE_GPTMR0)
181 static int bfin_timer_set_next_event(unsigned long cycles
,
182 struct clock_event_device
*evt
)
184 disable_gptimers(TIMER0bit
);
186 /* it starts counting three SCLK cycles after the TIMENx bit is set */
187 set_gptimer_pwidth(TIMER0_id
, cycles
- 3);
188 enable_gptimers(TIMER0bit
);
192 static void bfin_timer_set_mode(enum clock_event_mode mode
,
193 struct clock_event_device
*evt
)
196 case CLOCK_EVT_MODE_PERIODIC
: {
197 set_gptimer_config(TIMER0_id
, \
198 TIMER_OUT_DIS
| TIMER_IRQ_ENA
| \
199 TIMER_PERIOD_CNT
| TIMER_MODE_PWM
);
200 set_gptimer_period(TIMER0_id
, get_sclk() / HZ
);
201 set_gptimer_pwidth(TIMER0_id
, get_sclk() / HZ
- 1);
202 enable_gptimers(TIMER0bit
);
205 case CLOCK_EVT_MODE_ONESHOT
:
206 disable_gptimers(TIMER0bit
);
207 set_gptimer_config(TIMER0_id
, \
208 TIMER_OUT_DIS
| TIMER_IRQ_ENA
| TIMER_MODE_PWM
);
209 set_gptimer_period(TIMER0_id
, 0);
211 case CLOCK_EVT_MODE_UNUSED
:
212 case CLOCK_EVT_MODE_SHUTDOWN
:
213 disable_gptimers(TIMER0bit
);
215 case CLOCK_EVT_MODE_RESUME
:
220 static void bfin_timer_ack(void)
222 set_gptimer_status(TIMER_GROUP1
, TIMER_STATUS_TIMIL0
);
225 static void __init
bfin_timer_init(void)
227 disable_gptimers(TIMER0bit
);
230 static unsigned long __init
bfin_clockevent_check(void)
232 setup_irq(IRQ_TIMER0
, &bfin_timer_irq
);
236 #else /* CONFIG_TICKSOURCE_CORETMR */
238 static int bfin_timer_set_next_event(unsigned long cycles
,
239 struct clock_event_device
*evt
)
241 bfin_write_TCNTL(TMPWR
);
243 bfin_write_TCOUNT(cycles
);
245 bfin_write_TCNTL(TMPWR
| TMREN
);
249 static void bfin_timer_set_mode(enum clock_event_mode mode
,
250 struct clock_event_device
*evt
)
253 case CLOCK_EVT_MODE_PERIODIC
: {
254 unsigned long tcount
= ((get_cclk() / (HZ
* TIME_SCALE
)) - 1);
255 bfin_write_TCNTL(TMPWR
);
257 bfin_write_TSCALE(TIME_SCALE
- 1);
258 bfin_write_TPERIOD(tcount
);
259 bfin_write_TCOUNT(tcount
);
261 bfin_write_TCNTL(TMPWR
| TMREN
| TAUTORLD
);
264 case CLOCK_EVT_MODE_ONESHOT
:
265 bfin_write_TCNTL(TMPWR
);
267 bfin_write_TSCALE(TIME_SCALE
- 1);
268 bfin_write_TPERIOD(0);
269 bfin_write_TCOUNT(0);
271 case CLOCK_EVT_MODE_UNUSED
:
272 case CLOCK_EVT_MODE_SHUTDOWN
:
276 case CLOCK_EVT_MODE_RESUME
:
281 static void bfin_timer_ack(void)
285 static void __init
bfin_timer_init(void)
287 /* power up the timer, but don't enable it just yet */
288 bfin_write_TCNTL(TMPWR
);
292 * the TSCALE prescaler counter.
294 bfin_write_TSCALE(TIME_SCALE
- 1);
295 bfin_write_TPERIOD(0);
296 bfin_write_TCOUNT(0);
301 static unsigned long __init
bfin_clockevent_check(void)
303 setup_irq(IRQ_CORETMR
, &bfin_timer_irq
);
304 return get_cclk() / TIME_SCALE
;
307 void __init
setup_core_timer(void)
310 bfin_timer_set_mode(CLOCK_EVT_MODE_PERIODIC
, NULL
);
312 #endif /* CONFIG_TICKSOURCE_GPTMR0 */
315 * timer_interrupt() needs to keep up the real-time clock,
316 * as well as call the "do_timer()" routine every clocktick
318 irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
320 struct clock_event_device
*evt
= dev_id
;
322 evt
->event_handler(evt
);
327 static int __init
bfin_clockevent_init(void)
329 unsigned long timer_clk
;
331 timer_clk
= bfin_clockevent_check();
335 clockevent_bfin
.mult
= div_sc(timer_clk
, NSEC_PER_SEC
, clockevent_bfin
.shift
);
336 clockevent_bfin
.max_delta_ns
= clockevent_delta2ns(-1, &clockevent_bfin
);
337 clockevent_bfin
.min_delta_ns
= clockevent_delta2ns(100, &clockevent_bfin
);
338 clockevent_bfin
.cpumask
= cpumask_of(0);
339 clockevents_register_device(&clockevent_bfin
);
344 void __init
time_init(void)
346 time_t secs_since_1970
= (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
348 #ifdef CONFIG_RTC_DRV_BFIN
349 /* [#2663] hack to filter junk RTC values that would cause
350 * userspace to have to deal with time values greater than
351 * 2^31 seconds (which uClibc cannot cope with yet)
353 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
354 printk(KERN_NOTICE
"bfin-rtc: invalid date; resetting\n");
355 bfin_write_RTC_STAT(0);
359 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
360 xtime
.tv_sec
= secs_since_1970
;
362 set_normalized_timespec(&wall_to_monotonic
, -xtime
.tv_sec
, -xtime
.tv_nsec
);
364 bfin_cs_cycles_init();
365 bfin_cs_gptimer0_init();
366 bfin_clockevent_init();