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