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/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 *rst;
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;
59 ret = phy_init(priv->phy);
61 goto err_disable_clks;
63 ret = phy_power_on(priv->phy);
74 clk_disable_unprepare(priv->clks[clk]);
79 static void ohci_platform_power_off(struct platform_device *dev)
81 struct usb_hcd *hcd = platform_get_drvdata(dev);
82 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
86 phy_power_off(priv->phy);
90 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
92 clk_disable_unprepare(priv->clks[clk]);
95 static struct hc_driver __read_mostly ohci_platform_hc_driver;
97 static const struct ohci_driver_overrides platform_overrides __initconst = {
98 .product_desc = "Generic Platform OHCI controller",
99 .extra_priv_size = sizeof(struct ohci_platform_priv),
102 static struct usb_ohci_pdata ohci_platform_defaults = {
103 .power_on = ohci_platform_power_on,
104 .power_suspend = ohci_platform_power_off,
105 .power_off = ohci_platform_power_off,
108 static int ohci_platform_probe(struct platform_device *dev)
111 struct resource *res_mem;
112 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
113 struct ohci_platform_priv *priv;
114 struct ohci_hcd *ohci;
115 int err, irq, clk = 0;
121 * Use reasonable defaults so platforms don't have to provide these
122 * with DT probing on ARM.
125 pdata = &ohci_platform_defaults;
127 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
131 irq = platform_get_irq(dev, 0);
133 dev_err(&dev->dev, "no irq provided");
137 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
138 dev_name(&dev->dev));
142 platform_set_drvdata(dev, hcd);
143 dev->dev.platform_data = pdata;
144 priv = hcd_to_ohci_priv(hcd);
145 ohci = hcd_to_ohci(hcd);
147 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
148 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
149 ohci->flags |= OHCI_QUIRK_BE_MMIO;
151 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
152 ohci->flags |= OHCI_QUIRK_BE_DESC;
154 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
155 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
157 if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
158 ohci->flags |= OHCI_QUIRK_FRAME_NO;
160 of_property_read_u32(dev->dev.of_node, "num-ports",
163 priv->phy = devm_phy_get(&dev->dev, "usb");
164 if (IS_ERR(priv->phy)) {
165 err = PTR_ERR(priv->phy);
166 if (err == -EPROBE_DEFER)
171 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
172 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
173 if (IS_ERR(priv->clks[clk])) {
174 err = PTR_ERR(priv->clks[clk]);
175 if (err == -EPROBE_DEFER)
177 priv->clks[clk] = NULL;
184 priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
185 if (IS_ERR(priv->rst)) {
186 err = PTR_ERR(priv->rst);
187 if (err == -EPROBE_DEFER)
191 err = reset_control_deassert(priv->rst);
196 if (pdata->big_endian_desc)
197 ohci->flags |= OHCI_QUIRK_BE_DESC;
198 if (pdata->big_endian_mmio)
199 ohci->flags |= OHCI_QUIRK_BE_MMIO;
200 if (pdata->no_big_frame_no)
201 ohci->flags |= OHCI_QUIRK_FRAME_NO;
202 if (pdata->num_ports)
203 ohci->num_ports = pdata->num_ports;
205 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
206 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
208 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
213 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
214 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
216 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
222 if (pdata->power_on) {
223 err = pdata->power_on(dev);
228 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
229 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
230 if (IS_ERR(hcd->regs)) {
231 err = PTR_ERR(hcd->regs);
234 hcd->rsrc_start = res_mem->start;
235 hcd->rsrc_len = resource_size(res_mem);
237 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
241 device_wakeup_enable(hcd->self.controller);
243 platform_set_drvdata(dev, hcd);
248 if (pdata->power_off)
249 pdata->power_off(dev);
252 reset_control_assert(priv->rst);
255 clk_put(priv->clks[clk]);
257 if (pdata == &ohci_platform_defaults)
258 dev->dev.platform_data = NULL;
265 static int ohci_platform_remove(struct platform_device *dev)
267 struct usb_hcd *hcd = platform_get_drvdata(dev);
268 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
269 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
274 if (pdata->power_off)
275 pdata->power_off(dev);
278 reset_control_assert(priv->rst);
280 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
281 clk_put(priv->clks[clk]);
285 if (pdata == &ohci_platform_defaults)
286 dev->dev.platform_data = NULL;
291 #ifdef CONFIG_PM_SLEEP
292 static int ohci_platform_suspend(struct device *dev)
294 struct usb_hcd *hcd = dev_get_drvdata(dev);
295 struct usb_ohci_pdata *pdata = dev->platform_data;
296 struct platform_device *pdev =
297 container_of(dev, struct platform_device, dev);
298 bool do_wakeup = device_may_wakeup(dev);
301 ret = ohci_suspend(hcd, do_wakeup);
305 if (pdata->power_suspend)
306 pdata->power_suspend(pdev);
311 static int ohci_platform_resume(struct device *dev)
313 struct usb_hcd *hcd = dev_get_drvdata(dev);
314 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
315 struct platform_device *pdev =
316 container_of(dev, struct platform_device, dev);
318 if (pdata->power_on) {
319 int err = pdata->power_on(pdev);
324 ohci_resume(hcd, false);
327 #endif /* CONFIG_PM_SLEEP */
329 static const struct of_device_id ohci_platform_ids[] = {
330 { .compatible = "generic-ohci", },
333 MODULE_DEVICE_TABLE(of, ohci_platform_ids);
335 static const struct platform_device_id ohci_platform_table[] = {
336 { "ohci-platform", 0 },
339 MODULE_DEVICE_TABLE(platform, ohci_platform_table);
341 static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
342 ohci_platform_resume);
344 static struct platform_driver ohci_platform_driver = {
345 .id_table = ohci_platform_table,
346 .probe = ohci_platform_probe,
347 .remove = ohci_platform_remove,
348 .shutdown = usb_hcd_platform_shutdown,
350 .owner = THIS_MODULE,
351 .name = "ohci-platform",
352 .pm = &ohci_platform_pm_ops,
353 .of_match_table = ohci_platform_ids,
357 static int __init ohci_platform_init(void)
362 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
364 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
365 return platform_driver_register(&ohci_platform_driver);
367 module_init(ohci_platform_init);
369 static void __exit ohci_platform_cleanup(void)
371 platform_driver_unregister(&ohci_platform_driver);
373 module_exit(ohci_platform_cleanup);
375 MODULE_DESCRIPTION(DRIVER_DESC);
376 MODULE_AUTHOR("Hauke Mehrtens");
377 MODULE_AUTHOR("Alan Stern");
378 MODULE_LICENSE("GPL");