Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / arch / ppc / kernel / prep_time.c
blob78634c6e2591cbb6fc11462ec89e20b37e5c6d5d
1 /*
2 * linux/arch/i386/kernel/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 intel version
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>
16 #include <linux/mm.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/init.h>
23 #include <asm/init.h>
24 #include <asm/segment.h>
25 #include <asm/io.h>
26 #include <asm/processor.h>
27 #include <asm/machdep.h>
28 #include <asm/prep_nvram.h>
29 #include <asm/mk48t59.h>
31 #include <asm/time.h>
34 * The motorola uses the m48t18 rtc (includes DS1643) whose registers
35 * are at a higher end of nvram (1ff8-1fff) than the ibm mc146818
36 * rtc (ds1386) which has regs at addr 0-d). The intel gets
37 * past this because the bios emulates the mc146818.
39 * Why in the world did they have to use different clocks?
41 * Right now things are hacked to check which machine we're on then
42 * use the appropriate macro. This is very very ugly and I should
43 * probably have a function that checks which machine we're on then
44 * does things correctly transparently or a function pointer which
45 * is setup at boot time to use the correct addresses.
46 * -- Cort
50 * Set the hardware clock. -- Cort
52 __prep
53 int mc146818_set_rtc_time(unsigned long nowtime)
55 unsigned char save_control, save_freq_select;
56 struct rtc_time tm;
58 to_tm(nowtime, &tm);
60 /* tell the clock it's being set */
61 save_control = CMOS_READ(RTC_CONTROL);
63 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
65 /* stop and reset prescaler */
66 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
68 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
70 tm.tm_year = (tm.tm_year - 1900) % 100;
71 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
72 BIN_TO_BCD(tm.tm_sec);
73 BIN_TO_BCD(tm.tm_min);
74 BIN_TO_BCD(tm.tm_hour);
75 BIN_TO_BCD(tm.tm_mon);
76 BIN_TO_BCD(tm.tm_mday);
77 BIN_TO_BCD(tm.tm_year);
79 CMOS_WRITE(tm.tm_sec, RTC_SECONDS);
80 CMOS_WRITE(tm.tm_min, RTC_MINUTES);
81 CMOS_WRITE(tm.tm_hour, RTC_HOURS);
82 CMOS_WRITE(tm.tm_mon, RTC_MONTH);
83 CMOS_WRITE(tm.tm_mday, RTC_DAY_OF_MONTH);
84 CMOS_WRITE(tm.tm_year, RTC_YEAR);
86 /* The following flags have to be released exactly in this order,
87 * otherwise the DS12887 (popular MC146818A clone with integrated
88 * battery and quartz) will not reset the oscillator and will not
89 * update precisely 500 ms later. You won't find this mentioned in
90 * the Dallas Semiconductor data sheets, but who believes data
91 * sheets anyway ... -- Markus Kuhn
93 CMOS_WRITE(save_control, RTC_CONTROL);
94 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
96 return 0;
99 __prep
100 unsigned long mc146818_get_rtc_time(void)
102 unsigned int year, mon, day, hour, min, sec;
103 int uip, i;
105 /* The Linux interpretation of the CMOS clock register contents:
106 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
107 * RTC registers show the second which has precisely just started.
108 * Let's hope other operating systems interpret the RTC the same way.
111 /* Since the UIP flag is set for about 2.2 ms and the clock
112 * is typically written with a precision of 1 jiffy, trying
113 * to obtain a precision better than a few milliseconds is
114 * an illusion. Only consistency is interesting, this also
115 * allows to use the routine for /dev/rtc without a potential
116 * 1 second kernel busy loop triggered by any reader of /dev/rtc.
119 for ( i = 0; i<1000000; i++) {
120 uip = CMOS_READ(RTC_FREQ_SELECT);
121 sec = CMOS_READ(RTC_SECONDS);
122 min = CMOS_READ(RTC_MINUTES);
123 hour = CMOS_READ(RTC_HOURS);
124 day = CMOS_READ(RTC_DAY_OF_MONTH);
125 mon = CMOS_READ(RTC_MONTH);
126 year = CMOS_READ(RTC_YEAR);
127 uip |= CMOS_READ(RTC_FREQ_SELECT);
128 if ((uip & RTC_UIP)==0) break;
131 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
132 || RTC_ALWAYS_BCD)
134 BCD_TO_BIN(sec);
135 BCD_TO_BIN(min);
136 BCD_TO_BIN(hour);
137 BCD_TO_BIN(day);
138 BCD_TO_BIN(mon);
139 BCD_TO_BIN(year);
141 if ((year += 1900) < 1970)
142 year += 100;
143 return mktime(year, mon, day, hour, min, sec);
146 __prep
147 int mk48t59_set_rtc_time(unsigned long nowtime)
149 unsigned char save_control;
150 struct rtc_time tm;
153 to_tm(nowtime, &tm);
155 /* tell the clock it's being written */
156 save_control = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLA);
158 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA,
159 (save_control | MK48T59_RTC_CA_WRITE));
161 tm.tm_year = (tm.tm_year - 1900) % 100;
162 BIN_TO_BCD(tm.tm_sec);
163 BIN_TO_BCD(tm.tm_min);
164 BIN_TO_BCD(tm.tm_hour);
165 BIN_TO_BCD(tm.tm_mon);
166 BIN_TO_BCD(tm.tm_mday);
167 BIN_TO_BCD(tm.tm_year);
169 ppc_md.nvram_write_val(MK48T59_RTC_SECONDS, tm.tm_sec);
170 ppc_md.nvram_write_val(MK48T59_RTC_MINUTES, tm.tm_min);
171 ppc_md.nvram_write_val(MK48T59_RTC_HOURS, tm.tm_hour);
172 ppc_md.nvram_write_val(MK48T59_RTC_MONTH, tm.tm_mon);
173 ppc_md.nvram_write_val(MK48T59_RTC_DAY_OF_MONTH, tm.tm_mday);
174 ppc_md.nvram_write_val(MK48T59_RTC_YEAR, tm.tm_year);
176 /* Turn off the write bit. */
177 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
179 return 0;
182 __prep
183 unsigned long mk48t59_get_rtc_time(void)
185 unsigned char save_control;
186 unsigned int year, mon, day, hour, min, sec;
188 /* Simple: freeze the clock, read it and allow updates again */
189 save_control = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLA);
190 save_control &= ~MK48T59_RTC_CA_READ;
191 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
193 /* Set the register to read the value. */
194 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA,
195 (save_control | MK48T59_RTC_CA_READ));
197 sec = ppc_md.nvram_read_val(MK48T59_RTC_SECONDS);
198 min = ppc_md.nvram_read_val(MK48T59_RTC_MINUTES);
199 hour = ppc_md.nvram_read_val(MK48T59_RTC_HOURS);
200 day = ppc_md.nvram_read_val(MK48T59_RTC_DAY_OF_MONTH);
201 mon = ppc_md.nvram_read_val(MK48T59_RTC_MONTH);
202 year = ppc_md.nvram_read_val(MK48T59_RTC_YEAR);
204 /* Let the time values change again. */
205 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
207 BCD_TO_BIN(sec);
208 BCD_TO_BIN(min);
209 BCD_TO_BIN(hour);
210 BCD_TO_BIN(day);
211 BCD_TO_BIN(mon);
212 BCD_TO_BIN(year);
214 year = year + 1900;
215 if (year < 1970) {
216 year += 100;
219 return mktime(year, mon, day, hour, min, sec);