1 // SPDX-License-Identifier: GPL-2.0
3 * Generic platform ohci driver
5 * Copyright 2007 Michael Buesch <m@bues.ch>
6 * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
7 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
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>
34 #define DRIVER_DESC "OHCI generic platform driver"
35 #define OHCI_MAX_CLKS 3
36 #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
38 struct ohci_platform_priv
{
39 struct clk
*clks
[OHCI_MAX_CLKS
];
40 struct reset_control
*resets
;
43 static const char hcd_name
[] = "ohci-platform";
45 static int ohci_platform_power_on(struct platform_device
*dev
)
47 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
48 struct ohci_platform_priv
*priv
= hcd_to_ohci_priv(hcd
);
51 for (clk
= 0; clk
< OHCI_MAX_CLKS
&& priv
->clks
[clk
]; clk
++) {
52 ret
= clk_prepare_enable(priv
->clks
[clk
]);
54 goto err_disable_clks
;
61 clk_disable_unprepare(priv
->clks
[clk
]);
66 static void ohci_platform_power_off(struct platform_device
*dev
)
68 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
69 struct ohci_platform_priv
*priv
= hcd_to_ohci_priv(hcd
);
72 for (clk
= OHCI_MAX_CLKS
- 1; clk
>= 0; clk
--)
74 clk_disable_unprepare(priv
->clks
[clk
]);
77 static struct hc_driver __read_mostly ohci_platform_hc_driver
;
79 static const struct ohci_driver_overrides platform_overrides __initconst
= {
80 .product_desc
= "Generic Platform OHCI controller",
81 .extra_priv_size
= sizeof(struct ohci_platform_priv
),
84 static struct usb_ohci_pdata ohci_platform_defaults
= {
85 .power_on
= ohci_platform_power_on
,
86 .power_suspend
= ohci_platform_power_off
,
87 .power_off
= ohci_platform_power_off
,
90 static int ohci_platform_probe(struct platform_device
*dev
)
93 struct resource
*res_mem
;
94 struct usb_ohci_pdata
*pdata
= dev_get_platdata(&dev
->dev
);
95 struct ohci_platform_priv
*priv
;
96 struct ohci_hcd
*ohci
;
97 int err
, irq
, clk
= 0;
103 * Use reasonable defaults so platforms don't have to provide these
104 * with DT probing on ARM.
107 pdata
= &ohci_platform_defaults
;
109 err
= dma_coerce_mask_and_coherent(&dev
->dev
, DMA_BIT_MASK(32));
113 irq
= platform_get_irq(dev
, 0);
117 hcd
= usb_create_hcd(&ohci_platform_hc_driver
, &dev
->dev
,
118 dev_name(&dev
->dev
));
122 platform_set_drvdata(dev
, hcd
);
123 dev
->dev
.platform_data
= pdata
;
124 priv
= hcd_to_ohci_priv(hcd
);
125 ohci
= hcd_to_ohci(hcd
);
127 if (pdata
== &ohci_platform_defaults
&& dev
->dev
.of_node
) {
128 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian-regs"))
129 ohci
->flags
|= OHCI_QUIRK_BE_MMIO
;
131 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian-desc"))
132 ohci
->flags
|= OHCI_QUIRK_BE_DESC
;
134 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian"))
135 ohci
->flags
|= OHCI_QUIRK_BE_MMIO
| OHCI_QUIRK_BE_DESC
;
137 if (of_property_read_bool(dev
->dev
.of_node
, "no-big-frame-no"))
138 ohci
->flags
|= OHCI_QUIRK_FRAME_NO
;
140 if (of_property_read_bool(dev
->dev
.of_node
,
141 "remote-wakeup-connected"))
142 ohci
->hc_control
= OHCI_CTRL_RWC
;
144 of_property_read_u32(dev
->dev
.of_node
, "num-ports",
147 for (clk
= 0; clk
< OHCI_MAX_CLKS
; clk
++) {
148 priv
->clks
[clk
] = of_clk_get(dev
->dev
.of_node
, clk
);
149 if (IS_ERR(priv
->clks
[clk
])) {
150 err
= PTR_ERR(priv
->clks
[clk
]);
151 if (err
== -EPROBE_DEFER
)
153 priv
->clks
[clk
] = NULL
;
158 priv
->resets
= devm_reset_control_array_get_optional_shared(
160 if (IS_ERR(priv
->resets
)) {
161 err
= PTR_ERR(priv
->resets
);
165 err
= reset_control_deassert(priv
->resets
);
170 if (pdata
->big_endian_desc
)
171 ohci
->flags
|= OHCI_QUIRK_BE_DESC
;
172 if (pdata
->big_endian_mmio
)
173 ohci
->flags
|= OHCI_QUIRK_BE_MMIO
;
174 if (pdata
->no_big_frame_no
)
175 ohci
->flags
|= OHCI_QUIRK_FRAME_NO
;
176 if (pdata
->num_ports
)
177 ohci
->num_ports
= pdata
->num_ports
;
179 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
180 if (ohci
->flags
& OHCI_QUIRK_BE_MMIO
) {
182 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
187 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
188 if (ohci
->flags
& OHCI_QUIRK_BE_DESC
) {
190 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
196 pm_runtime_set_active(&dev
->dev
);
197 pm_runtime_enable(&dev
->dev
);
198 if (pdata
->power_on
) {
199 err
= pdata
->power_on(dev
);
204 res_mem
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
205 hcd
->regs
= devm_ioremap_resource(&dev
->dev
, res_mem
);
206 if (IS_ERR(hcd
->regs
)) {
207 err
= PTR_ERR(hcd
->regs
);
210 hcd
->rsrc_start
= res_mem
->start
;
211 hcd
->rsrc_len
= resource_size(res_mem
);
213 err
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
217 device_wakeup_enable(hcd
->self
.controller
);
219 platform_set_drvdata(dev
, hcd
);
224 if (pdata
->power_off
)
225 pdata
->power_off(dev
);
227 pm_runtime_disable(&dev
->dev
);
228 reset_control_assert(priv
->resets
);
231 clk_put(priv
->clks
[clk
]);
233 if (pdata
== &ohci_platform_defaults
)
234 dev
->dev
.platform_data
= NULL
;
241 static int ohci_platform_remove(struct platform_device
*dev
)
243 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
244 struct usb_ohci_pdata
*pdata
= dev_get_platdata(&dev
->dev
);
245 struct ohci_platform_priv
*priv
= hcd_to_ohci_priv(hcd
);
248 pm_runtime_get_sync(&dev
->dev
);
251 if (pdata
->power_off
)
252 pdata
->power_off(dev
);
254 reset_control_assert(priv
->resets
);
256 for (clk
= 0; clk
< OHCI_MAX_CLKS
&& priv
->clks
[clk
]; clk
++)
257 clk_put(priv
->clks
[clk
]);
261 pm_runtime_put_sync(&dev
->dev
);
262 pm_runtime_disable(&dev
->dev
);
264 if (pdata
== &ohci_platform_defaults
)
265 dev
->dev
.platform_data
= NULL
;
270 #ifdef CONFIG_PM_SLEEP
271 static int ohci_platform_suspend(struct device
*dev
)
273 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
274 struct usb_ohci_pdata
*pdata
= dev
->platform_data
;
275 struct platform_device
*pdev
= to_platform_device(dev
);
276 bool do_wakeup
= device_may_wakeup(dev
);
279 ret
= ohci_suspend(hcd
, do_wakeup
);
283 if (pdata
->power_suspend
)
284 pdata
->power_suspend(pdev
);
289 static int ohci_platform_resume(struct device
*dev
)
291 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
292 struct usb_ohci_pdata
*pdata
= dev_get_platdata(dev
);
293 struct platform_device
*pdev
= to_platform_device(dev
);
295 if (pdata
->power_on
) {
296 int err
= pdata
->power_on(pdev
);
301 ohci_resume(hcd
, false);
303 pm_runtime_disable(dev
);
304 pm_runtime_set_active(dev
);
305 pm_runtime_enable(dev
);
309 #endif /* CONFIG_PM_SLEEP */
311 static const struct of_device_id ohci_platform_ids
[] = {
312 { .compatible
= "generic-ohci", },
313 { .compatible
= "cavium,octeon-6335-ohci", },
314 { .compatible
= "ti,ohci-omap3", },
317 MODULE_DEVICE_TABLE(of
, ohci_platform_ids
);
319 static const struct platform_device_id ohci_platform_table
[] = {
320 { "ohci-platform", 0 },
323 MODULE_DEVICE_TABLE(platform
, ohci_platform_table
);
325 static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops
, ohci_platform_suspend
,
326 ohci_platform_resume
);
328 static struct platform_driver ohci_platform_driver
= {
329 .id_table
= ohci_platform_table
,
330 .probe
= ohci_platform_probe
,
331 .remove
= ohci_platform_remove
,
332 .shutdown
= usb_hcd_platform_shutdown
,
334 .name
= "ohci-platform",
335 .pm
= &ohci_platform_pm_ops
,
336 .of_match_table
= ohci_platform_ids
,
340 static int __init
ohci_platform_init(void)
345 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
347 ohci_init_driver(&ohci_platform_hc_driver
, &platform_overrides
);
348 return platform_driver_register(&ohci_platform_driver
);
350 module_init(ohci_platform_init
);
352 static void __exit
ohci_platform_cleanup(void)
354 platform_driver_unregister(&ohci_platform_driver
);
356 module_exit(ohci_platform_cleanup
);
358 MODULE_DESCRIPTION(DRIVER_DESC
);
359 MODULE_AUTHOR("Hauke Mehrtens");
360 MODULE_AUTHOR("Alan Stern");
361 MODULE_LICENSE("GPL");