obio.c revision 1.39 1 1.39 pk /* $NetBSD: obio.c,v 1.39 1998/01/25 19:44:43 pk Exp $ */
2 1.39 pk
3 1.39 pk /*-
4 1.39 pk * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.39 pk * All rights reserved.
6 1.39 pk *
7 1.39 pk * This code is derived from software contributed to The NetBSD Foundation
8 1.39 pk * by Paul Kranenburg.
9 1.39 pk *
10 1.39 pk * Redistribution and use in source and binary forms, with or without
11 1.39 pk * modification, are permitted provided that the following conditions
12 1.39 pk * are met:
13 1.39 pk * 1. Redistributions of source code must retain the above copyright
14 1.39 pk * notice, this list of conditions and the following disclaimer.
15 1.39 pk * 2. Redistributions in binary form must reproduce the above copyright
16 1.39 pk * notice, this list of conditions and the following disclaimer in the
17 1.39 pk * documentation and/or other materials provided with the distribution.
18 1.39 pk * 3. All advertising materials mentioning features or use of this software
19 1.39 pk * must display the following acknowledgement:
20 1.39 pk * This product includes software developed by the NetBSD
21 1.39 pk * Foundation, Inc. and its contributors.
22 1.39 pk * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.39 pk * contributors may be used to endorse or promote products derived
24 1.39 pk * from this software without specific prior written permission.
25 1.39 pk *
26 1.39 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.39 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.39 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.39 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.39 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.39 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.39 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.39 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.39 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.39 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.39 pk * POSSIBILITY OF SUCH DAMAGE.
37 1.39 pk */
38 1.1 deraadt
39 1.1 deraadt /*
40 1.1 deraadt * Copyright (c) 1993, 1994 Theo de Raadt
41 1.33 pk * Copyright (c) 1995, 1997 Paul Kranenburg
42 1.1 deraadt * All rights reserved.
43 1.1 deraadt *
44 1.1 deraadt * Redistribution and use in source and binary forms, with or without
45 1.1 deraadt * modification, are permitted provided that the following conditions
46 1.1 deraadt * are met:
47 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
48 1.1 deraadt * notice, this list of conditions and the following disclaimer.
49 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
51 1.1 deraadt * documentation and/or other materials provided with the distribution.
52 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
53 1.1 deraadt * must display the following acknowledgement:
54 1.1 deraadt * This product includes software developed by Theo de Raadt.
55 1.1 deraadt * 4. The name of the author may not be used to endorse or promote products
56 1.1 deraadt * derived from this software without specific prior written permission.
57 1.1 deraadt *
58 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59 1.1 deraadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 1.1 deraadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 1.1 deraadt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62 1.1 deraadt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63 1.1 deraadt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 1.1 deraadt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 1.1 deraadt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 1.1 deraadt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 1.1 deraadt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 1.1 deraadt */
69 1.1 deraadt
70 1.1 deraadt #include <sys/param.h>
71 1.20 christos #include <sys/systm.h>
72 1.1 deraadt #include <sys/device.h>
73 1.1 deraadt #include <sys/malloc.h>
74 1.1 deraadt
75 1.1 deraadt #ifdef DEBUG
76 1.1 deraadt #include <sys/proc.h>
77 1.1 deraadt #include <sys/syslog.h>
78 1.1 deraadt #endif
79 1.1 deraadt
80 1.1 deraadt #include <vm/vm.h>
81 1.1 deraadt
82 1.1 deraadt #include <machine/autoconf.h>
83 1.1 deraadt #include <machine/pmap.h>
84 1.2 deraadt #include <machine/oldmon.h>
85 1.5 deraadt #include <machine/cpu.h>
86 1.2 deraadt #include <machine/ctlreg.h>
87 1.2 deraadt #include <sparc/sparc/asm.h>
88 1.2 deraadt #include <sparc/sparc/vaddrs.h>
89 1.30 pk #include <sparc/sparc/cpuvar.h>
90 1.22 pk #include <sparc/dev/sbusvar.h>
91 1.1 deraadt
92 1.4 deraadt struct bus_softc {
93 1.22 pk union {
94 1.22 pk struct device scu_dev; /* base device */
95 1.22 pk struct sbus_softc scu_sbus; /* obio is another sbus slot */
96 1.22 pk } bu;
97 1.1 deraadt };
98 1.1 deraadt
99 1.32 pk
100 1.1 deraadt /* autoconfiguration driver */
101 1.39 pk int obioprint __P((void *, const char *));
102 1.39 pk static int obiomatch __P((struct device *, struct cfdata *, void *));
103 1.39 pk static void obioattach __P((struct device *, struct device *, void *));
104 1.39 pk
105 1.39 pk #if defined(SUN4)
106 1.39 pk static void obioattach4 __P((struct device *, struct device *, void *));
107 1.39 pk int obiosearch __P((struct device *, struct cfdata *, void *));
108 1.39 pk #endif
109 1.39 pk #if defined(SUN4M)
110 1.39 pk static void obioattach4m __P((struct device *, struct device *, void *));
111 1.39 pk #endif
112 1.4 deraadt
113 1.21 thorpej struct cfattach obio_ca = {
114 1.39 pk sizeof(struct bus_softc), obiomatch, obioattach
115 1.4 deraadt };
116 1.21 thorpej
117 1.1 deraadt int
118 1.39 pk obiomatch(parent, cf, aux)
119 1.1 deraadt struct device *parent;
120 1.28 pk struct cfdata *cf;
121 1.28 pk void *aux;
122 1.1 deraadt {
123 1.3 deraadt register struct confargs *ca = aux;
124 1.3 deraadt register struct romaux *ra = &ca->ca_ra;
125 1.3 deraadt
126 1.3 deraadt return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
127 1.1 deraadt }
128 1.1 deraadt
129 1.1 deraadt int
130 1.39 pk obioprint(args, name)
131 1.1 deraadt void *args;
132 1.39 pk const char *name;
133 1.1 deraadt {
134 1.2 deraadt register struct confargs *ca = args;
135 1.2 deraadt
136 1.2 deraadt if (ca->ca_ra.ra_name == NULL)
137 1.2 deraadt ca->ca_ra.ra_name = "<unknown>";
138 1.22 pk
139 1.39 pk if (name)
140 1.39 pk printf("[%s at %s]", ca->ca_ra.ra_name, name);
141 1.22 pk
142 1.27 christos printf(" addr %p", ca->ca_ra.ra_paddr);
143 1.22 pk
144 1.39 pk return (UNCONF);
145 1.39 pk }
146 1.22 pk
147 1.39 pk void
148 1.39 pk obioattach(parent, self, args)
149 1.39 pk struct device *parent, *self;
150 1.39 pk void *args;
151 1.39 pk {
152 1.39 pk #if defined(SUN4)
153 1.39 pk if (CPU_ISSUN4)
154 1.39 pk obioattach4(parent, self, args);
155 1.39 pk #endif
156 1.39 pk #if defined(SUN4M)
157 1.39 pk if (CPU_ISSUN4M)
158 1.39 pk obioattach4m(parent, self, args);
159 1.39 pk #endif
160 1.1 deraadt }
161 1.1 deraadt
162 1.39 pk #if defined(SUN4)
163 1.39 pk void
164 1.39 pk obioattach4(parent, self, args)
165 1.39 pk struct device *parent, *self;
166 1.32 pk void *args;
167 1.32 pk {
168 1.39 pk if (self->dv_unit > 0) {
169 1.39 pk printf(" unsupported\n");
170 1.39 pk return;
171 1.39 pk }
172 1.32 pk
173 1.39 pk printf("\n");
174 1.39 pk (void)config_search(obiosearch, self, args);
175 1.39 pk obio_bus_untmp();
176 1.32 pk }
177 1.39 pk #endif
178 1.14 pk
179 1.39 pk #if defined(SUN4M)
180 1.22 pk void
181 1.39 pk obioattach4m(parent, self, args)
182 1.22 pk struct device *parent, *self;
183 1.22 pk void *args;
184 1.22 pk {
185 1.22 pk register struct bus_softc *sc = (struct bus_softc *)self;
186 1.22 pk struct confargs oca, *ca = args;
187 1.22 pk register struct romaux *ra = &ca->ca_ra;
188 1.22 pk register int node0, node;
189 1.22 pk register char *name;
190 1.22 pk register const char *sp;
191 1.22 pk const char *const *ssp;
192 1.31 pk int rlen;
193 1.22 pk extern int autoconf_nzs;
194 1.22 pk
195 1.22 pk static const char *const special4m[] = {
196 1.22 pk /* find these first */
197 1.22 pk "eeprom",
198 1.22 pk "counter",
199 1.29 pk #if 0 /* Not all sun4m's have an `auxio' */
200 1.22 pk "auxio",
201 1.29 pk #endif
202 1.22 pk "",
203 1.22 pk /* place device to ignore here */
204 1.22 pk "interrupt",
205 1.22 pk NULL
206 1.22 pk };
207 1.22 pk
208 1.22 pk /*
209 1.22 pk * There is only one obio bus (it is in fact one of the Sbus slots)
210 1.22 pk */
211 1.32 pk if (self->dv_unit > 0) {
212 1.27 christos printf(" unsupported\n");
213 1.22 pk return;
214 1.22 pk }
215 1.22 pk
216 1.27 christos printf("\n");
217 1.22 pk
218 1.22 pk if (ra->ra_bp != NULL && strcmp(ra->ra_bp->name, "obio") == 0)
219 1.22 pk oca.ca_ra.ra_bp = ra->ra_bp + 1;
220 1.22 pk else
221 1.22 pk oca.ca_ra.ra_bp = NULL;
222 1.22 pk
223 1.31 pk node = ra->ra_node;
224 1.31 pk rlen = getproplen(node, "ranges");
225 1.31 pk if (rlen > 0) {
226 1.31 pk sc->bu.scu_sbus.sc_nrange = rlen / sizeof(struct rom_range);
227 1.31 pk sc->bu.scu_sbus.sc_range =
228 1.31 pk (struct rom_range *)malloc(rlen, M_DEVBUF, M_NOWAIT);
229 1.31 pk if (sc->bu.scu_sbus.sc_range == 0)
230 1.31 pk panic("obio: PROM ranges too large: %d", rlen);
231 1.31 pk (void)getprop(node, "ranges", sc->bu.scu_sbus.sc_range, rlen);
232 1.31 pk }
233 1.22 pk
234 1.22 pk /*
235 1.22 pk * Loop through ROM children, fixing any relative addresses
236 1.22 pk * and then configuring each device.
237 1.22 pk * We first do the crucial ones, such as eeprom, etc.
238 1.22 pk */
239 1.22 pk node0 = firstchild(ra->ra_node);
240 1.22 pk for (ssp = special4m ; *(sp = *ssp) != 0; ssp++) {
241 1.22 pk if ((node = findnode(node0, sp)) == 0) {
242 1.27 christos printf("could not find %s amongst obio devices\n", sp);
243 1.22 pk panic(sp);
244 1.22 pk }
245 1.22 pk if (!romprop(&oca.ca_ra, sp, node))
246 1.22 pk continue;
247 1.22 pk
248 1.22 pk sbus_translate(self, &oca);
249 1.22 pk oca.ca_bustype = BUS_OBIO;
250 1.39 pk (void) config_found(self, (void *)&oca, obioprint);
251 1.22 pk }
252 1.22 pk
253 1.22 pk for (node = node0; node; node = nextsibling(node)) {
254 1.22 pk name = getpropstring(node, "name");
255 1.22 pk for (ssp = special4m ; (sp = *ssp) != NULL; ssp++)
256 1.22 pk if (strcmp(name, sp) == 0)
257 1.22 pk break;
258 1.22 pk
259 1.22 pk if (sp != NULL || !romprop(&oca.ca_ra, name, node))
260 1.22 pk continue;
261 1.22 pk
262 1.22 pk if (strcmp(name, "zs") == 0)
263 1.22 pk /* XXX - see autoconf.c for this hack */
264 1.22 pk autoconf_nzs++;
265 1.22 pk
266 1.22 pk /* Translate into parent address spaces */
267 1.22 pk sbus_translate(self, &oca);
268 1.22 pk oca.ca_bustype = BUS_OBIO;
269 1.39 pk (void) config_found(self, (void *)&oca, obioprint);
270 1.22 pk }
271 1.22 pk }
272 1.35 pk #endif
273 1.35 pk
274 1.34 pk
275 1.39 pk #if defined(SUN4)
276 1.14 pk int
277 1.39 pk obiosearch(parent, cf, args)
278 1.14 pk struct device *parent;
279 1.28 pk struct cfdata *cf;
280 1.28 pk void *args;
281 1.1 deraadt {
282 1.3 deraadt register struct confargs *ca = args;
283 1.3 deraadt struct confargs oca;
284 1.4 deraadt caddr_t tmp;
285 1.1 deraadt
286 1.23 chuck
287 1.39 pk /*
288 1.39 pk * Avoid sun4m entries which don't have valid PAs.
289 1.39 pk * no point in even probing them.
290 1.39 pk */
291 1.39 pk if (cf->cf_loc[0] == -1)
292 1.39 pk return (0);
293 1.39 pk
294 1.39 pk /*
295 1.39 pk * On the 4/100 obio addresses must be mapped at
296 1.39 pk * 0x0YYYYYYY, but alias higher up (we avoid the
297 1.39 pk * alias condition because it causes pmap difficulties)
298 1.39 pk * XXX: We also assume that 4/[23]00 obio addresses
299 1.39 pk * must be 0xZYYYYYYY, where (Z != 0)
300 1.39 pk */
301 1.39 pk if (cpuinfo.cpu_type == CPUTYP_4_100 &&
302 1.39 pk (cf->cf_loc[0] & 0xf0000000))
303 1.39 pk return (0);
304 1.39 pk if (cpuinfo.cpu_type != CPUTYP_4_100 &&
305 1.39 pk !(cf->cf_loc[0] & 0xf0000000))
306 1.39 pk return (0);
307 1.14 pk
308 1.14 pk oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
309 1.14 pk oca.ca_ra.ra_len = 0;
310 1.14 pk oca.ca_ra.ra_nreg = 1;
311 1.39 pk oca.ca_ra.ra_iospace = PMAP_OBIO;
312 1.35 pk
313 1.14 pk if (oca.ca_ra.ra_paddr)
314 1.34 pk tmp = (caddr_t)mapdev(oca.ca_ra.ra_reg, TMPMAP_VA, 0, NBPG);
315 1.24 mrg else
316 1.24 mrg tmp = NULL;
317 1.14 pk oca.ca_ra.ra_vaddr = tmp;
318 1.14 pk oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
319 1.39 pk oca.ca_ra.ra_intr[0].int_vec = -1;
320 1.14 pk oca.ca_ra.ra_nintr = 1;
321 1.14 pk oca.ca_ra.ra_name = cf->cf_driver->cd_name;
322 1.39 pk
323 1.15 pk if (ca->ca_ra.ra_bp != NULL &&
324 1.39 pk strcmp(ca->ca_ra.ra_bp->name, "obio") == 0)
325 1.15 pk oca.ca_ra.ra_bp = ca->ca_ra.ra_bp + 1;
326 1.15 pk else
327 1.15 pk oca.ca_ra.ra_bp = NULL;
328 1.39 pk
329 1.39 pk oca.ca_bustype = BUS_OBIO;
330 1.14 pk
331 1.21 thorpej if ((*cf->cf_attach->ca_match)(parent, cf, &oca) == 0)
332 1.39 pk return (0);
333 1.14 pk
334 1.14 pk /*
335 1.39 pk * Check if XXmatch routine replaced the temporary mapping with
336 1.19 chuck * a real mapping. If not, then make sure we don't pass the
337 1.19 chuck * tmp mapping to the attach routine.
338 1.14 pk */
339 1.19 chuck if (oca.ca_ra.ra_vaddr == tmp)
340 1.19 chuck oca.ca_ra.ra_vaddr = NULL; /* wipe out tmp address */
341 1.14 pk /*
342 1.22 pk * the match routine will set "ra_len" if it wants us to
343 1.19 chuck * establish a mapping for it.
344 1.14 pk * (which won't be seen on future XXmatch calls,
345 1.14 pk * so not as useful as it seems.)
346 1.14 pk */
347 1.22 pk if (oca.ca_ra.ra_len)
348 1.14 pk oca.ca_ra.ra_vaddr =
349 1.39 pk obio_bus_map(oca.ca_ra.ra_reg, oca.ca_ra.ra_len);
350 1.5 deraadt
351 1.39 pk config_attach(parent, cf, &oca, obioprint);
352 1.39 pk return (1);
353 1.5 deraadt }
354 1.4 deraadt
355 1.2 deraadt #define getpte(va) lda(va, ASI_PTE)
356 1.2 deraadt
357 1.2 deraadt /*
358 1.2 deraadt * If we can find a mapping that was established by the rom, use it.
359 1.2 deraadt * Else, create a new mapping.
360 1.2 deraadt */
361 1.2 deraadt void *
362 1.39 pk obio_bus_map(pa, len)
363 1.17 pk struct rom_reg *pa;
364 1.2 deraadt int len;
365 1.2 deraadt {
366 1.2 deraadt
367 1.32 pk if (CPU_ISSUN4 && len <= NBPG) {
368 1.32 pk u_long pf = (u_long)(pa->rr_paddr) >> PGSHIFT;
369 1.34 pk int pgtype = PMAP_T2PTE_4(pa->rr_iospace);
370 1.32 pk u_long va, pte;
371 1.32 pk
372 1.3 deraadt for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
373 1.3 deraadt pte = getpte(va);
374 1.4 deraadt if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
375 1.3 deraadt (pte & PG_PFNUM) == pf)
376 1.19 chuck return ((void *)
377 1.19 chuck (va | ((u_long)pa->rr_paddr & PGOFSET)) );
378 1.19 chuck /* note: preserve page offset */
379 1.3 deraadt }
380 1.2 deraadt }
381 1.32 pk
382 1.34 pk return mapiodev(pa, 0, len);
383 1.2 deraadt }
384 1.39 pk #endif /* SUN4 */
385 1.2 deraadt
386 1.2 deraadt void
387 1.39 pk obio_bus_untmp()
388 1.2 deraadt {
389 1.13 mycroft pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
390 1.1 deraadt }
391