2 * Generic platform ehci driver
4 * Copyright 2007 Steven Brown <sbrown@cortland.com>
5 * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
6 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
8 * Derived from the ohci-ssb driver
9 * Copyright 2007 Michael Buesch <m@bues.ch>
11 * Derived from the EHCI-PCI driver
12 * Copyright (c) 2000-2004 by David Brownell
14 * Derived from the ohci-pci driver
15 * Copyright 1999 Roman Weissgaerber
16 * Copyright 2000-2002 David Brownell
17 * Copyright 1999 Linus Torvalds
18 * Copyright 1999 Gregory P. Smith
20 * Licensed under the GNU/GPL. See COPYING for details.
22 #include <linux/acpi.h>
23 #include <linux/clk.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/err.h>
26 #include <linux/kernel.h>
27 #include <linux/hrtimer.h>
29 #include <linux/module.h>
31 #include <linux/phy/phy.h>
32 #include <linux/platform_device.h>
33 #include <linux/reset.h>
34 #include <linux/usb.h>
35 #include <linux/usb/hcd.h>
36 #include <linux/usb/ehci_pdriver.h>
37 #include <linux/usb/of.h>
41 #define DRIVER_DESC "EHCI generic platform driver"
42 #define EHCI_MAX_CLKS 4
43 #define EHCI_MAX_RSTS 4
44 #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
46 struct ehci_platform_priv
{
47 struct clk
*clks
[EHCI_MAX_CLKS
];
48 struct reset_control
*rsts
[EHCI_MAX_RSTS
];
54 static const char hcd_name
[] = "ehci-platform";
56 static int ehci_platform_reset(struct usb_hcd
*hcd
)
58 struct platform_device
*pdev
= to_platform_device(hcd
->self
.controller
);
59 struct usb_ehci_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
60 struct ehci_hcd
*ehci
= hcd_to_ehci(hcd
);
63 ehci
->has_synopsys_hc_bug
= pdata
->has_synopsys_hc_bug
;
65 if (pdata
->pre_setup
) {
66 retval
= pdata
->pre_setup(hcd
);
71 ehci
->caps
= hcd
->regs
+ pdata
->caps_offset
;
72 retval
= ehci_setup(hcd
);
76 if (pdata
->no_io_watchdog
)
77 ehci
->need_io_watchdog
= 0;
81 static int ehci_platform_power_on(struct platform_device
*dev
)
83 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
84 struct ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
85 int clk
, ret
, phy_num
;
87 for (clk
= 0; clk
< EHCI_MAX_CLKS
&& priv
->clks
[clk
]; clk
++) {
88 ret
= clk_prepare_enable(priv
->clks
[clk
]);
90 goto err_disable_clks
;
93 for (phy_num
= 0; phy_num
< priv
->num_phys
; phy_num
++) {
94 ret
= phy_init(priv
->phys
[phy_num
]);
97 ret
= phy_power_on(priv
->phys
[phy_num
]);
99 phy_exit(priv
->phys
[phy_num
]);
107 while (--phy_num
>= 0) {
108 phy_power_off(priv
->phys
[phy_num
]);
109 phy_exit(priv
->phys
[phy_num
]);
113 clk_disable_unprepare(priv
->clks
[clk
]);
118 static void ehci_platform_power_off(struct platform_device
*dev
)
120 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
121 struct ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
124 for (phy_num
= 0; phy_num
< priv
->num_phys
; phy_num
++) {
125 phy_power_off(priv
->phys
[phy_num
]);
126 phy_exit(priv
->phys
[phy_num
]);
129 for (clk
= EHCI_MAX_CLKS
- 1; clk
>= 0; clk
--)
131 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
= ehci_platform_reset
,
138 .extra_priv_size
= sizeof(struct ehci_platform_priv
),
141 static struct usb_ehci_pdata ehci_platform_defaults
= {
142 .power_on
= ehci_platform_power_on
,
143 .power_suspend
= ehci_platform_power_off
,
144 .power_off
= ehci_platform_power_off
,
147 static int ehci_platform_probe(struct platform_device
*dev
)
150 struct resource
*res_mem
;
151 struct usb_ehci_pdata
*pdata
= dev_get_platdata(&dev
->dev
);
152 struct ehci_platform_priv
*priv
;
153 struct ehci_hcd
*ehci
;
154 int err
, irq
, phy_num
, clk
= 0, rst
;
160 * Use reasonable defaults so platforms don't have to provide these
161 * with DT probing on ARM.
164 pdata
= &ehci_platform_defaults
;
166 err
= dma_coerce_mask_and_coherent(&dev
->dev
,
167 pdata
->dma_mask_64
? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
169 dev_err(&dev
->dev
, "Error: DMA mask configuration failed\n");
173 irq
= platform_get_irq(dev
, 0);
175 dev_err(&dev
->dev
, "no irq provided");
179 hcd
= usb_create_hcd(&ehci_platform_hc_driver
, &dev
->dev
,
180 dev_name(&dev
->dev
));
184 platform_set_drvdata(dev
, hcd
);
185 dev
->dev
.platform_data
= pdata
;
186 priv
= hcd_to_ehci_priv(hcd
);
187 ehci
= hcd_to_ehci(hcd
);
189 if (pdata
== &ehci_platform_defaults
&& dev
->dev
.of_node
) {
190 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian-regs"))
191 ehci
->big_endian_mmio
= 1;
193 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian-desc"))
194 ehci
->big_endian_desc
= 1;
196 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian"))
197 ehci
->big_endian_mmio
= ehci
->big_endian_desc
= 1;
199 if (of_property_read_bool(dev
->dev
.of_node
,
200 "needs-reset-on-resume"))
201 priv
->reset_on_resume
= true;
203 if (of_property_read_bool(dev
->dev
.of_node
,
204 "has-transaction-translator"))
207 priv
->num_phys
= of_count_phandle_with_args(dev
->dev
.of_node
,
208 "phys", "#phy-cells");
210 if (priv
->num_phys
> 0) {
211 priv
->phys
= devm_kcalloc(&dev
->dev
, priv
->num_phys
,
212 sizeof(struct phy
*), GFP_KERNEL
);
218 for (phy_num
= 0; phy_num
< priv
->num_phys
; phy_num
++) {
219 priv
->phys
[phy_num
] = devm_of_phy_get_by_index(
220 &dev
->dev
, dev
->dev
.of_node
, phy_num
);
221 if (IS_ERR(priv
->phys
[phy_num
])) {
222 err
= PTR_ERR(priv
->phys
[phy_num
]);
224 } else if (!hcd
->phy
) {
225 /* Avoiding phy_get() in usb_add_hcd() */
226 hcd
->phy
= priv
->phys
[phy_num
];
230 for (clk
= 0; clk
< EHCI_MAX_CLKS
; clk
++) {
231 priv
->clks
[clk
] = of_clk_get(dev
->dev
.of_node
, clk
);
232 if (IS_ERR(priv
->clks
[clk
])) {
233 err
= PTR_ERR(priv
->clks
[clk
]);
234 if (err
== -EPROBE_DEFER
)
236 priv
->clks
[clk
] = NULL
;
242 for (rst
= 0; rst
< EHCI_MAX_RSTS
; rst
++) {
243 priv
->rsts
[rst
] = devm_reset_control_get_shared_by_index(
245 if (IS_ERR(priv
->rsts
[rst
])) {
246 err
= PTR_ERR(priv
->rsts
[rst
]);
247 if (err
== -EPROBE_DEFER
)
249 priv
->rsts
[rst
] = NULL
;
253 err
= reset_control_deassert(priv
->rsts
[rst
]);
258 if (pdata
->big_endian_desc
)
259 ehci
->big_endian_desc
= 1;
260 if (pdata
->big_endian_mmio
)
261 ehci
->big_endian_mmio
= 1;
264 if (pdata
->reset_on_resume
)
265 priv
->reset_on_resume
= true;
267 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
268 if (ehci
->big_endian_mmio
) {
270 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
275 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
276 if (ehci
->big_endian_desc
) {
278 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
284 if (pdata
->power_on
) {
285 err
= pdata
->power_on(dev
);
290 res_mem
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
291 hcd
->regs
= devm_ioremap_resource(&dev
->dev
, res_mem
);
292 if (IS_ERR(hcd
->regs
)) {
293 err
= PTR_ERR(hcd
->regs
);
296 hcd
->rsrc_start
= res_mem
->start
;
297 hcd
->rsrc_len
= resource_size(res_mem
);
299 err
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
303 device_wakeup_enable(hcd
->self
.controller
);
304 device_enable_async_suspend(hcd
->self
.controller
);
305 platform_set_drvdata(dev
, hcd
);
310 if (pdata
->power_off
)
311 pdata
->power_off(dev
);
314 reset_control_assert(priv
->rsts
[rst
]);
317 clk_put(priv
->clks
[clk
]);
319 if (pdata
== &ehci_platform_defaults
)
320 dev
->dev
.platform_data
= NULL
;
327 static int ehci_platform_remove(struct platform_device
*dev
)
329 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
330 struct usb_ehci_pdata
*pdata
= dev_get_platdata(&dev
->dev
);
331 struct ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
336 if (pdata
->power_off
)
337 pdata
->power_off(dev
);
339 for (rst
= 0; rst
< EHCI_MAX_RSTS
&& priv
->rsts
[rst
]; rst
++)
340 reset_control_assert(priv
->rsts
[rst
]);
342 for (clk
= 0; clk
< EHCI_MAX_CLKS
&& priv
->clks
[clk
]; clk
++)
343 clk_put(priv
->clks
[clk
]);
347 if (pdata
== &ehci_platform_defaults
)
348 dev
->dev
.platform_data
= NULL
;
353 #ifdef CONFIG_PM_SLEEP
354 static int ehci_platform_suspend(struct device
*dev
)
356 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
357 struct usb_ehci_pdata
*pdata
= dev_get_platdata(dev
);
358 struct platform_device
*pdev
= to_platform_device(dev
);
359 bool do_wakeup
= device_may_wakeup(dev
);
362 ret
= ehci_suspend(hcd
, do_wakeup
);
366 if (pdata
->power_suspend
)
367 pdata
->power_suspend(pdev
);
372 static int ehci_platform_resume(struct device
*dev
)
374 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
375 struct usb_ehci_pdata
*pdata
= dev_get_platdata(dev
);
376 struct platform_device
*pdev
= to_platform_device(dev
);
377 struct ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
378 struct device
*companion_dev
;
380 if (pdata
->power_on
) {
381 int err
= pdata
->power_on(pdev
);
386 companion_dev
= usb_of_get_companion_dev(hcd
->self
.controller
);
388 device_pm_wait_for_dev(hcd
->self
.controller
, companion_dev
);
389 put_device(companion_dev
);
392 ehci_resume(hcd
, priv
->reset_on_resume
);
395 #endif /* CONFIG_PM_SLEEP */
397 static const struct of_device_id vt8500_ehci_ids
[] = {
398 { .compatible
= "via,vt8500-ehci", },
399 { .compatible
= "wm,prizm-ehci", },
400 { .compatible
= "generic-ehci", },
401 { .compatible
= "cavium,octeon-6335-ehci", },
404 MODULE_DEVICE_TABLE(of
, vt8500_ehci_ids
);
406 static const struct acpi_device_id ehci_acpi_match
[] = {
407 { "PNP0D20", 0 }, /* EHCI controller without debug */
410 MODULE_DEVICE_TABLE(acpi
, ehci_acpi_match
);
412 static const struct platform_device_id ehci_platform_table
[] = {
413 { "ehci-platform", 0 },
416 MODULE_DEVICE_TABLE(platform
, ehci_platform_table
);
418 static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops
, ehci_platform_suspend
,
419 ehci_platform_resume
);
421 static struct platform_driver ehci_platform_driver
= {
422 .id_table
= ehci_platform_table
,
423 .probe
= ehci_platform_probe
,
424 .remove
= ehci_platform_remove
,
425 .shutdown
= usb_hcd_platform_shutdown
,
427 .name
= "ehci-platform",
428 .pm
= &ehci_platform_pm_ops
,
429 .of_match_table
= vt8500_ehci_ids
,
430 .acpi_match_table
= ACPI_PTR(ehci_acpi_match
),
434 static int __init
ehci_platform_init(void)
439 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
441 ehci_init_driver(&ehci_platform_hc_driver
, &platform_overrides
);
442 return platform_driver_register(&ehci_platform_driver
);
444 module_init(ehci_platform_init
);
446 static void __exit
ehci_platform_cleanup(void)
448 platform_driver_unregister(&ehci_platform_driver
);
450 module_exit(ehci_platform_cleanup
);
452 MODULE_DESCRIPTION(DRIVER_DESC
);
453 MODULE_AUTHOR("Hauke Mehrtens");
454 MODULE_AUTHOR("Alan Stern");
455 MODULE_LICENSE("GPL");