sbus.c revision 152684
190618Stmm/*-
290618Stmm * Copyright (c) 1998 The NetBSD Foundation, Inc.
390618Stmm * All rights reserved.
490618Stmm *
590618Stmm * This code is derived from software contributed to The NetBSD Foundation
690618Stmm * by Paul Kranenburg.
790618Stmm *
890618Stmm * Redistribution and use in source and binary forms, with or without
990618Stmm * modification, are permitted provided that the following conditions
1090618Stmm * are met:
1190618Stmm * 1. Redistributions of source code must retain the above copyright
1290618Stmm *    notice, this list of conditions and the following disclaimer.
1390618Stmm * 2. Redistributions in binary form must reproduce the above copyright
1490618Stmm *    notice, this list of conditions and the following disclaimer in the
1590618Stmm *    documentation and/or other materials provided with the distribution.
1690618Stmm * 3. All advertising materials mentioning features or use of this software
1790618Stmm *    must display the following acknowledgement:
1890618Stmm *        This product includes software developed by the NetBSD
1990618Stmm *        Foundation, Inc. and its contributors.
2090618Stmm * 4. Neither the name of The NetBSD Foundation nor the names of its
2190618Stmm *    contributors may be used to endorse or promote products derived
2290618Stmm *    from this software without specific prior written permission.
2390618Stmm *
2490618Stmm * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2590618Stmm * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2690618Stmm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2790618Stmm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2890618Stmm * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2990618Stmm * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3090618Stmm * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3190618Stmm * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3290618Stmm * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3390618Stmm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3490618Stmm * POSSIBILITY OF SUCH DAMAGE.
3590618Stmm */
36139825Simp/*-
3790618Stmm * Copyright (c) 1992, 1993
3890618Stmm *	The Regents of the University of California.  All rights reserved.
3990618Stmm *
4090618Stmm * This software was developed by the Computer Systems Engineering group
4190618Stmm * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
4290618Stmm * contributed to Berkeley.
4390618Stmm *
4490618Stmm * All advertising materials mentioning features or use of this software
4590618Stmm * must display the following acknowledgement:
4690618Stmm *	This product includes software developed by the University of
4790618Stmm *	California, Lawrence Berkeley Laboratory.
4890618Stmm *
4990618Stmm * Redistribution and use in source and binary forms, with or without
5090618Stmm * modification, are permitted provided that the following conditions
5190618Stmm * are met:
5290618Stmm * 1. Redistributions of source code must retain the above copyright
5390618Stmm *    notice, this list of conditions and the following disclaimer.
5490618Stmm * 2. Redistributions in binary form must reproduce the above copyright
5590618Stmm *    notice, this list of conditions and the following disclaimer in the
5690618Stmm *    documentation and/or other materials provided with the distribution.
5790618Stmm * 4. Neither the name of the University nor the names of its contributors
5890618Stmm *    may be used to endorse or promote products derived from this software
5990618Stmm *    without specific prior written permission.
6090618Stmm *
6190618Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6290618Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6390618Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6490618Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6590618Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6690618Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6790618Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6890618Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6990618Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
7090618Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7190618Stmm * SUCH DAMAGE.
7290618Stmm */
73139825Simp/*-
7490618Stmm * Copyright (c) 1999 Eduardo Horvath
7590618Stmm * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>.
7690618Stmm * All rights reserved.
7790618Stmm *
7890618Stmm * Redistribution and use in source and binary forms, with or without
7990618Stmm * modification, are permitted provided that the following conditions
8090618Stmm * are met:
8190618Stmm * 1. Redistributions of source code must retain the above copyright
8290618Stmm *    notice, this list of conditions and the following disclaimer.
8390618Stmm *
8490618Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
8590618Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8690618Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8790618Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
8890618Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8990618Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
9090618Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
9190618Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9290618Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9390618Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9490618Stmm * SUCH DAMAGE.
9590618Stmm *
9690618Stmm *	from: @(#)sbus.c	8.1 (Berkeley) 6/11/93
9790618Stmm *	from: NetBSD: sbus.c,v 1.46 2001/10/07 20:30:41 eeh Exp
9890618Stmm */
9990618Stmm
100145185Smarius#include <sys/cdefs.h>
101145185Smarius__FBSDID("$FreeBSD: head/sys/sparc64/sbus/sbus.c 152684 2005-11-22 16:39:44Z marius $");
102145185Smarius
10390618Stmm/*
104145185Smarius * SBus support.
10590618Stmm */
106131376Smarius
10790618Stmm#include <sys/param.h>
10890618Stmm#include <sys/systm.h>
10990618Stmm#include <sys/bus.h>
11090618Stmm#include <sys/kernel.h>
11190618Stmm#include <sys/malloc.h>
112130068Sphk#include <sys/module.h>
113107477Stmm#include <sys/pcpu.h>
11490618Stmm#include <sys/reboot.h>
11590618Stmm
116133589Smarius#include <dev/ofw/ofw_bus.h>
117152684Smarius#include <dev/ofw/ofw_bus_subr.h>
118119338Simp#include <dev/ofw/openfirm.h>
11990618Stmm
12090618Stmm#include <machine/bus.h>
121116541Stmm#include <machine/bus_private.h>
12290618Stmm#include <machine/iommureg.h>
12390618Stmm#include <machine/bus_common.h>
12490618Stmm#include <machine/intr_machdep.h>
12590618Stmm#include <machine/nexusvar.h>
12690618Stmm#include <machine/ofw_upa.h>
12790618Stmm#include <machine/resource.h>
12890618Stmm
12990618Stmm#include <sys/rman.h>
13090618Stmm
13190618Stmm#include <machine/iommuvar.h>
13290618Stmm
13390618Stmm#include <sparc64/sbus/ofw_sbus.h>
13490618Stmm#include <sparc64/sbus/sbusreg.h>
13590618Stmm#include <sparc64/sbus/sbusvar.h>
13690618Stmm
13790618Stmmstruct sbus_devinfo {
13890618Stmm	int			sdi_burstsz;
139145186Smarius	int			sdi_clockfreq;
14090618Stmm	int			sdi_slot;
14190618Stmm
142152684Smarius	struct ofw_bus_devinfo	sdi_obdinfo;
14390618Stmm	struct resource_list	sdi_rl;
14490618Stmm};
14590618Stmm
14690618Stmm/* Range descriptor, allocated for each sc_range. */
14790618Stmmstruct sbus_rd {
14890618Stmm	bus_addr_t		rd_poffset;
14990618Stmm	bus_addr_t		rd_pend;
15090618Stmm	int			rd_slot;
15190618Stmm	bus_addr_t		rd_coffset;
15290618Stmm	bus_addr_t		rd_cend;
15390618Stmm	struct rman		rd_rman;
15490618Stmm	bus_space_handle_t	rd_bushandle;
15590618Stmm	struct resource		*rd_res;
15690618Stmm};
15790618Stmm
15890618Stmmstruct sbus_softc {
15990618Stmm	bus_space_tag_t		sc_bustag;
16090618Stmm	bus_space_handle_t	sc_bushandle;
16190618Stmm	bus_dma_tag_t		sc_dmatag;
16290618Stmm	bus_dma_tag_t		sc_cdmatag;
16390618Stmm	bus_space_tag_t		sc_cbustag;
16490618Stmm	int			sc_clockfreq;	/* clock frequency (in Hz) */
16590618Stmm	struct upa_regs		*sc_reg;
16690618Stmm	int			sc_nreg;
16790618Stmm	int			sc_nrange;
16890618Stmm	struct sbus_rd		*sc_rd;
169145185Smarius	int			sc_burst;	/* burst transfer sizes supp. */
17090618Stmm
17190618Stmm	struct resource		*sc_sysio_res;
172145185Smarius	int			sc_ign;		/* IGN for this sysio */
173145185Smarius	struct iommu_state	sc_is;		/* IOMMU state (iommuvar.h) */
17490618Stmm
17590618Stmm	struct resource		*sc_ot_ires;
17690618Stmm	void			*sc_ot_ihand;
17790618Stmm	struct resource		*sc_pf_ires;
17890618Stmm	void			*sc_pf_ihand;
17990618Stmm};
18090618Stmm
18190618Stmmstruct sbus_clr {
18290618Stmm	struct sbus_softc	*scl_sc;
183145185Smarius	bus_addr_t		scl_clr;	/* clear register */
184145185Smarius	driver_intr_t		*scl_handler;	/* handler to call */
185145185Smarius	void			*scl_arg;	/* argument for the handler */
186145185Smarius	void			*scl_cookie;	/* parent bus int. cookie */
18790618Stmm};
18890618Stmm
18990618Stmm#define	SYSIO_READ8(sc, off) \
19090618Stmm	bus_space_read_8((sc)->sc_bustag, (sc)->sc_bushandle, (off))
19190618Stmm#define	SYSIO_WRITE8(sc, off, v) \
19290618Stmm	bus_space_write_8((sc)->sc_bustag, (sc)->sc_bushandle, (off), (v))
19390618Stmm
194145185Smariusstatic device_probe_t sbus_probe;
195145186Smariusstatic device_attach_t sbus_attach;
196145185Smariusstatic bus_print_child_t sbus_print_child;
197145185Smariusstatic bus_probe_nomatch_t sbus_probe_nomatch;
198145185Smariusstatic bus_read_ivar_t sbus_read_ivar;
199145185Smariusstatic bus_get_resource_list_t sbus_get_resource_list;
200145185Smariusstatic bus_setup_intr_t sbus_setup_intr;
201145185Smariusstatic bus_teardown_intr_t sbus_teardown_intr;
202145185Smariusstatic bus_alloc_resource_t sbus_alloc_resource;
203145185Smariusstatic bus_release_resource_t sbus_release_resource;
204145185Smariusstatic bus_activate_resource_t sbus_activate_resource;
205145185Smariusstatic bus_deactivate_resource_t sbus_deactivate_resource;
206152684Smariusstatic ofw_bus_get_devinfo_t sbus_get_devinfo;
20790618Stmm
208146391Smariusstatic int sbus_inlist(const char *, const char **);
209152684Smariusstatic struct sbus_devinfo * sbus_setup_dinfo(device_t, struct sbus_softc *,
210152684Smarius    phandle_t);
211152684Smariusstatic void sbus_destroy_dinfo(struct sbus_devinfo *);
21290618Stmmstatic void sbus_intr_stub(void *);
21390618Stmmstatic bus_space_tag_t sbus_alloc_bustag(struct sbus_softc *);
21490618Stmmstatic void sbus_overtemp(void *);
21590618Stmmstatic void sbus_pwrfail(void *);
216152684Smariusstatic int sbus_print_res(struct sbus_devinfo *);
21790618Stmm
21890618Stmmstatic device_method_t sbus_methods[] = {
21990618Stmm	/* Device interface */
22090618Stmm	DEVMETHOD(device_probe,		sbus_probe),
221145186Smarius	DEVMETHOD(device_attach,	sbus_attach),
22290618Stmm
22390618Stmm	/* Bus interface */
22490618Stmm	DEVMETHOD(bus_print_child,	sbus_print_child),
22590618Stmm	DEVMETHOD(bus_probe_nomatch,	sbus_probe_nomatch),
22690618Stmm	DEVMETHOD(bus_read_ivar,	sbus_read_ivar),
22790618Stmm	DEVMETHOD(bus_setup_intr, 	sbus_setup_intr),
22890618Stmm	DEVMETHOD(bus_teardown_intr,	sbus_teardown_intr),
22990618Stmm	DEVMETHOD(bus_alloc_resource,	sbus_alloc_resource),
23090618Stmm	DEVMETHOD(bus_activate_resource,	sbus_activate_resource),
23190618Stmm	DEVMETHOD(bus_deactivate_resource,	sbus_deactivate_resource),
23290618Stmm	DEVMETHOD(bus_release_resource,	sbus_release_resource),
23390618Stmm	DEVMETHOD(bus_get_resource_list, sbus_get_resource_list),
23490618Stmm	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
23590618Stmm
236133589Smarius	/* ofw_bus interface */
237152684Smarius	DEVMETHOD(ofw_bus_get_devinfo,	sbus_get_devinfo),
238152684Smarius	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
239152684Smarius	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
240152684Smarius	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
241152684Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
242152684Smarius	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
243133589Smarius
24490618Stmm	{ 0, 0 }
24590618Stmm};
24690618Stmm
24790618Stmmstatic driver_t sbus_driver = {
24890618Stmm	"sbus",
24990618Stmm	sbus_methods,
25090618Stmm	sizeof(struct sbus_softc),
25190618Stmm};
25290618Stmm
25390618Stmmstatic devclass_t sbus_devclass;
25490618Stmm
25590618StmmDRIVER_MODULE(sbus, nexus, sbus_driver, sbus_devclass, 0, 0);
25690618Stmm
25790618Stmm#define	OFW_SBUS_TYPE	"sbus"
25890618Stmm#define	OFW_SBUS_NAME	"sbus"
25990618Stmm
260146391Smariusstatic const char *sbus_order_first[] = {
261146391Smarius	"auxio",
262146391Smarius	"dma",
263146391Smarius	NULL
264146391Smarius};
265146391Smarius
26690618Stmmstatic int
267146391Smariussbus_inlist(const char *name, const char **list)
268146391Smarius{
269146391Smarius	int i;
270146391Smarius
271146391Smarius	if (name == NULL)
272146391Smarius		return (0);
273146391Smarius	for (i = 0; list[i] != NULL; i++) {
274146391Smarius		if (strcmp(name, list[i]) == 0)
275146391Smarius			return (1);
276146391Smarius	}
277146391Smarius	return (0);
278146391Smarius}
279146391Smarius
280146391Smariusstatic int
28190618Stmmsbus_probe(device_t dev)
28290618Stmm{
283145186Smarius	char *t;
284145186Smarius
285145186Smarius	t = nexus_get_device_type(dev);
286145186Smarius	if (((t == NULL || strcmp(t, OFW_SBUS_TYPE) != 0)) &&
287145186Smarius	    strcmp(nexus_get_name(dev), OFW_SBUS_NAME) != 0)
288145186Smarius		return (ENXIO);
289145186Smarius	device_set_desc(dev, "U2S UPA-SBus bridge");
290145186Smarius	return (0);
291145186Smarius}
292145186Smarius
293145186Smariusstatic int
294145186Smariussbus_attach(device_t dev)
295145186Smarius{
296145185Smarius	struct sbus_softc *sc;
29790618Stmm	struct sbus_devinfo *sdi;
29890618Stmm	struct sbus_ranges *range;
29990618Stmm	struct resource *res;
30090618Stmm	device_t cdev;
30190618Stmm	bus_addr_t phys;
30290618Stmm	bus_size_t size;
303152684Smarius	char *name;
304145185Smarius	phandle_t child, node;
30590618Stmm	u_int64_t mr;
30690618Stmm	int intr, clock, rid, vec, i;
30790618Stmm
308145185Smarius	sc = device_get_softc(dev);
309145185Smarius	node = nexus_get_node(dev);
310145185Smarius
31190618Stmm	if ((sc->sc_nreg = OF_getprop_alloc(node, "reg", sizeof(*sc->sc_reg),
31290618Stmm	    (void **)&sc->sc_reg)) == -1) {
313145185Smarius		panic("%s: error getting reg property", __func__);
31490618Stmm	}
31590618Stmm	if (sc->sc_nreg < 1)
316145185Smarius		panic("%s: bogus properties", __func__);
31790618Stmm	phys = UPA_REG_PHYS(&sc->sc_reg[0]);
31890618Stmm	size = UPA_REG_SIZE(&sc->sc_reg[0]);
31990618Stmm	rid = 0;
32090618Stmm	sc->sc_sysio_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, phys,
32190618Stmm	    phys + size - 1, size, RF_ACTIVE);
32290618Stmm	if (sc->sc_sysio_res == NULL ||
32390618Stmm	    rman_get_start(sc->sc_sysio_res) != phys)
324145185Smarius		panic("%s: cannot allocate device memory", __func__);
32590618Stmm	sc->sc_bustag = rman_get_bustag(sc->sc_sysio_res);
32690618Stmm	sc->sc_bushandle = rman_get_bushandle(sc->sc_sysio_res);
32790618Stmm
32890618Stmm	if (OF_getprop(node, "interrupts", &intr, sizeof(intr)) == -1)
329145185Smarius		panic("%s: cannot get IGN", __func__);
330146391Smarius	sc->sc_ign = (intr & INTMAP_IGN_MASK) >> INTMAP_IGN_SHIFT;
33190618Stmm	sc->sc_cbustag = sbus_alloc_bustag(sc);
33290618Stmm
33390618Stmm	/*
33490618Stmm	 * Record clock frequency for synchronous SCSI.
33590618Stmm	 * IS THIS THE CORRECT DEFAULT??
33690618Stmm	 */
33790618Stmm	if (OF_getprop(node, "clock-frequency", &clock, sizeof(clock)) == -1)
33890618Stmm		clock = 25000000;
33990618Stmm	sc->sc_clockfreq = clock;
34090618Stmm	clock /= 1000;
34190618Stmm	device_printf(dev, "clock %d.%03d MHz\n", clock / 1000, clock % 1000);
34290618Stmm
34390618Stmm	/*
34490618Stmm	 * Collect address translations from the OBP.
34590618Stmm	 */
34690618Stmm	if ((sc->sc_nrange = OF_getprop_alloc(node, "ranges",
34790618Stmm	    sizeof(*range), (void **)&range)) == -1) {
348145185Smarius		panic("%s: error getting ranges property", __func__);
34990618Stmm	}
35090618Stmm	sc->sc_rd = (struct sbus_rd *)malloc(sizeof(*sc->sc_rd) * sc->sc_nrange,
35190618Stmm	    M_DEVBUF, M_NOWAIT);
35290618Stmm	if (sc->sc_rd == NULL)
353145185Smarius		panic("%s: cannot allocate rmans", __func__);
35490618Stmm	/*
35590618Stmm	 * Preallocate all space that the SBus bridge decodes, so that nothing
35690618Stmm	 * else gets in the way; set up rmans etc.
35790618Stmm	 */
35890618Stmm	for (i = 0; i < sc->sc_nrange; i++) {
35990618Stmm		phys = range[i].poffset | ((bus_addr_t)range[i].pspace << 32);
36090618Stmm		size = range[i].size;
36190618Stmm		sc->sc_rd[i].rd_slot = range[i].cspace;
36290618Stmm		sc->sc_rd[i].rd_coffset = range[i].coffset;
36390618Stmm		sc->sc_rd[i].rd_cend = sc->sc_rd[i].rd_coffset + size;
36490618Stmm		rid = 0;
36590618Stmm		if ((res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, phys,
36690618Stmm		    phys + size - 1, size, RF_ACTIVE)) == NULL)
367145185Smarius			panic("%s: cannot allocate decoded range", __func__);
36890618Stmm		sc->sc_rd[i].rd_bushandle = rman_get_bushandle(res);
36990618Stmm		sc->sc_rd[i].rd_rman.rm_type = RMAN_ARRAY;
37090618Stmm		sc->sc_rd[i].rd_rman.rm_descr = "SBus Device Memory";
37190618Stmm		if (rman_init(&sc->sc_rd[i].rd_rman) != 0 ||
37290618Stmm		    rman_manage_region(&sc->sc_rd[i].rd_rman, 0, size) != 0)
373145185Smarius			panic("%s: failed to set up memory rman", __func__);
37490618Stmm		sc->sc_rd[i].rd_poffset = phys;
37590618Stmm		sc->sc_rd[i].rd_pend = phys + size;
37690618Stmm		sc->sc_rd[i].rd_res = res;
37790618Stmm	}
37890618Stmm	free(range, M_OFWPROP);
37990618Stmm
38090618Stmm	/*
38190618Stmm	 * Get the SBus burst transfer size if burst transfers are supported.
38290618Stmm	 * XXX: is the default correct?
38390618Stmm	 */
38490618Stmm	if (OF_getprop(node, "burst-sizes", &sc->sc_burst,
38590618Stmm	    sizeof(sc->sc_burst)) == -1 || sc->sc_burst == 0)
38690618Stmm		sc->sc_burst = SBUS_BURST_DEF;
38790618Stmm
38890618Stmm	/* initalise the IOMMU */
38990618Stmm
39090618Stmm	/* punch in our copies */
39190618Stmm	sc->sc_is.is_bustag = sc->sc_bustag;
39290618Stmm	sc->sc_is.is_bushandle = sc->sc_bushandle;
39390618Stmm	sc->sc_is.is_iommu = SBR_IOMMU;
39490618Stmm	sc->sc_is.is_dtag = SBR_IOMMU_TLB_TAG_DIAG;
39590618Stmm	sc->sc_is.is_ddram = SBR_IOMMU_TLB_DATA_DIAG;
39690618Stmm	sc->sc_is.is_dqueue = SBR_IOMMU_QUEUE_DIAG;
39790618Stmm	sc->sc_is.is_dva = SBR_IOMMU_SVADIAG;
39890618Stmm	sc->sc_is.is_dtcmp = 0;
39990618Stmm	sc->sc_is.is_sb[0] = SBR_STRBUF;
400123865Sobrien	sc->sc_is.is_sb[1] = 0;
40190618Stmm
40290618Stmm	/* give us a nice name.. */
40390618Stmm	name = (char *)malloc(32, M_DEVBUF, M_NOWAIT);
404145185Smarius	if (name == NULL)
405145185Smarius		panic("%s: cannot malloc iommu name", __func__);
40690618Stmm	snprintf(name, 32, "%s dvma", device_get_name(dev));
40790618Stmm
408100188Stmm	/*
409100188Stmm	 * Note: the SBus IOMMU ignores the high bits of an address, so a NULL
410100188Stmm	 * DMA pointer will be translated by the first page of the IOTSB.
411100188Stmm	 * To detect bugs we'll allocate and ignore the first entry.
412100188Stmm	 */
413114484Stmm	iommu_init(name, &sc->sc_is, 3, -1, 1);
41490618Stmm
415116213Stmm	/* Create the DMA tag. */
416116213Stmm	sc->sc_dmatag = nexus_get_dmatag(dev);
417116213Stmm	if (bus_dma_tag_create(sc->sc_dmatag, 8, 1, 0, 0x3ffffffff, NULL, NULL,
418117126Sscottl	    0x3ffffffff, 0xff, 0xffffffff, 0, NULL, NULL, &sc->sc_cdmatag) != 0)
419145185Smarius		panic("%s: bus_dma_tag_create failed", __func__);
420116213Stmm	/* Customize the tag. */
421116213Stmm	sc->sc_cdmatag->dt_cookie = &sc->sc_is;
422116541Stmm	sc->sc_cdmatag->dt_mt = &iommu_dma_methods;
423116213Stmm	/* XXX: register as root dma tag (kludge). */
424116213Stmm	sparc64_root_dma_tag = sc->sc_cdmatag;
425116213Stmm
426145185Smarius	/* Enable the over-temperature and power-fail interrupts. */
42790618Stmm	rid = 0;
42890618Stmm	mr = SYSIO_READ8(sc, SBR_THERM_INT_MAP);
42990618Stmm	vec = INTVEC(mr);
43090618Stmm	if ((sc->sc_ot_ires = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, vec,
43190618Stmm	    vec, 1, RF_ACTIVE)) == NULL)
432145185Smarius		panic("%s: failed to get temperature interrupt", __func__);
43390618Stmm	bus_setup_intr(dev, sc->sc_ot_ires, INTR_TYPE_MISC | INTR_FAST,
43490618Stmm	    sbus_overtemp, sc, &sc->sc_ot_ihand);
435107477Stmm	SYSIO_WRITE8(sc, SBR_THERM_INT_MAP, INTMAP_ENABLE(mr, PCPU_GET(mid)));
43690618Stmm	rid = 0;
43790618Stmm	mr = SYSIO_READ8(sc, SBR_POWER_INT_MAP);
43890618Stmm	vec = INTVEC(mr);
43990618Stmm	if ((sc->sc_pf_ires = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, vec,
44090618Stmm	    vec, 1, RF_ACTIVE)) == NULL)
441145185Smarius		panic("%s: failed to get power fail interrupt", __func__);
44290618Stmm	bus_setup_intr(dev, sc->sc_pf_ires, INTR_TYPE_MISC | INTR_FAST,
44390618Stmm	    sbus_pwrfail, sc, &sc->sc_pf_ihand);
444107477Stmm	SYSIO_WRITE8(sc, SBR_POWER_INT_MAP, INTMAP_ENABLE(mr, PCPU_GET(mid)));
44590618Stmm
44690618Stmm	/* Initialize the counter-timer. */
44790618Stmm	sparc64_counter_init(sc->sc_bustag, sc->sc_bushandle, SBR_TC0);
44890618Stmm
44990618Stmm	/*
45090618Stmm	 * Loop through ROM children, fixing any relative addresses
45190618Stmm	 * and then configuring each device.
45290618Stmm	 */
45390618Stmm	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
454152684Smarius		if ((sdi = sbus_setup_dinfo(dev, sc, child)) == NULL)
45590618Stmm			continue;
456146391Smarius		/*
457146391Smarius		 * For devices where there are variants that are actually
458146391Smarius		 * split into two SBus devices (as opposed to the first
459146391Smarius		 * half of the device being a SBus device and the second
460146391Smarius		 * half hanging off of the first one) like 'auxio' and
461146391Smarius		 * 'SUNW,fdtwo' or 'dma' and 'esp' probe the SBus device
462146391Smarius		 * which is a prerequisite to the driver attaching to the
463146391Smarius		 * second one with a lower order. Saves us from dealing
464146391Smarius		 * with different probe orders in the respective device
465146391Smarius		 * drivers which generally is more hackish.
466146391Smarius		 */
467146391Smarius		cdev = device_add_child_ordered(dev, (OF_child(child) == 0 &&
468152684Smarius		    sbus_inlist(sdi->sdi_obdinfo.obd_name, sbus_order_first)) ?
469152684Smarius		    SBUS_ORDER_FIRST : SBUS_ORDER_NORMAL, NULL, -1);
470152684Smarius		if (cdev == NULL) {
471152684Smarius			device_printf(dev,
472152684Smarius			    "<%s>: device_add_child_ordered failed\n",
473152684Smarius			    sdi->sdi_obdinfo.obd_name);
474152684Smarius			sbus_destroy_dinfo(sdi);
475152684Smarius			continue;
476152684Smarius		}
47790618Stmm		device_set_ivars(cdev, sdi);
47890618Stmm	}
479145186Smarius	return (bus_generic_attach(dev));
48090618Stmm}
48190618Stmm
48290618Stmmstatic struct sbus_devinfo *
483152684Smariussbus_setup_dinfo(device_t dev, struct sbus_softc *sc, phandle_t node)
48490618Stmm{
48590618Stmm	struct sbus_devinfo *sdi;
48690618Stmm	struct sbus_regs *reg;
48790618Stmm	u_int32_t base, iv, *intr;
48890618Stmm	int i, nreg, nintr, slot, rslot;
48990618Stmm
490111119Simp	sdi = malloc(sizeof(*sdi), M_DEVBUF, M_ZERO | M_WAITOK);
491152684Smarius	if (ofw_bus_gen_setup_devinfo(&sdi->sdi_obdinfo, node) != 0) {
492152684Smarius		free(sdi, M_DEVBUF);
49390618Stmm		return (NULL);
494152684Smarius	}
49590618Stmm	resource_list_init(&sdi->sdi_rl);
49690618Stmm	slot = -1;
49790618Stmm	nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
49890618Stmm	if (nreg == -1) {
499152684Smarius		if (sdi->sdi_obdinfo.obd_type == NULL ||
500152684Smarius		    strcmp(sdi->sdi_obdinfo.obd_type, "hierarchical") != 0) {
501152684Smarius			device_printf(dev, "<%s>: incomplete\n",
502152684Smarius			    sdi->sdi_obdinfo.obd_name);
503152684Smarius			goto fail;
50490618Stmm		}
50590618Stmm	} else {
50690618Stmm		for (i = 0; i < nreg; i++) {
50790618Stmm			base = reg[i].sbr_offset;
50890618Stmm			if (SBUS_ABS(base)) {
50990618Stmm				rslot = SBUS_ABS_TO_SLOT(base);
51090618Stmm				base = SBUS_ABS_TO_OFFSET(base);
51190618Stmm			} else
51290618Stmm				rslot = reg[i].sbr_slot;
513152684Smarius			if (slot != -1 && slot != rslot) {
514152684Smarius				device_printf(dev, "<%s>: multiple slots\n",
515152684Smarius				    sdi->sdi_obdinfo.obd_name);
516152684Smarius				free(reg, M_OFWPROP);
517152684Smarius				goto fail;
518152684Smarius			}
51990618Stmm			slot = rslot;
52090618Stmm
52190618Stmm			resource_list_add(&sdi->sdi_rl, SYS_RES_MEMORY, i,
52290618Stmm			    base, base + reg[i].sbr_size, reg[i].sbr_size);
52390618Stmm		}
52490618Stmm		free(reg, M_OFWPROP);
52590618Stmm	}
52690618Stmm	sdi->sdi_slot = slot;
52790618Stmm
52890618Stmm	/*
529145185Smarius	 * The `interrupts' property contains the SBus interrupt level.
53090618Stmm	 */
531145185Smarius	nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
532145185Smarius	    (void **)&intr);
53390618Stmm	if (nintr != -1) {
53490618Stmm		for (i = 0; i < nintr; i++) {
53590618Stmm			iv = intr[i];
53690618Stmm			/*
537145185Smarius			 * SBus card devices need the slot number encoded into
53890618Stmm			 * the vector as this is generally not done.
53990618Stmm			 */
540107477Stmm			if ((iv & INTMAP_OBIO_MASK) == 0)
54190618Stmm				iv |= slot << 3;
54290618Stmm			/* Set the ign as appropriate. */
543146391Smarius			iv |= sc->sc_ign << INTMAP_IGN_SHIFT;
54490618Stmm			resource_list_add(&sdi->sdi_rl, SYS_RES_IRQ, i,
54590618Stmm			    iv, iv, 1);
54690618Stmm		}
54790618Stmm		free(intr, M_OFWPROP);
54890618Stmm	}
54990618Stmm	if (OF_getprop(node, "burst-sizes", &sdi->sdi_burstsz,
55090618Stmm	    sizeof(sdi->sdi_burstsz)) == -1)
55190618Stmm		sdi->sdi_burstsz = sc->sc_burst;
55290618Stmm	else
55390618Stmm		sdi->sdi_burstsz &= sc->sc_burst;
554145186Smarius	if (OF_getprop(node, "clock-frequency", &sdi->sdi_clockfreq,
555145186Smarius	    sizeof(sdi->sdi_clockfreq)) == -1)
556145186Smarius		sdi->sdi_clockfreq = sc->sc_clockfreq;
55790618Stmm
55890618Stmm	return (sdi);
559152684Smarius
560152684Smariusfail:
561152684Smarius	sbus_destroy_dinfo(sdi);
562152684Smarius	return (NULL);
56390618Stmm}
56490618Stmm
56590618Stmmstatic void
56690618Stmmsbus_destroy_dinfo(struct sbus_devinfo *dinfo)
56790618Stmm{
56890618Stmm
56990618Stmm	resource_list_free(&dinfo->sdi_rl);
570152684Smarius	ofw_bus_gen_destroy_devinfo(&dinfo->sdi_obdinfo);
57190618Stmm	free(dinfo, M_DEVBUF);
57290618Stmm}
57390618Stmm
57490618Stmmstatic int
57590618Stmmsbus_print_child(device_t dev, device_t child)
57690618Stmm{
57790618Stmm	int rv;
57890618Stmm
57990618Stmm	rv = bus_print_child_header(dev, child);
580152684Smarius	rv += sbus_print_res(device_get_ivars(child));
58190618Stmm	rv += bus_print_child_footer(dev, child);
58290618Stmm	return (rv);
58390618Stmm}
58490618Stmm
58590618Stmmstatic void
58690618Stmmsbus_probe_nomatch(device_t dev, device_t child)
58790618Stmm{
588152684Smarius	const char *type;
58990618Stmm
590152684Smarius	device_printf(dev, "<%s>", ofw_bus_get_name(child));
591152684Smarius	sbus_print_res(device_get_ivars(child));
592152684Smarius	type = ofw_bus_get_type(child);
593145186Smarius	printf(" type %s (no driver attached)\n",
594152684Smarius	    type != NULL ? type : "unknown");
59590618Stmm}
59690618Stmm
59790618Stmmstatic int
59890618Stmmsbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
59990618Stmm{
600146391Smarius	struct sbus_softc *sc;
60190618Stmm	struct sbus_devinfo *dinfo;
60290618Stmm
603146391Smarius	sc = device_get_softc(dev);
60490618Stmm	if ((dinfo = device_get_ivars(child)) == NULL)
60590618Stmm		return (ENOENT);
60690618Stmm	switch (which) {
60790618Stmm	case SBUS_IVAR_BURSTSZ:
60890618Stmm		*result = dinfo->sdi_burstsz;
60990618Stmm		break;
61090618Stmm	case SBUS_IVAR_CLOCKFREQ:
611145186Smarius		*result = dinfo->sdi_clockfreq;
61290618Stmm		break;
613146391Smarius	case SBUS_IVAR_IGN:
614146391Smarius		*result = sc->sc_ign;
615146391Smarius		break;
61690618Stmm	case SBUS_IVAR_SLOT:
61790618Stmm		*result = dinfo->sdi_slot;
61890618Stmm		break;
61990618Stmm	default:
62090618Stmm		return (ENOENT);
62190618Stmm	}
622146391Smarius	return (0);
62390618Stmm}
62490618Stmm
62590618Stmmstatic struct resource_list *
62690618Stmmsbus_get_resource_list(device_t dev, device_t child)
62790618Stmm{
62890618Stmm	struct sbus_devinfo *sdi;
62990618Stmm
63090618Stmm	sdi = device_get_ivars(child);
63190618Stmm	return (&sdi->sdi_rl);
63290618Stmm}
63390618Stmm
63490618Stmm/* Write to the correct clr register, and call the actual handler. */
63590618Stmmstatic void
63690618Stmmsbus_intr_stub(void *arg)
63790618Stmm{
63890618Stmm	struct sbus_clr *scl;
63990618Stmm
64090618Stmm	scl = (struct sbus_clr *)arg;
64190618Stmm	scl->scl_handler(scl->scl_arg);
64290618Stmm	SYSIO_WRITE8(scl->scl_sc, scl->scl_clr, 0);
64390618Stmm}
64490618Stmm
64590618Stmmstatic int
646145185Smariussbus_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
647145185Smarius    driver_intr_t *intr, void *arg, void **cookiep)
64890618Stmm{
64990618Stmm	struct sbus_softc *sc;
65090618Stmm	struct sbus_clr *scl;
65190618Stmm	bus_addr_t intrmapptr, intrclrptr, intrptr;
65290618Stmm	u_int64_t intrmap;
65390618Stmm	u_int32_t inr, slot;
65490618Stmm	int error, i;
65590618Stmm	long vec = rman_get_start(ires);
65690618Stmm
657145185Smarius	sc = device_get_softc(dev);
65890618Stmm	scl = (struct sbus_clr *)malloc(sizeof(*scl), M_DEVBUF, M_NOWAIT);
65990618Stmm	if (scl == NULL)
660123865Sobrien		return (0);
66190618Stmm	intrptr = intrmapptr = intrclrptr = 0;
66290618Stmm	intrmap = 0;
663107474Stmm	inr = INTVEC(vec);
664107477Stmm	if ((inr & INTMAP_OBIO_MASK) == 0) {
665107474Stmm		/*
666145185Smarius		 * We're in an SBus slot, register the map and clear
667107474Stmm		 * intr registers.
668107474Stmm		 */
669107474Stmm		slot = INTSLOT(vec);
670107474Stmm		intrmapptr = SBR_SLOT0_INT_MAP + slot * 8;
671107474Stmm		intrclrptr = SBR_SLOT0_INT_CLR +
672107474Stmm		    (slot * 8 * 8) + (INTPRI(vec) * 8);
673107474Stmm		/* Enable the interrupt, insert IGN. */
674146391Smarius		intrmap = inr | (sc->sc_ign << INTMAP_IGN_SHIFT);
675107474Stmm	} else {
676107474Stmm		intrptr = SBR_SCSI_INT_MAP;
677107474Stmm		/* Insert IGN */
678146391Smarius		inr |= sc->sc_ign << INTMAP_IGN_SHIFT;
679107474Stmm		for (i = 0; intrptr <= SBR_RESERVED_INT_MAP &&
680107474Stmm			 INTVEC(intrmap = SYSIO_READ8(sc, intrptr)) !=
681107474Stmm			 INTVEC(inr); intrptr += 8, i++)
682107474Stmm			;
683107474Stmm		if (INTVEC(intrmap) == INTVEC(inr)) {
684107474Stmm			/* Register the map and clear intr registers */
685107474Stmm			intrmapptr = intrptr;
686107474Stmm			intrclrptr = SBR_SCSI_INT_CLR + i * 8;
687107474Stmm			/* Enable the interrupt */
688107474Stmm		} else
689145185Smarius			panic("%s: IRQ not found!", __func__);
690107474Stmm	}
69190618Stmm
69290618Stmm	scl->scl_sc = sc;
69390618Stmm	scl->scl_arg = arg;
69490618Stmm	scl->scl_handler = intr;
69590618Stmm	scl->scl_clr = intrclrptr;
69690618Stmm	/* Disable the interrupt while we fiddle with it */
697107474Stmm	SYSIO_WRITE8(sc, intrmapptr, intrmap);
69890618Stmm	error = BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
69990618Stmm	    sbus_intr_stub, scl, cookiep);
70090618Stmm	if (error != 0) {
70190618Stmm		free(scl, M_DEVBUF);
70290618Stmm		return (error);
70390618Stmm	}
70490618Stmm	scl->scl_cookie = *cookiep;
70590618Stmm	*cookiep = scl;
70690618Stmm
70790618Stmm	/*
70890618Stmm	 * Clear the interrupt, it might have been triggered before it was
70990618Stmm	 * set up.
71090618Stmm	 */
711107474Stmm	SYSIO_WRITE8(sc, intrclrptr, 0);
71290618Stmm	/*
713107477Stmm	 * Enable the interrupt and program the target module now we have the
714107477Stmm	 * handler installed.
71590618Stmm	 */
716107477Stmm	SYSIO_WRITE8(sc, intrmapptr, INTMAP_ENABLE(intrmap, PCPU_GET(mid)));
71790618Stmm	return (error);
71890618Stmm}
71990618Stmm
72090618Stmmstatic int
721152684Smariussbus_teardown_intr(device_t dev, device_t child, struct resource *vec,
722152684Smarius    void *cookie)
72390618Stmm{
72490618Stmm	struct sbus_clr *scl;
72590618Stmm	int error;
72690618Stmm
72790618Stmm	scl = (struct sbus_clr *)cookie;
72890618Stmm	error = BUS_TEARDOWN_INTR(device_get_parent(dev), child, vec,
72990618Stmm	    scl->scl_cookie);
73090618Stmm	/*
731145185Smarius	 * Don't disable the interrupt for now, so that stray interrupts get
73290618Stmm	 * detected...
73390618Stmm	 */
73490618Stmm	if (error != 0)
73590618Stmm		free(scl, M_DEVBUF);
73690618Stmm	return (error);
73790618Stmm}
73890618Stmm
73990618Stmmstatic struct resource *
74090618Stmmsbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
74190618Stmm    u_long start, u_long end, u_long count, u_int flags)
74290618Stmm{
74390618Stmm	struct sbus_softc *sc;
74490618Stmm	struct rman *rm;
74590618Stmm	struct resource *rv;
74690618Stmm	struct resource_list *rl;
74790618Stmm	struct resource_list_entry *rle;
748145186Smarius	device_t schild;
74990618Stmm	bus_space_handle_t bh;
75090618Stmm	bus_addr_t toffs;
75190618Stmm	bus_size_t tend;
752145186Smarius	int i, slot;
753145186Smarius	int isdefault, needactivate, passthrough;
75490618Stmm
755145185Smarius	isdefault = (start == 0UL && end == ~0UL);
756145185Smarius	needactivate = flags & RF_ACTIVE;
757145186Smarius	passthrough = (device_get_parent(child) != bus);
758145186Smarius	rle = NULL;
759145186Smarius	sc = device_get_softc(bus);
760145186Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
76190618Stmm	switch (type) {
76290618Stmm	case SYS_RES_IRQ:
763145186Smarius		return (resource_list_alloc(rl, bus, child, type, rid, start,
764145186Smarius		    end, count, flags));
76590618Stmm	case SYS_RES_MEMORY:
766145186Smarius		if (!passthrough) {
767145186Smarius			rle = resource_list_find(rl, type, *rid);
768145186Smarius			if (rle == NULL)
769145186Smarius				return (NULL);
770145186Smarius			if (rle->res != NULL)
771145186Smarius				panic("%s: resource entry is busy", __func__);
772145186Smarius			if (isdefault) {
773145186Smarius				start = rle->start;
774145186Smarius				count = ulmax(count, rle->count);
775145186Smarius				end = ulmax(rle->end, start + count - 1);
776145186Smarius			}
777145186Smarius		}
77890618Stmm		rm = NULL;
77990618Stmm		bh = toffs = tend = 0;
780145186Smarius		schild = child;
781145186Smarius		while (device_get_parent(schild) != bus)
782145186Smarius			schild = device_get_parent(child);
783145186Smarius		slot = sbus_get_slot(schild);
78490618Stmm		for (i = 0; i < sc->sc_nrange; i++) {
785145186Smarius			if (sc->sc_rd[i].rd_slot != slot ||
78690618Stmm			    start < sc->sc_rd[i].rd_coffset ||
78790618Stmm			    start > sc->sc_rd[i].rd_cend)
78890618Stmm				continue;
78990618Stmm			/* Disallow cross-range allocations. */
79090618Stmm			if (end > sc->sc_rd[i].rd_cend)
79190618Stmm				return (NULL);
79290618Stmm			/* We've found the connection to the parent bus */
79390618Stmm			toffs = start - sc->sc_rd[i].rd_coffset;
79490618Stmm			tend = end - sc->sc_rd[i].rd_coffset;
79590618Stmm			rm = &sc->sc_rd[i].rd_rman;
79690618Stmm			bh = sc->sc_rd[i].rd_bushandle;
79790618Stmm		}
798123865Sobrien		if (toffs == 0L)
79990618Stmm			return (NULL);
80090618Stmm		flags &= ~RF_ACTIVE;
80190618Stmm		rv = rman_reserve_resource(rm, toffs, tend, count, flags,
80290618Stmm		    child);
80390618Stmm		if (rv == NULL)
80490618Stmm			return (NULL);
80590618Stmm		rman_set_bustag(rv, sc->sc_cbustag);
80690618Stmm		rman_set_bushandle(rv, bh + rman_get_start(rv));
80790618Stmm		if (needactivate) {
80890618Stmm			if (bus_activate_resource(child, type, *rid, rv)) {
80990618Stmm				rman_release_resource(rv);
81090618Stmm				return (NULL);
81190618Stmm			}
81290618Stmm		}
813145186Smarius		if (!passthrough)
814145186Smarius			rle->res = rv;
815145186Smarius		return (rv);
81690618Stmm	default:
81790618Stmm		return (NULL);
81890618Stmm	}
81990618Stmm}
82090618Stmm
82190618Stmmstatic int
82290618Stmmsbus_activate_resource(device_t bus, device_t child, int type, int rid,
82390618Stmm    struct resource *r)
82490618Stmm{
82590618Stmm
826108798Stmm	if (type == SYS_RES_IRQ) {
827108798Stmm		return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
828108798Stmm		    child, type, rid, r));
829108798Stmm	}
83090618Stmm	return (rman_activate_resource(r));
83190618Stmm}
83290618Stmm
83390618Stmmstatic int
83490618Stmmsbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
83590618Stmm    struct resource *r)
83690618Stmm{
83790618Stmm
838108798Stmm	if (type == SYS_RES_IRQ) {
839108798Stmm		return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus),
840108798Stmm		    child, type, rid, r));
841108798Stmm	}
84290618Stmm	return (rman_deactivate_resource(r));
84390618Stmm}
84490618Stmm
84590618Stmmstatic int
84690618Stmmsbus_release_resource(device_t bus, device_t child, int type, int rid,
84790618Stmm    struct resource *r)
84890618Stmm{
849145186Smarius	struct resource_list *rl;
850108798Stmm	struct resource_list_entry *rle;
851145186Smarius	int error, passthrough;
85290618Stmm
853145186Smarius	passthrough = (device_get_parent(child) != bus);
854145186Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
85590618Stmm	if (type == SYS_RES_IRQ)
856145186Smarius		return (resource_list_release(rl, bus, child, type, rid, r));
857145186Smarius	if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
858145186Smarius		error = bus_deactivate_resource(child, type, rid, r);
859108798Stmm		if (error != 0)
860108798Stmm			return (error);
86190618Stmm	}
862145186Smarius	error = rman_release_resource(r);
863145186Smarius	if (error != 0 || passthrough)
864108798Stmm		return (error);
865145186Smarius	rle = resource_list_find(rl, type, rid);
866108798Stmm	if (rle == NULL)
867145185Smarius		panic("%s: cannot find resource", __func__);
868108798Stmm	if (rle->res == NULL)
869145185Smarius		panic("%s: resource entry is not busy", __func__);
870108798Stmm	rle->res = NULL;
871108798Stmm	return (0);
87290618Stmm}
87390618Stmm
874152684Smariusstatic const struct ofw_bus_devinfo *
875152684Smariussbus_get_devinfo(device_t bus, device_t child)
876152684Smarius{
877152684Smarius	struct sbus_devinfo *sdi;
878152684Smarius
879152684Smarius	sdi = device_get_ivars(child);
880152684Smarius	return (&sdi->sdi_obdinfo);
881152684Smarius}
882152684Smarius
88390618Stmm/*
88490618Stmm * Handle an overtemp situation.
88590618Stmm *
88690618Stmm * SPARCs have temperature sensors which generate interrupts
88790618Stmm * if the machine's temperature exceeds a certain threshold.
88890618Stmm * This handles the interrupt and powers off the machine.
88990618Stmm * The same needs to be done to PCI controller drivers.
89090618Stmm */
89190618Stmmstatic void
89290618Stmmsbus_overtemp(void *arg)
89390618Stmm{
89490618Stmm
89590618Stmm	printf("DANGER: OVER TEMPERATURE detected\nShutting down NOW.\n");
89690618Stmm	shutdown_nice(RB_POWEROFF);
89790618Stmm}
89890618Stmm
89990618Stmm/* Try to shut down in time in case of power failure. */
90090618Stmmstatic void
90190618Stmmsbus_pwrfail(void *arg)
90290618Stmm{
90390618Stmm
90490618Stmm	printf("Power failure detected\nShutting down NOW.\n");
90590618Stmm	shutdown_nice(0);
90690618Stmm}
90790618Stmm
90890618Stmmstatic bus_space_tag_t
90990618Stmmsbus_alloc_bustag(struct sbus_softc *sc)
91090618Stmm{
91190618Stmm	bus_space_tag_t sbt;
91290618Stmm
91390618Stmm	sbt = (bus_space_tag_t)malloc(sizeof(struct bus_space_tag), M_DEVBUF,
91490618Stmm	    M_NOWAIT | M_ZERO);
91590618Stmm	if (sbt == NULL)
916145185Smarius		panic("%s: out of memory", __func__);
91790618Stmm
918108815Stmm	sbt->bst_cookie = sc;
919108815Stmm	sbt->bst_parent = sc->sc_bustag;
920108815Stmm	sbt->bst_type = SBUS_BUS_SPACE;
92190618Stmm	return (sbt);
92290618Stmm}
923133589Smarius
924152684Smariusstatic int
925152684Smariussbus_print_res(struct sbus_devinfo *sdi)
926133589Smarius{
927152684Smarius	int rv;
928133589Smarius
929152684Smarius	rv = 0;
930152684Smarius	rv += resource_list_print_type(&sdi->sdi_rl, "mem", SYS_RES_MEMORY,
931152684Smarius	    "%#lx");
932152684Smarius	rv += resource_list_print_type(&sdi->sdi_rl, "irq", SYS_RES_IRQ,
933152684Smarius	    "%ld");
934152684Smarius	return (rv);
935133589Smarius}
936