ehci-tegra.c 14 KB

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