1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2015 Michal Meloun 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * This is a generic syscon driver, whose purpose is to provide access to 31 * various unrelated bits packed in a single register space. It is usually used 32 * as a fallback to more specific driver, but works well enough for simple 33 * access. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bus.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/module.h> 44 #include <sys/mutex.h> 45 #include <sys/rman.h> 46 47 #include <machine/bus.h> 48 49 #include <dev/ofw/ofw_bus.h> 50 #include <dev/ofw/ofw_bus_subr.h> 51 52 #include "syscon_if.h" 53 #include "syscon.h" 54 #include "syscon_generic.h" 55 56 MALLOC_DECLARE(M_SYSCON); 57 58 static uint32_t syscon_generic_read_4(struct syscon *syscon, bus_size_t offset); 59 static int syscon_generic_write_4(struct syscon *syscon, bus_size_t offset, 60 uint32_t val); 61 static int syscon_generic_modify_4(struct syscon *syscon, bus_size_t offset, 62 uint32_t clear_bits, uint32_t set_bits); 63 64 /* 65 * Generic syscon driver (FDT) 66 */ 67 static struct ofw_compat_data compat_data[] = { 68 {"syscon", 1}, 69 {NULL, 0} 70 }; 71 72 #define SYSCON_LOCK(_sc) mtx_lock_spin(&(_sc)->mtx) 73 #define SYSCON_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->mtx) 74 #define SYSCON_LOCK_INIT(_sc) mtx_init(&(_sc)->mtx, \ 75 device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN) 76 #define SYSCON_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx); 77 #define SYSCON_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED); 78 #define SYSCON_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_NOTOWNED); 79 80 static syscon_method_t syscon_generic_methods[] = { 81 SYSCONMETHOD(syscon_read_4, syscon_generic_read_4), 82 SYSCONMETHOD(syscon_write_4, syscon_generic_write_4), 83 SYSCONMETHOD(syscon_modify_4, syscon_generic_modify_4), 84 85 SYSCONMETHOD_END 86 }; 87 DEFINE_CLASS_1(syscon_generic, syscon_generic_class, syscon_generic_methods, 88 0, syscon_class); 89 90 static uint32_t 91 syscon_generic_read_4(struct syscon *syscon, bus_size_t offset) 92 { 93 struct syscon_generic_softc *sc; 94 uint32_t val; 95 96 sc = device_get_softc(syscon->pdev); 97 98 SYSCON_LOCK(sc); 99 val = bus_read_4(sc->mem_res, offset); 100 SYSCON_UNLOCK(sc); 101 return (val); 102 } 103 104 static int 105 syscon_generic_write_4(struct syscon *syscon, bus_size_t offset, uint32_t val) 106 { 107 struct syscon_generic_softc *sc; 108 109 sc = device_get_softc(syscon->pdev); 110 111 SYSCON_LOCK(sc); 112 bus_write_4(sc->mem_res, offset, val); 113 SYSCON_UNLOCK(sc); 114 return (0); 115 } 116 117 static int 118 syscon_generic_modify_4(struct syscon *syscon, bus_size_t offset, 119 uint32_t clear_bits, uint32_t set_bits) 120 { 121 struct syscon_generic_softc *sc; 122 uint32_t val; 123 124 sc = device_get_softc(syscon->pdev); 125 126 SYSCON_LOCK(sc); 127 val = bus_read_4(sc->mem_res, offset); 128 val &= ~clear_bits; 129 val |= set_bits; 130 bus_write_4(sc->mem_res, offset, val); 131 SYSCON_UNLOCK(sc); 132 return (0); 133 } 134 135 static int 136 syscon_generic_probe(device_t dev) 137 { 138 139 if (!ofw_bus_status_okay(dev)) 140 return (ENXIO); 141 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 142 return (ENXIO); 143 144 device_set_desc(dev, "syscon"); 145 if (!bootverbose) 146 device_quiet(dev); 147 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 rid = 0; 160 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 if (sc->syscon != NULL) { 185 syscon_unregister(sc->syscon); 186 free(sc->syscon, M_SYSCON); 187 } 188 189 SYSCON_LOCK_DESTROY(sc); 190 191 if (sc->mem_res != NULL) 192 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res); 193 return (0); 194 } 195 196 static device_method_t syscon_generic_dmethods[] = { 197 /* Device interface */ 198 DEVMETHOD(device_probe, syscon_generic_probe), 199 DEVMETHOD(device_attach, syscon_generic_attach), 200 DEVMETHOD(device_detach, syscon_generic_detach), 201 202 DEVMETHOD_END 203 }; 204 205 DEFINE_CLASS_0(syscon_generic, syscon_generic_driver, syscon_generic_dmethods, 206 sizeof(struct syscon_generic_softc)); 207 static devclass_t syscon_generic_devclass; 208 209 EARLY_DRIVER_MODULE(syscon_generic, simplebus, syscon_generic_driver, 210 syscon_generic_devclass, 0, 0, BUS_PASS_DEFAULT); 211 MODULE_VERSION(syscon_generic, 1); 212