dwc3-xilinx.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dwc3-xilinx.c - Xilinx DWC3 controller specific glue driver
  4. *
  5. * Authors: Manish Narani <[email protected]>
  6. * Anurag Kumar Vulisha <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/clk.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/reset.h>
  20. #include <linux/of_address.h>
  21. #include <linux/delay.h>
  22. #include <linux/firmware/xlnx-zynqmp.h>
  23. #include <linux/io.h>
  24. #include <linux/phy/phy.h>
  25. /* USB phy reset mask register */
  26. #define XLNX_USB_PHY_RST_EN 0x001C
  27. #define XLNX_PHY_RST_MASK 0x1
  28. /* Xilinx USB 3.0 IP Register */
  29. #define XLNX_USB_TRAFFIC_ROUTE_CONFIG 0x005C
  30. #define XLNX_USB_TRAFFIC_ROUTE_FPD 0x1
  31. /* Versal USB Reset ID */
  32. #define VERSAL_USB_RESET_ID 0xC104036
  33. #define XLNX_USB_FPD_PIPE_CLK 0x7c
  34. #define PIPE_CLK_DESELECT 1
  35. #define PIPE_CLK_SELECT 0
  36. #define XLNX_USB_FPD_POWER_PRSNT 0x80
  37. #define FPD_POWER_PRSNT_OPTION BIT(0)
  38. struct dwc3_xlnx {
  39. int num_clocks;
  40. struct clk_bulk_data *clks;
  41. struct device *dev;
  42. void __iomem *regs;
  43. int (*pltfm_init)(struct dwc3_xlnx *data);
  44. struct phy *usb3_phy;
  45. };
  46. static void dwc3_xlnx_mask_phy_rst(struct dwc3_xlnx *priv_data, bool mask)
  47. {
  48. u32 reg;
  49. /*
  50. * Enable or disable ULPI PHY reset from USB Controller.
  51. * This does not actually reset the phy, but just controls
  52. * whether USB controller can or cannot reset ULPI PHY.
  53. */
  54. reg = readl(priv_data->regs + XLNX_USB_PHY_RST_EN);
  55. if (mask)
  56. reg &= ~XLNX_PHY_RST_MASK;
  57. else
  58. reg |= XLNX_PHY_RST_MASK;
  59. writel(reg, priv_data->regs + XLNX_USB_PHY_RST_EN);
  60. }
  61. static int dwc3_xlnx_init_versal(struct dwc3_xlnx *priv_data)
  62. {
  63. struct device *dev = priv_data->dev;
  64. int ret;
  65. dwc3_xlnx_mask_phy_rst(priv_data, false);
  66. /* Assert and De-assert reset */
  67. ret = zynqmp_pm_reset_assert(VERSAL_USB_RESET_ID,
  68. PM_RESET_ACTION_ASSERT);
  69. if (ret < 0) {
  70. dev_err_probe(dev, ret, "failed to assert Reset\n");
  71. return ret;
  72. }
  73. ret = zynqmp_pm_reset_assert(VERSAL_USB_RESET_ID,
  74. PM_RESET_ACTION_RELEASE);
  75. if (ret < 0) {
  76. dev_err_probe(dev, ret, "failed to De-assert Reset\n");
  77. return ret;
  78. }
  79. dwc3_xlnx_mask_phy_rst(priv_data, true);
  80. return 0;
  81. }
  82. static int dwc3_xlnx_init_zynqmp(struct dwc3_xlnx *priv_data)
  83. {
  84. struct device *dev = priv_data->dev;
  85. struct reset_control *crst, *hibrst, *apbrst;
  86. struct gpio_desc *reset_gpio;
  87. int ret = 0;
  88. u32 reg;
  89. priv_data->usb3_phy = devm_phy_optional_get(dev, "usb3-phy");
  90. if (IS_ERR(priv_data->usb3_phy)) {
  91. ret = PTR_ERR(priv_data->usb3_phy);
  92. dev_err_probe(dev, ret,
  93. "failed to get USB3 PHY\n");
  94. goto err;
  95. }
  96. /*
  97. * The following core resets are not required unless a USB3 PHY
  98. * is used, and the subsequent register settings are not required
  99. * unless a core reset is performed (they should be set properly
  100. * by the first-stage boot loader, but may be reverted by a core
  101. * reset). They may also break the configuration if USB3 is actually
  102. * in use but the usb3-phy entry is missing from the device tree.
  103. * Therefore, skip these operations in this case.
  104. */
  105. if (!priv_data->usb3_phy)
  106. goto skip_usb3_phy;
  107. crst = devm_reset_control_get_exclusive(dev, "usb_crst");
  108. if (IS_ERR(crst)) {
  109. ret = PTR_ERR(crst);
  110. dev_err_probe(dev, ret,
  111. "failed to get core reset signal\n");
  112. goto err;
  113. }
  114. hibrst = devm_reset_control_get_exclusive(dev, "usb_hibrst");
  115. if (IS_ERR(hibrst)) {
  116. ret = PTR_ERR(hibrst);
  117. dev_err_probe(dev, ret,
  118. "failed to get hibernation reset signal\n");
  119. goto err;
  120. }
  121. apbrst = devm_reset_control_get_exclusive(dev, "usb_apbrst");
  122. if (IS_ERR(apbrst)) {
  123. ret = PTR_ERR(apbrst);
  124. dev_err_probe(dev, ret,
  125. "failed to get APB reset signal\n");
  126. goto err;
  127. }
  128. ret = reset_control_assert(crst);
  129. if (ret < 0) {
  130. dev_err(dev, "Failed to assert core reset\n");
  131. goto err;
  132. }
  133. ret = reset_control_assert(hibrst);
  134. if (ret < 0) {
  135. dev_err(dev, "Failed to assert hibernation reset\n");
  136. goto err;
  137. }
  138. ret = reset_control_assert(apbrst);
  139. if (ret < 0) {
  140. dev_err(dev, "Failed to assert APB reset\n");
  141. goto err;
  142. }
  143. ret = phy_init(priv_data->usb3_phy);
  144. if (ret < 0) {
  145. phy_exit(priv_data->usb3_phy);
  146. goto err;
  147. }
  148. ret = reset_control_deassert(apbrst);
  149. if (ret < 0) {
  150. dev_err(dev, "Failed to release APB reset\n");
  151. goto err;
  152. }
  153. /* Set PIPE Power Present signal in FPD Power Present Register*/
  154. writel(FPD_POWER_PRSNT_OPTION, priv_data->regs + XLNX_USB_FPD_POWER_PRSNT);
  155. /* Set the PIPE Clock Select bit in FPD PIPE Clock register */
  156. writel(PIPE_CLK_SELECT, priv_data->regs + XLNX_USB_FPD_PIPE_CLK);
  157. ret = reset_control_deassert(crst);
  158. if (ret < 0) {
  159. dev_err(dev, "Failed to release core reset\n");
  160. goto err;
  161. }
  162. ret = reset_control_deassert(hibrst);
  163. if (ret < 0) {
  164. dev_err(dev, "Failed to release hibernation reset\n");
  165. goto err;
  166. }
  167. ret = phy_power_on(priv_data->usb3_phy);
  168. if (ret < 0) {
  169. phy_exit(priv_data->usb3_phy);
  170. goto err;
  171. }
  172. skip_usb3_phy:
  173. /* ulpi reset via gpio-modepin or gpio-framework driver */
  174. reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  175. if (IS_ERR(reset_gpio)) {
  176. return dev_err_probe(dev, PTR_ERR(reset_gpio),
  177. "Failed to request reset GPIO\n");
  178. }
  179. if (reset_gpio) {
  180. /* Toggle ulpi to reset the phy. */
  181. gpiod_set_value_cansleep(reset_gpio, 1);
  182. usleep_range(5000, 10000);
  183. gpiod_set_value_cansleep(reset_gpio, 0);
  184. usleep_range(5000, 10000);
  185. }
  186. /*
  187. * This routes the USB DMA traffic to go through FPD path instead
  188. * of reaching DDR directly. This traffic routing is needed to
  189. * make SMMU and CCI work with USB DMA.
  190. */
  191. if (of_dma_is_coherent(dev->of_node) || device_iommu_mapped(dev)) {
  192. reg = readl(priv_data->regs + XLNX_USB_TRAFFIC_ROUTE_CONFIG);
  193. reg |= XLNX_USB_TRAFFIC_ROUTE_FPD;
  194. writel(reg, priv_data->regs + XLNX_USB_TRAFFIC_ROUTE_CONFIG);
  195. }
  196. err:
  197. return ret;
  198. }
  199. static const struct of_device_id dwc3_xlnx_of_match[] = {
  200. {
  201. .compatible = "xlnx,zynqmp-dwc3",
  202. .data = &dwc3_xlnx_init_zynqmp,
  203. },
  204. {
  205. .compatible = "xlnx,versal-dwc3",
  206. .data = &dwc3_xlnx_init_versal,
  207. },
  208. { /* Sentinel */ }
  209. };
  210. MODULE_DEVICE_TABLE(of, dwc3_xlnx_of_match);
  211. static int dwc3_xlnx_probe(struct platform_device *pdev)
  212. {
  213. struct dwc3_xlnx *priv_data;
  214. struct device *dev = &pdev->dev;
  215. struct device_node *np = dev->of_node;
  216. const struct of_device_id *match;
  217. void __iomem *regs;
  218. int ret;
  219. priv_data = devm_kzalloc(dev, sizeof(*priv_data), GFP_KERNEL);
  220. if (!priv_data)
  221. return -ENOMEM;
  222. regs = devm_platform_ioremap_resource(pdev, 0);
  223. if (IS_ERR(regs)) {
  224. ret = PTR_ERR(regs);
  225. dev_err_probe(dev, ret, "failed to map registers\n");
  226. return ret;
  227. }
  228. match = of_match_node(dwc3_xlnx_of_match, pdev->dev.of_node);
  229. priv_data->pltfm_init = match->data;
  230. priv_data->regs = regs;
  231. priv_data->dev = dev;
  232. platform_set_drvdata(pdev, priv_data);
  233. ret = devm_clk_bulk_get_all(priv_data->dev, &priv_data->clks);
  234. if (ret < 0)
  235. return ret;
  236. priv_data->num_clocks = ret;
  237. ret = clk_bulk_prepare_enable(priv_data->num_clocks, priv_data->clks);
  238. if (ret)
  239. return ret;
  240. ret = priv_data->pltfm_init(priv_data);
  241. if (ret)
  242. goto err_clk_put;
  243. ret = of_platform_populate(np, NULL, NULL, dev);
  244. if (ret)
  245. goto err_clk_put;
  246. pm_runtime_set_active(dev);
  247. pm_runtime_enable(dev);
  248. pm_suspend_ignore_children(dev, false);
  249. pm_runtime_get_sync(dev);
  250. return 0;
  251. err_clk_put:
  252. clk_bulk_disable_unprepare(priv_data->num_clocks, priv_data->clks);
  253. return ret;
  254. }
  255. static int dwc3_xlnx_remove(struct platform_device *pdev)
  256. {
  257. struct dwc3_xlnx *priv_data = platform_get_drvdata(pdev);
  258. struct device *dev = &pdev->dev;
  259. of_platform_depopulate(dev);
  260. clk_bulk_disable_unprepare(priv_data->num_clocks, priv_data->clks);
  261. priv_data->num_clocks = 0;
  262. pm_runtime_disable(dev);
  263. pm_runtime_put_noidle(dev);
  264. pm_runtime_set_suspended(dev);
  265. return 0;
  266. }
  267. static int __maybe_unused dwc3_xlnx_runtime_suspend(struct device *dev)
  268. {
  269. struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
  270. clk_bulk_disable(priv_data->num_clocks, priv_data->clks);
  271. return 0;
  272. }
  273. static int __maybe_unused dwc3_xlnx_runtime_resume(struct device *dev)
  274. {
  275. struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
  276. return clk_bulk_enable(priv_data->num_clocks, priv_data->clks);
  277. }
  278. static int __maybe_unused dwc3_xlnx_runtime_idle(struct device *dev)
  279. {
  280. pm_runtime_mark_last_busy(dev);
  281. pm_runtime_autosuspend(dev);
  282. return 0;
  283. }
  284. static int __maybe_unused dwc3_xlnx_suspend(struct device *dev)
  285. {
  286. struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
  287. phy_exit(priv_data->usb3_phy);
  288. /* Disable the clocks */
  289. clk_bulk_disable(priv_data->num_clocks, priv_data->clks);
  290. return 0;
  291. }
  292. static int __maybe_unused dwc3_xlnx_resume(struct device *dev)
  293. {
  294. struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
  295. int ret;
  296. ret = clk_bulk_enable(priv_data->num_clocks, priv_data->clks);
  297. if (ret)
  298. return ret;
  299. ret = phy_init(priv_data->usb3_phy);
  300. if (ret < 0)
  301. return ret;
  302. ret = phy_power_on(priv_data->usb3_phy);
  303. if (ret < 0) {
  304. phy_exit(priv_data->usb3_phy);
  305. return ret;
  306. }
  307. return 0;
  308. }
  309. static const struct dev_pm_ops dwc3_xlnx_dev_pm_ops = {
  310. SET_SYSTEM_SLEEP_PM_OPS(dwc3_xlnx_suspend, dwc3_xlnx_resume)
  311. SET_RUNTIME_PM_OPS(dwc3_xlnx_runtime_suspend,
  312. dwc3_xlnx_runtime_resume, dwc3_xlnx_runtime_idle)
  313. };
  314. static struct platform_driver dwc3_xlnx_driver = {
  315. .probe = dwc3_xlnx_probe,
  316. .remove = dwc3_xlnx_remove,
  317. .driver = {
  318. .name = "dwc3-xilinx",
  319. .of_match_table = dwc3_xlnx_of_match,
  320. .pm = &dwc3_xlnx_dev_pm_ops,
  321. },
  322. };
  323. module_platform_driver(dwc3_xlnx_driver);
  324. MODULE_LICENSE("GPL v2");
  325. MODULE_DESCRIPTION("Xilinx DWC3 controller specific glue driver");
  326. MODULE_AUTHOR("Manish Narani <[email protected]>");
  327. MODULE_AUTHOR("Anurag Kumar Vulisha <[email protected]>");