2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/gpio/gpiobusvar.h>
48 #include <dev/extres/clk/clk.h>
49 #include <dev/extres/hwreset/hwreset.h>
50 #include <dev/extres/regulator/regulator.h>
51 #include <dev/extres/phy/phy.h>
55 #define USBPHY_NPHYS 4
57 static struct ofw_compat_data compat_data[] = {
58 { "allwinner,sun4i-a10-usb-phy", 1 },
59 { "allwinner,sun5i-a13-usb-phy", 1 },
60 { "allwinner,sun6i-a31-usb-phy", 1 },
61 { "allwinner,sun7i-a20-usb-phy", 1 },
62 { "allwinner,sun8i-a83t-usb-phy", 1 },
63 { "allwinner,sun8i-h3-usb-phy", 1 },
67 struct awusbphy_softc {
68 regulator_t reg[USBPHY_NPHYS];
69 gpio_pin_t id_det_pin;
71 gpio_pin_t vbus_det_pin;
76 awusbphy_init(device_t dev)
78 struct awusbphy_softc *sc;
86 sc = device_get_softc(dev);
87 node = ofw_bus_get_node(dev);
90 for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) {
91 error = clk_enable(clk);
93 device_printf(dev, "couldn't enable clock %s\n",
99 /* De-assert resets */
100 for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) {
101 error = hwreset_deassert(rst);
103 device_printf(dev, "couldn't de-assert reset %d\n",
110 for (off = 0; off < USBPHY_NPHYS; off++) {
111 snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off);
112 if (regulator_get_by_ofw_property(dev, 0, pname, ®) == 0)
117 error = gpio_pin_get_by_ofw_property(dev, node, "usb0_id_det-gpios",
120 sc->id_det_valid = 1;
121 error = gpio_pin_get_by_ofw_property(dev, node, "usb0_vbus_det-gpios",
124 sc->vbus_det_valid = 1;
130 awusbphy_vbus_detect(device_t dev, int *val)
132 struct awusbphy_softc *sc;
136 sc = device_get_softc(dev);
138 if (sc->vbus_det_valid) {
139 error = gpio_pin_is_active(sc->vbus_det_pin, &active);
151 awusbphy_phy_enable(device_t dev, intptr_t phy, bool enable)
153 struct awusbphy_softc *sc;
157 if (phy < 0 || phy >= USBPHY_NPHYS)
160 sc = device_get_softc(dev);
162 /* Regulators are optional. If not found, return success. */
168 /* If an external vbus is detected, do not enable phy 0 */
170 error = awusbphy_vbus_detect(dev, &vbus_det);
171 if (error == 0 && vbus_det == 1)
176 error = regulator_enable(reg);
178 error = regulator_disable(reg);
181 "couldn't %s regulator for phy %jd\n",
182 enable ? "enable" : "disable", (intmax_t)phy);
190 awusbphy_probe(device_t dev)
192 if (!ofw_bus_status_okay(dev))
195 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
198 device_set_desc(dev, "Allwinner USB PHY");
199 return (BUS_PROBE_DEFAULT);
203 awusbphy_attach(device_t dev)
207 error = awusbphy_init(dev);
209 device_printf(dev, "failed to initialize USB PHY, error %d\n",
214 phy_register_provider(dev);
219 static device_method_t awusbphy_methods[] = {
220 /* Device interface */
221 DEVMETHOD(device_probe, awusbphy_probe),
222 DEVMETHOD(device_attach, awusbphy_attach),
225 DEVMETHOD(phy_enable, awusbphy_phy_enable),
230 static driver_t awusbphy_driver = {
233 sizeof(struct awusbphy_softc)
236 static devclass_t awusbphy_devclass;
238 EARLY_DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass,
239 0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
240 MODULE_VERSION(awusbphy, 1);