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