2 * linux/arch/i386/kernel/time_hpet.c
3 * This code largely copied from arch/x86_64/kernel/time.c
4 * See that file for credits.
6 * 2003-06-30 Venkatesh Pallipadi - Additional changes for HPET support
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/param.h>
12 #include <linux/string.h>
13 #include <linux/init.h>
14 #include <linux/smp.h>
16 #include <asm/timer.h>
17 #include <asm/fixmap.h>
20 #include <linux/timex.h>
21 #include <linux/config.h>
24 #include <linux/hpet.h>
26 unsigned long hpet_period; /* fsecs / HPET clock */
27 unsigned long hpet_tick; /* hpet clks count per tick */
28 unsigned long hpet_address; /* hpet memory map physical address */
30 static int use_hpet; /* can be used for runtime check of hpet */
31 static int boot_hpet_disable; /* boottime override for HPET timer */
32 static unsigned long hpet_virt_address; /* hpet kernel virtual address */
34 #define FSEC_TO_USEC (1000000000UL)
36 int hpet_readl(unsigned long a)
38 return readl(hpet_virt_address + a);
41 void hpet_writel(unsigned long d, unsigned long a)
43 writel(d, hpet_virt_address + a);
46 #ifdef CONFIG_X86_LOCAL_APIC
48 * HPET counters dont wrap around on every tick. They just change the
49 * comparator value and continue. Next tick can be caught by checking
50 * for a change in the comparator value. Used in apic.c.
52 void __init wait_hpet_tick(void)
54 unsigned int start_cmp_val, end_cmp_val;
56 start_cmp_val = hpet_readl(HPET_T0_CMP);
58 end_cmp_val = hpet_readl(HPET_T0_CMP);
59 } while (start_cmp_val == end_cmp_val);
64 * Check whether HPET was found by ACPI boot parse. If yes setup HPET
65 * counter 0 for kernel base timer.
67 int __init hpet_enable(void)
70 unsigned long tick_fsec_low, tick_fsec_high; /* tick in femto sec */
71 unsigned long hpet_tick_rem;
73 if (boot_hpet_disable)
79 hpet_virt_address = (unsigned long) ioremap_nocache(hpet_address,
82 * Read the period, compute tick and quotient.
84 id = hpet_readl(HPET_ID);
87 * We are checking for value '1' or more in number field.
88 * So, we are OK with HPET_EMULATE_RTC part too, where we need
89 * to have atleast 2 timers.
91 if (!(id & HPET_ID_NUMBER) ||
92 !(id & HPET_ID_LEGSUP))
95 hpet_period = hpet_readl(HPET_PERIOD);
96 if ((hpet_period < HPET_MIN_PERIOD) || (hpet_period > HPET_MAX_PERIOD))
101 * First changing tick into fsec
102 * Then 64 bit div to find number of hpet clk per tick
104 ASM_MUL64_REG(tick_fsec_low, tick_fsec_high,
105 KERNEL_TICK_USEC, FSEC_TO_USEC);
106 ASM_DIV64_REG(hpet_tick, hpet_tick_rem,
107 hpet_period, tick_fsec_low, tick_fsec_high);
109 if (hpet_tick_rem > (hpet_period >> 1))
110 hpet_tick++; /* rounding the result */
113 * Stop the timers and reset the main counter.
115 cfg = hpet_readl(HPET_CFG);
116 cfg &= ~HPET_CFG_ENABLE;
117 hpet_writel(cfg, HPET_CFG);
118 hpet_writel(0, HPET_COUNTER);
119 hpet_writel(0, HPET_COUNTER + 4);
122 * Set up timer 0, as periodic with first interrupt to happen at
123 * hpet_tick, and period also hpet_tick.
125 cfg = hpet_readl(HPET_T0_CFG);
126 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
127 HPET_TN_SETVAL | HPET_TN_32BIT;
128 hpet_writel(cfg, HPET_T0_CFG);
129 hpet_writel(hpet_tick, HPET_T0_CMP);
134 cfg = hpet_readl(HPET_CFG);
135 cfg |= HPET_CFG_ENABLE | HPET_CFG_LEGACY;
136 hpet_writel(cfg, HPET_CFG);
145 memset(&hd, 0, sizeof (hd));
147 ntimer = hpet_readl(HPET_ID);
148 ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
152 * Register with driver.
153 * Timer0 and Timer1 is used by platform.
155 hd.hd_address = hpet_virt_address;
156 hd.hd_nirqs = ntimer;
157 hd.hd_flags = HPET_DATA_PLATFORM;
158 #ifndef CONFIG_HPET_EMULATE_RTC
163 hd.hd_irq[0] = HPET_LEGACY_8254;
164 hd.hd_irq[1] = HPET_LEGACY_RTC;
167 struct hpet_timer *timer;
170 hpet = (struct hpet *) hpet_virt_address;
172 for (i = 2, timer = &hpet->hpet_timers[2]; i < ntimer;
174 hd.hd_irq[i] = (timer->hpet_config &
175 Tn_INT_ROUTE_CNF_MASK) >>
176 Tn_INT_ROUTE_CNF_SHIFT;
184 #ifdef CONFIG_X86_LOCAL_APIC
185 wait_timer_tick = wait_hpet_tick;
190 int is_hpet_enabled(void)
195 int is_hpet_capable(void)
197 if (!boot_hpet_disable && hpet_address)
202 static int __init hpet_setup(char* str)
205 if (!strncmp("disable", str, 7))
206 boot_hpet_disable = 1;
211 __setup("hpet=", hpet_setup);
213 #ifdef CONFIG_HPET_EMULATE_RTC
214 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
215 * is enabled, we support RTC interrupt functionality in software.
216 * RTC has 3 kinds of interrupts:
217 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
219 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
220 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
221 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
222 * (1) and (2) above are implemented using polling at a frequency of
223 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
224 * overhead. (DEFAULT_RTC_INT_FREQ)
225 * For (3), we use interrupts at 64Hz or user specified periodic
226 * frequency, whichever is higher.
228 #include <linux/mc146818rtc.h>
229 #include <linux/rtc.h>
231 extern irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
233 #define DEFAULT_RTC_INT_FREQ 64
234 #define RTC_NUM_INTS 1
236 static unsigned long UIE_on;
237 static unsigned long prev_update_sec;
239 static unsigned long AIE_on;
240 static struct rtc_time alarm_time;
242 static unsigned long PIE_on;
243 static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
244 static unsigned long PIE_count;
246 static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
249 * Timer 1 for RTC, we do not use periodic interrupt feature,
250 * even if HPET supports periodic interrupts on Timer 1.
251 * The reason being, to set up a periodic interrupt in HPET, we need to
252 * stop the main counter. And if we do that everytime someone diables/enables
253 * RTC, we will have adverse effect on main kernel timer running on Timer 0.
254 * So, for the time being, simulate the periodic interrupt in software.
256 * hpet_rtc_timer_init() is called for the first time and during subsequent
257 * interuppts reinit happens through hpet_rtc_timer_reinit().
259 int hpet_rtc_timer_init(void)
261 unsigned int cfg, cnt;
264 if (!is_hpet_enabled())
267 * Set the counter 1 and enable the interrupts.
269 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
270 hpet_rtc_int_freq = PIE_freq;
272 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
274 local_irq_save(flags);
275 cnt = hpet_readl(HPET_COUNTER);
276 cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
277 hpet_writel(cnt, HPET_T1_CMP);
278 local_irq_restore(flags);
280 cfg = hpet_readl(HPET_T1_CFG);
281 cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;
282 hpet_writel(cfg, HPET_T1_CFG);
287 static void hpet_rtc_timer_reinit(void)
289 unsigned int cfg, cnt;
291 if (!(PIE_on | AIE_on | UIE_on))
294 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
295 hpet_rtc_int_freq = PIE_freq;
297 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
299 /* It is more accurate to use the comparator value than current count.*/
300 cnt = hpet_readl(HPET_T1_CMP);
301 cnt += hpet_tick*HZ/hpet_rtc_int_freq;
302 hpet_writel(cnt, HPET_T1_CMP);
304 cfg = hpet_readl(HPET_T1_CFG);
305 cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;
306 hpet_writel(cfg, HPET_T1_CFG);
312 * The functions below are called from rtc driver.
313 * Return 0 if HPET is not being used.
314 * Otherwise do the necessary changes and return 1.
316 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
318 if (!is_hpet_enabled())
321 if (bit_mask & RTC_UIE)
323 if (bit_mask & RTC_PIE)
325 if (bit_mask & RTC_AIE)
331 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
333 int timer_init_reqd = 0;
335 if (!is_hpet_enabled())
338 if (!(PIE_on | AIE_on | UIE_on))
341 if (bit_mask & RTC_UIE) {
344 if (bit_mask & RTC_PIE) {
348 if (bit_mask & RTC_AIE) {
353 hpet_rtc_timer_init();
358 int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
360 if (!is_hpet_enabled())
363 alarm_time.tm_hour = hrs;
364 alarm_time.tm_min = min;
365 alarm_time.tm_sec = sec;
370 int hpet_set_periodic_freq(unsigned long freq)
372 if (!is_hpet_enabled())
381 int hpet_rtc_dropped_irq(void)
383 if (!is_hpet_enabled())
389 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
391 struct rtc_time curr_time;
392 unsigned long rtc_int_flag = 0;
393 int call_rtc_interrupt = 0;
395 hpet_rtc_timer_reinit();
397 if (UIE_on | AIE_on) {
398 rtc_get_rtc_time(&curr_time);
401 if (curr_time.tm_sec != prev_update_sec) {
402 /* Set update int info, call real rtc int routine */
403 call_rtc_interrupt = 1;
404 rtc_int_flag = RTC_UF;
405 prev_update_sec = curr_time.tm_sec;
410 if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
411 /* Set periodic int info, call real rtc int routine */
412 call_rtc_interrupt = 1;
413 rtc_int_flag |= RTC_PF;
418 if ((curr_time.tm_sec == alarm_time.tm_sec) &&
419 (curr_time.tm_min == alarm_time.tm_min) &&
420 (curr_time.tm_hour == alarm_time.tm_hour)) {
421 /* Set alarm int info, call real rtc int routine */
422 call_rtc_interrupt = 1;
423 rtc_int_flag |= RTC_AF;
426 if (call_rtc_interrupt) {
427 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
428 rtc_interrupt(rtc_int_flag, dev_id, regs);