1 /*- 2 * Copyright (c) 2015 The FreeBSD Foundation 3 * 4 * This software was developed by Semihalf under 5 * the sponsorship of the FreeBSD Foundation. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/socket.h> 36 37 #include <dev/ofw/ofw_bus.h> 38 #include <dev/ofw/ofw_bus_subr.h> 39 #include <dev/fdt/simplebus.h> 40 41 #include <net/if.h> 42 43 #include <machine/bus.h> 44 #include <machine/resource.h> 45 46 #include "thunder_mdio_var.h" 47 48 static int thunder_mdio_fdt_probe(device_t); 49 static int thunder_mdio_fdt_attach(device_t); 50 51 static device_method_t thunder_mdio_fdt_methods[] = { 52 /* Device interface */ 53 DEVMETHOD(device_probe, thunder_mdio_fdt_probe), 54 DEVMETHOD(device_attach, thunder_mdio_fdt_attach), 55 56 /* End */ 57 DEVMETHOD_END 58 }; 59 60 DEFINE_CLASS_1(thunder_mdio, thunder_mdio_fdt_driver, thunder_mdio_fdt_methods, 61 sizeof(struct thunder_mdio_softc), thunder_mdio_driver); 62 63 EARLY_DRIVER_MODULE(thunder_mdio, ofwbus, thunder_mdio_fdt_driver, 0, 0, 64 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 65 EARLY_DRIVER_MODULE(thunder_mdio, mdionexus, thunder_mdio_fdt_driver, 0, 0, 66 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 67 68 static struct ofw_compat_data mdio_compat_data[] = { 69 {"cavium,octeon-3860-mdio", true}, 70 {"cavium,thunder-8890-mdio", true}, 71 {NULL, false} 72 }; 73 74 static int 75 thunder_mdio_fdt_probe(device_t dev) 76 { 77 78 if (!ofw_bus_status_okay(dev)) 79 return (ENXIO); 80 if (!ofw_bus_search_compatible(dev, mdio_compat_data)->ocd_data) 81 return (ENXIO); 82 83 device_set_desc(dev, THUNDER_MDIO_DEVSTR); 84 return (BUS_PROBE_DEFAULT); 85 } 86 87 static int 88 thunder_mdio_fdt_attach(device_t dev) 89 { 90 phandle_t node; 91 int ret; 92 93 /* Call core attach */ 94 ret = thunder_mdio_attach(dev); 95 if (ret != 0) 96 return (ret); 97 /* 98 * Register device to this node/xref. 99 * Thanks to that we will be able to retrieve device_t structure 100 * while holding only node reference acquired from FDT. 101 */ 102 node = ofw_bus_get_node(dev); 103 OF_device_register_xref(OF_xref_from_node(node), dev); 104 105 return (0); 106 } 107 108 struct mdionexus_softc { 109 struct simplebus_softc simplebus_sc; 110 }; 111 112 static device_probe_t mdionexus_fdt_probe; 113 static device_attach_t mdionexus_fdt_attach; 114 115 static const struct ofw_bus_devinfo * mdionexus_ofw_get_devinfo(device_t, 116 device_t); 117 118 static device_method_t mdionexus_fdt_methods[] = { 119 /* Device interface */ 120 DEVMETHOD(device_probe, mdionexus_fdt_probe), 121 DEVMETHOD(device_attach, mdionexus_fdt_attach), 122 123 /* Bus interface */ 124 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 125 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 126 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 127 128 /* ofw_bus interface */ 129 DEVMETHOD(ofw_bus_get_devinfo, mdionexus_ofw_get_devinfo), 130 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 131 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 132 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 133 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 134 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 135 136 DEVMETHOD_END 137 }; 138 139 DEFINE_CLASS_0(mdionexus, mdionexus_fdt_driver, mdionexus_fdt_methods, 140 sizeof(struct mdionexus_softc)); 141 142 EARLY_DRIVER_MODULE(mdionexus, mrmlbus, mdionexus_fdt_driver, 0, 0, 143 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 144 145 static int mdionexus_ofw_fill_ranges(phandle_t, struct simplebus_softc *); 146 static int mdionexus_ofw_bus_attach(device_t); 147 148 static int 149 mdionexus_fdt_probe(device_t dev) 150 { 151 152 if (!ofw_bus_status_okay(dev)) 153 return (ENXIO); 154 155 if (!ofw_bus_is_compatible(dev, "cavium,thunder-8890-mdio-nexus")) 156 return (ENXIO); 157 158 device_set_desc(dev, "Cavium ThunderX MDIO nexus"); 159 return (BUS_PROBE_SPECIFIC); 160 } 161 162 static int 163 mdionexus_fdt_attach(device_t dev) 164 { 165 int err; 166 167 err = mdionexus_ofw_bus_attach(dev); 168 if (err != 0) 169 return (err); 170 171 return (bus_generic_attach(dev)); 172 } 173 174 /* OFW bus interface */ 175 struct mdionexus_ofw_devinfo { 176 struct ofw_bus_devinfo di_dinfo; 177 struct resource_list di_rl; 178 }; 179 180 static const struct ofw_bus_devinfo * 181 mdionexus_ofw_get_devinfo(device_t bus __unused, device_t child) 182 { 183 struct mdionexus_ofw_devinfo *di; 184 185 di = device_get_ivars(child); 186 return (&di->di_dinfo); 187 } 188 189 /* Helper functions */ 190 191 static int 192 mdionexus_ofw_fill_ranges(phandle_t node, struct simplebus_softc *sc) 193 { 194 int host_address_cells; 195 cell_t *base_ranges; 196 ssize_t nbase_ranges; 197 int err; 198 int i, j, k; 199 200 err = OF_searchencprop(OF_parent(node), "#address-cells", 201 &host_address_cells, sizeof(host_address_cells)); 202 if (err <= 0) 203 return (-1); 204 205 nbase_ranges = OF_getproplen(node, "ranges"); 206 if (nbase_ranges < 0) 207 return (-1); 208 sc->nranges = nbase_ranges / sizeof(cell_t) / 209 (sc->acells + host_address_cells + sc->scells); 210 if (sc->nranges == 0) 211 return (0); 212 213 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]), 214 M_THUNDER_MDIO, M_WAITOK); 215 base_ranges = malloc(nbase_ranges, M_THUNDER_MDIO, M_WAITOK); 216 OF_getencprop(node, "ranges", base_ranges, nbase_ranges); 217 218 for (i = 0, j = 0; i < sc->nranges; i++) { 219 sc->ranges[i].bus = 0; 220 for (k = 0; k < sc->acells; k++) { 221 sc->ranges[i].bus <<= 32; 222 sc->ranges[i].bus |= base_ranges[j++]; 223 } 224 sc->ranges[i].host = 0; 225 for (k = 0; k < host_address_cells; k++) { 226 sc->ranges[i].host <<= 32; 227 sc->ranges[i].host |= base_ranges[j++]; 228 } 229 sc->ranges[i].size = 0; 230 for (k = 0; k < sc->scells; k++) { 231 sc->ranges[i].size <<= 32; 232 sc->ranges[i].size |= base_ranges[j++]; 233 } 234 } 235 236 free(base_ranges, M_THUNDER_MDIO); 237 return (sc->nranges); 238 } 239 240 static int 241 mdionexus_ofw_bus_attach(device_t dev) 242 { 243 struct simplebus_softc *sc; 244 struct mdionexus_ofw_devinfo *di; 245 device_t child; 246 phandle_t parent, node; 247 248 parent = ofw_bus_get_node(dev); 249 simplebus_init(dev, parent); 250 251 sc = (struct simplebus_softc *)device_get_softc(dev); 252 253 if (mdionexus_ofw_fill_ranges(parent, sc) < 0) { 254 device_printf(dev, "could not get ranges\n"); 255 return (ENXIO); 256 } 257 /* Iterate through all bus subordinates */ 258 for (node = OF_child(parent); node > 0; node = OF_peer(node)) { 259 /* Allocate and populate devinfo. */ 260 di = malloc(sizeof(*di), M_THUNDER_MDIO, M_WAITOK | M_ZERO); 261 if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) { 262 free(di, M_THUNDER_MDIO); 263 continue; 264 } 265 266 /* Initialize and populate resource list. */ 267 resource_list_init(&di->di_rl); 268 ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, 269 &di->di_rl); 270 ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL); 271 272 /* Add newbus device for this FDT node */ 273 child = device_add_child(dev, NULL, -1); 274 if (child == NULL) { 275 resource_list_free(&di->di_rl); 276 ofw_bus_gen_destroy_devinfo(&di->di_dinfo); 277 free(di, M_THUNDER_MDIO); 278 continue; 279 } 280 281 device_set_ivars(child, di); 282 } 283 284 return (0); 285 } 286