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