xref: /netbsd-src/sys/arch/arm/amlogic/meson_usbctrl.c (revision 90313c06e62e910bf0d1bb24faa9d17dcefd0ab6)
1*90313c06Smsaitoh /* $NetBSD: meson_usbctrl.c,v 1.6 2024/02/07 04:20:26 msaitoh Exp $ */
28afae5d5Sryo 
38afae5d5Sryo /*
4*90313c06Smsaitoh  * Copyright (c) 2021 Ryo Shimizu
58afae5d5Sryo  * All rights reserved.
68afae5d5Sryo  *
78afae5d5Sryo  * Redistribution and use in source and binary forms, with or without
88afae5d5Sryo  * modification, are permitted provided that the following conditions
98afae5d5Sryo  * are met:
108afae5d5Sryo  * 1. Redistributions of source code must retain the above copyright
118afae5d5Sryo  *    notice, this list of conditions and the following disclaimer.
128afae5d5Sryo  * 2. Redistributions in binary form must reproduce the above copyright
138afae5d5Sryo  *    notice, this list of conditions and the following disclaimer in the
148afae5d5Sryo  *    documentation and/or other materials provided with the distribution.
158afae5d5Sryo  *
168afae5d5Sryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
178afae5d5Sryo  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
188afae5d5Sryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198afae5d5Sryo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
208afae5d5Sryo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
218afae5d5Sryo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
228afae5d5Sryo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238afae5d5Sryo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
248afae5d5Sryo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
258afae5d5Sryo  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
268afae5d5Sryo  * POSSIBILITY OF SUCH DAMAGE.
278afae5d5Sryo  */
288afae5d5Sryo 
298afae5d5Sryo #include <sys/cdefs.h>
30*90313c06Smsaitoh __KERNEL_RCSID(0, "$NetBSD: meson_usbctrl.c,v 1.6 2024/02/07 04:20:26 msaitoh Exp $");
318afae5d5Sryo 
328afae5d5Sryo #include <sys/param.h>
338afae5d5Sryo #include <sys/types.h>
348afae5d5Sryo #include <sys/bus.h>
358afae5d5Sryo #include <sys/device.h>
368afae5d5Sryo 
378afae5d5Sryo #include <dev/fdt/fdtvar.h>
388afae5d5Sryo 
398afae5d5Sryo /*
408afae5d5Sryo  * USB Glue registers: 0xffe09000
418afae5d5Sryo  */
428afae5d5Sryo 
438afae5d5Sryo /* usb2 phy ports control registers */
448afae5d5Sryo #define MESONUSBCTRL_MAXPHYS				3
458afae5d5Sryo #define U2P_R0_REG(i)					(0x20 * (i) + 0x00)
468afae5d5Sryo #define  U2P_R0_DRV_VBUS				__BIT(5)
478afae5d5Sryo #define  U2P_R0_ID_PULLUP				__BIT(4)
488afae5d5Sryo #define  U2P_R0_POWER_ON_RESET				__BIT(3)
498afae5d5Sryo #define  U2P_R0_HAST_MODE				__BIT(2)
508afae5d5Sryo #define  U2P_R0_POWER_OK				__BIT(1)
518afae5d5Sryo #define  U2P_R0_HOST_DEVICE				__BIT(0)
528afae5d5Sryo #define U2P_R1_REG(i)					(0x20 * (i) + 0x04)
538afae5d5Sryo #define  U2P_R1_VBUS_VALID				__BIT(3)
548afae5d5Sryo #define  U2P_R1_OTG_SESSION_VALID			__BIT(2)
558afae5d5Sryo #define  U2P_R1_ID_DIG					__BIT(1)
568afae5d5Sryo #define  U2P_R1_PHY_READY				__BIT(0)
578afae5d5Sryo 
588afae5d5Sryo /* glue registers */
598afae5d5Sryo #define USB_R0_REG					0x80
608afae5d5Sryo #define  USB_R0_U2D_ACT					__BIT(31)
618afae5d5Sryo #define  USB_R0_U2D_SS_SCALEDOWN_MODE_MASK		__BITS(30,29)
628afae5d5Sryo #define  USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK		__BITS(28,19)
638afae5d5Sryo #define  USB_R0_P30_LANE0_EXT_PCLK_REQ			__BIT(18)
648afae5d5Sryo #define  USB_R0_P30_LANE0_TX2RX_LOOPBACK		__BIT(17)
658afae5d5Sryo #define USB_R1_REG					0x84
668afae5d5Sryo #define  USB_R1_P30_PCS_TX_SWING_FULL_MASK		__BITS(31,25)
678afae5d5Sryo #define  USB_R1_U3H_FLADJ_30MHZ_REG_MASK		__BITS(24,19)
688afae5d5Sryo #define  USB_R1_U3H_HOST_MSI_ENABLE			__BIT(18)
698afae5d5Sryo #define  USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT	__BIT(17)
708afae5d5Sryo #define  USB_R1_U3H_HOST_U3_PORT_DISABLE		__BIT(16)
718afae5d5Sryo #define  USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK		__BITS(13,12)
728afae5d5Sryo #define  USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK		__BITS(9,7)
738afae5d5Sryo #define  USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK		__BITS(4,2)
748afae5d5Sryo #define  USB_R1_U3H_PME_ENABLE				__BIT(1)
758afae5d5Sryo #define  USB_R1_U3H_BIGENDIAN_GS			__BIT(0)
768afae5d5Sryo #define USB_R2_REG					0x88
778afae5d5Sryo #define  USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK		__BITS(31,26)
788afae5d5Sryo #define  USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK		__BITS(25,20)
798afae5d5Sryo #define USB_R3_REG					0x8c
808afae5d5Sryo #define  USB_R3_P30_REF_SSP_EN				__BIT(13)
818afae5d5Sryo #define  USB_R3_P30_SSC_REF_CLK_SEL_MASK		__BITS(12,4)
828afae5d5Sryo #define  USB_R3_P30_SSC_RANGE_MASK			__BITS(3,1)
838afae5d5Sryo #define  USB_R3_P30_SSC_ENABLE				__BIT(0)
848afae5d5Sryo #define USB_R4_REG					0x90
858afae5d5Sryo #define  USB_R4_P21_ONLY				__BIT(4)
868afae5d5Sryo #define  USB_R4_MEM_PD_MASK				__BITS(3,2)
878afae5d5Sryo #define  USB_R4_P21_SLEEP_M0				__BIT(1)
888afae5d5Sryo #define  USB_R4_P21_PORT_RESET_0			__BIT(0)
898afae5d5Sryo #define USB_R5_REG					0x94
908afae5d5Sryo #define  USB_R5_ID_DIG_CNT_MASK				__BITS(23,16)
918afae5d5Sryo #define  USB_R5_ID_DIG_TH_MASK				__BITS(15,8)
928afae5d5Sryo #define  USB_R5_ID_DIG_IRQ				__BIT(7)
938afae5d5Sryo #define  USB_R5_ID_DIG_CURR				__BIT(6)
948afae5d5Sryo #define  USB_R5_ID_DIG_EN_1				__BIT(5)
958afae5d5Sryo #define  USB_R5_ID_DIG_EN_0				__BIT(4)
968afae5d5Sryo #define  USB_R5_ID_DIG_CFG_MASK				__BITS(3,2)
978afae5d5Sryo #define  USB_R5_ID_DIG_REG				__BIT(1)
988afae5d5Sryo #define  USB_R5_ID_DIG_SYNC				__BIT(0)
998afae5d5Sryo 
1008afae5d5Sryo #define USBCTRL_READ_REG(sc, reg) \
1018afae5d5Sryo 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
1028afae5d5Sryo #define USBCTRL_WRITE_REG(sc, reg, val) \
1038afae5d5Sryo 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
1048afae5d5Sryo 
1058afae5d5Sryo struct meson_usbctrl_config {
1068afae5d5Sryo 	int num_phys;
1078afae5d5Sryo };
1088afae5d5Sryo 
1098afae5d5Sryo struct meson_usbctrl_config mesong12_conf = {
1108afae5d5Sryo 	.num_phys = 3
1118afae5d5Sryo };
1128afae5d5Sryo 
113646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
114646c0f59Sthorpej 	{ .compat = "amlogic,meson-g12a-usb-ctrl", .data = &mesong12_conf },
1152dcdd1cdSthorpej 	DEVICE_COMPAT_EOL
1168afae5d5Sryo };
1178afae5d5Sryo 
1188afae5d5Sryo struct meson_usbctrl_softc {
1198afae5d5Sryo 	device_t sc_dev;
1208afae5d5Sryo 	bus_space_tag_t sc_bst;
1218afae5d5Sryo 	bus_space_handle_t sc_bsh;
122646c0f59Sthorpej 	const struct meson_usbctrl_config *sc_conf;
1238afae5d5Sryo 	struct fdtbus_regulator *sc_supply;
1248afae5d5Sryo 	int sc_phandle;
1258afae5d5Sryo };
1268afae5d5Sryo 
1278afae5d5Sryo static void
meson_usbctrl_usb2_init(struct meson_usbctrl_softc * sc)1288afae5d5Sryo meson_usbctrl_usb2_init(struct meson_usbctrl_softc *sc)
1298afae5d5Sryo {
1308afae5d5Sryo 	int i;
1318afae5d5Sryo 	const char *p;
1328afae5d5Sryo 
1338afae5d5Sryo 	for (i = 0; i < sc->sc_conf->num_phys; i++) {
1348afae5d5Sryo 		/* setup only for usb2 phys */
1358afae5d5Sryo 		p = fdtbus_get_string_index(sc->sc_phandle, "phy-names", i);
1368afae5d5Sryo 		if (p == NULL || strstr(p, "usb2") == NULL)
1378afae5d5Sryo 			continue;
1388afae5d5Sryo 
1398afae5d5Sryo 		USBCTRL_WRITE_REG(sc, U2P_R0_REG(i),
1408afae5d5Sryo 		    USBCTRL_READ_REG(sc, U2P_R0_REG(i)) |
1418afae5d5Sryo 		    U2P_R0_POWER_ON_RESET);
1428afae5d5Sryo 
1438afae5d5Sryo 		/* XXX: OTG not supported. always set HOST_DEVICE mode */
1448afae5d5Sryo 		USBCTRL_WRITE_REG(sc, U2P_R0_REG(i),
1458afae5d5Sryo 		    USBCTRL_READ_REG(sc, U2P_R0_REG(i)) |
1468afae5d5Sryo 		    U2P_R0_HOST_DEVICE);
1478afae5d5Sryo 
1488afae5d5Sryo 		USBCTRL_WRITE_REG(sc, U2P_R0_REG(i),
1498afae5d5Sryo 		    USBCTRL_READ_REG(sc, U2P_R0_REG(i)) &
1508afae5d5Sryo 		    ~U2P_R0_POWER_ON_RESET);
1518afae5d5Sryo 	}
1528afae5d5Sryo }
1538afae5d5Sryo 
1548afae5d5Sryo static void
meson_usbctrl_usb_glue_init(struct meson_usbctrl_softc * sc)1558afae5d5Sryo meson_usbctrl_usb_glue_init(struct meson_usbctrl_softc *sc)
1568afae5d5Sryo {
1578afae5d5Sryo 	uint32_t val;
1588afae5d5Sryo 
1598afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R1_REG);
1608afae5d5Sryo 	val &= ~USB_R1_U3H_FLADJ_30MHZ_REG_MASK;
1618afae5d5Sryo 	val |= __SHIFTIN(0x20, USB_R1_U3H_FLADJ_30MHZ_REG_MASK);
1628afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R1_REG, val);
1638afae5d5Sryo 
1648afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R5_REG);
1658afae5d5Sryo 	val |= USB_R5_ID_DIG_EN_0;
1668afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R5_REG, val);
1678afae5d5Sryo 
1688afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R5_REG);
1698afae5d5Sryo 	val |= USB_R5_ID_DIG_EN_1;
1708afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R5_REG, val);
1718afae5d5Sryo 
1728afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R5_REG);
1738afae5d5Sryo 	val &= ~USB_R5_ID_DIG_TH_MASK;
1748afae5d5Sryo 	val |= __SHIFTIN(0xff, USB_R5_ID_DIG_TH_MASK);
1758afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R5_REG, val);
1768afae5d5Sryo }
1778afae5d5Sryo 
1788afae5d5Sryo static void
meson_usbctrl_usb3_init(struct meson_usbctrl_softc * sc)1798afae5d5Sryo meson_usbctrl_usb3_init(struct meson_usbctrl_softc *sc)
1808afae5d5Sryo {
1818afae5d5Sryo 	uint32_t val;
1828afae5d5Sryo 
1838afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R3_REG);
1848afae5d5Sryo 	val &= ~USB_R3_P30_SSC_RANGE_MASK;
1858afae5d5Sryo 	val &= ~USB_R3_P30_SSC_ENABLE;
1868afae5d5Sryo 	val |= __SHIFTIN(2, USB_R3_P30_SSC_RANGE_MASK);
1878afae5d5Sryo 	val |= USB_R3_P30_REF_SSP_EN;
1888afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R3_REG, val);
1898afae5d5Sryo 
1908afae5d5Sryo 	delay(2);
1918afae5d5Sryo 
1928afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R2_REG);
1938afae5d5Sryo 	val &= ~USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK;
1948afae5d5Sryo 	val |= __SHIFTIN(0x15, USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK);
1958afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R2_REG, val);
1968afae5d5Sryo 
1978afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R2_REG);
1988afae5d5Sryo 	val &= ~USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK;
1998afae5d5Sryo 	val |= __SHIFTIN(0x20, USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK);
2008afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R2_REG, val);
2018afae5d5Sryo 
2028afae5d5Sryo 	delay(2);
2038afae5d5Sryo 
2048afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R1_REG);
2058afae5d5Sryo 	val |= USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT;
2068afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R1_REG, val);
2078afae5d5Sryo 
2088afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R1_REG);
2098afae5d5Sryo 	val &= ~USB_R1_P30_PCS_TX_SWING_FULL_MASK;
2108afae5d5Sryo 	val |= __SHIFTIN(127, USB_R1_P30_PCS_TX_SWING_FULL_MASK);
2118afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R1_REG, val);
2128afae5d5Sryo 
2138afae5d5Sryo 	/* XXX: force HOST_DEVICE mode */
2148afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R0_REG);
2158afae5d5Sryo 	val &= ~USB_R0_U2D_ACT;
2168afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R0_REG, val);
2178afae5d5Sryo 
2188afae5d5Sryo 	val = USBCTRL_READ_REG(sc, USB_R4_REG);
2198afae5d5Sryo 	val &= ~USB_R4_P21_SLEEP_M0;
2208afae5d5Sryo 	USBCTRL_WRITE_REG(sc, USB_R4_REG, val);
2218afae5d5Sryo }
2228afae5d5Sryo 
2238afae5d5Sryo static void
meson_usbctrl_enable_usb3_phys(struct meson_usbctrl_softc * sc)2248afae5d5Sryo meson_usbctrl_enable_usb3_phys(struct meson_usbctrl_softc *sc)
2258afae5d5Sryo {
2268afae5d5Sryo 	struct fdtbus_phy *phy;
2278afae5d5Sryo 	int i;
2288afae5d5Sryo 	const char *phyname;
2298afae5d5Sryo 
2308afae5d5Sryo 	/*
2318afae5d5Sryo 	 * enable only for usb3 phys.
2328afae5d5Sryo 	 * node of "snps,dwc3" decl in "amlogic,meson-g12a-usb-ctrl" have
2338afae5d5Sryo 	 * no "phys" property, so enable the phy here.
2348afae5d5Sryo 	 */
2358afae5d5Sryo 	for (i = 0; i < sc->sc_conf->num_phys; i++) {
2368afae5d5Sryo 		phyname = fdtbus_get_string_index(sc->sc_phandle,
2378afae5d5Sryo 		    "phy-names", i);
2388afae5d5Sryo 		if (strstr(phyname, "usb3") == NULL)
2398afae5d5Sryo 			continue;
2408afae5d5Sryo 
2418afae5d5Sryo 		phy = fdtbus_phy_get_index(sc->sc_phandle, i);
2428afae5d5Sryo 		if (phy == NULL)
2438afae5d5Sryo 			continue;
2448afae5d5Sryo 		if (fdtbus_phy_enable(phy, true) != 0)
2458afae5d5Sryo 			aprint_error_dev(sc->sc_dev, "couldn't enable phy %s\n",
2468afae5d5Sryo 			    phyname);
2478afae5d5Sryo 	}
2488afae5d5Sryo }
2498afae5d5Sryo 
2508afae5d5Sryo static int
meson_usbctrl_match(device_t parent,cfdata_t cf,void * aux)2518afae5d5Sryo meson_usbctrl_match(device_t parent, cfdata_t cf, void *aux)
2528afae5d5Sryo {
2538afae5d5Sryo 	struct fdt_attach_args * const faa = aux;
2548afae5d5Sryo 
2556e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
2568afae5d5Sryo }
2578afae5d5Sryo 
2588afae5d5Sryo static void
meson_usbctrl_attach(device_t parent,device_t self,void * aux)2598afae5d5Sryo meson_usbctrl_attach(device_t parent, device_t self, void *aux)
2608afae5d5Sryo {
2618afae5d5Sryo 	struct meson_usbctrl_softc * const sc = device_private(self);
2628afae5d5Sryo 	struct fdt_attach_args * const faa = aux;
2638afae5d5Sryo 	bus_addr_t addr;
2648afae5d5Sryo 	bus_size_t size;
2658afae5d5Sryo 	int phandle, child;
2668afae5d5Sryo 
2678afae5d5Sryo 	sc->sc_dev = self;
2688afae5d5Sryo 	sc->sc_bst = faa->faa_bst;
2698afae5d5Sryo 	sc->sc_phandle = phandle = faa->faa_phandle;
2706e54367aSthorpej 	sc->sc_conf = of_compatible_lookup(phandle, compat_data)->data;
2718afae5d5Sryo 
2728afae5d5Sryo 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
2738afae5d5Sryo 		aprint_error(": couldn't get registers\n");
2748afae5d5Sryo 		return;
2758afae5d5Sryo 	}
2768afae5d5Sryo 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
2778afae5d5Sryo 		aprint_error(": couldn't map registers\n");
2788afae5d5Sryo 		return;
2798afae5d5Sryo 	}
2808afae5d5Sryo 
2818afae5d5Sryo 	aprint_naive("\n");
2828afae5d5Sryo 	aprint_normal(": USB Controllers\n");
2838afae5d5Sryo 
2848afae5d5Sryo 	sc->sc_supply = fdtbus_regulator_acquire(phandle, "vbus-supply");
2858afae5d5Sryo 	if (sc->sc_supply != NULL)
2868afae5d5Sryo 		fdtbus_regulator_enable(sc->sc_supply);	/* USB HOST MODE */
2878afae5d5Sryo 
2888afae5d5Sryo 	meson_usbctrl_usb2_init(sc);
2898afae5d5Sryo 	meson_usbctrl_usb_glue_init(sc);
2908afae5d5Sryo 	meson_usbctrl_usb3_init(sc);
2918afae5d5Sryo 	meson_usbctrl_enable_usb3_phys(sc);
2928afae5d5Sryo 
2938afae5d5Sryo 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
2948afae5d5Sryo 		fdt_add_child(parent, child, faa, 0);
2958afae5d5Sryo 	}
2968afae5d5Sryo }
2978afae5d5Sryo 
2988afae5d5Sryo CFATTACH_DECL_NEW(meson_usbctrl, sizeof(struct meson_usbctrl_softc),
2998afae5d5Sryo     meson_usbctrl_match, meson_usbctrl_attach, NULL, NULL);
300