1 // SPDX-License-Identifier: GPL-2.0
3 * Generic platform ehci driver
9 * Derived from the ohci-ssb driver
12 * Derived from the EHCI-PCI driver
13 * Copyright (c) 2000-2004 by David Brownell
15 * Derived from the ohci-pci driver
16 * Copyright 1999 Roman Weissgaerber
17 * Copyright 2000-2002 David Brownell
18 * Copyright 1999 Linus Torvalds
19 * Copyright 1999 Gregory P. Smith
21 #include <linux/acpi.h>
22 #include <linux/clk.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/err.h>
25 #include <linux/kernel.h>
26 #include <linux/hrtimer.h>
28 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/reset.h>
32 #include <linux/usb.h>
33 #include <linux/usb/hcd.h>
34 #include <linux/usb/ehci_pdriver.h>
35 #include <linux/usb/of.h>
39 #define DRIVER_DESC "EHCI generic platform driver"
40 #define EHCI_MAX_CLKS 4
41 #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
43 struct ehci_platform_priv {
44 struct clk *clks[EHCI_MAX_CLKS];
45 struct reset_control *rsts;
49 static const char hcd_name[] = "ehci-platform";
51 static int ehci_platform_reset(struct usb_hcd *hcd)
53 struct platform_device *pdev = to_platform_device(hcd->self.controller);
54 struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
55 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
58 ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
60 if (pdata->pre_setup) {
61 retval = pdata->pre_setup(hcd);
66 ehci->caps = hcd->regs + pdata->caps_offset;
67 retval = ehci_setup(hcd);
71 if (pdata->no_io_watchdog)
72 ehci->need_io_watchdog = 0;
76 static int ehci_platform_power_on(struct platform_device *dev)
78 struct usb_hcd *hcd = platform_get_drvdata(dev);
79 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
82 for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
83 ret = clk_prepare_enable(priv->clks[clk]);
85 goto err_disable_clks;
92 clk_disable_unprepare(priv->clks[clk]);
97 static void ehci_platform_power_off(struct platform_device *dev)
99 struct usb_hcd *hcd = platform_get_drvdata(dev);
100 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
103 for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
105 clk_disable_unprepare(priv->clks[clk]);
108 static struct hc_driver __read_mostly ehci_platform_hc_driver;
110 static const struct ehci_driver_overrides platform_overrides __initconst = {
111 .reset = ehci_platform_reset,
112 .extra_priv_size = sizeof(struct ehci_platform_priv),
115 static struct usb_ehci_pdata ehci_platform_defaults = {
116 .power_on = ehci_platform_power_on,
117 .power_suspend = ehci_platform_power_off,
118 .power_off = ehci_platform_power_off,
121 static int ehci_platform_probe(struct platform_device *dev)
124 struct resource *res_mem;
125 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
126 struct ehci_platform_priv *priv;
127 struct ehci_hcd *ehci;
128 int err, irq, clk = 0;
134 * Use reasonable defaults so platforms don't have to provide these
135 * with DT probing on ARM.
138 pdata = &ehci_platform_defaults;
140 err = dma_coerce_mask_and_coherent(&dev->dev,
141 pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
143 dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
147 irq = platform_get_irq(dev, 0);
151 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
152 dev_name(&dev->dev));
156 platform_set_drvdata(dev, hcd);
157 dev->dev.platform_data = pdata;
158 priv = hcd_to_ehci_priv(hcd);
159 ehci = hcd_to_ehci(hcd);
161 if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
162 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
163 ehci->big_endian_mmio = 1;
165 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
166 ehci->big_endian_desc = 1;
168 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
169 ehci->big_endian_mmio = ehci->big_endian_desc = 1;
171 if (of_property_read_bool(dev->dev.of_node,
172 "needs-reset-on-resume"))
173 priv->reset_on_resume = true;
175 if (of_property_read_bool(dev->dev.of_node,
176 "has-transaction-translator"))
179 for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
180 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
181 if (IS_ERR(priv->clks[clk])) {
182 err = PTR_ERR(priv->clks[clk]);
183 if (err == -EPROBE_DEFER)
185 priv->clks[clk] = NULL;
191 priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev);
192 if (IS_ERR(priv->rsts)) {
193 err = PTR_ERR(priv->rsts);
197 err = reset_control_deassert(priv->rsts);
201 if (pdata->big_endian_desc)
202 ehci->big_endian_desc = 1;
203 if (pdata->big_endian_mmio)
204 ehci->big_endian_mmio = 1;
207 if (pdata->reset_on_resume)
208 priv->reset_on_resume = true;
210 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
211 if (ehci->big_endian_mmio) {
213 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
218 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
219 if (ehci->big_endian_desc) {
221 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
227 if (pdata->power_on) {
228 err = pdata->power_on(dev);
233 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
234 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
235 if (IS_ERR(hcd->regs)) {
236 err = PTR_ERR(hcd->regs);
239 hcd->rsrc_start = res_mem->start;
240 hcd->rsrc_len = resource_size(res_mem);
242 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
246 device_wakeup_enable(hcd->self.controller);
247 device_enable_async_suspend(hcd->self.controller);
248 platform_set_drvdata(dev, hcd);
253 if (pdata->power_off)
254 pdata->power_off(dev);
256 reset_control_assert(priv->rsts);
259 clk_put(priv->clks[clk]);
261 if (pdata == &ehci_platform_defaults)
262 dev->dev.platform_data = NULL;
269 static int ehci_platform_remove(struct platform_device *dev)
271 struct usb_hcd *hcd = platform_get_drvdata(dev);
272 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
273 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
278 if (pdata->power_off)
279 pdata->power_off(dev);
281 reset_control_assert(priv->rsts);
283 for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
284 clk_put(priv->clks[clk]);
288 if (pdata == &ehci_platform_defaults)
289 dev->dev.platform_data = NULL;
294 #ifdef CONFIG_PM_SLEEP
295 static int ehci_platform_suspend(struct device *dev)
297 struct usb_hcd *hcd = dev_get_drvdata(dev);
298 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
299 struct platform_device *pdev = to_platform_device(dev);
300 bool do_wakeup = device_may_wakeup(dev);
303 ret = ehci_suspend(hcd, do_wakeup);
307 if (pdata->power_suspend)
308 pdata->power_suspend(pdev);
313 static int ehci_platform_resume(struct device *dev)
315 struct usb_hcd *hcd = dev_get_drvdata(dev);
316 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
317 struct platform_device *pdev = to_platform_device(dev);
318 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
319 struct device *companion_dev;
321 if (pdata->power_on) {
322 int err = pdata->power_on(pdev);
327 companion_dev = usb_of_get_companion_dev(hcd->self.controller);
329 device_pm_wait_for_dev(hcd->self.controller, companion_dev);
330 put_device(companion_dev);
333 ehci_resume(hcd, priv->reset_on_resume);
336 #endif /* CONFIG_PM_SLEEP */
338 static const struct of_device_id vt8500_ehci_ids[] = {
339 { .compatible = "via,vt8500-ehci", },
340 { .compatible = "wm,prizm-ehci", },
341 { .compatible = "generic-ehci", },
342 { .compatible = "cavium,octeon-6335-ehci", },
345 MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
347 static const struct acpi_device_id ehci_acpi_match[] = {
348 { "PNP0D20", 0 }, /* EHCI controller without debug */
351 MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
353 static const struct platform_device_id ehci_platform_table[] = {
354 { "ehci-platform", 0 },
357 MODULE_DEVICE_TABLE(platform, ehci_platform_table);
359 static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
360 ehci_platform_resume);
362 static struct platform_driver ehci_platform_driver = {
363 .id_table = ehci_platform_table,
364 .probe = ehci_platform_probe,
365 .remove = ehci_platform_remove,
366 .shutdown = usb_hcd_platform_shutdown,
368 .name = "ehci-platform",
369 .pm = &ehci_platform_pm_ops,
370 .of_match_table = vt8500_ehci_ids,
371 .acpi_match_table = ACPI_PTR(ehci_acpi_match),
375 static int __init ehci_platform_init(void)
380 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
382 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
383 return platform_driver_register(&ehci_platform_driver);
385 module_init(ehci_platform_init);
387 static void __exit ehci_platform_cleanup(void)
389 platform_driver_unregister(&ehci_platform_driver);
391 module_exit(ehci_platform_cleanup);
393 MODULE_DESCRIPTION(DRIVER_DESC);
394 MODULE_AUTHOR("Hauke Mehrtens");
395 MODULE_AUTHOR("Alan Stern");
396 MODULE_LICENSE("GPL");