1 /*- 2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 /* 27 * Allwinner module software reset registers 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/bus.h> 36 #include <sys/rman.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <machine/bus.h> 42 43 #include <dev/ofw/ofw_bus.h> 44 #include <dev/ofw/ofw_bus_subr.h> 45 46 #include <dev/extres/hwreset/hwreset.h> 47 48 #include "hwreset_if.h" 49 50 #define RESET_OFFSET(index) ((index / 32) * 4) 51 #define RESET_SHIFT(index) (index % 32) 52 53 static struct ofw_compat_data compat_data[] = { 54 { "allwinner,sun6i-a31-ahb1-reset", 1 }, 55 { "allwinner,sun6i-a31-clock-reset", 1 }, 56 { NULL, 0 } 57 }; 58 59 struct aw_reset_softc { 60 struct resource *res; 61 struct mtx mtx; 62 }; 63 64 static struct resource_spec aw_reset_spec[] = { 65 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 66 { -1, 0 } 67 }; 68 69 #define RESET_READ(sc, reg) bus_read_4((sc)->res, (reg)) 70 #define RESET_WRITE(sc, reg, val) bus_write_4((sc)->res, (reg), (val)) 71 72 static int 73 aw_reset_assert(device_t dev, intptr_t id, bool reset) 74 { 75 struct aw_reset_softc *sc; 76 uint32_t reg_value; 77 78 sc = device_get_softc(dev); 79 80 mtx_lock(&sc->mtx); 81 reg_value = RESET_READ(sc, RESET_OFFSET(id)); 82 if (reset) 83 reg_value &= ~(1 << RESET_SHIFT(id)); 84 else 85 reg_value |= (1 << RESET_SHIFT(id)); 86 RESET_WRITE(sc, RESET_OFFSET(id), reg_value); 87 mtx_unlock(&sc->mtx); 88 89 return (0); 90 } 91 92 static int 93 aw_reset_is_asserted(device_t dev, intptr_t id, bool *reset) 94 { 95 struct aw_reset_softc *sc; 96 uint32_t reg_value; 97 98 sc = device_get_softc(dev); 99 100 mtx_lock(&sc->mtx); 101 reg_value = RESET_READ(sc, RESET_OFFSET(id)); 102 mtx_unlock(&sc->mtx); 103 104 *reset = (reg_value & (1 << RESET_SHIFT(id))) != 0 ? false : true; 105 106 return (0); 107 } 108 109 static int 110 aw_reset_probe(device_t dev) 111 { 112 if (!ofw_bus_status_okay(dev)) 113 return (ENXIO); 114 115 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 116 return (ENXIO); 117 118 device_set_desc(dev, "Allwinner Module Resets"); 119 return (BUS_PROBE_DEFAULT); 120 } 121 122 static int 123 aw_reset_attach(device_t dev) 124 { 125 struct aw_reset_softc *sc; 126 127 sc = device_get_softc(dev); 128 129 if (bus_alloc_resources(dev, aw_reset_spec, &sc->res) != 0) { 130 device_printf(dev, "cannot allocate resources for device\n"); 131 return (ENXIO); 132 } 133 134 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); 135 136 hwreset_register_ofw_provider(dev); 137 138 return (0); 139 } 140 141 static device_method_t aw_reset_methods[] = { 142 /* Device interface */ 143 DEVMETHOD(device_probe, aw_reset_probe), 144 DEVMETHOD(device_attach, aw_reset_attach), 145 146 /* Reset interface */ 147 DEVMETHOD(hwreset_assert, aw_reset_assert), 148 DEVMETHOD(hwreset_is_asserted, aw_reset_is_asserted), 149 150 DEVMETHOD_END 151 }; 152 153 static driver_t aw_reset_driver = { 154 "aw_reset", 155 aw_reset_methods, 156 sizeof(struct aw_reset_softc), 157 }; 158 159 EARLY_DRIVER_MODULE(aw_reset, simplebus, aw_reset_driver, 0, 0, 160 BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE); 161 MODULE_VERSION(aw_reset, 1); 162