ehci-tegra.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 setup_vbus_gpio(struct platform_device *pdev,
  271. struct tegra_ehci_platform_data *pdata)
  272. {
  273. int err = 0;
  274. int gpio;
  275. gpio = pdata->vbus_gpio;
  276. if (!gpio_is_valid(gpio))
  277. gpio = of_get_named_gpio(pdev->dev.of_node,
  278. "nvidia,vbus-gpio", 0);
  279. if (!gpio_is_valid(gpio))
  280. return 0;
  281. err = gpio_request(gpio, "vbus_gpio");
  282. if (err) {
  283. dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
  284. return err;
  285. }
  286. err = gpio_direction_output(gpio, 1);
  287. if (err) {
  288. dev_err(&pdev->dev, "can't enable vbus\n");
  289. return err;
  290. }
  291. return err;
  292. }
  293. static int tegra_ehci_probe(struct platform_device *pdev)
  294. {
  295. struct resource *res;
  296. struct usb_hcd *hcd;
  297. struct ehci_hcd *ehci;
  298. struct tegra_ehci_hcd *tegra;
  299. struct tegra_ehci_platform_data *pdata;
  300. int err = 0;
  301. int irq;
  302. struct device_node *np_phy;
  303. struct usb_phy *u_phy;
  304. pdata = pdev->dev.platform_data;
  305. if (!pdata) {
  306. dev_err(&pdev->dev, "Platform data missing\n");
  307. return -EINVAL;
  308. }
  309. /* Right now device-tree probed devices don't get dma_mask set.
  310. * Since shared usb code relies on it, set it here for now.
  311. * Once we have dma capability bindings this can go away.
  312. */
  313. if (!pdev->dev.dma_mask)
  314. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  315. if (!pdev->dev.coherent_dma_mask)
  316. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  317. setup_vbus_gpio(pdev, pdata);
  318. hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
  319. dev_name(&pdev->dev));
  320. if (!hcd) {
  321. dev_err(&pdev->dev, "Unable to create HCD\n");
  322. err = -ENOMEM;
  323. goto cleanup_vbus_gpio;
  324. }
  325. platform_set_drvdata(pdev, hcd);
  326. ehci = hcd_to_ehci(hcd);
  327. tegra = (struct tegra_ehci_hcd *)ehci->priv;
  328. hcd->has_tt = 1;
  329. tegra->clk = devm_clk_get(&pdev->dev, NULL);
  330. if (IS_ERR(tegra->clk)) {
  331. dev_err(&pdev->dev, "Can't get ehci clock\n");
  332. err = PTR_ERR(tegra->clk);
  333. goto cleanup_hcd_create;
  334. }
  335. err = clk_prepare_enable(tegra->clk);
  336. if (err)
  337. goto cleanup_clk_get;
  338. tegra_periph_reset_assert(tegra->clk);
  339. udelay(1);
  340. tegra_periph_reset_deassert(tegra->clk);
  341. np_phy = of_parse_phandle(pdev->dev.of_node, "nvidia,phy", 0);
  342. if (!np_phy) {
  343. err = -ENODEV;
  344. goto cleanup_clk_en;
  345. }
  346. u_phy = tegra_usb_get_phy(np_phy);
  347. if (IS_ERR(u_phy)) {
  348. err = PTR_ERR(u_phy);
  349. goto cleanup_clk_en;
  350. }
  351. hcd->phy = u_phy;
  352. tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
  353. "nvidia,needs-double-reset");
  354. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  355. if (!res) {
  356. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  357. err = -ENXIO;
  358. goto cleanup_clk_en;
  359. }
  360. hcd->rsrc_start = res->start;
  361. hcd->rsrc_len = resource_size(res);
  362. hcd->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  363. if (!hcd->regs) {
  364. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  365. err = -ENOMEM;
  366. goto cleanup_clk_en;
  367. }
  368. ehci->caps = hcd->regs + 0x100;
  369. err = usb_phy_init(hcd->phy);
  370. if (err) {
  371. dev_err(&pdev->dev, "Failed to initialize phy\n");
  372. goto cleanup_clk_en;
  373. }
  374. u_phy->otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  375. GFP_KERNEL);
  376. if (!u_phy->otg) {
  377. dev_err(&pdev->dev, "Failed to alloc memory for otg\n");
  378. err = -ENOMEM;
  379. goto cleanup_phy;
  380. }
  381. u_phy->otg->host = hcd_to_bus(hcd);
  382. err = usb_phy_set_suspend(hcd->phy, 0);
  383. if (err) {
  384. dev_err(&pdev->dev, "Failed to power on the phy\n");
  385. goto cleanup_phy;
  386. }
  387. irq = platform_get_irq(pdev, 0);
  388. if (!irq) {
  389. dev_err(&pdev->dev, "Failed to get IRQ\n");
  390. err = -ENODEV;
  391. goto cleanup_phy;
  392. }
  393. if (pdata->operating_mode == TEGRA_USB_OTG) {
  394. tegra->transceiver =
  395. devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  396. if (!IS_ERR(tegra->transceiver))
  397. otg_set_host(tegra->transceiver->otg, &hcd->self);
  398. } else {
  399. tegra->transceiver = ERR_PTR(-ENODEV);
  400. }
  401. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  402. if (err) {
  403. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  404. goto cleanup_transceiver;
  405. }
  406. return err;
  407. cleanup_transceiver:
  408. if (!IS_ERR(tegra->transceiver))
  409. otg_set_host(tegra->transceiver->otg, NULL);
  410. cleanup_phy:
  411. usb_phy_shutdown(hcd->phy);
  412. cleanup_clk_en:
  413. clk_disable_unprepare(tegra->clk);
  414. cleanup_clk_get:
  415. clk_put(tegra->clk);
  416. cleanup_hcd_create:
  417. usb_put_hcd(hcd);
  418. cleanup_vbus_gpio:
  419. /* FIXME: Undo setup_vbus_gpio() here */
  420. return err;
  421. }
  422. static int tegra_ehci_remove(struct platform_device *pdev)
  423. {
  424. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  425. struct tegra_ehci_hcd *tegra =
  426. (struct tegra_ehci_hcd *)hcd_to_ehci(hcd)->priv;
  427. if (!IS_ERR(tegra->transceiver))
  428. otg_set_host(tegra->transceiver->otg, NULL);
  429. usb_phy_shutdown(hcd->phy);
  430. usb_remove_hcd(hcd);
  431. usb_put_hcd(hcd);
  432. clk_disable_unprepare(tegra->clk);
  433. return 0;
  434. }
  435. static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
  436. {
  437. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  438. if (hcd->driver->shutdown)
  439. hcd->driver->shutdown(hcd);
  440. }
  441. static struct of_device_id tegra_ehci_of_match[] = {
  442. { .compatible = "nvidia,tegra20-ehci", },
  443. { },
  444. };
  445. static struct platform_driver tegra_ehci_driver = {
  446. .probe = tegra_ehci_probe,
  447. .remove = tegra_ehci_remove,
  448. .shutdown = tegra_ehci_hcd_shutdown,
  449. .driver = {
  450. .name = DRV_NAME,
  451. .of_match_table = tegra_ehci_of_match,
  452. }
  453. };
  454. static const struct ehci_driver_overrides tegra_overrides __initconst = {
  455. .extra_priv_size = sizeof(struct tegra_ehci_hcd),
  456. };
  457. static int __init ehci_tegra_init(void)
  458. {
  459. if (usb_disabled())
  460. return -ENODEV;
  461. pr_info(DRV_NAME ": " DRIVER_DESC "\n");
  462. ehci_init_driver(&tegra_ehci_hc_driver, &tegra_overrides);
  463. /*
  464. * The Tegra HW has some unusual quirks, which require Tegra-specific
  465. * workarounds. We override certain hc_driver functions here to
  466. * achieve that. We explicitly do not enhance ehci_driver_overrides to
  467. * allow this more easily, since this is an unusual case, and we don't
  468. * want to encourage others to override these functions by making it
  469. * too easy.
  470. */
  471. orig_hub_control = tegra_ehci_hc_driver.hub_control;
  472. tegra_ehci_hc_driver.map_urb_for_dma = tegra_ehci_map_urb_for_dma;
  473. tegra_ehci_hc_driver.unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma;
  474. tegra_ehci_hc_driver.hub_control = tegra_ehci_hub_control;
  475. return platform_driver_register(&tegra_ehci_driver);
  476. }
  477. module_init(ehci_tegra_init);
  478. static void __exit ehci_tegra_cleanup(void)
  479. {
  480. platform_driver_unregister(&tegra_ehci_driver);
  481. }
  482. module_exit(ehci_tegra_cleanup);
  483. MODULE_DESCRIPTION(DRIVER_DESC);
  484. MODULE_LICENSE("GPL");
  485. MODULE_ALIAS("platform:" DRV_NAME);
  486. MODULE_DEVICE_TABLE(of, tegra_ehci_of_match);