sbus.c revision 1.44 1 /* $NetBSD: sbus.c,v 1.44 2001/12/31 15:00:58 uwe 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 static paddr_t sbus_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t,
110 int, int));
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 static paddr_t
581 sbus_bus_mmap(t, baddr, off, prot, flags)
582 bus_space_tag_t t;
583 bus_addr_t baddr;
584 off_t off;
585 int prot;
586 int flags;
587 {
588 struct sbus_softc *sc = t->cookie;
589 int slot = BUS_ADDR_IOSPACE(baddr);
590 int i;
591
592 for (i = 0; i < sc->sc_nrange; i++) {
593 bus_addr_t paddr;
594
595 if (sc->sc_range[i].cspace != slot)
596 continue;
597
598 paddr = BUS_ADDR(sc->sc_range[i].pspace,
599 sc->sc_range[i].poffset + BUS_ADDR_PADDR(baddr));
600 return (bus_space_mmap(sc->sc_bustag, paddr, off,
601 prot, flags));
602 }
603
604 return (-1);
605 }
606
607 bus_addr_t
608 sbus_bus_addr(t, btype, offset)
609 bus_space_tag_t t;
610 u_int btype;
611 u_int offset;
612 {
613
614 /* XXX: sbus_bus_addr should be g/c'ed */
615 return (BUS_ADDR(btype, offset));
616 }
617
618
619 /*
620 * Each attached device calls sbus_establish after it initializes
621 * its sbusdev portion.
622 */
623 void
624 sbus_establish(sd, dev)
625 register struct sbusdev *sd;
626 register struct device *dev;
627 {
628 register struct sbus_softc *sc;
629 register struct device *curdev;
630
631 /*
632 * We have to look for the sbus by name, since it is not necessarily
633 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
634 * We don't just use the device structure of the above-attached
635 * sbus, since we might (in the future) support multiple sbus's.
636 */
637 for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
638 if (!curdev || !curdev->dv_xname)
639 panic("sbus_establish: can't find sbus parent for %s",
640 sd->sd_dev->dv_xname
641 ? sd->sd_dev->dv_xname
642 : "<unknown>" );
643
644 if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
645 break;
646 }
647 sc = (struct sbus_softc *) curdev;
648
649 sd->sd_dev = dev;
650 sd->sd_bchain = sc->sc_sbdev;
651 sc->sc_sbdev = sd;
652 }
653
654 /*
655 * Reset the given sbus. (???)
656 */
657 void
658 sbusreset(sbus)
659 int sbus;
660 {
661 register struct sbusdev *sd;
662 struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
663 struct device *dev;
664
665 printf("reset %s:", sc->sc_dev.dv_xname);
666 for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
667 if (sd->sd_reset) {
668 dev = sd->sd_dev;
669 (*sd->sd_reset)(dev);
670 printf(" %s", dev->dv_xname);
671 }
672 }
673 }
674
675
676 /*
677 * Get interrupt attributes for an Sbus device.
678 */
679 int
680 sbus_get_intr(sc, node, ipp, np)
681 struct sbus_softc *sc;
682 int node;
683 struct sbus_intr **ipp;
684 int *np;
685 {
686 int error, n;
687 u_int32_t *ipl = NULL;
688
689 /*
690 * The `interrupts' property contains the Sbus interrupt level.
691 */
692 if (PROM_getprop(node, "interrupts", sizeof(int), np, (void **)&ipl) == 0) {
693 /* Change format to an `struct sbus_intr' array */
694 struct sbus_intr *ip;
695 ip = malloc(*np * sizeof(struct sbus_intr), M_DEVBUF, M_NOWAIT);
696 if (ip == NULL) {
697 free(ipl, M_DEVBUF);
698 return (ENOMEM);
699 }
700 for (n = 0; n < *np; n++) {
701 ip[n].sbi_pri = ipl[n];
702 ip[n].sbi_vec = 0;
703 }
704 free(ipl, M_DEVBUF);
705 *ipp = ip;
706 return (0);
707 }
708
709 /*
710 * Fall back on `intr' property.
711 */
712 *ipp = NULL;
713 error = PROM_getprop(node, "intr", sizeof(struct sbus_intr),
714 np, (void **)ipp);
715 switch (error) {
716 case 0:
717 for (n = *np; n-- > 0;) {
718 (*ipp)[n].sbi_pri &= 0xf;
719 (*ipp)[n].sbi_pri |= SBUS_INTR_COMPAT;
720 }
721 break;
722 case ENOENT:
723 error = 0;
724 break;
725 }
726
727 return (error);
728 }
729
730
731 /*
732 * Install an interrupt handler for an Sbus device.
733 */
734 void *
735 sbus_intr_establish(t, pri, level, flags, handler, arg)
736 bus_space_tag_t t;
737 int pri;
738 int level;
739 int flags;
740 int (*handler) __P((void *));
741 void *arg;
742 {
743 struct sbus_softc *sc = t->cookie;
744 struct intrhand *ih;
745 int pil;
746
747 ih = (struct intrhand *)
748 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
749 if (ih == NULL)
750 return (NULL);
751
752 /*
753 * Translate Sbus interrupt priority to CPU interrupt level
754 */
755 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
756 pil = pri;
757 else if ((pri & SBUS_INTR_COMPAT) != 0)
758 pil = pri & ~SBUS_INTR_COMPAT;
759 else
760 pil = sc->sc_intr2ipl[pri];
761
762 ih->ih_fun = handler;
763 ih->ih_arg = arg;
764 if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
765 intr_fasttrap(pil, (void (*)__P((void)))handler);
766 else
767 intr_establish(pil, ih);
768 return (ih);
769 }
770
771 static bus_space_tag_t
772 sbus_alloc_bustag(sc)
773 struct sbus_softc *sc;
774 {
775 bus_space_tag_t sbt;
776
777 sbt = (bus_space_tag_t)
778 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
779 if (sbt == NULL)
780 return (NULL);
781
782 bzero(sbt, sizeof *sbt);
783 sbt->cookie = sc;
784 sbt->parent = sc->sc_bustag;
785 sbt->sparc_bus_map = _sbus_bus_map;
786 sbt->sparc_bus_mmap = sbus_bus_mmap;
787 sbt->sparc_intr_establish = sbus_intr_establish;
788 return (sbt);
789 }
790
791 int
792 sbus_error()
793 {
794 struct sbus_softc *sc = sbus_sc;
795 bus_space_handle_t bh = sc->sc_bh;
796 u_int32_t afsr, afva;
797 char bits[64];
798 static int straytime, nstray;
799 int timesince;
800
801 afsr = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFSR_REG);
802 afva = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFAR_REG);
803 printf("sbus error:\n\tAFSR %s\n",
804 bitmask_snprintf(afsr, SBUS_AFSR_BITS, bits, sizeof(bits)));
805 printf("\taddress: 0x%x%x\n", afsr & SBUS_AFSR_PAH, afva);
806
807 /* For now, do the same dance as on stray interrupts */
808 timesince = time.tv_sec - straytime;
809 if (timesince <= 10) {
810 if (++nstray > 9)
811 panic("too many SBus errors");
812 } else {
813 straytime = time.tv_sec;
814 nstray = 1;
815 }
816
817 /* Unlock registers and clear interrupt */
818 bus_space_write_4(sc->sc_bustag, bh, SBUS_AFSR_REG, afsr);
819
820 return (0);
821 }
822