1 /*- 2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * Allwinner USB PHY 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/bus.h> 39 #include <sys/rman.h> 40 #include <sys/kernel.h> 41 #include <sys/module.h> 42 #include <sys/gpio.h> 43 44 #include <dev/ofw/ofw_bus.h> 45 #include <dev/ofw/ofw_bus_subr.h> 46 47 #include <dev/extres/clk/clk.h> 48 #include <dev/extres/hwreset/hwreset.h> 49 #include <dev/extres/regulator/regulator.h> 50 51 #define USBPHY_NUMOFF 3 52 53 static struct ofw_compat_data compat_data[] = { 54 { "allwinner,sun4i-a10-usb-phy", 1 }, 55 { "allwinner,sun5i-a13-usb-phy", 1 }, 56 { "allwinner,sun6i-a31-usb-phy", 1 }, 57 { "allwinner,sun7i-a20-usb-phy", 1 }, 58 { "allwinner,sun8i-a83t-usb-phy", 1 }, 59 { "allwinner,sun8i-h3-usb-phy", 1 }, 60 { NULL, 0 } 61 }; 62 63 static int 64 awusbphy_init(device_t dev) 65 { 66 char pname[20]; 67 int error, off; 68 regulator_t reg; 69 hwreset_t rst; 70 clk_t clk; 71 72 /* Enable clocks */ 73 for (off = 0; clk_get_by_ofw_index(dev, off, &clk) == 0; off++) { 74 error = clk_enable(clk); 75 if (error != 0) { 76 device_printf(dev, "couldn't enable clock %s\n", 77 clk_get_name(clk)); 78 return (error); 79 } 80 } 81 82 /* De-assert resets */ 83 for (off = 0; hwreset_get_by_ofw_idx(dev, off, &rst) == 0; off++) { 84 error = hwreset_deassert(rst); 85 if (error != 0) { 86 device_printf(dev, "couldn't de-assert reset %d\n", 87 off); 88 return (error); 89 } 90 } 91 92 /* Enable regulator(s) */ 93 for (off = 0; off < USBPHY_NUMOFF; off++) { 94 snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off); 95 if (regulator_get_by_ofw_property(dev, pname, ®) != 0) 96 continue; 97 error = regulator_enable(reg); 98 if (error != 0) { 99 device_printf(dev, "couldn't enable regulator %s\n", 100 pname); 101 return (error); 102 } 103 } 104 105 return (0); 106 } 107 108 static int 109 awusbphy_probe(device_t dev) 110 { 111 if (!ofw_bus_status_okay(dev)) 112 return (ENXIO); 113 114 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 115 return (ENXIO); 116 117 device_set_desc(dev, "Allwinner USB PHY"); 118 return (BUS_PROBE_DEFAULT); 119 } 120 121 static int 122 awusbphy_attach(device_t dev) 123 { 124 int error; 125 126 error = awusbphy_init(dev); 127 if (error) 128 device_printf(dev, "failed to initialize USB PHY, error %d\n", 129 error); 130 131 return (error); 132 } 133 134 static device_method_t awusbphy_methods[] = { 135 /* Device interface */ 136 DEVMETHOD(device_probe, awusbphy_probe), 137 DEVMETHOD(device_attach, awusbphy_attach), 138 139 DEVMETHOD_END 140 }; 141 142 static driver_t awusbphy_driver = { 143 "awusbphy", 144 awusbphy_methods, 145 0, 146 }; 147 148 static devclass_t awusbphy_devclass; 149 150 DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass, 0, 0); 151 MODULE_VERSION(awusbphy, 1); 152