1 #include <linux/kernel.h>
2 #include <linux/sched.h>
3 #include <linux/init.h>
4 #include <linux/mc146818rtc.h>
5 #include <linux/time.h>
6 #include <linux/clocksource.h>
7 #include <linux/ioport.h>
8 #include <linux/acpi.h>
9 #include <linux/hpet.h>
10 #include <asm/pgtable.h>
11 #include <asm/vsyscall.h>
12 #include <asm/timex.h>
15 int nohpet __initdata;
17 unsigned long hpet_address;
18 unsigned long hpet_period; /* fsecs / HPET clock */
19 unsigned long hpet_tick; /* HPET clocks / interrupt */
21 int hpet_use_timer; /* Use counter of hpet for time keeping,
26 static __init int late_hpet_init(void)
34 memset(&hd, 0, sizeof(hd));
36 ntimer = hpet_readl(HPET_ID);
37 ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
41 * Register with driver.
42 * Timer0 and Timer1 is used by platform.
44 hd.hd_phys_address = hpet_address;
45 hd.hd_address = (void __iomem *)fix_to_virt(FIX_HPET_BASE);
47 hd.hd_flags = HPET_DATA_PLATFORM;
48 hpet_reserve_timer(&hd, 0);
49 #ifdef CONFIG_HPET_EMULATE_RTC
50 hpet_reserve_timer(&hd, 1);
52 hd.hd_irq[0] = HPET_LEGACY_8254;
53 hd.hd_irq[1] = HPET_LEGACY_RTC;
56 struct hpet_timer *timer;
59 hpet = (struct hpet *) fix_to_virt(FIX_HPET_BASE);
60 timer = &hpet->hpet_timers[2];
61 for (i = 2; i < ntimer; timer++, i++)
62 hd.hd_irq[i] = (timer->hpet_config &
63 Tn_INT_ROUTE_CNF_MASK) >>
64 Tn_INT_ROUTE_CNF_SHIFT;
71 fs_initcall(late_hpet_init);
74 int hpet_timer_stop_set_go(unsigned long tick)
79 * Stop the timers and reset the main counter.
82 cfg = hpet_readl(HPET_CFG);
83 cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
84 hpet_writel(cfg, HPET_CFG);
85 hpet_writel(0, HPET_COUNTER);
86 hpet_writel(0, HPET_COUNTER + 4);
89 * Set up timer 0, as periodic with first interrupt to happen at hpet_tick,
90 * and period also hpet_tick.
93 hpet_writel(HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |
94 HPET_TN_32BIT, HPET_T0_CFG);
95 hpet_writel(hpet_tick, HPET_T0_CMP); /* next interrupt */
96 hpet_writel(hpet_tick, HPET_T0_CMP); /* period */
97 cfg |= HPET_CFG_LEGACY;
103 cfg |= HPET_CFG_ENABLE;
104 hpet_writel(cfg, HPET_CFG);
109 int hpet_arch_init(void)
115 set_fixmap_nocache(FIX_HPET_BASE, hpet_address);
116 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
119 * Read the period, compute tick and quotient.
122 id = hpet_readl(HPET_ID);
124 if (!(id & HPET_ID_VENDOR) || !(id & HPET_ID_NUMBER))
127 hpet_period = hpet_readl(HPET_PERIOD);
128 if (hpet_period < 100000 || hpet_period > 100000000)
131 hpet_tick = (FSEC_PER_TICK + hpet_period / 2) / hpet_period;
133 hpet_use_timer = (id & HPET_ID_LEGSUP);
135 return hpet_timer_stop_set_go(hpet_tick);
138 int hpet_reenable(void)
140 return hpet_timer_stop_set_go(hpet_tick);
144 * calibrate_tsc() calibrates the processor TSC in a very simple way, comparing
145 * it to the HPET timer of known frequency.
148 #define TICK_COUNT 100000000
149 #define TICK_MIN 5000
152 * Some platforms take periodic SMI interrupts with 5ms duration. Make sure none
153 * occurs between the reads of the hpet & TSC.
155 static void __init read_hpet_tsc(int *hpet, int *tsc)
157 int tsc1, tsc2, hpet1;
160 tsc1 = get_cycles_sync();
161 hpet1 = hpet_readl(HPET_COUNTER);
162 tsc2 = get_cycles_sync();
163 } while (tsc2 - tsc1 > TICK_MIN);
168 unsigned int __init hpet_calibrate_tsc(void)
170 int tsc_start, hpet_start;
171 int tsc_now, hpet_now;
174 local_irq_save(flags);
176 read_hpet_tsc(&hpet_start, &tsc_start);
180 read_hpet_tsc(&hpet_now, &tsc_now);
181 local_irq_restore(flags);
182 } while ((tsc_now - tsc_start) < TICK_COUNT &&
183 (hpet_now - hpet_start) < TICK_COUNT);
185 return (tsc_now - tsc_start) * 1000000000L
186 / ((hpet_now - hpet_start) * hpet_period / 1000);
189 #ifdef CONFIG_HPET_EMULATE_RTC
190 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
191 * is enabled, we support RTC interrupt functionality in software.
192 * RTC has 3 kinds of interrupts:
193 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
195 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
196 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
197 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
198 * (1) and (2) above are implemented using polling at a frequency of
199 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
200 * overhead. (DEFAULT_RTC_INT_FREQ)
201 * For (3), we use interrupts at 64Hz or user specified periodic
202 * frequency, whichever is higher.
204 #include <linux/rtc.h>
206 #define DEFAULT_RTC_INT_FREQ 64
207 #define RTC_NUM_INTS 1
209 static unsigned long UIE_on;
210 static unsigned long prev_update_sec;
212 static unsigned long AIE_on;
213 static struct rtc_time alarm_time;
215 static unsigned long PIE_on;
216 static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
217 static unsigned long PIE_count;
219 static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
220 static unsigned int hpet_t1_cmp; /* cached comparator register */
222 int is_hpet_enabled(void)
224 return hpet_address != 0;
228 * Timer 1 for RTC, we do not use periodic interrupt feature,
229 * even if HPET supports periodic interrupts on Timer 1.
230 * The reason being, to set up a periodic interrupt in HPET, we need to
231 * stop the main counter. And if we do that everytime someone diables/enables
232 * RTC, we will have adverse effect on main kernel timer running on Timer 0.
233 * So, for the time being, simulate the periodic interrupt in software.
235 * hpet_rtc_timer_init() is called for the first time and during subsequent
236 * interuppts reinit happens through hpet_rtc_timer_reinit().
238 int hpet_rtc_timer_init(void)
240 unsigned int cfg, cnt;
243 if (!is_hpet_enabled())
246 * Set the counter 1 and enable the interrupts.
248 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
249 hpet_rtc_int_freq = PIE_freq;
251 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
253 local_irq_save(flags);
255 cnt = hpet_readl(HPET_COUNTER);
256 cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
257 hpet_writel(cnt, HPET_T1_CMP);
260 cfg = hpet_readl(HPET_T1_CFG);
261 cfg &= ~HPET_TN_PERIODIC;
262 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
263 hpet_writel(cfg, HPET_T1_CFG);
265 local_irq_restore(flags);
270 static void hpet_rtc_timer_reinit(void)
272 unsigned int cfg, cnt, ticks_per_int, lost_ints;
274 if (unlikely(!(PIE_on | AIE_on | UIE_on))) {
275 cfg = hpet_readl(HPET_T1_CFG);
276 cfg &= ~HPET_TN_ENABLE;
277 hpet_writel(cfg, HPET_T1_CFG);
281 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
282 hpet_rtc_int_freq = PIE_freq;
284 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
286 /* It is more accurate to use the comparator value than current count.*/
287 ticks_per_int = hpet_tick * HZ / hpet_rtc_int_freq;
288 hpet_t1_cmp += ticks_per_int;
289 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
292 * If the interrupt handler was delayed too long, the write above tries
293 * to schedule the next interrupt in the past and the hardware would
294 * not interrupt until the counter had wrapped around.
295 * So we have to check that the comparator wasn't set to a past time.
297 cnt = hpet_readl(HPET_COUNTER);
298 if (unlikely((int)(cnt - hpet_t1_cmp) > 0)) {
299 lost_ints = (cnt - hpet_t1_cmp) / ticks_per_int + 1;
300 /* Make sure that, even with the time needed to execute
301 * this code, the next scheduled interrupt has been moved
302 * back to the future: */
305 hpet_t1_cmp += lost_ints * ticks_per_int;
306 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
309 PIE_count += lost_ints;
311 if (printk_ratelimit())
312 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
318 * The functions below are called from rtc driver.
319 * Return 0 if HPET is not being used.
320 * Otherwise do the necessary changes and return 1.
322 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
324 if (!is_hpet_enabled())
327 if (bit_mask & RTC_UIE)
329 if (bit_mask & RTC_PIE)
331 if (bit_mask & RTC_AIE)
337 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
339 int timer_init_reqd = 0;
341 if (!is_hpet_enabled())
344 if (!(PIE_on | AIE_on | UIE_on))
347 if (bit_mask & RTC_UIE) {
350 if (bit_mask & RTC_PIE) {
354 if (bit_mask & RTC_AIE) {
359 hpet_rtc_timer_init();
364 int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
366 if (!is_hpet_enabled())
369 alarm_time.tm_hour = hrs;
370 alarm_time.tm_min = min;
371 alarm_time.tm_sec = sec;
376 int hpet_set_periodic_freq(unsigned long freq)
378 if (!is_hpet_enabled())
387 int hpet_rtc_dropped_irq(void)
389 if (!is_hpet_enabled())
395 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
397 struct rtc_time curr_time;
398 unsigned long rtc_int_flag = 0;
399 int call_rtc_interrupt = 0;
401 hpet_rtc_timer_reinit();
403 if (UIE_on | AIE_on) {
404 rtc_get_rtc_time(&curr_time);
407 if (curr_time.tm_sec != prev_update_sec) {
408 /* Set update int info, call real rtc int routine */
409 call_rtc_interrupt = 1;
410 rtc_int_flag = RTC_UF;
411 prev_update_sec = curr_time.tm_sec;
416 if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
417 /* Set periodic int info, call real rtc int routine */
418 call_rtc_interrupt = 1;
419 rtc_int_flag |= RTC_PF;
424 if ((curr_time.tm_sec == alarm_time.tm_sec) &&
425 (curr_time.tm_min == alarm_time.tm_min) &&
426 (curr_time.tm_hour == alarm_time.tm_hour)) {
427 /* Set alarm int info, call real rtc int routine */
428 call_rtc_interrupt = 1;
429 rtc_int_flag |= RTC_AF;
432 if (call_rtc_interrupt) {
433 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
434 rtc_interrupt(rtc_int_flag, dev_id);
440 static int __init nohpet_setup(char *s)
446 __setup("nohpet", nohpet_setup);
448 #define HPET_MASK 0xFFFFFFFF
449 #define HPET_SHIFT 22
451 /* FSEC = 10^-15 NSEC = 10^-9 */
452 #define FSEC_PER_NSEC 1000000
454 static void *hpet_ptr;
456 static cycle_t read_hpet(void)
458 return (cycle_t)readl(hpet_ptr);
461 static cycle_t __vsyscall_fn vread_hpet(void)
463 return readl((void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
466 struct clocksource clocksource_hpet = {
470 .mask = (cycle_t)HPET_MASK,
471 .mult = 0, /* set below */
473 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
477 static int __init init_hpet_clocksource(void)
479 unsigned long hpet_period;
480 void __iomem *hpet_base;
486 /* calculate the hpet address: */
487 hpet_base = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
488 hpet_ptr = hpet_base + HPET_COUNTER;
490 /* calculate the frequency: */
491 hpet_period = readl(hpet_base + HPET_PERIOD);
494 * hpet period is in femto seconds per cycle
495 * so we need to convert this to ns/cyc units
496 * aproximated by mult/2^shift
498 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
499 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
500 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
501 * (fsec/cyc << shift)/1000000 = mult
502 * (hpet_period << shift)/FSEC_PER_NSEC = mult
504 tmp = (u64)hpet_period << HPET_SHIFT;
505 do_div(tmp, FSEC_PER_NSEC);
506 clocksource_hpet.mult = (u32)tmp;
508 return clocksource_register(&clocksource_hpet);
511 module_init(init_hpet_clocksource);