1 #include <linux/clocksource.h>
2 #include <linux/clockchips.h>
3 #include <linux/delay.h>
4 #include <linux/errno.h>
5 #include <linux/hpet.h>
6 #include <linux/init.h>
7 #include <linux/sysdev.h>
10 #include <asm/fixmap.h>
12 #include <asm/i8253.h>
15 #define HPET_MASK CLOCKSOURCE_MASK(32)
20 #define FSEC_PER_NSEC 1000000
23 * HPET address is set in acpi/boot.c, when an ACPI entry exists
25 unsigned long hpet_address;
26 static void __iomem *hpet_virt_address;
28 unsigned long hpet_readl(unsigned long a)
30 return readl(hpet_virt_address + a);
33 static inline void hpet_writel(unsigned long d, unsigned long a)
35 writel(d, hpet_virt_address + a);
40 #include <asm/pgtable.h>
42 static inline void hpet_set_mapping(void)
44 set_fixmap_nocache(FIX_HPET_BASE, hpet_address);
45 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
46 hpet_virt_address = (void __iomem *)fix_to_virt(FIX_HPET_BASE);
49 static inline void hpet_clear_mapping(void)
51 hpet_virt_address = NULL;
56 static inline void hpet_set_mapping(void)
58 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
61 static inline void hpet_clear_mapping(void)
63 iounmap(hpet_virt_address);
64 hpet_virt_address = NULL;
69 * HPET command line enable / disable
71 static int boot_hpet_disable;
74 static int __init hpet_setup(char* str)
77 if (!strncmp("disable", str, 7))
78 boot_hpet_disable = 1;
79 if (!strncmp("force", str, 5))
84 __setup("hpet=", hpet_setup);
86 static int __init disable_hpet(char *str)
88 boot_hpet_disable = 1;
91 __setup("nohpet", disable_hpet);
93 static inline int is_hpet_capable(void)
95 return (!boot_hpet_disable && hpet_address);
99 * HPET timer interrupt enable / disable
101 static int hpet_legacy_int_enabled;
104 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
106 int is_hpet_enabled(void)
108 return is_hpet_capable() && hpet_legacy_int_enabled;
112 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
113 * timer 0 and timer 1 in case of RTC emulation.
116 static void hpet_reserve_platform_timers(unsigned long id)
118 struct hpet __iomem *hpet = hpet_virt_address;
119 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
120 unsigned int nrtimers, i;
123 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
125 memset(&hd, 0, sizeof (hd));
126 hd.hd_phys_address = hpet_address;
127 hd.hd_address = hpet;
128 hd.hd_nirqs = nrtimers;
129 hd.hd_flags = HPET_DATA_PLATFORM;
130 hpet_reserve_timer(&hd, 0);
132 #ifdef CONFIG_HPET_EMULATE_RTC
133 hpet_reserve_timer(&hd, 1);
135 hd.hd_irq[0] = HPET_LEGACY_8254;
136 hd.hd_irq[1] = HPET_LEGACY_RTC;
138 for (i = 2; i < nrtimers; timer++, i++)
139 hd.hd_irq[i] = (timer->hpet_config & Tn_INT_ROUTE_CNF_MASK) >>
140 Tn_INT_ROUTE_CNF_SHIFT;
144 static void hpet_reserve_platform_timers(unsigned long id) { }
150 static unsigned long hpet_period;
152 static void hpet_legacy_set_mode(enum clock_event_mode mode,
153 struct clock_event_device *evt);
154 static int hpet_legacy_next_event(unsigned long delta,
155 struct clock_event_device *evt);
158 * The hpet clock event device
160 static struct clock_event_device hpet_clockevent = {
162 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
163 .set_mode = hpet_legacy_set_mode,
164 .set_next_event = hpet_legacy_next_event,
170 static void hpet_start_counter(void)
172 unsigned long cfg = hpet_readl(HPET_CFG);
174 cfg &= ~HPET_CFG_ENABLE;
175 hpet_writel(cfg, HPET_CFG);
176 hpet_writel(0, HPET_COUNTER);
177 hpet_writel(0, HPET_COUNTER + 4);
178 cfg |= HPET_CFG_ENABLE;
179 hpet_writel(cfg, HPET_CFG);
182 static void hpet_resume_device(void)
187 static void hpet_restart_counter(void)
189 hpet_resume_device();
190 hpet_start_counter();
193 static void hpet_enable_legacy_int(void)
195 unsigned long cfg = hpet_readl(HPET_CFG);
197 cfg |= HPET_CFG_LEGACY;
198 hpet_writel(cfg, HPET_CFG);
199 hpet_legacy_int_enabled = 1;
202 static void hpet_legacy_clockevent_register(void)
206 /* Start HPET legacy interrupts */
207 hpet_enable_legacy_int();
210 * The period is a femto seconds value. We need to calculate the
211 * scaled math multiplication factor for nanosecond to hpet tick
214 hpet_freq = 1000000000000000ULL;
215 do_div(hpet_freq, hpet_period);
216 hpet_clockevent.mult = div_sc((unsigned long) hpet_freq,
218 /* Calculate the min / max delta */
219 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
221 hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30,
225 * Start hpet with the boot cpu mask and make it
226 * global after the IO_APIC has been initialized.
228 hpet_clockevent.cpumask = cpumask_of_cpu(smp_processor_id());
229 clockevents_register_device(&hpet_clockevent);
230 global_clock_event = &hpet_clockevent;
231 printk(KERN_DEBUG "hpet clockevent registered\n");
234 static void hpet_legacy_set_mode(enum clock_event_mode mode,
235 struct clock_event_device *evt)
237 unsigned long cfg, cmp, now;
241 case CLOCK_EVT_MODE_PERIODIC:
242 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
243 delta >>= hpet_clockevent.shift;
244 now = hpet_readl(HPET_COUNTER);
245 cmp = now + (unsigned long) delta;
246 cfg = hpet_readl(HPET_T0_CFG);
247 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
248 HPET_TN_SETVAL | HPET_TN_32BIT;
249 hpet_writel(cfg, HPET_T0_CFG);
251 * The first write after writing TN_SETVAL to the
252 * config register sets the counter value, the second
253 * write sets the period.
255 hpet_writel(cmp, HPET_T0_CMP);
257 hpet_writel((unsigned long) delta, HPET_T0_CMP);
260 case CLOCK_EVT_MODE_ONESHOT:
261 cfg = hpet_readl(HPET_T0_CFG);
262 cfg &= ~HPET_TN_PERIODIC;
263 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
264 hpet_writel(cfg, HPET_T0_CFG);
267 case CLOCK_EVT_MODE_UNUSED:
268 case CLOCK_EVT_MODE_SHUTDOWN:
269 cfg = hpet_readl(HPET_T0_CFG);
270 cfg &= ~HPET_TN_ENABLE;
271 hpet_writel(cfg, HPET_T0_CFG);
274 case CLOCK_EVT_MODE_RESUME:
275 hpet_enable_legacy_int();
280 static int hpet_legacy_next_event(unsigned long delta,
281 struct clock_event_device *evt)
285 cnt = hpet_readl(HPET_COUNTER);
287 hpet_writel(cnt, HPET_T0_CMP);
289 return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0) ? -ETIME : 0;
293 * Clock source related code
295 static cycle_t read_hpet(void)
297 return (cycle_t)hpet_readl(HPET_COUNTER);
301 static cycle_t __vsyscall_fn vread_hpet(void)
303 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
307 static struct clocksource clocksource_hpet = {
313 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
314 .resume = hpet_restart_counter,
320 static int hpet_clocksource_register(void)
325 /* Start the counter */
326 hpet_start_counter();
328 /* Verify whether hpet counter works */
333 * We don't know the TSC frequency yet, but waiting for
334 * 200000 TSC cycles is safe:
341 } while ((now - start) < 200000UL);
343 if (t1 == read_hpet()) {
345 "HPET counter not counting. HPET disabled\n");
349 /* Initialize and register HPET clocksource
351 * hpet period is in femto seconds per cycle
352 * so we need to convert this to ns/cyc units
353 * approximated by mult/2^shift
355 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
356 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
357 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
358 * (fsec/cyc << shift)/1000000 = mult
359 * (hpet_period << shift)/FSEC_PER_NSEC = mult
361 tmp = (u64)hpet_period << HPET_SHIFT;
362 do_div(tmp, FSEC_PER_NSEC);
363 clocksource_hpet.mult = (u32)tmp;
365 clocksource_register(&clocksource_hpet);
371 * Try to setup the HPET timer
373 int __init hpet_enable(void)
377 if (!is_hpet_capable())
383 * Read the period and check for a sane value:
385 hpet_period = hpet_readl(HPET_PERIOD);
386 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
390 * Read the HPET ID register to retrieve the IRQ routing
391 * information and the number of channels
393 id = hpet_readl(HPET_ID);
395 #ifdef CONFIG_HPET_EMULATE_RTC
397 * The legacy routing mode needs at least two channels, tick timer
398 * and the rtc emulation channel.
400 if (!(id & HPET_ID_NUMBER))
404 if (hpet_clocksource_register())
407 if (id & HPET_ID_LEGSUP) {
408 hpet_legacy_clockevent_register();
414 hpet_clear_mapping();
415 boot_hpet_disable = 1;
420 * Needs to be late, as the reserve_timer code calls kalloc !
422 * Not a problem on i386 as hpet_enable is called from late_time_init,
423 * but on x86_64 it is necessary !
425 static __init int hpet_late_init(void)
427 if (boot_hpet_disable)
431 if (!force_hpet_address)
434 hpet_address = force_hpet_address;
436 if (!hpet_virt_address)
440 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
444 fs_initcall(hpet_late_init);
446 void hpet_disable(void)
448 if (is_hpet_capable()) {
449 unsigned long cfg = hpet_readl(HPET_CFG);
451 if (hpet_legacy_int_enabled) {
452 cfg &= ~HPET_CFG_LEGACY;
453 hpet_legacy_int_enabled = 0;
455 cfg &= ~HPET_CFG_ENABLE;
456 hpet_writel(cfg, HPET_CFG);
460 #ifdef CONFIG_HPET_EMULATE_RTC
462 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
463 * is enabled, we support RTC interrupt functionality in software.
464 * RTC has 3 kinds of interrupts:
465 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
467 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
468 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
469 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
470 * (1) and (2) above are implemented using polling at a frequency of
471 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
472 * overhead. (DEFAULT_RTC_INT_FREQ)
473 * For (3), we use interrupts at 64Hz or user specified periodic
474 * frequency, whichever is higher.
476 #include <linux/mc146818rtc.h>
477 #include <linux/rtc.h>
479 #define DEFAULT_RTC_INT_FREQ 64
480 #define DEFAULT_RTC_SHIFT 6
481 #define RTC_NUM_INTS 1
483 static unsigned long hpet_rtc_flags;
484 static unsigned long hpet_prev_update_sec;
485 static struct rtc_time hpet_alarm_time;
486 static unsigned long hpet_pie_count;
487 static unsigned long hpet_t1_cmp;
488 static unsigned long hpet_default_delta;
489 static unsigned long hpet_pie_delta;
490 static unsigned long hpet_pie_limit;
493 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
494 * is not supported by all HPET implementations for timer 1.
496 * hpet_rtc_timer_init() is called when the rtc is initialized.
498 int hpet_rtc_timer_init(void)
500 unsigned long cfg, cnt, delta, flags;
502 if (!is_hpet_enabled())
505 if (!hpet_default_delta) {
508 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
509 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
510 hpet_default_delta = (unsigned long) clc;
513 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
514 delta = hpet_default_delta;
516 delta = hpet_pie_delta;
518 local_irq_save(flags);
520 cnt = delta + hpet_readl(HPET_COUNTER);
521 hpet_writel(cnt, HPET_T1_CMP);
524 cfg = hpet_readl(HPET_T1_CFG);
525 cfg &= ~HPET_TN_PERIODIC;
526 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
527 hpet_writel(cfg, HPET_T1_CFG);
529 local_irq_restore(flags);
535 * The functions below are called from rtc driver.
536 * Return 0 if HPET is not being used.
537 * Otherwise do the necessary changes and return 1.
539 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
541 if (!is_hpet_enabled())
544 hpet_rtc_flags &= ~bit_mask;
548 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
550 unsigned long oldbits = hpet_rtc_flags;
552 if (!is_hpet_enabled())
555 hpet_rtc_flags |= bit_mask;
558 hpet_rtc_timer_init();
563 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
566 if (!is_hpet_enabled())
569 hpet_alarm_time.tm_hour = hrs;
570 hpet_alarm_time.tm_min = min;
571 hpet_alarm_time.tm_sec = sec;
576 int hpet_set_periodic_freq(unsigned long freq)
580 if (!is_hpet_enabled())
583 if (freq <= DEFAULT_RTC_INT_FREQ)
584 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
586 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
588 clc >>= hpet_clockevent.shift;
589 hpet_pie_delta = (unsigned long) clc;
594 int hpet_rtc_dropped_irq(void)
596 return is_hpet_enabled();
599 static void hpet_rtc_timer_reinit(void)
601 unsigned long cfg, delta;
604 if (unlikely(!hpet_rtc_flags)) {
605 cfg = hpet_readl(HPET_T1_CFG);
606 cfg &= ~HPET_TN_ENABLE;
607 hpet_writel(cfg, HPET_T1_CFG);
611 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
612 delta = hpet_default_delta;
614 delta = hpet_pie_delta;
617 * Increment the comparator value until we are ahead of the
621 hpet_t1_cmp += delta;
622 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
624 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
627 if (hpet_rtc_flags & RTC_PIE)
628 hpet_pie_count += lost_ints;
629 if (printk_ratelimit())
630 printk(KERN_WARNING "rtc: lost %d interrupts\n",
635 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
637 struct rtc_time curr_time;
638 unsigned long rtc_int_flag = 0;
640 hpet_rtc_timer_reinit();
642 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
643 rtc_get_rtc_time(&curr_time);
645 if (hpet_rtc_flags & RTC_UIE &&
646 curr_time.tm_sec != hpet_prev_update_sec) {
647 rtc_int_flag = RTC_UF;
648 hpet_prev_update_sec = curr_time.tm_sec;
651 if (hpet_rtc_flags & RTC_PIE &&
652 ++hpet_pie_count >= hpet_pie_limit) {
653 rtc_int_flag |= RTC_PF;
657 if (hpet_rtc_flags & RTC_AIE &&
658 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
659 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
660 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
661 rtc_int_flag |= RTC_AF;
664 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
665 rtc_interrupt(rtc_int_flag, dev_id);