xref: /netbsd-src/sys/arch/sparc/dev/sbus.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: sbus.c,v 1.42 2001/10/05 13:32:23 mrg Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1992, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This software was developed by the Computer Systems Engineering group
44  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45  * contributed to Berkeley.
46  *
47  * All advertising materials mentioning features or use of this software
48  * must display the following acknowledgement:
49  *	This product includes software developed by the University of
50  *	California, Lawrence Berkeley Laboratory.
51  *
52  * Redistribution and use in source and binary forms, with or without
53  * modification, are permitted provided that the following conditions
54  * are met:
55  * 1. Redistributions of source code must retain the above copyright
56  *    notice, this list of conditions and the following disclaimer.
57  * 2. Redistributions in binary form must reproduce the above copyright
58  *    notice, this list of conditions and the following disclaimer in the
59  *    documentation and/or other materials provided with the distribution.
60  * 3. All advertising materials mentioning features or use of this software
61  *    must display the following acknowledgement:
62  *	This product includes software developed by the University of
63  *	California, Berkeley and its contributors.
64  * 4. Neither the name of the University nor the names of its contributors
65  *    may be used to endorse or promote products derived from this software
66  *    without specific prior written permission.
67  *
68  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78  * SUCH DAMAGE.
79  *
80  *	@(#)sbus.c	8.1 (Berkeley) 6/11/93
81  */
82 
83 /*
84  * Sbus stuff.
85  */
86 
87 #include <sys/param.h>
88 #include <sys/malloc.h>
89 #include <sys/kernel.h>
90 #include <sys/systm.h>
91 #include <sys/device.h>
92 
93 #include <uvm/uvm_extern.h>
94 
95 #include <machine/bus.h>
96 #include <sparc/dev/sbusreg.h>
97 #include <dev/sbus/sbusvar.h>
98 #include <dev/sbus/xboxvar.h>
99 
100 #include <sparc/sparc/iommuvar.h>
101 #include <machine/autoconf.h>
102 
103 
104 void sbusreset __P((int));
105 
106 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
107 static int sbus_get_intr __P((struct sbus_softc *, int,
108 			      struct sbus_intr **, int *));
109 int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
110 			      int, bus_space_handle_t *));
111 static int _sbus_bus_map __P((
112 		bus_space_tag_t,
113 		bus_type_t,		/*slot*/
114 		bus_addr_t,		/*offset*/
115 		bus_size_t,		/*size*/
116 		int,			/*flags*/
117 		vaddr_t,		/*preferred virtual address */
118 		bus_space_handle_t *));
119 static void *sbus_intr_establish __P((
120 		bus_space_tag_t,
121 		int,			/*Sbus interrupt level*/
122 		int,			/*`device class' priority*/
123 		int,			/*flags*/
124 		int (*) __P((void *)),	/*handler*/
125 		void *));		/*handler arg*/
126 
127 
128 /* autoconfiguration driver */
129 int	sbus_match_mainbus __P((struct device *, struct cfdata *, void *));
130 int	sbus_match_iommu __P((struct device *, struct cfdata *, void *));
131 int	sbus_match_xbox __P((struct device *, struct cfdata *, void *));
132 void	sbus_attach_mainbus __P((struct device *, struct device *, void *));
133 void	sbus_attach_iommu __P((struct device *, struct device *, void *));
134 void	sbus_attach_xbox __P((struct device *, struct device *, void *));
135 
136 static	int sbus_error __P((void));
137 int	(*sbuserr_handler) __P((void));
138 
139 struct cfattach sbus_mainbus_ca = {
140 	sizeof(struct sbus_softc), sbus_match_mainbus, sbus_attach_mainbus
141 };
142 struct cfattach sbus_iommu_ca = {
143 	sizeof(struct sbus_softc), sbus_match_iommu, sbus_attach_iommu
144 };
145 struct cfattach sbus_xbox_ca = {
146 	sizeof(struct sbus_softc), sbus_match_xbox, sbus_attach_xbox
147 };
148 
149 extern struct cfdriver sbus_cd;
150 
151 /* The "primary" Sbus */
152 struct sbus_softc *sbus_sc;
153 
154 /* If the PROM does not provide the `ranges' property, we make up our own */
155 struct sbus_range sbus_translations[] = {
156 	/* Assume a maximum of 4 Sbus slots, all mapped to on-board io space */
157 	{ 0, 0, PMAP_OBIO, SBUS_ADDR(0,0), 1 << 25 },
158 	{ 1, 0, PMAP_OBIO, SBUS_ADDR(1,0), 1 << 25 },
159 	{ 2, 0, PMAP_OBIO, SBUS_ADDR(2,0), 1 << 25 },
160 	{ 3, 0, PMAP_OBIO, SBUS_ADDR(3,0), 1 << 25 }
161 };
162 
163 /*
164  * Child devices receive the Sbus interrupt level in their attach
165  * arguments. We translate these to CPU IPLs using the following
166  * tables. Note: obio bus interrupt levels are identical to the
167  * processor IPL.
168  *
169  * The second set of tables is used when the Sbus interrupt level
170  * cannot be had from the PROM as an `interrupt' property. We then
171  * fall back on the `intr' property which contains the CPU IPL.
172  */
173 
174 /* Translate Sbus interrupt level to processor IPL */
175 static int intr_sbus2ipl_4c[] = {
176 	0, 1, 2, 3, 5, 7, 8, 9
177 };
178 static int intr_sbus2ipl_4m[] = {
179 	0, 2, 3, 5, 7, 9, 11, 13
180 };
181 
182 /*
183  * This value is or'ed into the attach args' interrupt level cookie
184  * if the interrupt level comes from an `intr' property, i.e. it is
185  * not an Sbus interrupt level.
186  */
187 #define SBUS_INTR_COMPAT	0x80000000
188 
189 
190 /*
191  * Print the location of some sbus-attached device (called just
192  * before attaching that device).  If `sbus' is not NULL, the
193  * device was found but not configured; print the sbus as well.
194  * Return UNCONF (config_find ignores this if the device was configured).
195  */
196 int
197 sbus_print(args, busname)
198 	void *args;
199 	const char *busname;
200 {
201 	struct sbus_attach_args *sa = args;
202 	int i;
203 
204 	if (busname)
205 		printf("%s at %s", sa->sa_name, busname);
206 	printf(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset);
207 	for (i = 0; i < sa->sa_nintr; i++) {
208 		u_int32_t level = sa->sa_intr[i].sbi_pri;
209 		struct sbus_softc *sc =
210 			(struct sbus_softc *) sa->sa_bustag->cookie;
211 
212 		printf(" level %d", level & ~SBUS_INTR_COMPAT);
213 		if ((level & SBUS_INTR_COMPAT) == 0) {
214 			int ipl = sc->sc_intr2ipl[level];
215 			if (ipl != level)
216 				printf(" (ipl %d)", ipl);
217 		}
218 	}
219 	return (UNCONF);
220 }
221 
222 int
223 sbus_match_mainbus(parent, cf, aux)
224 	struct device *parent;
225 	struct cfdata *cf;
226 	void *aux;
227 {
228 	struct mainbus_attach_args *ma = aux;
229 
230 	if (CPU_ISSUN4)
231 		return (0);
232 
233 	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
234 }
235 
236 int
237 sbus_match_iommu(parent, cf, aux)
238 	struct device *parent;
239 	struct cfdata *cf;
240 	void *aux;
241 {
242 	struct iommu_attach_args *ia = aux;
243 
244 	if (CPU_ISSUN4)
245 		return (0);
246 
247 	return (strcmp(cf->cf_driver->cd_name, ia->iom_name) == 0);
248 }
249 
250 int
251 sbus_match_xbox(parent, cf, aux)
252 	struct device *parent;
253 	struct cfdata *cf;
254 	void *aux;
255 {
256 	struct xbox_attach_args *xa = aux;
257 
258 	if (CPU_ISSUN4)
259 		return (0);
260 
261 	return (strcmp(cf->cf_driver->cd_name, xa->xa_name) == 0);
262 }
263 
264 /*
265  * Attach an Sbus.
266  */
267 void
268 sbus_attach_mainbus(parent, self, aux)
269 	struct device *parent;
270 	struct device *self;
271 	void *aux;
272 {
273 	struct sbus_softc *sc = (struct sbus_softc *)self;
274 	struct mainbus_attach_args *ma = aux;
275 	int node = ma->ma_node;
276 
277 	/*
278 	 * XXX there is only one Sbus, for now -- do not know how to
279 	 * address children on others
280 	 */
281 	if (sc->sc_dev.dv_unit > 0) {
282 		printf(" unsupported\n");
283 		return;
284 	}
285 
286 	sc->sc_bustag = ma->ma_bustag;
287 	sc->sc_dmatag = ma->ma_dmatag;
288 
289 #if 0	/* sbus at mainbus (sun4c): `reg' prop is not control space */
290 	if (ma->ma_size == 0)
291 		printf("%s: no Sbus registers", self->dv_xname);
292 
293 	if (bus_space_map2(ma->ma_bustag,
294 			  (bus_type_t)ma->ma_iospace,
295 			  (bus_addr_t)ma->ma_paddr,
296 			  (bus_size_t)ma->ma_size,
297 			  BUS_SPACE_MAP_LINEAR,
298 			  0, &sc->sc_bh) != 0) {
299 		panic("%s: can't map sbusbusreg", self->dv_xname);
300 	}
301 #endif
302 
303 	/* Setup interrupt translation tables */
304 	sc->sc_intr2ipl = CPU_ISSUN4C
305 				? intr_sbus2ipl_4c
306 				: intr_sbus2ipl_4m;
307 
308 	/*
309 	 * Record clock frequency for synchronous SCSI.
310 	 * IS THIS THE CORRECT DEFAULT??
311 	 */
312 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
313 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
314 
315 	sbus_sc = sc;
316 	sbus_attach_common(sc, "sbus", node, NULL);
317 }
318 
319 
320 void
321 sbus_attach_iommu(parent, self, aux)
322 	struct device *parent;
323 	struct device *self;
324 	void *aux;
325 {
326 	struct sbus_softc *sc = (struct sbus_softc *)self;
327 	struct iommu_attach_args *ia = aux;
328 	int node = ia->iom_node;
329 
330 	sc->sc_bustag = ia->iom_bustag;
331 	sc->sc_dmatag = ia->iom_dmatag;
332 
333 	if (ia->iom_nreg == 0)
334 		panic("%s: no Sbus registers", self->dv_xname);
335 
336 	if (bus_space_map2(ia->iom_bustag,
337 			  (bus_type_t)ia->iom_reg[0].ior_iospace,
338 			  (bus_addr_t)ia->iom_reg[0].ior_pa,
339 			  (bus_size_t)ia->iom_reg[0].ior_size,
340 			  BUS_SPACE_MAP_LINEAR,
341 			  0, &sc->sc_bh) != 0) {
342 		panic("%s: can't map sbusbusreg", self->dv_xname);
343 	}
344 
345 	/* Setup interrupt translation tables */
346 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
347 
348 	/*
349 	 * Record clock frequency for synchronous SCSI.
350 	 * IS THIS THE CORRECT DEFAULT??
351 	 */
352 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
353 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
354 
355 	sbus_sc = sc;
356 	sbuserr_handler = sbus_error;
357 	sbus_attach_common(sc, "sbus", node, NULL);
358 }
359 
360 void
361 sbus_attach_xbox(parent, self, aux)
362 	struct device *parent;
363 	struct device *self;
364 	void *aux;
365 {
366 	struct sbus_softc *sc = (struct sbus_softc *)self;
367 	struct xbox_attach_args *xa = aux;
368 	int node = xa->xa_node;
369 
370 	sc->sc_bustag = xa->xa_bustag;
371 	sc->sc_dmatag = xa->xa_dmatag;
372 
373 	/* Setup interrupt translation tables */
374 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
375 
376 	/*
377 	 * Record clock frequency for synchronous SCSI.
378 	 * IS THIS THE CORRECT DEFAULT??
379 	 */
380 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
381 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
382 
383 	sbus_attach_common(sc, "sbus", node, NULL);
384 }
385 
386 void
387 sbus_attach_common(sc, busname, busnode, specials)
388 	struct sbus_softc *sc;
389 	char *busname;
390 	int busnode;
391 	const char * const *specials;
392 {
393 	int node0, node, error;
394 	const char *sp;
395 	const char *const *ssp;
396 	bus_space_tag_t sbt;
397 	struct sbus_attach_args sa;
398 
399 	sbt = sbus_alloc_bustag(sc);
400 
401 	/*
402 	 * Get the SBus burst transfer size if burst transfers are supported
403 	 */
404 	sc->sc_burst = PROM_getpropint(busnode, "burst-sizes", 0);
405 
406 
407 	if (CPU_ISSUN4M) {
408 		/*
409 		 * Some models (e.g. SS20) erroneously report 64-bit
410 		 * burst capability. We mask it out here for all SUN4Ms,
411 		 * since probably no member of that class supports
412 		 * 64-bit Sbus bursts.
413 		 */
414 		sc->sc_burst &= ~SBUS_BURST_64;
415 	}
416 
417 	/*
418 	 * Collect address translations from the OBP.
419 	 */
420 	error = PROM_getprop(busnode, "ranges", sizeof(struct rom_range),
421 			&sc->sc_nrange, (void **)&sc->sc_range);
422 	switch (error) {
423 	case 0:
424 		break;
425 	case ENOENT:
426 		/* Fall back to our own `range' construction */
427 		sc->sc_range = sbus_translations;
428 		sc->sc_nrange =
429 			sizeof(sbus_translations)/sizeof(sbus_translations[0]);
430 		break;
431 	default:
432 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
433 	}
434 
435 	/*
436 	 * Loop through ROM children, fixing any relative addresses
437 	 * and then configuring each device.
438 	 * `specials' is an array of device names that are treated
439 	 * specially:
440 	 */
441 	node0 = firstchild(busnode);
442 	for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
443 		if ((node = findnode(node0, sp)) == 0) {
444 			panic("could not find %s amongst %s devices",
445 				sp, busname);
446 		}
447 
448 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
449 					   node, &sa) != 0) {
450 			panic("sbus_attach: %s: incomplete", sp);
451 		}
452 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
453 		sbus_destroy_attach_args(&sa);
454 	}
455 
456 	for (node = node0; node; node = nextsibling(node)) {
457 		char *name = PROM_getpropstring(node, "name");
458 		for (ssp = specials, sp = NULL;
459 		     ssp != NULL && (sp = *ssp) != NULL;
460 		     ssp++)
461 			if (strcmp(name, sp) == 0)
462 				break;
463 
464 		if (sp != NULL)
465 			/* Already configured as an "early" device */
466 			continue;
467 
468 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
469 					   node, &sa) != 0) {
470 			printf("sbus_attach: %s: incomplete\n", name);
471 			continue;
472 		}
473 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
474 		sbus_destroy_attach_args(&sa);
475 	}
476 }
477 
478 int
479 sbus_setup_attach_args(sc, bustag, dmatag, node, sa)
480 	struct sbus_softc	*sc;
481 	bus_space_tag_t		bustag;
482 	bus_dma_tag_t		dmatag;
483 	int			node;
484 	struct sbus_attach_args	*sa;
485 {
486 	int n, error;
487 
488 	bzero(sa, sizeof(struct sbus_attach_args));
489 	error = PROM_getprop(node, "name", 1, &n, (void **)&sa->sa_name);
490 	if (error != 0)
491 		return (error);
492 	sa->sa_name[n] = '\0';
493 
494 	sa->sa_bustag = bustag;
495 	sa->sa_dmatag = dmatag;
496 	sa->sa_node = node;
497 	sa->sa_frequency = sc->sc_clockfreq;
498 
499 	error = PROM_getprop(node, "reg", sizeof(struct sbus_reg),
500 			&sa->sa_nreg, (void **)&sa->sa_reg);
501 	if (error != 0) {
502 		char buf[32];
503 		if (error != ENOENT ||
504 		    !node_has_property(node, "device_type") ||
505 		    strcmp(PROM_getpropstringA(node, "device_type", buf, sizeof buf),
506 			   "hierarchical") != 0)
507 			return (error);
508 	}
509 	for (n = 0; n < sa->sa_nreg; n++) {
510 		/* Convert to relative addressing, if necessary */
511 		u_int32_t base = sa->sa_reg[n].sbr_offset;
512 		if (SBUS_ABS(base)) {
513 			sa->sa_reg[n].sbr_slot = SBUS_ABS_TO_SLOT(base);
514 			sa->sa_reg[n].sbr_offset = SBUS_ABS_TO_OFFSET(base);
515 		}
516 	}
517 
518 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0)
519 		return (error);
520 
521 	error = PROM_getprop(node, "address", sizeof(u_int32_t),
522 			 &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs);
523 	if (error != 0 && error != ENOENT)
524 		return (error);
525 
526 	return (0);
527 }
528 
529 void
530 sbus_destroy_attach_args(sa)
531 	struct sbus_attach_args	*sa;
532 {
533 	if (sa->sa_name != NULL)
534 		free(sa->sa_name, M_DEVBUF);
535 
536 	if (sa->sa_nreg != 0)
537 		free(sa->sa_reg, M_DEVBUF);
538 
539 	if (sa->sa_intr)
540 		free(sa->sa_intr, M_DEVBUF);
541 
542 	if (sa->sa_promvaddrs)
543 		free(sa->sa_promvaddrs, M_DEVBUF);
544 
545 	bzero(sa, sizeof(struct sbus_attach_args));/*DEBUG*/
546 }
547 
548 
549 int
550 _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp)
551 	bus_space_tag_t t;
552 	bus_type_t btype;
553 	bus_addr_t offset;
554 	bus_size_t size;
555 	int	flags;
556 	vaddr_t vaddr;
557 	bus_space_handle_t *hp;
558 {
559 	struct sbus_softc *sc = t->cookie;
560 	int slot = btype;
561 	int i;
562 
563 	for (i = 0; i < sc->sc_nrange; i++) {
564 		bus_addr_t paddr;
565 		bus_type_t iospace;
566 
567 		if (sc->sc_range[i].cspace != slot)
568 			continue;
569 
570 		/* We've found the connection to the parent bus */
571 		paddr = sc->sc_range[i].poffset + BUS_ADDR_PADDR(offset);
572 		iospace = sc->sc_range[i].pspace;
573 		return (bus_space_map2(sc->sc_bustag, iospace, paddr,
574 					size, flags, vaddr, hp));
575 	}
576 
577 	return (EINVAL);
578 }
579 
580 int
581 sbus_bus_mmap(t, btype, paddr, flags, hp)
582 	bus_space_tag_t t;
583 	bus_type_t btype;
584 	bus_addr_t paddr;
585 	int flags;
586 	bus_space_handle_t *hp;
587 {
588 	int slot = (int)btype;
589 	int offset = (int)paddr;
590 	struct sbus_softc *sc = t->cookie;
591 	int i;
592 
593 	for (i = 0; i < sc->sc_nrange; i++) {
594 		bus_addr_t paddr;
595 
596 		if (sc->sc_range[i].cspace != slot)
597 			continue;
598 
599 		paddr = BUS_ADDR(sc->sc_range[i].pspace,
600 			sc->sc_range[i].poffset + offset);
601 		if ((*hp = bus_space_mmap(sc->sc_bustag, paddr, 0,
602 			0/*prot unused*/, flags)) != -1)
603 			return (0);
604 	}
605 
606 	return (-1);
607 }
608 
609 bus_addr_t
610 sbus_bus_addr(t, btype, offset)
611 	bus_space_tag_t t;
612 	u_int btype;
613 	u_int offset;
614 {
615 	int slot = (int)btype;
616 	struct sbus_softc *sc = t->cookie;
617 	int i;
618 
619 	for (i = 0; i < sc->sc_nrange; i++) {
620 		bus_addr_t baddr;
621 		bus_addr_t iospace;
622 
623 		if (sc->sc_range[i].cspace != slot)
624 			continue;
625 
626 		baddr = sc->sc_range[i].poffset + offset;
627 		iospace = (bus_addr_t)sc->sc_range[i].pspace;
628 		baddr = baddr|(iospace<<32);
629 		return (baddr);
630 	}
631 	return (-1);
632 }
633 
634 
635 /*
636  * Each attached device calls sbus_establish after it initializes
637  * its sbusdev portion.
638  */
639 void
640 sbus_establish(sd, dev)
641 	register struct sbusdev *sd;
642 	register struct device *dev;
643 {
644 	register struct sbus_softc *sc;
645 	register struct device *curdev;
646 
647 	/*
648 	 * We have to look for the sbus by name, since it is not necessarily
649 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
650 	 * We don't just use the device structure of the above-attached
651 	 * sbus, since we might (in the future) support multiple sbus's.
652 	 */
653 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
654 		if (!curdev || !curdev->dv_xname)
655 			panic("sbus_establish: can't find sbus parent for %s",
656 			      sd->sd_dev->dv_xname
657 					? sd->sd_dev->dv_xname
658 					: "<unknown>" );
659 
660 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
661 			break;
662 	}
663 	sc = (struct sbus_softc *) curdev;
664 
665 	sd->sd_dev = dev;
666 	sd->sd_bchain = sc->sc_sbdev;
667 	sc->sc_sbdev = sd;
668 }
669 
670 /*
671  * Reset the given sbus. (???)
672  */
673 void
674 sbusreset(sbus)
675 	int sbus;
676 {
677 	register struct sbusdev *sd;
678 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
679 	struct device *dev;
680 
681 	printf("reset %s:", sc->sc_dev.dv_xname);
682 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
683 		if (sd->sd_reset) {
684 			dev = sd->sd_dev;
685 			(*sd->sd_reset)(dev);
686 			printf(" %s", dev->dv_xname);
687 		}
688 	}
689 }
690 
691 
692 /*
693  * Get interrupt attributes for an Sbus device.
694  */
695 int
696 sbus_get_intr(sc, node, ipp, np)
697 	struct sbus_softc *sc;
698 	int node;
699 	struct sbus_intr **ipp;
700 	int *np;
701 {
702 	int error, n;
703 	u_int32_t *ipl = NULL;
704 
705 	/*
706 	 * The `interrupts' property contains the Sbus interrupt level.
707 	 */
708 	if (PROM_getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) {
709 		/* Change format to an `struct sbus_intr' array */
710 		struct sbus_intr *ip;
711 		ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT);
712 		if (ip == NULL) {
713 			free(ipl, M_DEVBUF);
714 			return (ENOMEM);
715 		}
716 		for (n = 0; n < *np; n++) {
717 			ip[n].sbi_pri = ipl[n];
718 			ip[n].sbi_vec = 0;
719 		}
720 		free(ipl, M_DEVBUF);
721 		*ipp = ip;
722 		return (0);
723 	}
724 
725 	/*
726 	 * Fall back on `intr' property.
727 	 */
728 	*ipp = NULL;
729 	error = PROM_getprop(node, "intr", sizeof(struct sbus_intr),
730 			np, (void **)ipp);
731 	switch (error) {
732 	case 0:
733 		for (n = *np; n-- > 0;) {
734 			(*ipp)[n].sbi_pri &= 0xf;
735 			(*ipp)[n].sbi_pri |= SBUS_INTR_COMPAT;
736 		}
737 		break;
738 	case ENOENT:
739 		error = 0;
740 		break;
741 	}
742 
743 	return (error);
744 }
745 
746 
747 /*
748  * Install an interrupt handler for an Sbus device.
749  */
750 void *
751 sbus_intr_establish(t, pri, level, flags, handler, arg)
752 	bus_space_tag_t t;
753 	int pri;
754 	int level;
755 	int flags;
756 	int (*handler) __P((void *));
757 	void *arg;
758 {
759 	struct sbus_softc *sc = t->cookie;
760 	struct intrhand *ih;
761 	int pil;
762 
763 	ih = (struct intrhand *)
764 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
765 	if (ih == NULL)
766 		return (NULL);
767 
768 	/*
769 	 * Translate Sbus interrupt priority to CPU interrupt level
770 	 */
771 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
772 		pil = pri;
773 	else if ((pri & SBUS_INTR_COMPAT) != 0)
774 		pil = pri & ~SBUS_INTR_COMPAT;
775 	else
776 		pil = sc->sc_intr2ipl[pri];
777 
778 	ih->ih_fun = handler;
779 	ih->ih_arg = arg;
780 	if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
781 		intr_fasttrap(pil, (void (*)__P((void)))handler);
782 	else
783 		intr_establish(pil, ih);
784 	return (ih);
785 }
786 
787 static bus_space_tag_t
788 sbus_alloc_bustag(sc)
789 	struct sbus_softc *sc;
790 {
791 	bus_space_tag_t sbt;
792 
793 	sbt = (bus_space_tag_t)
794 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
795 	if (sbt == NULL)
796 		return (NULL);
797 
798 	bzero(sbt, sizeof *sbt);
799 	sbt->cookie = sc;
800 	sbt->parent = sc->sc_bustag;
801 	sbt->sparc_bus_map = _sbus_bus_map;
802 	sbt->sparc_bus_mmap = sc->sc_bustag->sparc_bus_mmap;
803 	sbt->sparc_intr_establish = sbus_intr_establish;
804 	return (sbt);
805 }
806 
807 int
808 sbus_error()
809 {
810 	struct sbus_softc *sc = sbus_sc;
811 	bus_space_handle_t bh = sc->sc_bh;
812 	u_int32_t afsr, afva;
813 	char bits[64];
814 static	int straytime, nstray;
815 	int timesince;
816 
817 	afsr = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFSR_REG);
818 	afva = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFAR_REG);
819 	printf("sbus error:\n\tAFSR %s\n",
820 		bitmask_snprintf(afsr, SBUS_AFSR_BITS, bits, sizeof(bits)));
821 	printf("\taddress: 0x%x%x\n", afsr & SBUS_AFSR_PAH, afva);
822 
823 	/* For now, do the same dance as on stray interrupts */
824 	timesince = time.tv_sec - straytime;
825 	if (timesince <= 10) {
826 		if (++nstray > 9)
827 			panic("too many SBus errors");
828 	} else {
829 		straytime = time.tv_sec;
830 		nstray = 1;
831 	}
832 
833 	/* Unlock registers and clear interrupt */
834 	bus_space_write_4(sc->sc_bustag, bh, SBUS_AFSR_REG, afsr);
835 
836 	return (0);
837 }
838