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