2 * arch/ppc/platforms/chrp_time.c
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
6 * Adapted for PowerPC (PReP) by Gary Thomas
7 * Modified by Cort Dougan (cort@cs.nmt.edu).
8 * Copied and modified from arch/i386/kernel/time.c
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/param.h>
15 #include <linux/string.h>
17 #include <linux/interrupt.h>
18 #include <linux/time.h>
19 #include <linux/timex.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/mc146818rtc.h>
22 #include <linux/init.h>
23 #include <linux/bcd.h>
26 #include <asm/nvram.h>
28 #include <asm/sections.h>
31 extern spinlock_t rtc_lock;
33 static int nvram_as1 = NVRAM_AS1;
34 static int nvram_as0 = NVRAM_AS0;
35 static int nvram_data = NVRAM_DATA;
37 long __init chrp_time_init(void)
39 struct device_node *rtcs;
42 rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
44 rtcs = find_compatible_devices("rtc", "ds1385-rtc");
45 if (rtcs == NULL || rtcs->addrs == NULL)
47 base = rtcs->addrs[0].address;
50 nvram_data = base + 1;
55 int chrp_cmos_clock_read(int addr)
58 outb(addr>>8, nvram_as1);
59 outb(addr, nvram_as0);
60 return (inb(nvram_data));
63 void chrp_cmos_clock_write(unsigned long val, int addr)
66 outb(addr>>8, nvram_as1);
67 outb(addr, nvram_as0);
68 outb(val, nvram_data);
73 * Set the hardware clock. -- Cort
75 int chrp_set_rtc_time(struct rtc_time *tmarg)
77 unsigned char save_control, save_freq_select;
78 struct rtc_time tm = *tmarg;
82 save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
84 chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
86 save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
88 chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
90 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
91 BIN_TO_BCD(tm.tm_sec);
92 BIN_TO_BCD(tm.tm_min);
93 BIN_TO_BCD(tm.tm_hour);
94 BIN_TO_BCD(tm.tm_mon);
95 BIN_TO_BCD(tm.tm_mday);
96 BIN_TO_BCD(tm.tm_year);
98 chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
99 chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
100 chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
101 chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
102 chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
103 chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
105 /* The following flags have to be released exactly in this order,
106 * otherwise the DS12887 (popular MC146818A clone with integrated
107 * battery and quartz) will not reset the oscillator and will not
108 * update precisely 500 ms later. You won't find this mentioned in
109 * the Dallas Semiconductor data sheets, but who believes data
110 * sheets anyway ... -- Markus Kuhn
112 chrp_cmos_clock_write(save_control, RTC_CONTROL);
113 chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
115 spin_unlock(&rtc_lock);
119 void chrp_get_rtc_time(struct rtc_time *tm)
121 unsigned int year, mon, day, hour, min, sec;
124 /* The Linux interpretation of the CMOS clock register contents:
125 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
126 * RTC registers show the second which has precisely just started.
127 * Let's hope other operating systems interpret the RTC the same way.
130 /* Since the UIP flag is set for about 2.2 ms and the clock
131 * is typically written with a precision of 1 jiffy, trying
132 * to obtain a precision better than a few milliseconds is
133 * an illusion. Only consistency is interesting, this also
134 * allows to use the routine for /dev/rtc without a potential
135 * 1 second kernel busy loop triggered by any reader of /dev/rtc.
138 for ( i = 0; i<1000000; i++) {
139 uip = chrp_cmos_clock_read(RTC_FREQ_SELECT);
140 sec = chrp_cmos_clock_read(RTC_SECONDS);
141 min = chrp_cmos_clock_read(RTC_MINUTES);
142 hour = chrp_cmos_clock_read(RTC_HOURS);
143 day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
144 mon = chrp_cmos_clock_read(RTC_MONTH);
145 year = chrp_cmos_clock_read(RTC_YEAR);
146 uip |= chrp_cmos_clock_read(RTC_FREQ_SELECT);
147 if ((uip & RTC_UIP)==0) break;
150 if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
169 void __init chrp_calibrate_decr(void)
171 struct device_node *cpu;
172 unsigned int freq, *fp;
175 * The cpu node should have a timebase-frequency property
176 * to tell us the rate at which the decrementer counts.
178 freq = 16666000; /* hardcoded default */
179 cpu = find_type_devices("cpu");
181 fp = (unsigned int *)
182 get_property(cpu, "timebase-frequency", NULL);