ci_hdrc_tegra.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016, NVIDIA Corporation
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/reset.h>
  12. #include <linux/usb.h>
  13. #include <linux/usb/chipidea.h>
  14. #include <linux/usb/hcd.h>
  15. #include <linux/usb/of.h>
  16. #include <linux/usb/phy.h>
  17. #include <soc/tegra/common.h>
  18. #include "../host/ehci.h"
  19. #include "ci.h"
  20. struct tegra_usb {
  21. struct ci_hdrc_platform_data data;
  22. struct platform_device *dev;
  23. const struct tegra_usb_soc_info *soc;
  24. struct usb_phy *phy;
  25. struct clk *clk;
  26. bool needs_double_reset;
  27. };
  28. struct tegra_usb_soc_info {
  29. unsigned long flags;
  30. unsigned int txfifothresh;
  31. enum usb_dr_mode dr_mode;
  32. };
  33. static const struct tegra_usb_soc_info tegra20_ehci_soc_info = {
  34. .flags = CI_HDRC_REQUIRES_ALIGNED_DMA |
  35. CI_HDRC_OVERRIDE_PHY_CONTROL |
  36. CI_HDRC_SUPPORTS_RUNTIME_PM,
  37. .dr_mode = USB_DR_MODE_HOST,
  38. .txfifothresh = 10,
  39. };
  40. static const struct tegra_usb_soc_info tegra30_ehci_soc_info = {
  41. .flags = CI_HDRC_REQUIRES_ALIGNED_DMA |
  42. CI_HDRC_OVERRIDE_PHY_CONTROL |
  43. CI_HDRC_SUPPORTS_RUNTIME_PM,
  44. .dr_mode = USB_DR_MODE_HOST,
  45. .txfifothresh = 16,
  46. };
  47. static const struct tegra_usb_soc_info tegra20_udc_soc_info = {
  48. .flags = CI_HDRC_REQUIRES_ALIGNED_DMA |
  49. CI_HDRC_OVERRIDE_PHY_CONTROL |
  50. CI_HDRC_SUPPORTS_RUNTIME_PM,
  51. .dr_mode = USB_DR_MODE_UNKNOWN,
  52. .txfifothresh = 10,
  53. };
  54. static const struct tegra_usb_soc_info tegra30_udc_soc_info = {
  55. .flags = CI_HDRC_REQUIRES_ALIGNED_DMA |
  56. CI_HDRC_OVERRIDE_PHY_CONTROL |
  57. CI_HDRC_SUPPORTS_RUNTIME_PM,
  58. .dr_mode = USB_DR_MODE_UNKNOWN,
  59. .txfifothresh = 16,
  60. };
  61. static const struct of_device_id tegra_usb_of_match[] = {
  62. {
  63. .compatible = "nvidia,tegra20-ehci",
  64. .data = &tegra20_ehci_soc_info,
  65. }, {
  66. .compatible = "nvidia,tegra30-ehci",
  67. .data = &tegra30_ehci_soc_info,
  68. }, {
  69. .compatible = "nvidia,tegra20-udc",
  70. .data = &tegra20_udc_soc_info,
  71. }, {
  72. .compatible = "nvidia,tegra30-udc",
  73. .data = &tegra30_udc_soc_info,
  74. }, {
  75. .compatible = "nvidia,tegra114-udc",
  76. .data = &tegra30_udc_soc_info,
  77. }, {
  78. .compatible = "nvidia,tegra124-udc",
  79. .data = &tegra30_udc_soc_info,
  80. }, {
  81. /* sentinel */
  82. }
  83. };
  84. MODULE_DEVICE_TABLE(of, tegra_usb_of_match);
  85. static int tegra_usb_reset_controller(struct device *dev)
  86. {
  87. struct reset_control *rst, *rst_utmi;
  88. struct device_node *phy_np;
  89. int err;
  90. rst = devm_reset_control_get_shared(dev, "usb");
  91. if (IS_ERR(rst)) {
  92. dev_err(dev, "can't get ehci reset: %pe\n", rst);
  93. return PTR_ERR(rst);
  94. }
  95. phy_np = of_parse_phandle(dev->of_node, "nvidia,phy", 0);
  96. if (!phy_np)
  97. return -ENOENT;
  98. /*
  99. * The 1st USB controller contains some UTMI pad registers that are
  100. * global for all the controllers on the chip. Those registers are
  101. * also cleared when reset is asserted to the 1st controller.
  102. */
  103. rst_utmi = of_reset_control_get_shared(phy_np, "utmi-pads");
  104. if (IS_ERR(rst_utmi)) {
  105. dev_warn(dev, "can't get utmi-pads reset from the PHY\n");
  106. dev_warn(dev, "continuing, but please update your DT\n");
  107. } else {
  108. /*
  109. * PHY driver performs UTMI-pads reset in a case of a
  110. * non-legacy DT.
  111. */
  112. reset_control_put(rst_utmi);
  113. }
  114. of_node_put(phy_np);
  115. /* reset control is shared, hence initialize it first */
  116. err = reset_control_deassert(rst);
  117. if (err)
  118. return err;
  119. err = reset_control_assert(rst);
  120. if (err)
  121. return err;
  122. udelay(1);
  123. err = reset_control_deassert(rst);
  124. if (err)
  125. return err;
  126. return 0;
  127. }
  128. static int tegra_usb_notify_event(struct ci_hdrc *ci, unsigned int event)
  129. {
  130. struct tegra_usb *usb = dev_get_drvdata(ci->dev->parent);
  131. struct ehci_hcd *ehci;
  132. switch (event) {
  133. case CI_HDRC_CONTROLLER_RESET_EVENT:
  134. if (ci->hcd) {
  135. ehci = hcd_to_ehci(ci->hcd);
  136. ehci->has_tdi_phy_lpm = false;
  137. ehci_writel(ehci, usb->soc->txfifothresh << 16,
  138. &ehci->regs->txfill_tuning);
  139. }
  140. break;
  141. }
  142. return 0;
  143. }
  144. static int tegra_usb_internal_port_reset(struct ehci_hcd *ehci,
  145. u32 __iomem *portsc_reg,
  146. unsigned long *flags)
  147. {
  148. u32 saved_usbintr, temp;
  149. unsigned int i, tries;
  150. int retval = 0;
  151. saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
  152. /* disable USB interrupt */
  153. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  154. spin_unlock_irqrestore(&ehci->lock, *flags);
  155. /*
  156. * Here we have to do Port Reset at most twice for
  157. * Port Enable bit to be set.
  158. */
  159. for (i = 0; i < 2; i++) {
  160. temp = ehci_readl(ehci, portsc_reg);
  161. temp |= PORT_RESET;
  162. ehci_writel(ehci, temp, portsc_reg);
  163. fsleep(10000);
  164. temp &= ~PORT_RESET;
  165. ehci_writel(ehci, temp, portsc_reg);
  166. fsleep(1000);
  167. tries = 100;
  168. do {
  169. fsleep(1000);
  170. /*
  171. * Up to this point, Port Enable bit is
  172. * expected to be set after 2 ms waiting.
  173. * USB1 usually takes extra 45 ms, for safety,
  174. * we take 100 ms as timeout.
  175. */
  176. temp = ehci_readl(ehci, portsc_reg);
  177. } while (!(temp & PORT_PE) && tries--);
  178. if (temp & PORT_PE)
  179. break;
  180. }
  181. if (i == 2)
  182. retval = -ETIMEDOUT;
  183. /*
  184. * Clear Connect Status Change bit if it's set.
  185. * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
  186. */
  187. if (temp & PORT_CSC)
  188. ehci_writel(ehci, PORT_CSC, portsc_reg);
  189. /*
  190. * Write to clear any interrupt status bits that might be set
  191. * during port reset.
  192. */
  193. temp = ehci_readl(ehci, &ehci->regs->status);
  194. ehci_writel(ehci, temp, &ehci->regs->status);
  195. /* restore original interrupt-enable bits */
  196. spin_lock_irqsave(&ehci->lock, *flags);
  197. ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
  198. return retval;
  199. }
  200. static int tegra_ehci_hub_control(struct ci_hdrc *ci, u16 typeReq, u16 wValue,
  201. u16 wIndex, char *buf, u16 wLength,
  202. bool *done, unsigned long *flags)
  203. {
  204. struct tegra_usb *usb = dev_get_drvdata(ci->dev->parent);
  205. struct ehci_hcd *ehci = hcd_to_ehci(ci->hcd);
  206. u32 __iomem *status_reg;
  207. int retval = 0;
  208. status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
  209. switch (typeReq) {
  210. case SetPortFeature:
  211. if (wValue != USB_PORT_FEAT_RESET || !usb->needs_double_reset)
  212. break;
  213. /* for USB1 port we need to issue Port Reset twice internally */
  214. retval = tegra_usb_internal_port_reset(ehci, status_reg, flags);
  215. *done = true;
  216. break;
  217. }
  218. return retval;
  219. }
  220. static void tegra_usb_enter_lpm(struct ci_hdrc *ci, bool enable)
  221. {
  222. /*
  223. * Touching any register which belongs to AHB clock domain will
  224. * hang CPU if USB controller is put into low power mode because
  225. * AHB USB clock is gated on Tegra in the LPM.
  226. *
  227. * Tegra PHY has a separate register for checking the clock status
  228. * and usb_phy_set_suspend() takes care of gating/ungating the clocks
  229. * and restoring the PHY state on Tegra. Hence DEVLC/PORTSC registers
  230. * shouldn't be touched directly by the CI driver.
  231. */
  232. usb_phy_set_suspend(ci->usb_phy, enable);
  233. }
  234. static int tegra_usb_probe(struct platform_device *pdev)
  235. {
  236. const struct tegra_usb_soc_info *soc;
  237. struct tegra_usb *usb;
  238. int err;
  239. usb = devm_kzalloc(&pdev->dev, sizeof(*usb), GFP_KERNEL);
  240. if (!usb)
  241. return -ENOMEM;
  242. platform_set_drvdata(pdev, usb);
  243. soc = of_device_get_match_data(&pdev->dev);
  244. if (!soc) {
  245. dev_err(&pdev->dev, "failed to match OF data\n");
  246. return -EINVAL;
  247. }
  248. usb->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "nvidia,phy", 0);
  249. if (IS_ERR(usb->phy))
  250. return dev_err_probe(&pdev->dev, PTR_ERR(usb->phy),
  251. "failed to get PHY");
  252. usb->clk = devm_clk_get(&pdev->dev, NULL);
  253. if (IS_ERR(usb->clk))
  254. return dev_err_probe(&pdev->dev, PTR_ERR(usb->clk),
  255. "failed to get clock");
  256. err = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
  257. if (err)
  258. return err;
  259. pm_runtime_enable(&pdev->dev);
  260. err = pm_runtime_resume_and_get(&pdev->dev);
  261. if (err)
  262. return err;
  263. if (device_property_present(&pdev->dev, "nvidia,needs-double-reset"))
  264. usb->needs_double_reset = true;
  265. err = tegra_usb_reset_controller(&pdev->dev);
  266. if (err) {
  267. dev_err_probe(&pdev->dev, err, "failed to reset controller");
  268. goto fail_power_off;
  269. }
  270. /*
  271. * USB controller registers shouldn't be touched before PHY is
  272. * initialized, otherwise CPU will hang because clocks are gated.
  273. * PHY driver controls gating of internal USB clocks on Tegra.
  274. */
  275. err = usb_phy_init(usb->phy);
  276. if (err)
  277. goto fail_power_off;
  278. /* setup and register ChipIdea HDRC device */
  279. usb->soc = soc;
  280. usb->data.name = "tegra-usb";
  281. usb->data.flags = soc->flags;
  282. usb->data.usb_phy = usb->phy;
  283. usb->data.dr_mode = soc->dr_mode;
  284. usb->data.capoffset = DEF_CAPOFFSET;
  285. usb->data.enter_lpm = tegra_usb_enter_lpm;
  286. usb->data.hub_control = tegra_ehci_hub_control;
  287. usb->data.notify_event = tegra_usb_notify_event;
  288. /* Tegra PHY driver currently doesn't support LPM for ULPI */
  289. if (of_usb_get_phy_mode(pdev->dev.of_node) == USBPHY_INTERFACE_MODE_ULPI)
  290. usb->data.flags &= ~CI_HDRC_SUPPORTS_RUNTIME_PM;
  291. usb->dev = ci_hdrc_add_device(&pdev->dev, pdev->resource,
  292. pdev->num_resources, &usb->data);
  293. if (IS_ERR(usb->dev)) {
  294. err = dev_err_probe(&pdev->dev, PTR_ERR(usb->dev),
  295. "failed to add HDRC device");
  296. goto phy_shutdown;
  297. }
  298. return 0;
  299. phy_shutdown:
  300. usb_phy_shutdown(usb->phy);
  301. fail_power_off:
  302. pm_runtime_put_sync_suspend(&pdev->dev);
  303. pm_runtime_force_suspend(&pdev->dev);
  304. return err;
  305. }
  306. static void tegra_usb_remove(struct platform_device *pdev)
  307. {
  308. struct tegra_usb *usb = platform_get_drvdata(pdev);
  309. ci_hdrc_remove_device(usb->dev);
  310. usb_phy_shutdown(usb->phy);
  311. pm_runtime_put_sync_suspend(&pdev->dev);
  312. pm_runtime_force_suspend(&pdev->dev);
  313. }
  314. static int tegra_usb_runtime_resume(struct device *dev)
  315. {
  316. struct tegra_usb *usb = dev_get_drvdata(dev);
  317. int err;
  318. err = clk_prepare_enable(usb->clk);
  319. if (err < 0) {
  320. dev_err(dev, "failed to enable clock: %d\n", err);
  321. return err;
  322. }
  323. return 0;
  324. }
  325. static int tegra_usb_runtime_suspend(struct device *dev)
  326. {
  327. struct tegra_usb *usb = dev_get_drvdata(dev);
  328. clk_disable_unprepare(usb->clk);
  329. return 0;
  330. }
  331. static const struct dev_pm_ops tegra_usb_pm = {
  332. RUNTIME_PM_OPS(tegra_usb_runtime_suspend, tegra_usb_runtime_resume, NULL)
  333. };
  334. static struct platform_driver tegra_usb_driver = {
  335. .driver = {
  336. .name = "tegra-usb",
  337. .of_match_table = tegra_usb_of_match,
  338. .pm = pm_ptr(&tegra_usb_pm),
  339. },
  340. .probe = tegra_usb_probe,
  341. .remove_new = tegra_usb_remove,
  342. };
  343. module_platform_driver(tegra_usb_driver);
  344. MODULE_DESCRIPTION("NVIDIA Tegra USB driver");
  345. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  346. MODULE_LICENSE("GPL v2");