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 1000000L
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;
110 EXPORT_SYMBOL_GPL(is_hpet_enabled);
113 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
114 * timer 0 and timer 1 in case of RTC emulation.
117 static void hpet_reserve_platform_timers(unsigned long id)
119 struct hpet __iomem *hpet = hpet_virt_address;
120 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
121 unsigned int nrtimers, i;
124 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
126 memset(&hd, 0, sizeof (hd));
127 hd.hd_phys_address = hpet_address;
128 hd.hd_address = hpet;
129 hd.hd_nirqs = nrtimers;
130 hd.hd_flags = HPET_DATA_PLATFORM;
131 hpet_reserve_timer(&hd, 0);
133 #ifdef CONFIG_HPET_EMULATE_RTC
134 hpet_reserve_timer(&hd, 1);
137 hd.hd_irq[0] = HPET_LEGACY_8254;
138 hd.hd_irq[1] = HPET_LEGACY_RTC;
140 for (i = 2; i < nrtimers; timer++, i++) {
141 hd.hd_irq[i] = (readl(&timer->hpet_config) & Tn_INT_ROUTE_CNF_MASK) >>
142 Tn_INT_ROUTE_CNF_SHIFT;
149 static void hpet_reserve_platform_timers(unsigned long id) { }
155 static unsigned long hpet_period;
157 static void hpet_legacy_set_mode(enum clock_event_mode mode,
158 struct clock_event_device *evt);
159 static int hpet_legacy_next_event(unsigned long delta,
160 struct clock_event_device *evt);
163 * The hpet clock event device
165 static struct clock_event_device hpet_clockevent = {
167 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
168 .set_mode = hpet_legacy_set_mode,
169 .set_next_event = hpet_legacy_next_event,
175 static void hpet_start_counter(void)
177 unsigned long cfg = hpet_readl(HPET_CFG);
179 cfg &= ~HPET_CFG_ENABLE;
180 hpet_writel(cfg, HPET_CFG);
181 hpet_writel(0, HPET_COUNTER);
182 hpet_writel(0, HPET_COUNTER + 4);
183 cfg |= HPET_CFG_ENABLE;
184 hpet_writel(cfg, HPET_CFG);
187 static void hpet_resume_device(void)
192 static void hpet_restart_counter(void)
194 hpet_resume_device();
195 hpet_start_counter();
198 static void hpet_enable_legacy_int(void)
200 unsigned long cfg = hpet_readl(HPET_CFG);
202 cfg |= HPET_CFG_LEGACY;
203 hpet_writel(cfg, HPET_CFG);
204 hpet_legacy_int_enabled = 1;
207 static void hpet_legacy_clockevent_register(void)
209 /* Start HPET legacy interrupts */
210 hpet_enable_legacy_int();
213 * The mult factor is defined as (include/linux/clockchips.h)
214 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
215 * hpet_period is in units of femtoseconds (per cycle), so
216 * mult/2^shift = cyc/ns = 10^6/hpet_period
217 * mult = (10^6 * 2^shift)/hpet_period
218 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
220 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
221 hpet_period, hpet_clockevent.shift);
222 /* Calculate the min / max delta */
223 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
225 hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30,
229 * Start hpet with the boot cpu mask and make it
230 * global after the IO_APIC has been initialized.
232 hpet_clockevent.cpumask = cpumask_of_cpu(smp_processor_id());
233 clockevents_register_device(&hpet_clockevent);
234 global_clock_event = &hpet_clockevent;
235 printk(KERN_DEBUG "hpet clockevent registered\n");
238 static void hpet_legacy_set_mode(enum clock_event_mode mode,
239 struct clock_event_device *evt)
241 unsigned long cfg, cmp, now;
245 case CLOCK_EVT_MODE_PERIODIC:
246 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
247 delta >>= hpet_clockevent.shift;
248 now = hpet_readl(HPET_COUNTER);
249 cmp = now + (unsigned long) delta;
250 cfg = hpet_readl(HPET_T0_CFG);
251 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
252 HPET_TN_SETVAL | HPET_TN_32BIT;
253 hpet_writel(cfg, HPET_T0_CFG);
255 * The first write after writing TN_SETVAL to the
256 * config register sets the counter value, the second
257 * write sets the period.
259 hpet_writel(cmp, HPET_T0_CMP);
261 hpet_writel((unsigned long) delta, HPET_T0_CMP);
264 case CLOCK_EVT_MODE_ONESHOT:
265 cfg = hpet_readl(HPET_T0_CFG);
266 cfg &= ~HPET_TN_PERIODIC;
267 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
268 hpet_writel(cfg, HPET_T0_CFG);
271 case CLOCK_EVT_MODE_UNUSED:
272 case CLOCK_EVT_MODE_SHUTDOWN:
273 cfg = hpet_readl(HPET_T0_CFG);
274 cfg &= ~HPET_TN_ENABLE;
275 hpet_writel(cfg, HPET_T0_CFG);
278 case CLOCK_EVT_MODE_RESUME:
279 hpet_enable_legacy_int();
284 static int hpet_legacy_next_event(unsigned long delta,
285 struct clock_event_device *evt)
289 cnt = hpet_readl(HPET_COUNTER);
291 hpet_writel(cnt, HPET_T0_CMP);
293 return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0) ? -ETIME : 0;
297 * Clock source related code
299 static cycle_t read_hpet(void)
301 return (cycle_t)hpet_readl(HPET_COUNTER);
305 static cycle_t __vsyscall_fn vread_hpet(void)
307 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
311 static struct clocksource clocksource_hpet = {
317 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
318 .resume = hpet_restart_counter,
324 static int hpet_clocksource_register(void)
329 /* Start the counter */
330 hpet_start_counter();
332 /* Verify whether hpet counter works */
337 * We don't know the TSC frequency yet, but waiting for
338 * 200000 TSC cycles is safe:
345 } while ((now - start) < 200000UL);
347 if (t1 == read_hpet()) {
349 "HPET counter not counting. HPET disabled\n");
354 * The definition of mult is (include/linux/clocksource.h)
355 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
356 * so we first need to convert hpet_period to ns/cyc units:
357 * mult/2^shift = ns/cyc = hpet_period/10^6
358 * mult = (hpet_period * 2^shift)/10^6
359 * mult = (hpet_period << shift)/FSEC_PER_NSEC
361 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
363 clocksource_register(&clocksource_hpet);
369 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
371 int __init hpet_enable(void)
375 if (!is_hpet_capable())
381 * Read the period and check for a sane value:
383 hpet_period = hpet_readl(HPET_PERIOD);
384 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
388 * Read the HPET ID register to retrieve the IRQ routing
389 * information and the number of channels
391 id = hpet_readl(HPET_ID);
393 #ifdef CONFIG_HPET_EMULATE_RTC
395 * The legacy routing mode needs at least two channels, tick timer
396 * and the rtc emulation channel.
398 if (!(id & HPET_ID_NUMBER))
402 if (hpet_clocksource_register())
405 if (id & HPET_ID_LEGSUP) {
406 hpet_legacy_clockevent_register();
412 hpet_clear_mapping();
413 boot_hpet_disable = 1;
418 * Needs to be late, as the reserve_timer code calls kalloc !
420 * Not a problem on i386 as hpet_enable is called from late_time_init,
421 * but on x86_64 it is necessary !
423 static __init int hpet_late_init(void)
425 if (boot_hpet_disable)
429 if (!force_hpet_address)
432 hpet_address = force_hpet_address;
434 if (!hpet_virt_address)
438 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
442 fs_initcall(hpet_late_init);
444 void hpet_disable(void)
446 if (is_hpet_capable()) {
447 unsigned long cfg = hpet_readl(HPET_CFG);
449 if (hpet_legacy_int_enabled) {
450 cfg &= ~HPET_CFG_LEGACY;
451 hpet_legacy_int_enabled = 0;
453 cfg &= ~HPET_CFG_ENABLE;
454 hpet_writel(cfg, HPET_CFG);
458 #ifdef CONFIG_HPET_EMULATE_RTC
460 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
461 * is enabled, we support RTC interrupt functionality in software.
462 * RTC has 3 kinds of interrupts:
463 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
465 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
466 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
467 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
468 * (1) and (2) above are implemented using polling at a frequency of
469 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
470 * overhead. (DEFAULT_RTC_INT_FREQ)
471 * For (3), we use interrupts at 64Hz or user specified periodic
472 * frequency, whichever is higher.
474 #include <linux/mc146818rtc.h>
475 #include <linux/rtc.h>
478 #define DEFAULT_RTC_INT_FREQ 64
479 #define DEFAULT_RTC_SHIFT 6
480 #define RTC_NUM_INTS 1
482 static unsigned long hpet_rtc_flags;
483 static unsigned long hpet_prev_update_sec;
484 static struct rtc_time hpet_alarm_time;
485 static unsigned long hpet_pie_count;
486 static unsigned long hpet_t1_cmp;
487 static unsigned long hpet_default_delta;
488 static unsigned long hpet_pie_delta;
489 static unsigned long hpet_pie_limit;
491 static rtc_irq_handler irq_handler;
494 * Registers a IRQ handler.
496 int hpet_register_irq_handler(rtc_irq_handler handler)
498 if (!is_hpet_enabled())
503 irq_handler = handler;
507 EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
510 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
513 void hpet_unregister_irq_handler(rtc_irq_handler handler)
515 if (!is_hpet_enabled())
521 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
524 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
525 * is not supported by all HPET implementations for timer 1.
527 * hpet_rtc_timer_init() is called when the rtc is initialized.
529 int hpet_rtc_timer_init(void)
531 unsigned long cfg, cnt, delta, flags;
533 if (!is_hpet_enabled())
536 if (!hpet_default_delta) {
539 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
540 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
541 hpet_default_delta = (unsigned long) clc;
544 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
545 delta = hpet_default_delta;
547 delta = hpet_pie_delta;
549 local_irq_save(flags);
551 cnt = delta + hpet_readl(HPET_COUNTER);
552 hpet_writel(cnt, HPET_T1_CMP);
555 cfg = hpet_readl(HPET_T1_CFG);
556 cfg &= ~HPET_TN_PERIODIC;
557 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
558 hpet_writel(cfg, HPET_T1_CFG);
560 local_irq_restore(flags);
564 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
567 * The functions below are called from rtc driver.
568 * Return 0 if HPET is not being used.
569 * Otherwise do the necessary changes and return 1.
571 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
573 if (!is_hpet_enabled())
576 hpet_rtc_flags &= ~bit_mask;
579 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
581 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
583 unsigned long oldbits = hpet_rtc_flags;
585 if (!is_hpet_enabled())
588 hpet_rtc_flags |= bit_mask;
591 hpet_rtc_timer_init();
595 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
597 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
600 if (!is_hpet_enabled())
603 hpet_alarm_time.tm_hour = hrs;
604 hpet_alarm_time.tm_min = min;
605 hpet_alarm_time.tm_sec = sec;
609 EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
611 int hpet_set_periodic_freq(unsigned long freq)
615 if (!is_hpet_enabled())
618 if (freq <= DEFAULT_RTC_INT_FREQ)
619 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
621 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
623 clc >>= hpet_clockevent.shift;
624 hpet_pie_delta = (unsigned long) clc;
628 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
630 int hpet_rtc_dropped_irq(void)
632 return is_hpet_enabled();
634 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
636 static void hpet_rtc_timer_reinit(void)
638 unsigned long cfg, delta;
641 if (unlikely(!hpet_rtc_flags)) {
642 cfg = hpet_readl(HPET_T1_CFG);
643 cfg &= ~HPET_TN_ENABLE;
644 hpet_writel(cfg, HPET_T1_CFG);
648 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
649 delta = hpet_default_delta;
651 delta = hpet_pie_delta;
654 * Increment the comparator value until we are ahead of the
658 hpet_t1_cmp += delta;
659 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
661 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
664 if (hpet_rtc_flags & RTC_PIE)
665 hpet_pie_count += lost_ints;
666 if (printk_ratelimit())
667 printk(KERN_WARNING "rtc: lost %d interrupts\n",
672 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
674 struct rtc_time curr_time;
675 unsigned long rtc_int_flag = 0;
677 hpet_rtc_timer_reinit();
678 memset(&curr_time, 0, sizeof(struct rtc_time));
680 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
681 get_rtc_time(&curr_time);
683 if (hpet_rtc_flags & RTC_UIE &&
684 curr_time.tm_sec != hpet_prev_update_sec) {
685 rtc_int_flag = RTC_UF;
686 hpet_prev_update_sec = curr_time.tm_sec;
689 if (hpet_rtc_flags & RTC_PIE &&
690 ++hpet_pie_count >= hpet_pie_limit) {
691 rtc_int_flag |= RTC_PF;
695 if (hpet_rtc_flags & RTC_AIE &&
696 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
697 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
698 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
699 rtc_int_flag |= RTC_AF;
702 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
704 irq_handler(rtc_int_flag, dev_id);
708 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);