aw_usbphy.c revision 297627
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: head/sys/arm/allwinner/aw_usbphy.c 297627 2016-04-06 23:11:03Z jmcneill $ 27 */ 28 29/* 30 * Allwinner USB PHY 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/arm/allwinner/aw_usbphy.c 297627 2016-04-06 23:11:03Z jmcneill $"); 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 53static 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 { NULL, 0 } 59}; 60 61static int 62awusbphy_init(device_t dev) 63{ 64 char pname[20]; 65 int error, off; 66 regulator_t reg; 67 hwreset_t rst; 68 clk_t clk; 69 70 /* Enable clocks */ 71 for (off = 0; clk_get_by_ofw_index(dev, off, &clk) == 0; off++) { 72 error = clk_enable(clk); 73 if (error != 0) { 74 device_printf(dev, "couldn't enable clock %s\n", 75 clk_get_name(clk)); 76 return (error); 77 } 78 } 79 80 /* De-assert resets */ 81 for (off = 0; hwreset_get_by_ofw_idx(dev, off, &rst) == 0; off++) { 82 error = hwreset_deassert(rst); 83 if (error != 0) { 84 device_printf(dev, "couldn't de-assert reset %d\n", 85 off); 86 return (error); 87 } 88 } 89 90 /* Enable regulator(s) */ 91 for (off = 0; off < USBPHY_NUMOFF; off++) { 92 snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off); 93 if (regulator_get_by_ofw_property(dev, pname, ®) != 0) 94 continue; 95 error = regulator_enable(reg); 96 if (error != 0) { 97 device_printf(dev, "couldn't enable regulator %s\n", 98 pname); 99 return (error); 100 } 101 } 102 103 return (0); 104} 105 106static int 107awusbphy_probe(device_t dev) 108{ 109 if (!ofw_bus_status_okay(dev)) 110 return (ENXIO); 111 112 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 113 return (ENXIO); 114 115 device_set_desc(dev, "Allwinner USB PHY"); 116 return (BUS_PROBE_DEFAULT); 117} 118 119static int 120awusbphy_attach(device_t dev) 121{ 122 int error; 123 124 error = awusbphy_init(dev); 125 if (error) 126 device_printf(dev, "failed to initialize USB PHY, error %d\n", 127 error); 128 129 return (error); 130} 131 132static device_method_t awusbphy_methods[] = { 133 /* Device interface */ 134 DEVMETHOD(device_probe, awusbphy_probe), 135 DEVMETHOD(device_attach, awusbphy_attach), 136 137 DEVMETHOD_END 138}; 139 140static driver_t awusbphy_driver = { 141 "awusbphy", 142 awusbphy_methods, 143 0, 144}; 145 146static devclass_t awusbphy_devclass; 147 148DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass, 0, 0); 149MODULE_VERSION(awusbphy, 1); 150