Home | History | Annotate | Line # | Download | only in nvidia
tegra210_xusbpad.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: tegra210_xusbpad.c,v 1.1 2017/09/19 23:18:01 jmcneill 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  jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra210_xusbpad.c,v 1.1 2017/09/19 23:18:01 jmcneill 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/kernel.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <arm/nvidia/tegra_reg.h>
     40  1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     41  1.1  jmcneill #include <arm/nvidia/tegra_xusbpad.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill struct tegra210_xusbpad_softc {
     46  1.1  jmcneill 	device_t		sc_dev;
     47  1.1  jmcneill 	int			sc_phandle;
     48  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     49  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     50  1.1  jmcneill 
     51  1.1  jmcneill 	struct fdtbus_reset	*sc_rst;
     52  1.1  jmcneill };
     53  1.1  jmcneill 
     54  1.1  jmcneill #define	RD4(sc, reg)					\
     55  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     56  1.1  jmcneill #define	WR4(sc, reg, val)				\
     57  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     58  1.1  jmcneill #define	SETCLR4(sc, reg, set, clr)			\
     59  1.1  jmcneill 	tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))
     60  1.1  jmcneill 
     61  1.1  jmcneill static const char * tegra210_xusbpad_usb2_func[] = { "snps", "xusb", "uart" };
     62  1.1  jmcneill static const char * tegra210_xusbpad_hsic_func[] = { "snps", "xusb" };
     63  1.1  jmcneill static const char * tegra210_xusbpad_pcie_func[] = { "pcie-x1", "usb3-ss", "sata", "pcie-x4" };
     64  1.1  jmcneill 
     65  1.1  jmcneill #define	XUSBPAD_LANE(n, r, m, f)		\
     66  1.1  jmcneill 	{					\
     67  1.1  jmcneill 		.name = (n),			\
     68  1.1  jmcneill 		.reg = (r),			\
     69  1.1  jmcneill 		.mask = (m),			\
     70  1.1  jmcneill 		.funcs = (f),			\
     71  1.1  jmcneill 		.nfuncs = __arraycount(f)	\
     72  1.1  jmcneill 	}
     73  1.1  jmcneill 
     74  1.1  jmcneill static const struct tegra210_xusbpad_lane {
     75  1.1  jmcneill 	const char		*name;
     76  1.1  jmcneill 	bus_size_t		reg;
     77  1.1  jmcneill 	uint32_t		mask;
     78  1.1  jmcneill 	const char		**funcs;
     79  1.1  jmcneill 	int			nfuncs;
     80  1.1  jmcneill } tegra210_xusbpad_lanes[] = {
     81  1.1  jmcneill 	XUSBPAD_LANE("usb2-0", 0x04, __BITS(1,0), tegra210_xusbpad_usb2_func),
     82  1.1  jmcneill 	XUSBPAD_LANE("usb2-1", 0x04, __BITS(3,2), tegra210_xusbpad_usb2_func),
     83  1.1  jmcneill 	XUSBPAD_LANE("usb2-2", 0x04, __BITS(5,4), tegra210_xusbpad_usb2_func),
     84  1.1  jmcneill 	XUSBPAD_LANE("usb2-3", 0x04, __BITS(7,6), tegra210_xusbpad_usb2_func),
     85  1.1  jmcneill 
     86  1.1  jmcneill 	XUSBPAD_LANE("hsic-0", 0x04, __BIT(14), tegra210_xusbpad_hsic_func),
     87  1.1  jmcneill 	XUSBPAD_LANE("hsic-1", 0x04, __BIT(15), tegra210_xusbpad_hsic_func),
     88  1.1  jmcneill 
     89  1.1  jmcneill 	XUSBPAD_LANE("pcie-0", 0x28, __BITS(13,12), tegra210_xusbpad_pcie_func),
     90  1.1  jmcneill 	XUSBPAD_LANE("pcie-1", 0x28, __BITS(15,14), tegra210_xusbpad_pcie_func),
     91  1.1  jmcneill 	XUSBPAD_LANE("pcie-2", 0x28, __BITS(17,16), tegra210_xusbpad_pcie_func),
     92  1.1  jmcneill 	XUSBPAD_LANE("pcie-3", 0x28, __BITS(19,18), tegra210_xusbpad_pcie_func),
     93  1.1  jmcneill 	XUSBPAD_LANE("pcie-4", 0x28, __BITS(21,20), tegra210_xusbpad_pcie_func),
     94  1.1  jmcneill 	XUSBPAD_LANE("pcie-5", 0x28, __BITS(23,22), tegra210_xusbpad_pcie_func),
     95  1.1  jmcneill 	XUSBPAD_LANE("pcie-6", 0x28, __BITS(25,24), tegra210_xusbpad_pcie_func),
     96  1.1  jmcneill 
     97  1.1  jmcneill 	XUSBPAD_LANE("sata-0", 0x28, __BITS(31,30), tegra210_xusbpad_pcie_func),
     98  1.1  jmcneill };
     99  1.1  jmcneill 
    100  1.1  jmcneill static int
    101  1.1  jmcneill tegra210_xusbpad_find_func(const struct tegra210_xusbpad_lane *lane,
    102  1.1  jmcneill     const char *func)
    103  1.1  jmcneill {
    104  1.1  jmcneill 	for (int n = 0; n < lane->nfuncs; n++)
    105  1.1  jmcneill 		if (strcmp(lane->funcs[n], func) == 0)
    106  1.1  jmcneill 			return n;
    107  1.1  jmcneill 	return -1;
    108  1.1  jmcneill }
    109  1.1  jmcneill 
    110  1.1  jmcneill static const struct tegra210_xusbpad_lane *
    111  1.1  jmcneill tegra210_xusbpad_find_lane(const char *name)
    112  1.1  jmcneill {
    113  1.1  jmcneill 	for (int n = 0; n < __arraycount(tegra210_xusbpad_lanes); n++)
    114  1.1  jmcneill 		if (strcmp(tegra210_xusbpad_lanes[n].name, name) == 0)
    115  1.1  jmcneill 			return &tegra210_xusbpad_lanes[n];
    116  1.1  jmcneill 	return NULL;
    117  1.1  jmcneill }
    118  1.1  jmcneill 
    119  1.1  jmcneill static void
    120  1.1  jmcneill tegra210_xusbpad_configure_lane(struct tegra210_xusbpad_softc *sc,
    121  1.1  jmcneill     int phandle)
    122  1.1  jmcneill {
    123  1.1  jmcneill 	const struct tegra210_xusbpad_lane *lane;
    124  1.1  jmcneill 	const char *name, *function;
    125  1.1  jmcneill 	int func;
    126  1.1  jmcneill 
    127  1.1  jmcneill 	name = fdtbus_get_string(phandle, "name");
    128  1.1  jmcneill 	if (name == NULL) {
    129  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no 'name' property\n");
    130  1.1  jmcneill 		return;
    131  1.1  jmcneill 	}
    132  1.1  jmcneill 	function = fdtbus_get_string(phandle, "nvidia,function");
    133  1.1  jmcneill 	if (function == NULL) {
    134  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no 'nvidia,function' property\n");
    135  1.1  jmcneill 		return;
    136  1.1  jmcneill 	}
    137  1.1  jmcneill 
    138  1.1  jmcneill 	lane = tegra210_xusbpad_find_lane(name);
    139  1.1  jmcneill 	if (lane == NULL) {
    140  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "unsupported lane '%s'\n", name);
    141  1.1  jmcneill 		return;
    142  1.1  jmcneill 	}
    143  1.1  jmcneill 	func = tegra210_xusbpad_find_func(lane, function);
    144  1.1  jmcneill 	if (func == -1) {
    145  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "unsupported function '%s'\n", function);
    146  1.1  jmcneill 		return;
    147  1.1  jmcneill 	}
    148  1.1  jmcneill 
    149  1.1  jmcneill 	aprint_debug_dev(sc->sc_dev, "[%s] set func %s\n", name, function);
    150  1.1  jmcneill 	SETCLR4(sc, lane->reg, __SHIFTIN(func, lane->mask), lane->mask);
    151  1.1  jmcneill }
    152  1.1  jmcneill 
    153  1.1  jmcneill static void
    154  1.1  jmcneill tegra210_xusbpad_configure_pads(struct tegra210_xusbpad_softc *sc,
    155  1.1  jmcneill     const char *name)
    156  1.1  jmcneill {
    157  1.1  jmcneill 	struct fdtbus_reset *rst;
    158  1.1  jmcneill 	struct clk *clk;
    159  1.1  jmcneill 	int phandle, child;
    160  1.1  jmcneill 
    161  1.1  jmcneill 	/* Search for the pad's node */
    162  1.1  jmcneill 	phandle = of_find_firstchild_byname(sc->sc_phandle, "pads");
    163  1.1  jmcneill 	if (phandle == -1) {
    164  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no 'pads' node\n");
    165  1.1  jmcneill 		return;
    166  1.1  jmcneill 	}
    167  1.1  jmcneill 	phandle = of_find_firstchild_byname(phandle, name);
    168  1.1  jmcneill 	if (phandle == -1) {
    169  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no 'pads/%s' node\n", name);
    170  1.1  jmcneill 		return;
    171  1.1  jmcneill 	}
    172  1.1  jmcneill 
    173  1.1  jmcneill 	if (!fdtbus_status_okay(phandle))
    174  1.1  jmcneill 		return;		/* pad is disabled */
    175  1.1  jmcneill 
    176  1.1  jmcneill 	/* Enable the pad's resources */
    177  1.1  jmcneill 	clk = fdtbus_clock_get_index(phandle, 0);
    178  1.1  jmcneill 	if (clk && clk_enable(clk) != 0) {
    179  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't enable %s's clock\n", name);
    180  1.1  jmcneill 		return;
    181  1.1  jmcneill 	}
    182  1.1  jmcneill 	rst = fdtbus_reset_get_index(phandle, 0);
    183  1.1  jmcneill 	if (rst && fdtbus_reset_deassert(rst) != 0) {
    184  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't de-assert %s's reset\n", name);
    185  1.1  jmcneill 		return;
    186  1.1  jmcneill 	}
    187  1.1  jmcneill 
    188  1.1  jmcneill 	/* Configure lanes */
    189  1.1  jmcneill 	phandle = of_find_firstchild_byname(phandle, "lanes");
    190  1.1  jmcneill 	if (phandle == -1) {
    191  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no 'pads/%s/lanes' node\n", name);
    192  1.1  jmcneill 		return;
    193  1.1  jmcneill 	}
    194  1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    195  1.1  jmcneill 		if (!fdtbus_status_okay(child))
    196  1.1  jmcneill 			continue;
    197  1.1  jmcneill 		tegra210_xusbpad_configure_lane(sc, child);
    198  1.1  jmcneill 	}
    199  1.1  jmcneill }
    200  1.1  jmcneill 
    201  1.1  jmcneill static void
    202  1.1  jmcneill tegra210_xusbpad_sata_enable(device_t dev)
    203  1.1  jmcneill {
    204  1.1  jmcneill }
    205  1.1  jmcneill 
    206  1.1  jmcneill static void
    207  1.1  jmcneill tegra210_xusbpad_xhci_enable(device_t dev)
    208  1.1  jmcneill {
    209  1.1  jmcneill }
    210  1.1  jmcneill 
    211  1.1  jmcneill static const struct tegra_xusbpad_ops tegra210_xusbpad_ops = {
    212  1.1  jmcneill 	.sata_enable = tegra210_xusbpad_sata_enable,
    213  1.1  jmcneill 	.xhci_enable = tegra210_xusbpad_xhci_enable,
    214  1.1  jmcneill };
    215  1.1  jmcneill 
    216  1.1  jmcneill static int
    217  1.1  jmcneill tegra210_xusbpad_match(device_t parent, cfdata_t cf, void *aux)
    218  1.1  jmcneill {
    219  1.1  jmcneill 	const char * const compatible[] = {
    220  1.1  jmcneill 		"nvidia,tegra210-xusb-padctl",
    221  1.1  jmcneill 		NULL
    222  1.1  jmcneill 	};
    223  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    224  1.1  jmcneill 
    225  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    226  1.1  jmcneill }
    227  1.1  jmcneill 
    228  1.1  jmcneill static void
    229  1.1  jmcneill tegra210_xusbpad_attach(device_t parent, device_t self, void *aux)
    230  1.1  jmcneill {
    231  1.1  jmcneill 	struct tegra210_xusbpad_softc * const sc = device_private(self);
    232  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    233  1.1  jmcneill 	bus_addr_t addr;
    234  1.1  jmcneill 	bus_size_t size;
    235  1.1  jmcneill 	int error;
    236  1.1  jmcneill 
    237  1.1  jmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    238  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    239  1.1  jmcneill 		return;
    240  1.1  jmcneill 	}
    241  1.1  jmcneill 	sc->sc_rst = fdtbus_reset_get(faa->faa_phandle, "padctl");
    242  1.1  jmcneill 	if (sc->sc_rst == NULL) {
    243  1.1  jmcneill 		aprint_error(": couldn't get reset padctl\n");
    244  1.1  jmcneill 		return;
    245  1.1  jmcneill 	}
    246  1.1  jmcneill 
    247  1.1  jmcneill 	sc->sc_dev = self;
    248  1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    249  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    250  1.1  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    251  1.1  jmcneill 	if (error) {
    252  1.1  jmcneill 		aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
    253  1.1  jmcneill 		return;
    254  1.1  jmcneill 	}
    255  1.1  jmcneill 
    256  1.1  jmcneill 	aprint_naive("\n");
    257  1.1  jmcneill 	aprint_normal(": XUSB PADCTL\n");
    258  1.1  jmcneill 
    259  1.1  jmcneill 	fdtbus_reset_deassert(sc->sc_rst);
    260  1.1  jmcneill 
    261  1.1  jmcneill 	tegra_xusbpad_register(self, &tegra210_xusbpad_ops);
    262  1.1  jmcneill 
    263  1.1  jmcneill 	tegra210_xusbpad_configure_pads(sc, "usb2");
    264  1.1  jmcneill 	tegra210_xusbpad_configure_pads(sc, "hsic");
    265  1.1  jmcneill 	tegra210_xusbpad_configure_pads(sc, "pcie");
    266  1.1  jmcneill 	tegra210_xusbpad_configure_pads(sc, "sata");
    267  1.1  jmcneill }
    268  1.1  jmcneill 
    269  1.1  jmcneill CFATTACH_DECL_NEW(tegra210_xusbpad, sizeof(struct tegra210_xusbpad_softc),
    270  1.1  jmcneill 	tegra210_xusbpad_match, tegra210_xusbpad_attach, NULL, NULL);
    271