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;
36 unsigned long hpet_num_timers;
37 static void __iomem *hpet_virt_address;
40 struct clock_event_device evt;
48 unsigned long hpet_readl(unsigned long a)
50 return readl(hpet_virt_address + a);
53 static inline void hpet_writel(unsigned long d, unsigned long a)
55 writel(d, hpet_virt_address + a);
59 #include <asm/pgtable.h>
62 static inline void hpet_set_mapping(void)
64 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
66 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
70 static inline void hpet_clear_mapping(void)
72 iounmap(hpet_virt_address);
73 hpet_virt_address = NULL;
77 * HPET command line enable / disable
79 static int boot_hpet_disable;
82 static int __init hpet_setup(char *str)
85 if (!strncmp("disable", str, 7))
86 boot_hpet_disable = 1;
87 if (!strncmp("force", str, 5))
92 __setup("hpet=", hpet_setup);
94 static int __init disable_hpet(char *str)
96 boot_hpet_disable = 1;
99 __setup("nohpet", disable_hpet);
101 static inline int is_hpet_capable(void)
103 return !boot_hpet_disable && hpet_address;
107 * HPET timer interrupt enable / disable
109 static int hpet_legacy_int_enabled;
112 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
114 int is_hpet_enabled(void)
116 return is_hpet_capable() && hpet_legacy_int_enabled;
118 EXPORT_SYMBOL_GPL(is_hpet_enabled);
121 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
122 * timer 0 and timer 1 in case of RTC emulation.
126 static void hpet_reserve_msi_timers(struct hpet_data *hd);
128 static void hpet_reserve_platform_timers(unsigned long id)
130 struct hpet __iomem *hpet = hpet_virt_address;
131 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
132 unsigned int nrtimers, i;
135 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
137 memset(&hd, 0, sizeof(hd));
138 hd.hd_phys_address = hpet_address;
139 hd.hd_address = hpet;
140 hd.hd_nirqs = nrtimers;
141 hpet_reserve_timer(&hd, 0);
143 #ifdef CONFIG_HPET_EMULATE_RTC
144 hpet_reserve_timer(&hd, 1);
148 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
149 * is wrong for i8259!) not the output IRQ. Many BIOS writers
150 * don't bother configuring *any* comparator interrupts.
152 hd.hd_irq[0] = HPET_LEGACY_8254;
153 hd.hd_irq[1] = HPET_LEGACY_RTC;
155 for (i = 2; i < nrtimers; timer++, i++) {
156 hd.hd_irq[i] = (readl(&timer->hpet_config) &
157 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
160 hpet_reserve_msi_timers(&hd);
166 static void hpet_reserve_platform_timers(unsigned long id) { }
172 static unsigned long hpet_period;
174 static void hpet_legacy_set_mode(enum clock_event_mode mode,
175 struct clock_event_device *evt);
176 static int hpet_legacy_next_event(unsigned long delta,
177 struct clock_event_device *evt);
180 * The hpet clock event device
182 static struct clock_event_device hpet_clockevent = {
184 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
185 .set_mode = hpet_legacy_set_mode,
186 .set_next_event = hpet_legacy_next_event,
192 static void hpet_start_counter(void)
194 unsigned long cfg = hpet_readl(HPET_CFG);
196 cfg &= ~HPET_CFG_ENABLE;
197 hpet_writel(cfg, HPET_CFG);
198 hpet_writel(0, HPET_COUNTER);
199 hpet_writel(0, HPET_COUNTER + 4);
200 cfg |= HPET_CFG_ENABLE;
201 hpet_writel(cfg, HPET_CFG);
204 static void hpet_resume_device(void)
209 static void hpet_restart_counter(void)
211 hpet_resume_device();
212 hpet_start_counter();
215 static void hpet_enable_legacy_int(void)
217 unsigned long cfg = hpet_readl(HPET_CFG);
219 cfg |= HPET_CFG_LEGACY;
220 hpet_writel(cfg, HPET_CFG);
221 hpet_legacy_int_enabled = 1;
224 static void hpet_legacy_clockevent_register(void)
226 /* Start HPET legacy interrupts */
227 hpet_enable_legacy_int();
230 * The mult factor is defined as (include/linux/clockchips.h)
231 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
232 * hpet_period is in units of femtoseconds (per cycle), so
233 * mult/2^shift = cyc/ns = 10^6/hpet_period
234 * mult = (10^6 * 2^shift)/hpet_period
235 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
237 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
238 hpet_period, hpet_clockevent.shift);
239 /* Calculate the min / max delta */
240 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
242 /* 5 usec minimum reprogramming delta. */
243 hpet_clockevent.min_delta_ns = 5000;
246 * Start hpet with the boot cpu mask and make it
247 * global after the IO_APIC has been initialized.
249 hpet_clockevent.cpumask = cpumask_of_cpu(smp_processor_id());
250 clockevents_register_device(&hpet_clockevent);
251 global_clock_event = &hpet_clockevent;
252 printk(KERN_DEBUG "hpet clockevent registered\n");
255 static int hpet_setup_msi_irq(unsigned int irq);
257 static void hpet_set_mode(enum clock_event_mode mode,
258 struct clock_event_device *evt, int timer)
260 unsigned long cfg, cmp, now;
264 case CLOCK_EVT_MODE_PERIODIC:
265 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
266 delta >>= evt->shift;
267 now = hpet_readl(HPET_COUNTER);
268 cmp = now + (unsigned long) delta;
269 cfg = hpet_readl(HPET_Tn_CFG(timer));
270 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
271 HPET_TN_SETVAL | HPET_TN_32BIT;
272 hpet_writel(cfg, HPET_Tn_CFG(timer));
274 * The first write after writing TN_SETVAL to the
275 * config register sets the counter value, the second
276 * write sets the period.
278 hpet_writel(cmp, HPET_Tn_CMP(timer));
280 hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
283 case CLOCK_EVT_MODE_ONESHOT:
284 cfg = hpet_readl(HPET_Tn_CFG(timer));
285 cfg &= ~HPET_TN_PERIODIC;
286 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
287 hpet_writel(cfg, HPET_Tn_CFG(timer));
290 case CLOCK_EVT_MODE_UNUSED:
291 case CLOCK_EVT_MODE_SHUTDOWN:
292 cfg = hpet_readl(HPET_Tn_CFG(timer));
293 cfg &= ~HPET_TN_ENABLE;
294 hpet_writel(cfg, HPET_Tn_CFG(timer));
297 case CLOCK_EVT_MODE_RESUME:
299 hpet_enable_legacy_int();
301 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
302 hpet_setup_msi_irq(hdev->irq);
303 disable_irq(hdev->irq);
304 irq_set_affinity(hdev->irq, cpumask_of_cpu(hdev->cpu));
305 enable_irq(hdev->irq);
311 static int hpet_next_event(unsigned long delta,
312 struct clock_event_device *evt, int timer)
316 cnt = hpet_readl(HPET_COUNTER);
318 hpet_writel(cnt, HPET_Tn_CMP(timer));
321 * We need to read back the CMP register to make sure that
322 * what we wrote hit the chip before we compare it to the
325 WARN_ON((u32)hpet_readl(HPET_T0_CMP) != cnt);
327 return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
330 static void hpet_legacy_set_mode(enum clock_event_mode mode,
331 struct clock_event_device *evt)
333 hpet_set_mode(mode, evt, 0);
336 static int hpet_legacy_next_event(unsigned long delta,
337 struct clock_event_device *evt)
339 return hpet_next_event(delta, evt, 0);
345 #ifdef CONFIG_PCI_MSI
347 static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
348 static struct hpet_dev *hpet_devs;
350 void hpet_msi_unmask(unsigned int irq)
352 struct hpet_dev *hdev = get_irq_data(irq);
356 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
358 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
361 void hpet_msi_mask(unsigned int irq)
364 struct hpet_dev *hdev = get_irq_data(irq);
367 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
369 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
372 void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
374 struct hpet_dev *hdev = get_irq_data(irq);
376 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
377 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
380 void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
382 struct hpet_dev *hdev = get_irq_data(irq);
384 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
385 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
389 static void hpet_msi_set_mode(enum clock_event_mode mode,
390 struct clock_event_device *evt)
392 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
393 hpet_set_mode(mode, evt, hdev->num);
396 static int hpet_msi_next_event(unsigned long delta,
397 struct clock_event_device *evt)
399 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
400 return hpet_next_event(delta, evt, hdev->num);
403 static int hpet_setup_msi_irq(unsigned int irq)
405 if (arch_setup_hpet_msi(irq)) {
412 static int hpet_assign_irq(struct hpet_dev *dev)
420 set_irq_data(irq, dev);
422 if (hpet_setup_msi_irq(irq))
429 static irqreturn_t hpet_interrupt_handler(int irq, void *data)
431 struct hpet_dev *dev = (struct hpet_dev *)data;
432 struct clock_event_device *hevt = &dev->evt;
434 if (!hevt->event_handler) {
435 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
440 hevt->event_handler(hevt);
444 static int hpet_setup_irq(struct hpet_dev *dev)
447 if (request_irq(dev->irq, hpet_interrupt_handler,
448 IRQF_SHARED|IRQF_NOBALANCING, dev->name, dev))
451 disable_irq(dev->irq);
452 irq_set_affinity(dev->irq, cpumask_of_cpu(dev->cpu));
453 enable_irq(dev->irq);
455 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
456 dev->name, dev->irq);
461 /* This should be called in specific @cpu */
462 static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
464 struct clock_event_device *evt = &hdev->evt;
467 WARN_ON(cpu != smp_processor_id());
468 if (!(hdev->flags & HPET_DEV_VALID))
471 if (hpet_setup_msi_irq(hdev->irq))
475 per_cpu(cpu_hpet_dev, cpu) = hdev;
476 evt->name = hdev->name;
477 hpet_setup_irq(hdev);
478 evt->irq = hdev->irq;
481 evt->features = CLOCK_EVT_FEAT_ONESHOT;
482 if (hdev->flags & HPET_DEV_PERI_CAP)
483 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
485 evt->set_mode = hpet_msi_set_mode;
486 evt->set_next_event = hpet_msi_next_event;
490 * The period is a femto seconds value. We need to calculate the
491 * scaled math multiplication factor for nanosecond to hpet tick
494 hpet_freq = 1000000000000000ULL;
495 do_div(hpet_freq, hpet_period);
496 evt->mult = div_sc((unsigned long) hpet_freq,
497 NSEC_PER_SEC, evt->shift);
498 /* Calculate the max delta */
499 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
500 /* 5 usec minimum reprogramming delta. */
501 evt->min_delta_ns = 5000;
503 evt->cpumask = cpumask_of_cpu(hdev->cpu);
504 clockevents_register_device(evt);
508 /* Reserve at least one timer for userspace (/dev/hpet) */
509 #define RESERVE_TIMERS 1
511 #define RESERVE_TIMERS 0
514 static void hpet_msi_capability_lookup(unsigned int start_timer)
517 unsigned int num_timers;
518 unsigned int num_timers_used = 0;
521 id = hpet_readl(HPET_ID);
523 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
524 num_timers++; /* Value read out starts from 0 */
526 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
530 hpet_num_timers = num_timers;
532 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
533 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
534 unsigned long cfg = hpet_readl(HPET_Tn_CFG(i));
536 /* Only consider HPET timer with MSI support */
537 if (!(cfg & HPET_TN_FSB_CAP))
541 if (cfg & HPET_TN_PERIODIC_CAP)
542 hdev->flags |= HPET_DEV_PERI_CAP;
545 sprintf(hdev->name, "hpet%d", i);
546 if (hpet_assign_irq(hdev))
549 hdev->flags |= HPET_DEV_FSB_CAP;
550 hdev->flags |= HPET_DEV_VALID;
552 if (num_timers_used == num_possible_cpus())
556 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
557 num_timers, num_timers_used);
561 static void hpet_reserve_msi_timers(struct hpet_data *hd)
568 for (i = 0; i < hpet_num_timers; i++) {
569 struct hpet_dev *hdev = &hpet_devs[i];
571 if (!(hdev->flags & HPET_DEV_VALID))
574 hd->hd_irq[hdev->num] = hdev->irq;
575 hpet_reserve_timer(hd, hdev->num);
580 static struct hpet_dev *hpet_get_unused_timer(void)
587 for (i = 0; i < hpet_num_timers; i++) {
588 struct hpet_dev *hdev = &hpet_devs[i];
590 if (!(hdev->flags & HPET_DEV_VALID))
592 if (test_and_set_bit(HPET_DEV_USED_BIT,
593 (unsigned long *)&hdev->flags))
600 struct hpet_work_struct {
601 struct delayed_work work;
602 struct completion complete;
605 static void hpet_work(struct work_struct *w)
607 struct hpet_dev *hdev;
608 int cpu = smp_processor_id();
609 struct hpet_work_struct *hpet_work;
611 hpet_work = container_of(w, struct hpet_work_struct, work.work);
613 hdev = hpet_get_unused_timer();
615 init_one_hpet_msi_clockevent(hdev, cpu);
617 complete(&hpet_work->complete);
620 static int hpet_cpuhp_notify(struct notifier_block *n,
621 unsigned long action, void *hcpu)
623 unsigned long cpu = (unsigned long)hcpu;
624 struct hpet_work_struct work;
625 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
627 switch (action & 0xf) {
629 INIT_DELAYED_WORK(&work.work, hpet_work);
630 init_completion(&work.complete);
631 /* FIXME: add schedule_work_on() */
632 schedule_delayed_work_on(cpu, &work.work, 0);
633 wait_for_completion(&work.complete);
637 free_irq(hdev->irq, hdev);
638 hdev->flags &= ~HPET_DEV_USED;
639 per_cpu(cpu_hpet_dev, cpu) = NULL;
647 static int hpet_setup_msi_irq(unsigned int irq)
651 static void hpet_msi_capability_lookup(unsigned int start_timer)
657 static void hpet_reserve_msi_timers(struct hpet_data *hd)
663 static int hpet_cpuhp_notify(struct notifier_block *n,
664 unsigned long action, void *hcpu)
672 * Clock source related code
674 static cycle_t read_hpet(void)
676 return (cycle_t)hpet_readl(HPET_COUNTER);
680 static cycle_t __vsyscall_fn vread_hpet(void)
682 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
686 static struct clocksource clocksource_hpet = {
692 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
693 .resume = hpet_restart_counter,
699 static int hpet_clocksource_register(void)
704 /* Start the counter */
705 hpet_start_counter();
707 /* Verify whether hpet counter works */
712 * We don't know the TSC frequency yet, but waiting for
713 * 200000 TSC cycles is safe:
720 } while ((now - start) < 200000UL);
722 if (t1 == read_hpet()) {
724 "HPET counter not counting. HPET disabled\n");
729 * The definition of mult is (include/linux/clocksource.h)
730 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
731 * so we first need to convert hpet_period to ns/cyc units:
732 * mult/2^shift = ns/cyc = hpet_period/10^6
733 * mult = (hpet_period * 2^shift)/10^6
734 * mult = (hpet_period << shift)/FSEC_PER_NSEC
736 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
738 clocksource_register(&clocksource_hpet);
744 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
746 int __init hpet_enable(void)
751 if (!is_hpet_capable())
757 * Read the period and check for a sane value:
759 hpet_period = hpet_readl(HPET_PERIOD);
762 * AMD SB700 based systems with spread spectrum enabled use a
763 * SMM based HPET emulation to provide proper frequency
764 * setting. The SMM code is initialized with the first HPET
765 * register access and takes some time to complete. During
766 * this time the config register reads 0xffffffff. We check
767 * for max. 1000 loops whether the config register reads a non
768 * 0xffffffff value to make sure that HPET is up and running
769 * before we go further. A counting loop is safe, as the HPET
770 * access takes thousands of CPU cycles. On non SB700 based
771 * machines this check is only done once and has no side
774 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
777 "HPET config register value = 0xFFFFFFFF. "
783 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
787 * Read the HPET ID register to retrieve the IRQ routing
788 * information and the number of channels
790 id = hpet_readl(HPET_ID);
792 #ifdef CONFIG_HPET_EMULATE_RTC
794 * The legacy routing mode needs at least two channels, tick timer
795 * and the rtc emulation channel.
797 if (!(id & HPET_ID_NUMBER))
801 if (hpet_clocksource_register())
804 if (id & HPET_ID_LEGSUP) {
805 hpet_legacy_clockevent_register();
806 hpet_msi_capability_lookup(2);
809 hpet_msi_capability_lookup(0);
813 hpet_clear_mapping();
814 boot_hpet_disable = 1;
819 * Needs to be late, as the reserve_timer code calls kalloc !
821 * Not a problem on i386 as hpet_enable is called from late_time_init,
822 * but on x86_64 it is necessary !
824 static __init int hpet_late_init(void)
828 if (boot_hpet_disable)
832 if (!force_hpet_address)
835 hpet_address = force_hpet_address;
837 if (!hpet_virt_address)
841 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
843 for_each_online_cpu(cpu) {
844 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
847 /* This notifier should be called after workqueue is ready */
848 hotcpu_notifier(hpet_cpuhp_notify, -20);
852 fs_initcall(hpet_late_init);
854 void hpet_disable(void)
856 if (is_hpet_capable()) {
857 unsigned long cfg = hpet_readl(HPET_CFG);
859 if (hpet_legacy_int_enabled) {
860 cfg &= ~HPET_CFG_LEGACY;
861 hpet_legacy_int_enabled = 0;
863 cfg &= ~HPET_CFG_ENABLE;
864 hpet_writel(cfg, HPET_CFG);
868 #ifdef CONFIG_HPET_EMULATE_RTC
870 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
871 * is enabled, we support RTC interrupt functionality in software.
872 * RTC has 3 kinds of interrupts:
873 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
875 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
876 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
877 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
878 * (1) and (2) above are implemented using polling at a frequency of
879 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
880 * overhead. (DEFAULT_RTC_INT_FREQ)
881 * For (3), we use interrupts at 64Hz or user specified periodic
882 * frequency, whichever is higher.
884 #include <linux/mc146818rtc.h>
885 #include <linux/rtc.h>
888 #define DEFAULT_RTC_INT_FREQ 64
889 #define DEFAULT_RTC_SHIFT 6
890 #define RTC_NUM_INTS 1
892 static unsigned long hpet_rtc_flags;
893 static int hpet_prev_update_sec;
894 static struct rtc_time hpet_alarm_time;
895 static unsigned long hpet_pie_count;
896 static unsigned long hpet_t1_cmp;
897 static unsigned long hpet_default_delta;
898 static unsigned long hpet_pie_delta;
899 static unsigned long hpet_pie_limit;
901 static rtc_irq_handler irq_handler;
904 * Registers a IRQ handler.
906 int hpet_register_irq_handler(rtc_irq_handler handler)
908 if (!is_hpet_enabled())
913 irq_handler = handler;
917 EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
920 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
923 void hpet_unregister_irq_handler(rtc_irq_handler handler)
925 if (!is_hpet_enabled())
931 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
934 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
935 * is not supported by all HPET implementations for timer 1.
937 * hpet_rtc_timer_init() is called when the rtc is initialized.
939 int hpet_rtc_timer_init(void)
941 unsigned long cfg, cnt, delta, flags;
943 if (!is_hpet_enabled())
946 if (!hpet_default_delta) {
949 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
950 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
951 hpet_default_delta = (unsigned long) clc;
954 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
955 delta = hpet_default_delta;
957 delta = hpet_pie_delta;
959 local_irq_save(flags);
961 cnt = delta + hpet_readl(HPET_COUNTER);
962 hpet_writel(cnt, HPET_T1_CMP);
965 cfg = hpet_readl(HPET_T1_CFG);
966 cfg &= ~HPET_TN_PERIODIC;
967 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
968 hpet_writel(cfg, HPET_T1_CFG);
970 local_irq_restore(flags);
974 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
977 * The functions below are called from rtc driver.
978 * Return 0 if HPET is not being used.
979 * Otherwise do the necessary changes and return 1.
981 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
983 if (!is_hpet_enabled())
986 hpet_rtc_flags &= ~bit_mask;
989 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
991 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
993 unsigned long oldbits = hpet_rtc_flags;
995 if (!is_hpet_enabled())
998 hpet_rtc_flags |= bit_mask;
1000 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1001 hpet_prev_update_sec = -1;
1004 hpet_rtc_timer_init();
1008 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
1010 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1013 if (!is_hpet_enabled())
1016 hpet_alarm_time.tm_hour = hrs;
1017 hpet_alarm_time.tm_min = min;
1018 hpet_alarm_time.tm_sec = sec;
1022 EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
1024 int hpet_set_periodic_freq(unsigned long freq)
1028 if (!is_hpet_enabled())
1031 if (freq <= DEFAULT_RTC_INT_FREQ)
1032 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1034 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1036 clc >>= hpet_clockevent.shift;
1037 hpet_pie_delta = (unsigned long) clc;
1041 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
1043 int hpet_rtc_dropped_irq(void)
1045 return is_hpet_enabled();
1047 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
1049 static void hpet_rtc_timer_reinit(void)
1051 unsigned long cfg, delta;
1054 if (unlikely(!hpet_rtc_flags)) {
1055 cfg = hpet_readl(HPET_T1_CFG);
1056 cfg &= ~HPET_TN_ENABLE;
1057 hpet_writel(cfg, HPET_T1_CFG);
1061 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1062 delta = hpet_default_delta;
1064 delta = hpet_pie_delta;
1067 * Increment the comparator value until we are ahead of the
1071 hpet_t1_cmp += delta;
1072 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1074 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
1077 if (hpet_rtc_flags & RTC_PIE)
1078 hpet_pie_count += lost_ints;
1079 if (printk_ratelimit())
1080 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
1085 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1087 struct rtc_time curr_time;
1088 unsigned long rtc_int_flag = 0;
1090 hpet_rtc_timer_reinit();
1091 memset(&curr_time, 0, sizeof(struct rtc_time));
1093 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
1094 get_rtc_time(&curr_time);
1096 if (hpet_rtc_flags & RTC_UIE &&
1097 curr_time.tm_sec != hpet_prev_update_sec) {
1098 if (hpet_prev_update_sec >= 0)
1099 rtc_int_flag = RTC_UF;
1100 hpet_prev_update_sec = curr_time.tm_sec;
1103 if (hpet_rtc_flags & RTC_PIE &&
1104 ++hpet_pie_count >= hpet_pie_limit) {
1105 rtc_int_flag |= RTC_PF;
1109 if (hpet_rtc_flags & RTC_AIE &&
1110 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1111 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1112 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1113 rtc_int_flag |= RTC_AF;
1116 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1118 irq_handler(rtc_int_flag, dev_id);
1122 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);