2 * Copyright (c) 1999-2002 Eduardo Horvath
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * from: NetBSD: sbus.c,v 1.50 2002/06/20 18:26:24 eeh Exp
31 * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>.
32 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #include <sys/module.h>
68 #include <sys/queue.h>
69 #include <sys/reboot.h>
72 #include <dev/ofw/ofw_bus.h>
73 #include <dev/ofw/ofw_bus_subr.h>
74 #include <dev/ofw/openfirm.h>
76 #include <machine/bus.h>
77 #include <machine/bus_common.h>
78 #include <machine/bus_private.h>
79 #include <machine/iommureg.h>
80 #include <machine/iommuvar.h>
81 #include <machine/resource.h>
83 #include <sparc64/sbus/ofw_sbus.h>
84 #include <sparc64/sbus/sbusreg.h>
85 #include <sparc64/sbus/sbusvar.h>
92 struct ofw_bus_devinfo sdi_obdinfo;
93 struct resource_list sdi_rl;
96 /* Range descriptor, allocated for each sc_range. */
98 bus_addr_t rd_poffset;
101 bus_addr_t rd_coffset;
104 bus_space_handle_t rd_bushandle;
105 struct resource *rd_res;
110 bus_dma_tag_t sc_cdmatag;
111 bus_space_tag_t sc_cbustag;
112 int sc_clockfreq; /* clock frequency (in Hz) */
114 struct sbus_rd *sc_rd;
115 int sc_burst; /* burst transfer sizes supp. */
117 struct resource *sc_sysio_res;
118 int sc_ign; /* IGN for this sysio */
119 struct iommu_state sc_is; /* IOMMU state (iommuvar.h) */
121 struct resource *sc_ot_ires;
123 struct resource *sc_pf_ires;
127 #define SYSIO_READ8(sc, off) \
128 bus_read_8((sc)->sc_sysio_res, (off))
129 #define SYSIO_WRITE8(sc, off, v) \
130 bus_write_8((sc)->sc_sysio_res, (off), (v))
132 static device_probe_t sbus_probe;
133 static device_attach_t sbus_attach;
134 static bus_print_child_t sbus_print_child;
135 static bus_probe_nomatch_t sbus_probe_nomatch;
136 static bus_read_ivar_t sbus_read_ivar;
137 static bus_get_resource_list_t sbus_get_resource_list;
138 static bus_setup_intr_t sbus_setup_intr;
139 static bus_alloc_resource_t sbus_alloc_resource;
140 static bus_release_resource_t sbus_release_resource;
141 static bus_activate_resource_t sbus_activate_resource;
142 static bus_deactivate_resource_t sbus_deactivate_resource;
143 static bus_get_dma_tag_t sbus_get_dma_tag;
144 static ofw_bus_get_devinfo_t sbus_get_devinfo;
146 static int sbus_inlist(const char *, const char *const *);
147 static struct sbus_devinfo * sbus_setup_dinfo(device_t, struct sbus_softc *,
149 static void sbus_destroy_dinfo(struct sbus_devinfo *);
150 static void sbus_intr_enable(void *);
151 static void sbus_intr_disable(void *);
152 static void sbus_intr_assign(void *);
153 static void sbus_intr_clear(void *);
154 static int sbus_find_intrmap(struct sbus_softc *, u_int, bus_addr_t *,
156 static bus_space_tag_t sbus_alloc_bustag(struct sbus_softc *);
157 static driver_intr_t sbus_overtemp;
158 static driver_intr_t sbus_pwrfail;
159 static int sbus_print_res(struct sbus_devinfo *);
161 static device_method_t sbus_methods[] = {
162 /* Device interface */
163 DEVMETHOD(device_probe, sbus_probe),
164 DEVMETHOD(device_attach, sbus_attach),
165 DEVMETHOD(device_shutdown, bus_generic_shutdown),
166 DEVMETHOD(device_suspend, bus_generic_suspend),
167 DEVMETHOD(device_resume, bus_generic_resume),
170 DEVMETHOD(bus_print_child, sbus_print_child),
171 DEVMETHOD(bus_probe_nomatch, sbus_probe_nomatch),
172 DEVMETHOD(bus_read_ivar, sbus_read_ivar),
173 DEVMETHOD(bus_alloc_resource, sbus_alloc_resource),
174 DEVMETHOD(bus_activate_resource, sbus_activate_resource),
175 DEVMETHOD(bus_deactivate_resource, sbus_deactivate_resource),
176 DEVMETHOD(bus_release_resource, sbus_release_resource),
177 DEVMETHOD(bus_setup_intr, sbus_setup_intr),
178 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
179 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
180 DEVMETHOD(bus_get_resource_list, sbus_get_resource_list),
181 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
182 DEVMETHOD(bus_get_dma_tag, sbus_get_dma_tag),
184 /* ofw_bus interface */
185 DEVMETHOD(ofw_bus_get_devinfo, sbus_get_devinfo),
186 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
187 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
188 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
189 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
190 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
195 static driver_t sbus_driver = {
198 sizeof(struct sbus_softc),
201 static devclass_t sbus_devclass;
203 EARLY_DRIVER_MODULE(sbus, nexus, sbus_driver, sbus_devclass, 0, 0,
205 MODULE_DEPEND(sbus, nexus, 1, 1, 1);
206 MODULE_VERSION(sbus, 1);
208 #define OFW_SBUS_TYPE "sbus"
209 #define OFW_SBUS_NAME "sbus"
211 static const struct intr_controller sbus_ic = {
219 struct sbus_softc *sica_sc;
224 static const char *const sbus_order_first[] = {
231 sbus_inlist(const char *name, const char *const *list)
237 for (i = 0; list[i] != NULL; i++) {
238 if (strcmp(name, list[i]) == 0)
245 sbus_probe(device_t dev)
249 t = ofw_bus_get_type(dev);
250 if (((t == NULL || strcmp(t, OFW_SBUS_TYPE) != 0)) &&
251 strcmp(ofw_bus_get_name(dev), OFW_SBUS_NAME) != 0)
253 device_set_desc(dev, "U2S UPA-SBus bridge");
258 sbus_attach(device_t dev)
260 struct sbus_softc *sc;
261 struct sbus_devinfo *sdi;
262 struct sbus_icarg *sica;
263 struct sbus_ranges *range;
264 struct resource *res;
265 struct resource_list *rl;
267 bus_addr_t intrclr, intrmap, phys;
270 phandle_t child, node;
274 sc = device_get_softc(dev);
276 node = ofw_bus_get_node(dev);
279 sc->sc_sysio_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
281 if (sc->sc_sysio_res == NULL)
282 panic("%s: cannot allocate device memory", __func__);
284 if (OF_getprop(node, "interrupts", &prop, sizeof(prop)) == -1)
285 panic("%s: cannot get IGN", __func__);
286 sc->sc_ign = INTIGN(prop);
287 sc->sc_cbustag = sbus_alloc_bustag(sc);
290 * Record clock frequency for synchronous SCSI.
291 * IS THIS THE CORRECT DEFAULT??
293 if (OF_getprop(node, "clock-frequency", &prop, sizeof(prop)) == -1)
295 sc->sc_clockfreq = prop;
297 device_printf(dev, "clock %d.%03d MHz\n", prop / 1000, prop % 1000);
300 * Collect address translations from the OBP.
302 if ((sc->sc_nrange = OF_getprop_alloc(node, "ranges",
303 sizeof(*range), (void **)&range)) == -1) {
304 panic("%s: error getting ranges property", __func__);
306 sc->sc_rd = malloc(sizeof(*sc->sc_rd) * sc->sc_nrange, M_DEVBUF,
308 if (sc->sc_rd == NULL)
309 panic("%s: cannot allocate rmans", __func__);
311 * Preallocate all space that the SBus bridge decodes, so that nothing
312 * else gets in the way; set up rmans etc.
314 rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
315 for (i = 0; i < sc->sc_nrange; i++) {
316 phys = range[i].poffset | ((bus_addr_t)range[i].pspace << 32);
317 size = range[i].size;
318 sc->sc_rd[i].rd_slot = range[i].cspace;
319 sc->sc_rd[i].rd_coffset = range[i].coffset;
320 sc->sc_rd[i].rd_cend = sc->sc_rd[i].rd_coffset + size;
321 j = resource_list_add_next(rl, SYS_RES_MEMORY, phys,
322 phys + size - 1, size);
323 if ((res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &j,
325 panic("%s: cannot allocate decoded range", __func__);
326 sc->sc_rd[i].rd_bushandle = rman_get_bushandle(res);
327 sc->sc_rd[i].rd_rman.rm_type = RMAN_ARRAY;
328 sc->sc_rd[i].rd_rman.rm_descr = "SBus Device Memory";
329 if (rman_init(&sc->sc_rd[i].rd_rman) != 0 ||
330 rman_manage_region(&sc->sc_rd[i].rd_rman, 0, size) != 0)
331 panic("%s: failed to set up memory rman", __func__);
332 sc->sc_rd[i].rd_poffset = phys;
333 sc->sc_rd[i].rd_pend = phys + size;
334 sc->sc_rd[i].rd_res = res;
336 free(range, M_OFWPROP);
339 * Get the SBus burst transfer size if burst transfers are supported.
341 if (OF_getprop(node, "up-burst-sizes", &sc->sc_burst,
342 sizeof(sc->sc_burst)) == -1 || sc->sc_burst == 0)
344 (SBUS_BURST64_DEF << SBUS_BURST64_SHIFT) | SBUS_BURST_DEF;
347 /* initalise the IOMMU */
349 /* punch in our copies */
350 sc->sc_is.is_pmaxaddr = IOMMU_MAXADDR(SBUS_IOMMU_BITS);
351 sc->sc_is.is_bustag = rman_get_bustag(sc->sc_sysio_res);
352 sc->sc_is.is_bushandle = rman_get_bushandle(sc->sc_sysio_res);
353 sc->sc_is.is_iommu = SBR_IOMMU;
354 sc->sc_is.is_dtag = SBR_IOMMU_TLB_TAG_DIAG;
355 sc->sc_is.is_ddram = SBR_IOMMU_TLB_DATA_DIAG;
356 sc->sc_is.is_dqueue = SBR_IOMMU_QUEUE_DIAG;
357 sc->sc_is.is_dva = SBR_IOMMU_SVADIAG;
358 sc->sc_is.is_dtcmp = 0;
359 sc->sc_is.is_sb[0] = SBR_STRBUF;
360 sc->sc_is.is_sb[1] = 0;
363 * Note: the SBus IOMMU ignores the high bits of an address, so a NULL
364 * DMA pointer will be translated by the first page of the IOTSB.
365 * To detect bugs we'll allocate and ignore the first entry.
367 iommu_init(device_get_nameunit(dev), &sc->sc_is, 3, -1, 1);
369 /* Create the DMA tag. */
370 if (bus_dma_tag_create(bus_get_dma_tag(dev), 8, 0,
371 sc->sc_is.is_pmaxaddr, ~0, NULL, NULL, sc->sc_is.is_pmaxaddr,
372 0xff, 0xffffffff, 0, NULL, NULL, &sc->sc_cdmatag) != 0)
373 panic("%s: bus_dma_tag_create failed", __func__);
374 /* Customize the tag. */
375 sc->sc_cdmatag->dt_cookie = &sc->sc_is;
376 sc->sc_cdmatag->dt_mt = &iommu_dma_methods;
379 * Hunt through all the interrupt mapping regs and register our
380 * interrupt controller for the corresponding interrupt vectors.
381 * We do this early in order to be able to catch stray interrupts.
383 for (i = 0; i <= SBUS_MAX_INO; i++) {
384 if (sbus_find_intrmap(sc, i, &intrmap, &intrclr) == 0)
386 sica = malloc(sizeof(*sica), M_DEVBUF, M_NOWAIT);
388 panic("%s: could not allocate interrupt controller "
389 "argument", __func__);
391 sica->sica_map = intrmap;
392 sica->sica_clr = intrclr;
395 "intr map (INO %d, %s) %#lx: %#lx, clr: %#lx\n",
396 i, (i & INTMAP_OBIO_MASK) == 0 ? "SBus slot" : "OBIO",
397 (u_long)intrmap, (u_long)SYSIO_READ8(sc, intrmap),
400 j = intr_controller_register(INTMAP_VEC(sc->sc_ign, i),
403 device_printf(dev, "could not register interrupt "
404 "controller for INO %d (%d)\n", i, j);
407 /* Enable the over-temperature and power-fail interrupts. */
409 sc->sc_ot_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
411 if (sc->sc_ot_ires == NULL ||
412 INTIGN(vec = rman_get_start(sc->sc_ot_ires)) != sc->sc_ign ||
413 INTVEC(SYSIO_READ8(sc, SBR_THERM_INT_MAP)) != vec ||
414 intr_vectors[vec].iv_ic != &sbus_ic ||
415 bus_setup_intr(dev, sc->sc_ot_ires, INTR_TYPE_MISC | INTR_BRIDGE,
416 NULL, sbus_overtemp, sc, &sc->sc_ot_ihand) != 0)
417 panic("%s: failed to set up temperature interrupt", __func__);
419 sc->sc_pf_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
421 if (sc->sc_pf_ires == NULL ||
422 INTIGN(vec = rman_get_start(sc->sc_pf_ires)) != sc->sc_ign ||
423 INTVEC(SYSIO_READ8(sc, SBR_POWER_INT_MAP)) != vec ||
424 intr_vectors[vec].iv_ic != &sbus_ic ||
425 bus_setup_intr(dev, sc->sc_pf_ires, INTR_TYPE_MISC | INTR_BRIDGE,
426 NULL, sbus_pwrfail, sc, &sc->sc_pf_ihand) != 0)
427 panic("%s: failed to set up power fail interrupt", __func__);
429 /* Initialize the counter-timer. */
430 sparc64_counter_init(device_get_nameunit(dev),
431 rman_get_bustag(sc->sc_sysio_res),
432 rman_get_bushandle(sc->sc_sysio_res), SBR_TC0);
435 * Loop through ROM children, fixing any relative addresses
436 * and then configuring each device.
438 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
439 if ((sdi = sbus_setup_dinfo(dev, sc, child)) == NULL)
442 * For devices where there are variants that are actually
443 * split into two SBus devices (as opposed to the first
444 * half of the device being a SBus device and the second
445 * half hanging off of the first one) like 'auxio' and
446 * 'SUNW,fdtwo' or 'dma' and 'esp' probe the SBus device
447 * which is a prerequisite to the driver attaching to the
448 * second one with a lower order. Saves us from dealing
449 * with different probe orders in the respective device
450 * drivers which generally is more hackish.
452 cdev = device_add_child_ordered(dev, (OF_child(child) == 0 &&
453 sbus_inlist(sdi->sdi_obdinfo.obd_name, sbus_order_first)) ?
454 SBUS_ORDER_FIRST : SBUS_ORDER_NORMAL, NULL, -1);
457 "<%s>: device_add_child_ordered failed\n",
458 sdi->sdi_obdinfo.obd_name);
459 sbus_destroy_dinfo(sdi);
462 device_set_ivars(cdev, sdi);
464 return (bus_generic_attach(dev));
467 static struct sbus_devinfo *
468 sbus_setup_dinfo(device_t dev, struct sbus_softc *sc, phandle_t node)
470 struct sbus_devinfo *sdi;
471 struct sbus_regs *reg;
472 u_int32_t base, iv, *intr;
473 int i, nreg, nintr, slot, rslot;
475 sdi = malloc(sizeof(*sdi), M_DEVBUF, M_ZERO | M_WAITOK);
476 if (ofw_bus_gen_setup_devinfo(&sdi->sdi_obdinfo, node) != 0) {
480 resource_list_init(&sdi->sdi_rl);
482 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®);
484 if (sdi->sdi_obdinfo.obd_type == NULL ||
485 strcmp(sdi->sdi_obdinfo.obd_type, "hierarchical") != 0) {
486 device_printf(dev, "<%s>: incomplete\n",
487 sdi->sdi_obdinfo.obd_name);
491 for (i = 0; i < nreg; i++) {
492 base = reg[i].sbr_offset;
493 if (SBUS_ABS(base)) {
494 rslot = SBUS_ABS_TO_SLOT(base);
495 base = SBUS_ABS_TO_OFFSET(base);
497 rslot = reg[i].sbr_slot;
498 if (slot != -1 && slot != rslot) {
499 device_printf(dev, "<%s>: multiple slots\n",
500 sdi->sdi_obdinfo.obd_name);
501 free(reg, M_OFWPROP);
506 resource_list_add(&sdi->sdi_rl, SYS_RES_MEMORY, i,
507 base, base + reg[i].sbr_size, reg[i].sbr_size);
509 free(reg, M_OFWPROP);
511 sdi->sdi_slot = slot;
514 * The `interrupts' property contains the SBus interrupt level.
516 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
519 for (i = 0; i < nintr; i++) {
522 * SBus card devices need the slot number encoded into
523 * the vector as this is generally not done.
525 if ((iv & INTMAP_OBIO_MASK) == 0)
527 iv = INTMAP_VEC(sc->sc_ign, iv);
528 resource_list_add(&sdi->sdi_rl, SYS_RES_IRQ, i,
531 free(intr, M_OFWPROP);
533 if (OF_getprop(node, "burst-sizes", &sdi->sdi_burstsz,
534 sizeof(sdi->sdi_burstsz)) == -1)
535 sdi->sdi_burstsz = sc->sc_burst;
537 sdi->sdi_burstsz &= sc->sc_burst;
538 if (OF_getprop(node, "clock-frequency", &sdi->sdi_clockfreq,
539 sizeof(sdi->sdi_clockfreq)) == -1)
540 sdi->sdi_clockfreq = sc->sc_clockfreq;
545 sbus_destroy_dinfo(sdi);
550 sbus_destroy_dinfo(struct sbus_devinfo *dinfo)
553 resource_list_free(&dinfo->sdi_rl);
554 ofw_bus_gen_destroy_devinfo(&dinfo->sdi_obdinfo);
555 free(dinfo, M_DEVBUF);
559 sbus_print_child(device_t dev, device_t child)
563 rv = bus_print_child_header(dev, child);
564 rv += sbus_print_res(device_get_ivars(child));
565 rv += bus_print_child_footer(dev, child);
570 sbus_probe_nomatch(device_t dev, device_t child)
574 device_printf(dev, "<%s>", ofw_bus_get_name(child));
575 sbus_print_res(device_get_ivars(child));
576 type = ofw_bus_get_type(child);
577 printf(" type %s (no driver attached)\n",
578 type != NULL ? type : "unknown");
582 sbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
584 struct sbus_softc *sc;
585 struct sbus_devinfo *dinfo;
587 sc = device_get_softc(dev);
588 if ((dinfo = device_get_ivars(child)) == NULL)
591 case SBUS_IVAR_BURSTSZ:
592 *result = dinfo->sdi_burstsz;
594 case SBUS_IVAR_CLOCKFREQ:
595 *result = dinfo->sdi_clockfreq;
598 *result = sc->sc_ign;
601 *result = dinfo->sdi_slot;
609 static struct resource_list *
610 sbus_get_resource_list(device_t dev, device_t child)
612 struct sbus_devinfo *sdi;
614 sdi = device_get_ivars(child);
615 return (&sdi->sdi_rl);
619 sbus_intr_enable(void *arg)
621 struct intr_vector *iv = arg;
622 struct sbus_icarg *sica = iv->iv_icarg;
624 SYSIO_WRITE8(sica->sica_sc, sica->sica_map,
625 INTMAP_ENABLE(iv->iv_vec, iv->iv_mid));
629 sbus_intr_disable(void *arg)
631 struct intr_vector *iv = arg;
632 struct sbus_icarg *sica = iv->iv_icarg;
634 SYSIO_WRITE8(sica->sica_sc, sica->sica_map, iv->iv_vec);
638 sbus_intr_assign(void *arg)
640 struct intr_vector *iv = arg;
641 struct sbus_icarg *sica = iv->iv_icarg;
643 SYSIO_WRITE8(sica->sica_sc, sica->sica_map, INTMAP_TID(
644 SYSIO_READ8(sica->sica_sc, sica->sica_map), iv->iv_mid));
648 sbus_intr_clear(void *arg)
650 struct intr_vector *iv = arg;
651 struct sbus_icarg *sica = iv->iv_icarg;
653 SYSIO_WRITE8(sica->sica_sc, sica->sica_clr, INTCLR_IDLE);
657 sbus_find_intrmap(struct sbus_softc *sc, u_int ino, bus_addr_t *intrmapptr,
658 bus_addr_t *intrclrptr)
660 bus_addr_t intrclr, intrmap;
663 if (ino > SBUS_MAX_INO) {
664 device_printf(sc->sc_dev, "out of range INO %d requested\n",
669 if ((ino & INTMAP_OBIO_MASK) == 0) {
670 intrmap = SBR_SLOT0_INT_MAP + INTSLOT(ino) * 8;
671 intrclr = SBR_SLOT0_INT_CLR +
672 (INTSLOT(ino) * 8 * 8) + (INTPRI(ino) * 8);
675 for (i = 0, intrmap = SBR_SCSI_INT_MAP;
676 intrmap <= SBR_RESERVED_INT_MAP; intrmap += 8, i++) {
677 if (INTVEC(SYSIO_READ8(sc, intrmap)) ==
678 INTMAP_VEC(sc->sc_ign, ino)) {
679 intrclr = SBR_SCSI_INT_CLR + i * 8;
686 if (intrmapptr != NULL)
687 *intrmapptr = intrmap;
688 if (intrclrptr != NULL)
689 *intrclrptr = intrclr;
694 sbus_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
695 driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
697 struct sbus_softc *sc;
700 sc = device_get_softc(dev);
702 * Make sure the vector is fully specified and we registered
703 * our interrupt controller for it.
705 vec = rman_get_start(ires);
706 if (INTIGN(vec) != sc->sc_ign || intr_vectors[vec].iv_ic != &sbus_ic) {
707 device_printf(dev, "invalid interrupt vector 0x%lx\n", vec);
710 return (bus_generic_setup_intr(dev, child, ires, flags, filt, intr,
714 static struct resource *
715 sbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
716 u_long start, u_long end, u_long count, u_int flags)
718 struct sbus_softc *sc;
721 struct resource_list *rl;
722 struct resource_list_entry *rle;
724 bus_space_handle_t bh;
728 int isdefault, needactivate, passthrough;
730 isdefault = (start == 0UL && end == ~0UL);
731 needactivate = flags & RF_ACTIVE;
732 passthrough = (device_get_parent(child) != bus);
734 sc = device_get_softc(bus);
735 rl = BUS_GET_RESOURCE_LIST(bus, child);
738 return (resource_list_alloc(rl, bus, child, type, rid, start,
742 rle = resource_list_find(rl, type, *rid);
745 if (rle->res != NULL)
746 panic("%s: resource entry is busy", __func__);
749 count = ulmax(count, rle->count);
750 end = ulmax(rle->end, start + count - 1);
754 bh = toffs = tend = 0;
756 while (device_get_parent(schild) != bus)
757 schild = device_get_parent(schild);
758 slot = sbus_get_slot(schild);
759 for (i = 0; i < sc->sc_nrange; i++) {
760 if (sc->sc_rd[i].rd_slot != slot ||
761 start < sc->sc_rd[i].rd_coffset ||
762 start > sc->sc_rd[i].rd_cend)
764 /* Disallow cross-range allocations. */
765 if (end > sc->sc_rd[i].rd_cend)
767 /* We've found the connection to the parent bus */
768 toffs = start - sc->sc_rd[i].rd_coffset;
769 tend = end - sc->sc_rd[i].rd_coffset;
770 rm = &sc->sc_rd[i].rd_rman;
771 bh = sc->sc_rd[i].rd_bushandle;
777 rv = rman_reserve_resource(rm, toffs, tend, count, flags,
781 rman_set_rid(rv, *rid);
782 rman_set_bustag(rv, sc->sc_cbustag);
783 rman_set_bushandle(rv, bh + rman_get_start(rv));
785 if (bus_activate_resource(child, type, *rid, rv)) {
786 rman_release_resource(rv);
799 sbus_activate_resource(device_t bus, device_t child, int type, int rid,
805 if (type == SYS_RES_IRQ) {
806 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
807 child, type, rid, r));
809 if (type == SYS_RES_MEMORY) {
811 * Need to memory-map the device space, as some drivers
812 * depend on the virtual address being set and usable.
814 error = sparc64_bus_mem_map(rman_get_bustag(r),
815 rman_get_bushandle(r), rman_get_size(r), 0, 0, &p);
818 rman_set_virtual(r, p);
820 return (rman_activate_resource(r));
824 sbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
828 if (type == SYS_RES_IRQ) {
829 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus),
830 child, type, rid, r));
832 if (type == SYS_RES_MEMORY) {
833 sparc64_bus_mem_unmap(rman_get_virtual(r), rman_get_size(r));
834 rman_set_virtual(r, NULL);
836 return (rman_deactivate_resource(r));
840 sbus_release_resource(device_t bus, device_t child, int type, int rid,
843 struct resource_list *rl;
844 struct resource_list_entry *rle;
845 int error, passthrough;
847 passthrough = (device_get_parent(child) != bus);
848 rl = BUS_GET_RESOURCE_LIST(bus, child);
849 if (type == SYS_RES_IRQ)
850 return (resource_list_release(rl, bus, child, type, rid, r));
851 if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
852 error = bus_deactivate_resource(child, type, rid, r);
856 error = rman_release_resource(r);
857 if (error != 0 || passthrough)
859 rle = resource_list_find(rl, type, rid);
861 panic("%s: cannot find resource", __func__);
862 if (rle->res == NULL)
863 panic("%s: resource entry is not busy", __func__);
869 sbus_get_dma_tag(device_t bus, device_t child)
871 struct sbus_softc *sc;
873 sc = device_get_softc(bus);
874 return (sc->sc_cdmatag);
877 static const struct ofw_bus_devinfo *
878 sbus_get_devinfo(device_t bus, device_t child)
880 struct sbus_devinfo *sdi;
882 sdi = device_get_ivars(child);
883 return (&sdi->sdi_obdinfo);
887 * Handle an overtemp situation.
889 * SPARCs have temperature sensors which generate interrupts
890 * if the machine's temperature exceeds a certain threshold.
891 * This handles the interrupt and powers off the machine.
892 * The same needs to be done to PCI controller drivers.
895 sbus_overtemp(void *arg)
899 /* As the interrupt is cleared we may be called multiple times. */
903 printf("DANGER: OVER TEMPERATURE detected\nShutting down NOW.\n");
904 shutdown_nice(RB_POWEROFF);
907 /* Try to shut down in time in case of power failure. */
909 sbus_pwrfail(void *arg)
913 /* As the interrupt is cleared we may be called multiple times. */
917 printf("Power failure detected\nShutting down NOW.\n");
921 static bus_space_tag_t
922 sbus_alloc_bustag(struct sbus_softc *sc)
926 sbt = (bus_space_tag_t)malloc(sizeof(struct bus_space_tag), M_DEVBUF,
929 panic("%s: out of memory", __func__);
931 sbt->bst_cookie = sc;
932 sbt->bst_parent = rman_get_bustag(sc->sc_sysio_res);
933 sbt->bst_type = SBUS_BUS_SPACE;
938 sbus_print_res(struct sbus_devinfo *sdi)
943 rv += resource_list_print_type(&sdi->sdi_rl, "mem", SYS_RES_MEMORY,
945 rv += resource_list_print_type(&sdi->sdi_rl, "irq", SYS_RES_IRQ,