1 // SPDX-License-Identifier: GPL-2.0
3 * Generic platform ehci driver
5 * Copyright 2007 Steven Brown <sbrown@cortland.com>
6 * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
7 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
9 * Derived from the ohci-ssb driver
10 * Copyright 2007 Michael Buesch <m@bues.ch>
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);
149 dev_err(&dev
->dev
, "no irq provided");
153 hcd
= usb_create_hcd(&ehci_platform_hc_driver
, &dev
->dev
,
154 dev_name(&dev
->dev
));
158 platform_set_drvdata(dev
, hcd
);
159 dev
->dev
.platform_data
= pdata
;
160 priv
= hcd_to_ehci_priv(hcd
);
161 ehci
= hcd_to_ehci(hcd
);
163 if (pdata
== &ehci_platform_defaults
&& dev
->dev
.of_node
) {
164 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian-regs"))
165 ehci
->big_endian_mmio
= 1;
167 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian-desc"))
168 ehci
->big_endian_desc
= 1;
170 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian"))
171 ehci
->big_endian_mmio
= ehci
->big_endian_desc
= 1;
173 if (of_property_read_bool(dev
->dev
.of_node
,
174 "needs-reset-on-resume"))
175 priv
->reset_on_resume
= true;
177 if (of_property_read_bool(dev
->dev
.of_node
,
178 "has-transaction-translator"))
181 for (clk
= 0; clk
< EHCI_MAX_CLKS
; clk
++) {
182 priv
->clks
[clk
] = of_clk_get(dev
->dev
.of_node
, clk
);
183 if (IS_ERR(priv
->clks
[clk
])) {
184 err
= PTR_ERR(priv
->clks
[clk
]);
185 if (err
== -EPROBE_DEFER
)
187 priv
->clks
[clk
] = NULL
;
193 priv
->rsts
= devm_reset_control_array_get_optional_shared(&dev
->dev
);
194 if (IS_ERR(priv
->rsts
)) {
195 err
= PTR_ERR(priv
->rsts
);
199 err
= reset_control_deassert(priv
->rsts
);
203 if (pdata
->big_endian_desc
)
204 ehci
->big_endian_desc
= 1;
205 if (pdata
->big_endian_mmio
)
206 ehci
->big_endian_mmio
= 1;
209 if (pdata
->reset_on_resume
)
210 priv
->reset_on_resume
= true;
212 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
213 if (ehci
->big_endian_mmio
) {
215 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
220 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
221 if (ehci
->big_endian_desc
) {
223 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
229 if (pdata
->power_on
) {
230 err
= pdata
->power_on(dev
);
235 res_mem
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
236 hcd
->regs
= devm_ioremap_resource(&dev
->dev
, res_mem
);
237 if (IS_ERR(hcd
->regs
)) {
238 err
= PTR_ERR(hcd
->regs
);
241 hcd
->rsrc_start
= res_mem
->start
;
242 hcd
->rsrc_len
= resource_size(res_mem
);
244 err
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
248 device_wakeup_enable(hcd
->self
.controller
);
249 device_enable_async_suspend(hcd
->self
.controller
);
250 platform_set_drvdata(dev
, hcd
);
255 if (pdata
->power_off
)
256 pdata
->power_off(dev
);
258 reset_control_assert(priv
->rsts
);
261 clk_put(priv
->clks
[clk
]);
263 if (pdata
== &ehci_platform_defaults
)
264 dev
->dev
.platform_data
= NULL
;
271 static int ehci_platform_remove(struct platform_device
*dev
)
273 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
274 struct usb_ehci_pdata
*pdata
= dev_get_platdata(&dev
->dev
);
275 struct ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
280 if (pdata
->power_off
)
281 pdata
->power_off(dev
);
283 reset_control_assert(priv
->rsts
);
285 for (clk
= 0; clk
< EHCI_MAX_CLKS
&& priv
->clks
[clk
]; clk
++)
286 clk_put(priv
->clks
[clk
]);
290 if (pdata
== &ehci_platform_defaults
)
291 dev
->dev
.platform_data
= NULL
;
296 #ifdef CONFIG_PM_SLEEP
297 static int ehci_platform_suspend(struct device
*dev
)
299 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
300 struct usb_ehci_pdata
*pdata
= dev_get_platdata(dev
);
301 struct platform_device
*pdev
= to_platform_device(dev
);
302 bool do_wakeup
= device_may_wakeup(dev
);
305 ret
= ehci_suspend(hcd
, do_wakeup
);
309 if (pdata
->power_suspend
)
310 pdata
->power_suspend(pdev
);
315 static int ehci_platform_resume(struct device
*dev
)
317 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
318 struct usb_ehci_pdata
*pdata
= dev_get_platdata(dev
);
319 struct platform_device
*pdev
= to_platform_device(dev
);
320 struct ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
321 struct device
*companion_dev
;
323 if (pdata
->power_on
) {
324 int err
= pdata
->power_on(pdev
);
329 companion_dev
= usb_of_get_companion_dev(hcd
->self
.controller
);
331 device_pm_wait_for_dev(hcd
->self
.controller
, companion_dev
);
332 put_device(companion_dev
);
335 ehci_resume(hcd
, priv
->reset_on_resume
);
338 #endif /* CONFIG_PM_SLEEP */
340 static const struct of_device_id vt8500_ehci_ids
[] = {
341 { .compatible
= "via,vt8500-ehci", },
342 { .compatible
= "wm,prizm-ehci", },
343 { .compatible
= "generic-ehci", },
344 { .compatible
= "cavium,octeon-6335-ehci", },
347 MODULE_DEVICE_TABLE(of
, vt8500_ehci_ids
);
349 static const struct acpi_device_id ehci_acpi_match
[] = {
350 { "PNP0D20", 0 }, /* EHCI controller without debug */
353 MODULE_DEVICE_TABLE(acpi
, ehci_acpi_match
);
355 static const struct platform_device_id ehci_platform_table
[] = {
356 { "ehci-platform", 0 },
359 MODULE_DEVICE_TABLE(platform
, ehci_platform_table
);
361 static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops
, ehci_platform_suspend
,
362 ehci_platform_resume
);
364 static struct platform_driver ehci_platform_driver
= {
365 .id_table
= ehci_platform_table
,
366 .probe
= ehci_platform_probe
,
367 .remove
= ehci_platform_remove
,
368 .shutdown
= usb_hcd_platform_shutdown
,
370 .name
= "ehci-platform",
371 .pm
= &ehci_platform_pm_ops
,
372 .of_match_table
= vt8500_ehci_ids
,
373 .acpi_match_table
= ACPI_PTR(ehci_acpi_match
),
377 static int __init
ehci_platform_init(void)
382 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
384 ehci_init_driver(&ehci_platform_hc_driver
, &platform_overrides
);
385 return platform_driver_register(&ehci_platform_driver
);
387 module_init(ehci_platform_init
);
389 static void __exit
ehci_platform_cleanup(void)
391 platform_driver_unregister(&ehci_platform_driver
);
393 module_exit(ehci_platform_cleanup
);
395 MODULE_DESCRIPTION(DRIVER_DESC
);
396 MODULE_AUTHOR("Hauke Mehrtens");
397 MODULE_AUTHOR("Alan Stern");
398 MODULE_LICENSE("GPL");