Pull novell-bugzilla-156426 into release branch
[pandora-kernel.git] / arch / mips / au1000 / common / time.c
1 /*
2  *
3  * Copyright (C) 2001 MontaVista Software, ppopov@mvista.com
4  * Copied and modified Carsten Langgaard's time.c
5  *
6  * Carsten Langgaard, carstenl@mips.com
7  * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
8  *
9  * ########################################################################
10  *
11  *  This program is free software; you can distribute it and/or modify it
12  *  under the terms of the GNU General Public License (Version 2) as
13  *  published by the Free Software Foundation.
14  *
15  *  This program is distributed in the hope it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  *  for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
23  *
24  * ########################################################################
25  *
26  * Setting up the clock on the MIPS boards.
27  *
28  * Update.  Always configure the kernel with CONFIG_NEW_TIME_C.  This
29  * will use the user interface gettimeofday() functions from the
30  * arch/mips/kernel/time.c, and we provide the clock interrupt processing
31  * and the timer offset compute functions.  If CONFIG_PM is selected,
32  * we also ensure the 32KHz timer is available.   -- Dan
33  */
34
35 #include <linux/types.h>
36 #include <linux/config.h>
37 #include <linux/init.h>
38 #include <linux/kernel_stat.h>
39 #include <linux/sched.h>
40 #include <linux/spinlock.h>
41 #include <linux/hardirq.h>
42
43 #include <asm/compiler.h>
44 #include <asm/mipsregs.h>
45 #include <asm/ptrace.h>
46 #include <asm/time.h>
47 #include <asm/div64.h>
48 #include <asm/mach-au1x00/au1000.h>
49
50 #include <linux/mc146818rtc.h>
51 #include <linux/timex.h>
52
53 extern void do_softirq(void);
54 extern volatile unsigned long wall_jiffies;
55 unsigned long missed_heart_beats = 0;
56
57 static unsigned long r4k_offset; /* Amount to increment compare reg each time */
58 static unsigned long r4k_cur;    /* What counter should be at next timer irq */
59 int     no_au1xxx_32khz;
60 extern int allow_au1k_wait; /* default off for CP0 Counter */
61
62 /* Cycle counter value at the previous timer interrupt.. */
63 static unsigned int timerhi = 0, timerlo = 0;
64
65 #ifdef CONFIG_PM
66 #if HZ < 100 || HZ > 1000
67 #error "unsupported HZ value! Must be in [100,1000]"
68 #endif
69 #define MATCH20_INC (328*100/HZ) /* magic number 328 is for HZ=100... */
70 extern void startup_match20_interrupt(irqreturn_t (*handler)(int, void *, struct pt_regs *));
71 static unsigned long last_pc0, last_match20;
72 #endif
73
74 static DEFINE_SPINLOCK(time_lock);
75
76 static inline void ack_r4ktimer(unsigned long newval)
77 {
78         write_c0_compare(newval);
79 }
80
81 /*
82  * There are a lot of conceptually broken versions of the MIPS timer interrupt
83  * handler floating around.  This one is rather different, but the algorithm
84  * is provably more robust.
85  */
86 unsigned long wtimer;
87 void mips_timer_interrupt(struct pt_regs *regs)
88 {
89         int irq = 63;
90         unsigned long count;
91
92         irq_enter();
93         kstat_this_cpu.irqs[irq]++;
94
95         if (r4k_offset == 0)
96                 goto null;
97
98         do {
99                 count = read_c0_count();
100                 timerhi += (count < timerlo);   /* Wrap around */
101                 timerlo = count;
102
103                 kstat_this_cpu.irqs[irq]++;
104                 do_timer(regs);
105 #ifndef CONFIG_SMP
106                 update_process_times(user_mode(regs));
107 #endif
108                 r4k_cur += r4k_offset;
109                 ack_r4ktimer(r4k_cur);
110
111         } while (((unsigned long)read_c0_count()
112                  - r4k_cur) < 0x7fffffff);
113
114         irq_exit();
115         return;
116
117 null:
118         ack_r4ktimer(0);
119         irq_exit();
120 }
121
122 #ifdef CONFIG_PM
123 irqreturn_t counter0_irq(int irq, void *dev_id, struct pt_regs *regs)
124 {
125         unsigned long pc0;
126         int time_elapsed;
127         static int jiffie_drift = 0;
128
129         if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
130                 /* should never happen! */
131                 printk(KERN_WARNING "counter 0 w status error\n");
132                 return IRQ_NONE;
133         }
134
135         pc0 = au_readl(SYS_TOYREAD);
136         if (pc0 < last_match20) {
137                 /* counter overflowed */
138                 time_elapsed = (0xffffffff - last_match20) + pc0;
139         }
140         else {
141                 time_elapsed = pc0 - last_match20;
142         }
143
144         while (time_elapsed > 0) {
145                 do_timer(regs);
146 #ifndef CONFIG_SMP
147                 update_process_times(user_mode(regs));
148 #endif
149                 time_elapsed -= MATCH20_INC;
150                 last_match20 += MATCH20_INC;
151                 jiffie_drift++;
152         }
153
154         last_pc0 = pc0;
155         au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
156         au_sync();
157
158         /* our counter ticks at 10.009765625 ms/tick, we we're running
159          * almost 10uS too slow per tick.
160          */
161
162         if (jiffie_drift >= 999) {
163                 jiffie_drift -= 999;
164                 do_timer(regs); /* increment jiffies by one */
165 #ifndef CONFIG_SMP
166                 update_process_times(user_mode(regs));
167 #endif
168         }
169
170         return IRQ_HANDLED;
171 }
172
173 /* When we wakeup from sleep, we have to "catch up" on all of the
174  * timer ticks we have missed.
175  */
176 void
177 wakeup_counter0_adjust(void)
178 {
179         unsigned long pc0;
180         int time_elapsed;
181
182         pc0 = au_readl(SYS_TOYREAD);
183         if (pc0 < last_match20) {
184                 /* counter overflowed */
185                 time_elapsed = (0xffffffff - last_match20) + pc0;
186         }
187         else {
188                 time_elapsed = pc0 - last_match20;
189         }
190
191         while (time_elapsed > 0) {
192                 time_elapsed -= MATCH20_INC;
193                 last_match20 += MATCH20_INC;
194         }
195
196         last_pc0 = pc0;
197         au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
198         au_sync();
199
200 }
201
202 /* This is just for debugging to set the timer for a sleep delay.
203 */
204 void
205 wakeup_counter0_set(int ticks)
206 {
207         unsigned long pc0;
208
209         pc0 = au_readl(SYS_TOYREAD);
210         last_pc0 = pc0;
211         au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
212         au_sync();
213 }
214 #endif
215
216 /* I haven't found anyone that doesn't use a 12 MHz source clock,
217  * but just in case.....
218  */
219 #ifdef CONFIG_AU1000_SRC_CLK
220 #define AU1000_SRC_CLK  CONFIG_AU1000_SRC_CLK
221 #else
222 #define AU1000_SRC_CLK  12000000
223 #endif
224
225 /*
226  * We read the real processor speed from the PLL.  This is important
227  * because it is more accurate than computing it from the 32KHz
228  * counter, if it exists.  If we don't have an accurate processor
229  * speed, all of the peripherals that derive their clocks based on
230  * this advertised speed will introduce error and sometimes not work
231  * properly.  This function is futher convoluted to still allow configurations
232  * to do that in case they have really, really old silicon with a
233  * write-only PLL register, that we need the 32KHz when power management
234  * "wait" is enabled, and we need to detect if the 32KHz isn't present
235  * but requested......got it? :-)               -- Dan
236  */
237 unsigned long cal_r4koff(void)
238 {
239         unsigned long count;
240         unsigned long cpu_speed;
241         unsigned long flags;
242         unsigned long counter;
243
244         spin_lock_irqsave(&time_lock, flags);
245
246         /* Power management cares if we don't have a 32KHz counter.
247         */
248         no_au1xxx_32khz = 0;
249         counter = au_readl(SYS_COUNTER_CNTRL);
250         if (counter & SYS_CNTRL_E0) {
251                 int trim_divide = 16;
252
253                 au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
254
255                 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
256                 /* RTC now ticks at 32.768/16 kHz */
257                 au_writel(trim_divide-1, SYS_RTCTRIM);
258                 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
259
260                 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
261                 au_writel (0, SYS_TOYWRITE);
262                 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
263
264 #if defined(CONFIG_AU1000_USE32K)
265                 {
266                         unsigned long start, end;
267
268                         start = au_readl(SYS_RTCREAD);
269                         start += 2;
270                         /* wait for the beginning of a new tick
271                         */
272                         while (au_readl(SYS_RTCREAD) < start);
273
274                         /* Start r4k counter.
275                         */
276                         write_c0_count(0);
277
278                         /* Wait 0.5 seconds.
279                         */
280                         end = start + (32768 / trim_divide)/2;
281
282                         while (end > au_readl(SYS_RTCREAD));
283
284                         count = read_c0_count();
285                         cpu_speed = count * 2;
286                 }
287 #else
288                 cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) *
289                         AU1000_SRC_CLK;
290                 count = cpu_speed / 2;
291 #endif
292         }
293         else {
294                 /* The 32KHz oscillator isn't running, so assume there
295                  * isn't one and grab the processor speed from the PLL.
296                  * NOTE: some old silicon doesn't allow reading the PLL.
297                  */
298                 cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
299                 count = cpu_speed / 2;
300                 no_au1xxx_32khz = 1;
301         }
302         mips_hpt_frequency = count;
303         // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16)
304         set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
305         spin_unlock_irqrestore(&time_lock, flags);
306         return (cpu_speed / HZ);
307 }
308
309 /* This is for machines which generate the exact clock. */
310 #define USECS_PER_JIFFY (1000000/HZ)
311 #define USECS_PER_JIFFY_FRAC (0x100000000LL*1000000/HZ&0xffffffff)
312
313 static unsigned long
314 div64_32(unsigned long v1, unsigned long v2, unsigned long v3)
315 {
316         unsigned long r0;
317         do_div64_32(r0, v1, v2, v3);
318         return r0;
319 }
320
321 static unsigned long do_fast_cp0_gettimeoffset(void)
322 {
323         u32 count;
324         unsigned long res, tmp;
325         unsigned long r0;
326
327         /* Last jiffy when do_fast_gettimeoffset() was called. */
328         static unsigned long last_jiffies=0;
329         unsigned long quotient;
330
331         /*
332          * Cached "1/(clocks per usec)*2^32" value.
333          * It has to be recalculated once each jiffy.
334          */
335         static unsigned long cached_quotient=0;
336
337         tmp = jiffies;
338
339         quotient = cached_quotient;
340
341         if (tmp && last_jiffies != tmp) {
342                 last_jiffies = tmp;
343                 if (last_jiffies != 0) {
344                         r0 = div64_32(timerhi, timerlo, tmp);
345                         quotient = div64_32(USECS_PER_JIFFY, USECS_PER_JIFFY_FRAC, r0);
346                         cached_quotient = quotient;
347                 }
348         }
349
350         /* Get last timer tick in absolute kernel time */
351         count = read_c0_count();
352
353         /* .. relative to previous jiffy (32 bits is enough) */
354         count -= timerlo;
355
356         __asm__("multu\t%1,%2\n\t"
357                 "mfhi\t%0"
358                 : "=r" (res)
359                 : "r" (count), "r" (quotient)
360                 : "hi", "lo", GCC_REG_ACCUM);
361
362         /*
363          * Due to possible jiffies inconsistencies, we need to check
364          * the result so that we'll get a timer that is monotonic.
365          */
366         if (res >= USECS_PER_JIFFY)
367                 res = USECS_PER_JIFFY-1;
368
369         return res;
370 }
371
372 #ifdef CONFIG_PM
373 static unsigned long do_fast_pm_gettimeoffset(void)
374 {
375         unsigned long pc0;
376         unsigned long offset;
377
378         pc0 = au_readl(SYS_TOYREAD);
379         au_sync();
380         offset = pc0 - last_pc0;
381         if (offset > 2*MATCH20_INC) {
382                 printk("huge offset %x, last_pc0 %x last_match20 %x pc0 %x\n",
383                                 (unsigned)offset, (unsigned)last_pc0,
384                                 (unsigned)last_match20, (unsigned)pc0);
385         }
386         offset = (unsigned long)((offset * 305) / 10);
387         return offset;
388 }
389 #endif
390
391 void au1xxx_timer_setup(struct irqaction *irq)
392 {
393         unsigned int est_freq;
394         extern unsigned long (*do_gettimeoffset)(void);
395
396         printk("calculating r4koff... ");
397         r4k_offset = cal_r4koff();
398         printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset);
399
400         //est_freq = 2*r4k_offset*HZ;
401         est_freq = r4k_offset*HZ;
402         est_freq += 5000;    /* round */
403         est_freq -= est_freq%10000;
404         printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
405                (est_freq%1000000)*100/1000000);
406         set_au1x00_speed(est_freq);
407         set_au1x00_lcd_clock(); // program the LCD clock
408
409         r4k_cur = (read_c0_count() + r4k_offset);
410         write_c0_compare(r4k_cur);
411
412 #ifdef CONFIG_PM
413         /*
414          * setup counter 0, since it keeps ticking after a
415          * 'wait' instruction has been executed. The CP0 timer and
416          * counter 1 do NOT continue running after 'wait'
417          *
418          * It's too early to call request_irq() here, so we handle
419          * counter 0 interrupt as a special irq and it doesn't show
420          * up under /proc/interrupts.
421          *
422          * Check to ensure we really have a 32KHz oscillator before
423          * we do this.
424          */
425         if (no_au1xxx_32khz) {
426                 unsigned int c0_status;
427
428                 printk("WARNING: no 32KHz clock found.\n");
429                 do_gettimeoffset = do_fast_cp0_gettimeoffset;
430
431                 /* Ensure we get CPO_COUNTER interrupts.
432                 */
433                 c0_status = read_c0_status();
434                 c0_status |= IE_IRQ5;
435                 write_c0_status(c0_status);
436         }
437         else {
438                 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
439                 au_writel(0, SYS_TOYWRITE);
440                 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
441
442                 au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK);
443                 au_writel(~0, SYS_WAKESRC);
444                 au_sync();
445                 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
446
447                 /* setup match20 to interrupt once every HZ */
448                 last_pc0 = last_match20 = au_readl(SYS_TOYREAD);
449                 au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
450                 au_sync();
451                 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
452                 startup_match20_interrupt(counter0_irq);
453
454                 do_gettimeoffset = do_fast_pm_gettimeoffset;
455
456                 /* We can use the real 'wait' instruction.
457                 */
458                 allow_au1k_wait = 1;
459         }
460
461 #else
462         /* We have to do this here instead of in timer_init because
463          * the generic code in arch/mips/kernel/time.c will write
464          * over our function pointer.
465          */
466         do_gettimeoffset = do_fast_cp0_gettimeoffset;
467 #endif
468 }
469
470 void __init au1xxx_time_init(void)
471 {
472 }