ehci-tegra.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * EHCI-compliant USB host controller driver for NVIDIA Tegra SoCs
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. * Copyright (C) 2009 - 2013 NVIDIA Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/clk/tegra.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/err.h>
  22. #include <linux/gpio.h>
  23. #include <linux/io.h>
  24. #include <linux/irq.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/slab.h>
  31. #include <linux/usb/ehci_def.h>
  32. #include <linux/usb/tegra_usb_phy.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/hcd.h>
  35. #include <linux/usb/otg.h>
  36. #include "ehci.h"
  37. #define TEGRA_USB_BASE 0xC5000000
  38. #define TEGRA_USB2_BASE 0xC5004000
  39. #define TEGRA_USB3_BASE 0xC5008000
  40. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  41. #define TEGRA_USB_DMA_ALIGN 32
  42. #define DRIVER_DESC "Tegra EHCI driver"
  43. #define DRV_NAME "tegra-ehci"
  44. static struct hc_driver __read_mostly tegra_ehci_hc_driver;
  45. static int (*orig_hub_control)(struct usb_hcd *hcd,
  46. u16 typeReq, u16 wValue, u16 wIndex,
  47. char *buf, u16 wLength);
  48. struct tegra_ehci_hcd {
  49. struct tegra_usb_phy *phy;
  50. struct clk *clk;
  51. int port_resuming;
  52. bool needs_double_reset;
  53. enum tegra_usb_phy_port_speed port_speed;
  54. };
  55. static int tegra_ehci_internal_port_reset(
  56. struct ehci_hcd *ehci,
  57. u32 __iomem *portsc_reg
  58. )
  59. {
  60. u32 temp;
  61. unsigned long flags;
  62. int retval = 0;
  63. int i, tries;
  64. u32 saved_usbintr;
  65. spin_lock_irqsave(&ehci->lock, flags);
  66. saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
  67. /* disable USB interrupt */
  68. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  69. spin_unlock_irqrestore(&ehci->lock, flags);
  70. /*
  71. * Here we have to do Port Reset at most twice for
  72. * Port Enable bit to be set.
  73. */
  74. for (i = 0; i < 2; i++) {
  75. temp = ehci_readl(ehci, portsc_reg);
  76. temp |= PORT_RESET;
  77. ehci_writel(ehci, temp, portsc_reg);
  78. mdelay(10);
  79. temp &= ~PORT_RESET;
  80. ehci_writel(ehci, temp, portsc_reg);
  81. mdelay(1);
  82. tries = 100;
  83. do {
  84. mdelay(1);
  85. /*
  86. * Up to this point, Port Enable bit is
  87. * expected to be set after 2 ms waiting.
  88. * USB1 usually takes extra 45 ms, for safety,
  89. * we take 100 ms as timeout.
  90. */
  91. temp = ehci_readl(ehci, portsc_reg);
  92. } while (!(temp & PORT_PE) && tries--);
  93. if (temp & PORT_PE)
  94. break;
  95. }
  96. if (i == 2)
  97. retval = -ETIMEDOUT;
  98. /*
  99. * Clear Connect Status Change bit if it's set.
  100. * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
  101. */
  102. if (temp & PORT_CSC)
  103. ehci_writel(ehci, PORT_CSC, portsc_reg);
  104. /*
  105. * Write to clear any interrupt status bits that might be set
  106. * during port reset.
  107. */
  108. temp = ehci_readl(ehci, &ehci->regs->status);
  109. ehci_writel(ehci, temp, &ehci->regs->status);
  110. /* restore original interrupt enable bits */
  111. ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
  112. return retval;
  113. }
  114. static int tegra_ehci_hub_control(
  115. struct usb_hcd *hcd,
  116. u16 typeReq,
  117. u16 wValue,
  118. u16 wIndex,
  119. char *buf,
  120. u16 wLength
  121. )
  122. {
  123. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  124. struct tegra_ehci_hcd *tegra = (struct tegra_ehci_hcd *)ehci->priv;
  125. u32 __iomem *status_reg;
  126. u32 temp;
  127. unsigned long flags;
  128. int retval = 0;
  129. status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
  130. spin_lock_irqsave(&ehci->lock, flags);
  131. if (typeReq == GetPortStatus) {
  132. temp = ehci_readl(ehci, status_reg);
  133. if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
  134. /* Resume completed, re-enable disconnect detection */
  135. tegra->port_resuming = 0;
  136. tegra_usb_phy_postresume(hcd->phy);
  137. }
  138. }
  139. else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
  140. temp = ehci_readl(ehci, status_reg);
  141. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
  142. retval = -EPIPE;
  143. goto done;
  144. }
  145. temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
  146. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  147. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  148. /*
  149. * If a transaction is in progress, there may be a delay in
  150. * suspending the port. Poll until the port is suspended.
  151. */
  152. if (ehci_handshake(ehci, status_reg, PORT_SUSPEND,
  153. PORT_SUSPEND, 5000))
  154. pr_err("%s: timeout waiting for SUSPEND\n", __func__);
  155. set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
  156. goto done;
  157. }
  158. /* For USB1 port we need to issue Port Reset twice internally */
  159. if (tegra->needs_double_reset &&
  160. (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
  161. spin_unlock_irqrestore(&ehci->lock, flags);
  162. return tegra_ehci_internal_port_reset(ehci, status_reg);
  163. }
  164. /*
  165. * Tegra host controller will time the resume operation to clear the bit
  166. * when the port control state switches to HS or FS Idle. This behavior
  167. * is different from EHCI where the host controller driver is required
  168. * to set this bit to a zero after the resume duration is timed in the
  169. * driver.
  170. */
  171. else if (typeReq == ClearPortFeature &&
  172. wValue == USB_PORT_FEAT_SUSPEND) {
  173. temp = ehci_readl(ehci, status_reg);
  174. if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
  175. retval = -EPIPE;
  176. goto done;
  177. }
  178. if (!(temp & PORT_SUSPEND))
  179. goto done;
  180. /* Disable disconnect detection during port resume */
  181. tegra_usb_phy_preresume(hcd->phy);
  182. ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
  183. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  184. /* start resume signalling */
  185. ehci_writel(ehci, temp | PORT_RESUME, status_reg);
  186. set_bit(wIndex-1, &ehci->resuming_ports);
  187. spin_unlock_irqrestore(&ehci->lock, flags);
  188. msleep(20);
  189. spin_lock_irqsave(&ehci->lock, flags);
  190. /* Poll until the controller clears RESUME and SUSPEND */
  191. if (ehci_handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
  192. pr_err("%s: timeout waiting for RESUME\n", __func__);
  193. if (ehci_handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
  194. pr_err("%s: timeout waiting for SUSPEND\n", __func__);
  195. ehci->reset_done[wIndex-1] = 0;
  196. clear_bit(wIndex-1, &ehci->resuming_ports);
  197. tegra->port_resuming = 1;
  198. goto done;
  199. }
  200. spin_unlock_irqrestore(&ehci->lock, flags);
  201. /* Handle the hub control events here */
  202. return orig_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  203. done:
  204. spin_unlock_irqrestore(&ehci->lock, flags);
  205. return retval;
  206. }
  207. struct dma_aligned_buffer {
  208. void *kmalloc_ptr;
  209. void *old_xfer_buffer;
  210. u8 data[0];
  211. };
  212. static void free_dma_aligned_buffer(struct urb *urb)
  213. {
  214. struct dma_aligned_buffer *temp;
  215. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  216. return;
  217. temp = container_of(urb->transfer_buffer,
  218. struct dma_aligned_buffer, data);
  219. if (usb_urb_dir_in(urb))
  220. memcpy(temp->old_xfer_buffer, temp->data,
  221. urb->transfer_buffer_length);
  222. urb->transfer_buffer = temp->old_xfer_buffer;
  223. kfree(temp->kmalloc_ptr);
  224. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  225. }
  226. static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  227. {
  228. struct dma_aligned_buffer *temp, *kmalloc_ptr;
  229. size_t kmalloc_size;
  230. if (urb->num_sgs || urb->sg ||
  231. urb->transfer_buffer_length == 0 ||
  232. !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
  233. return 0;
  234. /* Allocate a buffer with enough padding for alignment */
  235. kmalloc_size = urb->transfer_buffer_length +
  236. sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1;
  237. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  238. if (!kmalloc_ptr)
  239. return -ENOMEM;
  240. /* Position our struct dma_aligned_buffer such that data is aligned */
  241. temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
  242. temp->kmalloc_ptr = kmalloc_ptr;
  243. temp->old_xfer_buffer = urb->transfer_buffer;
  244. if (usb_urb_dir_out(urb))
  245. memcpy(temp->data, urb->transfer_buffer,
  246. urb->transfer_buffer_length);
  247. urb->transfer_buffer = temp->data;
  248. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  249. return 0;
  250. }
  251. static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  252. gfp_t mem_flags)
  253. {
  254. int ret;
  255. ret = alloc_dma_aligned_buffer(urb, mem_flags);
  256. if (ret)
  257. return ret;
  258. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  259. if (ret)
  260. free_dma_aligned_buffer(urb);
  261. return ret;
  262. }
  263. static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  264. {
  265. usb_hcd_unmap_urb_for_dma(hcd, urb);
  266. free_dma_aligned_buffer(urb);
  267. }
  268. static int tegra_ehci_probe(struct platform_device *pdev)
  269. {
  270. struct resource *res;
  271. struct usb_hcd *hcd;
  272. struct ehci_hcd *ehci;
  273. struct tegra_ehci_hcd *tegra;
  274. int err = 0;
  275. int irq;
  276. struct usb_phy *u_phy;
  277. /* Right now device-tree probed devices don't get dma_mask set.
  278. * Since shared usb code relies on it, set it here for now.
  279. * Once we have dma capability bindings this can go away.
  280. */
  281. if (!pdev->dev.dma_mask)
  282. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  283. if (!pdev->dev.coherent_dma_mask)
  284. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  285. hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
  286. dev_name(&pdev->dev));
  287. if (!hcd) {
  288. dev_err(&pdev->dev, "Unable to create HCD\n");
  289. return -ENOMEM;
  290. }
  291. platform_set_drvdata(pdev, hcd);
  292. ehci = hcd_to_ehci(hcd);
  293. tegra = (struct tegra_ehci_hcd *)ehci->priv;
  294. hcd->has_tt = 1;
  295. tegra->clk = devm_clk_get(&pdev->dev, NULL);
  296. if (IS_ERR(tegra->clk)) {
  297. dev_err(&pdev->dev, "Can't get ehci clock\n");
  298. err = PTR_ERR(tegra->clk);
  299. goto cleanup_hcd_create;
  300. }
  301. err = clk_prepare_enable(tegra->clk);
  302. if (err)
  303. goto cleanup_clk_get;
  304. tegra_periph_reset_assert(tegra->clk);
  305. udelay(1);
  306. tegra_periph_reset_deassert(tegra->clk);
  307. u_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "nvidia,phy", 0);
  308. if (IS_ERR(u_phy)) {
  309. err = PTR_ERR(u_phy);
  310. goto cleanup_clk_en;
  311. }
  312. hcd->phy = u_phy;
  313. tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
  314. "nvidia,needs-double-reset");
  315. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  316. if (!res) {
  317. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  318. err = -ENXIO;
  319. goto cleanup_clk_en;
  320. }
  321. hcd->rsrc_start = res->start;
  322. hcd->rsrc_len = resource_size(res);
  323. hcd->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  324. if (!hcd->regs) {
  325. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  326. err = -ENOMEM;
  327. goto cleanup_clk_en;
  328. }
  329. ehci->caps = hcd->regs + 0x100;
  330. err = usb_phy_init(hcd->phy);
  331. if (err) {
  332. dev_err(&pdev->dev, "Failed to initialize phy\n");
  333. goto cleanup_clk_en;
  334. }
  335. u_phy->otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  336. GFP_KERNEL);
  337. if (!u_phy->otg) {
  338. dev_err(&pdev->dev, "Failed to alloc memory for otg\n");
  339. err = -ENOMEM;
  340. goto cleanup_phy;
  341. }
  342. u_phy->otg->host = hcd_to_bus(hcd);
  343. err = usb_phy_set_suspend(hcd->phy, 0);
  344. if (err) {
  345. dev_err(&pdev->dev, "Failed to power on the phy\n");
  346. goto cleanup_phy;
  347. }
  348. irq = platform_get_irq(pdev, 0);
  349. if (!irq) {
  350. dev_err(&pdev->dev, "Failed to get IRQ\n");
  351. err = -ENODEV;
  352. goto cleanup_phy;
  353. }
  354. otg_set_host(u_phy->otg, &hcd->self);
  355. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  356. if (err) {
  357. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  358. goto cleanup_otg_set_host;
  359. }
  360. return err;
  361. cleanup_otg_set_host:
  362. otg_set_host(u_phy->otg, NULL);
  363. cleanup_phy:
  364. usb_phy_shutdown(hcd->phy);
  365. cleanup_clk_en:
  366. clk_disable_unprepare(tegra->clk);
  367. cleanup_clk_get:
  368. clk_put(tegra->clk);
  369. cleanup_hcd_create:
  370. usb_put_hcd(hcd);
  371. return err;
  372. }
  373. static int tegra_ehci_remove(struct platform_device *pdev)
  374. {
  375. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  376. struct tegra_ehci_hcd *tegra =
  377. (struct tegra_ehci_hcd *)hcd_to_ehci(hcd)->priv;
  378. otg_set_host(hcd->phy->otg, NULL);
  379. usb_phy_shutdown(hcd->phy);
  380. usb_remove_hcd(hcd);
  381. usb_put_hcd(hcd);
  382. clk_disable_unprepare(tegra->clk);
  383. return 0;
  384. }
  385. static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
  386. {
  387. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  388. if (hcd->driver->shutdown)
  389. hcd->driver->shutdown(hcd);
  390. }
  391. static struct of_device_id tegra_ehci_of_match[] = {
  392. { .compatible = "nvidia,tegra20-ehci", },
  393. { },
  394. };
  395. static struct platform_driver tegra_ehci_driver = {
  396. .probe = tegra_ehci_probe,
  397. .remove = tegra_ehci_remove,
  398. .shutdown = tegra_ehci_hcd_shutdown,
  399. .driver = {
  400. .name = DRV_NAME,
  401. .of_match_table = tegra_ehci_of_match,
  402. }
  403. };
  404. static const struct ehci_driver_overrides tegra_overrides __initconst = {
  405. .extra_priv_size = sizeof(struct tegra_ehci_hcd),
  406. };
  407. static int __init ehci_tegra_init(void)
  408. {
  409. if (usb_disabled())
  410. return -ENODEV;
  411. pr_info(DRV_NAME ": " DRIVER_DESC "\n");
  412. ehci_init_driver(&tegra_ehci_hc_driver, &tegra_overrides);
  413. /*
  414. * The Tegra HW has some unusual quirks, which require Tegra-specific
  415. * workarounds. We override certain hc_driver functions here to
  416. * achieve that. We explicitly do not enhance ehci_driver_overrides to
  417. * allow this more easily, since this is an unusual case, and we don't
  418. * want to encourage others to override these functions by making it
  419. * too easy.
  420. */
  421. orig_hub_control = tegra_ehci_hc_driver.hub_control;
  422. tegra_ehci_hc_driver.map_urb_for_dma = tegra_ehci_map_urb_for_dma;
  423. tegra_ehci_hc_driver.unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma;
  424. tegra_ehci_hc_driver.hub_control = tegra_ehci_hub_control;
  425. return platform_driver_register(&tegra_ehci_driver);
  426. }
  427. module_init(ehci_tegra_init);
  428. static void __exit ehci_tegra_cleanup(void)
  429. {
  430. platform_driver_unregister(&tegra_ehci_driver);
  431. }
  432. module_exit(ehci_tegra_cleanup);
  433. MODULE_DESCRIPTION(DRIVER_DESC);
  434. MODULE_LICENSE("GPL");
  435. MODULE_ALIAS("platform:" DRV_NAME);
  436. MODULE_DEVICE_TABLE(of, tegra_ehci_of_match);