1 #include <linux/clocksource.h>
2 #include <linux/clockchips.h>
3 #include <linux/interrupt.h>
4 #include <linux/sysdev.h>
5 #include <linux/delay.h>
6 #include <linux/errno.h>
7 #include <linux/hpet.h>
8 #include <linux/init.h>
13 #include <asm/fixmap.h>
14 #include <asm/i8253.h>
17 #define HPET_MASK CLOCKSOURCE_MASK(32)
22 #define FSEC_PER_NSEC 1000000L
24 #define HPET_DEV_USED_BIT 2
25 #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
26 #define HPET_DEV_VALID 0x8
27 #define HPET_DEV_FSB_CAP 0x1000
28 #define HPET_DEV_PERI_CAP 0x2000
30 #define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
33 * HPET address is set in acpi/boot.c, when an ACPI entry exists
35 unsigned long hpet_address;
39 static unsigned long hpet_num_timers;
41 static void __iomem *hpet_virt_address;
44 struct clock_event_device evt;
52 unsigned long hpet_readl(unsigned long a)
54 return readl(hpet_virt_address + a);
57 static inline void hpet_writel(unsigned long d, unsigned long a)
59 writel(d, hpet_virt_address + a);
63 #include <asm/pgtable.h>
66 static inline void hpet_set_mapping(void)
68 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
70 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
74 static inline void hpet_clear_mapping(void)
76 iounmap(hpet_virt_address);
77 hpet_virt_address = NULL;
81 * HPET command line enable / disable
83 static int boot_hpet_disable;
85 static int hpet_verbose;
87 static int __init hpet_setup(char *str)
90 if (!strncmp("disable", str, 7))
91 boot_hpet_disable = 1;
92 if (!strncmp("force", str, 5))
94 if (!strncmp("verbose", str, 7))
99 __setup("hpet=", hpet_setup);
101 static int __init disable_hpet(char *str)
103 boot_hpet_disable = 1;
106 __setup("nohpet", disable_hpet);
108 static inline int is_hpet_capable(void)
110 return !boot_hpet_disable && hpet_address;
114 * HPET timer interrupt enable / disable
116 static int hpet_legacy_int_enabled;
119 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
121 int is_hpet_enabled(void)
123 return is_hpet_capable() && hpet_legacy_int_enabled;
125 EXPORT_SYMBOL_GPL(is_hpet_enabled);
127 static void _hpet_print_config(const char *function, int line)
130 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
131 l = hpet_readl(HPET_ID);
132 h = hpet_readl(HPET_PERIOD);
133 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
134 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
135 l = hpet_readl(HPET_CFG);
136 h = hpet_readl(HPET_STATUS);
137 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
138 l = hpet_readl(HPET_COUNTER);
139 h = hpet_readl(HPET_COUNTER+4);
140 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
142 for (i = 0; i < timers; i++) {
143 l = hpet_readl(HPET_Tn_CFG(i));
144 h = hpet_readl(HPET_Tn_CFG(i)+4);
145 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
147 l = hpet_readl(HPET_Tn_CMP(i));
148 h = hpet_readl(HPET_Tn_CMP(i)+4);
149 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
151 l = hpet_readl(HPET_Tn_ROUTE(i));
152 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
153 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
158 #define hpet_print_config() \
161 _hpet_print_config(__FUNCTION__, __LINE__); \
165 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
166 * timer 0 and timer 1 in case of RTC emulation.
170 static void hpet_reserve_msi_timers(struct hpet_data *hd);
172 static void hpet_reserve_platform_timers(unsigned long id)
174 struct hpet __iomem *hpet = hpet_virt_address;
175 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
176 unsigned int nrtimers, i;
179 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
181 memset(&hd, 0, sizeof(hd));
182 hd.hd_phys_address = hpet_address;
183 hd.hd_address = hpet;
184 hd.hd_nirqs = nrtimers;
185 hpet_reserve_timer(&hd, 0);
187 #ifdef CONFIG_HPET_EMULATE_RTC
188 hpet_reserve_timer(&hd, 1);
192 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
193 * is wrong for i8259!) not the output IRQ. Many BIOS writers
194 * don't bother configuring *any* comparator interrupts.
196 hd.hd_irq[0] = HPET_LEGACY_8254;
197 hd.hd_irq[1] = HPET_LEGACY_RTC;
199 for (i = 2; i < nrtimers; timer++, i++) {
200 hd.hd_irq[i] = (readl(&timer->hpet_config) &
201 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
204 hpet_reserve_msi_timers(&hd);
210 static void hpet_reserve_platform_timers(unsigned long id) { }
216 static unsigned long hpet_period;
218 static void hpet_legacy_set_mode(enum clock_event_mode mode,
219 struct clock_event_device *evt);
220 static int hpet_legacy_next_event(unsigned long delta,
221 struct clock_event_device *evt);
224 * The hpet clock event device
226 static struct clock_event_device hpet_clockevent = {
228 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
229 .set_mode = hpet_legacy_set_mode,
230 .set_next_event = hpet_legacy_next_event,
236 static void hpet_stop_counter(void)
238 unsigned long cfg = hpet_readl(HPET_CFG);
239 cfg &= ~HPET_CFG_ENABLE;
240 hpet_writel(cfg, HPET_CFG);
243 static void hpet_reset_counter(void)
245 hpet_writel(0, HPET_COUNTER);
246 hpet_writel(0, HPET_COUNTER + 4);
249 static void hpet_start_counter(void)
251 unsigned long cfg = hpet_readl(HPET_CFG);
252 cfg |= HPET_CFG_ENABLE;
253 hpet_writel(cfg, HPET_CFG);
256 static void hpet_restart_counter(void)
259 hpet_reset_counter();
260 hpet_start_counter();
263 static void hpet_resume_device(void)
268 static void hpet_resume_counter(void)
270 hpet_resume_device();
271 hpet_restart_counter();
274 static void hpet_enable_legacy_int(void)
276 unsigned long cfg = hpet_readl(HPET_CFG);
278 cfg |= HPET_CFG_LEGACY;
279 hpet_writel(cfg, HPET_CFG);
280 hpet_legacy_int_enabled = 1;
283 static void hpet_legacy_clockevent_register(void)
285 /* Start HPET legacy interrupts */
286 hpet_enable_legacy_int();
289 * The mult factor is defined as (include/linux/clockchips.h)
290 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
291 * hpet_period is in units of femtoseconds (per cycle), so
292 * mult/2^shift = cyc/ns = 10^6/hpet_period
293 * mult = (10^6 * 2^shift)/hpet_period
294 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
296 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
297 hpet_period, hpet_clockevent.shift);
298 /* Calculate the min / max delta */
299 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
301 /* 5 usec minimum reprogramming delta. */
302 hpet_clockevent.min_delta_ns = 5000;
305 * Start hpet with the boot cpu mask and make it
306 * global after the IO_APIC has been initialized.
308 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
309 clockevents_register_device(&hpet_clockevent);
310 global_clock_event = &hpet_clockevent;
311 printk(KERN_DEBUG "hpet clockevent registered\n");
314 static int hpet_setup_msi_irq(unsigned int irq);
316 static void hpet_set_mode(enum clock_event_mode mode,
317 struct clock_event_device *evt, int timer)
319 unsigned long cfg, cmp, now;
323 case CLOCK_EVT_MODE_PERIODIC:
325 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
326 delta >>= evt->shift;
327 now = hpet_readl(HPET_COUNTER);
328 cmp = now + (unsigned long) delta;
329 cfg = hpet_readl(HPET_Tn_CFG(timer));
330 /* Make sure we use edge triggered interrupts */
331 cfg &= ~HPET_TN_LEVEL;
332 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
333 HPET_TN_SETVAL | HPET_TN_32BIT;
334 hpet_writel(cfg, HPET_Tn_CFG(timer));
335 hpet_writel(cmp, HPET_Tn_CMP(timer));
338 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
339 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
340 * bit is automatically cleared after the first write.
341 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
342 * Publication # 24674)
344 hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
345 hpet_start_counter();
349 case CLOCK_EVT_MODE_ONESHOT:
350 cfg = hpet_readl(HPET_Tn_CFG(timer));
351 cfg &= ~HPET_TN_PERIODIC;
352 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
353 hpet_writel(cfg, HPET_Tn_CFG(timer));
356 case CLOCK_EVT_MODE_UNUSED:
357 case CLOCK_EVT_MODE_SHUTDOWN:
358 cfg = hpet_readl(HPET_Tn_CFG(timer));
359 cfg &= ~HPET_TN_ENABLE;
360 hpet_writel(cfg, HPET_Tn_CFG(timer));
363 case CLOCK_EVT_MODE_RESUME:
365 hpet_enable_legacy_int();
367 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
368 hpet_setup_msi_irq(hdev->irq);
369 disable_irq(hdev->irq);
370 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
371 enable_irq(hdev->irq);
378 static int hpet_next_event(unsigned long delta,
379 struct clock_event_device *evt, int timer)
383 cnt = hpet_readl(HPET_COUNTER);
385 hpet_writel(cnt, HPET_Tn_CMP(timer));
388 * We need to read back the CMP register on certain HPET
389 * implementations (ATI chipsets) which seem to delay the
390 * transfer of the compare register into the internal compare
391 * logic. With small deltas this might actually be too late as
392 * the counter could already be higher than the compare value
393 * at that point and we would wait for the next hpet interrupt
394 * forever. We found out that reading the CMP register back
395 * forces the transfer so we can rely on the comparison with
396 * the counter register below. If the read back from the
397 * compare register does not match the value we programmed
398 * then we might have a real hardware problem. We can not do
399 * much about it here, but at least alert the user/admin with
400 * a prominent warning.
401 * An erratum on some chipsets (ICH9,..), results in comparator read
402 * immediately following a write returning old value. Workaround
403 * for this is to read this value second time, when first
404 * read returns old value.
406 if (unlikely((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt)) {
407 WARN_ONCE((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt,
408 KERN_WARNING "hpet: compare register read back failed.\n");
411 return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
414 static void hpet_legacy_set_mode(enum clock_event_mode mode,
415 struct clock_event_device *evt)
417 hpet_set_mode(mode, evt, 0);
420 static int hpet_legacy_next_event(unsigned long delta,
421 struct clock_event_device *evt)
423 return hpet_next_event(delta, evt, 0);
429 #ifdef CONFIG_PCI_MSI
431 static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
432 static struct hpet_dev *hpet_devs;
434 void hpet_msi_unmask(unsigned int irq)
436 struct hpet_dev *hdev = get_irq_data(irq);
440 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
442 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
445 void hpet_msi_mask(unsigned int irq)
448 struct hpet_dev *hdev = get_irq_data(irq);
451 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
453 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
456 void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
458 struct hpet_dev *hdev = get_irq_data(irq);
460 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
461 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
464 void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
466 struct hpet_dev *hdev = get_irq_data(irq);
468 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
469 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
473 static void hpet_msi_set_mode(enum clock_event_mode mode,
474 struct clock_event_device *evt)
476 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
477 hpet_set_mode(mode, evt, hdev->num);
480 static int hpet_msi_next_event(unsigned long delta,
481 struct clock_event_device *evt)
483 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
484 return hpet_next_event(delta, evt, hdev->num);
487 static int hpet_setup_msi_irq(unsigned int irq)
489 if (arch_setup_hpet_msi(irq)) {
496 static int hpet_assign_irq(struct hpet_dev *dev)
504 set_irq_data(irq, dev);
506 if (hpet_setup_msi_irq(irq))
513 static irqreturn_t hpet_interrupt_handler(int irq, void *data)
515 struct hpet_dev *dev = (struct hpet_dev *)data;
516 struct clock_event_device *hevt = &dev->evt;
518 if (!hevt->event_handler) {
519 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
524 hevt->event_handler(hevt);
528 static int hpet_setup_irq(struct hpet_dev *dev)
531 if (request_irq(dev->irq, hpet_interrupt_handler,
532 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
536 disable_irq(dev->irq);
537 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
538 enable_irq(dev->irq);
540 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
541 dev->name, dev->irq);
546 /* This should be called in specific @cpu */
547 static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
549 struct clock_event_device *evt = &hdev->evt;
552 WARN_ON(cpu != smp_processor_id());
553 if (!(hdev->flags & HPET_DEV_VALID))
556 if (hpet_setup_msi_irq(hdev->irq))
560 per_cpu(cpu_hpet_dev, cpu) = hdev;
561 evt->name = hdev->name;
562 hpet_setup_irq(hdev);
563 evt->irq = hdev->irq;
566 evt->features = CLOCK_EVT_FEAT_ONESHOT;
567 if (hdev->flags & HPET_DEV_PERI_CAP)
568 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
570 evt->set_mode = hpet_msi_set_mode;
571 evt->set_next_event = hpet_msi_next_event;
575 * The period is a femto seconds value. We need to calculate the
576 * scaled math multiplication factor for nanosecond to hpet tick
579 hpet_freq = 1000000000000000ULL;
580 do_div(hpet_freq, hpet_period);
581 evt->mult = div_sc((unsigned long) hpet_freq,
582 NSEC_PER_SEC, evt->shift);
583 /* Calculate the max delta */
584 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
585 /* 5 usec minimum reprogramming delta. */
586 evt->min_delta_ns = 5000;
588 evt->cpumask = cpumask_of(hdev->cpu);
589 clockevents_register_device(evt);
593 /* Reserve at least one timer for userspace (/dev/hpet) */
594 #define RESERVE_TIMERS 1
596 #define RESERVE_TIMERS 0
599 static void hpet_msi_capability_lookup(unsigned int start_timer)
602 unsigned int num_timers;
603 unsigned int num_timers_used = 0;
606 if (hpet_msi_disable)
609 id = hpet_readl(HPET_ID);
611 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
612 num_timers++; /* Value read out starts from 0 */
615 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
619 hpet_num_timers = num_timers;
621 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
622 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
623 unsigned long cfg = hpet_readl(HPET_Tn_CFG(i));
625 /* Only consider HPET timer with MSI support */
626 if (!(cfg & HPET_TN_FSB_CAP))
630 if (cfg & HPET_TN_PERIODIC_CAP)
631 hdev->flags |= HPET_DEV_PERI_CAP;
634 sprintf(hdev->name, "hpet%d", i);
635 if (hpet_assign_irq(hdev))
638 hdev->flags |= HPET_DEV_FSB_CAP;
639 hdev->flags |= HPET_DEV_VALID;
641 if (num_timers_used == num_possible_cpus())
645 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
646 num_timers, num_timers_used);
650 static void hpet_reserve_msi_timers(struct hpet_data *hd)
657 for (i = 0; i < hpet_num_timers; i++) {
658 struct hpet_dev *hdev = &hpet_devs[i];
660 if (!(hdev->flags & HPET_DEV_VALID))
663 hd->hd_irq[hdev->num] = hdev->irq;
664 hpet_reserve_timer(hd, hdev->num);
669 static struct hpet_dev *hpet_get_unused_timer(void)
676 for (i = 0; i < hpet_num_timers; i++) {
677 struct hpet_dev *hdev = &hpet_devs[i];
679 if (!(hdev->flags & HPET_DEV_VALID))
681 if (test_and_set_bit(HPET_DEV_USED_BIT,
682 (unsigned long *)&hdev->flags))
689 struct hpet_work_struct {
690 struct delayed_work work;
691 struct completion complete;
694 static void hpet_work(struct work_struct *w)
696 struct hpet_dev *hdev;
697 int cpu = smp_processor_id();
698 struct hpet_work_struct *hpet_work;
700 hpet_work = container_of(w, struct hpet_work_struct, work.work);
702 hdev = hpet_get_unused_timer();
704 init_one_hpet_msi_clockevent(hdev, cpu);
706 complete(&hpet_work->complete);
709 static int hpet_cpuhp_notify(struct notifier_block *n,
710 unsigned long action, void *hcpu)
712 unsigned long cpu = (unsigned long)hcpu;
713 struct hpet_work_struct work;
714 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
716 switch (action & 0xf) {
718 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
719 init_completion(&work.complete);
720 /* FIXME: add schedule_work_on() */
721 schedule_delayed_work_on(cpu, &work.work, 0);
722 wait_for_completion(&work.complete);
723 destroy_timer_on_stack(&work.work.timer);
727 free_irq(hdev->irq, hdev);
728 hdev->flags &= ~HPET_DEV_USED;
729 per_cpu(cpu_hpet_dev, cpu) = NULL;
737 static int hpet_setup_msi_irq(unsigned int irq)
741 static void hpet_msi_capability_lookup(unsigned int start_timer)
747 static void hpet_reserve_msi_timers(struct hpet_data *hd)
753 static int hpet_cpuhp_notify(struct notifier_block *n,
754 unsigned long action, void *hcpu)
762 * Clock source related code
764 static cycle_t read_hpet(struct clocksource *cs)
766 return (cycle_t)hpet_readl(HPET_COUNTER);
770 static cycle_t __vsyscall_fn vread_hpet(void)
772 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
776 static struct clocksource clocksource_hpet = {
782 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
783 .resume = hpet_resume_counter,
789 static int hpet_clocksource_register(void)
794 /* Start the counter */
795 hpet_restart_counter();
797 /* Verify whether hpet counter works */
798 t1 = hpet_readl(HPET_COUNTER);
802 * We don't know the TSC frequency yet, but waiting for
803 * 200000 TSC cycles is safe:
810 } while ((now - start) < 200000UL);
812 if (t1 == hpet_readl(HPET_COUNTER)) {
814 "HPET counter not counting. HPET disabled\n");
819 * The definition of mult is (include/linux/clocksource.h)
820 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
821 * so we first need to convert hpet_period to ns/cyc units:
822 * mult/2^shift = ns/cyc = hpet_period/10^6
823 * mult = (hpet_period * 2^shift)/10^6
824 * mult = (hpet_period << shift)/FSEC_PER_NSEC
826 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
828 clocksource_register(&clocksource_hpet);
834 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
836 int __init hpet_enable(void)
841 if (!is_hpet_capable())
847 * Read the period and check for a sane value:
849 hpet_period = hpet_readl(HPET_PERIOD);
852 * AMD SB700 based systems with spread spectrum enabled use a
853 * SMM based HPET emulation to provide proper frequency
854 * setting. The SMM code is initialized with the first HPET
855 * register access and takes some time to complete. During
856 * this time the config register reads 0xffffffff. We check
857 * for max. 1000 loops whether the config register reads a non
858 * 0xffffffff value to make sure that HPET is up and running
859 * before we go further. A counting loop is safe, as the HPET
860 * access takes thousands of CPU cycles. On non SB700 based
861 * machines this check is only done once and has no side
864 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
867 "HPET config register value = 0xFFFFFFFF. "
873 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
877 * Read the HPET ID register to retrieve the IRQ routing
878 * information and the number of channels
880 id = hpet_readl(HPET_ID);
883 #ifdef CONFIG_HPET_EMULATE_RTC
885 * The legacy routing mode needs at least two channels, tick timer
886 * and the rtc emulation channel.
888 if (!(id & HPET_ID_NUMBER))
892 if (hpet_clocksource_register())
895 if (id & HPET_ID_LEGSUP) {
896 hpet_legacy_clockevent_register();
897 hpet_msi_capability_lookup(2);
900 hpet_msi_capability_lookup(0);
904 hpet_clear_mapping();
910 * Needs to be late, as the reserve_timer code calls kalloc !
912 * Not a problem on i386 as hpet_enable is called from late_time_init,
913 * but on x86_64 it is necessary !
915 static __init int hpet_late_init(void)
919 if (boot_hpet_disable)
923 if (!force_hpet_address)
926 hpet_address = force_hpet_address;
930 if (!hpet_virt_address)
933 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
936 if (hpet_msi_disable)
939 for_each_online_cpu(cpu) {
940 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
943 /* This notifier should be called after workqueue is ready */
944 hotcpu_notifier(hpet_cpuhp_notify, -20);
948 fs_initcall(hpet_late_init);
950 void hpet_disable(void)
952 if (is_hpet_capable()) {
953 unsigned long cfg = hpet_readl(HPET_CFG);
955 if (hpet_legacy_int_enabled) {
956 cfg &= ~HPET_CFG_LEGACY;
957 hpet_legacy_int_enabled = 0;
959 cfg &= ~HPET_CFG_ENABLE;
960 hpet_writel(cfg, HPET_CFG);
964 #ifdef CONFIG_HPET_EMULATE_RTC
966 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
967 * is enabled, we support RTC interrupt functionality in software.
968 * RTC has 3 kinds of interrupts:
969 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
971 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
972 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
973 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
974 * (1) and (2) above are implemented using polling at a frequency of
975 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
976 * overhead. (DEFAULT_RTC_INT_FREQ)
977 * For (3), we use interrupts at 64Hz or user specified periodic
978 * frequency, whichever is higher.
980 #include <linux/mc146818rtc.h>
981 #include <linux/rtc.h>
984 #define DEFAULT_RTC_INT_FREQ 64
985 #define DEFAULT_RTC_SHIFT 6
986 #define RTC_NUM_INTS 1
988 static unsigned long hpet_rtc_flags;
989 static int hpet_prev_update_sec;
990 static struct rtc_time hpet_alarm_time;
991 static unsigned long hpet_pie_count;
992 static u32 hpet_t1_cmp;
993 static unsigned long hpet_default_delta;
994 static unsigned long hpet_pie_delta;
995 static unsigned long hpet_pie_limit;
997 static rtc_irq_handler irq_handler;
1000 * Check that the hpet counter c1 is ahead of the c2
1002 static inline int hpet_cnt_ahead(u32 c1, u32 c2)
1004 return (s32)(c2 - c1) < 0;
1008 * Registers a IRQ handler.
1010 int hpet_register_irq_handler(rtc_irq_handler handler)
1012 if (!is_hpet_enabled())
1017 irq_handler = handler;
1021 EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
1024 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
1027 void hpet_unregister_irq_handler(rtc_irq_handler handler)
1029 if (!is_hpet_enabled())
1035 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1038 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1039 * is not supported by all HPET implementations for timer 1.
1041 * hpet_rtc_timer_init() is called when the rtc is initialized.
1043 int hpet_rtc_timer_init(void)
1045 unsigned long cfg, cnt, delta, flags;
1047 if (!is_hpet_enabled())
1050 if (!hpet_default_delta) {
1053 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1054 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
1055 hpet_default_delta = (unsigned long) clc;
1058 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1059 delta = hpet_default_delta;
1061 delta = hpet_pie_delta;
1063 local_irq_save(flags);
1065 cnt = delta + hpet_readl(HPET_COUNTER);
1066 hpet_writel(cnt, HPET_T1_CMP);
1069 cfg = hpet_readl(HPET_T1_CFG);
1070 cfg &= ~HPET_TN_PERIODIC;
1071 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1072 hpet_writel(cfg, HPET_T1_CFG);
1074 local_irq_restore(flags);
1078 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
1081 * The functions below are called from rtc driver.
1082 * Return 0 if HPET is not being used.
1083 * Otherwise do the necessary changes and return 1.
1085 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1087 if (!is_hpet_enabled())
1090 hpet_rtc_flags &= ~bit_mask;
1093 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
1095 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1097 unsigned long oldbits = hpet_rtc_flags;
1099 if (!is_hpet_enabled())
1102 hpet_rtc_flags |= bit_mask;
1104 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1105 hpet_prev_update_sec = -1;
1108 hpet_rtc_timer_init();
1112 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
1114 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1117 if (!is_hpet_enabled())
1120 hpet_alarm_time.tm_hour = hrs;
1121 hpet_alarm_time.tm_min = min;
1122 hpet_alarm_time.tm_sec = sec;
1126 EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
1128 int hpet_set_periodic_freq(unsigned long freq)
1132 if (!is_hpet_enabled())
1135 if (freq <= DEFAULT_RTC_INT_FREQ)
1136 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1138 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1140 clc >>= hpet_clockevent.shift;
1141 hpet_pie_delta = (unsigned long) clc;
1145 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
1147 int hpet_rtc_dropped_irq(void)
1149 return is_hpet_enabled();
1151 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
1153 static void hpet_rtc_timer_reinit(void)
1155 unsigned long cfg, delta;
1158 if (unlikely(!hpet_rtc_flags)) {
1159 cfg = hpet_readl(HPET_T1_CFG);
1160 cfg &= ~HPET_TN_ENABLE;
1161 hpet_writel(cfg, HPET_T1_CFG);
1165 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1166 delta = hpet_default_delta;
1168 delta = hpet_pie_delta;
1171 * Increment the comparator value until we are ahead of the
1175 hpet_t1_cmp += delta;
1176 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1178 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
1181 if (hpet_rtc_flags & RTC_PIE)
1182 hpet_pie_count += lost_ints;
1183 if (printk_ratelimit())
1184 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
1189 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1191 struct rtc_time curr_time;
1192 unsigned long rtc_int_flag = 0;
1194 hpet_rtc_timer_reinit();
1195 memset(&curr_time, 0, sizeof(struct rtc_time));
1197 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
1198 get_rtc_time(&curr_time);
1200 if (hpet_rtc_flags & RTC_UIE &&
1201 curr_time.tm_sec != hpet_prev_update_sec) {
1202 if (hpet_prev_update_sec >= 0)
1203 rtc_int_flag = RTC_UF;
1204 hpet_prev_update_sec = curr_time.tm_sec;
1207 if (hpet_rtc_flags & RTC_PIE &&
1208 ++hpet_pie_count >= hpet_pie_limit) {
1209 rtc_int_flag |= RTC_PF;
1213 if (hpet_rtc_flags & RTC_AIE &&
1214 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1215 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1216 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1217 rtc_int_flag |= RTC_AF;
1220 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1222 irq_handler(rtc_int_flag, dev_id);
1226 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);