x86: hpet: stop HPET_COUNTER when programming periodic mode
[linux-2.6/mini2440.git] / arch / x86 / kernel / hpet.c
bloba54d2718541a29bc6ec54ca7b6f7b6f80472b8da
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>
9 #include <linux/cpu.h>
10 #include <linux/pm.h>
11 #include <linux/io.h>
13 #include <asm/fixmap.h>
14 #include <asm/i8253.h>
15 #include <asm/hpet.h>
17 #define HPET_MASK CLOCKSOURCE_MASK(32)
18 #define HPET_SHIFT 22
20 /* FSEC = 10^-15
21 NSEC = 10^-9 */
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 #ifdef CONFIG_PCI_MSI
37 static unsigned long hpet_num_timers;
38 #endif
39 static void __iomem *hpet_virt_address;
41 struct hpet_dev {
42 struct clock_event_device evt;
43 unsigned int num;
44 int cpu;
45 unsigned int irq;
46 unsigned int flags;
47 char name[10];
50 unsigned long hpet_readl(unsigned long a)
52 return readl(hpet_virt_address + a);
55 static inline void hpet_writel(unsigned long d, unsigned long a)
57 writel(d, hpet_virt_address + a);
60 #ifdef CONFIG_X86_64
61 #include <asm/pgtable.h>
62 #endif
64 static inline void hpet_set_mapping(void)
66 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
67 #ifdef CONFIG_X86_64
68 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
69 #endif
72 static inline void hpet_clear_mapping(void)
74 iounmap(hpet_virt_address);
75 hpet_virt_address = NULL;
79 * HPET command line enable / disable
81 static int boot_hpet_disable;
82 int hpet_force_user;
84 static int __init hpet_setup(char *str)
86 if (str) {
87 if (!strncmp("disable", str, 7))
88 boot_hpet_disable = 1;
89 if (!strncmp("force", str, 5))
90 hpet_force_user = 1;
92 return 1;
94 __setup("hpet=", hpet_setup);
96 static int __init disable_hpet(char *str)
98 boot_hpet_disable = 1;
99 return 1;
101 __setup("nohpet", disable_hpet);
103 static inline int is_hpet_capable(void)
105 return !boot_hpet_disable && hpet_address;
109 * HPET timer interrupt enable / disable
111 static int hpet_legacy_int_enabled;
114 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
116 int is_hpet_enabled(void)
118 return is_hpet_capable() && hpet_legacy_int_enabled;
120 EXPORT_SYMBOL_GPL(is_hpet_enabled);
123 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
124 * timer 0 and timer 1 in case of RTC emulation.
126 #ifdef CONFIG_HPET
128 static void hpet_reserve_msi_timers(struct hpet_data *hd);
130 static void hpet_reserve_platform_timers(unsigned long id)
132 struct hpet __iomem *hpet = hpet_virt_address;
133 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
134 unsigned int nrtimers, i;
135 struct hpet_data hd;
137 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
139 memset(&hd, 0, sizeof(hd));
140 hd.hd_phys_address = hpet_address;
141 hd.hd_address = hpet;
142 hd.hd_nirqs = nrtimers;
143 hpet_reserve_timer(&hd, 0);
145 #ifdef CONFIG_HPET_EMULATE_RTC
146 hpet_reserve_timer(&hd, 1);
147 #endif
150 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
151 * is wrong for i8259!) not the output IRQ. Many BIOS writers
152 * don't bother configuring *any* comparator interrupts.
154 hd.hd_irq[0] = HPET_LEGACY_8254;
155 hd.hd_irq[1] = HPET_LEGACY_RTC;
157 for (i = 2; i < nrtimers; timer++, i++) {
158 hd.hd_irq[i] = (readl(&timer->hpet_config) &
159 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
162 hpet_reserve_msi_timers(&hd);
164 hpet_alloc(&hd);
167 #else
168 static void hpet_reserve_platform_timers(unsigned long id) { }
169 #endif
172 * Common hpet info
174 static unsigned long hpet_period;
176 static void hpet_legacy_set_mode(enum clock_event_mode mode,
177 struct clock_event_device *evt);
178 static int hpet_legacy_next_event(unsigned long delta,
179 struct clock_event_device *evt);
182 * The hpet clock event device
184 static struct clock_event_device hpet_clockevent = {
185 .name = "hpet",
186 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
187 .set_mode = hpet_legacy_set_mode,
188 .set_next_event = hpet_legacy_next_event,
189 .shift = 32,
190 .irq = 0,
191 .rating = 50,
194 static void hpet_stop_counter(void)
196 unsigned long cfg = hpet_readl(HPET_CFG);
197 cfg &= ~HPET_CFG_ENABLE;
198 hpet_writel(cfg, HPET_CFG);
199 hpet_writel(0, HPET_COUNTER);
200 hpet_writel(0, HPET_COUNTER + 4);
203 static void hpet_start_counter(void)
205 unsigned long cfg = hpet_readl(HPET_CFG);
206 cfg |= HPET_CFG_ENABLE;
207 hpet_writel(cfg, HPET_CFG);
210 static void hpet_restart_counter(void)
212 hpet_stop_counter();
213 hpet_start_counter();
216 static void hpet_resume_device(void)
218 force_hpet_resume();
221 static void hpet_resume_counter(void)
223 hpet_resume_device();
224 hpet_restart_counter();
227 static void hpet_enable_legacy_int(void)
229 unsigned long cfg = hpet_readl(HPET_CFG);
231 cfg |= HPET_CFG_LEGACY;
232 hpet_writel(cfg, HPET_CFG);
233 hpet_legacy_int_enabled = 1;
236 static void hpet_legacy_clockevent_register(void)
238 /* Start HPET legacy interrupts */
239 hpet_enable_legacy_int();
242 * The mult factor is defined as (include/linux/clockchips.h)
243 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
244 * hpet_period is in units of femtoseconds (per cycle), so
245 * mult/2^shift = cyc/ns = 10^6/hpet_period
246 * mult = (10^6 * 2^shift)/hpet_period
247 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
249 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
250 hpet_period, hpet_clockevent.shift);
251 /* Calculate the min / max delta */
252 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
253 &hpet_clockevent);
254 /* 5 usec minimum reprogramming delta. */
255 hpet_clockevent.min_delta_ns = 5000;
258 * Start hpet with the boot cpu mask and make it
259 * global after the IO_APIC has been initialized.
261 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
262 clockevents_register_device(&hpet_clockevent);
263 global_clock_event = &hpet_clockevent;
264 printk(KERN_DEBUG "hpet clockevent registered\n");
267 static int hpet_setup_msi_irq(unsigned int irq);
269 static void hpet_set_mode(enum clock_event_mode mode,
270 struct clock_event_device *evt, int timer)
272 unsigned long cfg;
273 uint64_t delta;
275 switch (mode) {
276 case CLOCK_EVT_MODE_PERIODIC:
277 hpet_stop_counter();
278 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
279 delta >>= evt->shift;
280 cfg = hpet_readl(HPET_Tn_CFG(timer));
281 /* Make sure we use edge triggered interrupts */
282 cfg &= ~HPET_TN_LEVEL;
283 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
284 HPET_TN_SETVAL | HPET_TN_32BIT;
285 hpet_writel(cfg, HPET_Tn_CFG(timer));
286 hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
287 hpet_start_counter();
288 break;
290 case CLOCK_EVT_MODE_ONESHOT:
291 cfg = hpet_readl(HPET_Tn_CFG(timer));
292 cfg &= ~HPET_TN_PERIODIC;
293 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
294 hpet_writel(cfg, HPET_Tn_CFG(timer));
295 break;
297 case CLOCK_EVT_MODE_UNUSED:
298 case CLOCK_EVT_MODE_SHUTDOWN:
299 cfg = hpet_readl(HPET_Tn_CFG(timer));
300 cfg &= ~HPET_TN_ENABLE;
301 hpet_writel(cfg, HPET_Tn_CFG(timer));
302 break;
304 case CLOCK_EVT_MODE_RESUME:
305 if (timer == 0) {
306 hpet_enable_legacy_int();
307 } else {
308 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
309 hpet_setup_msi_irq(hdev->irq);
310 disable_irq(hdev->irq);
311 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
312 enable_irq(hdev->irq);
314 break;
318 static int hpet_next_event(unsigned long delta,
319 struct clock_event_device *evt, int timer)
321 u32 cnt;
323 cnt = hpet_readl(HPET_COUNTER);
324 cnt += (u32) delta;
325 hpet_writel(cnt, HPET_Tn_CMP(timer));
328 * We need to read back the CMP register to make sure that
329 * what we wrote hit the chip before we compare it to the
330 * counter.
332 WARN_ON_ONCE((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt);
334 return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
337 static void hpet_legacy_set_mode(enum clock_event_mode mode,
338 struct clock_event_device *evt)
340 hpet_set_mode(mode, evt, 0);
343 static int hpet_legacy_next_event(unsigned long delta,
344 struct clock_event_device *evt)
346 return hpet_next_event(delta, evt, 0);
350 * HPET MSI Support
352 #ifdef CONFIG_PCI_MSI
354 static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
355 static struct hpet_dev *hpet_devs;
357 void hpet_msi_unmask(unsigned int irq)
359 struct hpet_dev *hdev = get_irq_data(irq);
360 unsigned long cfg;
362 /* unmask it */
363 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
364 cfg |= HPET_TN_FSB;
365 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
368 void hpet_msi_mask(unsigned int irq)
370 unsigned long cfg;
371 struct hpet_dev *hdev = get_irq_data(irq);
373 /* mask it */
374 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
375 cfg &= ~HPET_TN_FSB;
376 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
379 void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
381 struct hpet_dev *hdev = get_irq_data(irq);
383 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
384 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
387 void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
389 struct hpet_dev *hdev = get_irq_data(irq);
391 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
392 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
393 msg->address_hi = 0;
396 static void hpet_msi_set_mode(enum clock_event_mode mode,
397 struct clock_event_device *evt)
399 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
400 hpet_set_mode(mode, evt, hdev->num);
403 static int hpet_msi_next_event(unsigned long delta,
404 struct clock_event_device *evt)
406 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
407 return hpet_next_event(delta, evt, hdev->num);
410 static int hpet_setup_msi_irq(unsigned int irq)
412 if (arch_setup_hpet_msi(irq)) {
413 destroy_irq(irq);
414 return -EINVAL;
416 return 0;
419 static int hpet_assign_irq(struct hpet_dev *dev)
421 unsigned int irq;
423 irq = create_irq();
424 if (!irq)
425 return -EINVAL;
427 set_irq_data(irq, dev);
429 if (hpet_setup_msi_irq(irq))
430 return -EINVAL;
432 dev->irq = irq;
433 return 0;
436 static irqreturn_t hpet_interrupt_handler(int irq, void *data)
438 struct hpet_dev *dev = (struct hpet_dev *)data;
439 struct clock_event_device *hevt = &dev->evt;
441 if (!hevt->event_handler) {
442 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
443 dev->num);
444 return IRQ_HANDLED;
447 hevt->event_handler(hevt);
448 return IRQ_HANDLED;
451 static int hpet_setup_irq(struct hpet_dev *dev)
454 if (request_irq(dev->irq, hpet_interrupt_handler,
455 IRQF_DISABLED|IRQF_NOBALANCING, dev->name, dev))
456 return -1;
458 disable_irq(dev->irq);
459 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
460 enable_irq(dev->irq);
462 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
463 dev->name, dev->irq);
465 return 0;
468 /* This should be called in specific @cpu */
469 static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
471 struct clock_event_device *evt = &hdev->evt;
472 uint64_t hpet_freq;
474 WARN_ON(cpu != smp_processor_id());
475 if (!(hdev->flags & HPET_DEV_VALID))
476 return;
478 if (hpet_setup_msi_irq(hdev->irq))
479 return;
481 hdev->cpu = cpu;
482 per_cpu(cpu_hpet_dev, cpu) = hdev;
483 evt->name = hdev->name;
484 hpet_setup_irq(hdev);
485 evt->irq = hdev->irq;
487 evt->rating = 110;
488 evt->features = CLOCK_EVT_FEAT_ONESHOT;
489 if (hdev->flags & HPET_DEV_PERI_CAP)
490 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
492 evt->set_mode = hpet_msi_set_mode;
493 evt->set_next_event = hpet_msi_next_event;
494 evt->shift = 32;
497 * The period is a femto seconds value. We need to calculate the
498 * scaled math multiplication factor for nanosecond to hpet tick
499 * conversion.
501 hpet_freq = 1000000000000000ULL;
502 do_div(hpet_freq, hpet_period);
503 evt->mult = div_sc((unsigned long) hpet_freq,
504 NSEC_PER_SEC, evt->shift);
505 /* Calculate the max delta */
506 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
507 /* 5 usec minimum reprogramming delta. */
508 evt->min_delta_ns = 5000;
510 evt->cpumask = cpumask_of(hdev->cpu);
511 clockevents_register_device(evt);
514 #ifdef CONFIG_HPET
515 /* Reserve at least one timer for userspace (/dev/hpet) */
516 #define RESERVE_TIMERS 1
517 #else
518 #define RESERVE_TIMERS 0
519 #endif
521 static void hpet_msi_capability_lookup(unsigned int start_timer)
523 unsigned int id;
524 unsigned int num_timers;
525 unsigned int num_timers_used = 0;
526 int i;
528 id = hpet_readl(HPET_ID);
530 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
531 num_timers++; /* Value read out starts from 0 */
533 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
534 if (!hpet_devs)
535 return;
537 hpet_num_timers = num_timers;
539 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
540 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
541 unsigned long cfg = hpet_readl(HPET_Tn_CFG(i));
543 /* Only consider HPET timer with MSI support */
544 if (!(cfg & HPET_TN_FSB_CAP))
545 continue;
547 hdev->flags = 0;
548 if (cfg & HPET_TN_PERIODIC_CAP)
549 hdev->flags |= HPET_DEV_PERI_CAP;
550 hdev->num = i;
552 sprintf(hdev->name, "hpet%d", i);
553 if (hpet_assign_irq(hdev))
554 continue;
556 hdev->flags |= HPET_DEV_FSB_CAP;
557 hdev->flags |= HPET_DEV_VALID;
558 num_timers_used++;
559 if (num_timers_used == num_possible_cpus())
560 break;
563 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
564 num_timers, num_timers_used);
567 #ifdef CONFIG_HPET
568 static void hpet_reserve_msi_timers(struct hpet_data *hd)
570 int i;
572 if (!hpet_devs)
573 return;
575 for (i = 0; i < hpet_num_timers; i++) {
576 struct hpet_dev *hdev = &hpet_devs[i];
578 if (!(hdev->flags & HPET_DEV_VALID))
579 continue;
581 hd->hd_irq[hdev->num] = hdev->irq;
582 hpet_reserve_timer(hd, hdev->num);
585 #endif
587 static struct hpet_dev *hpet_get_unused_timer(void)
589 int i;
591 if (!hpet_devs)
592 return NULL;
594 for (i = 0; i < hpet_num_timers; i++) {
595 struct hpet_dev *hdev = &hpet_devs[i];
597 if (!(hdev->flags & HPET_DEV_VALID))
598 continue;
599 if (test_and_set_bit(HPET_DEV_USED_BIT,
600 (unsigned long *)&hdev->flags))
601 continue;
602 return hdev;
604 return NULL;
607 struct hpet_work_struct {
608 struct delayed_work work;
609 struct completion complete;
612 static void hpet_work(struct work_struct *w)
614 struct hpet_dev *hdev;
615 int cpu = smp_processor_id();
616 struct hpet_work_struct *hpet_work;
618 hpet_work = container_of(w, struct hpet_work_struct, work.work);
620 hdev = hpet_get_unused_timer();
621 if (hdev)
622 init_one_hpet_msi_clockevent(hdev, cpu);
624 complete(&hpet_work->complete);
627 static int hpet_cpuhp_notify(struct notifier_block *n,
628 unsigned long action, void *hcpu)
630 unsigned long cpu = (unsigned long)hcpu;
631 struct hpet_work_struct work;
632 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
634 switch (action & 0xf) {
635 case CPU_ONLINE:
636 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
637 init_completion(&work.complete);
638 /* FIXME: add schedule_work_on() */
639 schedule_delayed_work_on(cpu, &work.work, 0);
640 wait_for_completion(&work.complete);
641 destroy_timer_on_stack(&work.work.timer);
642 break;
643 case CPU_DEAD:
644 if (hdev) {
645 free_irq(hdev->irq, hdev);
646 hdev->flags &= ~HPET_DEV_USED;
647 per_cpu(cpu_hpet_dev, cpu) = NULL;
649 break;
651 return NOTIFY_OK;
653 #else
655 static int hpet_setup_msi_irq(unsigned int irq)
657 return 0;
659 static void hpet_msi_capability_lookup(unsigned int start_timer)
661 return;
664 #ifdef CONFIG_HPET
665 static void hpet_reserve_msi_timers(struct hpet_data *hd)
667 return;
669 #endif
671 static int hpet_cpuhp_notify(struct notifier_block *n,
672 unsigned long action, void *hcpu)
674 return NOTIFY_OK;
677 #endif
680 * Clock source related code
682 static cycle_t read_hpet(void)
684 return (cycle_t)hpet_readl(HPET_COUNTER);
687 #ifdef CONFIG_X86_64
688 static cycle_t __vsyscall_fn vread_hpet(void)
690 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
692 #endif
694 static struct clocksource clocksource_hpet = {
695 .name = "hpet",
696 .rating = 250,
697 .read = read_hpet,
698 .mask = HPET_MASK,
699 .shift = HPET_SHIFT,
700 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
701 .resume = hpet_resume_counter,
702 #ifdef CONFIG_X86_64
703 .vread = vread_hpet,
704 #endif
707 static int hpet_clocksource_register(void)
709 u64 start, now;
710 cycle_t t1;
712 /* Start the counter */
713 hpet_restart_counter();
715 /* Verify whether hpet counter works */
716 t1 = read_hpet();
717 rdtscll(start);
720 * We don't know the TSC frequency yet, but waiting for
721 * 200000 TSC cycles is safe:
722 * 4 GHz == 50us
723 * 1 GHz == 200us
725 do {
726 rep_nop();
727 rdtscll(now);
728 } while ((now - start) < 200000UL);
730 if (t1 == read_hpet()) {
731 printk(KERN_WARNING
732 "HPET counter not counting. HPET disabled\n");
733 return -ENODEV;
737 * The definition of mult is (include/linux/clocksource.h)
738 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
739 * so we first need to convert hpet_period to ns/cyc units:
740 * mult/2^shift = ns/cyc = hpet_period/10^6
741 * mult = (hpet_period * 2^shift)/10^6
742 * mult = (hpet_period << shift)/FSEC_PER_NSEC
744 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
746 clocksource_register(&clocksource_hpet);
748 return 0;
752 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
754 int __init hpet_enable(void)
756 unsigned long id;
757 int i;
759 if (!is_hpet_capable())
760 return 0;
762 hpet_set_mapping();
765 * Read the period and check for a sane value:
767 hpet_period = hpet_readl(HPET_PERIOD);
770 * AMD SB700 based systems with spread spectrum enabled use a
771 * SMM based HPET emulation to provide proper frequency
772 * setting. The SMM code is initialized with the first HPET
773 * register access and takes some time to complete. During
774 * this time the config register reads 0xffffffff. We check
775 * for max. 1000 loops whether the config register reads a non
776 * 0xffffffff value to make sure that HPET is up and running
777 * before we go further. A counting loop is safe, as the HPET
778 * access takes thousands of CPU cycles. On non SB700 based
779 * machines this check is only done once and has no side
780 * effects.
782 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
783 if (i == 1000) {
784 printk(KERN_WARNING
785 "HPET config register value = 0xFFFFFFFF. "
786 "Disabling HPET\n");
787 goto out_nohpet;
791 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
792 goto out_nohpet;
795 * Read the HPET ID register to retrieve the IRQ routing
796 * information and the number of channels
798 id = hpet_readl(HPET_ID);
800 #ifdef CONFIG_HPET_EMULATE_RTC
802 * The legacy routing mode needs at least two channels, tick timer
803 * and the rtc emulation channel.
805 if (!(id & HPET_ID_NUMBER))
806 goto out_nohpet;
807 #endif
809 if (hpet_clocksource_register())
810 goto out_nohpet;
812 if (id & HPET_ID_LEGSUP) {
813 hpet_legacy_clockevent_register();
814 hpet_msi_capability_lookup(2);
815 return 1;
817 hpet_msi_capability_lookup(0);
818 return 0;
820 out_nohpet:
821 hpet_clear_mapping();
822 hpet_address = 0;
823 return 0;
827 * Needs to be late, as the reserve_timer code calls kalloc !
829 * Not a problem on i386 as hpet_enable is called from late_time_init,
830 * but on x86_64 it is necessary !
832 static __init int hpet_late_init(void)
834 int cpu;
836 if (boot_hpet_disable)
837 return -ENODEV;
839 if (!hpet_address) {
840 if (!force_hpet_address)
841 return -ENODEV;
843 hpet_address = force_hpet_address;
844 hpet_enable();
847 if (!hpet_virt_address)
848 return -ENODEV;
850 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
852 for_each_online_cpu(cpu) {
853 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
856 /* This notifier should be called after workqueue is ready */
857 hotcpu_notifier(hpet_cpuhp_notify, -20);
859 return 0;
861 fs_initcall(hpet_late_init);
863 void hpet_disable(void)
865 if (is_hpet_capable()) {
866 unsigned long cfg = hpet_readl(HPET_CFG);
868 if (hpet_legacy_int_enabled) {
869 cfg &= ~HPET_CFG_LEGACY;
870 hpet_legacy_int_enabled = 0;
872 cfg &= ~HPET_CFG_ENABLE;
873 hpet_writel(cfg, HPET_CFG);
877 #ifdef CONFIG_HPET_EMULATE_RTC
879 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
880 * is enabled, we support RTC interrupt functionality in software.
881 * RTC has 3 kinds of interrupts:
882 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
883 * is updated
884 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
885 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
886 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
887 * (1) and (2) above are implemented using polling at a frequency of
888 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
889 * overhead. (DEFAULT_RTC_INT_FREQ)
890 * For (3), we use interrupts at 64Hz or user specified periodic
891 * frequency, whichever is higher.
893 #include <linux/mc146818rtc.h>
894 #include <linux/rtc.h>
895 #include <asm/rtc.h>
897 #define DEFAULT_RTC_INT_FREQ 64
898 #define DEFAULT_RTC_SHIFT 6
899 #define RTC_NUM_INTS 1
901 static unsigned long hpet_rtc_flags;
902 static int hpet_prev_update_sec;
903 static struct rtc_time hpet_alarm_time;
904 static unsigned long hpet_pie_count;
905 static u32 hpet_t1_cmp;
906 static unsigned long hpet_default_delta;
907 static unsigned long hpet_pie_delta;
908 static unsigned long hpet_pie_limit;
910 static rtc_irq_handler irq_handler;
913 * Check that the hpet counter c1 is ahead of the c2
915 static inline int hpet_cnt_ahead(u32 c1, u32 c2)
917 return (s32)(c2 - c1) < 0;
921 * Registers a IRQ handler.
923 int hpet_register_irq_handler(rtc_irq_handler handler)
925 if (!is_hpet_enabled())
926 return -ENODEV;
927 if (irq_handler)
928 return -EBUSY;
930 irq_handler = handler;
932 return 0;
934 EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
937 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
938 * and does cleanup.
940 void hpet_unregister_irq_handler(rtc_irq_handler handler)
942 if (!is_hpet_enabled())
943 return;
945 irq_handler = NULL;
946 hpet_rtc_flags = 0;
948 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
951 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
952 * is not supported by all HPET implementations for timer 1.
954 * hpet_rtc_timer_init() is called when the rtc is initialized.
956 int hpet_rtc_timer_init(void)
958 unsigned long cfg, cnt, delta, flags;
960 if (!is_hpet_enabled())
961 return 0;
963 if (!hpet_default_delta) {
964 uint64_t clc;
966 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
967 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
968 hpet_default_delta = (unsigned long) clc;
971 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
972 delta = hpet_default_delta;
973 else
974 delta = hpet_pie_delta;
976 local_irq_save(flags);
978 cnt = delta + hpet_readl(HPET_COUNTER);
979 hpet_writel(cnt, HPET_T1_CMP);
980 hpet_t1_cmp = cnt;
982 cfg = hpet_readl(HPET_T1_CFG);
983 cfg &= ~HPET_TN_PERIODIC;
984 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
985 hpet_writel(cfg, HPET_T1_CFG);
987 local_irq_restore(flags);
989 return 1;
991 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
994 * The functions below are called from rtc driver.
995 * Return 0 if HPET is not being used.
996 * Otherwise do the necessary changes and return 1.
998 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1000 if (!is_hpet_enabled())
1001 return 0;
1003 hpet_rtc_flags &= ~bit_mask;
1004 return 1;
1006 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
1008 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1010 unsigned long oldbits = hpet_rtc_flags;
1012 if (!is_hpet_enabled())
1013 return 0;
1015 hpet_rtc_flags |= bit_mask;
1017 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1018 hpet_prev_update_sec = -1;
1020 if (!oldbits)
1021 hpet_rtc_timer_init();
1023 return 1;
1025 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
1027 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1028 unsigned char sec)
1030 if (!is_hpet_enabled())
1031 return 0;
1033 hpet_alarm_time.tm_hour = hrs;
1034 hpet_alarm_time.tm_min = min;
1035 hpet_alarm_time.tm_sec = sec;
1037 return 1;
1039 EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
1041 int hpet_set_periodic_freq(unsigned long freq)
1043 uint64_t clc;
1045 if (!is_hpet_enabled())
1046 return 0;
1048 if (freq <= DEFAULT_RTC_INT_FREQ)
1049 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1050 else {
1051 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1052 do_div(clc, freq);
1053 clc >>= hpet_clockevent.shift;
1054 hpet_pie_delta = (unsigned long) clc;
1056 return 1;
1058 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
1060 int hpet_rtc_dropped_irq(void)
1062 return is_hpet_enabled();
1064 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
1066 static void hpet_rtc_timer_reinit(void)
1068 unsigned long cfg, delta;
1069 int lost_ints = -1;
1071 if (unlikely(!hpet_rtc_flags)) {
1072 cfg = hpet_readl(HPET_T1_CFG);
1073 cfg &= ~HPET_TN_ENABLE;
1074 hpet_writel(cfg, HPET_T1_CFG);
1075 return;
1078 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1079 delta = hpet_default_delta;
1080 else
1081 delta = hpet_pie_delta;
1084 * Increment the comparator value until we are ahead of the
1085 * current count.
1087 do {
1088 hpet_t1_cmp += delta;
1089 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1090 lost_ints++;
1091 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
1093 if (lost_ints) {
1094 if (hpet_rtc_flags & RTC_PIE)
1095 hpet_pie_count += lost_ints;
1096 if (printk_ratelimit())
1097 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
1098 lost_ints);
1102 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1104 struct rtc_time curr_time;
1105 unsigned long rtc_int_flag = 0;
1107 hpet_rtc_timer_reinit();
1108 memset(&curr_time, 0, sizeof(struct rtc_time));
1110 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
1111 get_rtc_time(&curr_time);
1113 if (hpet_rtc_flags & RTC_UIE &&
1114 curr_time.tm_sec != hpet_prev_update_sec) {
1115 if (hpet_prev_update_sec >= 0)
1116 rtc_int_flag = RTC_UF;
1117 hpet_prev_update_sec = curr_time.tm_sec;
1120 if (hpet_rtc_flags & RTC_PIE &&
1121 ++hpet_pie_count >= hpet_pie_limit) {
1122 rtc_int_flag |= RTC_PF;
1123 hpet_pie_count = 0;
1126 if (hpet_rtc_flags & RTC_AIE &&
1127 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1128 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1129 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1130 rtc_int_flag |= RTC_AF;
1132 if (rtc_int_flag) {
1133 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1134 if (irq_handler)
1135 irq_handler(rtc_int_flag, dev_id);
1137 return IRQ_HANDLED;
1139 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
1140 #endif