sbus.c revision 1.20 1 /* $NetBSD: sbus.c,v 1.20 1998/03/21 22:03:33 pk 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/systm.h>
90 #include <sys/device.h>
91 #include <vm/vm.h>
92
93 #include <machine/bus.h>
94 #include <sparc/dev/sbusreg.h>
95 #include <sparc/dev/sbusvar.h>
96
97 #include <sparc/sparc/iommuvar.h>
98 #include <machine/autoconf.h>
99
100
101 void sbusreset __P((int));
102
103 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
104 static int sbus_get_intr __P((struct sbus_softc *, int, int *));
105 static int sbus_bus_mmap __P((void *, bus_type_t, bus_addr_t, int));
106 static int _sbus_bus_map __P((
107 void *, /*cookie*/
108 bus_type_t, /*slot*/
109 bus_addr_t, /*offset*/
110 bus_size_t, /*size*/
111 int, /*flags*/
112 vm_offset_t, /*preferred virtual address */
113 bus_space_handle_t *));
114
115
116 /* autoconfiguration driver */
117 int sbus_match_mainbus __P((struct device *, struct cfdata *, void *));
118 int sbus_match_iommu __P((struct device *, struct cfdata *, void *));
119 void sbus_attach_mainbus __P((struct device *, struct device *, void *));
120 void sbus_attach_iommu __P((struct device *, struct device *, void *));
121
122 struct cfattach sbus_mainbus_ca = {
123 sizeof(struct sbus_softc), sbus_match_mainbus, sbus_attach_mainbus
124 };
125 struct cfattach sbus_iommu_ca = {
126 sizeof(struct sbus_softc), sbus_match_iommu, sbus_attach_iommu
127 };
128
129 extern struct cfdriver sbus_cd;
130
131 /* If the PROM does not provide the `ranges' property, we make up our own */
132 struct rom_range sbus_translations[] = {
133 /* Assume a maximum of 4 Sbus slots, all mapped to on-board io space */
134 { 0, 0, PMAP_OBIO, SBUS_ADDR(0,0), 1 << 25 },
135 { 1, 0, PMAP_OBIO, SBUS_ADDR(1,0), 1 << 25 },
136 { 2, 0, PMAP_OBIO, SBUS_ADDR(2,0), 1 << 25 },
137 { 3, 0, PMAP_OBIO, SBUS_ADDR(3,0), 1 << 25 }
138 };
139
140 /*
141 * Child devices receive the Sbus interrupt level in their attach
142 * arguments. We translate these to CPU IPLs using the following
143 * tables. Note: obio bus interrupt levels are identical to the
144 * processor IPL.
145 *
146 * The second set of tables is used when the Sbus interrupt level
147 * cannot be had from the PROM as an `interrupt' property. We then
148 * fall back on the `intr' property which contains the CPU IPL.
149 */
150
151 /* Translate Sbus interrupt level to processor IPL */
152 static int intr_sbus2ipl_4c[] = {
153 0, 1, 2, 3, 5, 7, 8, 9
154 };
155 static int intr_sbus2ipl_4m[] = {
156 0, 2, 3, 5, 7, 9, 11, 13
157 };
158
159 /*
160 * This value is or'ed into the attach args' interrupt level cookie
161 * if the interrupt level comes from an `intr' property, i.e. it is
162 * not an Sbus interrupt level.
163 */
164 #define SBUS_INTR_COMPAT 0x80000000
165
166
167 /*
168 * Print the location of some sbus-attached device (called just
169 * before attaching that device). If `sbus' is not NULL, the
170 * device was found but not configured; print the sbus as well.
171 * Return UNCONF (config_find ignores this if the device was configured).
172 */
173 int
174 sbus_print(args, busname)
175 void *args;
176 const char *busname;
177 {
178 struct sbus_attach_args *sa = args;
179
180 if (busname)
181 printf("%s at %s", sa->sa_name, busname);
182 printf(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset);
183 if (sa->sa_pri) {
184 int level = sa->sa_pri;
185 struct sbus_softc *sc =
186 (struct sbus_softc *) sa->sa_bustag->cookie;
187
188 printf(" level %d", level & ~SBUS_INTR_COMPAT);
189 if ((level & SBUS_INTR_COMPAT) == 0) {
190 int ipl = sc->sc_intr2ipl[level];
191 if (ipl != level)
192 printf(" (ipl %d)", ipl);
193 }
194 }
195 return (UNCONF);
196 }
197
198 int
199 sbus_match_mainbus(parent, cf, aux)
200 struct device *parent;
201 struct cfdata *cf;
202 void *aux;
203 {
204 struct mainbus_attach_args *ma = aux;
205
206 if (CPU_ISSUN4)
207 return (0);
208
209 return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
210 }
211
212 int
213 sbus_match_iommu(parent, cf, aux)
214 struct device *parent;
215 struct cfdata *cf;
216 void *aux;
217 {
218 struct iommu_attach_args *ia = aux;
219
220 if (CPU_ISSUN4)
221 return (0);
222
223 return (strcmp(cf->cf_driver->cd_name, ia->iom_name) == 0);
224 }
225
226 /*
227 * Attach an Sbus.
228 */
229 void
230 sbus_attach_mainbus(parent, self, aux)
231 struct device *parent;
232 struct device *self;
233 void *aux;
234 {
235 struct sbus_softc *sc = (struct sbus_softc *)self;
236 struct mainbus_attach_args *ma = aux;
237 int node = ma->ma_node;
238
239 /*
240 * XXX there is only one Sbus, for now -- do not know how to
241 * address children on others
242 */
243 if (sc->sc_dev.dv_unit > 0) {
244 printf(" unsupported\n");
245 return;
246 }
247
248 sc->sc_bustag = ma->ma_bustag;
249 sc->sc_dmatag = ma->ma_dmatag;
250
251 /* Setup interrupt translation tables */
252 sc->sc_intr2ipl = CPU_ISSUN4C
253 ? intr_sbus2ipl_4c
254 : intr_sbus2ipl_4m;
255
256 /*
257 * Record clock frequency for synchronous SCSI.
258 * IS THIS THE CORRECT DEFAULT??
259 */
260 sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
261 printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
262
263 sbus_attach(sc, "sbus", node, ma->ma_bp, NULL);
264 }
265
266 void
267 sbus_attach_iommu(parent, self, aux)
268 struct device *parent;
269 struct device *self;
270 void *aux;
271 {
272 struct sbus_softc *sc = (struct sbus_softc *)self;
273 struct iommu_attach_args *ia = aux;
274 int node = ia->iom_node;
275
276 sc->sc_bustag = ia->iom_bustag;
277 sc->sc_dmatag = ia->iom_dmatag;
278
279 /* Setup interrupt translation tables */
280 sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
281
282 /*
283 * Record clock frequency for synchronous SCSI.
284 * IS THIS THE CORRECT DEFAULT??
285 */
286 sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
287 printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
288
289 sbus_attach(sc, "sbus", node, ia->iom_bp, NULL);
290 }
291
292 void
293 sbus_attach(sc, busname, busnode, bp, specials)
294 struct sbus_softc *sc;
295 char *busname;
296 int busnode;
297 struct bootpath *bp;
298 const char * const *specials;
299 {
300 int node0, node, error;
301 const char *sp;
302 const char *const *ssp;
303 bus_space_tag_t sbt;
304 struct sbus_attach_args sa;
305
306 sbt = sbus_alloc_bustag(sc);
307
308 /*
309 * Get the SBus burst transfer size if burst transfers are supported
310 */
311 sc->sc_burst = getpropint(busnode, "burst-sizes", 0);
312
313 /* Propagate bootpath */
314 if (bp != NULL && strcmp(bp->name, busname) == 0)
315 bp++;
316 else
317 bp = NULL;
318
319 /*
320 * Collect address translations from the OBP.
321 */
322 error = getpropA(busnode, "ranges", sizeof(struct rom_range),
323 &sc->sc_nrange, (void **)&sc->sc_range);
324 switch (error) {
325 case 0:
326 break;
327 case ENOENT:
328 /* Fall back to our own `range' construction */
329 sc->sc_range = sbus_translations;
330 sc->sc_nrange =
331 sizeof(sbus_translations)/sizeof(sbus_translations[0]);
332 break;
333 default:
334 panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
335 }
336
337 /*
338 * Loop through ROM children, fixing any relative addresses
339 * and then configuring each device.
340 * `specials' is an array of device names that are treated
341 * specially:
342 */
343 node0 = firstchild(busnode);
344 for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
345 if ((node = findnode(node0, sp)) == 0) {
346 panic("could not find %s amongst %s devices",
347 sp, busname);
348 }
349
350 if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
351 node, bp, &sa) != 0) {
352 panic("sbus_attach: %s: incomplete", sp);
353 }
354 (void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
355 }
356
357 for (node = node0; node; node = nextsibling(node)) {
358 char *name = getpropstring(node, "name");
359 for (ssp = specials, sp = NULL;
360 ssp != NULL && (sp = *ssp) != NULL;
361 ssp++)
362 if (strcmp(name, sp) == 0)
363 break;
364
365 if (sp != NULL)
366 /* Already configured as an "early" device */
367 continue;
368
369 if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
370 node, bp, &sa) != 0) {
371 printf("sbus_attach: %s: incomplete\n", name);
372 continue;
373 }
374 (void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
375 }
376 }
377
378 int
379 sbus_setup_attach_args(sc, bustag, dmatag, node, bp, sa)
380 struct sbus_softc *sc;
381 bus_space_tag_t bustag;
382 bus_dma_tag_t dmatag;
383 int node;
384 struct bootpath *bp;
385 struct sbus_attach_args *sa;
386 {
387 struct rom_reg romreg;
388 int base;
389 int error;
390
391 bzero(sa, sizeof(struct sbus_attach_args));
392 sa->sa_name = getpropstring(node, "name");
393 sa->sa_bustag = bustag;
394 sa->sa_dmatag = dmatag;
395 sa->sa_node = node;
396 sa->sa_bp = bp;
397
398 if ((error = getprop_reg1(node, &romreg)) != 0)
399 return (error);
400
401 /* We pass only the first "reg" property */
402 base = (int)romreg.rr_paddr;
403 if (SBUS_ABS(base)) {
404 sa->sa_slot = SBUS_ABS_TO_SLOT(base);
405 sa->sa_offset = SBUS_ABS_TO_OFFSET(base);
406 } else {
407 sa->sa_slot = romreg.rr_iospace;
408 sa->sa_offset = base;
409 }
410 sa->sa_size = romreg.rr_len;
411
412 if ((error = sbus_get_intr(sc, node, &sa->sa_pri)) != 0)
413 return (error);
414
415 if ((error = getprop_address1(node, &sa->sa_promvaddr)) != 0)
416 return (error);
417
418 return (0);
419 }
420
421 int
422 _sbus_bus_map(cookie, btype, offset, size, flags, vaddr, hp)
423 void *cookie;
424 bus_type_t btype;
425 bus_addr_t offset;
426 bus_size_t size;
427 int flags;
428 vm_offset_t vaddr;
429 bus_space_handle_t *hp;
430 {
431 struct sbus_softc *sc = cookie;
432 int slot = btype;
433 int i;
434
435 for (i = 0; i < sc->sc_nrange; i++) {
436 bus_addr_t paddr;
437 bus_type_t iospace;
438
439 if (sc->sc_range[i].cspace != slot)
440 continue;
441
442 /* We've found the connection to the parent bus */
443 paddr = sc->sc_range[i].poffset + offset;
444 iospace = sc->sc_range[i].pspace;
445 return (bus_space_map2(sc->sc_bustag, iospace, paddr,
446 size, flags, vaddr, hp));
447 }
448
449 return (EINVAL);
450 }
451
452 int
453 sbus_bus_mmap(cookie, btype, paddr, flags)
454 void *cookie;
455 bus_type_t btype;
456 bus_addr_t paddr;
457 int flags;
458 {
459 int slot = (int)btype;
460 int offset = (int)paddr;
461 struct sbus_softc *sc = cookie;
462 int i;
463
464 for (i = 0; i < sc->sc_nrange; i++) {
465 bus_addr_t paddr;
466 bus_addr_t iospace;
467
468 if (sc->sc_range[i].cspace != slot)
469 continue;
470
471 paddr = sc->sc_range[i].poffset + offset;
472 iospace = (bus_addr_t)sc->sc_range[i].pspace;
473 return (bus_space_mmap(sc->sc_bustag, iospace, paddr, flags));
474 }
475
476 return (-1);
477 }
478
479
480 /*
481 * Each attached device calls sbus_establish after it initializes
482 * its sbusdev portion.
483 */
484 void
485 sbus_establish(sd, dev)
486 register struct sbusdev *sd;
487 register struct device *dev;
488 {
489 register struct sbus_softc *sc;
490 register struct device *curdev;
491
492 /*
493 * We have to look for the sbus by name, since it is not necessarily
494 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
495 * We don't just use the device structure of the above-attached
496 * sbus, since we might (in the future) support multiple sbus's.
497 */
498 for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
499 if (!curdev || !curdev->dv_xname)
500 panic("sbus_establish: can't find sbus parent for %s",
501 sd->sd_dev->dv_xname
502 ? sd->sd_dev->dv_xname
503 : "<unknown>" );
504
505 if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
506 break;
507 }
508 sc = (struct sbus_softc *) curdev;
509
510 sd->sd_dev = dev;
511 sd->sd_bchain = sc->sc_sbdev;
512 sc->sc_sbdev = sd;
513 }
514
515 /*
516 * Reset the given sbus. (???)
517 */
518 void
519 sbusreset(sbus)
520 int sbus;
521 {
522 register struct sbusdev *sd;
523 struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
524 struct device *dev;
525
526 printf("reset %s:", sc->sc_dev.dv_xname);
527 for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
528 if (sd->sd_reset) {
529 dev = sd->sd_dev;
530 (*sd->sd_reset)(dev);
531 printf(" %s", dev->dv_xname);
532 }
533 }
534 }
535
536
537 /*
538 * Get interrupt attributes for an Sbus device.
539 */
540 int
541 sbus_get_intr(sc, node, ip)
542 struct sbus_softc *sc;
543 int node;
544 int *ip;
545 {
546 struct rom_intr *rip;
547 int *ipl;
548 int n;
549
550 /*
551 * The `interrupts' property contains the Sbus interrupt level.
552 */
553 ipl = NULL;
554 if (getpropA(node, "interrupts", sizeof(int), &n, (void **)&ipl) == 0) {
555 *ip = ipl[0];
556 free(ipl, M_DEVBUF);
557 return (0);
558 }
559
560 /*
561 * Fall back on `intr' property.
562 */
563 rip = NULL;
564 switch (getpropA(node, "intr", sizeof(*rip), &n, (void **)&rip)) {
565 case 0:
566 *ip = (rip[0].int_pri & 0xf) | SBUS_INTR_COMPAT;
567 free(rip, M_DEVBUF);
568 return (0);
569 case ENOENT:
570 *ip = 0;
571 return (0);
572 }
573
574 return (-1);
575 }
576
577
578 /*
579 * Install an interrupt handler for an Sbus device.
580 */
581 void *
582 sbus_intr_establish(cookie, level, flags, handler, arg)
583 void *cookie;
584 int level;
585 int flags;
586 int (*handler) __P((void *));
587 void *arg;
588 {
589 struct sbus_softc *sc = cookie;
590 struct intrhand *ih;
591 int ipl;
592
593 ih = (struct intrhand *)
594 malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
595 if (ih == NULL)
596 return (NULL);
597
598 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
599 ipl = level;
600 else if ((level & SBUS_INTR_COMPAT) != 0)
601 ipl = level & ~SBUS_INTR_COMPAT;
602 else
603 ipl = sc->sc_intr2ipl[level];
604
605 ih->ih_fun = handler;
606 ih->ih_arg = arg;
607 if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
608 intr_fasttrap(ipl, (void (*)__P((void)))handler);
609 else
610 intr_establish(ipl, ih);
611 return (ih);
612 }
613
614 static bus_space_tag_t
615 sbus_alloc_bustag(sc)
616 struct sbus_softc *sc;
617 {
618 bus_space_tag_t sbt;
619
620 sbt = (bus_space_tag_t)
621 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
622 if (sbt == NULL)
623 return (NULL);
624
625 bzero(sbt, sizeof *sbt);
626 sbt->cookie = sc;
627 sbt->sparc_bus_map = _sbus_bus_map;
628 sbt->sparc_bus_mmap = sbus_bus_mmap;
629 sbt->sparc_intr_establish = sbus_intr_establish;
630 return (sbt);
631 }
632
633