ehci-tegra.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * EHCI-compliant USB host controller driver for NVIDIA Tegra SoCs
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. * Copyright (C) 2009 NVIDIA Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/platform_data/tegra_usb.h>
  21. #include <linux/irq.h>
  22. #include <linux/usb/otg.h>
  23. #include <linux/gpio.h>
  24. #include <linux/of.h>
  25. #include <linux/of_gpio.h>
  26. #include <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 otg_transceiver *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 temp_buffer {
  387. void *kmalloc_ptr;
  388. void *old_xfer_buffer;
  389. u8 data[0];
  390. };
  391. static void free_temp_buffer(struct urb *urb)
  392. {
  393. enum dma_data_direction dir;
  394. struct temp_buffer *temp;
  395. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  396. return;
  397. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  398. temp = container_of(urb->transfer_buffer, struct temp_buffer,
  399. data);
  400. if (dir == DMA_FROM_DEVICE)
  401. memcpy(temp->old_xfer_buffer, temp->data,
  402. urb->transfer_buffer_length);
  403. urb->transfer_buffer = temp->old_xfer_buffer;
  404. kfree(temp->kmalloc_ptr);
  405. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  406. }
  407. static int alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
  408. {
  409. enum dma_data_direction dir;
  410. struct temp_buffer *temp, *kmalloc_ptr;
  411. size_t kmalloc_size;
  412. if (urb->num_sgs || urb->sg ||
  413. urb->transfer_buffer_length == 0 ||
  414. !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
  415. return 0;
  416. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  417. /* Allocate a buffer with enough padding for alignment */
  418. kmalloc_size = urb->transfer_buffer_length +
  419. sizeof(struct temp_buffer) + TEGRA_USB_DMA_ALIGN - 1;
  420. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  421. if (!kmalloc_ptr)
  422. return -ENOMEM;
  423. /* Position our struct temp_buffer such that data is aligned */
  424. temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
  425. temp->kmalloc_ptr = kmalloc_ptr;
  426. temp->old_xfer_buffer = urb->transfer_buffer;
  427. if (dir == DMA_TO_DEVICE)
  428. memcpy(temp->data, urb->transfer_buffer,
  429. urb->transfer_buffer_length);
  430. urb->transfer_buffer = temp->data;
  431. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  432. return 0;
  433. }
  434. static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  435. gfp_t mem_flags)
  436. {
  437. int ret;
  438. ret = alloc_temp_buffer(urb, mem_flags);
  439. if (ret)
  440. return ret;
  441. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  442. if (ret)
  443. free_temp_buffer(urb);
  444. return ret;
  445. }
  446. static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  447. {
  448. usb_hcd_unmap_urb_for_dma(hcd, urb);
  449. free_temp_buffer(urb);
  450. }
  451. static const struct hc_driver tegra_ehci_hc_driver = {
  452. .description = hcd_name,
  453. .product_desc = "Tegra EHCI Host Controller",
  454. .hcd_priv_size = sizeof(struct ehci_hcd),
  455. .flags = HCD_USB2 | HCD_MEMORY,
  456. .reset = tegra_ehci_setup,
  457. .irq = ehci_irq,
  458. .start = ehci_run,
  459. .stop = ehci_stop,
  460. .shutdown = tegra_ehci_shutdown,
  461. .urb_enqueue = ehci_urb_enqueue,
  462. .urb_dequeue = ehci_urb_dequeue,
  463. .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
  464. .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
  465. .endpoint_disable = ehci_endpoint_disable,
  466. .endpoint_reset = ehci_endpoint_reset,
  467. .get_frame_number = ehci_get_frame,
  468. .hub_status_data = ehci_hub_status_data,
  469. .hub_control = tegra_ehci_hub_control,
  470. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  471. #ifdef CONFIG_PM
  472. .bus_suspend = tegra_ehci_bus_suspend,
  473. .bus_resume = tegra_ehci_bus_resume,
  474. #endif
  475. .relinquish_port = ehci_relinquish_port,
  476. .port_handed_over = ehci_port_handed_over,
  477. };
  478. static int setup_vbus_gpio(struct platform_device *pdev)
  479. {
  480. int err = 0;
  481. int gpio;
  482. if (!pdev->dev.of_node)
  483. return 0;
  484. gpio = of_get_named_gpio(pdev->dev.of_node, "nvidia,vbus-gpio", 0);
  485. if (!gpio_is_valid(gpio))
  486. return 0;
  487. err = gpio_request(gpio, "vbus_gpio");
  488. if (err) {
  489. dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
  490. return err;
  491. }
  492. err = gpio_direction_output(gpio, 1);
  493. if (err) {
  494. dev_err(&pdev->dev, "can't enable vbus\n");
  495. return err;
  496. }
  497. gpio_set_value(gpio, 1);
  498. return err;
  499. }
  500. static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
  501. static int tegra_ehci_probe(struct platform_device *pdev)
  502. {
  503. struct resource *res;
  504. struct usb_hcd *hcd;
  505. struct tegra_ehci_hcd *tegra;
  506. struct tegra_ehci_platform_data *pdata;
  507. int err = 0;
  508. int irq;
  509. int instance = pdev->id;
  510. pdata = pdev->dev.platform_data;
  511. if (!pdata) {
  512. dev_err(&pdev->dev, "Platform data missing\n");
  513. return -EINVAL;
  514. }
  515. /* Right now device-tree probed devices don't get dma_mask set.
  516. * Since shared usb code relies on it, set it here for now.
  517. * Once we have dma capability bindings this can go away.
  518. */
  519. if (!pdev->dev.dma_mask)
  520. pdev->dev.dma_mask = &tegra_ehci_dma_mask;
  521. setup_vbus_gpio(pdev);
  522. tegra = kzalloc(sizeof(struct tegra_ehci_hcd), GFP_KERNEL);
  523. if (!tegra)
  524. return -ENOMEM;
  525. hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
  526. dev_name(&pdev->dev));
  527. if (!hcd) {
  528. dev_err(&pdev->dev, "Unable to create HCD\n");
  529. err = -ENOMEM;
  530. goto fail_hcd;
  531. }
  532. platform_set_drvdata(pdev, tegra);
  533. tegra->clk = clk_get(&pdev->dev, NULL);
  534. if (IS_ERR(tegra->clk)) {
  535. dev_err(&pdev->dev, "Can't get ehci clock\n");
  536. err = PTR_ERR(tegra->clk);
  537. goto fail_clk;
  538. }
  539. err = clk_enable(tegra->clk);
  540. if (err)
  541. goto fail_clken;
  542. tegra->emc_clk = clk_get(&pdev->dev, "emc");
  543. if (IS_ERR(tegra->emc_clk)) {
  544. dev_err(&pdev->dev, "Can't get emc clock\n");
  545. err = PTR_ERR(tegra->emc_clk);
  546. goto fail_emc_clk;
  547. }
  548. clk_enable(tegra->emc_clk);
  549. clk_set_rate(tegra->emc_clk, 400000000);
  550. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  551. if (!res) {
  552. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  553. err = -ENXIO;
  554. goto fail_io;
  555. }
  556. hcd->rsrc_start = res->start;
  557. hcd->rsrc_len = resource_size(res);
  558. hcd->regs = ioremap(res->start, resource_size(res));
  559. if (!hcd->regs) {
  560. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  561. err = -ENOMEM;
  562. goto fail_io;
  563. }
  564. /* This is pretty ugly and needs to be fixed when we do only
  565. * device-tree probing. Old code relies on the platform_device
  566. * numbering that we lack for device-tree-instantiated devices.
  567. */
  568. if (instance < 0) {
  569. switch (res->start) {
  570. case TEGRA_USB_BASE:
  571. instance = 0;
  572. break;
  573. case TEGRA_USB2_BASE:
  574. instance = 1;
  575. break;
  576. case TEGRA_USB3_BASE:
  577. instance = 2;
  578. break;
  579. default:
  580. err = -ENODEV;
  581. dev_err(&pdev->dev, "unknown usb instance\n");
  582. goto fail_phy;
  583. }
  584. }
  585. tegra->phy = tegra_usb_phy_open(instance, hcd->regs, pdata->phy_config,
  586. TEGRA_USB_PHY_MODE_HOST);
  587. if (IS_ERR(tegra->phy)) {
  588. dev_err(&pdev->dev, "Failed to open USB phy\n");
  589. err = -ENXIO;
  590. goto fail_phy;
  591. }
  592. err = tegra_usb_phy_power_on(tegra->phy);
  593. if (err) {
  594. dev_err(&pdev->dev, "Failed to power on the phy\n");
  595. goto fail;
  596. }
  597. tegra->host_resumed = 1;
  598. tegra->power_down_on_bus_suspend = pdata->power_down_on_bus_suspend;
  599. tegra->ehci = hcd_to_ehci(hcd);
  600. irq = platform_get_irq(pdev, 0);
  601. if (!irq) {
  602. dev_err(&pdev->dev, "Failed to get IRQ\n");
  603. err = -ENODEV;
  604. goto fail;
  605. }
  606. set_irq_flags(irq, IRQF_VALID);
  607. #ifdef CONFIG_USB_OTG_UTILS
  608. if (pdata->operating_mode == TEGRA_USB_OTG) {
  609. tegra->transceiver = otg_get_transceiver();
  610. if (tegra->transceiver)
  611. otg_set_host(tegra->transceiver, &hcd->self);
  612. }
  613. #endif
  614. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  615. if (err) {
  616. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  617. goto fail;
  618. }
  619. return err;
  620. fail:
  621. #ifdef CONFIG_USB_OTG_UTILS
  622. if (tegra->transceiver) {
  623. otg_set_host(tegra->transceiver, NULL);
  624. otg_put_transceiver(tegra->transceiver);
  625. }
  626. #endif
  627. tegra_usb_phy_close(tegra->phy);
  628. fail_phy:
  629. iounmap(hcd->regs);
  630. fail_io:
  631. clk_disable(tegra->emc_clk);
  632. clk_put(tegra->emc_clk);
  633. fail_emc_clk:
  634. clk_disable(tegra->clk);
  635. fail_clken:
  636. clk_put(tegra->clk);
  637. fail_clk:
  638. usb_put_hcd(hcd);
  639. fail_hcd:
  640. kfree(tegra);
  641. return err;
  642. }
  643. #ifdef CONFIG_PM
  644. static int tegra_ehci_resume(struct platform_device *pdev)
  645. {
  646. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  647. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  648. if (tegra->bus_suspended)
  649. return 0;
  650. return tegra_usb_resume(hcd);
  651. }
  652. static int tegra_ehci_suspend(struct platform_device *pdev, pm_message_t state)
  653. {
  654. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  655. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  656. if (tegra->bus_suspended)
  657. return 0;
  658. if (time_before(jiffies, tegra->ehci->next_statechange))
  659. msleep(10);
  660. return tegra_usb_suspend(hcd);
  661. }
  662. #endif
  663. static int tegra_ehci_remove(struct platform_device *pdev)
  664. {
  665. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  666. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  667. if (tegra == NULL || hcd == NULL)
  668. return -EINVAL;
  669. #ifdef CONFIG_USB_OTG_UTILS
  670. if (tegra->transceiver) {
  671. otg_set_host(tegra->transceiver, NULL);
  672. otg_put_transceiver(tegra->transceiver);
  673. }
  674. #endif
  675. usb_remove_hcd(hcd);
  676. usb_put_hcd(hcd);
  677. tegra_usb_phy_close(tegra->phy);
  678. iounmap(hcd->regs);
  679. clk_disable(tegra->clk);
  680. clk_put(tegra->clk);
  681. clk_disable(tegra->emc_clk);
  682. clk_put(tegra->emc_clk);
  683. kfree(tegra);
  684. return 0;
  685. }
  686. static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
  687. {
  688. struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
  689. struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
  690. if (hcd->driver->shutdown)
  691. hcd->driver->shutdown(hcd);
  692. }
  693. static struct of_device_id tegra_ehci_of_match[] __devinitdata = {
  694. { .compatible = "nvidia,tegra20-ehci", },
  695. { },
  696. };
  697. static struct platform_driver tegra_ehci_driver = {
  698. .probe = tegra_ehci_probe,
  699. .remove = tegra_ehci_remove,
  700. #ifdef CONFIG_PM
  701. .suspend = tegra_ehci_suspend,
  702. .resume = tegra_ehci_resume,
  703. #endif
  704. .shutdown = tegra_ehci_hcd_shutdown,
  705. .driver = {
  706. .name = "tegra-ehci",
  707. .of_match_table = tegra_ehci_of_match,
  708. }
  709. };