Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: time.c,v 1.42 2002/01/23 14:33:55 davem Exp $ |
| 2 | * time.c: UltraSparc timer and TOD clock support. |
| 3 | * |
| 4 | * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu) |
| 5 | * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be) |
| 6 | * |
| 7 | * Based largely on code which is: |
| 8 | * |
| 9 | * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu) |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/param.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/timex.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <linux/mc146818rtc.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/profile.h> |
| 27 | #include <linux/bcd.h> |
| 28 | #include <linux/jiffies.h> |
| 29 | #include <linux/cpufreq.h> |
| 30 | #include <linux/percpu.h> |
| 31 | #include <linux/profile.h> |
David S. Miller | 8ba706a | 2006-03-01 17:32:46 -0800 | [diff] [blame] | 32 | #include <linux/miscdevice.h> |
| 33 | #include <linux/rtc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <asm/oplib.h> |
| 36 | #include <asm/mostek.h> |
| 37 | #include <asm/timer.h> |
| 38 | #include <asm/irq.h> |
| 39 | #include <asm/io.h> |
David S. Miller | ff0d2fc | 2006-06-29 15:28:05 -0700 | [diff] [blame] | 40 | #include <asm/prom.h> |
| 41 | #include <asm/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/starfire.h> |
| 43 | #include <asm/smp.h> |
| 44 | #include <asm/sections.h> |
| 45 | #include <asm/cpudata.h> |
David S. Miller | 8ba706a | 2006-03-01 17:32:46 -0800 | [diff] [blame] | 46 | #include <asm/uaccess.h> |
David S. Miller | 07f8e5f | 2006-06-21 23:34:02 -0700 | [diff] [blame] | 47 | #include <asm/prom.h> |
Al Viro | 63540ba | 2006-10-09 11:51:14 +0100 | [diff] [blame] | 48 | #include <asm/irq_regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | DEFINE_SPINLOCK(mostek_lock); |
| 51 | DEFINE_SPINLOCK(rtc_lock); |
Al Viro | ef0299b | 2005-04-24 12:28:36 -0700 | [diff] [blame] | 52 | void __iomem *mstk48t02_regs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #ifdef CONFIG_PCI |
| 54 | unsigned long ds1287_regs = 0UL; |
| 55 | #endif |
| 56 | |
Al Viro | ef0299b | 2005-04-24 12:28:36 -0700 | [diff] [blame] | 57 | static void __iomem *mstk48t08_regs; |
| 58 | static void __iomem *mstk48t59_regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | static int set_rtc_mmss(unsigned long); |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #define TICK_PRIV_BIT (1UL << 63) |
| 63 | |
| 64 | #ifdef CONFIG_SMP |
| 65 | unsigned long profile_pc(struct pt_regs *regs) |
| 66 | { |
| 67 | unsigned long pc = instruction_pointer(regs); |
| 68 | |
| 69 | if (in_lock_functions(pc)) |
| 70 | return regs->u_regs[UREG_RETPC]; |
| 71 | return pc; |
| 72 | } |
| 73 | EXPORT_SYMBOL(profile_pc); |
| 74 | #endif |
| 75 | |
| 76 | static void tick_disable_protection(void) |
| 77 | { |
| 78 | /* Set things up so user can access tick register for profiling |
| 79 | * purposes. Also workaround BB_ERRATA_1 by doing a dummy |
| 80 | * read back of %tick after writing it. |
| 81 | */ |
| 82 | __asm__ __volatile__( |
| 83 | " ba,pt %%xcc, 1f\n" |
| 84 | " nop\n" |
| 85 | " .align 64\n" |
| 86 | "1: rd %%tick, %%g2\n" |
| 87 | " add %%g2, 6, %%g2\n" |
| 88 | " andn %%g2, %0, %%g2\n" |
| 89 | " wrpr %%g2, 0, %%tick\n" |
| 90 | " rdpr %%tick, %%g0" |
| 91 | : /* no outputs */ |
| 92 | : "r" (TICK_PRIV_BIT) |
| 93 | : "g2"); |
| 94 | } |
| 95 | |
| 96 | static void tick_init_tick(unsigned long offset) |
| 97 | { |
| 98 | tick_disable_protection(); |
| 99 | |
| 100 | __asm__ __volatile__( |
| 101 | " rd %%tick, %%g1\n" |
| 102 | " andn %%g1, %1, %%g1\n" |
| 103 | " ba,pt %%xcc, 1f\n" |
| 104 | " add %%g1, %0, %%g1\n" |
| 105 | " .align 64\n" |
| 106 | "1: wr %%g1, 0x0, %%tick_cmpr\n" |
| 107 | " rd %%tick_cmpr, %%g0" |
| 108 | : /* no outputs */ |
| 109 | : "r" (offset), "r" (TICK_PRIV_BIT) |
| 110 | : "g1"); |
| 111 | } |
| 112 | |
| 113 | static unsigned long tick_get_tick(void) |
| 114 | { |
| 115 | unsigned long ret; |
| 116 | |
| 117 | __asm__ __volatile__("rd %%tick, %0\n\t" |
| 118 | "mov %0, %0" |
| 119 | : "=r" (ret)); |
| 120 | |
| 121 | return ret & ~TICK_PRIV_BIT; |
| 122 | } |
| 123 | |
| 124 | static unsigned long tick_get_compare(void) |
| 125 | { |
| 126 | unsigned long ret; |
| 127 | |
| 128 | __asm__ __volatile__("rd %%tick_cmpr, %0\n\t" |
| 129 | "mov %0, %0" |
| 130 | : "=r" (ret)); |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | static unsigned long tick_add_compare(unsigned long adj) |
| 136 | { |
| 137 | unsigned long new_compare; |
| 138 | |
| 139 | /* Workaround for Spitfire Errata (#54 I think??), I discovered |
| 140 | * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch |
| 141 | * number 103640. |
| 142 | * |
| 143 | * On Blackbird writes to %tick_cmpr can fail, the |
| 144 | * workaround seems to be to execute the wr instruction |
| 145 | * at the start of an I-cache line, and perform a dummy |
| 146 | * read back from %tick_cmpr right after writing to it. -DaveM |
| 147 | */ |
| 148 | __asm__ __volatile__("rd %%tick_cmpr, %0\n\t" |
| 149 | "ba,pt %%xcc, 1f\n\t" |
| 150 | " add %0, %1, %0\n\t" |
| 151 | ".align 64\n" |
| 152 | "1:\n\t" |
| 153 | "wr %0, 0, %%tick_cmpr\n\t" |
| 154 | "rd %%tick_cmpr, %%g0" |
| 155 | : "=&r" (new_compare) |
| 156 | : "r" (adj)); |
| 157 | |
| 158 | return new_compare; |
| 159 | } |
| 160 | |
| 161 | static unsigned long tick_add_tick(unsigned long adj, unsigned long offset) |
| 162 | { |
| 163 | unsigned long new_tick, tmp; |
| 164 | |
| 165 | /* Also need to handle Blackbird bug here too. */ |
| 166 | __asm__ __volatile__("rd %%tick, %0\n\t" |
| 167 | "add %0, %2, %0\n\t" |
| 168 | "wrpr %0, 0, %%tick\n\t" |
| 169 | "andn %0, %4, %1\n\t" |
| 170 | "ba,pt %%xcc, 1f\n\t" |
| 171 | " add %1, %3, %1\n\t" |
| 172 | ".align 64\n" |
| 173 | "1:\n\t" |
| 174 | "wr %1, 0, %%tick_cmpr\n\t" |
| 175 | "rd %%tick_cmpr, %%g0" |
| 176 | : "=&r" (new_tick), "=&r" (tmp) |
| 177 | : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT)); |
| 178 | |
| 179 | return new_tick; |
| 180 | } |
| 181 | |
David S. Miller | d369ddd | 2005-07-10 15:45:11 -0700 | [diff] [blame] | 182 | static struct sparc64_tick_ops tick_operations __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | .init_tick = tick_init_tick, |
| 184 | .get_tick = tick_get_tick, |
| 185 | .get_compare = tick_get_compare, |
| 186 | .add_tick = tick_add_tick, |
| 187 | .add_compare = tick_add_compare, |
| 188 | .softint_mask = 1UL << 0, |
| 189 | }; |
| 190 | |
David S. Miller | fc32149 | 2005-11-07 14:10:10 -0800 | [diff] [blame] | 191 | struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations; |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | static void stick_init_tick(unsigned long offset) |
| 194 | { |
David S. Miller | 7aa6264 | 2006-02-11 23:14:59 -0800 | [diff] [blame] | 195 | /* Writes to the %tick and %stick register are not |
| 196 | * allowed on sun4v. The Hypervisor controls that |
| 197 | * bit, per-strand. |
| 198 | */ |
| 199 | if (tlb_type != hypervisor) { |
| 200 | tick_disable_protection(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
David S. Miller | 7aa6264 | 2006-02-11 23:14:59 -0800 | [diff] [blame] | 202 | /* Let the user get at STICK too. */ |
| 203 | __asm__ __volatile__( |
| 204 | " rd %%asr24, %%g2\n" |
| 205 | " andn %%g2, %0, %%g2\n" |
| 206 | " wr %%g2, 0, %%asr24" |
| 207 | : /* no outputs */ |
| 208 | : "r" (TICK_PRIV_BIT) |
| 209 | : "g1", "g2"); |
| 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | __asm__ __volatile__( |
| 213 | " rd %%asr24, %%g1\n" |
| 214 | " andn %%g1, %1, %%g1\n" |
| 215 | " add %%g1, %0, %%g1\n" |
| 216 | " wr %%g1, 0x0, %%asr25" |
| 217 | : /* no outputs */ |
| 218 | : "r" (offset), "r" (TICK_PRIV_BIT) |
| 219 | : "g1"); |
| 220 | } |
| 221 | |
| 222 | static unsigned long stick_get_tick(void) |
| 223 | { |
| 224 | unsigned long ret; |
| 225 | |
| 226 | __asm__ __volatile__("rd %%asr24, %0" |
| 227 | : "=r" (ret)); |
| 228 | |
| 229 | return ret & ~TICK_PRIV_BIT; |
| 230 | } |
| 231 | |
| 232 | static unsigned long stick_get_compare(void) |
| 233 | { |
| 234 | unsigned long ret; |
| 235 | |
| 236 | __asm__ __volatile__("rd %%asr25, %0" |
| 237 | : "=r" (ret)); |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static unsigned long stick_add_tick(unsigned long adj, unsigned long offset) |
| 243 | { |
| 244 | unsigned long new_tick, tmp; |
| 245 | |
| 246 | __asm__ __volatile__("rd %%asr24, %0\n\t" |
| 247 | "add %0, %2, %0\n\t" |
| 248 | "wr %0, 0, %%asr24\n\t" |
| 249 | "andn %0, %4, %1\n\t" |
| 250 | "add %1, %3, %1\n\t" |
| 251 | "wr %1, 0, %%asr25" |
| 252 | : "=&r" (new_tick), "=&r" (tmp) |
| 253 | : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT)); |
| 254 | |
| 255 | return new_tick; |
| 256 | } |
| 257 | |
| 258 | static unsigned long stick_add_compare(unsigned long adj) |
| 259 | { |
| 260 | unsigned long new_compare; |
| 261 | |
| 262 | __asm__ __volatile__("rd %%asr25, %0\n\t" |
| 263 | "add %0, %1, %0\n\t" |
| 264 | "wr %0, 0, %%asr25" |
| 265 | : "=&r" (new_compare) |
| 266 | : "r" (adj)); |
| 267 | |
| 268 | return new_compare; |
| 269 | } |
| 270 | |
David S. Miller | d369ddd | 2005-07-10 15:45:11 -0700 | [diff] [blame] | 271 | static struct sparc64_tick_ops stick_operations __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | .init_tick = stick_init_tick, |
| 273 | .get_tick = stick_get_tick, |
| 274 | .get_compare = stick_get_compare, |
| 275 | .add_tick = stick_add_tick, |
| 276 | .add_compare = stick_add_compare, |
| 277 | .softint_mask = 1UL << 16, |
| 278 | }; |
| 279 | |
| 280 | /* On Hummingbird the STICK/STICK_CMPR register is implemented |
| 281 | * in I/O space. There are two 64-bit registers each, the |
| 282 | * first holds the low 32-bits of the value and the second holds |
| 283 | * the high 32-bits. |
| 284 | * |
| 285 | * Since STICK is constantly updating, we have to access it carefully. |
| 286 | * |
| 287 | * The sequence we use to read is: |
Richard Mortimer | 9eb3394 | 2006-01-17 15:21:01 -0800 | [diff] [blame] | 288 | * 1) read high |
| 289 | * 2) read low |
| 290 | * 3) read high again, if it rolled re-read both low and high again. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | * |
| 292 | * Writing STICK safely is also tricky: |
| 293 | * 1) write low to zero |
| 294 | * 2) write high |
| 295 | * 3) write low |
| 296 | */ |
| 297 | #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL |
| 298 | #define HBIRD_STICK_ADDR 0x1fe0000f070UL |
| 299 | |
| 300 | static unsigned long __hbird_read_stick(void) |
| 301 | { |
| 302 | unsigned long ret, tmp1, tmp2, tmp3; |
Richard Mortimer | 9eb3394 | 2006-01-17 15:21:01 -0800 | [diff] [blame] | 303 | unsigned long addr = HBIRD_STICK_ADDR+8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Richard Mortimer | 9eb3394 | 2006-01-17 15:21:01 -0800 | [diff] [blame] | 305 | __asm__ __volatile__("ldxa [%1] %5, %2\n" |
| 306 | "1:\n\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | "sub %1, 0x8, %1\n\t" |
Richard Mortimer | 9eb3394 | 2006-01-17 15:21:01 -0800 | [diff] [blame] | 308 | "ldxa [%1] %5, %3\n\t" |
| 309 | "add %1, 0x8, %1\n\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | "ldxa [%1] %5, %4\n\t" |
| 311 | "cmp %4, %2\n\t" |
Richard Mortimer | 9eb3394 | 2006-01-17 15:21:01 -0800 | [diff] [blame] | 312 | "bne,a,pn %%xcc, 1b\n\t" |
| 313 | " mov %4, %2\n\t" |
| 314 | "sllx %4, 32, %4\n\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | "or %3, %4, %0\n\t" |
| 316 | : "=&r" (ret), "=&r" (addr), |
| 317 | "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3) |
| 318 | : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr)); |
| 319 | |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | static unsigned long __hbird_read_compare(void) |
| 324 | { |
| 325 | unsigned long low, high; |
| 326 | unsigned long addr = HBIRD_STICKCMP_ADDR; |
| 327 | |
| 328 | __asm__ __volatile__("ldxa [%2] %3, %0\n\t" |
| 329 | "add %2, 0x8, %2\n\t" |
| 330 | "ldxa [%2] %3, %1" |
| 331 | : "=&r" (low), "=&r" (high), "=&r" (addr) |
| 332 | : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr)); |
| 333 | |
| 334 | return (high << 32UL) | low; |
| 335 | } |
| 336 | |
| 337 | static void __hbird_write_stick(unsigned long val) |
| 338 | { |
| 339 | unsigned long low = (val & 0xffffffffUL); |
| 340 | unsigned long high = (val >> 32UL); |
| 341 | unsigned long addr = HBIRD_STICK_ADDR; |
| 342 | |
| 343 | __asm__ __volatile__("stxa %%g0, [%0] %4\n\t" |
| 344 | "add %0, 0x8, %0\n\t" |
| 345 | "stxa %3, [%0] %4\n\t" |
| 346 | "sub %0, 0x8, %0\n\t" |
| 347 | "stxa %2, [%0] %4" |
| 348 | : "=&r" (addr) |
| 349 | : "0" (addr), "r" (low), "r" (high), |
| 350 | "i" (ASI_PHYS_BYPASS_EC_E)); |
| 351 | } |
| 352 | |
| 353 | static void __hbird_write_compare(unsigned long val) |
| 354 | { |
| 355 | unsigned long low = (val & 0xffffffffUL); |
| 356 | unsigned long high = (val >> 32UL); |
| 357 | unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL; |
| 358 | |
| 359 | __asm__ __volatile__("stxa %3, [%0] %4\n\t" |
| 360 | "sub %0, 0x8, %0\n\t" |
| 361 | "stxa %2, [%0] %4" |
| 362 | : "=&r" (addr) |
| 363 | : "0" (addr), "r" (low), "r" (high), |
| 364 | "i" (ASI_PHYS_BYPASS_EC_E)); |
| 365 | } |
| 366 | |
| 367 | static void hbtick_init_tick(unsigned long offset) |
| 368 | { |
| 369 | unsigned long val; |
| 370 | |
| 371 | tick_disable_protection(); |
| 372 | |
| 373 | /* XXX This seems to be necessary to 'jumpstart' Hummingbird |
| 374 | * XXX into actually sending STICK interrupts. I think because |
| 375 | * XXX of how we store %tick_cmpr in head.S this somehow resets the |
| 376 | * XXX {TICK + STICK} interrupt mux. -DaveM |
| 377 | */ |
| 378 | __hbird_write_stick(__hbird_read_stick()); |
| 379 | |
| 380 | val = __hbird_read_stick() & ~TICK_PRIV_BIT; |
| 381 | __hbird_write_compare(val + offset); |
| 382 | } |
| 383 | |
| 384 | static unsigned long hbtick_get_tick(void) |
| 385 | { |
| 386 | return __hbird_read_stick() & ~TICK_PRIV_BIT; |
| 387 | } |
| 388 | |
| 389 | static unsigned long hbtick_get_compare(void) |
| 390 | { |
| 391 | return __hbird_read_compare(); |
| 392 | } |
| 393 | |
| 394 | static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset) |
| 395 | { |
| 396 | unsigned long val; |
| 397 | |
| 398 | val = __hbird_read_stick() + adj; |
| 399 | __hbird_write_stick(val); |
| 400 | |
| 401 | val &= ~TICK_PRIV_BIT; |
| 402 | __hbird_write_compare(val + offset); |
| 403 | |
| 404 | return val; |
| 405 | } |
| 406 | |
| 407 | static unsigned long hbtick_add_compare(unsigned long adj) |
| 408 | { |
| 409 | unsigned long val = __hbird_read_compare() + adj; |
| 410 | |
| 411 | val &= ~TICK_PRIV_BIT; |
| 412 | __hbird_write_compare(val); |
| 413 | |
| 414 | return val; |
| 415 | } |
| 416 | |
David S. Miller | d369ddd | 2005-07-10 15:45:11 -0700 | [diff] [blame] | 417 | static struct sparc64_tick_ops hbtick_operations __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | .init_tick = hbtick_init_tick, |
| 419 | .get_tick = hbtick_get_tick, |
| 420 | .get_compare = hbtick_get_compare, |
| 421 | .add_tick = hbtick_add_tick, |
| 422 | .add_compare = hbtick_add_compare, |
| 423 | .softint_mask = 1UL << 0, |
| 424 | }; |
| 425 | |
| 426 | /* timer_interrupt() needs to keep up the real-time clock, |
| 427 | * as well as call the "do_timer()" routine every clocktick |
| 428 | * |
| 429 | * NOTE: On SUN5 systems the ticker interrupt comes in using 2 |
| 430 | * interrupts, one at level14 and one with softint bit 0. |
| 431 | */ |
David S. Miller | d369ddd | 2005-07-10 15:45:11 -0700 | [diff] [blame] | 432 | unsigned long timer_tick_offset __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
David S. Miller | d369ddd | 2005-07-10 15:45:11 -0700 | [diff] [blame] | 434 | static unsigned long timer_ticks_per_nsec_quotient __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | #define TICK_SIZE (tick_nsec / 1000) |
| 437 | |
David S. Miller | a58c9f3 | 2007-02-22 04:16:21 -0800 | [diff] [blame^] | 438 | #define USEC_AFTER 500000 |
| 439 | #define USEC_BEFORE 500000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
David S. Miller | a58c9f3 | 2007-02-22 04:16:21 -0800 | [diff] [blame^] | 441 | static void sync_cmos_clock(unsigned long dummy); |
| 442 | |
| 443 | static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0); |
| 444 | |
| 445 | static void sync_cmos_clock(unsigned long dummy) |
| 446 | { |
| 447 | struct timeval now, next; |
| 448 | int fail = 1; |
| 449 | |
| 450 | /* |
| 451 | * If we have an externally synchronized Linux clock, then update |
| 452 | * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be |
| 453 | * called as close as possible to 500 ms before the new second starts. |
| 454 | * This code is run on a timer. If the clock is set, that timer |
| 455 | * may not expire at the correct time. Thus, we adjust... |
| 456 | */ |
| 457 | if (!ntp_synced()) |
| 458 | /* |
| 459 | * Not synced, exit, do not restart a timer (if one is |
| 460 | * running, let it run out). |
| 461 | */ |
| 462 | return; |
| 463 | |
| 464 | do_gettimeofday(&now); |
| 465 | if (now.tv_usec >= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 && |
| 466 | now.tv_usec <= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2) |
| 467 | fail = set_rtc_mmss(now.tv_sec); |
| 468 | |
| 469 | next.tv_usec = USEC_AFTER - now.tv_usec; |
| 470 | if (next.tv_usec <= 0) |
| 471 | next.tv_usec += USEC_PER_SEC; |
| 472 | |
| 473 | if (!fail) |
| 474 | next.tv_sec = 659; |
| 475 | else |
| 476 | next.tv_sec = 0; |
| 477 | |
| 478 | if (next.tv_usec >= USEC_PER_SEC) { |
| 479 | next.tv_sec++; |
| 480 | next.tv_usec -= USEC_PER_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
David S. Miller | a58c9f3 | 2007-02-22 04:16:21 -0800 | [diff] [blame^] | 482 | mod_timer(&sync_cmos_timer, jiffies + timeval_to_jiffies(&next)); |
| 483 | } |
| 484 | |
| 485 | void notify_arch_cmos_timer(void) |
| 486 | { |
| 487 | mod_timer(&sync_cmos_timer, jiffies + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Al Viro | 63540ba | 2006-10-09 11:51:14 +0100 | [diff] [blame] | 490 | irqreturn_t timer_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
David S. Miller | d369ddd | 2005-07-10 15:45:11 -0700 | [diff] [blame] | 492 | unsigned long ticks, compare, pstate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | write_seqlock(&xtime_lock); |
| 495 | |
| 496 | do { |
| 497 | #ifndef CONFIG_SMP |
Al Viro | 63540ba | 2006-10-09 11:51:14 +0100 | [diff] [blame] | 498 | profile_tick(CPU_PROFILING); |
| 499 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | #endif |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 501 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
| 503 | /* Guarantee that the following sequences execute |
| 504 | * uninterrupted. |
| 505 | */ |
| 506 | __asm__ __volatile__("rdpr %%pstate, %0\n\t" |
| 507 | "wrpr %0, %1, %%pstate" |
| 508 | : "=r" (pstate) |
| 509 | : "i" (PSTATE_IE)); |
| 510 | |
David S. Miller | d369ddd | 2005-07-10 15:45:11 -0700 | [diff] [blame] | 511 | compare = tick_ops->add_compare(timer_tick_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | ticks = tick_ops->get_tick(); |
| 513 | |
| 514 | /* Restore PSTATE_IE. */ |
| 515 | __asm__ __volatile__("wrpr %0, 0x0, %%pstate" |
| 516 | : /* no outputs */ |
| 517 | : "r" (pstate)); |
David S. Miller | d369ddd | 2005-07-10 15:45:11 -0700 | [diff] [blame] | 518 | } while (time_after_eq(ticks, compare)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | write_sequnlock(&xtime_lock); |
| 521 | |
| 522 | return IRQ_HANDLED; |
| 523 | } |
| 524 | |
| 525 | #ifdef CONFIG_SMP |
| 526 | void timer_tick_interrupt(struct pt_regs *regs) |
| 527 | { |
| 528 | write_seqlock(&xtime_lock); |
| 529 | |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 530 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | write_sequnlock(&xtime_lock); |
| 533 | } |
| 534 | #endif |
| 535 | |
| 536 | /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */ |
| 537 | static void __init kick_start_clock(void) |
| 538 | { |
Al Viro | ef0299b | 2005-04-24 12:28:36 -0700 | [diff] [blame] | 539 | void __iomem *regs = mstk48t02_regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | u8 sec, tmp; |
| 541 | int i, count; |
| 542 | |
| 543 | prom_printf("CLOCK: Clock was stopped. Kick start "); |
| 544 | |
| 545 | spin_lock_irq(&mostek_lock); |
| 546 | |
| 547 | /* Turn on the kick start bit to start the oscillator. */ |
| 548 | tmp = mostek_read(regs + MOSTEK_CREG); |
| 549 | tmp |= MSTK_CREG_WRITE; |
| 550 | mostek_write(regs + MOSTEK_CREG, tmp); |
| 551 | tmp = mostek_read(regs + MOSTEK_SEC); |
| 552 | tmp &= ~MSTK_STOP; |
| 553 | mostek_write(regs + MOSTEK_SEC, tmp); |
| 554 | tmp = mostek_read(regs + MOSTEK_HOUR); |
| 555 | tmp |= MSTK_KICK_START; |
| 556 | mostek_write(regs + MOSTEK_HOUR, tmp); |
| 557 | tmp = mostek_read(regs + MOSTEK_CREG); |
| 558 | tmp &= ~MSTK_CREG_WRITE; |
| 559 | mostek_write(regs + MOSTEK_CREG, tmp); |
| 560 | |
| 561 | spin_unlock_irq(&mostek_lock); |
| 562 | |
| 563 | /* Delay to allow the clock oscillator to start. */ |
| 564 | sec = MSTK_REG_SEC(regs); |
| 565 | for (i = 0; i < 3; i++) { |
| 566 | while (sec == MSTK_REG_SEC(regs)) |
| 567 | for (count = 0; count < 100000; count++) |
| 568 | /* nothing */ ; |
| 569 | prom_printf("."); |
| 570 | sec = MSTK_REG_SEC(regs); |
| 571 | } |
| 572 | prom_printf("\n"); |
| 573 | |
| 574 | spin_lock_irq(&mostek_lock); |
| 575 | |
| 576 | /* Turn off kick start and set a "valid" time and date. */ |
| 577 | tmp = mostek_read(regs + MOSTEK_CREG); |
| 578 | tmp |= MSTK_CREG_WRITE; |
| 579 | mostek_write(regs + MOSTEK_CREG, tmp); |
| 580 | tmp = mostek_read(regs + MOSTEK_HOUR); |
| 581 | tmp &= ~MSTK_KICK_START; |
| 582 | mostek_write(regs + MOSTEK_HOUR, tmp); |
| 583 | MSTK_SET_REG_SEC(regs,0); |
| 584 | MSTK_SET_REG_MIN(regs,0); |
| 585 | MSTK_SET_REG_HOUR(regs,0); |
| 586 | MSTK_SET_REG_DOW(regs,5); |
| 587 | MSTK_SET_REG_DOM(regs,1); |
| 588 | MSTK_SET_REG_MONTH(regs,8); |
| 589 | MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO); |
| 590 | tmp = mostek_read(regs + MOSTEK_CREG); |
| 591 | tmp &= ~MSTK_CREG_WRITE; |
| 592 | mostek_write(regs + MOSTEK_CREG, tmp); |
| 593 | |
| 594 | spin_unlock_irq(&mostek_lock); |
| 595 | |
| 596 | /* Ensure the kick start bit is off. If it isn't, turn it off. */ |
| 597 | while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) { |
| 598 | prom_printf("CLOCK: Kick start still on!\n"); |
| 599 | |
| 600 | spin_lock_irq(&mostek_lock); |
| 601 | |
| 602 | tmp = mostek_read(regs + MOSTEK_CREG); |
| 603 | tmp |= MSTK_CREG_WRITE; |
| 604 | mostek_write(regs + MOSTEK_CREG, tmp); |
| 605 | |
| 606 | tmp = mostek_read(regs + MOSTEK_HOUR); |
| 607 | tmp &= ~MSTK_KICK_START; |
| 608 | mostek_write(regs + MOSTEK_HOUR, tmp); |
| 609 | |
| 610 | tmp = mostek_read(regs + MOSTEK_CREG); |
| 611 | tmp &= ~MSTK_CREG_WRITE; |
| 612 | mostek_write(regs + MOSTEK_CREG, tmp); |
| 613 | |
| 614 | spin_unlock_irq(&mostek_lock); |
| 615 | } |
| 616 | |
| 617 | prom_printf("CLOCK: Kick start procedure successful.\n"); |
| 618 | } |
| 619 | |
| 620 | /* Return nonzero if the clock chip battery is low. */ |
| 621 | static int __init has_low_battery(void) |
| 622 | { |
Al Viro | ef0299b | 2005-04-24 12:28:36 -0700 | [diff] [blame] | 623 | void __iomem *regs = mstk48t02_regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | u8 data1, data2; |
| 625 | |
| 626 | spin_lock_irq(&mostek_lock); |
| 627 | |
| 628 | data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */ |
| 629 | mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */ |
| 630 | data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */ |
| 631 | mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */ |
| 632 | |
| 633 | spin_unlock_irq(&mostek_lock); |
| 634 | |
| 635 | return (data1 == data2); /* Was the write blocked? */ |
| 636 | } |
| 637 | |
| 638 | /* Probe for the real time clock chip. */ |
| 639 | static void __init set_system_time(void) |
| 640 | { |
| 641 | unsigned int year, mon, day, hour, min, sec; |
Al Viro | ef0299b | 2005-04-24 12:28:36 -0700 | [diff] [blame] | 642 | void __iomem *mregs = mstk48t02_regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | #ifdef CONFIG_PCI |
| 644 | unsigned long dregs = ds1287_regs; |
| 645 | #else |
| 646 | unsigned long dregs = 0UL; |
| 647 | #endif |
| 648 | u8 tmp; |
| 649 | |
| 650 | if (!mregs && !dregs) { |
| 651 | prom_printf("Something wrong, clock regs not mapped yet.\n"); |
| 652 | prom_halt(); |
| 653 | } |
| 654 | |
| 655 | if (mregs) { |
| 656 | spin_lock_irq(&mostek_lock); |
| 657 | |
| 658 | /* Traditional Mostek chip. */ |
| 659 | tmp = mostek_read(mregs + MOSTEK_CREG); |
| 660 | tmp |= MSTK_CREG_READ; |
| 661 | mostek_write(mregs + MOSTEK_CREG, tmp); |
| 662 | |
| 663 | sec = MSTK_REG_SEC(mregs); |
| 664 | min = MSTK_REG_MIN(mregs); |
| 665 | hour = MSTK_REG_HOUR(mregs); |
| 666 | day = MSTK_REG_DOM(mregs); |
| 667 | mon = MSTK_REG_MONTH(mregs); |
| 668 | year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) ); |
| 669 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | /* Dallas 12887 RTC chip. */ |
| 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | do { |
| 673 | sec = CMOS_READ(RTC_SECONDS); |
| 674 | min = CMOS_READ(RTC_MINUTES); |
| 675 | hour = CMOS_READ(RTC_HOURS); |
| 676 | day = CMOS_READ(RTC_DAY_OF_MONTH); |
| 677 | mon = CMOS_READ(RTC_MONTH); |
| 678 | year = CMOS_READ(RTC_YEAR); |
| 679 | } while (sec != CMOS_READ(RTC_SECONDS)); |
Matt Mackall | 3dedf53 | 2006-03-28 01:56:01 -0800 | [diff] [blame] | 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
| 682 | BCD_TO_BIN(sec); |
| 683 | BCD_TO_BIN(min); |
| 684 | BCD_TO_BIN(hour); |
| 685 | BCD_TO_BIN(day); |
| 686 | BCD_TO_BIN(mon); |
| 687 | BCD_TO_BIN(year); |
| 688 | } |
| 689 | if ((year += 1900) < 1970) |
| 690 | year += 100; |
| 691 | } |
| 692 | |
| 693 | xtime.tv_sec = mktime(year, mon, day, hour, min, sec); |
| 694 | xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ); |
| 695 | set_normalized_timespec(&wall_to_monotonic, |
| 696 | -xtime.tv_sec, -xtime.tv_nsec); |
| 697 | |
| 698 | if (mregs) { |
| 699 | tmp = mostek_read(mregs + MOSTEK_CREG); |
| 700 | tmp &= ~MSTK_CREG_READ; |
| 701 | mostek_write(mregs + MOSTEK_CREG, tmp); |
| 702 | |
| 703 | spin_unlock_irq(&mostek_lock); |
| 704 | } |
| 705 | } |
| 706 | |
David S. Miller | 4bdff41 | 2006-02-11 01:01:55 -0800 | [diff] [blame] | 707 | /* davem suggests we keep this within the 4M locked kernel image */ |
| 708 | static u32 starfire_get_time(void) |
| 709 | { |
| 710 | static char obp_gettod[32]; |
| 711 | static u32 unix_tod; |
| 712 | |
| 713 | sprintf(obp_gettod, "h# %08x unix-gettod", |
| 714 | (unsigned int) (long) &unix_tod); |
| 715 | prom_feval(obp_gettod); |
| 716 | |
| 717 | return unix_tod; |
| 718 | } |
| 719 | |
David S. Miller | 8ba706a | 2006-03-01 17:32:46 -0800 | [diff] [blame] | 720 | static int starfire_set_time(u32 val) |
| 721 | { |
| 722 | /* Do nothing, time is set using the service processor |
| 723 | * console on this platform. |
| 724 | */ |
| 725 | return 0; |
| 726 | } |
| 727 | |
David S. Miller | 4bdff41 | 2006-02-11 01:01:55 -0800 | [diff] [blame] | 728 | static u32 hypervisor_get_time(void) |
| 729 | { |
| 730 | register unsigned long func asm("%o5"); |
| 731 | register unsigned long arg0 asm("%o0"); |
| 732 | register unsigned long arg1 asm("%o1"); |
| 733 | int retries = 10000; |
| 734 | |
| 735 | retry: |
| 736 | func = HV_FAST_TOD_GET; |
| 737 | arg0 = 0; |
| 738 | arg1 = 0; |
| 739 | __asm__ __volatile__("ta %6" |
| 740 | : "=&r" (func), "=&r" (arg0), "=&r" (arg1) |
| 741 | : "0" (func), "1" (arg0), "2" (arg1), |
| 742 | "i" (HV_FAST_TRAP)); |
| 743 | if (arg0 == HV_EOK) |
| 744 | return arg1; |
| 745 | if (arg0 == HV_EWOULDBLOCK) { |
| 746 | if (--retries > 0) { |
| 747 | udelay(100); |
| 748 | goto retry; |
| 749 | } |
| 750 | printk(KERN_WARNING "SUN4V: tod_get() timed out.\n"); |
| 751 | return 0; |
| 752 | } |
| 753 | printk(KERN_WARNING "SUN4V: tod_get() not supported.\n"); |
| 754 | return 0; |
| 755 | } |
| 756 | |
David S. Miller | 8ba706a | 2006-03-01 17:32:46 -0800 | [diff] [blame] | 757 | static int hypervisor_set_time(u32 secs) |
| 758 | { |
| 759 | register unsigned long func asm("%o5"); |
| 760 | register unsigned long arg0 asm("%o0"); |
| 761 | int retries = 10000; |
| 762 | |
| 763 | retry: |
| 764 | func = HV_FAST_TOD_SET; |
| 765 | arg0 = secs; |
| 766 | __asm__ __volatile__("ta %4" |
| 767 | : "=&r" (func), "=&r" (arg0) |
| 768 | : "0" (func), "1" (arg0), |
| 769 | "i" (HV_FAST_TRAP)); |
| 770 | if (arg0 == HV_EOK) |
| 771 | return 0; |
| 772 | if (arg0 == HV_EWOULDBLOCK) { |
| 773 | if (--retries > 0) { |
| 774 | udelay(100); |
| 775 | goto retry; |
| 776 | } |
| 777 | printk(KERN_WARNING "SUN4V: tod_set() timed out.\n"); |
| 778 | return -EAGAIN; |
| 779 | } |
| 780 | printk(KERN_WARNING "SUN4V: tod_set() not supported.\n"); |
| 781 | return -EOPNOTSUPP; |
| 782 | } |
| 783 | |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 784 | static int __init clock_model_matches(char *model) |
| 785 | { |
| 786 | if (strcmp(model, "mk48t02") && |
| 787 | strcmp(model, "mk48t08") && |
| 788 | strcmp(model, "mk48t59") && |
| 789 | strcmp(model, "m5819") && |
| 790 | strcmp(model, "m5819p") && |
| 791 | strcmp(model, "m5823") && |
| 792 | strcmp(model, "ds1287")) |
| 793 | return 0; |
| 794 | |
| 795 | return 1; |
| 796 | } |
| 797 | |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 798 | static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match) |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 799 | { |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 800 | struct device_node *dp = op->node; |
| 801 | char *model = of_get_property(dp, "model", NULL); |
| 802 | unsigned long size, flags; |
| 803 | void __iomem *regs; |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 804 | |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 805 | if (!model || !clock_model_matches(model)) |
| 806 | return -ENODEV; |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 807 | |
David S. Miller | 9152148 | 2006-06-29 14:39:40 -0700 | [diff] [blame] | 808 | /* On an Enterprise system there can be multiple mostek clocks. |
| 809 | * We should only match the one that is on the central FHC bus. |
| 810 | */ |
David S. Miller | c3a8b85 | 2006-06-29 14:43:37 -0700 | [diff] [blame] | 811 | if (!strcmp(dp->parent->name, "fhc") && |
| 812 | strcmp(dp->parent->parent->name, "central") != 0) |
David S. Miller | 9152148 | 2006-06-29 14:39:40 -0700 | [diff] [blame] | 813 | return -ENODEV; |
| 814 | |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 815 | size = (op->resource[0].end - op->resource[0].start) + 1; |
| 816 | regs = of_ioremap(&op->resource[0], 0, size, "clock"); |
| 817 | if (!regs) |
| 818 | return -ENOMEM; |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 819 | |
Randy Dunlap | 7233589 | 2006-07-05 20:18:39 -0700 | [diff] [blame] | 820 | #ifdef CONFIG_PCI |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 821 | if (!strcmp(model, "ds1287") || |
| 822 | !strcmp(model, "m5819") || |
| 823 | !strcmp(model, "m5819p") || |
| 824 | !strcmp(model, "m5823")) { |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 825 | ds1287_regs = (unsigned long) regs; |
Randy Dunlap | 7233589 | 2006-07-05 20:18:39 -0700 | [diff] [blame] | 826 | } else |
| 827 | #endif |
| 828 | if (model[5] == '0' && model[6] == '2') { |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 829 | mstk48t02_regs = regs; |
| 830 | } else if(model[5] == '0' && model[6] == '8') { |
| 831 | mstk48t08_regs = regs; |
| 832 | mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02; |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 833 | } else { |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 834 | mstk48t59_regs = regs; |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 835 | mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02; |
| 836 | } |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 837 | |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 838 | printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 840 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
David S. Miller | b4bca26 | 2005-04-21 21:42:34 -0700 | [diff] [blame] | 842 | if (mstk48t02_regs != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | /* Report a low battery voltage condition. */ |
| 844 | if (has_low_battery()) |
| 845 | prom_printf("NVRAM: Low battery voltage!\n"); |
| 846 | |
| 847 | /* Kick start the clock if it is completely stopped. */ |
| 848 | if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP) |
| 849 | kick_start_clock(); |
| 850 | } |
| 851 | |
| 852 | set_system_time(); |
| 853 | |
| 854 | local_irq_restore(flags); |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 855 | |
| 856 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
David S. Miller | ee5caf0 | 2006-06-29 14:36:52 -0700 | [diff] [blame] | 859 | static struct of_device_id clock_match[] = { |
| 860 | { |
| 861 | .name = "eeprom", |
| 862 | }, |
| 863 | { |
| 864 | .name = "rtc", |
| 865 | }, |
| 866 | {}, |
| 867 | }; |
| 868 | |
| 869 | static struct of_platform_driver clock_driver = { |
| 870 | .name = "clock", |
| 871 | .match_table = clock_match, |
| 872 | .probe = clock_probe, |
| 873 | }; |
| 874 | |
| 875 | static int __init clock_init(void) |
| 876 | { |
| 877 | if (this_is_starfire) { |
| 878 | xtime.tv_sec = starfire_get_time(); |
| 879 | xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ); |
| 880 | set_normalized_timespec(&wall_to_monotonic, |
| 881 | -xtime.tv_sec, -xtime.tv_nsec); |
| 882 | return 0; |
| 883 | } |
| 884 | if (tlb_type == hypervisor) { |
| 885 | xtime.tv_sec = hypervisor_get_time(); |
| 886 | xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ); |
| 887 | set_normalized_timespec(&wall_to_monotonic, |
| 888 | -xtime.tv_sec, -xtime.tv_nsec); |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | return of_register_driver(&clock_driver, &of_bus_type); |
| 893 | } |
| 894 | |
| 895 | /* Must be after subsys_initcall() so that busses are probed. Must |
| 896 | * be before device_initcall() because things like the RTC driver |
| 897 | * need to see the clock registers. |
| 898 | */ |
| 899 | fs_initcall(clock_init); |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | /* This is gets the master TICK_INT timer going. */ |
| 902 | static unsigned long sparc64_init_timers(void) |
| 903 | { |
David S. Miller | 07f8e5f | 2006-06-21 23:34:02 -0700 | [diff] [blame] | 904 | struct device_node *dp; |
| 905 | struct property *prop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | unsigned long clock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | #ifdef CONFIG_SMP |
| 908 | extern void smp_tick_init(void); |
| 909 | #endif |
| 910 | |
David S. Miller | 07f8e5f | 2006-06-21 23:34:02 -0700 | [diff] [blame] | 911 | dp = of_find_node_by_path("/"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | if (tlb_type == spitfire) { |
| 913 | unsigned long ver, manuf, impl; |
| 914 | |
| 915 | __asm__ __volatile__ ("rdpr %%ver, %0" |
| 916 | : "=&r" (ver)); |
| 917 | manuf = ((ver >> 48) & 0xffff); |
| 918 | impl = ((ver >> 32) & 0xffff); |
| 919 | if (manuf == 0x17 && impl == 0x13) { |
| 920 | /* Hummingbird, aka Ultra-IIe */ |
| 921 | tick_ops = &hbtick_operations; |
David S. Miller | 07f8e5f | 2006-06-21 23:34:02 -0700 | [diff] [blame] | 922 | prop = of_find_property(dp, "stick-frequency", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } else { |
| 924 | tick_ops = &tick_operations; |
David S. Miller | 07f8e5f | 2006-06-21 23:34:02 -0700 | [diff] [blame] | 925 | cpu_find_by_instance(0, &dp, NULL); |
| 926 | prop = of_find_property(dp, "clock-frequency", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | } |
| 928 | } else { |
| 929 | tick_ops = &stick_operations; |
David S. Miller | 07f8e5f | 2006-06-21 23:34:02 -0700 | [diff] [blame] | 930 | prop = of_find_property(dp, "stick-frequency", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | } |
David S. Miller | 07f8e5f | 2006-06-21 23:34:02 -0700 | [diff] [blame] | 932 | clock = *(unsigned int *) prop->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | timer_tick_offset = clock / HZ; |
| 934 | |
| 935 | #ifdef CONFIG_SMP |
| 936 | smp_tick_init(); |
| 937 | #endif |
| 938 | |
| 939 | return clock; |
| 940 | } |
| 941 | |
David S. Miller | fd0504c3 | 2006-06-20 01:20:00 -0700 | [diff] [blame] | 942 | static void sparc64_start_timers(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
| 944 | unsigned long pstate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | /* Guarantee that the following sequences execute |
| 947 | * uninterrupted. |
| 948 | */ |
| 949 | __asm__ __volatile__("rdpr %%pstate, %0\n\t" |
| 950 | "wrpr %0, %1, %%pstate" |
| 951 | : "=r" (pstate) |
| 952 | : "i" (PSTATE_IE)); |
| 953 | |
| 954 | tick_ops->init_tick(timer_tick_offset); |
| 955 | |
| 956 | /* Restore PSTATE_IE. */ |
| 957 | __asm__ __volatile__("wrpr %0, 0x0, %%pstate" |
| 958 | : /* no outputs */ |
| 959 | : "r" (pstate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | struct freq_table { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | unsigned long clock_tick_ref; |
| 964 | unsigned int ref_freq; |
| 965 | }; |
David S. Miller | 3763be3 | 2006-02-17 12:33:13 -0800 | [diff] [blame] | 966 | static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 968 | unsigned long sparc64_get_clock_tick(unsigned int cpu) |
| 969 | { |
| 970 | struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu); |
| 971 | |
| 972 | if (ft->clock_tick_ref) |
| 973 | return ft->clock_tick_ref; |
| 974 | return cpu_data(cpu).clock_tick; |
| 975 | } |
| 976 | |
| 977 | #ifdef CONFIG_CPU_FREQ |
| 978 | |
| 979 | static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
| 980 | void *data) |
| 981 | { |
| 982 | struct cpufreq_freqs *freq = data; |
| 983 | unsigned int cpu = freq->cpu; |
| 984 | struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu); |
| 985 | |
| 986 | if (!ft->ref_freq) { |
| 987 | ft->ref_freq = freq->old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | ft->clock_tick_ref = cpu_data(cpu).clock_tick; |
| 989 | } |
| 990 | if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) || |
| 991 | (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) || |
| 992 | (val == CPUFREQ_RESUMECHANGE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | cpu_data(cpu).clock_tick = |
| 994 | cpufreq_scale(ft->clock_tick_ref, |
| 995 | ft->ref_freq, |
| 996 | freq->new); |
| 997 | } |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | static struct notifier_block sparc64_cpufreq_notifier_block = { |
| 1003 | .notifier_call = sparc64_cpufreq_notifier |
| 1004 | }; |
| 1005 | |
| 1006 | #endif /* CONFIG_CPU_FREQ */ |
| 1007 | |
| 1008 | static struct time_interpolator sparc64_cpu_interpolator = { |
| 1009 | .source = TIME_SOURCE_CPU, |
| 1010 | .shift = 16, |
| 1011 | .mask = 0xffffffffffffffffLL |
| 1012 | }; |
| 1013 | |
| 1014 | /* The quotient formula is taken from the IA64 port. */ |
David S. Miller | e17ba8f | 2006-09-23 18:31:42 -0700 | [diff] [blame] | 1015 | #define SPARC64_NSEC_PER_CYC_SHIFT 10UL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | void __init time_init(void) |
| 1017 | { |
| 1018 | unsigned long clock = sparc64_init_timers(); |
| 1019 | |
| 1020 | sparc64_cpu_interpolator.frequency = clock; |
| 1021 | register_time_interpolator(&sparc64_cpu_interpolator); |
| 1022 | |
| 1023 | /* Now that the interpolator is registered, it is |
| 1024 | * safe to start the timer ticking. |
| 1025 | */ |
David S. Miller | fd0504c3 | 2006-06-20 01:20:00 -0700 | [diff] [blame] | 1026 | sparc64_start_timers(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | timer_ticks_per_nsec_quotient = |
| 1029 | (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) + |
| 1030 | (clock / 2)) / clock); |
| 1031 | |
| 1032 | #ifdef CONFIG_CPU_FREQ |
| 1033 | cpufreq_register_notifier(&sparc64_cpufreq_notifier_block, |
| 1034 | CPUFREQ_TRANSITION_NOTIFIER); |
| 1035 | #endif |
| 1036 | } |
| 1037 | |
| 1038 | unsigned long long sched_clock(void) |
| 1039 | { |
| 1040 | unsigned long ticks = tick_ops->get_tick(); |
| 1041 | |
| 1042 | return (ticks * timer_ticks_per_nsec_quotient) |
| 1043 | >> SPARC64_NSEC_PER_CYC_SHIFT; |
| 1044 | } |
| 1045 | |
| 1046 | static int set_rtc_mmss(unsigned long nowtime) |
| 1047 | { |
| 1048 | int real_seconds, real_minutes, chip_minutes; |
Al Viro | ef0299b | 2005-04-24 12:28:36 -0700 | [diff] [blame] | 1049 | void __iomem *mregs = mstk48t02_regs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | #ifdef CONFIG_PCI |
| 1051 | unsigned long dregs = ds1287_regs; |
| 1052 | #else |
| 1053 | unsigned long dregs = 0UL; |
| 1054 | #endif |
| 1055 | unsigned long flags; |
| 1056 | u8 tmp; |
| 1057 | |
| 1058 | /* |
| 1059 | * Not having a register set can lead to trouble. |
| 1060 | * Also starfire doesn't have a tod clock. |
| 1061 | */ |
| 1062 | if (!mregs && !dregs) |
| 1063 | return -1; |
| 1064 | |
| 1065 | if (mregs) { |
| 1066 | spin_lock_irqsave(&mostek_lock, flags); |
| 1067 | |
| 1068 | /* Read the current RTC minutes. */ |
| 1069 | tmp = mostek_read(mregs + MOSTEK_CREG); |
| 1070 | tmp |= MSTK_CREG_READ; |
| 1071 | mostek_write(mregs + MOSTEK_CREG, tmp); |
| 1072 | |
| 1073 | chip_minutes = MSTK_REG_MIN(mregs); |
| 1074 | |
| 1075 | tmp = mostek_read(mregs + MOSTEK_CREG); |
| 1076 | tmp &= ~MSTK_CREG_READ; |
| 1077 | mostek_write(mregs + MOSTEK_CREG, tmp); |
| 1078 | |
| 1079 | /* |
| 1080 | * since we're only adjusting minutes and seconds, |
| 1081 | * don't interfere with hour overflow. This avoids |
| 1082 | * messing with unknown time zones but requires your |
| 1083 | * RTC not to be off by more than 15 minutes |
| 1084 | */ |
| 1085 | real_seconds = nowtime % 60; |
| 1086 | real_minutes = nowtime / 60; |
| 1087 | if (((abs(real_minutes - chip_minutes) + 15)/30) & 1) |
| 1088 | real_minutes += 30; /* correct for half hour time zone */ |
| 1089 | real_minutes %= 60; |
| 1090 | |
| 1091 | if (abs(real_minutes - chip_minutes) < 30) { |
| 1092 | tmp = mostek_read(mregs + MOSTEK_CREG); |
| 1093 | tmp |= MSTK_CREG_WRITE; |
| 1094 | mostek_write(mregs + MOSTEK_CREG, tmp); |
| 1095 | |
| 1096 | MSTK_SET_REG_SEC(mregs,real_seconds); |
| 1097 | MSTK_SET_REG_MIN(mregs,real_minutes); |
| 1098 | |
| 1099 | tmp = mostek_read(mregs + MOSTEK_CREG); |
| 1100 | tmp &= ~MSTK_CREG_WRITE; |
| 1101 | mostek_write(mregs + MOSTEK_CREG, tmp); |
| 1102 | |
| 1103 | spin_unlock_irqrestore(&mostek_lock, flags); |
| 1104 | |
| 1105 | return 0; |
| 1106 | } else { |
| 1107 | spin_unlock_irqrestore(&mostek_lock, flags); |
| 1108 | |
| 1109 | return -1; |
| 1110 | } |
| 1111 | } else { |
| 1112 | int retval = 0; |
| 1113 | unsigned char save_control, save_freq_select; |
| 1114 | |
| 1115 | /* Stolen from arch/i386/kernel/time.c, see there for |
| 1116 | * credits and descriptive comments. |
| 1117 | */ |
| 1118 | spin_lock_irqsave(&rtc_lock, flags); |
| 1119 | save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */ |
| 1120 | CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL); |
| 1121 | |
| 1122 | save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */ |
| 1123 | CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); |
| 1124 | |
| 1125 | chip_minutes = CMOS_READ(RTC_MINUTES); |
| 1126 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
| 1127 | BCD_TO_BIN(chip_minutes); |
| 1128 | real_seconds = nowtime % 60; |
| 1129 | real_minutes = nowtime / 60; |
| 1130 | if (((abs(real_minutes - chip_minutes) + 15)/30) & 1) |
| 1131 | real_minutes += 30; |
| 1132 | real_minutes %= 60; |
| 1133 | |
| 1134 | if (abs(real_minutes - chip_minutes) < 30) { |
| 1135 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
| 1136 | BIN_TO_BCD(real_seconds); |
| 1137 | BIN_TO_BCD(real_minutes); |
| 1138 | } |
| 1139 | CMOS_WRITE(real_seconds,RTC_SECONDS); |
| 1140 | CMOS_WRITE(real_minutes,RTC_MINUTES); |
| 1141 | } else { |
| 1142 | printk(KERN_WARNING |
| 1143 | "set_rtc_mmss: can't update from %d to %d\n", |
| 1144 | chip_minutes, real_minutes); |
| 1145 | retval = -1; |
| 1146 | } |
| 1147 | |
| 1148 | CMOS_WRITE(save_control, RTC_CONTROL); |
| 1149 | CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); |
| 1150 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 1151 | |
| 1152 | return retval; |
| 1153 | } |
| 1154 | } |
David S. Miller | 8ba706a | 2006-03-01 17:32:46 -0800 | [diff] [blame] | 1155 | |
| 1156 | #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */ |
| 1157 | static unsigned char mini_rtc_status; /* bitmapped status byte. */ |
| 1158 | |
| 1159 | /* months start at 0 now */ |
| 1160 | static unsigned char days_in_mo[] = |
| 1161 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 1162 | |
| 1163 | #define FEBRUARY 2 |
| 1164 | #define STARTOFTIME 1970 |
| 1165 | #define SECDAY 86400L |
| 1166 | #define SECYR (SECDAY * 365) |
| 1167 | #define leapyear(year) ((year) % 4 == 0 && \ |
| 1168 | ((year) % 100 != 0 || (year) % 400 == 0)) |
| 1169 | #define days_in_year(a) (leapyear(a) ? 366 : 365) |
| 1170 | #define days_in_month(a) (month_days[(a) - 1]) |
| 1171 | |
| 1172 | static int month_days[12] = { |
| 1173 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 1174 | }; |
| 1175 | |
| 1176 | /* |
| 1177 | * This only works for the Gregorian calendar - i.e. after 1752 (in the UK) |
| 1178 | */ |
| 1179 | static void GregorianDay(struct rtc_time * tm) |
| 1180 | { |
| 1181 | int leapsToDate; |
| 1182 | int lastYear; |
| 1183 | int day; |
| 1184 | int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; |
| 1185 | |
| 1186 | lastYear = tm->tm_year - 1; |
| 1187 | |
| 1188 | /* |
| 1189 | * Number of leap corrections to apply up to end of last year |
| 1190 | */ |
| 1191 | leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400; |
| 1192 | |
| 1193 | /* |
| 1194 | * This year is a leap year if it is divisible by 4 except when it is |
| 1195 | * divisible by 100 unless it is divisible by 400 |
| 1196 | * |
| 1197 | * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was |
| 1198 | */ |
| 1199 | day = tm->tm_mon > 2 && leapyear(tm->tm_year); |
| 1200 | |
| 1201 | day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + |
| 1202 | tm->tm_mday; |
| 1203 | |
| 1204 | tm->tm_wday = day % 7; |
| 1205 | } |
| 1206 | |
| 1207 | static void to_tm(int tim, struct rtc_time *tm) |
| 1208 | { |
| 1209 | register int i; |
| 1210 | register long hms, day; |
| 1211 | |
| 1212 | day = tim / SECDAY; |
| 1213 | hms = tim % SECDAY; |
| 1214 | |
| 1215 | /* Hours, minutes, seconds are easy */ |
| 1216 | tm->tm_hour = hms / 3600; |
| 1217 | tm->tm_min = (hms % 3600) / 60; |
| 1218 | tm->tm_sec = (hms % 3600) % 60; |
| 1219 | |
| 1220 | /* Number of years in days */ |
| 1221 | for (i = STARTOFTIME; day >= days_in_year(i); i++) |
| 1222 | day -= days_in_year(i); |
| 1223 | tm->tm_year = i; |
| 1224 | |
| 1225 | /* Number of months in days left */ |
| 1226 | if (leapyear(tm->tm_year)) |
| 1227 | days_in_month(FEBRUARY) = 29; |
| 1228 | for (i = 1; day >= days_in_month(i); i++) |
| 1229 | day -= days_in_month(i); |
| 1230 | days_in_month(FEBRUARY) = 28; |
| 1231 | tm->tm_mon = i; |
| 1232 | |
| 1233 | /* Days are what is left over (+1) from all that. */ |
| 1234 | tm->tm_mday = day + 1; |
| 1235 | |
| 1236 | /* |
| 1237 | * Determine the day of week |
| 1238 | */ |
| 1239 | GregorianDay(tm); |
| 1240 | } |
| 1241 | |
| 1242 | /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970, |
| 1243 | * aka Unix time. So we have to convert to/from rtc_time. |
| 1244 | */ |
| 1245 | static inline void mini_get_rtc_time(struct rtc_time *time) |
| 1246 | { |
| 1247 | unsigned long flags; |
| 1248 | u32 seconds; |
| 1249 | |
| 1250 | spin_lock_irqsave(&rtc_lock, flags); |
| 1251 | seconds = 0; |
| 1252 | if (this_is_starfire) |
| 1253 | seconds = starfire_get_time(); |
| 1254 | else if (tlb_type == hypervisor) |
| 1255 | seconds = hypervisor_get_time(); |
| 1256 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 1257 | |
| 1258 | to_tm(seconds, time); |
David S. Miller | c4f8ef7 | 2006-03-02 20:28:34 -0800 | [diff] [blame] | 1259 | time->tm_year -= 1900; |
| 1260 | time->tm_mon -= 1; |
David S. Miller | 8ba706a | 2006-03-01 17:32:46 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | static inline int mini_set_rtc_time(struct rtc_time *time) |
| 1264 | { |
| 1265 | u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1, |
| 1266 | time->tm_mday, time->tm_hour, |
| 1267 | time->tm_min, time->tm_sec); |
| 1268 | unsigned long flags; |
| 1269 | int err; |
| 1270 | |
| 1271 | spin_lock_irqsave(&rtc_lock, flags); |
| 1272 | err = -ENODEV; |
| 1273 | if (this_is_starfire) |
| 1274 | err = starfire_set_time(seconds); |
| 1275 | else if (tlb_type == hypervisor) |
| 1276 | err = hypervisor_set_time(seconds); |
| 1277 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 1278 | |
| 1279 | return err; |
| 1280 | } |
| 1281 | |
| 1282 | static int mini_rtc_ioctl(struct inode *inode, struct file *file, |
| 1283 | unsigned int cmd, unsigned long arg) |
| 1284 | { |
| 1285 | struct rtc_time wtime; |
| 1286 | void __user *argp = (void __user *)arg; |
| 1287 | |
| 1288 | switch (cmd) { |
| 1289 | |
| 1290 | case RTC_PLL_GET: |
| 1291 | return -EINVAL; |
| 1292 | |
| 1293 | case RTC_PLL_SET: |
| 1294 | return -EINVAL; |
| 1295 | |
| 1296 | case RTC_UIE_OFF: /* disable ints from RTC updates. */ |
| 1297 | return 0; |
| 1298 | |
| 1299 | case RTC_UIE_ON: /* enable ints for RTC updates. */ |
| 1300 | return -EINVAL; |
| 1301 | |
| 1302 | case RTC_RD_TIME: /* Read the time/date from RTC */ |
| 1303 | /* this doesn't get week-day, who cares */ |
| 1304 | memset(&wtime, 0, sizeof(wtime)); |
| 1305 | mini_get_rtc_time(&wtime); |
| 1306 | |
| 1307 | return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0; |
| 1308 | |
| 1309 | case RTC_SET_TIME: /* Set the RTC */ |
| 1310 | { |
| 1311 | int year; |
| 1312 | unsigned char leap_yr; |
| 1313 | |
| 1314 | if (!capable(CAP_SYS_TIME)) |
| 1315 | return -EACCES; |
| 1316 | |
| 1317 | if (copy_from_user(&wtime, argp, sizeof(wtime))) |
| 1318 | return -EFAULT; |
| 1319 | |
| 1320 | year = wtime.tm_year + 1900; |
| 1321 | leap_yr = ((!(year % 4) && (year % 100)) || |
| 1322 | !(year % 400)); |
| 1323 | |
| 1324 | if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1)) |
| 1325 | return -EINVAL; |
| 1326 | |
| 1327 | if (wtime.tm_mday < 0 || wtime.tm_mday > |
| 1328 | (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr))) |
| 1329 | return -EINVAL; |
| 1330 | |
| 1331 | if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 || |
| 1332 | wtime.tm_min < 0 || wtime.tm_min >= 60 || |
| 1333 | wtime.tm_sec < 0 || wtime.tm_sec >= 60) |
| 1334 | return -EINVAL; |
| 1335 | |
| 1336 | return mini_set_rtc_time(&wtime); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | return -EINVAL; |
| 1341 | } |
| 1342 | |
| 1343 | static int mini_rtc_open(struct inode *inode, struct file *file) |
| 1344 | { |
| 1345 | if (mini_rtc_status & RTC_IS_OPEN) |
| 1346 | return -EBUSY; |
| 1347 | |
| 1348 | mini_rtc_status |= RTC_IS_OPEN; |
| 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | static int mini_rtc_release(struct inode *inode, struct file *file) |
| 1354 | { |
| 1355 | mini_rtc_status &= ~RTC_IS_OPEN; |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 1360 | static const struct file_operations mini_rtc_fops = { |
David S. Miller | 8ba706a | 2006-03-01 17:32:46 -0800 | [diff] [blame] | 1361 | .owner = THIS_MODULE, |
| 1362 | .ioctl = mini_rtc_ioctl, |
| 1363 | .open = mini_rtc_open, |
| 1364 | .release = mini_rtc_release, |
| 1365 | }; |
| 1366 | |
| 1367 | static struct miscdevice rtc_mini_dev = |
| 1368 | { |
| 1369 | .minor = RTC_MINOR, |
| 1370 | .name = "rtc", |
| 1371 | .fops = &mini_rtc_fops, |
| 1372 | }; |
| 1373 | |
| 1374 | static int __init rtc_mini_init(void) |
| 1375 | { |
| 1376 | int retval; |
| 1377 | |
| 1378 | if (tlb_type != hypervisor && !this_is_starfire) |
| 1379 | return -ENODEV; |
| 1380 | |
| 1381 | printk(KERN_INFO "Mini RTC Driver\n"); |
| 1382 | |
| 1383 | retval = misc_register(&rtc_mini_dev); |
| 1384 | if (retval < 0) |
| 1385 | return retval; |
| 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static void __exit rtc_mini_exit(void) |
| 1391 | { |
| 1392 | misc_deregister(&rtc_mini_dev); |
| 1393 | } |
| 1394 | |
| 1395 | |
| 1396 | module_init(rtc_mini_init); |
| 1397 | module_exit(rtc_mini_exit); |