ohci-da8xx.c 14 KB

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