ehci-tegra.c 14 KB

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