syscon.c revision 1.1
11.1Sjmcneill/* $NetBSD: syscon.c,v 1.1 2018/06/30 12:35:18 jmcneill 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.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: syscon.c,v 1.1 2018/06/30 12:35:18 jmcneill 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.1Sjmcneillstatic const char *compatible[] = { 561.1Sjmcneill "syscon", 571.1Sjmcneill NULL 581.1Sjmcneill}; 591.1Sjmcneill 601.1SjmcneillCFATTACH_DECL_NEW(syscon, sizeof(struct syscon_softc), 611.1Sjmcneill syscon_match, syscon_attach, NULL, NULL); 621.1Sjmcneill 631.1Sjmcneillstatic void 641.1Sjmcneillsyscon_generic_lock(void *priv) 651.1Sjmcneill{ 661.1Sjmcneill struct syscon_softc * const sc = priv; 671.1Sjmcneill 681.1Sjmcneill mutex_enter(&sc->sc_lock); 691.1Sjmcneill} 701.1Sjmcneill 711.1Sjmcneillstatic void 721.1Sjmcneillsyscon_generic_unlock(void *priv) 731.1Sjmcneill{ 741.1Sjmcneill struct syscon_softc * const sc = priv; 751.1Sjmcneill 761.1Sjmcneill mutex_exit(&sc->sc_lock); 771.1Sjmcneill} 781.1Sjmcneill 791.1Sjmcneillstatic uint32_t 801.1Sjmcneillsyscon_generic_read_4(void *priv, bus_size_t reg) 811.1Sjmcneill{ 821.1Sjmcneill struct syscon_softc * const sc = priv; 831.1Sjmcneill 841.1Sjmcneill KASSERT(mutex_owned(&sc->sc_lock)); 851.1Sjmcneill 861.1Sjmcneill return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg); 871.1Sjmcneill} 881.1Sjmcneill 891.1Sjmcneillstatic void 901.1Sjmcneillsyscon_generic_write_4(void *priv, bus_size_t reg, uint32_t val) 911.1Sjmcneill{ 921.1Sjmcneill struct syscon_softc * const sc = priv; 931.1Sjmcneill 941.1Sjmcneill KASSERT(mutex_owned(&sc->sc_lock)); 951.1Sjmcneill 961.1Sjmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val); 971.1Sjmcneill} 981.1Sjmcneill 991.1Sjmcneillstatic int 1001.1Sjmcneillsyscon_match(device_t parent, cfdata_t cf, void *aux) 1011.1Sjmcneill{ 1021.1Sjmcneill struct fdt_attach_args * const faa = aux; 1031.1Sjmcneill 1041.1Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 1051.1Sjmcneill} 1061.1Sjmcneill 1071.1Sjmcneillstatic void 1081.1Sjmcneillsyscon_attach(device_t parent, device_t self, void *aux) 1091.1Sjmcneill{ 1101.1Sjmcneill struct syscon_softc * const sc = device_private(self); 1111.1Sjmcneill struct fdt_attach_args * const faa = aux; 1121.1Sjmcneill const int phandle = faa->faa_phandle; 1131.1Sjmcneill bus_addr_t addr; 1141.1Sjmcneill bus_size_t size; 1151.1Sjmcneill 1161.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1171.1Sjmcneill aprint_error(": couldn't get registers\n"); 1181.1Sjmcneill return; 1191.1Sjmcneill } 1201.1Sjmcneill 1211.1Sjmcneill sc->sc_dev = self; 1221.1Sjmcneill sc->sc_bst = faa->faa_bst; 1231.1Sjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 1241.1Sjmcneill aprint_error(": couldn't map registers\n"); 1251.1Sjmcneill return; 1261.1Sjmcneill } 1271.1Sjmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 1281.1Sjmcneill sc->sc_syscon.priv = sc; 1291.1Sjmcneill sc->sc_syscon.lock = syscon_generic_lock; 1301.1Sjmcneill sc->sc_syscon.unlock = syscon_generic_unlock; 1311.1Sjmcneill sc->sc_syscon.read_4 = syscon_generic_read_4; 1321.1Sjmcneill sc->sc_syscon.write_4 = syscon_generic_write_4; 1331.1Sjmcneill 1341.1Sjmcneill aprint_naive("\n"); 1351.1Sjmcneill aprint_normal(": System Controller Registers\n"); 1361.1Sjmcneill 1371.1Sjmcneill fdtbus_register_syscon(self, phandle, &sc->sc_syscon); 1381.1Sjmcneill} 139