clock.c revision 1.8 1 1.8 gwr /* $NetBSD: clock.c,v 1.8 1997/02/19 23:38:46 gwr Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1994 Gordon W. Ross
5 1.1 gwr * Copyright (c) 1993 Adam Glass
6 1.1 gwr * Copyright (c) 1988 University of Utah.
7 1.1 gwr * Copyright (c) 1982, 1990, 1993
8 1.1 gwr * The Regents of the University of California. All rights reserved.
9 1.1 gwr *
10 1.1 gwr * This code is derived from software contributed to Berkeley by
11 1.1 gwr * the Systems Programming Group of the University of Utah Computer
12 1.1 gwr * Science Department.
13 1.1 gwr *
14 1.1 gwr * Redistribution and use in source and binary forms, with or without
15 1.1 gwr * modification, are permitted provided that the following conditions
16 1.1 gwr * are met:
17 1.1 gwr * 1. Redistributions of source code must retain the above copyright
18 1.1 gwr * notice, this list of conditions and the following disclaimer.
19 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 gwr * notice, this list of conditions and the following disclaimer in the
21 1.1 gwr * documentation and/or other materials provided with the distribution.
22 1.1 gwr * 3. All advertising materials mentioning features or use of this software
23 1.1 gwr * must display the following acknowledgement:
24 1.1 gwr * This product includes software developed by the University of
25 1.1 gwr * California, Berkeley and its contributors.
26 1.1 gwr * 4. Neither the name of the University nor the names of its contributors
27 1.1 gwr * may be used to endorse or promote products derived from this software
28 1.1 gwr * without specific prior written permission.
29 1.1 gwr *
30 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 1.1 gwr * SUCH DAMAGE.
41 1.1 gwr *
42 1.1 gwr * from: Utah Hdr: clock.c 1.18 91/01/21$
43 1.1 gwr * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
44 1.1 gwr */
45 1.1 gwr
46 1.1 gwr /*
47 1.3 gwr * Machine-dependent clock routines for the Mostek48t02
48 1.1 gwr */
49 1.1 gwr
50 1.1 gwr #include <sys/param.h>
51 1.1 gwr #include <sys/systm.h>
52 1.1 gwr #include <sys/time.h>
53 1.1 gwr #include <sys/kernel.h>
54 1.1 gwr #include <sys/device.h>
55 1.1 gwr
56 1.1 gwr #include <machine/autoconf.h>
57 1.1 gwr #include <machine/cpu.h>
58 1.1 gwr #include <machine/mon.h>
59 1.1 gwr #include <machine/obio.h>
60 1.3 gwr #include <machine/machdep.h>
61 1.1 gwr
62 1.8 gwr #include <dev/clock_subr.h>
63 1.8 gwr
64 1.3 gwr #include <sun3/sun3/interreg.h>
65 1.3 gwr #include "mostek48t02.h"
66 1.1 gwr
67 1.1 gwr #define CLOCK_PRI 5
68 1.1 gwr
69 1.1 gwr void _isr_clock __P((void)); /* in locore.s */
70 1.1 gwr void clock_intr __P((struct clockframe));
71 1.1 gwr
72 1.1 gwr /* Note: this is used by locore.s:__isr_clock */
73 1.3 gwr static volatile void *clock_va;
74 1.1 gwr
75 1.1 gwr static int clock_match __P((struct device *, struct cfdata *, void *args));
76 1.1 gwr static void clock_attach __P((struct device *, struct device *, void *));
77 1.1 gwr
78 1.1 gwr struct cfattach clock_ca = {
79 1.1 gwr sizeof(struct device), clock_match, clock_attach
80 1.1 gwr };
81 1.1 gwr
82 1.1 gwr struct cfdriver clock_cd = {
83 1.1 gwr NULL, "clock", DV_DULL
84 1.1 gwr };
85 1.1 gwr
86 1.1 gwr /*
87 1.7 gwr * XXX Need to determine which type of clock we have!
88 1.7 gwr * XXX The Sun3/80 always has the MK4802, while the
89 1.7 gwr * XXX Sun3/470 can (reportedly) have that or the old
90 1.7 gwr * XXX intersil7170. Should have two clock drivers...
91 1.1 gwr */
92 1.1 gwr static int
93 1.1 gwr clock_match(parent, cf, args)
94 1.1 gwr struct device *parent;
95 1.1 gwr struct cfdata *cf;
96 1.1 gwr void *args;
97 1.1 gwr {
98 1.1 gwr struct confargs *ca = args;
99 1.1 gwr
100 1.1 gwr /* This driver only supports one unit. */
101 1.1 gwr if (cf->cf_unit != 0)
102 1.1 gwr return (0);
103 1.1 gwr
104 1.1 gwr /* Validate the given address. */
105 1.1 gwr if (ca->ca_paddr != OBIO_CLOCK2)
106 1.1 gwr return (0);
107 1.1 gwr
108 1.1 gwr /* Default interrupt priority. */
109 1.1 gwr if (ca->ca_intpri == -1)
110 1.1 gwr ca->ca_intpri = CLOCK_PRI;
111 1.1 gwr
112 1.1 gwr return (1);
113 1.1 gwr }
114 1.1 gwr
115 1.1 gwr static void
116 1.1 gwr clock_attach(parent, self, args)
117 1.1 gwr struct device *parent;
118 1.1 gwr struct device *self;
119 1.1 gwr void *args;
120 1.1 gwr {
121 1.1 gwr
122 1.1 gwr printf("\n");
123 1.1 gwr
124 1.1 gwr /*
125 1.1 gwr * Can not hook up the ISR until cpu_initclocks()
126 1.1 gwr * because hardclock is not ready until then.
127 1.1 gwr * For now, the handler is _isr_autovec(), which
128 1.1 gwr * will complain if it gets clock interrupts.
129 1.1 gwr */
130 1.1 gwr }
131 1.1 gwr
132 1.1 gwr /*
133 1.1 gwr * Set and/or clear the desired clock bits in the interrupt
134 1.1 gwr * register. We have to be extremely careful that we do it
135 1.1 gwr * in such a manner that we don't get ourselves lost.
136 1.1 gwr */
137 1.1 gwr void
138 1.1 gwr set_clk_mode(on, off, enable)
139 1.1 gwr u_char on, off;
140 1.1 gwr int enable;
141 1.1 gwr {
142 1.1 gwr register u_char interreg;
143 1.1 gwr register int s;
144 1.1 gwr
145 1.4 gwr /* If we don't have this, we must not have touched it! */
146 1.5 gwr if (!interrupt_reg)
147 1.4 gwr return;
148 1.4 gwr
149 1.1 gwr s = getsr();
150 1.1 gwr if ((s & PSL_IPL) < PSL_IPL7)
151 1.1 gwr panic("set_clk_mode: ipl");
152 1.1 gwr
153 1.1 gwr /*
154 1.1 gwr * make sure that we are only playing w/
155 1.1 gwr * clock interrupt register bits
156 1.1 gwr */
157 1.1 gwr on &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
158 1.1 gwr off &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
159 1.1 gwr
160 1.1 gwr /*
161 1.1 gwr * Get a copy of current interrupt register,
162 1.1 gwr * turning off any undesired bits (aka `off')
163 1.1 gwr */
164 1.1 gwr interreg = *interrupt_reg & ~(off | IREG_ALL_ENAB);
165 1.1 gwr *interrupt_reg &= ~IREG_ALL_ENAB;
166 1.1 gwr
167 1.1 gwr /*
168 1.1 gwr * Next we turns off the CLK5 and CLK7 bits to clear
169 1.1 gwr * the flip-flops, then we disable clock interrupts.
170 1.1 gwr * Now we can read the clock's interrupt register
171 1.1 gwr * to clear any pending signals there.
172 1.1 gwr */
173 1.1 gwr *interrupt_reg &= ~(IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
174 1.3 gwr
175 1.3 gwr /* XXX - hit the clock? */
176 1.1 gwr
177 1.1 gwr /*
178 1.1 gwr * Now we set all the desired bits
179 1.1 gwr * in the interrupt register, then
180 1.1 gwr * we turn the clock back on and
181 1.1 gwr * finally we can enable all interrupts.
182 1.1 gwr */
183 1.1 gwr *interrupt_reg |= (interreg | on); /* enable flip-flops */
184 1.1 gwr
185 1.3 gwr /* XXX - hit the clock? */
186 1.1 gwr
187 1.1 gwr *interrupt_reg |= IREG_ALL_ENAB; /* enable interrupts */
188 1.1 gwr }
189 1.1 gwr
190 1.1 gwr /* Called very early by internal_configure. */
191 1.1 gwr void clock_init()
192 1.1 gwr {
193 1.3 gwr /* XXX - Yes, use the EEPROM address. Same H/W device. */
194 1.3 gwr clock_va = obio_find_mapping(OBIO_EEPROM, sizeof(struct clockreg));
195 1.1 gwr
196 1.4 gwr if (!clock_va || !interrupt_reg) {
197 1.4 gwr mon_printf("clock_init\n");
198 1.4 gwr sunmon_abort();
199 1.4 gwr }
200 1.1 gwr
201 1.1 gwr /* Turn off clock interrupts until cpu_initclocks() */
202 1.6 gwr /* intreg_init() already cleared the interrupt register. */
203 1.1 gwr }
204 1.1 gwr
205 1.1 gwr /*
206 1.1 gwr * Set up the real-time clock (enable clock interrupts).
207 1.1 gwr * Leave stathz 0 since there is no secondary clock available.
208 1.1 gwr * Note that clock interrupts MUST STAY DISABLED until here.
209 1.1 gwr */
210 1.1 gwr void
211 1.1 gwr cpu_initclocks(void)
212 1.1 gwr {
213 1.1 gwr int s;
214 1.1 gwr
215 1.3 gwr if (!clock_va)
216 1.1 gwr panic("cpu_initclocks");
217 1.1 gwr s = splhigh();
218 1.1 gwr
219 1.1 gwr /* Install isr (in locore.s) that calls clock_intr(). */
220 1.1 gwr isr_add_custom(5, (void*)_isr_clock);
221 1.1 gwr
222 1.1 gwr /* Set the clock to interrupt 100 time per second. */
223 1.3 gwr /* XXX - Hard wired? */
224 1.1 gwr
225 1.1 gwr *interrupt_reg |= IREG_CLOCK_ENAB_5; /* enable clock */
226 1.3 gwr
227 1.3 gwr /* XXX enable the clock? */
228 1.3 gwr
229 1.1 gwr *interrupt_reg |= IREG_ALL_ENAB; /* enable interrupts */
230 1.1 gwr splx(s);
231 1.1 gwr }
232 1.1 gwr
233 1.1 gwr /*
234 1.1 gwr * This doesn't need to do anything, as we have only one timer and
235 1.1 gwr * profhz==stathz==hz.
236 1.1 gwr */
237 1.1 gwr void
238 1.1 gwr setstatclockrate(newhz)
239 1.1 gwr int newhz;
240 1.1 gwr {
241 1.1 gwr /* nothing */
242 1.1 gwr }
243 1.1 gwr
244 1.1 gwr /*
245 1.3 gwr * This is is called by the "custom" interrupt handler.
246 1.1 gwr */
247 1.1 gwr void
248 1.1 gwr clock_intr(cf)
249 1.1 gwr struct clockframe cf;
250 1.1 gwr {
251 1.3 gwr /* volatile struct clockreg *clk = clock_va; */
252 1.1 gwr
253 1.3 gwr #if 1 /* XXX - Needed? */
254 1.1 gwr /* Pulse the clock intr. enable low. */
255 1.1 gwr *interrupt_reg &= ~IREG_CLOCK_ENAB_5;
256 1.1 gwr *interrupt_reg |= IREG_CLOCK_ENAB_5;
257 1.3 gwr #endif
258 1.1 gwr
259 1.1 gwr hardclock(&cf);
260 1.1 gwr }
261 1.1 gwr
262 1.1 gwr /*
263 1.1 gwr * Return the best possible estimate of the time in the timeval
264 1.1 gwr * to which tvp points. We do this by returning the current time
265 1.1 gwr * plus the amount of time since the last clock interrupt.
266 1.1 gwr *
267 1.1 gwr * Check that this time is no less than any previously-reported time,
268 1.1 gwr * which could happen around the time of a clock adjustment. Just for
269 1.1 gwr * fun, we guarantee that the time will be greater than the value
270 1.1 gwr * obtained by a previous call.
271 1.1 gwr */
272 1.1 gwr void
273 1.1 gwr microtime(tvp)
274 1.1 gwr register struct timeval *tvp;
275 1.1 gwr {
276 1.1 gwr int s = splhigh();
277 1.1 gwr static struct timeval lasttime;
278 1.1 gwr
279 1.1 gwr *tvp = time;
280 1.1 gwr tvp->tv_usec++; /* XXX */
281 1.1 gwr while (tvp->tv_usec > 1000000) {
282 1.1 gwr tvp->tv_sec++;
283 1.1 gwr tvp->tv_usec -= 1000000;
284 1.1 gwr }
285 1.1 gwr if (tvp->tv_sec == lasttime.tv_sec &&
286 1.1 gwr tvp->tv_usec <= lasttime.tv_usec &&
287 1.1 gwr (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000)
288 1.1 gwr {
289 1.1 gwr tvp->tv_sec++;
290 1.1 gwr tvp->tv_usec -= 1000000;
291 1.1 gwr }
292 1.1 gwr lasttime = *tvp;
293 1.1 gwr splx(s);
294 1.1 gwr }
295 1.1 gwr
296 1.1 gwr
297 1.1 gwr /*
298 1.1 gwr * Machine-dependent clock routines.
299 1.1 gwr *
300 1.1 gwr * Inittodr initializes the time of day hardware which provides
301 1.1 gwr * date functions.
302 1.1 gwr *
303 1.1 gwr * Resettodr restores the time of day hardware after a time change.
304 1.1 gwr */
305 1.1 gwr
306 1.1 gwr static long clk_get_secs(void);
307 1.1 gwr static void clk_set_secs(long);
308 1.1 gwr
309 1.1 gwr /*
310 1.1 gwr * Initialize the time of day register, based on the time base
311 1.1 gwr * which is, e.g. from a filesystem.
312 1.1 gwr */
313 1.1 gwr void inittodr(fs_time)
314 1.1 gwr time_t fs_time;
315 1.1 gwr {
316 1.1 gwr long diff, clk_time;
317 1.1 gwr long long_ago = (5 * SECYR);
318 1.1 gwr int clk_bad = 0;
319 1.1 gwr
320 1.1 gwr /*
321 1.1 gwr * Sanity check time from file system.
322 1.1 gwr * If it is zero,assume filesystem time is just unknown
323 1.1 gwr * instead of preposterous. Don't bark.
324 1.1 gwr */
325 1.1 gwr if (fs_time < long_ago) {
326 1.1 gwr /*
327 1.1 gwr * If fs_time is zero, assume filesystem time is just
328 1.1 gwr * unknown instead of preposterous. Don't bark.
329 1.1 gwr */
330 1.1 gwr if (fs_time != 0)
331 1.1 gwr printf("WARNING: preposterous time in file system\n");
332 1.1 gwr /* 1991/07/01 12:00:00 */
333 1.1 gwr fs_time = 21*SECYR + 186*SECDAY + SECDAY/2;
334 1.1 gwr }
335 1.1 gwr
336 1.1 gwr clk_time = clk_get_secs();
337 1.1 gwr
338 1.1 gwr /* Sanity check time from clock. */
339 1.1 gwr if (clk_time < long_ago) {
340 1.1 gwr printf("WARNING: bad date in battery clock");
341 1.1 gwr clk_bad = 1;
342 1.1 gwr clk_time = fs_time;
343 1.1 gwr } else {
344 1.1 gwr /* Does the clock time jive with the file system? */
345 1.1 gwr diff = clk_time - fs_time;
346 1.1 gwr if (diff < 0)
347 1.1 gwr diff = -diff;
348 1.1 gwr if (diff >= (SECDAY*2)) {
349 1.1 gwr printf("WARNING: clock %s %d days",
350 1.1 gwr (clk_time < fs_time) ? "lost" : "gained",
351 1.1 gwr (int) (diff / SECDAY));
352 1.1 gwr clk_bad = 1;
353 1.1 gwr }
354 1.1 gwr }
355 1.1 gwr if (clk_bad)
356 1.1 gwr printf(" -- CHECK AND RESET THE DATE!\n");
357 1.1 gwr time.tv_sec = clk_time;
358 1.1 gwr }
359 1.1 gwr
360 1.1 gwr /*
361 1.1 gwr * Resettodr restores the time of day hardware after a time change.
362 1.1 gwr */
363 1.1 gwr void resettodr()
364 1.1 gwr {
365 1.1 gwr clk_set_secs(time.tv_sec);
366 1.1 gwr }
367 1.1 gwr
368 1.1 gwr
369 1.1 gwr /*
370 1.3 gwr * Routines to copy state into and out of the clock.
371 1.3 gwr * The clock CSR has to be set for read or write.
372 1.1 gwr */
373 1.3 gwr static void
374 1.8 gwr clk_get_dt(struct clock_ymdhms *dt)
375 1.1 gwr {
376 1.3 gwr volatile struct clockreg *cl = clock_va;
377 1.1 gwr int s;
378 1.1 gwr
379 1.1 gwr s = splhigh();
380 1.7 gwr
381 1.3 gwr /* enable read (stop time) */
382 1.3 gwr cl->cl_csr |= CLK_READ;
383 1.1 gwr
384 1.3 gwr /* Copy the info */
385 1.3 gwr dt->dt_sec = cl->cl_sec;
386 1.3 gwr dt->dt_min = cl->cl_min;
387 1.3 gwr dt->dt_hour = cl->cl_hour;
388 1.3 gwr dt->dt_wday = cl->cl_wday;
389 1.3 gwr dt->dt_day = cl->cl_mday;
390 1.3 gwr dt->dt_mon = cl->cl_month;
391 1.3 gwr dt->dt_year = cl->cl_year;
392 1.1 gwr
393 1.3 gwr /* Done reading (time wears on) */
394 1.3 gwr cl->cl_csr &= ~CLK_READ;
395 1.1 gwr splx(s);
396 1.1 gwr }
397 1.1 gwr
398 1.3 gwr static void
399 1.8 gwr clk_set_dt(struct clock_ymdhms *dt)
400 1.1 gwr {
401 1.3 gwr volatile struct clockreg *cl = clock_va;
402 1.1 gwr int s;
403 1.1 gwr
404 1.1 gwr s = splhigh();
405 1.3 gwr /* enable write */
406 1.3 gwr cl->cl_csr |= CLK_WRITE;
407 1.1 gwr
408 1.3 gwr /* Copy the info */
409 1.3 gwr cl->cl_sec = dt->dt_sec;
410 1.3 gwr cl->cl_min = dt->dt_min;
411 1.3 gwr cl->cl_hour = dt->dt_hour;
412 1.3 gwr cl->cl_wday = dt->dt_wday;
413 1.3 gwr cl->cl_mday = dt->dt_day;
414 1.3 gwr cl->cl_month = dt->dt_mon;
415 1.3 gwr cl->cl_year = dt->dt_year;
416 1.1 gwr
417 1.3 gwr /* load them up */
418 1.3 gwr cl->cl_csr &= ~CLK_WRITE;
419 1.1 gwr splx(s);
420 1.1 gwr }
421 1.1 gwr
422 1.1 gwr
423 1.3 gwr /*
424 1.8 gwr * Now routines to get and set clock as POSIX time.
425 1.8 gwr * Our clock keeps "years since 1/1/1968".
426 1.7 gwr */
427 1.8 gwr #define CLOCK_BASE_YEAR 1968
428 1.7 gwr
429 1.3 gwr static long
430 1.3 gwr clk_get_secs()
431 1.3 gwr {
432 1.8 gwr struct clock_ymdhms dt;
433 1.8 gwr long secs;
434 1.3 gwr
435 1.3 gwr clk_get_dt(&dt);
436 1.7 gwr
437 1.7 gwr /* Convert BCD values to binary. */
438 1.7 gwr dt.dt_sec = FROMBCD(dt.dt_sec);
439 1.7 gwr dt.dt_min = FROMBCD(dt.dt_min);
440 1.7 gwr dt.dt_hour = FROMBCD(dt.dt_hour);
441 1.7 gwr dt.dt_day = FROMBCD(dt.dt_day);
442 1.7 gwr dt.dt_mon = FROMBCD(dt.dt_mon);
443 1.7 gwr dt.dt_year = FROMBCD(dt.dt_year);
444 1.7 gwr
445 1.8 gwr if ((dt.dt_hour > 24) ||
446 1.8 gwr (dt.dt_day > 31) ||
447 1.8 gwr (dt.dt_mon > 12))
448 1.8 gwr return (0);
449 1.8 gwr
450 1.8 gwr dt.dt_year += CLOCK_BASE_YEAR;
451 1.8 gwr secs = clock_ymdhms_to_secs(&dt);
452 1.8 gwr return (secs);
453 1.3 gwr }
454 1.7 gwr
455 1.3 gwr static void
456 1.3 gwr clk_set_secs(secs)
457 1.3 gwr long secs;
458 1.3 gwr {
459 1.8 gwr struct clock_ymdhms dt;
460 1.3 gwr
461 1.8 gwr clock_secs_to_ymdhms(secs, &dt);
462 1.8 gwr dt.dt_year -= CLOCK_BASE_YEAR;
463 1.7 gwr
464 1.7 gwr /* Convert binary values to BCD. */
465 1.7 gwr dt.dt_sec = TOBCD(dt.dt_sec);
466 1.7 gwr dt.dt_min = TOBCD(dt.dt_min);
467 1.7 gwr dt.dt_hour = TOBCD(dt.dt_hour);
468 1.7 gwr dt.dt_day = TOBCD(dt.dt_day);
469 1.7 gwr dt.dt_mon = TOBCD(dt.dt_mon);
470 1.7 gwr dt.dt_year = TOBCD(dt.dt_year);
471 1.7 gwr
472 1.3 gwr clk_set_dt(&dt);
473 1.1 gwr }
474