1 /* $NetBSD: syscon.c,v 1.2 2018/06/30 16:22:56 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca> 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 ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * 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 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: syscon.c,v 1.2 2018/06/30 16:22:56 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/device.h> 35 #include <sys/systm.h> 36 #include <sys/sysctl.h> 37 #include <sys/kmem.h> 38 #include <sys/gpio.h> 39 40 #include <dev/fdt/fdtvar.h> 41 #include <dev/fdt/syscon.h> 42 43 struct syscon_softc { 44 device_t sc_dev; 45 bus_space_tag_t sc_bst; 46 bus_space_handle_t sc_bsh; 47 kmutex_t sc_lock; 48 49 struct syscon sc_syscon; 50 }; 51 52 static int syscon_match(device_t, cfdata_t, void *); 53 static void syscon_attach(device_t, device_t, void *); 54 55 static const char *compatible[] = { 56 "syscon", 57 NULL 58 }; 59 60 CFATTACH_DECL_NEW(syscon, sizeof(struct syscon_softc), 61 syscon_match, syscon_attach, NULL, NULL); 62 63 static void 64 syscon_generic_lock(void *priv) 65 { 66 struct syscon_softc * const sc = priv; 67 68 mutex_enter(&sc->sc_lock); 69 } 70 71 static void 72 syscon_generic_unlock(void *priv) 73 { 74 struct syscon_softc * const sc = priv; 75 76 mutex_exit(&sc->sc_lock); 77 } 78 79 static uint32_t 80 syscon_generic_read_4(void *priv, bus_size_t reg) 81 { 82 struct syscon_softc * const sc = priv; 83 84 KASSERT(mutex_owned(&sc->sc_lock)); 85 86 return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg); 87 } 88 89 static void 90 syscon_generic_write_4(void *priv, bus_size_t reg, uint32_t val) 91 { 92 struct syscon_softc * const sc = priv; 93 94 KASSERT(mutex_owned(&sc->sc_lock)); 95 96 bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val); 97 } 98 99 static int 100 syscon_match(device_t parent, cfdata_t cf, void *aux) 101 { 102 struct fdt_attach_args * const faa = aux; 103 104 return of_match_compatible(faa->faa_phandle, compatible); 105 } 106 107 static void 108 syscon_attach(device_t parent, device_t self, void *aux) 109 { 110 struct syscon_softc * const sc = device_private(self); 111 struct fdt_attach_args * const faa = aux; 112 const int phandle = faa->faa_phandle; 113 bus_addr_t addr; 114 bus_size_t size; 115 116 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 117 aprint_error(": couldn't get registers\n"); 118 return; 119 } 120 121 sc->sc_dev = self; 122 sc->sc_bst = faa->faa_bst; 123 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 124 aprint_error(": couldn't map registers\n"); 125 return; 126 } 127 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 128 sc->sc_syscon.priv = sc; 129 sc->sc_syscon.lock = syscon_generic_lock; 130 sc->sc_syscon.unlock = syscon_generic_unlock; 131 sc->sc_syscon.read_4 = syscon_generic_read_4; 132 sc->sc_syscon.write_4 = syscon_generic_write_4; 133 134 aprint_naive("\n"); 135 aprint_normal(": System Controller Registers\n"); 136 137 fdtbus_register_syscon(self, phandle, &sc->sc_syscon); 138 139 fdt_add_bus(self, phandle, faa); 140 } 141