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>
8 #include <linux/delay.h>
13 extern struct clock_event_device *global_clock_event;
15 #define HPET_MASK CLOCKSOURCE_MASK(32)
18 /* FSEC = 10^-15 NSEC = 10^-9 */
19 #define FSEC_PER_NSEC 1000000
22 * HPET address is set in acpi/boot.c, when an ACPI entry exists
24 unsigned long hpet_address;
25 static void __iomem * hpet_virt_address;
27 static inline unsigned long hpet_readl(unsigned long a)
29 return readl(hpet_virt_address + a);
32 static inline void hpet_writel(unsigned long d, unsigned long a)
34 writel(d, hpet_virt_address + a);
38 * HPET command line enable / disable
40 static int boot_hpet_disable;
42 static int __init hpet_setup(char* str)
45 if (!strncmp("disable", str, 7))
46 boot_hpet_disable = 1;
50 __setup("hpet=", hpet_setup);
52 static inline int is_hpet_capable(void)
54 return (!boot_hpet_disable && hpet_address);
58 * HPET timer interrupt enable / disable
60 static int hpet_legacy_int_enabled;
63 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
65 int is_hpet_enabled(void)
67 return is_hpet_capable() && hpet_legacy_int_enabled;
71 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
72 * timer 0 and timer 1 in case of RTC emulation.
75 static void hpet_reserve_platform_timers(unsigned long id)
77 struct hpet __iomem *hpet = hpet_virt_address;
78 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
79 unsigned int nrtimers, i;
82 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
84 memset(&hd, 0, sizeof (hd));
85 hd.hd_phys_address = hpet_address;
86 hd.hd_address = hpet_virt_address;
87 hd.hd_nirqs = nrtimers;
88 hd.hd_flags = HPET_DATA_PLATFORM;
89 hpet_reserve_timer(&hd, 0);
91 #ifdef CONFIG_HPET_EMULATE_RTC
92 hpet_reserve_timer(&hd, 1);
95 hd.hd_irq[0] = HPET_LEGACY_8254;
96 hd.hd_irq[1] = HPET_LEGACY_RTC;
98 for (i = 2; i < nrtimers; timer++, i++)
99 hd.hd_irq[i] = (timer->hpet_config & Tn_INT_ROUTE_CNF_MASK) >>
100 Tn_INT_ROUTE_CNF_SHIFT;
106 static void hpet_reserve_platform_timers(unsigned long id) { }
112 static unsigned long hpet_period;
114 static void hpet_set_mode(enum clock_event_mode mode,
115 struct clock_event_device *evt);
116 static int hpet_next_event(unsigned long delta,
117 struct clock_event_device *evt);
120 * The hpet clock event device
122 static struct clock_event_device hpet_clockevent = {
124 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
125 .set_mode = hpet_set_mode,
126 .set_next_event = hpet_next_event,
131 static void hpet_start_counter(void)
133 unsigned long cfg = hpet_readl(HPET_CFG);
135 cfg &= ~HPET_CFG_ENABLE;
136 hpet_writel(cfg, HPET_CFG);
137 hpet_writel(0, HPET_COUNTER);
138 hpet_writel(0, HPET_COUNTER + 4);
139 cfg |= HPET_CFG_ENABLE;
140 hpet_writel(cfg, HPET_CFG);
143 static void hpet_enable_int(void)
145 unsigned long cfg = hpet_readl(HPET_CFG);
147 cfg |= HPET_CFG_LEGACY;
148 hpet_writel(cfg, HPET_CFG);
149 hpet_legacy_int_enabled = 1;
152 static void hpet_set_mode(enum clock_event_mode mode,
153 struct clock_event_device *evt)
155 unsigned long cfg, cmp, now;
159 case CLOCK_EVT_MODE_PERIODIC:
160 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
161 delta >>= hpet_clockevent.shift;
162 now = hpet_readl(HPET_COUNTER);
163 cmp = now + (unsigned long) delta;
164 cfg = hpet_readl(HPET_T0_CFG);
165 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
166 HPET_TN_SETVAL | HPET_TN_32BIT;
167 hpet_writel(cfg, HPET_T0_CFG);
169 * The first write after writing TN_SETVAL to the
170 * config register sets the counter value, the second
171 * write sets the period.
173 hpet_writel(cmp, HPET_T0_CMP);
175 hpet_writel((unsigned long) delta, HPET_T0_CMP);
178 case CLOCK_EVT_MODE_ONESHOT:
179 cfg = hpet_readl(HPET_T0_CFG);
180 cfg &= ~HPET_TN_PERIODIC;
181 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
182 hpet_writel(cfg, HPET_T0_CFG);
185 case CLOCK_EVT_MODE_UNUSED:
186 case CLOCK_EVT_MODE_SHUTDOWN:
187 cfg = hpet_readl(HPET_T0_CFG);
188 cfg &= ~HPET_TN_ENABLE;
189 hpet_writel(cfg, HPET_T0_CFG);
192 case CLOCK_EVT_MODE_RESUME:
198 static int hpet_next_event(unsigned long delta,
199 struct clock_event_device *evt)
203 cnt = hpet_readl(HPET_COUNTER);
205 hpet_writel(cnt, HPET_T0_CMP);
207 return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0) ? -ETIME : 0;
211 * Clock source related code
213 static cycle_t read_hpet(void)
215 return (cycle_t)hpet_readl(HPET_COUNTER);
218 static struct clocksource clocksource_hpet = {
224 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
225 .resume = hpet_start_counter,
229 * Try to setup the HPET timer
231 int __init hpet_enable(void)
237 if (!is_hpet_capable())
240 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
243 * Read the period and check for a sane value:
245 hpet_period = hpet_readl(HPET_PERIOD);
246 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
250 * The period is a femto seconds value. We need to calculate the
251 * scaled math multiplication factor for nanosecond to hpet tick
254 hpet_freq = 1000000000000000ULL;
255 do_div(hpet_freq, hpet_period);
256 hpet_clockevent.mult = div_sc((unsigned long) hpet_freq,
258 /* Calculate the min / max delta */
259 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
261 hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30,
265 * Read the HPET ID register to retrieve the IRQ routing
266 * information and the number of channels
268 id = hpet_readl(HPET_ID);
270 #ifdef CONFIG_HPET_EMULATE_RTC
272 * The legacy routing mode needs at least two channels, tick timer
273 * and the rtc emulation channel.
275 if (!(id & HPET_ID_NUMBER))
279 /* Start the counter */
280 hpet_start_counter();
282 /* Initialize and register HPET clocksource
284 * hpet period is in femto seconds per cycle
285 * so we need to convert this to ns/cyc units
286 * aproximated by mult/2^shift
288 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
289 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
290 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
291 * (fsec/cyc << shift)/1000000 = mult
292 * (hpet_period << shift)/FSEC_PER_NSEC = mult
294 tmp = (u64)hpet_period << HPET_SHIFT;
295 do_div(tmp, FSEC_PER_NSEC);
296 clocksource_hpet.mult = (u32)tmp;
298 clocksource_register(&clocksource_hpet);
300 if (id & HPET_ID_LEGSUP) {
302 hpet_reserve_platform_timers(id);
304 * Start hpet with the boot cpu mask and make it
305 * global after the IO_APIC has been initialized.
307 hpet_clockevent.cpumask =cpumask_of_cpu(0);
308 clockevents_register_device(&hpet_clockevent);
309 global_clock_event = &hpet_clockevent;
315 iounmap(hpet_virt_address);
316 hpet_virt_address = NULL;
317 boot_hpet_disable = 1;
322 #ifdef CONFIG_HPET_EMULATE_RTC
324 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
325 * is enabled, we support RTC interrupt functionality in software.
326 * RTC has 3 kinds of interrupts:
327 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
329 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
330 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
331 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
332 * (1) and (2) above are implemented using polling at a frequency of
333 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
334 * overhead. (DEFAULT_RTC_INT_FREQ)
335 * For (3), we use interrupts at 64Hz or user specified periodic
336 * frequency, whichever is higher.
338 #include <linux/mc146818rtc.h>
339 #include <linux/rtc.h>
341 #define DEFAULT_RTC_INT_FREQ 64
342 #define DEFAULT_RTC_SHIFT 6
343 #define RTC_NUM_INTS 1
345 static unsigned long hpet_rtc_flags;
346 static unsigned long hpet_prev_update_sec;
347 static struct rtc_time hpet_alarm_time;
348 static unsigned long hpet_pie_count;
349 static unsigned long hpet_t1_cmp;
350 static unsigned long hpet_default_delta;
351 static unsigned long hpet_pie_delta;
352 static unsigned long hpet_pie_limit;
355 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
356 * is not supported by all HPET implementations for timer 1.
358 * hpet_rtc_timer_init() is called when the rtc is initialized.
360 int hpet_rtc_timer_init(void)
362 unsigned long cfg, cnt, delta, flags;
364 if (!is_hpet_enabled())
367 if (!hpet_default_delta) {
370 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
371 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
372 hpet_default_delta = (unsigned long) clc;
375 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
376 delta = hpet_default_delta;
378 delta = hpet_pie_delta;
380 local_irq_save(flags);
382 cnt = delta + hpet_readl(HPET_COUNTER);
383 hpet_writel(cnt, HPET_T1_CMP);
386 cfg = hpet_readl(HPET_T1_CFG);
387 cfg &= ~HPET_TN_PERIODIC;
388 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
389 hpet_writel(cfg, HPET_T1_CFG);
391 local_irq_restore(flags);
397 * The functions below are called from rtc driver.
398 * Return 0 if HPET is not being used.
399 * Otherwise do the necessary changes and return 1.
401 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
403 if (!is_hpet_enabled())
406 hpet_rtc_flags &= ~bit_mask;
410 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
412 unsigned long oldbits = hpet_rtc_flags;
414 if (!is_hpet_enabled())
417 hpet_rtc_flags |= bit_mask;
420 hpet_rtc_timer_init();
425 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
428 if (!is_hpet_enabled())
431 hpet_alarm_time.tm_hour = hrs;
432 hpet_alarm_time.tm_min = min;
433 hpet_alarm_time.tm_sec = sec;
438 int hpet_set_periodic_freq(unsigned long freq)
442 if (!is_hpet_enabled())
445 if (freq <= DEFAULT_RTC_INT_FREQ)
446 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
448 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
450 clc >>= hpet_clockevent.shift;
451 hpet_pie_delta = (unsigned long) clc;
456 int hpet_rtc_dropped_irq(void)
458 return is_hpet_enabled();
461 static void hpet_rtc_timer_reinit(void)
463 unsigned long cfg, delta;
466 if (unlikely(!hpet_rtc_flags)) {
467 cfg = hpet_readl(HPET_T1_CFG);
468 cfg &= ~HPET_TN_ENABLE;
469 hpet_writel(cfg, HPET_T1_CFG);
473 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
474 delta = hpet_default_delta;
476 delta = hpet_pie_delta;
479 * Increment the comparator value until we are ahead of the
483 hpet_t1_cmp += delta;
484 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
486 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
489 if (hpet_rtc_flags & RTC_PIE)
490 hpet_pie_count += lost_ints;
491 if (printk_ratelimit())
492 printk(KERN_WARNING "rtc: lost %d interrupts\n",
497 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
499 struct rtc_time curr_time;
500 unsigned long rtc_int_flag = 0;
502 hpet_rtc_timer_reinit();
504 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
505 rtc_get_rtc_time(&curr_time);
507 if (hpet_rtc_flags & RTC_UIE &&
508 curr_time.tm_sec != hpet_prev_update_sec) {
509 rtc_int_flag = RTC_UF;
510 hpet_prev_update_sec = curr_time.tm_sec;
513 if (hpet_rtc_flags & RTC_PIE &&
514 ++hpet_pie_count >= hpet_pie_limit) {
515 rtc_int_flag |= RTC_PF;
519 if (hpet_rtc_flags & RTC_PIE &&
520 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
521 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
522 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
523 rtc_int_flag |= RTC_AF;
526 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
527 rtc_interrupt(rtc_int_flag, dev_id);