Home | History | Annotate | Line # | Download | only in amlogic
      1  1.6   thorpej /* $NetBSD: meson_usbphy.c,v 1.6 2021/01/27 03:10:18 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2019 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.1  jmcneill 
     31  1.6   thorpej __KERNEL_RCSID(0, "$NetBSD: meson_usbphy.c,v 1.6 2021/01/27 03:10:18 thorpej Exp $");
     32  1.1  jmcneill 
     33  1.1  jmcneill #include <sys/param.h>
     34  1.1  jmcneill #include <sys/bus.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/intr.h>
     37  1.1  jmcneill #include <sys/systm.h>
     38  1.1  jmcneill #include <sys/time.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #define	CBUS_REG(x)			((x) << 2)
     43  1.1  jmcneill #define	PREI_USB_PHY_CFG_REG		CBUS_REG(0x00)
     44  1.1  jmcneill #define	 PREI_USB_PHY_CFG_CLK_32K_ALT_SEL	__BIT(15)
     45  1.1  jmcneill #define	PREI_USB_PHY_CTRL_REG		CBUS_REG(0x01)
     46  1.1  jmcneill #define	 PREI_USB_PHY_CTRL_FSEL		__BITS(24,22)
     47  1.1  jmcneill #define	 PREI_USB_PHY_CTRL_FSEL_24M	5
     48  1.1  jmcneill #define	 PREI_USB_PHY_CTRL_FSEL_12M	2
     49  1.1  jmcneill #define	 PREI_USB_PHY_CTRL_POR		__BIT(15)
     50  1.1  jmcneill #define	 PREI_USB_PHY_CTRL_CLK_DET	__BIT(8)
     51  1.1  jmcneill #define	PREI_USB_PHY_ADP_BC_REG		CBUS_REG(0x03)
     52  1.1  jmcneill #define	 PREI_USB_PHY_ADP_BC_ACA_FLOATING __BIT(26)
     53  1.1  jmcneill #define	 PREI_USB_PHY_ADP_BC_ACA_ENABLE	__BIT(16)
     54  1.1  jmcneill 
     55  1.1  jmcneill static int meson_usbphy_match(device_t, cfdata_t, void *);
     56  1.1  jmcneill static void meson_usbphy_attach(device_t, device_t, void *);
     57  1.1  jmcneill 
     58  1.1  jmcneill enum meson_usbphy_type {
     59  1.1  jmcneill 	USBPHY_MESON8B,
     60  1.1  jmcneill };
     61  1.1  jmcneill 
     62  1.3   thorpej static const struct device_compatible_entry compat_data[] = {
     63  1.3   thorpej 	{ .compat = "amlogic,meson8b-usb2-phy",
     64  1.3   thorpej 	  .value = USBPHY_MESON8B },
     65  1.3   thorpej 	{ .compat = "amlogic,meson-gxbb-usb2-phy",
     66  1.3   thorpej 	  .value = USBPHY_MESON8B },
     67  1.3   thorpej 
     68  1.5   thorpej 	DEVICE_COMPAT_EOL
     69  1.1  jmcneill };
     70  1.1  jmcneill 
     71  1.1  jmcneill struct meson_usbphy_softc {
     72  1.1  jmcneill 	device_t		sc_dev;
     73  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     74  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     75  1.1  jmcneill 	int			sc_phandle;
     76  1.1  jmcneill 	const char		*sc_dr_mode;
     77  1.1  jmcneill 	enum meson_usbphy_type	sc_type;
     78  1.1  jmcneill };
     79  1.1  jmcneill 
     80  1.1  jmcneill #define	PHY_READ(sc, reg)				\
     81  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     82  1.1  jmcneill #define	PHY_WRITE(sc, reg, val)			\
     83  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     84  1.1  jmcneill 
     85  1.1  jmcneill CFATTACH_DECL_NEW(meson_usbphy, sizeof(struct meson_usbphy_softc),
     86  1.1  jmcneill 	meson_usbphy_match, meson_usbphy_attach, NULL, NULL);
     87  1.1  jmcneill 
     88  1.1  jmcneill static const char *
     89  1.1  jmcneill meson_usbphy_dr_mode(struct meson_usbphy_softc *sc)
     90  1.1  jmcneill {
     91  1.1  jmcneill 	int index, phandle;
     92  1.1  jmcneill 
     93  1.1  jmcneill 	index = 0;
     94  1.1  jmcneill 	while ((phandle = fdt_find_with_property("phys", &index)) != -1) {
     95  1.1  jmcneill 		const int phy_phandle = fdtbus_get_phandle(phandle, "phys");
     96  1.1  jmcneill 		if (phy_phandle != sc->sc_phandle)
     97  1.1  jmcneill 			continue;
     98  1.1  jmcneill 		return fdtbus_get_string(phandle, "dr_mode");
     99  1.1  jmcneill 	}
    100  1.1  jmcneill 
    101  1.1  jmcneill 	return NULL;
    102  1.1  jmcneill }
    103  1.1  jmcneill 
    104  1.1  jmcneill static void *
    105  1.1  jmcneill meson_usbphy_acquire(device_t dev, const void *data, size_t len)
    106  1.1  jmcneill {
    107  1.1  jmcneill 	if (len != 0)
    108  1.1  jmcneill 		return NULL;
    109  1.1  jmcneill 
    110  1.1  jmcneill 	return (void *)(uintptr_t)1;
    111  1.1  jmcneill }
    112  1.1  jmcneill 
    113  1.1  jmcneill static void
    114  1.1  jmcneill meson_usbphy_release(device_t dev, void *priv)
    115  1.1  jmcneill {
    116  1.1  jmcneill }
    117  1.1  jmcneill 
    118  1.1  jmcneill static int
    119  1.1  jmcneill meson_usbphy_enable(device_t dev, void *priv, bool enable)
    120  1.1  jmcneill {
    121  1.1  jmcneill 	struct meson_usbphy_softc * const sc = device_private(dev);
    122  1.2  jmcneill 	struct fdtbus_regulator *reg;
    123  1.1  jmcneill 	uint32_t val;
    124  1.2  jmcneill 	int error;
    125  1.1  jmcneill 
    126  1.1  jmcneill 	if (enable) {
    127  1.2  jmcneill 		if (of_hasprop(sc->sc_phandle, "phy-supply")) {
    128  1.2  jmcneill 			reg = fdtbus_regulator_acquire(sc->sc_phandle, "phy-supply");
    129  1.2  jmcneill 			if (reg != NULL) {
    130  1.2  jmcneill 				error = fdtbus_regulator_enable(reg);
    131  1.2  jmcneill 				if (error != 0)
    132  1.2  jmcneill 					device_printf(dev, "WARNING: couldn't enable phy-supply: %d\n", error);
    133  1.2  jmcneill 			} else {
    134  1.2  jmcneill 				device_printf(dev, "WARNING: couldn't acquire phy-supply\n");
    135  1.2  jmcneill 			}
    136  1.2  jmcneill 		}
    137  1.2  jmcneill 
    138  1.1  jmcneill 		delay(1000);
    139  1.1  jmcneill 
    140  1.1  jmcneill 		val = PHY_READ(sc, PREI_USB_PHY_CFG_REG);
    141  1.1  jmcneill 		val |= PREI_USB_PHY_CFG_CLK_32K_ALT_SEL;
    142  1.1  jmcneill 		PHY_WRITE(sc, PREI_USB_PHY_CFG_REG, val);
    143  1.1  jmcneill 
    144  1.1  jmcneill 		val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
    145  1.1  jmcneill 		val &= ~PREI_USB_PHY_CTRL_FSEL;
    146  1.1  jmcneill 		val |= __SHIFTIN(PREI_USB_PHY_CTRL_FSEL_24M,
    147  1.1  jmcneill 				 PREI_USB_PHY_CTRL_FSEL);
    148  1.1  jmcneill 		val |= PREI_USB_PHY_CTRL_POR;
    149  1.1  jmcneill 		PHY_WRITE(sc, PREI_USB_PHY_CTRL_REG, val);
    150  1.1  jmcneill 
    151  1.1  jmcneill 		delay(1000);
    152  1.1  jmcneill 
    153  1.1  jmcneill 		val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
    154  1.1  jmcneill 		val &= ~PREI_USB_PHY_CTRL_POR;
    155  1.1  jmcneill 		PHY_WRITE(sc, PREI_USB_PHY_CTRL_REG, val);
    156  1.1  jmcneill 
    157  1.1  jmcneill 		delay(50000);
    158  1.1  jmcneill 
    159  1.1  jmcneill 		val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
    160  1.1  jmcneill 		if ((val & PREI_USB_PHY_CTRL_CLK_DET) == 0)
    161  1.1  jmcneill 			aprint_error_dev(dev, "WARNING: USB PHY clock not detected\n");
    162  1.1  jmcneill 
    163  1.1  jmcneill 		if (sc->sc_dr_mode && strcmp(sc->sc_dr_mode, "host") == 0) {
    164  1.1  jmcneill 			val = PHY_READ(sc, PREI_USB_PHY_ADP_BC_REG);
    165  1.1  jmcneill 			val |= PREI_USB_PHY_ADP_BC_ACA_ENABLE;
    166  1.1  jmcneill 			PHY_WRITE(sc, PREI_USB_PHY_ADP_BC_REG, val);
    167  1.1  jmcneill 
    168  1.1  jmcneill 			delay(1000);
    169  1.1  jmcneill 
    170  1.1  jmcneill 			val = PHY_READ(sc, PREI_USB_PHY_ADP_BC_REG);
    171  1.1  jmcneill 			if ((val & PREI_USB_PHY_ADP_BC_ACA_FLOATING) != 0)
    172  1.1  jmcneill 				aprint_error_dev(dev, "WARNING: USB PHY failed to enable ACA detection\n");
    173  1.1  jmcneill 		}
    174  1.1  jmcneill 	}
    175  1.1  jmcneill 
    176  1.1  jmcneill 	return 0;
    177  1.1  jmcneill }
    178  1.1  jmcneill 
    179  1.1  jmcneill const struct fdtbus_phy_controller_func meson_usbphy_funcs = {
    180  1.1  jmcneill 	.acquire = meson_usbphy_acquire,
    181  1.1  jmcneill 	.release = meson_usbphy_release,
    182  1.1  jmcneill 	.enable = meson_usbphy_enable,
    183  1.1  jmcneill };
    184  1.1  jmcneill 
    185  1.1  jmcneill static int
    186  1.1  jmcneill meson_usbphy_match(device_t parent, cfdata_t cf, void *aux)
    187  1.1  jmcneill {
    188  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    189  1.1  jmcneill 
    190  1.6   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    191  1.1  jmcneill }
    192  1.1  jmcneill 
    193  1.1  jmcneill static void
    194  1.1  jmcneill meson_usbphy_attach(device_t parent, device_t self, void *aux)
    195  1.1  jmcneill {
    196  1.1  jmcneill 	struct meson_usbphy_softc * const sc = device_private(self);
    197  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    198  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    199  1.1  jmcneill 	struct fdtbus_reset *rst;
    200  1.1  jmcneill 	struct clk *clk;
    201  1.1  jmcneill 	bus_addr_t addr;
    202  1.1  jmcneill 	bus_size_t size;
    203  1.1  jmcneill 	u_int n;
    204  1.1  jmcneill 
    205  1.1  jmcneill 	sc->sc_dev = self;
    206  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    207  1.1  jmcneill 	sc->sc_phandle = phandle;
    208  1.6   thorpej 	sc->sc_type = of_compatible_lookup(phandle, compat_data)->value;
    209  1.1  jmcneill 
    210  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    211  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    212  1.1  jmcneill 		return;
    213  1.1  jmcneill 	}
    214  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    215  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    216  1.1  jmcneill 		return;
    217  1.1  jmcneill 	}
    218  1.1  jmcneill 
    219  1.1  jmcneill 	/* Enable clocks */
    220  1.1  jmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
    221  1.1  jmcneill 		if (clk_enable(clk) != 0) {
    222  1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
    223  1.1  jmcneill 			return;
    224  1.1  jmcneill 		}
    225  1.1  jmcneill 	/* De-assert resets */
    226  1.1  jmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
    227  1.1  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    228  1.1  jmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
    229  1.1  jmcneill 			return;
    230  1.1  jmcneill 		}
    231  1.1  jmcneill 
    232  1.1  jmcneill 	sc->sc_dr_mode = meson_usbphy_dr_mode(sc);
    233  1.1  jmcneill 
    234  1.1  jmcneill 	aprint_naive("\n");
    235  1.1  jmcneill 	aprint_normal(": USB2 PHY (%s)\n", sc->sc_dr_mode ? sc->sc_dr_mode : "unknown mode");
    236  1.1  jmcneill 
    237  1.1  jmcneill 	fdtbus_register_phy_controller(self, phandle, &meson_usbphy_funcs);
    238  1.1  jmcneill }
    239