1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * OHCI HCD (Host Controller Driver) for USB. 4 * 5 * TI DA8xx (OMAP-L1x) Bus Glue 6 * 7 * Derived from: ohci-omap.c and ohci-s3c2410.c 8 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com> 9 */ 10 11 #include <linux/clk.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/io.h> 14 #include <linux/interrupt.h> 15 #include <linux/jiffies.h> 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/platform_device.h> 19 #include <linux/phy/phy.h> 20 #include <linux/platform_data/usb-davinci.h> 21 #include <linux/regulator/consumer.h> 22 #include <linux/usb.h> 23 #include <linux/usb/hcd.h> 24 #include <asm/unaligned.h> 25 26 #include "ohci.h" 27 28 #define DRIVER_DESC "DA8XX" 29 #define DRV_NAME "ohci-da8xx" 30 31 static struct hc_driver __read_mostly ohci_da8xx_hc_driver; 32 33 static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq, 34 u16 wValue, u16 wIndex, char *buf, u16 wLength); 35 static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf); 36 37 struct da8xx_ohci_hcd { 38 struct usb_hcd *hcd; 39 struct clk *usb11_clk; 40 struct phy *usb11_phy; 41 struct regulator *vbus_reg; 42 struct notifier_block nb; 43 unsigned int reg_enabled; 44 struct gpio_desc *vbus_gpio; 45 struct gpio_desc *oc_gpio; 46 }; 47 48 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv) 49 50 /* Over-current indicator change bitmask */ 51 static volatile u16 ocic_mask; 52 53 static int ohci_da8xx_enable(struct usb_hcd *hcd) 54 { 55 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 56 int ret; 57 58 ret = clk_prepare_enable(da8xx_ohci->usb11_clk); 59 if (ret) 60 return ret; 61 62 ret = phy_init(da8xx_ohci->usb11_phy); 63 if (ret) 64 goto err_phy_init; 65 66 ret = phy_power_on(da8xx_ohci->usb11_phy); 67 if (ret) 68 goto err_phy_power_on; 69 70 return 0; 71 72 err_phy_power_on: 73 phy_exit(da8xx_ohci->usb11_phy); 74 err_phy_init: 75 clk_disable_unprepare(da8xx_ohci->usb11_clk); 76 77 return ret; 78 } 79 80 static void ohci_da8xx_disable(struct usb_hcd *hcd) 81 { 82 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 83 84 phy_power_off(da8xx_ohci->usb11_phy); 85 phy_exit(da8xx_ohci->usb11_phy); 86 clk_disable_unprepare(da8xx_ohci->usb11_clk); 87 } 88 89 static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on) 90 { 91 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 92 struct device *dev = hcd->self.controller; 93 int ret; 94 95 if (da8xx_ohci->vbus_gpio) { 96 gpiod_set_value_cansleep(da8xx_ohci->vbus_gpio, on); 97 return 0; 98 } 99 100 if (!da8xx_ohci->vbus_reg) 101 return 0; 102 103 if (on && !da8xx_ohci->reg_enabled) { 104 ret = regulator_enable(da8xx_ohci->vbus_reg); 105 if (ret) { 106 dev_err(dev, "Failed to enable regulator: %d\n", ret); 107 return ret; 108 } 109 da8xx_ohci->reg_enabled = 1; 110 111 } else if (!on && da8xx_ohci->reg_enabled) { 112 ret = regulator_disable(da8xx_ohci->vbus_reg); 113 if (ret) { 114 dev_err(dev, "Failed to disable regulator: %d\n", ret); 115 return ret; 116 } 117 da8xx_ohci->reg_enabled = 0; 118 } 119 120 return 0; 121 } 122 123 static int ohci_da8xx_get_power(struct usb_hcd *hcd) 124 { 125 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 126 127 if (da8xx_ohci->vbus_gpio) 128 return gpiod_get_value_cansleep(da8xx_ohci->vbus_gpio); 129 130 if (da8xx_ohci->vbus_reg) 131 return regulator_is_enabled(da8xx_ohci->vbus_reg); 132 133 return 1; 134 } 135 136 static int ohci_da8xx_get_oci(struct usb_hcd *hcd) 137 { 138 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 139 unsigned int flags; 140 int ret; 141 142 if (da8xx_ohci->oc_gpio) 143 return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio); 144 145 if (!da8xx_ohci->vbus_reg) 146 return 0; 147 148 ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags); 149 if (ret) 150 return ret; 151 152 if (flags & REGULATOR_ERROR_OVER_CURRENT) 153 return 1; 154 155 return 0; 156 } 157 158 static int ohci_da8xx_has_set_power(struct usb_hcd *hcd) 159 { 160 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 161 162 if (da8xx_ohci->vbus_gpio) 163 return 1; 164 165 if (da8xx_ohci->vbus_reg) 166 return 1; 167 168 return 0; 169 } 170 171 static int ohci_da8xx_has_oci(struct usb_hcd *hcd) 172 { 173 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 174 175 if (da8xx_ohci->oc_gpio) 176 return 1; 177 178 if (da8xx_ohci->vbus_reg) 179 return 1; 180 181 return 0; 182 } 183 184 static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd) 185 { 186 struct device *dev = hcd->self.controller; 187 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev); 188 189 if (hub && hub->potpgt) 190 return 1; 191 192 return 0; 193 } 194 195 static int ohci_da8xx_regulator_event(struct notifier_block *nb, 196 unsigned long event, void *data) 197 { 198 struct da8xx_ohci_hcd *da8xx_ohci = 199 container_of(nb, struct da8xx_ohci_hcd, nb); 200 201 if (event & REGULATOR_EVENT_OVER_CURRENT) { 202 ocic_mask |= 1 << 1; 203 ohci_da8xx_set_power(da8xx_ohci->hcd, 0); 204 } 205 206 return 0; 207 } 208 209 static irqreturn_t ohci_da8xx_oc_handler(int irq, void *data) 210 { 211 struct da8xx_ohci_hcd *da8xx_ohci = data; 212 213 if (gpiod_get_value(da8xx_ohci->oc_gpio)) 214 gpiod_set_value(da8xx_ohci->vbus_gpio, 0); 215 216 return IRQ_HANDLED; 217 } 218 219 static int ohci_da8xx_register_notify(struct usb_hcd *hcd) 220 { 221 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd); 222 struct device *dev = hcd->self.controller; 223 int ret = 0; 224 225 if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) { 226 da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event; 227 ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg, 228 &da8xx_ohci->nb); 229 } 230 231 if (ret) 232 dev_err(dev, "Failed to register notifier: %d\n", ret); 233 234 return ret; 235 } 236 237 static int ohci_da8xx_reset(struct usb_hcd *hcd) 238 { 239 struct device *dev = hcd->self.controller; 240 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev); 241 struct ohci_hcd *ohci = hcd_to_ohci(hcd); 242 int result; 243 u32 rh_a; 244 245 dev_dbg(dev, "starting USB controller\n"); 246 247 result = ohci_da8xx_enable(hcd); 248 if (result < 0) 249 return result; 250 251 /* 252 * DA8xx only have 1 port connected to the pins but the HC root hub 253 * register A reports 2 ports, thus we'll have to override it... 254 */ 255 ohci->num_ports = 1; 256 257 result = ohci_setup(hcd); 258 if (result < 0) { 259 ohci_da8xx_disable(hcd); 260 return result; 261 } 262 263 /* 264 * Since we're providing a board-specific root hub port power control 265 * and over-current reporting, we have to override the HC root hub A 266 * register's default value, so that ohci_hub_control() could return 267 * the correct hub descriptor... 268 */ 269 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a); 270 if (ohci_da8xx_has_set_power(hcd)) { 271 rh_a &= ~RH_A_NPS; 272 rh_a |= RH_A_PSM; 273 } 274 if (ohci_da8xx_has_oci(hcd)) { 275 rh_a &= ~RH_A_NOCP; 276 rh_a |= RH_A_OCPM; 277 } 278 if (ohci_da8xx_has_potpgt(hcd)) { 279 rh_a &= ~RH_A_POTPGT; 280 rh_a |= hub->potpgt << 24; 281 } 282 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a); 283 284 return result; 285 } 286 287 /* 288 * Update the status data from the hub with the over-current indicator change. 289 */ 290 static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf) 291 { 292 int length = orig_ohci_hub_status_data(hcd, buf); 293 294 /* See if we have OCIC bit set on port 1 */ 295 if (ocic_mask & (1 << 1)) { 296 dev_dbg(hcd->self.controller, "over-current indicator change " 297 "on port 1\n"); 298 299 if (!length) 300 length = 1; 301 302 buf[0] |= 1 << 1; 303 } 304 return length; 305 } 306 307 /* 308 * Look at the control requests to the root hub and see if we need to override. 309 */ 310 static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, 311 u16 wIndex, char *buf, u16 wLength) 312 { 313 struct device *dev = hcd->self.controller; 314 int temp; 315 316 switch (typeReq) { 317 case GetPortStatus: 318 /* Check the port number */ 319 if (wIndex != 1) 320 break; 321 322 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex); 323 324 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1); 325 326 /* The port power status (PPS) bit defaults to 1 */ 327 if (!ohci_da8xx_get_power(hcd)) 328 temp &= ~RH_PS_PPS; 329 330 /* The port over-current indicator (POCI) bit is always 0 */ 331 if (ohci_da8xx_get_oci(hcd) > 0) 332 temp |= RH_PS_POCI; 333 334 /* The over-current indicator change (OCIC) bit is 0 too */ 335 if (ocic_mask & (1 << wIndex)) 336 temp |= RH_PS_OCIC; 337 338 put_unaligned(cpu_to_le32(temp), (__le32 *)buf); 339 return 0; 340 case SetPortFeature: 341 temp = 1; 342 goto check_port; 343 case ClearPortFeature: 344 temp = 0; 345 346 check_port: 347 /* Check the port number */ 348 if (wIndex != 1) 349 break; 350 351 switch (wValue) { 352 case USB_PORT_FEAT_POWER: 353 dev_dbg(dev, "%sPortFeature(%u): %s\n", 354 temp ? "Set" : "Clear", wIndex, "POWER"); 355 356 return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0; 357 case USB_PORT_FEAT_C_OVER_CURRENT: 358 dev_dbg(dev, "%sPortFeature(%u): %s\n", 359 temp ? "Set" : "Clear", wIndex, 360 "C_OVER_CURRENT"); 361 362 if (temp) 363 ocic_mask |= 1 << wIndex; 364 else 365 ocic_mask &= ~(1 << wIndex); 366 return 0; 367 } 368 } 369 370 return orig_ohci_hub_control(hcd, typeReq, wValue, 371 wIndex, buf, wLength); 372 } 373 374 /*-------------------------------------------------------------------------*/ 375 #ifdef CONFIG_OF 376 static const struct of_device_id da8xx_ohci_ids[] = { 377 { .compatible = "ti,da830-ohci" }, 378 { } 379 }; 380 MODULE_DEVICE_TABLE(of, da8xx_ohci_ids); 381 #endif 382 383 static int ohci_da8xx_probe(struct platform_device *pdev) 384 { 385 struct da8xx_ohci_hcd *da8xx_ohci; 386 struct device *dev = &pdev->dev; 387 int error, hcd_irq, oc_irq; 388 struct usb_hcd *hcd; 389 struct resource *mem; 390 391 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev)); 392 if (!hcd) 393 return -ENOMEM; 394 395 da8xx_ohci = to_da8xx_ohci(hcd); 396 da8xx_ohci->hcd = hcd; 397 398 da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL); 399 if (IS_ERR(da8xx_ohci->usb11_clk)) { 400 error = PTR_ERR(da8xx_ohci->usb11_clk); 401 if (error != -EPROBE_DEFER) 402 dev_err(dev, "Failed to get clock.\n"); 403 goto err; 404 } 405 406 da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy"); 407 if (IS_ERR(da8xx_ohci->usb11_phy)) { 408 error = PTR_ERR(da8xx_ohci->usb11_phy); 409 if (error != -EPROBE_DEFER) 410 dev_err(dev, "Failed to get phy.\n"); 411 goto err; 412 } 413 414 da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus"); 415 if (IS_ERR(da8xx_ohci->vbus_reg)) { 416 error = PTR_ERR(da8xx_ohci->vbus_reg); 417 if (error == -ENODEV) { 418 da8xx_ohci->vbus_reg = NULL; 419 } else if (error == -EPROBE_DEFER) { 420 goto err; 421 } else { 422 dev_err(dev, "Failed to get regulator\n"); 423 goto err; 424 } 425 } 426 427 da8xx_ohci->vbus_gpio = devm_gpiod_get_optional(dev, "vbus", 428 GPIOD_OUT_HIGH); 429 if (IS_ERR(da8xx_ohci->vbus_gpio)) 430 goto err; 431 432 da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN); 433 if (IS_ERR(da8xx_ohci->oc_gpio)) 434 goto err; 435 436 if (da8xx_ohci->oc_gpio) { 437 oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio); 438 if (oc_irq < 0) 439 goto err; 440 441 error = devm_request_irq(dev, oc_irq, ohci_da8xx_oc_handler, 442 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 443 "OHCI over-current indicator", da8xx_ohci); 444 if (error) 445 goto err; 446 } 447 448 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 449 hcd->regs = devm_ioremap_resource(dev, mem); 450 if (IS_ERR(hcd->regs)) { 451 error = PTR_ERR(hcd->regs); 452 goto err; 453 } 454 hcd->rsrc_start = mem->start; 455 hcd->rsrc_len = resource_size(mem); 456 457 hcd_irq = platform_get_irq(pdev, 0); 458 if (hcd_irq < 0) { 459 error = -ENODEV; 460 goto err; 461 } 462 463 error = usb_add_hcd(hcd, hcd_irq, 0); 464 if (error) 465 goto err; 466 467 device_wakeup_enable(hcd->self.controller); 468 469 error = ohci_da8xx_register_notify(hcd); 470 if (error) 471 goto err_remove_hcd; 472 473 return 0; 474 475 err_remove_hcd: 476 usb_remove_hcd(hcd); 477 err: 478 usb_put_hcd(hcd); 479 return error; 480 } 481 482 static int ohci_da8xx_remove(struct platform_device *pdev) 483 { 484 struct usb_hcd *hcd = platform_get_drvdata(pdev); 485 486 usb_remove_hcd(hcd); 487 usb_put_hcd(hcd); 488 489 return 0; 490 } 491 492 #ifdef CONFIG_PM 493 static int ohci_da8xx_suspend(struct platform_device *pdev, 494 pm_message_t message) 495 { 496 struct usb_hcd *hcd = platform_get_drvdata(pdev); 497 struct ohci_hcd *ohci = hcd_to_ohci(hcd); 498 bool do_wakeup = device_may_wakeup(&pdev->dev); 499 int ret; 500 501 502 if (time_before(jiffies, ohci->next_statechange)) 503 msleep(5); 504 ohci->next_statechange = jiffies; 505 506 ret = ohci_suspend(hcd, do_wakeup); 507 if (ret) 508 return ret; 509 510 ohci_da8xx_disable(hcd); 511 hcd->state = HC_STATE_SUSPENDED; 512 513 return ret; 514 } 515 516 static int ohci_da8xx_resume(struct platform_device *dev) 517 { 518 struct usb_hcd *hcd = platform_get_drvdata(dev); 519 struct ohci_hcd *ohci = hcd_to_ohci(hcd); 520 int ret; 521 522 if (time_before(jiffies, ohci->next_statechange)) 523 msleep(5); 524 ohci->next_statechange = jiffies; 525 526 ret = ohci_da8xx_enable(hcd); 527 if (ret) 528 return ret; 529 530 ohci_resume(hcd, false); 531 532 return 0; 533 } 534 #endif 535 536 static const struct ohci_driver_overrides da8xx_overrides __initconst = { 537 .reset = ohci_da8xx_reset, 538 .extra_priv_size = sizeof(struct da8xx_ohci_hcd), 539 }; 540 541 /* 542 * Driver definition to register with platform structure. 543 */ 544 static struct platform_driver ohci_hcd_da8xx_driver = { 545 .probe = ohci_da8xx_probe, 546 .remove = ohci_da8xx_remove, 547 .shutdown = usb_hcd_platform_shutdown, 548 #ifdef CONFIG_PM 549 .suspend = ohci_da8xx_suspend, 550 .resume = ohci_da8xx_resume, 551 #endif 552 .driver = { 553 .name = DRV_NAME, 554 .of_match_table = of_match_ptr(da8xx_ohci_ids), 555 }, 556 }; 557 558 static int __init ohci_da8xx_init(void) 559 { 560 561 if (usb_disabled()) 562 return -ENODEV; 563 564 pr_info("%s: " DRIVER_DESC "\n", DRV_NAME); 565 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides); 566 567 /* 568 * The Davinci da8xx HW has some unusual quirks, which require 569 * da8xx-specific workarounds. We override certain hc_driver 570 * functions here to achieve that. We explicitly do not enhance 571 * ohci_driver_overrides to allow this more easily, since this 572 * is an unusual case, and we don't want to encourage others to 573 * override these functions by making it too easy. 574 */ 575 576 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control; 577 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data; 578 579 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data; 580 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control; 581 582 return platform_driver_register(&ohci_hcd_da8xx_driver); 583 } 584 module_init(ohci_da8xx_init); 585 586 static void __exit ohci_da8xx_exit(void) 587 { 588 platform_driver_unregister(&ohci_hcd_da8xx_driver); 589 } 590 module_exit(ohci_da8xx_exit); 591 MODULE_DESCRIPTION(DRIVER_DESC); 592 MODULE_LICENSE("GPL"); 593 MODULE_ALIAS("platform:" DRV_NAME); 594