Home | History | Annotate | Line # | Download | only in fdt
      1 /* $NetBSD: syscon.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 Jared McNeill <jmcneill (at) 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.5 2021/01/27 03:10:21 thorpej 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 struct device_compatible_entry compat_data[] = {
     56 	{ .compat = "syscon" },
     57 	{ .compat = "simple-mfd" },
     58 	DEVICE_COMPAT_EOL
     59 };
     60 
     61 CFATTACH_DECL_NEW(syscon, sizeof(struct syscon_softc),
     62 	syscon_match, syscon_attach, NULL, NULL);
     63 
     64 static void
     65 syscon_generic_lock(void *priv)
     66 {
     67 	struct syscon_softc * const sc = priv;
     68 
     69 	mutex_enter(&sc->sc_lock);
     70 }
     71 
     72 static void
     73 syscon_generic_unlock(void *priv)
     74 {
     75 	struct syscon_softc * const sc = priv;
     76 
     77 	mutex_exit(&sc->sc_lock);
     78 }
     79 
     80 static uint32_t
     81 syscon_generic_read_4(void *priv, bus_size_t reg)
     82 {
     83 	struct syscon_softc * const sc = priv;
     84 
     85 	KASSERT(mutex_owned(&sc->sc_lock));
     86 
     87 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
     88 }
     89 
     90 static void
     91 syscon_generic_write_4(void *priv, bus_size_t reg, uint32_t val)
     92 {
     93 	struct syscon_softc * const sc = priv;
     94 
     95 	KASSERT(mutex_owned(&sc->sc_lock));
     96 
     97 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val);
     98 }
     99 
    100 static int
    101 syscon_match(device_t parent, cfdata_t cf, void *aux)
    102 {
    103 	struct fdt_attach_args * const faa = aux;
    104 
    105 	return of_compatible_match(faa->faa_phandle, compat_data);
    106 }
    107 
    108 static void
    109 syscon_attach(device_t parent, device_t self, void *aux)
    110 {
    111 	struct syscon_softc * const sc = device_private(self);
    112 	struct fdt_attach_args * const faa = aux;
    113 	const int phandle = faa->faa_phandle;
    114 	bus_addr_t addr;
    115 	bus_size_t size;
    116 	int child;
    117 
    118 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    119 		aprint_error(": couldn't get registers\n");
    120 		return;
    121 	}
    122 
    123 	sc->sc_dev = self;
    124 	sc->sc_bst = faa->faa_bst;
    125 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    126 		aprint_error(": couldn't map registers\n");
    127 		return;
    128 	}
    129 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    130 	sc->sc_syscon.priv = sc;
    131 	sc->sc_syscon.lock = syscon_generic_lock;
    132 	sc->sc_syscon.unlock = syscon_generic_unlock;
    133 	sc->sc_syscon.read_4 = syscon_generic_read_4;
    134 	sc->sc_syscon.write_4 = syscon_generic_write_4;
    135 
    136 	aprint_naive("\n");
    137 	aprint_normal(": System Controller Registers\n");
    138 
    139 	fdtbus_register_syscon(self, phandle, &sc->sc_syscon);
    140 
    141 	fdt_add_bus(self, phandle, faa);
    142 
    143 	child = of_find_firstchild_byname(phandle, "clocks");
    144 	if (child > 0)
    145 		fdt_add_bus(self, child, faa);
    146 }
    147