1 #include <linux/kernel.h>
2 #include <linux/sched.h>
3 #include <linux/init.h>
4 #include <linux/mc146818rtc.h>
5 #include <linux/time.h>
6 #include <linux/clocksource.h>
7 #include <linux/ioport.h>
8 #include <linux/acpi.h>
9 #include <linux/hpet.h>
10 #include <asm/pgtable.h>
11 #include <asm/vsyscall.h>
12 #include <asm/timex.h>
15 #define HPET_MASK 0xFFFFFFFF
18 /* FSEC = 10^-15 NSEC = 10^-9 */
19 #define FSEC_PER_NSEC 1000000
21 int nohpet __initdata;
23 unsigned long hpet_address;
24 unsigned long hpet_period; /* fsecs / HPET clock */
25 unsigned long hpet_tick; /* HPET clocks / interrupt */
27 int hpet_use_timer; /* Use counter of hpet for time keeping,
32 static __init int late_hpet_init(void)
40 memset(&hd, 0, sizeof(hd));
42 ntimer = hpet_readl(HPET_ID);
43 ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
47 * Register with driver.
48 * Timer0 and Timer1 is used by platform.
50 hd.hd_phys_address = hpet_address;
51 hd.hd_address = (void __iomem *)fix_to_virt(FIX_HPET_BASE);
53 hd.hd_flags = HPET_DATA_PLATFORM;
54 hpet_reserve_timer(&hd, 0);
55 #ifdef CONFIG_HPET_EMULATE_RTC
56 hpet_reserve_timer(&hd, 1);
58 hd.hd_irq[0] = HPET_LEGACY_8254;
59 hd.hd_irq[1] = HPET_LEGACY_RTC;
62 struct hpet_timer *timer;
65 hpet = (struct hpet *) fix_to_virt(FIX_HPET_BASE);
66 timer = &hpet->hpet_timers[2];
67 for (i = 2; i < ntimer; timer++, i++)
68 hd.hd_irq[i] = (timer->hpet_config &
69 Tn_INT_ROUTE_CNF_MASK) >>
70 Tn_INT_ROUTE_CNF_SHIFT;
77 fs_initcall(late_hpet_init);
80 int hpet_timer_stop_set_go(unsigned long tick)
85 * Stop the timers and reset the main counter.
88 cfg = hpet_readl(HPET_CFG);
89 cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
90 hpet_writel(cfg, HPET_CFG);
91 hpet_writel(0, HPET_COUNTER);
92 hpet_writel(0, HPET_COUNTER + 4);
95 * Set up timer 0, as periodic with first interrupt to happen at hpet_tick,
96 * and period also hpet_tick.
99 hpet_writel(HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |
100 HPET_TN_32BIT, HPET_T0_CFG);
101 hpet_writel(hpet_tick, HPET_T0_CMP); /* next interrupt */
102 hpet_writel(hpet_tick, HPET_T0_CMP); /* period */
103 cfg |= HPET_CFG_LEGACY;
109 cfg |= HPET_CFG_ENABLE;
110 hpet_writel(cfg, HPET_CFG);
115 static cycle_t read_hpet(void)
117 return (cycle_t)hpet_readl(HPET_COUNTER);
120 static cycle_t __vsyscall_fn vread_hpet(void)
122 return readl((void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
125 struct clocksource clocksource_hpet = {
129 .mask = (cycle_t)HPET_MASK,
130 .mult = 0, /* set below */
132 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
136 int hpet_arch_init(void)
143 set_fixmap_nocache(FIX_HPET_BASE, hpet_address);
144 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
147 * Read the period, compute tick and quotient.
150 id = hpet_readl(HPET_ID);
152 if (!(id & HPET_ID_VENDOR) || !(id & HPET_ID_NUMBER))
155 hpet_period = hpet_readl(HPET_PERIOD);
156 if (hpet_period < 100000 || hpet_period > 100000000)
159 hpet_tick = (FSEC_PER_TICK + hpet_period / 2) / hpet_period;
161 hpet_use_timer = (id & HPET_ID_LEGSUP);
164 * hpet period is in femto seconds per cycle
165 * so we need to convert this to ns/cyc units
166 * aproximated by mult/2^shift
168 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
169 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
170 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
171 * (fsec/cyc << shift)/1000000 = mult
172 * (hpet_period << shift)/FSEC_PER_NSEC = mult
174 tmp = (u64)hpet_period << HPET_SHIFT;
175 do_div(tmp, FSEC_PER_NSEC);
176 clocksource_hpet.mult = (u32)tmp;
177 clocksource_register(&clocksource_hpet);
179 return hpet_timer_stop_set_go(hpet_tick);
182 int hpet_reenable(void)
184 return hpet_timer_stop_set_go(hpet_tick);
188 * calibrate_tsc() calibrates the processor TSC in a very simple way, comparing
189 * it to the HPET timer of known frequency.
192 #define TICK_COUNT 100000000
193 #define TICK_MIN 5000
196 * Some platforms take periodic SMI interrupts with 5ms duration. Make sure none
197 * occurs between the reads of the hpet & TSC.
199 static void __init read_hpet_tsc(int *hpet, int *tsc)
201 int tsc1, tsc2, hpet1;
204 tsc1 = get_cycles_sync();
205 hpet1 = hpet_readl(HPET_COUNTER);
206 tsc2 = get_cycles_sync();
207 } while (tsc2 - tsc1 > TICK_MIN);
212 unsigned int __init hpet_calibrate_tsc(void)
214 int tsc_start, hpet_start;
215 int tsc_now, hpet_now;
218 local_irq_save(flags);
220 read_hpet_tsc(&hpet_start, &tsc_start);
224 read_hpet_tsc(&hpet_now, &tsc_now);
225 local_irq_restore(flags);
226 } while ((tsc_now - tsc_start) < TICK_COUNT &&
227 (hpet_now - hpet_start) < TICK_COUNT);
229 return (tsc_now - tsc_start) * 1000000000L
230 / ((hpet_now - hpet_start) * hpet_period / 1000);
233 #ifdef CONFIG_HPET_EMULATE_RTC
234 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
235 * is enabled, we support RTC interrupt functionality in software.
236 * RTC has 3 kinds of interrupts:
237 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
239 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
240 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
241 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
242 * (1) and (2) above are implemented using polling at a frequency of
243 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
244 * overhead. (DEFAULT_RTC_INT_FREQ)
245 * For (3), we use interrupts at 64Hz or user specified periodic
246 * frequency, whichever is higher.
248 #include <linux/rtc.h>
250 #define DEFAULT_RTC_INT_FREQ 64
251 #define RTC_NUM_INTS 1
253 static unsigned long UIE_on;
254 static unsigned long prev_update_sec;
256 static unsigned long AIE_on;
257 static struct rtc_time alarm_time;
259 static unsigned long PIE_on;
260 static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
261 static unsigned long PIE_count;
263 static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
264 static unsigned int hpet_t1_cmp; /* cached comparator register */
266 int is_hpet_enabled(void)
268 return hpet_address != 0;
272 * Timer 1 for RTC, we do not use periodic interrupt feature,
273 * even if HPET supports periodic interrupts on Timer 1.
274 * The reason being, to set up a periodic interrupt in HPET, we need to
275 * stop the main counter. And if we do that everytime someone diables/enables
276 * RTC, we will have adverse effect on main kernel timer running on Timer 0.
277 * So, for the time being, simulate the periodic interrupt in software.
279 * hpet_rtc_timer_init() is called for the first time and during subsequent
280 * interuppts reinit happens through hpet_rtc_timer_reinit().
282 int hpet_rtc_timer_init(void)
284 unsigned int cfg, cnt;
287 if (!is_hpet_enabled())
290 * Set the counter 1 and enable the interrupts.
292 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
293 hpet_rtc_int_freq = PIE_freq;
295 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
297 local_irq_save(flags);
299 cnt = hpet_readl(HPET_COUNTER);
300 cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
301 hpet_writel(cnt, HPET_T1_CMP);
304 cfg = hpet_readl(HPET_T1_CFG);
305 cfg &= ~HPET_TN_PERIODIC;
306 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
307 hpet_writel(cfg, HPET_T1_CFG);
309 local_irq_restore(flags);
314 static void hpet_rtc_timer_reinit(void)
316 unsigned int cfg, cnt, ticks_per_int, lost_ints;
318 if (unlikely(!(PIE_on | AIE_on | UIE_on))) {
319 cfg = hpet_readl(HPET_T1_CFG);
320 cfg &= ~HPET_TN_ENABLE;
321 hpet_writel(cfg, HPET_T1_CFG);
325 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
326 hpet_rtc_int_freq = PIE_freq;
328 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
330 /* It is more accurate to use the comparator value than current count.*/
331 ticks_per_int = hpet_tick * HZ / hpet_rtc_int_freq;
332 hpet_t1_cmp += ticks_per_int;
333 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
336 * If the interrupt handler was delayed too long, the write above tries
337 * to schedule the next interrupt in the past and the hardware would
338 * not interrupt until the counter had wrapped around.
339 * So we have to check that the comparator wasn't set to a past time.
341 cnt = hpet_readl(HPET_COUNTER);
342 if (unlikely((int)(cnt - hpet_t1_cmp) > 0)) {
343 lost_ints = (cnt - hpet_t1_cmp) / ticks_per_int + 1;
344 /* Make sure that, even with the time needed to execute
345 * this code, the next scheduled interrupt has been moved
346 * back to the future: */
349 hpet_t1_cmp += lost_ints * ticks_per_int;
350 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
353 PIE_count += lost_ints;
355 if (printk_ratelimit())
356 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
362 * The functions below are called from rtc driver.
363 * Return 0 if HPET is not being used.
364 * Otherwise do the necessary changes and return 1.
366 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
368 if (!is_hpet_enabled())
371 if (bit_mask & RTC_UIE)
373 if (bit_mask & RTC_PIE)
375 if (bit_mask & RTC_AIE)
381 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
383 int timer_init_reqd = 0;
385 if (!is_hpet_enabled())
388 if (!(PIE_on | AIE_on | UIE_on))
391 if (bit_mask & RTC_UIE) {
394 if (bit_mask & RTC_PIE) {
398 if (bit_mask & RTC_AIE) {
403 hpet_rtc_timer_init();
408 int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
410 if (!is_hpet_enabled())
413 alarm_time.tm_hour = hrs;
414 alarm_time.tm_min = min;
415 alarm_time.tm_sec = sec;
420 int hpet_set_periodic_freq(unsigned long freq)
422 if (!is_hpet_enabled())
431 int hpet_rtc_dropped_irq(void)
433 if (!is_hpet_enabled())
439 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
441 struct rtc_time curr_time;
442 unsigned long rtc_int_flag = 0;
443 int call_rtc_interrupt = 0;
445 hpet_rtc_timer_reinit();
447 if (UIE_on | AIE_on) {
448 rtc_get_rtc_time(&curr_time);
451 if (curr_time.tm_sec != prev_update_sec) {
452 /* Set update int info, call real rtc int routine */
453 call_rtc_interrupt = 1;
454 rtc_int_flag = RTC_UF;
455 prev_update_sec = curr_time.tm_sec;
460 if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
461 /* Set periodic int info, call real rtc int routine */
462 call_rtc_interrupt = 1;
463 rtc_int_flag |= RTC_PF;
468 if ((curr_time.tm_sec == alarm_time.tm_sec) &&
469 (curr_time.tm_min == alarm_time.tm_min) &&
470 (curr_time.tm_hour == alarm_time.tm_hour)) {
471 /* Set alarm int info, call real rtc int routine */
472 call_rtc_interrupt = 1;
473 rtc_int_flag |= RTC_AF;
476 if (call_rtc_interrupt) {
477 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
478 rtc_interrupt(rtc_int_flag, dev_id);
484 static int __init nohpet_setup(char *s)
490 __setup("nohpet", nohpet_setup);