ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sh / kernel / time.c
1 /* $Id: time.c,v 1.19 2004/02/27 00:40:48 lethal Exp $
2  *
3  *  linux/arch/sh/kernel/time.c
4  *
5  *  Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka
6  *  Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org>
7  *  Copyright (C) 2002, 2003  Paul Mundt
8  *  Copyright (C) 2002  M. R. Brown  <mrbrown@linux-sh.org>
9  *
10  *  Some code taken from i386 version.
11  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
12  */
13
14 #include <linux/config.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/kernel.h>
19 #include <linux/param.h>
20 #include <linux/string.h>
21 #include <linux/mm.h>
22 #include <linux/interrupt.h>
23 #include <linux/time.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/smp.h>
27
28 #include <asm/processor.h>
29 #include <asm/uaccess.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <asm/delay.h>
33 #include <asm/machvec.h>
34 #include <asm/rtc.h>
35 #include <asm/freq.h>
36 #ifdef CONFIG_SH_KGDB
37 #include <asm/kgdb.h>
38 #endif
39
40 #include <linux/timex.h>
41 #include <linux/irq.h>
42
43 #define TMU_TOCR_INIT   0x00
44 #define TMU0_TCR_INIT   0x0020
45 #define TMU_TSTR_INIT   1
46
47 #define TMU0_TCR_CALIB  0x0000
48
49 #if defined(CONFIG_CPU_SH3)
50 #define TMU_TOCR        0xfffffe90      /* Byte access */
51 #define TMU_TSTR        0xfffffe92      /* Byte access */
52
53 #define TMU0_TCOR       0xfffffe94      /* Long access */
54 #define TMU0_TCNT       0xfffffe98      /* Long access */
55 #define TMU0_TCR        0xfffffe9c      /* Word access */
56 #elif defined(CONFIG_CPU_SH4)
57 #define TMU_TOCR        0xffd80000      /* Byte access */
58 #define TMU_TSTR        0xffd80004      /* Byte access */
59
60 #define TMU0_TCOR       0xffd80008      /* Long access */
61 #define TMU0_TCNT       0xffd8000c      /* Long access */
62 #define TMU0_TCR        0xffd80010      /* Word access */
63
64 #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
65 #define CLOCKGEN_MEMCLKCR 0xbb040038
66 #define MEMCLKCR_RATIO_MASK 0x7
67 #endif /* CONFIG_CPU_SUBTYPE_ST40STB1 */
68 #endif /* CONFIG_CPU_SH3 or CONFIG_CPU_SH4 */
69
70 extern unsigned long wall_jiffies;
71 #define TICK_SIZE (tick_nsec / 1000)
72 spinlock_t tmu0_lock = SPIN_LOCK_UNLOCKED;
73
74 u64 jiffies_64 = INITIAL_JIFFIES;
75
76 EXPORT_SYMBOL(jiffies_64);
77
78 /* XXX: Can we initialize this in a routine somewhere?  Dreamcast doesn't want
79  * these routines anywhere... */
80 #ifdef CONFIG_SH_RTC
81 void (*rtc_get_time)(struct timespec *) = sh_rtc_gettimeofday;
82 int (*rtc_set_time)(const time_t) = sh_rtc_settimeofday;
83 #else
84 void (*rtc_get_time)(struct timespec *) = 0;
85 int (*rtc_set_time)(const time_t) = 0;
86 #endif
87
88 #if defined(CONFIG_CPU_SH3)
89 static int stc_multipliers[] = { 1, 2, 3, 4, 6, 1, 1, 1 };
90 static int stc_values[]      = { 0, 1, 4, 2, 5, 0, 0, 0 };
91 #define bfc_divisors stc_multipliers
92 #define bfc_values stc_values
93 static int ifc_divisors[]    = { 1, 2, 3, 4, 1, 1, 1, 1 };
94 static int ifc_values[]      = { 0, 1, 4, 2, 0, 0, 0, 0 };
95 static int pfc_divisors[]    = { 1, 2, 3, 4, 6, 1, 1, 1 };
96 static int pfc_values[]      = { 0, 1, 4, 2, 5, 0, 0, 0 };
97 #elif defined(CONFIG_CPU_SH4)
98 static int ifc_divisors[] = { 1, 2, 3, 4, 6, 8, 1, 1 };
99 static int ifc_values[]   = { 0, 1, 2, 3, 0, 4, 0, 5 };
100 #define bfc_divisors ifc_divisors       /* Same */
101 #define bfc_values ifc_values
102 static int pfc_divisors[] = { 2, 3, 4, 6, 8, 2, 2, 2 };
103 static int pfc_values[]   = { 0, 0, 1, 2, 0, 3, 0, 4 };
104 #else
105 #error "Unknown ifc/bfc/pfc/stc values for this processor"
106 #endif
107
108 /*
109  * Scheduler clock - returns current time in nanosec units.
110  */
111 unsigned long long sched_clock(void)
112 {
113         return (unsigned long long)jiffies * (1000000000 / HZ);
114 }
115
116 static unsigned long do_gettimeoffset(void)
117 {
118         int count;
119         unsigned long flags;
120
121         static int count_p = 0x7fffffff;    /* for the first call after boot */
122         static unsigned long jiffies_p = 0;
123
124         /*
125          * cache volatile jiffies temporarily; we have IRQs turned off. 
126          */
127         unsigned long jiffies_t;
128
129         spin_lock_irqsave(&tmu0_lock, flags);
130         /* timer count may underflow right here */
131         count = ctrl_inl(TMU0_TCNT);    /* read the latched count */
132
133         jiffies_t = jiffies;
134
135         /*
136          * avoiding timer inconsistencies (they are rare, but they happen)...
137          * there is one kind of problem that must be avoided here:
138          *  1. the timer counter underflows
139          */
140
141         if( jiffies_t == jiffies_p ) {
142                 if( count > count_p ) {
143                         /* the nutcase */
144
145                         if(ctrl_inw(TMU0_TCR) & 0x100) { /* Check UNF bit */
146                                 /*
147                                  * We cannot detect lost timer interrupts ... 
148                                  * well, that's why we call them lost, don't we? :)
149                                  * [hmm, on the Pentium and Alpha we can ... sort of]
150                                  */
151                                 count -= LATCH;
152                         } else {
153                                 printk("do_slow_gettimeoffset(): hardware timer problem?\n");
154                         }
155                 }
156         } else
157                 jiffies_p = jiffies_t;
158
159         count_p = count;
160         spin_unlock_irqrestore(&tmu0_lock, flags);
161
162         count = ((LATCH-1) - count) * TICK_SIZE;
163         count = (count + LATCH/2) / LATCH;
164
165         return count;
166 }
167
168 void do_gettimeofday(struct timeval *tv)
169 {
170         unsigned long seq;
171         unsigned long usec, sec;
172         unsigned long lost;
173
174         do {
175                 seq = read_seqbegin(&xtime_lock);
176                 usec = do_gettimeoffset();
177                 
178                 lost = jiffies - wall_jiffies;
179                 if (lost)
180                         usec += lost * (1000000 / HZ);
181
182                 sec = xtime.tv_sec;
183                 usec += xtime.tv_nsec / 1000;
184         } while (read_seqretry(&xtime_lock, seq));
185
186         while (usec >= 1000000) {
187                 usec -= 1000000;
188                 sec++;
189         }
190
191         tv->tv_sec = sec;
192         tv->tv_usec = usec;
193 }
194
195 EXPORT_SYMBOL(do_gettimeofday);
196
197 int do_settimeofday(struct timespec *tv)
198 {
199         time_t wtm_sec, sec = tv->tv_sec;
200         long wtm_nsec, nsec = tv->tv_nsec;
201
202         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
203                 return -EINVAL;
204
205         write_seqlock_irq(&xtime_lock);
206         /*
207          * This is revolting. We need to set "xtime" correctly. However, the
208          * value in this location is the value at the most recent update of
209          * wall time.  Discover what correction gettimeofday() would have
210          * made, and then undo it!
211          */
212         nsec -= 1000 * (do_gettimeoffset() +
213                                 (jiffies - wall_jiffies) * (1000000 / HZ));
214
215         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
216         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
217
218         set_normalized_timespec(&xtime, sec, nsec);
219         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
220
221         time_adjust = 0;                /* stop active adjtime() */
222         time_status |= STA_UNSYNC;
223         time_maxerror = NTP_PHASE_LIMIT;
224         time_esterror = NTP_PHASE_LIMIT;
225         write_sequnlock_irq(&xtime_lock);
226         clock_was_set();
227
228         return 0;
229 }
230
231 EXPORT_SYMBOL(do_settimeofday);
232
233 /* last time the RTC clock got updated */
234 static long last_rtc_update;
235
236 /* Profiling definitions */
237 extern unsigned long prof_cpu_mask;
238 extern unsigned int * prof_buffer;
239 extern unsigned long prof_len;
240 extern unsigned long prof_shift;
241 extern char _stext;
242
243 static inline void sh_do_profile(unsigned long pc)
244 {
245         /* Don't profile cpu_idle.. */
246         if (!prof_buffer || !current->pid)
247                 return;
248
249         if (pc >= 0xa0000000UL && pc < 0xc0000000UL)
250                 pc -= 0x20000000;
251
252         pc -= (unsigned long)&_stext;
253         pc >>= prof_shift;
254
255         /*
256          * Don't ignore out-of-bounds PC values silently,
257          * put them into the last histogram slot, so if
258          * present, they will show up as a sharp peak.
259          */
260         if (pc > prof_len - 1)
261                 pc = prof_len - 1;
262
263         atomic_inc((atomic_t *)&prof_buffer[pc]);
264 }
265
266 /*
267  * timer_interrupt() needs to keep up the real-time clock,
268  * as well as call the "do_timer()" routine every clocktick
269  */
270 static inline void do_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
271 {
272         do_timer(regs);
273
274         if (!user_mode(regs))
275                 sh_do_profile(regs->pc);
276
277 #ifdef CONFIG_HEARTBEAT
278         if (sh_mv.mv_heartbeat != NULL) 
279                 sh_mv.mv_heartbeat();
280 #endif
281
282         /*
283          * If we have an externally synchronized Linux clock, then update
284          * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
285          * called as close as possible to 500 ms before the new second starts.
286          */
287         if ((time_status & STA_UNSYNC) == 0 &&
288             xtime.tv_sec > last_rtc_update + 660 &&
289             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
290             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
291                 if (rtc_set_time(xtime.tv_sec) == 0)
292                         last_rtc_update = xtime.tv_sec;
293                 else
294                         last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
295         }
296 }
297
298 /*
299  * This is the same as the above, except we _also_ save the current
300  * Time Stamp Counter value at the time of the timer interrupt, so that
301  * we later on can estimate the time of day more exactly.
302  */
303 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
304 {
305         unsigned long timer_status;
306
307         /* Clear UNF bit */
308         timer_status = ctrl_inw(TMU0_TCR);
309         timer_status &= ~0x100;
310         ctrl_outw(timer_status, TMU0_TCR);
311
312         /*
313          * Here we are in the timer irq handler. We just have irqs locally
314          * disabled but we don't know if the timer_bh is running on the other
315          * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
316          * the irq version of write_lock because as just said we have irq
317          * locally disabled. -arca
318          */
319         write_seqlock(&xtime_lock);
320         do_timer_interrupt(irq, NULL, regs);
321         write_sequnlock(&xtime_lock);
322
323         return IRQ_HANDLED;
324 }
325
326 /*
327  * Hah!  We'll see if this works (switching from usecs to nsecs).
328  */
329 static unsigned int __init get_timer_frequency(void)
330 {
331         u32 freq;
332         struct timespec ts1, ts2;
333         unsigned long diff_nsec;
334         unsigned long factor;
335
336         /* Setup the timer:  We don't want to generate interrupts, just
337          * have it count down at its natural rate.
338          */
339         ctrl_outb(0, TMU_TSTR);
340         ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
341         ctrl_outw(TMU0_TCR_CALIB, TMU0_TCR);
342         ctrl_outl(0xffffffff, TMU0_TCOR);
343         ctrl_outl(0xffffffff, TMU0_TCNT);
344
345         rtc_get_time(&ts2);
346
347         do {
348                 rtc_get_time(&ts1);
349         } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
350
351         /* actually start the timer */
352         ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
353
354         do {
355                 rtc_get_time(&ts2);
356         } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
357
358         freq = 0xffffffff - ctrl_inl(TMU0_TCNT);
359         if (ts2.tv_nsec < ts1.tv_nsec) {
360                 ts2.tv_nsec += 1000000000;
361                 ts2.tv_sec--;
362         }
363
364         diff_nsec = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
365
366         /* this should work well if the RTC has a precision of n Hz, where
367          * n is an integer.  I don't think we have to worry about the other
368          * cases. */
369         factor = (1000000000 + diff_nsec/2) / diff_nsec;
370
371         if (factor * diff_nsec > 1100000000 ||
372             factor * diff_nsec <  900000000)
373                 panic("weird RTC (diff_nsec %ld)", diff_nsec);
374
375         return freq * factor;
376 }
377
378 void (*board_time_init)(void) = 0;
379 void (*board_timer_setup)(struct irqaction *irq) = 0;
380
381 static unsigned int sh_pclk_freq __initdata = CONFIG_SH_PCLK_FREQ;
382
383 static int __init sh_pclk_setup(char *str)
384 {
385         unsigned int freq;
386
387         if (get_option(&str, &freq))
388                 sh_pclk_freq = freq;
389
390         return 1;
391 }
392 __setup("sh_pclk=", sh_pclk_setup);
393
394 static struct irqaction irq0  = { timer_interrupt, SA_INTERRUPT, 0, "timer", NULL, NULL};
395
396 void get_current_frequency_divisors(unsigned int *ifc, unsigned int *bfc, unsigned int *pfc)
397 {
398         unsigned int frqcr = ctrl_inw(FRQCR);
399
400 #if defined(CONFIG_CPU_SH3)
401         unsigned int tmp;
402
403         tmp  = (frqcr & 0x8000) >> 13;
404         tmp |= (frqcr & 0x0030) >>  4;
405         *bfc = stc_multipliers[tmp];
406         tmp  = (frqcr & 0x4000)  >> 12;
407         tmp |= (frqcr & 0x000c) >> 2;
408         *ifc = ifc_divisors[tmp];
409         tmp  = (frqcr & 0x2000) >> 11;
410         tmp |= frqcr & 0x0003;
411         *pfc = pfc_divisors[tmp];
412 #elif defined(CONFIG_CPU_SH4)
413         *ifc = ifc_divisors[(frqcr >> 6) & 0x0007];
414         *bfc = bfc_divisors[(frqcr >> 3) & 0x0007];
415         *pfc = pfc_divisors[frqcr & 0x0007];
416 #endif
417 }
418
419 /*
420  * This bit of ugliness builds up accessor routines to get at both
421  * the divisors and the physical values.
422  */
423 #define _FREQ_TABLE(x) \
424         unsigned int get_##x##_divisor(unsigned int value)      \
425                 { return x##_divisors[value]; }                 \
426                                                                 \
427         unsigned int get_##x##_value(unsigned int divisor)      \
428                 { return x##_values[(divisor - 1)]; }
429
430 _FREQ_TABLE(ifc);
431 _FREQ_TABLE(bfc);
432 _FREQ_TABLE(pfc);
433
434 void __init time_init(void)
435 {
436         unsigned int timer_freq = 0;
437         unsigned int ifc, pfc, bfc;
438         unsigned long interval;
439
440         if (board_time_init)
441                 board_time_init();
442
443         get_current_frequency_divisors(&ifc, &bfc, &pfc);
444
445         /*
446          * If we don't have an RTC (such as with the SH7300), don't attempt to
447          * probe the timer frequency. Rely on an either hardcoded peripheral
448          * clock value, or on the sh_pclk command line option.
449          */
450         current_cpu_data.module_clock = sh_pclk_freq;
451
452         /* XXX: Switch this over to a more generic test. */
453         if (current_cpu_data.type != CPU_SH7300) {
454                 unsigned int freq;
455
456                 /* 
457                  * If we've specified a peripheral clock frequency, and we have
458                  * an RTC, compare it against the autodetected value. Complain
459                  * if there's a mismatch.
460                  *
461                  * Note: We should allow for some high and low watermarks for
462                  * the frequency here (compensating for potential drift), as
463                  * otherwise we'll likely end up triggering this essentially
464                  * on every boot.
465                  */
466                 timer_freq = get_timer_frequency();
467                 freq = timer_freq * 4;
468
469                 if (sh_pclk_freq && sh_pclk_freq != freq) {
470                         printk(KERN_NOTICE "Calculated peripheral clock value "
471                                "%d differs from sh_pclk value %d, fixing..\n",
472                                freq, sh_pclk_freq);
473                         current_cpu_data.module_clock = freq;
474                 }
475         }
476
477         rtc_get_time(&xtime);
478
479         set_normalized_timespec(&wall_to_monotonic,
480                                 -xtime.tv_sec, -xtime.tv_nsec);
481
482         if (board_timer_setup) {
483                 board_timer_setup(&irq0);
484         } else {
485                 setup_irq(TIMER_IRQ, &irq0);
486         }
487
488         if (!current_cpu_data.master_clock)
489                 current_cpu_data.master_clock = current_cpu_data.module_clock * pfc;
490         if (!current_cpu_data.bus_clock)
491                 current_cpu_data.bus_clock = current_cpu_data.master_clock / bfc;
492         if (!current_cpu_data.cpu_clock)
493                 current_cpu_data.cpu_clock = current_cpu_data.master_clock / ifc;
494
495         printk("CPU clock: %d.%02dMHz\n",
496                (current_cpu_data.cpu_clock / 1000000),
497                (current_cpu_data.cpu_clock % 1000000)/10000);
498         printk("Bus clock: %d.%02dMHz\n",
499                (current_cpu_data.bus_clock / 1000000),
500                (current_cpu_data.bus_clock % 1000000)/10000);
501 #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
502         printk("Memory clock: %d.%02dMHz\n",
503                (current_cpu_data.memory_clock / 1000000),
504                (current_cpu_data.memory_clock % 1000000)/10000);
505 #endif
506         printk("Module clock: %d.%02dMHz\n",
507                (current_cpu_data.module_clock / 1000000),
508                (current_cpu_data.module_clock % 1000000)/10000);
509         interval = (current_cpu_data.module_clock/4 + HZ/2) / HZ;
510
511         printk("Interval = %ld\n", interval);
512
513         /* Start TMU0 */
514         ctrl_outb(0, TMU_TSTR);
515         ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
516         ctrl_outw(TMU0_TCR_INIT, TMU0_TCR);
517         ctrl_outl(interval, TMU0_TCOR);
518         ctrl_outl(interval, TMU0_TCNT);
519         ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
520
521 #if defined(CONFIG_SH_KGDB)
522         /*
523          * Set up kgdb as requested. We do it here because the serial
524          * init uses the timer vars we just set up for figuring baud.
525          */
526         kgdb_init();
527 #endif
528 }