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>
24 #ifdef CONFIG_CYCLES_CLOCKSOURCE
26 /* Accelerators for sched_clock()
27 * convert from cycles(64bits) => nanoseconds (64bits)
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)
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
38 * And since SC is a constant power of two, we can convert the div
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)
45 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
48 static unsigned long cyc2ns_scale
;
49 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
51 static inline void set_cyc2ns_scale(unsigned long cpu_khz
)
53 cyc2ns_scale
= (1000000 << CYC2NS_SCALE_FACTOR
) / cpu_khz
;
56 static inline unsigned long long cycles_2_ns(cycle_t cyc
)
58 return (cyc
* cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
61 static cycle_t
read_cycles(void)
63 return __bfin_cycles_off
+ (get_cycles() << __bfin_cycles_mod
);
66 unsigned long long sched_clock(void)
68 return cycles_2_ns(read_cycles());
71 static struct clocksource clocksource_bfin
= {
72 .name
= "bfin_cycles",
75 .mask
= CLOCKSOURCE_MASK(64),
77 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
80 static int __init
bfin_clocksource_init(void)
82 set_cyc2ns_scale(get_cclk() / 1000);
84 clocksource_bfin
.mult
= clocksource_hz2mult(get_cclk(), clocksource_bfin
.shift
);
86 if (clocksource_register(&clocksource_bfin
))
87 panic("failed to register clocksource");
93 # define bfin_clocksource_init()
96 static int bfin_timer_set_next_event(unsigned long cycles
,
97 struct clock_event_device
*evt
)
99 bfin_write_TCOUNT(cycles
);
104 static void bfin_timer_set_mode(enum clock_event_mode mode
,
105 struct clock_event_device
*evt
)
108 case CLOCK_EVT_MODE_PERIODIC
: {
109 unsigned long tcount
= ((get_cclk() / (HZ
* TIME_SCALE
)) - 1);
110 bfin_write_TCNTL(TMPWR
);
111 bfin_write_TSCALE(TIME_SCALE
- 1);
113 bfin_write_TPERIOD(tcount
);
114 bfin_write_TCOUNT(tcount
);
115 bfin_write_TCNTL(TMPWR
| TMREN
| TAUTORLD
);
119 case CLOCK_EVT_MODE_ONESHOT
:
120 bfin_write_TSCALE(TIME_SCALE
- 1);
121 bfin_write_TCOUNT(0);
122 bfin_write_TCNTL(TMPWR
| TMREN
);
125 case CLOCK_EVT_MODE_UNUSED
:
126 case CLOCK_EVT_MODE_SHUTDOWN
:
130 case CLOCK_EVT_MODE_RESUME
:
135 static void __init
bfin_timer_init(void)
137 /* power up the timer, but don't enable it just yet */
138 bfin_write_TCNTL(TMPWR
);
142 * the TSCALE prescaler counter.
144 bfin_write_TSCALE(TIME_SCALE
- 1);
145 bfin_write_TPERIOD(0);
146 bfin_write_TCOUNT(0);
148 /* now enable the timer */
153 * timer_interrupt() needs to keep up the real-time clock,
154 * as well as call the "do_timer()" routine every clocktick
156 #ifdef CONFIG_CORE_TIMER_IRQ_L1
157 __attribute__((l1_text
))
159 irqreturn_t
timer_interrupt(int irq
, void *dev_id
);
161 static struct clock_event_device clockevent_bfin
= {
162 .name
= "bfin_core_timer",
163 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
165 .cpumask
= CPU_MASK_CPU0
,
166 .set_next_event
= bfin_timer_set_next_event
,
167 .set_mode
= bfin_timer_set_mode
,
170 static struct irqaction bfin_timer_irq
= {
171 .name
= "Blackfin Core Timer",
172 .flags
= IRQF_DISABLED
| IRQF_TIMER
| IRQF_IRQPOLL
,
173 .handler
= timer_interrupt
,
174 .dev_id
= &clockevent_bfin
,
177 irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
179 struct clock_event_device
*evt
= dev_id
;
180 evt
->event_handler(evt
);
184 static int __init
bfin_clockevent_init(void)
186 unsigned long timer_clk
;
188 timer_clk
= get_cclk() / TIME_SCALE
;
190 setup_irq(IRQ_CORETMR
, &bfin_timer_irq
);
193 clockevent_bfin
.mult
= div_sc(timer_clk
, NSEC_PER_SEC
, clockevent_bfin
.shift
);
194 clockevent_bfin
.max_delta_ns
= clockevent_delta2ns(-1, &clockevent_bfin
);
195 clockevent_bfin
.min_delta_ns
= clockevent_delta2ns(100, &clockevent_bfin
);
196 clockevents_register_device(&clockevent_bfin
);
201 void __init
time_init(void)
203 time_t secs_since_1970
= (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
205 #ifdef CONFIG_RTC_DRV_BFIN
206 /* [#2663] hack to filter junk RTC values that would cause
207 * userspace to have to deal with time values greater than
208 * 2^31 seconds (which uClibc cannot cope with yet)
210 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
211 printk(KERN_NOTICE
"bfin-rtc: invalid date; resetting\n");
212 bfin_write_RTC_STAT(0);
216 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
217 xtime
.tv_sec
= secs_since_1970
;
219 set_normalized_timespec(&wall_to_monotonic
, -xtime
.tv_sec
, -xtime
.tv_nsec
);
221 bfin_clocksource_init();
222 bfin_clockevent_init();