vserver 1.9.3
[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 /*
64  * Check whether HPET was found by ACPI boot parse. If yes setup HPET
65  * counter 0 for kernel base timer.
66  */
67 int __init hpet_enable(void)
68 {
69         unsigned int cfg, id;
70         unsigned long tick_fsec_low, tick_fsec_high; /* tick in femto sec */
71         unsigned long hpet_tick_rem;
72
73         if (boot_hpet_disable)
74                 return -1;
75
76         if (!hpet_address) {
77                 return -1;
78         }
79         hpet_virt_address = ioremap_nocache(hpet_address, 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
139 #ifdef  CONFIG_HPET
140         {
141                 struct hpet_data        hd;
142                 unsigned int            ntimer;
143
144                 memset(&hd, 0, sizeof (hd));
145
146                 ntimer = hpet_readl(HPET_ID);
147                 ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
148                 ntimer++;
149
150                 /*
151                  * Register with driver.
152                  * Timer0 and Timer1 is used by platform.
153                  */
154                 hd.hd_address = hpet_virt_address;
155                 hd.hd_nirqs = ntimer;
156                 hd.hd_flags = HPET_DATA_PLATFORM;
157                 hpet_reserve_timer(&hd, 0);
158 #ifdef  CONFIG_HPET_EMULATE_RTC
159                 hpet_reserve_timer(&hd, 1);
160 #endif
161                 hd.hd_irq[0] = HPET_LEGACY_8254;
162                 hd.hd_irq[1] = HPET_LEGACY_RTC;
163                 if (ntimer > 2) {
164                         struct hpet __iomem     *hpet;
165                         struct hpet_timer __iomem *timer;
166                         int                     i;
167
168                         hpet = hpet_virt_address;
169
170                         for (i = 2, timer = &hpet->hpet_timers[2]; i < ntimer;
171                                 timer++, i++)
172                                 hd.hd_irq[i] = (timer->hpet_config &
173                                         Tn_INT_ROUTE_CNF_MASK) >>
174                                         Tn_INT_ROUTE_CNF_SHIFT;
175
176                 }
177
178                 hpet_alloc(&hd);
179         }
180 #endif
181
182 #ifdef CONFIG_X86_LOCAL_APIC
183         wait_timer_tick = wait_hpet_tick;
184 #endif
185         return 0;
186 }
187
188 int is_hpet_enabled(void)
189 {
190         return use_hpet;
191 }
192
193 int is_hpet_capable(void)
194 {
195         if (!boot_hpet_disable && hpet_address)
196                 return 1;
197         return 0;
198 }
199
200 static int __init hpet_setup(char* str)
201 {
202         if (str) {
203                 if (!strncmp("disable", str, 7))
204                         boot_hpet_disable = 1;
205         }
206         return 1;
207 }
208
209 __setup("hpet=", hpet_setup);
210
211 #ifdef CONFIG_HPET_EMULATE_RTC
212 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
213  * is enabled, we support RTC interrupt functionality in software.
214  * RTC has 3 kinds of interrupts:
215  * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
216  *    is updated
217  * 2) Alarm Interrupt - generate an interrupt at a specific time of day
218  * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
219  *    2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
220  * (1) and (2) above are implemented using polling at a frequency of
221  * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
222  * overhead. (DEFAULT_RTC_INT_FREQ)
223  * For (3), we use interrupts at 64Hz or user specified periodic
224  * frequency, whichever is higher.
225  */
226 #include <linux/mc146818rtc.h>
227 #include <linux/rtc.h>
228
229 extern irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
230
231 #define DEFAULT_RTC_INT_FREQ    64
232 #define RTC_NUM_INTS            1
233
234 static unsigned long UIE_on;
235 static unsigned long prev_update_sec;
236
237 static unsigned long AIE_on;
238 static struct rtc_time alarm_time;
239
240 static unsigned long PIE_on;
241 static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
242 static unsigned long PIE_count;
243
244 static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
245
246 /*
247  * Timer 1 for RTC, we do not use periodic interrupt feature,
248  * even if HPET supports periodic interrupts on Timer 1.
249  * The reason being, to set up a periodic interrupt in HPET, we need to
250  * stop the main counter. And if we do that everytime someone diables/enables
251  * RTC, we will have adverse effect on main kernel timer running on Timer 0.
252  * So, for the time being, simulate the periodic interrupt in software.
253  *
254  * hpet_rtc_timer_init() is called for the first time and during subsequent
255  * interuppts reinit happens through hpet_rtc_timer_reinit().
256  */
257 int hpet_rtc_timer_init(void)
258 {
259         unsigned int cfg, cnt;
260         unsigned long flags;
261
262         if (!is_hpet_enabled())
263                 return 0;
264         /*
265          * Set the counter 1 and enable the interrupts.
266          */
267         if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
268                 hpet_rtc_int_freq = PIE_freq;
269         else
270                 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
271
272         local_irq_save(flags);
273         cnt = hpet_readl(HPET_COUNTER);
274         cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
275         hpet_writel(cnt, HPET_T1_CMP);
276         local_irq_restore(flags);
277
278         cfg = hpet_readl(HPET_T1_CFG);
279         cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;
280         hpet_writel(cfg, HPET_T1_CFG);
281
282         return 1;
283 }
284
285 static void hpet_rtc_timer_reinit(void)
286 {
287         unsigned int cfg, cnt;
288
289         if (!(PIE_on | AIE_on | UIE_on))
290                 return;
291
292         if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
293                 hpet_rtc_int_freq = PIE_freq;
294         else
295                 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
296
297         /* It is more accurate to use the comparator value than current count.*/
298         cnt = hpet_readl(HPET_T1_CMP);
299         cnt += hpet_tick*HZ/hpet_rtc_int_freq;
300         hpet_writel(cnt, HPET_T1_CMP);
301
302         cfg = hpet_readl(HPET_T1_CFG);
303         cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;
304         hpet_writel(cfg, HPET_T1_CFG);
305
306         return;
307 }
308
309 /*
310  * The functions below are called from rtc driver.
311  * Return 0 if HPET is not being used.
312  * Otherwise do the necessary changes and return 1.
313  */
314 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
315 {
316         if (!is_hpet_enabled())
317                 return 0;
318
319         if (bit_mask & RTC_UIE)
320                 UIE_on = 0;
321         if (bit_mask & RTC_PIE)
322                 PIE_on = 0;
323         if (bit_mask & RTC_AIE)
324                 AIE_on = 0;
325
326         return 1;
327 }
328
329 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
330 {
331         int timer_init_reqd = 0;
332
333         if (!is_hpet_enabled())
334                 return 0;
335
336         if (!(PIE_on | AIE_on | UIE_on))
337                 timer_init_reqd = 1;
338
339         if (bit_mask & RTC_UIE) {
340                 UIE_on = 1;
341         }
342         if (bit_mask & RTC_PIE) {
343                 PIE_on = 1;
344                 PIE_count = 0;
345         }
346         if (bit_mask & RTC_AIE) {
347                 AIE_on = 1;
348         }
349
350         if (timer_init_reqd)
351                 hpet_rtc_timer_init();
352
353         return 1;
354 }
355
356 int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
357 {
358         if (!is_hpet_enabled())
359                 return 0;
360
361         alarm_time.tm_hour = hrs;
362         alarm_time.tm_min = min;
363         alarm_time.tm_sec = sec;
364
365         return 1;
366 }
367
368 int hpet_set_periodic_freq(unsigned long freq)
369 {
370         if (!is_hpet_enabled())
371                 return 0;
372
373         PIE_freq = freq;
374         PIE_count = 0;
375
376         return 1;
377 }
378
379 int hpet_rtc_dropped_irq(void)
380 {
381         if (!is_hpet_enabled())
382                 return 0;
383
384         return 1;
385 }
386
387 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
388 {
389         struct rtc_time curr_time;
390         unsigned long rtc_int_flag = 0;
391         int call_rtc_interrupt = 0;
392
393         hpet_rtc_timer_reinit();
394
395         if (UIE_on | AIE_on) {
396                 rtc_get_rtc_time(&curr_time);
397         }
398         if (UIE_on) {
399                 if (curr_time.tm_sec != prev_update_sec) {
400                         /* Set update int info, call real rtc int routine */
401                         call_rtc_interrupt = 1;
402                         rtc_int_flag = RTC_UF;
403                         prev_update_sec = curr_time.tm_sec;
404                 }
405         }
406         if (PIE_on) {
407                 PIE_count++;
408                 if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
409                         /* Set periodic int info, call real rtc int routine */
410                         call_rtc_interrupt = 1;
411                         rtc_int_flag |= RTC_PF;
412                         PIE_count = 0;
413                 }
414         }
415         if (AIE_on) {
416                 if ((curr_time.tm_sec == alarm_time.tm_sec) &&
417                     (curr_time.tm_min == alarm_time.tm_min) &&
418                     (curr_time.tm_hour == alarm_time.tm_hour)) {
419                         /* Set alarm int info, call real rtc int routine */
420                         call_rtc_interrupt = 1;
421                         rtc_int_flag |= RTC_AF;
422                 }
423         }
424         if (call_rtc_interrupt) {
425                 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
426                 rtc_interrupt(rtc_int_flag, dev_id, regs);
427         }
428         return IRQ_HANDLED;
429 }
430 #endif
431