Home | History | Annotate | Line # | Download | only in sunxi
sunxi_usbphy.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: sunxi_usbphy.c,v 1.1 2017/06/29 17:08:52 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 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_usbphy.c,v 1.1 2017/06/29 17:08:52 jmcneill 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	OTG_PHY_CFG		0x20
     43  1.1  jmcneill #define	 OTG_PHY_ROUTE_OTG	__BIT(0)
     44  1.1  jmcneill 
     45  1.1  jmcneill #define	HCI_ICR			0x00
     46  1.1  jmcneill #define	 HCI_AHB_INCR8		__BIT(10)
     47  1.1  jmcneill #define	 HCI_AHB_INCR4		__BIT(9)
     48  1.1  jmcneill #define	 HCI_AHB_INCRX_ALIGN	__BIT(8)
     49  1.1  jmcneill #define	 HCI_ULPI_BYPASS	__BIT(0)
     50  1.1  jmcneill #define	PMU_UNK_H3		0x10
     51  1.1  jmcneill #define	 PMU_UNK_H3_CLR		__BIT(1)
     52  1.1  jmcneill 
     53  1.1  jmcneill static int sunxi_usbphy_match(device_t, cfdata_t, void *);
     54  1.1  jmcneill static void sunxi_usbphy_attach(device_t, device_t, void *);
     55  1.1  jmcneill 
     56  1.1  jmcneill static const char * const compatible[] = {
     57  1.1  jmcneill 	"allwinner,sun8i-h3-usb-phy",
     58  1.1  jmcneill 	NULL
     59  1.1  jmcneill };
     60  1.1  jmcneill 
     61  1.1  jmcneill #define	SUNXI_MAXUSBPHY		5
     62  1.1  jmcneill 
     63  1.1  jmcneill struct sunxi_usbphy {
     64  1.1  jmcneill 	u_int			phy_index;
     65  1.1  jmcneill 	bus_space_handle_t	phy_bsh;
     66  1.1  jmcneill 	struct fdtbus_regulator *phy_reg;
     67  1.1  jmcneill };
     68  1.1  jmcneill 
     69  1.1  jmcneill struct sunxi_usbphy_softc {
     70  1.1  jmcneill 	device_t		sc_dev;
     71  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     72  1.1  jmcneill 
     73  1.1  jmcneill 	struct sunxi_usbphy	sc_phys[SUNXI_MAXUSBPHY];
     74  1.1  jmcneill 	u_int			sc_nphys;
     75  1.1  jmcneill 
     76  1.1  jmcneill 	struct fdtbus_gpio_pin	*sc_gpio_id_det;
     77  1.1  jmcneill 	struct fdtbus_gpio_pin	*sc_gpio_vbus_det;
     78  1.1  jmcneill };
     79  1.1  jmcneill 
     80  1.1  jmcneill #define	USBPHY_READ(sc, id, reg)			\
     81  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst,			\
     82  1.1  jmcneill 	    (sc)->sc_phys[(id)].phy_bsh, (reg))
     83  1.1  jmcneill #define	USBPHY_WRITE(sc, id, reg, val)			\
     84  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst,			\
     85  1.1  jmcneill 	    (sc)->sc_phys[(id)].phy_bsh, (reg), (val))
     86  1.1  jmcneill 
     87  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_usbphy, sizeof(struct sunxi_usbphy_softc),
     88  1.1  jmcneill 	sunxi_usbphy_match, sunxi_usbphy_attach, NULL, NULL);
     89  1.1  jmcneill 
     90  1.1  jmcneill static bool
     91  1.1  jmcneill sunxi_usbphy_vbus_detect(struct sunxi_usbphy_softc *sc)
     92  1.1  jmcneill {
     93  1.1  jmcneill 	if (sc->sc_gpio_vbus_det)
     94  1.1  jmcneill 		return fdtbus_gpio_read(sc->sc_gpio_vbus_det);
     95  1.1  jmcneill 	return 1;
     96  1.1  jmcneill }
     97  1.1  jmcneill 
     98  1.1  jmcneill static void *
     99  1.1  jmcneill sunxi_usbphy_acquire(device_t dev, const void *data, size_t len)
    100  1.1  jmcneill {
    101  1.1  jmcneill 	struct sunxi_usbphy_softc * const sc = device_private(dev);
    102  1.1  jmcneill 
    103  1.1  jmcneill 	if (len != 4)
    104  1.1  jmcneill 		return NULL;
    105  1.1  jmcneill 
    106  1.1  jmcneill 	const int phy_id = be32dec(data);
    107  1.1  jmcneill 	if (phy_id >= sc->sc_nphys)
    108  1.1  jmcneill 		return NULL;
    109  1.1  jmcneill 
    110  1.1  jmcneill 	return &sc->sc_phys[phy_id];
    111  1.1  jmcneill }
    112  1.1  jmcneill 
    113  1.1  jmcneill static void
    114  1.1  jmcneill sunxi_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 sunxi_usbphy_enable(device_t dev, void *priv, bool enable)
    120  1.1  jmcneill {
    121  1.1  jmcneill 	struct sunxi_usbphy_softc * const sc = device_private(dev);
    122  1.1  jmcneill 	struct sunxi_usbphy * const phy = priv;
    123  1.1  jmcneill 	uint32_t val;
    124  1.1  jmcneill 
    125  1.1  jmcneill 	if (phy->phy_index > 0) {
    126  1.1  jmcneill 		/* Enable passby */
    127  1.1  jmcneill 		val = USBPHY_READ(sc, phy->phy_index, HCI_ICR);
    128  1.1  jmcneill 		val |= HCI_ULPI_BYPASS;
    129  1.1  jmcneill 		val |= HCI_AHB_INCR8;
    130  1.1  jmcneill 		val |= HCI_AHB_INCR4;
    131  1.1  jmcneill 		val |= HCI_AHB_INCRX_ALIGN;
    132  1.1  jmcneill 		USBPHY_WRITE(sc, phy->phy_index, HCI_ICR, val);
    133  1.1  jmcneill 	}
    134  1.1  jmcneill 
    135  1.1  jmcneill 	/* H3-specific */
    136  1.1  jmcneill 	val = USBPHY_READ(sc, phy->phy_index, PMU_UNK_H3);
    137  1.1  jmcneill 	val &= ~PMU_UNK_H3_CLR;
    138  1.1  jmcneill 	USBPHY_WRITE(sc, phy->phy_index, PMU_UNK_H3, val);
    139  1.1  jmcneill 
    140  1.1  jmcneill 	if (phy->phy_reg == NULL)
    141  1.1  jmcneill 		return 0;
    142  1.1  jmcneill 
    143  1.1  jmcneill 	if (enable) {
    144  1.1  jmcneill 		/* If an external vbus is detected, do not enable phy 0 */
    145  1.1  jmcneill 		if (phy->phy_index == 0 && sunxi_usbphy_vbus_detect(sc))
    146  1.1  jmcneill 			return 0;
    147  1.1  jmcneill 		return fdtbus_regulator_enable(phy->phy_reg);
    148  1.1  jmcneill 	} else {
    149  1.1  jmcneill 		return fdtbus_regulator_disable(phy->phy_reg);
    150  1.1  jmcneill 	}
    151  1.1  jmcneill }
    152  1.1  jmcneill 
    153  1.1  jmcneill const struct fdtbus_phy_controller_func sunxi_usbphy_funcs = {
    154  1.1  jmcneill 	.acquire = sunxi_usbphy_acquire,
    155  1.1  jmcneill 	.release = sunxi_usbphy_release,
    156  1.1  jmcneill 	.enable = sunxi_usbphy_enable,
    157  1.1  jmcneill };
    158  1.1  jmcneill 
    159  1.1  jmcneill static int
    160  1.1  jmcneill sunxi_usbphy_match(device_t parent, cfdata_t cf, void *aux)
    161  1.1  jmcneill {
    162  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    163  1.1  jmcneill 
    164  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    165  1.1  jmcneill }
    166  1.1  jmcneill 
    167  1.1  jmcneill static void
    168  1.1  jmcneill sunxi_usbphy_attach(device_t parent, device_t self, void *aux)
    169  1.1  jmcneill {
    170  1.1  jmcneill 	struct sunxi_usbphy_softc * const sc = device_private(self);
    171  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    172  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    173  1.1  jmcneill 	struct fdtbus_reset *rst;
    174  1.1  jmcneill 	struct sunxi_usbphy *phy;
    175  1.1  jmcneill 	struct clk *clk;
    176  1.1  jmcneill 	bus_addr_t addr;
    177  1.1  jmcneill 	bus_size_t size;
    178  1.1  jmcneill 	char pname[20];
    179  1.1  jmcneill 	u_int n;
    180  1.1  jmcneill 
    181  1.1  jmcneill 	sc->sc_dev = self;
    182  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    183  1.1  jmcneill 
    184  1.1  jmcneill 	for (sc->sc_nphys = 0; sc->sc_nphys < SUNXI_MAXUSBPHY; sc->sc_nphys++) {
    185  1.1  jmcneill 		if (fdtbus_get_reg(phandle, sc->sc_nphys, &addr, &size) != 0)
    186  1.1  jmcneill 			break;
    187  1.1  jmcneill 		phy = &sc->sc_phys[sc->sc_nphys];
    188  1.1  jmcneill 		phy->phy_index = sc->sc_nphys;
    189  1.1  jmcneill 		if (bus_space_map(sc->sc_bst, addr, size, 0, &phy->phy_bsh) != 0) {
    190  1.1  jmcneill 			aprint_error(": failed to map reg #%d\n", sc->sc_nphys);
    191  1.1  jmcneill 			return;
    192  1.1  jmcneill 		}
    193  1.1  jmcneill 		/* Get optional regulator */
    194  1.1  jmcneill 		snprintf(pname, sizeof(pname), "usb%d_vbus-supply", sc->sc_nphys);
    195  1.1  jmcneill 		phy->phy_reg = fdtbus_regulator_acquire(phandle, pname);
    196  1.1  jmcneill 	}
    197  1.1  jmcneill 
    198  1.1  jmcneill 	/* Enable clocks */
    199  1.1  jmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
    200  1.1  jmcneill 		if (clk_enable(clk) != 0) {
    201  1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
    202  1.1  jmcneill 			return;
    203  1.1  jmcneill 		}
    204  1.1  jmcneill 	/* De-assert resets */
    205  1.1  jmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
    206  1.1  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    207  1.1  jmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
    208  1.1  jmcneill 			return;
    209  1.1  jmcneill 		}
    210  1.1  jmcneill 
    211  1.1  jmcneill 	aprint_naive("\n");
    212  1.1  jmcneill 	aprint_normal(": USB PHY\n");
    213  1.1  jmcneill 
    214  1.1  jmcneill 	fdtbus_register_phy_controller(self, phandle, &sunxi_usbphy_funcs);
    215  1.1  jmcneill }
    216