1 /*
2  *  arch/ppc/platforms/prep_time.c
3  *
4  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
5  *
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
9  */
10 
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>
22 
23 #include <asm/sections.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>
30 
31 #include <asm/time.h>
32 
33 extern spinlock_t rtc_lock;
34 
35 /*
36  * The motorola uses the m48t18 rtc (includes DS1643) whose registers
37  * are at a higher end of nvram (1ff8-1fff) than the ibm mc146818
38  * rtc (ds1386) which has regs at addr 0-d).  The intel gets
39  * past this because the bios emulates the mc146818.
40  *
41  * Why in the world did they have to use different clocks?
42  *
43  * Right now things are hacked to check which machine we're on then
44  * use the appropriate macro.  This is very very ugly and I should
45  * probably have a function that checks which machine we're on then
46  * does things correctly transparently or a function pointer which
47  * is setup at boot time to use the correct addresses.
48  * -- Cort
49  */
50 
51 /*
52  * Set the hardware clock. -- Cort
53  */
54 __prep
mc146818_set_rtc_time(unsigned long nowtime)55 int mc146818_set_rtc_time(unsigned long nowtime)
56 {
57 	unsigned char save_control, save_freq_select;
58 	struct rtc_time tm;
59 
60 	spin_lock(&rtc_lock);
61 	to_tm(nowtime, &tm);
62 
63 	/* tell the clock it's being set */
64 	save_control = CMOS_READ(RTC_CONTROL);
65 
66 	CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
67 
68 	/* stop and reset prescaler */
69 	save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
70 
71 	CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
72 
73         tm.tm_year = (tm.tm_year - 1900) % 100;
74 	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
75 		BIN_TO_BCD(tm.tm_sec);
76 		BIN_TO_BCD(tm.tm_min);
77 		BIN_TO_BCD(tm.tm_hour);
78 		BIN_TO_BCD(tm.tm_mon);
79 		BIN_TO_BCD(tm.tm_mday);
80 		BIN_TO_BCD(tm.tm_year);
81 	}
82 	CMOS_WRITE(tm.tm_sec,  RTC_SECONDS);
83 	CMOS_WRITE(tm.tm_min,  RTC_MINUTES);
84 	CMOS_WRITE(tm.tm_hour, RTC_HOURS);
85 	CMOS_WRITE(tm.tm_mon,  RTC_MONTH);
86 	CMOS_WRITE(tm.tm_mday, RTC_DAY_OF_MONTH);
87 	CMOS_WRITE(tm.tm_year, RTC_YEAR);
88 
89 	/* The following flags have to be released exactly in this order,
90 	 * otherwise the DS12887 (popular MC146818A clone with integrated
91 	 * battery and quartz) will not reset the oscillator and will not
92 	 * update precisely 500 ms later. You won't find this mentioned in
93 	 * the Dallas Semiconductor data sheets, but who believes data
94 	 * sheets anyway ...                           -- Markus Kuhn
95 	 */
96 	CMOS_WRITE(save_control,     RTC_CONTROL);
97 	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
98 	spin_unlock(&rtc_lock);
99 
100 	return 0;
101 }
102 
103 __prep
mc146818_get_rtc_time(void)104 unsigned long mc146818_get_rtc_time(void)
105 {
106 	unsigned int year, mon, day, hour, min, sec;
107 	int uip, i;
108 
109 	/* The Linux interpretation of the CMOS clock register contents:
110 	 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
111 	 * RTC registers show the second which has precisely just started.
112 	 * Let's hope other operating systems interpret the RTC the same way.
113 	 */
114 
115 	/* Since the UIP flag is set for about 2.2 ms and the clock
116 	 * is typically written with a precision of 1 jiffy, trying
117 	 * to obtain a precision better than a few milliseconds is
118 	 * an illusion. Only consistency is interesting, this also
119 	 * allows to use the routine for /dev/rtc without a potential
120 	 * 1 second kernel busy loop triggered by any reader of /dev/rtc.
121 	 */
122 
123 	for ( i = 0; i<1000000; i++) {
124 		uip = CMOS_READ(RTC_FREQ_SELECT);
125 		sec = CMOS_READ(RTC_SECONDS);
126 		min = CMOS_READ(RTC_MINUTES);
127 		hour = CMOS_READ(RTC_HOURS);
128 		day = CMOS_READ(RTC_DAY_OF_MONTH);
129 		mon = CMOS_READ(RTC_MONTH);
130 		year = CMOS_READ(RTC_YEAR);
131 		uip |= CMOS_READ(RTC_FREQ_SELECT);
132 		if ((uip & RTC_UIP)==0) break;
133 	}
134 
135 	if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
136 	    || RTC_ALWAYS_BCD)
137 	{
138 		BCD_TO_BIN(sec);
139 		BCD_TO_BIN(min);
140 		BCD_TO_BIN(hour);
141 		BCD_TO_BIN(day);
142 		BCD_TO_BIN(mon);
143 		BCD_TO_BIN(year);
144 	}
145 	if ((year += 1900) < 1970)
146 		year += 100;
147 	return mktime(year, mon, day, hour, min, sec);
148 }
149 
150 __prep
mk48t59_set_rtc_time(unsigned long nowtime)151 int mk48t59_set_rtc_time(unsigned long nowtime)
152 {
153 	unsigned char save_control;
154 	struct rtc_time tm;
155 
156 	spin_lock(&rtc_lock);
157 	to_tm(nowtime, &tm);
158 
159 	/* tell the clock it's being written */
160 	save_control = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLA);
161 
162 	ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA,
163 			     (save_control | MK48T59_RTC_CA_WRITE));
164 
165         tm.tm_year = (tm.tm_year - 1900) % 100;
166 	BIN_TO_BCD(tm.tm_sec);
167 	BIN_TO_BCD(tm.tm_min);
168 	BIN_TO_BCD(tm.tm_hour);
169 	BIN_TO_BCD(tm.tm_mon);
170 	BIN_TO_BCD(tm.tm_mday);
171 	BIN_TO_BCD(tm.tm_year);
172 
173 	ppc_md.nvram_write_val(MK48T59_RTC_SECONDS,      tm.tm_sec);
174 	ppc_md.nvram_write_val(MK48T59_RTC_MINUTES,      tm.tm_min);
175 	ppc_md.nvram_write_val(MK48T59_RTC_HOURS,        tm.tm_hour);
176 	ppc_md.nvram_write_val(MK48T59_RTC_MONTH,        tm.tm_mon);
177 	ppc_md.nvram_write_val(MK48T59_RTC_DAY_OF_MONTH, tm.tm_mday);
178 	ppc_md.nvram_write_val(MK48T59_RTC_YEAR,         tm.tm_year);
179 
180 	/* Turn off the write bit. */
181 	ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
182 	spin_unlock(&rtc_lock);
183 
184 	return 0;
185 }
186 
187 __prep
mk48t59_get_rtc_time(void)188 unsigned long mk48t59_get_rtc_time(void)
189 {
190 	unsigned char save_control;
191 	unsigned int year, mon, day, hour, min, sec;
192 
193 	/* Simple: freeze the clock, read it and allow updates again */
194 	save_control = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLA);
195 	save_control &= ~MK48T59_RTC_CA_READ;
196 	ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
197 
198 	/* Set the register to read the value. */
199 	ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA,
200 			     (save_control | MK48T59_RTC_CA_READ));
201 
202 	sec = ppc_md.nvram_read_val(MK48T59_RTC_SECONDS);
203 	min = ppc_md.nvram_read_val(MK48T59_RTC_MINUTES);
204 	hour = ppc_md.nvram_read_val(MK48T59_RTC_HOURS);
205 	day = ppc_md.nvram_read_val(MK48T59_RTC_DAY_OF_MONTH);
206 	mon = ppc_md.nvram_read_val(MK48T59_RTC_MONTH);
207 	year = ppc_md.nvram_read_val(MK48T59_RTC_YEAR);
208 
209 	/* Let the time values change again. */
210 	ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
211 
212 	BCD_TO_BIN(sec);
213 	BCD_TO_BIN(min);
214 	BCD_TO_BIN(hour);
215 	BCD_TO_BIN(day);
216 	BCD_TO_BIN(mon);
217 	BCD_TO_BIN(year);
218 
219 	year = year + 1900;
220 	if (year < 1970) {
221 		year += 100;
222 	}
223 
224 	return mktime(year, mon, day, hour, min, sec);
225 }
226