1 /*- 2 * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org> 3 * 4 * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se> 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 ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * 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 * $FreeBSD$ 28 */ 29 /* Based on sys/arm/ti/ti_sysc.c */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 #include <sys/resource.h> 40 #include <sys/rman.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 44 #include <machine/bus.h> 45 #include <machine/resource.h> 46 47 #include <dev/fdt/simplebus.h> 48 49 #include <dev/ofw/openfirm.h> 50 #include <dev/ofw/ofw_bus.h> 51 #include <dev/ofw/ofw_bus_subr.h> 52 53 #include "syscon_if.h" 54 #include <dev/extres/syscon/syscon.h> 55 #include "clkdev_if.h" 56 57 #if 0 58 #define DPRINTF(dev, msg...) device_printf(dev, msg) 59 #else 60 #define DPRINTF(dev, msg...) 61 #endif 62 63 MALLOC_DECLARE(M_SYSCON); 64 65 struct ti_scm_syscon_softc { 66 struct simplebus_softc sc_simplebus; 67 device_t dev; 68 struct syscon * syscon; 69 struct resource * res[1]; 70 bus_space_tag_t bst; 71 bus_space_handle_t bsh; 72 struct mtx mtx; 73 }; 74 75 static struct resource_spec ti_scm_syscon_res_spec[] = { 76 { SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE }, 77 { -1, 0 } 78 }; 79 80 /* Device */ 81 static struct ofw_compat_data compat_data[] = { 82 { "syscon", 1 }, 83 { NULL, 0 } 84 }; 85 86 /* --- dev/extres/syscon syscon_method_t interface --- */ 87 static int 88 ti_scm_syscon_write_4(struct syscon *syscon, bus_size_t offset, uint32_t val) 89 { 90 struct ti_scm_syscon_softc *sc; 91 92 sc = device_get_softc(syscon->pdev); 93 DPRINTF(sc->dev, "offset=%lx write %x\n", offset, val); 94 mtx_lock(&sc->mtx); 95 bus_space_write_4(sc->bst, sc->bsh, offset, val); 96 mtx_unlock(&sc->mtx); 97 return (0); 98 } 99 100 static uint32_t 101 ti_scm_syscon_read_4(struct syscon *syscon, bus_size_t offset) 102 { 103 struct ti_scm_syscon_softc *sc; 104 uint32_t val; 105 106 sc = device_get_softc(syscon->pdev); 107 108 mtx_lock(&sc->mtx); 109 val = bus_space_read_4(sc->bst, sc->bsh, offset); 110 mtx_unlock(&sc->mtx); 111 DPRINTF(sc->dev, "offset=%lx Read %x\n", offset, val); 112 return (val); 113 } 114 static int 115 ti_scm_syscon_modify_4(struct syscon *syscon, bus_size_t offset, uint32_t clr, uint32_t set) 116 { 117 struct ti_scm_syscon_softc *sc; 118 uint32_t reg; 119 120 sc = device_get_softc(syscon->pdev); 121 122 mtx_lock(&sc->mtx); 123 reg = bus_space_read_4(sc->bst, sc->bsh, offset); 124 reg &= ~clr; 125 reg |= set; 126 bus_space_write_4(sc->bst, sc->bsh, offset, reg); 127 mtx_unlock(&sc->mtx); 128 DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", offset, reg, clr, set); 129 130 return (0); 131 } 132 133 static syscon_method_t ti_scm_syscon_reg_methods[] = { 134 SYSCONMETHOD(syscon_read_4, ti_scm_syscon_read_4), 135 SYSCONMETHOD(syscon_write_4, ti_scm_syscon_write_4), 136 SYSCONMETHOD(syscon_modify_4, ti_scm_syscon_modify_4), 137 138 SYSCONMETHOD_END 139 }; 140 141 DEFINE_CLASS_1(ti_scm_syscon_reg, ti_scm_syscon_reg_class, ti_scm_syscon_reg_methods, 142 0, syscon_class); 143 144 /* device interface */ 145 static int 146 ti_scm_syscon_probe(device_t dev) 147 { 148 if (!ofw_bus_status_okay(dev)) 149 return (ENXIO); 150 151 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 152 return (ENXIO); 153 154 device_set_desc(dev, "TI OMAP Control Module Syscon"); 155 return(BUS_PROBE_DEFAULT); 156 } 157 158 static int 159 ti_scm_syscon_attach(device_t dev) 160 { 161 struct ti_scm_syscon_softc *sc; 162 phandle_t node, child; 163 int err; 164 165 sc = device_get_softc(dev); 166 sc->dev = dev; 167 168 if (bus_alloc_resources(dev, ti_scm_syscon_res_spec, sc->res)) { 169 device_printf(sc->dev, "Cant allocate resources\n"); 170 return (ENXIO); 171 } 172 173 sc->dev = dev; 174 sc->bst = rman_get_bustag(sc->res[0]); 175 sc->bsh = rman_get_bushandle(sc->res[0]); 176 177 mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF); 178 node = ofw_bus_get_node(sc->dev); 179 180 /* dev/extres/syscon interface */ 181 sc->syscon = syscon_create_ofw_node(dev, &ti_scm_syscon_reg_class, node); 182 if (sc->syscon == NULL) { 183 device_printf(dev, "Failed to create/register syscon\n"); 184 return (ENXIO); 185 } 186 187 simplebus_init(sc->dev, node); 188 189 err = bus_generic_probe(sc->dev); 190 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 191 simplebus_add_device(sc->dev, child, 0, NULL, -1, NULL); 192 } 193 194 return (bus_generic_attach(sc->dev)); 195 } 196 197 /* syscon interface */ 198 static int 199 ti_scm_syscon_get_handle(device_t dev, struct syscon **syscon) 200 { 201 struct ti_scm_syscon_softc *sc; 202 203 sc = device_get_softc(dev); 204 *syscon = sc->syscon; 205 if (*syscon == NULL) 206 return (ENODEV); 207 return (0); 208 } 209 210 /* clkdev interface */ 211 static int 212 ti_scm_syscon_clk_write_4(device_t dev, bus_addr_t addr, uint32_t val) 213 { 214 struct ti_scm_syscon_softc *sc; 215 216 sc = device_get_softc(dev); 217 DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val); 218 bus_space_write_4(sc->bst, sc->bsh, addr, val); 219 return (0); 220 } 221 222 static int 223 ti_scm_syscon_clk_read_4(device_t dev, bus_addr_t addr, uint32_t *val) 224 { 225 struct ti_scm_syscon_softc *sc; 226 227 sc = device_get_softc(dev); 228 229 *val = bus_space_read_4(sc->bst, sc->bsh, addr); 230 DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, *val); 231 return (0); 232 } 233 234 static int 235 ti_scm_syscon_clk_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set) 236 { 237 struct ti_scm_syscon_softc *sc; 238 uint32_t reg; 239 240 sc = device_get_softc(dev); 241 242 reg = bus_space_read_4(sc->bst, sc->bsh, addr); 243 reg &= ~clr; 244 reg |= set; 245 bus_space_write_4(sc->bst, sc->bsh, addr, reg); 246 DPRINTF(sc->dev, "offset=%lx reg: %x (clr %x set %x)\n", addr, reg, clr, set); 247 248 return (0); 249 } 250 251 static void 252 ti_scm_syscon_clk_device_lock(device_t dev) 253 { 254 struct ti_scm_syscon_softc *sc; 255 256 sc = device_get_softc(dev); 257 mtx_lock(&sc->mtx); 258 } 259 260 static void 261 ti_scm_syscon_clk_device_unlock(device_t dev) 262 { 263 struct ti_scm_syscon_softc *sc; 264 sc = device_get_softc(dev); 265 mtx_unlock(&sc->mtx); 266 } 267 268 static device_method_t ti_scm_syscon_methods[] = { 269 DEVMETHOD(device_probe, ti_scm_syscon_probe), 270 DEVMETHOD(device_attach, ti_scm_syscon_attach), 271 272 /* syscon interface */ 273 DEVMETHOD(syscon_get_handle, ti_scm_syscon_get_handle), 274 275 /* clkdev interface */ 276 DEVMETHOD(clkdev_write_4, ti_scm_syscon_clk_write_4), 277 DEVMETHOD(clkdev_read_4, ti_scm_syscon_clk_read_4), 278 DEVMETHOD(clkdev_modify_4, ti_scm_syscon_clk_modify_4), 279 DEVMETHOD(clkdev_device_lock, ti_scm_syscon_clk_device_lock), 280 DEVMETHOD(clkdev_device_unlock, ti_scm_syscon_clk_device_unlock), 281 282 DEVMETHOD_END 283 }; 284 285 DEFINE_CLASS_1(ti_scm_syscon, ti_scm_syscon_driver, ti_scm_syscon_methods, 286 sizeof(struct ti_scm_syscon_softc), simplebus_driver); 287 288 static devclass_t ti_scm_syscon_devclass; 289 290 EARLY_DRIVER_MODULE(ti_scm_syscon, simplebus, ti_scm_syscon_driver, 291 ti_scm_syscon_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 292 MODULE_VERSION(ti_scm_syscon, 1); 293 MODULE_DEPEND(ti_scm_syscon, ti_scm, 1, 1, 1); 294