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
;
73 static int __init
hpet_setup(char* str
)
76 if (!strncmp("disable", str
, 7))
77 boot_hpet_disable
= 1;
81 __setup("hpet=", hpet_setup
);
83 static int __init
disable_hpet(char *str
)
85 boot_hpet_disable
= 1;
88 __setup("nohpet", disable_hpet
);
90 static inline int is_hpet_capable(void)
92 return (!boot_hpet_disable
&& hpet_address
);
96 * HPET timer interrupt enable / disable
98 static int hpet_legacy_int_enabled
;
101 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
103 int is_hpet_enabled(void)
105 return is_hpet_capable() && hpet_legacy_int_enabled
;
109 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
110 * timer 0 and timer 1 in case of RTC emulation.
113 static void hpet_reserve_platform_timers(unsigned long id
)
115 struct hpet __iomem
*hpet
= hpet_virt_address
;
116 struct hpet_timer __iomem
*timer
= &hpet
->hpet_timers
[2];
117 unsigned int nrtimers
, i
;
120 nrtimers
= ((id
& HPET_ID_NUMBER
) >> HPET_ID_NUMBER_SHIFT
) + 1;
122 memset(&hd
, 0, sizeof (hd
));
123 hd
.hd_phys_address
= hpet_address
;
124 hd
.hd_address
= hpet
;
125 hd
.hd_nirqs
= nrtimers
;
126 hd
.hd_flags
= HPET_DATA_PLATFORM
;
127 hpet_reserve_timer(&hd
, 0);
129 #ifdef CONFIG_HPET_EMULATE_RTC
130 hpet_reserve_timer(&hd
, 1);
133 hd
.hd_irq
[0] = HPET_LEGACY_8254
;
134 hd
.hd_irq
[1] = HPET_LEGACY_RTC
;
136 for (i
= 2; i
< nrtimers
; timer
++, i
++)
137 hd
.hd_irq
[i
] = (timer
->hpet_config
& Tn_INT_ROUTE_CNF_MASK
) >>
138 Tn_INT_ROUTE_CNF_SHIFT
;
144 static void hpet_reserve_platform_timers(unsigned long id
) { }
150 static unsigned long hpet_period
;
152 static void hpet_legacy_set_mode(enum clock_event_mode mode
,
153 struct clock_event_device
*evt
);
154 static int hpet_legacy_next_event(unsigned long delta
,
155 struct clock_event_device
*evt
);
158 * The hpet clock event device
160 static struct clock_event_device hpet_clockevent
= {
162 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
163 .set_mode
= hpet_legacy_set_mode
,
164 .set_next_event
= hpet_legacy_next_event
,
169 static void hpet_start_counter(void)
171 unsigned long cfg
= hpet_readl(HPET_CFG
);
173 cfg
&= ~HPET_CFG_ENABLE
;
174 hpet_writel(cfg
, HPET_CFG
);
175 hpet_writel(0, HPET_COUNTER
);
176 hpet_writel(0, HPET_COUNTER
+ 4);
177 cfg
|= HPET_CFG_ENABLE
;
178 hpet_writel(cfg
, HPET_CFG
);
181 static void hpet_enable_legacy_int(void)
183 unsigned long cfg
= hpet_readl(HPET_CFG
);
185 cfg
|= HPET_CFG_LEGACY
;
186 hpet_writel(cfg
, HPET_CFG
);
187 hpet_legacy_int_enabled
= 1;
190 static void hpet_legacy_clockevent_register(void)
194 /* Start HPET legacy interrupts */
195 hpet_enable_legacy_int();
198 * The period is a femto seconds value. We need to calculate the
199 * scaled math multiplication factor for nanosecond to hpet tick
202 hpet_freq
= 1000000000000000ULL;
203 do_div(hpet_freq
, hpet_period
);
204 hpet_clockevent
.mult
= div_sc((unsigned long) hpet_freq
,
206 /* Calculate the min / max delta */
207 hpet_clockevent
.max_delta_ns
= clockevent_delta2ns(0x7FFFFFFF,
209 hpet_clockevent
.min_delta_ns
= clockevent_delta2ns(0x30,
213 * Start hpet with the boot cpu mask and make it
214 * global after the IO_APIC has been initialized.
216 hpet_clockevent
.cpumask
= cpumask_of_cpu(smp_processor_id());
217 clockevents_register_device(&hpet_clockevent
);
218 global_clock_event
= &hpet_clockevent
;
219 printk(KERN_DEBUG
"hpet clockevent registered\n");
222 static void hpet_legacy_set_mode(enum clock_event_mode mode
,
223 struct clock_event_device
*evt
)
225 unsigned long cfg
, cmp
, now
;
229 case CLOCK_EVT_MODE_PERIODIC
:
230 delta
= ((uint64_t)(NSEC_PER_SEC
/HZ
)) * hpet_clockevent
.mult
;
231 delta
>>= hpet_clockevent
.shift
;
232 now
= hpet_readl(HPET_COUNTER
);
233 cmp
= now
+ (unsigned long) delta
;
234 cfg
= hpet_readl(HPET_T0_CFG
);
235 cfg
|= HPET_TN_ENABLE
| HPET_TN_PERIODIC
|
236 HPET_TN_SETVAL
| HPET_TN_32BIT
;
237 hpet_writel(cfg
, HPET_T0_CFG
);
239 * The first write after writing TN_SETVAL to the
240 * config register sets the counter value, the second
241 * write sets the period.
243 hpet_writel(cmp
, HPET_T0_CMP
);
245 hpet_writel((unsigned long) delta
, HPET_T0_CMP
);
248 case CLOCK_EVT_MODE_ONESHOT
:
249 cfg
= hpet_readl(HPET_T0_CFG
);
250 cfg
&= ~HPET_TN_PERIODIC
;
251 cfg
|= HPET_TN_ENABLE
| HPET_TN_32BIT
;
252 hpet_writel(cfg
, HPET_T0_CFG
);
255 case CLOCK_EVT_MODE_UNUSED
:
256 case CLOCK_EVT_MODE_SHUTDOWN
:
257 cfg
= hpet_readl(HPET_T0_CFG
);
258 cfg
&= ~HPET_TN_ENABLE
;
259 hpet_writel(cfg
, HPET_T0_CFG
);
262 case CLOCK_EVT_MODE_RESUME
:
263 hpet_enable_legacy_int();
268 static int hpet_legacy_next_event(unsigned long delta
,
269 struct clock_event_device
*evt
)
273 cnt
= hpet_readl(HPET_COUNTER
);
275 hpet_writel(cnt
, HPET_T0_CMP
);
277 return ((long)(hpet_readl(HPET_COUNTER
) - cnt
) > 0) ? -ETIME
: 0;
281 * Clock source related code
283 static cycle_t
read_hpet(void)
285 return (cycle_t
)hpet_readl(HPET_COUNTER
);
289 static cycle_t __vsyscall_fn
vread_hpet(void)
291 return readl((const void __iomem
*)fix_to_virt(VSYSCALL_HPET
) + 0xf0);
295 static struct clocksource clocksource_hpet
= {
301 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
302 .resume
= hpet_start_counter
,
308 static int hpet_clocksource_register(void)
313 /* Start the counter */
314 hpet_start_counter();
316 /* Verify whether hpet counter works */
321 * We don't know the TSC frequency yet, but waiting for
322 * 200000 TSC cycles is safe:
329 } while ((now
- start
) < 200000UL);
331 if (t1
== read_hpet()) {
333 "HPET counter not counting. HPET disabled\n");
337 /* Initialize and register HPET clocksource
339 * hpet period is in femto seconds per cycle
340 * so we need to convert this to ns/cyc units
341 * aproximated by mult/2^shift
343 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
344 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
345 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
346 * (fsec/cyc << shift)/1000000 = mult
347 * (hpet_period << shift)/FSEC_PER_NSEC = mult
349 tmp
= (u64
)hpet_period
<< HPET_SHIFT
;
350 do_div(tmp
, FSEC_PER_NSEC
);
351 clocksource_hpet
.mult
= (u32
)tmp
;
353 clocksource_register(&clocksource_hpet
);
359 * Try to setup the HPET timer
361 int __init
hpet_enable(void)
365 if (!is_hpet_capable())
371 * Read the period and check for a sane value:
373 hpet_period
= hpet_readl(HPET_PERIOD
);
374 if (hpet_period
< HPET_MIN_PERIOD
|| hpet_period
> HPET_MAX_PERIOD
)
378 * Read the HPET ID register to retrieve the IRQ routing
379 * information and the number of channels
381 id
= hpet_readl(HPET_ID
);
383 #ifdef CONFIG_HPET_EMULATE_RTC
385 * The legacy routing mode needs at least two channels, tick timer
386 * and the rtc emulation channel.
388 if (!(id
& HPET_ID_NUMBER
))
392 if (hpet_clocksource_register())
395 if (id
& HPET_ID_LEGSUP
) {
396 hpet_legacy_clockevent_register();
402 hpet_clear_mapping();
403 boot_hpet_disable
= 1;
408 * Needs to be late, as the reserve_timer code calls kalloc !
410 * Not a problem on i386 as hpet_enable is called from late_time_init,
411 * but on x86_64 it is necessary !
413 static __init
int hpet_late_init(void)
415 if (!is_hpet_capable())
418 hpet_reserve_platform_timers(hpet_readl(HPET_ID
));
421 fs_initcall(hpet_late_init
);
423 #ifdef CONFIG_HPET_EMULATE_RTC
425 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
426 * is enabled, we support RTC interrupt functionality in software.
427 * RTC has 3 kinds of interrupts:
428 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
430 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
431 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
432 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
433 * (1) and (2) above are implemented using polling at a frequency of
434 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
435 * overhead. (DEFAULT_RTC_INT_FREQ)
436 * For (3), we use interrupts at 64Hz or user specified periodic
437 * frequency, whichever is higher.
439 #include <linux/mc146818rtc.h>
440 #include <linux/rtc.h>
442 #define DEFAULT_RTC_INT_FREQ 64
443 #define DEFAULT_RTC_SHIFT 6
444 #define RTC_NUM_INTS 1
446 static unsigned long hpet_rtc_flags
;
447 static unsigned long hpet_prev_update_sec
;
448 static struct rtc_time hpet_alarm_time
;
449 static unsigned long hpet_pie_count
;
450 static unsigned long hpet_t1_cmp
;
451 static unsigned long hpet_default_delta
;
452 static unsigned long hpet_pie_delta
;
453 static unsigned long hpet_pie_limit
;
456 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
457 * is not supported by all HPET implementations for timer 1.
459 * hpet_rtc_timer_init() is called when the rtc is initialized.
461 int hpet_rtc_timer_init(void)
463 unsigned long cfg
, cnt
, delta
, flags
;
465 if (!is_hpet_enabled())
468 if (!hpet_default_delta
) {
471 clc
= (uint64_t) hpet_clockevent
.mult
* NSEC_PER_SEC
;
472 clc
>>= hpet_clockevent
.shift
+ DEFAULT_RTC_SHIFT
;
473 hpet_default_delta
= (unsigned long) clc
;
476 if (!(hpet_rtc_flags
& RTC_PIE
) || hpet_pie_limit
)
477 delta
= hpet_default_delta
;
479 delta
= hpet_pie_delta
;
481 local_irq_save(flags
);
483 cnt
= delta
+ hpet_readl(HPET_COUNTER
);
484 hpet_writel(cnt
, HPET_T1_CMP
);
487 cfg
= hpet_readl(HPET_T1_CFG
);
488 cfg
&= ~HPET_TN_PERIODIC
;
489 cfg
|= HPET_TN_ENABLE
| HPET_TN_32BIT
;
490 hpet_writel(cfg
, HPET_T1_CFG
);
492 local_irq_restore(flags
);
498 * The functions below are called from rtc driver.
499 * Return 0 if HPET is not being used.
500 * Otherwise do the necessary changes and return 1.
502 int hpet_mask_rtc_irq_bit(unsigned long bit_mask
)
504 if (!is_hpet_enabled())
507 hpet_rtc_flags
&= ~bit_mask
;
511 int hpet_set_rtc_irq_bit(unsigned long bit_mask
)
513 unsigned long oldbits
= hpet_rtc_flags
;
515 if (!is_hpet_enabled())
518 hpet_rtc_flags
|= bit_mask
;
521 hpet_rtc_timer_init();
526 int hpet_set_alarm_time(unsigned char hrs
, unsigned char min
,
529 if (!is_hpet_enabled())
532 hpet_alarm_time
.tm_hour
= hrs
;
533 hpet_alarm_time
.tm_min
= min
;
534 hpet_alarm_time
.tm_sec
= sec
;
539 int hpet_set_periodic_freq(unsigned long freq
)
543 if (!is_hpet_enabled())
546 if (freq
<= DEFAULT_RTC_INT_FREQ
)
547 hpet_pie_limit
= DEFAULT_RTC_INT_FREQ
/ freq
;
549 clc
= (uint64_t) hpet_clockevent
.mult
* NSEC_PER_SEC
;
551 clc
>>= hpet_clockevent
.shift
;
552 hpet_pie_delta
= (unsigned long) clc
;
557 int hpet_rtc_dropped_irq(void)
559 return is_hpet_enabled();
562 static void hpet_rtc_timer_reinit(void)
564 unsigned long cfg
, delta
;
567 if (unlikely(!hpet_rtc_flags
)) {
568 cfg
= hpet_readl(HPET_T1_CFG
);
569 cfg
&= ~HPET_TN_ENABLE
;
570 hpet_writel(cfg
, HPET_T1_CFG
);
574 if (!(hpet_rtc_flags
& RTC_PIE
) || hpet_pie_limit
)
575 delta
= hpet_default_delta
;
577 delta
= hpet_pie_delta
;
580 * Increment the comparator value until we are ahead of the
584 hpet_t1_cmp
+= delta
;
585 hpet_writel(hpet_t1_cmp
, HPET_T1_CMP
);
587 } while ((long)(hpet_readl(HPET_COUNTER
) - hpet_t1_cmp
) > 0);
590 if (hpet_rtc_flags
& RTC_PIE
)
591 hpet_pie_count
+= lost_ints
;
592 if (printk_ratelimit())
593 printk(KERN_WARNING
"rtc: lost %d interrupts\n",
598 irqreturn_t
hpet_rtc_interrupt(int irq
, void *dev_id
)
600 struct rtc_time curr_time
;
601 unsigned long rtc_int_flag
= 0;
603 hpet_rtc_timer_reinit();
605 if (hpet_rtc_flags
& (RTC_UIE
| RTC_AIE
))
606 rtc_get_rtc_time(&curr_time
);
608 if (hpet_rtc_flags
& RTC_UIE
&&
609 curr_time
.tm_sec
!= hpet_prev_update_sec
) {
610 rtc_int_flag
= RTC_UF
;
611 hpet_prev_update_sec
= curr_time
.tm_sec
;
614 if (hpet_rtc_flags
& RTC_PIE
&&
615 ++hpet_pie_count
>= hpet_pie_limit
) {
616 rtc_int_flag
|= RTC_PF
;
620 if (hpet_rtc_flags
& RTC_PIE
&&
621 (curr_time
.tm_sec
== hpet_alarm_time
.tm_sec
) &&
622 (curr_time
.tm_min
== hpet_alarm_time
.tm_min
) &&
623 (curr_time
.tm_hour
== hpet_alarm_time
.tm_hour
))
624 rtc_int_flag
|= RTC_AF
;
627 rtc_int_flag
|= (RTC_IRQF
| (RTC_NUM_INTS
<< 8));
628 rtc_interrupt(rtc_int_flag
, dev_id
);