ohci-da8xx.c 13 KB

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