1 // SPDX-License-Identifier: GPL-2.0
3 * Generic platform ohci driver
9 * Derived from the OCHI-SSB driver
10 * Derived from the OHCI-PCI driver
11 * Copyright 1999 Roman Weissgaerber
12 * Copyright 2000-2002 David Brownell
13 * Copyright 1999 Linus Torvalds
14 * Copyright 1999 Gregory P. Smith
17 #include <linux/clk.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/hrtimer.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/err.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/reset.h>
28 #include <linux/usb/ohci_pdriver.h>
29 #include <linux/usb.h>
30 #include <linux/usb/hcd.h>
31 #include <linux/usb/of.h>
35 #define DRIVER_DESC "OHCI generic platform driver"
36 #define OHCI_MAX_CLKS 3
37 #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
39 struct ohci_platform_priv {
40 struct clk *clks[OHCI_MAX_CLKS];
41 struct reset_control *resets;
44 static const char hcd_name[] = "ohci-platform";
46 static int ohci_platform_power_on(struct platform_device *dev)
48 struct usb_hcd *hcd = platform_get_drvdata(dev);
49 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
52 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
53 ret = clk_prepare_enable(priv->clks[clk]);
55 goto err_disable_clks;
62 clk_disable_unprepare(priv->clks[clk]);
67 static void ohci_platform_power_off(struct platform_device *dev)
69 struct usb_hcd *hcd = platform_get_drvdata(dev);
70 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
73 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
75 clk_disable_unprepare(priv->clks[clk]);
78 static struct hc_driver __read_mostly ohci_platform_hc_driver;
80 static const struct ohci_driver_overrides platform_overrides __initconst = {
81 .product_desc = "Generic Platform OHCI controller",
82 .extra_priv_size = sizeof(struct ohci_platform_priv),
85 static struct usb_ohci_pdata ohci_platform_defaults = {
86 .power_on = ohci_platform_power_on,
87 .power_suspend = ohci_platform_power_off,
88 .power_off = ohci_platform_power_off,
91 static int ohci_platform_probe(struct platform_device *dev)
94 struct resource *res_mem;
95 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
96 struct ohci_platform_priv *priv;
97 struct ohci_hcd *ohci;
98 int err, irq, clk = 0;
104 * Use reasonable defaults so platforms don't have to provide these
105 * with DT probing on ARM.
108 pdata = &ohci_platform_defaults;
110 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
114 irq = platform_get_irq(dev, 0);
118 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
119 dev_name(&dev->dev));
123 platform_set_drvdata(dev, hcd);
124 dev->dev.platform_data = pdata;
125 priv = hcd_to_ohci_priv(hcd);
126 ohci = hcd_to_ohci(hcd);
128 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
129 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
130 ohci->flags |= OHCI_QUIRK_BE_MMIO;
132 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
133 ohci->flags |= OHCI_QUIRK_BE_DESC;
135 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
136 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
138 if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
139 ohci->flags |= OHCI_QUIRK_FRAME_NO;
141 if (of_property_read_bool(dev->dev.of_node,
142 "remote-wakeup-connected"))
143 ohci->hc_control = OHCI_CTRL_RWC;
145 of_property_read_u32(dev->dev.of_node, "num-ports",
148 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
149 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
150 if (IS_ERR(priv->clks[clk])) {
151 err = PTR_ERR(priv->clks[clk]);
152 if (err == -EPROBE_DEFER)
154 priv->clks[clk] = NULL;
159 priv->resets = devm_reset_control_array_get_optional_shared(
161 if (IS_ERR(priv->resets)) {
162 err = PTR_ERR(priv->resets);
166 err = reset_control_deassert(priv->resets);
171 if (pdata->big_endian_desc)
172 ohci->flags |= OHCI_QUIRK_BE_DESC;
173 if (pdata->big_endian_mmio)
174 ohci->flags |= OHCI_QUIRK_BE_MMIO;
175 if (pdata->no_big_frame_no)
176 ohci->flags |= OHCI_QUIRK_FRAME_NO;
177 if (pdata->num_ports)
178 ohci->num_ports = pdata->num_ports;
180 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
181 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
183 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
188 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
189 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
191 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
197 pm_runtime_set_active(&dev->dev);
198 pm_runtime_enable(&dev->dev);
199 if (pdata->power_on) {
200 err = pdata->power_on(dev);
205 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
206 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
207 if (IS_ERR(hcd->regs)) {
208 err = PTR_ERR(hcd->regs);
211 hcd->rsrc_start = res_mem->start;
212 hcd->rsrc_len = resource_size(res_mem);
214 hcd->tpl_support = of_usb_host_tpl_support(dev->dev.of_node);
216 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
220 device_wakeup_enable(hcd->self.controller);
222 platform_set_drvdata(dev, hcd);
227 if (pdata->power_off)
228 pdata->power_off(dev);
230 pm_runtime_disable(&dev->dev);
231 reset_control_assert(priv->resets);
234 clk_put(priv->clks[clk]);
236 if (pdata == &ohci_platform_defaults)
237 dev->dev.platform_data = NULL;
244 static int ohci_platform_remove(struct platform_device *dev)
246 struct usb_hcd *hcd = platform_get_drvdata(dev);
247 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
248 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
251 pm_runtime_get_sync(&dev->dev);
254 if (pdata->power_off)
255 pdata->power_off(dev);
257 reset_control_assert(priv->resets);
259 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
260 clk_put(priv->clks[clk]);
264 pm_runtime_put_sync(&dev->dev);
265 pm_runtime_disable(&dev->dev);
267 if (pdata == &ohci_platform_defaults)
268 dev->dev.platform_data = NULL;
273 #ifdef CONFIG_PM_SLEEP
274 static int ohci_platform_suspend(struct device *dev)
276 struct usb_hcd *hcd = dev_get_drvdata(dev);
277 struct usb_ohci_pdata *pdata = dev->platform_data;
278 struct platform_device *pdev = to_platform_device(dev);
279 bool do_wakeup = device_may_wakeup(dev);
282 ret = ohci_suspend(hcd, do_wakeup);
286 if (pdata->power_suspend)
287 pdata->power_suspend(pdev);
292 static int ohci_platform_resume(struct device *dev)
294 struct usb_hcd *hcd = dev_get_drvdata(dev);
295 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
296 struct platform_device *pdev = to_platform_device(dev);
298 if (pdata->power_on) {
299 int err = pdata->power_on(pdev);
304 ohci_resume(hcd, false);
306 pm_runtime_disable(dev);
307 pm_runtime_set_active(dev);
308 pm_runtime_enable(dev);
312 #endif /* CONFIG_PM_SLEEP */
314 static const struct of_device_id ohci_platform_ids[] = {
315 { .compatible = "generic-ohci", },
316 { .compatible = "cavium,octeon-6335-ohci", },
317 { .compatible = "ti,ohci-omap3", },
320 MODULE_DEVICE_TABLE(of, ohci_platform_ids);
322 static const struct platform_device_id ohci_platform_table[] = {
323 { "ohci-platform", 0 },
326 MODULE_DEVICE_TABLE(platform, ohci_platform_table);
328 static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
329 ohci_platform_resume);
331 static struct platform_driver ohci_platform_driver = {
332 .id_table = ohci_platform_table,
333 .probe = ohci_platform_probe,
334 .remove = ohci_platform_remove,
335 .shutdown = usb_hcd_platform_shutdown,
337 .name = "ohci-platform",
338 .pm = &ohci_platform_pm_ops,
339 .of_match_table = ohci_platform_ids,
340 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
344 static int __init ohci_platform_init(void)
349 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
351 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
352 return platform_driver_register(&ohci_platform_driver);
354 module_init(ohci_platform_init);
356 static void __exit ohci_platform_cleanup(void)
358 platform_driver_unregister(&ohci_platform_driver);
360 module_exit(ohci_platform_cleanup);
362 MODULE_DESCRIPTION(DRIVER_DESC);
363 MODULE_AUTHOR("Hauke Mehrtens");
364 MODULE_AUTHOR("Alan Stern");
365 MODULE_LICENSE("GPL");