1 #include <linux/clocksource.h>
2 #include <linux/clockchips.h>
3 #include <linux/errno.h>
4 #include <linux/hpet.h>
5 #include <linux/init.h>
6 #include <linux/sysdev.h>
12 extern struct clock_event_device
*global_clock_event
;
14 #define HPET_MASK CLOCKSOURCE_MASK(32)
17 /* FSEC = 10^-15 NSEC = 10^-9 */
18 #define FSEC_PER_NSEC 1000000
21 * HPET address is set in acpi/boot.c, when an ACPI entry exists
23 unsigned long hpet_address
;
24 static void __iomem
* hpet_virt_address
;
26 static inline unsigned long hpet_readl(unsigned long a
)
28 return readl(hpet_virt_address
+ a
);
31 static inline void hpet_writel(unsigned long d
, unsigned long a
)
33 writel(d
, hpet_virt_address
+ a
);
37 * HPET command line enable / disable
39 static int boot_hpet_disable
;
41 static int __init
hpet_setup(char* str
)
44 if (!strncmp("disable", str
, 7))
45 boot_hpet_disable
= 1;
49 __setup("hpet=", hpet_setup
);
51 static inline int is_hpet_capable(void)
53 return (!boot_hpet_disable
&& hpet_address
);
57 * HPET timer interrupt enable / disable
59 static int hpet_legacy_int_enabled
;
62 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
64 int is_hpet_enabled(void)
66 return is_hpet_capable() && hpet_legacy_int_enabled
;
70 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
71 * timer 0 and timer 1 in case of RTC emulation.
74 static void hpet_reserve_platform_timers(unsigned long id
)
76 struct hpet __iomem
*hpet
= hpet_virt_address
;
77 struct hpet_timer __iomem
*timer
= &hpet
->hpet_timers
[2];
78 unsigned int nrtimers
, i
;
81 nrtimers
= ((id
& HPET_ID_NUMBER
) >> HPET_ID_NUMBER_SHIFT
) + 1;
83 memset(&hd
, 0, sizeof (hd
));
84 hd
.hd_phys_address
= hpet_address
;
85 hd
.hd_address
= hpet_virt_address
;
86 hd
.hd_nirqs
= nrtimers
;
87 hd
.hd_flags
= HPET_DATA_PLATFORM
;
88 hpet_reserve_timer(&hd
, 0);
90 #ifdef CONFIG_HPET_EMULATE_RTC
91 hpet_reserve_timer(&hd
, 1);
94 hd
.hd_irq
[0] = HPET_LEGACY_8254
;
95 hd
.hd_irq
[1] = HPET_LEGACY_RTC
;
97 for (i
= 2; i
< nrtimers
; timer
++, i
++)
98 hd
.hd_irq
[i
] = (timer
->hpet_config
& Tn_INT_ROUTE_CNF_MASK
) >>
99 Tn_INT_ROUTE_CNF_SHIFT
;
105 static void hpet_reserve_platform_timers(unsigned long id
) { }
111 static unsigned long hpet_period
;
113 static void hpet_set_mode(enum clock_event_mode mode
,
114 struct clock_event_device
*evt
);
115 static int hpet_next_event(unsigned long delta
,
116 struct clock_event_device
*evt
);
119 * The hpet clock event device
121 static struct clock_event_device hpet_clockevent
= {
123 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
124 .set_mode
= hpet_set_mode
,
125 .set_next_event
= hpet_next_event
,
130 static void hpet_start_counter(void)
132 unsigned long cfg
= hpet_readl(HPET_CFG
);
134 cfg
&= ~HPET_CFG_ENABLE
;
135 hpet_writel(cfg
, HPET_CFG
);
136 hpet_writel(0, HPET_COUNTER
);
137 hpet_writel(0, HPET_COUNTER
+ 4);
138 cfg
|= HPET_CFG_ENABLE
;
139 hpet_writel(cfg
, HPET_CFG
);
142 static void hpet_enable_int(void)
144 unsigned long cfg
= hpet_readl(HPET_CFG
);
146 cfg
|= HPET_CFG_LEGACY
;
147 hpet_writel(cfg
, HPET_CFG
);
148 hpet_legacy_int_enabled
= 1;
151 static void hpet_set_mode(enum clock_event_mode mode
,
152 struct clock_event_device
*evt
)
154 unsigned long cfg
, cmp
, now
;
158 case CLOCK_EVT_MODE_PERIODIC
:
159 delta
= ((uint64_t)(NSEC_PER_SEC
/HZ
)) * hpet_clockevent
.mult
;
160 delta
>>= hpet_clockevent
.shift
;
161 now
= hpet_readl(HPET_COUNTER
);
162 cmp
= now
+ (unsigned long) delta
;
163 cfg
= hpet_readl(HPET_T0_CFG
);
164 cfg
|= HPET_TN_ENABLE
| HPET_TN_PERIODIC
|
165 HPET_TN_SETVAL
| HPET_TN_32BIT
;
166 hpet_writel(cfg
, HPET_T0_CFG
);
168 * The first write after writing TN_SETVAL to the
169 * config register sets the counter value, the second
170 * write sets the period.
172 hpet_writel(cmp
, HPET_T0_CMP
);
174 hpet_writel((unsigned long) delta
, HPET_T0_CMP
);
177 case CLOCK_EVT_MODE_ONESHOT
:
178 cfg
= hpet_readl(HPET_T0_CFG
);
179 cfg
&= ~HPET_TN_PERIODIC
;
180 cfg
|= HPET_TN_ENABLE
| HPET_TN_32BIT
;
181 hpet_writel(cfg
, HPET_T0_CFG
);
184 case CLOCK_EVT_MODE_UNUSED
:
185 case CLOCK_EVT_MODE_SHUTDOWN
:
186 cfg
= hpet_readl(HPET_T0_CFG
);
187 cfg
&= ~HPET_TN_ENABLE
;
188 hpet_writel(cfg
, HPET_T0_CFG
);
193 static int hpet_next_event(unsigned long delta
,
194 struct clock_event_device
*evt
)
198 cnt
= hpet_readl(HPET_COUNTER
);
200 hpet_writel(cnt
, HPET_T0_CMP
);
202 return ((long)(hpet_readl(HPET_COUNTER
) - cnt
) > 0) ? -ETIME
: 0;
206 * Clock source related code
208 static cycle_t
read_hpet(void)
210 return (cycle_t
)hpet_readl(HPET_COUNTER
);
213 static struct clocksource clocksource_hpet
= {
219 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
223 * Try to setup the HPET timer
225 int __init
hpet_enable(void)
231 if (!is_hpet_capable())
234 hpet_virt_address
= ioremap_nocache(hpet_address
, HPET_MMAP_SIZE
);
237 * Read the period and check for a sane value:
239 hpet_period
= hpet_readl(HPET_PERIOD
);
240 if (hpet_period
< HPET_MIN_PERIOD
|| hpet_period
> HPET_MAX_PERIOD
)
244 * The period is a femto seconds value. We need to calculate the
245 * scaled math multiplication factor for nanosecond to hpet tick
248 hpet_freq
= 1000000000000000ULL;
249 do_div(hpet_freq
, hpet_period
);
250 hpet_clockevent
.mult
= div_sc((unsigned long) hpet_freq
,
252 /* Calculate the min / max delta */
253 hpet_clockevent
.max_delta_ns
= clockevent_delta2ns(0x7FFFFFFF,
255 hpet_clockevent
.min_delta_ns
= clockevent_delta2ns(0x30,
259 * Read the HPET ID register to retrieve the IRQ routing
260 * information and the number of channels
262 id
= hpet_readl(HPET_ID
);
264 #ifdef CONFIG_HPET_EMULATE_RTC
266 * The legacy routing mode needs at least two channels, tick timer
267 * and the rtc emulation channel.
269 if (!(id
& HPET_ID_NUMBER
))
273 /* Start the counter */
274 hpet_start_counter();
276 /* Initialize and register HPET clocksource
278 * hpet period is in femto seconds per cycle
279 * so we need to convert this to ns/cyc units
280 * aproximated by mult/2^shift
282 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
283 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
284 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
285 * (fsec/cyc << shift)/1000000 = mult
286 * (hpet_period << shift)/FSEC_PER_NSEC = mult
288 tmp
= (u64
)hpet_period
<< HPET_SHIFT
;
289 do_div(tmp
, FSEC_PER_NSEC
);
290 clocksource_hpet
.mult
= (u32
)tmp
;
292 clocksource_register(&clocksource_hpet
);
295 if (id
& HPET_ID_LEGSUP
) {
297 hpet_reserve_platform_timers(id
);
299 * Start hpet with the boot cpu mask and make it
300 * global after the IO_APIC has been initialized.
302 hpet_clockevent
.cpumask
=cpumask_of_cpu(0);
303 clockevents_register_device(&hpet_clockevent
);
304 global_clock_event
= &hpet_clockevent
;
310 iounmap(hpet_virt_address
);
311 hpet_virt_address
= NULL
;
312 boot_hpet_disable
= 1;
317 #ifdef CONFIG_HPET_EMULATE_RTC
319 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
320 * is enabled, we support RTC interrupt functionality in software.
321 * RTC has 3 kinds of interrupts:
322 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
324 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
325 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
326 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
327 * (1) and (2) above are implemented using polling at a frequency of
328 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
329 * overhead. (DEFAULT_RTC_INT_FREQ)
330 * For (3), we use interrupts at 64Hz or user specified periodic
331 * frequency, whichever is higher.
333 #include <linux/mc146818rtc.h>
334 #include <linux/rtc.h>
336 #define DEFAULT_RTC_INT_FREQ 64
337 #define DEFAULT_RTC_SHIFT 6
338 #define RTC_NUM_INTS 1
340 static unsigned long hpet_rtc_flags
;
341 static unsigned long hpet_prev_update_sec
;
342 static struct rtc_time hpet_alarm_time
;
343 static unsigned long hpet_pie_count
;
344 static unsigned long hpet_t1_cmp
;
345 static unsigned long hpet_default_delta
;
346 static unsigned long hpet_pie_delta
;
347 static unsigned long hpet_pie_limit
;
350 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
351 * is not supported by all HPET implementations for timer 1.
353 * hpet_rtc_timer_init() is called when the rtc is initialized.
355 int hpet_rtc_timer_init(void)
357 unsigned long cfg
, cnt
, delta
, flags
;
359 if (!is_hpet_enabled())
362 if (!hpet_default_delta
) {
365 clc
= (uint64_t) hpet_clockevent
.mult
* NSEC_PER_SEC
;
366 clc
>>= hpet_clockevent
.shift
+ DEFAULT_RTC_SHIFT
;
367 hpet_default_delta
= (unsigned long) clc
;
370 if (!(hpet_rtc_flags
& RTC_PIE
) || hpet_pie_limit
)
371 delta
= hpet_default_delta
;
373 delta
= hpet_pie_delta
;
375 local_irq_save(flags
);
377 cnt
= delta
+ hpet_readl(HPET_COUNTER
);
378 hpet_writel(cnt
, HPET_T1_CMP
);
381 cfg
= hpet_readl(HPET_T1_CFG
);
382 cfg
&= ~HPET_TN_PERIODIC
;
383 cfg
|= HPET_TN_ENABLE
| HPET_TN_32BIT
;
384 hpet_writel(cfg
, HPET_T1_CFG
);
386 local_irq_restore(flags
);
392 * The functions below are called from rtc driver.
393 * Return 0 if HPET is not being used.
394 * Otherwise do the necessary changes and return 1.
396 int hpet_mask_rtc_irq_bit(unsigned long bit_mask
)
398 if (!is_hpet_enabled())
401 hpet_rtc_flags
&= ~bit_mask
;
405 int hpet_set_rtc_irq_bit(unsigned long bit_mask
)
407 unsigned long oldbits
= hpet_rtc_flags
;
409 if (!is_hpet_enabled())
412 hpet_rtc_flags
|= bit_mask
;
415 hpet_rtc_timer_init();
420 int hpet_set_alarm_time(unsigned char hrs
, unsigned char min
,
423 if (!is_hpet_enabled())
426 hpet_alarm_time
.tm_hour
= hrs
;
427 hpet_alarm_time
.tm_min
= min
;
428 hpet_alarm_time
.tm_sec
= sec
;
433 int hpet_set_periodic_freq(unsigned long freq
)
437 if (!is_hpet_enabled())
440 if (freq
<= DEFAULT_RTC_INT_FREQ
)
441 hpet_pie_limit
= DEFAULT_RTC_INT_FREQ
/ freq
;
443 clc
= (uint64_t) hpet_clockevent
.mult
* NSEC_PER_SEC
;
445 clc
>>= hpet_clockevent
.shift
;
446 hpet_pie_delta
= (unsigned long) clc
;
451 int hpet_rtc_dropped_irq(void)
453 return is_hpet_enabled();
456 static void hpet_rtc_timer_reinit(void)
458 unsigned long cfg
, delta
;
461 if (unlikely(!hpet_rtc_flags
)) {
462 cfg
= hpet_readl(HPET_T1_CFG
);
463 cfg
&= ~HPET_TN_ENABLE
;
464 hpet_writel(cfg
, HPET_T1_CFG
);
468 if (!(hpet_rtc_flags
& RTC_PIE
) || hpet_pie_limit
)
469 delta
= hpet_default_delta
;
471 delta
= hpet_pie_delta
;
474 * Increment the comparator value until we are ahead of the
478 hpet_t1_cmp
+= delta
;
479 hpet_writel(hpet_t1_cmp
, HPET_T1_CMP
);
481 } while ((long)(hpet_readl(HPET_COUNTER
) - hpet_t1_cmp
) > 0);
484 if (hpet_rtc_flags
& RTC_PIE
)
485 hpet_pie_count
+= lost_ints
;
486 if (printk_ratelimit())
487 printk(KERN_WARNING
"rtc: lost %d interrupts\n",
492 irqreturn_t
hpet_rtc_interrupt(int irq
, void *dev_id
)
494 struct rtc_time curr_time
;
495 unsigned long rtc_int_flag
= 0;
497 hpet_rtc_timer_reinit();
499 if (hpet_rtc_flags
& (RTC_UIE
| RTC_AIE
))
500 rtc_get_rtc_time(&curr_time
);
502 if (hpet_rtc_flags
& RTC_UIE
&&
503 curr_time
.tm_sec
!= hpet_prev_update_sec
) {
504 rtc_int_flag
= RTC_UF
;
505 hpet_prev_update_sec
= curr_time
.tm_sec
;
508 if (hpet_rtc_flags
& RTC_PIE
&&
509 ++hpet_pie_count
>= hpet_pie_limit
) {
510 rtc_int_flag
|= RTC_PF
;
514 if (hpet_rtc_flags
& RTC_PIE
&&
515 (curr_time
.tm_sec
== hpet_alarm_time
.tm_sec
) &&
516 (curr_time
.tm_min
== hpet_alarm_time
.tm_min
) &&
517 (curr_time
.tm_hour
== hpet_alarm_time
.tm_hour
))
518 rtc_int_flag
|= RTC_AF
;
521 rtc_int_flag
|= (RTC_IRQF
| (RTC_NUM_INTS
<< 8));
522 rtc_interrupt(rtc_int_flag
, dev_id
);
530 * Suspend/resume part
535 static int hpet_suspend(struct sys_device
*sys_device
, pm_message_t state
)
537 unsigned long cfg
= hpet_readl(HPET_CFG
);
539 cfg
&= ~(HPET_CFG_ENABLE
|HPET_CFG_LEGACY
);
540 hpet_writel(cfg
, HPET_CFG
);
545 static int hpet_resume(struct sys_device
*sys_device
)
549 hpet_start_counter();
551 id
= hpet_readl(HPET_ID
);
553 if (id
& HPET_ID_LEGSUP
)
559 static struct sysdev_class hpet_class
= {
560 set_kset_name("hpet"),
561 .suspend
= hpet_suspend
,
562 .resume
= hpet_resume
,
565 static struct sys_device hpet_device
= {
571 static __init
int hpet_register_sysfs(void)
575 if (!is_hpet_capable())
578 err
= sysdev_class_register(&hpet_class
);
581 err
= sysdev_register(&hpet_device
);
583 sysdev_class_unregister(&hpet_class
);
589 device_initcall(hpet_register_sysfs
);