Home | History | Annotate | Line # | Download | only in sunxi
sunxi_sramc.c revision 1.1.4.1
      1  1.1.4.1  pgoyette /* $NetBSD: sunxi_sramc.c,v 1.1.4.1 2019/01/26 22:00:01 pgoyette Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21      1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22      1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23      1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24      1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  jmcneill  * SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill #include <sys/cdefs.h>
     30  1.1.4.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: sunxi_sramc.c,v 1.1.4.1 2019/01/26 22:00:01 pgoyette Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.1  jmcneill #include <sys/bus.h>
     34      1.1  jmcneill #include <sys/device.h>
     35      1.1  jmcneill #include <sys/intr.h>
     36      1.1  jmcneill #include <sys/systm.h>
     37      1.1  jmcneill #include <sys/kmem.h>
     38  1.1.4.1  pgoyette #include <sys/mutex.h>
     39      1.1  jmcneill 
     40      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     41  1.1.4.1  pgoyette #include <dev/fdt/syscon.h>
     42      1.1  jmcneill 
     43      1.1  jmcneill #include <arm/sunxi/sunxi_sramc.h>
     44      1.1  jmcneill 
     45      1.1  jmcneill static const char * compatible[] = {
     46      1.1  jmcneill 	"allwinner,sun4i-a10-sram-controller",
     47  1.1.4.1  pgoyette 	"allwinner,sun50i-a64-system-control",
     48  1.1.4.1  pgoyette 	"allwinner,sun50i-h6-system-control",
     49      1.1  jmcneill 	NULL
     50      1.1  jmcneill };
     51      1.1  jmcneill 
     52      1.1  jmcneill static const struct sunxi_sramc_area {
     53      1.1  jmcneill 	const char			*compatible;
     54      1.1  jmcneill 	const char			*desc;
     55      1.1  jmcneill 	bus_size_t			reg;
     56      1.1  jmcneill 	uint32_t			mask;
     57  1.1.4.1  pgoyette 	u_int				flags;
     58  1.1.4.1  pgoyette #define	SUNXI_SRAMC_F_SWAP		__BIT(0)
     59      1.1  jmcneill } sunxi_sramc_areas[] = {
     60      1.1  jmcneill 	{ "allwinner,sun4i-a10-sram-a3-a4",
     61      1.1  jmcneill 	  "SRAM A3/A4",
     62  1.1.4.1  pgoyette 	  0x04, __BITS(5,4), 0 },
     63      1.1  jmcneill 	{ "allwinner,sun4i-a10-sram-d",
     64      1.1  jmcneill 	  "SRAM D",
     65  1.1.4.1  pgoyette 	  0x04, __BIT(0), 0 },
     66  1.1.4.1  pgoyette 	{ "allwinner,sun50i-a64-sram-c",
     67  1.1.4.1  pgoyette 	  "SRAM C",
     68  1.1.4.1  pgoyette 	  0x04, __BIT(24), SUNXI_SRAMC_F_SWAP },
     69      1.1  jmcneill };
     70      1.1  jmcneill 
     71      1.1  jmcneill struct sunxi_sramc_node {
     72      1.1  jmcneill 	int				phandle;
     73      1.1  jmcneill 	const struct sunxi_sramc_area	*area;
     74      1.1  jmcneill 	TAILQ_ENTRY(sunxi_sramc_node)	nodes;
     75      1.1  jmcneill };
     76      1.1  jmcneill 
     77      1.1  jmcneill struct sunxi_sramc_softc {
     78      1.1  jmcneill 	device_t			sc_dev;
     79      1.1  jmcneill 	int				sc_phandle;
     80      1.1  jmcneill 	bus_space_tag_t			sc_bst;
     81      1.1  jmcneill 	bus_space_handle_t		sc_bsh;
     82  1.1.4.1  pgoyette 	kmutex_t			sc_lock;
     83  1.1.4.1  pgoyette 	struct syscon			sc_syscon;
     84      1.1  jmcneill 	TAILQ_HEAD(, sunxi_sramc_node)	sc_nodes;
     85      1.1  jmcneill };
     86      1.1  jmcneill 
     87      1.1  jmcneill static struct sunxi_sramc_softc *sramc_softc = NULL;
     88      1.1  jmcneill 
     89      1.1  jmcneill #define SRAMC_READ(sc, reg) \
     90      1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     91      1.1  jmcneill #define SRAMC_WRITE(sc, reg, val) \
     92      1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     93      1.1  jmcneill 
     94      1.1  jmcneill static void
     95      1.1  jmcneill sunxi_sramc_init_mmio(struct sunxi_sramc_softc *sc, int phandle)
     96      1.1  jmcneill {
     97      1.1  jmcneill 	struct sunxi_sramc_node *node;
     98      1.1  jmcneill 	int child, i;
     99      1.1  jmcneill 
    100      1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child))
    101      1.1  jmcneill 		for (i = 0; i < __arraycount(sunxi_sramc_areas); i++) {
    102      1.1  jmcneill 			const char * area_compatible[] = { sunxi_sramc_areas[i].compatible, NULL };
    103      1.1  jmcneill 			if (of_match_compatible(child, area_compatible)) {
    104      1.1  jmcneill 				node = kmem_alloc(sizeof(*node), KM_SLEEP);
    105      1.1  jmcneill 				node->phandle = child;
    106      1.1  jmcneill 				node->area = &sunxi_sramc_areas[i];
    107      1.1  jmcneill 				TAILQ_INSERT_TAIL(&sc->sc_nodes, node, nodes);
    108      1.1  jmcneill 				aprint_verbose_dev(sc->sc_dev, "area: %s\n", node->area->desc);
    109      1.1  jmcneill 				break;
    110      1.1  jmcneill 			}
    111      1.1  jmcneill 		}
    112      1.1  jmcneill }
    113      1.1  jmcneill 
    114      1.1  jmcneill static void
    115      1.1  jmcneill sunxi_sramc_init(struct sunxi_sramc_softc *sc)
    116      1.1  jmcneill {
    117      1.1  jmcneill 	const char * mmio_compatible[] = { "mmio-sram", NULL };
    118      1.1  jmcneill 	int child;
    119      1.1  jmcneill 
    120      1.1  jmcneill 	for (child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
    121      1.1  jmcneill 		if (!of_match_compatible(child, mmio_compatible))
    122      1.1  jmcneill 			continue;
    123      1.1  jmcneill 		sunxi_sramc_init_mmio(sc, child);
    124      1.1  jmcneill 	}
    125      1.1  jmcneill }
    126      1.1  jmcneill 
    127  1.1.4.1  pgoyette static void
    128  1.1.4.1  pgoyette sunxi_sramc_lock(void *priv)
    129  1.1.4.1  pgoyette {
    130  1.1.4.1  pgoyette 	struct sunxi_sramc_softc * const sc = priv;
    131  1.1.4.1  pgoyette 
    132  1.1.4.1  pgoyette 	mutex_enter(&sc->sc_lock);
    133  1.1.4.1  pgoyette }
    134  1.1.4.1  pgoyette 
    135  1.1.4.1  pgoyette static void
    136  1.1.4.1  pgoyette sunxi_sramc_unlock(void *priv)
    137  1.1.4.1  pgoyette {
    138  1.1.4.1  pgoyette 	struct sunxi_sramc_softc * const sc = priv;
    139  1.1.4.1  pgoyette 
    140  1.1.4.1  pgoyette 	mutex_exit(&sc->sc_lock);
    141  1.1.4.1  pgoyette }
    142  1.1.4.1  pgoyette 
    143  1.1.4.1  pgoyette static uint32_t
    144  1.1.4.1  pgoyette sunxi_sramc_read_4(void *priv, bus_size_t reg)
    145  1.1.4.1  pgoyette {
    146  1.1.4.1  pgoyette 	struct sunxi_sramc_softc * const sc = priv;
    147  1.1.4.1  pgoyette 
    148  1.1.4.1  pgoyette 	KASSERT(mutex_owned(&sc->sc_lock));
    149  1.1.4.1  pgoyette 
    150  1.1.4.1  pgoyette 	return SRAMC_READ(sc, reg);
    151  1.1.4.1  pgoyette }
    152  1.1.4.1  pgoyette 
    153  1.1.4.1  pgoyette static void
    154  1.1.4.1  pgoyette sunxi_sramc_write_4(void *priv, bus_size_t reg, uint32_t val)
    155  1.1.4.1  pgoyette {
    156  1.1.4.1  pgoyette 	struct sunxi_sramc_softc * const sc = priv;
    157  1.1.4.1  pgoyette 
    158  1.1.4.1  pgoyette 	KASSERT(mutex_owned(&sc->sc_lock));
    159  1.1.4.1  pgoyette 
    160  1.1.4.1  pgoyette 	SRAMC_WRITE(sc, reg, val);
    161  1.1.4.1  pgoyette }
    162  1.1.4.1  pgoyette 
    163      1.1  jmcneill static int
    164      1.1  jmcneill sunxi_sramc_match(device_t parent, cfdata_t cf, void *aux)
    165      1.1  jmcneill {
    166      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    167      1.1  jmcneill 
    168      1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    169      1.1  jmcneill }
    170      1.1  jmcneill 
    171      1.1  jmcneill static void
    172      1.1  jmcneill sunxi_sramc_attach(device_t parent, device_t self, void *aux)
    173      1.1  jmcneill {
    174      1.1  jmcneill 	struct sunxi_sramc_softc * const sc = device_private(self);
    175      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    176      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    177      1.1  jmcneill 	bus_addr_t addr;
    178      1.1  jmcneill 	bus_size_t size;
    179      1.1  jmcneill 
    180      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    181      1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    182      1.1  jmcneill 		return;
    183      1.1  jmcneill 	}
    184      1.1  jmcneill 
    185      1.1  jmcneill 	sc->sc_dev = self;
    186      1.1  jmcneill 	sc->sc_phandle = phandle;
    187      1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    188      1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    189      1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    190      1.1  jmcneill 		return;
    191      1.1  jmcneill 	}
    192  1.1.4.1  pgoyette 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    193      1.1  jmcneill 	TAILQ_INIT(&sc->sc_nodes);
    194      1.1  jmcneill 
    195      1.1  jmcneill 	aprint_naive("\n");
    196      1.1  jmcneill 	aprint_normal(": SRAM Controller\n");
    197      1.1  jmcneill 
    198      1.1  jmcneill 	sunxi_sramc_init(sc);
    199      1.1  jmcneill 
    200      1.1  jmcneill 	KASSERT(sramc_softc == NULL);
    201      1.1  jmcneill 	sramc_softc = sc;
    202  1.1.4.1  pgoyette 
    203  1.1.4.1  pgoyette 	sc->sc_syscon.priv = sc;
    204  1.1.4.1  pgoyette 	sc->sc_syscon.lock = sunxi_sramc_lock;
    205  1.1.4.1  pgoyette 	sc->sc_syscon.unlock = sunxi_sramc_unlock;
    206  1.1.4.1  pgoyette 	sc->sc_syscon.read_4 = sunxi_sramc_read_4;
    207  1.1.4.1  pgoyette 	sc->sc_syscon.write_4 = sunxi_sramc_write_4;
    208  1.1.4.1  pgoyette 	fdtbus_register_syscon(self, phandle, &sc->sc_syscon);
    209      1.1  jmcneill }
    210      1.1  jmcneill 
    211      1.1  jmcneill CFATTACH_DECL_NEW(sunxi_sramc, sizeof(struct sunxi_sramc_softc),
    212      1.1  jmcneill 	sunxi_sramc_match, sunxi_sramc_attach, NULL, NULL);
    213      1.1  jmcneill 
    214      1.1  jmcneill static int
    215      1.1  jmcneill sunxi_sramc_map(const int node_phandle, u_int config)
    216      1.1  jmcneill {
    217      1.1  jmcneill 	struct sunxi_sramc_softc * const sc = sramc_softc;
    218      1.1  jmcneill 	struct sunxi_sramc_node *node;
    219      1.1  jmcneill 	uint32_t val;
    220      1.1  jmcneill 
    221      1.1  jmcneill 	if (sc == NULL)
    222      1.1  jmcneill 		return ENXIO;
    223      1.1  jmcneill 
    224      1.1  jmcneill 	TAILQ_FOREACH(node, &sc->sc_nodes, nodes)
    225      1.1  jmcneill 		if (node->phandle == node_phandle) {
    226      1.1  jmcneill 			if (config > __SHIFTOUT_MASK(node->area->mask))
    227      1.1  jmcneill 				return ERANGE;
    228  1.1.4.1  pgoyette 			if ((node->area->flags & SUNXI_SRAMC_F_SWAP) != 0)
    229  1.1.4.1  pgoyette 				config = !config;
    230      1.1  jmcneill 			val = SRAMC_READ(sc, node->area->reg);
    231      1.1  jmcneill 			val &= ~node->area->mask;
    232      1.1  jmcneill 			val |= __SHIFTIN(config, node->area->mask);
    233      1.1  jmcneill 			SRAMC_WRITE(sc, node->area->reg, val);
    234      1.1  jmcneill 			return 0;
    235      1.1  jmcneill 		}
    236      1.1  jmcneill 
    237      1.1  jmcneill 	return EINVAL;
    238      1.1  jmcneill }
    239      1.1  jmcneill 
    240      1.1  jmcneill int
    241      1.1  jmcneill sunxi_sramc_claim(const int phandle)
    242      1.1  jmcneill {
    243      1.1  jmcneill 	const u_int *data;
    244      1.1  jmcneill 	int len;
    245      1.1  jmcneill 
    246      1.1  jmcneill 	data = fdtbus_get_prop(phandle, "allwinner,sram", &len);
    247      1.1  jmcneill 	if (data == NULL)
    248      1.1  jmcneill 		return ENOENT;
    249      1.1  jmcneill 	if (len != 8)
    250      1.1  jmcneill 		return EIO;
    251      1.1  jmcneill 
    252      1.1  jmcneill 	const int node_phandle = fdtbus_get_phandle_from_native(be32toh(data[0]));
    253      1.1  jmcneill 	const u_int config = be32toh(data[1]);
    254      1.1  jmcneill 
    255      1.1  jmcneill 	return sunxi_sramc_map(node_phandle, config);
    256      1.1  jmcneill }
    257