ehci-tegra.c 22 KB

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