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