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