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