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>
53 #include "phynode_if.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;
75 /* Phy class and methods. */
76 static int awusbphy_phy_enable(struct phynode *phy, bool enable);
77 static phynode_method_t awusbphy_phynode_methods[] = {
78 PHYNODEMETHOD(phynode_enable, awusbphy_phy_enable),
82 DEFINE_CLASS_1(awusbphy_phynode, awusbphy_phynode_class, awusbphy_phynode_methods,
86 awusbphy_init(device_t dev)
88 struct awusbphy_softc *sc;
96 sc = device_get_softc(dev);
97 node = ofw_bus_get_node(dev);
100 for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) {
101 error = clk_enable(clk);
103 device_printf(dev, "couldn't enable clock %s\n",
109 /* De-assert resets */
110 for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) {
111 error = hwreset_deassert(rst);
113 device_printf(dev, "couldn't de-assert reset %d\n",
120 for (off = 0; off < USBPHY_NPHYS; off++) {
121 snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off);
122 if (regulator_get_by_ofw_property(dev, 0, pname, ®) == 0)
127 error = gpio_pin_get_by_ofw_property(dev, node, "usb0_id_det-gpios",
130 sc->id_det_valid = 1;
131 error = gpio_pin_get_by_ofw_property(dev, node, "usb0_vbus_det-gpios",
134 sc->vbus_det_valid = 1;
140 awusbphy_vbus_detect(device_t dev, int *val)
142 struct awusbphy_softc *sc;
146 sc = device_get_softc(dev);
148 if (sc->vbus_det_valid) {
149 error = gpio_pin_is_active(sc->vbus_det_pin, &active);
161 awusbphy_phy_enable(struct phynode *phynode, bool enable)
165 struct awusbphy_softc *sc;
169 dev = phynode_get_device(phynode);
170 phy = phynode_get_id(phynode);
171 sc = device_get_softc(dev);
173 if (phy < 0 || phy >= USBPHY_NPHYS)
176 sc = device_get_softc(dev);
178 /* Regulators are optional. If not found, return success. */
184 /* If an external vbus is detected, do not enable phy 0 */
186 error = awusbphy_vbus_detect(dev, &vbus_det);
187 if (error == 0 && vbus_det == 1)
192 error = regulator_enable(reg);
194 error = regulator_disable(reg);
197 "couldn't %s regulator for phy %jd\n",
198 enable ? "enable" : "disable", (intmax_t)phy);
206 awusbphy_probe(device_t dev)
208 if (!ofw_bus_status_okay(dev))
211 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
214 device_set_desc(dev, "Allwinner USB PHY");
215 return (BUS_PROBE_DEFAULT);
219 awusbphy_attach(device_t dev)
222 struct phynode *phynode;
223 struct phynode_init_def phy_init;
226 error = awusbphy_init(dev);
228 device_printf(dev, "failed to initialize USB PHY, error %d\n",
233 /* Create and register phys. */
234 for (i = 0; i < USBPHY_NPHYS; i++) {
235 bzero(&phy_init, sizeof(phy_init));
237 phy_init.ofw_node = ofw_bus_get_node(dev);
238 phynode = phynode_create(dev, &awusbphy_phynode_class,
240 if (phynode == NULL) {
241 device_printf(dev, "failed to create USB PHY\n");
244 if (phynode_register(phynode) == NULL) {
245 device_printf(dev, "failed to create USB PHY\n");
253 static device_method_t awusbphy_methods[] = {
254 /* Device interface */
255 DEVMETHOD(device_probe, awusbphy_probe),
256 DEVMETHOD(device_attach, awusbphy_attach),
261 static driver_t awusbphy_driver = {
264 sizeof(struct awusbphy_softc)
267 static devclass_t awusbphy_devclass;
269 EARLY_DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass,
270 0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
271 MODULE_VERSION(awusbphy, 1);