clock.c revision 1.31 1 1.31 gdamore /* $NetBSD: clock.c,v 1.31 2006/09/05 06:45:05 gdamore Exp $ */
2 1.25 agc
3 1.25 agc /*
4 1.25 agc * Copyright (c) 1982, 1990, 1993
5 1.25 agc * The Regents of the University of California. All rights reserved.
6 1.25 agc *
7 1.25 agc * This code is derived from software contributed to Berkeley by
8 1.25 agc * the Systems Programming Group of the University of Utah Computer
9 1.25 agc * Science Department.
10 1.25 agc *
11 1.25 agc * Redistribution and use in source and binary forms, with or without
12 1.25 agc * modification, are permitted provided that the following conditions
13 1.25 agc * are met:
14 1.25 agc * 1. Redistributions of source code must retain the above copyright
15 1.25 agc * notice, this list of conditions and the following disclaimer.
16 1.25 agc * 2. Redistributions in binary form must reproduce the above copyright
17 1.25 agc * notice, this list of conditions and the following disclaimer in the
18 1.25 agc * documentation and/or other materials provided with the distribution.
19 1.25 agc * 3. Neither the name of the University nor the names of its contributors
20 1.25 agc * may be used to endorse or promote products derived from this software
21 1.25 agc * without specific prior written permission.
22 1.25 agc *
23 1.25 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.25 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.25 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.25 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.25 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.25 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.25 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.25 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.25 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.25 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.25 agc * SUCH DAMAGE.
34 1.25 agc *
35 1.25 agc * from: Utah Hdr: clock.c 1.18 91/01/21$
36 1.25 agc * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
37 1.25 agc */
38 1.1 gwr
39 1.1 gwr /*
40 1.1 gwr * Copyright (c) 1994 Gordon W. Ross
41 1.1 gwr * Copyright (c) 1993 Adam Glass
42 1.1 gwr * Copyright (c) 1988 University of Utah.
43 1.1 gwr *
44 1.1 gwr * This code is derived from software contributed to Berkeley by
45 1.1 gwr * the Systems Programming Group of the University of Utah Computer
46 1.1 gwr * Science Department.
47 1.1 gwr *
48 1.1 gwr * Redistribution and use in source and binary forms, with or without
49 1.1 gwr * modification, are permitted provided that the following conditions
50 1.1 gwr * are met:
51 1.1 gwr * 1. Redistributions of source code must retain the above copyright
52 1.1 gwr * notice, this list of conditions and the following disclaimer.
53 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
54 1.1 gwr * notice, this list of conditions and the following disclaimer in the
55 1.1 gwr * documentation and/or other materials provided with the distribution.
56 1.1 gwr * 3. All advertising materials mentioning features or use of this software
57 1.1 gwr * must display the following acknowledgement:
58 1.1 gwr * This product includes software developed by the University of
59 1.1 gwr * California, Berkeley and its contributors.
60 1.1 gwr * 4. Neither the name of the University nor the names of its contributors
61 1.1 gwr * may be used to endorse or promote products derived from this software
62 1.1 gwr * without specific prior written permission.
63 1.1 gwr *
64 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 1.1 gwr * SUCH DAMAGE.
75 1.1 gwr *
76 1.1 gwr * from: Utah Hdr: clock.c 1.18 91/01/21$
77 1.1 gwr * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
78 1.1 gwr */
79 1.1 gwr
80 1.1 gwr /*
81 1.10 gwr * Machine-dependent clock routines. Sun3X machines may have
82 1.10 gwr * either the Mostek 48T02 or the Intersil 7170 clock.
83 1.10 gwr *
84 1.10 gwr * It is tricky to determine which you have, because there is
85 1.10 gwr * always something responding at the address where the Mostek
86 1.10 gwr * clock might be found: either a Mostek or plain-old EEPROM.
87 1.10 gwr * Therefore, we cheat. If we find an Intersil clock, assume
88 1.10 gwr * that what responds at the end of the EEPROM space is just
89 1.10 gwr * plain-old EEPROM (not a Mostek clock). Worse, there are
90 1.10 gwr * H/W problems with probing for an Intersil on the 3/80, so
91 1.10 gwr * on that machine we "know" there is a Mostek clock.
92 1.10 gwr *
93 1.10 gwr * Note that the probing algorithm described above requires
94 1.10 gwr * that we probe the intersil before we probe the mostek!
95 1.1 gwr */
96 1.24 lukem
97 1.24 lukem #include <sys/cdefs.h>
98 1.31 gdamore __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.31 2006/09/05 06:45:05 gdamore Exp $");
99 1.1 gwr
100 1.1 gwr #include <sys/param.h>
101 1.1 gwr #include <sys/systm.h>
102 1.1 gwr #include <sys/time.h>
103 1.1 gwr #include <sys/kernel.h>
104 1.1 gwr #include <sys/device.h>
105 1.1 gwr
106 1.27 tsutsui #include <uvm/uvm_extern.h>
107 1.27 tsutsui
108 1.9 gwr #include <m68k/asm_single.h>
109 1.9 gwr
110 1.1 gwr #include <machine/autoconf.h>
111 1.1 gwr #include <machine/cpu.h>
112 1.10 gwr #include <machine/idprom.h>
113 1.10 gwr #include <machine/leds.h>
114 1.10 gwr
115 1.15 gwr #include <dev/clock_subr.h>
116 1.15 gwr #include <dev/ic/intersil7170.h>
117 1.15 gwr
118 1.15 gwr #include <sun3/sun3/machdep.h>
119 1.10 gwr #include <sun3/sun3/interreg.h>
120 1.1 gwr
121 1.15 gwr #include <sun3/sun3x/mk48t02.h>
122 1.8 gwr
123 1.27 tsutsui extern int intrcnt[];
124 1.27 tsutsui
125 1.10 gwr #define SUN3_470 Yes
126 1.1 gwr
127 1.1 gwr #define CLOCK_PRI 5
128 1.9 gwr #define IREG_CLK_BITS (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5)
129 1.1 gwr
130 1.10 gwr /*
131 1.10 gwr * Only one of these two variables should be non-zero after
132 1.10 gwr * autoconfiguration determines which clock we have.
133 1.10 gwr */
134 1.10 gwr static volatile void *intersil_va;
135 1.10 gwr static volatile void *mostek_clk_va;
136 1.10 gwr
137 1.28 chs void _isr_clock(void); /* in locore.s */
138 1.28 chs void clock_intr(struct clockframe);
139 1.1 gwr
140 1.1 gwr
141 1.28 chs static int clock_match(struct device *, struct cfdata *, void *);
142 1.28 chs static void clock_attach(struct device *, struct device *, void *);
143 1.1 gwr
144 1.22 thorpej CFATTACH_DECL(clock, sizeof(struct device),
145 1.23 thorpej clock_match, clock_attach, NULL, NULL);
146 1.1 gwr
147 1.10 gwr #ifdef SUN3_470
148 1.10 gwr
149 1.10 gwr #define intersil_clock ((volatile struct intersil7170 *) intersil_va)
150 1.10 gwr
151 1.10 gwr #define intersil_command(run, interrupt) \
152 1.10 gwr (run | interrupt | INTERSIL_CMD_FREQ_32K | INTERSIL_CMD_24HR_MODE | \
153 1.10 gwr INTERSIL_CMD_NORMAL_MODE)
154 1.10 gwr
155 1.10 gwr #define intersil_clear() (void)intersil_clock->clk_intr_reg
156 1.10 gwr
157 1.28 chs static int oclock_match(struct device *, struct cfdata *, void *);
158 1.28 chs static void oclock_attach(struct device *, struct device *, void *);
159 1.10 gwr
160 1.22 thorpej CFATTACH_DECL(oclock, sizeof(struct device),
161 1.23 thorpej oclock_match, oclock_attach, NULL, NULL);
162 1.10 gwr
163 1.31 gdamore static int clk_get_secs(todr_chip_handle_t, volatile struct timeval *);
164 1.31 gdamore static int clk_set_secs(todr_chip_handle_t, volatile struct timeval *);
165 1.31 gdamore static struct todr_chip_handle clk_hdl = {
166 1.31 gdamore .todr_gettime = clk_get_secs,
167 1.31 gdamore .todr_settime = clk_set_secs,
168 1.31 gdamore };
169 1.31 gdamore
170 1.9 gwr /*
171 1.10 gwr * Is there an intersil clock?
172 1.9 gwr */
173 1.28 chs static int
174 1.28 chs oclock_match(struct device *parent, struct cfdata *cf, void *args)
175 1.10 gwr {
176 1.10 gwr struct confargs *ca = args;
177 1.10 gwr
178 1.10 gwr /* This driver only supports one unit. */
179 1.18 tsutsui if (intersil_va)
180 1.10 gwr return (0);
181 1.10 gwr
182 1.10 gwr /*
183 1.10 gwr * The 3/80 can not probe the Intersil absent,
184 1.10 gwr * but it never has one, so "just say no."
185 1.10 gwr */
186 1.29 thorpej if (cpu_machine_id == ID_SUN3X_80)
187 1.10 gwr return (0);
188 1.10 gwr
189 1.10 gwr /* OK, really probe for the Intersil. */
190 1.10 gwr if (bus_peek(ca->ca_bustype, ca->ca_paddr, 1) == -1)
191 1.10 gwr return (0);
192 1.10 gwr
193 1.16 gwr /* Default interrupt priority. */
194 1.16 gwr if (ca->ca_intpri == -1)
195 1.16 gwr ca->ca_intpri = CLOCK_PRI;
196 1.16 gwr
197 1.10 gwr return (1);
198 1.10 gwr }
199 1.10 gwr
200 1.10 gwr /*
201 1.10 gwr * Attach the intersil clock.
202 1.10 gwr */
203 1.28 chs static void
204 1.28 chs oclock_attach(struct device *parent, struct device *self, void *args)
205 1.9 gwr {
206 1.10 gwr struct confargs *ca = args;
207 1.10 gwr caddr_t va;
208 1.10 gwr
209 1.10 gwr printf("\n");
210 1.10 gwr
211 1.10 gwr /* Get a mapping for it. */
212 1.15 gwr va = bus_mapin(ca->ca_bustype,
213 1.15 gwr ca->ca_paddr, sizeof(struct intersil7170));
214 1.10 gwr if (!va)
215 1.10 gwr panic("oclock_attach");
216 1.10 gwr intersil_va = va;
217 1.10 gwr
218 1.10 gwr #ifdef DIAGNOSTIC
219 1.10 gwr /* Verify correct probe order... */
220 1.10 gwr if (mostek_clk_va) {
221 1.10 gwr mostek_clk_va = 0;
222 1.26 tsutsui printf("%s: warning - mostek found also!\n", self->dv_xname);
223 1.9 gwr }
224 1.10 gwr #endif
225 1.10 gwr
226 1.10 gwr /*
227 1.10 gwr * Set the clock to the correct interrupt rate, but
228 1.10 gwr * do not enable the interrupt until cpu_initclocks.
229 1.10 gwr * XXX: Actually, the interrupt_reg should be zero
230 1.10 gwr * at this point, so the clock interrupts should not
231 1.10 gwr * affect us, but we need to set the rate...
232 1.10 gwr */
233 1.10 gwr intersil_clock->clk_cmd_reg =
234 1.26 tsutsui intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IDISABLE);
235 1.10 gwr intersil_clear();
236 1.10 gwr
237 1.10 gwr /* Set the clock to 100 Hz, but do not enable it yet. */
238 1.10 gwr intersil_clock->clk_intr_reg = INTERSIL_INTER_CSECONDS;
239 1.10 gwr
240 1.10 gwr /*
241 1.10 gwr * Can not hook up the ISR until cpu_initclocks()
242 1.10 gwr * because hardclock is not ready until then.
243 1.10 gwr * For now, the handler is _isr_autovec(), which
244 1.10 gwr * will complain if it gets clock interrupts.
245 1.10 gwr */
246 1.31 gdamore
247 1.31 gdamore todr_attach(&clk_hdl);
248 1.9 gwr }
249 1.10 gwr #endif /* SUN3_470 */
250 1.10 gwr
251 1.9 gwr
252 1.1 gwr /*
253 1.10 gwr * Is there a Mostek clock? Hard to tell...
254 1.10 gwr * (See comment at top of this file.)
255 1.1 gwr */
256 1.28 chs static int
257 1.28 chs clock_match(struct device *parent, struct cfdata *cf, void *args)
258 1.1 gwr {
259 1.16 gwr struct confargs *ca = args;
260 1.1 gwr
261 1.1 gwr /* This driver only supports one unit. */
262 1.18 tsutsui if (mostek_clk_va)
263 1.11 gwr return (0);
264 1.11 gwr
265 1.10 gwr /* If intersil was found, use that. */
266 1.10 gwr if (intersil_va)
267 1.1 gwr return (0);
268 1.16 gwr /* Else assume a Mostek is there... */
269 1.16 gwr
270 1.16 gwr /* Default interrupt priority. */
271 1.16 gwr if (ca->ca_intpri == -1)
272 1.16 gwr ca->ca_intpri = CLOCK_PRI;
273 1.1 gwr
274 1.1 gwr return (1);
275 1.1 gwr }
276 1.1 gwr
277 1.10 gwr /*
278 1.10 gwr * Attach the mostek clock.
279 1.10 gwr */
280 1.28 chs static void
281 1.28 chs clock_attach(struct device *parent, struct device *self, void *args)
282 1.1 gwr {
283 1.10 gwr struct confargs *ca = args;
284 1.10 gwr caddr_t va;
285 1.1 gwr
286 1.1 gwr printf("\n");
287 1.1 gwr
288 1.10 gwr /* Get a mapping for it. */
289 1.15 gwr va = bus_mapin(ca->ca_bustype,
290 1.15 gwr ca->ca_paddr, sizeof(struct mostek_clkreg));
291 1.10 gwr if (!va)
292 1.10 gwr panic("clock_attach");
293 1.10 gwr mostek_clk_va = va;
294 1.13 gwr
295 1.1 gwr /*
296 1.1 gwr * Can not hook up the ISR until cpu_initclocks()
297 1.1 gwr * because hardclock is not ready until then.
298 1.1 gwr * For now, the handler is _isr_autovec(), which
299 1.1 gwr * will complain if it gets clock interrupts.
300 1.1 gwr */
301 1.31 gdamore
302 1.31 gdamore todr_attach(&clk_hdl);
303 1.1 gwr }
304 1.1 gwr
305 1.1 gwr /*
306 1.1 gwr * Set and/or clear the desired clock bits in the interrupt
307 1.1 gwr * register. We have to be extremely careful that we do it
308 1.1 gwr * in such a manner that we don't get ourselves lost.
309 1.9 gwr * XXX: Watch out! It's really easy to break this!
310 1.1 gwr */
311 1.1 gwr void
312 1.28 chs set_clk_mode(u_char on, u_char off, int enable_clk)
313 1.1 gwr {
314 1.19 tsutsui u_char interreg;
315 1.1 gwr
316 1.9 gwr /*
317 1.9 gwr * If we have not yet mapped the register,
318 1.9 gwr * then we do not want to do any of this...
319 1.9 gwr */
320 1.5 gwr if (!interrupt_reg)
321 1.4 gwr return;
322 1.4 gwr
323 1.9 gwr #ifdef DIAGNOSTIC
324 1.9 gwr /* Assertion: were are at splhigh! */
325 1.9 gwr if ((getsr() & PSL_IPL) < PSL_IPL7)
326 1.9 gwr panic("set_clk_mode: bad ipl");
327 1.9 gwr #endif
328 1.1 gwr
329 1.1 gwr /*
330 1.1 gwr * make sure that we are only playing w/
331 1.1 gwr * clock interrupt register bits
332 1.1 gwr */
333 1.9 gwr on &= IREG_CLK_BITS;
334 1.9 gwr off &= IREG_CLK_BITS;
335 1.1 gwr
336 1.9 gwr /* First, turn off the "master" enable bit. */
337 1.9 gwr single_inst_bclr_b(*interrupt_reg, IREG_ALL_ENAB);
338 1.1 gwr
339 1.1 gwr /*
340 1.9 gwr * Save the current interrupt register clock bits,
341 1.9 gwr * and turn off/on the requested bits in the copy.
342 1.1 gwr */
343 1.9 gwr interreg = *interrupt_reg & IREG_CLK_BITS;
344 1.9 gwr interreg &= ~off;
345 1.9 gwr interreg |= on;
346 1.9 gwr
347 1.9 gwr /* Clear the CLK5 and CLK7 bits to clear the flip-flops. */
348 1.9 gwr single_inst_bclr_b(*interrupt_reg, IREG_CLK_BITS);
349 1.9 gwr
350 1.9 gwr #ifdef SUN3_470
351 1.9 gwr if (intersil_va) {
352 1.9 gwr /*
353 1.9 gwr * Then disable clock interrupts, and read the clock's
354 1.9 gwr * interrupt register to clear any pending signals there.
355 1.9 gwr */
356 1.9 gwr intersil_clock->clk_cmd_reg =
357 1.26 tsutsui intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IDISABLE);
358 1.9 gwr intersil_clear();
359 1.9 gwr }
360 1.9 gwr #endif /* SUN3_470 */
361 1.3 gwr
362 1.9 gwr /* Set the requested bits in the interrupt register. */
363 1.9 gwr single_inst_bset_b(*interrupt_reg, interreg);
364 1.1 gwr
365 1.9 gwr #ifdef SUN3_470
366 1.9 gwr /* Turn the clock back on (maybe) */
367 1.9 gwr if (intersil_va && enable_clk)
368 1.9 gwr intersil_clock->clk_cmd_reg =
369 1.26 tsutsui intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
370 1.9 gwr #endif /* SUN3_470 */
371 1.1 gwr
372 1.9 gwr /* Finally, turn the "master" enable back on. */
373 1.9 gwr single_inst_bset_b(*interrupt_reg, IREG_ALL_ENAB);
374 1.1 gwr }
375 1.1 gwr
376 1.1 gwr /*
377 1.1 gwr * Set up the real-time clock (enable clock interrupts).
378 1.1 gwr * Leave stathz 0 since there is no secondary clock available.
379 1.1 gwr * Note that clock interrupts MUST STAY DISABLED until here.
380 1.1 gwr */
381 1.1 gwr void
382 1.1 gwr cpu_initclocks(void)
383 1.1 gwr {
384 1.1 gwr int s;
385 1.1 gwr
386 1.1 gwr s = splhigh();
387 1.1 gwr
388 1.1 gwr /* Install isr (in locore.s) that calls clock_intr(). */
389 1.26 tsutsui isr_add_custom(CLOCK_PRI, (void *)_isr_clock);
390 1.1 gwr
391 1.9 gwr /* Now enable the clock at level 5 in the interrupt reg. */
392 1.9 gwr set_clk_mode(IREG_CLOCK_ENAB_5, 0, 1);
393 1.3 gwr
394 1.1 gwr splx(s);
395 1.1 gwr }
396 1.1 gwr
397 1.1 gwr /*
398 1.1 gwr * This doesn't need to do anything, as we have only one timer and
399 1.1 gwr * profhz==stathz==hz.
400 1.1 gwr */
401 1.28 chs void
402 1.28 chs setstatclockrate(int newhz)
403 1.1 gwr {
404 1.26 tsutsui
405 1.1 gwr /* nothing */
406 1.1 gwr }
407 1.1 gwr
408 1.1 gwr /*
409 1.10 gwr * Clock interrupt handler (for both Intersil and Mostek).
410 1.10 gwr * XXX - Is it worth the trouble to save a few cycles here
411 1.10 gwr * by making two separate interrupt handlers?
412 1.10 gwr *
413 1.3 gwr * This is is called by the "custom" interrupt handler.
414 1.9 gwr * Note that we can get ZS interrupts while this runs,
415 1.9 gwr * and zshard may touch the interrupt_reg, so we must
416 1.9 gwr * be careful to use the single_inst_* macros to modify
417 1.9 gwr * the interrupt register atomically.
418 1.1 gwr */
419 1.1 gwr void
420 1.28 chs clock_intr(struct clockframe cf)
421 1.1 gwr {
422 1.10 gwr extern char _Idle[]; /* locore.s */
423 1.10 gwr
424 1.10 gwr #ifdef SUN3_470
425 1.10 gwr if (intersil_va) {
426 1.10 gwr /* Read the clock interrupt register. */
427 1.10 gwr intersil_clear();
428 1.10 gwr }
429 1.10 gwr #endif /* SUN3_470 */
430 1.1 gwr
431 1.1 gwr /* Pulse the clock intr. enable low. */
432 1.9 gwr single_inst_bclr_b(*interrupt_reg, IREG_CLOCK_ENAB_5);
433 1.9 gwr single_inst_bset_b(*interrupt_reg, IREG_CLOCK_ENAB_5);
434 1.1 gwr
435 1.10 gwr #ifdef SUN3_470
436 1.10 gwr if (intersil_va) {
437 1.10 gwr /* Read the clock intr. reg. AGAIN! */
438 1.10 gwr intersil_clear();
439 1.10 gwr }
440 1.10 gwr #endif /* SUN3_470 */
441 1.27 tsutsui
442 1.27 tsutsui intrcnt[CLOCK_PRI]++;
443 1.27 tsutsui uvmexp.intrs++;
444 1.13 gwr
445 1.13 gwr /* Entertainment! */
446 1.13 gwr if (cf.cf_pc == (long)_Idle)
447 1.13 gwr leds_intr();
448 1.10 gwr
449 1.9 gwr /* Call common clock interrupt handler. */
450 1.1 gwr hardclock(&cf);
451 1.1 gwr }
452 1.9 gwr
453 1.1 gwr /*
454 1.1 gwr * Machine-dependent clock routines.
455 1.1 gwr *
456 1.1 gwr * Inittodr initializes the time of day hardware which provides
457 1.1 gwr * date functions.
458 1.1 gwr *
459 1.1 gwr * Resettodr restores the time of day hardware after a time change.
460 1.1 gwr */
461 1.1 gwr
462 1.1 gwr
463 1.1 gwr /*
464 1.10 gwr * Now routines to get and set clock as POSIX time.
465 1.10 gwr * Our clock keeps "years since 1/1/1968".
466 1.10 gwr */
467 1.10 gwr #define CLOCK_BASE_YEAR 1968
468 1.10 gwr #ifdef SUN3_470
469 1.28 chs static void intersil_get_dt(struct clock_ymdhms *);
470 1.28 chs static void intersil_set_dt(struct clock_ymdhms *);
471 1.10 gwr #endif /* SUN3_470 */
472 1.28 chs static void mostek_get_dt(struct clock_ymdhms *);
473 1.28 chs static void mostek_set_dt(struct clock_ymdhms *);
474 1.10 gwr
475 1.31 gdamore static int
476 1.31 gdamore clk_get_secs(todr_chip_handle_t tch, volatile struct timeval *tvp)
477 1.10 gwr {
478 1.10 gwr struct clock_ymdhms dt;
479 1.10 gwr
480 1.20 tsutsui memset(&dt, 0, sizeof(dt));
481 1.10 gwr
482 1.10 gwr #ifdef SUN3_470
483 1.10 gwr if (intersil_va)
484 1.10 gwr intersil_get_dt(&dt);
485 1.10 gwr #endif /* SUN3_470 */
486 1.10 gwr if (mostek_clk_va) {
487 1.10 gwr /* Read the Mostek. */
488 1.10 gwr mostek_get_dt(&dt);
489 1.10 gwr /* Convert BCD values to binary. */
490 1.10 gwr dt.dt_sec = FROMBCD(dt.dt_sec);
491 1.10 gwr dt.dt_min = FROMBCD(dt.dt_min);
492 1.10 gwr dt.dt_hour = FROMBCD(dt.dt_hour);
493 1.10 gwr dt.dt_day = FROMBCD(dt.dt_day);
494 1.10 gwr dt.dt_mon = FROMBCD(dt.dt_mon);
495 1.10 gwr dt.dt_year = FROMBCD(dt.dt_year);
496 1.10 gwr }
497 1.10 gwr
498 1.10 gwr if ((dt.dt_hour > 24) ||
499 1.26 tsutsui (dt.dt_day > 31) ||
500 1.26 tsutsui (dt.dt_mon > 12))
501 1.31 gdamore return (-1);
502 1.10 gwr
503 1.10 gwr dt.dt_year += CLOCK_BASE_YEAR;
504 1.31 gdamore tvp->tv_sec = clock_ymdhms_to_secs(&dt);
505 1.31 gdamore return 0;
506 1.10 gwr }
507 1.10 gwr
508 1.31 gdamore static int
509 1.31 gdamore clk_set_secs(todr_chip_handle_t tch, volatile struct timeval *tvp)
510 1.10 gwr {
511 1.10 gwr struct clock_ymdhms dt;
512 1.10 gwr
513 1.31 gdamore clock_secs_to_ymdhms(tvp->tv_sec, &dt);
514 1.10 gwr dt.dt_year -= CLOCK_BASE_YEAR;
515 1.10 gwr
516 1.10 gwr #ifdef SUN3_470
517 1.10 gwr if (intersil_va)
518 1.10 gwr intersil_set_dt(&dt);
519 1.10 gwr #endif /* SUN3_470 */
520 1.10 gwr
521 1.10 gwr if (mostek_clk_va) {
522 1.10 gwr /* Convert binary values to BCD. */
523 1.10 gwr dt.dt_sec = TOBCD(dt.dt_sec);
524 1.10 gwr dt.dt_min = TOBCD(dt.dt_min);
525 1.10 gwr dt.dt_hour = TOBCD(dt.dt_hour);
526 1.10 gwr dt.dt_day = TOBCD(dt.dt_day);
527 1.10 gwr dt.dt_mon = TOBCD(dt.dt_mon);
528 1.10 gwr dt.dt_year = TOBCD(dt.dt_year);
529 1.10 gwr /* Write the Mostek. */
530 1.10 gwr mostek_set_dt(&dt);
531 1.10 gwr }
532 1.31 gdamore return 0;
533 1.10 gwr }
534 1.10 gwr
535 1.10 gwr #ifdef SUN3_470
536 1.10 gwr
537 1.10 gwr /*
538 1.10 gwr * Routines to copy state into and out of the clock.
539 1.10 gwr * The intersil registers have to be read or written
540 1.10 gwr * in sequential order (or so it appears). -gwr
541 1.10 gwr */
542 1.10 gwr static void
543 1.10 gwr intersil_get_dt(struct clock_ymdhms *dt)
544 1.10 gwr {
545 1.10 gwr volatile struct intersil_dt *isdt;
546 1.10 gwr int s;
547 1.10 gwr
548 1.10 gwr isdt = &intersil_clock->counters;
549 1.10 gwr s = splhigh();
550 1.10 gwr
551 1.10 gwr /* Enable read (stop time) */
552 1.10 gwr intersil_clock->clk_cmd_reg =
553 1.26 tsutsui intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
554 1.10 gwr
555 1.10 gwr /* Copy the info. Careful about the order! */
556 1.10 gwr dt->dt_sec = isdt->dt_csec; /* throw-away */
557 1.10 gwr dt->dt_hour = isdt->dt_hour;
558 1.10 gwr dt->dt_min = isdt->dt_min;
559 1.10 gwr dt->dt_sec = isdt->dt_sec;
560 1.10 gwr dt->dt_mon = isdt->dt_month;
561 1.10 gwr dt->dt_day = isdt->dt_day;
562 1.10 gwr dt->dt_year = isdt->dt_year;
563 1.10 gwr dt->dt_wday = isdt->dt_dow;
564 1.10 gwr
565 1.10 gwr /* Done reading (time wears on) */
566 1.10 gwr intersil_clock->clk_cmd_reg =
567 1.26 tsutsui intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
568 1.10 gwr splx(s);
569 1.10 gwr }
570 1.10 gwr
571 1.10 gwr static void
572 1.10 gwr intersil_set_dt(struct clock_ymdhms *dt)
573 1.10 gwr {
574 1.10 gwr volatile struct intersil_dt *isdt;
575 1.10 gwr int s;
576 1.10 gwr
577 1.10 gwr isdt = &intersil_clock->counters;
578 1.10 gwr s = splhigh();
579 1.10 gwr
580 1.10 gwr /* Enable write (stop time) */
581 1.10 gwr intersil_clock->clk_cmd_reg =
582 1.26 tsutsui intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
583 1.10 gwr
584 1.10 gwr /* Copy the info. Careful about the order! */
585 1.10 gwr isdt->dt_csec = 0;
586 1.10 gwr isdt->dt_hour = dt->dt_hour;
587 1.10 gwr isdt->dt_min = dt->dt_min;
588 1.10 gwr isdt->dt_sec = dt->dt_sec;
589 1.10 gwr isdt->dt_month= dt->dt_mon;
590 1.10 gwr isdt->dt_day = dt->dt_day;
591 1.10 gwr isdt->dt_year = dt->dt_year;
592 1.10 gwr isdt->dt_dow = dt->dt_wday;
593 1.10 gwr
594 1.10 gwr /* Done writing (time wears on) */
595 1.10 gwr intersil_clock->clk_cmd_reg =
596 1.26 tsutsui intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
597 1.10 gwr splx(s);
598 1.10 gwr }
599 1.10 gwr
600 1.10 gwr #endif /* SUN3_470 */
601 1.10 gwr
602 1.10 gwr
603 1.10 gwr /*
604 1.3 gwr * Routines to copy state into and out of the clock.
605 1.3 gwr * The clock CSR has to be set for read or write.
606 1.1 gwr */
607 1.3 gwr static void
608 1.10 gwr mostek_get_dt(struct clock_ymdhms *dt)
609 1.1 gwr {
610 1.10 gwr volatile struct mostek_clkreg *cl = mostek_clk_va;
611 1.1 gwr int s;
612 1.1 gwr
613 1.1 gwr s = splhigh();
614 1.7 gwr
615 1.3 gwr /* enable read (stop time) */
616 1.3 gwr cl->cl_csr |= CLK_READ;
617 1.1 gwr
618 1.3 gwr /* Copy the info */
619 1.3 gwr dt->dt_sec = cl->cl_sec;
620 1.3 gwr dt->dt_min = cl->cl_min;
621 1.3 gwr dt->dt_hour = cl->cl_hour;
622 1.3 gwr dt->dt_wday = cl->cl_wday;
623 1.3 gwr dt->dt_day = cl->cl_mday;
624 1.3 gwr dt->dt_mon = cl->cl_month;
625 1.3 gwr dt->dt_year = cl->cl_year;
626 1.1 gwr
627 1.3 gwr /* Done reading (time wears on) */
628 1.3 gwr cl->cl_csr &= ~CLK_READ;
629 1.1 gwr splx(s);
630 1.1 gwr }
631 1.1 gwr
632 1.3 gwr static void
633 1.10 gwr mostek_set_dt(struct clock_ymdhms *dt)
634 1.1 gwr {
635 1.10 gwr volatile struct mostek_clkreg *cl = mostek_clk_va;
636 1.1 gwr int s;
637 1.1 gwr
638 1.1 gwr s = splhigh();
639 1.3 gwr /* enable write */
640 1.3 gwr cl->cl_csr |= CLK_WRITE;
641 1.1 gwr
642 1.3 gwr /* Copy the info */
643 1.3 gwr cl->cl_sec = dt->dt_sec;
644 1.3 gwr cl->cl_min = dt->dt_min;
645 1.3 gwr cl->cl_hour = dt->dt_hour;
646 1.3 gwr cl->cl_wday = dt->dt_wday;
647 1.3 gwr cl->cl_mday = dt->dt_day;
648 1.3 gwr cl->cl_month = dt->dt_mon;
649 1.3 gwr cl->cl_year = dt->dt_year;
650 1.1 gwr
651 1.3 gwr /* load them up */
652 1.3 gwr cl->cl_csr &= ~CLK_WRITE;
653 1.1 gwr splx(s);
654 1.1 gwr }
655 1.1 gwr
656