dwc3-xilinx.c 11 KB

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