ehci-tegra.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 <mach/usb_phy.h>
  29. #include <mach/iomap.h>
  30. #define TEGRA_USB_DMA_ALIGN 32
  31. struct tegra_ehci_hcd {
  32. struct ehci_hcd *ehci;
  33. struct tegra_usb_phy *phy;
  34. struct clk *clk;
  35. struct clk *emc_clk;
  36. struct usb_phy *transceiver;
  37. int host_resumed;
  38. int port_resuming;
  39. enum tegra_usb_phy_port_speed port_speed;
  40. };
  41. static void tegra_ehci_power_up(struct usb_hcd *hcd)
  42. {
  43. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  44. clk_prepare_enable(tegra->emc_clk);
  45. clk_prepare_enable(tegra->clk);
  46. tegra_usb_phy_power_on(tegra->phy);
  47. tegra->host_resumed = 1;
  48. }
  49. static void tegra_ehci_power_down(struct usb_hcd *hcd)
  50. {
  51. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  52. tegra->host_resumed = 0;
  53. tegra_usb_phy_power_off(tegra->phy);
  54. clk_disable_unprepare(tegra->clk);
  55. clk_disable_unprepare(tegra->emc_clk);
  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 = dev_get_drvdata(hcd->self.controller);
  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(tegra->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 (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->phy->instance == 0 &&
  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(tegra->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 (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
  194. pr_err("%s: timeout waiting for RESUME\n", __func__);
  195. if (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 ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  205. done:
  206. spin_unlock_irqrestore(&ehci->lock, flags);
  207. return retval;
  208. }
  209. static void tegra_ehci_restart(struct usb_hcd *hcd)
  210. {
  211. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  212. ehci_reset(ehci);
  213. /* setup the frame list and Async q heads */
  214. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  215. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  216. /* setup the command register and set the controller in RUN mode */
  217. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  218. ehci->command |= CMD_RUN;
  219. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  220. down_write(&ehci_cf_port_reset_rwsem);
  221. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  222. /* flush posted writes */
  223. ehci_readl(ehci, &ehci->regs->command);
  224. up_write(&ehci_cf_port_reset_rwsem);
  225. }
  226. static void tegra_ehci_shutdown(struct usb_hcd *hcd)
  227. {
  228. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  229. /* ehci_shutdown touches the USB controller registers, make sure
  230. * controller has clocks to it */
  231. if (!tegra->host_resumed)
  232. tegra_ehci_power_up(hcd);
  233. ehci_shutdown(hcd);
  234. }
  235. static int tegra_ehci_setup(struct usb_hcd *hcd)
  236. {
  237. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  238. int retval;
  239. /* EHCI registers start at offset 0x100 */
  240. ehci->caps = hcd->regs + 0x100;
  241. /* switch to host mode */
  242. hcd->has_tt = 1;
  243. retval = ehci_setup(hcd);
  244. if (retval)
  245. return retval;
  246. ehci_port_power(ehci, 1);
  247. return retval;
  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(tegra->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(tegra->phy);
  456. goto done;
  457. restart:
  458. if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
  459. tegra_ehci_phy_restore_end(tegra->phy);
  460. tegra_ehci_restart(hcd);
  461. done:
  462. tegra_usb_phy_preresume(tegra->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. static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
  508. static int tegra_ehci_probe(struct platform_device *pdev)
  509. {
  510. struct resource *res;
  511. struct usb_hcd *hcd;
  512. struct tegra_ehci_hcd *tegra;
  513. struct tegra_ehci_platform_data *pdata;
  514. int err = 0;
  515. int irq;
  516. int instance = pdev->id;
  517. pdata = pdev->dev.platform_data;
  518. if (!pdata) {
  519. dev_err(&pdev->dev, "Platform data missing\n");
  520. return -EINVAL;
  521. }
  522. /* Right now device-tree probed devices don't get dma_mask set.
  523. * Since shared usb code relies on it, set it here for now.
  524. * Once we have dma capability bindings this can go away.
  525. */
  526. if (!pdev->dev.dma_mask)
  527. pdev->dev.dma_mask = &tegra_ehci_dma_mask;
  528. setup_vbus_gpio(pdev, pdata);
  529. tegra = kzalloc(sizeof(struct tegra_ehci_hcd), GFP_KERNEL);
  530. if (!tegra)
  531. return -ENOMEM;
  532. hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
  533. dev_name(&pdev->dev));
  534. if (!hcd) {
  535. dev_err(&pdev->dev, "Unable to create HCD\n");
  536. err = -ENOMEM;
  537. goto fail_hcd;
  538. }
  539. platform_set_drvdata(pdev, tegra);
  540. tegra->clk = clk_get(&pdev->dev, NULL);
  541. if (IS_ERR(tegra->clk)) {
  542. dev_err(&pdev->dev, "Can't get ehci clock\n");
  543. err = PTR_ERR(tegra->clk);
  544. goto fail_clk;
  545. }
  546. err = clk_prepare_enable(tegra->clk);
  547. if (err)
  548. goto fail_clken;
  549. tegra->emc_clk = clk_get(&pdev->dev, "emc");
  550. if (IS_ERR(tegra->emc_clk)) {
  551. dev_err(&pdev->dev, "Can't get emc clock\n");
  552. err = PTR_ERR(tegra->emc_clk);
  553. goto fail_emc_clk;
  554. }
  555. clk_prepare_enable(tegra->emc_clk);
  556. clk_set_rate(tegra->emc_clk, 400000000);
  557. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  558. if (!res) {
  559. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  560. err = -ENXIO;
  561. goto fail_io;
  562. }
  563. hcd->rsrc_start = res->start;
  564. hcd->rsrc_len = resource_size(res);
  565. hcd->regs = ioremap(res->start, resource_size(res));
  566. if (!hcd->regs) {
  567. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  568. err = -ENOMEM;
  569. goto fail_io;
  570. }
  571. /* This is pretty ugly and needs to be fixed when we do only
  572. * device-tree probing. Old code relies on the platform_device
  573. * numbering that we lack for device-tree-instantiated devices.
  574. */
  575. if (instance < 0) {
  576. switch (res->start) {
  577. case TEGRA_USB_BASE:
  578. instance = 0;
  579. break;
  580. case TEGRA_USB2_BASE:
  581. instance = 1;
  582. break;
  583. case TEGRA_USB3_BASE:
  584. instance = 2;
  585. break;
  586. default:
  587. err = -ENODEV;
  588. dev_err(&pdev->dev, "unknown usb instance\n");
  589. goto fail_phy;
  590. }
  591. }
  592. tegra->phy = tegra_usb_phy_open(&pdev->dev, instance, hcd->regs,
  593. pdata->phy_config,
  594. TEGRA_USB_PHY_MODE_HOST);
  595. if (IS_ERR(tegra->phy)) {
  596. dev_err(&pdev->dev, "Failed to open USB phy\n");
  597. err = -ENXIO;
  598. goto fail_phy;
  599. }
  600. err = tegra_usb_phy_power_on(tegra->phy);
  601. if (err) {
  602. dev_err(&pdev->dev, "Failed to power on the phy\n");
  603. goto fail;
  604. }
  605. tegra->host_resumed = 1;
  606. tegra->ehci = hcd_to_ehci(hcd);
  607. irq = platform_get_irq(pdev, 0);
  608. if (!irq) {
  609. dev_err(&pdev->dev, "Failed to get IRQ\n");
  610. err = -ENODEV;
  611. goto fail;
  612. }
  613. #ifdef CONFIG_USB_OTG_UTILS
  614. if (pdata->operating_mode == TEGRA_USB_OTG) {
  615. tegra->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  616. if (!IS_ERR_OR_NULL(tegra->transceiver))
  617. otg_set_host(tegra->transceiver->otg, &hcd->self);
  618. }
  619. #endif
  620. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  621. if (err) {
  622. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  623. goto fail;
  624. }
  625. pm_runtime_set_active(&pdev->dev);
  626. pm_runtime_get_noresume(&pdev->dev);
  627. /* Don't skip the pm_runtime_forbid call if wakeup isn't working */
  628. /* if (!pdata->power_down_on_bus_suspend) */
  629. pm_runtime_forbid(&pdev->dev);
  630. pm_runtime_enable(&pdev->dev);
  631. pm_runtime_put_sync(&pdev->dev);
  632. return err;
  633. fail:
  634. #ifdef CONFIG_USB_OTG_UTILS
  635. if (!IS_ERR_OR_NULL(tegra->transceiver)) {
  636. otg_set_host(tegra->transceiver->otg, NULL);
  637. usb_put_phy(tegra->transceiver);
  638. }
  639. #endif
  640. tegra_usb_phy_close(tegra->phy);
  641. fail_phy:
  642. iounmap(hcd->regs);
  643. fail_io:
  644. clk_disable_unprepare(tegra->emc_clk);
  645. clk_put(tegra->emc_clk);
  646. fail_emc_clk:
  647. clk_disable_unprepare(tegra->clk);
  648. fail_clken:
  649. clk_put(tegra->clk);
  650. fail_clk:
  651. usb_put_hcd(hcd);
  652. fail_hcd:
  653. kfree(tegra);
  654. return err;
  655. }
  656. static int tegra_ehci_remove(struct platform_device *pdev)
  657. {
  658. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  659. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  660. if (tegra == NULL || hcd == NULL)
  661. return -EINVAL;
  662. pm_runtime_get_sync(&pdev->dev);
  663. pm_runtime_disable(&pdev->dev);
  664. pm_runtime_put_noidle(&pdev->dev);
  665. #ifdef CONFIG_USB_OTG_UTILS
  666. if (!IS_ERR_OR_NULL(tegra->transceiver)) {
  667. otg_set_host(tegra->transceiver->otg, NULL);
  668. usb_put_phy(tegra->transceiver);
  669. }
  670. #endif
  671. usb_remove_hcd(hcd);
  672. usb_put_hcd(hcd);
  673. tegra_usb_phy_close(tegra->phy);
  674. iounmap(hcd->regs);
  675. clk_disable_unprepare(tegra->clk);
  676. clk_put(tegra->clk);
  677. clk_disable_unprepare(tegra->emc_clk);
  678. clk_put(tegra->emc_clk);
  679. kfree(tegra);
  680. return 0;
  681. }
  682. static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
  683. {
  684. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  685. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  686. if (hcd->driver->shutdown)
  687. hcd->driver->shutdown(hcd);
  688. }
  689. static struct of_device_id tegra_ehci_of_match[] __devinitdata = {
  690. { .compatible = "nvidia,tegra20-ehci", },
  691. { },
  692. };
  693. static struct platform_driver tegra_ehci_driver = {
  694. .probe = tegra_ehci_probe,
  695. .remove = tegra_ehci_remove,
  696. .shutdown = tegra_ehci_hcd_shutdown,
  697. .driver = {
  698. .name = "tegra-ehci",
  699. .of_match_table = tegra_ehci_of_match,
  700. #ifdef CONFIG_PM
  701. .pm = &tegra_ehci_pm_ops,
  702. #endif
  703. }
  704. };