ehci-tegra.c 22 KB

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