ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / kernel / time_hpet.c
1 /*
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.
5  *
6  *  2003-06-30    Venkatesh Pallipadi - Additional changes for HPET support
7  */
8
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>
15
16 #include <asm/timer.h>
17 #include <asm/fixmap.h>
18 #include <asm/apic.h>
19
20 #include <linux/timex.h>
21 #include <linux/config.h>
22
23 #include <asm/hpet.h>
24
25 unsigned long hpet_period;      /* fsecs / HPET clock */
26 unsigned long hpet_tick;        /* hpet clks count per tick */
27 unsigned long hpet_address;     /* hpet memory map physical address */
28
29 static int use_hpet;            /* can be used for runtime check of hpet */
30 static int boot_hpet_disable;   /* boottime override for HPET timer */
31 static unsigned long hpet_virt_address; /* hpet kernel virtual address */
32
33 #define FSEC_TO_USEC (1000000000UL)
34
35 int hpet_readl(unsigned long a)
36 {
37         return readl(hpet_virt_address + a);
38 }
39
40 void hpet_writel(unsigned long d, unsigned long a)
41 {
42         writel(d, hpet_virt_address + a);
43 }
44
45 #ifdef CONFIG_X86_LOCAL_APIC
46 /*
47  * HPET counters dont wrap around on every tick. They just change the
48  * comparator value and continue. Next tick can be caught by checking
49  * for a change in the comparator value. Used in apic.c.
50  */
51 void __init wait_hpet_tick(void)
52 {
53         unsigned int start_cmp_val, end_cmp_val;
54
55         start_cmp_val = hpet_readl(HPET_T0_CMP);
56         do {
57                 end_cmp_val = hpet_readl(HPET_T0_CMP);
58         } while (start_cmp_val == end_cmp_val);
59 }
60 #endif
61
62 /*
63  * Check whether HPET was found by ACPI boot parse. If yes setup HPET
64  * counter 0 for kernel base timer.
65  */
66 int __init hpet_enable(void)
67 {
68         unsigned int cfg, id;
69         unsigned long tick_fsec_low, tick_fsec_high; /* tick in femto sec */
70         unsigned long hpet_tick_rem;
71
72         if (boot_hpet_disable)
73                 return -1;
74
75         if (!hpet_address) {
76                 return -1;
77         }
78         hpet_virt_address = (unsigned long) ioremap_nocache(hpet_address,
79                                                             HPET_MMAP_SIZE);
80         /*
81          * Read the period, compute tick and quotient.
82          */
83         id = hpet_readl(HPET_ID);
84
85         /*
86          * We are checking for value '1' or more in number field.
87          * So, we are OK with HPET_EMULATE_RTC part too, where we need
88          * to have atleast 2 timers.
89          */
90         if (!(id & HPET_ID_NUMBER) ||
91             !(id & HPET_ID_LEGSUP))
92                 return -1;
93
94         hpet_period = hpet_readl(HPET_PERIOD);
95         if ((hpet_period < HPET_MIN_PERIOD) || (hpet_period > HPET_MAX_PERIOD))
96                 return -1;
97
98         /*
99          * 64 bit math
100          * First changing tick into fsec
101          * Then 64 bit div to find number of hpet clk per tick
102          */
103         ASM_MUL64_REG(tick_fsec_low, tick_fsec_high,
104                         KERNEL_TICK_USEC, FSEC_TO_USEC);
105         ASM_DIV64_REG(hpet_tick, hpet_tick_rem,
106                         hpet_period, tick_fsec_low, tick_fsec_high);
107
108         if (hpet_tick_rem > (hpet_period >> 1))
109                 hpet_tick++; /* rounding the result */
110
111         /*
112          * Stop the timers and reset the main counter.
113          */
114         cfg = hpet_readl(HPET_CFG);
115         cfg &= ~HPET_CFG_ENABLE;
116         hpet_writel(cfg, HPET_CFG);
117         hpet_writel(0, HPET_COUNTER);
118         hpet_writel(0, HPET_COUNTER + 4);
119
120         /*
121          * Set up timer 0, as periodic with first interrupt to happen at
122          * hpet_tick, and period also hpet_tick.
123          */
124         cfg = hpet_readl(HPET_T0_CFG);
125         cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
126                HPET_TN_SETVAL | HPET_TN_32BIT;
127         hpet_writel(cfg, HPET_T0_CFG);
128         hpet_writel(hpet_tick, HPET_T0_CMP);
129
130         /*
131          * Go!
132          */
133         cfg = hpet_readl(HPET_CFG);
134         cfg |= HPET_CFG_ENABLE | HPET_CFG_LEGACY;
135         hpet_writel(cfg, HPET_CFG);
136
137         use_hpet = 1;
138 #ifdef CONFIG_X86_LOCAL_APIC
139         wait_timer_tick = wait_hpet_tick;
140 #endif
141         return 0;
142 }
143
144 int is_hpet_enabled(void)
145 {
146         return use_hpet;
147 }
148
149 int is_hpet_capable(void)
150 {
151         if (!boot_hpet_disable && hpet_address)
152                 return 1;
153         return 0;
154 }
155
156 static int __init hpet_setup(char* str)
157 {
158         if (str) {
159                 if (!strncmp("disable", str, 7))
160                         boot_hpet_disable = 1;
161         }
162         return 1;
163 }
164
165 __setup("hpet=", hpet_setup);
166
167 #ifdef CONFIG_HPET_EMULATE_RTC
168 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
169  * is enabled, we support RTC interrupt functionality in software.
170  * RTC has 3 kinds of interrupts:
171  * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
172  *    is updated
173  * 2) Alarm Interrupt - generate an interrupt at a specific time of day
174  * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
175  *    2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
176  * (1) and (2) above are implemented using polling at a frequency of
177  * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
178  * overhead. (DEFAULT_RTC_INT_FREQ)
179  * For (3), we use interrupts at 64Hz or user specified periodic
180  * frequency, whichever is higher.
181  */
182 #include <linux/mc146818rtc.h>
183 #include <linux/rtc.h>
184
185 extern irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
186
187 #define DEFAULT_RTC_INT_FREQ    64
188 #define RTC_NUM_INTS            1
189
190 static unsigned long UIE_on;
191 static unsigned long prev_update_sec;
192
193 static unsigned long AIE_on;
194 static struct rtc_time alarm_time;
195
196 static unsigned long PIE_on;
197 static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
198 static unsigned long PIE_count;
199
200 static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
201
202 /*
203  * Timer 1 for RTC, we do not use periodic interrupt feature,
204  * even if HPET supports periodic interrupts on Timer 1.
205  * The reason being, to set up a periodic interrupt in HPET, we need to
206  * stop the main counter. And if we do that everytime someone diables/enables
207  * RTC, we will have adverse effect on main kernel timer running on Timer 0.
208  * So, for the time being, simulate the periodic interrupt in software.
209  *
210  * hpet_rtc_timer_init() is called for the first time and during subsequent
211  * interuppts reinit happens through hpet_rtc_timer_reinit().
212  */
213 int hpet_rtc_timer_init(void)
214 {
215         unsigned int cfg, cnt;
216         unsigned long flags;
217
218         if (!is_hpet_enabled())
219                 return 0;
220         /*
221          * Set the counter 1 and enable the interrupts.
222          */
223         if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
224                 hpet_rtc_int_freq = PIE_freq;
225         else
226                 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
227
228         local_irq_save(flags);
229         cnt = hpet_readl(HPET_COUNTER);
230         cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
231         hpet_writel(cnt, HPET_T1_CMP);
232         local_irq_restore(flags);
233
234         cfg = hpet_readl(HPET_T1_CFG);
235         cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;
236         hpet_writel(cfg, HPET_T1_CFG);
237
238         return 1;
239 }
240
241 static void hpet_rtc_timer_reinit(void)
242 {
243         unsigned int cfg, cnt;
244
245         if (!(PIE_on | AIE_on | UIE_on))
246                 return;
247
248         if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
249                 hpet_rtc_int_freq = PIE_freq;
250         else
251                 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
252
253         /* It is more accurate to use the comparator value than current count.*/
254         cnt = hpet_readl(HPET_T1_CMP);
255         cnt += hpet_tick*HZ/hpet_rtc_int_freq;
256         hpet_writel(cnt, HPET_T1_CMP);
257
258         cfg = hpet_readl(HPET_T1_CFG);
259         cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;
260         hpet_writel(cfg, HPET_T1_CFG);
261
262         return;
263 }
264
265 /*
266  * The functions below are called from rtc driver.
267  * Return 0 if HPET is not being used.
268  * Otherwise do the necessary changes and return 1.
269  */
270 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
271 {
272         if (!is_hpet_enabled())
273                 return 0;
274
275         if (bit_mask & RTC_UIE)
276                 UIE_on = 0;
277         if (bit_mask & RTC_PIE)
278                 PIE_on = 0;
279         if (bit_mask & RTC_AIE)
280                 AIE_on = 0;
281
282         return 1;
283 }
284
285 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
286 {
287         int timer_init_reqd = 0;
288
289         if (!is_hpet_enabled())
290                 return 0;
291
292         if (!(PIE_on | AIE_on | UIE_on))
293                 timer_init_reqd = 1;
294
295         if (bit_mask & RTC_UIE) {
296                 UIE_on = 1;
297         }
298         if (bit_mask & RTC_PIE) {
299                 PIE_on = 1;
300                 PIE_count = 0;
301         }
302         if (bit_mask & RTC_AIE) {
303                 AIE_on = 1;
304         }
305
306         if (timer_init_reqd)
307                 hpet_rtc_timer_init();
308
309         return 1;
310 }
311
312 int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
313 {
314         if (!is_hpet_enabled())
315                 return 0;
316
317         alarm_time.tm_hour = hrs;
318         alarm_time.tm_min = min;
319         alarm_time.tm_sec = sec;
320
321         return 1;
322 }
323
324 int hpet_set_periodic_freq(unsigned long freq)
325 {
326         if (!is_hpet_enabled())
327                 return 0;
328
329         PIE_freq = freq;
330         PIE_count = 0;
331
332         return 1;
333 }
334
335 int hpet_rtc_dropped_irq(void)
336 {
337         if (!is_hpet_enabled())
338                 return 0;
339
340         return 1;
341 }
342
343 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
344 {
345         struct rtc_time curr_time;
346         unsigned long rtc_int_flag = 0;
347         int call_rtc_interrupt = 0;
348
349         hpet_rtc_timer_reinit();
350
351         if (UIE_on | AIE_on) {
352                 rtc_get_rtc_time(&curr_time);
353         }
354         if (UIE_on) {
355                 if (curr_time.tm_sec != prev_update_sec) {
356                         /* Set update int info, call real rtc int routine */
357                         call_rtc_interrupt = 1;
358                         rtc_int_flag = RTC_UF;
359                         prev_update_sec = curr_time.tm_sec;
360                 }
361         }
362         if (PIE_on) {
363                 PIE_count++;
364                 if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
365                         /* Set periodic int info, call real rtc int routine */
366                         call_rtc_interrupt = 1;
367                         rtc_int_flag |= RTC_PF;
368                         PIE_count = 0;
369                 }
370         }
371         if (AIE_on) {
372                 if ((curr_time.tm_sec == alarm_time.tm_sec) &&
373                     (curr_time.tm_min == alarm_time.tm_min) &&
374                     (curr_time.tm_hour == alarm_time.tm_hour)) {
375                         /* Set alarm int info, call real rtc int routine */
376                         call_rtc_interrupt = 1;
377                         rtc_int_flag |= RTC_AF;
378                 }
379         }
380         if (call_rtc_interrupt) {
381                 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
382                 rtc_interrupt(rtc_int_flag, dev_id, regs);
383         }
384         return IRQ_HANDLED;
385 }
386 #endif
387