1 1.11 thorpej /* $NetBSD: sunxi_sramc.c,v 1.11 2021/01/27 03:10:20 thorpej Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.11 thorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_sramc.c,v 1.11 2021/01/27 03:10:20 thorpej Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/bus.h> 34 1.1 jmcneill #include <sys/device.h> 35 1.1 jmcneill #include <sys/intr.h> 36 1.1 jmcneill #include <sys/systm.h> 37 1.1 jmcneill #include <sys/kmem.h> 38 1.2 jmcneill #include <sys/mutex.h> 39 1.1 jmcneill 40 1.1 jmcneill #include <dev/fdt/fdtvar.h> 41 1.2 jmcneill #include <dev/fdt/syscon.h> 42 1.1 jmcneill 43 1.1 jmcneill #include <arm/sunxi/sunxi_sramc.h> 44 1.1 jmcneill 45 1.6 thorpej static const struct device_compatible_entry compat_data[] = { 46 1.6 thorpej /* old compat string */ 47 1.6 thorpej { .compat = "allwinner,sun4i-a10-sram-controller" }, 48 1.6 thorpej { .compat = "allwinner,sun4i-a10-system-control" }, 49 1.6 thorpej { .compat = "allwinner,sun8i-h3-system-control" }, 50 1.6 thorpej { .compat = "allwinner,sun50i-a64-system-control" }, 51 1.6 thorpej { .compat = "allwinner,sun50i-h5-system-control" }, 52 1.6 thorpej { .compat = "allwinner,sun50i-h6-system-control" }, 53 1.10 thorpej DEVICE_COMPAT_EOL 54 1.1 jmcneill }; 55 1.1 jmcneill 56 1.6 thorpej struct sunxi_sramc_area { 57 1.1 jmcneill const char *desc; 58 1.1 jmcneill bus_size_t reg; 59 1.1 jmcneill uint32_t mask; 60 1.2 jmcneill u_int flags; 61 1.2 jmcneill #define SUNXI_SRAMC_F_SWAP __BIT(0) 62 1.6 thorpej }; 63 1.6 thorpej 64 1.6 thorpej static const struct sunxi_sramc_area sunxi_sramc_area_a3_a4 = { 65 1.6 thorpej .desc = "SRAM A3/A4", 66 1.6 thorpej .reg = 0x04, 67 1.6 thorpej .mask = __BITS(5,4), 68 1.6 thorpej .flags = 0, 69 1.6 thorpej }; 70 1.6 thorpej 71 1.6 thorpej static const struct sunxi_sramc_area sunxi_sramc_area_d = { 72 1.6 thorpej .desc = "SRAM D", 73 1.6 thorpej .reg = 0x04, 74 1.6 thorpej .mask = __BIT(0), 75 1.6 thorpej .flags = 0, 76 1.6 thorpej }; 77 1.6 thorpej 78 1.6 thorpej static const struct sunxi_sramc_area sunxi_sramc_area_c = { 79 1.6 thorpej .desc = "SRAM C", 80 1.6 thorpej .reg = 0x04, 81 1.6 thorpej .mask = __BIT(24), 82 1.6 thorpej .flags = SUNXI_SRAMC_F_SWAP, 83 1.6 thorpej }; 84 1.6 thorpej 85 1.6 thorpej static const struct device_compatible_entry sunxi_sramc_areas[] = { 86 1.6 thorpej { .compat = "allwinner,sun4i-a10-sram-a3-a4", 87 1.6 thorpej .data = &sunxi_sramc_area_a3_a4 }, 88 1.6 thorpej 89 1.6 thorpej { .compat = "allwinner,sun4i-a10-sram-d", 90 1.6 thorpej .data = &sunxi_sramc_area_d }, 91 1.6 thorpej 92 1.6 thorpej { .compat = "allwinner,sun50i-a64-sram-c", 93 1.6 thorpej .data = &sunxi_sramc_area_c }, 94 1.6 thorpej 95 1.10 thorpej DEVICE_COMPAT_EOL 96 1.1 jmcneill }; 97 1.1 jmcneill 98 1.1 jmcneill struct sunxi_sramc_node { 99 1.1 jmcneill int phandle; 100 1.1 jmcneill const struct sunxi_sramc_area *area; 101 1.1 jmcneill TAILQ_ENTRY(sunxi_sramc_node) nodes; 102 1.1 jmcneill }; 103 1.1 jmcneill 104 1.1 jmcneill struct sunxi_sramc_softc { 105 1.1 jmcneill device_t sc_dev; 106 1.1 jmcneill int sc_phandle; 107 1.1 jmcneill bus_space_tag_t sc_bst; 108 1.1 jmcneill bus_space_handle_t sc_bsh; 109 1.2 jmcneill kmutex_t sc_lock; 110 1.2 jmcneill struct syscon sc_syscon; 111 1.1 jmcneill TAILQ_HEAD(, sunxi_sramc_node) sc_nodes; 112 1.1 jmcneill }; 113 1.1 jmcneill 114 1.1 jmcneill static struct sunxi_sramc_softc *sramc_softc = NULL; 115 1.1 jmcneill 116 1.1 jmcneill #define SRAMC_READ(sc, reg) \ 117 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 118 1.1 jmcneill #define SRAMC_WRITE(sc, reg, val) \ 119 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 120 1.1 jmcneill 121 1.1 jmcneill static void 122 1.1 jmcneill sunxi_sramc_init_mmio(struct sunxi_sramc_softc *sc, int phandle) 123 1.1 jmcneill { 124 1.6 thorpej const struct device_compatible_entry *dce; 125 1.1 jmcneill struct sunxi_sramc_node *node; 126 1.6 thorpej int child; 127 1.1 jmcneill 128 1.6 thorpej for (child = OF_child(phandle); child; child = OF_peer(child)) { 129 1.11 thorpej dce = of_compatible_lookup(child, sunxi_sramc_areas); 130 1.8 thorpej if (dce != NULL) { 131 1.6 thorpej node = kmem_alloc(sizeof(*node), KM_SLEEP); 132 1.6 thorpej node->phandle = child; 133 1.6 thorpej node->area = dce->data; 134 1.6 thorpej TAILQ_INSERT_TAIL(&sc->sc_nodes, node, nodes); 135 1.6 thorpej aprint_verbose_dev(sc->sc_dev, "area: %s\n", 136 1.6 thorpej node->area->desc); 137 1.1 jmcneill } 138 1.6 thorpej } 139 1.1 jmcneill } 140 1.1 jmcneill 141 1.1 jmcneill static void 142 1.1 jmcneill sunxi_sramc_init(struct sunxi_sramc_softc *sc) 143 1.1 jmcneill { 144 1.6 thorpej const struct device_compatible_entry mmio_compat_data[] = { 145 1.6 thorpej { .compat = "mmio-sram" }, 146 1.10 thorpej DEVICE_COMPAT_EOL 147 1.6 thorpej }; 148 1.1 jmcneill int child; 149 1.1 jmcneill 150 1.1 jmcneill for (child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) { 151 1.11 thorpej if (!of_compatible_match(child, mmio_compat_data)) 152 1.1 jmcneill continue; 153 1.1 jmcneill sunxi_sramc_init_mmio(sc, child); 154 1.1 jmcneill } 155 1.1 jmcneill } 156 1.1 jmcneill 157 1.2 jmcneill static void 158 1.2 jmcneill sunxi_sramc_lock(void *priv) 159 1.2 jmcneill { 160 1.2 jmcneill struct sunxi_sramc_softc * const sc = priv; 161 1.2 jmcneill 162 1.2 jmcneill mutex_enter(&sc->sc_lock); 163 1.2 jmcneill } 164 1.2 jmcneill 165 1.2 jmcneill static void 166 1.2 jmcneill sunxi_sramc_unlock(void *priv) 167 1.2 jmcneill { 168 1.2 jmcneill struct sunxi_sramc_softc * const sc = priv; 169 1.2 jmcneill 170 1.2 jmcneill mutex_exit(&sc->sc_lock); 171 1.2 jmcneill } 172 1.2 jmcneill 173 1.2 jmcneill static uint32_t 174 1.2 jmcneill sunxi_sramc_read_4(void *priv, bus_size_t reg) 175 1.2 jmcneill { 176 1.2 jmcneill struct sunxi_sramc_softc * const sc = priv; 177 1.2 jmcneill 178 1.2 jmcneill KASSERT(mutex_owned(&sc->sc_lock)); 179 1.2 jmcneill 180 1.2 jmcneill return SRAMC_READ(sc, reg); 181 1.2 jmcneill } 182 1.2 jmcneill 183 1.2 jmcneill static void 184 1.2 jmcneill sunxi_sramc_write_4(void *priv, bus_size_t reg, uint32_t val) 185 1.2 jmcneill { 186 1.2 jmcneill struct sunxi_sramc_softc * const sc = priv; 187 1.2 jmcneill 188 1.2 jmcneill KASSERT(mutex_owned(&sc->sc_lock)); 189 1.2 jmcneill 190 1.2 jmcneill SRAMC_WRITE(sc, reg, val); 191 1.2 jmcneill } 192 1.2 jmcneill 193 1.1 jmcneill static int 194 1.1 jmcneill sunxi_sramc_match(device_t parent, cfdata_t cf, void *aux) 195 1.1 jmcneill { 196 1.1 jmcneill struct fdt_attach_args * const faa = aux; 197 1.1 jmcneill 198 1.11 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 199 1.1 jmcneill } 200 1.1 jmcneill 201 1.1 jmcneill static void 202 1.1 jmcneill sunxi_sramc_attach(device_t parent, device_t self, void *aux) 203 1.1 jmcneill { 204 1.1 jmcneill struct sunxi_sramc_softc * const sc = device_private(self); 205 1.1 jmcneill struct fdt_attach_args * const faa = aux; 206 1.1 jmcneill const int phandle = faa->faa_phandle; 207 1.1 jmcneill bus_addr_t addr; 208 1.1 jmcneill bus_size_t size; 209 1.1 jmcneill 210 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 211 1.1 jmcneill aprint_error(": couldn't get registers\n"); 212 1.1 jmcneill return; 213 1.1 jmcneill } 214 1.1 jmcneill 215 1.1 jmcneill sc->sc_dev = self; 216 1.1 jmcneill sc->sc_phandle = phandle; 217 1.1 jmcneill sc->sc_bst = faa->faa_bst; 218 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 219 1.1 jmcneill aprint_error(": couldn't map registers\n"); 220 1.1 jmcneill return; 221 1.1 jmcneill } 222 1.2 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 223 1.1 jmcneill TAILQ_INIT(&sc->sc_nodes); 224 1.1 jmcneill 225 1.1 jmcneill aprint_naive("\n"); 226 1.1 jmcneill aprint_normal(": SRAM Controller\n"); 227 1.1 jmcneill 228 1.1 jmcneill sunxi_sramc_init(sc); 229 1.1 jmcneill 230 1.1 jmcneill KASSERT(sramc_softc == NULL); 231 1.1 jmcneill sramc_softc = sc; 232 1.2 jmcneill 233 1.2 jmcneill sc->sc_syscon.priv = sc; 234 1.2 jmcneill sc->sc_syscon.lock = sunxi_sramc_lock; 235 1.2 jmcneill sc->sc_syscon.unlock = sunxi_sramc_unlock; 236 1.2 jmcneill sc->sc_syscon.read_4 = sunxi_sramc_read_4; 237 1.2 jmcneill sc->sc_syscon.write_4 = sunxi_sramc_write_4; 238 1.2 jmcneill fdtbus_register_syscon(self, phandle, &sc->sc_syscon); 239 1.1 jmcneill } 240 1.1 jmcneill 241 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_sramc, sizeof(struct sunxi_sramc_softc), 242 1.1 jmcneill sunxi_sramc_match, sunxi_sramc_attach, NULL, NULL); 243 1.1 jmcneill 244 1.1 jmcneill static int 245 1.1 jmcneill sunxi_sramc_map(const int node_phandle, u_int config) 246 1.1 jmcneill { 247 1.1 jmcneill struct sunxi_sramc_softc * const sc = sramc_softc; 248 1.1 jmcneill struct sunxi_sramc_node *node; 249 1.1 jmcneill uint32_t val; 250 1.1 jmcneill 251 1.1 jmcneill if (sc == NULL) 252 1.1 jmcneill return ENXIO; 253 1.1 jmcneill 254 1.1 jmcneill TAILQ_FOREACH(node, &sc->sc_nodes, nodes) 255 1.1 jmcneill if (node->phandle == node_phandle) { 256 1.1 jmcneill if (config > __SHIFTOUT_MASK(node->area->mask)) 257 1.1 jmcneill return ERANGE; 258 1.2 jmcneill if ((node->area->flags & SUNXI_SRAMC_F_SWAP) != 0) 259 1.2 jmcneill config = !config; 260 1.1 jmcneill val = SRAMC_READ(sc, node->area->reg); 261 1.1 jmcneill val &= ~node->area->mask; 262 1.1 jmcneill val |= __SHIFTIN(config, node->area->mask); 263 1.1 jmcneill SRAMC_WRITE(sc, node->area->reg, val); 264 1.1 jmcneill return 0; 265 1.1 jmcneill } 266 1.1 jmcneill 267 1.1 jmcneill return EINVAL; 268 1.1 jmcneill } 269 1.1 jmcneill 270 1.1 jmcneill int 271 1.1 jmcneill sunxi_sramc_claim(const int phandle) 272 1.1 jmcneill { 273 1.1 jmcneill const u_int *data; 274 1.1 jmcneill int len; 275 1.1 jmcneill 276 1.1 jmcneill data = fdtbus_get_prop(phandle, "allwinner,sram", &len); 277 1.1 jmcneill if (data == NULL) 278 1.1 jmcneill return ENOENT; 279 1.1 jmcneill if (len != 8) 280 1.1 jmcneill return EIO; 281 1.1 jmcneill 282 1.1 jmcneill const int node_phandle = fdtbus_get_phandle_from_native(be32toh(data[0])); 283 1.1 jmcneill const u_int config = be32toh(data[1]); 284 1.1 jmcneill 285 1.1 jmcneill return sunxi_sramc_map(node_phandle, config); 286 1.1 jmcneill } 287