blob: a713bb43db84e90e00278a09fa29b5d09de11022 [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>
42#include <asm/sun4paddr.h>
43#include <asm/page.h>
44#include <asm/pcic.h>
Al Viro0d844382006-10-08 14:30:44 +010045#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Al Viro32231a62007-07-21 19:18:57 -070047#include "irq.h"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049DEFINE_SPINLOCK(rtc_lock);
Adrian Bunkc61c65c2008-06-05 11:40:58 -070050static enum sparc_clock_type sp_clock_typ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051DEFINE_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[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 unsigned long pc = regs->pc;
85
86 if (in_lock_functions(pc) ||
87 (pc >= (unsigned long) __copy_user_begin &&
88 pc < (unsigned long) __copy_user_end) ||
89 (pc >= (unsigned long) __atomic_begin &&
90 pc < (unsigned long) __atomic_end) ||
91 (pc >= (unsigned long) __bzero_begin &&
David S. Miller8a8b8362006-12-17 16:18:47 -080092 pc < (unsigned long) __bzero_end))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pc = regs->u_regs[UREG_RETPC];
94 return pc;
95}
96
Martin Habets9550e592006-10-17 19:21:48 -070097EXPORT_SYMBOL(profile_pc);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099__volatile__ unsigned int *master_l10_counter;
100__volatile__ unsigned int *master_l10_limit;
101
102/*
103 * timer_interrupt() needs to keep up the real-time clock,
104 * as well as call the "do_timer()" routine every clocktick
105 */
106
107#define TICK_SIZE (tick_nsec / 1000)
108
Jeff Garzik5dc07422007-10-31 05:08:48 -0400109static irqreturn_t timer_interrupt(int dummy, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 /* last time the cmos clock got updated */
112 static long last_rtc_update;
113
114#ifndef CONFIG_SMP
Al Viro0d844382006-10-08 14:30:44 +0100115 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#endif
117
118 /* Protect counter clear so that do_gettimeoffset works */
119 write_seqlock(&xtime_lock);
120#ifdef CONFIG_SUN4
121 if((idprom->id_machtype == (SM_SUN4 | SM_4_260)) ||
122 (idprom->id_machtype == (SM_SUN4 | SM_4_110))) {
123 int temp;
124 intersil_read_intr(intersil_clock, temp);
125 /* re-enable the irq */
126 enable_pil_irq(10);
127 }
128#endif
129 clear_clock_irq();
130
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700131 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* Determine when to update the Mostek clock. */
john stultzb149ee22005-09-06 15:17:46 -0700134 if (ntp_synced() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 xtime.tv_sec > last_rtc_update + 660 &&
136 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
137 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
138 if (set_rtc_mmss(xtime.tv_sec) == 0)
139 last_rtc_update = xtime.tv_sec;
140 else
141 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
142 }
143 write_sequnlock(&xtime_lock);
144
Peter Zijlstraaa02cd22008-02-13 21:33:16 +0100145#ifndef CONFIG_SMP
146 update_process_times(user_mode(get_irq_regs()));
147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return IRQ_HANDLED;
149}
150
151/* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
Krzysztof Helt42e28262007-05-19 12:03:24 -0700152static void __devinit kick_start_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
155 unsigned char sec;
156 int i, count;
157
158 prom_printf("CLOCK: Clock was stopped. Kick start ");
159
160 spin_lock_irq(&mostek_lock);
161
162 /* Turn on the kick start bit to start the oscillator. */
163 regs->creg |= MSTK_CREG_WRITE;
164 regs->sec &= ~MSTK_STOP;
165 regs->hour |= MSTK_KICK_START;
166 regs->creg &= ~MSTK_CREG_WRITE;
167
168 spin_unlock_irq(&mostek_lock);
169
170 /* Delay to allow the clock oscillator to start. */
171 sec = MSTK_REG_SEC(regs);
172 for (i = 0; i < 3; i++) {
173 while (sec == MSTK_REG_SEC(regs))
174 for (count = 0; count < 100000; count++)
175 /* nothing */ ;
176 prom_printf(".");
177 sec = regs->sec;
178 }
179 prom_printf("\n");
180
181 spin_lock_irq(&mostek_lock);
182
183 /* Turn off kick start and set a "valid" time and date. */
184 regs->creg |= MSTK_CREG_WRITE;
185 regs->hour &= ~MSTK_KICK_START;
186 MSTK_SET_REG_SEC(regs,0);
187 MSTK_SET_REG_MIN(regs,0);
188 MSTK_SET_REG_HOUR(regs,0);
189 MSTK_SET_REG_DOW(regs,5);
190 MSTK_SET_REG_DOM(regs,1);
191 MSTK_SET_REG_MONTH(regs,8);
192 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
193 regs->creg &= ~MSTK_CREG_WRITE;
194
195 spin_unlock_irq(&mostek_lock);
196
197 /* Ensure the kick start bit is off. If it isn't, turn it off. */
198 while (regs->hour & MSTK_KICK_START) {
199 prom_printf("CLOCK: Kick start still on!\n");
200
201 spin_lock_irq(&mostek_lock);
202 regs->creg |= MSTK_CREG_WRITE;
203 regs->hour &= ~MSTK_KICK_START;
204 regs->creg &= ~MSTK_CREG_WRITE;
205 spin_unlock_irq(&mostek_lock);
206 }
207
208 prom_printf("CLOCK: Kick start procedure successful.\n");
209}
210
211/* Return nonzero if the clock chip battery is low. */
David S. Miller64d329e2007-10-27 00:17:01 -0700212static inline int has_low_battery(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
215 unsigned char data1, data2;
216
217 spin_lock_irq(&mostek_lock);
218 data1 = regs->eeprom[0]; /* Read some data. */
219 regs->eeprom[0] = ~data1; /* Write back the complement. */
220 data2 = regs->eeprom[0]; /* Read back the complement. */
221 regs->eeprom[0] = data1; /* Restore the original value. */
222 spin_unlock_irq(&mostek_lock);
223
224 return (data1 == data2); /* Was the write blocked? */
225}
226
Krzysztof Helt42e28262007-05-19 12:03:24 -0700227static void __devinit mostek_set_system_time(void)
Bob Breuer96ba9892006-07-27 22:08:01 -0700228{
229 unsigned int year, mon, day, hour, min, sec;
230 struct mostek48t02 *mregs;
231
232 mregs = (struct mostek48t02 *)mstk48t02_regs;
233 if(!mregs) {
234 prom_printf("Something wrong, clock regs not mapped yet.\n");
235 prom_halt();
236 }
237 spin_lock_irq(&mostek_lock);
238 mregs->creg |= MSTK_CREG_READ;
239 sec = MSTK_REG_SEC(mregs);
240 min = MSTK_REG_MIN(mregs);
241 hour = MSTK_REG_HOUR(mregs);
242 day = MSTK_REG_DOM(mregs);
243 mon = MSTK_REG_MONTH(mregs);
244 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
245 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
246 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
247 set_normalized_timespec(&wall_to_monotonic,
248 -xtime.tv_sec, -xtime.tv_nsec);
249 mregs->creg &= ~MSTK_CREG_READ;
250 spin_unlock_irq(&mostek_lock);
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* Probe for the real time clock chip on Sun4 */
David S. Miller64d329e2007-10-27 00:17:01 -0700254static inline void sun4_clock_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256#ifdef CONFIG_SUN4
257 int temp;
258 struct resource r;
259
260 memset(&r, 0, sizeof(r));
261 if( idprom->id_machtype == (SM_SUN4 | SM_4_330) ) {
262 sp_clock_typ = MSTK48T02;
263 r.start = sun4_clock_physaddr;
David S. Miller454eeb22008-08-27 04:05:35 -0700264 mstk48t02_regs = of_ioremap(&r, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 sizeof(struct mostek48t02), NULL);
266 mstk48t08_regs = NULL; /* To catch weirdness */
267 intersil_clock = NULL; /* just in case */
268
269 /* Kick start the clock if it is completely stopped. */
270 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
271 kick_start_clock();
272 } else if( idprom->id_machtype == (SM_SUN4 | SM_4_260)) {
273 /* intersil setup code */
274 printk("Clock: INTERSIL at %8x ",sun4_clock_physaddr);
275 sp_clock_typ = INTERSIL;
276 r.start = sun4_clock_physaddr;
277 intersil_clock = (struct intersil *)
David S. Miller454eeb22008-08-27 04:05:35 -0700278 of_ioremap(&r, 0, sizeof(*intersil_clock), "intersil");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 mstk48t02_regs = 0; /* just be sure */
280 mstk48t08_regs = NULL; /* ditto */
281 /* initialise the clock */
282
283 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
284
285 intersil_start(intersil_clock);
286
287 intersil_read_intr(intersil_clock, temp);
288 while (!(temp & 0x80))
289 intersil_read_intr(intersil_clock, temp);
290
291 intersil_read_intr(intersil_clock, temp);
292 while (!(temp & 0x80))
293 intersil_read_intr(intersil_clock, temp);
294
295 intersil_stop(intersil_clock);
296
297 }
298#endif
299}
300
Bob Breuer96ba9892006-07-27 22:08:01 -0700301#ifndef CONFIG_SUN4
David S. Milleree5caf02006-06-29 14:36:52 -0700302static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
David S. Milleree5caf02006-06-29 14:36:52 -0700304 struct device_node *dp = op->node;
Stephen Rothwell8271f042007-03-29 00:47:23 -0700305 const char *model = of_get_property(dp, "model", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David S. Milleree5caf02006-06-29 14:36:52 -0700307 if (!model)
308 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
David S. Milleree5caf02006-06-29 14:36:52 -0700310 if (!strcmp(model, "mk48t02")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 sp_clock_typ = MSTK48T02;
David S. Milleree5caf02006-06-29 14:36:52 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Map the clock register io area read-only */
David S. Milleree5caf02006-06-29 14:36:52 -0700314 mstk48t02_regs = of_ioremap(&op->resource[0], 0,
315 sizeof(struct mostek48t02),
316 "mk48t02");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 mstk48t08_regs = NULL; /* To catch weirdness */
David S. Milleree5caf02006-06-29 14:36:52 -0700318 } else if (!strcmp(model, "mk48t08")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 sp_clock_typ = MSTK48T08;
David S. Milleree5caf02006-06-29 14:36:52 -0700320 mstk48t08_regs = of_ioremap(&op->resource[0], 0,
321 sizeof(struct mostek48t08),
322 "mk48t08");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 mstk48t02_regs = &mstk48t08_regs->regs;
David S. Milleree5caf02006-06-29 14:36:52 -0700325 } else
326 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Report a low battery voltage condition. */
329 if (has_low_battery())
330 printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
331
332 /* Kick start the clock if it is completely stopped. */
333 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
334 kick_start_clock();
David S. Milleree5caf02006-06-29 14:36:52 -0700335
Bob Breuer96ba9892006-07-27 22:08:01 -0700336 mostek_set_system_time();
337
David S. Milleree5caf02006-06-29 14:36:52 -0700338 return 0;
339}
340
341static struct of_device_id clock_match[] = {
342 {
343 .name = "eeprom",
344 },
345 {},
346};
347
348static struct of_platform_driver clock_driver = {
David S. Milleree5caf02006-06-29 14:36:52 -0700349 .match_table = clock_match,
350 .probe = clock_probe,
Stephen Rothwella2cd1552007-10-10 23:27:34 -0700351 .driver = {
352 .name = "clock",
353 },
David S. Milleree5caf02006-06-29 14:36:52 -0700354};
355
356
357/* Probe for the mostek real time clock chip. */
Bob Breuer96ba9892006-07-27 22:08:01 -0700358static int __init clock_init(void)
David S. Milleree5caf02006-06-29 14:36:52 -0700359{
Stephen Rothwell37b77542007-04-30 17:43:56 +1000360 return of_register_driver(&clock_driver, &of_platform_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Bob Breuer96ba9892006-07-27 22:08:01 -0700363/* Must be after subsys_initcall() so that busses are probed. Must
364 * be before device_initcall() because things like the RTC driver
365 * need to see the clock registers.
366 */
367fs_initcall(clock_init);
368#endif /* !CONFIG_SUN4 */
369
Adrian Bunkc61c65c2008-06-05 11:40:58 -0700370static void __init sbus_time_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
374 btfixup();
375
376 if (ARCH_SUN4)
377 sun4_clock_probe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 sparc_init_timers(timer_interrupt);
380
381#ifdef CONFIG_SUN4
382 if(idprom->id_machtype == (SM_SUN4 | SM_4_330)) {
Bob Breuer96ba9892006-07-27 22:08:01 -0700383 mostek_set_system_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else if(idprom->id_machtype == (SM_SUN4 | SM_4_260) ) {
385 /* initialise the intersil on sun4 */
Bob Breuer96ba9892006-07-27 22:08:01 -0700386 unsigned int year, mon, day, hour, min, sec;
387 int temp;
388 struct intersil *iregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 iregs=intersil_clock;
391 if(!iregs) {
392 prom_printf("Something wrong, clock regs not mapped yet.\n");
393 prom_halt();
394 }
395
396 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
397 disable_pil_irq(10);
398 intersil_stop(iregs);
399 intersil_read_intr(intersil_clock, temp);
400
401 temp = iregs->clk.int_csec;
402
403 sec = iregs->clk.int_sec;
404 min = iregs->clk.int_min;
405 hour = iregs->clk.int_hour;
406 day = iregs->clk.int_day;
407 mon = iregs->clk.int_month;
408 year = MSTK_CVT_YEAR(iregs->clk.int_year);
409
410 enable_pil_irq(10);
411 intersil_start(iregs);
412
413 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
414 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
415 set_normalized_timespec(&wall_to_monotonic,
416 -xtime.tv_sec, -xtime.tv_nsec);
417 printk("%u/%u/%u %u:%u:%u\n",day,mon,year,hour,min,sec);
418 }
419#endif
420
421 /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
422 local_irq_enable();
423}
424
425void __init time_init(void)
426{
427#ifdef CONFIG_PCI
428 extern void pci_time_init(void);
429 if (pcic_present()) {
430 pci_time_init();
431 return;
432 }
433#endif
434 sbus_time_init();
435}
436
Adrian Bunk31156242005-10-03 17:37:02 -0700437static inline unsigned long do_gettimeoffset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
David S. Miller000775c2008-01-09 05:09:06 -0800439 unsigned long val = *master_l10_counter;
440 unsigned long usec = (val >> 10) & 0x1fffff;
441
442 /* Limit hit? */
443 if (val & 0x80000000)
444 usec += 1000000 / HZ;
445
446 return usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* Ok, my cute asm atomicity trick doesn't work anymore.
450 * There are just too many variables that need to be protected
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700451 * now (both members of xtime, et al.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453void do_gettimeofday(struct timeval *tv)
454{
455 unsigned long flags;
456 unsigned long seq;
457 unsigned long usec, sec;
458 unsigned long max_ntp_tick = tick_usec - tickadj;
459
460 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 seq = read_seqbegin_irqsave(&xtime_lock, flags);
462 usec = do_gettimeoffset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /*
465 * If time_adjust is negative then NTP is slowing the clock
466 * so make sure not to go into next possible interval.
467 * Better to lose some accuracy than have time go backwards..
468 */
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700469 if (unlikely(time_adjust < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 usec = min(usec, max_ntp_tick);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 sec = xtime.tv_sec;
473 usec += (xtime.tv_nsec / 1000);
474 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
475
476 while (usec >= 1000000) {
477 usec -= 1000000;
478 sec++;
479 }
480
481 tv->tv_sec = sec;
482 tv->tv_usec = usec;
483}
484
485EXPORT_SYMBOL(do_gettimeofday);
486
487int do_settimeofday(struct timespec *tv)
488{
489 int ret;
490
491 write_seqlock_irq(&xtime_lock);
492 ret = bus_do_settimeofday(tv);
493 write_sequnlock_irq(&xtime_lock);
494 clock_was_set();
495 return ret;
496}
497
498EXPORT_SYMBOL(do_settimeofday);
499
500static int sbus_do_settimeofday(struct timespec *tv)
501{
502 time_t wtm_sec, sec = tv->tv_sec;
503 long wtm_nsec, nsec = tv->tv_nsec;
504
505 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
506 return -EINVAL;
507
508 /*
509 * This is revolting. We need to set "xtime" correctly. However, the
510 * value in this location is the value at the most recent update of
511 * wall time. Discover what correction gettimeofday() would have
512 * made, and then undo it!
513 */
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700514 nsec -= 1000 * do_gettimeoffset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
517 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
518
519 set_normalized_timespec(&xtime, sec, nsec);
520 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
521
john stultzb149ee22005-09-06 15:17:46 -0700522 ntp_clear();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return 0;
524}
525
526/*
527 * BUG: This routine does not handle hour overflow properly; it just
528 * sets the minutes. Usually you won't notice until after reboot!
529 */
530static int set_rtc_mmss(unsigned long nowtime)
531{
532 int real_seconds, real_minutes, mostek_minutes;
533 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
534 unsigned long flags;
535#ifdef CONFIG_SUN4
536 struct intersil *iregs = intersil_clock;
537 int temp;
538#endif
539
540 /* Not having a register set can lead to trouble. */
541 if (!regs) {
542#ifdef CONFIG_SUN4
543 if(!iregs)
544 return -1;
545 else {
546 temp = iregs->clk.int_csec;
547
548 mostek_minutes = iregs->clk.int_min;
549
550 real_seconds = nowtime % 60;
551 real_minutes = nowtime / 60;
552 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
553 real_minutes += 30; /* correct for half hour time zone */
554 real_minutes %= 60;
555
556 if (abs(real_minutes - mostek_minutes) < 30) {
557 intersil_stop(iregs);
558 iregs->clk.int_sec=real_seconds;
559 iregs->clk.int_min=real_minutes;
560 intersil_start(iregs);
561 } else {
562 printk(KERN_WARNING
563 "set_rtc_mmss: can't update from %d to %d\n",
564 mostek_minutes, real_minutes);
565 return -1;
566 }
567
568 return 0;
569 }
570#endif
571 }
572
573 spin_lock_irqsave(&mostek_lock, flags);
574 /* Read the current RTC minutes. */
575 regs->creg |= MSTK_CREG_READ;
576 mostek_minutes = MSTK_REG_MIN(regs);
577 regs->creg &= ~MSTK_CREG_READ;
578
579 /*
580 * since we're only adjusting minutes and seconds,
581 * don't interfere with hour overflow. This avoids
582 * messing with unknown time zones but requires your
583 * RTC not to be off by more than 15 minutes
584 */
585 real_seconds = nowtime % 60;
586 real_minutes = nowtime / 60;
587 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
588 real_minutes += 30; /* correct for half hour time zone */
589 real_minutes %= 60;
590
591 if (abs(real_minutes - mostek_minutes) < 30) {
592 regs->creg |= MSTK_CREG_WRITE;
593 MSTK_SET_REG_SEC(regs,real_seconds);
594 MSTK_SET_REG_MIN(regs,real_minutes);
595 regs->creg &= ~MSTK_CREG_WRITE;
596 spin_unlock_irqrestore(&mostek_lock, flags);
597 return 0;
598 } else {
599 spin_unlock_irqrestore(&mostek_lock, flags);
600 return -1;
601 }
602}