ci_hdrc_tegra.c 10 KB

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