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