1 #include <linux/clocksource.h>
2 #include <linux/clockchips.h>
3 #include <linux/interrupt.h>
4 #include <linux/export.h>
5 #include <linux/sysdev.h>
6 #include <linux/delay.h>
7 #include <linux/errno.h>
8 #include <linux/i8253.h>
9 #include <linux/slab.h>
10 #include <linux/hpet.h>
11 #include <linux/init.h>
12 #include <linux/cpu.h>
16 #include <asm/fixmap.h>
20 #define HPET_MASK CLOCKSOURCE_MASK(32)
24 #define FSEC_PER_NSEC 1000000L
26 #define HPET_DEV_USED_BIT 2
27 #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
28 #define HPET_DEV_VALID 0x8
29 #define HPET_DEV_FSB_CAP 0x1000
30 #define HPET_DEV_PERI_CAP 0x2000
32 #define HPET_MIN_CYCLES 128
33 #define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
35 #define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
38 * HPET address is set in acpi/boot.c, when an ACPI entry exists
40 unsigned long hpet_address
;
41 u8 hpet_blockid
; /* OS timer block num */
45 static unsigned long hpet_num_timers
;
47 static void __iomem
*hpet_virt_address
;
50 struct clock_event_device evt
;
58 inline unsigned int hpet_readl(unsigned int a
)
60 return readl(hpet_virt_address
+ a
);
63 static inline void hpet_writel(unsigned int d
, unsigned int a
)
65 writel(d
, hpet_virt_address
+ a
);
69 #include <asm/pgtable.h>
72 static inline void hpet_set_mapping(void)
74 hpet_virt_address
= ioremap_nocache(hpet_address
, HPET_MMAP_SIZE
);
76 __set_fixmap(VSYSCALL_HPET
, hpet_address
, PAGE_KERNEL_VVAR_NOCACHE
);
80 static inline void hpet_clear_mapping(void)
82 iounmap(hpet_virt_address
);
83 hpet_virt_address
= NULL
;
87 * HPET command line enable / disable
89 static int boot_hpet_disable
;
91 static int hpet_verbose
;
93 static int __init
hpet_setup(char *str
)
96 if (!strncmp("disable", str
, 7))
97 boot_hpet_disable
= 1;
98 if (!strncmp("force", str
, 5))
100 if (!strncmp("verbose", str
, 7))
105 __setup("hpet=", hpet_setup
);
107 static int __init
disable_hpet(char *str
)
109 boot_hpet_disable
= 1;
112 __setup("nohpet", disable_hpet
);
114 static inline int is_hpet_capable(void)
116 return !boot_hpet_disable
&& hpet_address
;
120 * HPET timer interrupt enable / disable
122 static int hpet_legacy_int_enabled
;
125 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
127 int is_hpet_enabled(void)
129 return is_hpet_capable() && hpet_legacy_int_enabled
;
131 EXPORT_SYMBOL_GPL(is_hpet_enabled
);
133 static void _hpet_print_config(const char *function
, int line
)
136 printk(KERN_INFO
"hpet: %s(%d):\n", function
, line
);
137 l
= hpet_readl(HPET_ID
);
138 h
= hpet_readl(HPET_PERIOD
);
139 timers
= ((l
& HPET_ID_NUMBER
) >> HPET_ID_NUMBER_SHIFT
) + 1;
140 printk(KERN_INFO
"hpet: ID: 0x%x, PERIOD: 0x%x\n", l
, h
);
141 l
= hpet_readl(HPET_CFG
);
142 h
= hpet_readl(HPET_STATUS
);
143 printk(KERN_INFO
"hpet: CFG: 0x%x, STATUS: 0x%x\n", l
, h
);
144 l
= hpet_readl(HPET_COUNTER
);
145 h
= hpet_readl(HPET_COUNTER
+4);
146 printk(KERN_INFO
"hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l
, h
);
148 for (i
= 0; i
< timers
; i
++) {
149 l
= hpet_readl(HPET_Tn_CFG(i
));
150 h
= hpet_readl(HPET_Tn_CFG(i
)+4);
151 printk(KERN_INFO
"hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
153 l
= hpet_readl(HPET_Tn_CMP(i
));
154 h
= hpet_readl(HPET_Tn_CMP(i
)+4);
155 printk(KERN_INFO
"hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
157 l
= hpet_readl(HPET_Tn_ROUTE(i
));
158 h
= hpet_readl(HPET_Tn_ROUTE(i
)+4);
159 printk(KERN_INFO
"hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
164 #define hpet_print_config() \
167 _hpet_print_config(__FUNCTION__, __LINE__); \
171 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
172 * timer 0 and timer 1 in case of RTC emulation.
176 static void hpet_reserve_msi_timers(struct hpet_data
*hd
);
178 static void hpet_reserve_platform_timers(unsigned int id
)
180 struct hpet __iomem
*hpet
= hpet_virt_address
;
181 struct hpet_timer __iomem
*timer
= &hpet
->hpet_timers
[2];
182 unsigned int nrtimers
, i
;
185 nrtimers
= ((id
& HPET_ID_NUMBER
) >> HPET_ID_NUMBER_SHIFT
) + 1;
187 memset(&hd
, 0, sizeof(hd
));
188 hd
.hd_phys_address
= hpet_address
;
189 hd
.hd_address
= hpet
;
190 hd
.hd_nirqs
= nrtimers
;
191 hpet_reserve_timer(&hd
, 0);
193 #ifdef CONFIG_HPET_EMULATE_RTC
194 hpet_reserve_timer(&hd
, 1);
198 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
199 * is wrong for i8259!) not the output IRQ. Many BIOS writers
200 * don't bother configuring *any* comparator interrupts.
202 hd
.hd_irq
[0] = HPET_LEGACY_8254
;
203 hd
.hd_irq
[1] = HPET_LEGACY_RTC
;
205 for (i
= 2; i
< nrtimers
; timer
++, i
++) {
206 hd
.hd_irq
[i
] = (readl(&timer
->hpet_config
) &
207 Tn_INT_ROUTE_CNF_MASK
) >> Tn_INT_ROUTE_CNF_SHIFT
;
210 hpet_reserve_msi_timers(&hd
);
216 static void hpet_reserve_platform_timers(unsigned int id
) { }
222 static unsigned long hpet_freq
;
224 static void hpet_legacy_set_mode(enum clock_event_mode mode
,
225 struct clock_event_device
*evt
);
226 static int hpet_legacy_next_event(unsigned long delta
,
227 struct clock_event_device
*evt
);
230 * The hpet clock event device
232 static struct clock_event_device hpet_clockevent
= {
234 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
235 .set_mode
= hpet_legacy_set_mode
,
236 .set_next_event
= hpet_legacy_next_event
,
241 static void hpet_stop_counter(void)
243 unsigned long cfg
= hpet_readl(HPET_CFG
);
244 cfg
&= ~HPET_CFG_ENABLE
;
245 hpet_writel(cfg
, HPET_CFG
);
248 static void hpet_reset_counter(void)
250 hpet_writel(0, HPET_COUNTER
);
251 hpet_writel(0, HPET_COUNTER
+ 4);
254 static void hpet_start_counter(void)
256 unsigned int cfg
= hpet_readl(HPET_CFG
);
257 cfg
|= HPET_CFG_ENABLE
;
258 hpet_writel(cfg
, HPET_CFG
);
261 static void hpet_restart_counter(void)
264 hpet_reset_counter();
265 hpet_start_counter();
268 static void hpet_resume_device(void)
273 static void hpet_resume_counter(struct clocksource
*cs
)
275 hpet_resume_device();
276 hpet_restart_counter();
279 static void hpet_enable_legacy_int(void)
281 unsigned int cfg
= hpet_readl(HPET_CFG
);
283 cfg
|= HPET_CFG_LEGACY
;
284 hpet_writel(cfg
, HPET_CFG
);
285 hpet_legacy_int_enabled
= 1;
288 static void hpet_legacy_clockevent_register(void)
290 /* Start HPET legacy interrupts */
291 hpet_enable_legacy_int();
294 * Start hpet with the boot cpu mask and make it
295 * global after the IO_APIC has been initialized.
297 hpet_clockevent
.cpumask
= cpumask_of(smp_processor_id());
298 clockevents_config_and_register(&hpet_clockevent
, hpet_freq
,
299 HPET_MIN_PROG_DELTA
, 0x7FFFFFFF);
300 global_clock_event
= &hpet_clockevent
;
301 printk(KERN_DEBUG
"hpet clockevent registered\n");
304 static int hpet_setup_msi_irq(unsigned int irq
);
306 static void hpet_set_mode(enum clock_event_mode mode
,
307 struct clock_event_device
*evt
, int timer
)
309 unsigned int cfg
, cmp
, now
;
313 case CLOCK_EVT_MODE_PERIODIC
:
315 delta
= ((uint64_t)(NSEC_PER_SEC
/HZ
)) * evt
->mult
;
316 delta
>>= evt
->shift
;
317 now
= hpet_readl(HPET_COUNTER
);
318 cmp
= now
+ (unsigned int) delta
;
319 cfg
= hpet_readl(HPET_Tn_CFG(timer
));
320 /* Make sure we use edge triggered interrupts */
321 cfg
&= ~HPET_TN_LEVEL
;
322 cfg
|= HPET_TN_ENABLE
| HPET_TN_PERIODIC
|
323 HPET_TN_SETVAL
| HPET_TN_32BIT
;
324 hpet_writel(cfg
, HPET_Tn_CFG(timer
));
325 hpet_writel(cmp
, HPET_Tn_CMP(timer
));
328 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
329 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
330 * bit is automatically cleared after the first write.
331 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
332 * Publication # 24674)
334 hpet_writel((unsigned int) delta
, HPET_Tn_CMP(timer
));
335 hpet_start_counter();
339 case CLOCK_EVT_MODE_ONESHOT
:
340 cfg
= hpet_readl(HPET_Tn_CFG(timer
));
341 cfg
&= ~HPET_TN_PERIODIC
;
342 cfg
|= HPET_TN_ENABLE
| HPET_TN_32BIT
;
343 hpet_writel(cfg
, HPET_Tn_CFG(timer
));
346 case CLOCK_EVT_MODE_UNUSED
:
347 case CLOCK_EVT_MODE_SHUTDOWN
:
348 cfg
= hpet_readl(HPET_Tn_CFG(timer
));
349 cfg
&= ~HPET_TN_ENABLE
;
350 hpet_writel(cfg
, HPET_Tn_CFG(timer
));
353 case CLOCK_EVT_MODE_RESUME
:
355 hpet_enable_legacy_int();
357 struct hpet_dev
*hdev
= EVT_TO_HPET_DEV(evt
);
358 hpet_setup_msi_irq(hdev
->irq
);
359 disable_irq(hdev
->irq
);
360 irq_set_affinity(hdev
->irq
, cpumask_of(hdev
->cpu
));
361 enable_irq(hdev
->irq
);
368 static int hpet_next_event(unsigned long delta
,
369 struct clock_event_device
*evt
, int timer
)
374 cnt
= hpet_readl(HPET_COUNTER
);
376 hpet_writel(cnt
, HPET_Tn_CMP(timer
));
379 * HPETs are a complete disaster. The compare register is
380 * based on a equal comparison and neither provides a less
381 * than or equal functionality (which would require to take
382 * the wraparound into account) nor a simple count down event
383 * mode. Further the write to the comparator register is
384 * delayed internally up to two HPET clock cycles in certain
385 * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
386 * longer delays. We worked around that by reading back the
387 * compare register, but that required another workaround for
388 * ICH9,10 chips where the first readout after write can
389 * return the old stale value. We already had a minimum
390 * programming delta of 5us enforced, but a NMI or SMI hitting
391 * between the counter readout and the comparator write can
392 * move us behind that point easily. Now instead of reading
393 * the compare register back several times, we make the ETIME
394 * decision based on the following: Return ETIME if the
395 * counter value after the write is less than HPET_MIN_CYCLES
396 * away from the event or if the counter is already ahead of
397 * the event. The minimum programming delta for the generic
398 * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
400 res
= (s32
)(cnt
- hpet_readl(HPET_COUNTER
));
402 return res
< HPET_MIN_CYCLES
? -ETIME
: 0;
405 static void hpet_legacy_set_mode(enum clock_event_mode mode
,
406 struct clock_event_device
*evt
)
408 hpet_set_mode(mode
, evt
, 0);
411 static int hpet_legacy_next_event(unsigned long delta
,
412 struct clock_event_device
*evt
)
414 return hpet_next_event(delta
, evt
, 0);
420 #ifdef CONFIG_PCI_MSI
422 static DEFINE_PER_CPU(struct hpet_dev
*, cpu_hpet_dev
);
423 static struct hpet_dev
*hpet_devs
;
425 void hpet_msi_unmask(struct irq_data
*data
)
427 struct hpet_dev
*hdev
= data
->handler_data
;
431 cfg
= hpet_readl(HPET_Tn_CFG(hdev
->num
));
433 hpet_writel(cfg
, HPET_Tn_CFG(hdev
->num
));
436 void hpet_msi_mask(struct irq_data
*data
)
438 struct hpet_dev
*hdev
= data
->handler_data
;
442 cfg
= hpet_readl(HPET_Tn_CFG(hdev
->num
));
444 hpet_writel(cfg
, HPET_Tn_CFG(hdev
->num
));
447 void hpet_msi_write(struct hpet_dev
*hdev
, struct msi_msg
*msg
)
449 hpet_writel(msg
->data
, HPET_Tn_ROUTE(hdev
->num
));
450 hpet_writel(msg
->address_lo
, HPET_Tn_ROUTE(hdev
->num
) + 4);
453 void hpet_msi_read(struct hpet_dev
*hdev
, struct msi_msg
*msg
)
455 msg
->data
= hpet_readl(HPET_Tn_ROUTE(hdev
->num
));
456 msg
->address_lo
= hpet_readl(HPET_Tn_ROUTE(hdev
->num
) + 4);
460 static void hpet_msi_set_mode(enum clock_event_mode mode
,
461 struct clock_event_device
*evt
)
463 struct hpet_dev
*hdev
= EVT_TO_HPET_DEV(evt
);
464 hpet_set_mode(mode
, evt
, hdev
->num
);
467 static int hpet_msi_next_event(unsigned long delta
,
468 struct clock_event_device
*evt
)
470 struct hpet_dev
*hdev
= EVT_TO_HPET_DEV(evt
);
471 return hpet_next_event(delta
, evt
, hdev
->num
);
474 static int hpet_setup_msi_irq(unsigned int irq
)
476 if (arch_setup_hpet_msi(irq
, hpet_blockid
)) {
483 static int hpet_assign_irq(struct hpet_dev
*dev
)
487 irq
= create_irq_nr(0, -1);
491 irq_set_handler_data(irq
, dev
);
493 if (hpet_setup_msi_irq(irq
))
500 static irqreturn_t
hpet_interrupt_handler(int irq
, void *data
)
502 struct hpet_dev
*dev
= (struct hpet_dev
*)data
;
503 struct clock_event_device
*hevt
= &dev
->evt
;
505 if (!hevt
->event_handler
) {
506 printk(KERN_INFO
"Spurious HPET timer interrupt on HPET timer %d\n",
511 hevt
->event_handler(hevt
);
515 static int hpet_setup_irq(struct hpet_dev
*dev
)
518 if (request_irq(dev
->irq
, hpet_interrupt_handler
,
519 IRQF_TIMER
| IRQF_DISABLED
| IRQF_NOBALANCING
,
523 disable_irq(dev
->irq
);
524 irq_set_affinity(dev
->irq
, cpumask_of(dev
->cpu
));
525 enable_irq(dev
->irq
);
527 printk(KERN_DEBUG
"hpet: %s irq %d for MSI\n",
528 dev
->name
, dev
->irq
);
533 /* This should be called in specific @cpu */
534 static void init_one_hpet_msi_clockevent(struct hpet_dev
*hdev
, int cpu
)
536 struct clock_event_device
*evt
= &hdev
->evt
;
538 WARN_ON(cpu
!= smp_processor_id());
539 if (!(hdev
->flags
& HPET_DEV_VALID
))
542 if (hpet_setup_msi_irq(hdev
->irq
))
546 per_cpu(cpu_hpet_dev
, cpu
) = hdev
;
547 evt
->name
= hdev
->name
;
548 hpet_setup_irq(hdev
);
549 evt
->irq
= hdev
->irq
;
552 evt
->features
= CLOCK_EVT_FEAT_ONESHOT
;
553 if (hdev
->flags
& HPET_DEV_PERI_CAP
)
554 evt
->features
|= CLOCK_EVT_FEAT_PERIODIC
;
556 evt
->set_mode
= hpet_msi_set_mode
;
557 evt
->set_next_event
= hpet_msi_next_event
;
558 evt
->cpumask
= cpumask_of(hdev
->cpu
);
560 clockevents_config_and_register(evt
, hpet_freq
, HPET_MIN_PROG_DELTA
,
565 /* Reserve at least one timer for userspace (/dev/hpet) */
566 #define RESERVE_TIMERS 1
568 #define RESERVE_TIMERS 0
571 static void hpet_msi_capability_lookup(unsigned int start_timer
)
574 unsigned int num_timers
;
575 unsigned int num_timers_used
= 0;
578 if (hpet_msi_disable
)
581 if (boot_cpu_has(X86_FEATURE_ARAT
))
583 id
= hpet_readl(HPET_ID
);
585 num_timers
= ((id
& HPET_ID_NUMBER
) >> HPET_ID_NUMBER_SHIFT
);
586 num_timers
++; /* Value read out starts from 0 */
589 hpet_devs
= kzalloc(sizeof(struct hpet_dev
) * num_timers
, GFP_KERNEL
);
593 hpet_num_timers
= num_timers
;
595 for (i
= start_timer
; i
< num_timers
- RESERVE_TIMERS
; i
++) {
596 struct hpet_dev
*hdev
= &hpet_devs
[num_timers_used
];
597 unsigned int cfg
= hpet_readl(HPET_Tn_CFG(i
));
599 /* Only consider HPET timer with MSI support */
600 if (!(cfg
& HPET_TN_FSB_CAP
))
604 if (cfg
& HPET_TN_PERIODIC_CAP
)
605 hdev
->flags
|= HPET_DEV_PERI_CAP
;
608 sprintf(hdev
->name
, "hpet%d", i
);
609 if (hpet_assign_irq(hdev
))
612 hdev
->flags
|= HPET_DEV_FSB_CAP
;
613 hdev
->flags
|= HPET_DEV_VALID
;
615 if (num_timers_used
== num_possible_cpus())
619 printk(KERN_INFO
"HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
620 num_timers
, num_timers_used
);
624 static void hpet_reserve_msi_timers(struct hpet_data
*hd
)
631 for (i
= 0; i
< hpet_num_timers
; i
++) {
632 struct hpet_dev
*hdev
= &hpet_devs
[i
];
634 if (!(hdev
->flags
& HPET_DEV_VALID
))
637 hd
->hd_irq
[hdev
->num
] = hdev
->irq
;
638 hpet_reserve_timer(hd
, hdev
->num
);
643 static struct hpet_dev
*hpet_get_unused_timer(void)
650 for (i
= 0; i
< hpet_num_timers
; i
++) {
651 struct hpet_dev
*hdev
= &hpet_devs
[i
];
653 if (!(hdev
->flags
& HPET_DEV_VALID
))
655 if (test_and_set_bit(HPET_DEV_USED_BIT
,
656 (unsigned long *)&hdev
->flags
))
663 struct hpet_work_struct
{
664 struct delayed_work work
;
665 struct completion complete
;
668 static void hpet_work(struct work_struct
*w
)
670 struct hpet_dev
*hdev
;
671 int cpu
= smp_processor_id();
672 struct hpet_work_struct
*hpet_work
;
674 hpet_work
= container_of(w
, struct hpet_work_struct
, work
.work
);
676 hdev
= hpet_get_unused_timer();
678 init_one_hpet_msi_clockevent(hdev
, cpu
);
680 complete(&hpet_work
->complete
);
683 static int hpet_cpuhp_notify(struct notifier_block
*n
,
684 unsigned long action
, void *hcpu
)
686 unsigned long cpu
= (unsigned long)hcpu
;
687 struct hpet_work_struct work
;
688 struct hpet_dev
*hdev
= per_cpu(cpu_hpet_dev
, cpu
);
690 switch (action
& 0xf) {
692 INIT_DELAYED_WORK_ONSTACK(&work
.work
, hpet_work
);
693 init_completion(&work
.complete
);
694 /* FIXME: add schedule_work_on() */
695 schedule_delayed_work_on(cpu
, &work
.work
, 0);
696 wait_for_completion(&work
.complete
);
697 destroy_timer_on_stack(&work
.work
.timer
);
701 free_irq(hdev
->irq
, hdev
);
702 hdev
->flags
&= ~HPET_DEV_USED
;
703 per_cpu(cpu_hpet_dev
, cpu
) = NULL
;
711 static int hpet_setup_msi_irq(unsigned int irq
)
715 static void hpet_msi_capability_lookup(unsigned int start_timer
)
721 static void hpet_reserve_msi_timers(struct hpet_data
*hd
)
727 static int hpet_cpuhp_notify(struct notifier_block
*n
,
728 unsigned long action
, void *hcpu
)
736 * Clock source related code
738 static cycle_t
read_hpet(struct clocksource
*cs
)
740 return (cycle_t
)hpet_readl(HPET_COUNTER
);
743 static struct clocksource clocksource_hpet
= {
748 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
749 .resume
= hpet_resume_counter
,
751 .archdata
= { .vclock_mode
= VCLOCK_HPET
},
755 static int hpet_clocksource_register(void)
760 /* Start the counter */
761 hpet_restart_counter();
763 /* Verify whether hpet counter works */
764 t1
= hpet_readl(HPET_COUNTER
);
768 * We don't know the TSC frequency yet, but waiting for
769 * 200000 TSC cycles is safe:
776 } while ((now
- start
) < 200000UL);
778 if (t1
== hpet_readl(HPET_COUNTER
)) {
780 "HPET counter not counting. HPET disabled\n");
784 clocksource_register_hz(&clocksource_hpet
, (u32
)hpet_freq
);
789 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
791 int __init
hpet_enable(void)
793 unsigned long hpet_period
;
798 if (!is_hpet_capable())
804 * Read the period and check for a sane value:
806 hpet_period
= hpet_readl(HPET_PERIOD
);
809 * AMD SB700 based systems with spread spectrum enabled use a
810 * SMM based HPET emulation to provide proper frequency
811 * setting. The SMM code is initialized with the first HPET
812 * register access and takes some time to complete. During
813 * this time the config register reads 0xffffffff. We check
814 * for max. 1000 loops whether the config register reads a non
815 * 0xffffffff value to make sure that HPET is up and running
816 * before we go further. A counting loop is safe, as the HPET
817 * access takes thousands of CPU cycles. On non SB700 based
818 * machines this check is only done once and has no side
821 for (i
= 0; hpet_readl(HPET_CFG
) == 0xFFFFFFFF; i
++) {
824 "HPET config register value = 0xFFFFFFFF. "
830 if (hpet_period
< HPET_MIN_PERIOD
|| hpet_period
> HPET_MAX_PERIOD
)
834 * The period is a femto seconds value. Convert it to a
838 do_div(freq
, hpet_period
);
842 * Read the HPET ID register to retrieve the IRQ routing
843 * information and the number of channels
845 id
= hpet_readl(HPET_ID
);
848 #ifdef CONFIG_HPET_EMULATE_RTC
850 * The legacy routing mode needs at least two channels, tick timer
851 * and the rtc emulation channel.
853 if (!(id
& HPET_ID_NUMBER
))
857 if (hpet_clocksource_register())
860 if (id
& HPET_ID_LEGSUP
) {
861 hpet_legacy_clockevent_register();
867 hpet_clear_mapping();
873 * Needs to be late, as the reserve_timer code calls kalloc !
875 * Not a problem on i386 as hpet_enable is called from late_time_init,
876 * but on x86_64 it is necessary !
878 static __init
int hpet_late_init(void)
882 if (boot_hpet_disable
)
886 if (!force_hpet_address
)
889 hpet_address
= force_hpet_address
;
893 if (!hpet_virt_address
)
896 if (hpet_readl(HPET_ID
) & HPET_ID_LEGSUP
)
897 hpet_msi_capability_lookup(2);
899 hpet_msi_capability_lookup(0);
901 hpet_reserve_platform_timers(hpet_readl(HPET_ID
));
904 if (hpet_msi_disable
)
907 if (boot_cpu_has(X86_FEATURE_ARAT
))
910 for_each_online_cpu(cpu
) {
911 hpet_cpuhp_notify(NULL
, CPU_ONLINE
, (void *)(long)cpu
);
914 /* This notifier should be called after workqueue is ready */
915 hotcpu_notifier(hpet_cpuhp_notify
, -20);
919 fs_initcall(hpet_late_init
);
921 void hpet_disable(void)
923 if (is_hpet_capable() && hpet_virt_address
) {
924 unsigned int cfg
= hpet_readl(HPET_CFG
);
926 if (hpet_legacy_int_enabled
) {
927 cfg
&= ~HPET_CFG_LEGACY
;
928 hpet_legacy_int_enabled
= 0;
930 cfg
&= ~HPET_CFG_ENABLE
;
931 hpet_writel(cfg
, HPET_CFG
);
935 #ifdef CONFIG_HPET_EMULATE_RTC
937 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
938 * is enabled, we support RTC interrupt functionality in software.
939 * RTC has 3 kinds of interrupts:
940 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
942 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
943 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
944 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
945 * (1) and (2) above are implemented using polling at a frequency of
946 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
947 * overhead. (DEFAULT_RTC_INT_FREQ)
948 * For (3), we use interrupts at 64Hz or user specified periodic
949 * frequency, whichever is higher.
951 #include <linux/mc146818rtc.h>
952 #include <linux/rtc.h>
955 #define DEFAULT_RTC_INT_FREQ 64
956 #define DEFAULT_RTC_SHIFT 6
957 #define RTC_NUM_INTS 1
959 static unsigned long hpet_rtc_flags
;
960 static int hpet_prev_update_sec
;
961 static struct rtc_time hpet_alarm_time
;
962 static unsigned long hpet_pie_count
;
963 static u32 hpet_t1_cmp
;
964 static u32 hpet_default_delta
;
965 static u32 hpet_pie_delta
;
966 static unsigned long hpet_pie_limit
;
968 static rtc_irq_handler irq_handler
;
971 * Check that the hpet counter c1 is ahead of the c2
973 static inline int hpet_cnt_ahead(u32 c1
, u32 c2
)
975 return (s32
)(c2
- c1
) < 0;
979 * Registers a IRQ handler.
981 int hpet_register_irq_handler(rtc_irq_handler handler
)
983 if (!is_hpet_enabled())
988 irq_handler
= handler
;
992 EXPORT_SYMBOL_GPL(hpet_register_irq_handler
);
995 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
998 void hpet_unregister_irq_handler(rtc_irq_handler handler
)
1000 if (!is_hpet_enabled())
1006 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler
);
1009 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1010 * is not supported by all HPET implementations for timer 1.
1012 * hpet_rtc_timer_init() is called when the rtc is initialized.
1014 int hpet_rtc_timer_init(void)
1016 unsigned int cfg
, cnt
, delta
;
1017 unsigned long flags
;
1019 if (!is_hpet_enabled())
1022 if (!hpet_default_delta
) {
1025 clc
= (uint64_t) hpet_clockevent
.mult
* NSEC_PER_SEC
;
1026 clc
>>= hpet_clockevent
.shift
+ DEFAULT_RTC_SHIFT
;
1027 hpet_default_delta
= clc
;
1030 if (!(hpet_rtc_flags
& RTC_PIE
) || hpet_pie_limit
)
1031 delta
= hpet_default_delta
;
1033 delta
= hpet_pie_delta
;
1035 local_irq_save(flags
);
1037 cnt
= delta
+ hpet_readl(HPET_COUNTER
);
1038 hpet_writel(cnt
, HPET_T1_CMP
);
1041 cfg
= hpet_readl(HPET_T1_CFG
);
1042 cfg
&= ~HPET_TN_PERIODIC
;
1043 cfg
|= HPET_TN_ENABLE
| HPET_TN_32BIT
;
1044 hpet_writel(cfg
, HPET_T1_CFG
);
1046 local_irq_restore(flags
);
1050 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init
);
1053 * The functions below are called from rtc driver.
1054 * Return 0 if HPET is not being used.
1055 * Otherwise do the necessary changes and return 1.
1057 int hpet_mask_rtc_irq_bit(unsigned long bit_mask
)
1059 if (!is_hpet_enabled())
1062 hpet_rtc_flags
&= ~bit_mask
;
1065 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit
);
1067 int hpet_set_rtc_irq_bit(unsigned long bit_mask
)
1069 unsigned long oldbits
= hpet_rtc_flags
;
1071 if (!is_hpet_enabled())
1074 hpet_rtc_flags
|= bit_mask
;
1076 if ((bit_mask
& RTC_UIE
) && !(oldbits
& RTC_UIE
))
1077 hpet_prev_update_sec
= -1;
1080 hpet_rtc_timer_init();
1084 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit
);
1086 int hpet_set_alarm_time(unsigned char hrs
, unsigned char min
,
1089 if (!is_hpet_enabled())
1092 hpet_alarm_time
.tm_hour
= hrs
;
1093 hpet_alarm_time
.tm_min
= min
;
1094 hpet_alarm_time
.tm_sec
= sec
;
1098 EXPORT_SYMBOL_GPL(hpet_set_alarm_time
);
1100 int hpet_set_periodic_freq(unsigned long freq
)
1104 if (!is_hpet_enabled())
1107 if (freq
<= DEFAULT_RTC_INT_FREQ
)
1108 hpet_pie_limit
= DEFAULT_RTC_INT_FREQ
/ freq
;
1110 clc
= (uint64_t) hpet_clockevent
.mult
* NSEC_PER_SEC
;
1112 clc
>>= hpet_clockevent
.shift
;
1113 hpet_pie_delta
= clc
;
1118 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq
);
1120 int hpet_rtc_dropped_irq(void)
1122 return is_hpet_enabled();
1124 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq
);
1126 static void hpet_rtc_timer_reinit(void)
1128 unsigned int cfg
, delta
;
1131 if (unlikely(!hpet_rtc_flags
)) {
1132 cfg
= hpet_readl(HPET_T1_CFG
);
1133 cfg
&= ~HPET_TN_ENABLE
;
1134 hpet_writel(cfg
, HPET_T1_CFG
);
1138 if (!(hpet_rtc_flags
& RTC_PIE
) || hpet_pie_limit
)
1139 delta
= hpet_default_delta
;
1141 delta
= hpet_pie_delta
;
1144 * Increment the comparator value until we are ahead of the
1148 hpet_t1_cmp
+= delta
;
1149 hpet_writel(hpet_t1_cmp
, HPET_T1_CMP
);
1151 } while (!hpet_cnt_ahead(hpet_t1_cmp
, hpet_readl(HPET_COUNTER
)));
1154 if (hpet_rtc_flags
& RTC_PIE
)
1155 hpet_pie_count
+= lost_ints
;
1156 if (printk_ratelimit())
1157 printk(KERN_WARNING
"hpet1: lost %d rtc interrupts\n",
1162 irqreturn_t
hpet_rtc_interrupt(int irq
, void *dev_id
)
1164 struct rtc_time curr_time
;
1165 unsigned long rtc_int_flag
= 0;
1167 hpet_rtc_timer_reinit();
1168 memset(&curr_time
, 0, sizeof(struct rtc_time
));
1170 if (hpet_rtc_flags
& (RTC_UIE
| RTC_AIE
))
1171 get_rtc_time(&curr_time
);
1173 if (hpet_rtc_flags
& RTC_UIE
&&
1174 curr_time
.tm_sec
!= hpet_prev_update_sec
) {
1175 if (hpet_prev_update_sec
>= 0)
1176 rtc_int_flag
= RTC_UF
;
1177 hpet_prev_update_sec
= curr_time
.tm_sec
;
1180 if (hpet_rtc_flags
& RTC_PIE
&&
1181 ++hpet_pie_count
>= hpet_pie_limit
) {
1182 rtc_int_flag
|= RTC_PF
;
1186 if (hpet_rtc_flags
& RTC_AIE
&&
1187 (curr_time
.tm_sec
== hpet_alarm_time
.tm_sec
) &&
1188 (curr_time
.tm_min
== hpet_alarm_time
.tm_min
) &&
1189 (curr_time
.tm_hour
== hpet_alarm_time
.tm_hour
))
1190 rtc_int_flag
|= RTC_AF
;
1193 rtc_int_flag
|= (RTC_IRQF
| (RTC_NUM_INTS
<< 8));
1195 irq_handler(rtc_int_flag
, dev_id
);
1199 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt
);