blob: 4369299064c775836aea25fdb87c3d4ac249d843 [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>
Maxime Ripard4615f3b2014-05-13 17:44:20 +020027#include <linux/reset.h>
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010028#include <linux/usb/ohci_pdriver.h>
Manjunath Goudar928fb682013-06-03 20:46:08 +053029#include <linux/usb.h>
30#include <linux/usb/hcd.h>
31
32#include "ohci.h"
33
34#define DRIVER_DESC "OHCI generic platform driver"
Hans de Goedeca52a172014-02-07 16:36:40 +010035#define OHCI_MAX_CLKS 3
36#define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
37
38struct ohci_platform_priv {
39 struct clk *clks[OHCI_MAX_CLKS];
Maxime Ripard4615f3b2014-05-13 17:44:20 +020040 struct reset_control *rst;
Hans de Goedeca52a172014-02-07 16:36:40 +010041 struct phy *phy;
42};
Manjunath Goudar928fb682013-06-03 20:46:08 +053043
44static const char hcd_name[] = "ohci-platform";
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010045
46static int ohci_platform_reset(struct usb_hcd *hcd)
47{
48 struct platform_device *pdev = to_platform_device(hcd->self.controller);
Jingoo Hand4f09e22013-07-30 19:59:40 +090049 struct usb_ohci_pdata *pdata = dev_get_platdata(&pdev->dev);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010050 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010051
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010052 if (pdata->no_big_frame_no)
53 ohci->flags |= OHCI_QUIRK_FRAME_NO;
Florian Fainelli2b16e392012-10-08 15:11:26 +020054 if (pdata->num_ports)
55 ohci->num_ports = pdata->num_ports;
56
Manjunath Goudar928fb682013-06-03 20:46:08 +053057 return ohci_setup(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010058}
59
Hans de Goedeca52a172014-02-07 16:36:40 +010060static int ohci_platform_power_on(struct platform_device *dev)
61{
62 struct usb_hcd *hcd = platform_get_drvdata(dev);
63 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
64 int clk, ret;
65
66 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
67 ret = clk_prepare_enable(priv->clks[clk]);
68 if (ret)
69 goto err_disable_clks;
70 }
71
72 if (priv->phy) {
73 ret = phy_init(priv->phy);
74 if (ret)
75 goto err_disable_clks;
76
77 ret = phy_power_on(priv->phy);
78 if (ret)
79 goto err_exit_phy;
80 }
81
82 return 0;
83
84err_exit_phy:
85 phy_exit(priv->phy);
86err_disable_clks:
87 while (--clk >= 0)
88 clk_disable_unprepare(priv->clks[clk]);
89
90 return ret;
91}
92
93static void ohci_platform_power_off(struct platform_device *dev)
94{
95 struct usb_hcd *hcd = platform_get_drvdata(dev);
96 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
97 int clk;
98
99 if (priv->phy) {
100 phy_power_off(priv->phy);
101 phy_exit(priv->phy);
102 }
103
104 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
105 if (priv->clks[clk])
106 clk_disable_unprepare(priv->clks[clk]);
107}
108
Manjunath Goudar928fb682013-06-03 20:46:08 +0530109static struct hc_driver __read_mostly ohci_platform_hc_driver;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100110
Manjunath Goudar928fb682013-06-03 20:46:08 +0530111static const struct ohci_driver_overrides platform_overrides __initconst = {
Hans de Goedeca52a172014-02-07 16:36:40 +0100112 .product_desc = "Generic Platform OHCI controller",
113 .reset = ohci_platform_reset,
114 .extra_priv_size = sizeof(struct ohci_platform_priv),
115};
116
117static struct usb_ohci_pdata ohci_platform_defaults = {
118 .power_on = ohci_platform_power_on,
119 .power_suspend = ohci_platform_power_off,
120 .power_off = ohci_platform_power_off,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100121};
122
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500123static int ohci_platform_probe(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100124{
125 struct usb_hcd *hcd;
126 struct resource *res_mem;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900127 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100128 struct ohci_platform_priv *priv;
Hans de Goedeb1034412014-02-07 16:36:42 +0100129 struct ohci_hcd *ohci;
Hans de Goedeca52a172014-02-07 16:36:40 +0100130 int err, irq, clk = 0;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100131
132 if (usb_disabled())
133 return -ENODEV;
134
Hans de Goedeca52a172014-02-07 16:36:40 +0100135 /*
136 * Use reasonable defaults so platforms don't have to provide these
137 * with DT probing on ARM.
138 */
139 if (!pdata)
140 pdata = &ohci_platform_defaults;
141
142 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
143 if (err)
144 return err;
145
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100146 irq = platform_get_irq(dev, 0);
147 if (irq < 0) {
Florian Fainelli976baf6e2012-10-08 15:11:42 +0200148 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100149 return irq;
150 }
151
152 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
153 if (!res_mem) {
Florian Fainelliac0e3c02012-10-08 15:11:44 +0200154 dev_err(&dev->dev, "no memory resource provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100155 return -ENXIO;
156 }
157
Hans de Goedeca52a172014-02-07 16:36:40 +0100158 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
159 dev_name(&dev->dev));
160 if (!hcd)
161 return -ENOMEM;
162
163 platform_set_drvdata(dev, hcd);
164 dev->dev.platform_data = pdata;
165 priv = hcd_to_ohci_priv(hcd);
Hans de Goedeb1034412014-02-07 16:36:42 +0100166 ohci = hcd_to_ohci(hcd);
Hans de Goedeca52a172014-02-07 16:36:40 +0100167
168 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
Hans de Goedeb1034412014-02-07 16:36:42 +0100169 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
170 ohci->flags |= OHCI_QUIRK_BE_MMIO;
171
172 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
173 ohci->flags |= OHCI_QUIRK_BE_DESC;
174
175 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
176 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
177
Hans de Goedeca52a172014-02-07 16:36:40 +0100178 priv->phy = devm_phy_get(&dev->dev, "usb");
179 if (IS_ERR(priv->phy)) {
180 err = PTR_ERR(priv->phy);
181 if (err == -EPROBE_DEFER)
182 goto err_put_hcd;
183 priv->phy = NULL;
184 }
185
186 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
187 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
188 if (IS_ERR(priv->clks[clk])) {
189 err = PTR_ERR(priv->clks[clk]);
190 if (err == -EPROBE_DEFER)
191 goto err_put_clks;
192 priv->clks[clk] = NULL;
193 break;
194 }
195 }
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200196
197 }
198
199 priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
200 if (IS_ERR(priv->rst)) {
201 err = PTR_ERR(priv->rst);
202 if (err == -EPROBE_DEFER)
203 goto err_put_clks;
204 priv->rst = NULL;
205 } else {
206 err = reset_control_deassert(priv->rst);
207 if (err)
208 goto err_put_clks;
Hans de Goedeca52a172014-02-07 16:36:40 +0100209 }
210
Alan Sternadff5292014-02-11 11:26:00 -0500211 if (pdata->big_endian_desc)
212 ohci->flags |= OHCI_QUIRK_BE_DESC;
213 if (pdata->big_endian_mmio)
214 ohci->flags |= OHCI_QUIRK_BE_MMIO;
215
216#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
217 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
218 dev_err(&dev->dev,
219 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
220 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200221 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500222 }
223#endif
224#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
225 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
226 dev_err(&dev->dev,
227 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
228 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200229 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500230 }
231#endif
232
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700233 if (pdata->power_on) {
234 err = pdata->power_on(dev);
235 if (err < 0)
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200236 goto err_reset;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700237 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100238
239 hcd->rsrc_start = res_mem->start;
240 hcd->rsrc_len = resource_size(res_mem);
241
Thierry Reding148e1132013-01-21 11:09:22 +0100242 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
243 if (IS_ERR(hcd->regs)) {
244 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100245 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200246 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100247 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
248 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100249 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100250
Peter Chen3c9740a2013-11-05 10:46:02 +0800251 device_wakeup_enable(hcd->self.controller);
252
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100253 platform_set_drvdata(dev, hcd);
254
255 return err;
256
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700257err_power:
258 if (pdata->power_off)
259 pdata->power_off(dev);
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200260err_reset:
261 if (priv->rst)
262 reset_control_assert(priv->rst);
Hans de Goedeca52a172014-02-07 16:36:40 +0100263err_put_clks:
264 while (--clk >= 0)
265 clk_put(priv->clks[clk]);
266err_put_hcd:
267 if (pdata == &ohci_platform_defaults)
268 dev->dev.platform_data = NULL;
269
270 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700271
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100272 return err;
273}
274
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500275static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100276{
277 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900278 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100279 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
280 int clk;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100281
282 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100283
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700284 if (pdata->power_off)
285 pdata->power_off(dev);
286
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200287 if (priv->rst)
288 reset_control_assert(priv->rst);
289
Hans de Goedeca52a172014-02-07 16:36:40 +0100290 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
291 clk_put(priv->clks[clk]);
292
293 usb_put_hcd(hcd);
294
295 if (pdata == &ohci_platform_defaults)
296 dev->dev.platform_data = NULL;
297
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100298 return 0;
299}
300
301#ifdef CONFIG_PM
302
303static int ohci_platform_suspend(struct device *dev)
304{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530305 struct usb_hcd *hcd = dev_get_drvdata(dev);
306 struct usb_ohci_pdata *pdata = dev->platform_data;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700307 struct platform_device *pdev =
308 container_of(dev, struct platform_device, dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530309 bool do_wakeup = device_may_wakeup(dev);
310 int ret;
311
312 ret = ohci_suspend(hcd, do_wakeup);
313 if (ret)
314 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700315
316 if (pdata->power_suspend)
317 pdata->power_suspend(pdev);
318
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530319 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100320}
321
322static int ohci_platform_resume(struct device *dev)
323{
324 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900325 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700326 struct platform_device *pdev =
327 container_of(dev, struct platform_device, dev);
328
329 if (pdata->power_on) {
330 int err = pdata->power_on(pdev);
331 if (err < 0)
332 return err;
333 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100334
Florian Fainellicfa49b42012-10-08 15:11:29 +0200335 ohci_resume(hcd, false);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100336 return 0;
337}
338
339#else /* !CONFIG_PM */
340#define ohci_platform_suspend NULL
341#define ohci_platform_resume NULL
342#endif /* CONFIG_PM */
343
Hans de Goedeca52a172014-02-07 16:36:40 +0100344static const struct of_device_id ohci_platform_ids[] = {
Hans de Goedece149c32014-02-11 17:35:28 +0100345 { .compatible = "generic-ohci", },
Hans de Goedeca52a172014-02-07 16:36:40 +0100346 { }
347};
348MODULE_DEVICE_TABLE(of, ohci_platform_ids);
349
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100350static const struct platform_device_id ohci_platform_table[] = {
351 { "ohci-platform", 0 },
352 { }
353};
354MODULE_DEVICE_TABLE(platform, ohci_platform_table);
355
356static const struct dev_pm_ops ohci_platform_pm_ops = {
357 .suspend = ohci_platform_suspend,
358 .resume = ohci_platform_resume,
359};
360
361static struct platform_driver ohci_platform_driver = {
362 .id_table = ohci_platform_table,
363 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500364 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100365 .shutdown = usb_hcd_platform_shutdown,
366 .driver = {
367 .owner = THIS_MODULE,
368 .name = "ohci-platform",
369 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100370 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100371 }
372};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530373
374static int __init ohci_platform_init(void)
375{
376 if (usb_disabled())
377 return -ENODEV;
378
379 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
380
381 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
382 return platform_driver_register(&ohci_platform_driver);
383}
384module_init(ohci_platform_init);
385
386static void __exit ohci_platform_cleanup(void)
387{
388 platform_driver_unregister(&ohci_platform_driver);
389}
390module_exit(ohci_platform_cleanup);
391
392MODULE_DESCRIPTION(DRIVER_DESC);
393MODULE_AUTHOR("Hauke Mehrtens");
394MODULE_AUTHOR("Alan Stern");
395MODULE_LICENSE("GPL");