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