ehci-tegra.c 22 KB

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