dwc3-xilinx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #define XLNX_USB_FPD_PIPE_CLK 0x7c
  31. #define PIPE_CLK_DESELECT 1
  32. #define PIPE_CLK_SELECT 0
  33. #define XLNX_USB_FPD_POWER_PRSNT 0x80
  34. #define FPD_POWER_PRSNT_OPTION BIT(0)
  35. struct dwc3_xlnx {
  36. int num_clocks;
  37. struct clk_bulk_data *clks;
  38. struct device *dev;
  39. void __iomem *regs;
  40. int (*pltfm_init)(struct dwc3_xlnx *data);
  41. struct phy *usb3_phy;
  42. };
  43. static void dwc3_xlnx_mask_phy_rst(struct dwc3_xlnx *priv_data, bool mask)
  44. {
  45. u32 reg;
  46. /*
  47. * Enable or disable ULPI PHY reset from USB Controller.
  48. * This does not actually reset the phy, but just controls
  49. * whether USB controller can or cannot reset ULPI PHY.
  50. */
  51. reg = readl(priv_data->regs + XLNX_USB_PHY_RST_EN);
  52. if (mask)
  53. reg &= ~XLNX_PHY_RST_MASK;
  54. else
  55. reg |= XLNX_PHY_RST_MASK;
  56. writel(reg, priv_data->regs + XLNX_USB_PHY_RST_EN);
  57. }
  58. static int dwc3_xlnx_init_versal(struct dwc3_xlnx *priv_data)
  59. {
  60. struct device *dev = priv_data->dev;
  61. struct reset_control *crst;
  62. int ret;
  63. crst = devm_reset_control_get_exclusive(dev, NULL);
  64. if (IS_ERR(crst))
  65. return dev_err_probe(dev, PTR_ERR(crst), "failed to get reset signal\n");
  66. dwc3_xlnx_mask_phy_rst(priv_data, false);
  67. /* Assert and De-assert reset */
  68. ret = reset_control_assert(crst);
  69. if (ret < 0) {
  70. dev_err_probe(dev, ret, "failed to assert Reset\n");
  71. return ret;
  72. }
  73. ret = reset_control_deassert(crst);
  74. if (ret < 0) {
  75. dev_err_probe(dev, ret, "failed to De-assert Reset\n");
  76. return ret;
  77. }
  78. dwc3_xlnx_mask_phy_rst(priv_data, true);
  79. return 0;
  80. }
  81. static int dwc3_xlnx_init_zynqmp(struct dwc3_xlnx *priv_data)
  82. {
  83. struct device *dev = priv_data->dev;
  84. struct reset_control *crst, *hibrst, *apbrst;
  85. struct gpio_desc *reset_gpio;
  86. int ret = 0;
  87. u32 reg;
  88. priv_data->usb3_phy = devm_phy_optional_get(dev, "usb3-phy");
  89. if (IS_ERR(priv_data->usb3_phy)) {
  90. ret = PTR_ERR(priv_data->usb3_phy);
  91. dev_err_probe(dev, ret,
  92. "failed to get USB3 PHY\n");
  93. goto err;
  94. }
  95. /*
  96. * The following core resets are not required unless a USB3 PHY
  97. * is used, and the subsequent register settings are not required
  98. * unless a core reset is performed (they should be set properly
  99. * by the first-stage boot loader, but may be reverted by a core
  100. * reset). They may also break the configuration if USB3 is actually
  101. * in use but the usb3-phy entry is missing from the device tree.
  102. * Therefore, skip these operations in this case.
  103. */
  104. if (!priv_data->usb3_phy) {
  105. /* Deselect the PIPE Clock Select bit in FPD PIPE Clock register */
  106. writel(PIPE_CLK_DESELECT, priv_data->regs + XLNX_USB_FPD_PIPE_CLK);
  107. goto skip_usb3_phy;
  108. }
  109. crst = devm_reset_control_get_exclusive(dev, "usb_crst");
  110. if (IS_ERR(crst)) {
  111. ret = PTR_ERR(crst);
  112. dev_err_probe(dev, ret,
  113. "failed to get core reset signal\n");
  114. goto err;
  115. }
  116. hibrst = devm_reset_control_get_exclusive(dev, "usb_hibrst");
  117. if (IS_ERR(hibrst)) {
  118. ret = PTR_ERR(hibrst);
  119. dev_err_probe(dev, ret,
  120. "failed to get hibernation reset signal\n");
  121. goto err;
  122. }
  123. apbrst = devm_reset_control_get_exclusive(dev, "usb_apbrst");
  124. if (IS_ERR(apbrst)) {
  125. ret = PTR_ERR(apbrst);
  126. dev_err_probe(dev, ret,
  127. "failed to get APB reset signal\n");
  128. goto err;
  129. }
  130. ret = reset_control_assert(crst);
  131. if (ret < 0) {
  132. dev_err(dev, "Failed to assert core reset\n");
  133. goto err;
  134. }
  135. ret = reset_control_assert(hibrst);
  136. if (ret < 0) {
  137. dev_err(dev, "Failed to assert hibernation reset\n");
  138. goto err;
  139. }
  140. ret = reset_control_assert(apbrst);
  141. if (ret < 0) {
  142. dev_err(dev, "Failed to assert APB reset\n");
  143. goto err;
  144. }
  145. ret = phy_init(priv_data->usb3_phy);
  146. if (ret < 0) {
  147. phy_exit(priv_data->usb3_phy);
  148. goto err;
  149. }
  150. ret = reset_control_deassert(apbrst);
  151. if (ret < 0) {
  152. dev_err(dev, "Failed to release APB reset\n");
  153. goto err;
  154. }
  155. /* Set PIPE Power Present signal in FPD Power Present Register*/
  156. writel(FPD_POWER_PRSNT_OPTION, priv_data->regs + XLNX_USB_FPD_POWER_PRSNT);
  157. /* Set the PIPE Clock Select bit in FPD PIPE Clock register */
  158. writel(PIPE_CLK_SELECT, priv_data->regs + XLNX_USB_FPD_PIPE_CLK);
  159. ret = reset_control_deassert(crst);
  160. if (ret < 0) {
  161. dev_err(dev, "Failed to release core reset\n");
  162. goto err;
  163. }
  164. ret = reset_control_deassert(hibrst);
  165. if (ret < 0) {
  166. dev_err(dev, "Failed to release hibernation reset\n");
  167. goto err;
  168. }
  169. ret = phy_power_on(priv_data->usb3_phy);
  170. if (ret < 0) {
  171. phy_exit(priv_data->usb3_phy);
  172. goto err;
  173. }
  174. skip_usb3_phy:
  175. /* ulpi reset via gpio-modepin or gpio-framework driver */
  176. reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  177. if (IS_ERR(reset_gpio)) {
  178. return dev_err_probe(dev, PTR_ERR(reset_gpio),
  179. "Failed to request reset GPIO\n");
  180. }
  181. if (reset_gpio) {
  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_set_swnode(struct device *dev)
  212. {
  213. struct device_node *np = dev->of_node, *dwc3_np;
  214. struct property_entry props[2];
  215. int prop_idx = 0, ret = 0;
  216. dwc3_np = of_get_compatible_child(np, "snps,dwc3");
  217. if (!dwc3_np) {
  218. ret = -ENODEV;
  219. dev_err(dev, "failed to find dwc3 core child\n");
  220. return ret;
  221. }
  222. memset(props, 0, sizeof(struct property_entry) * ARRAY_SIZE(props));
  223. if (of_dma_is_coherent(dwc3_np))
  224. props[prop_idx++] = PROPERTY_ENTRY_U16("snps,gsbuscfg0-reqinfo",
  225. 0xffff);
  226. of_node_put(dwc3_np);
  227. if (prop_idx)
  228. ret = device_create_managed_software_node(dev, props, NULL);
  229. return ret;
  230. }
  231. static int dwc3_xlnx_probe(struct platform_device *pdev)
  232. {
  233. struct dwc3_xlnx *priv_data;
  234. struct device *dev = &pdev->dev;
  235. struct device_node *np = dev->of_node;
  236. const struct of_device_id *match;
  237. void __iomem *regs;
  238. int ret;
  239. priv_data = devm_kzalloc(dev, sizeof(*priv_data), GFP_KERNEL);
  240. if (!priv_data)
  241. return -ENOMEM;
  242. regs = devm_platform_ioremap_resource(pdev, 0);
  243. if (IS_ERR(regs))
  244. return dev_err_probe(dev, PTR_ERR(regs), "failed to map registers\n");
  245. match = of_match_node(dwc3_xlnx_of_match, pdev->dev.of_node);
  246. priv_data->pltfm_init = match->data;
  247. priv_data->regs = regs;
  248. priv_data->dev = dev;
  249. platform_set_drvdata(pdev, priv_data);
  250. ret = devm_clk_bulk_get_all(priv_data->dev, &priv_data->clks);
  251. if (ret < 0)
  252. return ret;
  253. priv_data->num_clocks = ret;
  254. ret = clk_bulk_prepare_enable(priv_data->num_clocks, priv_data->clks);
  255. if (ret)
  256. return ret;
  257. ret = priv_data->pltfm_init(priv_data);
  258. if (ret)
  259. goto err_clk_put;
  260. ret = dwc3_set_swnode(dev);
  261. if (ret)
  262. goto err_clk_put;
  263. ret = of_platform_populate(np, NULL, NULL, dev);
  264. if (ret)
  265. goto err_clk_put;
  266. pm_runtime_set_active(dev);
  267. ret = devm_pm_runtime_enable(dev);
  268. if (ret < 0)
  269. goto err_pm_set_suspended;
  270. pm_suspend_ignore_children(dev, false);
  271. ret = pm_runtime_resume_and_get(dev);
  272. if (ret < 0)
  273. goto err_pm_set_suspended;
  274. return 0;
  275. err_pm_set_suspended:
  276. of_platform_depopulate(dev);
  277. pm_runtime_set_suspended(dev);
  278. err_clk_put:
  279. clk_bulk_disable_unprepare(priv_data->num_clocks, priv_data->clks);
  280. return ret;
  281. }
  282. static void dwc3_xlnx_remove(struct platform_device *pdev)
  283. {
  284. struct dwc3_xlnx *priv_data = platform_get_drvdata(pdev);
  285. struct device *dev = &pdev->dev;
  286. of_platform_depopulate(dev);
  287. clk_bulk_disable_unprepare(priv_data->num_clocks, priv_data->clks);
  288. priv_data->num_clocks = 0;
  289. pm_runtime_put_noidle(dev);
  290. pm_runtime_set_suspended(dev);
  291. }
  292. static int __maybe_unused dwc3_xlnx_runtime_suspend(struct device *dev)
  293. {
  294. struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
  295. clk_bulk_disable(priv_data->num_clocks, priv_data->clks);
  296. return 0;
  297. }
  298. static int __maybe_unused dwc3_xlnx_runtime_resume(struct device *dev)
  299. {
  300. struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
  301. return clk_bulk_enable(priv_data->num_clocks, priv_data->clks);
  302. }
  303. static int __maybe_unused dwc3_xlnx_runtime_idle(struct device *dev)
  304. {
  305. pm_runtime_mark_last_busy(dev);
  306. pm_runtime_autosuspend(dev);
  307. return 0;
  308. }
  309. static int __maybe_unused dwc3_xlnx_suspend(struct device *dev)
  310. {
  311. struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
  312. phy_exit(priv_data->usb3_phy);
  313. /* Disable the clocks */
  314. clk_bulk_disable(priv_data->num_clocks, priv_data->clks);
  315. return 0;
  316. }
  317. static int __maybe_unused dwc3_xlnx_resume(struct device *dev)
  318. {
  319. struct dwc3_xlnx *priv_data = dev_get_drvdata(dev);
  320. int ret;
  321. ret = clk_bulk_enable(priv_data->num_clocks, priv_data->clks);
  322. if (ret)
  323. return ret;
  324. ret = phy_init(priv_data->usb3_phy);
  325. if (ret < 0)
  326. return ret;
  327. ret = phy_power_on(priv_data->usb3_phy);
  328. if (ret < 0) {
  329. phy_exit(priv_data->usb3_phy);
  330. return ret;
  331. }
  332. return 0;
  333. }
  334. static const struct dev_pm_ops dwc3_xlnx_dev_pm_ops = {
  335. SET_SYSTEM_SLEEP_PM_OPS(dwc3_xlnx_suspend, dwc3_xlnx_resume)
  336. SET_RUNTIME_PM_OPS(dwc3_xlnx_runtime_suspend,
  337. dwc3_xlnx_runtime_resume, dwc3_xlnx_runtime_idle)
  338. };
  339. static struct platform_driver dwc3_xlnx_driver = {
  340. .probe = dwc3_xlnx_probe,
  341. .remove_new = dwc3_xlnx_remove,
  342. .driver = {
  343. .name = "dwc3-xilinx",
  344. .of_match_table = dwc3_xlnx_of_match,
  345. .pm = &dwc3_xlnx_dev_pm_ops,
  346. },
  347. };
  348. module_platform_driver(dwc3_xlnx_driver);
  349. MODULE_LICENSE("GPL v2");
  350. MODULE_DESCRIPTION("Xilinx DWC3 controller specific glue driver");
  351. MODULE_AUTHOR("Manish Narani <manish.narani@xilinx.com>");
  352. MODULE_AUTHOR("Anurag Kumar Vulisha <anurag.kumar.vulisha@xilinx.com>");