11.5Sthorpej/* $NetBSD: syscon.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca> 51.1Sjmcneill * All rights reserved. 61.1Sjmcneill * 71.1Sjmcneill * Redistribution and use in source and binary forms, with or without 81.1Sjmcneill * modification, are permitted provided that the following conditions 91.1Sjmcneill * are met: 101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright 111.1Sjmcneill * notice, this list of conditions and the following disclaimer. 121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 131.1Sjmcneill * notice, this list of conditions and the following disclaimer in the 141.1Sjmcneill * documentation and/or other materials provided with the distribution. 151.1Sjmcneill * 161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261.1Sjmcneill * SUCH DAMAGE. 271.1Sjmcneill */ 281.1Sjmcneill 291.1Sjmcneill#include <sys/cdefs.h> 301.5Sthorpej__KERNEL_RCSID(0, "$NetBSD: syscon.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $"); 311.1Sjmcneill 321.1Sjmcneill#include <sys/param.h> 331.1Sjmcneill#include <sys/bus.h> 341.1Sjmcneill#include <sys/device.h> 351.1Sjmcneill#include <sys/systm.h> 361.1Sjmcneill#include <sys/sysctl.h> 371.1Sjmcneill#include <sys/kmem.h> 381.1Sjmcneill#include <sys/gpio.h> 391.1Sjmcneill 401.1Sjmcneill#include <dev/fdt/fdtvar.h> 411.1Sjmcneill#include <dev/fdt/syscon.h> 421.1Sjmcneill 431.1Sjmcneillstruct syscon_softc { 441.1Sjmcneill device_t sc_dev; 451.1Sjmcneill bus_space_tag_t sc_bst; 461.1Sjmcneill bus_space_handle_t sc_bsh; 471.1Sjmcneill kmutex_t sc_lock; 481.1Sjmcneill 491.1Sjmcneill struct syscon sc_syscon; 501.1Sjmcneill}; 511.1Sjmcneill 521.1Sjmcneillstatic int syscon_match(device_t, cfdata_t, void *); 531.1Sjmcneillstatic void syscon_attach(device_t, device_t, void *); 541.1Sjmcneill 551.5Sthorpejstatic const struct device_compatible_entry compat_data[] = { 561.5Sthorpej { .compat = "syscon" }, 571.5Sthorpej { .compat = "simple-mfd" }, 581.5Sthorpej DEVICE_COMPAT_EOL 591.1Sjmcneill}; 601.1Sjmcneill 611.1SjmcneillCFATTACH_DECL_NEW(syscon, sizeof(struct syscon_softc), 621.1Sjmcneill syscon_match, syscon_attach, NULL, NULL); 631.1Sjmcneill 641.1Sjmcneillstatic void 651.1Sjmcneillsyscon_generic_lock(void *priv) 661.1Sjmcneill{ 671.1Sjmcneill struct syscon_softc * const sc = priv; 681.1Sjmcneill 691.1Sjmcneill mutex_enter(&sc->sc_lock); 701.1Sjmcneill} 711.1Sjmcneill 721.1Sjmcneillstatic void 731.1Sjmcneillsyscon_generic_unlock(void *priv) 741.1Sjmcneill{ 751.1Sjmcneill struct syscon_softc * const sc = priv; 761.1Sjmcneill 771.1Sjmcneill mutex_exit(&sc->sc_lock); 781.1Sjmcneill} 791.1Sjmcneill 801.1Sjmcneillstatic uint32_t 811.1Sjmcneillsyscon_generic_read_4(void *priv, bus_size_t reg) 821.1Sjmcneill{ 831.1Sjmcneill struct syscon_softc * const sc = priv; 841.1Sjmcneill 851.1Sjmcneill KASSERT(mutex_owned(&sc->sc_lock)); 861.1Sjmcneill 871.1Sjmcneill return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg); 881.1Sjmcneill} 891.1Sjmcneill 901.1Sjmcneillstatic void 911.1Sjmcneillsyscon_generic_write_4(void *priv, bus_size_t reg, uint32_t val) 921.1Sjmcneill{ 931.1Sjmcneill struct syscon_softc * const sc = priv; 941.1Sjmcneill 951.1Sjmcneill KASSERT(mutex_owned(&sc->sc_lock)); 961.1Sjmcneill 971.1Sjmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val); 981.1Sjmcneill} 991.1Sjmcneill 1001.1Sjmcneillstatic int 1011.1Sjmcneillsyscon_match(device_t parent, cfdata_t cf, void *aux) 1021.1Sjmcneill{ 1031.1Sjmcneill struct fdt_attach_args * const faa = aux; 1041.1Sjmcneill 1051.5Sthorpej return of_compatible_match(faa->faa_phandle, compat_data); 1061.1Sjmcneill} 1071.1Sjmcneill 1081.1Sjmcneillstatic void 1091.1Sjmcneillsyscon_attach(device_t parent, device_t self, void *aux) 1101.1Sjmcneill{ 1111.1Sjmcneill struct syscon_softc * const sc = device_private(self); 1121.1Sjmcneill struct fdt_attach_args * const faa = aux; 1131.1Sjmcneill const int phandle = faa->faa_phandle; 1141.1Sjmcneill bus_addr_t addr; 1151.1Sjmcneill bus_size_t size; 1161.4Sjmcneill int child; 1171.1Sjmcneill 1181.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1191.1Sjmcneill aprint_error(": couldn't get registers\n"); 1201.1Sjmcneill return; 1211.1Sjmcneill } 1221.1Sjmcneill 1231.1Sjmcneill sc->sc_dev = self; 1241.1Sjmcneill sc->sc_bst = faa->faa_bst; 1251.1Sjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 1261.1Sjmcneill aprint_error(": couldn't map registers\n"); 1271.1Sjmcneill return; 1281.1Sjmcneill } 1291.1Sjmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 1301.1Sjmcneill sc->sc_syscon.priv = sc; 1311.1Sjmcneill sc->sc_syscon.lock = syscon_generic_lock; 1321.1Sjmcneill sc->sc_syscon.unlock = syscon_generic_unlock; 1331.1Sjmcneill sc->sc_syscon.read_4 = syscon_generic_read_4; 1341.1Sjmcneill sc->sc_syscon.write_4 = syscon_generic_write_4; 1351.1Sjmcneill 1361.1Sjmcneill aprint_naive("\n"); 1371.1Sjmcneill aprint_normal(": System Controller Registers\n"); 1381.1Sjmcneill 1391.1Sjmcneill fdtbus_register_syscon(self, phandle, &sc->sc_syscon); 1401.2Sjmcneill 1411.2Sjmcneill fdt_add_bus(self, phandle, faa); 1421.4Sjmcneill 1431.4Sjmcneill child = of_find_firstchild_byname(phandle, "clocks"); 1441.4Sjmcneill if (child > 0) 1451.4Sjmcneill fdt_add_bus(self, child, faa); 1461.1Sjmcneill} 147