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