blob: 48e1217c1e42ee897f5983300e4853b37a081319 [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>
David S. Miller777a4472007-02-22 06:24:10 -080034#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/oplib.h>
37#include <asm/mostek.h>
38#include <asm/timer.h>
39#include <asm/irq.h>
40#include <asm/io.h>
David S. Millerff0d2fc2006-06-29 15:28:05 -070041#include <asm/prom.h>
42#include <asm/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/starfire.h>
44#include <asm/smp.h>
45#include <asm/sections.h>
46#include <asm/cpudata.h>
David S. Miller8ba706a2006-03-01 17:32:46 -080047#include <asm/uaccess.h>
David S. Miller07f8e5f2006-06-21 23:34:02 -070048#include <asm/prom.h>
Al Viro63540ba2006-10-09 11:51:14 +010049#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51DEFINE_SPINLOCK(mostek_lock);
52DEFINE_SPINLOCK(rtc_lock);
Al Viroef0299b2005-04-24 12:28:36 -070053void __iomem *mstk48t02_regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#ifdef CONFIG_PCI
55unsigned long ds1287_regs = 0UL;
56#endif
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
David S. Millerd369ddd2005-07-10 15:45:11 -0700427unsigned long timer_tick_offset __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David S. Millerd369ddd2005-07-10 15:45:11 -0700429static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431#define TICK_SIZE (tick_nsec / 1000)
432
David S. Millera58c9f32007-02-22 04:16:21 -0800433#define USEC_AFTER 500000
434#define USEC_BEFORE 500000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
David S. Millera58c9f32007-02-22 04:16:21 -0800436static void sync_cmos_clock(unsigned long dummy);
437
438static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
439
440static void sync_cmos_clock(unsigned long dummy)
441{
442 struct timeval now, next;
443 int fail = 1;
444
445 /*
446 * If we have an externally synchronized Linux clock, then update
447 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
448 * called as close as possible to 500 ms before the new second starts.
449 * This code is run on a timer. If the clock is set, that timer
450 * may not expire at the correct time. Thus, we adjust...
451 */
452 if (!ntp_synced())
453 /*
454 * Not synced, exit, do not restart a timer (if one is
455 * running, let it run out).
456 */
457 return;
458
459 do_gettimeofday(&now);
460 if (now.tv_usec >= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 &&
461 now.tv_usec <= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2)
462 fail = set_rtc_mmss(now.tv_sec);
463
464 next.tv_usec = USEC_AFTER - now.tv_usec;
465 if (next.tv_usec <= 0)
466 next.tv_usec += USEC_PER_SEC;
467
468 if (!fail)
469 next.tv_sec = 659;
470 else
471 next.tv_sec = 0;
472
473 if (next.tv_usec >= USEC_PER_SEC) {
474 next.tv_sec++;
475 next.tv_usec -= USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
David S. Millera58c9f32007-02-22 04:16:21 -0800477 mod_timer(&sync_cmos_timer, jiffies + timeval_to_jiffies(&next));
478}
479
480void notify_arch_cmos_timer(void)
481{
482 mod_timer(&sync_cmos_timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
David S. Miller777a4472007-02-22 06:24:10 -0800485void timer_interrupt(int irq, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
David S. Miller777a4472007-02-22 06:24:10 -0800487 struct pt_regs *old_regs = set_irq_regs(regs);
David S. Millerd369ddd2005-07-10 15:45:11 -0700488 unsigned long ticks, compare, pstate;
David S. Miller777a4472007-02-22 06:24:10 -0800489 unsigned long tick_mask = tick_ops->softint_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David S. Miller777a4472007-02-22 06:24:10 -0800491 clear_softint(tick_mask);
492
493 irq_enter();
494
495 kstat_this_cpu.irqs[0]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 do {
Al Viro63540ba2006-10-09 11:51:14 +0100498 profile_tick(CPU_PROFILING);
499 update_process_times(user_mode(get_irq_regs()));
David S. Miller777a4472007-02-22 06:24:10 -0800500
501 if (smp_processor_id() == boot_cpu_id) {
502 write_seqlock(&xtime_lock);
503 do_timer(1);
504 write_sequnlock(&xtime_lock);
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /* Guarantee that the following sequences execute
508 * uninterrupted.
509 */
510 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
511 "wrpr %0, %1, %%pstate"
512 : "=r" (pstate)
513 : "i" (PSTATE_IE));
514
David S. Millerd369ddd2005-07-10 15:45:11 -0700515 compare = tick_ops->add_compare(timer_tick_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 ticks = tick_ops->get_tick();
517
518 /* Restore PSTATE_IE. */
519 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
520 : /* no outputs */
521 : "r" (pstate));
David S. Miller777a4472007-02-22 06:24:10 -0800522 } while (unlikely(time_after_eq(ticks, compare)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David S. Miller777a4472007-02-22 06:24:10 -0800524 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
David S. Miller777a4472007-02-22 06:24:10 -0800526 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
530static void __init kick_start_clock(void)
531{
Al Viroef0299b2005-04-24 12:28:36 -0700532 void __iomem *regs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 u8 sec, tmp;
534 int i, count;
535
536 prom_printf("CLOCK: Clock was stopped. Kick start ");
537
538 spin_lock_irq(&mostek_lock);
539
540 /* Turn on the kick start bit to start the oscillator. */
541 tmp = mostek_read(regs + MOSTEK_CREG);
542 tmp |= MSTK_CREG_WRITE;
543 mostek_write(regs + MOSTEK_CREG, tmp);
544 tmp = mostek_read(regs + MOSTEK_SEC);
545 tmp &= ~MSTK_STOP;
546 mostek_write(regs + MOSTEK_SEC, tmp);
547 tmp = mostek_read(regs + MOSTEK_HOUR);
548 tmp |= MSTK_KICK_START;
549 mostek_write(regs + MOSTEK_HOUR, tmp);
550 tmp = mostek_read(regs + MOSTEK_CREG);
551 tmp &= ~MSTK_CREG_WRITE;
552 mostek_write(regs + MOSTEK_CREG, tmp);
553
554 spin_unlock_irq(&mostek_lock);
555
556 /* Delay to allow the clock oscillator to start. */
557 sec = MSTK_REG_SEC(regs);
558 for (i = 0; i < 3; i++) {
559 while (sec == MSTK_REG_SEC(regs))
560 for (count = 0; count < 100000; count++)
561 /* nothing */ ;
562 prom_printf(".");
563 sec = MSTK_REG_SEC(regs);
564 }
565 prom_printf("\n");
566
567 spin_lock_irq(&mostek_lock);
568
569 /* Turn off kick start and set a "valid" time and date. */
570 tmp = mostek_read(regs + MOSTEK_CREG);
571 tmp |= MSTK_CREG_WRITE;
572 mostek_write(regs + MOSTEK_CREG, tmp);
573 tmp = mostek_read(regs + MOSTEK_HOUR);
574 tmp &= ~MSTK_KICK_START;
575 mostek_write(regs + MOSTEK_HOUR, tmp);
576 MSTK_SET_REG_SEC(regs,0);
577 MSTK_SET_REG_MIN(regs,0);
578 MSTK_SET_REG_HOUR(regs,0);
579 MSTK_SET_REG_DOW(regs,5);
580 MSTK_SET_REG_DOM(regs,1);
581 MSTK_SET_REG_MONTH(regs,8);
582 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
583 tmp = mostek_read(regs + MOSTEK_CREG);
584 tmp &= ~MSTK_CREG_WRITE;
585 mostek_write(regs + MOSTEK_CREG, tmp);
586
587 spin_unlock_irq(&mostek_lock);
588
589 /* Ensure the kick start bit is off. If it isn't, turn it off. */
590 while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
591 prom_printf("CLOCK: Kick start still on!\n");
592
593 spin_lock_irq(&mostek_lock);
594
595 tmp = mostek_read(regs + MOSTEK_CREG);
596 tmp |= MSTK_CREG_WRITE;
597 mostek_write(regs + MOSTEK_CREG, tmp);
598
599 tmp = mostek_read(regs + MOSTEK_HOUR);
600 tmp &= ~MSTK_KICK_START;
601 mostek_write(regs + MOSTEK_HOUR, tmp);
602
603 tmp = mostek_read(regs + MOSTEK_CREG);
604 tmp &= ~MSTK_CREG_WRITE;
605 mostek_write(regs + MOSTEK_CREG, tmp);
606
607 spin_unlock_irq(&mostek_lock);
608 }
609
610 prom_printf("CLOCK: Kick start procedure successful.\n");
611}
612
613/* Return nonzero if the clock chip battery is low. */
614static int __init has_low_battery(void)
615{
Al Viroef0299b2005-04-24 12:28:36 -0700616 void __iomem *regs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 u8 data1, data2;
618
619 spin_lock_irq(&mostek_lock);
620
621 data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
622 mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
623 data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
624 mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
625
626 spin_unlock_irq(&mostek_lock);
627
628 return (data1 == data2); /* Was the write blocked? */
629}
630
631/* Probe for the real time clock chip. */
632static void __init set_system_time(void)
633{
634 unsigned int year, mon, day, hour, min, sec;
Al Viroef0299b2005-04-24 12:28:36 -0700635 void __iomem *mregs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#ifdef CONFIG_PCI
637 unsigned long dregs = ds1287_regs;
638#else
639 unsigned long dregs = 0UL;
640#endif
641 u8 tmp;
642
643 if (!mregs && !dregs) {
644 prom_printf("Something wrong, clock regs not mapped yet.\n");
645 prom_halt();
646 }
647
648 if (mregs) {
649 spin_lock_irq(&mostek_lock);
650
651 /* Traditional Mostek chip. */
652 tmp = mostek_read(mregs + MOSTEK_CREG);
653 tmp |= MSTK_CREG_READ;
654 mostek_write(mregs + MOSTEK_CREG, tmp);
655
656 sec = MSTK_REG_SEC(mregs);
657 min = MSTK_REG_MIN(mregs);
658 hour = MSTK_REG_HOUR(mregs);
659 day = MSTK_REG_DOM(mregs);
660 mon = MSTK_REG_MONTH(mregs);
661 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
662 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* Dallas 12887 RTC chip. */
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 do {
666 sec = CMOS_READ(RTC_SECONDS);
667 min = CMOS_READ(RTC_MINUTES);
668 hour = CMOS_READ(RTC_HOURS);
669 day = CMOS_READ(RTC_DAY_OF_MONTH);
670 mon = CMOS_READ(RTC_MONTH);
671 year = CMOS_READ(RTC_YEAR);
672 } while (sec != CMOS_READ(RTC_SECONDS));
Matt Mackall3dedf532006-03-28 01:56:01 -0800673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
675 BCD_TO_BIN(sec);
676 BCD_TO_BIN(min);
677 BCD_TO_BIN(hour);
678 BCD_TO_BIN(day);
679 BCD_TO_BIN(mon);
680 BCD_TO_BIN(year);
681 }
682 if ((year += 1900) < 1970)
683 year += 100;
684 }
685
686 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
687 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
688 set_normalized_timespec(&wall_to_monotonic,
689 -xtime.tv_sec, -xtime.tv_nsec);
690
691 if (mregs) {
692 tmp = mostek_read(mregs + MOSTEK_CREG);
693 tmp &= ~MSTK_CREG_READ;
694 mostek_write(mregs + MOSTEK_CREG, tmp);
695
696 spin_unlock_irq(&mostek_lock);
697 }
698}
699
David S. Miller4bdff412006-02-11 01:01:55 -0800700/* davem suggests we keep this within the 4M locked kernel image */
701static u32 starfire_get_time(void)
702{
703 static char obp_gettod[32];
704 static u32 unix_tod;
705
706 sprintf(obp_gettod, "h# %08x unix-gettod",
707 (unsigned int) (long) &unix_tod);
708 prom_feval(obp_gettod);
709
710 return unix_tod;
711}
712
David S. Miller8ba706a2006-03-01 17:32:46 -0800713static int starfire_set_time(u32 val)
714{
715 /* Do nothing, time is set using the service processor
716 * console on this platform.
717 */
718 return 0;
719}
720
David S. Miller4bdff412006-02-11 01:01:55 -0800721static u32 hypervisor_get_time(void)
722{
723 register unsigned long func asm("%o5");
724 register unsigned long arg0 asm("%o0");
725 register unsigned long arg1 asm("%o1");
726 int retries = 10000;
727
728retry:
729 func = HV_FAST_TOD_GET;
730 arg0 = 0;
731 arg1 = 0;
732 __asm__ __volatile__("ta %6"
733 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
734 : "0" (func), "1" (arg0), "2" (arg1),
735 "i" (HV_FAST_TRAP));
736 if (arg0 == HV_EOK)
737 return arg1;
738 if (arg0 == HV_EWOULDBLOCK) {
739 if (--retries > 0) {
740 udelay(100);
741 goto retry;
742 }
743 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
744 return 0;
745 }
746 printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
747 return 0;
748}
749
David S. Miller8ba706a2006-03-01 17:32:46 -0800750static int hypervisor_set_time(u32 secs)
751{
752 register unsigned long func asm("%o5");
753 register unsigned long arg0 asm("%o0");
754 int retries = 10000;
755
756retry:
757 func = HV_FAST_TOD_SET;
758 arg0 = secs;
759 __asm__ __volatile__("ta %4"
760 : "=&r" (func), "=&r" (arg0)
761 : "0" (func), "1" (arg0),
762 "i" (HV_FAST_TRAP));
763 if (arg0 == HV_EOK)
764 return 0;
765 if (arg0 == HV_EWOULDBLOCK) {
766 if (--retries > 0) {
767 udelay(100);
768 goto retry;
769 }
770 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
771 return -EAGAIN;
772 }
773 printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
774 return -EOPNOTSUPP;
775}
776
David S. Miller690c8fd2006-06-22 19:12:03 -0700777static int __init clock_model_matches(char *model)
778{
779 if (strcmp(model, "mk48t02") &&
780 strcmp(model, "mk48t08") &&
781 strcmp(model, "mk48t59") &&
782 strcmp(model, "m5819") &&
783 strcmp(model, "m5819p") &&
784 strcmp(model, "m5823") &&
785 strcmp(model, "ds1287"))
786 return 0;
787
788 return 1;
789}
790
David S. Milleree5caf02006-06-29 14:36:52 -0700791static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
David S. Miller690c8fd2006-06-22 19:12:03 -0700792{
David S. Milleree5caf02006-06-29 14:36:52 -0700793 struct device_node *dp = op->node;
794 char *model = of_get_property(dp, "model", NULL);
795 unsigned long size, flags;
796 void __iomem *regs;
David S. Miller690c8fd2006-06-22 19:12:03 -0700797
David S. Milleree5caf02006-06-29 14:36:52 -0700798 if (!model || !clock_model_matches(model))
799 return -ENODEV;
David S. Miller690c8fd2006-06-22 19:12:03 -0700800
David S. Miller91521482006-06-29 14:39:40 -0700801 /* On an Enterprise system there can be multiple mostek clocks.
802 * We should only match the one that is on the central FHC bus.
803 */
David S. Millerc3a8b852006-06-29 14:43:37 -0700804 if (!strcmp(dp->parent->name, "fhc") &&
805 strcmp(dp->parent->parent->name, "central") != 0)
David S. Miller91521482006-06-29 14:39:40 -0700806 return -ENODEV;
807
David S. Milleree5caf02006-06-29 14:36:52 -0700808 size = (op->resource[0].end - op->resource[0].start) + 1;
809 regs = of_ioremap(&op->resource[0], 0, size, "clock");
810 if (!regs)
811 return -ENOMEM;
David S. Miller690c8fd2006-06-22 19:12:03 -0700812
Randy Dunlap72335892006-07-05 20:18:39 -0700813#ifdef CONFIG_PCI
David S. Miller690c8fd2006-06-22 19:12:03 -0700814 if (!strcmp(model, "ds1287") ||
815 !strcmp(model, "m5819") ||
816 !strcmp(model, "m5819p") ||
817 !strcmp(model, "m5823")) {
David S. Milleree5caf02006-06-29 14:36:52 -0700818 ds1287_regs = (unsigned long) regs;
Randy Dunlap72335892006-07-05 20:18:39 -0700819 } else
820#endif
821 if (model[5] == '0' && model[6] == '2') {
David S. Milleree5caf02006-06-29 14:36:52 -0700822 mstk48t02_regs = regs;
823 } else if(model[5] == '0' && model[6] == '8') {
824 mstk48t08_regs = regs;
825 mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
David S. Miller690c8fd2006-06-22 19:12:03 -0700826 } else {
David S. Milleree5caf02006-06-29 14:36:52 -0700827 mstk48t59_regs = regs;
David S. Miller690c8fd2006-06-22 19:12:03 -0700828 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
829 }
David S. Miller690c8fd2006-06-22 19:12:03 -0700830
David S. Milleree5caf02006-06-29 14:36:52 -0700831 printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
David S. Miller690c8fd2006-06-22 19:12:03 -0700833 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
David S. Millerb4bca262005-04-21 21:42:34 -0700835 if (mstk48t02_regs != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Report a low battery voltage condition. */
837 if (has_low_battery())
838 prom_printf("NVRAM: Low battery voltage!\n");
839
840 /* Kick start the clock if it is completely stopped. */
841 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
842 kick_start_clock();
843 }
844
845 set_system_time();
846
847 local_irq_restore(flags);
David S. Milleree5caf02006-06-29 14:36:52 -0700848
849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
David S. Milleree5caf02006-06-29 14:36:52 -0700852static struct of_device_id clock_match[] = {
853 {
854 .name = "eeprom",
855 },
856 {
857 .name = "rtc",
858 },
859 {},
860};
861
862static struct of_platform_driver clock_driver = {
863 .name = "clock",
864 .match_table = clock_match,
865 .probe = clock_probe,
866};
867
868static int __init clock_init(void)
869{
870 if (this_is_starfire) {
871 xtime.tv_sec = starfire_get_time();
872 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
873 set_normalized_timespec(&wall_to_monotonic,
874 -xtime.tv_sec, -xtime.tv_nsec);
875 return 0;
876 }
877 if (tlb_type == hypervisor) {
878 xtime.tv_sec = hypervisor_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
885 return of_register_driver(&clock_driver, &of_bus_type);
886}
887
888/* Must be after subsys_initcall() so that busses are probed. Must
889 * be before device_initcall() because things like the RTC driver
890 * need to see the clock registers.
891 */
892fs_initcall(clock_init);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/* This is gets the master TICK_INT timer going. */
895static unsigned long sparc64_init_timers(void)
896{
David S. Miller07f8e5f2006-06-21 23:34:02 -0700897 struct device_node *dp;
898 struct property *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned long clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900#ifdef CONFIG_SMP
901 extern void smp_tick_init(void);
902#endif
903
David S. Miller07f8e5f2006-06-21 23:34:02 -0700904 dp = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (tlb_type == spitfire) {
906 unsigned long ver, manuf, impl;
907
908 __asm__ __volatile__ ("rdpr %%ver, %0"
909 : "=&r" (ver));
910 manuf = ((ver >> 48) & 0xffff);
911 impl = ((ver >> 32) & 0xffff);
912 if (manuf == 0x17 && impl == 0x13) {
913 /* Hummingbird, aka Ultra-IIe */
914 tick_ops = &hbtick_operations;
David S. Miller07f8e5f2006-06-21 23:34:02 -0700915 prop = of_find_property(dp, "stick-frequency", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 } else {
917 tick_ops = &tick_operations;
David S. Miller07f8e5f2006-06-21 23:34:02 -0700918 cpu_find_by_instance(0, &dp, NULL);
919 prop = of_find_property(dp, "clock-frequency", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 } else {
922 tick_ops = &stick_operations;
David S. Miller07f8e5f2006-06-21 23:34:02 -0700923 prop = of_find_property(dp, "stick-frequency", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
David S. Miller07f8e5f2006-06-21 23:34:02 -0700925 clock = *(unsigned int *) prop->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 timer_tick_offset = clock / HZ;
927
928#ifdef CONFIG_SMP
929 smp_tick_init();
930#endif
931
932 return clock;
933}
934
David S. Millerfd0504c32006-06-20 01:20:00 -0700935static void sparc64_start_timers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 unsigned long pstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* Guarantee that the following sequences execute
940 * uninterrupted.
941 */
942 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
943 "wrpr %0, %1, %%pstate"
944 : "=r" (pstate)
945 : "i" (PSTATE_IE));
946
947 tick_ops->init_tick(timer_tick_offset);
948
949 /* Restore PSTATE_IE. */
950 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
951 : /* no outputs */
952 : "r" (pstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955struct freq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned long clock_tick_ref;
957 unsigned int ref_freq;
958};
David S. Miller3763be32006-02-17 12:33:13 -0800959static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961unsigned long sparc64_get_clock_tick(unsigned int cpu)
962{
963 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
964
965 if (ft->clock_tick_ref)
966 return ft->clock_tick_ref;
967 return cpu_data(cpu).clock_tick;
968}
969
970#ifdef CONFIG_CPU_FREQ
971
972static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
973 void *data)
974{
975 struct cpufreq_freqs *freq = data;
976 unsigned int cpu = freq->cpu;
977 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
978
979 if (!ft->ref_freq) {
980 ft->ref_freq = freq->old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
982 }
983 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
984 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
985 (val == CPUFREQ_RESUMECHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 cpu_data(cpu).clock_tick =
987 cpufreq_scale(ft->clock_tick_ref,
988 ft->ref_freq,
989 freq->new);
990 }
991
992 return 0;
993}
994
995static struct notifier_block sparc64_cpufreq_notifier_block = {
996 .notifier_call = sparc64_cpufreq_notifier
997};
998
999#endif /* CONFIG_CPU_FREQ */
1000
1001static struct time_interpolator sparc64_cpu_interpolator = {
1002 .source = TIME_SOURCE_CPU,
1003 .shift = 16,
1004 .mask = 0xffffffffffffffffLL
1005};
1006
1007/* The quotient formula is taken from the IA64 port. */
David S. Millere17ba8f2006-09-23 18:31:42 -07001008#define SPARC64_NSEC_PER_CYC_SHIFT 10UL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009void __init time_init(void)
1010{
1011 unsigned long clock = sparc64_init_timers();
1012
1013 sparc64_cpu_interpolator.frequency = clock;
1014 register_time_interpolator(&sparc64_cpu_interpolator);
1015
1016 /* Now that the interpolator is registered, it is
1017 * safe to start the timer ticking.
1018 */
David S. Millerfd0504c32006-06-20 01:20:00 -07001019 sparc64_start_timers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 timer_ticks_per_nsec_quotient =
1022 (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
1023 (clock / 2)) / clock);
1024
1025#ifdef CONFIG_CPU_FREQ
1026 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
1027 CPUFREQ_TRANSITION_NOTIFIER);
1028#endif
1029}
1030
1031unsigned long long sched_clock(void)
1032{
1033 unsigned long ticks = tick_ops->get_tick();
1034
1035 return (ticks * timer_ticks_per_nsec_quotient)
1036 >> SPARC64_NSEC_PER_CYC_SHIFT;
1037}
1038
1039static int set_rtc_mmss(unsigned long nowtime)
1040{
1041 int real_seconds, real_minutes, chip_minutes;
Al Viroef0299b2005-04-24 12:28:36 -07001042 void __iomem *mregs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#ifdef CONFIG_PCI
1044 unsigned long dregs = ds1287_regs;
1045#else
1046 unsigned long dregs = 0UL;
1047#endif
1048 unsigned long flags;
1049 u8 tmp;
1050
1051 /*
1052 * Not having a register set can lead to trouble.
1053 * Also starfire doesn't have a tod clock.
1054 */
1055 if (!mregs && !dregs)
1056 return -1;
1057
1058 if (mregs) {
1059 spin_lock_irqsave(&mostek_lock, flags);
1060
1061 /* Read the current RTC minutes. */
1062 tmp = mostek_read(mregs + MOSTEK_CREG);
1063 tmp |= MSTK_CREG_READ;
1064 mostek_write(mregs + MOSTEK_CREG, tmp);
1065
1066 chip_minutes = MSTK_REG_MIN(mregs);
1067
1068 tmp = mostek_read(mregs + MOSTEK_CREG);
1069 tmp &= ~MSTK_CREG_READ;
1070 mostek_write(mregs + MOSTEK_CREG, tmp);
1071
1072 /*
1073 * since we're only adjusting minutes and seconds,
1074 * don't interfere with hour overflow. This avoids
1075 * messing with unknown time zones but requires your
1076 * RTC not to be off by more than 15 minutes
1077 */
1078 real_seconds = nowtime % 60;
1079 real_minutes = nowtime / 60;
1080 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1081 real_minutes += 30; /* correct for half hour time zone */
1082 real_minutes %= 60;
1083
1084 if (abs(real_minutes - chip_minutes) < 30) {
1085 tmp = mostek_read(mregs + MOSTEK_CREG);
1086 tmp |= MSTK_CREG_WRITE;
1087 mostek_write(mregs + MOSTEK_CREG, tmp);
1088
1089 MSTK_SET_REG_SEC(mregs,real_seconds);
1090 MSTK_SET_REG_MIN(mregs,real_minutes);
1091
1092 tmp = mostek_read(mregs + MOSTEK_CREG);
1093 tmp &= ~MSTK_CREG_WRITE;
1094 mostek_write(mregs + MOSTEK_CREG, tmp);
1095
1096 spin_unlock_irqrestore(&mostek_lock, flags);
1097
1098 return 0;
1099 } else {
1100 spin_unlock_irqrestore(&mostek_lock, flags);
1101
1102 return -1;
1103 }
1104 } else {
1105 int retval = 0;
1106 unsigned char save_control, save_freq_select;
1107
1108 /* Stolen from arch/i386/kernel/time.c, see there for
1109 * credits and descriptive comments.
1110 */
1111 spin_lock_irqsave(&rtc_lock, flags);
1112 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
1113 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1114
1115 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
1116 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1117
1118 chip_minutes = CMOS_READ(RTC_MINUTES);
1119 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1120 BCD_TO_BIN(chip_minutes);
1121 real_seconds = nowtime % 60;
1122 real_minutes = nowtime / 60;
1123 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1124 real_minutes += 30;
1125 real_minutes %= 60;
1126
1127 if (abs(real_minutes - chip_minutes) < 30) {
1128 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1129 BIN_TO_BCD(real_seconds);
1130 BIN_TO_BCD(real_minutes);
1131 }
1132 CMOS_WRITE(real_seconds,RTC_SECONDS);
1133 CMOS_WRITE(real_minutes,RTC_MINUTES);
1134 } else {
1135 printk(KERN_WARNING
1136 "set_rtc_mmss: can't update from %d to %d\n",
1137 chip_minutes, real_minutes);
1138 retval = -1;
1139 }
1140
1141 CMOS_WRITE(save_control, RTC_CONTROL);
1142 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1143 spin_unlock_irqrestore(&rtc_lock, flags);
1144
1145 return retval;
1146 }
1147}
David S. Miller8ba706a2006-03-01 17:32:46 -08001148
1149#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
1150static unsigned char mini_rtc_status; /* bitmapped status byte. */
1151
1152/* months start at 0 now */
1153static unsigned char days_in_mo[] =
1154{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1155
1156#define FEBRUARY 2
1157#define STARTOFTIME 1970
1158#define SECDAY 86400L
1159#define SECYR (SECDAY * 365)
1160#define leapyear(year) ((year) % 4 == 0 && \
1161 ((year) % 100 != 0 || (year) % 400 == 0))
1162#define days_in_year(a) (leapyear(a) ? 366 : 365)
1163#define days_in_month(a) (month_days[(a) - 1])
1164
1165static int month_days[12] = {
1166 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1167};
1168
1169/*
1170 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1171 */
1172static void GregorianDay(struct rtc_time * tm)
1173{
1174 int leapsToDate;
1175 int lastYear;
1176 int day;
1177 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1178
1179 lastYear = tm->tm_year - 1;
1180
1181 /*
1182 * Number of leap corrections to apply up to end of last year
1183 */
1184 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1185
1186 /*
1187 * This year is a leap year if it is divisible by 4 except when it is
1188 * divisible by 100 unless it is divisible by 400
1189 *
1190 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1191 */
1192 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1193
1194 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1195 tm->tm_mday;
1196
1197 tm->tm_wday = day % 7;
1198}
1199
1200static void to_tm(int tim, struct rtc_time *tm)
1201{
1202 register int i;
1203 register long hms, day;
1204
1205 day = tim / SECDAY;
1206 hms = tim % SECDAY;
1207
1208 /* Hours, minutes, seconds are easy */
1209 tm->tm_hour = hms / 3600;
1210 tm->tm_min = (hms % 3600) / 60;
1211 tm->tm_sec = (hms % 3600) % 60;
1212
1213 /* Number of years in days */
1214 for (i = STARTOFTIME; day >= days_in_year(i); i++)
1215 day -= days_in_year(i);
1216 tm->tm_year = i;
1217
1218 /* Number of months in days left */
1219 if (leapyear(tm->tm_year))
1220 days_in_month(FEBRUARY) = 29;
1221 for (i = 1; day >= days_in_month(i); i++)
1222 day -= days_in_month(i);
1223 days_in_month(FEBRUARY) = 28;
1224 tm->tm_mon = i;
1225
1226 /* Days are what is left over (+1) from all that. */
1227 tm->tm_mday = day + 1;
1228
1229 /*
1230 * Determine the day of week
1231 */
1232 GregorianDay(tm);
1233}
1234
1235/* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1236 * aka Unix time. So we have to convert to/from rtc_time.
1237 */
1238static inline void mini_get_rtc_time(struct rtc_time *time)
1239{
1240 unsigned long flags;
1241 u32 seconds;
1242
1243 spin_lock_irqsave(&rtc_lock, flags);
1244 seconds = 0;
1245 if (this_is_starfire)
1246 seconds = starfire_get_time();
1247 else if (tlb_type == hypervisor)
1248 seconds = hypervisor_get_time();
1249 spin_unlock_irqrestore(&rtc_lock, flags);
1250
1251 to_tm(seconds, time);
David S. Millerc4f8ef72006-03-02 20:28:34 -08001252 time->tm_year -= 1900;
1253 time->tm_mon -= 1;
David S. Miller8ba706a2006-03-01 17:32:46 -08001254}
1255
1256static inline int mini_set_rtc_time(struct rtc_time *time)
1257{
1258 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1259 time->tm_mday, time->tm_hour,
1260 time->tm_min, time->tm_sec);
1261 unsigned long flags;
1262 int err;
1263
1264 spin_lock_irqsave(&rtc_lock, flags);
1265 err = -ENODEV;
1266 if (this_is_starfire)
1267 err = starfire_set_time(seconds);
1268 else if (tlb_type == hypervisor)
1269 err = hypervisor_set_time(seconds);
1270 spin_unlock_irqrestore(&rtc_lock, flags);
1271
1272 return err;
1273}
1274
1275static int mini_rtc_ioctl(struct inode *inode, struct file *file,
1276 unsigned int cmd, unsigned long arg)
1277{
1278 struct rtc_time wtime;
1279 void __user *argp = (void __user *)arg;
1280
1281 switch (cmd) {
1282
1283 case RTC_PLL_GET:
1284 return -EINVAL;
1285
1286 case RTC_PLL_SET:
1287 return -EINVAL;
1288
1289 case RTC_UIE_OFF: /* disable ints from RTC updates. */
1290 return 0;
1291
1292 case RTC_UIE_ON: /* enable ints for RTC updates. */
1293 return -EINVAL;
1294
1295 case RTC_RD_TIME: /* Read the time/date from RTC */
1296 /* this doesn't get week-day, who cares */
1297 memset(&wtime, 0, sizeof(wtime));
1298 mini_get_rtc_time(&wtime);
1299
1300 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
1301
1302 case RTC_SET_TIME: /* Set the RTC */
1303 {
1304 int year;
1305 unsigned char leap_yr;
1306
1307 if (!capable(CAP_SYS_TIME))
1308 return -EACCES;
1309
1310 if (copy_from_user(&wtime, argp, sizeof(wtime)))
1311 return -EFAULT;
1312
1313 year = wtime.tm_year + 1900;
1314 leap_yr = ((!(year % 4) && (year % 100)) ||
1315 !(year % 400));
1316
1317 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
1318 return -EINVAL;
1319
1320 if (wtime.tm_mday < 0 || wtime.tm_mday >
1321 (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
1322 return -EINVAL;
1323
1324 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
1325 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
1326 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
1327 return -EINVAL;
1328
1329 return mini_set_rtc_time(&wtime);
1330 }
1331 }
1332
1333 return -EINVAL;
1334}
1335
1336static int mini_rtc_open(struct inode *inode, struct file *file)
1337{
1338 if (mini_rtc_status & RTC_IS_OPEN)
1339 return -EBUSY;
1340
1341 mini_rtc_status |= RTC_IS_OPEN;
1342
1343 return 0;
1344}
1345
1346static int mini_rtc_release(struct inode *inode, struct file *file)
1347{
1348 mini_rtc_status &= ~RTC_IS_OPEN;
1349 return 0;
1350}
1351
1352
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001353static const struct file_operations mini_rtc_fops = {
David S. Miller8ba706a2006-03-01 17:32:46 -08001354 .owner = THIS_MODULE,
1355 .ioctl = mini_rtc_ioctl,
1356 .open = mini_rtc_open,
1357 .release = mini_rtc_release,
1358};
1359
1360static struct miscdevice rtc_mini_dev =
1361{
1362 .minor = RTC_MINOR,
1363 .name = "rtc",
1364 .fops = &mini_rtc_fops,
1365};
1366
1367static int __init rtc_mini_init(void)
1368{
1369 int retval;
1370
1371 if (tlb_type != hypervisor && !this_is_starfire)
1372 return -ENODEV;
1373
1374 printk(KERN_INFO "Mini RTC Driver\n");
1375
1376 retval = misc_register(&rtc_mini_dev);
1377 if (retval < 0)
1378 return retval;
1379
1380 return 0;
1381}
1382
1383static void __exit rtc_mini_exit(void)
1384{
1385 misc_deregister(&rtc_mini_dev);
1386}
1387
1388
1389module_init(rtc_mini_init);
1390module_exit(rtc_mini_exit);