1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
7 * Author: Peter Griffin <peter.griffin@linaro.org>
9 * Derived from ehci-platform.c
12 #include <linux/clk.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/hrtimer.h>
18 #include <linux/module.h>
20 #include <linux/phy/phy.h>
21 #include <linux/platform_device.h>
22 #include <linux/reset.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/usb/ehci_pdriver.h>
26 #include <linux/pinctrl/consumer.h>
30 #define USB_MAX_CLKS 3
32 struct st_ehci_platform_priv {
33 struct clk *clks[USB_MAX_CLKS];
35 struct reset_control *rst;
36 struct reset_control *pwr;
40 #define DRIVER_DESC "EHCI STMicroelectronics driver"
42 #define hcd_to_ehci_priv(h) \
43 ((struct st_ehci_platform_priv *)hcd_to_ehci(h)->priv)
45 #define EHCI_CAPS_SIZE 0x10
46 #define AHB2STBUS_INSREG01 (EHCI_CAPS_SIZE + 0x84)
48 static int st_ehci_platform_reset(struct usb_hcd *hcd)
50 struct platform_device *pdev = to_platform_device(hcd->self.controller);
51 struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
52 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
55 /* Set EHCI packet buffer IN/OUT threshold to 128 bytes */
56 threshold = 128 | (128 << 16);
57 writel(threshold, hcd->regs + AHB2STBUS_INSREG01);
59 ehci->caps = hcd->regs + pdata->caps_offset;
60 return ehci_setup(hcd);
63 static int st_ehci_platform_power_on(struct platform_device *dev)
65 struct usb_hcd *hcd = platform_get_drvdata(dev);
66 struct st_ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
69 ret = reset_control_deassert(priv->pwr);
73 ret = reset_control_deassert(priv->rst);
75 goto err_assert_power;
77 /* some SoCs don't have a dedicated 48Mhz clock, but those that do
78 need the rate to be explicitly set */
80 ret = clk_set_rate(priv->clk48, 48000000);
82 goto err_assert_reset;
85 for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
86 ret = clk_prepare_enable(priv->clks[clk]);
88 goto err_disable_clks;
91 ret = phy_init(priv->phy);
93 goto err_disable_clks;
95 ret = phy_power_on(priv->phy);
105 clk_disable_unprepare(priv->clks[clk]);
107 reset_control_assert(priv->rst);
109 reset_control_assert(priv->pwr);
114 static void st_ehci_platform_power_off(struct platform_device *dev)
116 struct usb_hcd *hcd = platform_get_drvdata(dev);
117 struct st_ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
120 reset_control_assert(priv->pwr);
122 reset_control_assert(priv->rst);
124 phy_power_off(priv->phy);
128 for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
130 clk_disable_unprepare(priv->clks[clk]);
134 static struct hc_driver __read_mostly ehci_platform_hc_driver;
136 static const struct ehci_driver_overrides platform_overrides __initconst = {
137 .reset = st_ehci_platform_reset,
138 .extra_priv_size = sizeof(struct st_ehci_platform_priv),
141 static struct usb_ehci_pdata ehci_platform_defaults = {
142 .power_on = st_ehci_platform_power_on,
143 .power_suspend = st_ehci_platform_power_off,
144 .power_off = st_ehci_platform_power_off,
147 static int st_ehci_platform_probe(struct platform_device *dev)
150 struct resource *res_mem;
151 struct usb_ehci_pdata *pdata = &ehci_platform_defaults;
152 struct st_ehci_platform_priv *priv;
153 int err, irq, clk = 0;
158 irq = platform_get_irq(dev, 0);
161 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
163 dev_err(&dev->dev, "no memory resource provided");
167 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
168 dev_name(&dev->dev));
172 platform_set_drvdata(dev, hcd);
173 dev->dev.platform_data = pdata;
174 priv = hcd_to_ehci_priv(hcd);
176 priv->phy = devm_phy_get(&dev->dev, "usb");
177 if (IS_ERR(priv->phy)) {
178 err = PTR_ERR(priv->phy);
182 for (clk = 0; clk < USB_MAX_CLKS; clk++) {
183 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
184 if (IS_ERR(priv->clks[clk])) {
185 err = PTR_ERR(priv->clks[clk]);
186 if (err == -EPROBE_DEFER)
188 priv->clks[clk] = NULL;
193 /* some SoCs don't have a dedicated 48Mhz clock, but those that
194 do need the rate to be explicitly set */
195 priv->clk48 = devm_clk_get(&dev->dev, "clk48");
196 if (IS_ERR(priv->clk48)) {
197 dev_info(&dev->dev, "48MHz clk not found\n");
202 devm_reset_control_get_optional_shared(&dev->dev, "power");
203 if (IS_ERR(priv->pwr)) {
204 err = PTR_ERR(priv->pwr);
205 if (err == -EPROBE_DEFER)
211 devm_reset_control_get_optional_shared(&dev->dev, "softreset");
212 if (IS_ERR(priv->rst)) {
213 err = PTR_ERR(priv->rst);
214 if (err == -EPROBE_DEFER)
219 if (pdata->power_on) {
220 err = pdata->power_on(dev);
225 hcd->rsrc_start = res_mem->start;
226 hcd->rsrc_len = resource_size(res_mem);
228 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
229 if (IS_ERR(hcd->regs)) {
230 err = PTR_ERR(hcd->regs);
234 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
238 device_wakeup_enable(hcd->self.controller);
239 platform_set_drvdata(dev, hcd);
245 clk_put(priv->clks[clk]);
247 if (pdata == &ehci_platform_defaults)
248 dev->dev.platform_data = NULL;
255 static int st_ehci_platform_remove(struct platform_device *dev)
257 struct usb_hcd *hcd = platform_get_drvdata(dev);
258 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
259 struct st_ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
264 if (pdata->power_off)
265 pdata->power_off(dev);
267 for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
268 clk_put(priv->clks[clk]);
272 if (pdata == &ehci_platform_defaults)
273 dev->dev.platform_data = NULL;
278 #ifdef CONFIG_PM_SLEEP
280 static int st_ehci_suspend(struct device *dev)
282 struct usb_hcd *hcd = dev_get_drvdata(dev);
283 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
284 struct platform_device *pdev = to_platform_device(dev);
285 bool do_wakeup = device_may_wakeup(dev);
288 ret = ehci_suspend(hcd, do_wakeup);
292 if (pdata->power_suspend)
293 pdata->power_suspend(pdev);
295 pinctrl_pm_select_sleep_state(dev);
300 static int st_ehci_resume(struct device *dev)
302 struct usb_hcd *hcd = dev_get_drvdata(dev);
303 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
304 struct platform_device *pdev = to_platform_device(dev);
307 pinctrl_pm_select_default_state(dev);
309 if (pdata->power_on) {
310 err = pdata->power_on(pdev);
315 ehci_resume(hcd, false);
319 static SIMPLE_DEV_PM_OPS(st_ehci_pm_ops, st_ehci_suspend, st_ehci_resume);
321 #endif /* CONFIG_PM_SLEEP */
323 static const struct of_device_id st_ehci_ids[] = {
324 { .compatible = "st,st-ehci-300x", },
327 MODULE_DEVICE_TABLE(of, st_ehci_ids);
329 static struct platform_driver ehci_platform_driver = {
330 .probe = st_ehci_platform_probe,
331 .remove = st_ehci_platform_remove,
332 .shutdown = usb_hcd_platform_shutdown,
335 #ifdef CONFIG_PM_SLEEP
336 .pm = &st_ehci_pm_ops,
338 .of_match_table = st_ehci_ids,
342 static int __init ehci_platform_init(void)
347 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
348 return platform_driver_register(&ehci_platform_driver);
350 module_init(ehci_platform_init);
352 static void __exit ehci_platform_cleanup(void)
354 platform_driver_unregister(&ehci_platform_driver);
356 module_exit(ehci_platform_cleanup);
358 MODULE_DESCRIPTION(DRIVER_DESC);
359 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
360 MODULE_LICENSE("GPL");