1 /* $NetBSD: clock.c,v 1.36 2008/03/28 20:26:13 tsutsui Exp $ */
4 * Copyright (c) 1982, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: Utah Hdr: clock.c 1.18 91/01/21$
36 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
40 * Copyright (c) 1994 Gordon W. Ross
41 * Copyright (c) 1993 Adam Glass
42 * Copyright (c) 1988 University of Utah.
44 * This code is derived from software contributed to Berkeley by
45 * the Systems Programming Group of the University of Utah Computer
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * from: Utah Hdr: clock.c 1.18 91/01/21$
77 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
81 * Machine-dependent clock routines. Sun3X machines may have
82 * either the Mostek 48T02 or the Intersil 7170 clock.
84 * It is tricky to determine which you have, because there is
85 * always something responding at the address where the Mostek
86 * clock might be found: either a Mostek or plain-old EEPROM.
87 * Therefore, we cheat. If we find an Intersil clock, assume
88 * that what responds at the end of the EEPROM space is just
89 * plain-old EEPROM (not a Mostek clock). Worse, there are
90 * H/W problems with probing for an Intersil on the 3/80, so
91 * on that machine we "know" there is a Mostek clock.
93 * Note that the probing algorithm described above requires
94 * that we probe the intersil before we probe the mostek!
97 #include <sys/cdefs.h>
98 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.36 2008/03/28 20:26:13 tsutsui Exp $");
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/time.h>
103 #include <sys/kernel.h>
104 #include <sys/device.h>
106 #include <uvm/uvm_extern.h>
108 #include <m68k/asm_single.h>
110 #include <machine/autoconf.h>
111 #include <machine/bus.h>
112 #include <machine/cpu.h>
113 #include <machine/idprom.h>
114 #include <machine/leds.h>
116 #include <dev/clock_subr.h>
117 #include <dev/ic/intersil7170reg.h>
118 #include <dev/ic/intersil7170var.h>
119 #include <dev/ic/mk48txxreg.h>
120 #include <dev/ic/mk48txxvar.h>
122 #include <sun3/sun3/machdep.h>
123 #include <sun3/sun3/interreg.h>
125 extern int intrcnt
[];
130 #define IREG_CLK_BITS (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5)
132 #define MKCLOCK_REG_OFFSET (MK48T02_CLKOFF + MK48TXX_ICSR)
135 * Only one of these two variables should be non-zero after
136 * autoconfiguration determines which clock we have.
138 static volatile void *intersil_va
;
139 static volatile void *mostek_clk_va
;
141 void _isr_clock(void); /* in locore.s */
142 void clock_intr(struct clockframe
);
145 static int clock_match(device_t
, cfdata_t
, void *);
146 static void clock_attach(device_t
, device_t
, void *);
148 CFATTACH_DECL_NEW(clock
, sizeof(struct mk48txx_softc
),
149 clock_match
, clock_attach
, NULL
, NULL
);
153 #define intersil_clock ((volatile struct intersil7170 *)intersil_va)
155 #define intersil_clear() (void)intersil_clock->clk_intr_reg
157 static int oclock_match(device_t
, cfdata_t
, void *);
158 static void oclock_attach(device_t
, device_t
, void *);
160 CFATTACH_DECL_NEW(oclock
, sizeof(struct intersil7170_softc
),
161 oclock_match
, oclock_attach
, NULL
, NULL
);
165 * Is there an intersil clock?
168 oclock_match(device_t parent
, cfdata_t cf
, void *aux
)
170 struct confargs
*ca
= aux
;
172 /* This driver only supports one unit. */
177 * The 3/80 can not probe the Intersil absent,
178 * but it never has one, so "just say no."
180 if (cpu_machine_id
== ID_SUN3X_80
)
183 /* OK, really probe for the Intersil. */
184 if (bus_peek(ca
->ca_bustype
, ca
->ca_paddr
, 1) == -1)
187 /* Default interrupt priority. */
188 if (ca
->ca_intpri
== -1)
189 ca
->ca_intpri
= CLOCK_PRI
;
195 * Attach the intersil clock.
198 oclock_attach(device_t parent
, device_t self
, void *aux
)
200 struct intersil7170_softc
*sc
= device_private(self
);
201 struct confargs
*ca
= aux
;
205 /* Get a mapping for it. */
206 sc
->sc_bst
= ca
->ca_bustag
;
207 if (bus_space_map(sc
->sc_bst
, ca
->ca_paddr
, sizeof(struct intersil7170
),
208 0, &sc
->sc_bsh
) != 0) {
209 aprint_error(": can't map registers\n");
213 intersil_va
= bus_space_vaddr(sc
->sc_bst
, sc
->sc_bsh
);
216 /* Verify correct probe order... */
218 mostek_clk_va
= NULL
;
220 aprint_error_dev(self
, "warning - mostek found also!\n");
225 * Set the clock to the correct interrupt rate, but
226 * do not enable the interrupt until cpu_initclocks.
227 * XXX: Actually, the interrupt_reg should be zero
228 * at this point, so the clock interrupts should not
229 * affect us, but we need to set the rate...
231 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, INTERSIL_ICMD
,
232 INTERSIL_COMMAND(INTERSIL_CMD_RUN
, INTERSIL_CMD_IDISABLE
));
233 (void)bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, INTERSIL_IINTR
);
235 /* Set the clock to 100 Hz, but do not enable it yet. */
236 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
,
237 INTERSIL_IINTR
, INTERSIL_INTER_CSECONDS
);
240 intersil7170_attach(sc
);
245 * Can not hook up the ISR until cpu_initclocks()
246 * because hardclock is not ready until then.
247 * For now, the handler is _isr_autovec(), which
248 * will complain if it gets clock interrupts.
251 #endif /* SUN3_470 */
255 * Is there a Mostek clock? Hard to tell...
256 * (See comment at top of this file.)
259 clock_match(device_t parent
, cfdata_t cf
, void *args
)
261 struct confargs
*ca
= args
;
263 /* This driver only supports one unit. */
267 /* If intersil was found, use that. */
270 /* Else assume a Mostek is there... */
272 /* Default interrupt priority. */
273 if (ca
->ca_intpri
== -1)
274 ca
->ca_intpri
= CLOCK_PRI
;
280 * Attach the mostek clock.
283 clock_attach(device_t parent
, device_t self
, void *aux
)
285 struct mk48txx_softc
*sc
= device_private(self
);
286 struct confargs
*ca
= aux
;
289 sc
->sc_bst
= ca
->ca_bustag
;
290 if (bus_space_map(sc
->sc_bst
, ca
->ca_paddr
- MKCLOCK_REG_OFFSET
,
291 MK48T02_CLKSZ
, 0, &sc
->sc_bsh
) != 0) {
292 aprint_error(": can't map device space\n");
296 mostek_clk_va
= bus_space_vaddr(sc
->sc_bst
, sc
->sc_bsh
);
298 sc
->sc_model
= "mk48t02";
307 * Set and/or clear the desired clock bits in the interrupt
308 * register. We have to be extremely careful that we do it
309 * in such a manner that we don't get ourselves lost.
310 * XXX: Watch out! It's really easy to break this!
313 set_clk_mode(u_char on
, u_char off
, int enable_clk
)
318 * If we have not yet mapped the register,
319 * then we do not want to do any of this...
325 /* Assertion: were are at splhigh! */
326 if ((getsr() & PSL_IPL
) < PSL_IPL7
)
327 panic("set_clk_mode: bad ipl");
331 * make sure that we are only playing w/
332 * clock interrupt register bits
335 off
&= IREG_CLK_BITS
;
337 /* First, turn off the "master" enable bit. */
338 single_inst_bclr_b(*interrupt_reg
, IREG_ALL_ENAB
);
341 * Save the current interrupt register clock bits,
342 * and turn off/on the requested bits in the copy.
344 interreg
= *interrupt_reg
& IREG_CLK_BITS
;
348 /* Clear the CLK5 and CLK7 bits to clear the flip-flops. */
349 single_inst_bclr_b(*interrupt_reg
, IREG_CLK_BITS
);
354 * Then disable clock interrupts, and read the clock's
355 * interrupt register to clear any pending signals there.
357 intersil_clock
->clk_cmd_reg
=
358 INTERSIL_COMMAND(INTERSIL_CMD_RUN
, INTERSIL_CMD_IDISABLE
);
361 #endif /* SUN3_470 */
363 /* Set the requested bits in the interrupt register. */
364 single_inst_bset_b(*interrupt_reg
, interreg
);
367 /* Turn the clock back on (maybe) */
368 if (intersil_va
&& enable_clk
)
369 intersil_clock
->clk_cmd_reg
=
370 INTERSIL_COMMAND(INTERSIL_CMD_RUN
, INTERSIL_CMD_IENABLE
);
371 #endif /* SUN3_470 */
373 /* Finally, turn the "master" enable back on. */
374 single_inst_bset_b(*interrupt_reg
, IREG_ALL_ENAB
);
378 * Set up the real-time clock (enable clock interrupts).
379 * Leave stathz 0 since there is no secondary clock available.
380 * Note that clock interrupts MUST STAY DISABLED until here.
389 /* Install isr (in locore.s) that calls clock_intr(). */
390 isr_add_custom(CLOCK_PRI
, (void *)_isr_clock
);
392 /* Now enable the clock at level 5 in the interrupt reg. */
393 set_clk_mode(IREG_CLOCK_ENAB_5
, 0, 1);
399 * This doesn't need to do anything, as we have only one timer and
400 * profhz==stathz==hz.
403 setstatclockrate(int newhz
)
410 * Clock interrupt handler (for both Intersil and Mostek).
411 * XXX - Is it worth the trouble to save a few cycles here
412 * by making two separate interrupt handlers?
414 * This is is called by the "custom" interrupt handler.
415 * Note that we can get ZS interrupts while this runs,
416 * and zshard may touch the interrupt_reg, so we must
417 * be careful to use the single_inst_* macros to modify
418 * the interrupt register atomically.
421 clock_intr(struct clockframe cf
)
423 extern char _Idle
[]; /* locore.s */
429 /* Read the clock interrupt register. */
432 #endif /* SUN3_470 */
434 /* Pulse the clock intr. enable low. */
435 single_inst_bclr_b(*interrupt_reg
, IREG_CLOCK_ENAB_5
);
436 single_inst_bset_b(*interrupt_reg
, IREG_CLOCK_ENAB_5
);
440 /* Read the clock intr. reg. AGAIN! */
443 #endif /* SUN3_470 */
445 intrcnt
[CLOCK_PRI
]++;
449 if (cf
.cf_pc
== (long)_Idle
)
452 /* Call common clock interrupt handler. */