ehci-tegra.c 22 KB

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