rtc.c revision 1.2 1 1.2 sato /* $NetBSD: rtc.c,v 1.2 1999/12/07 04:54:54 sato Exp $ */
2 1.1 takemura
3 1.1 takemura /*-
4 1.1 takemura * Copyright (c) 1999 Shin Takemura. All rights reserved.
5 1.1 takemura * Copyright (c) 1999 SATO Kazumi. All rights reserved.
6 1.1 takemura * Copyright (c) 1999 PocketBSD Project. All rights reserved.
7 1.1 takemura *
8 1.1 takemura * Redistribution and use in source and binary forms, with or without
9 1.1 takemura * modification, are permitted provided that the following conditions
10 1.1 takemura * are met:
11 1.1 takemura * 1. Redistributions of source code must retain the above copyright
12 1.1 takemura * notice, this list of conditions and the following disclaimer.
13 1.1 takemura * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 takemura * notice, this list of conditions and the following disclaimer in the
15 1.1 takemura * documentation and/or other materials provided with the distribution.
16 1.1 takemura * 3. All advertising materials mentioning features or use of this software
17 1.1 takemura * must display the following acknowledgement:
18 1.1 takemura * This product includes software developed by the PocketBSD project
19 1.1 takemura * and its contributors.
20 1.1 takemura * 4. Neither the name of the project nor the names of its contributors
21 1.1 takemura * may be used to endorse or promote products derived from this software
22 1.1 takemura * without specific prior written permission.
23 1.1 takemura *
24 1.1 takemura * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 takemura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 takemura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 takemura * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 takemura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 takemura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 takemura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 takemura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 takemura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 takemura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 takemura * SUCH DAMAGE.
35 1.1 takemura *
36 1.1 takemura */
37 1.1 takemura
38 1.1 takemura #include <sys/param.h>
39 1.1 takemura #include <sys/systm.h>
40 1.1 takemura #include <sys/device.h>
41 1.1 takemura #include <sys/reboot.h>
42 1.1 takemura
43 1.1 takemura #include <machine/bus.h>
44 1.1 takemura #include <machine/clock_machdep.h>
45 1.1 takemura #include <machine/cpu.h>
46 1.1 takemura
47 1.1 takemura #include <hpcmips/vr/vr.h>
48 1.1 takemura #include <hpcmips/vr/vripvar.h>
49 1.1 takemura #include <hpcmips/vr/rtcreg.h>
50 1.1 takemura #include <dev/dec/clockvar.h>
51 1.1 takemura
52 1.1 takemura #if 0
53 1.1 takemura #define RTCDEBUG /* rtc debugging infomation */
54 1.1 takemura #define RTC_HEARTBEAT /* HEARTBEAT print */
55 1.1 takemura #define RECALC_CPUSPEED /* cpuspeed recalculaton */
56 1.1 takemura #define RECALC_CPUSPEED_DEBUG /* XXX */
57 1.1 takemura #endif
58 1.1 takemura
59 1.1 takemura
60 1.1 takemura struct vrrtc_softc {
61 1.1 takemura struct device sc_dev;
62 1.1 takemura bus_space_tag_t sc_iot;
63 1.1 takemura bus_space_handle_t sc_ioh;
64 1.1 takemura void *sc_ih;
65 1.1 takemura };
66 1.1 takemura
67 1.1 takemura void clock_init __P((struct device *));
68 1.1 takemura void clock_get __P((struct device *, time_t, struct clocktime *));
69 1.1 takemura void clock_set __P((struct device *, struct clocktime *));
70 1.1 takemura
71 1.1 takemura static const struct clockfns clockfns = {
72 1.1 takemura clock_init, clock_get, clock_set,
73 1.1 takemura };
74 1.1 takemura
75 1.1 takemura int vrrtc_match __P((struct device *, struct cfdata *, void *));
76 1.1 takemura void vrrtc_attach __P((struct device *, struct device *, void *));
77 1.1 takemura int vrrtc_intr __P((void*, u_int32_t, u_int32_t));
78 1.1 takemura
79 1.1 takemura struct cfattach vrrtc_ca = {
80 1.1 takemura sizeof(struct vrrtc_softc), vrrtc_match, vrrtc_attach
81 1.1 takemura };
82 1.1 takemura
83 1.1 takemura void vrrtc_write __P((struct vrrtc_softc *, int, unsigned short));
84 1.1 takemura unsigned short vrrtc_read __P((struct vrrtc_softc *, int));
85 1.1 takemura void cvt_timehl_ct __P((u_long, u_long, struct clocktime *));
86 1.1 takemura int vrrtc_recalc_cpuspeed __P((struct device *));
87 1.1 takemura
88 1.1 takemura
89 1.1 takemura extern int rtc_offset;
90 1.1 takemura
91 1.1 takemura int
92 1.1 takemura vrrtc_match(parent, cf, aux)
93 1.1 takemura struct device *parent;
94 1.1 takemura struct cfdata *cf;
95 1.1 takemura void *aux;
96 1.1 takemura {
97 1.1 takemura return(1);
98 1.1 takemura }
99 1.1 takemura
100 1.1 takemura inline void
101 1.1 takemura vrrtc_write(sc, port, val)
102 1.1 takemura struct vrrtc_softc *sc;
103 1.1 takemura int port;
104 1.1 takemura unsigned short val;
105 1.1 takemura {
106 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
107 1.1 takemura }
108 1.1 takemura
109 1.1 takemura inline unsigned short
110 1.1 takemura vrrtc_read(sc, port)
111 1.1 takemura struct vrrtc_softc *sc;
112 1.1 takemura int port;
113 1.1 takemura {
114 1.1 takemura return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
115 1.1 takemura }
116 1.1 takemura
117 1.1 takemura void
118 1.1 takemura vrrtc_attach(parent, self, aux)
119 1.1 takemura struct device *parent;
120 1.1 takemura struct device *self;
121 1.1 takemura void *aux;
122 1.1 takemura {
123 1.1 takemura struct vrip_attach_args *va = aux;
124 1.1 takemura struct vrrtc_softc *sc = (void*)self;
125 1.1 takemura
126 1.1 takemura sc->sc_iot = va->va_iot;
127 1.1 takemura if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
128 1.1 takemura 0 /* no flags */, &sc->sc_ioh)) {
129 1.1 takemura printf("vrrtc_attach: can't map i/o space\n");
130 1.1 takemura return;
131 1.1 takemura }
132 1.1 takemura /* RTC interrupt handler is directly dispatched from CPU intr */
133 1.1 takemura vr_intr_establish(VR_INTR1, vrrtc_intr, sc);
134 1.1 takemura /* But need to set level 1 interupt mask register,
135 1.1 takemura * so regsiter fake interrurpt handler
136 1.1 takemura */
137 1.1 takemura if (!(sc->sc_ih = vrip_intr_establish(va->va_vc, va->va_intr,
138 1.1 takemura IPL_CLOCK, 0, 0))) {
139 1.1 takemura printf (":can't map interrupt.\n");
140 1.1 takemura return;
141 1.1 takemura }
142 1.1 takemura /*
143 1.1 takemura * Rtc is attached to call this routine
144 1.1 takemura * before cpu_initclock() calls clock_init().
145 1.1 takemura * So we must disable all interrupt for now.
146 1.1 takemura */
147 1.1 takemura /*
148 1.1 takemura * Disable all rtc interrupts
149 1.1 takemura */
150 1.1 takemura /* Disable Elapse compare intr */
151 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_H_REG_W, 0);
152 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_M_REG_W, 0);
153 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_L_REG_W, 0);
154 1.1 takemura /* Disable RTC Long1 intr */
155 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W, 0);
156 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_L_REG_W, 0);
157 1.1 takemura /* Disable RTC Long2 intr */
158 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL2_H_REG_W, 0);
159 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL2_L_REG_W, 0);
160 1.1 takemura /* Disable RTC TCLK intr */
161 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, TCLK_H_REG_W, 0);
162 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, TCLK_L_REG_W, 0);
163 1.1 takemura /*
164 1.1 takemura * Clear all rtc intrrupts.
165 1.1 takemura */
166 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCINT_REG_W, RTCINT_ALL);
167 1.1 takemura
168 1.1 takemura clockattach(&sc->sc_dev, &clockfns);
169 1.1 takemura }
170 1.1 takemura
171 1.1 takemura int
172 1.1 takemura vrrtc_intr(arg, pc, statusReg)
173 1.1 takemura void *arg;
174 1.1 takemura u_int32_t pc;
175 1.1 takemura u_int32_t statusReg;
176 1.1 takemura {
177 1.1 takemura struct vrrtc_softc *sc = arg;
178 1.1 takemura struct clockframe cf;
179 1.1 takemura
180 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCINT_REG_W, RTCINT_ALL);
181 1.1 takemura cf.pc = pc;
182 1.1 takemura cf.sr = statusReg;
183 1.1 takemura hardclock(&cf);
184 1.1 takemura intrcnt[HARDCLOCK]++;
185 1.1 takemura
186 1.1 takemura #ifdef RTC_HEARTBEAT
187 1.1 takemura if ((intrcnt[HARDCLOCK] % (CLOCK_RATE * 5)) == 0) {
188 1.1 takemura struct clocktime ct;
189 1.1 takemura clock_get((struct device *)sc, NULL, &ct);
190 1.1 takemura printf("%s(%d): rtc_intr: %2d.%2d.%2d %02d:%02d:%02d\n",
191 1.1 takemura __FILE__, __LINE__,
192 1.1 takemura ct.year, ct.mon, ct.day,
193 1.1 takemura ct.hour, ct.min, ct.sec);
194 1.1 takemura }
195 1.1 takemura #endif
196 1.1 takemura return 0;
197 1.1 takemura }
198 1.1 takemura
199 1.1 takemura
200 1.1 takemura int
201 1.1 takemura vrrtc_recalc_cpuspeed(dev)
202 1.1 takemura struct device *dev;
203 1.1 takemura {
204 1.1 takemura struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
205 1.1 takemura u_long otimeh;
206 1.1 takemura u_long otimel;
207 1.1 takemura u_long timeh;
208 1.1 takemura u_long timel;
209 1.1 takemura
210 1.1 takemura otimeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_H_REG_W);
211 1.1 takemura otimel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_M_REG_W);
212 1.1 takemura otimel = (otimel << 16)
213 1.1 takemura | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_L_REG_W);
214 1.1 takemura
215 1.1 takemura #define MSEC 1000
216 1.1 takemura /* wait 1msec */
217 1.1 takemura DELAY(MSEC);
218 1.1 takemura
219 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_H_REG_W);
220 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_M_REG_W);
221 1.1 takemura timel = (timel << 16)
222 1.1 takemura | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_L_REG_W);
223 1.1 takemura
224 1.1 takemura if (timeh-otimeh > 0){
225 1.1 takemura /* cpuspeed is too large (> 2 sec)*/
226 1.1 takemura cpuspeed = cpuspeed/((timeh-otimeh)*2*MSEC);
227 1.1 takemura cpuspeed +=1;
228 1.1 takemura return 0;
229 1.1 takemura }
230 1.1 takemura if (timel-otimel < (ETIME_L_HZ/MSEC/10)) {
231 1.1 takemura /* cpuspeed is too small (< 0.1msec) */
232 1.1 takemura cpuspeed *=10;
233 1.1 takemura return -1;
234 1.1 takemura }
235 1.1 takemura cpuspeed = cpuspeed * (ETIME_L_HZ/MSEC) / (timel-otimel);
236 1.1 takemura return 0;
237 1.1 takemura }
238 1.1 takemura
239 1.1 takemura void
240 1.1 takemura clock_init(dev)
241 1.1 takemura struct device *dev;
242 1.1 takemura {
243 1.1 takemura struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
244 1.1 takemura #ifdef RTCDEBUG
245 1.1 takemura int timeh;
246 1.1 takemura int timel;
247 1.1 takemura #endif /* RTCDEBUG */
248 1.1 takemura #ifdef RECALC_CPUSPEED
249 1.1 takemura int maxrecalc = 3;
250 1.1 takemura #endif /* RECALC_CPUSPEED */
251 1.1 takemura
252 1.1 takemura #ifdef RTCDEBUG
253 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_H_REG_W);
254 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_M_REG_W);
255 1.1 takemura timel = (timel << 16)
256 1.1 takemura | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_L_REG_W);
257 1.1 takemura printf("clock_init() Elapse Time %04x%04x\n", timeh, timel);
258 1.1 takemura
259 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_H_REG_W);
260 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_M_REG_W);
261 1.1 takemura timel = (timel << 16)
262 1.1 takemura | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_L_REG_W);
263 1.1 takemura printf("clock_init() Elapse Compare %04x%04x\n", timeh, timel);
264 1.1 takemura
265 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W);
266 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_L_REG_W);
267 1.1 takemura printf("clock_init() LONG1 %04x%04x\n", timeh, timel);
268 1.1 takemura
269 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_CNT_H_REG_W);
270 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_CNT_L_REG_W);
271 1.1 takemura printf("clock_init() LONG1 CNTL %04x%04x\n", timeh, timel);
272 1.1 takemura
273 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_H_REG_W);
274 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_L_REG_W);
275 1.1 takemura printf("clock_init() LONG2 %04x%04x\n", timeh, timel);
276 1.1 takemura
277 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_CNT_H_REG_W);
278 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_CNT_L_REG_W);
279 1.1 takemura printf("clock_init() LONG2 CNTL %04x%04x\n", timeh, timel);
280 1.1 takemura
281 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_H_REG_W);
282 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_L_REG_W);
283 1.1 takemura printf("clock_init() TCLK %04x%04x\n", timeh, timel);
284 1.1 takemura
285 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_CNT_H_REG_W);
286 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_CNT_L_REG_W);
287 1.1 takemura printf("clock_init() TCLK CNTL %04x%04x\n", timeh, timel);
288 1.1 takemura #endif /* RTCDEBUG */
289 1.1 takemura /*
290 1.1 takemura * Set tick (CLOCK_RATE)
291 1.1 takemura */
292 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W, 0);
293 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh,
294 1.1 takemura RTCL1_L_REG_W, RTCL1_L_HZ/CLOCK_RATE);
295 1.1 takemura
296 1.1 takemura #ifdef RECALC_CPUSPEED
297 1.1 takemura /* calcurate cpu speed */
298 1.1 takemura while (maxrecalc-- > 0 && vrrtc_recalc_cpuspeed(dev))
299 1.1 takemura ;
300 1.1 takemura #ifdef RECALC_CPUSPEED_DEBUG
301 1.1 takemura printf("clock_init() cpuspeed = %d\n", cpuspeed);
302 1.1 takemura #endif /* RECALC_CPUSPEED_DEBUG */
303 1.1 takemura #endif /* RECALC_CPUSPEED */
304 1.1 takemura }
305 1.1 takemura
306 1.1 takemura static int m2d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
307 1.1 takemura
308 1.1 takemura void
309 1.1 takemura cvt_timehl_ct(timeh, timel, ct)
310 1.1 takemura u_long timeh; /* 2 sec */
311 1.1 takemura u_long timel; /* 1/32768 sec */
312 1.1 takemura struct clocktime *ct;
313 1.1 takemura {
314 1.1 takemura u_long year, month, date, hour, min, sec, sec2;
315 1.1 takemura
316 1.1 takemura timeh -= EPOCHOFF;
317 1.1 takemura
318 1.2 sato timeh += (rtc_offset*SEC2MIN);
319 1.1 takemura
320 1.1 takemura year = EPOCHYEAR;
321 1.2 sato sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
322 1.1 takemura while (timeh > sec2) {
323 1.1 takemura year++;
324 1.1 takemura timeh -= sec2;
325 1.2 sato sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
326 1.1 takemura }
327 1.1 takemura
328 1.1 takemura #ifdef RTCDEBUG
329 1.1 takemura printf("cvt_timehl_ct: timeh %08lx year %ld yrref %ld\n",
330 1.1 takemura timeh, year, sec2);
331 1.1 takemura #endif /* RTCDEBUG */
332 1.1 takemura
333 1.1 takemura month = 0; /* now month is 0..11 */
334 1.2 sato sec2 = SEC2DAY * m2d[month];
335 1.1 takemura while (timeh > sec2) {
336 1.1 takemura timeh -= sec2;
337 1.1 takemura month++;
338 1.2 sato sec2 = SEC2DAY * m2d[month];
339 1.1 takemura if (month == 1 && LEAPYEAR4(year)) /* feb. and leapyear */
340 1.2 sato sec2 += SEC2DAY;
341 1.1 takemura }
342 1.1 takemura month +=1; /* now month is 1..12 */
343 1.1 takemura
344 1.1 takemura #ifdef RTCDEBUG
345 1.1 takemura printf("cvt_timehl_ct: timeh %08lx month %ld mref %ld\n",
346 1.1 takemura timeh, month, sec2);
347 1.1 takemura #endif /* RTCDEBUG */
348 1.1 takemura
349 1.2 sato sec2 = SEC2DAY;
350 1.1 takemura date = timeh/sec2+1; /* date is 1..31 */
351 1.1 takemura timeh -= (date-1)*sec2;
352 1.1 takemura
353 1.1 takemura #ifdef RTCDEBUG
354 1.1 takemura printf("cvt_timehl_ct: timeh %08lx date %ld dref %ld\n",
355 1.1 takemura timeh, date, sec2);
356 1.1 takemura #endif /* RTCDEBUG */
357 1.1 takemura
358 1.2 sato sec2 = SEC2HOUR;
359 1.1 takemura hour = timeh/sec2;
360 1.1 takemura timeh -= hour*sec2;
361 1.1 takemura
362 1.2 sato sec2 = SEC2MIN;
363 1.1 takemura min = timeh/sec2;
364 1.1 takemura timeh -= min*sec2;
365 1.1 takemura
366 1.1 takemura sec = timeh*2 + timel/ETIME_L_HZ;
367 1.1 takemura
368 1.1 takemura #ifdef RTCDEBUG
369 1.1 takemura printf("cvt_timehl_ct: hour %ld min %ld sec %ld\n", hour, min, sec);
370 1.1 takemura #endif /* RTCDEBUG */
371 1.1 takemura
372 1.1 takemura if (ct) {
373 1.2 sato ct->year = year - YBASE; /* base 1900 */
374 1.1 takemura ct->mon = month;
375 1.1 takemura ct->day = date;
376 1.1 takemura ct->hour = hour;
377 1.1 takemura ct->min = min;
378 1.1 takemura ct->sec = sec;
379 1.1 takemura }
380 1.1 takemura }
381 1.1 takemura
382 1.1 takemura void
383 1.1 takemura clock_get(dev, base, ct)
384 1.1 takemura struct device *dev;
385 1.1 takemura time_t base;
386 1.1 takemura struct clocktime *ct;
387 1.1 takemura {
388 1.1 takemura
389 1.1 takemura struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
390 1.1 takemura u_long timeh; /* elapse time (2*timeh sec) */
391 1.1 takemura u_long timel; /* timel/32768 sec */
392 1.1 takemura
393 1.1 takemura timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_H_REG_W);
394 1.1 takemura timeh = (timeh << 16)
395 1.1 takemura | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_M_REG_W);
396 1.1 takemura timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_L_REG_W);
397 1.1 takemura
398 1.1 takemura #ifdef RTCDEBUG
399 1.1 takemura printf("clock_get: timeh %08lx timel %08lx\n", timeh, timel);
400 1.1 takemura #endif /* RTCDEBUG */
401 1.1 takemura
402 1.1 takemura cvt_timehl_ct(timeh, timel, ct);
403 1.1 takemura
404 1.1 takemura #ifdef RTCDEBUG
405 1.1 takemura printf("clock_get: %d/%d/%d/%d/%d/%d\n",
406 1.1 takemura ct->year, ct->mon, ct->day, ct->hour, ct->min, ct->sec);
407 1.1 takemura #endif /* RTCDEBUG */
408 1.1 takemura }
409 1.1 takemura
410 1.1 takemura
411 1.1 takemura void
412 1.1 takemura clock_set(dev, ct)
413 1.1 takemura struct device *dev;
414 1.1 takemura struct clocktime *ct;
415 1.1 takemura {
416 1.1 takemura struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
417 1.1 takemura u_long timeh; /* elapse time (2*timeh sec) */
418 1.1 takemura u_long timel; /* timel/32768 sec */
419 1.1 takemura int year, month, sec2;
420 1.1 takemura
421 1.1 takemura timeh = 0;
422 1.1 takemura timel = 0;
423 1.1 takemura
424 1.1 takemura #ifdef RTCDEBUG
425 1.1 takemura printf("clock_set: %d/%d/%d/%d/%d/%d\n",
426 1.1 takemura ct->year, ct->mon, ct->day, ct->hour, ct->min, ct->sec);
427 1.1 takemura #endif /* RTCDEBUG */
428 1.2 sato ct->year += YBASE;
429 1.1 takemura #ifdef RTCDEBUG
430 1.1 takemura printf("clock_set: %d/%d/%d/%d/%d/%d\n",
431 1.1 takemura ct->year, ct->mon, ct->day, ct->hour, ct->min, ct->sec);
432 1.1 takemura #endif /* RTCDEBUG */
433 1.1 takemura year = EPOCHYEAR;
434 1.2 sato sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
435 1.1 takemura while (year < ct->year) {
436 1.1 takemura year++;
437 1.1 takemura timeh += sec2;
438 1.2 sato sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
439 1.1 takemura }
440 1.1 takemura month = 1; /* now month is 1..12 */
441 1.2 sato sec2 = SEC2DAY * m2d[month-1];
442 1.1 takemura while (month < ct->mon) {
443 1.1 takemura month++;
444 1.1 takemura timeh += sec2;
445 1.2 sato sec2 = SEC2DAY * m2d[month-1];
446 1.1 takemura if (month == 2 && LEAPYEAR4(year)) /* feb. and leapyear */
447 1.2 sato sec2 += SEC2DAY;
448 1.1 takemura }
449 1.1 takemura
450 1.2 sato timeh += (ct->day - 1)*SEC2DAY;
451 1.1 takemura
452 1.2 sato timeh += ct->hour*SEC2HOUR;
453 1.1 takemura
454 1.2 sato timeh += ct->min*SEC2MIN;
455 1.1 takemura
456 1.1 takemura timeh += ct->sec/2;
457 1.1 takemura timel += (ct->sec%2)*ETIME_L_HZ;
458 1.1 takemura
459 1.1 takemura timeh += EPOCHOFF;
460 1.2 sato timeh -= (rtc_offset*SEC2MIN);
461 1.1 takemura
462 1.1 takemura #ifdef RTCDEBUG
463 1.1 takemura cvt_timehl_ct(timeh, timel, NULL);
464 1.1 takemura #endif /* RTCDEBUG */
465 1.1 takemura
466 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh,
467 1.1 takemura ETIME_H_REG_W, (timeh>>16)&0xffff);
468 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, ETIME_M_REG_W, timeh&0xffff);
469 1.1 takemura bus_space_write_2(sc->sc_iot, sc->sc_ioh, ETIME_L_REG_W, timel);
470 1.1 takemura
471 1.1 takemura }
472 1.1 takemura
473