Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / sparc64 / dev / mkclock.c
blob11704c3d11d29f6669b1ef9bc96e98726fd348d4
1 /* $NetBSD: mkclock.c,v 1.5 2008/03/29 05:47:53 tsutsui Exp $ */
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1994 Gordon W. Ross
7 * Copyright (c) 1993 Adam Glass
8 * Copyright (c) 1996 Paul Kranenburg
9 * Copyright (c) 1996
10 * The President and Fellows of Harvard College. All rights reserved.
12 * This software was developed by the Computer Systems Engineering group
13 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
14 * contributed to Berkeley.
16 * All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Harvard University.
19 * This product includes software developed by the University of
20 * California, Lawrence Berkeley Laboratory.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by the University of
34 * California, Berkeley and its contributors.
35 * This product includes software developed by Paul Kranenburg.
36 * This product includes software developed by Harvard University.
37 * 4. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
53 * @(#)clock.c 8.1 (Berkeley) 6/11/93
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.5 2008/03/29 05:47:53 tsutsui Exp $");
60 /*
61 * Clock driver for 'mkclock' - Mostek MK48Txx TOD clock.
64 #include <sys/param.h>
65 #include <sys/kernel.h>
66 #include <sys/device.h>
67 #include <sys/proc.h>
68 #include <sys/resourcevar.h>
69 #include <sys/malloc.h>
70 #include <sys/systm.h>
72 #include <uvm/uvm_extern.h>
74 #include <machine/bus.h>
75 #include <machine/autoconf.h>
76 #include <machine/eeprom.h>
77 #include <machine/cpu.h>
79 #include <dev/clock_subr.h>
80 #include <dev/ic/mk48txxreg.h>
81 #include <dev/ic/mk48txxvar.h>
83 #include <sparc64/dev/iommureg.h>
84 #include <sparc64/dev/sbusreg.h>
85 #include <dev/sbus/sbusvar.h>
86 #include <dev/ebus/ebusreg.h>
87 #include <dev/ebus/ebusvar.h>
90 * clock (eeprom) attaches at the sbus or the ebus (PCI)
92 static int mkclock_sbus_match(device_t, cfdata_t, void *);
93 static void mkclock_sbus_attach(device_t, device_t, void *);
95 static int mkclock_ebus_match(device_t, cfdata_t, void *);
96 static void mkclock_ebus_attach(device_t, device_t, void *);
98 static void mkclock_attach(struct mk48txx_softc *, int);
100 static int mkclock_wenable(struct todr_chip_handle *, int);
103 CFATTACH_DECL_NEW(mkclock_sbus, sizeof(struct mk48txx_softc),
104 mkclock_sbus_match, mkclock_sbus_attach, NULL, NULL);
106 CFATTACH_DECL_NEW(mkclock_ebus, sizeof(struct mk48txx_softc),
107 mkclock_ebus_match, mkclock_ebus_attach, NULL, NULL);
110 * The OPENPROM calls the clock the "eeprom", so we have to have our
111 * own special match function to call it the "clock".
113 static int
114 mkclock_sbus_match(device_t parent, cfdata_t cf, void *aux)
116 struct sbus_attach_args *sa = aux;
118 return (strcmp("eeprom", sa->sa_name) == 0);
121 static int
122 mkclock_ebus_match(device_t parent, cfdata_t cf, void *aux)
124 struct ebus_attach_args *ea = aux;
126 return (strcmp("eeprom", ea->ea_name) == 0);
130 * Attach a clock (really `eeprom') to the sbus or ebus.
132 * We ignore any existing virtual address as we need to map
133 * this read-only and make it read-write only temporarily,
134 * whenever we read or write the clock chip. The clock also
135 * contains the ID ``PROM'', and I have already had the pleasure
136 * of reloading the CPU type, Ethernet address, etc, by hand from
137 * the console FORTH interpreter. I intend not to enjoy it again.
139 * the MK48T02 is 2K. the MK48T08 is 8K, and the MK48T59 is
140 * supposed to be identical to it.
142 * This is *UGLY*! We probably have multiple mappings. But I do
143 * know that this all fits inside an 8K page, so I'll just map in
144 * once.
146 * What we really need is some way to record the bus attach args
147 * so we can call *_bus_map() later with BUS_SPACE_MAP_READONLY
148 * or not to write enable/disable the device registers. This is
149 * a non-trivial operation.
152 /* ARGSUSED */
153 static void
154 mkclock_sbus_attach(device_t parent, device_t self, void *aux)
156 struct mk48txx_softc *sc = device_private(self);
157 struct sbus_attach_args *sa = aux;
158 int sz;
160 sc->sc_dev = self;
161 sc->sc_bst = sa->sa_bustag;
163 /* use sa->sa_regs[0].size? */
164 sz = 8192;
166 if (sbus_bus_map(sc->sc_bst,
167 sa->sa_slot,
168 trunc_page(sa->sa_offset),
170 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_READONLY,
171 &sc->sc_bsh) != 0) {
172 aprint_error(": can't map register\n");
173 return;
175 mkclock_attach(sc, sa->sa_node);
179 /* ARGSUSED */
180 static void
181 mkclock_ebus_attach(struct device *parent, struct device *self, void *aux)
183 struct mk48txx_softc *sc = device_private(self);
184 struct ebus_attach_args *ea = aux;
185 int sz;
187 sc->sc_dev = self;
188 sc->sc_bst = ea->ea_bustag;
190 /* hard code to 8K? */
191 sz = ea->ea_reg[0].size;
193 if (bus_space_map(sc->sc_bst,
194 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
196 BUS_SPACE_MAP_LINEAR,
197 &sc->sc_bsh) != 0) {
198 aprint_error(": can't map register\n");
199 return;
201 mkclock_attach(sc, ea->ea_node);
205 static void
206 mkclock_attach(struct mk48txx_softc *sc, int node)
209 sc->sc_model = prom_getpropstring(node, "model");
211 #ifdef DIAGNOSTIC
212 if (sc->sc_model == NULL)
213 panic("clockattach: no model property");
214 #endif
216 /* Our TOD clock year 0 is 1968 */
217 sc->sc_year0 = 1968;
219 /* Save info for the clock wenable call. */
220 sc->sc_handle.todr_setwen = mkclock_wenable;
222 mk48txx_attach(sc);
224 aprint_normal("\n");
228 * Write en/dis-able clock registers. We coordinate so that several
229 * writers can run simultaneously.
231 static int
232 mkclock_wenable(struct todr_chip_handle *handle, int onoff)
234 struct mk48txx_softc *sc;
235 vm_prot_t prot;
236 vaddr_t va;
237 int s, err = 0;
238 static int writers;
240 s = splhigh();
241 if (onoff)
242 prot = writers++ == 0 ? VM_PROT_READ|VM_PROT_WRITE : 0;
243 else
244 prot = --writers == 0 ? VM_PROT_READ : 0;
245 splx(s);
246 if (prot == VM_PROT_NONE) {
247 return 0;
249 sc = handle->cookie;
250 va = (vaddr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
251 if (va == 0UL) {
252 printf("clock_wenable: WARNING -- cannot get va\n");
253 return EIO;
255 pmap_kprotect(va, prot);
256 return (err);