11.1Sjmcneill/* $NetBSD: rk3288_usb.c,v 1.1 2021/11/12 22:02:08 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2021 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.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: rk3288_usb.c,v 1.1 2021/11/12 22:02:08 jmcneill 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#include <sys/kmem.h>
401.1Sjmcneill
411.1Sjmcneill#include <dev/fdt/fdtvar.h>
421.1Sjmcneill#include <dev/fdt/syscon.h>
431.1Sjmcneill
441.1Sjmcneill#define	GRF_UOCn_CON0_SIDDQ	__BIT(13)
451.1Sjmcneill
461.1Sjmcneillstatic int rk3288_usb_match(device_t, cfdata_t, void *);
471.1Sjmcneillstatic void rk3288_usb_attach(device_t, device_t, void *);
481.1Sjmcneill
491.1Sjmcneillstatic const struct device_compatible_entry compat_data[] = {
501.1Sjmcneill	{ .compat = "rockchip,rk3288-usb-phy" },
511.1Sjmcneill	DEVICE_COMPAT_EOL
521.1Sjmcneill};
531.1Sjmcneill
541.1SjmcneillCFATTACH_DECL_NEW(rk3288_usb, 0,
551.1Sjmcneill	rk3288_usb_match, rk3288_usb_attach, NULL, NULL);
561.1Sjmcneill
571.1Sjmcneillstatic int
581.1Sjmcneillrk3288_usb_match(device_t parent, cfdata_t cf, void *aux)
591.1Sjmcneill{
601.1Sjmcneill	struct fdt_attach_args * const faa = aux;
611.1Sjmcneill
621.1Sjmcneill	return of_compatible_match(faa->faa_phandle, compat_data);
631.1Sjmcneill}
641.1Sjmcneill
651.1Sjmcneillstatic void
661.1Sjmcneillrk3288_usb_attach(device_t parent, device_t self, void *aux)
671.1Sjmcneill{
681.1Sjmcneill	struct fdt_attach_args * const faa = aux;
691.1Sjmcneill	const int phandle = faa->faa_phandle;
701.1Sjmcneill	int child;
711.1Sjmcneill
721.1Sjmcneill	aprint_naive("\n");
731.1Sjmcneill	aprint_normal(": USB PHY\n");
741.1Sjmcneill
751.1Sjmcneill	for (child = OF_child(phandle); child; child = OF_peer(child)) {
761.1Sjmcneill		if (!fdtbus_status_okay(child))
771.1Sjmcneill			continue;
781.1Sjmcneill
791.1Sjmcneill		struct fdt_attach_args cfaa = *faa;
801.1Sjmcneill		cfaa.faa_phandle = child;
811.1Sjmcneill		cfaa.faa_name = fdtbus_get_string(child, "name");
821.1Sjmcneill		cfaa.faa_quiet = false;
831.1Sjmcneill
841.1Sjmcneill		config_found(self, &cfaa, NULL, CFARGS_NONE);
851.1Sjmcneill	}
861.1Sjmcneill}
871.1Sjmcneill
881.1Sjmcneill/*
891.1Sjmcneill * USB PHY
901.1Sjmcneill */
911.1Sjmcneill
921.1Sjmcneillstatic int rk3288_usbphy_match(device_t, cfdata_t, void *);
931.1Sjmcneillstatic void rk3288_usbphy_attach(device_t, device_t, void *);
941.1Sjmcneill
951.1Sjmcneillstruct rk3288_usbphy_softc {
961.1Sjmcneill	device_t	sc_dev;
971.1Sjmcneill	int		sc_phandle;
981.1Sjmcneill	struct syscon	*sc_syscon;
991.1Sjmcneill	bus_addr_t	sc_reg;
1001.1Sjmcneill};
1011.1Sjmcneill
1021.1SjmcneillCFATTACH_DECL_NEW(rk3288_usbphy, sizeof(struct rk3288_usbphy_softc),
1031.1Sjmcneill	rk3288_usbphy_match, rk3288_usbphy_attach, NULL, NULL);
1041.1Sjmcneill
1051.1Sjmcneillstatic void *
1061.1Sjmcneillrk3288_usbphy_acquire(device_t dev, const void *data, size_t len)
1071.1Sjmcneill{
1081.1Sjmcneill	struct rk3288_usbphy_softc * const sc = device_private(dev);
1091.1Sjmcneill
1101.1Sjmcneill	if (len != 0)
1111.1Sjmcneill		return NULL;
1121.1Sjmcneill
1131.1Sjmcneill	return sc;
1141.1Sjmcneill}
1151.1Sjmcneill
1161.1Sjmcneillstatic void
1171.1Sjmcneillrk3288_usbphy_release(device_t dev, void *priv)
1181.1Sjmcneill{
1191.1Sjmcneill}
1201.1Sjmcneill
1211.1Sjmcneillstatic int
1221.1Sjmcneillrk3288_usbphy_enable(device_t dev, void *priv, bool enable)
1231.1Sjmcneill{
1241.1Sjmcneill	struct rk3288_usbphy_softc * const sc = device_private(dev);
1251.1Sjmcneill	uint32_t write_mask, write_val;
1261.1Sjmcneill
1271.1Sjmcneill	write_mask = GRF_UOCn_CON0_SIDDQ << 16;
1281.1Sjmcneill	write_val = enable ? 0 : GRF_UOCn_CON0_SIDDQ;
1291.1Sjmcneill
1301.1Sjmcneill	syscon_lock(sc->sc_syscon);
1311.1Sjmcneill	syscon_write_4(sc->sc_syscon, sc->sc_reg, write_mask | write_val);
1321.1Sjmcneill	syscon_unlock(sc->sc_syscon);
1331.1Sjmcneill
1341.1Sjmcneill	return 0;
1351.1Sjmcneill}
1361.1Sjmcneill
1371.1Sjmcneillconst struct fdtbus_phy_controller_func rk3288_usbphy_funcs = {
1381.1Sjmcneill	.acquire = rk3288_usbphy_acquire,
1391.1Sjmcneill	.release = rk3288_usbphy_release,
1401.1Sjmcneill	.enable = rk3288_usbphy_enable,
1411.1Sjmcneill};
1421.1Sjmcneill
1431.1Sjmcneillstatic int
1441.1Sjmcneillrk3288_usbphy_match(device_t parent, cfdata_t cf, void *aux)
1451.1Sjmcneill{
1461.1Sjmcneill	return 1;
1471.1Sjmcneill}
1481.1Sjmcneill
1491.1Sjmcneillstatic void
1501.1Sjmcneillrk3288_usbphy_attach(device_t parent, device_t self, void *aux)
1511.1Sjmcneill{
1521.1Sjmcneill	struct rk3288_usbphy_softc * const sc = device_private(self);
1531.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1541.1Sjmcneill	const int phandle = faa->faa_phandle;
1551.1Sjmcneill	struct fdtbus_reset *rst;
1561.1Sjmcneill
1571.1Sjmcneill	sc->sc_dev = self;
1581.1Sjmcneill	sc->sc_phandle = phandle;
1591.1Sjmcneill	sc->sc_syscon = fdtbus_syscon_lookup(OF_parent(OF_parent(phandle)));
1601.1Sjmcneill	if (sc->sc_syscon == NULL) {
1611.1Sjmcneill		aprint_error(": couldn't find syscon\n");
1621.1Sjmcneill		return;
1631.1Sjmcneill	}
1641.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &sc->sc_reg, NULL) != 0) {
1651.1Sjmcneill		aprint_error(": couldn't find registers\n");
1661.1Sjmcneill		return;
1671.1Sjmcneill	}
1681.1Sjmcneill
1691.1Sjmcneill	rst = fdtbus_reset_get(phandle, "phy-reset");
1701.1Sjmcneill	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
1711.1Sjmcneill		aprint_error(": couldn't de-assert reset\n");
1721.1Sjmcneill		return;
1731.1Sjmcneill	}
1741.1Sjmcneill	if (fdtbus_clock_enable(phandle, "phyclk", true) != 0) {
1751.1Sjmcneill		aprint_error(": couildn't enable clock\n");
1761.1Sjmcneill		return;
1771.1Sjmcneill	}
1781.1Sjmcneill
1791.1Sjmcneill	aprint_naive("\n");
1801.1Sjmcneill	aprint_normal(": USB port\n");
1811.1Sjmcneill
1821.1Sjmcneill	fdtbus_register_phy_controller(self, phandle, &rk3288_usbphy_funcs);
1831.1Sjmcneill}
184