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/usb/ohci_pdriver.h>
28 #include <linux/usb.h>
29 #include <linux/usb/hcd.h>
33 #define DRIVER_DESC "OHCI generic platform driver"
34 #define OHCI_MAX_CLKS 3
35 #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
37 struct ohci_platform_priv {
38 struct clk *clks[OHCI_MAX_CLKS];
42 static const char hcd_name[] = "ohci-platform";
44 static int ohci_platform_reset(struct usb_hcd *hcd)
46 struct platform_device *pdev = to_platform_device(hcd->self.controller);
47 struct usb_ohci_pdata *pdata = dev_get_platdata(&pdev->dev);
48 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
50 if (pdata->no_big_frame_no)
51 ohci->flags |= OHCI_QUIRK_FRAME_NO;
53 ohci->num_ports = pdata->num_ports;
55 return ohci_setup(hcd);
58 static int ohci_platform_power_on(struct platform_device *dev)
60 struct usb_hcd *hcd = platform_get_drvdata(dev);
61 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
64 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
65 ret = clk_prepare_enable(priv->clks[clk]);
67 goto err_disable_clks;
71 ret = phy_init(priv->phy);
73 goto err_disable_clks;
75 ret = phy_power_on(priv->phy);
86 clk_disable_unprepare(priv->clks[clk]);
91 static void ohci_platform_power_off(struct platform_device *dev)
93 struct usb_hcd *hcd = platform_get_drvdata(dev);
94 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
98 phy_power_off(priv->phy);
102 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
104 clk_disable_unprepare(priv->clks[clk]);
107 static struct hc_driver __read_mostly ohci_platform_hc_driver;
109 static const struct ohci_driver_overrides platform_overrides __initconst = {
110 .product_desc = "Generic Platform OHCI controller",
111 .reset = ohci_platform_reset,
112 .extra_priv_size = sizeof(struct ohci_platform_priv),
115 static struct usb_ohci_pdata ohci_platform_defaults = {
116 .power_on = ohci_platform_power_on,
117 .power_suspend = ohci_platform_power_off,
118 .power_off = ohci_platform_power_off,
121 static int ohci_platform_probe(struct platform_device *dev)
124 struct resource *res_mem;
125 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
126 struct ohci_platform_priv *priv;
127 struct ohci_hcd *ohci;
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 = &ohci_platform_defaults;
140 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
144 irq = platform_get_irq(dev, 0);
146 dev_err(&dev->dev, "no irq provided");
150 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
152 dev_err(&dev->dev, "no memory resource provided");
156 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
157 dev_name(&dev->dev));
161 platform_set_drvdata(dev, hcd);
162 dev->dev.platform_data = pdata;
163 priv = hcd_to_ohci_priv(hcd);
164 ohci = hcd_to_ohci(hcd);
166 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
167 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
168 ohci->flags |= OHCI_QUIRK_BE_MMIO;
170 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
171 ohci->flags |= OHCI_QUIRK_BE_DESC;
173 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
174 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
176 priv->phy = devm_phy_get(&dev->dev, "usb");
177 if (IS_ERR(priv->phy)) {
178 err = PTR_ERR(priv->phy);
179 if (err == -EPROBE_DEFER)
184 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
185 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
186 if (IS_ERR(priv->clks[clk])) {
187 err = PTR_ERR(priv->clks[clk]);
188 if (err == -EPROBE_DEFER)
190 priv->clks[clk] = NULL;
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;
201 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
202 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
204 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
209 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
210 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
212 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
218 if (pdata->power_on) {
219 err = pdata->power_on(dev);
224 hcd->rsrc_start = res_mem->start;
225 hcd->rsrc_len = resource_size(res_mem);
227 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
228 if (IS_ERR(hcd->regs)) {
229 err = PTR_ERR(hcd->regs);
232 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
236 device_wakeup_enable(hcd->self.controller);
238 platform_set_drvdata(dev, hcd);
243 if (pdata->power_off)
244 pdata->power_off(dev);
247 clk_put(priv->clks[clk]);
249 if (pdata == &ohci_platform_defaults)
250 dev->dev.platform_data = NULL;
257 static int ohci_platform_remove(struct platform_device *dev)
259 struct usb_hcd *hcd = platform_get_drvdata(dev);
260 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
261 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
266 if (pdata->power_off)
267 pdata->power_off(dev);
269 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
270 clk_put(priv->clks[clk]);
274 if (pdata == &ohci_platform_defaults)
275 dev->dev.platform_data = NULL;
282 static int ohci_platform_suspend(struct device *dev)
284 struct usb_hcd *hcd = dev_get_drvdata(dev);
285 struct usb_ohci_pdata *pdata = dev->platform_data;
286 struct platform_device *pdev =
287 container_of(dev, struct platform_device, dev);
288 bool do_wakeup = device_may_wakeup(dev);
291 ret = ohci_suspend(hcd, do_wakeup);
295 if (pdata->power_suspend)
296 pdata->power_suspend(pdev);
301 static int ohci_platform_resume(struct device *dev)
303 struct usb_hcd *hcd = dev_get_drvdata(dev);
304 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
305 struct platform_device *pdev =
306 container_of(dev, struct platform_device, dev);
308 if (pdata->power_on) {
309 int err = pdata->power_on(pdev);
314 ohci_resume(hcd, false);
318 #else /* !CONFIG_PM */
319 #define ohci_platform_suspend NULL
320 #define ohci_platform_resume NULL
321 #endif /* CONFIG_PM */
323 static const struct of_device_id ohci_platform_ids[] = {
324 { .compatible = "generic-ohci", },
327 MODULE_DEVICE_TABLE(of, ohci_platform_ids);
329 static const struct platform_device_id ohci_platform_table[] = {
330 { "ohci-platform", 0 },
333 MODULE_DEVICE_TABLE(platform, ohci_platform_table);
335 static const struct dev_pm_ops ohci_platform_pm_ops = {
336 .suspend = ohci_platform_suspend,
337 .resume = ohci_platform_resume,
340 static struct platform_driver ohci_platform_driver = {
341 .id_table = ohci_platform_table,
342 .probe = ohci_platform_probe,
343 .remove = ohci_platform_remove,
344 .shutdown = usb_hcd_platform_shutdown,
346 .owner = THIS_MODULE,
347 .name = "ohci-platform",
348 .pm = &ohci_platform_pm_ops,
349 .of_match_table = ohci_platform_ids,
353 static int __init ohci_platform_init(void)
358 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
360 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
361 return platform_driver_register(&ohci_platform_driver);
363 module_init(ohci_platform_init);
365 static void __exit ohci_platform_cleanup(void)
367 platform_driver_unregister(&ohci_platform_driver);
369 module_exit(ohci_platform_cleanup);
371 MODULE_DESCRIPTION(DRIVER_DESC);
372 MODULE_AUTHOR("Hauke Mehrtens");
373 MODULE_AUTHOR("Alan Stern");
374 MODULE_LICENSE("GPL");