ehci-tegra.c 15 KB

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