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>
17 #include <linux/interrupt.h>
18 #include <linux/timex.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/init.h>
23 #include <asm/segment.h>
25 #include <asm/processor.h>
26 #include <asm/machdep.h>
27 #include <asm/prep_nvram.h>
28 #include <asm/mk48t59.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.
49 * Set the hardware clock. -- Cort
52 int mc146818_set_rtc_time(unsigned long nowtime
)
54 unsigned char save_control
, save_freq_select
;
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
);
99 unsigned long mc146818_get_rtc_time(void)
101 unsigned int year
, mon
, day
, hour
, min
, sec
;
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
)
113 for (i
= 0 ; i
< 1000000 ; i
++) /* must try at least 2.228 ms */
114 if (!(CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
))
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
)
134 if ((year
+= 1900) < 1970)
136 return mktime(year
, mon
, day
, hour
, min
, sec
);
140 int mk48t59_set_rtc_time(unsigned long nowtime
)
142 unsigned char save_control
;
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
);
176 unsigned long mk48t59_get_rtc_time(void)
178 unsigned char save_control
;
179 unsigned int year
, mon
, day
, hour
, min
, sec
;
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
) {
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
);
229 return mktime(year
, mon
, day
, hour
, min
, sec
);