ehci-tegra.c 21 KB

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