Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/arch/m32r/kernel/time.c | |
3 | * | |
4 | * Copyright (c) 2001, 2002 Hiroyuki Kondo, Hirokazu Takata, | |
5 | * Hitoshi Yamamoto | |
6 | * Taken from i386 version. | |
7 | * Copyright (C) 1991, 1992, 1995 Linus Torvalds | |
8 | * Copyright (C) 1996, 1997, 1998 Ralf Baechle | |
9 | * | |
10 | * This file contains the time handling details for PC-style clocks as | |
11 | * found in some MIPS systems. | |
12 | * | |
13 | * Some code taken from sh version. | |
14 | * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka | |
15 | * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org> | |
16 | */ | |
17 | ||
18 | #undef DEBUG_TIMER | |
19 | ||
1da177e4 LT |
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> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/interrupt.h> | |
29 | #include <linux/profile.h> | |
30 | ||
31 | #include <asm/io.h> | |
32 | #include <asm/m32r.h> | |
33 | ||
34 | #include <asm/hw_irq.h> | |
35 | ||
36 | #ifdef CONFIG_SMP | |
37 | extern void send_IPI_allbutself(int, int); | |
38 | extern void smp_local_timer_interrupt(struct pt_regs *); | |
39 | #endif | |
40 | ||
1da177e4 LT |
41 | extern unsigned long wall_jiffies; |
42 | #define TICK_SIZE (tick_nsec / 1000) | |
43 | ||
44 | /* | |
45 | * Change this if you have some constant time drift | |
46 | */ | |
47 | ||
48 | /* This is for machines which generate the exact clock. */ | |
49 | #define USECS_PER_JIFFY (1000000/HZ) | |
50 | ||
51 | static unsigned long latch; | |
52 | ||
53 | static unsigned long do_gettimeoffset(void) | |
54 | { | |
55 | unsigned long elapsed_time = 0; /* [us] */ | |
56 | ||
57 | #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \ | |
58 | || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \ | |
9287d95e | 59 | || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104) |
1da177e4 LT |
60 | #ifndef CONFIG_SMP |
61 | ||
62 | unsigned long count; | |
63 | ||
64 | /* timer count may underflow right here */ | |
65 | count = inl(M32R_MFT2CUT_PORTL); | |
66 | ||
67 | if (inl(M32R_ICU_CR18_PORTL) & 0x00000100) /* underflow check */ | |
68 | count = 0; | |
69 | ||
70 | count = (latch - count) * TICK_SIZE; | |
71 | elapsed_time = (count + latch / 2) / latch; | |
72 | /* NOTE: LATCH is equal to the "interval" value (= reload count). */ | |
73 | ||
74 | #else /* CONFIG_SMP */ | |
75 | unsigned long count; | |
76 | static unsigned long p_jiffies = -1; | |
77 | static unsigned long p_count = 0; | |
78 | ||
79 | /* timer count may underflow right here */ | |
80 | count = inl(M32R_MFT2CUT_PORTL); | |
81 | ||
82 | if (jiffies == p_jiffies && count > p_count) | |
83 | count = 0; | |
84 | ||
85 | p_jiffies = jiffies; | |
86 | p_count = count; | |
87 | ||
88 | count = (latch - count) * TICK_SIZE; | |
89 | elapsed_time = (count + latch / 2) / latch; | |
90 | /* NOTE: LATCH is equal to the "interval" value (= reload count). */ | |
91 | #endif /* CONFIG_SMP */ | |
92 | #elif defined(CONFIG_CHIP_M32310) | |
93 | #warning do_gettimeoffse not implemented | |
94 | #else | |
95 | #error no chip configuration | |
96 | #endif | |
97 | ||
98 | return elapsed_time; | |
99 | } | |
100 | ||
101 | /* | |
102 | * This version of gettimeofday has near microsecond resolution. | |
103 | */ | |
104 | void do_gettimeofday(struct timeval *tv) | |
105 | { | |
106 | unsigned long seq; | |
107 | unsigned long usec, sec; | |
108 | unsigned long max_ntp_tick = tick_usec - tickadj; | |
109 | ||
110 | do { | |
111 | unsigned long lost; | |
112 | ||
113 | seq = read_seqbegin(&xtime_lock); | |
114 | ||
115 | usec = do_gettimeoffset(); | |
116 | lost = jiffies - wall_jiffies; | |
117 | ||
118 | /* | |
119 | * If time_adjust is negative then NTP is slowing the clock | |
120 | * so make sure not to go into next possible interval. | |
121 | * Better to lose some accuracy than have time go backwards.. | |
122 | */ | |
123 | if (unlikely(time_adjust < 0)) { | |
124 | usec = min(usec, max_ntp_tick); | |
125 | if (lost) | |
126 | usec += lost * max_ntp_tick; | |
127 | } else if (unlikely(lost)) | |
128 | usec += lost * tick_usec; | |
129 | ||
130 | sec = xtime.tv_sec; | |
131 | usec += (xtime.tv_nsec / 1000); | |
132 | } while (read_seqretry(&xtime_lock, seq)); | |
133 | ||
134 | while (usec >= 1000000) { | |
135 | usec -= 1000000; | |
136 | sec++; | |
137 | } | |
138 | ||
139 | tv->tv_sec = sec; | |
140 | tv->tv_usec = usec; | |
141 | } | |
142 | ||
143 | EXPORT_SYMBOL(do_gettimeofday); | |
144 | ||
145 | int do_settimeofday(struct timespec *tv) | |
146 | { | |
147 | time_t wtm_sec, sec = tv->tv_sec; | |
148 | long wtm_nsec, nsec = tv->tv_nsec; | |
149 | ||
150 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | |
151 | return -EINVAL; | |
152 | ||
153 | write_seqlock_irq(&xtime_lock); | |
154 | /* | |
155 | * This is revolting. We need to set "xtime" correctly. However, the | |
156 | * value in this location is the value at the most recent update of | |
157 | * wall time. Discover what correction gettimeofday() would have | |
158 | * made, and then undo it! | |
159 | */ | |
160 | nsec -= do_gettimeoffset() * NSEC_PER_USEC; | |
161 | nsec -= (jiffies - wall_jiffies) * TICK_NSEC; | |
162 | ||
163 | wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); | |
164 | wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); | |
165 | ||
166 | set_normalized_timespec(&xtime, sec, nsec); | |
167 | set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); | |
168 | ||
b149ee22 | 169 | ntp_clear(); |
1da177e4 LT |
170 | write_sequnlock_irq(&xtime_lock); |
171 | clock_was_set(); | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | EXPORT_SYMBOL(do_settimeofday); | |
177 | ||
178 | /* | |
179 | * In order to set the CMOS clock precisely, set_rtc_mmss has to be | |
180 | * called 500 ms after the second nowtime has started, because when | |
181 | * nowtime is written into the registers of the CMOS clock, it will | |
182 | * jump to the next second precisely 500 ms later. Check the Motorola | |
183 | * MC146818A or Dallas DS12887 data sheet for details. | |
184 | * | |
185 | * BUG: This routine does not handle hour overflow properly; it just | |
186 | * sets the minutes. Usually you won't notice until after reboot! | |
187 | */ | |
188 | static inline int set_rtc_mmss(unsigned long nowtime) | |
189 | { | |
190 | return 0; | |
191 | } | |
192 | ||
193 | /* last time the cmos clock got updated */ | |
194 | static long last_rtc_update = 0; | |
195 | ||
196 | /* | |
197 | * timer_interrupt() needs to keep up the real-time clock, | |
198 | * as well as call the "do_timer()" routine every clocktick | |
199 | */ | |
2757a71c | 200 | irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs) |
1da177e4 LT |
201 | { |
202 | #ifndef CONFIG_SMP | |
203 | profile_tick(CPU_PROFILING, regs); | |
204 | #endif | |
3171a030 | 205 | do_timer(1); |
1da177e4 LT |
206 | |
207 | #ifndef CONFIG_SMP | |
208 | update_process_times(user_mode(regs)); | |
209 | #endif | |
210 | /* | |
211 | * If we have an externally synchronized Linux clock, then update | |
212 | * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be | |
213 | * called as close as possible to 500 ms before the new second starts. | |
214 | */ | |
2757a71c | 215 | write_seqlock(&xtime_lock); |
b149ee22 | 216 | if (ntp_synced() |
1da177e4 LT |
217 | && xtime.tv_sec > last_rtc_update + 660 |
218 | && (xtime.tv_nsec / 1000) >= 500000 - ((unsigned)TICK_SIZE) / 2 | |
219 | && (xtime.tv_nsec / 1000) <= 500000 + ((unsigned)TICK_SIZE) / 2) | |
220 | { | |
221 | if (set_rtc_mmss(xtime.tv_sec) == 0) | |
222 | last_rtc_update = xtime.tv_sec; | |
223 | else /* do it again in 60 s */ | |
224 | last_rtc_update = xtime.tv_sec - 600; | |
225 | } | |
2757a71c | 226 | write_sequnlock(&xtime_lock); |
1da177e4 LT |
227 | /* As we return to user mode fire off the other CPU schedulers.. |
228 | this is basically because we don't yet share IRQ's around. | |
229 | This message is rigged to be safe on the 386 - basically it's | |
230 | a hack, so don't look closely for now.. */ | |
231 | ||
232 | #ifdef CONFIG_SMP | |
233 | smp_local_timer_interrupt(regs); | |
2757a71c | 234 | smp_send_timer(); |
1da177e4 | 235 | #endif |
1da177e4 LT |
236 | |
237 | return IRQ_HANDLED; | |
238 | } | |
239 | ||
aff60147 | 240 | struct irqaction irq0 = { timer_interrupt, IRQF_DISABLED, CPU_MASK_NONE, |
1da177e4 LT |
241 | "MFT2", NULL, NULL }; |
242 | ||
243 | void __init time_init(void) | |
244 | { | |
245 | unsigned int epoch, year, mon, day, hour, min, sec; | |
246 | ||
247 | sec = min = hour = day = mon = year = 0; | |
248 | epoch = 0; | |
249 | ||
250 | year = 23; | |
251 | mon = 4; | |
252 | day = 17; | |
253 | ||
254 | /* Attempt to guess the epoch. This is the same heuristic as in rtc.c | |
255 | so no stupid things will happen to timekeeping. Who knows, maybe | |
256 | Ultrix also uses 1952 as epoch ... */ | |
257 | if (year > 10 && year < 44) | |
258 | epoch = 1980; | |
259 | else if (year < 96) | |
260 | epoch = 1952; | |
261 | year += epoch; | |
262 | ||
263 | xtime.tv_sec = mktime(year, mon, day, hour, min, sec); | |
264 | xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ); | |
265 | set_normalized_timespec(&wall_to_monotonic, | |
266 | -xtime.tv_sec, -xtime.tv_nsec); | |
267 | ||
268 | #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \ | |
269 | || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \ | |
9287d95e | 270 | || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104) |
1da177e4 LT |
271 | |
272 | /* M32102 MFT setup */ | |
273 | setup_irq(M32R_IRQ_MFT2, &irq0); | |
274 | { | |
275 | unsigned long bus_clock; | |
276 | unsigned short divide; | |
277 | ||
278 | bus_clock = boot_cpu_data.bus_clock; | |
279 | divide = boot_cpu_data.timer_divide; | |
280 | latch = (bus_clock/divide + HZ / 2) / HZ; | |
281 | ||
282 | printk("Timer start : latch = %ld\n", latch); | |
283 | ||
284 | outl((M32R_MFTMOD_CC_MASK | M32R_MFTMOD_TCCR \ | |
285 | |M32R_MFTMOD_CSSEL011), M32R_MFT2MOD_PORTL); | |
286 | outl(latch, M32R_MFT2RLD_PORTL); | |
287 | outl(latch, M32R_MFT2CUT_PORTL); | |
288 | outl(0, M32R_MFT2CMPRLD_PORTL); | |
289 | outl((M32R_MFTCR_MFT2MSK|M32R_MFTCR_MFT2EN), M32R_MFTCR_PORTL); | |
290 | } | |
291 | ||
292 | #elif defined(CONFIG_CHIP_M32310) | |
293 | #warning time_init not implemented | |
294 | #else | |
295 | #error no chip configuration | |
296 | #endif | |
297 | } | |
298 | ||
299 | /* | |
300 | * Scheduler clock - returns current time in nanosec units. | |
301 | */ | |
302 | unsigned long long sched_clock(void) | |
303 | { | |
304 | return (unsigned long long)jiffies * (1000000000 / HZ); | |
305 | } |