Home | History | Annotate | Line # | Download | only in nvidia
      1  1.9   thorpej /* $NetBSD: tegra_pinmux.c,v 1.9 2021/01/27 03:10:19 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015-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 "opt_tegra.h"
     30  1.1  jmcneill 
     31  1.1  jmcneill #include <sys/cdefs.h>
     32  1.9   thorpej __KERNEL_RCSID(0, "$NetBSD: tegra_pinmux.c,v 1.9 2021/01/27 03:10:19 thorpej Exp $");
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <sys/param.h>
     35  1.1  jmcneill #include <sys/types.h>
     36  1.1  jmcneill #include <sys/bus.h>
     37  1.1  jmcneill #include <sys/device.h>
     38  1.1  jmcneill #include <sys/intr.h>
     39  1.1  jmcneill #include <sys/systm.h>
     40  1.1  jmcneill #include <sys/kernel.h>
     41  1.1  jmcneill #include <sys/kmem.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <arm/nvidia/tegra_reg.h>
     44  1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     45  1.1  jmcneill #include <arm/nvidia/tegra_pinmux.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill /* PINMUX fields */
     50  1.1  jmcneill #define	PINMUX_DRV_TYPE		__BITS(14,13)
     51  1.1  jmcneill #define	PINMUX_E_SCHMT		__BIT(12)
     52  1.1  jmcneill #define	PINMUX_E_OD		__BIT(11)
     53  1.1  jmcneill #define	PINMUX_E_IO_HV		__BIT(10)
     54  1.1  jmcneill #define	PINMUX_E_HSM		__BIT(9)
     55  1.1  jmcneill #define	PINMUX_LOCK		__BIT(7)
     56  1.1  jmcneill #define	PINMUX_E_INPUT		__BIT(6)
     57  1.1  jmcneill #define	PINMUX_PARK		__BIT(5)
     58  1.1  jmcneill #define	PINMUX_TRISTATE		__BIT(4)
     59  1.1  jmcneill #define	PINMUX_PUPD		__BITS(3,2)
     60  1.1  jmcneill #define	PINMUX_PM		__BITS(1,0)
     61  1.1  jmcneill 
     62  1.1  jmcneill struct tegra_pinmux_softc {
     63  1.1  jmcneill 	device_t		sc_dev;
     64  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     65  1.1  jmcneill 	bus_space_handle_t	sc_bsh[2];
     66  1.1  jmcneill 	const struct tegra_pinmux_conf *sc_conf;
     67  1.1  jmcneill };
     68  1.1  jmcneill 
     69  1.1  jmcneill #define PADCTRL_WRITE(sc, reg, val) \
     70  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh[0], (reg), (val))
     71  1.1  jmcneill #define PADCTRL_READ(sc, reg) \
     72  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh[0], (reg))
     73  1.1  jmcneill #define PINMUX_WRITE(sc, reg, val) \
     74  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh[1], (reg), (val))
     75  1.1  jmcneill #define PINMUX_READ(sc, reg) \
     76  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh[1], (reg))
     77  1.1  jmcneill 
     78  1.7   thorpej static const struct device_compatible_entry compat_data[] = {
     79  1.1  jmcneill #ifdef SOC_TEGRA210
     80  1.7   thorpej 	{ .compat = "nvidia,tegra210-pinmux",	.data = &tegra210_pinmux_conf },
     81  1.1  jmcneill #endif
     82  1.9   thorpej 	DEVICE_COMPAT_EOL
     83  1.1  jmcneill };
     84  1.1  jmcneill 
     85  1.1  jmcneill static const struct tegra_pinmux_pins *
     86  1.1  jmcneill tegra_pinmux_lookup_byname(struct tegra_pinmux_softc *sc, const char *name)
     87  1.1  jmcneill {
     88  1.1  jmcneill 	const struct tegra_pinmux_pins *pin_def;
     89  1.1  jmcneill 	u_int n;
     90  1.1  jmcneill 
     91  1.1  jmcneill 	for (n = 0; n < sc->sc_conf->npins; n++) {
     92  1.1  jmcneill 		pin_def = &sc->sc_conf->pins[n];
     93  1.3     skrll 		if (strcmp(pin_def->tpp_name, name) == 0)
     94  1.1  jmcneill 			return pin_def;
     95  1.1  jmcneill 	}
     96  1.1  jmcneill 
     97  1.1  jmcneill 	return NULL;
     98  1.1  jmcneill }
     99  1.1  jmcneill 
    100  1.1  jmcneill static int
    101  1.1  jmcneill tegra_pinmux_lookup_func(const struct tegra_pinmux_pins *pin_def, const int phandle)
    102  1.1  jmcneill {
    103  1.1  jmcneill 	const char *func;
    104  1.1  jmcneill 	u_int n, valid;
    105  1.1  jmcneill 
    106  1.1  jmcneill 	func = fdtbus_get_string(phandle, "nvidia,function");
    107  1.1  jmcneill 	if (func == NULL)
    108  1.1  jmcneill 		return -1;
    109  1.1  jmcneill 
    110  1.1  jmcneill 	for (n = 0, valid = 0; n < TEGRA_PINMUX_MAXFUNC; n++) {
    111  1.3     skrll 		if (pin_def->tpp_functions[n] == NULL)
    112  1.1  jmcneill 			continue;
    113  1.1  jmcneill 		++valid;
    114  1.3     skrll 		if (strcmp(pin_def->tpp_functions[n], func) == 0)
    115  1.1  jmcneill 			return n;
    116  1.1  jmcneill 	}
    117  1.1  jmcneill 
    118  1.1  jmcneill 	if (valid > 0)
    119  1.1  jmcneill 		aprint_error("%s: pin %s does not support function %s\n",
    120  1.3     skrll 		    __func__, pin_def->tpp_name, func);
    121  1.1  jmcneill 
    122  1.1  jmcneill 	return -1;
    123  1.1  jmcneill }
    124  1.1  jmcneill 
    125  1.1  jmcneill static void
    126  1.1  jmcneill tegra_pinmux_pin_config(struct tegra_pinmux_softc *sc,
    127  1.1  jmcneill     const struct tegra_pinmux_pins *pin_def, const int phandle)
    128  1.1  jmcneill {
    129  1.1  jmcneill 	uint32_t cfg;
    130  1.1  jmcneill 	u_int val;
    131  1.1  jmcneill 
    132  1.3     skrll 	if (pin_def->tpp_type == TEGRA_PINMUX) {
    133  1.3     skrll 		cfg = PINMUX_READ(sc, pin_def->tpp_reg);
    134  1.3     skrll 		const uint32_t ocfg = cfg;
    135  1.3     skrll 
    136  1.3     skrll 		const int func = tegra_pinmux_lookup_func(pin_def, phandle);
    137  1.3     skrll 		if (func != -1) {
    138  1.3     skrll 			cfg &= ~PINMUX_PM;
    139  1.3     skrll 			cfg |= __SHIFTIN(func, PINMUX_PM);
    140  1.3     skrll 		}
    141  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,pull", &val) == 0) {
    142  1.3     skrll 			cfg &= ~PINMUX_PUPD;
    143  1.3     skrll 			cfg |= __SHIFTIN(val, PINMUX_PUPD);
    144  1.3     skrll 		}
    145  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,tristate", &val) == 0) {
    146  1.3     skrll 			cfg &= ~PINMUX_TRISTATE;
    147  1.3     skrll 			cfg |= __SHIFTIN(val, PINMUX_TRISTATE);
    148  1.3     skrll 		}
    149  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,open-drain", &val) == 0) {
    150  1.3     skrll 			cfg &= ~PINMUX_E_OD;
    151  1.3     skrll 			cfg |= __SHIFTIN(val, PINMUX_E_OD);
    152  1.3     skrll 		}
    153  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,lock", &val) == 0) {
    154  1.3     skrll 			cfg &= ~PINMUX_LOCK;
    155  1.3     skrll 			cfg |= __SHIFTIN(val, PINMUX_LOCK);
    156  1.3     skrll 		}
    157  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,io-hv", &val) == 0) {
    158  1.3     skrll 			cfg &= ~PINMUX_E_IO_HV;
    159  1.3     skrll 			cfg |= __SHIFTIN(val, PINMUX_E_IO_HV);
    160  1.3     skrll 		}
    161  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,high-speed-mode", &val) == 0) {
    162  1.3     skrll 			cfg &= ~PINMUX_E_HSM;
    163  1.3     skrll 			cfg |= __SHIFTIN(val, PINMUX_E_HSM);
    164  1.3     skrll 		}
    165  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,schmitt", &val) == 0) {
    166  1.3     skrll 			cfg &= ~PINMUX_E_SCHMT;
    167  1.3     skrll 			cfg |= __SHIFTIN(val, PINMUX_E_SCHMT);
    168  1.3     skrll 		}
    169  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,drive-type", &val) == 0) {
    170  1.3     skrll 			cfg &= ~PINMUX_DRV_TYPE;
    171  1.3     skrll 			cfg |= __SHIFTIN(val, PINMUX_DRV_TYPE);
    172  1.3     skrll 		}
    173  1.3     skrll 		aprint_debug_dev(sc->sc_dev, "pin %s %08x -> %08x\n",
    174  1.3     skrll 		    pin_def->tpp_name, ocfg, cfg);
    175  1.3     skrll 		if (cfg != ocfg)
    176  1.3     skrll 			PINMUX_WRITE(sc, pin_def->tpp_reg, cfg);
    177  1.3     skrll 
    178  1.3     skrll 	} else {
    179  1.3     skrll     		cfg = PADCTRL_READ(sc, pin_def->tpp_reg);
    180  1.3     skrll 		const uint32_t ocfg = cfg;
    181  1.3     skrll 
    182  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,pull-down-strength", &val) == 0) {
    183  1.3     skrll 			cfg &= ~pin_def->tpp_dg.drvdn_mask;
    184  1.3     skrll 			cfg |= __SHIFTIN(val, pin_def->tpp_dg.drvdn_mask);
    185  1.3     skrll 		}
    186  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,pull-up-strength", &val) == 0) {
    187  1.3     skrll 			cfg &= ~pin_def->tpp_dg.drvup_mask;
    188  1.3     skrll 			cfg |= __SHIFTIN(val, pin_def->tpp_dg.drvup_mask);
    189  1.3     skrll 		}
    190  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,slew-rate-falling", &val) == 0) {
    191  1.3     skrll 			cfg &= ~pin_def->tpp_dg.slwrf_mask;
    192  1.3     skrll 			cfg |= __SHIFTIN(val, pin_def->tpp_dg.slwrf_mask);
    193  1.3     skrll 		}
    194  1.3     skrll 		if (of_getprop_uint32(phandle, "nvidia,slew-rate-rising", &val) == 0) {
    195  1.3     skrll 			cfg &= ~pin_def->tpp_dg.slwrr_mask;
    196  1.3     skrll 			cfg |= __SHIFTIN(val, pin_def->tpp_dg.slwrr_mask);
    197  1.3     skrll 		}
    198  1.1  jmcneill 
    199  1.3     skrll 		aprint_debug_dev(sc->sc_dev, "pin %s %08x -> %08x\n",
    200  1.3     skrll 		    pin_def->tpp_name, ocfg, cfg);
    201  1.3     skrll 		if (cfg != ocfg)
    202  1.3     skrll 			PADCTRL_WRITE(sc, pin_def->tpp_reg, cfg);
    203  1.1  jmcneill 	}
    204  1.1  jmcneill 
    205  1.1  jmcneill }
    206  1.1  jmcneill 
    207  1.1  jmcneill static int
    208  1.1  jmcneill tegra_pinmux_set_config(device_t dev, const void *data, size_t len)
    209  1.1  jmcneill {
    210  1.1  jmcneill 	struct tegra_pinmux_softc * const sc = device_private(dev);
    211  1.1  jmcneill 	const struct tegra_pinmux_pins *pin_def;
    212  1.1  jmcneill 	int child;
    213  1.1  jmcneill 
    214  1.1  jmcneill 	if (len != 4)
    215  1.1  jmcneill 		return -1;
    216  1.1  jmcneill 
    217  1.1  jmcneill 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
    218  1.1  jmcneill 
    219  1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    220  1.1  jmcneill 		const char *pins = fdtbus_get_string(child, "nvidia,pins");
    221  1.1  jmcneill 		if (pins == NULL) {
    222  1.1  jmcneill 			aprint_error_dev(dev, "skipping %s (no nvidia,pins property)\n",
    223  1.1  jmcneill 			    fdtbus_get_string(child, "name"));
    224  1.1  jmcneill 			continue;
    225  1.1  jmcneill 		}
    226  1.1  jmcneill 		int pins_len = OF_getproplen(child, "nvidia,pins");
    227  1.1  jmcneill 
    228  1.1  jmcneill 		for (; pins_len > 0;
    229  1.1  jmcneill 		    pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
    230  1.1  jmcneill 			pin_def = tegra_pinmux_lookup_byname(sc, pins);
    231  1.1  jmcneill 			if (pin_def == NULL) {
    232  1.1  jmcneill 				aprint_error_dev(dev, "unknown pin name '%s'\n", pins);
    233  1.1  jmcneill 				continue;
    234  1.1  jmcneill 			}
    235  1.1  jmcneill 
    236  1.1  jmcneill 			tegra_pinmux_pin_config(sc, pin_def, child);
    237  1.1  jmcneill 		}
    238  1.1  jmcneill 	}
    239  1.1  jmcneill 
    240  1.1  jmcneill 	return 0;
    241  1.1  jmcneill }
    242  1.1  jmcneill 
    243  1.1  jmcneill static struct fdtbus_pinctrl_controller_func tegra_pinmux_funcs = {
    244  1.1  jmcneill 	.set_config = tegra_pinmux_set_config,
    245  1.1  jmcneill };
    246  1.1  jmcneill 
    247  1.1  jmcneill static int
    248  1.1  jmcneill tegra_pinmux_match(device_t parent, cfdata_t cf, void *aux)
    249  1.1  jmcneill {
    250  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    251  1.1  jmcneill 
    252  1.9   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    253  1.1  jmcneill }
    254  1.1  jmcneill 
    255  1.1  jmcneill static void
    256  1.1  jmcneill tegra_pinmux_attach(device_t parent, device_t self, void *aux)
    257  1.1  jmcneill {
    258  1.1  jmcneill 	struct tegra_pinmux_softc * const sc = device_private(self);
    259  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    260  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    261  1.1  jmcneill 	bus_addr_t addr;
    262  1.1  jmcneill 	bus_size_t size;
    263  1.1  jmcneill 	int error, res;
    264  1.1  jmcneill 	int child;
    265  1.1  jmcneill 
    266  1.1  jmcneill 	sc->sc_dev = self;
    267  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    268  1.1  jmcneill 	for (res = 0; res < __arraycount(sc->sc_bsh); res++) {
    269  1.1  jmcneill 		error = fdtbus_get_reg(phandle, res, &addr, &size);
    270  1.1  jmcneill 		if (error != 0) {
    271  1.1  jmcneill 			aprint_error(": couldn't get resource %d: %d\n", res, error);
    272  1.1  jmcneill 			return;
    273  1.1  jmcneill 		}
    274  1.1  jmcneill 		error = bus_space_map(sc->sc_bst, addr, size, res, &sc->sc_bsh[res]);
    275  1.1  jmcneill 		if (error) {
    276  1.6     skrll 			aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
    277  1.1  jmcneill 			return;
    278  1.1  jmcneill 		}
    279  1.1  jmcneill 	}
    280  1.9   thorpej 	sc->sc_conf = of_compatible_lookup(phandle, compat_data)->data;
    281  1.1  jmcneill 
    282  1.1  jmcneill 	aprint_naive("\n");
    283  1.1  jmcneill 	aprint_normal(": Pinmux\n");
    284  1.1  jmcneill 
    285  1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child))
    286  1.1  jmcneill 		fdtbus_register_pinctrl_config(self, child, &tegra_pinmux_funcs);
    287  1.1  jmcneill }
    288  1.1  jmcneill 
    289  1.1  jmcneill CFATTACH_DECL_NEW(tegra_pinmux, sizeof(struct tegra_pinmux_softc),
    290  1.1  jmcneill 	tegra_pinmux_match, tegra_pinmux_attach, NULL, NULL);
    291