1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018 Rubicon Communications, LLC (Netgate) 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/module.h> 38 #include <sys/mutex.h> 39 #include <sys/rman.h> 40 41 #include <machine/bus.h> 42 43 #include <dev/fdt/simplebus.h> 44 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 48 #include <dev/fdt/simple_mfd.h> 49 50 device_t simple_mfd_add_device(device_t dev, phandle_t node, u_int order, 51 const char *name, int unit, struct simplebus_devinfo *di); 52 struct simplebus_devinfo *simple_mfd_setup_dinfo(device_t dev, phandle_t node, struct simplebus_devinfo *di); 53 54 #include "syscon_if.h" 55 #include <dev/extres/syscon/syscon.h> 56 57 MALLOC_DECLARE(M_SYSCON); 58 59 static uint32_t simple_mfd_syscon_read_4(struct syscon *syscon, bus_size_t offset); 60 static int simple_mfd_syscon_write_4(struct syscon *syscon, bus_size_t offset, 61 uint32_t val); 62 static int simple_mfd_syscon_modify_4(struct syscon *syscon, bus_size_t offset, 63 uint32_t clear_bits, uint32_t set_bits); 64 65 #define SYSCON_LOCK(_sc) mtx_lock_spin(&(_sc)->mtx) 66 #define SYSCON_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->mtx) 67 #define SYSCON_LOCK_INIT(_sc) mtx_init(&(_sc)->mtx, \ 68 device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN) 69 #define SYSCON_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx); 70 #define SYSCON_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED); 71 #define SYSCON_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_NOTOWNED); 72 73 static syscon_method_t simple_mfd_syscon_methods[] = { 74 SYSCONMETHOD(syscon_read_4, simple_mfd_syscon_read_4), 75 SYSCONMETHOD(syscon_write_4, simple_mfd_syscon_write_4), 76 SYSCONMETHOD(syscon_modify_4, simple_mfd_syscon_modify_4), 77 78 SYSCONMETHOD_END 79 }; 80 DEFINE_CLASS_1(simple_mfd_syscon, simple_mfd_syscon_class, 81 simple_mfd_syscon_methods, 0, syscon_class); 82 83 static uint32_t 84 simple_mfd_syscon_read_4(struct syscon *syscon, bus_size_t offset) 85 { 86 struct simple_mfd_softc *sc; 87 uint32_t val; 88 89 sc = device_get_softc(syscon->pdev); 90 91 SYSCON_LOCK(sc); 92 val = bus_read_4(sc->mem_res, offset); 93 SYSCON_UNLOCK(sc); 94 return (val); 95 } 96 97 static int 98 simple_mfd_syscon_write_4(struct syscon *syscon, bus_size_t offset, 99 uint32_t val) 100 { 101 struct simple_mfd_softc *sc; 102 103 sc = device_get_softc(syscon->pdev); 104 105 SYSCON_LOCK(sc); 106 bus_write_4(sc->mem_res, offset, val); 107 SYSCON_UNLOCK(sc); 108 return (0); 109 } 110 111 static int 112 simple_mfd_syscon_modify_4(struct syscon *syscon, bus_size_t offset, 113 uint32_t clear_bits, uint32_t set_bits) 114 { 115 struct simple_mfd_softc *sc; 116 uint32_t val; 117 118 sc = device_get_softc(syscon->pdev); 119 120 SYSCON_LOCK(sc); 121 val = bus_read_4(sc->mem_res, offset); 122 val &= ~clear_bits; 123 val |= set_bits; 124 bus_write_4(sc->mem_res, offset, val); 125 SYSCON_UNLOCK(sc); 126 return (0); 127 } 128 129 static int 130 simple_mfd_probe(device_t dev) 131 { 132 133 if (!ofw_bus_status_okay(dev)) 134 return (ENXIO); 135 if (!ofw_bus_is_compatible(dev, "simple-mfd")) 136 return (ENXIO); 137 138 device_set_desc(dev, "Simple MFD (Multi-Functions Device)"); 139 140 return (BUS_PROBE_GENERIC); 141 } 142 143 static int 144 simple_mfd_attach(device_t dev) 145 { 146 struct simple_mfd_softc *sc; 147 phandle_t node, child; 148 device_t cdev; 149 int rid; 150 151 sc = device_get_softc(dev); 152 node = ofw_bus_get_node(dev); 153 154 sc->dev = dev; 155 rid = 0; 156 157 /* Parse address-cells and size-cells from the parent node as a fallback */ 158 if (OF_getencprop(node, "#address-cells", &sc->sc.acells, 159 sizeof(sc->sc.acells)) == -1) { 160 if (OF_getencprop(OF_parent(node), "#address-cells", &sc->sc.acells, 161 sizeof(sc->sc.acells)) == -1) { 162 sc->sc.acells = 2; 163 } 164 } 165 if (OF_getencprop(node, "#size-cells", &sc->sc.scells, 166 sizeof(sc->sc.scells)) == -1) { 167 if (OF_getencprop(OF_parent(node), "#size-cells", &sc->sc.scells, 168 sizeof(sc->sc.scells)) == -1) { 169 sc->sc.scells = 1; 170 } 171 } 172 173 /* If the node has a ranges prop, parse it so children mapping will be done correctly */ 174 if (OF_hasprop(node, "ranges")) { 175 if (simplebus_fill_ranges(node, &sc->sc) < 0) { 176 device_printf(dev, "could not get ranges\n"); 177 return (ENXIO); 178 } 179 } 180 181 /* Attach child devices */ 182 for (child = OF_child(node); child > 0; child = OF_peer(child)) { 183 cdev = simple_mfd_add_device(dev, child, 0, NULL, -1, NULL); 184 if (cdev != NULL) 185 device_probe_and_attach(cdev); 186 } 187 188 if (ofw_bus_is_compatible(dev, "syscon")) { 189 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 190 RF_ACTIVE); 191 if (sc->mem_res == NULL) { 192 device_printf(dev, 193 "Cannot allocate memory resource\n"); 194 return (ENXIO); 195 } 196 197 SYSCON_LOCK_INIT(sc); 198 sc->syscon = syscon_create_ofw_node(dev, 199 &simple_mfd_syscon_class, ofw_bus_get_node(dev)); 200 if (sc->syscon == NULL) { 201 device_printf(dev, 202 "Failed to create/register syscon\n"); 203 return (ENXIO); 204 } 205 } 206 return (bus_generic_attach(dev)); 207 } 208 209 static int 210 simple_mfd_detach(device_t dev) 211 { 212 struct simple_mfd_softc *sc; 213 214 sc = device_get_softc(dev); 215 if (ofw_bus_is_compatible(dev, "syscon")) { 216 if (sc->syscon != NULL) { 217 syscon_unregister(sc->syscon); 218 free(sc->syscon, M_SYSCON); 219 } 220 221 SYSCON_LOCK_DESTROY(sc); 222 223 if (sc->mem_res != NULL) 224 bus_release_resource(dev, SYS_RES_MEMORY, 0, 225 sc->mem_res); 226 } 227 return (0); 228 } 229 230 struct simplebus_devinfo * 231 simple_mfd_setup_dinfo(device_t dev, phandle_t node, 232 struct simplebus_devinfo *di) 233 { 234 struct simplebus_softc *sc; 235 struct simplebus_devinfo *ndi; 236 237 sc = device_get_softc(dev); 238 if (di == NULL) 239 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO); 240 else 241 ndi = di; 242 if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) { 243 if (di == NULL) 244 free(ndi, M_DEVBUF); 245 return (NULL); 246 } 247 248 /* reg resources is from the parent but interrupts is on the node itself */ 249 resource_list_init(&ndi->rl); 250 ofw_bus_reg_to_rl(dev, OF_parent(node), sc->acells, sc->scells, &ndi->rl); 251 ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL); 252 253 return (ndi); 254 } 255 256 device_t 257 simple_mfd_add_device(device_t dev, phandle_t node, u_int order, 258 const char *name, int unit, struct simplebus_devinfo *di) 259 { 260 struct simplebus_devinfo *ndi; 261 device_t cdev; 262 263 if ((ndi = simple_mfd_setup_dinfo(dev, node, di)) == NULL) 264 return (NULL); 265 cdev = device_add_child_ordered(dev, order, name, unit); 266 if (cdev == NULL) { 267 device_printf(dev, "<%s>: device_add_child failed\n", 268 ndi->obdinfo.obd_name); 269 resource_list_free(&ndi->rl); 270 ofw_bus_gen_destroy_devinfo(&ndi->obdinfo); 271 if (di == NULL) 272 free(ndi, M_DEVBUF); 273 return (NULL); 274 } 275 device_set_ivars(cdev, ndi); 276 277 return(cdev); 278 } 279 280 static device_method_t simple_mfd_methods[] = { 281 /* Device interface */ 282 DEVMETHOD(device_probe, simple_mfd_probe), 283 DEVMETHOD(device_attach, simple_mfd_attach), 284 DEVMETHOD(device_detach, simple_mfd_detach), 285 286 DEVMETHOD_END 287 }; 288 289 DEFINE_CLASS_1(simple_mfd, simple_mfd_driver, simple_mfd_methods, 290 sizeof(struct simple_mfd_softc), simplebus_driver); 291 292 static devclass_t simple_mfd_devclass; 293 294 EARLY_DRIVER_MODULE(simple_mfd, simplebus, simple_mfd_driver, 295 simple_mfd_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_LATE); 296 MODULE_VERSION(simple_mfd, 1); 297