2 * linux/arch/m32r/kernel/time.c
4 * Copyright (c) 2001, 2002 Hiroyuki Kondo, Hirokazu Takata,
6 * Taken from i386 version.
7 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
8 * Copyright (C) 1996, 1997, 1998 Ralf Baechle
10 * This file contains the time handling details for PC-style clocks as
11 * found in some MIPS systems.
13 * Some code taken from sh version.
14 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
15 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/param.h>
26 #include <linux/string.h>
28 #include <linux/interrupt.h>
29 #include <linux/profile.h>
34 #include <asm/hw_irq.h>
37 extern void send_IPI_allbutself(int, int);
38 extern void smp_local_timer_interrupt(struct pt_regs *);
41 #define TICK_SIZE (tick_nsec / 1000)
44 * Change this if you have some constant time drift
47 /* This is for machines which generate the exact clock. */
48 #define USECS_PER_JIFFY (1000000/HZ)
50 static unsigned long latch;
52 static unsigned long do_gettimeoffset(void)
54 unsigned long elapsed_time = 0; /* [us] */
56 #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
57 || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
58 || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
63 /* timer count may underflow right here */
64 count = inl(M32R_MFT2CUT_PORTL);
66 if (inl(M32R_ICU_CR18_PORTL) & 0x00000100) /* underflow check */
69 count = (latch - count) * TICK_SIZE;
70 elapsed_time = (count + latch / 2) / latch;
71 /* NOTE: LATCH is equal to the "interval" value (= reload count). */
73 #else /* CONFIG_SMP */
75 static unsigned long p_jiffies = -1;
76 static unsigned long p_count = 0;
78 /* timer count may underflow right here */
79 count = inl(M32R_MFT2CUT_PORTL);
81 if (jiffies == p_jiffies && count > p_count)
87 count = (latch - count) * TICK_SIZE;
88 elapsed_time = (count + latch / 2) / latch;
89 /* NOTE: LATCH is equal to the "interval" value (= reload count). */
90 #endif /* CONFIG_SMP */
91 #elif defined(CONFIG_CHIP_M32310)
92 #warning do_gettimeoffse not implemented
94 #error no chip configuration
101 * This version of gettimeofday has near microsecond resolution.
103 void do_gettimeofday(struct timeval *tv)
106 unsigned long usec, sec;
107 unsigned long max_ntp_tick = tick_usec - tickadj;
110 seq = read_seqbegin(&xtime_lock);
112 usec = do_gettimeoffset();
115 * If time_adjust is negative then NTP is slowing the clock
116 * so make sure not to go into next possible interval.
117 * Better to lose some accuracy than have time go backwards..
119 if (unlikely(time_adjust < 0))
120 usec = min(usec, max_ntp_tick);
123 usec += (xtime.tv_nsec / 1000);
124 } while (read_seqretry(&xtime_lock, seq));
126 while (usec >= 1000000) {
135 EXPORT_SYMBOL(do_gettimeofday);
137 int do_settimeofday(struct timespec *tv)
139 time_t wtm_sec, sec = tv->tv_sec;
140 long wtm_nsec, nsec = tv->tv_nsec;
142 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
145 write_seqlock_irq(&xtime_lock);
147 * This is revolting. We need to set "xtime" correctly. However, the
148 * value in this location is the value at the most recent update of
149 * wall time. Discover what correction gettimeofday() would have
150 * made, and then undo it!
152 nsec -= do_gettimeoffset() * NSEC_PER_USEC;
154 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
155 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
157 set_normalized_timespec(&xtime, sec, nsec);
158 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
161 write_sequnlock_irq(&xtime_lock);
167 EXPORT_SYMBOL(do_settimeofday);
170 * In order to set the CMOS clock precisely, set_rtc_mmss has to be
171 * called 500 ms after the second nowtime has started, because when
172 * nowtime is written into the registers of the CMOS clock, it will
173 * jump to the next second precisely 500 ms later. Check the Motorola
174 * MC146818A or Dallas DS12887 data sheet for details.
176 * BUG: This routine does not handle hour overflow properly; it just
177 * sets the minutes. Usually you won't notice until after reboot!
179 static inline int set_rtc_mmss(unsigned long nowtime)
184 /* last time the cmos clock got updated */
185 static long last_rtc_update = 0;
188 * timer_interrupt() needs to keep up the real-time clock,
189 * as well as call the "do_timer()" routine every clocktick
191 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
194 profile_tick(CPU_PROFILING, regs);
199 update_process_times(user_mode(regs));
202 * If we have an externally synchronized Linux clock, then update
203 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
204 * called as close as possible to 500 ms before the new second starts.
206 write_seqlock(&xtime_lock);
208 && xtime.tv_sec > last_rtc_update + 660
209 && (xtime.tv_nsec / 1000) >= 500000 - ((unsigned)TICK_SIZE) / 2
210 && (xtime.tv_nsec / 1000) <= 500000 + ((unsigned)TICK_SIZE) / 2)
212 if (set_rtc_mmss(xtime.tv_sec) == 0)
213 last_rtc_update = xtime.tv_sec;
214 else /* do it again in 60 s */
215 last_rtc_update = xtime.tv_sec - 600;
217 write_sequnlock(&xtime_lock);
218 /* As we return to user mode fire off the other CPU schedulers..
219 this is basically because we don't yet share IRQ's around.
220 This message is rigged to be safe on the 386 - basically it's
221 a hack, so don't look closely for now.. */
224 smp_local_timer_interrupt(regs);
231 struct irqaction irq0 = { timer_interrupt, IRQF_DISABLED, CPU_MASK_NONE,
232 "MFT2", NULL, NULL };
234 void __init time_init(void)
236 unsigned int epoch, year, mon, day, hour, min, sec;
238 sec = min = hour = day = mon = year = 0;
245 /* Attempt to guess the epoch. This is the same heuristic as in rtc.c
246 so no stupid things will happen to timekeeping. Who knows, maybe
247 Ultrix also uses 1952 as epoch ... */
248 if (year > 10 && year < 44)
254 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
255 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
256 set_normalized_timespec(&wall_to_monotonic,
257 -xtime.tv_sec, -xtime.tv_nsec);
259 #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
260 || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
261 || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
263 /* M32102 MFT setup */
264 setup_irq(M32R_IRQ_MFT2, &irq0);
266 unsigned long bus_clock;
267 unsigned short divide;
269 bus_clock = boot_cpu_data.bus_clock;
270 divide = boot_cpu_data.timer_divide;
271 latch = (bus_clock/divide + HZ / 2) / HZ;
273 printk("Timer start : latch = %ld\n", latch);
275 outl((M32R_MFTMOD_CC_MASK | M32R_MFTMOD_TCCR \
276 |M32R_MFTMOD_CSSEL011), M32R_MFT2MOD_PORTL);
277 outl(latch, M32R_MFT2RLD_PORTL);
278 outl(latch, M32R_MFT2CUT_PORTL);
279 outl(0, M32R_MFT2CMPRLD_PORTL);
280 outl((M32R_MFTCR_MFT2MSK|M32R_MFTCR_MFT2EN), M32R_MFTCR_PORTL);
283 #elif defined(CONFIG_CHIP_M32310)
284 #warning time_init not implemented
286 #error no chip configuration
291 * Scheduler clock - returns current time in nanosec units.
293 unsigned long long sched_clock(void)
295 return (unsigned long long)jiffies * (1000000000 / HZ);