OMAP: Add new function to check wether there is irq pending
[linux-ginger.git] / arch / blackfin / kernel / time-ts.c
blob27646121280a3f876295071084129148b1c0f250
1 /*
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
9 * kernel-base/COPYING.
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>
22 #include <asm/time.h>
24 #ifdef CONFIG_CYCLES_CLOCKSOURCE
26 /* 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)
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
39 * into a shift.
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(struct clocksource *cs)
63 return __bfin_cycles_off + (get_cycles() << __bfin_cycles_mod);
66 static struct clocksource clocksource_bfin = {
67 .name = "bfin_cycles",
68 .rating = 350,
69 .read = read_cycles,
70 .mask = CLOCKSOURCE_MASK(64),
71 .shift = 22,
72 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
75 unsigned long long sched_clock(void)
77 return cycles_2_ns(read_cycles(&clocksource_bfin));
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");
89 return 0;
92 #else
93 # define bfin_clocksource_init()
94 #endif
96 static int bfin_timer_set_next_event(unsigned long cycles,
97 struct clock_event_device *evt)
99 bfin_write_TCOUNT(cycles);
100 CSYNC();
101 return 0;
104 static void bfin_timer_set_mode(enum clock_event_mode mode,
105 struct clock_event_device *evt)
107 switch (mode) {
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);
112 CSYNC();
113 bfin_write_TPERIOD(tcount);
114 bfin_write_TCOUNT(tcount);
115 bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
116 CSYNC();
117 break;
119 case CLOCK_EVT_MODE_ONESHOT:
120 bfin_write_TSCALE(TIME_SCALE - 1);
121 bfin_write_TCOUNT(0);
122 bfin_write_TCNTL(TMPWR | TMREN);
123 CSYNC();
124 break;
125 case CLOCK_EVT_MODE_UNUSED:
126 case CLOCK_EVT_MODE_SHUTDOWN:
127 bfin_write_TCNTL(0);
128 CSYNC();
129 break;
130 case CLOCK_EVT_MODE_RESUME:
131 break;
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);
139 CSYNC();
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 */
149 CSYNC();
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))
158 #endif
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,
164 .shift = 32,
165 .set_next_event = bfin_timer_set_next_event,
166 .set_mode = bfin_timer_set_mode,
169 static struct irqaction bfin_timer_irq = {
170 .name = "Blackfin Core Timer",
171 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
172 .handler = timer_interrupt,
173 .dev_id = &clockevent_bfin,
176 irqreturn_t timer_interrupt(int irq, void *dev_id)
178 struct clock_event_device *evt = dev_id;
179 evt->event_handler(evt);
180 return IRQ_HANDLED;
183 static int __init bfin_clockevent_init(void)
185 unsigned long timer_clk;
187 timer_clk = get_cclk() / TIME_SCALE;
189 setup_irq(IRQ_CORETMR, &bfin_timer_irq);
190 bfin_timer_init();
192 clockevent_bfin.mult = div_sc(timer_clk, NSEC_PER_SEC, clockevent_bfin.shift);
193 clockevent_bfin.max_delta_ns = clockevent_delta2ns(-1, &clockevent_bfin);
194 clockevent_bfin.min_delta_ns = clockevent_delta2ns(100, &clockevent_bfin);
195 clockevent_bfin.cpumask = cpumask_of(0);
196 clockevents_register_device(&clockevent_bfin);
198 return 0;
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);
214 #endif
216 /* Initialize xtime. From now on, xtime is updated with timer interrupts */
217 xtime.tv_sec = secs_since_1970;
218 xtime.tv_nsec = 0;
219 set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
221 bfin_clocksource_init();
222 bfin_clockevent_init();