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 #include <machine/bus.h> 44 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 #include <dev/gpio/gpiobusvar.h> 48 49 #include <dev/extres/clk/clk.h> 50 #include <dev/extres/hwreset/hwreset.h> 51 #include <dev/extres/regulator/regulator.h> 52 #include <dev/extres/phy/phy.h> 53 54 #include "phy_if.h" 55 56 enum awusbphy_type { 57 AWUSBPHY_TYPE_A10 = 1, 58 AWUSBPHY_TYPE_A13, 59 AWUSBPHY_TYPE_A20, 60 AWUSBPHY_TYPE_A31, 61 AWUSBPHY_TYPE_H3, 62 AWUSBPHY_TYPE_A64 63 }; 64 65 struct aw_usbphy_conf { 66 int num_phys; 67 enum awusbphy_type phy_type; 68 bool pmu_unk1; 69 bool phy0_route; 70 }; 71 72 static const struct aw_usbphy_conf a10_usbphy_conf = { 73 .num_phys = 3, 74 .phy_type = AWUSBPHY_TYPE_A10, 75 .pmu_unk1 = false, 76 .phy0_route = false, 77 }; 78 79 static const struct aw_usbphy_conf a13_usbphy_conf = { 80 .num_phys = 2, 81 .phy_type = AWUSBPHY_TYPE_A13, 82 .pmu_unk1 = false, 83 .phy0_route = false, 84 }; 85 86 static const struct aw_usbphy_conf a20_usbphy_conf = { 87 .num_phys = 3, 88 .phy_type = AWUSBPHY_TYPE_A20, 89 .pmu_unk1 = false, 90 .phy0_route = false, 91 }; 92 93 static const struct aw_usbphy_conf a31_usbphy_conf = { 94 .num_phys = 3, 95 .phy_type = AWUSBPHY_TYPE_A31, 96 .pmu_unk1 = false, 97 .phy0_route = false, 98 }; 99 100 static const struct aw_usbphy_conf h3_usbphy_conf = { 101 .num_phys = 4, 102 .phy_type = AWUSBPHY_TYPE_H3, 103 .pmu_unk1 = true, 104 .phy0_route = false, 105 }; 106 107 static const struct aw_usbphy_conf a64_usbphy_conf = { 108 .num_phys = 2, 109 .phy_type = AWUSBPHY_TYPE_A64, 110 .pmu_unk1 = true, 111 .phy0_route = true, 112 }; 113 114 static struct ofw_compat_data compat_data[] = { 115 { "allwinner,sun4i-a10-usb-phy", (uintptr_t)&a10_usbphy_conf }, 116 { "allwinner,sun5i-a13-usb-phy", (uintptr_t)&a13_usbphy_conf }, 117 { "allwinner,sun6i-a31-usb-phy", (uintptr_t)&a31_usbphy_conf }, 118 { "allwinner,sun7i-a20-usb-phy", (uintptr_t)&a20_usbphy_conf }, 119 { "allwinner,sun8i-h3-usb-phy", (uintptr_t)&h3_usbphy_conf }, 120 { "allwinner,sun50i-a64-usb-phy", (uintptr_t)&a64_usbphy_conf }, 121 { NULL, 0 } 122 }; 123 124 struct awusbphy_softc { 125 struct resource * phy_ctrl; 126 struct resource ** pmu; 127 regulator_t * reg; 128 gpio_pin_t id_det_pin; 129 int id_det_valid; 130 gpio_pin_t vbus_det_pin; 131 int vbus_det_valid; 132 struct aw_usbphy_conf *phy_conf; 133 }; 134 135 #define RD4(res, o) bus_read_4(res, (o)) 136 #define WR4(res, o, v) bus_write_4(res, (o), (v)) 137 #define CLR4(res, o, m) WR4(res, o, RD4(res, o) & ~(m)) 138 #define SET4(res, o, m) WR4(res, o, RD4(res, o) | (m)) 139 140 #define OTG_PHY_CFG 0x20 141 #define OTG_PHY_ROUTE_OTG (1 << 0) 142 #define PMU_IRQ_ENABLE 0x00 143 #define PMU_AHB_INCR8 (1 << 10) 144 #define PMU_AHB_INCR4 (1 << 9) 145 #define PMU_AHB_INCRX_ALIGN (1 << 8) 146 #define PMU_ULPI_BYPASS (1 << 0) 147 #define PMU_UNK_H3 0x10 148 #define PMU_UNK_H3_CLR 0x2 149 150 static void 151 awusbphy_configure(device_t dev, int phyno) 152 { 153 struct awusbphy_softc *sc; 154 155 sc = device_get_softc(dev); 156 157 if (sc->pmu[phyno] == NULL) 158 return; 159 160 if (sc->phy_conf->pmu_unk1 == true) 161 CLR4(sc->pmu[phyno], PMU_UNK_H3, PMU_UNK_H3_CLR); 162 163 SET4(sc->pmu[phyno], PMU_IRQ_ENABLE, PMU_ULPI_BYPASS | 164 PMU_AHB_INCR8 | PMU_AHB_INCR4 | PMU_AHB_INCRX_ALIGN); 165 } 166 167 static int 168 awusbphy_init(device_t dev) 169 { 170 struct awusbphy_softc *sc; 171 phandle_t node; 172 char pname[20]; 173 int error, off, rid; 174 regulator_t reg; 175 hwreset_t rst; 176 clk_t clk; 177 178 sc = device_get_softc(dev); 179 node = ofw_bus_get_node(dev); 180 181 sc->phy_conf = (struct aw_usbphy_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 182 183 /* Get phy_ctrl region */ 184 if (ofw_bus_find_string_index(node, "reg-names", "phy_ctrl", &rid) != 0) { 185 device_printf(dev, "Cannot locate phy control resource\n"); 186 return (ENXIO); 187 } 188 sc->phy_ctrl = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 189 RF_ACTIVE); 190 if (sc->phy_ctrl == NULL) { 191 device_printf(dev, "Cannot allocate resource\n"); 192 return (ENXIO); 193 } 194 195 /* Enable clocks */ 196 for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) { 197 error = clk_enable(clk); 198 if (error != 0) { 199 device_printf(dev, "couldn't enable clock %s\n", 200 clk_get_name(clk)); 201 return (error); 202 } 203 } 204 205 /* De-assert resets */ 206 for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) { 207 error = hwreset_deassert(rst); 208 if (error != 0) { 209 device_printf(dev, "couldn't de-assert reset %d\n", 210 off); 211 return (error); 212 } 213 } 214 215 /* Get GPIOs */ 216 error = gpio_pin_get_by_ofw_property(dev, node, "usb0_id_det-gpios", 217 &sc->id_det_pin); 218 if (error == 0) 219 sc->id_det_valid = 1; 220 error = gpio_pin_get_by_ofw_property(dev, node, "usb0_vbus_det-gpios", 221 &sc->vbus_det_pin); 222 if (error == 0) 223 sc->vbus_det_valid = 1; 224 225 sc->reg = malloc(sizeof(*(sc->reg)) * sc->phy_conf->num_phys, M_DEVBUF, 226 M_WAITOK | M_ZERO); 227 sc->pmu = malloc(sizeof(*(sc->pmu)) * sc->phy_conf->num_phys, M_DEVBUF, 228 M_WAITOK | M_ZERO); 229 /* Get regulators */ 230 for (off = 0; off < sc->phy_conf->num_phys; off++) { 231 snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off); 232 if (regulator_get_by_ofw_property(dev, 0, pname, ®) == 0) 233 sc->reg[off] = reg; 234 235 snprintf(pname, sizeof(pname), "pmu%d", off); 236 if (ofw_bus_find_string_index(node, "reg-names", 237 pname, &rid) != 0) 238 continue; 239 240 sc->pmu[off] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 241 RF_ACTIVE); 242 if (sc->pmu[off] == NULL) { 243 device_printf(dev, "Cannot allocate resource\n"); 244 return (ENXIO); 245 } 246 } 247 248 return (0); 249 } 250 251 static int 252 awusbphy_vbus_detect(device_t dev, int *val) 253 { 254 struct awusbphy_softc *sc; 255 bool active; 256 int error; 257 258 sc = device_get_softc(dev); 259 260 if (sc->vbus_det_valid) { 261 error = gpio_pin_is_active(sc->vbus_det_pin, &active); 262 if (error != 0) { 263 device_printf(dev, "Cannot get status of id pin %d\n", 264 error); 265 return (error); 266 } 267 *val = active; 268 return (0); 269 } 270 271 *val = 1; 272 return (0); 273 } 274 275 static int 276 awusbphy_phy_enable(device_t dev, intptr_t phy, bool enable) 277 { 278 struct awusbphy_softc *sc; 279 regulator_t reg; 280 int error, vbus_det; 281 282 sc = device_get_softc(dev); 283 284 if (phy < 0 || phy >= sc->phy_conf->num_phys) 285 return (ERANGE); 286 287 /* Configure PHY */ 288 awusbphy_configure(dev, phy); 289 290 /* Regulators are optional. If not found, return success. */ 291 reg = sc->reg[phy]; 292 if (reg == NULL) 293 return (0); 294 295 if (enable) { 296 /* If an external vbus is detected, do not enable phy 0 */ 297 if (phy == 0) { 298 error = awusbphy_vbus_detect(dev, &vbus_det); 299 if (error) 300 goto out; 301 302 /* Depending on the PHY we need to route OTG to OHCI/EHCI */ 303 if (sc->phy_conf->phy0_route == true) { 304 if (vbus_det == 0) 305 /* Host mode */ 306 CLR4(sc->phy_ctrl, OTG_PHY_CFG, 307 OTG_PHY_ROUTE_OTG); 308 else 309 /* Peripheral mode */ 310 SET4(sc->phy_ctrl, OTG_PHY_CFG, 311 OTG_PHY_ROUTE_OTG); 312 } 313 if (vbus_det == 1) 314 return (0); 315 } else 316 error = 0; 317 if (error == 0) 318 error = regulator_enable(reg); 319 } else 320 error = regulator_disable(reg); 321 322 out: 323 if (error != 0) { 324 device_printf(dev, 325 "couldn't %s regulator for phy %jd\n", 326 enable ? "enable" : "disable", (intmax_t)phy); 327 return (error); 328 } 329 330 return (0); 331 } 332 333 static int 334 awusbphy_probe(device_t dev) 335 { 336 if (!ofw_bus_status_okay(dev)) 337 return (ENXIO); 338 339 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 340 return (ENXIO); 341 342 device_set_desc(dev, "Allwinner USB PHY"); 343 return (BUS_PROBE_DEFAULT); 344 } 345 346 static int 347 awusbphy_attach(device_t dev) 348 { 349 int error; 350 351 error = awusbphy_init(dev); 352 if (error) { 353 device_printf(dev, "failed to initialize USB PHY, error %d\n", 354 error); 355 return (error); 356 } 357 358 phy_register_provider(dev); 359 360 return (error); 361 } 362 363 static device_method_t awusbphy_methods[] = { 364 /* Device interface */ 365 DEVMETHOD(device_probe, awusbphy_probe), 366 DEVMETHOD(device_attach, awusbphy_attach), 367 368 /* PHY interface */ 369 DEVMETHOD(phy_enable, awusbphy_phy_enable), 370 371 DEVMETHOD_END 372 }; 373 374 static driver_t awusbphy_driver = { 375 "awusbphy", 376 awusbphy_methods, 377 sizeof(struct awusbphy_softc) 378 }; 379 380 static devclass_t awusbphy_devclass; 381 382 EARLY_DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass, 383 0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE); 384 MODULE_VERSION(awusbphy, 1); 385