ehci-tegra.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * EHCI-compliant USB host controller driver for NVIDIA Tegra SoCs
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. * Copyright (C) 2009 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/err.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/platform_data/tegra_usb.h>
  22. #include <linux/irq.h>
  23. #include <linux/usb/otg.h>
  24. #include <linux/gpio.h>
  25. #include <linux/of.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/usb/tegra_usb_phy.h>
  29. #define TEGRA_USB_BASE 0xC5000000
  30. #define TEGRA_USB2_BASE 0xC5004000
  31. #define TEGRA_USB3_BASE 0xC5008000
  32. #define TEGRA_USB_DMA_ALIGN 32
  33. struct tegra_ehci_hcd {
  34. struct ehci_hcd *ehci;
  35. struct tegra_usb_phy *phy;
  36. struct clk *clk;
  37. struct clk *emc_clk;
  38. struct usb_phy *transceiver;
  39. int host_resumed;
  40. int port_resuming;
  41. enum tegra_usb_phy_port_speed port_speed;
  42. };
  43. static void tegra_ehci_power_up(struct usb_hcd *hcd)
  44. {
  45. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  46. clk_prepare_enable(tegra->emc_clk);
  47. clk_prepare_enable(tegra->clk);
  48. usb_phy_set_suspend(&tegra->phy->u_phy, 0);
  49. tegra->host_resumed = 1;
  50. }
  51. static void tegra_ehci_power_down(struct usb_hcd *hcd)
  52. {
  53. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  54. tegra->host_resumed = 0;
  55. usb_phy_set_suspend(&tegra->phy->u_phy, 1);
  56. clk_disable_unprepare(tegra->clk);
  57. clk_disable_unprepare(tegra->emc_clk);
  58. }
  59. static int tegra_ehci_internal_port_reset(
  60. struct ehci_hcd *ehci,
  61. u32 __iomem *portsc_reg
  62. )
  63. {
  64. u32 temp;
  65. unsigned long flags;
  66. int retval = 0;
  67. int i, tries;
  68. u32 saved_usbintr;
  69. spin_lock_irqsave(&ehci->lock, flags);
  70. saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
  71. /* disable USB interrupt */
  72. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  73. spin_unlock_irqrestore(&ehci->lock, flags);
  74. /*
  75. * Here we have to do Port Reset at most twice for
  76. * Port Enable bit to be set.
  77. */
  78. for (i = 0; i < 2; i++) {
  79. temp = ehci_readl(ehci, portsc_reg);
  80. temp |= PORT_RESET;
  81. ehci_writel(ehci, temp, portsc_reg);
  82. mdelay(10);
  83. temp &= ~PORT_RESET;
  84. ehci_writel(ehci, temp, portsc_reg);
  85. mdelay(1);
  86. tries = 100;
  87. do {
  88. mdelay(1);
  89. /*
  90. * Up to this point, Port Enable bit is
  91. * expected to be set after 2 ms waiting.
  92. * USB1 usually takes extra 45 ms, for safety,
  93. * we take 100 ms as timeout.
  94. */
  95. temp = ehci_readl(ehci, portsc_reg);
  96. } while (!(temp & PORT_PE) && tries--);
  97. if (temp & PORT_PE)
  98. break;
  99. }
  100. if (i == 2)
  101. retval = -ETIMEDOUT;
  102. /*
  103. * Clear Connect Status Change bit if it's set.
  104. * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
  105. */
  106. if (temp & PORT_CSC)
  107. ehci_writel(ehci, PORT_CSC, portsc_reg);
  108. /*
  109. * Write to clear any interrupt status bits that might be set
  110. * during port reset.
  111. */
  112. temp = ehci_readl(ehci, &ehci->regs->status);
  113. ehci_writel(ehci, temp, &ehci->regs->status);
  114. /* restore original interrupt enable bits */
  115. ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
  116. return retval;
  117. }
  118. static int tegra_ehci_hub_control(
  119. struct usb_hcd *hcd,
  120. u16 typeReq,
  121. u16 wValue,
  122. u16 wIndex,
  123. char *buf,
  124. u16 wLength
  125. )
  126. {
  127. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  128. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  129. u32 __iomem *status_reg;
  130. u32 temp;
  131. unsigned long flags;
  132. int retval = 0;
  133. status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
  134. spin_lock_irqsave(&ehci->lock, flags);
  135. if (typeReq == GetPortStatus) {
  136. temp = ehci_readl(ehci, status_reg);
  137. if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
  138. /* Resume completed, re-enable disconnect detection */
  139. tegra->port_resuming = 0;
  140. tegra_usb_phy_postresume(tegra->phy);
  141. }
  142. }
  143. else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
  144. temp = ehci_readl(ehci, status_reg);
  145. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
  146. retval = -EPIPE;
  147. goto done;
  148. }
  149. temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
  150. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  151. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  152. /*
  153. * If a transaction is in progress, there may be a delay in
  154. * suspending the port. Poll until the port is suspended.
  155. */
  156. if (handshake(ehci, status_reg, PORT_SUSPEND,
  157. PORT_SUSPEND, 5000))
  158. pr_err("%s: timeout waiting for SUSPEND\n", __func__);
  159. set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
  160. goto done;
  161. }
  162. /* For USB1 port we need to issue Port Reset twice internally */
  163. if (tegra->phy->instance == 0 &&
  164. (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
  165. spin_unlock_irqrestore(&ehci->lock, flags);
  166. return tegra_ehci_internal_port_reset(ehci, status_reg);
  167. }
  168. /*
  169. * Tegra host controller will time the resume operation to clear the bit
  170. * when the port control state switches to HS or FS Idle. This behavior
  171. * is different from EHCI where the host controller driver is required
  172. * to set this bit to a zero after the resume duration is timed in the
  173. * driver.
  174. */
  175. else if (typeReq == ClearPortFeature &&
  176. wValue == USB_PORT_FEAT_SUSPEND) {
  177. temp = ehci_readl(ehci, status_reg);
  178. if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
  179. retval = -EPIPE;
  180. goto done;
  181. }
  182. if (!(temp & PORT_SUSPEND))
  183. goto done;
  184. /* Disable disconnect detection during port resume */
  185. tegra_usb_phy_preresume(tegra->phy);
  186. ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
  187. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  188. /* start resume signalling */
  189. ehci_writel(ehci, temp | PORT_RESUME, status_reg);
  190. set_bit(wIndex-1, &ehci->resuming_ports);
  191. spin_unlock_irqrestore(&ehci->lock, flags);
  192. msleep(20);
  193. spin_lock_irqsave(&ehci->lock, flags);
  194. /* Poll until the controller clears RESUME and SUSPEND */
  195. if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
  196. pr_err("%s: timeout waiting for RESUME\n", __func__);
  197. if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
  198. pr_err("%s: timeout waiting for SUSPEND\n", __func__);
  199. ehci->reset_done[wIndex-1] = 0;
  200. clear_bit(wIndex-1, &ehci->resuming_ports);
  201. tegra->port_resuming = 1;
  202. goto done;
  203. }
  204. spin_unlock_irqrestore(&ehci->lock, flags);
  205. /* Handle the hub control events here */
  206. return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  207. done:
  208. spin_unlock_irqrestore(&ehci->lock, flags);
  209. return retval;
  210. }
  211. static void tegra_ehci_restart(struct usb_hcd *hcd)
  212. {
  213. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  214. ehci_reset(ehci);
  215. /* setup the frame list and Async q heads */
  216. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  217. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  218. /* setup the command register and set the controller in RUN mode */
  219. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  220. ehci->command |= CMD_RUN;
  221. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  222. down_write(&ehci_cf_port_reset_rwsem);
  223. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  224. /* flush posted writes */
  225. ehci_readl(ehci, &ehci->regs->command);
  226. up_write(&ehci_cf_port_reset_rwsem);
  227. }
  228. static void tegra_ehci_shutdown(struct usb_hcd *hcd)
  229. {
  230. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  231. /* ehci_shutdown touches the USB controller registers, make sure
  232. * controller has clocks to it */
  233. if (!tegra->host_resumed)
  234. tegra_ehci_power_up(hcd);
  235. ehci_shutdown(hcd);
  236. }
  237. static int tegra_ehci_setup(struct usb_hcd *hcd)
  238. {
  239. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  240. /* EHCI registers start at offset 0x100 */
  241. ehci->caps = hcd->regs + 0x100;
  242. /* switch to host mode */
  243. hcd->has_tt = 1;
  244. return ehci_setup(hcd);
  245. }
  246. struct dma_aligned_buffer {
  247. void *kmalloc_ptr;
  248. void *old_xfer_buffer;
  249. u8 data[0];
  250. };
  251. static void free_dma_aligned_buffer(struct urb *urb)
  252. {
  253. struct dma_aligned_buffer *temp;
  254. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  255. return;
  256. temp = container_of(urb->transfer_buffer,
  257. struct dma_aligned_buffer, data);
  258. if (usb_urb_dir_in(urb))
  259. memcpy(temp->old_xfer_buffer, temp->data,
  260. urb->transfer_buffer_length);
  261. urb->transfer_buffer = temp->old_xfer_buffer;
  262. kfree(temp->kmalloc_ptr);
  263. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  264. }
  265. static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
  266. {
  267. struct dma_aligned_buffer *temp, *kmalloc_ptr;
  268. size_t kmalloc_size;
  269. if (urb->num_sgs || urb->sg ||
  270. urb->transfer_buffer_length == 0 ||
  271. !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
  272. return 0;
  273. /* Allocate a buffer with enough padding for alignment */
  274. kmalloc_size = urb->transfer_buffer_length +
  275. sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1;
  276. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  277. if (!kmalloc_ptr)
  278. return -ENOMEM;
  279. /* Position our struct dma_aligned_buffer such that data is aligned */
  280. temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
  281. temp->kmalloc_ptr = kmalloc_ptr;
  282. temp->old_xfer_buffer = urb->transfer_buffer;
  283. if (usb_urb_dir_out(urb))
  284. memcpy(temp->data, urb->transfer_buffer,
  285. urb->transfer_buffer_length);
  286. urb->transfer_buffer = temp->data;
  287. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  288. return 0;
  289. }
  290. static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  291. gfp_t mem_flags)
  292. {
  293. int ret;
  294. ret = alloc_dma_aligned_buffer(urb, mem_flags);
  295. if (ret)
  296. return ret;
  297. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  298. if (ret)
  299. free_dma_aligned_buffer(urb);
  300. return ret;
  301. }
  302. static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  303. {
  304. usb_hcd_unmap_urb_for_dma(hcd, urb);
  305. free_dma_aligned_buffer(urb);
  306. }
  307. static const struct hc_driver tegra_ehci_hc_driver = {
  308. .description = hcd_name,
  309. .product_desc = "Tegra EHCI Host Controller",
  310. .hcd_priv_size = sizeof(struct ehci_hcd),
  311. .flags = HCD_USB2 | HCD_MEMORY,
  312. /* standard ehci functions */
  313. .irq = ehci_irq,
  314. .start = ehci_run,
  315. .stop = ehci_stop,
  316. .urb_enqueue = ehci_urb_enqueue,
  317. .urb_dequeue = ehci_urb_dequeue,
  318. .endpoint_disable = ehci_endpoint_disable,
  319. .endpoint_reset = ehci_endpoint_reset,
  320. .get_frame_number = ehci_get_frame,
  321. .hub_status_data = ehci_hub_status_data,
  322. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  323. .relinquish_port = ehci_relinquish_port,
  324. .port_handed_over = ehci_port_handed_over,
  325. /* modified ehci functions for tegra */
  326. .reset = tegra_ehci_setup,
  327. .shutdown = tegra_ehci_shutdown,
  328. .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
  329. .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
  330. .hub_control = tegra_ehci_hub_control,
  331. #ifdef CONFIG_PM
  332. .bus_suspend = ehci_bus_suspend,
  333. .bus_resume = ehci_bus_resume,
  334. #endif
  335. };
  336. static int setup_vbus_gpio(struct platform_device *pdev,
  337. struct tegra_ehci_platform_data *pdata)
  338. {
  339. int err = 0;
  340. int gpio;
  341. gpio = pdata->vbus_gpio;
  342. if (!gpio_is_valid(gpio))
  343. gpio = of_get_named_gpio(pdev->dev.of_node,
  344. "nvidia,vbus-gpio", 0);
  345. if (!gpio_is_valid(gpio))
  346. return 0;
  347. err = gpio_request(gpio, "vbus_gpio");
  348. if (err) {
  349. dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
  350. return err;
  351. }
  352. err = gpio_direction_output(gpio, 1);
  353. if (err) {
  354. dev_err(&pdev->dev, "can't enable vbus\n");
  355. return err;
  356. }
  357. return err;
  358. }
  359. #ifdef CONFIG_PM
  360. static int controller_suspend(struct device *dev)
  361. {
  362. struct tegra_ehci_hcd *tegra =
  363. platform_get_drvdata(to_platform_device(dev));
  364. struct ehci_hcd *ehci = tegra->ehci;
  365. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  366. struct ehci_regs __iomem *hw = ehci->regs;
  367. unsigned long flags;
  368. if (time_before(jiffies, ehci->next_statechange))
  369. msleep(10);
  370. ehci_halt(ehci);
  371. spin_lock_irqsave(&ehci->lock, flags);
  372. tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
  373. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  374. spin_unlock_irqrestore(&ehci->lock, flags);
  375. tegra_ehci_power_down(hcd);
  376. return 0;
  377. }
  378. static int controller_resume(struct device *dev)
  379. {
  380. struct tegra_ehci_hcd *tegra =
  381. platform_get_drvdata(to_platform_device(dev));
  382. struct ehci_hcd *ehci = tegra->ehci;
  383. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  384. struct ehci_regs __iomem *hw = ehci->regs;
  385. unsigned long val;
  386. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  387. tegra_ehci_power_up(hcd);
  388. if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) {
  389. /* Wait for the phy to detect new devices
  390. * before we restart the controller */
  391. msleep(10);
  392. goto restart;
  393. }
  394. /* Force the phy to keep data lines in suspend state */
  395. tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
  396. /* Enable host mode */
  397. tdi_reset(ehci);
  398. /* Enable Port Power */
  399. val = readl(&hw->port_status[0]);
  400. val |= PORT_POWER;
  401. writel(val, &hw->port_status[0]);
  402. udelay(10);
  403. /* Check if the phy resume from LP0. When the phy resume from LP0
  404. * USB register will be reset. */
  405. if (!readl(&hw->async_next)) {
  406. /* Program the field PTC based on the saved speed mode */
  407. val = readl(&hw->port_status[0]);
  408. val &= ~PORT_TEST(~0);
  409. if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
  410. val |= PORT_TEST_FORCE;
  411. else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
  412. val |= PORT_TEST(6);
  413. else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
  414. val |= PORT_TEST(7);
  415. writel(val, &hw->port_status[0]);
  416. udelay(10);
  417. /* Disable test mode by setting PTC field to NORMAL_OP */
  418. val = readl(&hw->port_status[0]);
  419. val &= ~PORT_TEST(~0);
  420. writel(val, &hw->port_status[0]);
  421. udelay(10);
  422. }
  423. /* Poll until CCS is enabled */
  424. if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
  425. PORT_CONNECT, 2000)) {
  426. pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
  427. goto restart;
  428. }
  429. /* Poll until PE is enabled */
  430. if (handshake(ehci, &hw->port_status[0], PORT_PE,
  431. PORT_PE, 2000)) {
  432. pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
  433. goto restart;
  434. }
  435. /* Clear the PCI status, to avoid an interrupt taken upon resume */
  436. val = readl(&hw->status);
  437. val |= STS_PCD;
  438. writel(val, &hw->status);
  439. /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
  440. val = readl(&hw->port_status[0]);
  441. if ((val & PORT_POWER) && (val & PORT_PE)) {
  442. val |= PORT_SUSPEND;
  443. writel(val, &hw->port_status[0]);
  444. /* Wait until port suspend completes */
  445. if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
  446. PORT_SUSPEND, 1000)) {
  447. pr_err("%s: timeout waiting for PORT_SUSPEND\n",
  448. __func__);
  449. goto restart;
  450. }
  451. }
  452. tegra_ehci_phy_restore_end(tegra->phy);
  453. goto done;
  454. restart:
  455. if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
  456. tegra_ehci_phy_restore_end(tegra->phy);
  457. tegra_ehci_restart(hcd);
  458. done:
  459. tegra_usb_phy_preresume(tegra->phy);
  460. tegra->port_resuming = 1;
  461. return 0;
  462. }
  463. static int tegra_ehci_suspend(struct device *dev)
  464. {
  465. struct tegra_ehci_hcd *tegra =
  466. platform_get_drvdata(to_platform_device(dev));
  467. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  468. int rc = 0;
  469. /*
  470. * When system sleep is supported and USB controller wakeup is
  471. * implemented: If the controller is runtime-suspended and the
  472. * wakeup setting needs to be changed, call pm_runtime_resume().
  473. */
  474. if (HCD_HW_ACCESSIBLE(hcd))
  475. rc = controller_suspend(dev);
  476. return rc;
  477. }
  478. static int tegra_ehci_resume(struct device *dev)
  479. {
  480. int rc;
  481. rc = controller_resume(dev);
  482. if (rc == 0) {
  483. pm_runtime_disable(dev);
  484. pm_runtime_set_active(dev);
  485. pm_runtime_enable(dev);
  486. }
  487. return rc;
  488. }
  489. static int tegra_ehci_runtime_suspend(struct device *dev)
  490. {
  491. return controller_suspend(dev);
  492. }
  493. static int tegra_ehci_runtime_resume(struct device *dev)
  494. {
  495. return controller_resume(dev);
  496. }
  497. static const struct dev_pm_ops tegra_ehci_pm_ops = {
  498. .suspend = tegra_ehci_suspend,
  499. .resume = tegra_ehci_resume,
  500. .runtime_suspend = tegra_ehci_runtime_suspend,
  501. .runtime_resume = tegra_ehci_runtime_resume,
  502. };
  503. #endif
  504. static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
  505. static int tegra_ehci_probe(struct platform_device *pdev)
  506. {
  507. struct resource *res;
  508. struct usb_hcd *hcd;
  509. struct tegra_ehci_hcd *tegra;
  510. struct tegra_ehci_platform_data *pdata;
  511. int err = 0;
  512. int irq;
  513. int instance = pdev->id;
  514. pdata = pdev->dev.platform_data;
  515. if (!pdata) {
  516. dev_err(&pdev->dev, "Platform data missing\n");
  517. return -EINVAL;
  518. }
  519. /* Right now device-tree probed devices don't get dma_mask set.
  520. * Since shared usb code relies on it, set it here for now.
  521. * Once we have dma capability bindings this can go away.
  522. */
  523. if (!pdev->dev.dma_mask)
  524. pdev->dev.dma_mask = &tegra_ehci_dma_mask;
  525. setup_vbus_gpio(pdev, pdata);
  526. tegra = devm_kzalloc(&pdev->dev, sizeof(struct tegra_ehci_hcd),
  527. GFP_KERNEL);
  528. if (!tegra)
  529. return -ENOMEM;
  530. hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
  531. dev_name(&pdev->dev));
  532. if (!hcd) {
  533. dev_err(&pdev->dev, "Unable to create HCD\n");
  534. return -ENOMEM;
  535. }
  536. platform_set_drvdata(pdev, tegra);
  537. tegra->clk = devm_clk_get(&pdev->dev, NULL);
  538. if (IS_ERR(tegra->clk)) {
  539. dev_err(&pdev->dev, "Can't get ehci clock\n");
  540. err = PTR_ERR(tegra->clk);
  541. goto fail_clk;
  542. }
  543. err = clk_prepare_enable(tegra->clk);
  544. if (err)
  545. goto fail_clk;
  546. tegra->emc_clk = devm_clk_get(&pdev->dev, "emc");
  547. if (IS_ERR(tegra->emc_clk)) {
  548. dev_err(&pdev->dev, "Can't get emc clock\n");
  549. err = PTR_ERR(tegra->emc_clk);
  550. goto fail_emc_clk;
  551. }
  552. clk_prepare_enable(tegra->emc_clk);
  553. clk_set_rate(tegra->emc_clk, 400000000);
  554. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  555. if (!res) {
  556. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  557. err = -ENXIO;
  558. goto fail_io;
  559. }
  560. hcd->rsrc_start = res->start;
  561. hcd->rsrc_len = resource_size(res);
  562. hcd->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  563. if (!hcd->regs) {
  564. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  565. err = -ENOMEM;
  566. goto fail_io;
  567. }
  568. /* This is pretty ugly and needs to be fixed when we do only
  569. * device-tree probing. Old code relies on the platform_device
  570. * numbering that we lack for device-tree-instantiated devices.
  571. */
  572. if (instance < 0) {
  573. switch (res->start) {
  574. case TEGRA_USB_BASE:
  575. instance = 0;
  576. break;
  577. case TEGRA_USB2_BASE:
  578. instance = 1;
  579. break;
  580. case TEGRA_USB3_BASE:
  581. instance = 2;
  582. break;
  583. default:
  584. err = -ENODEV;
  585. dev_err(&pdev->dev, "unknown usb instance\n");
  586. goto fail_io;
  587. }
  588. }
  589. tegra->phy = tegra_usb_phy_open(&pdev->dev, instance, hcd->regs,
  590. pdata->phy_config,
  591. TEGRA_USB_PHY_MODE_HOST);
  592. if (IS_ERR(tegra->phy)) {
  593. dev_err(&pdev->dev, "Failed to open USB phy\n");
  594. err = -ENXIO;
  595. goto fail_io;
  596. }
  597. usb_phy_init(&tegra->phy->u_phy);
  598. err = usb_phy_set_suspend(&tegra->phy->u_phy, 0);
  599. if (err) {
  600. dev_err(&pdev->dev, "Failed to power on the phy\n");
  601. goto fail;
  602. }
  603. tegra->host_resumed = 1;
  604. tegra->ehci = hcd_to_ehci(hcd);
  605. irq = platform_get_irq(pdev, 0);
  606. if (!irq) {
  607. dev_err(&pdev->dev, "Failed to get IRQ\n");
  608. err = -ENODEV;
  609. goto fail;
  610. }
  611. #ifdef CONFIG_USB_OTG_UTILS
  612. if (pdata->operating_mode == TEGRA_USB_OTG) {
  613. tegra->transceiver =
  614. devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  615. if (!IS_ERR_OR_NULL(tegra->transceiver))
  616. otg_set_host(tegra->transceiver->otg, &hcd->self);
  617. }
  618. #endif
  619. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  620. if (err) {
  621. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  622. goto fail;
  623. }
  624. pm_runtime_set_active(&pdev->dev);
  625. pm_runtime_get_noresume(&pdev->dev);
  626. /* Don't skip the pm_runtime_forbid call if wakeup isn't working */
  627. /* if (!pdata->power_down_on_bus_suspend) */
  628. pm_runtime_forbid(&pdev->dev);
  629. pm_runtime_enable(&pdev->dev);
  630. pm_runtime_put_sync(&pdev->dev);
  631. return err;
  632. fail:
  633. #ifdef CONFIG_USB_OTG_UTILS
  634. if (!IS_ERR_OR_NULL(tegra->transceiver))
  635. otg_set_host(tegra->transceiver->otg, NULL);
  636. #endif
  637. usb_phy_shutdown(&tegra->phy->u_phy);
  638. fail_io:
  639. clk_disable_unprepare(tegra->emc_clk);
  640. fail_emc_clk:
  641. clk_disable_unprepare(tegra->clk);
  642. fail_clk:
  643. usb_put_hcd(hcd);
  644. return err;
  645. }
  646. static int tegra_ehci_remove(struct platform_device *pdev)
  647. {
  648. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  649. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  650. pm_runtime_get_sync(&pdev->dev);
  651. pm_runtime_disable(&pdev->dev);
  652. pm_runtime_put_noidle(&pdev->dev);
  653. #ifdef CONFIG_USB_OTG_UTILS
  654. if (!IS_ERR_OR_NULL(tegra->transceiver))
  655. otg_set_host(tegra->transceiver->otg, NULL);
  656. #endif
  657. usb_remove_hcd(hcd);
  658. usb_put_hcd(hcd);
  659. usb_phy_shutdown(&tegra->phy->u_phy);
  660. clk_disable_unprepare(tegra->clk);
  661. clk_disable_unprepare(tegra->emc_clk);
  662. return 0;
  663. }
  664. static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
  665. {
  666. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  667. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  668. if (hcd->driver->shutdown)
  669. hcd->driver->shutdown(hcd);
  670. }
  671. static struct of_device_id tegra_ehci_of_match[] = {
  672. { .compatible = "nvidia,tegra20-ehci", },
  673. { },
  674. };
  675. static struct platform_driver tegra_ehci_driver = {
  676. .probe = tegra_ehci_probe,
  677. .remove = tegra_ehci_remove,
  678. .shutdown = tegra_ehci_hcd_shutdown,
  679. .driver = {
  680. .name = "tegra-ehci",
  681. .of_match_table = tegra_ehci_of_match,
  682. #ifdef CONFIG_PM
  683. .pm = &tegra_ehci_pm_ops,
  684. #endif
  685. }
  686. };