1 /*- 2 * Copyright (c) 2011-2012 Semihalf. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include "opt_platform.h" 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/module.h> 36 37 #include <machine/bus.h> 38 39 #include <dev/ofw/ofw_bus.h> 40 #include <dev/ofw/ofw_bus_subr.h> 41 #include <dev/ofw/ofw_subr.h> 42 43 #include "bman.h" 44 #include "portals.h" 45 46 #define FBMAN_DEVSTR "Freescale Buffer Manager" 47 48 static int bman_fdt_probe(device_t); 49 50 static device_method_t bman_methods[] = { 51 /* Device interface */ 52 DEVMETHOD(device_probe, bman_fdt_probe), 53 DEVMETHOD(device_attach, bman_attach), 54 DEVMETHOD(device_detach, bman_detach), 55 56 DEVMETHOD(device_suspend, bman_suspend), 57 DEVMETHOD(device_resume, bman_resume), 58 DEVMETHOD(device_shutdown, bman_shutdown), 59 60 { 0, 0 } 61 }; 62 63 static driver_t bman_driver = { 64 "bman", 65 bman_methods, 66 sizeof(struct bman_softc), 67 }; 68 69 static devclass_t bman_devclass; 70 DRIVER_MODULE(bman, simplebus, bman_driver, bman_devclass, 0, 0); 71 72 static int 73 bman_fdt_probe(device_t dev) 74 { 75 76 if (!ofw_bus_is_compatible(dev, "fsl,bman")) 77 return (ENXIO); 78 79 device_set_desc(dev, FBMAN_DEVSTR); 80 81 return (BUS_PROBE_DEFAULT); 82 } 83 84 /* 85 * BMAN Portals 86 */ 87 #define BMAN_PORT_DEVSTR "Freescale Buffer Manager - Portals" 88 89 static device_probe_t bman_portals_fdt_probe; 90 static device_attach_t bman_portals_fdt_attach; 91 92 static device_method_t bm_portals_methods[] = { 93 /* Device interface */ 94 DEVMETHOD(device_probe, bman_portals_fdt_probe), 95 DEVMETHOD(device_attach, bman_portals_fdt_attach), 96 DEVMETHOD(device_detach, bman_portals_detach), 97 98 { 0, 0 } 99 }; 100 101 static driver_t bm_portals_driver = { 102 "bman-portals", 103 bm_portals_methods, 104 sizeof(struct dpaa_portals_softc), 105 }; 106 107 static devclass_t bm_portals_devclass; 108 DRIVER_MODULE(bman_portals, ofwbus, bm_portals_driver, bm_portals_devclass, 0, 0); 109 110 static void 111 get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep) 112 { 113 114 *addrp = 2; 115 *sizep = 1; 116 OF_getencprop(node, "#address-cells", addrp, sizeof(*addrp)); 117 OF_getencprop(node, "#size-cells", sizep, sizeof(*sizep)); 118 } 119 120 static int 121 bman_portals_fdt_probe(device_t dev) 122 { 123 124 if (!ofw_bus_is_compatible(dev, "bman-portals")) 125 return (ENXIO); 126 127 device_set_desc(dev, BMAN_PORT_DEVSTR); 128 129 return (BUS_PROBE_DEFAULT); 130 } 131 132 static int 133 bman_portals_fdt_attach(device_t dev) 134 { 135 struct dpaa_portals_softc *sc; 136 struct resource_list_entry *rle; 137 phandle_t node, child, cpu_node; 138 vm_paddr_t portal_pa; 139 vm_size_t portal_size; 140 uint32_t addr, size; 141 ihandle_t cpu; 142 int cpu_num, cpus, intr_rid; 143 struct dpaa_portals_devinfo di; 144 struct ofw_bus_devinfo ofw_di = {}; 145 146 cpus = 0; 147 sc = device_get_softc(dev); 148 sc->sc_dev = dev; 149 150 node = ofw_bus_get_node(dev); 151 get_addr_props(node, &addr, &size); 152 153 /* Find portals tied to CPUs */ 154 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 155 if (!ofw_bus_node_is_compatible(child, "fsl,bman-portal")) { 156 continue; 157 } 158 /* Checkout related cpu */ 159 if (OF_getprop(child, "cpu-handle", (void *)&cpu, 160 sizeof(cpu)) <= 0) { 161 continue; 162 } 163 /* Acquire cpu number */ 164 cpu_node = OF_instance_to_package(cpu); 165 if (OF_getencprop(cpu_node, "reg", &cpu_num, sizeof(cpu_num)) <= 0) { 166 device_printf(dev, "Could not retrieve CPU number.\n"); 167 return (ENXIO); 168 } 169 170 cpus++; 171 172 if (cpus > MAXCPU) 173 break; 174 175 if (ofw_bus_gen_setup_devinfo(&ofw_di, child) != 0) { 176 device_printf(dev, "could not set up devinfo\n"); 177 continue; 178 } 179 180 resource_list_init(&di.di_res); 181 if (ofw_bus_reg_to_rl(dev, child, addr, size, &di.di_res)) { 182 device_printf(dev, "%s: could not process 'reg' " 183 "property\n", ofw_di.obd_name); 184 ofw_bus_gen_destroy_devinfo(&ofw_di); 185 continue; 186 } 187 if (ofw_bus_intr_to_rl(dev, child, &di.di_res, &intr_rid)) { 188 device_printf(dev, "%s: could not process " 189 "'interrupts' property\n", ofw_di.obd_name); 190 resource_list_free(&di.di_res); 191 ofw_bus_gen_destroy_devinfo(&ofw_di); 192 continue; 193 } 194 di.di_intr_rid = intr_rid; 195 196 ofw_reg_to_paddr(child, 0, &portal_pa, &portal_size, NULL); 197 rle = resource_list_find(&di.di_res, SYS_RES_MEMORY, 0); 198 199 if (sc->sc_dp_pa == 0) 200 sc->sc_dp_pa = portal_pa - rle->start; 201 202 portal_size = rle->end + 1; 203 rle = resource_list_find(&di.di_res, SYS_RES_MEMORY, 1); 204 portal_size = ulmax(rle->end + 1, portal_size); 205 sc->sc_dp_size = ulmax(sc->sc_dp_size, portal_size); 206 207 if (dpaa_portal_alloc_res(dev, &di, cpu_num)) 208 goto err; 209 } 210 211 ofw_bus_gen_destroy_devinfo(&ofw_di); 212 213 return (bman_portals_attach(dev)); 214 err: 215 resource_list_free(&di.di_res); 216 ofw_bus_gen_destroy_devinfo(&ofw_di); 217 bman_portals_detach(dev); 218 return (ENXIO); 219 } 220