ohci-da8xx.c 13 KB

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