xref: /netbsd-src/sys/arch/sparc/dev/bootbus.c (revision 3944ff70a48a86bb622139705a33f8ef461e24f7)
1*3944ff70Sthorpej /*	$NetBSD: bootbus.c,v 1.24 2022/01/22 11:49:16 thorpej Exp $	*/
241c25cb6Sthorpej 
341c25cb6Sthorpej /*-
441c25cb6Sthorpej  * Copyright (c) 2002 The NetBSD Foundation, Inc.
541c25cb6Sthorpej  * All rights reserved.
641c25cb6Sthorpej  *
741c25cb6Sthorpej  * This code is derived from software contributed to The NetBSD Foundation
841c25cb6Sthorpej  * by Jason R. Thorpe.
941c25cb6Sthorpej  *
1041c25cb6Sthorpej  * Redistribution and use in source and binary forms, with or without
1141c25cb6Sthorpej  * modification, are permitted provided that the following conditions
1241c25cb6Sthorpej  * are met:
1341c25cb6Sthorpej  * 1. Redistributions of source code must retain the above copyright
1441c25cb6Sthorpej  *    notice, this list of conditions and the following disclaimer.
1541c25cb6Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1641c25cb6Sthorpej  *    notice, this list of conditions and the following disclaimer in the
1741c25cb6Sthorpej  *    documentation and/or other materials provided with the distribution.
1841c25cb6Sthorpej  *
1941c25cb6Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2041c25cb6Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2141c25cb6Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2241c25cb6Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2341c25cb6Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2441c25cb6Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2541c25cb6Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2641c25cb6Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2741c25cb6Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2841c25cb6Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2941c25cb6Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
3041c25cb6Sthorpej  */
3141c25cb6Sthorpej 
3241c25cb6Sthorpej /*
3341c25cb6Sthorpej  * Autoconfiguration support for Sun4d "bootbus".
3441c25cb6Sthorpej  */
3541c25cb6Sthorpej 
36a4183603Slukem #include <sys/cdefs.h>
37*3944ff70Sthorpej __KERNEL_RCSID(0, "$NetBSD: bootbus.c,v 1.24 2022/01/22 11:49:16 thorpej Exp $");
38a4183603Slukem 
3941c25cb6Sthorpej #include <sys/param.h>
4041c25cb6Sthorpej #include <sys/malloc.h>
4138e52ce4Sthorpej #include <sys/kmem.h>
4241c25cb6Sthorpej #include <sys/systm.h>
4341c25cb6Sthorpej #include <sys/device.h>
4441c25cb6Sthorpej 
450570742aSthorpej #include <machine/autoconf.h>
46b6584574Sdyoung #include <sys/bus.h>
4741c25cb6Sthorpej 
4841c25cb6Sthorpej #include <sparc/sparc/cpuunitvar.h>
4941c25cb6Sthorpej #include <sparc/dev/bootbusvar.h>
5041c25cb6Sthorpej 
5141c25cb6Sthorpej #include "locators.h"
5241c25cb6Sthorpej 
5341c25cb6Sthorpej struct bootbus_softc {
5441c25cb6Sthorpej 	int sc_node;				/* our OBP node */
5541c25cb6Sthorpej 
5641c25cb6Sthorpej 	bus_space_tag_t sc_st;			/* ours */
5741c25cb6Sthorpej 	bus_space_tag_t sc_bustag;		/* passed on to children */
5841c25cb6Sthorpej };
5941c25cb6Sthorpej 
60102f0440Stsutsui static int bootbus_match(device_t, cfdata_t, void *);
61102f0440Stsutsui static void bootbus_attach(device_t, device_t, void *);
6241c25cb6Sthorpej 
63142a0210Smrg CFATTACH_DECL_NEW(bootbus, sizeof(struct bootbus_softc),
644bf871a7Sthorpej     bootbus_match, bootbus_attach, NULL, NULL);
6541c25cb6Sthorpej 
66142a0210Smrg static int bootbus_submatch(device_t, cfdata_t, const int *, void *);
6741c25cb6Sthorpej static int bootbus_print(void *, const char *);
6841c25cb6Sthorpej 
6941c25cb6Sthorpej static int bootbus_setup_attach_args(struct bootbus_softc *, bus_space_tag_t,
7041c25cb6Sthorpej     int, struct bootbus_attach_args *);
7141c25cb6Sthorpej static void bootbus_destroy_attach_args(struct bootbus_attach_args *);
7241c25cb6Sthorpej 
7341c25cb6Sthorpej static int
bootbus_match(device_t parent,cfdata_t cf,void * aux)74102f0440Stsutsui bootbus_match(device_t parent, cfdata_t cf, void *aux)
7541c25cb6Sthorpej {
7641c25cb6Sthorpej 	struct cpuunit_attach_args *cpua = aux;
7741c25cb6Sthorpej 
78d1ad2ac4Sthorpej 	if (strcmp(cpua->cpua_name, cf->cf_name) == 0)
7941c25cb6Sthorpej 		return (1);
8041c25cb6Sthorpej 
8141c25cb6Sthorpej 	return (0);
8241c25cb6Sthorpej }
8341c25cb6Sthorpej 
8441c25cb6Sthorpej static void
bootbus_attach(device_t parent,device_t self,void * aux)85102f0440Stsutsui bootbus_attach(device_t parent, device_t self, void *aux)
8641c25cb6Sthorpej {
87102f0440Stsutsui 	struct bootbus_softc *sc = device_private(self);
8841c25cb6Sthorpej 	struct cpuunit_attach_args *cpua = aux;
8941c25cb6Sthorpej 	int node, error;
9041c25cb6Sthorpej 
9141c25cb6Sthorpej 	sc->sc_node = cpua->cpua_node;
9241c25cb6Sthorpej 	sc->sc_st = cpua->cpua_bustag;
9341c25cb6Sthorpej 
9441c25cb6Sthorpej 	printf("\n");
9541c25cb6Sthorpej 
9641c25cb6Sthorpej 	/*
9741c25cb6Sthorpej 	 * Initialize the bus space tag we pass on to our children.
9841c25cb6Sthorpej 	 */
9938e52ce4Sthorpej 	sc->sc_bustag = kmem_zalloc(sizeof(*sc->sc_bustag), KM_SLEEP);
10041c25cb6Sthorpej 	sc->sc_bustag->cookie = sc;
10141c25cb6Sthorpej 	sc->sc_bustag->parent = sc->sc_st;
102a9f285b0Sthorpej 	sc->sc_bustag->sparc_bus_map = sc->sc_st->sparc_bus_map;
103a9f285b0Sthorpej 	sc->sc_bustag->sparc_bus_mmap = sc->sc_st->sparc_bus_mmap;
10441c25cb6Sthorpej 
10541c25cb6Sthorpej 	/*
10641c25cb6Sthorpej 	 * Collect address translations from the OBP.
10741c25cb6Sthorpej 	 */
108ea53363eSpk 	error = prom_getprop(sc->sc_node, "ranges",
109a9f285b0Sthorpej 	    sizeof(struct openprom_range), &sc->sc_bustag->nranges,
1109e599bdbSmrg 	    &sc->sc_bustag->ranges);
11141c25cb6Sthorpej 	if (error) {
11241c25cb6Sthorpej 		printf("%s: error %d getting \"ranges\" property\n",
113102f0440Stsutsui 		    device_xname(self), error);
11441c25cb6Sthorpej 		panic("bootbus_attach");
11541c25cb6Sthorpej 	}
11641c25cb6Sthorpej 
11741c25cb6Sthorpej 	/* Attach the CPU (and possibly bootbus) child nodes. */
118*3944ff70Sthorpej 	devhandle_t selfh = device_handle(self);
11941c25cb6Sthorpej 	for (node = firstchild(sc->sc_node); node != 0;
12041c25cb6Sthorpej 	     node = nextsibling(node)) {
12141c25cb6Sthorpej 		struct bootbus_attach_args baa;
12241c25cb6Sthorpej 
12341c25cb6Sthorpej 		if (bootbus_setup_attach_args(sc, sc->sc_bustag, node, &baa))
12441c25cb6Sthorpej 			panic("bootbus_attach: failed to set up attach args");
12541c25cb6Sthorpej 
1262685996bSthorpej 		config_found(self, &baa, bootbus_print,
127*3944ff70Sthorpej 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh, node),
128c7fb772bSthorpej 			   .submatch = bootbus_submatch));
12941c25cb6Sthorpej 
13041c25cb6Sthorpej 		bootbus_destroy_attach_args(&baa);
13141c25cb6Sthorpej 	}
13241c25cb6Sthorpej }
13341c25cb6Sthorpej 
13441c25cb6Sthorpej static int
bootbus_submatch(device_t parent,cfdata_t cf,const int * ldesc,void * aux)135102f0440Stsutsui bootbus_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
13641c25cb6Sthorpej {
13741c25cb6Sthorpej 	struct bootbus_attach_args *baa = aux;
13841c25cb6Sthorpej 
13941c25cb6Sthorpej 	if (cf->cf_loc[BOOTBUSCF_SLOT] != BOOTBUSCF_SLOT_DEFAULT &&
14041c25cb6Sthorpej 	    cf->cf_loc[BOOTBUSCF_SLOT] != baa->ba_slot)
14141c25cb6Sthorpej 		return (0);
14241c25cb6Sthorpej 
14341c25cb6Sthorpej 	if (cf->cf_loc[BOOTBUSCF_OFFSET] != BOOTBUSCF_OFFSET_DEFAULT &&
14441c25cb6Sthorpej 	    cf->cf_loc[BOOTBUSCF_OFFSET] != baa->ba_offset)
14541c25cb6Sthorpej 		return (0);
14641c25cb6Sthorpej 
1476c88de3bSthorpej 	return (config_match(parent, cf, aux));
14841c25cb6Sthorpej }
14941c25cb6Sthorpej 
15041c25cb6Sthorpej static int
bootbus_print(void * aux,const char * pnp)15141c25cb6Sthorpej bootbus_print(void *aux, const char *pnp)
15241c25cb6Sthorpej {
15341c25cb6Sthorpej 	struct bootbus_attach_args *baa = aux;
15441c25cb6Sthorpej 	int i;
15541c25cb6Sthorpej 
15641c25cb6Sthorpej 	if (pnp)
1579c121415Sthorpej 		aprint_normal("%s at %s", baa->ba_name, pnp);
1589c121415Sthorpej 	aprint_normal(" slot %d offset 0x%x", baa->ba_slot, baa->ba_offset);
15941c25cb6Sthorpej 	for (i = 0; i < baa->ba_nintr; i++)
1609c121415Sthorpej 		aprint_normal(" ipl %d", baa->ba_intr[i].oi_pri);
16141c25cb6Sthorpej 
16241c25cb6Sthorpej 	return (UNCONF);
16341c25cb6Sthorpej }
16441c25cb6Sthorpej 
16541c25cb6Sthorpej static int
bootbus_setup_attach_args(struct bootbus_softc * sc,bus_space_tag_t bustag,int node,struct bootbus_attach_args * baa)16641c25cb6Sthorpej bootbus_setup_attach_args(struct bootbus_softc *sc, bus_space_tag_t bustag,
16741c25cb6Sthorpej     int node, struct bootbus_attach_args *baa)
16841c25cb6Sthorpej {
16941c25cb6Sthorpej 	int n, error;
17041c25cb6Sthorpej 
17141c25cb6Sthorpej 	memset(baa, 0, sizeof(*baa));
17241c25cb6Sthorpej 
173ea53363eSpk 	error = prom_getprop(node, "name", 1, &n, &baa->ba_name);
17441c25cb6Sthorpej 	if (error)
17541c25cb6Sthorpej 		return (error);
17641c25cb6Sthorpej 	baa->ba_name[n] = '\0';
17741c25cb6Sthorpej 
17841c25cb6Sthorpej 	baa->ba_bustag = bustag;
17941c25cb6Sthorpej 	baa->ba_node = node;
18041c25cb6Sthorpej 
181ea53363eSpk 	error = prom_getprop(node, "reg", sizeof(struct openprom_addr),
1829e599bdbSmrg 	    &baa->ba_nreg, &baa->ba_reg);
18341c25cb6Sthorpej 	if (error) {
18441c25cb6Sthorpej 		bootbus_destroy_attach_args(baa);
18541c25cb6Sthorpej 		return (error);
18641c25cb6Sthorpej 	}
18741c25cb6Sthorpej 
188ea53363eSpk 	error = prom_getprop(node, "intr", sizeof(struct openprom_intr),
1899e599bdbSmrg 	    &baa->ba_nintr, &baa->ba_intr);
19041c25cb6Sthorpej 	if (error != 0 && error != ENOENT) {
19141c25cb6Sthorpej 		bootbus_destroy_attach_args(baa);
19241c25cb6Sthorpej 		return (error);
19341c25cb6Sthorpej 	}
19441c25cb6Sthorpej 
195ea53363eSpk 	error = prom_getprop(node, "address", sizeof(uint32_t),
1969e599bdbSmrg 	    &baa->ba_npromvaddrs, &baa->ba_promvaddrs);
19741c25cb6Sthorpej 	if (error != 0 && error != ENOENT) {
19841c25cb6Sthorpej 		bootbus_destroy_attach_args(baa);
19941c25cb6Sthorpej 		return (error);
20041c25cb6Sthorpej 	}
20141c25cb6Sthorpej 
20241c25cb6Sthorpej 	return (0);
20341c25cb6Sthorpej }
20441c25cb6Sthorpej 
20541c25cb6Sthorpej static void
bootbus_destroy_attach_args(struct bootbus_attach_args * baa)20641c25cb6Sthorpej bootbus_destroy_attach_args(struct bootbus_attach_args *baa)
20741c25cb6Sthorpej {
20841c25cb6Sthorpej 
20941c25cb6Sthorpej 	if (baa->ba_name != NULL)
21041c25cb6Sthorpej 		free(baa->ba_name, M_DEVBUF);
21141c25cb6Sthorpej 
21241c25cb6Sthorpej 	if (baa->ba_reg != NULL)
21341c25cb6Sthorpej 		free(baa->ba_reg, M_DEVBUF);
21441c25cb6Sthorpej 
21541c25cb6Sthorpej 	if (baa->ba_intr != NULL)
21641c25cb6Sthorpej 		free(baa->ba_intr, M_DEVBUF);
21741c25cb6Sthorpej 
21841c25cb6Sthorpej 	if (baa->ba_promvaddrs != NULL)
21941c25cb6Sthorpej 		free(baa->ba_promvaddrs, M_DEVBUF);
22041c25cb6Sthorpej }
221