David S. Miller | 64d329e | 2007-10-27 00:17:01 -0700 | [diff] [blame] | 1 | /* linux/arch/sparc/kernel/time.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David S. Miller | 64d329e | 2007-10-27 00:17:01 -0700 | [diff] [blame] | 3 | * Copyright (C) 1995 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #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. Miller | 454eeb2 | 2008-08-27 04:05:35 -0700 | [diff] [blame] | 31 | #include <linux/of.h> |
Stephen Rothwell | 764f257 | 2008-08-07 15:33:36 -0700 | [diff] [blame] | 32 | #include <linux/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <asm/oplib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/page.h> |
| 43 | #include <asm/pcic.h> |
Al Viro | 0d84438 | 2006-10-08 14:30:44 +0100 | [diff] [blame] | 44 | #include <asm/irq_regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Al Viro | 32231a6 | 2007-07-21 19:18:57 -0700 | [diff] [blame] | 46 | #include "irq.h" |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | DEFINE_SPINLOCK(rtc_lock); |
Adrian Bunk | c61c65c | 2008-06-05 11:40:58 -0700 | [diff] [blame] | 49 | static enum sparc_clock_type sp_clock_typ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | DEFINE_SPINLOCK(mostek_lock); |
| 51 | void __iomem *mstk48t02_regs = NULL; |
Al Viro | c316ef0 | 2005-12-06 05:55:44 -0500 | [diff] [blame] | 52 | static struct mostek48t08 __iomem *mstk48t08_regs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static int set_rtc_mmss(unsigned long); |
| 54 | static int sbus_do_settimeofday(struct timespec *tv); |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | unsigned 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 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. Miller | 8a8b836 | 2006-12-17 16:18:47 -0800 | [diff] [blame] | 70 | pc < (unsigned long) __bzero_end)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | pc = regs->u_regs[UREG_RETPC]; |
| 72 | return pc; |
| 73 | } |
| 74 | |
Martin Habets | 9550e59 | 2006-10-17 19:21:48 -0700 | [diff] [blame] | 75 | EXPORT_SYMBOL(profile_pc); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | __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 Garzik | 5dc0742 | 2007-10-31 05:08:48 -0400 | [diff] [blame] | 87 | static irqreturn_t timer_interrupt(int dummy, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | /* last time the cmos clock got updated */ |
| 90 | static long last_rtc_update; |
| 91 | |
| 92 | #ifndef CONFIG_SMP |
Al Viro | 0d84438 | 2006-10-08 14:30:44 +0100 | [diff] [blame] | 93 | profile_tick(CPU_PROFILING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #endif |
| 95 | |
| 96 | /* Protect counter clear so that do_gettimeoffset works */ |
| 97 | write_seqlock(&xtime_lock); |
Adrian Bunk | 5110bd2 | 2008-08-31 20:59:37 -0700 | [diff] [blame^] | 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | clear_clock_irq(); |
| 100 | |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 101 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | /* Determine when to update the Mostek clock. */ |
john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 104 | if (ntp_synced() && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | 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 Zijlstra | aa02cd2 | 2008-02-13 21:33:16 +0100 | [diff] [blame] | 115 | #ifndef CONFIG_SMP |
| 116 | update_process_times(user_mode(get_irq_regs())); |
| 117 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return IRQ_HANDLED; |
| 119 | } |
| 120 | |
| 121 | /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */ |
Krzysztof Helt | 42e2826 | 2007-05-19 12:03:24 -0700 | [diff] [blame] | 122 | static void __devinit kick_start_clock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 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. Miller | 64d329e | 2007-10-27 00:17:01 -0700 | [diff] [blame] | 182 | static inline int has_low_battery(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 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 Helt | 42e2826 | 2007-05-19 12:03:24 -0700 | [diff] [blame] | 197 | static void __devinit mostek_set_system_time(void) |
Bob Breuer | 96ba989 | 2006-07-27 22:08:01 -0700 | [diff] [blame] | 198 | { |
| 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. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 223 | static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 225 | struct device_node *dp = op->node; |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 226 | const char *model = of_get_property(dp, "model", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 228 | if (!model) |
| 229 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 231 | if (!strcmp(model, "mk48t02")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | sp_clock_typ = MSTK48T02; |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* Map the clock register io area read-only */ |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 235 | mstk48t02_regs = of_ioremap(&op->resource[0], 0, |
| 236 | sizeof(struct mostek48t02), |
| 237 | "mk48t02"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | mstk48t08_regs = NULL; /* To catch weirdness */ |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 239 | } else if (!strcmp(model, "mk48t08")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | sp_clock_typ = MSTK48T08; |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 241 | mstk48t08_regs = of_ioremap(&op->resource[0], 0, |
| 242 | sizeof(struct mostek48t08), |
| 243 | "mk48t08"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | mstk48t02_regs = &mstk48t08_regs->regs; |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 246 | } else |
| 247 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 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. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 256 | |
Bob Breuer | 96ba989 | 2006-07-27 22:08:01 -0700 | [diff] [blame] | 257 | mostek_set_system_time(); |
| 258 | |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 262 | static struct of_device_id __initdata clock_match[] = { |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 263 | { |
| 264 | .name = "eeprom", |
| 265 | }, |
| 266 | {}, |
| 267 | }; |
| 268 | |
| 269 | static struct of_platform_driver clock_driver = { |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 270 | .match_table = clock_match, |
| 271 | .probe = clock_probe, |
Stephen Rothwell | a2cd155 | 2007-10-10 23:27:34 -0700 | [diff] [blame] | 272 | .driver = { |
| 273 | .name = "clock", |
| 274 | }, |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | |
| 278 | /* Probe for the mostek real time clock chip. */ |
Bob Breuer | 96ba989 | 2006-07-27 22:08:01 -0700 | [diff] [blame] | 279 | static int __init clock_init(void) |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 280 | { |
Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 281 | return of_register_driver(&clock_driver, &of_platform_bus_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Bob Breuer | 96ba989 | 2006-07-27 22:08:01 -0700 | [diff] [blame] | 284 | /* 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 | */ |
| 288 | fs_initcall(clock_init); |
Bob Breuer | 96ba989 | 2006-07-27 22:08:01 -0700 | [diff] [blame] | 289 | |
Adrian Bunk | c61c65c | 2008-06-05 11:40:58 -0700 | [diff] [blame] | 290 | static void __init sbus_time_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM); |
| 294 | btfixup(); |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | sparc_init_timers(timer_interrupt); |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */ |
| 299 | local_irq_enable(); |
| 300 | } |
| 301 | |
| 302 | void __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 Bunk | 3115624 | 2005-10-03 17:37:02 -0700 | [diff] [blame] | 314 | static inline unsigned long do_gettimeoffset(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
David S. Miller | 000775c | 2008-01-09 05:09:06 -0800 | [diff] [blame] | 316 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | /* Ok, my cute asm atomicity trick doesn't work anymore. |
| 327 | * There are just too many variables that need to be protected |
Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 328 | * now (both members of xtime, et al.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | */ |
| 330 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | seq = read_seqbegin_irqsave(&xtime_lock, flags); |
| 339 | usec = do_gettimeoffset(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 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 Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 346 | if (unlikely(time_adjust < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | usec = min(usec, max_ntp_tick); |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | 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 | |
| 362 | EXPORT_SYMBOL(do_gettimeofday); |
| 363 | |
| 364 | int 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 | |
| 375 | EXPORT_SYMBOL(do_settimeofday); |
| 376 | |
| 377 | static 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 Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 391 | nsec -= 1000 * do_gettimeoffset(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 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 stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 399 | ntp_clear(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | 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 | */ |
| 407 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 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 | } |