1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2019 Michal Meloun <mmel@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bus.h> 34 #include <sys/gpio.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/malloc.h> 38 #include <sys/rman.h> 39 40 #include <machine/bus.h> 41 42 #include <dev/extres/hwreset/hwreset.h> 43 #include <dev/extres/phy/phy_usb.h> 44 #include <dev/extres/regulator/regulator.h> 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 48 #include <dev/fdt/simple_mfd.h> 49 #include "phynode_if.h" 50 #include "phynode_usb_if.h" 51 52 static struct ofw_compat_data compat_data[] = { 53 {"qcom,usb-ss-ipq4019-phy", 1}, 54 {NULL, 0}, 55 }; 56 57 struct ipq4018_usb_ss_phy_softc { 58 device_t dev; 59 }; 60 61 struct ipq4018_usb_ss_phynode_sc { 62 struct phynode_usb_sc usb_sc; 63 int mode; 64 hwreset_t por_rst; 65 }; 66 67 static int 68 ipq4018_usb_ss_phynode_phy_enable(struct phynode *phynode, bool enable) 69 { 70 struct ipq4018_usb_ss_phynode_sc *sc; 71 device_t dev; 72 int rv; 73 74 dev = phynode_get_device(phynode); 75 sc = phynode_get_softc(phynode); 76 77 /* 78 * For power-off - assert por, sleep for 10ms 79 */ 80 rv = hwreset_assert(sc->por_rst); 81 if (rv != 0) 82 goto done; 83 DELAY(10*1000); 84 85 /* 86 * For power-on - power off first, then deassert por. 87 */ 88 if (enable) { 89 rv = hwreset_deassert(sc->por_rst); 90 if (rv != 0) 91 goto done; 92 DELAY(10*1000); 93 } 94 95 done: 96 if (rv != 0) { 97 device_printf(dev, "%s: failed, rv=%d\n", __func__, rv); 98 } 99 return (rv); 100 } 101 102 /* Phy controller class and methods. */ 103 static phynode_method_t ipq4018_usb_ss_phynode_methods[] = { 104 PHYNODEUSBMETHOD(phynode_enable, ipq4018_usb_ss_phynode_phy_enable), 105 PHYNODEUSBMETHOD_END 106 }; 107 DEFINE_CLASS_1(ipq4018_usb_ss_phynode, ipq4018_usb_ss_phynode_class, 108 ipq4018_usb_ss_phynode_methods, 109 sizeof(struct ipq4018_usb_ss_phynode_sc), phynode_usb_class); 110 111 static int 112 ipq4018_usb_ss_usbphy_init_phy(struct ipq4018_usb_ss_phy_softc *sc, 113 phandle_t node) 114 { 115 struct phynode *phynode; 116 struct phynode_init_def phy_init; 117 struct ipq4018_usb_ss_phynode_sc *phy_sc; 118 int rv; 119 hwreset_t por_rst = NULL; 120 121 /* FDT resources */ 122 rv = hwreset_get_by_ofw_name(sc->dev, node, "por_rst", &por_rst); 123 if (rv != 0 && rv != ENOENT) { 124 device_printf(sc->dev, "Cannot get 'por_rst' reset\n"); 125 goto fail; 126 } 127 128 /* Create and register phy. */ 129 bzero(&phy_init, sizeof(phy_init)); 130 phy_init.id = 1; 131 phy_init.ofw_node = node; 132 phynode = phynode_create(sc->dev, &ipq4018_usb_ss_phynode_class, 133 &phy_init); 134 if (phynode == NULL) { 135 device_printf(sc->dev, "Cannot create phy.\n"); 136 return (ENXIO); 137 } 138 139 phy_sc = phynode_get_softc(phynode); 140 phy_sc->por_rst = por_rst; 141 142 if (phynode_register(phynode) == NULL) { 143 device_printf(sc->dev, "Cannot register phy.\n"); 144 return (ENXIO); 145 } 146 147 (void) ipq4018_usb_ss_phynode_phy_enable(phynode, true); 148 149 return (0); 150 151 fail: 152 if (por_rst != NULL) 153 hwreset_release(por_rst); 154 155 return (ENXIO); 156 } 157 158 static int 159 ipq4018_usb_ss_usbphy_probe(device_t dev) 160 { 161 162 if (!ofw_bus_status_okay(dev)) 163 return (ENXIO); 164 165 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 166 return (ENXIO); 167 168 device_set_desc(dev, "IPQ4018/IPQ4019 USB SS PHY"); 169 return (BUS_PROBE_DEFAULT); 170 } 171 172 static int 173 ipq4018_usb_ss_usbphy_attach(device_t dev) 174 { 175 struct ipq4018_usb_ss_phy_softc *sc; 176 phandle_t node; 177 int rv; 178 179 sc = device_get_softc(dev); 180 sc->dev = dev; 181 node = ofw_bus_get_node(sc->dev); 182 183 rv = ipq4018_usb_ss_usbphy_init_phy(sc, node); 184 if (rv != 0) 185 goto fail; 186 return (bus_generic_attach(dev)); 187 188 fail: 189 return (ENXIO); 190 } 191 192 static int 193 ipq4018_usb_ss_usbphy_detach(device_t dev) 194 { 195 196 return (0); 197 } 198 199 static device_method_t ipq4018_usb_ss_usbphy_methods[] = { 200 /* Device interface */ 201 DEVMETHOD(device_probe, ipq4018_usb_ss_usbphy_probe), 202 DEVMETHOD(device_attach, ipq4018_usb_ss_usbphy_attach), 203 DEVMETHOD(device_detach, ipq4018_usb_ss_usbphy_detach), 204 DEVMETHOD_END 205 }; 206 207 static DEFINE_CLASS_0(ipq4018_usb_ss_usbphy, ipq4018_usb_ss_usbphy_driver, 208 ipq4018_usb_ss_usbphy_methods, 209 sizeof(struct ipq4018_usb_ss_phy_softc)); 210 EARLY_DRIVER_MODULE(ipq4018_usb_ss_usbphy, simplebus, 211 ipq4018_usb_ss_usbphy_driver, NULL, NULL, 212 BUS_PASS_TIMER + BUS_PASS_ORDER_LAST); 213