Import 2.3.18pre1
[davej-history.git] / arch / ppc / kernel / prep_time.c
blobf720841bec56910da7c5543d743972cd94ea7543
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/timex.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/init.h>
22 #include <asm/init.h>
23 #include <asm/segment.h>
24 #include <asm/io.h>
25 #include <asm/processor.h>
26 #include <asm/machdep.h>
27 #include <asm/prep_nvram.h>
28 #include <asm/mk48t59.h>
30 #include "time.h"
33 * The motorola uses the m48t18 rtc (includes DS1643) whose registers
34 * are at a higher end of nvram (1ff8-1fff) than the ibm mc146818
35 * rtc (ds1386) which has regs at addr 0-d). The intel gets
36 * past this because the bios emulates the mc146818.
38 * Why in the world did they have to use different clocks?
40 * Right now things are hacked to check which machine we're on then
41 * use the appropriate macro. This is very very ugly and I should
42 * probably have a function that checks which machine we're on then
43 * does things correctly transparently or a function pointer which
44 * is setup at boot time to use the correct addresses.
45 * -- Cort
49 * Set the hardware clock. -- Cort
51 __prep
52 int mc146818_set_rtc_time(unsigned long nowtime)
54 unsigned char save_control, save_freq_select;
55 struct rtc_time tm;
57 to_tm(nowtime, &tm);
59 /* tell the clock it's being set */
60 save_control = CMOS_READ(RTC_CONTROL);
62 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
64 /* stop and reset prescaler */
65 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
67 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
69 tm.tm_year = (tm.tm_year - 1900) % 100;
70 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
71 BIN_TO_BCD(tm.tm_sec);
72 BIN_TO_BCD(tm.tm_min);
73 BIN_TO_BCD(tm.tm_hour);
74 BIN_TO_BCD(tm.tm_mon);
75 BIN_TO_BCD(tm.tm_mday);
76 BIN_TO_BCD(tm.tm_year);
78 CMOS_WRITE(tm.tm_sec, RTC_SECONDS);
79 CMOS_WRITE(tm.tm_min, RTC_MINUTES);
80 CMOS_WRITE(tm.tm_hour, RTC_HOURS);
81 CMOS_WRITE(tm.tm_mon, RTC_MONTH);
82 CMOS_WRITE(tm.tm_mday, RTC_DAY_OF_MONTH);
83 CMOS_WRITE(tm.tm_year, RTC_YEAR);
85 /* The following flags have to be released exactly in this order,
86 * otherwise the DS12887 (popular MC146818A clone with integrated
87 * battery and quartz) will not reset the oscillator and will not
88 * update precisely 500 ms later. You won't find this mentioned in
89 * the Dallas Semiconductor data sheets, but who believes data
90 * sheets anyway ... -- Markus Kuhn
92 CMOS_WRITE(save_control, RTC_CONTROL);
93 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
95 return 0;
98 __prep
99 unsigned long mc146818_get_rtc_time(void)
101 unsigned int year, mon, day, hour, min, sec;
102 int i;
104 /* The Linux interpretation of the CMOS clock register contents:
105 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
106 * RTC registers show the second which has precisely just started.
107 * Let's hope other operating systems interpret the RTC the same way.
109 /* read RTC exactly on falling edge of update flag */
110 for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
111 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
112 break;
113 for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
114 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
115 break;
116 do { /* Isn't this overkill ? UIP above should guarantee consistency */
117 sec = CMOS_READ(RTC_SECONDS);
118 min = CMOS_READ(RTC_MINUTES);
119 hour = CMOS_READ(RTC_HOURS);
120 day = CMOS_READ(RTC_DAY_OF_MONTH);
121 mon = CMOS_READ(RTC_MONTH);
122 year = CMOS_READ(RTC_YEAR);
123 } while (sec != CMOS_READ(RTC_SECONDS));
124 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
125 || RTC_ALWAYS_BCD)
127 BCD_TO_BIN(sec);
128 BCD_TO_BIN(min);
129 BCD_TO_BIN(hour);
130 BCD_TO_BIN(day);
131 BCD_TO_BIN(mon);
132 BCD_TO_BIN(year);
134 if ((year += 1900) < 1970)
135 year += 100;
136 return mktime(year, mon, day, hour, min, sec);
139 __prep
140 int mk48t59_set_rtc_time(unsigned long nowtime)
142 unsigned char save_control;
143 struct rtc_time tm;
146 to_tm(nowtime, &tm);
148 /* tell the clock it's being written */
149 save_control = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLA);
151 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA,
152 (save_control | MK48T59_RTC_CA_WRITE));
154 tm.tm_year = (tm.tm_year - 1900) % 100;
155 BIN_TO_BCD(tm.tm_sec);
156 BIN_TO_BCD(tm.tm_min);
157 BIN_TO_BCD(tm.tm_hour);
158 BIN_TO_BCD(tm.tm_mon);
159 BIN_TO_BCD(tm.tm_mday);
160 BIN_TO_BCD(tm.tm_year);
162 ppc_md.nvram_write_val(MK48T59_RTC_SECONDS, tm.tm_sec);
163 ppc_md.nvram_write_val(MK48T59_RTC_MINUTES, tm.tm_min);
164 ppc_md.nvram_write_val(MK48T59_RTC_HOURS, tm.tm_hour);
165 ppc_md.nvram_write_val(MK48T59_RTC_MONTH, tm.tm_mon);
166 ppc_md.nvram_write_val(MK48T59_RTC_DAY_OF_MONTH, tm.tm_mday);
167 ppc_md.nvram_write_val(MK48T59_RTC_YEAR, tm.tm_year);
169 /* Turn off the write bit. */
170 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
172 return 0;
175 __prep
176 unsigned long mk48t59_get_rtc_time(void)
178 unsigned char save_control;
179 unsigned int year, mon, day, hour, min, sec;
180 int i;
182 /* Make sure the time is not stopped. */
183 save_control = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLB);
185 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA,
186 (save_control & (~MK48T59_RTC_CB_STOP)));
188 /* Now make sure the read bit is off so the value will change. */
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 /* Read the seconds value to see when it changes. */
194 sec = ppc_md.nvram_read_val(MK48T59_RTC_SECONDS);
196 /* Wait until the seconds value changes, then read the value. */
197 for (i = 0 ; i < 1000000 ; i++) { /* may take up to 1 second... */
198 if (ppc_md.nvram_read_val(MK48T59_RTC_SECONDS) != sec) {
199 break;
203 /* Set the register to read the value. */
204 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA,
205 (save_control | MK48T59_RTC_CA_READ));
207 sec = ppc_md.nvram_read_val(MK48T59_RTC_SECONDS);
208 min = ppc_md.nvram_read_val(MK48T59_RTC_MINUTES);
209 hour = ppc_md.nvram_read_val(MK48T59_RTC_HOURS);
210 day = ppc_md.nvram_read_val(MK48T59_RTC_DAY_OF_MONTH);
211 mon = ppc_md.nvram_read_val(MK48T59_RTC_MONTH);
212 year = ppc_md.nvram_read_val(MK48T59_RTC_YEAR);
214 /* Let the time values change again. */
215 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
217 BCD_TO_BIN(sec);
218 BCD_TO_BIN(min);
219 BCD_TO_BIN(hour);
220 BCD_TO_BIN(day);
221 BCD_TO_BIN(mon);
222 BCD_TO_BIN(year);
224 year = year + 1900;
225 if (year < 1970) {
226 year += 100;
229 return mktime(year, mon, day, hour, min, sec);