2 * Generic platform ohci driver
4 * Copyright 2007 Michael Buesch <m@bues.ch>
5 * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
6 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
8 * Derived from the OCHI-SSB driver
9 * Derived from the OHCI-PCI driver
10 * Copyright 1999 Roman Weissgaerber
11 * Copyright 2000-2002 David Brownell
12 * Copyright 1999 Linus Torvalds
13 * Copyright 1999 Gregory P. Smith
15 * Licensed under the GNU/GPL. See COPYING for details.
18 #include <linux/clk.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/hrtimer.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/err.h>
25 #include <linux/phy/phy.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/reset.h>
29 #include <linux/usb/ohci_pdriver.h>
30 #include <linux/usb.h>
31 #include <linux/usb/hcd.h>
35 #define DRIVER_DESC "OHCI generic platform driver"
36 #define OHCI_MAX_CLKS 3
37 #define OHCI_MAX_RESETS 2
38 #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
40 struct ohci_platform_priv
{
41 struct clk
*clks
[OHCI_MAX_CLKS
];
42 struct reset_control
*resets
[OHCI_MAX_RESETS
];
47 static const char hcd_name
[] = "ohci-platform";
49 static int ohci_platform_power_on(struct platform_device
*dev
)
51 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
52 struct ohci_platform_priv
*priv
= hcd_to_ohci_priv(hcd
);
53 int clk
, ret
, phy_num
;
55 for (clk
= 0; clk
< OHCI_MAX_CLKS
&& priv
->clks
[clk
]; clk
++) {
56 ret
= clk_prepare_enable(priv
->clks
[clk
]);
58 goto err_disable_clks
;
61 for (phy_num
= 0; phy_num
< priv
->num_phys
; phy_num
++) {
62 ret
= phy_init(priv
->phys
[phy_num
]);
65 ret
= phy_power_on(priv
->phys
[phy_num
]);
67 phy_exit(priv
->phys
[phy_num
]);
75 while (--phy_num
>= 0) {
76 phy_power_off(priv
->phys
[phy_num
]);
77 phy_exit(priv
->phys
[phy_num
]);
81 clk_disable_unprepare(priv
->clks
[clk
]);
86 static void ohci_platform_power_off(struct platform_device
*dev
)
88 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
89 struct ohci_platform_priv
*priv
= hcd_to_ohci_priv(hcd
);
92 for (phy_num
= 0; phy_num
< priv
->num_phys
; phy_num
++) {
93 phy_power_off(priv
->phys
[phy_num
]);
94 phy_exit(priv
->phys
[phy_num
]);
97 for (clk
= OHCI_MAX_CLKS
- 1; clk
>= 0; clk
--)
99 clk_disable_unprepare(priv
->clks
[clk
]);
102 static struct hc_driver __read_mostly ohci_platform_hc_driver
;
104 static const struct ohci_driver_overrides platform_overrides __initconst
= {
105 .product_desc
= "Generic Platform OHCI controller",
106 .extra_priv_size
= sizeof(struct ohci_platform_priv
),
109 static struct usb_ohci_pdata ohci_platform_defaults
= {
110 .power_on
= ohci_platform_power_on
,
111 .power_suspend
= ohci_platform_power_off
,
112 .power_off
= ohci_platform_power_off
,
115 static int ohci_platform_probe(struct platform_device
*dev
)
118 struct resource
*res_mem
;
119 struct usb_ohci_pdata
*pdata
= dev_get_platdata(&dev
->dev
);
120 struct ohci_platform_priv
*priv
;
121 struct ohci_hcd
*ohci
;
122 int err
, irq
, phy_num
, clk
= 0, rst
= 0;
128 * Use reasonable defaults so platforms don't have to provide these
129 * with DT probing on ARM.
132 pdata
= &ohci_platform_defaults
;
134 err
= dma_coerce_mask_and_coherent(&dev
->dev
, DMA_BIT_MASK(32));
138 irq
= platform_get_irq(dev
, 0);
140 dev_err(&dev
->dev
, "no irq provided");
144 hcd
= usb_create_hcd(&ohci_platform_hc_driver
, &dev
->dev
,
145 dev_name(&dev
->dev
));
149 platform_set_drvdata(dev
, hcd
);
150 dev
->dev
.platform_data
= pdata
;
151 priv
= hcd_to_ohci_priv(hcd
);
152 ohci
= hcd_to_ohci(hcd
);
154 if (pdata
== &ohci_platform_defaults
&& dev
->dev
.of_node
) {
155 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian-regs"))
156 ohci
->flags
|= OHCI_QUIRK_BE_MMIO
;
158 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian-desc"))
159 ohci
->flags
|= OHCI_QUIRK_BE_DESC
;
161 if (of_property_read_bool(dev
->dev
.of_node
, "big-endian"))
162 ohci
->flags
|= OHCI_QUIRK_BE_MMIO
| OHCI_QUIRK_BE_DESC
;
164 if (of_property_read_bool(dev
->dev
.of_node
, "no-big-frame-no"))
165 ohci
->flags
|= OHCI_QUIRK_FRAME_NO
;
167 if (of_property_read_bool(dev
->dev
.of_node
,
168 "remote-wakeup-connected"))
169 ohci
->hc_control
= OHCI_CTRL_RWC
;
171 of_property_read_u32(dev
->dev
.of_node
, "num-ports",
174 priv
->num_phys
= of_count_phandle_with_args(dev
->dev
.of_node
,
175 "phys", "#phy-cells");
177 if (priv
->num_phys
> 0) {
178 priv
->phys
= devm_kcalloc(&dev
->dev
, priv
->num_phys
,
179 sizeof(struct phy
*), GFP_KERNEL
);
185 for (phy_num
= 0; phy_num
< priv
->num_phys
; phy_num
++) {
186 priv
->phys
[phy_num
] = devm_of_phy_get_by_index(
187 &dev
->dev
, dev
->dev
.of_node
, phy_num
);
188 if (IS_ERR(priv
->phys
[phy_num
])) {
189 err
= PTR_ERR(priv
->phys
[phy_num
]);
191 } else if (!hcd
->phy
) {
192 /* Avoiding phy_get() in usb_add_hcd() */
193 hcd
->phy
= priv
->phys
[phy_num
];
197 for (clk
= 0; clk
< OHCI_MAX_CLKS
; clk
++) {
198 priv
->clks
[clk
] = of_clk_get(dev
->dev
.of_node
, clk
);
199 if (IS_ERR(priv
->clks
[clk
])) {
200 err
= PTR_ERR(priv
->clks
[clk
]);
201 if (err
== -EPROBE_DEFER
)
203 priv
->clks
[clk
] = NULL
;
207 for (rst
= 0; rst
< OHCI_MAX_RESETS
; rst
++) {
209 devm_reset_control_get_shared_by_index(
211 if (IS_ERR(priv
->resets
[rst
])) {
212 err
= PTR_ERR(priv
->resets
[rst
]);
213 if (err
== -EPROBE_DEFER
)
215 priv
->resets
[rst
] = NULL
;
218 err
= reset_control_deassert(priv
->resets
[rst
]);
224 if (pdata
->big_endian_desc
)
225 ohci
->flags
|= OHCI_QUIRK_BE_DESC
;
226 if (pdata
->big_endian_mmio
)
227 ohci
->flags
|= OHCI_QUIRK_BE_MMIO
;
228 if (pdata
->no_big_frame_no
)
229 ohci
->flags
|= OHCI_QUIRK_FRAME_NO
;
230 if (pdata
->num_ports
)
231 ohci
->num_ports
= pdata
->num_ports
;
233 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
234 if (ohci
->flags
& OHCI_QUIRK_BE_MMIO
) {
236 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
241 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
242 if (ohci
->flags
& OHCI_QUIRK_BE_DESC
) {
244 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
250 pm_runtime_set_active(&dev
->dev
);
251 pm_runtime_enable(&dev
->dev
);
252 if (pdata
->power_on
) {
253 err
= pdata
->power_on(dev
);
258 res_mem
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
259 hcd
->regs
= devm_ioremap_resource(&dev
->dev
, res_mem
);
260 if (IS_ERR(hcd
->regs
)) {
261 err
= PTR_ERR(hcd
->regs
);
264 hcd
->rsrc_start
= res_mem
->start
;
265 hcd
->rsrc_len
= resource_size(res_mem
);
267 err
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
271 device_wakeup_enable(hcd
->self
.controller
);
273 platform_set_drvdata(dev
, hcd
);
278 if (pdata
->power_off
)
279 pdata
->power_off(dev
);
281 pm_runtime_disable(&dev
->dev
);
283 reset_control_assert(priv
->resets
[rst
]);
286 clk_put(priv
->clks
[clk
]);
288 if (pdata
== &ohci_platform_defaults
)
289 dev
->dev
.platform_data
= NULL
;
296 static int ohci_platform_remove(struct platform_device
*dev
)
298 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
299 struct usb_ohci_pdata
*pdata
= dev_get_platdata(&dev
->dev
);
300 struct ohci_platform_priv
*priv
= hcd_to_ohci_priv(hcd
);
303 pm_runtime_get_sync(&dev
->dev
);
306 if (pdata
->power_off
)
307 pdata
->power_off(dev
);
309 for (rst
= 0; rst
< OHCI_MAX_RESETS
&& priv
->resets
[rst
]; rst
++)
310 reset_control_assert(priv
->resets
[rst
]);
312 for (clk
= 0; clk
< OHCI_MAX_CLKS
&& priv
->clks
[clk
]; clk
++)
313 clk_put(priv
->clks
[clk
]);
317 pm_runtime_put_sync(&dev
->dev
);
318 pm_runtime_disable(&dev
->dev
);
320 if (pdata
== &ohci_platform_defaults
)
321 dev
->dev
.platform_data
= NULL
;
326 #ifdef CONFIG_PM_SLEEP
327 static int ohci_platform_suspend(struct device
*dev
)
329 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
330 struct usb_ohci_pdata
*pdata
= dev
->platform_data
;
331 struct platform_device
*pdev
= to_platform_device(dev
);
332 bool do_wakeup
= device_may_wakeup(dev
);
335 ret
= ohci_suspend(hcd
, do_wakeup
);
339 if (pdata
->power_suspend
)
340 pdata
->power_suspend(pdev
);
345 static int ohci_platform_resume(struct device
*dev
)
347 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
348 struct usb_ohci_pdata
*pdata
= dev_get_platdata(dev
);
349 struct platform_device
*pdev
= to_platform_device(dev
);
351 if (pdata
->power_on
) {
352 int err
= pdata
->power_on(pdev
);
357 ohci_resume(hcd
, false);
360 #endif /* CONFIG_PM_SLEEP */
362 static const struct of_device_id ohci_platform_ids
[] = {
363 { .compatible
= "generic-ohci", },
364 { .compatible
= "cavium,octeon-6335-ohci", },
365 { .compatible
= "ti,ohci-omap3", },
368 MODULE_DEVICE_TABLE(of
, ohci_platform_ids
);
370 static const struct platform_device_id ohci_platform_table
[] = {
371 { "ohci-platform", 0 },
374 MODULE_DEVICE_TABLE(platform
, ohci_platform_table
);
376 static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops
, ohci_platform_suspend
,
377 ohci_platform_resume
);
379 static struct platform_driver ohci_platform_driver
= {
380 .id_table
= ohci_platform_table
,
381 .probe
= ohci_platform_probe
,
382 .remove
= ohci_platform_remove
,
383 .shutdown
= usb_hcd_platform_shutdown
,
385 .name
= "ohci-platform",
386 .pm
= &ohci_platform_pm_ops
,
387 .of_match_table
= ohci_platform_ids
,
391 static int __init
ohci_platform_init(void)
396 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
398 ohci_init_driver(&ohci_platform_hc_driver
, &platform_overrides
);
399 return platform_driver_register(&ohci_platform_driver
);
401 module_init(ohci_platform_init
);
403 static void __exit
ohci_platform_cleanup(void)
405 platform_driver_unregister(&ohci_platform_driver
);
407 module_exit(ohci_platform_cleanup
);
409 MODULE_DESCRIPTION(DRIVER_DESC
);
410 MODULE_AUTHOR("Hauke Mehrtens");
411 MODULE_AUTHOR("Alan Stern");
412 MODULE_LICENSE("GPL");