xref: /netbsd-src/sys/arch/sparc64/dev/mkclock.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: mkclock.c,v 1.12 2012/10/27 17:18:12 chs Exp $ */
2 
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.
11  *
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.
15  *
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.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  *
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.
40  *
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.
52  *
53  *	@(#)clock.c	8.1 (Berkeley) 6/11/93
54  *
55  */
56 
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.12 2012/10/27 17:18:12 chs Exp $");
59 
60 /*
61  * Clock driver for 'mkclock' - Mostek MK48Txx TOD clock.
62  */
63 
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>
71 
72 #include <uvm/uvm_extern.h>
73 
74 #include <sys/bus.h>
75 #include <machine/autoconf.h>
76 #include <machine/eeprom.h>
77 #include <machine/cpu.h>
78 
79 #include <dev/clock_subr.h>
80 #include <dev/ic/mk48txxreg.h>
81 #include <dev/ic/mk48txxvar.h>
82 
83 #include <dev/sbus/sbusvar.h>
84 #include <dev/ebus/ebusreg.h>
85 #include <dev/ebus/ebusvar.h>
86 
87 #include <sparc64/dev/fhcvar.h>
88 
89 /*
90  * clock (eeprom) attaches at the sbus or the ebus (PCI)
91  */
92 static int	mkclock_sbus_match(device_t, cfdata_t, void *);
93 static void	mkclock_sbus_attach(device_t, device_t, void *);
94 
95 static int	mkclock_ebus_match(device_t, cfdata_t, void *);
96 static void	mkclock_ebus_attach(device_t, device_t, void *);
97 
98 static int	mkclock_fhc_match(device_t, cfdata_t, void *);
99 static void	mkclock_fhc_attach(device_t, device_t, void *);
100 
101 static void	mkclock_attach(struct mk48txx_softc *, int);
102 
103 static int	mkclock_wenable(struct todr_chip_handle *, int);
104 
105 
106 CFATTACH_DECL_NEW(mkclock_sbus, sizeof(struct mk48txx_softc),
107     mkclock_sbus_match, mkclock_sbus_attach, NULL, NULL);
108 
109 CFATTACH_DECL_NEW(mkclock_ebus, sizeof(struct mk48txx_softc),
110     mkclock_ebus_match, mkclock_ebus_attach, NULL, NULL);
111 
112 CFATTACH_DECL_NEW(mkclock_fhc, sizeof(struct mk48txx_softc),
113     mkclock_fhc_match, mkclock_fhc_attach, NULL, NULL);
114 
115 /*
116  * The OPENPROM calls the clock the "eeprom", so we have to have our
117  * own special match function to call it the "clock".
118  */
119 static int
120 mkclock_sbus_match(device_t parent, cfdata_t cf, void *aux)
121 {
122 	struct sbus_attach_args *sa = aux;
123 
124 	return (strcmp("eeprom", sa->sa_name) == 0);
125 }
126 
127 static int
128 mkclock_ebus_match(device_t parent, cfdata_t cf, void *aux)
129 {
130 	struct ebus_attach_args *ea = aux;
131 
132 	return (strcmp("eeprom", ea->ea_name) == 0);
133 }
134 
135 static int
136 mkclock_fhc_match(device_t parent, cfdata_t cf, void *aux)
137 {
138 	struct fhc_attach_args *fa = aux;
139 
140 	return (strcmp("eeprom", fa->fa_name) == 0);
141 }
142 
143 /*
144  * Attach a clock (really `eeprom') to the sbus or ebus.
145  *
146  * We ignore any existing virtual address as we need to map
147  * this read-only and make it read-write only temporarily,
148  * whenever we read or write the clock chip.  The clock also
149  * contains the ID ``PROM'', and I have already had the pleasure
150  * of reloading the CPU type, Ethernet address, etc, by hand from
151  * the console FORTH interpreter.  I intend not to enjoy it again.
152  *
153  * the MK48T02 is 2K.  the MK48T08 is 8K, and the MK48T59 is
154  * supposed to be identical to it.
155  *
156  * This is *UGLY*!  We probably have multiple mappings.  But I do
157  * know that this all fits inside an 8K page, so I'll just map in
158  * once.
159  *
160  * What we really need is some way to record the bus attach args
161  * so we can call *_bus_map() later with BUS_SPACE_MAP_READONLY
162  * or not to write enable/disable the device registers.  This is
163  * a non-trivial operation.
164  */
165 
166 /* ARGSUSED */
167 static void
168 mkclock_sbus_attach(device_t parent, device_t self, void *aux)
169 {
170 	struct mk48txx_softc *sc = device_private(self);
171 	struct sbus_attach_args *sa = aux;
172 	int sz;
173 
174 	sc->sc_dev = self;
175 	sc->sc_bst = sa->sa_bustag;
176 
177 	/* use sa->sa_regs[0].size? */
178 	sz = 8192;
179 
180 	if (sbus_bus_map(sc->sc_bst,
181 			 sa->sa_slot,
182 			 trunc_page(sa->sa_offset),
183 			 sz,
184 			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_READONLY,
185 			 &sc->sc_bsh) != 0) {
186 		aprint_error(": can't map register\n");
187 		return;
188 	}
189 	mkclock_attach(sc, sa->sa_node);
190 }
191 
192 
193 /* ARGSUSED */
194 static void
195 mkclock_ebus_attach(device_t parent, device_t self, void *aux)
196 {
197 	struct mk48txx_softc *sc = device_private(self);
198 	struct ebus_attach_args *ea = aux;
199 	int sz;
200 
201 	sc->sc_dev = self;
202 	sc->sc_bst = ea->ea_bustag;
203 
204 	/* hard code to 8K? */
205 	sz = ea->ea_reg[0].size;
206 
207 	/* Use the PROM address if there. */
208 	if (ea->ea_nvaddr)
209 		sparc_promaddr_to_handle(sc->sc_bst, ea->ea_vaddr[0],
210 			&sc->sc_bsh);
211 	else if (bus_space_map(sc->sc_bst,
212 			 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
213 			 sz,
214 			 BUS_SPACE_MAP_LINEAR,
215 			 &sc->sc_bsh) != 0) {
216 		aprint_error(": can't map register\n");
217 		return;
218 	}
219 	mkclock_attach(sc, ea->ea_node);
220 }
221 
222 /* ARGSUSED */
223 static void
224 mkclock_fhc_attach(device_t parent, device_t self, void *aux)
225 {
226 	struct mk48txx_softc *sc = device_private(self);
227 	struct fhc_attach_args *fa = aux;
228 
229 	sc->sc_dev = self;
230 	sc->sc_bst = fa->fa_bustag;
231 
232 	if (fhc_bus_map(sc->sc_bst,
233 			fa->fa_reg[0].fbr_slot,
234 			(fa->fa_reg[0].fbr_offset & ~NBPG),
235 			fa->fa_reg[0].fbr_size,
236 			BUS_SPACE_MAP_LINEAR,
237 			&sc->sc_bsh) != 0) {
238 		aprint_error(": can't map register\n");
239 		return;
240 	}
241 	mkclock_attach(sc, fa->fa_node);
242 }
243 
244 
245 static void
246 mkclock_attach(struct mk48txx_softc *sc, int node)
247 {
248 
249 	sc->sc_model = prom_getpropstring(node, "model");
250 
251 #ifdef DIAGNOSTIC
252 	if (sc->sc_model == NULL)
253 		panic("clockattach: no model property");
254 #endif
255 
256 	/* Our TOD clock year 0 is 1968 */
257 	sc->sc_year0 = 1968;
258 
259 	/* Save info for the clock wenable call. */
260 	sc->sc_handle.todr_setwen = mkclock_wenable;
261 
262 	mk48txx_attach(sc);
263 
264 	aprint_normal("\n");
265 }
266 
267 /*
268  * Write en/dis-able clock registers.  We coordinate so that several
269  * writers can run simultaneously.
270  */
271 static int
272 mkclock_wenable(struct todr_chip_handle *handle, int onoff)
273 {
274 	struct mk48txx_softc *sc;
275 	vm_prot_t prot;
276 	vaddr_t va;
277 	int s, err = 0;
278 	static int writers;
279 
280 	/* XXXSMP */
281 	s = splhigh();
282 	if (onoff)
283 		prot = writers++ == 0 ? VM_PROT_READ|VM_PROT_WRITE : 0;
284 	else
285 		prot = --writers == 0 ? VM_PROT_READ : 0;
286 	splx(s);
287 	if (prot == VM_PROT_NONE) {
288 		return 0;
289 	}
290 	sc = handle->cookie;
291 	va = (vaddr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
292 	if (va == 0UL) {
293 		printf("clock_wenable: WARNING -- cannot get va\n");
294 		return EIO;
295 	}
296 	pmap_kprotect(va, prot);
297 	return (err);
298 }
299