1 /* $Id: time.c,v 1.42 2002/01/23 14:33:55 davem Exp $
2 * time.c: UltraSparc timer and TOD clock support.
4 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
7 * Based largely on code which is:
9 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
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>
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>
34 #include <asm/oplib.h>
35 #include <asm/mostek.h>
36 #include <asm/timer.h>
44 #include <asm/starfire.h>
46 #include <asm/sections.h>
47 #include <asm/cpudata.h>
49 DEFINE_SPINLOCK(mostek_lock);
50 DEFINE_SPINLOCK(rtc_lock);
51 void __iomem *mstk48t02_regs = NULL;
53 unsigned long ds1287_regs = 0UL;
56 extern unsigned long wall_jiffies;
58 static void __iomem *mstk48t08_regs;
59 static void __iomem *mstk48t59_regs;
61 static int set_rtc_mmss(unsigned long);
63 #define TICK_PRIV_BIT (1UL << 63)
66 unsigned long profile_pc(struct pt_regs *regs)
68 unsigned long pc = instruction_pointer(regs);
70 if (in_lock_functions(pc))
71 return regs->u_regs[UREG_RETPC];
74 EXPORT_SYMBOL(profile_pc);
77 static void tick_disable_protection(void)
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.
87 "1: rd %%tick, %%g2\n"
88 " add %%g2, 6, %%g2\n"
89 " andn %%g2, %0, %%g2\n"
90 " wrpr %%g2, 0, %%tick\n"
97 static void tick_init_tick(unsigned long offset)
99 tick_disable_protection();
101 __asm__ __volatile__(
103 " andn %%g1, %1, %%g1\n"
105 " add %%g1, %0, %%g1\n"
107 "1: wr %%g1, 0x0, %%tick_cmpr\n"
108 " rd %%tick_cmpr, %%g0"
110 : "r" (offset), "r" (TICK_PRIV_BIT)
114 static unsigned long tick_get_tick(void)
118 __asm__ __volatile__("rd %%tick, %0\n\t"
122 return ret & ~TICK_PRIV_BIT;
125 static unsigned long tick_get_compare(void)
129 __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
136 static unsigned long tick_add_compare(unsigned long adj)
138 unsigned long new_compare;
140 /* Workaround for Spitfire Errata (#54 I think??), I discovered
141 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
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
149 __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
150 "ba,pt %%xcc, 1f\n\t"
151 " add %0, %1, %0\n\t"
154 "wr %0, 0, %%tick_cmpr\n\t"
155 "rd %%tick_cmpr, %%g0"
156 : "=&r" (new_compare)
162 static unsigned long tick_add_tick(unsigned long adj, unsigned long offset)
164 unsigned long new_tick, tmp;
166 /* Also need to handle Blackbird bug here too. */
167 __asm__ __volatile__("rd %%tick, %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"
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));
183 static struct sparc64_tick_ops tick_operations __read_mostly = {
184 .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,
192 struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
194 static void stick_init_tick(unsigned long offset)
196 tick_disable_protection();
198 /* Let the user get at STICK too. */
199 __asm__ __volatile__(
200 " rd %%asr24, %%g2\n"
201 " andn %%g2, %0, %%g2\n"
202 " wr %%g2, 0, %%asr24"
204 : "r" (TICK_PRIV_BIT)
207 __asm__ __volatile__(
208 " rd %%asr24, %%g1\n"
209 " andn %%g1, %1, %%g1\n"
210 " add %%g1, %0, %%g1\n"
211 " wr %%g1, 0x0, %%asr25"
213 : "r" (offset), "r" (TICK_PRIV_BIT)
217 static unsigned long stick_get_tick(void)
221 __asm__ __volatile__("rd %%asr24, %0"
224 return ret & ~TICK_PRIV_BIT;
227 static unsigned long stick_get_compare(void)
231 __asm__ __volatile__("rd %%asr25, %0"
237 static unsigned long stick_add_tick(unsigned long adj, unsigned long offset)
239 unsigned long new_tick, tmp;
241 __asm__ __volatile__("rd %%asr24, %0\n\t"
243 "wr %0, 0, %%asr24\n\t"
244 "andn %0, %4, %1\n\t"
247 : "=&r" (new_tick), "=&r" (tmp)
248 : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
253 static unsigned long stick_add_compare(unsigned long adj)
255 unsigned long new_compare;
257 __asm__ __volatile__("rd %%asr25, %0\n\t"
260 : "=&r" (new_compare)
266 static struct sparc64_tick_ops stick_operations __read_mostly = {
267 .init_tick = stick_init_tick,
268 .get_tick = stick_get_tick,
269 .get_compare = stick_get_compare,
270 .add_tick = stick_add_tick,
271 .add_compare = stick_add_compare,
272 .softint_mask = 1UL << 16,
275 /* On Hummingbird the STICK/STICK_CMPR register is implemented
276 * in I/O space. There are two 64-bit registers each, the
277 * first holds the low 32-bits of the value and the second holds
280 * Since STICK is constantly updating, we have to access it carefully.
282 * The sequence we use to read is:
285 * 3) read low again, if it rolled over increment high by 1
287 * Writing STICK safely is also tricky:
288 * 1) write low to zero
292 #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
293 #define HBIRD_STICK_ADDR 0x1fe0000f070UL
295 static unsigned long __hbird_read_stick(void)
297 unsigned long ret, tmp1, tmp2, tmp3;
298 unsigned long addr = HBIRD_STICK_ADDR;
300 __asm__ __volatile__("ldxa [%1] %5, %2\n\t"
301 "add %1, 0x8, %1\n\t"
302 "ldxa [%1] %5, %3\n\t"
303 "sub %1, 0x8, %1\n\t"
304 "ldxa [%1] %5, %4\n\t"
306 "blu,a,pn %%xcc, 1f\n\t"
309 "sllx %3, 32, %3\n\t"
311 : "=&r" (ret), "=&r" (addr),
312 "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
313 : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
318 static unsigned long __hbird_read_compare(void)
320 unsigned long low, high;
321 unsigned long addr = HBIRD_STICKCMP_ADDR;
323 __asm__ __volatile__("ldxa [%2] %3, %0\n\t"
324 "add %2, 0x8, %2\n\t"
326 : "=&r" (low), "=&r" (high), "=&r" (addr)
327 : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr));
329 return (high << 32UL) | low;
332 static void __hbird_write_stick(unsigned long val)
334 unsigned long low = (val & 0xffffffffUL);
335 unsigned long high = (val >> 32UL);
336 unsigned long addr = HBIRD_STICK_ADDR;
338 __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
339 "add %0, 0x8, %0\n\t"
340 "stxa %3, [%0] %4\n\t"
341 "sub %0, 0x8, %0\n\t"
344 : "0" (addr), "r" (low), "r" (high),
345 "i" (ASI_PHYS_BYPASS_EC_E));
348 static void __hbird_write_compare(unsigned long val)
350 unsigned long low = (val & 0xffffffffUL);
351 unsigned long high = (val >> 32UL);
352 unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
354 __asm__ __volatile__("stxa %3, [%0] %4\n\t"
355 "sub %0, 0x8, %0\n\t"
358 : "0" (addr), "r" (low), "r" (high),
359 "i" (ASI_PHYS_BYPASS_EC_E));
362 static void hbtick_init_tick(unsigned long offset)
366 tick_disable_protection();
368 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
369 * XXX into actually sending STICK interrupts. I think because
370 * XXX of how we store %tick_cmpr in head.S this somehow resets the
371 * XXX {TICK + STICK} interrupt mux. -DaveM
373 __hbird_write_stick(__hbird_read_stick());
375 val = __hbird_read_stick() & ~TICK_PRIV_BIT;
376 __hbird_write_compare(val + offset);
379 static unsigned long hbtick_get_tick(void)
381 return __hbird_read_stick() & ~TICK_PRIV_BIT;
384 static unsigned long hbtick_get_compare(void)
386 return __hbird_read_compare();
389 static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset)
393 val = __hbird_read_stick() + adj;
394 __hbird_write_stick(val);
396 val &= ~TICK_PRIV_BIT;
397 __hbird_write_compare(val + offset);
402 static unsigned long hbtick_add_compare(unsigned long adj)
404 unsigned long val = __hbird_read_compare() + adj;
406 val &= ~TICK_PRIV_BIT;
407 __hbird_write_compare(val);
412 static struct sparc64_tick_ops hbtick_operations __read_mostly = {
413 .init_tick = hbtick_init_tick,
414 .get_tick = hbtick_get_tick,
415 .get_compare = hbtick_get_compare,
416 .add_tick = hbtick_add_tick,
417 .add_compare = hbtick_add_compare,
418 .softint_mask = 1UL << 0,
421 /* timer_interrupt() needs to keep up the real-time clock,
422 * as well as call the "do_timer()" routine every clocktick
424 * NOTE: On SUN5 systems the ticker interrupt comes in using 2
425 * interrupts, one at level14 and one with softint bit 0.
427 unsigned long timer_tick_offset __read_mostly;
429 static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
431 #define TICK_SIZE (tick_nsec / 1000)
433 static inline void timer_check_rtc(void)
435 /* last time the cmos clock got updated */
436 static long last_rtc_update;
438 /* Determine when to update the Mostek clock. */
440 xtime.tv_sec > last_rtc_update + 660 &&
441 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
442 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
443 if (set_rtc_mmss(xtime.tv_sec) == 0)
444 last_rtc_update = xtime.tv_sec;
446 last_rtc_update = xtime.tv_sec - 600;
447 /* do it again in 60 s */
451 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
453 unsigned long ticks, compare, pstate;
455 write_seqlock(&xtime_lock);
459 profile_tick(CPU_PROFILING, regs);
460 update_process_times(user_mode(regs));
464 /* Guarantee that the following sequences execute
467 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
468 "wrpr %0, %1, %%pstate"
472 compare = tick_ops->add_compare(timer_tick_offset);
473 ticks = tick_ops->get_tick();
475 /* Restore PSTATE_IE. */
476 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
479 } while (time_after_eq(ticks, compare));
483 write_sequnlock(&xtime_lock);
489 void timer_tick_interrupt(struct pt_regs *regs)
491 write_seqlock(&xtime_lock);
497 write_sequnlock(&xtime_lock);
501 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
502 static void __init kick_start_clock(void)
504 void __iomem *regs = mstk48t02_regs;
508 prom_printf("CLOCK: Clock was stopped. Kick start ");
510 spin_lock_irq(&mostek_lock);
512 /* Turn on the kick start bit to start the oscillator. */
513 tmp = mostek_read(regs + MOSTEK_CREG);
514 tmp |= MSTK_CREG_WRITE;
515 mostek_write(regs + MOSTEK_CREG, tmp);
516 tmp = mostek_read(regs + MOSTEK_SEC);
518 mostek_write(regs + MOSTEK_SEC, tmp);
519 tmp = mostek_read(regs + MOSTEK_HOUR);
520 tmp |= MSTK_KICK_START;
521 mostek_write(regs + MOSTEK_HOUR, tmp);
522 tmp = mostek_read(regs + MOSTEK_CREG);
523 tmp &= ~MSTK_CREG_WRITE;
524 mostek_write(regs + MOSTEK_CREG, tmp);
526 spin_unlock_irq(&mostek_lock);
528 /* Delay to allow the clock oscillator to start. */
529 sec = MSTK_REG_SEC(regs);
530 for (i = 0; i < 3; i++) {
531 while (sec == MSTK_REG_SEC(regs))
532 for (count = 0; count < 100000; count++)
535 sec = MSTK_REG_SEC(regs);
539 spin_lock_irq(&mostek_lock);
541 /* Turn off kick start and set a "valid" time and date. */
542 tmp = mostek_read(regs + MOSTEK_CREG);
543 tmp |= MSTK_CREG_WRITE;
544 mostek_write(regs + MOSTEK_CREG, tmp);
545 tmp = mostek_read(regs + MOSTEK_HOUR);
546 tmp &= ~MSTK_KICK_START;
547 mostek_write(regs + MOSTEK_HOUR, tmp);
548 MSTK_SET_REG_SEC(regs,0);
549 MSTK_SET_REG_MIN(regs,0);
550 MSTK_SET_REG_HOUR(regs,0);
551 MSTK_SET_REG_DOW(regs,5);
552 MSTK_SET_REG_DOM(regs,1);
553 MSTK_SET_REG_MONTH(regs,8);
554 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
555 tmp = mostek_read(regs + MOSTEK_CREG);
556 tmp &= ~MSTK_CREG_WRITE;
557 mostek_write(regs + MOSTEK_CREG, tmp);
559 spin_unlock_irq(&mostek_lock);
561 /* Ensure the kick start bit is off. If it isn't, turn it off. */
562 while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
563 prom_printf("CLOCK: Kick start still on!\n");
565 spin_lock_irq(&mostek_lock);
567 tmp = mostek_read(regs + MOSTEK_CREG);
568 tmp |= MSTK_CREG_WRITE;
569 mostek_write(regs + MOSTEK_CREG, tmp);
571 tmp = mostek_read(regs + MOSTEK_HOUR);
572 tmp &= ~MSTK_KICK_START;
573 mostek_write(regs + MOSTEK_HOUR, tmp);
575 tmp = mostek_read(regs + MOSTEK_CREG);
576 tmp &= ~MSTK_CREG_WRITE;
577 mostek_write(regs + MOSTEK_CREG, tmp);
579 spin_unlock_irq(&mostek_lock);
582 prom_printf("CLOCK: Kick start procedure successful.\n");
585 /* Return nonzero if the clock chip battery is low. */
586 static int __init has_low_battery(void)
588 void __iomem *regs = mstk48t02_regs;
591 spin_lock_irq(&mostek_lock);
593 data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
594 mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
595 data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
596 mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
598 spin_unlock_irq(&mostek_lock);
600 return (data1 == data2); /* Was the write blocked? */
603 /* Probe for the real time clock chip. */
604 static void __init set_system_time(void)
606 unsigned int year, mon, day, hour, min, sec;
607 void __iomem *mregs = mstk48t02_regs;
609 unsigned long dregs = ds1287_regs;
611 unsigned long dregs = 0UL;
615 if (!mregs && !dregs) {
616 prom_printf("Something wrong, clock regs not mapped yet.\n");
621 spin_lock_irq(&mostek_lock);
623 /* Traditional Mostek chip. */
624 tmp = mostek_read(mregs + MOSTEK_CREG);
625 tmp |= MSTK_CREG_READ;
626 mostek_write(mregs + MOSTEK_CREG, tmp);
628 sec = MSTK_REG_SEC(mregs);
629 min = MSTK_REG_MIN(mregs);
630 hour = MSTK_REG_HOUR(mregs);
631 day = MSTK_REG_DOM(mregs);
632 mon = MSTK_REG_MONTH(mregs);
633 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
637 /* Dallas 12887 RTC chip. */
639 /* Stolen from arch/i386/kernel/time.c, see there for
640 * credits and descriptive comments.
642 for (i = 0; i < 1000000; i++) {
643 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
647 for (i = 0; i < 1000000; i++) {
648 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
653 sec = CMOS_READ(RTC_SECONDS);
654 min = CMOS_READ(RTC_MINUTES);
655 hour = CMOS_READ(RTC_HOURS);
656 day = CMOS_READ(RTC_DAY_OF_MONTH);
657 mon = CMOS_READ(RTC_MONTH);
658 year = CMOS_READ(RTC_YEAR);
659 } while (sec != CMOS_READ(RTC_SECONDS));
660 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
668 if ((year += 1900) < 1970)
672 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
673 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
674 set_normalized_timespec(&wall_to_monotonic,
675 -xtime.tv_sec, -xtime.tv_nsec);
678 tmp = mostek_read(mregs + MOSTEK_CREG);
679 tmp &= ~MSTK_CREG_READ;
680 mostek_write(mregs + MOSTEK_CREG, tmp);
682 spin_unlock_irq(&mostek_lock);
686 void __init clock_probe(void)
688 struct linux_prom_registers clk_reg[2];
690 int node, busnd = -1, err;
692 struct linux_central *cbus;
694 struct linux_ebus *ebus = NULL;
695 struct sparc_isa_bridge *isa_br = NULL;
704 if (this_is_starfire) {
705 /* davem suggests we keep this within the 4M locked kernel image */
706 static char obp_gettod[256];
709 sprintf(obp_gettod, "h# %08x unix-gettod",
710 (unsigned int) (long) &unix_tod);
711 prom_feval(obp_gettod);
712 xtime.tv_sec = unix_tod;
713 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
714 set_normalized_timespec(&wall_to_monotonic,
715 -xtime.tv_sec, -xtime.tv_nsec);
719 local_irq_save(flags);
723 busnd = central_bus->child->prom_node;
725 /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
726 * That way we handle the presence of multiple properly.
728 * As a special case, machines with Central must provide the
732 if (ebus_chain != NULL) {
735 busnd = ebus->prom_node;
737 if (isa_chain != NULL) {
740 busnd = isa_br->prom_node;
743 if (sbus_root != NULL && busnd == -1)
744 busnd = sbus_root->prom_node;
747 prom_printf("clock_probe: problem, cannot find bus to search.\n");
751 node = prom_getchild(busnd);
757 prom_getstring(node, "model", model, sizeof(model));
758 if (strcmp(model, "mk48t02") &&
759 strcmp(model, "mk48t08") &&
760 strcmp(model, "mk48t59") &&
761 strcmp(model, "m5819") &&
762 strcmp(model, "m5819p") &&
763 strcmp(model, "m5823") &&
764 strcmp(model, "ds1287")) {
766 prom_printf("clock_probe: Central bus lacks timer chip.\n");
771 node = prom_getsibling(node);
773 while ((node == 0) && ebus != NULL) {
776 busnd = ebus->prom_node;
777 node = prom_getchild(busnd);
780 while ((node == 0) && isa_br != NULL) {
781 isa_br = isa_br->next;
782 if (isa_br != NULL) {
783 busnd = isa_br->prom_node;
784 node = prom_getchild(busnd);
789 prom_printf("clock_probe: Cannot find timer chip\n");
795 err = prom_getproperty(node, "reg", (char *)clk_reg,
798 prom_printf("clock_probe: Cannot get Mostek reg property\n");
803 apply_fhc_ranges(central_bus->child, clk_reg, 1);
804 apply_central_ranges(central_bus, clk_reg, 1);
807 else if (ebus != NULL) {
808 struct linux_ebus_device *edev;
810 for_each_ebusdev(edev, ebus)
811 if (edev->prom_node == node)
814 if (isa_chain != NULL)
816 prom_printf("%s: Mostek not probed by EBUS\n",
821 if (!strcmp(model, "ds1287") ||
822 !strcmp(model, "m5819") ||
823 !strcmp(model, "m5819p") ||
824 !strcmp(model, "m5823")) {
825 ds1287_regs = edev->resource[0].start;
827 mstk48t59_regs = (void __iomem *)
828 edev->resource[0].start;
829 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
833 else if (isa_br != NULL) {
834 struct sparc_isa_device *isadev;
837 for_each_isadev(isadev, isa_br)
838 if (isadev->prom_node == node)
840 if (isadev == NULL) {
841 prom_printf("%s: Mostek not probed by ISA\n");
844 if (!strcmp(model, "ds1287") ||
845 !strcmp(model, "m5819") ||
846 !strcmp(model, "m5819p") ||
847 !strcmp(model, "m5823")) {
848 ds1287_regs = isadev->resource.start;
850 mstk48t59_regs = (void __iomem *)
851 isadev->resource.start;
852 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
858 if (sbus_root->num_sbus_ranges) {
859 int nranges = sbus_root->num_sbus_ranges;
862 for (rngc = 0; rngc < nranges; rngc++)
863 if (clk_reg[0].which_io ==
864 sbus_root->sbus_ranges[rngc].ot_child_space)
866 if (rngc == nranges) {
867 prom_printf("clock_probe: Cannot find ranges for "
871 clk_reg[0].which_io =
872 sbus_root->sbus_ranges[rngc].ot_parent_space;
873 clk_reg[0].phys_addr +=
874 sbus_root->sbus_ranges[rngc].ot_parent_base;
878 if(model[5] == '0' && model[6] == '2') {
879 mstk48t02_regs = (void __iomem *)
880 (((u64)clk_reg[0].phys_addr) |
881 (((u64)clk_reg[0].which_io)<<32UL));
882 } else if(model[5] == '0' && model[6] == '8') {
883 mstk48t08_regs = (void __iomem *)
884 (((u64)clk_reg[0].phys_addr) |
885 (((u64)clk_reg[0].which_io)<<32UL));
886 mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
888 mstk48t59_regs = (void __iomem *)
889 (((u64)clk_reg[0].phys_addr) |
890 (((u64)clk_reg[0].which_io)<<32UL));
891 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
896 if (mstk48t02_regs != NULL) {
897 /* Report a low battery voltage condition. */
898 if (has_low_battery())
899 prom_printf("NVRAM: Low battery voltage!\n");
901 /* Kick start the clock if it is completely stopped. */
902 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
908 local_irq_restore(flags);
911 /* This is gets the master TICK_INT timer going. */
912 static unsigned long sparc64_init_timers(void)
917 extern void smp_tick_init(void);
920 if (tlb_type == spitfire) {
921 unsigned long ver, manuf, impl;
923 __asm__ __volatile__ ("rdpr %%ver, %0"
925 manuf = ((ver >> 48) & 0xffff);
926 impl = ((ver >> 32) & 0xffff);
927 if (manuf == 0x17 && impl == 0x13) {
928 /* Hummingbird, aka Ultra-IIe */
929 tick_ops = &hbtick_operations;
930 node = prom_root_node;
931 clock = prom_getint(node, "stick-frequency");
933 tick_ops = &tick_operations;
934 cpu_find_by_instance(0, &node, NULL);
935 clock = prom_getint(node, "clock-frequency");
938 tick_ops = &stick_operations;
939 node = prom_root_node;
940 clock = prom_getint(node, "stick-frequency");
942 timer_tick_offset = clock / HZ;
951 static void sparc64_start_timers(irqreturn_t (*cfunc)(int, void *, struct pt_regs *))
953 unsigned long pstate;
956 /* Register IRQ handler. */
957 err = request_irq(build_irq(0, 0, 0UL, 0UL), cfunc, 0,
961 prom_printf("Serious problem, cannot register TICK_INT\n");
965 /* Guarantee that the following sequences execute
968 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
969 "wrpr %0, %1, %%pstate"
973 tick_ops->init_tick(timer_tick_offset);
975 /* Restore PSTATE_IE. */
976 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
984 unsigned long udelay_val_ref;
985 unsigned long clock_tick_ref;
986 unsigned int ref_freq;
988 static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0, 0 };
990 unsigned long sparc64_get_clock_tick(unsigned int cpu)
992 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
994 if (ft->clock_tick_ref)
995 return ft->clock_tick_ref;
996 return cpu_data(cpu).clock_tick;
999 #ifdef CONFIG_CPU_FREQ
1001 static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
1004 struct cpufreq_freqs *freq = data;
1005 unsigned int cpu = freq->cpu;
1006 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
1008 if (!ft->ref_freq) {
1009 ft->ref_freq = freq->old;
1010 ft->udelay_val_ref = cpu_data(cpu).udelay_val;
1011 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
1013 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
1014 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
1015 (val == CPUFREQ_RESUMECHANGE)) {
1016 cpu_data(cpu).udelay_val =
1017 cpufreq_scale(ft->udelay_val_ref,
1020 cpu_data(cpu).clock_tick =
1021 cpufreq_scale(ft->clock_tick_ref,
1029 static struct notifier_block sparc64_cpufreq_notifier_block = {
1030 .notifier_call = sparc64_cpufreq_notifier
1033 #endif /* CONFIG_CPU_FREQ */
1035 static struct time_interpolator sparc64_cpu_interpolator = {
1036 .source = TIME_SOURCE_CPU,
1038 .mask = 0xffffffffffffffffLL
1041 /* The quotient formula is taken from the IA64 port. */
1042 #define SPARC64_NSEC_PER_CYC_SHIFT 30UL
1043 void __init time_init(void)
1045 unsigned long clock = sparc64_init_timers();
1047 sparc64_cpu_interpolator.frequency = clock;
1048 register_time_interpolator(&sparc64_cpu_interpolator);
1050 /* Now that the interpolator is registered, it is
1051 * safe to start the timer ticking.
1053 sparc64_start_timers(timer_interrupt);
1055 timer_ticks_per_nsec_quotient =
1056 (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
1057 (clock / 2)) / clock);
1059 #ifdef CONFIG_CPU_FREQ
1060 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
1061 CPUFREQ_TRANSITION_NOTIFIER);
1065 unsigned long long sched_clock(void)
1067 unsigned long ticks = tick_ops->get_tick();
1069 return (ticks * timer_ticks_per_nsec_quotient)
1070 >> SPARC64_NSEC_PER_CYC_SHIFT;
1073 static int set_rtc_mmss(unsigned long nowtime)
1075 int real_seconds, real_minutes, chip_minutes;
1076 void __iomem *mregs = mstk48t02_regs;
1078 unsigned long dregs = ds1287_regs;
1080 unsigned long dregs = 0UL;
1082 unsigned long flags;
1086 * Not having a register set can lead to trouble.
1087 * Also starfire doesn't have a tod clock.
1089 if (!mregs && !dregs)
1093 spin_lock_irqsave(&mostek_lock, flags);
1095 /* Read the current RTC minutes. */
1096 tmp = mostek_read(mregs + MOSTEK_CREG);
1097 tmp |= MSTK_CREG_READ;
1098 mostek_write(mregs + MOSTEK_CREG, tmp);
1100 chip_minutes = MSTK_REG_MIN(mregs);
1102 tmp = mostek_read(mregs + MOSTEK_CREG);
1103 tmp &= ~MSTK_CREG_READ;
1104 mostek_write(mregs + MOSTEK_CREG, tmp);
1107 * since we're only adjusting minutes and seconds,
1108 * don't interfere with hour overflow. This avoids
1109 * messing with unknown time zones but requires your
1110 * RTC not to be off by more than 15 minutes
1112 real_seconds = nowtime % 60;
1113 real_minutes = nowtime / 60;
1114 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1115 real_minutes += 30; /* correct for half hour time zone */
1118 if (abs(real_minutes - chip_minutes) < 30) {
1119 tmp = mostek_read(mregs + MOSTEK_CREG);
1120 tmp |= MSTK_CREG_WRITE;
1121 mostek_write(mregs + MOSTEK_CREG, tmp);
1123 MSTK_SET_REG_SEC(mregs,real_seconds);
1124 MSTK_SET_REG_MIN(mregs,real_minutes);
1126 tmp = mostek_read(mregs + MOSTEK_CREG);
1127 tmp &= ~MSTK_CREG_WRITE;
1128 mostek_write(mregs + MOSTEK_CREG, tmp);
1130 spin_unlock_irqrestore(&mostek_lock, flags);
1134 spin_unlock_irqrestore(&mostek_lock, flags);
1140 unsigned char save_control, save_freq_select;
1142 /* Stolen from arch/i386/kernel/time.c, see there for
1143 * credits and descriptive comments.
1145 spin_lock_irqsave(&rtc_lock, flags);
1146 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
1147 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1149 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
1150 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1152 chip_minutes = CMOS_READ(RTC_MINUTES);
1153 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1154 BCD_TO_BIN(chip_minutes);
1155 real_seconds = nowtime % 60;
1156 real_minutes = nowtime / 60;
1157 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1161 if (abs(real_minutes - chip_minutes) < 30) {
1162 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1163 BIN_TO_BCD(real_seconds);
1164 BIN_TO_BCD(real_minutes);
1166 CMOS_WRITE(real_seconds,RTC_SECONDS);
1167 CMOS_WRITE(real_minutes,RTC_MINUTES);
1170 "set_rtc_mmss: can't update from %d to %d\n",
1171 chip_minutes, real_minutes);
1175 CMOS_WRITE(save_control, RTC_CONTROL);
1176 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1177 spin_unlock_irqrestore(&rtc_lock, flags);