sunxi_usb3phy.c revision 1.2
11.2Sthorpej/* $NetBSD: sunxi_usb3phy.c,v 1.2 2021/01/18 02:35:49 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.1Sjmcneill
311.2Sthorpej__KERNEL_RCSID(0, "$NetBSD: sunxi_usb3phy.c,v 1.2 2021/01/18 02:35:49 thorpej Exp $");
321.1Sjmcneill
331.1Sjmcneill#include <sys/param.h>
341.1Sjmcneill#include <sys/bus.h>
351.1Sjmcneill#include <sys/device.h>
361.1Sjmcneill#include <sys/intr.h>
371.1Sjmcneill#include <sys/systm.h>
381.1Sjmcneill#include <sys/time.h>
391.1Sjmcneill
401.1Sjmcneill#include <dev/fdt/fdtvar.h>
411.1Sjmcneill
421.1Sjmcneill#define	SUNXI_APP			0x00
431.1Sjmcneill#define	 APP_FORCE_VBUS			__BITS(13,12)
441.1Sjmcneill
451.1Sjmcneill#define	SUNXI_PIPE_CLOCK_CONTROL	0x14
461.1Sjmcneill#define	 PCC_PIPE_CLK_OPEN		__BIT(6)
471.1Sjmcneill
481.1Sjmcneill#define	SUNXI_PHY_TUNE_LOW		0x18
491.1Sjmcneill#define	 PTL_MAGIC			0x0047fc87
501.1Sjmcneill
511.1Sjmcneill#define	SUNXI_PHY_TUNE_HIGH		0x1c
521.1Sjmcneill#define	 PTH_TX_DEEMPH_3P5DB		__BITS(24,19)
531.1Sjmcneill#define	 PTH_TX_DEEMPH_6DB		__BITS(18,13)
541.1Sjmcneill#define	 PTH_TX_SWING_FULL		__BITS(12,6)
551.1Sjmcneill#define	 PTH_LOS_BIAS			__BITS(5,3)
561.1Sjmcneill#define	 PTH_TX_BOOST_LVL		__BITS(2,0)
571.1Sjmcneill
581.1Sjmcneill#define	SUNXI_PHY_EXTERNAL_CONTROL	0x20
591.1Sjmcneill#define	 PEC_REF_SSP_EN			__BIT(26)
601.1Sjmcneill#define	 PEC_SSC_EN			__BIT(24)
611.1Sjmcneill#define	 PEC_EXTERN_VBUS		__BITS(2,1)
621.1Sjmcneill
631.1Sjmcneillstatic int sunxi_usb3phy_match(device_t, cfdata_t, void *);
641.1Sjmcneillstatic void sunxi_usb3phy_attach(device_t, device_t, void *);
651.1Sjmcneill
661.1Sjmcneillenum sunxi_usb3phy_type {
671.1Sjmcneill	USB3PHY_H6 = 1,
681.1Sjmcneill};
691.1Sjmcneill
701.2Sthorpejstatic const struct device_compatible_entry compat_data[] = {
711.2Sthorpej	{ .compat = "allwinner,sun50i-h6-usb3-phy",	.value = USB3PHY_H6 },
721.2Sthorpej
731.2Sthorpej	{ 0 }
741.1Sjmcneill};
751.1Sjmcneill
761.1Sjmcneillstruct sunxi_usb3phy {
771.1Sjmcneill	bus_space_tag_t		phy_bst;
781.1Sjmcneill	bus_space_handle_t	phy_bsh;
791.1Sjmcneill	struct fdtbus_regulator *phy_reg;
801.1Sjmcneill};
811.1Sjmcneill
821.1Sjmcneillstruct sunxi_usb3phy_softc {
831.1Sjmcneill	device_t		sc_dev;
841.1Sjmcneill	enum sunxi_usb3phy_type	sc_type;
851.1Sjmcneill
861.1Sjmcneill	struct sunxi_usb3phy	sc_phy;
871.1Sjmcneill
881.1Sjmcneill	struct fdtbus_gpio_pin	*sc_gpio_id_det;
891.1Sjmcneill	struct fdtbus_gpio_pin	*sc_gpio_vbus_det;
901.1Sjmcneill};
911.1Sjmcneill
921.1Sjmcneill#define	PHY_READ(phy, reg)				\
931.1Sjmcneill	bus_space_read_4((phy)->phy_bst, (phy)->phy_bsh, (reg))
941.1Sjmcneill#define	PHY_WRITE(phy, reg, val)				\
951.1Sjmcneill	bus_space_write_4((phy)->phy_bst, (phy)->phy_bsh, (reg), (val))
961.1Sjmcneill
971.1SjmcneillCFATTACH_DECL_NEW(sunxi_usb3phy, sizeof(struct sunxi_usb3phy_softc),
981.1Sjmcneill	sunxi_usb3phy_match, sunxi_usb3phy_attach, NULL, NULL);
991.1Sjmcneill
1001.1Sjmcneillstatic void *
1011.1Sjmcneillsunxi_usb3phy_acquire(device_t dev, const void *data, size_t len)
1021.1Sjmcneill{
1031.1Sjmcneill	struct sunxi_usb3phy_softc * const sc = device_private(dev);
1041.1Sjmcneill
1051.1Sjmcneill	return &sc->sc_phy;
1061.1Sjmcneill}
1071.1Sjmcneill
1081.1Sjmcneillstatic void
1091.1Sjmcneillsunxi_usb3phy_release(device_t dev, void *priv)
1101.1Sjmcneill{
1111.1Sjmcneill}
1121.1Sjmcneill
1131.1Sjmcneillstatic int
1141.1Sjmcneillsunxi_usb3phy_enable(device_t dev, void *priv, bool enable)
1151.1Sjmcneill{
1161.1Sjmcneill	struct sunxi_usb3phy * const phy = priv;
1171.1Sjmcneill	uint32_t val;
1181.1Sjmcneill
1191.1Sjmcneill	if (enable) {
1201.1Sjmcneill		val = PHY_READ(phy, SUNXI_PHY_EXTERNAL_CONTROL);
1211.1Sjmcneill		val |= PEC_EXTERN_VBUS;
1221.1Sjmcneill		val |= PEC_SSC_EN;
1231.1Sjmcneill		val |= PEC_REF_SSP_EN;
1241.1Sjmcneill		PHY_WRITE(phy, SUNXI_PHY_EXTERNAL_CONTROL, val);
1251.1Sjmcneill
1261.1Sjmcneill		val = PHY_READ(phy, SUNXI_PIPE_CLOCK_CONTROL);
1271.1Sjmcneill		val |= PCC_PIPE_CLK_OPEN;
1281.1Sjmcneill		PHY_WRITE(phy, SUNXI_PIPE_CLOCK_CONTROL, val);
1291.1Sjmcneill
1301.1Sjmcneill		val = PHY_READ(phy, SUNXI_APP);
1311.1Sjmcneill		val |= APP_FORCE_VBUS;
1321.1Sjmcneill		PHY_WRITE(phy, SUNXI_APP, val);
1331.1Sjmcneill
1341.1Sjmcneill		PHY_WRITE(phy, SUNXI_PHY_TUNE_LOW, PTL_MAGIC);
1351.1Sjmcneill
1361.1Sjmcneill		val = PHY_READ(phy, SUNXI_PHY_TUNE_HIGH);
1371.1Sjmcneill		val |= PTH_TX_BOOST_LVL;
1381.1Sjmcneill		val |= PTH_LOS_BIAS;
1391.1Sjmcneill		val &= ~PTH_TX_SWING_FULL;
1401.1Sjmcneill		val |= __SHIFTIN(0x55, PTH_TX_SWING_FULL);
1411.1Sjmcneill		val &= ~PTH_TX_DEEMPH_6DB;
1421.1Sjmcneill		val |= __SHIFTIN(0x20, PTH_TX_DEEMPH_6DB);
1431.1Sjmcneill		val &= ~PTH_TX_DEEMPH_3P5DB;
1441.1Sjmcneill		val |= __SHIFTIN(0x15, PTH_TX_DEEMPH_3P5DB);
1451.1Sjmcneill		PHY_WRITE(phy, SUNXI_PHY_TUNE_HIGH, val);
1461.1Sjmcneill
1471.1Sjmcneill		return phy->phy_reg ? fdtbus_regulator_enable(phy->phy_reg) : 0;
1481.1Sjmcneill	} else {
1491.1Sjmcneill		return phy->phy_reg ? fdtbus_regulator_disable(phy->phy_reg) : 0;
1501.1Sjmcneill	}
1511.1Sjmcneill}
1521.1Sjmcneill
1531.1Sjmcneillconst struct fdtbus_phy_controller_func sunxi_usb3phy_funcs = {
1541.1Sjmcneill	.acquire = sunxi_usb3phy_acquire,
1551.1Sjmcneill	.release = sunxi_usb3phy_release,
1561.1Sjmcneill	.enable = sunxi_usb3phy_enable,
1571.1Sjmcneill};
1581.1Sjmcneill
1591.1Sjmcneillstatic int
1601.1Sjmcneillsunxi_usb3phy_match(device_t parent, cfdata_t cf, void *aux)
1611.1Sjmcneill{
1621.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1631.1Sjmcneill
1641.1Sjmcneill	return of_match_compat_data(faa->faa_phandle, compat_data);
1651.1Sjmcneill}
1661.1Sjmcneill
1671.1Sjmcneillstatic void
1681.1Sjmcneillsunxi_usb3phy_attach(device_t parent, device_t self, void *aux)
1691.1Sjmcneill{
1701.1Sjmcneill	struct sunxi_usb3phy_softc * const sc = device_private(self);
1711.1Sjmcneill	struct sunxi_usb3phy *phy = &sc->sc_phy;
1721.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1731.1Sjmcneill	const int phandle = faa->faa_phandle;
1741.1Sjmcneill	struct fdtbus_reset *rst;
1751.1Sjmcneill	struct clk *clk;
1761.1Sjmcneill	bus_addr_t addr;
1771.1Sjmcneill	bus_size_t size;
1781.1Sjmcneill	u_int n;
1791.1Sjmcneill
1801.1Sjmcneill	sc->sc_dev = self;
1811.2Sthorpej	sc->sc_type = of_search_compatible(phandle, compat_data)->value;
1821.1Sjmcneill
1831.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1841.1Sjmcneill		aprint_error(": couldn't get phy registers\n");
1851.1Sjmcneill		return;
1861.1Sjmcneill	}
1871.1Sjmcneill
1881.1Sjmcneill	phy->phy_bst = faa->faa_bst;
1891.1Sjmcneill	if (bus_space_map(phy->phy_bst, addr, size, 0, &phy->phy_bsh) != 0) {
1901.1Sjmcneill		aprint_error(": couldn't map phy registers\n");
1911.1Sjmcneill		return;
1921.1Sjmcneill	}
1931.1Sjmcneill
1941.1Sjmcneill	/* Get optional regulator */
1951.1Sjmcneill	phy->phy_reg = fdtbus_regulator_acquire(phandle, "phy-supply");
1961.1Sjmcneill
1971.1Sjmcneill	/* Enable clocks */
1981.1Sjmcneill	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
1991.1Sjmcneill		if (clk_enable(clk) != 0) {
2001.1Sjmcneill			aprint_error(": couldn't enable clock #%d\n", n);
2011.1Sjmcneill			return;
2021.1Sjmcneill		}
2031.1Sjmcneill	/* De-assert resets */
2041.1Sjmcneill	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
2051.1Sjmcneill		if (fdtbus_reset_deassert(rst) != 0) {
2061.1Sjmcneill			aprint_error(": couldn't de-assert reset #%d\n", n);
2071.1Sjmcneill			return;
2081.1Sjmcneill		}
2091.1Sjmcneill
2101.1Sjmcneill	aprint_naive("\n");
2111.1Sjmcneill	aprint_normal(": USB3 PHY\n");
2121.1Sjmcneill
2131.1Sjmcneill	fdtbus_register_phy_controller(self, phandle, &sunxi_usb3phy_funcs);
2141.1Sjmcneill}
215