11.2Sthorpej/* $NetBSD: mesongxl_usb3phy.c,v 1.2 2021/01/27 03:10:18 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2019 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: mesongxl_usb3phy.c,v 1.2 2021/01/27 03:10:18 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	USB3PHY_REG0			0x00
431.1Sjmcneill#define	 REG0_U2D_ACT			__BIT(31)
441.1Sjmcneill
451.1Sjmcneill#define	USB3PHY_REG1			0x04
461.1Sjmcneill#define	 REG1_U3H_FLADJ_30MHZ_REG	__BITS(24,19)
471.1Sjmcneill
481.1Sjmcneill#define	USB3PHY_REG4			0x10
491.1Sjmcneill#define	 REG4_P21_SLEEP_M0		__BIT(1)
501.1Sjmcneill
511.1Sjmcneill#define	USB3PHY_REG5			0x14
521.1Sjmcneill#define	 REG5_ID_DIG_TH			__BITS(15,8)
531.1Sjmcneill#define	 REG5_ID_DIG_EN_1		__BIT(5)
541.1Sjmcneill#define	 REG5_ID_DIG_EN_0		__BIT(4)
551.1Sjmcneill
561.1Sjmcneillstatic int mesongxl_usb3phy_match(device_t, cfdata_t, void *);
571.1Sjmcneillstatic void mesongxl_usb3phy_attach(device_t, device_t, void *);
581.1Sjmcneill
591.2Sthorpejstatic const struct device_compatible_entry compat_data[] = {
601.2Sthorpej	{ .compat = "amlogic,meson-gxl-usb3-phy" },
611.2Sthorpej	DEVICE_COMPAT_EOL
621.1Sjmcneill};
631.1Sjmcneill
641.1Sjmcneillstruct mesongxl_usb3phy_softc {
651.1Sjmcneill	device_t		sc_dev;
661.1Sjmcneill	bus_space_tag_t		sc_bst;
671.1Sjmcneill	bus_space_handle_t	sc_bsh;
681.1Sjmcneill	int			sc_phandle;
691.1Sjmcneill	struct clk		*sc_clk_phy;
701.1Sjmcneill	struct clk		*sc_clk_peripheral;
711.1Sjmcneill};
721.1Sjmcneill
731.1Sjmcneill#define	PHY_READ(sc, reg)				\
741.1Sjmcneill	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
751.1Sjmcneill#define	PHY_WRITE(sc, reg, val)			\
761.1Sjmcneill	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
771.1Sjmcneill
781.1SjmcneillCFATTACH_DECL_NEW(mesongxl_usb3phy, sizeof(struct mesongxl_usb3phy_softc),
791.1Sjmcneill	mesongxl_usb3phy_match, mesongxl_usb3phy_attach, NULL, NULL);
801.1Sjmcneill
811.1Sjmcneillstatic void *
821.1Sjmcneillmesongxl_usb3phy_acquire(device_t dev, const void *data, size_t len)
831.1Sjmcneill{
841.1Sjmcneill	if (len != 0)
851.1Sjmcneill		return NULL;
861.1Sjmcneill
871.1Sjmcneill	return (void *)(uintptr_t)1;
881.1Sjmcneill}
891.1Sjmcneill
901.1Sjmcneillstatic void
911.1Sjmcneillmesongxl_usb3phy_release(device_t dev, void *priv)
921.1Sjmcneill{
931.1Sjmcneill}
941.1Sjmcneill
951.1Sjmcneillstatic int
961.1Sjmcneillmesongxl_usb3phy_enable(device_t dev, void *priv, bool enable)
971.1Sjmcneill{
981.1Sjmcneill	struct mesongxl_usb3phy_softc * const sc = device_private(dev);
991.1Sjmcneill	uint32_t val;
1001.1Sjmcneill
1011.1Sjmcneill	if (enable) {
1021.1Sjmcneill		/* Power on PHY */
1031.1Sjmcneill		val = PHY_READ(sc, USB3PHY_REG5);
1041.1Sjmcneill		val |= REG5_ID_DIG_EN_0;
1051.1Sjmcneill		val |= REG5_ID_DIG_EN_1;
1061.1Sjmcneill		val &= ~REG5_ID_DIG_TH;
1071.1Sjmcneill		val |= __SHIFTIN(0xff, REG5_ID_DIG_TH);
1081.1Sjmcneill		PHY_WRITE(sc, USB3PHY_REG5, val);
1091.1Sjmcneill
1101.1Sjmcneill		/* Set host mode */
1111.1Sjmcneill		val = PHY_READ(sc, USB3PHY_REG0);
1121.1Sjmcneill		val &= ~REG0_U2D_ACT;
1131.1Sjmcneill		PHY_WRITE(sc, USB3PHY_REG0, val);
1141.1Sjmcneill
1151.1Sjmcneill		val = PHY_READ(sc, USB3PHY_REG4);
1161.1Sjmcneill		val &= ~REG4_P21_SLEEP_M0;
1171.1Sjmcneill		PHY_WRITE(sc, USB3PHY_REG4, val);
1181.1Sjmcneill	} else {
1191.1Sjmcneill		/* Power off PHY */
1201.1Sjmcneill		val = PHY_READ(sc, USB3PHY_REG5);
1211.1Sjmcneill		val &= ~REG5_ID_DIG_EN_0;
1221.1Sjmcneill		val &= ~REG5_ID_DIG_EN_1;
1231.1Sjmcneill		PHY_WRITE(sc, USB3PHY_REG5, val);
1241.1Sjmcneill	}
1251.1Sjmcneill
1261.1Sjmcneill	return 0;
1271.1Sjmcneill}
1281.1Sjmcneill
1291.1Sjmcneillconst struct fdtbus_phy_controller_func mesongxl_usb3phy_funcs = {
1301.1Sjmcneill	.acquire = mesongxl_usb3phy_acquire,
1311.1Sjmcneill	.release = mesongxl_usb3phy_release,
1321.1Sjmcneill	.enable = mesongxl_usb3phy_enable,
1331.1Sjmcneill};
1341.1Sjmcneill
1351.1Sjmcneillstatic void
1361.1Sjmcneillmesongxl_usb3phy_init(struct mesongxl_usb3phy_softc *sc)
1371.1Sjmcneill{
1381.1Sjmcneill	uint32_t val;
1391.1Sjmcneill
1401.1Sjmcneill	val = PHY_READ(sc, USB3PHY_REG1);
1411.1Sjmcneill	val &= ~REG1_U3H_FLADJ_30MHZ_REG;
1421.1Sjmcneill	val |= __SHIFTIN(0x20, REG1_U3H_FLADJ_30MHZ_REG);
1431.1Sjmcneill	PHY_WRITE(sc, USB3PHY_REG1, val);
1441.1Sjmcneill}
1451.1Sjmcneill
1461.1Sjmcneillstatic int
1471.1Sjmcneillmesongxl_usb3phy_match(device_t parent, cfdata_t cf, void *aux)
1481.1Sjmcneill{
1491.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1501.1Sjmcneill
1511.2Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
1521.1Sjmcneill}
1531.1Sjmcneill
1541.1Sjmcneillstatic void
1551.1Sjmcneillmesongxl_usb3phy_attach(device_t parent, device_t self, void *aux)
1561.1Sjmcneill{
1571.1Sjmcneill	struct mesongxl_usb3phy_softc * const sc = device_private(self);
1581.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1591.1Sjmcneill	const int phandle = faa->faa_phandle;
1601.1Sjmcneill	struct fdtbus_regulator *supply;
1611.1Sjmcneill	struct fdtbus_reset *rst;
1621.1Sjmcneill	bus_addr_t addr;
1631.1Sjmcneill	bus_size_t size;
1641.1Sjmcneill	u_int n;
1651.1Sjmcneill
1661.1Sjmcneill	sc->sc_dev = self;
1671.1Sjmcneill	sc->sc_bst = faa->faa_bst;
1681.1Sjmcneill	sc->sc_phandle = phandle;
1691.1Sjmcneill
1701.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1711.1Sjmcneill		aprint_error(": couldn't get registers\n");
1721.1Sjmcneill		return;
1731.1Sjmcneill	}
1741.1Sjmcneill	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1751.1Sjmcneill		aprint_error(": couldn't map registers\n");
1761.1Sjmcneill		return;
1771.1Sjmcneill	}
1781.1Sjmcneill
1791.1Sjmcneill	sc->sc_clk_phy = fdtbus_clock_get(phandle, "phy");
1801.1Sjmcneill	if (sc->sc_clk_phy == NULL) {
1811.1Sjmcneill		aprint_error(": couldn't get phy clock\n");
1821.1Sjmcneill		return;
1831.1Sjmcneill	}
1841.1Sjmcneill#if notyet
1851.1Sjmcneill	sc->sc_clk_peripheral = fdtbus_clock_get(phandle, "peripheral");
1861.1Sjmcneill	if (sc->sc_clk_peripheral == NULL) {
1871.1Sjmcneill		aprint_error(": couldn't get peripheral clock\n");
1881.1Sjmcneill		return;
1891.1Sjmcneill	}
1901.1Sjmcneill#endif
1911.1Sjmcneill
1921.1Sjmcneill	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++) {
1931.1Sjmcneill		if (fdtbus_reset_deassert(rst) != 0) {
1941.1Sjmcneill			aprint_error(": couldn't de-assert reset #%d\n", n);
1951.1Sjmcneill			return;
1961.1Sjmcneill		}
1971.1Sjmcneill	}
1981.1Sjmcneill	if (clk_enable(sc->sc_clk_phy) != 0) {
1991.1Sjmcneill		aprint_error(": couldn't enable phy clock\n");
2001.1Sjmcneill		return;
2011.1Sjmcneill	}
2021.1Sjmcneill#if notyet
2031.1Sjmcneill	if (clk_enable(sc->sc_clk_peripheral) != 0) {
2041.1Sjmcneill		aprint_error(": couldn't enable peripheral clock\n");
2051.1Sjmcneill		return;
2061.1Sjmcneill	}
2071.1Sjmcneill#endif
2081.1Sjmcneill
2091.1Sjmcneill	supply = fdtbus_regulator_acquire(phandle, "phy-supply");
2101.1Sjmcneill	if (supply != NULL) {
2111.1Sjmcneill		if (fdtbus_regulator_enable(supply) != 0) {
2121.1Sjmcneill			aprint_error(": couldn't enable supply\n");
2131.1Sjmcneill			return;
2141.1Sjmcneill		}
2151.1Sjmcneill	}
2161.1Sjmcneill
2171.1Sjmcneill	aprint_naive("\n");
2181.1Sjmcneill	aprint_normal(": USB3 PHY\n");
2191.1Sjmcneill
2201.1Sjmcneill	mesongxl_usb3phy_init(sc);
2211.1Sjmcneill
2221.1Sjmcneill	fdtbus_register_phy_controller(self, phandle, &mesongxl_usb3phy_funcs);
2231.1Sjmcneill}
224