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>
9 #include <linux/delay.h>
11 #include <asm/fixmap.h>
13 #include <asm/i8253.h>
16 #define HPET_MASK CLOCKSOURCE_MASK(32)
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
);
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
;
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
;
69 * HPET command line enable / disable
71 static int boot_hpet_disable
;
74 static int __init
hpet_setup(char* str
)
77 if (!strncmp("disable", str
, 7))
78 boot_hpet_disable
= 1;
79 if (!strncmp("force", str
, 5))
84 __setup("hpet=", hpet_setup
);
86 static int __init
disable_hpet(char *str
)
88 boot_hpet_disable
= 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.
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
;
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);
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
;
147 static void hpet_reserve_platform_timers(unsigned long id
) { }
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
= {
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
,
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)
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)
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
217 hpet_freq
= 1000000000000000ULL;
218 do_div(hpet_freq
, hpet_period
);
219 hpet_clockevent
.mult
= div_sc((unsigned long) hpet_freq
,
221 /* Calculate the min / max delta */
222 hpet_clockevent
.max_delta_ns
= clockevent_delta2ns(0x7FFFFFFF,
224 hpet_clockevent
.min_delta_ns
= clockevent_delta2ns(0x30,
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
;
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
);
260 hpet_writel((unsigned long) delta
, HPET_T0_CMP
);
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
);
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
);
277 case CLOCK_EVT_MODE_RESUME
:
278 hpet_enable_legacy_int();
283 static int hpet_legacy_next_event(unsigned long delta
,
284 struct clock_event_device
*evt
)
288 cnt
= hpet_readl(HPET_COUNTER
);
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
);
304 static cycle_t __vsyscall_fn
vread_hpet(void)
306 return readl((const void __iomem
*)fix_to_virt(VSYSCALL_HPET
) + 0xf0);
310 static struct clocksource clocksource_hpet
= {
316 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
317 .resume
= hpet_restart_counter
,
323 static int hpet_clocksource_register(void)
328 /* Start the counter */
329 hpet_start_counter();
331 /* Verify whether hpet counter works */
336 * We don't know the TSC frequency yet, but waiting for
337 * 200000 TSC cycles is safe:
344 } while ((now
- start
) < 200000UL);
346 if (t1
== read_hpet()) {
348 "HPET counter not counting. HPET disabled\n");
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
);
374 * Try to setup the HPET timer
376 int __init
hpet_enable(void)
380 if (!is_hpet_capable())
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
)
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
))
407 if (hpet_clocksource_register())
410 if (id
& HPET_ID_LEGSUP
) {
411 hpet_legacy_clockevent_register();
417 hpet_clear_mapping();
418 boot_hpet_disable
= 1;
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
)
434 if (!force_hpet_address
)
437 hpet_address
= force_hpet_address
;
439 if (!hpet_virt_address
)
443 hpet_reserve_platform_timers(hpet_readl(HPET_ID
));
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
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())
508 if (!hpet_default_delta
) {
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
;
519 delta
= hpet_pie_delta
;
521 local_irq_save(flags
);
523 cnt
= delta
+ hpet_readl(HPET_COUNTER
);
524 hpet_writel(cnt
, HPET_T1_CMP
);
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
);
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())
547 hpet_rtc_flags
&= ~bit_mask
;
551 int hpet_set_rtc_irq_bit(unsigned long bit_mask
)
553 unsigned long oldbits
= hpet_rtc_flags
;
555 if (!is_hpet_enabled())
558 hpet_rtc_flags
|= bit_mask
;
561 hpet_rtc_timer_init();
566 int hpet_set_alarm_time(unsigned char hrs
, unsigned char min
,
569 if (!is_hpet_enabled())
572 hpet_alarm_time
.tm_hour
= hrs
;
573 hpet_alarm_time
.tm_min
= min
;
574 hpet_alarm_time
.tm_sec
= sec
;
579 int hpet_set_periodic_freq(unsigned long freq
)
583 if (!is_hpet_enabled())
586 if (freq
<= DEFAULT_RTC_INT_FREQ
)
587 hpet_pie_limit
= DEFAULT_RTC_INT_FREQ
/ freq
;
589 clc
= (uint64_t) hpet_clockevent
.mult
* NSEC_PER_SEC
;
591 clc
>>= hpet_clockevent
.shift
;
592 hpet_pie_delta
= (unsigned long) clc
;
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
;
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
);
614 if (!(hpet_rtc_flags
& RTC_PIE
) || hpet_pie_limit
)
615 delta
= hpet_default_delta
;
617 delta
= hpet_pie_delta
;
620 * Increment the comparator value until we are ahead of the
624 hpet_t1_cmp
+= delta
;
625 hpet_writel(hpet_t1_cmp
, HPET_T1_CMP
);
627 } while ((long)(hpet_readl(HPET_COUNTER
) - hpet_t1_cmp
) > 0);
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",
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
;
660 if (hpet_rtc_flags
& RTC_PIE
&&
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
;
667 rtc_int_flag
|= (RTC_IRQF
| (RTC_NUM_INTS
<< 8));
668 rtc_interrupt(rtc_int_flag
, dev_id
);