usbnopphy.c revision 1.2
11.2Sthorpej/* $NetBSD: usbnopphy.c,v 1.2 2021/01/27 03:10:21 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: usbnopphy.c,v 1.2 2021/01/27 03:10:21 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.1Sjmcneillstatic int usbnopphy_match(device_t, cfdata_t, void *);
431.1Sjmcneillstatic void usbnopphy_attach(device_t, device_t, void *);
441.1Sjmcneill
451.2Sthorpejstatic const struct device_compatible_entry compat_data[] = {
461.2Sthorpej	{ .compat = "usb-nop-xceiv" },
471.2Sthorpej	DEVICE_COMPAT_EOL
481.1Sjmcneill};
491.1Sjmcneill
501.1Sjmcneillstruct usbnopphy_softc {
511.1Sjmcneill	device_t		sc_dev;
521.1Sjmcneill
531.1Sjmcneill	struct clk		*sc_clk;
541.1Sjmcneill	struct fdtbus_regulator *sc_reg;
551.1Sjmcneill	struct fdtbus_gpio_pin	*sc_pin_reset;
561.1Sjmcneill	struct fdtbus_gpio_pin	*sc_pin_vbus_det;
571.1Sjmcneill};
581.1Sjmcneill
591.1SjmcneillCFATTACH_DECL_NEW(usbnopphy, sizeof(struct usbnopphy_softc),
601.1Sjmcneill	usbnopphy_match, usbnopphy_attach, NULL, NULL);
611.1Sjmcneill
621.1Sjmcneillstatic void *
631.1Sjmcneillusbnopphy_acquire(device_t dev, const void *data, size_t len)
641.1Sjmcneill{
651.1Sjmcneill	struct usbnopphy_softc * const sc = device_private(dev);
661.1Sjmcneill
671.1Sjmcneill	return sc;
681.1Sjmcneill}
691.1Sjmcneill
701.1Sjmcneillstatic void
711.1Sjmcneillusbnopphy_release(device_t dev, void *priv)
721.1Sjmcneill{
731.1Sjmcneill}
741.1Sjmcneill
751.1Sjmcneillstatic int
761.1Sjmcneillusbnopphy_enable(device_t dev, void *priv, bool enable)
771.1Sjmcneill{
781.1Sjmcneill	struct usbnopphy_softc * const sc = device_private(dev);
791.1Sjmcneill	int error;
801.1Sjmcneill
811.1Sjmcneill	if (enable) {
821.1Sjmcneill		if (sc->sc_reg != NULL) {
831.1Sjmcneill			error = fdtbus_regulator_enable(sc->sc_reg);
841.1Sjmcneill			if (error != 0)
851.1Sjmcneill				return error;
861.1Sjmcneill		}
871.1Sjmcneill		if (sc->sc_clk != NULL) {
881.1Sjmcneill			error = clk_enable(sc->sc_clk);
891.1Sjmcneill			if (error != 0)
901.1Sjmcneill				return error;
911.1Sjmcneill		}
921.1Sjmcneill		if (sc->sc_pin_reset != NULL) {
931.1Sjmcneill			fdtbus_gpio_write(sc->sc_pin_reset, 1);
941.1Sjmcneill			delay(20000);
951.1Sjmcneill			fdtbus_gpio_write(sc->sc_pin_reset, 0);
961.1Sjmcneill		}
971.1Sjmcneill	} else {
981.1Sjmcneill		if (sc->sc_pin_reset != NULL)
991.1Sjmcneill			fdtbus_gpio_write(sc->sc_pin_reset, 1);
1001.1Sjmcneill		if (sc->sc_reg != NULL)
1011.1Sjmcneill			fdtbus_regulator_disable(sc->sc_reg);
1021.1Sjmcneill		if (sc->sc_clk != NULL)
1031.1Sjmcneill			clk_disable(sc->sc_clk);
1041.1Sjmcneill	}
1051.1Sjmcneill
1061.1Sjmcneill	return 0;
1071.1Sjmcneill}
1081.1Sjmcneill
1091.1Sjmcneillconst struct fdtbus_phy_controller_func usbnopphy_funcs = {
1101.1Sjmcneill	.acquire = usbnopphy_acquire,
1111.1Sjmcneill	.release = usbnopphy_release,
1121.1Sjmcneill	.enable = usbnopphy_enable,
1131.1Sjmcneill};
1141.1Sjmcneill
1151.1Sjmcneillstatic int
1161.1Sjmcneillusbnopphy_match(device_t parent, cfdata_t cf, void *aux)
1171.1Sjmcneill{
1181.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1191.1Sjmcneill
1201.2Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
1211.1Sjmcneill}
1221.1Sjmcneill
1231.1Sjmcneillstatic void
1241.1Sjmcneillusbnopphy_attach(device_t parent, device_t self, void *aux)
1251.1Sjmcneill{
1261.1Sjmcneill	struct usbnopphy_softc * const sc = device_private(self);
1271.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1281.1Sjmcneill	const int phandle = faa->faa_phandle;
1291.1Sjmcneill
1301.1Sjmcneill	sc->sc_dev = self;
1311.1Sjmcneill
1321.1Sjmcneill	sc->sc_reg = fdtbus_regulator_acquire(phandle, "vbus-regulator");
1331.1Sjmcneill	sc->sc_clk = fdtbus_clock_get(phandle, "main_clk");
1341.1Sjmcneill	sc->sc_pin_reset = fdtbus_gpio_acquire(phandle, "reset-gpios", GPIO_PIN_OUTPUT);
1351.1Sjmcneill
1361.1Sjmcneill	aprint_naive("\n");
1371.1Sjmcneill	aprint_normal(": USB PHY\n");
1381.1Sjmcneill
1391.1Sjmcneill	fdtbus_register_phy_controller(self, phandle, &usbnopphy_funcs);
1401.1Sjmcneill}
141