blob: b6ca0b25259adef55351d489c3055c3aa2728c26 [file] [log] [blame]
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +01001/*
2 * Generic platform ohci driver
3 *
4 * Copyright 2007 Michael Buesch <m@bues.ch>
5 * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
Hans de Goedeca52a172014-02-07 16:36:40 +01006 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +01007 *
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
14 *
15 * Licensed under the GNU/GPL. See COPYING for details.
16 */
Manjunath Goudar928fb682013-06-03 20:46:08 +053017
Hans de Goedeca52a172014-02-07 16:36:40 +010018#include <linux/clk.h>
19#include <linux/dma-mapping.h>
Manjunath Goudar928fb682013-06-03 20:46:08 +053020#include <linux/hrtimer.h>
21#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
Thierry Reding148e1132013-01-21 11:09:22 +010024#include <linux/err.h>
Hans de Goedeca52a172014-02-07 16:36:40 +010025#include <linux/phy/phy.h>
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010026#include <linux/platform_device.h>
27#include <linux/usb/ohci_pdriver.h>
Manjunath Goudar928fb682013-06-03 20:46:08 +053028#include <linux/usb.h>
29#include <linux/usb/hcd.h>
30
31#include "ohci.h"
32
33#define DRIVER_DESC "OHCI generic platform driver"
Hans de Goedeca52a172014-02-07 16:36:40 +010034#define OHCI_MAX_CLKS 3
35#define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
36
37struct ohci_platform_priv {
38 struct clk *clks[OHCI_MAX_CLKS];
39 struct phy *phy;
40};
Manjunath Goudar928fb682013-06-03 20:46:08 +053041
42static const char hcd_name[] = "ohci-platform";
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010043
44static int ohci_platform_reset(struct usb_hcd *hcd)
45{
46 struct platform_device *pdev = to_platform_device(hcd->self.controller);
Jingoo Hand4f09e22013-07-30 19:59:40 +090047 struct usb_ohci_pdata *pdata = dev_get_platdata(&pdev->dev);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010048 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010049
50 if (pdata->big_endian_desc)
51 ohci->flags |= OHCI_QUIRK_BE_DESC;
52 if (pdata->big_endian_mmio)
53 ohci->flags |= OHCI_QUIRK_BE_MMIO;
54 if (pdata->no_big_frame_no)
55 ohci->flags |= OHCI_QUIRK_FRAME_NO;
Florian Fainelli2b16e392012-10-08 15:11:26 +020056 if (pdata->num_ports)
57 ohci->num_ports = pdata->num_ports;
58
Manjunath Goudar928fb682013-06-03 20:46:08 +053059 return ohci_setup(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010060}
61
Hans de Goedeca52a172014-02-07 16:36:40 +010062static int ohci_platform_power_on(struct platform_device *dev)
63{
64 struct usb_hcd *hcd = platform_get_drvdata(dev);
65 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
66 int clk, ret;
67
68 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
69 ret = clk_prepare_enable(priv->clks[clk]);
70 if (ret)
71 goto err_disable_clks;
72 }
73
74 if (priv->phy) {
75 ret = phy_init(priv->phy);
76 if (ret)
77 goto err_disable_clks;
78
79 ret = phy_power_on(priv->phy);
80 if (ret)
81 goto err_exit_phy;
82 }
83
84 return 0;
85
86err_exit_phy:
87 phy_exit(priv->phy);
88err_disable_clks:
89 while (--clk >= 0)
90 clk_disable_unprepare(priv->clks[clk]);
91
92 return ret;
93}
94
95static void ohci_platform_power_off(struct platform_device *dev)
96{
97 struct usb_hcd *hcd = platform_get_drvdata(dev);
98 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
99 int clk;
100
101 if (priv->phy) {
102 phy_power_off(priv->phy);
103 phy_exit(priv->phy);
104 }
105
106 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
107 if (priv->clks[clk])
108 clk_disable_unprepare(priv->clks[clk]);
109}
110
Manjunath Goudar928fb682013-06-03 20:46:08 +0530111static struct hc_driver __read_mostly ohci_platform_hc_driver;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100112
Manjunath Goudar928fb682013-06-03 20:46:08 +0530113static const struct ohci_driver_overrides platform_overrides __initconst = {
Hans de Goedeca52a172014-02-07 16:36:40 +0100114 .product_desc = "Generic Platform OHCI controller",
115 .reset = ohci_platform_reset,
116 .extra_priv_size = sizeof(struct ohci_platform_priv),
117};
118
119static struct usb_ohci_pdata ohci_platform_defaults = {
120 .power_on = ohci_platform_power_on,
121 .power_suspend = ohci_platform_power_off,
122 .power_off = ohci_platform_power_off,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100123};
124
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500125static int ohci_platform_probe(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100126{
127 struct usb_hcd *hcd;
128 struct resource *res_mem;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900129 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100130 struct ohci_platform_priv *priv;
Hans de Goedeb1034412014-02-07 16:36:42 +0100131 struct ohci_hcd *ohci;
Hans de Goedeca52a172014-02-07 16:36:40 +0100132 int err, irq, clk = 0;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100133
134 if (usb_disabled())
135 return -ENODEV;
136
Hans de Goedeca52a172014-02-07 16:36:40 +0100137 /*
138 * Use reasonable defaults so platforms don't have to provide these
139 * with DT probing on ARM.
140 */
141 if (!pdata)
142 pdata = &ohci_platform_defaults;
143
144 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
145 if (err)
146 return err;
147
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100148 irq = platform_get_irq(dev, 0);
149 if (irq < 0) {
Florian Fainelli976baf62012-10-08 15:11:42 +0200150 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100151 return irq;
152 }
153
154 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
155 if (!res_mem) {
Florian Fainelliac0e3c02012-10-08 15:11:44 +0200156 dev_err(&dev->dev, "no memory resource provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100157 return -ENXIO;
158 }
159
Hans de Goedeca52a172014-02-07 16:36:40 +0100160 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
161 dev_name(&dev->dev));
162 if (!hcd)
163 return -ENOMEM;
164
165 platform_set_drvdata(dev, hcd);
166 dev->dev.platform_data = pdata;
167 priv = hcd_to_ohci_priv(hcd);
Hans de Goedeb1034412014-02-07 16:36:42 +0100168 ohci = hcd_to_ohci(hcd);
Hans de Goedeca52a172014-02-07 16:36:40 +0100169
170 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
Hans de Goedeb1034412014-02-07 16:36:42 +0100171 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
172 ohci->flags |= OHCI_QUIRK_BE_MMIO;
173
174 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
175 ohci->flags |= OHCI_QUIRK_BE_DESC;
176
177 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
178 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
179
180#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
181 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
182 dev_err(&dev->dev,
183 "Error big-endian-regs not compiled in\n");
184 err = -EINVAL;
185 goto err_put_hcd;
186 }
187#endif
188#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
189 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
190 dev_err(&dev->dev,
191 "Error big-endian-desc not compiled in\n");
192 err = -EINVAL;
193 goto err_put_hcd;
194 }
195#endif
Hans de Goedeca52a172014-02-07 16:36:40 +0100196 priv->phy = devm_phy_get(&dev->dev, "usb");
197 if (IS_ERR(priv->phy)) {
198 err = PTR_ERR(priv->phy);
199 if (err == -EPROBE_DEFER)
200 goto err_put_hcd;
201 priv->phy = NULL;
202 }
203
204 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
205 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
206 if (IS_ERR(priv->clks[clk])) {
207 err = PTR_ERR(priv->clks[clk]);
208 if (err == -EPROBE_DEFER)
209 goto err_put_clks;
210 priv->clks[clk] = NULL;
211 break;
212 }
213 }
214 }
215
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700216 if (pdata->power_on) {
217 err = pdata->power_on(dev);
218 if (err < 0)
Hans de Goedeca52a172014-02-07 16:36:40 +0100219 goto err_put_clks;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700220 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100221
222 hcd->rsrc_start = res_mem->start;
223 hcd->rsrc_len = resource_size(res_mem);
224
Thierry Reding148e1132013-01-21 11:09:22 +0100225 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
226 if (IS_ERR(hcd->regs)) {
227 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100228 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200229 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100230 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
231 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100232 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100233
Peter Chen3c9740a2013-11-05 10:46:02 +0800234 device_wakeup_enable(hcd->self.controller);
235
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100236 platform_set_drvdata(dev, hcd);
237
238 return err;
239
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700240err_power:
241 if (pdata->power_off)
242 pdata->power_off(dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100243err_put_clks:
244 while (--clk >= 0)
245 clk_put(priv->clks[clk]);
246err_put_hcd:
247 if (pdata == &ohci_platform_defaults)
248 dev->dev.platform_data = NULL;
249
250 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700251
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100252 return err;
253}
254
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500255static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100256{
257 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900258 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100259 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
260 int clk;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100261
262 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100263
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700264 if (pdata->power_off)
265 pdata->power_off(dev);
266
Hans de Goedeca52a172014-02-07 16:36:40 +0100267 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
268 clk_put(priv->clks[clk]);
269
270 usb_put_hcd(hcd);
271
272 if (pdata == &ohci_platform_defaults)
273 dev->dev.platform_data = NULL;
274
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100275 return 0;
276}
277
278#ifdef CONFIG_PM
279
280static int ohci_platform_suspend(struct device *dev)
281{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530282 struct usb_hcd *hcd = dev_get_drvdata(dev);
283 struct usb_ohci_pdata *pdata = dev->platform_data;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700284 struct platform_device *pdev =
285 container_of(dev, struct platform_device, dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530286 bool do_wakeup = device_may_wakeup(dev);
287 int ret;
288
289 ret = ohci_suspend(hcd, do_wakeup);
290 if (ret)
291 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700292
293 if (pdata->power_suspend)
294 pdata->power_suspend(pdev);
295
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530296 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100297}
298
299static int ohci_platform_resume(struct device *dev)
300{
301 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900302 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700303 struct platform_device *pdev =
304 container_of(dev, struct platform_device, dev);
305
306 if (pdata->power_on) {
307 int err = pdata->power_on(pdev);
308 if (err < 0)
309 return err;
310 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100311
Florian Fainellicfa49b42012-10-08 15:11:29 +0200312 ohci_resume(hcd, false);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100313 return 0;
314}
315
316#else /* !CONFIG_PM */
317#define ohci_platform_suspend NULL
318#define ohci_platform_resume NULL
319#endif /* CONFIG_PM */
320
Hans de Goedeca52a172014-02-07 16:36:40 +0100321static const struct of_device_id ohci_platform_ids[] = {
Hans de Goedece149c32014-02-11 17:35:28 +0100322 { .compatible = "generic-ohci", },
Hans de Goedeca52a172014-02-07 16:36:40 +0100323 { }
324};
325MODULE_DEVICE_TABLE(of, ohci_platform_ids);
326
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100327static const struct platform_device_id ohci_platform_table[] = {
328 { "ohci-platform", 0 },
329 { }
330};
331MODULE_DEVICE_TABLE(platform, ohci_platform_table);
332
333static const struct dev_pm_ops ohci_platform_pm_ops = {
334 .suspend = ohci_platform_suspend,
335 .resume = ohci_platform_resume,
336};
337
338static struct platform_driver ohci_platform_driver = {
339 .id_table = ohci_platform_table,
340 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500341 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100342 .shutdown = usb_hcd_platform_shutdown,
343 .driver = {
344 .owner = THIS_MODULE,
345 .name = "ohci-platform",
346 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100347 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100348 }
349};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530350
351static int __init ohci_platform_init(void)
352{
353 if (usb_disabled())
354 return -ENODEV;
355
356 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
357
358 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
359 return platform_driver_register(&ohci_platform_driver);
360}
361module_init(ohci_platform_init);
362
363static void __exit ohci_platform_cleanup(void)
364{
365 platform_driver_unregister(&ohci_platform_driver);
366}
367module_exit(ohci_platform_cleanup);
368
369MODULE_DESCRIPTION(DRIVER_DESC);
370MODULE_AUTHOR("Hauke Mehrtens");
371MODULE_AUTHOR("Alan Stern");
372MODULE_LICENSE("GPL");