blob: 348b820355615b221d71c83741fe92580053ae99 [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
12#include <linux/config.h>
13#include <linux/errno.h>
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/param.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/interrupt.h>
21#include <linux/time.h>
22#include <linux/timex.h>
23#include <linux/init.h>
24#include <linux/ioport.h>
25#include <linux/mc146818rtc.h>
26#include <linux/delay.h>
27#include <linux/profile.h>
28#include <linux/bcd.h>
29#include <linux/jiffies.h>
30#include <linux/cpufreq.h>
31#include <linux/percpu.h>
32#include <linux/profile.h>
David S. Miller8ba706a2006-03-01 17:32:46 -080033#include <linux/miscdevice.h>
34#include <linux/rtc.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>
41#include <asm/sbus.h>
42#include <asm/fhc.h>
43#include <asm/pbm.h>
44#include <asm/ebus.h>
45#include <asm/isa.h>
46#include <asm/starfire.h>
47#include <asm/smp.h>
48#include <asm/sections.h>
49#include <asm/cpudata.h>
David S. Miller8ba706a2006-03-01 17:32:46 -080050#include <asm/uaccess.h>
David S. Miller07f8e5f2006-06-21 23:34:02 -070051#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53DEFINE_SPINLOCK(mostek_lock);
54DEFINE_SPINLOCK(rtc_lock);
Al Viroef0299b2005-04-24 12:28:36 -070055void __iomem *mstk48t02_regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef CONFIG_PCI
57unsigned long ds1287_regs = 0UL;
58#endif
59
60extern unsigned long wall_jiffies;
61
Al Viroef0299b2005-04-24 12:28:36 -070062static void __iomem *mstk48t08_regs;
63static void __iomem *mstk48t59_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static int set_rtc_mmss(unsigned long);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define TICK_PRIV_BIT (1UL << 63)
68
69#ifdef CONFIG_SMP
70unsigned long profile_pc(struct pt_regs *regs)
71{
72 unsigned long pc = instruction_pointer(regs);
73
74 if (in_lock_functions(pc))
75 return regs->u_regs[UREG_RETPC];
76 return pc;
77}
78EXPORT_SYMBOL(profile_pc);
79#endif
80
81static void tick_disable_protection(void)
82{
83 /* Set things up so user can access tick register for profiling
84 * purposes. Also workaround BB_ERRATA_1 by doing a dummy
85 * read back of %tick after writing it.
86 */
87 __asm__ __volatile__(
88 " ba,pt %%xcc, 1f\n"
89 " nop\n"
90 " .align 64\n"
91 "1: rd %%tick, %%g2\n"
92 " add %%g2, 6, %%g2\n"
93 " andn %%g2, %0, %%g2\n"
94 " wrpr %%g2, 0, %%tick\n"
95 " rdpr %%tick, %%g0"
96 : /* no outputs */
97 : "r" (TICK_PRIV_BIT)
98 : "g2");
99}
100
101static void tick_init_tick(unsigned long offset)
102{
103 tick_disable_protection();
104
105 __asm__ __volatile__(
106 " rd %%tick, %%g1\n"
107 " andn %%g1, %1, %%g1\n"
108 " ba,pt %%xcc, 1f\n"
109 " add %%g1, %0, %%g1\n"
110 " .align 64\n"
111 "1: wr %%g1, 0x0, %%tick_cmpr\n"
112 " rd %%tick_cmpr, %%g0"
113 : /* no outputs */
114 : "r" (offset), "r" (TICK_PRIV_BIT)
115 : "g1");
116}
117
118static unsigned long tick_get_tick(void)
119{
120 unsigned long ret;
121
122 __asm__ __volatile__("rd %%tick, %0\n\t"
123 "mov %0, %0"
124 : "=r" (ret));
125
126 return ret & ~TICK_PRIV_BIT;
127}
128
129static unsigned long tick_get_compare(void)
130{
131 unsigned long ret;
132
133 __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
134 "mov %0, %0"
135 : "=r" (ret));
136
137 return ret;
138}
139
140static unsigned long tick_add_compare(unsigned long adj)
141{
142 unsigned long new_compare;
143
144 /* Workaround for Spitfire Errata (#54 I think??), I discovered
145 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
146 * number 103640.
147 *
148 * On Blackbird writes to %tick_cmpr can fail, the
149 * workaround seems to be to execute the wr instruction
150 * at the start of an I-cache line, and perform a dummy
151 * read back from %tick_cmpr right after writing to it. -DaveM
152 */
153 __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
154 "ba,pt %%xcc, 1f\n\t"
155 " add %0, %1, %0\n\t"
156 ".align 64\n"
157 "1:\n\t"
158 "wr %0, 0, %%tick_cmpr\n\t"
159 "rd %%tick_cmpr, %%g0"
160 : "=&r" (new_compare)
161 : "r" (adj));
162
163 return new_compare;
164}
165
166static unsigned long tick_add_tick(unsigned long adj, unsigned long offset)
167{
168 unsigned long new_tick, tmp;
169
170 /* Also need to handle Blackbird bug here too. */
171 __asm__ __volatile__("rd %%tick, %0\n\t"
172 "add %0, %2, %0\n\t"
173 "wrpr %0, 0, %%tick\n\t"
174 "andn %0, %4, %1\n\t"
175 "ba,pt %%xcc, 1f\n\t"
176 " add %1, %3, %1\n\t"
177 ".align 64\n"
178 "1:\n\t"
179 "wr %1, 0, %%tick_cmpr\n\t"
180 "rd %%tick_cmpr, %%g0"
181 : "=&r" (new_tick), "=&r" (tmp)
182 : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
183
184 return new_tick;
185}
186
David S. Millerd369ddd2005-07-10 15:45:11 -0700187static struct sparc64_tick_ops tick_operations __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .init_tick = tick_init_tick,
189 .get_tick = tick_get_tick,
190 .get_compare = tick_get_compare,
191 .add_tick = tick_add_tick,
192 .add_compare = tick_add_compare,
193 .softint_mask = 1UL << 0,
194};
195
David S. Millerfc321492005-11-07 14:10:10 -0800196struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static void stick_init_tick(unsigned long offset)
199{
David S. Miller7aa62642006-02-11 23:14:59 -0800200 /* Writes to the %tick and %stick register are not
201 * allowed on sun4v. The Hypervisor controls that
202 * bit, per-strand.
203 */
204 if (tlb_type != hypervisor) {
205 tick_disable_protection();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
David S. Miller7aa62642006-02-11 23:14:59 -0800207 /* Let the user get at STICK too. */
208 __asm__ __volatile__(
209 " rd %%asr24, %%g2\n"
210 " andn %%g2, %0, %%g2\n"
211 " wr %%g2, 0, %%asr24"
212 : /* no outputs */
213 : "r" (TICK_PRIV_BIT)
214 : "g1", "g2");
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 __asm__ __volatile__(
218 " rd %%asr24, %%g1\n"
219 " andn %%g1, %1, %%g1\n"
220 " add %%g1, %0, %%g1\n"
221 " wr %%g1, 0x0, %%asr25"
222 : /* no outputs */
223 : "r" (offset), "r" (TICK_PRIV_BIT)
224 : "g1");
225}
226
227static unsigned long stick_get_tick(void)
228{
229 unsigned long ret;
230
231 __asm__ __volatile__("rd %%asr24, %0"
232 : "=r" (ret));
233
234 return ret & ~TICK_PRIV_BIT;
235}
236
237static unsigned long stick_get_compare(void)
238{
239 unsigned long ret;
240
241 __asm__ __volatile__("rd %%asr25, %0"
242 : "=r" (ret));
243
244 return ret;
245}
246
247static unsigned long stick_add_tick(unsigned long adj, unsigned long offset)
248{
249 unsigned long new_tick, tmp;
250
251 __asm__ __volatile__("rd %%asr24, %0\n\t"
252 "add %0, %2, %0\n\t"
253 "wr %0, 0, %%asr24\n\t"
254 "andn %0, %4, %1\n\t"
255 "add %1, %3, %1\n\t"
256 "wr %1, 0, %%asr25"
257 : "=&r" (new_tick), "=&r" (tmp)
258 : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
259
260 return new_tick;
261}
262
263static unsigned long stick_add_compare(unsigned long adj)
264{
265 unsigned long new_compare;
266
267 __asm__ __volatile__("rd %%asr25, %0\n\t"
268 "add %0, %1, %0\n\t"
269 "wr %0, 0, %%asr25"
270 : "=&r" (new_compare)
271 : "r" (adj));
272
273 return new_compare;
274}
275
David S. Millerd369ddd2005-07-10 15:45:11 -0700276static struct sparc64_tick_ops stick_operations __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .init_tick = stick_init_tick,
278 .get_tick = stick_get_tick,
279 .get_compare = stick_get_compare,
280 .add_tick = stick_add_tick,
281 .add_compare = stick_add_compare,
282 .softint_mask = 1UL << 16,
283};
284
285/* On Hummingbird the STICK/STICK_CMPR register is implemented
286 * in I/O space. There are two 64-bit registers each, the
287 * first holds the low 32-bits of the value and the second holds
288 * the high 32-bits.
289 *
290 * Since STICK is constantly updating, we have to access it carefully.
291 *
292 * The sequence we use to read is:
Richard Mortimer9eb33942006-01-17 15:21:01 -0800293 * 1) read high
294 * 2) read low
295 * 3) read high again, if it rolled re-read both low and high again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Writing STICK safely is also tricky:
298 * 1) write low to zero
299 * 2) write high
300 * 3) write low
301 */
302#define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
303#define HBIRD_STICK_ADDR 0x1fe0000f070UL
304
305static unsigned long __hbird_read_stick(void)
306{
307 unsigned long ret, tmp1, tmp2, tmp3;
Richard Mortimer9eb33942006-01-17 15:21:01 -0800308 unsigned long addr = HBIRD_STICK_ADDR+8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Richard Mortimer9eb33942006-01-17 15:21:01 -0800310 __asm__ __volatile__("ldxa [%1] %5, %2\n"
311 "1:\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 "sub %1, 0x8, %1\n\t"
Richard Mortimer9eb33942006-01-17 15:21:01 -0800313 "ldxa [%1] %5, %3\n\t"
314 "add %1, 0x8, %1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 "ldxa [%1] %5, %4\n\t"
316 "cmp %4, %2\n\t"
Richard Mortimer9eb33942006-01-17 15:21:01 -0800317 "bne,a,pn %%xcc, 1b\n\t"
318 " mov %4, %2\n\t"
319 "sllx %4, 32, %4\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 "or %3, %4, %0\n\t"
321 : "=&r" (ret), "=&r" (addr),
322 "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
323 : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
324
325 return ret;
326}
327
328static unsigned long __hbird_read_compare(void)
329{
330 unsigned long low, high;
331 unsigned long addr = HBIRD_STICKCMP_ADDR;
332
333 __asm__ __volatile__("ldxa [%2] %3, %0\n\t"
334 "add %2, 0x8, %2\n\t"
335 "ldxa [%2] %3, %1"
336 : "=&r" (low), "=&r" (high), "=&r" (addr)
337 : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr));
338
339 return (high << 32UL) | low;
340}
341
342static void __hbird_write_stick(unsigned long val)
343{
344 unsigned long low = (val & 0xffffffffUL);
345 unsigned long high = (val >> 32UL);
346 unsigned long addr = HBIRD_STICK_ADDR;
347
348 __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
349 "add %0, 0x8, %0\n\t"
350 "stxa %3, [%0] %4\n\t"
351 "sub %0, 0x8, %0\n\t"
352 "stxa %2, [%0] %4"
353 : "=&r" (addr)
354 : "0" (addr), "r" (low), "r" (high),
355 "i" (ASI_PHYS_BYPASS_EC_E));
356}
357
358static void __hbird_write_compare(unsigned long val)
359{
360 unsigned long low = (val & 0xffffffffUL);
361 unsigned long high = (val >> 32UL);
362 unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
363
364 __asm__ __volatile__("stxa %3, [%0] %4\n\t"
365 "sub %0, 0x8, %0\n\t"
366 "stxa %2, [%0] %4"
367 : "=&r" (addr)
368 : "0" (addr), "r" (low), "r" (high),
369 "i" (ASI_PHYS_BYPASS_EC_E));
370}
371
372static void hbtick_init_tick(unsigned long offset)
373{
374 unsigned long val;
375
376 tick_disable_protection();
377
378 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
379 * XXX into actually sending STICK interrupts. I think because
380 * XXX of how we store %tick_cmpr in head.S this somehow resets the
381 * XXX {TICK + STICK} interrupt mux. -DaveM
382 */
383 __hbird_write_stick(__hbird_read_stick());
384
385 val = __hbird_read_stick() & ~TICK_PRIV_BIT;
386 __hbird_write_compare(val + offset);
387}
388
389static unsigned long hbtick_get_tick(void)
390{
391 return __hbird_read_stick() & ~TICK_PRIV_BIT;
392}
393
394static unsigned long hbtick_get_compare(void)
395{
396 return __hbird_read_compare();
397}
398
399static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset)
400{
401 unsigned long val;
402
403 val = __hbird_read_stick() + adj;
404 __hbird_write_stick(val);
405
406 val &= ~TICK_PRIV_BIT;
407 __hbird_write_compare(val + offset);
408
409 return val;
410}
411
412static unsigned long hbtick_add_compare(unsigned long adj)
413{
414 unsigned long val = __hbird_read_compare() + adj;
415
416 val &= ~TICK_PRIV_BIT;
417 __hbird_write_compare(val);
418
419 return val;
420}
421
David S. Millerd369ddd2005-07-10 15:45:11 -0700422static struct sparc64_tick_ops hbtick_operations __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .init_tick = hbtick_init_tick,
424 .get_tick = hbtick_get_tick,
425 .get_compare = hbtick_get_compare,
426 .add_tick = hbtick_add_tick,
427 .add_compare = hbtick_add_compare,
428 .softint_mask = 1UL << 0,
429};
430
431/* timer_interrupt() needs to keep up the real-time clock,
432 * as well as call the "do_timer()" routine every clocktick
433 *
434 * NOTE: On SUN5 systems the ticker interrupt comes in using 2
435 * interrupts, one at level14 and one with softint bit 0.
436 */
David S. Millerd369ddd2005-07-10 15:45:11 -0700437unsigned long timer_tick_offset __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
David S. Millerd369ddd2005-07-10 15:45:11 -0700439static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441#define TICK_SIZE (tick_nsec / 1000)
442
443static inline void timer_check_rtc(void)
444{
445 /* last time the cmos clock got updated */
446 static long last_rtc_update;
447
448 /* Determine when to update the Mostek clock. */
john stultzb149ee22005-09-06 15:17:46 -0700449 if (ntp_synced() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 xtime.tv_sec > last_rtc_update + 660 &&
451 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
452 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
453 if (set_rtc_mmss(xtime.tv_sec) == 0)
454 last_rtc_update = xtime.tv_sec;
455 else
456 last_rtc_update = xtime.tv_sec - 600;
457 /* do it again in 60 s */
458 }
459}
460
David S. Millerfd0504c32006-06-20 01:20:00 -0700461irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
David S. Millerd369ddd2005-07-10 15:45:11 -0700463 unsigned long ticks, compare, pstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 write_seqlock(&xtime_lock);
466
467 do {
468#ifndef CONFIG_SMP
469 profile_tick(CPU_PROFILING, regs);
470 update_process_times(user_mode(regs));
471#endif
472 do_timer(regs);
473
474 /* Guarantee that the following sequences execute
475 * uninterrupted.
476 */
477 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
478 "wrpr %0, %1, %%pstate"
479 : "=r" (pstate)
480 : "i" (PSTATE_IE));
481
David S. Millerd369ddd2005-07-10 15:45:11 -0700482 compare = tick_ops->add_compare(timer_tick_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ticks = tick_ops->get_tick();
484
485 /* Restore PSTATE_IE. */
486 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
487 : /* no outputs */
488 : "r" (pstate));
David S. Millerd369ddd2005-07-10 15:45:11 -0700489 } while (time_after_eq(ticks, compare));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 timer_check_rtc();
492
493 write_sequnlock(&xtime_lock);
494
495 return IRQ_HANDLED;
496}
497
498#ifdef CONFIG_SMP
499void timer_tick_interrupt(struct pt_regs *regs)
500{
501 write_seqlock(&xtime_lock);
502
503 do_timer(regs);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 timer_check_rtc();
506
507 write_sequnlock(&xtime_lock);
508}
509#endif
510
511/* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
512static void __init kick_start_clock(void)
513{
Al Viroef0299b2005-04-24 12:28:36 -0700514 void __iomem *regs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 u8 sec, tmp;
516 int i, count;
517
518 prom_printf("CLOCK: Clock was stopped. Kick start ");
519
520 spin_lock_irq(&mostek_lock);
521
522 /* Turn on the kick start bit to start the oscillator. */
523 tmp = mostek_read(regs + MOSTEK_CREG);
524 tmp |= MSTK_CREG_WRITE;
525 mostek_write(regs + MOSTEK_CREG, tmp);
526 tmp = mostek_read(regs + MOSTEK_SEC);
527 tmp &= ~MSTK_STOP;
528 mostek_write(regs + MOSTEK_SEC, tmp);
529 tmp = mostek_read(regs + MOSTEK_HOUR);
530 tmp |= MSTK_KICK_START;
531 mostek_write(regs + MOSTEK_HOUR, tmp);
532 tmp = mostek_read(regs + MOSTEK_CREG);
533 tmp &= ~MSTK_CREG_WRITE;
534 mostek_write(regs + MOSTEK_CREG, tmp);
535
536 spin_unlock_irq(&mostek_lock);
537
538 /* Delay to allow the clock oscillator to start. */
539 sec = MSTK_REG_SEC(regs);
540 for (i = 0; i < 3; i++) {
541 while (sec == MSTK_REG_SEC(regs))
542 for (count = 0; count < 100000; count++)
543 /* nothing */ ;
544 prom_printf(".");
545 sec = MSTK_REG_SEC(regs);
546 }
547 prom_printf("\n");
548
549 spin_lock_irq(&mostek_lock);
550
551 /* Turn off kick start and set a "valid" time and date. */
552 tmp = mostek_read(regs + MOSTEK_CREG);
553 tmp |= MSTK_CREG_WRITE;
554 mostek_write(regs + MOSTEK_CREG, tmp);
555 tmp = mostek_read(regs + MOSTEK_HOUR);
556 tmp &= ~MSTK_KICK_START;
557 mostek_write(regs + MOSTEK_HOUR, tmp);
558 MSTK_SET_REG_SEC(regs,0);
559 MSTK_SET_REG_MIN(regs,0);
560 MSTK_SET_REG_HOUR(regs,0);
561 MSTK_SET_REG_DOW(regs,5);
562 MSTK_SET_REG_DOM(regs,1);
563 MSTK_SET_REG_MONTH(regs,8);
564 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
565 tmp = mostek_read(regs + MOSTEK_CREG);
566 tmp &= ~MSTK_CREG_WRITE;
567 mostek_write(regs + MOSTEK_CREG, tmp);
568
569 spin_unlock_irq(&mostek_lock);
570
571 /* Ensure the kick start bit is off. If it isn't, turn it off. */
572 while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
573 prom_printf("CLOCK: Kick start still on!\n");
574
575 spin_lock_irq(&mostek_lock);
576
577 tmp = mostek_read(regs + MOSTEK_CREG);
578 tmp |= MSTK_CREG_WRITE;
579 mostek_write(regs + MOSTEK_CREG, tmp);
580
581 tmp = mostek_read(regs + MOSTEK_HOUR);
582 tmp &= ~MSTK_KICK_START;
583 mostek_write(regs + MOSTEK_HOUR, tmp);
584
585 tmp = mostek_read(regs + MOSTEK_CREG);
586 tmp &= ~MSTK_CREG_WRITE;
587 mostek_write(regs + MOSTEK_CREG, tmp);
588
589 spin_unlock_irq(&mostek_lock);
590 }
591
592 prom_printf("CLOCK: Kick start procedure successful.\n");
593}
594
595/* Return nonzero if the clock chip battery is low. */
596static int __init has_low_battery(void)
597{
Al Viroef0299b2005-04-24 12:28:36 -0700598 void __iomem *regs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 u8 data1, data2;
600
601 spin_lock_irq(&mostek_lock);
602
603 data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
604 mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
605 data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
606 mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
607
608 spin_unlock_irq(&mostek_lock);
609
610 return (data1 == data2); /* Was the write blocked? */
611}
612
613/* Probe for the real time clock chip. */
614static void __init set_system_time(void)
615{
616 unsigned int year, mon, day, hour, min, sec;
Al Viroef0299b2005-04-24 12:28:36 -0700617 void __iomem *mregs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618#ifdef CONFIG_PCI
619 unsigned long dregs = ds1287_regs;
620#else
621 unsigned long dregs = 0UL;
622#endif
623 u8 tmp;
624
625 if (!mregs && !dregs) {
626 prom_printf("Something wrong, clock regs not mapped yet.\n");
627 prom_halt();
628 }
629
630 if (mregs) {
631 spin_lock_irq(&mostek_lock);
632
633 /* Traditional Mostek chip. */
634 tmp = mostek_read(mregs + MOSTEK_CREG);
635 tmp |= MSTK_CREG_READ;
636 mostek_write(mregs + MOSTEK_CREG, tmp);
637
638 sec = MSTK_REG_SEC(mregs);
639 min = MSTK_REG_MIN(mregs);
640 hour = MSTK_REG_HOUR(mregs);
641 day = MSTK_REG_DOM(mregs);
642 mon = MSTK_REG_MONTH(mregs);
643 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
644 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Dallas 12887 RTC chip. */
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 do {
648 sec = CMOS_READ(RTC_SECONDS);
649 min = CMOS_READ(RTC_MINUTES);
650 hour = CMOS_READ(RTC_HOURS);
651 day = CMOS_READ(RTC_DAY_OF_MONTH);
652 mon = CMOS_READ(RTC_MONTH);
653 year = CMOS_READ(RTC_YEAR);
654 } while (sec != CMOS_READ(RTC_SECONDS));
Matt Mackall3dedf532006-03-28 01:56:01 -0800655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
657 BCD_TO_BIN(sec);
658 BCD_TO_BIN(min);
659 BCD_TO_BIN(hour);
660 BCD_TO_BIN(day);
661 BCD_TO_BIN(mon);
662 BCD_TO_BIN(year);
663 }
664 if ((year += 1900) < 1970)
665 year += 100;
666 }
667
668 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
669 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
670 set_normalized_timespec(&wall_to_monotonic,
671 -xtime.tv_sec, -xtime.tv_nsec);
672
673 if (mregs) {
674 tmp = mostek_read(mregs + MOSTEK_CREG);
675 tmp &= ~MSTK_CREG_READ;
676 mostek_write(mregs + MOSTEK_CREG, tmp);
677
678 spin_unlock_irq(&mostek_lock);
679 }
680}
681
David S. Miller4bdff412006-02-11 01:01:55 -0800682/* davem suggests we keep this within the 4M locked kernel image */
683static u32 starfire_get_time(void)
684{
685 static char obp_gettod[32];
686 static u32 unix_tod;
687
688 sprintf(obp_gettod, "h# %08x unix-gettod",
689 (unsigned int) (long) &unix_tod);
690 prom_feval(obp_gettod);
691
692 return unix_tod;
693}
694
David S. Miller8ba706a2006-03-01 17:32:46 -0800695static int starfire_set_time(u32 val)
696{
697 /* Do nothing, time is set using the service processor
698 * console on this platform.
699 */
700 return 0;
701}
702
David S. Miller4bdff412006-02-11 01:01:55 -0800703static u32 hypervisor_get_time(void)
704{
705 register unsigned long func asm("%o5");
706 register unsigned long arg0 asm("%o0");
707 register unsigned long arg1 asm("%o1");
708 int retries = 10000;
709
710retry:
711 func = HV_FAST_TOD_GET;
712 arg0 = 0;
713 arg1 = 0;
714 __asm__ __volatile__("ta %6"
715 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
716 : "0" (func), "1" (arg0), "2" (arg1),
717 "i" (HV_FAST_TRAP));
718 if (arg0 == HV_EOK)
719 return arg1;
720 if (arg0 == HV_EWOULDBLOCK) {
721 if (--retries > 0) {
722 udelay(100);
723 goto retry;
724 }
725 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
726 return 0;
727 }
728 printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
729 return 0;
730}
731
David S. Miller8ba706a2006-03-01 17:32:46 -0800732static int hypervisor_set_time(u32 secs)
733{
734 register unsigned long func asm("%o5");
735 register unsigned long arg0 asm("%o0");
736 int retries = 10000;
737
738retry:
739 func = HV_FAST_TOD_SET;
740 arg0 = secs;
741 __asm__ __volatile__("ta %4"
742 : "=&r" (func), "=&r" (arg0)
743 : "0" (func), "1" (arg0),
744 "i" (HV_FAST_TRAP));
745 if (arg0 == HV_EOK)
746 return 0;
747 if (arg0 == HV_EWOULDBLOCK) {
748 if (--retries > 0) {
749 udelay(100);
750 goto retry;
751 }
752 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
753 return -EAGAIN;
754 }
755 printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
756 return -EOPNOTSUPP;
757}
758
David S. Miller690c8fd2006-06-22 19:12:03 -0700759static int __init clock_model_matches(char *model)
760{
761 if (strcmp(model, "mk48t02") &&
762 strcmp(model, "mk48t08") &&
763 strcmp(model, "mk48t59") &&
764 strcmp(model, "m5819") &&
765 strcmp(model, "m5819p") &&
766 strcmp(model, "m5823") &&
767 strcmp(model, "ds1287"))
768 return 0;
769
770 return 1;
771}
772
773static void __init __clock_assign_common(void __iomem *addr, char *model)
774{
775 if (model[5] == '0' && model[6] == '2') {
776 mstk48t02_regs = addr;
777 } else if(model[5] == '0' && model[6] == '8') {
778 mstk48t08_regs = addr;
779 mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
780 } else {
781 mstk48t59_regs = addr;
782 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
783 }
784}
785
786static void __init clock_assign_clk_reg(struct linux_prom_registers *clk_reg,
787 char *model)
788{
789 unsigned long addr;
790
791 addr = ((unsigned long) clk_reg[0].phys_addr |
792 (((unsigned long) clk_reg[0].which_io) << 32UL));
793
794 __clock_assign_common((void __iomem *) addr, model);
795}
796
797static int __init clock_probe_central(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
David S. Millercecc4e92006-06-22 19:53:24 -0700799 struct linux_prom_registers clk_reg[2], *pr;
800 struct device_node *dp;
801 char *model;
David S. Miller690c8fd2006-06-22 19:12:03 -0700802
803 if (!central_bus)
804 return 0;
805
806 /* Get Central FHC's prom node. */
David S. Millercecc4e92006-06-22 19:53:24 -0700807 dp = central_bus->child->prom_node;
David S. Miller690c8fd2006-06-22 19:12:03 -0700808
809 /* Then get the first child device below it. */
David S. Millercecc4e92006-06-22 19:53:24 -0700810 dp = dp->child;
David S. Miller690c8fd2006-06-22 19:12:03 -0700811
David S. Millercecc4e92006-06-22 19:53:24 -0700812 while (dp) {
813 model = of_get_property(dp, "model", NULL);
814 if (!model || !clock_model_matches(model))
David S. Miller690c8fd2006-06-22 19:12:03 -0700815 goto next_sibling;
816
David S. Millercecc4e92006-06-22 19:53:24 -0700817 pr = of_get_property(dp, "reg", NULL);
818 memcpy(clk_reg, pr, sizeof(clk_reg));
David S. Miller690c8fd2006-06-22 19:12:03 -0700819
820 apply_fhc_ranges(central_bus->child, clk_reg, 1);
821 apply_central_ranges(central_bus, clk_reg, 1);
822
823 clock_assign_clk_reg(clk_reg, model);
824 return 1;
825
826 next_sibling:
David S. Millercecc4e92006-06-22 19:53:24 -0700827 dp = dp->sibling;
David S. Miller690c8fd2006-06-22 19:12:03 -0700828 }
829
830 return 0;
831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#ifdef CONFIG_PCI
David S. Miller690c8fd2006-06-22 19:12:03 -0700834static void __init clock_isa_ebus_assign_regs(struct resource *res, char *model)
835{
836 if (!strcmp(model, "ds1287") ||
837 !strcmp(model, "m5819") ||
838 !strcmp(model, "m5819p") ||
839 !strcmp(model, "m5823")) {
840 ds1287_regs = res->start;
841 } else {
842 mstk48t59_regs = (void __iomem *) res->start;
843 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
844 }
845}
846
847static int __init clock_probe_one_ebus_dev(struct linux_ebus_device *edev)
848{
849 struct device_node *dp = edev->prom_node;
850 char *model;
851
852 model = of_get_property(dp, "model", NULL);
853 if (!clock_model_matches(model))
854 return 0;
855
856 clock_isa_ebus_assign_regs(&edev->resource[0], model);
857
858 return 1;
859}
860
861static int __init clock_probe_ebus(void)
862{
863 struct linux_ebus *ebus;
864
865 for_each_ebus(ebus) {
866 struct linux_ebus_device *edev;
867
868 for_each_ebusdev(edev, ebus) {
869 if (clock_probe_one_ebus_dev(edev))
870 return 1;
871 }
872 }
873
874 return 0;
875}
876
877static int __init clock_probe_one_isa_dev(struct sparc_isa_device *idev)
878{
879 struct device_node *dp = idev->prom_node;
880 char *model;
881
882 model = of_get_property(dp, "model", NULL);
883 if (!clock_model_matches(model))
884 return 0;
885
886 clock_isa_ebus_assign_regs(&idev->resource, model);
887
888 return 1;
889}
890
891static int __init clock_probe_isa(void)
892{
893 struct sparc_isa_bridge *isa_br;
894
895 for_each_isa(isa_br) {
896 struct sparc_isa_device *isa_dev;
897
898 for_each_isadev(isa_dev, isa_br) {
899 if (clock_probe_one_isa_dev(isa_dev))
900 return 1;
901 }
902 }
903
904 return 0;
905}
906#endif /* CONFIG_PCI */
907
908#ifdef CONFIG_SBUS
909static int __init clock_probe_one_sbus_dev(struct sbus_bus *sbus, struct sbus_dev *sdev)
910{
911 struct resource *res;
912 char model[64];
913 void __iomem *addr;
914
915 prom_getstring(sdev->prom_node, "model", model, sizeof(model));
916 if (!clock_model_matches(model))
917 return 0;
918
919 res = &sdev->resource[0];
920 addr = sbus_ioremap(res, 0, 0x800UL, "eeprom");
921
922 __clock_assign_common(addr, model);
923
924 return 1;
925}
926
927static int __init clock_probe_sbus(void)
928{
929 struct sbus_bus *sbus;
930
931 for_each_sbus(sbus) {
932 struct sbus_dev *sdev;
933
934 for_each_sbusdev(sdev, sbus) {
935 if (clock_probe_one_sbus_dev(sbus, sdev))
936 return 1;
937 }
938 }
939
940 return 0;
941}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942#endif
David S. Miller690c8fd2006-06-22 19:12:03 -0700943
944void __init clock_probe(void)
945{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 static int invoked;
David S. Miller690c8fd2006-06-22 19:12:03 -0700947 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (invoked)
950 return;
951 invoked = 1;
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (this_is_starfire) {
David S. Miller4bdff412006-02-11 01:01:55 -0800954 xtime.tv_sec = starfire_get_time();
955 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
956 set_normalized_timespec(&wall_to_monotonic,
957 -xtime.tv_sec, -xtime.tv_nsec);
958 return;
959 }
960 if (tlb_type == hypervisor) {
961 xtime.tv_sec = hypervisor_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
963 set_normalized_timespec(&wall_to_monotonic,
964 -xtime.tv_sec, -xtime.tv_nsec);
965 return;
966 }
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
969 * That way we handle the presence of multiple properly.
970 *
971 * As a special case, machines with Central must provide the
972 * timer chip there.
973 */
David S. Miller690c8fd2006-06-22 19:12:03 -0700974 if (!clock_probe_central() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#ifdef CONFIG_PCI
David S. Miller690c8fd2006-06-22 19:12:03 -0700976 !clock_probe_ebus() &&
977 !clock_probe_isa() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978#endif
David S. Miller690c8fd2006-06-22 19:12:03 -0700979#ifdef CONFIG_SBUS
980 !clock_probe_sbus()
981#endif
982 ) {
983 printk(KERN_WARNING "No clock chip found.\n");
984 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
David S. Miller690c8fd2006-06-22 19:12:03 -0700987 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
David S. Millerb4bca262005-04-21 21:42:34 -0700989 if (mstk48t02_regs != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* Report a low battery voltage condition. */
991 if (has_low_battery())
992 prom_printf("NVRAM: Low battery voltage!\n");
993
994 /* Kick start the clock if it is completely stopped. */
995 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
996 kick_start_clock();
997 }
998
999 set_system_time();
1000
1001 local_irq_restore(flags);
1002}
1003
1004/* This is gets the master TICK_INT timer going. */
1005static unsigned long sparc64_init_timers(void)
1006{
David S. Miller07f8e5f2006-06-21 23:34:02 -07001007 struct device_node *dp;
1008 struct property *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unsigned long clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010#ifdef CONFIG_SMP
1011 extern void smp_tick_init(void);
1012#endif
1013
David S. Miller07f8e5f2006-06-21 23:34:02 -07001014 dp = of_find_node_by_path("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (tlb_type == spitfire) {
1016 unsigned long ver, manuf, impl;
1017
1018 __asm__ __volatile__ ("rdpr %%ver, %0"
1019 : "=&r" (ver));
1020 manuf = ((ver >> 48) & 0xffff);
1021 impl = ((ver >> 32) & 0xffff);
1022 if (manuf == 0x17 && impl == 0x13) {
1023 /* Hummingbird, aka Ultra-IIe */
1024 tick_ops = &hbtick_operations;
David S. Miller07f8e5f2006-06-21 23:34:02 -07001025 prop = of_find_property(dp, "stick-frequency", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 } else {
1027 tick_ops = &tick_operations;
David S. Miller07f8e5f2006-06-21 23:34:02 -07001028 cpu_find_by_instance(0, &dp, NULL);
1029 prop = of_find_property(dp, "clock-frequency", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 } else {
1032 tick_ops = &stick_operations;
David S. Miller07f8e5f2006-06-21 23:34:02 -07001033 prop = of_find_property(dp, "stick-frequency", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
David S. Miller07f8e5f2006-06-21 23:34:02 -07001035 clock = *(unsigned int *) prop->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 timer_tick_offset = clock / HZ;
1037
1038#ifdef CONFIG_SMP
1039 smp_tick_init();
1040#endif
1041
1042 return clock;
1043}
1044
David S. Millerfd0504c32006-06-20 01:20:00 -07001045static void sparc64_start_timers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 unsigned long pstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 /* Guarantee that the following sequences execute
1050 * uninterrupted.
1051 */
1052 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
1053 "wrpr %0, %1, %%pstate"
1054 : "=r" (pstate)
1055 : "i" (PSTATE_IE));
1056
1057 tick_ops->init_tick(timer_tick_offset);
1058
1059 /* Restore PSTATE_IE. */
1060 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
1061 : /* no outputs */
1062 : "r" (pstate));
1063
1064 local_irq_enable();
1065}
1066
1067struct freq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 unsigned long clock_tick_ref;
1069 unsigned int ref_freq;
1070};
David S. Miller3763be32006-02-17 12:33:13 -08001071static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073unsigned long sparc64_get_clock_tick(unsigned int cpu)
1074{
1075 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
1076
1077 if (ft->clock_tick_ref)
1078 return ft->clock_tick_ref;
1079 return cpu_data(cpu).clock_tick;
1080}
1081
1082#ifdef CONFIG_CPU_FREQ
1083
1084static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
1085 void *data)
1086{
1087 struct cpufreq_freqs *freq = data;
1088 unsigned int cpu = freq->cpu;
1089 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
1090
1091 if (!ft->ref_freq) {
1092 ft->ref_freq = freq->old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
1094 }
1095 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
1096 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
1097 (val == CPUFREQ_RESUMECHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 cpu_data(cpu).clock_tick =
1099 cpufreq_scale(ft->clock_tick_ref,
1100 ft->ref_freq,
1101 freq->new);
1102 }
1103
1104 return 0;
1105}
1106
1107static struct notifier_block sparc64_cpufreq_notifier_block = {
1108 .notifier_call = sparc64_cpufreq_notifier
1109};
1110
1111#endif /* CONFIG_CPU_FREQ */
1112
1113static struct time_interpolator sparc64_cpu_interpolator = {
1114 .source = TIME_SOURCE_CPU,
1115 .shift = 16,
1116 .mask = 0xffffffffffffffffLL
1117};
1118
1119/* The quotient formula is taken from the IA64 port. */
1120#define SPARC64_NSEC_PER_CYC_SHIFT 30UL
1121void __init time_init(void)
1122{
1123 unsigned long clock = sparc64_init_timers();
1124
1125 sparc64_cpu_interpolator.frequency = clock;
1126 register_time_interpolator(&sparc64_cpu_interpolator);
1127
1128 /* Now that the interpolator is registered, it is
1129 * safe to start the timer ticking.
1130 */
David S. Millerfd0504c32006-06-20 01:20:00 -07001131 sparc64_start_timers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 timer_ticks_per_nsec_quotient =
1134 (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
1135 (clock / 2)) / clock);
1136
1137#ifdef CONFIG_CPU_FREQ
1138 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
1139 CPUFREQ_TRANSITION_NOTIFIER);
1140#endif
1141}
1142
1143unsigned long long sched_clock(void)
1144{
1145 unsigned long ticks = tick_ops->get_tick();
1146
1147 return (ticks * timer_ticks_per_nsec_quotient)
1148 >> SPARC64_NSEC_PER_CYC_SHIFT;
1149}
1150
1151static int set_rtc_mmss(unsigned long nowtime)
1152{
1153 int real_seconds, real_minutes, chip_minutes;
Al Viroef0299b2005-04-24 12:28:36 -07001154 void __iomem *mregs = mstk48t02_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155#ifdef CONFIG_PCI
1156 unsigned long dregs = ds1287_regs;
1157#else
1158 unsigned long dregs = 0UL;
1159#endif
1160 unsigned long flags;
1161 u8 tmp;
1162
1163 /*
1164 * Not having a register set can lead to trouble.
1165 * Also starfire doesn't have a tod clock.
1166 */
1167 if (!mregs && !dregs)
1168 return -1;
1169
1170 if (mregs) {
1171 spin_lock_irqsave(&mostek_lock, flags);
1172
1173 /* Read the current RTC minutes. */
1174 tmp = mostek_read(mregs + MOSTEK_CREG);
1175 tmp |= MSTK_CREG_READ;
1176 mostek_write(mregs + MOSTEK_CREG, tmp);
1177
1178 chip_minutes = MSTK_REG_MIN(mregs);
1179
1180 tmp = mostek_read(mregs + MOSTEK_CREG);
1181 tmp &= ~MSTK_CREG_READ;
1182 mostek_write(mregs + MOSTEK_CREG, tmp);
1183
1184 /*
1185 * since we're only adjusting minutes and seconds,
1186 * don't interfere with hour overflow. This avoids
1187 * messing with unknown time zones but requires your
1188 * RTC not to be off by more than 15 minutes
1189 */
1190 real_seconds = nowtime % 60;
1191 real_minutes = nowtime / 60;
1192 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1193 real_minutes += 30; /* correct for half hour time zone */
1194 real_minutes %= 60;
1195
1196 if (abs(real_minutes - chip_minutes) < 30) {
1197 tmp = mostek_read(mregs + MOSTEK_CREG);
1198 tmp |= MSTK_CREG_WRITE;
1199 mostek_write(mregs + MOSTEK_CREG, tmp);
1200
1201 MSTK_SET_REG_SEC(mregs,real_seconds);
1202 MSTK_SET_REG_MIN(mregs,real_minutes);
1203
1204 tmp = mostek_read(mregs + MOSTEK_CREG);
1205 tmp &= ~MSTK_CREG_WRITE;
1206 mostek_write(mregs + MOSTEK_CREG, tmp);
1207
1208 spin_unlock_irqrestore(&mostek_lock, flags);
1209
1210 return 0;
1211 } else {
1212 spin_unlock_irqrestore(&mostek_lock, flags);
1213
1214 return -1;
1215 }
1216 } else {
1217 int retval = 0;
1218 unsigned char save_control, save_freq_select;
1219
1220 /* Stolen from arch/i386/kernel/time.c, see there for
1221 * credits and descriptive comments.
1222 */
1223 spin_lock_irqsave(&rtc_lock, flags);
1224 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
1225 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1226
1227 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
1228 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1229
1230 chip_minutes = CMOS_READ(RTC_MINUTES);
1231 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1232 BCD_TO_BIN(chip_minutes);
1233 real_seconds = nowtime % 60;
1234 real_minutes = nowtime / 60;
1235 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1236 real_minutes += 30;
1237 real_minutes %= 60;
1238
1239 if (abs(real_minutes - chip_minutes) < 30) {
1240 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1241 BIN_TO_BCD(real_seconds);
1242 BIN_TO_BCD(real_minutes);
1243 }
1244 CMOS_WRITE(real_seconds,RTC_SECONDS);
1245 CMOS_WRITE(real_minutes,RTC_MINUTES);
1246 } else {
1247 printk(KERN_WARNING
1248 "set_rtc_mmss: can't update from %d to %d\n",
1249 chip_minutes, real_minutes);
1250 retval = -1;
1251 }
1252
1253 CMOS_WRITE(save_control, RTC_CONTROL);
1254 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1255 spin_unlock_irqrestore(&rtc_lock, flags);
1256
1257 return retval;
1258 }
1259}
David S. Miller8ba706a2006-03-01 17:32:46 -08001260
1261#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
1262static unsigned char mini_rtc_status; /* bitmapped status byte. */
1263
1264/* months start at 0 now */
1265static unsigned char days_in_mo[] =
1266{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1267
1268#define FEBRUARY 2
1269#define STARTOFTIME 1970
1270#define SECDAY 86400L
1271#define SECYR (SECDAY * 365)
1272#define leapyear(year) ((year) % 4 == 0 && \
1273 ((year) % 100 != 0 || (year) % 400 == 0))
1274#define days_in_year(a) (leapyear(a) ? 366 : 365)
1275#define days_in_month(a) (month_days[(a) - 1])
1276
1277static int month_days[12] = {
1278 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1279};
1280
1281/*
1282 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1283 */
1284static void GregorianDay(struct rtc_time * tm)
1285{
1286 int leapsToDate;
1287 int lastYear;
1288 int day;
1289 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1290
1291 lastYear = tm->tm_year - 1;
1292
1293 /*
1294 * Number of leap corrections to apply up to end of last year
1295 */
1296 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1297
1298 /*
1299 * This year is a leap year if it is divisible by 4 except when it is
1300 * divisible by 100 unless it is divisible by 400
1301 *
1302 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1303 */
1304 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1305
1306 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1307 tm->tm_mday;
1308
1309 tm->tm_wday = day % 7;
1310}
1311
1312static void to_tm(int tim, struct rtc_time *tm)
1313{
1314 register int i;
1315 register long hms, day;
1316
1317 day = tim / SECDAY;
1318 hms = tim % SECDAY;
1319
1320 /* Hours, minutes, seconds are easy */
1321 tm->tm_hour = hms / 3600;
1322 tm->tm_min = (hms % 3600) / 60;
1323 tm->tm_sec = (hms % 3600) % 60;
1324
1325 /* Number of years in days */
1326 for (i = STARTOFTIME; day >= days_in_year(i); i++)
1327 day -= days_in_year(i);
1328 tm->tm_year = i;
1329
1330 /* Number of months in days left */
1331 if (leapyear(tm->tm_year))
1332 days_in_month(FEBRUARY) = 29;
1333 for (i = 1; day >= days_in_month(i); i++)
1334 day -= days_in_month(i);
1335 days_in_month(FEBRUARY) = 28;
1336 tm->tm_mon = i;
1337
1338 /* Days are what is left over (+1) from all that. */
1339 tm->tm_mday = day + 1;
1340
1341 /*
1342 * Determine the day of week
1343 */
1344 GregorianDay(tm);
1345}
1346
1347/* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1348 * aka Unix time. So we have to convert to/from rtc_time.
1349 */
1350static inline void mini_get_rtc_time(struct rtc_time *time)
1351{
1352 unsigned long flags;
1353 u32 seconds;
1354
1355 spin_lock_irqsave(&rtc_lock, flags);
1356 seconds = 0;
1357 if (this_is_starfire)
1358 seconds = starfire_get_time();
1359 else if (tlb_type == hypervisor)
1360 seconds = hypervisor_get_time();
1361 spin_unlock_irqrestore(&rtc_lock, flags);
1362
1363 to_tm(seconds, time);
David S. Millerc4f8ef72006-03-02 20:28:34 -08001364 time->tm_year -= 1900;
1365 time->tm_mon -= 1;
David S. Miller8ba706a2006-03-01 17:32:46 -08001366}
1367
1368static inline int mini_set_rtc_time(struct rtc_time *time)
1369{
1370 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1371 time->tm_mday, time->tm_hour,
1372 time->tm_min, time->tm_sec);
1373 unsigned long flags;
1374 int err;
1375
1376 spin_lock_irqsave(&rtc_lock, flags);
1377 err = -ENODEV;
1378 if (this_is_starfire)
1379 err = starfire_set_time(seconds);
1380 else if (tlb_type == hypervisor)
1381 err = hypervisor_set_time(seconds);
1382 spin_unlock_irqrestore(&rtc_lock, flags);
1383
1384 return err;
1385}
1386
1387static int mini_rtc_ioctl(struct inode *inode, struct file *file,
1388 unsigned int cmd, unsigned long arg)
1389{
1390 struct rtc_time wtime;
1391 void __user *argp = (void __user *)arg;
1392
1393 switch (cmd) {
1394
1395 case RTC_PLL_GET:
1396 return -EINVAL;
1397
1398 case RTC_PLL_SET:
1399 return -EINVAL;
1400
1401 case RTC_UIE_OFF: /* disable ints from RTC updates. */
1402 return 0;
1403
1404 case RTC_UIE_ON: /* enable ints for RTC updates. */
1405 return -EINVAL;
1406
1407 case RTC_RD_TIME: /* Read the time/date from RTC */
1408 /* this doesn't get week-day, who cares */
1409 memset(&wtime, 0, sizeof(wtime));
1410 mini_get_rtc_time(&wtime);
1411
1412 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
1413
1414 case RTC_SET_TIME: /* Set the RTC */
1415 {
1416 int year;
1417 unsigned char leap_yr;
1418
1419 if (!capable(CAP_SYS_TIME))
1420 return -EACCES;
1421
1422 if (copy_from_user(&wtime, argp, sizeof(wtime)))
1423 return -EFAULT;
1424
1425 year = wtime.tm_year + 1900;
1426 leap_yr = ((!(year % 4) && (year % 100)) ||
1427 !(year % 400));
1428
1429 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
1430 return -EINVAL;
1431
1432 if (wtime.tm_mday < 0 || wtime.tm_mday >
1433 (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
1434 return -EINVAL;
1435
1436 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
1437 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
1438 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
1439 return -EINVAL;
1440
1441 return mini_set_rtc_time(&wtime);
1442 }
1443 }
1444
1445 return -EINVAL;
1446}
1447
1448static int mini_rtc_open(struct inode *inode, struct file *file)
1449{
1450 if (mini_rtc_status & RTC_IS_OPEN)
1451 return -EBUSY;
1452
1453 mini_rtc_status |= RTC_IS_OPEN;
1454
1455 return 0;
1456}
1457
1458static int mini_rtc_release(struct inode *inode, struct file *file)
1459{
1460 mini_rtc_status &= ~RTC_IS_OPEN;
1461 return 0;
1462}
1463
1464
1465static struct file_operations mini_rtc_fops = {
1466 .owner = THIS_MODULE,
1467 .ioctl = mini_rtc_ioctl,
1468 .open = mini_rtc_open,
1469 .release = mini_rtc_release,
1470};
1471
1472static struct miscdevice rtc_mini_dev =
1473{
1474 .minor = RTC_MINOR,
1475 .name = "rtc",
1476 .fops = &mini_rtc_fops,
1477};
1478
1479static int __init rtc_mini_init(void)
1480{
1481 int retval;
1482
1483 if (tlb_type != hypervisor && !this_is_starfire)
1484 return -ENODEV;
1485
1486 printk(KERN_INFO "Mini RTC Driver\n");
1487
1488 retval = misc_register(&rtc_mini_dev);
1489 if (retval < 0)
1490 return retval;
1491
1492 return 0;
1493}
1494
1495static void __exit rtc_mini_exit(void)
1496{
1497 misc_deregister(&rtc_mini_dev);
1498}
1499
1500
1501module_init(rtc_mini_init);
1502module_exit(rtc_mini_exit);