ehci-tegra.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 <mach/usb_phy.h>
  24. #define TEGRA_USB_DMA_ALIGN 32
  25. struct tegra_ehci_hcd {
  26. struct ehci_hcd *ehci;
  27. struct tegra_usb_phy *phy;
  28. struct clk *clk;
  29. struct clk *emc_clk;
  30. struct otg_transceiver *transceiver;
  31. int host_resumed;
  32. int bus_suspended;
  33. int port_resuming;
  34. int power_down_on_bus_suspend;
  35. enum tegra_usb_phy_port_speed port_speed;
  36. };
  37. static void tegra_ehci_power_up(struct usb_hcd *hcd)
  38. {
  39. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  40. clk_enable(tegra->emc_clk);
  41. clk_enable(tegra->clk);
  42. tegra_usb_phy_power_on(tegra->phy);
  43. tegra->host_resumed = 1;
  44. }
  45. static void tegra_ehci_power_down(struct usb_hcd *hcd)
  46. {
  47. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  48. tegra->host_resumed = 0;
  49. tegra_usb_phy_power_off(tegra->phy);
  50. clk_disable(tegra->clk);
  51. clk_disable(tegra->emc_clk);
  52. }
  53. static int tegra_ehci_hub_control(
  54. struct usb_hcd *hcd,
  55. u16 typeReq,
  56. u16 wValue,
  57. u16 wIndex,
  58. char *buf,
  59. u16 wLength
  60. )
  61. {
  62. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  63. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  64. u32 __iomem *status_reg;
  65. u32 temp;
  66. unsigned long flags;
  67. int retval = 0;
  68. status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
  69. spin_lock_irqsave(&ehci->lock, flags);
  70. /*
  71. * In ehci_hub_control() for USB_PORT_FEAT_ENABLE clears the other bits
  72. * that are write on clear, by writing back the register read value, so
  73. * USB_PORT_FEAT_ENABLE is handled by masking the set on clear bits
  74. */
  75. if (typeReq == ClearPortFeature && wValue == USB_PORT_FEAT_ENABLE) {
  76. temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS;
  77. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  78. goto done;
  79. }
  80. else if (typeReq == GetPortStatus) {
  81. temp = ehci_readl(ehci, status_reg);
  82. if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
  83. /* Resume completed, re-enable disconnect detection */
  84. tegra->port_resuming = 0;
  85. tegra_usb_phy_postresume(tegra->phy);
  86. }
  87. }
  88. else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
  89. temp = ehci_readl(ehci, status_reg);
  90. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
  91. retval = -EPIPE;
  92. goto done;
  93. }
  94. temp &= ~PORT_WKCONN_E;
  95. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  96. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  97. /*
  98. * If a transaction is in progress, there may be a delay in
  99. * suspending the port. Poll until the port is suspended.
  100. */
  101. if (handshake(ehci, status_reg, PORT_SUSPEND,
  102. PORT_SUSPEND, 5000))
  103. pr_err("%s: timeout waiting for SUSPEND\n", __func__);
  104. set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
  105. goto done;
  106. }
  107. /*
  108. * Tegra host controller will time the resume operation to clear the bit
  109. * when the port control state switches to HS or FS Idle. This behavior
  110. * is different from EHCI where the host controller driver is required
  111. * to set this bit to a zero after the resume duration is timed in the
  112. * driver.
  113. */
  114. else if (typeReq == ClearPortFeature &&
  115. wValue == USB_PORT_FEAT_SUSPEND) {
  116. temp = ehci_readl(ehci, status_reg);
  117. if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
  118. retval = -EPIPE;
  119. goto done;
  120. }
  121. if (!(temp & PORT_SUSPEND))
  122. goto done;
  123. /* Disable disconnect detection during port resume */
  124. tegra_usb_phy_preresume(tegra->phy);
  125. ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
  126. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  127. /* start resume signalling */
  128. ehci_writel(ehci, temp | PORT_RESUME, status_reg);
  129. spin_unlock_irqrestore(&ehci->lock, flags);
  130. msleep(20);
  131. spin_lock_irqsave(&ehci->lock, flags);
  132. /* Poll until the controller clears RESUME and SUSPEND */
  133. if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
  134. pr_err("%s: timeout waiting for RESUME\n", __func__);
  135. if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
  136. pr_err("%s: timeout waiting for SUSPEND\n", __func__);
  137. ehci->reset_done[wIndex-1] = 0;
  138. tegra->port_resuming = 1;
  139. goto done;
  140. }
  141. spin_unlock_irqrestore(&ehci->lock, flags);
  142. /* Handle the hub control events here */
  143. return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
  144. done:
  145. spin_unlock_irqrestore(&ehci->lock, flags);
  146. return retval;
  147. }
  148. static void tegra_ehci_restart(struct usb_hcd *hcd)
  149. {
  150. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  151. ehci_reset(ehci);
  152. /* setup the frame list and Async q heads */
  153. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  154. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  155. /* setup the command register and set the controller in RUN mode */
  156. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  157. ehci->command |= CMD_RUN;
  158. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  159. down_write(&ehci_cf_port_reset_rwsem);
  160. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  161. /* flush posted writes */
  162. ehci_readl(ehci, &ehci->regs->command);
  163. up_write(&ehci_cf_port_reset_rwsem);
  164. }
  165. static int tegra_usb_suspend(struct usb_hcd *hcd)
  166. {
  167. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  168. struct ehci_regs __iomem *hw = tegra->ehci->regs;
  169. unsigned long flags;
  170. spin_lock_irqsave(&tegra->ehci->lock, flags);
  171. tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
  172. ehci_halt(tegra->ehci);
  173. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  174. spin_unlock_irqrestore(&tegra->ehci->lock, flags);
  175. tegra_ehci_power_down(hcd);
  176. return 0;
  177. }
  178. static int tegra_usb_resume(struct usb_hcd *hcd)
  179. {
  180. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  181. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  182. struct ehci_regs __iomem *hw = ehci->regs;
  183. unsigned long val;
  184. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  185. tegra_ehci_power_up(hcd);
  186. if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) {
  187. /* Wait for the phy to detect new devices
  188. * before we restart the controller */
  189. msleep(10);
  190. goto restart;
  191. }
  192. /* Force the phy to keep data lines in suspend state */
  193. tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
  194. /* Enable host mode */
  195. tdi_reset(ehci);
  196. /* Enable Port Power */
  197. val = readl(&hw->port_status[0]);
  198. val |= PORT_POWER;
  199. writel(val, &hw->port_status[0]);
  200. udelay(10);
  201. /* Check if the phy resume from LP0. When the phy resume from LP0
  202. * USB register will be reset. */
  203. if (!readl(&hw->async_next)) {
  204. /* Program the field PTC based on the saved speed mode */
  205. val = readl(&hw->port_status[0]);
  206. val &= ~PORT_TEST(~0);
  207. if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
  208. val |= PORT_TEST_FORCE;
  209. else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
  210. val |= PORT_TEST(6);
  211. else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
  212. val |= PORT_TEST(7);
  213. writel(val, &hw->port_status[0]);
  214. udelay(10);
  215. /* Disable test mode by setting PTC field to NORMAL_OP */
  216. val = readl(&hw->port_status[0]);
  217. val &= ~PORT_TEST(~0);
  218. writel(val, &hw->port_status[0]);
  219. udelay(10);
  220. }
  221. /* Poll until CCS is enabled */
  222. if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
  223. PORT_CONNECT, 2000)) {
  224. pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
  225. goto restart;
  226. }
  227. /* Poll until PE is enabled */
  228. if (handshake(ehci, &hw->port_status[0], PORT_PE,
  229. PORT_PE, 2000)) {
  230. pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
  231. goto restart;
  232. }
  233. /* Clear the PCI status, to avoid an interrupt taken upon resume */
  234. val = readl(&hw->status);
  235. val |= STS_PCD;
  236. writel(val, &hw->status);
  237. /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
  238. val = readl(&hw->port_status[0]);
  239. if ((val & PORT_POWER) && (val & PORT_PE)) {
  240. val |= PORT_SUSPEND;
  241. writel(val, &hw->port_status[0]);
  242. /* Wait until port suspend completes */
  243. if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
  244. PORT_SUSPEND, 1000)) {
  245. pr_err("%s: timeout waiting for PORT_SUSPEND\n",
  246. __func__);
  247. goto restart;
  248. }
  249. }
  250. tegra_ehci_phy_restore_end(tegra->phy);
  251. return 0;
  252. restart:
  253. if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
  254. tegra_ehci_phy_restore_end(tegra->phy);
  255. tegra_ehci_restart(hcd);
  256. return 0;
  257. }
  258. static void tegra_ehci_shutdown(struct usb_hcd *hcd)
  259. {
  260. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  261. /* ehci_shutdown touches the USB controller registers, make sure
  262. * controller has clocks to it */
  263. if (!tegra->host_resumed)
  264. tegra_ehci_power_up(hcd);
  265. ehci_shutdown(hcd);
  266. }
  267. static int tegra_ehci_setup(struct usb_hcd *hcd)
  268. {
  269. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  270. int retval;
  271. /* EHCI registers start at offset 0x100 */
  272. ehci->caps = hcd->regs + 0x100;
  273. ehci->regs = hcd->regs + 0x100 +
  274. HC_LENGTH(readl(&ehci->caps->hc_capbase));
  275. dbg_hcs_params(ehci, "reset");
  276. dbg_hcc_params(ehci, "reset");
  277. /* cache this readonly data; minimize chip reads */
  278. ehci->hcs_params = readl(&ehci->caps->hcs_params);
  279. /* switch to host mode */
  280. hcd->has_tt = 1;
  281. ehci_reset(ehci);
  282. retval = ehci_halt(ehci);
  283. if (retval)
  284. return retval;
  285. /* data structure init */
  286. retval = ehci_init(hcd);
  287. if (retval)
  288. return retval;
  289. ehci->sbrn = 0x20;
  290. ehci_port_power(ehci, 1);
  291. return retval;
  292. }
  293. #ifdef CONFIG_PM
  294. static int tegra_ehci_bus_suspend(struct usb_hcd *hcd)
  295. {
  296. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  297. int error_status = 0;
  298. error_status = ehci_bus_suspend(hcd);
  299. if (!error_status && tegra->power_down_on_bus_suspend) {
  300. tegra_usb_suspend(hcd);
  301. tegra->bus_suspended = 1;
  302. }
  303. return error_status;
  304. }
  305. static int tegra_ehci_bus_resume(struct usb_hcd *hcd)
  306. {
  307. struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
  308. if (tegra->bus_suspended && tegra->power_down_on_bus_suspend) {
  309. tegra_usb_resume(hcd);
  310. tegra->bus_suspended = 0;
  311. }
  312. tegra_usb_phy_preresume(tegra->phy);
  313. tegra->port_resuming = 1;
  314. return ehci_bus_resume(hcd);
  315. }
  316. #endif
  317. struct temp_buffer {
  318. void *kmalloc_ptr;
  319. void *old_xfer_buffer;
  320. u8 data[0];
  321. };
  322. static void free_temp_buffer(struct urb *urb)
  323. {
  324. enum dma_data_direction dir;
  325. struct temp_buffer *temp;
  326. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  327. return;
  328. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  329. temp = container_of(urb->transfer_buffer, struct temp_buffer,
  330. data);
  331. if (dir == DMA_FROM_DEVICE)
  332. memcpy(temp->old_xfer_buffer, temp->data,
  333. urb->transfer_buffer_length);
  334. urb->transfer_buffer = temp->old_xfer_buffer;
  335. kfree(temp->kmalloc_ptr);
  336. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  337. }
  338. static int alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
  339. {
  340. enum dma_data_direction dir;
  341. struct temp_buffer *temp, *kmalloc_ptr;
  342. size_t kmalloc_size;
  343. if (urb->num_sgs || urb->sg ||
  344. urb->transfer_buffer_length == 0 ||
  345. !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
  346. return 0;
  347. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  348. /* Allocate a buffer with enough padding for alignment */
  349. kmalloc_size = urb->transfer_buffer_length +
  350. sizeof(struct temp_buffer) + TEGRA_USB_DMA_ALIGN - 1;
  351. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  352. if (!kmalloc_ptr)
  353. return -ENOMEM;
  354. /* Position our struct temp_buffer such that data is aligned */
  355. temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
  356. temp->kmalloc_ptr = kmalloc_ptr;
  357. temp->old_xfer_buffer = urb->transfer_buffer;
  358. if (dir == DMA_TO_DEVICE)
  359. memcpy(temp->data, urb->transfer_buffer,
  360. urb->transfer_buffer_length);
  361. urb->transfer_buffer = temp->data;
  362. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  363. return 0;
  364. }
  365. static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  366. gfp_t mem_flags)
  367. {
  368. int ret;
  369. ret = alloc_temp_buffer(urb, mem_flags);
  370. if (ret)
  371. return ret;
  372. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  373. if (ret)
  374. free_temp_buffer(urb);
  375. return ret;
  376. }
  377. static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  378. {
  379. usb_hcd_unmap_urb_for_dma(hcd, urb);
  380. free_temp_buffer(urb);
  381. }
  382. static const struct hc_driver tegra_ehci_hc_driver = {
  383. .description = hcd_name,
  384. .product_desc = "Tegra EHCI Host Controller",
  385. .hcd_priv_size = sizeof(struct ehci_hcd),
  386. .flags = HCD_USB2 | HCD_MEMORY,
  387. .reset = tegra_ehci_setup,
  388. .irq = ehci_irq,
  389. .start = ehci_run,
  390. .stop = ehci_stop,
  391. .shutdown = tegra_ehci_shutdown,
  392. .urb_enqueue = ehci_urb_enqueue,
  393. .urb_dequeue = ehci_urb_dequeue,
  394. .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
  395. .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
  396. .endpoint_disable = ehci_endpoint_disable,
  397. .endpoint_reset = ehci_endpoint_reset,
  398. .get_frame_number = ehci_get_frame,
  399. .hub_status_data = ehci_hub_status_data,
  400. .hub_control = tegra_ehci_hub_control,
  401. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  402. #ifdef CONFIG_PM
  403. .bus_suspend = tegra_ehci_bus_suspend,
  404. .bus_resume = tegra_ehci_bus_resume,
  405. #endif
  406. .relinquish_port = ehci_relinquish_port,
  407. .port_handed_over = ehci_port_handed_over,
  408. };
  409. static int tegra_ehci_probe(struct platform_device *pdev)
  410. {
  411. struct resource *res;
  412. struct usb_hcd *hcd;
  413. struct tegra_ehci_hcd *tegra;
  414. struct tegra_ehci_platform_data *pdata;
  415. int err = 0;
  416. int irq;
  417. int instance = pdev->id;
  418. pdata = pdev->dev.platform_data;
  419. if (!pdata) {
  420. dev_err(&pdev->dev, "Platform data missing\n");
  421. return -EINVAL;
  422. }
  423. tegra = kzalloc(sizeof(struct tegra_ehci_hcd), GFP_KERNEL);
  424. if (!tegra)
  425. return -ENOMEM;
  426. hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
  427. dev_name(&pdev->dev));
  428. if (!hcd) {
  429. dev_err(&pdev->dev, "Unable to create HCD\n");
  430. err = -ENOMEM;
  431. goto fail_hcd;
  432. }
  433. platform_set_drvdata(pdev, tegra);
  434. tegra->clk = clk_get(&pdev->dev, NULL);
  435. if (IS_ERR(tegra->clk)) {
  436. dev_err(&pdev->dev, "Can't get ehci clock\n");
  437. err = PTR_ERR(tegra->clk);
  438. goto fail_clk;
  439. }
  440. err = clk_enable(tegra->clk);
  441. if (err)
  442. goto fail_clken;
  443. tegra->emc_clk = clk_get(&pdev->dev, "emc");
  444. if (IS_ERR(tegra->emc_clk)) {
  445. dev_err(&pdev->dev, "Can't get emc clock\n");
  446. err = PTR_ERR(tegra->emc_clk);
  447. goto fail_emc_clk;
  448. }
  449. clk_enable(tegra->emc_clk);
  450. clk_set_rate(tegra->emc_clk, 400000000);
  451. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  452. if (!res) {
  453. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  454. err = -ENXIO;
  455. goto fail_io;
  456. }
  457. hcd->rsrc_start = res->start;
  458. hcd->rsrc_len = resource_size(res);
  459. hcd->regs = ioremap(res->start, resource_size(res));
  460. if (!hcd->regs) {
  461. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  462. err = -ENOMEM;
  463. goto fail_io;
  464. }
  465. tegra->phy = tegra_usb_phy_open(instance, hcd->regs, pdata->phy_config,
  466. TEGRA_USB_PHY_MODE_HOST);
  467. if (IS_ERR(tegra->phy)) {
  468. dev_err(&pdev->dev, "Failed to open USB phy\n");
  469. err = -ENXIO;
  470. goto fail_phy;
  471. }
  472. err = tegra_usb_phy_power_on(tegra->phy);
  473. if (err) {
  474. dev_err(&pdev->dev, "Failed to power on the phy\n");
  475. goto fail;
  476. }
  477. tegra->host_resumed = 1;
  478. tegra->power_down_on_bus_suspend = pdata->power_down_on_bus_suspend;
  479. tegra->ehci = hcd_to_ehci(hcd);
  480. irq = platform_get_irq(pdev, 0);
  481. if (!irq) {
  482. dev_err(&pdev->dev, "Failed to get IRQ\n");
  483. err = -ENODEV;
  484. goto fail;
  485. }
  486. set_irq_flags(irq, IRQF_VALID);
  487. #ifdef CONFIG_USB_OTG_UTILS
  488. if (pdata->operating_mode == TEGRA_USB_OTG) {
  489. tegra->transceiver = otg_get_transceiver();
  490. if (tegra->transceiver)
  491. otg_set_host(tegra->transceiver, &hcd->self);
  492. }
  493. #endif
  494. err = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
  495. if (err) {
  496. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  497. goto fail;
  498. }
  499. return err;
  500. fail:
  501. #ifdef CONFIG_USB_OTG_UTILS
  502. if (tegra->transceiver) {
  503. otg_set_host(tegra->transceiver, NULL);
  504. otg_put_transceiver(tegra->transceiver);
  505. }
  506. #endif
  507. tegra_usb_phy_close(tegra->phy);
  508. fail_phy:
  509. iounmap(hcd->regs);
  510. fail_io:
  511. clk_disable(tegra->emc_clk);
  512. clk_put(tegra->emc_clk);
  513. fail_emc_clk:
  514. clk_disable(tegra->clk);
  515. fail_clken:
  516. clk_put(tegra->clk);
  517. fail_clk:
  518. usb_put_hcd(hcd);
  519. fail_hcd:
  520. kfree(tegra);
  521. return err;
  522. }
  523. #ifdef CONFIG_PM
  524. static int tegra_ehci_resume(struct platform_device *pdev)
  525. {
  526. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  527. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  528. if (tegra->bus_suspended)
  529. return 0;
  530. return tegra_usb_resume(hcd);
  531. }
  532. static int tegra_ehci_suspend(struct platform_device *pdev, pm_message_t state)
  533. {
  534. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  535. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  536. if (tegra->bus_suspended)
  537. return 0;
  538. if (time_before(jiffies, tegra->ehci->next_statechange))
  539. msleep(10);
  540. return tegra_usb_suspend(hcd);
  541. }
  542. #endif
  543. static int tegra_ehci_remove(struct platform_device *pdev)
  544. {
  545. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  546. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  547. if (tegra == NULL || hcd == NULL)
  548. return -EINVAL;
  549. #ifdef CONFIG_USB_OTG_UTILS
  550. if (tegra->transceiver) {
  551. otg_set_host(tegra->transceiver, NULL);
  552. otg_put_transceiver(tegra->transceiver);
  553. }
  554. #endif
  555. usb_remove_hcd(hcd);
  556. usb_put_hcd(hcd);
  557. tegra_usb_phy_close(tegra->phy);
  558. iounmap(hcd->regs);
  559. clk_disable(tegra->clk);
  560. clk_put(tegra->clk);
  561. clk_disable(tegra->emc_clk);
  562. clk_put(tegra->emc_clk);
  563. kfree(tegra);
  564. return 0;
  565. }
  566. static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
  567. {
  568. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  569. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  570. if (hcd->driver->shutdown)
  571. hcd->driver->shutdown(hcd);
  572. }
  573. static struct platform_driver tegra_ehci_driver = {
  574. .probe = tegra_ehci_probe,
  575. .remove = tegra_ehci_remove,
  576. #ifdef CONFIG_PM
  577. .suspend = tegra_ehci_suspend,
  578. .resume = tegra_ehci_resume,
  579. #endif
  580. .shutdown = tegra_ehci_hcd_shutdown,
  581. .driver = {
  582. .name = "tegra-ehci",
  583. }
  584. };