blob: 9631e8f4ae60cc669e47b21451dfbf195259ec13 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: time.c,v 1.60 2002/01/23 14:33:55 davem Exp $
2 * linux/arch/sparc/kernel/time.c
3 *
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
6 *
7 * Chris Davis (cdavis@cois.on.ca) 03/27/1998
8 * Added support for the intersil on the sun4/4200
9 *
10 * Gleb Raiko (rajko@mech.math.msu.su) 08/18/1998
11 * Support for MicroSPARC-IIep, PCI CPU.
12 *
13 * This file handles the Sparc specific time handling details.
14 *
15 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
16 * "A Kernel Model for Precision Timekeeping" by Dave Mills
17 */
18#include <linux/config.h>
19#include <linux/errno.h>
20#include <linux/module.h>
21#include <linux/sched.h>
22#include <linux/kernel.h>
23#include <linux/param.h>
24#include <linux/string.h>
25#include <linux/mm.h>
26#include <linux/interrupt.h>
27#include <linux/time.h>
28#include <linux/timex.h>
29#include <linux/init.h>
30#include <linux/pci.h>
31#include <linux/ioport.h>
32#include <linux/profile.h>
33
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>
42#include <asm/sun4paddr.h>
43#include <asm/page.h>
44#include <asm/pcic.h>
David S. Milleree5caf02006-06-29 14:36:52 -070045#include <asm/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47extern unsigned long wall_jiffies;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049DEFINE_SPINLOCK(rtc_lock);
50enum sparc_clock_type sp_clock_typ;
51DEFINE_SPINLOCK(mostek_lock);
52void __iomem *mstk48t02_regs = NULL;
Al Viroc316ef02005-12-06 05:55:44 -050053static struct mostek48t08 __iomem *mstk48t08_regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int set_rtc_mmss(unsigned long);
55static int sbus_do_settimeofday(struct timespec *tv);
56
57#ifdef CONFIG_SUN4
58struct intersil *intersil_clock;
59#define intersil_cmd(intersil_reg, intsil_cmd) intersil_reg->int_cmd_reg = \
60 (intsil_cmd)
61
62#define intersil_intr(intersil_reg, intsil_cmd) intersil_reg->int_intr_reg = \
63 (intsil_cmd)
64
65#define intersil_start(intersil_reg) intersil_cmd(intersil_reg, \
66 ( INTERSIL_START | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
67 INTERSIL_INTR_ENABLE))
68
69#define intersil_stop(intersil_reg) intersil_cmd(intersil_reg, \
70 ( INTERSIL_STOP | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
71 INTERSIL_INTR_ENABLE))
72
73#define intersil_read_intr(intersil_reg, towhere) towhere = \
74 intersil_reg->int_intr_reg
75
76#endif
77
78unsigned long profile_pc(struct pt_regs *regs)
79{
80 extern char __copy_user_begin[], __copy_user_end[];
81 extern char __atomic_begin[], __atomic_end[];
82 extern char __bzero_begin[], __bzero_end[];
83 extern char __bitops_begin[], __bitops_end[];
84
85 unsigned long pc = regs->pc;
86
87 if (in_lock_functions(pc) ||
88 (pc >= (unsigned long) __copy_user_begin &&
89 pc < (unsigned long) __copy_user_end) ||
90 (pc >= (unsigned long) __atomic_begin &&
91 pc < (unsigned long) __atomic_end) ||
92 (pc >= (unsigned long) __bzero_begin &&
93 pc < (unsigned long) __bzero_end) ||
94 (pc >= (unsigned long) __bitops_begin &&
95 pc < (unsigned long) __bitops_end))
96 pc = regs->u_regs[UREG_RETPC];
97 return pc;
98}
99
100__volatile__ unsigned int *master_l10_counter;
101__volatile__ unsigned int *master_l10_limit;
102
103/*
104 * timer_interrupt() needs to keep up the real-time clock,
105 * as well as call the "do_timer()" routine every clocktick
106 */
107
108#define TICK_SIZE (tick_nsec / 1000)
109
110irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
111{
112 /* last time the cmos clock got updated */
113 static long last_rtc_update;
114
115#ifndef CONFIG_SMP
116 profile_tick(CPU_PROFILING, regs);
117#endif
118
119 /* Protect counter clear so that do_gettimeoffset works */
120 write_seqlock(&xtime_lock);
121#ifdef CONFIG_SUN4
122 if((idprom->id_machtype == (SM_SUN4 | SM_4_260)) ||
123 (idprom->id_machtype == (SM_SUN4 | SM_4_110))) {
124 int temp;
125 intersil_read_intr(intersil_clock, temp);
126 /* re-enable the irq */
127 enable_pil_irq(10);
128 }
129#endif
130 clear_clock_irq();
131
132 do_timer(regs);
133#ifndef CONFIG_SMP
134 update_process_times(user_mode(regs));
135#endif
136
137
138 /* Determine when to update the Mostek clock. */
john stultzb149ee22005-09-06 15:17:46 -0700139 if (ntp_synced() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 xtime.tv_sec > last_rtc_update + 660 &&
141 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
142 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
143 if (set_rtc_mmss(xtime.tv_sec) == 0)
144 last_rtc_update = xtime.tv_sec;
145 else
146 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
147 }
148 write_sequnlock(&xtime_lock);
149
150 return IRQ_HANDLED;
151}
152
153/* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
154static void __init kick_start_clock(void)
155{
156 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
157 unsigned char sec;
158 int i, count;
159
160 prom_printf("CLOCK: Clock was stopped. Kick start ");
161
162 spin_lock_irq(&mostek_lock);
163
164 /* Turn on the kick start bit to start the oscillator. */
165 regs->creg |= MSTK_CREG_WRITE;
166 regs->sec &= ~MSTK_STOP;
167 regs->hour |= MSTK_KICK_START;
168 regs->creg &= ~MSTK_CREG_WRITE;
169
170 spin_unlock_irq(&mostek_lock);
171
172 /* Delay to allow the clock oscillator to start. */
173 sec = MSTK_REG_SEC(regs);
174 for (i = 0; i < 3; i++) {
175 while (sec == MSTK_REG_SEC(regs))
176 for (count = 0; count < 100000; count++)
177 /* nothing */ ;
178 prom_printf(".");
179 sec = regs->sec;
180 }
181 prom_printf("\n");
182
183 spin_lock_irq(&mostek_lock);
184
185 /* Turn off kick start and set a "valid" time and date. */
186 regs->creg |= MSTK_CREG_WRITE;
187 regs->hour &= ~MSTK_KICK_START;
188 MSTK_SET_REG_SEC(regs,0);
189 MSTK_SET_REG_MIN(regs,0);
190 MSTK_SET_REG_HOUR(regs,0);
191 MSTK_SET_REG_DOW(regs,5);
192 MSTK_SET_REG_DOM(regs,1);
193 MSTK_SET_REG_MONTH(regs,8);
194 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
195 regs->creg &= ~MSTK_CREG_WRITE;
196
197 spin_unlock_irq(&mostek_lock);
198
199 /* Ensure the kick start bit is off. If it isn't, turn it off. */
200 while (regs->hour & MSTK_KICK_START) {
201 prom_printf("CLOCK: Kick start still on!\n");
202
203 spin_lock_irq(&mostek_lock);
204 regs->creg |= MSTK_CREG_WRITE;
205 regs->hour &= ~MSTK_KICK_START;
206 regs->creg &= ~MSTK_CREG_WRITE;
207 spin_unlock_irq(&mostek_lock);
208 }
209
210 prom_printf("CLOCK: Kick start procedure successful.\n");
211}
212
213/* Return nonzero if the clock chip battery is low. */
214static __inline__ int has_low_battery(void)
215{
216 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
217 unsigned char data1, data2;
218
219 spin_lock_irq(&mostek_lock);
220 data1 = regs->eeprom[0]; /* Read some data. */
221 regs->eeprom[0] = ~data1; /* Write back the complement. */
222 data2 = regs->eeprom[0]; /* Read back the complement. */
223 regs->eeprom[0] = data1; /* Restore the original value. */
224 spin_unlock_irq(&mostek_lock);
225
226 return (data1 == data2); /* Was the write blocked? */
227}
228
229/* Probe for the real time clock chip on Sun4 */
230static __inline__ void sun4_clock_probe(void)
231{
232#ifdef CONFIG_SUN4
233 int temp;
234 struct resource r;
235
236 memset(&r, 0, sizeof(r));
237 if( idprom->id_machtype == (SM_SUN4 | SM_4_330) ) {
238 sp_clock_typ = MSTK48T02;
239 r.start = sun4_clock_physaddr;
240 mstk48t02_regs = sbus_ioremap(&r, 0,
241 sizeof(struct mostek48t02), NULL);
242 mstk48t08_regs = NULL; /* To catch weirdness */
243 intersil_clock = NULL; /* just in case */
244
245 /* Kick start the clock if it is completely stopped. */
246 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
247 kick_start_clock();
248 } else if( idprom->id_machtype == (SM_SUN4 | SM_4_260)) {
249 /* intersil setup code */
250 printk("Clock: INTERSIL at %8x ",sun4_clock_physaddr);
251 sp_clock_typ = INTERSIL;
252 r.start = sun4_clock_physaddr;
253 intersil_clock = (struct intersil *)
254 sbus_ioremap(&r, 0, sizeof(*intersil_clock), "intersil");
255 mstk48t02_regs = 0; /* just be sure */
256 mstk48t08_regs = NULL; /* ditto */
257 /* initialise the clock */
258
259 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
260
261 intersil_start(intersil_clock);
262
263 intersil_read_intr(intersil_clock, temp);
264 while (!(temp & 0x80))
265 intersil_read_intr(intersil_clock, temp);
266
267 intersil_read_intr(intersil_clock, temp);
268 while (!(temp & 0x80))
269 intersil_read_intr(intersil_clock, temp);
270
271 intersil_stop(intersil_clock);
272
273 }
274#endif
275}
276
David S. Milleree5caf02006-06-29 14:36:52 -0700277static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
David S. Milleree5caf02006-06-29 14:36:52 -0700279 struct device_node *dp = op->node;
280 char *model = of_get_property(dp, "model", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
David S. Milleree5caf02006-06-29 14:36:52 -0700282 if (!model)
283 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
David S. Milleree5caf02006-06-29 14:36:52 -0700285 if (!strcmp(model, "mk48t02")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 sp_clock_typ = MSTK48T02;
David S. Milleree5caf02006-06-29 14:36:52 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* Map the clock register io area read-only */
David S. Milleree5caf02006-06-29 14:36:52 -0700289 mstk48t02_regs = of_ioremap(&op->resource[0], 0,
290 sizeof(struct mostek48t02),
291 "mk48t02");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 mstk48t08_regs = NULL; /* To catch weirdness */
David S. Milleree5caf02006-06-29 14:36:52 -0700293 } else if (!strcmp(model, "mk48t08")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 sp_clock_typ = MSTK48T08;
David S. Milleree5caf02006-06-29 14:36:52 -0700295 mstk48t08_regs = of_ioremap(&op->resource[0], 0,
296 sizeof(struct mostek48t08),
297 "mk48t08");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 mstk48t02_regs = &mstk48t08_regs->regs;
David S. Milleree5caf02006-06-29 14:36:52 -0700300 } else
301 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Report a low battery voltage condition. */
304 if (has_low_battery())
305 printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
306
307 /* Kick start the clock if it is completely stopped. */
308 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
309 kick_start_clock();
David S. Milleree5caf02006-06-29 14:36:52 -0700310
311 return 0;
312}
313
314static struct of_device_id clock_match[] = {
315 {
316 .name = "eeprom",
317 },
318 {},
319};
320
321static struct of_platform_driver clock_driver = {
322 .name = "clock",
323 .match_table = clock_match,
324 .probe = clock_probe,
325};
326
327
328/* Probe for the mostek real time clock chip. */
329static void clock_init(void)
330{
331 of_register_driver(&clock_driver, &of_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334void __init sbus_time_init(void)
335{
336 unsigned int year, mon, day, hour, min, sec;
337 struct mostek48t02 *mregs;
338
339#ifdef CONFIG_SUN4
340 int temp;
341 struct intersil *iregs;
342#endif
343
344 BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
345 btfixup();
346
347 if (ARCH_SUN4)
348 sun4_clock_probe();
349 else
David S. Milleree5caf02006-06-29 14:36:52 -0700350 clock_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 sparc_init_timers(timer_interrupt);
353
354#ifdef CONFIG_SUN4
355 if(idprom->id_machtype == (SM_SUN4 | SM_4_330)) {
356#endif
357 mregs = (struct mostek48t02 *)mstk48t02_regs;
358 if(!mregs) {
359 prom_printf("Something wrong, clock regs not mapped yet.\n");
360 prom_halt();
361 }
362 spin_lock_irq(&mostek_lock);
363 mregs->creg |= MSTK_CREG_READ;
364 sec = MSTK_REG_SEC(mregs);
365 min = MSTK_REG_MIN(mregs);
366 hour = MSTK_REG_HOUR(mregs);
367 day = MSTK_REG_DOM(mregs);
368 mon = MSTK_REG_MONTH(mregs);
369 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
370 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
371 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
372 set_normalized_timespec(&wall_to_monotonic,
373 -xtime.tv_sec, -xtime.tv_nsec);
374 mregs->creg &= ~MSTK_CREG_READ;
375 spin_unlock_irq(&mostek_lock);
376#ifdef CONFIG_SUN4
377 } else if(idprom->id_machtype == (SM_SUN4 | SM_4_260) ) {
378 /* initialise the intersil on sun4 */
379
380 iregs=intersil_clock;
381 if(!iregs) {
382 prom_printf("Something wrong, clock regs not mapped yet.\n");
383 prom_halt();
384 }
385
386 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
387 disable_pil_irq(10);
388 intersil_stop(iregs);
389 intersil_read_intr(intersil_clock, temp);
390
391 temp = iregs->clk.int_csec;
392
393 sec = iregs->clk.int_sec;
394 min = iregs->clk.int_min;
395 hour = iregs->clk.int_hour;
396 day = iregs->clk.int_day;
397 mon = iregs->clk.int_month;
398 year = MSTK_CVT_YEAR(iregs->clk.int_year);
399
400 enable_pil_irq(10);
401 intersil_start(iregs);
402
403 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
404 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
405 set_normalized_timespec(&wall_to_monotonic,
406 -xtime.tv_sec, -xtime.tv_nsec);
407 printk("%u/%u/%u %u:%u:%u\n",day,mon,year,hour,min,sec);
408 }
409#endif
410
411 /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
412 local_irq_enable();
413}
414
415void __init time_init(void)
416{
417#ifdef CONFIG_PCI
418 extern void pci_time_init(void);
419 if (pcic_present()) {
420 pci_time_init();
421 return;
422 }
423#endif
424 sbus_time_init();
425}
426
Adrian Bunk31156242005-10-03 17:37:02 -0700427static inline unsigned long do_gettimeoffset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 return (*master_l10_counter >> 10) & 0x1fffff;
430}
431
432/*
433 * Returns nanoseconds
434 * XXX This is a suboptimal implementation.
435 */
436unsigned long long sched_clock(void)
437{
438 return (unsigned long long)jiffies * (1000000000 / HZ);
439}
440
441/* Ok, my cute asm atomicity trick doesn't work anymore.
442 * There are just too many variables that need to be protected
443 * now (both members of xtime, wall_jiffies, et al.)
444 */
445void do_gettimeofday(struct timeval *tv)
446{
447 unsigned long flags;
448 unsigned long seq;
449 unsigned long usec, sec;
450 unsigned long max_ntp_tick = tick_usec - tickadj;
451
452 do {
453 unsigned long lost;
454
455 seq = read_seqbegin_irqsave(&xtime_lock, flags);
456 usec = do_gettimeoffset();
457 lost = jiffies - wall_jiffies;
458
459 /*
460 * If time_adjust is negative then NTP is slowing the clock
461 * so make sure not to go into next possible interval.
462 * Better to lose some accuracy than have time go backwards..
463 */
464 if (unlikely(time_adjust < 0)) {
465 usec = min(usec, max_ntp_tick);
466
467 if (lost)
468 usec += lost * max_ntp_tick;
469 }
470 else if (unlikely(lost))
471 usec += lost * tick_usec;
472
473 sec = xtime.tv_sec;
474 usec += (xtime.tv_nsec / 1000);
475 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
476
477 while (usec >= 1000000) {
478 usec -= 1000000;
479 sec++;
480 }
481
482 tv->tv_sec = sec;
483 tv->tv_usec = usec;
484}
485
486EXPORT_SYMBOL(do_gettimeofday);
487
488int do_settimeofday(struct timespec *tv)
489{
490 int ret;
491
492 write_seqlock_irq(&xtime_lock);
493 ret = bus_do_settimeofday(tv);
494 write_sequnlock_irq(&xtime_lock);
495 clock_was_set();
496 return ret;
497}
498
499EXPORT_SYMBOL(do_settimeofday);
500
501static int sbus_do_settimeofday(struct timespec *tv)
502{
503 time_t wtm_sec, sec = tv->tv_sec;
504 long wtm_nsec, nsec = tv->tv_nsec;
505
506 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
507 return -EINVAL;
508
509 /*
510 * This is revolting. We need to set "xtime" correctly. However, the
511 * value in this location is the value at the most recent update of
512 * wall time. Discover what correction gettimeofday() would have
513 * made, and then undo it!
514 */
515 nsec -= 1000 * (do_gettimeoffset() +
516 (jiffies - wall_jiffies) * (USEC_PER_SEC / HZ));
517
518 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
519 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
520
521 set_normalized_timespec(&xtime, sec, nsec);
522 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
523
john stultzb149ee22005-09-06 15:17:46 -0700524 ntp_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 0;
526}
527
528/*
529 * BUG: This routine does not handle hour overflow properly; it just
530 * sets the minutes. Usually you won't notice until after reboot!
531 */
532static int set_rtc_mmss(unsigned long nowtime)
533{
534 int real_seconds, real_minutes, mostek_minutes;
535 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
536 unsigned long flags;
537#ifdef CONFIG_SUN4
538 struct intersil *iregs = intersil_clock;
539 int temp;
540#endif
541
542 /* Not having a register set can lead to trouble. */
543 if (!regs) {
544#ifdef CONFIG_SUN4
545 if(!iregs)
546 return -1;
547 else {
548 temp = iregs->clk.int_csec;
549
550 mostek_minutes = iregs->clk.int_min;
551
552 real_seconds = nowtime % 60;
553 real_minutes = nowtime / 60;
554 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
555 real_minutes += 30; /* correct for half hour time zone */
556 real_minutes %= 60;
557
558 if (abs(real_minutes - mostek_minutes) < 30) {
559 intersil_stop(iregs);
560 iregs->clk.int_sec=real_seconds;
561 iregs->clk.int_min=real_minutes;
562 intersil_start(iregs);
563 } else {
564 printk(KERN_WARNING
565 "set_rtc_mmss: can't update from %d to %d\n",
566 mostek_minutes, real_minutes);
567 return -1;
568 }
569
570 return 0;
571 }
572#endif
573 }
574
575 spin_lock_irqsave(&mostek_lock, flags);
576 /* Read the current RTC minutes. */
577 regs->creg |= MSTK_CREG_READ;
578 mostek_minutes = MSTK_REG_MIN(regs);
579 regs->creg &= ~MSTK_CREG_READ;
580
581 /*
582 * since we're only adjusting minutes and seconds,
583 * don't interfere with hour overflow. This avoids
584 * messing with unknown time zones but requires your
585 * RTC not to be off by more than 15 minutes
586 */
587 real_seconds = nowtime % 60;
588 real_minutes = nowtime / 60;
589 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
590 real_minutes += 30; /* correct for half hour time zone */
591 real_minutes %= 60;
592
593 if (abs(real_minutes - mostek_minutes) < 30) {
594 regs->creg |= MSTK_CREG_WRITE;
595 MSTK_SET_REG_SEC(regs,real_seconds);
596 MSTK_SET_REG_MIN(regs,real_minutes);
597 regs->creg &= ~MSTK_CREG_WRITE;
598 spin_unlock_irqrestore(&mostek_lock, flags);
599 return 0;
600 } else {
601 spin_unlock_irqrestore(&mostek_lock, flags);
602 return -1;
603 }
604}