1 /*- 2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 /* 29 * Allwinner USB PHY 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/bus.h> 38 #include <sys/rman.h> 39 #include <sys/kernel.h> 40 #include <sys/module.h> 41 #include <sys/gpio.h> 42 #include <machine/bus.h> 43 44 #include <dev/ofw/ofw_bus.h> 45 #include <dev/ofw/ofw_bus_subr.h> 46 #include <dev/gpio/gpiobusvar.h> 47 48 #include <dev/extres/clk/clk.h> 49 #include <dev/extres/hwreset/hwreset.h> 50 #include <dev/extres/regulator/regulator.h> 51 #include <dev/extres/phy/phy_usb.h> 52 53 #include "phynode_if.h" 54 55 enum awusbphy_type { 56 AWUSBPHY_TYPE_A10 = 1, 57 AWUSBPHY_TYPE_A13, 58 AWUSBPHY_TYPE_A20, 59 AWUSBPHY_TYPE_A31, 60 AWUSBPHY_TYPE_H3, 61 AWUSBPHY_TYPE_A64, 62 AWUSBPHY_TYPE_A83T, 63 AWUSBPHY_TYPE_H6, 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 const struct aw_usbphy_conf h6_usbphy_conf = { 123 .num_phys = 4, 124 .phy_type = AWUSBPHY_TYPE_H6, 125 .pmu_unk1 = false, 126 .phy0_route = true, 127 }; 128 129 static struct ofw_compat_data compat_data[] = { 130 { "allwinner,sun4i-a10-usb-phy", (uintptr_t)&a10_usbphy_conf }, 131 { "allwinner,sun5i-a13-usb-phy", (uintptr_t)&a13_usbphy_conf }, 132 { "allwinner,sun6i-a31-usb-phy", (uintptr_t)&a31_usbphy_conf }, 133 { "allwinner,sun7i-a20-usb-phy", (uintptr_t)&a20_usbphy_conf }, 134 { "allwinner,sun8i-h3-usb-phy", (uintptr_t)&h3_usbphy_conf }, 135 { "allwinner,sun50i-a64-usb-phy", (uintptr_t)&a64_usbphy_conf }, 136 { "allwinner,sun8i-a83t-usb-phy", (uintptr_t)&a83t_usbphy_conf }, 137 { "allwinner,sun50i-h6-usb-phy", (uintptr_t)&h6_usbphy_conf }, 138 { NULL, 0 } 139 }; 140 141 struct awusbphy_softc { 142 struct resource * phy_ctrl; 143 struct resource ** pmu; 144 regulator_t * reg; 145 gpio_pin_t id_det_pin; 146 int id_det_valid; 147 gpio_pin_t vbus_det_pin; 148 int vbus_det_valid; 149 struct aw_usbphy_conf *phy_conf; 150 int mode; 151 }; 152 153 /* Phy class and methods. */ 154 static int awusbphy_phy_enable(struct phynode *phy, bool enable); 155 static int awusbphy_get_mode(struct phynode *phy, int *mode); 156 static int awusbphy_set_mode(struct phynode *phy, int mode); 157 static phynode_usb_method_t awusbphy_phynode_methods[] = { 158 PHYNODEMETHOD(phynode_enable, awusbphy_phy_enable), 159 PHYNODEMETHOD(phynode_usb_get_mode, awusbphy_get_mode), 160 PHYNODEMETHOD(phynode_usb_set_mode, awusbphy_set_mode), 161 162 PHYNODEMETHOD_END 163 }; 164 DEFINE_CLASS_1(awusbphy_phynode, awusbphy_phynode_class, awusbphy_phynode_methods, 165 sizeof(struct phynode_usb_sc), phynode_usb_class); 166 167 #define RD4(res, o) bus_read_4(res, (o)) 168 #define WR4(res, o, v) bus_write_4(res, (o), (v)) 169 #define CLR4(res, o, m) WR4(res, o, RD4(res, o) & ~(m)) 170 #define SET4(res, o, m) WR4(res, o, RD4(res, o) | (m)) 171 172 #define PHY_CSR 0x00 173 #define ID_PULLUP_EN (1 << 17) 174 #define DPDM_PULLUP_EN (1 << 16) 175 #define FORCE_ID (0x3 << 14) 176 #define FORCE_ID_SHIFT 14 177 #define FORCE_ID_LOW 2 178 #define FORCE_VBUS_VALID (0x3 << 12) 179 #define FORCE_VBUS_VALID_SHIFT 12 180 #define FORCE_VBUS_VALID_HIGH 3 181 #define VBUS_CHANGE_DET (1 << 6) 182 #define ID_CHANGE_DET (1 << 5) 183 #define DPDM_CHANGE_DET (1 << 4) 184 #define OTG_PHY_CFG 0x20 185 #define OTG_PHY_ROUTE_OTG (1 << 0) 186 #define PMU_IRQ_ENABLE 0x00 187 #define PMU_AHB_INCR8 (1 << 10) 188 #define PMU_AHB_INCR4 (1 << 9) 189 #define PMU_AHB_INCRX_ALIGN (1 << 8) 190 #define PMU_ULPI_BYPASS (1 << 0) 191 #define PMU_UNK_H3 0x10 192 #define PMU_UNK_H3_CLR 0x2 193 #define PHY_CSR 0x00 194 #define ID_PULLUP_EN (1 << 17) 195 #define DPDM_PULLUP_EN (1 << 16) 196 #define FORCE_ID (0x3 << 14) 197 #define FORCE_ID_SHIFT 14 198 #define FORCE_ID_LOW 2 199 #define FORCE_VBUS_VALID (0x3 << 12) 200 #define FORCE_VBUS_VALID_SHIFT 12 201 #define FORCE_VBUS_VALID_HIGH 3 202 #define VBUS_CHANGE_DET (1 << 6) 203 #define ID_CHANGE_DET (1 << 5) 204 #define DPDM_CHANGE_DET (1 << 4) 205 206 static void 207 awusbphy_configure(device_t dev, int phyno) 208 { 209 struct awusbphy_softc *sc; 210 211 sc = device_get_softc(dev); 212 213 if (sc->pmu[phyno] == NULL) 214 return; 215 216 if (sc->phy_conf->pmu_unk1 == true) 217 CLR4(sc->pmu[phyno], PMU_UNK_H3, PMU_UNK_H3_CLR); 218 219 SET4(sc->pmu[phyno], PMU_IRQ_ENABLE, PMU_ULPI_BYPASS | 220 PMU_AHB_INCR8 | PMU_AHB_INCR4 | PMU_AHB_INCRX_ALIGN); 221 } 222 223 static int 224 awusbphy_init(device_t dev) 225 { 226 struct awusbphy_softc *sc; 227 phandle_t node; 228 char pname[20]; 229 uint32_t val; 230 int error, off, rid; 231 regulator_t reg; 232 hwreset_t rst; 233 clk_t clk; 234 235 sc = device_get_softc(dev); 236 node = ofw_bus_get_node(dev); 237 238 sc->phy_conf = (struct aw_usbphy_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 239 240 /* Get phy_ctrl region */ 241 if (ofw_bus_find_string_index(node, "reg-names", "phy_ctrl", &rid) != 0) { 242 device_printf(dev, "Cannot locate phy control resource\n"); 243 return (ENXIO); 244 } 245 sc->phy_ctrl = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 246 RF_ACTIVE); 247 if (sc->phy_ctrl == NULL) { 248 device_printf(dev, "Cannot allocate resource\n"); 249 return (ENXIO); 250 } 251 252 /* Enable clocks */ 253 for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) { 254 error = clk_enable(clk); 255 if (error != 0) { 256 device_printf(dev, "couldn't enable clock %s\n", 257 clk_get_name(clk)); 258 return (error); 259 } 260 } 261 262 /* De-assert resets */ 263 for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) { 264 error = hwreset_deassert(rst); 265 if (error != 0) { 266 device_printf(dev, "couldn't de-assert reset %d\n", 267 off); 268 return (error); 269 } 270 } 271 272 /* Get GPIOs */ 273 error = gpio_pin_get_by_ofw_property(dev, node, "usb0_id_det-gpios", 274 &sc->id_det_pin); 275 if (error == 0) 276 sc->id_det_valid = 1; 277 error = gpio_pin_get_by_ofw_property(dev, node, "usb0_vbus_det-gpios", 278 &sc->vbus_det_pin); 279 if (error == 0) 280 sc->vbus_det_valid = 1; 281 282 sc->reg = malloc(sizeof(*(sc->reg)) * sc->phy_conf->num_phys, M_DEVBUF, 283 M_WAITOK | M_ZERO); 284 sc->pmu = malloc(sizeof(*(sc->pmu)) * sc->phy_conf->num_phys, M_DEVBUF, 285 M_WAITOK | M_ZERO); 286 /* Get regulators */ 287 for (off = 0; off < sc->phy_conf->num_phys; off++) { 288 snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off); 289 if (regulator_get_by_ofw_property(dev, 0, pname, ®) == 0) 290 sc->reg[off] = reg; 291 292 snprintf(pname, sizeof(pname), "pmu%d", off); 293 if (ofw_bus_find_string_index(node, "reg-names", 294 pname, &rid) != 0) 295 continue; 296 297 sc->pmu[off] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 298 RF_ACTIVE); 299 if (sc->pmu[off] == NULL) { 300 device_printf(dev, "Cannot allocate resource\n"); 301 return (ENXIO); 302 } 303 } 304 305 /* Enable OTG PHY for host mode */ 306 val = bus_read_4(sc->phy_ctrl, PHY_CSR); 307 val &= ~(VBUS_CHANGE_DET | ID_CHANGE_DET | DPDM_CHANGE_DET); 308 val |= (ID_PULLUP_EN | DPDM_PULLUP_EN); 309 val &= ~FORCE_ID; 310 val |= (FORCE_ID_LOW << FORCE_ID_SHIFT); 311 val &= ~FORCE_VBUS_VALID; 312 val |= (FORCE_VBUS_VALID_HIGH << FORCE_VBUS_VALID_SHIFT); 313 bus_write_4(sc->phy_ctrl, PHY_CSR, val); 314 315 return (0); 316 } 317 318 static int 319 awusbphy_vbus_detect(device_t dev, int *val) 320 { 321 struct awusbphy_softc *sc; 322 bool active; 323 int error; 324 325 sc = device_get_softc(dev); 326 327 if (sc->vbus_det_valid) { 328 error = gpio_pin_is_active(sc->vbus_det_pin, &active); 329 if (error != 0) { 330 device_printf(dev, "Cannot get status of id pin %d\n", 331 error); 332 return (error); 333 } 334 *val = active; 335 return (0); 336 } 337 338 *val = 0; 339 return (0); 340 } 341 342 static int 343 awusbphy_phy_enable(struct phynode *phynode, bool enable) 344 { 345 device_t dev; 346 intptr_t phy; 347 struct awusbphy_softc *sc; 348 regulator_t reg; 349 int error, vbus_det; 350 351 dev = phynode_get_device(phynode); 352 phy = phynode_get_id(phynode); 353 sc = device_get_softc(dev); 354 355 if (phy < 0 || phy >= sc->phy_conf->num_phys) 356 return (ERANGE); 357 358 /* Configure PHY */ 359 awusbphy_configure(dev, phy); 360 361 /* Regulators are optional. If not found, return success. */ 362 reg = sc->reg[phy]; 363 if (reg == NULL) 364 return (0); 365 366 if (phy == 0) { 367 /* If an external vbus is detected, do not enable phy 0 */ 368 error = awusbphy_vbus_detect(dev, &vbus_det); 369 if (error) 370 goto out; 371 372 if (vbus_det == 1) { 373 if (bootverbose) 374 device_printf(dev, "External VBUS detected, not enabling the regulator\n"); 375 376 return (0); 377 } 378 } 379 if (enable) { 380 /* Depending on the PHY we need to route OTG to OHCI/EHCI */ 381 error = regulator_enable(reg); 382 } else 383 error = regulator_disable(reg); 384 385 out: 386 if (error != 0) { 387 device_printf(dev, 388 "couldn't %s regulator for phy %jd\n", 389 enable ? "enable" : "disable", (intmax_t)phy); 390 return (error); 391 } 392 393 return (0); 394 } 395 396 static int 397 awusbphy_get_mode(struct phynode *phynode, int *mode) 398 { 399 struct awusbphy_softc *sc; 400 device_t dev; 401 402 dev = phynode_get_device(phynode); 403 sc = device_get_softc(dev); 404 405 *mode = sc->mode; 406 407 return (0); 408 } 409 410 static int 411 awusbphy_set_mode(struct phynode *phynode, int mode) 412 { 413 device_t dev; 414 intptr_t phy; 415 struct awusbphy_softc *sc; 416 uint32_t val; 417 int error, vbus_det; 418 419 dev = phynode_get_device(phynode); 420 phy = phynode_get_id(phynode); 421 sc = device_get_softc(dev); 422 423 if (phy != 0) { 424 if (mode != PHY_USB_MODE_HOST) 425 return (EINVAL); 426 return (0); 427 } 428 429 switch (mode) { 430 case PHY_USB_MODE_HOST: 431 val = bus_read_4(sc->phy_ctrl, PHY_CSR); 432 val &= ~(VBUS_CHANGE_DET | ID_CHANGE_DET | DPDM_CHANGE_DET); 433 val |= (ID_PULLUP_EN | DPDM_PULLUP_EN); 434 val &= ~FORCE_ID; 435 val |= (FORCE_ID_LOW << FORCE_ID_SHIFT); 436 val &= ~FORCE_VBUS_VALID; 437 val |= (FORCE_VBUS_VALID_HIGH << FORCE_VBUS_VALID_SHIFT); 438 bus_write_4(sc->phy_ctrl, PHY_CSR, val); 439 if (sc->phy_conf->phy0_route == true) { 440 error = awusbphy_vbus_detect(dev, &vbus_det); 441 if (error) 442 goto out; 443 if (vbus_det == 0) 444 CLR4(sc->phy_ctrl, OTG_PHY_CFG, 445 OTG_PHY_ROUTE_OTG); 446 else 447 SET4(sc->phy_ctrl, OTG_PHY_CFG, 448 OTG_PHY_ROUTE_OTG); 449 } 450 break; 451 case PHY_USB_MODE_OTG: 452 /* TODO */ 453 break; 454 } 455 456 sc->mode = mode; 457 458 out: 459 return (0); 460 } 461 462 static int 463 awusbphy_probe(device_t dev) 464 { 465 if (!ofw_bus_status_okay(dev)) 466 return (ENXIO); 467 468 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 469 return (ENXIO); 470 471 device_set_desc(dev, "Allwinner USB PHY"); 472 return (BUS_PROBE_DEFAULT); 473 } 474 475 static int 476 awusbphy_attach(device_t dev) 477 { 478 int error; 479 struct phynode *phynode; 480 struct phynode_init_def phy_init; 481 struct awusbphy_softc *sc; 482 int i; 483 484 sc = device_get_softc(dev); 485 error = awusbphy_init(dev); 486 if (error) { 487 device_printf(dev, "failed to initialize USB PHY, error %d\n", 488 error); 489 return (error); 490 } 491 492 /* Create and register phys. */ 493 for (i = 0; i < sc->phy_conf->num_phys; i++) { 494 bzero(&phy_init, sizeof(phy_init)); 495 phy_init.id = i; 496 phy_init.ofw_node = ofw_bus_get_node(dev); 497 phynode = phynode_create(dev, &awusbphy_phynode_class, 498 &phy_init); 499 if (phynode == NULL) { 500 device_printf(dev, "failed to create USB PHY\n"); 501 return (ENXIO); 502 } 503 if (phynode_register(phynode) == NULL) { 504 device_printf(dev, "failed to create USB PHY\n"); 505 return (ENXIO); 506 } 507 } 508 509 return (error); 510 } 511 512 static device_method_t awusbphy_methods[] = { 513 /* Device interface */ 514 DEVMETHOD(device_probe, awusbphy_probe), 515 DEVMETHOD(device_attach, awusbphy_attach), 516 517 DEVMETHOD_END 518 }; 519 520 static driver_t awusbphy_driver = { 521 "awusbphy", 522 awusbphy_methods, 523 sizeof(struct awusbphy_softc) 524 }; 525 526 static devclass_t awusbphy_devclass; 527 /* aw_usbphy needs to come up after regulators/gpio/etc, but before ehci/ohci */ 528 EARLY_DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass, 529 0, 0, BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE); 530 MODULE_VERSION(awusbphy, 1); 531