ohci-da8xx.c 14 KB

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