Home | History | Annotate | Line # | Download | only in dev
bootbus.c revision 1.23
      1 /*	$NetBSD: bootbus.c,v 1.23 2021/08/07 16:19:05 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Autoconfiguration support for Sun4d "bootbus".
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: bootbus.c,v 1.23 2021/08/07 16:19:05 thorpej Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/malloc.h>
     41 #include <sys/kmem.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 
     45 #include <machine/autoconf.h>
     46 #include <sys/bus.h>
     47 
     48 #include <sparc/sparc/cpuunitvar.h>
     49 #include <sparc/dev/bootbusvar.h>
     50 
     51 #include "locators.h"
     52 
     53 struct bootbus_softc {
     54 	int sc_node;				/* our OBP node */
     55 
     56 	bus_space_tag_t sc_st;			/* ours */
     57 	bus_space_tag_t sc_bustag;		/* passed on to children */
     58 };
     59 
     60 static int bootbus_match(device_t, cfdata_t, void *);
     61 static void bootbus_attach(device_t, device_t, void *);
     62 
     63 CFATTACH_DECL_NEW(bootbus, sizeof(struct bootbus_softc),
     64     bootbus_match, bootbus_attach, NULL, NULL);
     65 
     66 static int bootbus_submatch(device_t, cfdata_t, const int *, void *);
     67 static int bootbus_print(void *, const char *);
     68 
     69 static int bootbus_setup_attach_args(struct bootbus_softc *, bus_space_tag_t,
     70     int, struct bootbus_attach_args *);
     71 static void bootbus_destroy_attach_args(struct bootbus_attach_args *);
     72 
     73 static int
     74 bootbus_match(device_t parent, cfdata_t cf, void *aux)
     75 {
     76 	struct cpuunit_attach_args *cpua = aux;
     77 
     78 	if (strcmp(cpua->cpua_name, cf->cf_name) == 0)
     79 		return (1);
     80 
     81 	return (0);
     82 }
     83 
     84 static void
     85 bootbus_attach(device_t parent, device_t self, void *aux)
     86 {
     87 	struct bootbus_softc *sc = device_private(self);
     88 	struct cpuunit_attach_args *cpua = aux;
     89 	int node, error;
     90 
     91 	sc->sc_node = cpua->cpua_node;
     92 	sc->sc_st = cpua->cpua_bustag;
     93 
     94 	printf("\n");
     95 
     96 	/*
     97 	 * Initialize the bus space tag we pass on to our children.
     98 	 */
     99 	sc->sc_bustag = kmem_zalloc(sizeof(*sc->sc_bustag), KM_SLEEP);
    100 	sc->sc_bustag->cookie = sc;
    101 	sc->sc_bustag->parent = sc->sc_st;
    102 	sc->sc_bustag->sparc_bus_map = sc->sc_st->sparc_bus_map;
    103 	sc->sc_bustag->sparc_bus_mmap = sc->sc_st->sparc_bus_mmap;
    104 
    105 	/*
    106 	 * Collect address translations from the OBP.
    107 	 */
    108 	error = prom_getprop(sc->sc_node, "ranges",
    109 	    sizeof(struct openprom_range), &sc->sc_bustag->nranges,
    110 	    &sc->sc_bustag->ranges);
    111 	if (error) {
    112 		printf("%s: error %d getting \"ranges\" property\n",
    113 		    device_xname(self), error);
    114 		panic("bootbus_attach");
    115 	}
    116 
    117 	/* Attach the CPU (and possibly bootbus) child nodes. */
    118 	for (node = firstchild(sc->sc_node); node != 0;
    119 	     node = nextsibling(node)) {
    120 		struct bootbus_attach_args baa;
    121 
    122 		if (bootbus_setup_attach_args(sc, sc->sc_bustag, node, &baa))
    123 			panic("bootbus_attach: failed to set up attach args");
    124 
    125 		config_found(self, &baa, bootbus_print,
    126 		    CFARGS(.devhandle = prom_node_to_devhandle(node),
    127 			   .submatch = bootbus_submatch));
    128 
    129 		bootbus_destroy_attach_args(&baa);
    130 	}
    131 }
    132 
    133 static int
    134 bootbus_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    135 {
    136 	struct bootbus_attach_args *baa = aux;
    137 
    138 	if (cf->cf_loc[BOOTBUSCF_SLOT] != BOOTBUSCF_SLOT_DEFAULT &&
    139 	    cf->cf_loc[BOOTBUSCF_SLOT] != baa->ba_slot)
    140 		return (0);
    141 
    142 	if (cf->cf_loc[BOOTBUSCF_OFFSET] != BOOTBUSCF_OFFSET_DEFAULT &&
    143 	    cf->cf_loc[BOOTBUSCF_OFFSET] != baa->ba_offset)
    144 		return (0);
    145 
    146 	return (config_match(parent, cf, aux));
    147 }
    148 
    149 static int
    150 bootbus_print(void *aux, const char *pnp)
    151 {
    152 	struct bootbus_attach_args *baa = aux;
    153 	int i;
    154 
    155 	if (pnp)
    156 		aprint_normal("%s at %s", baa->ba_name, pnp);
    157 	aprint_normal(" slot %d offset 0x%x", baa->ba_slot, baa->ba_offset);
    158 	for (i = 0; i < baa->ba_nintr; i++)
    159 		aprint_normal(" ipl %d", baa->ba_intr[i].oi_pri);
    160 
    161 	return (UNCONF);
    162 }
    163 
    164 static int
    165 bootbus_setup_attach_args(struct bootbus_softc *sc, bus_space_tag_t bustag,
    166     int node, struct bootbus_attach_args *baa)
    167 {
    168 	int n, error;
    169 
    170 	memset(baa, 0, sizeof(*baa));
    171 
    172 	error = prom_getprop(node, "name", 1, &n, &baa->ba_name);
    173 	if (error)
    174 		return (error);
    175 	baa->ba_name[n] = '\0';
    176 
    177 	baa->ba_bustag = bustag;
    178 	baa->ba_node = node;
    179 
    180 	error = prom_getprop(node, "reg", sizeof(struct openprom_addr),
    181 	    &baa->ba_nreg, &baa->ba_reg);
    182 	if (error) {
    183 		bootbus_destroy_attach_args(baa);
    184 		return (error);
    185 	}
    186 
    187 	error = prom_getprop(node, "intr", sizeof(struct openprom_intr),
    188 	    &baa->ba_nintr, &baa->ba_intr);
    189 	if (error != 0 && error != ENOENT) {
    190 		bootbus_destroy_attach_args(baa);
    191 		return (error);
    192 	}
    193 
    194 	error = prom_getprop(node, "address", sizeof(uint32_t),
    195 	    &baa->ba_npromvaddrs, &baa->ba_promvaddrs);
    196 	if (error != 0 && error != ENOENT) {
    197 		bootbus_destroy_attach_args(baa);
    198 		return (error);
    199 	}
    200 
    201 	return (0);
    202 }
    203 
    204 static void
    205 bootbus_destroy_attach_args(struct bootbus_attach_args *baa)
    206 {
    207 
    208 	if (baa->ba_name != NULL)
    209 		free(baa->ba_name, M_DEVBUF);
    210 
    211 	if (baa->ba_reg != NULL)
    212 		free(baa->ba_reg, M_DEVBUF);
    213 
    214 	if (baa->ba_intr != NULL)
    215 		free(baa->ba_intr, M_DEVBUF);
    216 
    217 	if (baa->ba_promvaddrs != NULL)
    218 		free(baa->ba_promvaddrs, M_DEVBUF);
    219 }
    220