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