rtclock.c revision 1.7 1 /* $NetBSD: rtclock.c,v 1.7 2001/01/09 11:38:32 minoura Exp $ */
2
3 /*
4 * Copyright 1993, 1994 Masaru Oki
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Masaru Oki.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * X680x0 internal real time clock interface
35 * alarm is not supported.
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/reboot.h>
44 #include <sys/file.h>
45 #include <sys/kernel.h>
46 #include <sys/device.h>
47
48 #include <machine/bus.h>
49
50 #include <arch/x68k/dev/rtclock_var.h>
51 #include <arch/x68k/dev/intiovar.h>
52
53 static u_long rtgettod __P((void));
54 static int rtsettod __P((long));
55
56 static int rtc_match __P((struct device *, struct cfdata *, void *));
57 static void rtc_attach __P((struct device *, struct device *, void *));
58
59 int rtclockinit __P((void));
60
61 struct cfattach rtc_ca = {
62 sizeof(struct rtc_softc), rtc_match, rtc_attach
63 };
64
65 static int
66 rtc_match(parent, cf, aux)
67 struct device *parent;
68 struct cfdata *cf;
69 void *aux;
70 {
71 struct intio_attach_args *ia = aux;
72
73 if (strcmp (ia->ia_name, "rtc") != 0)
74 return (0);
75 if (cf->cf_unit != 0)
76 return (0);
77
78 /* fixed address */
79 if (ia->ia_addr != RTC_ADDR)
80 return (0);
81 if (ia->ia_intr != -1)
82 return (0);
83
84 return (1);
85 }
86
87
88 static struct rtc_softc *rtc; /* XXX: softc cache */
89
90 static void
91 rtc_attach(parent, self, aux)
92 struct device *parent, *self;
93 void *aux;
94 {
95 struct rtc_softc *sc = (struct rtc_softc *)self;
96 struct intio_attach_args *ia = aux;
97 int r;
98
99 ia->ia_size = 0x20;
100 r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
101 #ifdef DIAGNOSTIC
102 if (r)
103 panic ("IO map for RTC corruption??");
104 #endif
105
106
107 sc->sc_bst = ia->ia_bst;
108 bus_space_map(sc->sc_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
109 rtc = sc;
110
111 rtclockinit();
112 printf (": RP5C15\n");
113 }
114
115
116
117 /*
118 * x68k/clock.c calls thru this vector, if it is set, to read
119 * the realtime clock.
120 */
121 u_long (*gettod) __P((void));
122 int (*settod) __P((long));
123
124 int
125 rtclockinit()
126 {
127 if (rtgettod()) {
128 gettod = rtgettod;
129 settod = rtsettod;
130 } else {
131 return 0;
132 }
133 return 1;
134 }
135
136 static int month_days[12] = {
137 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
138 };
139
140 static u_long
141 rtgettod()
142 {
143 register int i;
144 register u_long tmp;
145 int year, month, day, hour, min, sec;
146
147 /* hold clock */
148 RTC_WRITE(RTC_MODE, RTC_HOLD_CLOCK);
149
150 /* read it */
151 sec = RTC_REG(RTC_SEC10) * 10 + RTC_REG(RTC_SEC);
152 min = RTC_REG(RTC_MIN10) * 10 + RTC_REG(RTC_MIN);
153 hour = RTC_REG(RTC_HOUR10) * 10 + RTC_REG(RTC_HOUR);
154 day = RTC_REG(RTC_DAY10) * 10 + RTC_REG(RTC_DAY);
155 month = RTC_REG(RTC_MON10) * 10 + RTC_REG(RTC_MON);
156 year = RTC_REG(RTC_YEAR10) * 10 + RTC_REG(RTC_YEAR) + 1980;
157
158 /* let it run again.. */
159 RTC_WRITE(RTC_MODE, RTC_FREE_CLOCK);
160
161 #ifdef DIAGNOSTIC
162 range_test(hour, 0, 23);
163 range_test(day, 1, 31);
164 range_test(month, 1, 12);
165 range_test(year, STARTOFTIME, 2079);
166 #endif
167
168 tmp = 0;
169
170 for (i = STARTOFTIME; i < year; i++)
171 tmp += days_in_year(i);
172 if (leapyear(year) && month > FEBRUARY)
173 tmp++;
174
175 for (i = 1; i < month; i++)
176 tmp += days_in_month(i);
177
178 tmp += (day - 1);
179
180 tmp = ((tmp * 24 + hour) * 60 + min + rtc_offset) * 60 + sec;
181
182 return tmp;
183 }
184
185 static int
186 rtsettod (tim)
187 long tim;
188 {
189 /*
190 * I don't know if setting the clock is analogous
191 * to reading it, I don't have demo-code for setting.
192 * just give it a try..
193 */
194 register int i;
195 register long hms, day;
196 u_char sec1, sec2;
197 u_char min1, min2;
198 u_char hour1, hour2;
199 u_char day1, day2;
200 u_char mon1, mon2;
201 u_char year1, year2;
202
203 tim -= (rtc_offset * 60);
204
205 /* prepare values to be written to clock */
206 day = tim / SECDAY;
207 hms = tim % SECDAY;
208
209 hour2 = hms / 3600;
210 hour1 = hour2 / 10;
211 hour2 %= 10;
212
213 min2 = (hms % 3600) / 60;
214 min1 = min2 / 10;
215 min2 %= 10;
216
217 sec2 = (hms % 3600) % 60;
218 sec1 = sec2 / 10;
219 sec2 %= 10;
220
221 /* Number of years in days */
222 for (i = STARTOFTIME - 1980; day >= days_in_year(i); i++)
223 day -= days_in_year(i);
224 year1 = i / 10;
225 year2 = i % 10;
226
227 /* Number of months in days left */
228 if (leapyear(i))
229 days_in_month(FEBRUARY) = 29;
230 for (i = 1; day >= days_in_month(i); i++)
231 day -= days_in_month(i);
232 days_in_month(FEBRUARY) = 28;
233
234 mon1 = i / 10;
235 mon2 = i % 10;
236
237 /* Days are what is left over (+1) from all that. */
238 day ++;
239 day1 = day / 10;
240 day2 = day % 10;
241
242 RTC_WRITE(RTC_MODE, RTC_HOLD_CLOCK);
243 RTC_WRITE(RTC_SEC10, sec1);
244 RTC_WRITE(RTC_SEC, sec2);
245 RTC_WRITE(RTC_MIN10, min1);
246 RTC_WRITE(RTC_MIN, min2);
247 RTC_WRITE(RTC_HOUR10, hour1);
248 RTC_WRITE(RTC_HOUR, hour2);
249 RTC_WRITE(RTC_DAY10, day1);
250 RTC_WRITE(RTC_DAY, day2);
251 RTC_WRITE(RTC_MON10, mon1);
252 RTC_WRITE(RTC_MON, mon2);
253 RTC_WRITE(RTC_YEAR10, year1);
254 RTC_WRITE(RTC_YEAR, year2);
255 RTC_WRITE(RTC_MODE, RTC_FREE_CLOCK);
256
257 return 1;
258 }
259