ehci-tegra.c 21 KB

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