x86: fix RTC_AIE with CONFIG_HPET_EMULATE_RTC
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / hpet.c
blob2f99ee206b9527c3cad6250caa95e2a3ceac9268
1 #include <linux/clocksource.h>
2 #include <linux/clockchips.h>
3 #include <linux/delay.h>
4 #include <linux/errno.h>
5 #include <linux/hpet.h>
6 #include <linux/init.h>
7 #include <linux/sysdev.h>
8 #include <linux/pm.h>
9 #include <linux/delay.h>
11 #include <asm/fixmap.h>
12 #include <asm/hpet.h>
13 #include <asm/i8253.h>
14 #include <asm/io.h>
16 #define HPET_MASK CLOCKSOURCE_MASK(32)
17 #define HPET_SHIFT 22
19 /* FSEC = 10^-15 NSEC = 10^-9 */
20 #define FSEC_PER_NSEC 1000000
23 * HPET address is set in acpi/boot.c, when an ACPI entry exists
25 unsigned long hpet_address;
26 static void __iomem *hpet_virt_address;
28 unsigned long hpet_readl(unsigned long a)
30 return readl(hpet_virt_address + a);
33 static inline void hpet_writel(unsigned long d, unsigned long a)
35 writel(d, hpet_virt_address + a);
38 #ifdef CONFIG_X86_64
40 #include <asm/pgtable.h>
42 static inline void hpet_set_mapping(void)
44 set_fixmap_nocache(FIX_HPET_BASE, hpet_address);
45 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
46 hpet_virt_address = (void __iomem *)fix_to_virt(FIX_HPET_BASE);
49 static inline void hpet_clear_mapping(void)
51 hpet_virt_address = NULL;
54 #else
56 static inline void hpet_set_mapping(void)
58 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
61 static inline void hpet_clear_mapping(void)
63 iounmap(hpet_virt_address);
64 hpet_virt_address = NULL;
66 #endif
69 * HPET command line enable / disable
71 static int boot_hpet_disable;
72 int hpet_force_user;
74 static int __init hpet_setup(char* str)
76 if (str) {
77 if (!strncmp("disable", str, 7))
78 boot_hpet_disable = 1;
79 if (!strncmp("force", str, 5))
80 hpet_force_user = 1;
82 return 1;
84 __setup("hpet=", hpet_setup);
86 static int __init disable_hpet(char *str)
88 boot_hpet_disable = 1;
89 return 1;
91 __setup("nohpet", disable_hpet);
93 static inline int is_hpet_capable(void)
95 return (!boot_hpet_disable && hpet_address);
99 * HPET timer interrupt enable / disable
101 static int hpet_legacy_int_enabled;
104 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
106 int is_hpet_enabled(void)
108 return is_hpet_capable() && hpet_legacy_int_enabled;
112 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
113 * timer 0 and timer 1 in case of RTC emulation.
115 #ifdef CONFIG_HPET
116 static void hpet_reserve_platform_timers(unsigned long id)
118 struct hpet __iomem *hpet = hpet_virt_address;
119 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
120 unsigned int nrtimers, i;
121 struct hpet_data hd;
123 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
125 memset(&hd, 0, sizeof (hd));
126 hd.hd_phys_address = hpet_address;
127 hd.hd_address = hpet;
128 hd.hd_nirqs = nrtimers;
129 hd.hd_flags = HPET_DATA_PLATFORM;
130 hpet_reserve_timer(&hd, 0);
132 #ifdef CONFIG_HPET_EMULATE_RTC
133 hpet_reserve_timer(&hd, 1);
134 #endif
136 hd.hd_irq[0] = HPET_LEGACY_8254;
137 hd.hd_irq[1] = HPET_LEGACY_RTC;
139 for (i = 2; i < nrtimers; timer++, i++)
140 hd.hd_irq[i] = (timer->hpet_config & Tn_INT_ROUTE_CNF_MASK) >>
141 Tn_INT_ROUTE_CNF_SHIFT;
143 hpet_alloc(&hd);
146 #else
147 static void hpet_reserve_platform_timers(unsigned long id) { }
148 #endif
151 * Common hpet info
153 static unsigned long hpet_period;
155 static void hpet_legacy_set_mode(enum clock_event_mode mode,
156 struct clock_event_device *evt);
157 static int hpet_legacy_next_event(unsigned long delta,
158 struct clock_event_device *evt);
161 * The hpet clock event device
163 static struct clock_event_device hpet_clockevent = {
164 .name = "hpet",
165 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
166 .set_mode = hpet_legacy_set_mode,
167 .set_next_event = hpet_legacy_next_event,
168 .shift = 32,
169 .irq = 0,
170 .rating = 50,
173 static void hpet_start_counter(void)
175 unsigned long cfg = hpet_readl(HPET_CFG);
177 cfg &= ~HPET_CFG_ENABLE;
178 hpet_writel(cfg, HPET_CFG);
179 hpet_writel(0, HPET_COUNTER);
180 hpet_writel(0, HPET_COUNTER + 4);
181 cfg |= HPET_CFG_ENABLE;
182 hpet_writel(cfg, HPET_CFG);
185 static void hpet_resume_device(void)
187 force_hpet_resume();
190 static void hpet_restart_counter(void)
192 hpet_resume_device();
193 hpet_start_counter();
196 static void hpet_enable_legacy_int(void)
198 unsigned long cfg = hpet_readl(HPET_CFG);
200 cfg |= HPET_CFG_LEGACY;
201 hpet_writel(cfg, HPET_CFG);
202 hpet_legacy_int_enabled = 1;
205 static void hpet_legacy_clockevent_register(void)
207 uint64_t hpet_freq;
209 /* Start HPET legacy interrupts */
210 hpet_enable_legacy_int();
213 * The period is a femto seconds value. We need to calculate the
214 * scaled math multiplication factor for nanosecond to hpet tick
215 * conversion.
217 hpet_freq = 1000000000000000ULL;
218 do_div(hpet_freq, hpet_period);
219 hpet_clockevent.mult = div_sc((unsigned long) hpet_freq,
220 NSEC_PER_SEC, 32);
221 /* Calculate the min / max delta */
222 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
223 &hpet_clockevent);
224 hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30,
225 &hpet_clockevent);
228 * Start hpet with the boot cpu mask and make it
229 * global after the IO_APIC has been initialized.
231 hpet_clockevent.cpumask = cpumask_of_cpu(smp_processor_id());
232 clockevents_register_device(&hpet_clockevent);
233 global_clock_event = &hpet_clockevent;
234 printk(KERN_DEBUG "hpet clockevent registered\n");
237 static void hpet_legacy_set_mode(enum clock_event_mode mode,
238 struct clock_event_device *evt)
240 unsigned long cfg, cmp, now;
241 uint64_t delta;
243 switch(mode) {
244 case CLOCK_EVT_MODE_PERIODIC:
245 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
246 delta >>= hpet_clockevent.shift;
247 now = hpet_readl(HPET_COUNTER);
248 cmp = now + (unsigned long) delta;
249 cfg = hpet_readl(HPET_T0_CFG);
250 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
251 HPET_TN_SETVAL | HPET_TN_32BIT;
252 hpet_writel(cfg, HPET_T0_CFG);
254 * The first write after writing TN_SETVAL to the
255 * config register sets the counter value, the second
256 * write sets the period.
258 hpet_writel(cmp, HPET_T0_CMP);
259 udelay(1);
260 hpet_writel((unsigned long) delta, HPET_T0_CMP);
261 break;
263 case CLOCK_EVT_MODE_ONESHOT:
264 cfg = hpet_readl(HPET_T0_CFG);
265 cfg &= ~HPET_TN_PERIODIC;
266 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
267 hpet_writel(cfg, HPET_T0_CFG);
268 break;
270 case CLOCK_EVT_MODE_UNUSED:
271 case CLOCK_EVT_MODE_SHUTDOWN:
272 cfg = hpet_readl(HPET_T0_CFG);
273 cfg &= ~HPET_TN_ENABLE;
274 hpet_writel(cfg, HPET_T0_CFG);
275 break;
277 case CLOCK_EVT_MODE_RESUME:
278 hpet_enable_legacy_int();
279 break;
283 static int hpet_legacy_next_event(unsigned long delta,
284 struct clock_event_device *evt)
286 unsigned long cnt;
288 cnt = hpet_readl(HPET_COUNTER);
289 cnt += delta;
290 hpet_writel(cnt, HPET_T0_CMP);
292 return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0) ? -ETIME : 0;
296 * Clock source related code
298 static cycle_t read_hpet(void)
300 return (cycle_t)hpet_readl(HPET_COUNTER);
303 #ifdef CONFIG_X86_64
304 static cycle_t __vsyscall_fn vread_hpet(void)
306 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
308 #endif
310 static struct clocksource clocksource_hpet = {
311 .name = "hpet",
312 .rating = 250,
313 .read = read_hpet,
314 .mask = HPET_MASK,
315 .shift = HPET_SHIFT,
316 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
317 .resume = hpet_restart_counter,
318 #ifdef CONFIG_X86_64
319 .vread = vread_hpet,
320 #endif
323 static int hpet_clocksource_register(void)
325 u64 tmp, start, now;
326 cycle_t t1;
328 /* Start the counter */
329 hpet_start_counter();
331 /* Verify whether hpet counter works */
332 t1 = read_hpet();
333 rdtscll(start);
336 * We don't know the TSC frequency yet, but waiting for
337 * 200000 TSC cycles is safe:
338 * 4 GHz == 50us
339 * 1 GHz == 200us
341 do {
342 rep_nop();
343 rdtscll(now);
344 } while ((now - start) < 200000UL);
346 if (t1 == read_hpet()) {
347 printk(KERN_WARNING
348 "HPET counter not counting. HPET disabled\n");
349 return -ENODEV;
352 /* Initialize and register HPET clocksource
354 * hpet period is in femto seconds per cycle
355 * so we need to convert this to ns/cyc units
356 * approximated by mult/2^shift
358 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
359 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
360 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
361 * (fsec/cyc << shift)/1000000 = mult
362 * (hpet_period << shift)/FSEC_PER_NSEC = mult
364 tmp = (u64)hpet_period << HPET_SHIFT;
365 do_div(tmp, FSEC_PER_NSEC);
366 clocksource_hpet.mult = (u32)tmp;
368 clocksource_register(&clocksource_hpet);
370 return 0;
374 * Try to setup the HPET timer
376 int __init hpet_enable(void)
378 unsigned long id;
380 if (!is_hpet_capable())
381 return 0;
383 hpet_set_mapping();
386 * Read the period and check for a sane value:
388 hpet_period = hpet_readl(HPET_PERIOD);
389 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
390 goto out_nohpet;
393 * Read the HPET ID register to retrieve the IRQ routing
394 * information and the number of channels
396 id = hpet_readl(HPET_ID);
398 #ifdef CONFIG_HPET_EMULATE_RTC
400 * The legacy routing mode needs at least two channels, tick timer
401 * and the rtc emulation channel.
403 if (!(id & HPET_ID_NUMBER))
404 goto out_nohpet;
405 #endif
407 if (hpet_clocksource_register())
408 goto out_nohpet;
410 if (id & HPET_ID_LEGSUP) {
411 hpet_legacy_clockevent_register();
412 return 1;
414 return 0;
416 out_nohpet:
417 hpet_clear_mapping();
418 boot_hpet_disable = 1;
419 return 0;
423 * Needs to be late, as the reserve_timer code calls kalloc !
425 * Not a problem on i386 as hpet_enable is called from late_time_init,
426 * but on x86_64 it is necessary !
428 static __init int hpet_late_init(void)
430 if (boot_hpet_disable)
431 return -ENODEV;
433 if (!hpet_address) {
434 if (!force_hpet_address)
435 return -ENODEV;
437 hpet_address = force_hpet_address;
438 hpet_enable();
439 if (!hpet_virt_address)
440 return -ENODEV;
443 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
445 return 0;
447 fs_initcall(hpet_late_init);
449 void hpet_disable(void)
451 if (is_hpet_capable()) {
452 unsigned long cfg = hpet_readl(HPET_CFG);
454 if (hpet_legacy_int_enabled) {
455 cfg &= ~HPET_CFG_LEGACY;
456 hpet_legacy_int_enabled = 0;
458 cfg &= ~HPET_CFG_ENABLE;
459 hpet_writel(cfg, HPET_CFG);
463 #ifdef CONFIG_HPET_EMULATE_RTC
465 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
466 * is enabled, we support RTC interrupt functionality in software.
467 * RTC has 3 kinds of interrupts:
468 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
469 * is updated
470 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
471 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
472 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
473 * (1) and (2) above are implemented using polling at a frequency of
474 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
475 * overhead. (DEFAULT_RTC_INT_FREQ)
476 * For (3), we use interrupts at 64Hz or user specified periodic
477 * frequency, whichever is higher.
479 #include <linux/mc146818rtc.h>
480 #include <linux/rtc.h>
482 #define DEFAULT_RTC_INT_FREQ 64
483 #define DEFAULT_RTC_SHIFT 6
484 #define RTC_NUM_INTS 1
486 static unsigned long hpet_rtc_flags;
487 static unsigned long hpet_prev_update_sec;
488 static struct rtc_time hpet_alarm_time;
489 static unsigned long hpet_pie_count;
490 static unsigned long hpet_t1_cmp;
491 static unsigned long hpet_default_delta;
492 static unsigned long hpet_pie_delta;
493 static unsigned long hpet_pie_limit;
496 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
497 * is not supported by all HPET implementations for timer 1.
499 * hpet_rtc_timer_init() is called when the rtc is initialized.
501 int hpet_rtc_timer_init(void)
503 unsigned long cfg, cnt, delta, flags;
505 if (!is_hpet_enabled())
506 return 0;
508 if (!hpet_default_delta) {
509 uint64_t clc;
511 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
512 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
513 hpet_default_delta = (unsigned long) clc;
516 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
517 delta = hpet_default_delta;
518 else
519 delta = hpet_pie_delta;
521 local_irq_save(flags);
523 cnt = delta + hpet_readl(HPET_COUNTER);
524 hpet_writel(cnt, HPET_T1_CMP);
525 hpet_t1_cmp = cnt;
527 cfg = hpet_readl(HPET_T1_CFG);
528 cfg &= ~HPET_TN_PERIODIC;
529 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
530 hpet_writel(cfg, HPET_T1_CFG);
532 local_irq_restore(flags);
534 return 1;
538 * The functions below are called from rtc driver.
539 * Return 0 if HPET is not being used.
540 * Otherwise do the necessary changes and return 1.
542 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
544 if (!is_hpet_enabled())
545 return 0;
547 hpet_rtc_flags &= ~bit_mask;
548 return 1;
551 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
553 unsigned long oldbits = hpet_rtc_flags;
555 if (!is_hpet_enabled())
556 return 0;
558 hpet_rtc_flags |= bit_mask;
560 if (!oldbits)
561 hpet_rtc_timer_init();
563 return 1;
566 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
567 unsigned char sec)
569 if (!is_hpet_enabled())
570 return 0;
572 hpet_alarm_time.tm_hour = hrs;
573 hpet_alarm_time.tm_min = min;
574 hpet_alarm_time.tm_sec = sec;
576 return 1;
579 int hpet_set_periodic_freq(unsigned long freq)
581 uint64_t clc;
583 if (!is_hpet_enabled())
584 return 0;
586 if (freq <= DEFAULT_RTC_INT_FREQ)
587 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
588 else {
589 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
590 do_div(clc, freq);
591 clc >>= hpet_clockevent.shift;
592 hpet_pie_delta = (unsigned long) clc;
594 return 1;
597 int hpet_rtc_dropped_irq(void)
599 return is_hpet_enabled();
602 static void hpet_rtc_timer_reinit(void)
604 unsigned long cfg, delta;
605 int lost_ints = -1;
607 if (unlikely(!hpet_rtc_flags)) {
608 cfg = hpet_readl(HPET_T1_CFG);
609 cfg &= ~HPET_TN_ENABLE;
610 hpet_writel(cfg, HPET_T1_CFG);
611 return;
614 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
615 delta = hpet_default_delta;
616 else
617 delta = hpet_pie_delta;
620 * Increment the comparator value until we are ahead of the
621 * current count.
623 do {
624 hpet_t1_cmp += delta;
625 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
626 lost_ints++;
627 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
629 if (lost_ints) {
630 if (hpet_rtc_flags & RTC_PIE)
631 hpet_pie_count += lost_ints;
632 if (printk_ratelimit())
633 printk(KERN_WARNING "rtc: lost %d interrupts\n",
634 lost_ints);
638 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
640 struct rtc_time curr_time;
641 unsigned long rtc_int_flag = 0;
643 hpet_rtc_timer_reinit();
645 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
646 rtc_get_rtc_time(&curr_time);
648 if (hpet_rtc_flags & RTC_UIE &&
649 curr_time.tm_sec != hpet_prev_update_sec) {
650 rtc_int_flag = RTC_UF;
651 hpet_prev_update_sec = curr_time.tm_sec;
654 if (hpet_rtc_flags & RTC_PIE &&
655 ++hpet_pie_count >= hpet_pie_limit) {
656 rtc_int_flag |= RTC_PF;
657 hpet_pie_count = 0;
660 if (hpet_rtc_flags & RTC_AIE &&
661 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
662 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
663 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
664 rtc_int_flag |= RTC_AF;
666 if (rtc_int_flag) {
667 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
668 rtc_interrupt(rtc_int_flag, dev_id);
670 return IRQ_HANDLED;
672 #endif