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