1 // SPDX-License-Identifier: GPL-2.0
3 * cdns3-starfive.c - StarFive specific Glue layer for Cadence USB Controller
5 * Copyright (C) 2023 StarFive Technology Co., Ltd.
7 * Author: Minda Chen <minda.chen@starfivetech.com>
10 #include <linux/bits.h>
11 #include <linux/clk.h>
12 #include <linux/module.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
17 #include <linux/of_platform.h>
18 #include <linux/reset.h>
19 #include <linux/regmap.h>
20 #include <linux/usb/otg.h>
23 #define USB_STRAP_HOST BIT(17)
24 #define USB_STRAP_DEVICE BIT(18)
25 #define USB_STRAP_MASK GENMASK(18, 16)
27 #define USB_SUSPENDM_HOST BIT(19)
28 #define USB_SUSPENDM_MASK BIT(19)
30 #define USB_MISC_CFG_MASK GENMASK(23, 20)
31 #define USB_SUSPENDM_BYPS BIT(20)
32 #define USB_PLL_EN BIT(22)
33 #define USB_REFCLK_MODE BIT(23)
35 struct cdns_starfive {
37 struct regmap *stg_syscon;
38 struct reset_control *resets;
39 struct clk_bulk_data *clks;
44 static void cdns_mode_init(struct platform_device *pdev,
45 struct cdns_starfive *data)
47 enum usb_dr_mode mode;
49 regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
51 USB_SUSPENDM_BYPS | USB_PLL_EN | USB_REFCLK_MODE);
54 mode = usb_get_dr_mode(&pdev->dev);
57 case USB_DR_MODE_HOST:
58 regmap_update_bits(data->stg_syscon,
62 regmap_update_bits(data->stg_syscon,
68 case USB_DR_MODE_PERIPHERAL:
69 regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
70 USB_STRAP_MASK, USB_STRAP_DEVICE);
71 regmap_update_bits(data->stg_syscon, data->stg_usb_mode,
72 USB_SUSPENDM_MASK, 0);
79 static int cdns_clk_rst_init(struct cdns_starfive *data)
83 ret = clk_bulk_prepare_enable(data->num_clks, data->clks);
85 return dev_err_probe(data->dev, ret,
86 "failed to enable clocks\n");
88 ret = reset_control_deassert(data->resets);
90 dev_err(data->dev, "failed to reset clocks\n");
97 clk_bulk_disable_unprepare(data->num_clks, data->clks);
101 static void cdns_clk_rst_deinit(struct cdns_starfive *data)
103 reset_control_assert(data->resets);
104 clk_bulk_disable_unprepare(data->num_clks, data->clks);
107 static int cdns_starfive_probe(struct platform_device *pdev)
109 struct device *dev = &pdev->dev;
110 struct cdns_starfive *data;
114 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
121 syscon_regmap_lookup_by_phandle_args(pdev->dev.of_node,
122 "starfive,stg-syscon", 1, &args);
124 if (IS_ERR(data->stg_syscon))
125 return dev_err_probe(dev, PTR_ERR(data->stg_syscon),
126 "Failed to parse starfive,stg-syscon\n");
128 data->stg_usb_mode = args;
130 data->num_clks = devm_clk_bulk_get_all(data->dev, &data->clks);
131 if (data->num_clks < 0)
132 return dev_err_probe(data->dev, -ENODEV,
133 "Failed to get clocks\n");
135 data->resets = devm_reset_control_array_get_exclusive(data->dev);
136 if (IS_ERR(data->resets))
137 return dev_err_probe(data->dev, PTR_ERR(data->resets),
138 "Failed to get resets");
140 cdns_mode_init(pdev, data);
141 ret = cdns_clk_rst_init(data);
145 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
147 dev_err(dev, "Failed to create children\n");
148 cdns_clk_rst_deinit(data);
152 device_set_wakeup_capable(dev, true);
153 pm_runtime_set_active(dev);
154 pm_runtime_enable(dev);
155 platform_set_drvdata(pdev, data);
160 static int cdns_starfive_remove_core(struct device *dev, void *c)
162 struct platform_device *pdev = to_platform_device(dev);
164 platform_device_unregister(pdev);
169 static void cdns_starfive_remove(struct platform_device *pdev)
171 struct device *dev = &pdev->dev;
172 struct cdns_starfive *data = dev_get_drvdata(dev);
174 pm_runtime_get_sync(dev);
175 device_for_each_child(dev, NULL, cdns_starfive_remove_core);
177 pm_runtime_disable(dev);
178 pm_runtime_put_noidle(dev);
179 cdns_clk_rst_deinit(data);
180 platform_set_drvdata(pdev, NULL);
184 static int cdns_starfive_runtime_resume(struct device *dev)
186 struct cdns_starfive *data = dev_get_drvdata(dev);
188 return clk_bulk_prepare_enable(data->num_clks, data->clks);
191 static int cdns_starfive_runtime_suspend(struct device *dev)
193 struct cdns_starfive *data = dev_get_drvdata(dev);
195 clk_bulk_disable_unprepare(data->num_clks, data->clks);
200 #ifdef CONFIG_PM_SLEEP
201 static int cdns_starfive_resume(struct device *dev)
203 struct cdns_starfive *data = dev_get_drvdata(dev);
205 return cdns_clk_rst_init(data);
208 static int cdns_starfive_suspend(struct device *dev)
210 struct cdns_starfive *data = dev_get_drvdata(dev);
212 cdns_clk_rst_deinit(data);
219 static const struct dev_pm_ops cdns_starfive_pm_ops = {
220 SET_RUNTIME_PM_OPS(cdns_starfive_runtime_suspend,
221 cdns_starfive_runtime_resume, NULL)
222 SET_SYSTEM_SLEEP_PM_OPS(cdns_starfive_suspend, cdns_starfive_resume)
225 static const struct of_device_id cdns_starfive_of_match[] = {
226 { .compatible = "starfive,jh7110-usb", },
229 MODULE_DEVICE_TABLE(of, cdns_starfive_of_match);
231 static struct platform_driver cdns_starfive_driver = {
232 .probe = cdns_starfive_probe,
233 .remove_new = cdns_starfive_remove,
235 .name = "cdns3-starfive",
236 .of_match_table = cdns_starfive_of_match,
237 .pm = &cdns_starfive_pm_ops,
240 module_platform_driver(cdns_starfive_driver);
242 MODULE_ALIAS("platform:cdns3-starfive");
243 MODULE_LICENSE("GPL v2");
244 MODULE_DESCRIPTION("Cadence USB3 StarFive Glue Layer");