1 /*- 2 * Copyright (c) 2015 Michal Meloun 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 /* 28 * This is a generic syscon driver, whose purpose is to provide access to 29 * various unrelated bits packed in a single register space. It is usually used 30 * as a fallback to more specific driver, but works well enough for simple 31 * access. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/bus.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/module.h> 42 #include <sys/rman.h> 43 44 #include <machine/bus.h> 45 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 49 #include "syscon_if.h" 50 #include "syscon.h" 51 52 MALLOC_DECLARE(M_SYSCON); 53 54 static uint32_t syscon_generic_read_4(struct syscon *syscon, bus_size_t offset); 55 static int syscon_generic_write_4(struct syscon *syscon, bus_size_t offset, 56 uint32_t val); 57 static int syscon_generic_modify_4(struct syscon *syscon, bus_size_t offset, 58 uint32_t clear_bits, uint32_t set_bits); 59 60 /* 61 * Generic syscon driver (FDT) 62 */ 63 struct syscon_generic_softc { 64 device_t dev; 65 struct syscon *syscon; 66 struct resource *mem_res; 67 struct mtx mtx; 68 }; 69 70 static struct ofw_compat_data compat_data[] = { 71 {"syscon", 1}, 72 {NULL, 0} 73 }; 74 75 #define SYSCON_LOCK(_sc) mtx_lock(&(_sc)->mtx) 76 #define SYSCON_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx) 77 #define SYSCON_LOCK_INIT(_sc) mtx_init(&(_sc)->mtx, \ 78 device_get_nameunit((_sc)->dev), "syscon", MTX_DEF) 79 #define SYSCON_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx); 80 #define SYSCON_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED); 81 #define SYSCON_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_NOTOWNED); 82 83 static syscon_method_t syscon_generic_methods[] = { 84 SYSCONMETHOD(syscon_read_4, syscon_generic_read_4), 85 SYSCONMETHOD(syscon_write_4, syscon_generic_write_4), 86 SYSCONMETHOD(syscon_modify_4, syscon_generic_modify_4), 87 88 SYSCONMETHOD_END 89 }; 90 DEFINE_CLASS_1(syscon_generic, syscon_generic_class, syscon_generic_methods, 91 0, syscon_class); 92 93 static uint32_t 94 syscon_generic_read_4(struct syscon *syscon, bus_size_t offset) 95 { 96 struct syscon_generic_softc *sc; 97 uint32_t val; 98 99 sc = device_get_softc(syscon->pdev); 100 101 SYSCON_LOCK(sc); 102 val = bus_read_4(sc->mem_res, offset); 103 SYSCON_UNLOCK(sc); 104 return (val); 105 } 106 107 static int 108 syscon_generic_write_4(struct syscon *syscon, bus_size_t offset, uint32_t val) 109 { 110 struct syscon_generic_softc *sc; 111 112 sc = device_get_softc(syscon->pdev); 113 114 SYSCON_LOCK(sc); 115 bus_write_4(sc->mem_res, offset, val); 116 SYSCON_UNLOCK(sc); 117 return (0); 118 } 119 120 static int 121 syscon_generic_modify_4(struct syscon *syscon, bus_size_t offset, 122 uint32_t clear_bits, uint32_t set_bits) 123 { 124 struct syscon_generic_softc *sc; 125 uint32_t val; 126 127 sc = device_get_softc(syscon->pdev); 128 129 SYSCON_LOCK(sc); 130 val = bus_read_4(sc->mem_res, offset); 131 val &= ~clear_bits; 132 val |= set_bits; 133 bus_write_4(sc->mem_res, offset, val); 134 SYSCON_UNLOCK(sc); 135 return (0); 136 } 137 138 static int 139 syscon_generic_probe(device_t dev) 140 { 141 142 if (!ofw_bus_status_okay(dev)) 143 return (ENXIO); 144 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 145 return (ENXIO); 146 147 device_set_desc(dev, "syscon"); 148 return (BUS_PROBE_GENERIC); 149 } 150 151 static int 152 syscon_generic_attach(device_t dev) 153 { 154 struct syscon_generic_softc *sc; 155 int rid; 156 157 sc = device_get_softc(dev); 158 sc->dev = dev; 159 160 rid = 0; 161 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 162 RF_ACTIVE); 163 if (sc->mem_res == NULL) { 164 device_printf(dev, "Cannot allocate memory resource\n"); 165 return (ENXIO); 166 } 167 168 SYSCON_LOCK_INIT(sc); 169 sc->syscon = syscon_create_ofw_node(dev, &syscon_generic_class, 170 ofw_bus_get_node(dev)); 171 if (sc->syscon == NULL) { 172 device_printf(dev, "Failed to create/register syscon\n"); 173 return (ENXIO); 174 } 175 return (0); 176 } 177 178 static int 179 syscon_generic_detach(device_t dev) 180 { 181 struct syscon_generic_softc *sc; 182 183 sc = device_get_softc(dev); 184 185 if (sc->syscon != NULL) { 186 syscon_unregister(sc->syscon); 187 free(sc->syscon, M_SYSCON); 188 } 189 190 SYSCON_LOCK_DESTROY(sc); 191 192 if (sc->mem_res != NULL) 193 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res); 194 return (0); 195 } 196 197 static device_method_t syscon_generic_dmethods[] = { 198 /* Device interface */ 199 DEVMETHOD(device_probe, syscon_generic_probe), 200 DEVMETHOD(device_attach, syscon_generic_attach), 201 DEVMETHOD(device_detach, syscon_generic_detach), 202 203 DEVMETHOD_END 204 }; 205 206 DEFINE_CLASS_0(syscon_generic, syscon_generic_driver, syscon_generic_dmethods, 207 sizeof(struct syscon_generic_softc)); 208 static devclass_t syscon_generic_devclass; 209 EARLY_DRIVER_MODULE(syscon_generic, simplebus, syscon_generic_driver, 210 syscon_generic_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_LATE); 211 MODULE_VERSION(syscon_generic, 1); 212