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