ehci-tegra.c 20 KB

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