1 /* $NetBSD: bootbus.c,v 1.3 2002/08/25 17:54:57 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 * 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 * Autoconfiguration support for Sun4d "bootbus". 41 */ 42 43 #include <sys/param.h> 44 #include <sys/malloc.h> 45 #include <sys/systm.h> 46 #include <sys/device.h> 47 48 #include <machine/autoconf.h> 49 #include <machine/bus.h> 50 51 #include <sparc/sparc/cpuunitvar.h> 52 #include <sparc/dev/bootbusvar.h> 53 54 #include "locators.h" 55 56 struct bootbus_softc { 57 struct device sc_dev; 58 int sc_node; /* our OBP node */ 59 60 bus_space_tag_t sc_st; /* ours */ 61 bus_space_tag_t sc_bustag; /* passed on to children */ 62 }; 63 64 static int bootbus_match(struct device *, struct cfdata *, void *); 65 static void bootbus_attach(struct device *, struct device *, void *); 66 67 struct cfattach bootbus_ca = { 68 sizeof(struct bootbus_softc), bootbus_match, bootbus_attach, 69 }; 70 71 static int bootbus_submatch(struct device *, struct cfdata *, void *); 72 static int bootbus_print(void *, const char *); 73 74 static int bootbus_setup_attach_args(struct bootbus_softc *, bus_space_tag_t, 75 int, struct bootbus_attach_args *); 76 static void bootbus_destroy_attach_args(struct bootbus_attach_args *); 77 78 static int 79 bootbus_match(struct device *parent, struct cfdata *cf, void *aux) 80 { 81 struct cpuunit_attach_args *cpua = aux; 82 83 if (strcmp(cpua->cpua_name, cf->cf_driver->cd_name) == 0) 84 return (1); 85 86 return (0); 87 } 88 89 static void 90 bootbus_attach(struct device *parent, struct device *self, void *aux) 91 { 92 struct bootbus_softc *sc = (void *) self; 93 struct cpuunit_attach_args *cpua = aux; 94 int node, error; 95 96 sc->sc_node = cpua->cpua_node; 97 sc->sc_st = cpua->cpua_bustag; 98 99 printf("\n"); 100 101 /* 102 * Initialize the bus space tag we pass on to our children. 103 */ 104 sc->sc_bustag = malloc(sizeof(*sc->sc_bustag), M_DEVBUF, 105 M_WAITOK|M_ZERO); 106 sc->sc_bustag->cookie = sc; 107 sc->sc_bustag->parent = sc->sc_st; 108 sc->sc_bustag->sparc_bus_map = sc->sc_st->sparc_bus_map; 109 sc->sc_bustag->sparc_bus_mmap = sc->sc_st->sparc_bus_mmap; 110 111 /* 112 * Collect address translations from the OBP. 113 */ 114 error = PROM_getprop(sc->sc_node, "ranges", 115 sizeof(struct openprom_range), &sc->sc_bustag->nranges, 116 (void **) &sc->sc_bustag->ranges); 117 if (error) { 118 printf("%s: error %d getting \"ranges\" property\n", 119 sc->sc_dev.dv_xname, error); 120 panic("bootbus_attach"); 121 } 122 123 /* Attach the CPU (and possibly bootbus) child nodes. */ 124 for (node = firstchild(sc->sc_node); node != 0; 125 node = nextsibling(node)) { 126 struct bootbus_attach_args baa; 127 128 if (bootbus_setup_attach_args(sc, sc->sc_bustag, node, &baa)) 129 panic("bootbus_attach: failed to set up attach args"); 130 131 (void) config_found_sm(&sc->sc_dev, &baa, bootbus_print, 132 bootbus_submatch); 133 134 bootbus_destroy_attach_args(&baa); 135 } 136 } 137 138 static int 139 bootbus_submatch(struct device *parent, struct cfdata *cf, void *aux) 140 { 141 struct bootbus_attach_args *baa = aux; 142 143 if (cf->cf_loc[BOOTBUSCF_SLOT] != BOOTBUSCF_SLOT_DEFAULT && 144 cf->cf_loc[BOOTBUSCF_SLOT] != baa->ba_slot) 145 return (0); 146 147 if (cf->cf_loc[BOOTBUSCF_OFFSET] != BOOTBUSCF_OFFSET_DEFAULT && 148 cf->cf_loc[BOOTBUSCF_OFFSET] != baa->ba_offset) 149 return (0); 150 151 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 152 } 153 154 static int 155 bootbus_print(void *aux, const char *pnp) 156 { 157 struct bootbus_attach_args *baa = aux; 158 int i; 159 160 if (pnp) 161 printf("%s at %s", baa->ba_name, pnp); 162 printf(" slot %d offset 0x%x", baa->ba_slot, baa->ba_offset); 163 for (i = 0; i < baa->ba_nintr; i++) 164 printf(" ipl %d", baa->ba_intr[i].oi_pri); 165 166 return (UNCONF); 167 } 168 169 static int 170 bootbus_setup_attach_args(struct bootbus_softc *sc, bus_space_tag_t bustag, 171 int node, struct bootbus_attach_args *baa) 172 { 173 int n, error; 174 175 memset(baa, 0, sizeof(*baa)); 176 177 error = PROM_getprop(node, "name", 1, &n, (void **) &baa->ba_name); 178 if (error) 179 return (error); 180 baa->ba_name[n] = '\0'; 181 182 baa->ba_bustag = bustag; 183 baa->ba_node = node; 184 185 error = PROM_getprop(node, "reg", sizeof(struct openprom_addr), 186 &baa->ba_nreg, (void **) &baa->ba_reg); 187 if (error) { 188 bootbus_destroy_attach_args(baa); 189 return (error); 190 } 191 192 error = PROM_getprop(node, "intr", sizeof(struct openprom_intr), 193 &baa->ba_nintr, (void **) &baa->ba_intr); 194 if (error != 0 && error != ENOENT) { 195 bootbus_destroy_attach_args(baa); 196 return (error); 197 } 198 199 error = PROM_getprop(node, "address", sizeof(uint32_t), 200 &baa->ba_npromvaddrs, (void **) &baa->ba_promvaddrs); 201 if (error != 0 && error != ENOENT) { 202 bootbus_destroy_attach_args(baa); 203 return (error); 204 } 205 206 return (0); 207 } 208 209 static void 210 bootbus_destroy_attach_args(struct bootbus_attach_args *baa) 211 { 212 213 if (baa->ba_name != NULL) 214 free(baa->ba_name, M_DEVBUF); 215 216 if (baa->ba_reg != NULL) 217 free(baa->ba_reg, M_DEVBUF); 218 219 if (baa->ba_intr != NULL) 220 free(baa->ba_intr, M_DEVBUF); 221 222 if (baa->ba_promvaddrs != NULL) 223 free(baa->ba_promvaddrs, M_DEVBUF); 224 } 225