Home | History | Annotate | Line # | Download | only in starfive
      1 /* $NetBSD: jh7110_syscon.c,v 1.1 2024/11/11 20:30:08 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: jh7110_syscon.c,v 1.1 2024/11/11 20:30:08 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 
     37 #include <sys/mutex.h>
     38 
     39 #include <dev/fdt/fdtvar.h>
     40 #include <dev/fdt/syscon.h>
     41 
     42 struct jh7110_syscon_softc {
     43 	device_t		sc_dev;
     44 	bus_space_tag_t		sc_bst;
     45 	bus_space_handle_t	sc_bsh;
     46 	int			sc_phandle;
     47 
     48 	kmutex_t		sc_lock;
     49 	struct syscon		sc_syscon;
     50 };
     51 
     52 #define RD4(sc, reg)							       \
     53 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     54 #define WR4(sc, reg, val)						       \
     55 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     56 
     57 
     58 static void
     59 jh7110_syscon_lock(void *priv)
     60 {
     61 	struct jh7110_syscon_softc * const sc = priv;
     62 
     63 	mutex_enter(&sc->sc_lock);
     64 }
     65 
     66 static void
     67 jh7110_syscon_unlock(void *priv)
     68 {
     69 	struct jh7110_syscon_softc * const sc = priv;
     70 
     71 	mutex_exit(&sc->sc_lock);
     72 }
     73 
     74 static uint32_t
     75 jh7110_syscon_read_4(void *priv, bus_size_t reg)
     76 {
     77 	struct jh7110_syscon_softc * const sc = priv;
     78 
     79 	KASSERT(mutex_owned(&sc->sc_lock));
     80 
     81 	return RD4(sc, reg);
     82 }
     83 
     84 static void
     85 jh7110_syscon_write_4(void *priv, bus_size_t reg, uint32_t val)
     86 {
     87 	struct jh7110_syscon_softc * const sc = priv;
     88 
     89 	KASSERT(mutex_owned(&sc->sc_lock));
     90 
     91 	WR4(sc, reg, val);
     92 }
     93 
     94 
     95 /* Compat string(s) */
     96 static const struct device_compatible_entry compat_data[] = {
     97 	{ .compat = "starfive,jh7110-stg-syscon" },
     98 	DEVICE_COMPAT_EOL
     99 };
    100 
    101 static int
    102 jh7110_syscon_match(device_t parent, cfdata_t cf, void *aux)
    103 {
    104 	struct fdt_attach_args * const faa = aux;
    105 
    106 	return of_compatible_match(faa->faa_phandle, compat_data);
    107 }
    108 
    109 static void
    110 jh7110_syscon_attach(device_t parent, device_t self, void *aux)
    111 {
    112 	struct jh7110_syscon_softc *sc = device_private(self);
    113 	struct fdt_attach_args * const faa = aux;
    114 	const int phandle = faa->faa_phandle;
    115 	const bus_space_tag_t bst = faa->faa_bst;
    116 	bus_addr_t addr;
    117 	bus_size_t size;
    118 	int error;
    119 
    120 	sc->sc_dev = self;
    121 	sc->sc_phandle = phandle;
    122 	sc->sc_bst = bst;
    123 
    124 	error = fdtbus_get_reg(phandle, 0, &addr, &size);
    125 	if (error) {
    126 		aprint_error(": couldn't get registers\n");
    127 		return;
    128 	}
    129 
    130 	error = bus_space_map(bst, addr, size, 0, &sc->sc_bsh);
    131 	if (error) {
    132 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr,
    133 		    error);
    134 		return;
    135 	}
    136 
    137 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    138 
    139 	aprint_naive("\n");
    140 	aprint_normal(": JH7110 STG system controller\n");
    141 
    142 	sc->sc_syscon.priv = sc;
    143 	sc->sc_syscon.lock = jh7110_syscon_lock;
    144 	sc->sc_syscon.unlock = jh7110_syscon_unlock;
    145 	sc->sc_syscon.read_4 = jh7110_syscon_read_4;
    146 	sc->sc_syscon.write_4 = jh7110_syscon_write_4;
    147 	fdtbus_register_syscon(self, phandle, &sc->sc_syscon);
    148 }
    149 
    150 CFATTACH_DECL_NEW(jh7110_syscon, sizeof(struct jh7110_syscon_softc),
    151 	jh7110_syscon_match, jh7110_syscon_attach, NULL, NULL);
    152