ehci-tegra.c 22 KB

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