0093-usb-cdns3-Add-StarFive-JH7110-USB-driver.patch 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. From 27c75f360a76084ee6a249876483cfa8d0bedd65 Mon Sep 17 00:00:00 2001
  2. From: Minda Chen <[email protected]>
  3. Date: Thu, 18 May 2023 19:27:49 +0800
  4. Subject: [PATCH 093/122] usb: cdns3: Add StarFive JH7110 USB driver
  5. Adds Specific Glue layer to support USB peripherals on
  6. StarFive JH7110 SoC.
  7. There is a Cadence USB3 core for JH7110 SoCs, the cdns
  8. core is the child of this USB wrapper module device.
  9. Signed-off-by: Minda Chen <[email protected]>
  10. Acked-by: Peter Chen <[email protected]>
  11. ---
  12. drivers/usb/cdns3/Kconfig | 11 ++
  13. drivers/usb/cdns3/Makefile | 1 +
  14. drivers/usb/cdns3/cdns3-starfive.c | 246 +++++++++++++++++++++++++++++
  15. 3 files changed, 258 insertions(+)
  16. create mode 100644 drivers/usb/cdns3/cdns3-starfive.c
  17. --- a/drivers/usb/cdns3/Kconfig
  18. +++ b/drivers/usb/cdns3/Kconfig
  19. @@ -78,6 +78,17 @@ config USB_CDNS3_IMX
  20. For example, imx8qm and imx8qxp.
  21. +config USB_CDNS3_STARFIVE
  22. + tristate "Cadence USB3 support on StarFive SoC platforms"
  23. + depends on ARCH_STARFIVE || COMPILE_TEST
  24. + help
  25. + Say 'Y' or 'M' here if you are building for StarFive SoCs
  26. + platforms that contain Cadence USB3 controller core.
  27. +
  28. + e.g. JH7110.
  29. +
  30. + If you choose to build this driver as module it will
  31. + be dynamically linked and module will be called cdns3-starfive.ko
  32. endif
  33. if USB_CDNS_SUPPORT
  34. --- a/drivers/usb/cdns3/Makefile
  35. +++ b/drivers/usb/cdns3/Makefile
  36. @@ -24,6 +24,7 @@ endif
  37. obj-$(CONFIG_USB_CDNS3_PCI_WRAP) += cdns3-pci-wrap.o
  38. obj-$(CONFIG_USB_CDNS3_TI) += cdns3-ti.o
  39. obj-$(CONFIG_USB_CDNS3_IMX) += cdns3-imx.o
  40. +obj-$(CONFIG_USB_CDNS3_STARFIVE) += cdns3-starfive.o
  41. cdnsp-udc-pci-y := cdnsp-pci.o
  42. --- /dev/null
  43. +++ b/drivers/usb/cdns3/cdns3-starfive.c
  44. @@ -0,0 +1,246 @@
  45. +// SPDX-License-Identifier: GPL-2.0
  46. +/**
  47. + * cdns3-starfive.c - StarFive specific Glue layer for Cadence USB Controller
  48. + *
  49. + * Copyright (C) 2023 StarFive Technology Co., Ltd.
  50. + *
  51. + * Author: Minda Chen <[email protected]>
  52. + */
  53. +
  54. +#include <linux/bits.h>
  55. +#include <linux/clk.h>
  56. +#include <linux/module.h>
  57. +#include <linux/mfd/syscon.h>
  58. +#include <linux/kernel.h>
  59. +#include <linux/platform_device.h>
  60. +#include <linux/io.h>
  61. +#include <linux/of_platform.h>
  62. +#include <linux/reset.h>
  63. +#include <linux/regmap.h>
  64. +#include <linux/usb/otg.h>
  65. +#include "core.h"
  66. +
  67. +#define USB_STRAP_HOST BIT(17)
  68. +#define USB_STRAP_DEVICE BIT(18)
  69. +#define USB_STRAP_MASK GENMASK(18, 16)
  70. +
  71. +#define USB_SUSPENDM_HOST BIT(19)
  72. +#define USB_SUSPENDM_MASK BIT(19)
  73. +
  74. +#define USB_MISC_CFG_MASK GENMASK(23, 20)
  75. +#define USB_SUSPENDM_BYPS BIT(20)
  76. +#define USB_PLL_EN BIT(22)
  77. +#define USB_REFCLK_MODE BIT(23)
  78. +
  79. +struct cdns_starfive {
  80. + struct device *dev;
  81. + struct regmap *stg_syscon;
  82. + struct reset_control *resets;
  83. + struct clk_bulk_data *clks;
  84. + int num_clks;
  85. + u32 stg_usb_mode;
  86. +};
  87. +
  88. +static void cdns_mode_init(struct platform_device *pdev,
  89. + struct cdns_starfive *data)
  90. +{
  91. + enum usb_dr_mode mode;
  92. +
  93. + regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
  94. + USB_MISC_CFG_MASK,
  95. + USB_SUSPENDM_BYPS | USB_PLL_EN | USB_REFCLK_MODE);
  96. +
  97. + /* dr mode setting */
  98. + mode = usb_get_dr_mode(&pdev->dev);
  99. +
  100. + switch (mode) {
  101. + case USB_DR_MODE_HOST:
  102. + regmap_update_bits(data->stg_syscon,
  103. + data->stg_usb_mode,
  104. + USB_STRAP_MASK,
  105. + USB_STRAP_HOST);
  106. + regmap_update_bits(data->stg_syscon,
  107. + data->stg_usb_mode,
  108. + USB_SUSPENDM_MASK,
  109. + USB_SUSPENDM_HOST);
  110. + break;
  111. +
  112. + case USB_DR_MODE_PERIPHERAL:
  113. + regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
  114. + USB_STRAP_MASK, USB_STRAP_DEVICE);
  115. + regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
  116. + USB_SUSPENDM_MASK, 0);
  117. + break;
  118. + default:
  119. + break;
  120. + }
  121. +}
  122. +
  123. +static int cdns_clk_rst_init(struct cdns_starfive *data)
  124. +{
  125. + int ret;
  126. +
  127. + ret = clk_bulk_prepare_enable(data->num_clks, data->clks);
  128. + if (ret)
  129. + return dev_err_probe(data->dev, ret,
  130. + "failed to enable clocks\n");
  131. +
  132. + ret = reset_control_deassert(data->resets);
  133. + if (ret) {
  134. + dev_err(data->dev, "failed to reset clocks\n");
  135. + goto err_clk_init;
  136. + }
  137. +
  138. + return ret;
  139. +
  140. +err_clk_init:
  141. + clk_bulk_disable_unprepare(data->num_clks, data->clks);
  142. + return ret;
  143. +}
  144. +
  145. +static void cdns_clk_rst_deinit(struct cdns_starfive *data)
  146. +{
  147. + reset_control_assert(data->resets);
  148. + clk_bulk_disable_unprepare(data->num_clks, data->clks);
  149. +}
  150. +
  151. +static int cdns_starfive_probe(struct platform_device *pdev)
  152. +{
  153. + struct device *dev = &pdev->dev;
  154. + struct cdns_starfive *data;
  155. + unsigned int args;
  156. + int ret;
  157. +
  158. + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  159. + if (!data)
  160. + return -ENOMEM;
  161. +
  162. + data->dev = dev;
  163. +
  164. + data->stg_syscon =
  165. + syscon_regmap_lookup_by_phandle_args(pdev->dev.of_node,
  166. + "starfive,stg-syscon", 1, &args);
  167. +
  168. + if (IS_ERR(data->stg_syscon))
  169. + return dev_err_probe(dev, PTR_ERR(data->stg_syscon),
  170. + "Failed to parse starfive,stg-syscon\n");
  171. +
  172. + data->stg_usb_mode = args;
  173. +
  174. + data->num_clks = devm_clk_bulk_get_all(data->dev, &data->clks);
  175. + if (data->num_clks < 0)
  176. + return dev_err_probe(data->dev, -ENODEV,
  177. + "Failed to get clocks\n");
  178. +
  179. + data->resets = devm_reset_control_array_get_exclusive(data->dev);
  180. + if (IS_ERR(data->resets))
  181. + return dev_err_probe(data->dev, PTR_ERR(data->resets),
  182. + "Failed to get resets");
  183. +
  184. + cdns_mode_init(pdev, data);
  185. + ret = cdns_clk_rst_init(data);
  186. + if (ret)
  187. + return ret;
  188. +
  189. + ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
  190. + if (ret) {
  191. + dev_err(dev, "Failed to create children\n");
  192. + cdns_clk_rst_deinit(data);
  193. + return ret;
  194. + }
  195. +
  196. + device_set_wakeup_capable(dev, true);
  197. + pm_runtime_set_active(dev);
  198. + pm_runtime_enable(dev);
  199. + platform_set_drvdata(pdev, data);
  200. +
  201. + return 0;
  202. +}
  203. +
  204. +static int cdns_starfive_remove_core(struct device *dev, void *c)
  205. +{
  206. + struct platform_device *pdev = to_platform_device(dev);
  207. +
  208. + platform_device_unregister(pdev);
  209. +
  210. + return 0;
  211. +}
  212. +
  213. +static int cdns_starfive_remove(struct platform_device *pdev)
  214. +{
  215. + struct device *dev = &pdev->dev;
  216. + struct cdns_starfive *data = dev_get_drvdata(dev);
  217. +
  218. + pm_runtime_get_sync(dev);
  219. + device_for_each_child(dev, NULL, cdns_starfive_remove_core);
  220. +
  221. + pm_runtime_disable(dev);
  222. + pm_runtime_put_noidle(dev);
  223. + cdns_clk_rst_deinit(data);
  224. + platform_set_drvdata(pdev, NULL);
  225. +
  226. + return 0;
  227. +}
  228. +
  229. +#ifdef CONFIG_PM
  230. +static int cdns_starfive_runtime_resume(struct device *dev)
  231. +{
  232. + struct cdns_starfive *data = dev_get_drvdata(dev);
  233. +
  234. + return clk_bulk_prepare_enable(data->num_clks, data->clks);
  235. +}
  236. +
  237. +static int cdns_starfive_runtime_suspend(struct device *dev)
  238. +{
  239. + struct cdns_starfive *data = dev_get_drvdata(dev);
  240. +
  241. + clk_bulk_disable_unprepare(data->num_clks, data->clks);
  242. +
  243. + return 0;
  244. +}
  245. +
  246. +#ifdef CONFIG_PM_SLEEP
  247. +static int cdns_starfive_resume(struct device *dev)
  248. +{
  249. + struct cdns_starfive *data = dev_get_drvdata(dev);
  250. +
  251. + return cdns_clk_rst_init(data);
  252. +}
  253. +
  254. +static int cdns_starfive_suspend(struct device *dev)
  255. +{
  256. + struct cdns_starfive *data = dev_get_drvdata(dev);
  257. +
  258. + cdns_clk_rst_deinit(data);
  259. +
  260. + return 0;
  261. +}
  262. +#endif
  263. +#endif
  264. +
  265. +static const struct dev_pm_ops cdns_starfive_pm_ops = {
  266. + SET_RUNTIME_PM_OPS(cdns_starfive_runtime_suspend,
  267. + cdns_starfive_runtime_resume, NULL)
  268. + SET_SYSTEM_SLEEP_PM_OPS(cdns_starfive_suspend, cdns_starfive_resume)
  269. +};
  270. +
  271. +static const struct of_device_id cdns_starfive_of_match[] = {
  272. + { .compatible = "starfive,jh7110-usb", },
  273. + { /* sentinel */ }
  274. +};
  275. +MODULE_DEVICE_TABLE(of, cdns_starfive_of_match);
  276. +
  277. +static struct platform_driver cdns_starfive_driver = {
  278. + .probe = cdns_starfive_probe,
  279. + .remove = cdns_starfive_remove,
  280. + .driver = {
  281. + .name = "cdns3-starfive",
  282. + .of_match_table = cdns_starfive_of_match,
  283. + .pm = &cdns_starfive_pm_ops,
  284. + },
  285. +};
  286. +module_platform_driver(cdns_starfive_driver);
  287. +
  288. +MODULE_ALIAS("platform:cdns3-starfive");
  289. +MODULE_LICENSE("GPL v2");
  290. +MODULE_DESCRIPTION("Cadence USB3 StarFive Glue Layer");