ehci-tegra.c 14 KB

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