blob: f0a2874b04e9b1665c91de2ff052bc4a7189ec7f [file] [log] [blame]
David S. Miller64d329e2007-10-27 00:17:01 -07001/* linux/arch/sparc/kernel/time.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Miller64d329e2007-10-27 00:17:01 -07003 * Copyright (C) 1995 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
5 *
6 * Chris Davis (cdavis@cois.on.ca) 03/27/1998
7 * Added support for the intersil on the sun4/4200
8 *
9 * Gleb Raiko (rajko@mech.math.msu.su) 08/18/1998
10 * Support for MicroSPARC-IIep, PCI CPU.
11 *
12 * This file handles the Sparc specific time handling details.
13 *
14 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
15 * "A Kernel Model for Precision Timekeeping" by Dave Mills
16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/module.h>
19#include <linux/sched.h>
20#include <linux/kernel.h>
21#include <linux/param.h>
22#include <linux/string.h>
23#include <linux/mm.h>
24#include <linux/interrupt.h>
25#include <linux/time.h>
26#include <linux/timex.h>
27#include <linux/init.h>
28#include <linux/pci.h>
29#include <linux/ioport.h>
30#include <linux/profile.h>
David S. Miller454eeb22008-08-27 04:05:35 -070031#include <linux/of.h>
Stephen Rothwell764f2572008-08-07 15:33:36 -070032#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/oplib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/timer.h>
36#include <asm/mostek.h>
37#include <asm/system.h>
38#include <asm/irq.h>
39#include <asm/io.h>
40#include <asm/idprom.h>
41#include <asm/machines.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/page.h>
43#include <asm/pcic.h>
Al Viro0d844382006-10-08 14:30:44 +010044#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Al Viro32231a62007-07-21 19:18:57 -070046#include "irq.h"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048DEFINE_SPINLOCK(rtc_lock);
Adrian Bunkc61c65c2008-06-05 11:40:58 -070049static enum sparc_clock_type sp_clock_typ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050DEFINE_SPINLOCK(mostek_lock);
51void __iomem *mstk48t02_regs = NULL;
Al Viroc316ef02005-12-06 05:55:44 -050052static struct mostek48t08 __iomem *mstk48t08_regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int set_rtc_mmss(unsigned long);
54static int sbus_do_settimeofday(struct timespec *tv);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056unsigned long profile_pc(struct pt_regs *regs)
57{
58 extern char __copy_user_begin[], __copy_user_end[];
59 extern char __atomic_begin[], __atomic_end[];
60 extern char __bzero_begin[], __bzero_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 unsigned long pc = regs->pc;
63
64 if (in_lock_functions(pc) ||
65 (pc >= (unsigned long) __copy_user_begin &&
66 pc < (unsigned long) __copy_user_end) ||
67 (pc >= (unsigned long) __atomic_begin &&
68 pc < (unsigned long) __atomic_end) ||
69 (pc >= (unsigned long) __bzero_begin &&
David S. Miller8a8b8362006-12-17 16:18:47 -080070 pc < (unsigned long) __bzero_end))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 pc = regs->u_regs[UREG_RETPC];
72 return pc;
73}
74
Martin Habets9550e592006-10-17 19:21:48 -070075EXPORT_SYMBOL(profile_pc);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077__volatile__ unsigned int *master_l10_counter;
78__volatile__ unsigned int *master_l10_limit;
79
80/*
81 * timer_interrupt() needs to keep up the real-time clock,
82 * as well as call the "do_timer()" routine every clocktick
83 */
84
85#define TICK_SIZE (tick_nsec / 1000)
86
Jeff Garzik5dc07422007-10-31 05:08:48 -040087static irqreturn_t timer_interrupt(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 /* last time the cmos clock got updated */
90 static long last_rtc_update;
91
92#ifndef CONFIG_SMP
Al Viro0d844382006-10-08 14:30:44 +010093 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#endif
95
96 /* Protect counter clear so that do_gettimeoffset works */
97 write_seqlock(&xtime_lock);
Adrian Bunk5110bd22008-08-31 20:59:37 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 clear_clock_irq();
100
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700101 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* Determine when to update the Mostek clock. */
john stultzb149ee22005-09-06 15:17:46 -0700104 if (ntp_synced() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 xtime.tv_sec > last_rtc_update + 660 &&
106 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
107 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
108 if (set_rtc_mmss(xtime.tv_sec) == 0)
109 last_rtc_update = xtime.tv_sec;
110 else
111 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
112 }
113 write_sequnlock(&xtime_lock);
114
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100115#ifndef CONFIG_SMP
116 update_process_times(user_mode(get_irq_regs()));
117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return IRQ_HANDLED;
119}
120
121/* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
Krzysztof Helt42e28262007-05-19 12:03:24 -0700122static void __devinit kick_start_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
125 unsigned char sec;
126 int i, count;
127
128 prom_printf("CLOCK: Clock was stopped. Kick start ");
129
130 spin_lock_irq(&mostek_lock);
131
132 /* Turn on the kick start bit to start the oscillator. */
133 regs->creg |= MSTK_CREG_WRITE;
134 regs->sec &= ~MSTK_STOP;
135 regs->hour |= MSTK_KICK_START;
136 regs->creg &= ~MSTK_CREG_WRITE;
137
138 spin_unlock_irq(&mostek_lock);
139
140 /* Delay to allow the clock oscillator to start. */
141 sec = MSTK_REG_SEC(regs);
142 for (i = 0; i < 3; i++) {
143 while (sec == MSTK_REG_SEC(regs))
144 for (count = 0; count < 100000; count++)
145 /* nothing */ ;
146 prom_printf(".");
147 sec = regs->sec;
148 }
149 prom_printf("\n");
150
151 spin_lock_irq(&mostek_lock);
152
153 /* Turn off kick start and set a "valid" time and date. */
154 regs->creg |= MSTK_CREG_WRITE;
155 regs->hour &= ~MSTK_KICK_START;
156 MSTK_SET_REG_SEC(regs,0);
157 MSTK_SET_REG_MIN(regs,0);
158 MSTK_SET_REG_HOUR(regs,0);
159 MSTK_SET_REG_DOW(regs,5);
160 MSTK_SET_REG_DOM(regs,1);
161 MSTK_SET_REG_MONTH(regs,8);
162 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
163 regs->creg &= ~MSTK_CREG_WRITE;
164
165 spin_unlock_irq(&mostek_lock);
166
167 /* Ensure the kick start bit is off. If it isn't, turn it off. */
168 while (regs->hour & MSTK_KICK_START) {
169 prom_printf("CLOCK: Kick start still on!\n");
170
171 spin_lock_irq(&mostek_lock);
172 regs->creg |= MSTK_CREG_WRITE;
173 regs->hour &= ~MSTK_KICK_START;
174 regs->creg &= ~MSTK_CREG_WRITE;
175 spin_unlock_irq(&mostek_lock);
176 }
177
178 prom_printf("CLOCK: Kick start procedure successful.\n");
179}
180
181/* Return nonzero if the clock chip battery is low. */
David S. Miller64d329e2007-10-27 00:17:01 -0700182static inline int has_low_battery(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
185 unsigned char data1, data2;
186
187 spin_lock_irq(&mostek_lock);
188 data1 = regs->eeprom[0]; /* Read some data. */
189 regs->eeprom[0] = ~data1; /* Write back the complement. */
190 data2 = regs->eeprom[0]; /* Read back the complement. */
191 regs->eeprom[0] = data1; /* Restore the original value. */
192 spin_unlock_irq(&mostek_lock);
193
194 return (data1 == data2); /* Was the write blocked? */
195}
196
Krzysztof Helt42e28262007-05-19 12:03:24 -0700197static void __devinit mostek_set_system_time(void)
Bob Breuer96ba9892006-07-27 22:08:01 -0700198{
199 unsigned int year, mon, day, hour, min, sec;
200 struct mostek48t02 *mregs;
201
202 mregs = (struct mostek48t02 *)mstk48t02_regs;
203 if(!mregs) {
204 prom_printf("Something wrong, clock regs not mapped yet.\n");
205 prom_halt();
206 }
207 spin_lock_irq(&mostek_lock);
208 mregs->creg |= MSTK_CREG_READ;
209 sec = MSTK_REG_SEC(mregs);
210 min = MSTK_REG_MIN(mregs);
211 hour = MSTK_REG_HOUR(mregs);
212 day = MSTK_REG_DOM(mregs);
213 mon = MSTK_REG_MONTH(mregs);
214 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
215 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
216 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
217 set_normalized_timespec(&wall_to_monotonic,
218 -xtime.tv_sec, -xtime.tv_nsec);
219 mregs->creg &= ~MSTK_CREG_READ;
220 spin_unlock_irq(&mostek_lock);
221}
222
David S. Milleree5caf02006-06-29 14:36:52 -0700223static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
David S. Milleree5caf02006-06-29 14:36:52 -0700225 struct device_node *dp = op->node;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700226 const char *model = of_get_property(dp, "model", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
David S. Milleree5caf02006-06-29 14:36:52 -0700228 if (!model)
229 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
David S. Milleree5caf02006-06-29 14:36:52 -0700231 if (!strcmp(model, "mk48t02")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sp_clock_typ = MSTK48T02;
David S. Milleree5caf02006-06-29 14:36:52 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Map the clock register io area read-only */
David S. Milleree5caf02006-06-29 14:36:52 -0700235 mstk48t02_regs = of_ioremap(&op->resource[0], 0,
236 sizeof(struct mostek48t02),
237 "mk48t02");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 mstk48t08_regs = NULL; /* To catch weirdness */
David S. Milleree5caf02006-06-29 14:36:52 -0700239 } else if (!strcmp(model, "mk48t08")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 sp_clock_typ = MSTK48T08;
David S. Milleree5caf02006-06-29 14:36:52 -0700241 mstk48t08_regs = of_ioremap(&op->resource[0], 0,
242 sizeof(struct mostek48t08),
243 "mk48t08");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 mstk48t02_regs = &mstk48t08_regs->regs;
David S. Milleree5caf02006-06-29 14:36:52 -0700246 } else
247 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* Report a low battery voltage condition. */
250 if (has_low_battery())
251 printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
252
253 /* Kick start the clock if it is completely stopped. */
254 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
255 kick_start_clock();
David S. Milleree5caf02006-06-29 14:36:52 -0700256
Bob Breuer96ba9892006-07-27 22:08:01 -0700257 mostek_set_system_time();
258
David S. Milleree5caf02006-06-29 14:36:52 -0700259 return 0;
260}
261
David S. Millerfd098312008-08-31 01:23:17 -0700262static struct of_device_id __initdata clock_match[] = {
David S. Milleree5caf02006-06-29 14:36:52 -0700263 {
264 .name = "eeprom",
265 },
266 {},
267};
268
269static struct of_platform_driver clock_driver = {
David S. Milleree5caf02006-06-29 14:36:52 -0700270 .match_table = clock_match,
271 .probe = clock_probe,
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700272 .driver = {
273 .name = "clock",
274 },
David S. Milleree5caf02006-06-29 14:36:52 -0700275};
276
277
278/* Probe for the mostek real time clock chip. */
Bob Breuer96ba9892006-07-27 22:08:01 -0700279static int __init clock_init(void)
David S. Milleree5caf02006-06-29 14:36:52 -0700280{
Stephen Rothwell37b77542007-04-30 17:43:56 +1000281 return of_register_driver(&clock_driver, &of_platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Bob Breuer96ba9892006-07-27 22:08:01 -0700284/* Must be after subsys_initcall() so that busses are probed. Must
285 * be before device_initcall() because things like the RTC driver
286 * need to see the clock registers.
287 */
288fs_initcall(clock_init);
Bob Breuer96ba9892006-07-27 22:08:01 -0700289
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700290static void __init sbus_time_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
294 btfixup();
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 sparc_init_timers(timer_interrupt);
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
299 local_irq_enable();
300}
301
302void __init time_init(void)
303{
304#ifdef CONFIG_PCI
305 extern void pci_time_init(void);
306 if (pcic_present()) {
307 pci_time_init();
308 return;
309 }
310#endif
311 sbus_time_init();
312}
313
Adrian Bunk31156242005-10-03 17:37:02 -0700314static inline unsigned long do_gettimeoffset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
David S. Miller000775c2008-01-09 05:09:06 -0800316 unsigned long val = *master_l10_counter;
317 unsigned long usec = (val >> 10) & 0x1fffff;
318
319 /* Limit hit? */
320 if (val & 0x80000000)
321 usec += 1000000 / HZ;
322
323 return usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/* Ok, my cute asm atomicity trick doesn't work anymore.
327 * There are just too many variables that need to be protected
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700328 * now (both members of xtime, et al.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330void do_gettimeofday(struct timeval *tv)
331{
332 unsigned long flags;
333 unsigned long seq;
334 unsigned long usec, sec;
335 unsigned long max_ntp_tick = tick_usec - tickadj;
336
337 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 seq = read_seqbegin_irqsave(&xtime_lock, flags);
339 usec = do_gettimeoffset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
342 * If time_adjust is negative then NTP is slowing the clock
343 * so make sure not to go into next possible interval.
344 * Better to lose some accuracy than have time go backwards..
345 */
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700346 if (unlikely(time_adjust < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 usec = min(usec, max_ntp_tick);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 sec = xtime.tv_sec;
350 usec += (xtime.tv_nsec / 1000);
351 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
352
353 while (usec >= 1000000) {
354 usec -= 1000000;
355 sec++;
356 }
357
358 tv->tv_sec = sec;
359 tv->tv_usec = usec;
360}
361
362EXPORT_SYMBOL(do_gettimeofday);
363
364int do_settimeofday(struct timespec *tv)
365{
366 int ret;
367
368 write_seqlock_irq(&xtime_lock);
369 ret = bus_do_settimeofday(tv);
370 write_sequnlock_irq(&xtime_lock);
371 clock_was_set();
372 return ret;
373}
374
375EXPORT_SYMBOL(do_settimeofday);
376
377static int sbus_do_settimeofday(struct timespec *tv)
378{
379 time_t wtm_sec, sec = tv->tv_sec;
380 long wtm_nsec, nsec = tv->tv_nsec;
381
382 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
383 return -EINVAL;
384
385 /*
386 * This is revolting. We need to set "xtime" correctly. However, the
387 * value in this location is the value at the most recent update of
388 * wall time. Discover what correction gettimeofday() would have
389 * made, and then undo it!
390 */
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700391 nsec -= 1000 * do_gettimeoffset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
394 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
395
396 set_normalized_timespec(&xtime, sec, nsec);
397 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
398
john stultzb149ee22005-09-06 15:17:46 -0700399 ntp_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401}
402
403/*
404 * BUG: This routine does not handle hour overflow properly; it just
405 * sets the minutes. Usually you won't notice until after reboot!
406 */
407static int set_rtc_mmss(unsigned long nowtime)
408{
409 int real_seconds, real_minutes, mostek_minutes;
410 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
411 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 spin_lock_irqsave(&mostek_lock, flags);
414 /* Read the current RTC minutes. */
415 regs->creg |= MSTK_CREG_READ;
416 mostek_minutes = MSTK_REG_MIN(regs);
417 regs->creg &= ~MSTK_CREG_READ;
418
419 /*
420 * since we're only adjusting minutes and seconds,
421 * don't interfere with hour overflow. This avoids
422 * messing with unknown time zones but requires your
423 * RTC not to be off by more than 15 minutes
424 */
425 real_seconds = nowtime % 60;
426 real_minutes = nowtime / 60;
427 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
428 real_minutes += 30; /* correct for half hour time zone */
429 real_minutes %= 60;
430
431 if (abs(real_minutes - mostek_minutes) < 30) {
432 regs->creg |= MSTK_CREG_WRITE;
433 MSTK_SET_REG_SEC(regs,real_seconds);
434 MSTK_SET_REG_MIN(regs,real_minutes);
435 regs->creg &= ~MSTK_CREG_WRITE;
436 spin_unlock_irqrestore(&mostek_lock, flags);
437 return 0;
438 } else {
439 spin_unlock_irqrestore(&mostek_lock, flags);
440 return -1;
441 }
442}