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 static __init unsigned long dummy_get_tick(void)
68 static __initdata struct sparc64_tick_ops dummy_tick_ops = {
69 .get_tick = dummy_get_tick,
72 struct sparc64_tick_ops *tick_ops __read_mostly = &dummy_tick_ops;
74 #define TICK_PRIV_BIT (1UL << 63)
77 unsigned long profile_pc(struct pt_regs *regs)
79 unsigned long pc = instruction_pointer(regs);
81 if (in_lock_functions(pc))
82 return regs->u_regs[UREG_RETPC];
85 EXPORT_SYMBOL(profile_pc);
88 static void tick_disable_protection(void)
90 /* Set things up so user can access tick register for profiling
91 * purposes. Also workaround BB_ERRATA_1 by doing a dummy
92 * read back of %tick after writing it.
98 "1: rd %%tick, %%g2\n"
99 " add %%g2, 6, %%g2\n"
100 " andn %%g2, %0, %%g2\n"
101 " wrpr %%g2, 0, %%tick\n"
104 : "r" (TICK_PRIV_BIT)
108 static void tick_init_tick(unsigned long offset)
110 tick_disable_protection();
112 __asm__ __volatile__(
114 " andn %%g1, %1, %%g1\n"
116 " add %%g1, %0, %%g1\n"
118 "1: wr %%g1, 0x0, %%tick_cmpr\n"
119 " rd %%tick_cmpr, %%g0"
121 : "r" (offset), "r" (TICK_PRIV_BIT)
125 static unsigned long tick_get_tick(void)
129 __asm__ __volatile__("rd %%tick, %0\n\t"
133 return ret & ~TICK_PRIV_BIT;
136 static unsigned long tick_get_compare(void)
140 __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
147 static unsigned long tick_add_compare(unsigned long adj)
149 unsigned long new_compare;
151 /* Workaround for Spitfire Errata (#54 I think??), I discovered
152 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
155 * On Blackbird writes to %tick_cmpr can fail, the
156 * workaround seems to be to execute the wr instruction
157 * at the start of an I-cache line, and perform a dummy
158 * read back from %tick_cmpr right after writing to it. -DaveM
160 __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
161 "ba,pt %%xcc, 1f\n\t"
162 " add %0, %1, %0\n\t"
165 "wr %0, 0, %%tick_cmpr\n\t"
166 "rd %%tick_cmpr, %%g0"
167 : "=&r" (new_compare)
173 static unsigned long tick_add_tick(unsigned long adj, unsigned long offset)
175 unsigned long new_tick, tmp;
177 /* Also need to handle Blackbird bug here too. */
178 __asm__ __volatile__("rd %%tick, %0\n\t"
180 "wrpr %0, 0, %%tick\n\t"
181 "andn %0, %4, %1\n\t"
182 "ba,pt %%xcc, 1f\n\t"
183 " add %1, %3, %1\n\t"
186 "wr %1, 0, %%tick_cmpr\n\t"
187 "rd %%tick_cmpr, %%g0"
188 : "=&r" (new_tick), "=&r" (tmp)
189 : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
194 static struct sparc64_tick_ops tick_operations __read_mostly = {
195 .init_tick = tick_init_tick,
196 .get_tick = tick_get_tick,
197 .get_compare = tick_get_compare,
198 .add_tick = tick_add_tick,
199 .add_compare = tick_add_compare,
200 .softint_mask = 1UL << 0,
203 static void stick_init_tick(unsigned long offset)
205 tick_disable_protection();
207 /* 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"
213 : "r" (TICK_PRIV_BIT)
216 __asm__ __volatile__(
217 " rd %%asr24, %%g1\n"
218 " andn %%g1, %1, %%g1\n"
219 " add %%g1, %0, %%g1\n"
220 " wr %%g1, 0x0, %%asr25"
222 : "r" (offset), "r" (TICK_PRIV_BIT)
226 static unsigned long stick_get_tick(void)
230 __asm__ __volatile__("rd %%asr24, %0"
233 return ret & ~TICK_PRIV_BIT;
236 static unsigned long stick_get_compare(void)
240 __asm__ __volatile__("rd %%asr25, %0"
246 static unsigned long stick_add_tick(unsigned long adj, unsigned long offset)
248 unsigned long new_tick, tmp;
250 __asm__ __volatile__("rd %%asr24, %0\n\t"
252 "wr %0, 0, %%asr24\n\t"
253 "andn %0, %4, %1\n\t"
256 : "=&r" (new_tick), "=&r" (tmp)
257 : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
262 static unsigned long stick_add_compare(unsigned long adj)
264 unsigned long new_compare;
266 __asm__ __volatile__("rd %%asr25, %0\n\t"
269 : "=&r" (new_compare)
275 static struct sparc64_tick_ops stick_operations __read_mostly = {
276 .init_tick = stick_init_tick,
277 .get_tick = stick_get_tick,
278 .get_compare = stick_get_compare,
279 .add_tick = stick_add_tick,
280 .add_compare = stick_add_compare,
281 .softint_mask = 1UL << 16,
284 /* On Hummingbird the STICK/STICK_CMPR register is implemented
285 * in I/O space. There are two 64-bit registers each, the
286 * first holds the low 32-bits of the value and the second holds
289 * Since STICK is constantly updating, we have to access it carefully.
291 * The sequence we use to read is:
294 * 3) read low again, if it rolled over increment high by 1
296 * Writing STICK safely is also tricky:
297 * 1) write low to zero
301 #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
302 #define HBIRD_STICK_ADDR 0x1fe0000f070UL
304 static unsigned long __hbird_read_stick(void)
306 unsigned long ret, tmp1, tmp2, tmp3;
307 unsigned long addr = HBIRD_STICK_ADDR;
309 __asm__ __volatile__("ldxa [%1] %5, %2\n\t"
310 "add %1, 0x8, %1\n\t"
311 "ldxa [%1] %5, %3\n\t"
312 "sub %1, 0x8, %1\n\t"
313 "ldxa [%1] %5, %4\n\t"
315 "blu,a,pn %%xcc, 1f\n\t"
318 "sllx %3, 32, %3\n\t"
320 : "=&r" (ret), "=&r" (addr),
321 "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
322 : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
327 static unsigned long __hbird_read_compare(void)
329 unsigned long low, high;
330 unsigned long addr = HBIRD_STICKCMP_ADDR;
332 __asm__ __volatile__("ldxa [%2] %3, %0\n\t"
333 "add %2, 0x8, %2\n\t"
335 : "=&r" (low), "=&r" (high), "=&r" (addr)
336 : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr));
338 return (high << 32UL) | low;
341 static void __hbird_write_stick(unsigned long val)
343 unsigned long low = (val & 0xffffffffUL);
344 unsigned long high = (val >> 32UL);
345 unsigned long addr = HBIRD_STICK_ADDR;
347 __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
348 "add %0, 0x8, %0\n\t"
349 "stxa %3, [%0] %4\n\t"
350 "sub %0, 0x8, %0\n\t"
353 : "0" (addr), "r" (low), "r" (high),
354 "i" (ASI_PHYS_BYPASS_EC_E));
357 static void __hbird_write_compare(unsigned long val)
359 unsigned long low = (val & 0xffffffffUL);
360 unsigned long high = (val >> 32UL);
361 unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
363 __asm__ __volatile__("stxa %3, [%0] %4\n\t"
364 "sub %0, 0x8, %0\n\t"
367 : "0" (addr), "r" (low), "r" (high),
368 "i" (ASI_PHYS_BYPASS_EC_E));
371 static void hbtick_init_tick(unsigned long offset)
375 tick_disable_protection();
377 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
378 * XXX into actually sending STICK interrupts. I think because
379 * XXX of how we store %tick_cmpr in head.S this somehow resets the
380 * XXX {TICK + STICK} interrupt mux. -DaveM
382 __hbird_write_stick(__hbird_read_stick());
384 val = __hbird_read_stick() & ~TICK_PRIV_BIT;
385 __hbird_write_compare(val + offset);
388 static unsigned long hbtick_get_tick(void)
390 return __hbird_read_stick() & ~TICK_PRIV_BIT;
393 static unsigned long hbtick_get_compare(void)
395 return __hbird_read_compare();
398 static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset)
402 val = __hbird_read_stick() + adj;
403 __hbird_write_stick(val);
405 val &= ~TICK_PRIV_BIT;
406 __hbird_write_compare(val + offset);
411 static unsigned long hbtick_add_compare(unsigned long adj)
413 unsigned long val = __hbird_read_compare() + adj;
415 val &= ~TICK_PRIV_BIT;
416 __hbird_write_compare(val);
421 static struct sparc64_tick_ops hbtick_operations __read_mostly = {
422 .init_tick = hbtick_init_tick,
423 .get_tick = hbtick_get_tick,
424 .get_compare = hbtick_get_compare,
425 .add_tick = hbtick_add_tick,
426 .add_compare = hbtick_add_compare,
427 .softint_mask = 1UL << 0,
430 /* timer_interrupt() needs to keep up the real-time clock,
431 * as well as call the "do_timer()" routine every clocktick
433 * NOTE: On SUN5 systems the ticker interrupt comes in using 2
434 * interrupts, one at level14 and one with softint bit 0.
436 unsigned long timer_tick_offset __read_mostly;
438 static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
440 #define TICK_SIZE (tick_nsec / 1000)
442 static inline void timer_check_rtc(void)
444 /* last time the cmos clock got updated */
445 static long last_rtc_update;
447 /* Determine when to update the Mostek clock. */
449 xtime.tv_sec > last_rtc_update + 660 &&
450 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
451 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
452 if (set_rtc_mmss(xtime.tv_sec) == 0)
453 last_rtc_update = xtime.tv_sec;
455 last_rtc_update = xtime.tv_sec - 600;
456 /* do it again in 60 s */
460 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
462 unsigned long ticks, compare, pstate;
464 write_seqlock(&xtime_lock);
468 profile_tick(CPU_PROFILING, regs);
469 update_process_times(user_mode(regs));
473 /* Guarantee that the following sequences execute
476 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
477 "wrpr %0, %1, %%pstate"
481 compare = tick_ops->add_compare(timer_tick_offset);
482 ticks = tick_ops->get_tick();
484 /* Restore PSTATE_IE. */
485 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
488 } while (time_after_eq(ticks, compare));
492 write_sequnlock(&xtime_lock);
498 void timer_tick_interrupt(struct pt_regs *regs)
500 write_seqlock(&xtime_lock);
506 write_sequnlock(&xtime_lock);
510 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
511 static void __init kick_start_clock(void)
513 void __iomem *regs = mstk48t02_regs;
517 prom_printf("CLOCK: Clock was stopped. Kick start ");
519 spin_lock_irq(&mostek_lock);
521 /* Turn on the kick start bit to start the oscillator. */
522 tmp = mostek_read(regs + MOSTEK_CREG);
523 tmp |= MSTK_CREG_WRITE;
524 mostek_write(regs + MOSTEK_CREG, tmp);
525 tmp = mostek_read(regs + MOSTEK_SEC);
527 mostek_write(regs + MOSTEK_SEC, tmp);
528 tmp = mostek_read(regs + MOSTEK_HOUR);
529 tmp |= MSTK_KICK_START;
530 mostek_write(regs + MOSTEK_HOUR, tmp);
531 tmp = mostek_read(regs + MOSTEK_CREG);
532 tmp &= ~MSTK_CREG_WRITE;
533 mostek_write(regs + MOSTEK_CREG, tmp);
535 spin_unlock_irq(&mostek_lock);
537 /* Delay to allow the clock oscillator to start. */
538 sec = MSTK_REG_SEC(regs);
539 for (i = 0; i < 3; i++) {
540 while (sec == MSTK_REG_SEC(regs))
541 for (count = 0; count < 100000; count++)
544 sec = MSTK_REG_SEC(regs);
548 spin_lock_irq(&mostek_lock);
550 /* Turn off kick start and set a "valid" time and date. */
551 tmp = mostek_read(regs + MOSTEK_CREG);
552 tmp |= MSTK_CREG_WRITE;
553 mostek_write(regs + MOSTEK_CREG, tmp);
554 tmp = mostek_read(regs + MOSTEK_HOUR);
555 tmp &= ~MSTK_KICK_START;
556 mostek_write(regs + MOSTEK_HOUR, tmp);
557 MSTK_SET_REG_SEC(regs,0);
558 MSTK_SET_REG_MIN(regs,0);
559 MSTK_SET_REG_HOUR(regs,0);
560 MSTK_SET_REG_DOW(regs,5);
561 MSTK_SET_REG_DOM(regs,1);
562 MSTK_SET_REG_MONTH(regs,8);
563 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
564 tmp = mostek_read(regs + MOSTEK_CREG);
565 tmp &= ~MSTK_CREG_WRITE;
566 mostek_write(regs + MOSTEK_CREG, tmp);
568 spin_unlock_irq(&mostek_lock);
570 /* Ensure the kick start bit is off. If it isn't, turn it off. */
571 while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
572 prom_printf("CLOCK: Kick start still on!\n");
574 spin_lock_irq(&mostek_lock);
576 tmp = mostek_read(regs + MOSTEK_CREG);
577 tmp |= MSTK_CREG_WRITE;
578 mostek_write(regs + MOSTEK_CREG, tmp);
580 tmp = mostek_read(regs + MOSTEK_HOUR);
581 tmp &= ~MSTK_KICK_START;
582 mostek_write(regs + MOSTEK_HOUR, tmp);
584 tmp = mostek_read(regs + MOSTEK_CREG);
585 tmp &= ~MSTK_CREG_WRITE;
586 mostek_write(regs + MOSTEK_CREG, tmp);
588 spin_unlock_irq(&mostek_lock);
591 prom_printf("CLOCK: Kick start procedure successful.\n");
594 /* Return nonzero if the clock chip battery is low. */
595 static int __init has_low_battery(void)
597 void __iomem *regs = mstk48t02_regs;
600 spin_lock_irq(&mostek_lock);
602 data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
603 mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
604 data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
605 mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
607 spin_unlock_irq(&mostek_lock);
609 return (data1 == data2); /* Was the write blocked? */
612 /* Probe for the real time clock chip. */
613 static void __init set_system_time(void)
615 unsigned int year, mon, day, hour, min, sec;
616 void __iomem *mregs = mstk48t02_regs;
618 unsigned long dregs = ds1287_regs;
620 unsigned long dregs = 0UL;
624 if (!mregs && !dregs) {
625 prom_printf("Something wrong, clock regs not mapped yet.\n");
630 spin_lock_irq(&mostek_lock);
632 /* Traditional Mostek chip. */
633 tmp = mostek_read(mregs + MOSTEK_CREG);
634 tmp |= MSTK_CREG_READ;
635 mostek_write(mregs + MOSTEK_CREG, tmp);
637 sec = MSTK_REG_SEC(mregs);
638 min = MSTK_REG_MIN(mregs);
639 hour = MSTK_REG_HOUR(mregs);
640 day = MSTK_REG_DOM(mregs);
641 mon = MSTK_REG_MONTH(mregs);
642 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
646 /* Dallas 12887 RTC chip. */
648 /* Stolen from arch/i386/kernel/time.c, see there for
649 * credits and descriptive comments.
651 for (i = 0; i < 1000000; i++) {
652 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
656 for (i = 0; i < 1000000; i++) {
657 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
662 sec = CMOS_READ(RTC_SECONDS);
663 min = CMOS_READ(RTC_MINUTES);
664 hour = CMOS_READ(RTC_HOURS);
665 day = CMOS_READ(RTC_DAY_OF_MONTH);
666 mon = CMOS_READ(RTC_MONTH);
667 year = CMOS_READ(RTC_YEAR);
668 } while (sec != CMOS_READ(RTC_SECONDS));
669 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
677 if ((year += 1900) < 1970)
681 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
682 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
683 set_normalized_timespec(&wall_to_monotonic,
684 -xtime.tv_sec, -xtime.tv_nsec);
687 tmp = mostek_read(mregs + MOSTEK_CREG);
688 tmp &= ~MSTK_CREG_READ;
689 mostek_write(mregs + MOSTEK_CREG, tmp);
691 spin_unlock_irq(&mostek_lock);
695 void __init clock_probe(void)
697 struct linux_prom_registers clk_reg[2];
699 int node, busnd = -1, err;
701 struct linux_central *cbus;
703 struct linux_ebus *ebus = NULL;
704 struct sparc_isa_bridge *isa_br = NULL;
713 if (this_is_starfire) {
714 /* davem suggests we keep this within the 4M locked kernel image */
715 static char obp_gettod[256];
718 sprintf(obp_gettod, "h# %08x unix-gettod",
719 (unsigned int) (long) &unix_tod);
720 prom_feval(obp_gettod);
721 xtime.tv_sec = unix_tod;
722 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
723 set_normalized_timespec(&wall_to_monotonic,
724 -xtime.tv_sec, -xtime.tv_nsec);
728 local_irq_save(flags);
732 busnd = central_bus->child->prom_node;
734 /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
735 * That way we handle the presence of multiple properly.
737 * As a special case, machines with Central must provide the
741 if (ebus_chain != NULL) {
744 busnd = ebus->prom_node;
746 if (isa_chain != NULL) {
749 busnd = isa_br->prom_node;
752 if (sbus_root != NULL && busnd == -1)
753 busnd = sbus_root->prom_node;
756 prom_printf("clock_probe: problem, cannot find bus to search.\n");
760 node = prom_getchild(busnd);
766 prom_getstring(node, "model", model, sizeof(model));
767 if (strcmp(model, "mk48t02") &&
768 strcmp(model, "mk48t08") &&
769 strcmp(model, "mk48t59") &&
770 strcmp(model, "m5819") &&
771 strcmp(model, "m5819p") &&
772 strcmp(model, "m5823") &&
773 strcmp(model, "ds1287")) {
775 prom_printf("clock_probe: Central bus lacks timer chip.\n");
780 node = prom_getsibling(node);
782 while ((node == 0) && ebus != NULL) {
785 busnd = ebus->prom_node;
786 node = prom_getchild(busnd);
789 while ((node == 0) && isa_br != NULL) {
790 isa_br = isa_br->next;
791 if (isa_br != NULL) {
792 busnd = isa_br->prom_node;
793 node = prom_getchild(busnd);
798 prom_printf("clock_probe: Cannot find timer chip\n");
804 err = prom_getproperty(node, "reg", (char *)clk_reg,
807 prom_printf("clock_probe: Cannot get Mostek reg property\n");
812 apply_fhc_ranges(central_bus->child, clk_reg, 1);
813 apply_central_ranges(central_bus, clk_reg, 1);
816 else if (ebus != NULL) {
817 struct linux_ebus_device *edev;
819 for_each_ebusdev(edev, ebus)
820 if (edev->prom_node == node)
823 if (isa_chain != NULL)
825 prom_printf("%s: Mostek not probed by EBUS\n",
830 if (!strcmp(model, "ds1287") ||
831 !strcmp(model, "m5819") ||
832 !strcmp(model, "m5819p") ||
833 !strcmp(model, "m5823")) {
834 ds1287_regs = edev->resource[0].start;
836 mstk48t59_regs = (void __iomem *)
837 edev->resource[0].start;
838 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
842 else if (isa_br != NULL) {
843 struct sparc_isa_device *isadev;
846 for_each_isadev(isadev, isa_br)
847 if (isadev->prom_node == node)
849 if (isadev == NULL) {
850 prom_printf("%s: Mostek not probed by ISA\n");
853 if (!strcmp(model, "ds1287") ||
854 !strcmp(model, "m5819") ||
855 !strcmp(model, "m5819p") ||
856 !strcmp(model, "m5823")) {
857 ds1287_regs = isadev->resource.start;
859 mstk48t59_regs = (void __iomem *)
860 isadev->resource.start;
861 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
867 if (sbus_root->num_sbus_ranges) {
868 int nranges = sbus_root->num_sbus_ranges;
871 for (rngc = 0; rngc < nranges; rngc++)
872 if (clk_reg[0].which_io ==
873 sbus_root->sbus_ranges[rngc].ot_child_space)
875 if (rngc == nranges) {
876 prom_printf("clock_probe: Cannot find ranges for "
880 clk_reg[0].which_io =
881 sbus_root->sbus_ranges[rngc].ot_parent_space;
882 clk_reg[0].phys_addr +=
883 sbus_root->sbus_ranges[rngc].ot_parent_base;
887 if(model[5] == '0' && model[6] == '2') {
888 mstk48t02_regs = (void __iomem *)
889 (((u64)clk_reg[0].phys_addr) |
890 (((u64)clk_reg[0].which_io)<<32UL));
891 } else if(model[5] == '0' && model[6] == '8') {
892 mstk48t08_regs = (void __iomem *)
893 (((u64)clk_reg[0].phys_addr) |
894 (((u64)clk_reg[0].which_io)<<32UL));
895 mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
897 mstk48t59_regs = (void __iomem *)
898 (((u64)clk_reg[0].phys_addr) |
899 (((u64)clk_reg[0].which_io)<<32UL));
900 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
905 if (mstk48t02_regs != NULL) {
906 /* Report a low battery voltage condition. */
907 if (has_low_battery())
908 prom_printf("NVRAM: Low battery voltage!\n");
910 /* Kick start the clock if it is completely stopped. */
911 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
917 local_irq_restore(flags);
920 /* This is gets the master TICK_INT timer going. */
921 static unsigned long sparc64_init_timers(void)
926 extern void smp_tick_init(void);
929 if (tlb_type == spitfire) {
930 unsigned long ver, manuf, impl;
932 __asm__ __volatile__ ("rdpr %%ver, %0"
934 manuf = ((ver >> 48) & 0xffff);
935 impl = ((ver >> 32) & 0xffff);
936 if (manuf == 0x17 && impl == 0x13) {
937 /* Hummingbird, aka Ultra-IIe */
938 tick_ops = &hbtick_operations;
939 node = prom_root_node;
940 clock = prom_getint(node, "stick-frequency");
942 tick_ops = &tick_operations;
943 cpu_find_by_instance(0, &node, NULL);
944 clock = prom_getint(node, "clock-frequency");
947 tick_ops = &stick_operations;
948 node = prom_root_node;
949 clock = prom_getint(node, "stick-frequency");
951 timer_tick_offset = clock / HZ;
960 static void sparc64_start_timers(irqreturn_t (*cfunc)(int, void *, struct pt_regs *))
962 unsigned long pstate;
965 /* Register IRQ handler. */
966 err = request_irq(build_irq(0, 0, 0UL, 0UL), cfunc, 0,
970 prom_printf("Serious problem, cannot register TICK_INT\n");
974 /* Guarantee that the following sequences execute
977 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
978 "wrpr %0, %1, %%pstate"
982 tick_ops->init_tick(timer_tick_offset);
984 /* Restore PSTATE_IE. */
985 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
993 unsigned long udelay_val_ref;
994 unsigned long clock_tick_ref;
995 unsigned int ref_freq;
997 static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0, 0 };
999 unsigned long sparc64_get_clock_tick(unsigned int cpu)
1001 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
1003 if (ft->clock_tick_ref)
1004 return ft->clock_tick_ref;
1005 return cpu_data(cpu).clock_tick;
1008 #ifdef CONFIG_CPU_FREQ
1010 static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
1013 struct cpufreq_freqs *freq = data;
1014 unsigned int cpu = freq->cpu;
1015 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
1017 if (!ft->ref_freq) {
1018 ft->ref_freq = freq->old;
1019 ft->udelay_val_ref = cpu_data(cpu).udelay_val;
1020 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
1022 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
1023 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
1024 (val == CPUFREQ_RESUMECHANGE)) {
1025 cpu_data(cpu).udelay_val =
1026 cpufreq_scale(ft->udelay_val_ref,
1029 cpu_data(cpu).clock_tick =
1030 cpufreq_scale(ft->clock_tick_ref,
1038 static struct notifier_block sparc64_cpufreq_notifier_block = {
1039 .notifier_call = sparc64_cpufreq_notifier
1042 #endif /* CONFIG_CPU_FREQ */
1044 static struct time_interpolator sparc64_cpu_interpolator = {
1045 .source = TIME_SOURCE_CPU,
1047 .mask = 0xffffffffffffffffLL
1050 /* The quotient formula is taken from the IA64 port. */
1051 #define SPARC64_NSEC_PER_CYC_SHIFT 30UL
1052 void __init time_init(void)
1054 unsigned long clock = sparc64_init_timers();
1056 sparc64_cpu_interpolator.frequency = clock;
1057 register_time_interpolator(&sparc64_cpu_interpolator);
1059 /* Now that the interpolator is registered, it is
1060 * safe to start the timer ticking.
1062 sparc64_start_timers(timer_interrupt);
1064 timer_ticks_per_nsec_quotient =
1065 (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
1066 (clock / 2)) / clock);
1068 #ifdef CONFIG_CPU_FREQ
1069 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
1070 CPUFREQ_TRANSITION_NOTIFIER);
1074 unsigned long long sched_clock(void)
1076 unsigned long ticks = tick_ops->get_tick();
1078 return (ticks * timer_ticks_per_nsec_quotient)
1079 >> SPARC64_NSEC_PER_CYC_SHIFT;
1082 static int set_rtc_mmss(unsigned long nowtime)
1084 int real_seconds, real_minutes, chip_minutes;
1085 void __iomem *mregs = mstk48t02_regs;
1087 unsigned long dregs = ds1287_regs;
1089 unsigned long dregs = 0UL;
1091 unsigned long flags;
1095 * Not having a register set can lead to trouble.
1096 * Also starfire doesn't have a tod clock.
1098 if (!mregs && !dregs)
1102 spin_lock_irqsave(&mostek_lock, flags);
1104 /* Read the current RTC minutes. */
1105 tmp = mostek_read(mregs + MOSTEK_CREG);
1106 tmp |= MSTK_CREG_READ;
1107 mostek_write(mregs + MOSTEK_CREG, tmp);
1109 chip_minutes = MSTK_REG_MIN(mregs);
1111 tmp = mostek_read(mregs + MOSTEK_CREG);
1112 tmp &= ~MSTK_CREG_READ;
1113 mostek_write(mregs + MOSTEK_CREG, tmp);
1116 * since we're only adjusting minutes and seconds,
1117 * don't interfere with hour overflow. This avoids
1118 * messing with unknown time zones but requires your
1119 * RTC not to be off by more than 15 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; /* correct for half hour time zone */
1127 if (abs(real_minutes - chip_minutes) < 30) {
1128 tmp = mostek_read(mregs + MOSTEK_CREG);
1129 tmp |= MSTK_CREG_WRITE;
1130 mostek_write(mregs + MOSTEK_CREG, tmp);
1132 MSTK_SET_REG_SEC(mregs,real_seconds);
1133 MSTK_SET_REG_MIN(mregs,real_minutes);
1135 tmp = mostek_read(mregs + MOSTEK_CREG);
1136 tmp &= ~MSTK_CREG_WRITE;
1137 mostek_write(mregs + MOSTEK_CREG, tmp);
1139 spin_unlock_irqrestore(&mostek_lock, flags);
1143 spin_unlock_irqrestore(&mostek_lock, flags);
1149 unsigned char save_control, save_freq_select;
1151 /* Stolen from arch/i386/kernel/time.c, see there for
1152 * credits and descriptive comments.
1154 spin_lock_irqsave(&rtc_lock, flags);
1155 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
1156 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1158 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
1159 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1161 chip_minutes = CMOS_READ(RTC_MINUTES);
1162 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1163 BCD_TO_BIN(chip_minutes);
1164 real_seconds = nowtime % 60;
1165 real_minutes = nowtime / 60;
1166 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1170 if (abs(real_minutes - chip_minutes) < 30) {
1171 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1172 BIN_TO_BCD(real_seconds);
1173 BIN_TO_BCD(real_minutes);
1175 CMOS_WRITE(real_seconds,RTC_SECONDS);
1176 CMOS_WRITE(real_minutes,RTC_MINUTES);
1179 "set_rtc_mmss: can't update from %d to %d\n",
1180 chip_minutes, real_minutes);
1184 CMOS_WRITE(save_control, RTC_CONTROL);
1185 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1186 spin_unlock_irqrestore(&rtc_lock, flags);