Home | History | Annotate | Line # | Download | only in rockchip
rk3328_iomux.c revision 1.2.6.1
      1  1.2.6.1    martin /* $NetBSD: rk3328_iomux.c,v 1.2.6.1 2019/10/03 17:23:11 martin Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2018 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.2.6.1    martin __KERNEL_RCSID(0, "$NetBSD: rk3328_iomux.c,v 1.2.6.1 2019/10/03 17:23:11 martin 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/mutex.h>
     38      1.1  jmcneill #include <sys/kmem.h>
     39      1.1  jmcneill #include <sys/lwp.h>
     40      1.1  jmcneill 
     41      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     42      1.1  jmcneill #include <dev/fdt/syscon.h>
     43      1.1  jmcneill 
     44      1.1  jmcneill #define	GRF_GPIO_P_REG(_bank, _idx)	(0x0100 + (_bank) * 0x10 + ((_idx) >> 3) * 4)
     45      1.1  jmcneill #define	 GRF_GPIO_P_CTL(_idx)		(0x3 << (((_idx) & 7) * 2))
     46      1.1  jmcneill #define	  GRF_GPIO_P_CTL_Z		0
     47      1.1  jmcneill #define	  GRF_GPIO_P_CTL_PULLUP		1
     48      1.1  jmcneill #define	  GRF_GPIO_P_CTL_PULLDOWN	2
     49      1.1  jmcneill #define	  GRF_GPIO_P_CTL_REPEATER	3
     50      1.1  jmcneill #define	  GRF_GPIO_P_CTL_MASK		0x3
     51      1.1  jmcneill #define	 GRF_GPIO_P_WRITE_EN(_idx)	(0x3 << (((_idx) & 7) * 2 + 16))
     52      1.1  jmcneill 
     53      1.1  jmcneill #define	GRF_GPIO_E_REG(_bank, _idx)	(0x0200 + (_bank) * 0x10 + ((_idx) >> 3) * 4)
     54      1.1  jmcneill #define	 GRF_GPIO_E_CTL(_idx)		(0x3 << (((_idx) & 7) * 2))
     55      1.1  jmcneill #define	  GRF_GPIO_E_CTL_2MA		0
     56      1.1  jmcneill #define	  GRF_GPIO_E_CTL_4MA		1
     57      1.1  jmcneill #define	  GRF_GPIO_E_CTL_8MA		2
     58      1.1  jmcneill #define	  GRF_GPIO_E_CTL_12MA		3
     59      1.1  jmcneill #define	  GRF_GPIO_E_CTL_MASK		0x3
     60      1.1  jmcneill #define	 GRF_GPIO_E_WRITE_EN(_idx)	(0x3 << (((_idx) & 7) * 2 + 16))
     61      1.1  jmcneill 
     62      1.1  jmcneill struct rk3328_iomux {
     63      1.1  jmcneill 	bus_size_t		base;
     64      1.1  jmcneill 	u_int			type;
     65      1.1  jmcneill #define	RK3328_IOMUX_TYPE_3BIT	0x01
     66      1.1  jmcneill };
     67      1.1  jmcneill 
     68      1.1  jmcneill struct rk3328_iomux_bank {
     69      1.1  jmcneill 	struct rk3328_iomux		iomux[4];
     70      1.1  jmcneill };
     71      1.1  jmcneill 
     72      1.1  jmcneill static const struct rk3328_iomux_bank rk3328_iomux_banks[] = {
     73      1.1  jmcneill 	[0] = {
     74      1.1  jmcneill 		.iomux = {
     75      1.1  jmcneill 			[0] = { .base = 0x0000 },
     76      1.1  jmcneill 			[1] = { .base = 0x0004 },
     77      1.1  jmcneill 			[2] = { .base = 0x0008 },
     78      1.1  jmcneill 			[3] = { .base = 0x000c },
     79      1.1  jmcneill 		},
     80      1.1  jmcneill 	},
     81      1.1  jmcneill 	[1] = {
     82      1.1  jmcneill 		.iomux = {
     83      1.1  jmcneill 			[0] = { .base = 0x0010 },
     84      1.1  jmcneill 			[1] = { .base = 0x0014 },
     85      1.1  jmcneill 			[2] = { .base = 0x0018 },
     86      1.1  jmcneill 			[3] = { .base = 0x001c },
     87      1.1  jmcneill 		}
     88      1.1  jmcneill 	},
     89      1.1  jmcneill 	[2] = {
     90      1.1  jmcneill 		.iomux = {
     91      1.1  jmcneill 			[0] = { .base = 0x0020 },
     92      1.1  jmcneill 			[1] = { .base = 0x0024, .type = RK3328_IOMUX_TYPE_3BIT },
     93      1.1  jmcneill 			[2] = { .base = 0x002c, .type = RK3328_IOMUX_TYPE_3BIT },
     94      1.1  jmcneill 			[3] = { .base = 0x0034 },
     95      1.1  jmcneill 		},
     96      1.1  jmcneill 	},
     97      1.1  jmcneill 	[3] = {
     98      1.1  jmcneill 		.iomux = {
     99      1.1  jmcneill 			[0] = { .base = 0x0038, .type = RK3328_IOMUX_TYPE_3BIT },
    100      1.1  jmcneill 			[1] = { .base = 0x0040, .type = RK3328_IOMUX_TYPE_3BIT },
    101      1.1  jmcneill 			[2] = { .base = 0x0048 },
    102      1.1  jmcneill 			[3] = { .base = 0x004c },
    103      1.1  jmcneill 		},
    104      1.1  jmcneill 	},
    105      1.1  jmcneill };
    106      1.1  jmcneill 
    107      1.1  jmcneill struct rk3328_iomux_conf {
    108      1.1  jmcneill 	const struct rk3328_iomux_bank *banks;
    109      1.1  jmcneill 	u_int nbanks;
    110      1.1  jmcneill };
    111      1.1  jmcneill 
    112      1.1  jmcneill static const struct rk3328_iomux_conf rk3328_iomux_conf = {
    113      1.1  jmcneill 	.banks = rk3328_iomux_banks,
    114      1.1  jmcneill 	.nbanks = __arraycount(rk3328_iomux_banks),
    115      1.1  jmcneill };
    116      1.1  jmcneill 
    117      1.1  jmcneill static const struct of_compat_data compat_data[] = {
    118      1.1  jmcneill 	{ "rockchip,rk3328-pinctrl",	(uintptr_t)&rk3328_iomux_conf },
    119      1.1  jmcneill 	{ NULL }
    120      1.1  jmcneill };
    121      1.1  jmcneill 
    122      1.1  jmcneill struct rk3328_iomux_softc {
    123      1.1  jmcneill 	device_t sc_dev;
    124      1.1  jmcneill 	struct syscon *sc_syscon;
    125      1.1  jmcneill 
    126      1.1  jmcneill 	const struct rk3328_iomux_conf *sc_conf;
    127      1.1  jmcneill };
    128      1.1  jmcneill 
    129      1.1  jmcneill #define	LOCK(sc)		\
    130      1.1  jmcneill 	syscon_lock((sc)->sc_syscon)
    131      1.1  jmcneill #define	UNLOCK(sc)		\
    132      1.1  jmcneill 	syscon_unlock((sc)->sc_syscon)
    133      1.1  jmcneill #define	RD4(sc, reg) 		\
    134      1.1  jmcneill 	syscon_read_4((sc)->sc_syscon, (reg))
    135      1.1  jmcneill #define	WR4(sc, reg, val) 	\
    136      1.1  jmcneill 	syscon_write_4((sc)->sc_syscon, (reg), (val))
    137      1.1  jmcneill 
    138      1.1  jmcneill static int	rk3328_iomux_match(device_t, cfdata_t, void *);
    139      1.1  jmcneill static void	rk3328_iomux_attach(device_t, device_t, void *);
    140      1.1  jmcneill 
    141      1.1  jmcneill CFATTACH_DECL_NEW(rk3328_iomux, sizeof(struct rk3328_iomux_softc),
    142      1.1  jmcneill 	rk3328_iomux_match, rk3328_iomux_attach, NULL, NULL);
    143      1.1  jmcneill 
    144      1.1  jmcneill static void
    145      1.1  jmcneill rk3328_iomux_calc_iomux_reg(struct rk3328_iomux_softc *sc, u_int bank, u_int pin, bus_size_t *reg, uint32_t *mask)
    146      1.1  jmcneill {
    147      1.1  jmcneill 	const struct rk3328_iomux_bank *banks = sc->sc_conf->banks;
    148      1.1  jmcneill 
    149      1.1  jmcneill 	KASSERT(bank < sc->sc_conf->nbanks);
    150      1.1  jmcneill 
    151      1.1  jmcneill 	*reg = banks[bank].iomux[pin / 8].base;
    152      1.1  jmcneill 	if (banks[bank].iomux[pin / 8].type & RK3328_IOMUX_TYPE_3BIT) {
    153      1.1  jmcneill 		if ((pin % 8) >= 5)
    154      1.1  jmcneill 			*reg += 0x04;
    155      1.1  jmcneill 		const u_int bit = (pin % 8 % 5) * 3;
    156      1.1  jmcneill 		*mask = 7 << bit;
    157      1.1  jmcneill 	} else {
    158      1.1  jmcneill 		const u_int bit = (pin % 8) * 2;
    159      1.1  jmcneill 		*mask = 3 << bit;
    160      1.1  jmcneill 	}
    161      1.1  jmcneill }
    162      1.1  jmcneill 
    163      1.1  jmcneill static void
    164      1.1  jmcneill rk3328_iomux_set_bias(struct rk3328_iomux_softc *sc, u_int bank, u_int idx, u_int bias)
    165      1.1  jmcneill {
    166      1.1  jmcneill 	WR4(sc, GRF_GPIO_P_REG(bank, idx),
    167      1.1  jmcneill 	    __SHIFTIN(GRF_GPIO_P_CTL_MASK, GRF_GPIO_P_WRITE_EN(idx)) |
    168      1.1  jmcneill 	    __SHIFTIN(bias, GRF_GPIO_P_CTL(idx)));
    169      1.1  jmcneill }
    170      1.1  jmcneill 
    171      1.1  jmcneill static void
    172      1.1  jmcneill rk3328_iomux_set_drive_strength(struct rk3328_iomux_softc *sc, u_int bank, u_int idx, u_int drv)
    173      1.1  jmcneill {
    174      1.1  jmcneill 	WR4(sc, GRF_GPIO_E_REG(bank, idx),
    175      1.1  jmcneill 	    __SHIFTIN(GRF_GPIO_E_CTL_MASK, GRF_GPIO_E_WRITE_EN(idx)) |
    176      1.1  jmcneill 	    __SHIFTIN(drv, GRF_GPIO_E_CTL(idx)));
    177      1.1  jmcneill }
    178      1.1  jmcneill 
    179      1.1  jmcneill static void
    180      1.1  jmcneill rk3328_iomux_set_mux(struct rk3328_iomux_softc *sc, u_int bank, u_int idx, u_int mux)
    181      1.1  jmcneill {
    182      1.1  jmcneill 	bus_size_t reg;
    183      1.1  jmcneill 	uint32_t mask;
    184      1.1  jmcneill 
    185      1.1  jmcneill 	rk3328_iomux_calc_iomux_reg(sc, bank, idx, &reg, &mask);
    186      1.1  jmcneill 
    187      1.1  jmcneill 	WR4(sc, reg, (mask << 16) | __SHIFTIN(mux, mask));
    188      1.1  jmcneill }
    189      1.1  jmcneill 
    190      1.1  jmcneill static int
    191      1.1  jmcneill rk3328_iomux_config(struct rk3328_iomux_softc *sc, const int phandle, u_int bank, u_int idx, u_int mux)
    192      1.1  jmcneill {
    193      1.1  jmcneill 
    194      1.2   thorpej 	const int bias = fdtbus_pinctrl_parse_bias(phandle, NULL);
    195      1.2   thorpej 	switch (bias) {
    196      1.2   thorpej 	case 0:
    197      1.1  jmcneill 		rk3328_iomux_set_bias(sc, bank, idx, GRF_GPIO_P_CTL_Z);
    198      1.2   thorpej 		break;
    199      1.2   thorpej 	case GPIO_PIN_PULLUP:
    200      1.1  jmcneill 		rk3328_iomux_set_bias(sc, bank, idx, GRF_GPIO_P_CTL_PULLUP);
    201      1.2   thorpej 		break;
    202      1.2   thorpej 	case GPIO_PIN_PULLDOWN:
    203      1.1  jmcneill 		rk3328_iomux_set_bias(sc, bank, idx, GRF_GPIO_P_CTL_PULLDOWN);
    204      1.2   thorpej 		break;
    205      1.2   thorpej 	}
    206      1.1  jmcneill 
    207      1.2   thorpej 	const int drv = fdtbus_pinctrl_parse_drive_strength(phandle);
    208      1.2   thorpej 	switch (drv) {
    209      1.2   thorpej 	case -1:
    210      1.2   thorpej 		break;
    211      1.2   thorpej 	case 2:
    212      1.2   thorpej 		rk3328_iomux_set_drive_strength(sc, bank, idx, GRF_GPIO_E_CTL_2MA);
    213      1.2   thorpej 		break;
    214      1.2   thorpej 	case 4:
    215      1.2   thorpej 		rk3328_iomux_set_drive_strength(sc, bank, idx, GRF_GPIO_E_CTL_4MA);
    216      1.2   thorpej 		break;
    217      1.2   thorpej 	case 8:
    218      1.2   thorpej 		rk3328_iomux_set_drive_strength(sc, bank, idx, GRF_GPIO_E_CTL_8MA);
    219      1.2   thorpej 		break;
    220      1.2   thorpej 	case 12:
    221      1.2   thorpej 		rk3328_iomux_set_drive_strength(sc, bank, idx, GRF_GPIO_E_CTL_12MA);
    222      1.2   thorpej 		break;
    223      1.2   thorpej 	default:
    224      1.2   thorpej 		aprint_error_dev(sc->sc_dev, "unsupported drive-strength %u\n", drv);
    225      1.2   thorpej 		return EINVAL;
    226      1.1  jmcneill 	}
    227      1.1  jmcneill 
    228      1.1  jmcneill #if notyet
    229      1.2   thorpej 	int output_value;
    230      1.2   thorpej 	const int direction =
    231      1.2   thorpej 	    fdtbus_pinctrl_parse_input_output(phandle, &output_value);
    232      1.2   thorpej 	if (direction != -1) {
    233      1.2   thorpej 		rk3328_iomux_set_direction(sc, bank, idx, direction,
    234      1.2   thorpej 		    output_value);
    235      1.2   thorpej 	}
    236      1.1  jmcneill #endif
    237      1.1  jmcneill 
    238      1.1  jmcneill 	rk3328_iomux_set_mux(sc, bank, idx, mux);
    239      1.1  jmcneill 
    240      1.1  jmcneill 	return 0;
    241      1.1  jmcneill }
    242      1.1  jmcneill 
    243      1.1  jmcneill static int
    244      1.1  jmcneill rk3328_iomux_pinctrl_set_config(device_t dev, const void *data, size_t len)
    245      1.1  jmcneill {
    246      1.1  jmcneill 	struct rk3328_iomux_softc * const sc = device_private(dev);
    247      1.1  jmcneill 	int pins_len;
    248      1.1  jmcneill 
    249      1.1  jmcneill 	if (len != 4)
    250      1.1  jmcneill 		return -1;
    251      1.1  jmcneill 
    252      1.1  jmcneill 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
    253      1.1  jmcneill 	const u_int *pins = fdtbus_get_prop(phandle, "rockchip,pins", &pins_len);
    254      1.1  jmcneill 
    255      1.1  jmcneill 	while (pins_len >= 16) {
    256      1.1  jmcneill 		const u_int bank = be32toh(pins[0]);
    257      1.1  jmcneill 		const u_int idx = be32toh(pins[1]);
    258      1.1  jmcneill 		const u_int mux = be32toh(pins[2]);
    259      1.1  jmcneill 		const int cfg = fdtbus_get_phandle_from_native(be32toh(pins[3]));
    260      1.1  jmcneill 
    261      1.1  jmcneill 		LOCK(sc);
    262      1.1  jmcneill 		rk3328_iomux_config(sc, cfg, bank, idx, mux);
    263      1.1  jmcneill 		UNLOCK(sc);
    264      1.1  jmcneill 
    265      1.1  jmcneill 		pins_len -= 16;
    266      1.1  jmcneill 		pins += 4;
    267      1.1  jmcneill 	}
    268      1.1  jmcneill 
    269      1.1  jmcneill 	return 0;
    270      1.1  jmcneill }
    271      1.1  jmcneill 
    272      1.1  jmcneill static struct fdtbus_pinctrl_controller_func rk3328_iomux_pinctrl_funcs = {
    273      1.1  jmcneill 	.set_config = rk3328_iomux_pinctrl_set_config,
    274      1.1  jmcneill };
    275      1.1  jmcneill 
    276      1.1  jmcneill static int
    277      1.1  jmcneill rk3328_iomux_match(device_t parent, cfdata_t cf, void *aux)
    278      1.1  jmcneill {
    279      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    280      1.1  jmcneill 
    281      1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    282      1.1  jmcneill }
    283      1.1  jmcneill 
    284      1.1  jmcneill static void
    285      1.1  jmcneill rk3328_iomux_attach(device_t parent, device_t self, void *aux)
    286      1.1  jmcneill {
    287      1.1  jmcneill 	struct rk3328_iomux_softc * const sc = device_private(self);
    288      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    289      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    290      1.1  jmcneill 	int child, sub;
    291      1.1  jmcneill 
    292      1.1  jmcneill 	sc->sc_dev = self;
    293      1.1  jmcneill 	sc->sc_syscon = fdtbus_syscon_acquire(phandle, "rockchip,grf");
    294      1.1  jmcneill 	if (sc->sc_syscon == NULL) {
    295      1.1  jmcneill 		aprint_error(": couldn't acquire grf syscon\n");
    296      1.1  jmcneill 		return;
    297      1.1  jmcneill 	}
    298      1.1  jmcneill 	sc->sc_conf = (void *)of_search_compatible(phandle, compat_data)->data;
    299      1.1  jmcneill 
    300      1.1  jmcneill 	aprint_naive("\n");
    301      1.1  jmcneill 	aprint_normal(": RK3328 IOMUX control\n");
    302      1.1  jmcneill 
    303      1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    304      1.1  jmcneill 		for (sub = OF_child(child); sub; sub = OF_peer(sub)) {
    305      1.1  jmcneill 			if (!of_hasprop(sub, "rockchip,pins"))
    306      1.1  jmcneill 				continue;
    307      1.1  jmcneill 			fdtbus_register_pinctrl_config(self, sub, &rk3328_iomux_pinctrl_funcs);
    308      1.1  jmcneill 		}
    309      1.1  jmcneill 	}
    310      1.1  jmcneill 
    311      1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    312      1.1  jmcneill 		struct fdt_attach_args cfaa = *faa;
    313      1.1  jmcneill 		cfaa.faa_phandle = child;
    314      1.1  jmcneill 		cfaa.faa_name = fdtbus_get_string(child, "name");
    315      1.1  jmcneill 		cfaa.faa_quiet = false;
    316      1.1  jmcneill 
    317      1.1  jmcneill 		config_found(self, &cfaa, NULL);
    318      1.1  jmcneill 	}
    319      1.1  jmcneill }
    320