blob: 49304ddde4226f7e41d4bd42e53d7a4e976cd5fb [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;
131 int err, irq, clk = 0;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100132
133 if (usb_disabled())
134 return -ENODEV;
135
Hans de Goedeca52a172014-02-07 16:36:40 +0100136 /*
137 * Use reasonable defaults so platforms don't have to provide these
138 * with DT probing on ARM.
139 */
140 if (!pdata)
141 pdata = &ohci_platform_defaults;
142
143 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
144 if (err)
145 return err;
146
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100147 irq = platform_get_irq(dev, 0);
148 if (irq < 0) {
Florian Fainelli976baf6e2012-10-08 15:11:42 +0200149 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100150 return irq;
151 }
152
153 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
154 if (!res_mem) {
Florian Fainelliac0e3c02012-10-08 15:11:44 +0200155 dev_err(&dev->dev, "no memory resource provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100156 return -ENXIO;
157 }
158
Hans de Goedeca52a172014-02-07 16:36:40 +0100159 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
160 dev_name(&dev->dev));
161 if (!hcd)
162 return -ENOMEM;
163
164 platform_set_drvdata(dev, hcd);
165 dev->dev.platform_data = pdata;
166 priv = hcd_to_ohci_priv(hcd);
167
168 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
169 priv->phy = devm_phy_get(&dev->dev, "usb");
170 if (IS_ERR(priv->phy)) {
171 err = PTR_ERR(priv->phy);
172 if (err == -EPROBE_DEFER)
173 goto err_put_hcd;
174 priv->phy = NULL;
175 }
176
177 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
178 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
179 if (IS_ERR(priv->clks[clk])) {
180 err = PTR_ERR(priv->clks[clk]);
181 if (err == -EPROBE_DEFER)
182 goto err_put_clks;
183 priv->clks[clk] = NULL;
184 break;
185 }
186 }
187 }
188
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700189 if (pdata->power_on) {
190 err = pdata->power_on(dev);
191 if (err < 0)
Hans de Goedeca52a172014-02-07 16:36:40 +0100192 goto err_put_clks;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700193 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100194
195 hcd->rsrc_start = res_mem->start;
196 hcd->rsrc_len = resource_size(res_mem);
197
Thierry Reding148e1132013-01-21 11:09:22 +0100198 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
199 if (IS_ERR(hcd->regs)) {
200 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100201 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200202 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100203 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
204 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100205 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100206
Peter Chen3c9740a2013-11-05 10:46:02 +0800207 device_wakeup_enable(hcd->self.controller);
208
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100209 platform_set_drvdata(dev, hcd);
210
211 return err;
212
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700213err_power:
214 if (pdata->power_off)
215 pdata->power_off(dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100216err_put_clks:
217 while (--clk >= 0)
218 clk_put(priv->clks[clk]);
219err_put_hcd:
220 if (pdata == &ohci_platform_defaults)
221 dev->dev.platform_data = NULL;
222
223 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700224
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100225 return err;
226}
227
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500228static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100229{
230 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900231 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100232 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
233 int clk;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100234
235 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100236
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700237 if (pdata->power_off)
238 pdata->power_off(dev);
239
Hans de Goedeca52a172014-02-07 16:36:40 +0100240 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
241 clk_put(priv->clks[clk]);
242
243 usb_put_hcd(hcd);
244
245 if (pdata == &ohci_platform_defaults)
246 dev->dev.platform_data = NULL;
247
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100248 return 0;
249}
250
251#ifdef CONFIG_PM
252
253static int ohci_platform_suspend(struct device *dev)
254{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530255 struct usb_hcd *hcd = dev_get_drvdata(dev);
256 struct usb_ohci_pdata *pdata = dev->platform_data;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700257 struct platform_device *pdev =
258 container_of(dev, struct platform_device, dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530259 bool do_wakeup = device_may_wakeup(dev);
260 int ret;
261
262 ret = ohci_suspend(hcd, do_wakeup);
263 if (ret)
264 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700265
266 if (pdata->power_suspend)
267 pdata->power_suspend(pdev);
268
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530269 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100270}
271
272static int ohci_platform_resume(struct device *dev)
273{
274 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900275 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700276 struct platform_device *pdev =
277 container_of(dev, struct platform_device, dev);
278
279 if (pdata->power_on) {
280 int err = pdata->power_on(pdev);
281 if (err < 0)
282 return err;
283 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100284
Florian Fainellicfa49b42012-10-08 15:11:29 +0200285 ohci_resume(hcd, false);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100286 return 0;
287}
288
289#else /* !CONFIG_PM */
290#define ohci_platform_suspend NULL
291#define ohci_platform_resume NULL
292#endif /* CONFIG_PM */
293
Hans de Goedeca52a172014-02-07 16:36:40 +0100294static const struct of_device_id ohci_platform_ids[] = {
295 { .compatible = "usb-ohci", },
296 { }
297};
298MODULE_DEVICE_TABLE(of, ohci_platform_ids);
299
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100300static const struct platform_device_id ohci_platform_table[] = {
301 { "ohci-platform", 0 },
302 { }
303};
304MODULE_DEVICE_TABLE(platform, ohci_platform_table);
305
306static const struct dev_pm_ops ohci_platform_pm_ops = {
307 .suspend = ohci_platform_suspend,
308 .resume = ohci_platform_resume,
309};
310
311static struct platform_driver ohci_platform_driver = {
312 .id_table = ohci_platform_table,
313 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500314 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100315 .shutdown = usb_hcd_platform_shutdown,
316 .driver = {
317 .owner = THIS_MODULE,
318 .name = "ohci-platform",
319 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100320 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100321 }
322};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530323
324static int __init ohci_platform_init(void)
325{
326 if (usb_disabled())
327 return -ENODEV;
328
329 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
330
331 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
332 return platform_driver_register(&ohci_platform_driver);
333}
334module_init(ohci_platform_init);
335
336static void __exit ohci_platform_cleanup(void)
337{
338 platform_driver_unregister(&ohci_platform_driver);
339}
340module_exit(ohci_platform_cleanup);
341
342MODULE_DESCRIPTION(DRIVER_DESC);
343MODULE_AUTHOR("Hauke Mehrtens");
344MODULE_AUTHOR("Alan Stern");
345MODULE_LICENSE("GPL");