blob: b6002c951c5cbbede27756b449b9b66e4a97e1fc [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
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010050 if (pdata->no_big_frame_no)
51 ohci->flags |= OHCI_QUIRK_FRAME_NO;
Florian Fainelli2b16e392012-10-08 15:11:26 +020052 if (pdata->num_ports)
53 ohci->num_ports = pdata->num_ports;
54
Manjunath Goudar928fb682013-06-03 20:46:08 +053055 return ohci_setup(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010056}
57
Hans de Goedeca52a172014-02-07 16:36:40 +010058static int ohci_platform_power_on(struct platform_device *dev)
59{
60 struct usb_hcd *hcd = platform_get_drvdata(dev);
61 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
62 int clk, ret;
63
64 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
65 ret = clk_prepare_enable(priv->clks[clk]);
66 if (ret)
67 goto err_disable_clks;
68 }
69
70 if (priv->phy) {
71 ret = phy_init(priv->phy);
72 if (ret)
73 goto err_disable_clks;
74
75 ret = phy_power_on(priv->phy);
76 if (ret)
77 goto err_exit_phy;
78 }
79
80 return 0;
81
82err_exit_phy:
83 phy_exit(priv->phy);
84err_disable_clks:
85 while (--clk >= 0)
86 clk_disable_unprepare(priv->clks[clk]);
87
88 return ret;
89}
90
91static void ohci_platform_power_off(struct platform_device *dev)
92{
93 struct usb_hcd *hcd = platform_get_drvdata(dev);
94 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
95 int clk;
96
97 if (priv->phy) {
98 phy_power_off(priv->phy);
99 phy_exit(priv->phy);
100 }
101
102 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
103 if (priv->clks[clk])
104 clk_disable_unprepare(priv->clks[clk]);
105}
106
Manjunath Goudar928fb682013-06-03 20:46:08 +0530107static struct hc_driver __read_mostly ohci_platform_hc_driver;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100108
Manjunath Goudar928fb682013-06-03 20:46:08 +0530109static const struct ohci_driver_overrides platform_overrides __initconst = {
Hans de Goedeca52a172014-02-07 16:36:40 +0100110 .product_desc = "Generic Platform OHCI controller",
111 .reset = ohci_platform_reset,
112 .extra_priv_size = sizeof(struct ohci_platform_priv),
113};
114
115static 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,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100119};
120
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500121static int ohci_platform_probe(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100122{
123 struct usb_hcd *hcd;
124 struct resource *res_mem;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900125 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100126 struct ohci_platform_priv *priv;
Hans de Goedeb1034412014-02-07 16:36:42 +0100127 struct ohci_hcd *ohci;
Hans de Goedeca52a172014-02-07 16:36:40 +0100128 int err, irq, clk = 0;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100129
130 if (usb_disabled())
131 return -ENODEV;
132
Hans de Goedeca52a172014-02-07 16:36:40 +0100133 /*
134 * Use reasonable defaults so platforms don't have to provide these
135 * with DT probing on ARM.
136 */
137 if (!pdata)
138 pdata = &ohci_platform_defaults;
139
140 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
141 if (err)
142 return err;
143
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100144 irq = platform_get_irq(dev, 0);
145 if (irq < 0) {
Florian Fainelli976baf6e2012-10-08 15:11:42 +0200146 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100147 return irq;
148 }
149
150 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
151 if (!res_mem) {
Florian Fainelliac0e3c02012-10-08 15:11:44 +0200152 dev_err(&dev->dev, "no memory resource provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100153 return -ENXIO;
154 }
155
Hans de Goedeca52a172014-02-07 16:36:40 +0100156 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
157 dev_name(&dev->dev));
158 if (!hcd)
159 return -ENOMEM;
160
161 platform_set_drvdata(dev, hcd);
162 dev->dev.platform_data = pdata;
163 priv = hcd_to_ohci_priv(hcd);
Hans de Goedeb1034412014-02-07 16:36:42 +0100164 ohci = hcd_to_ohci(hcd);
Hans de Goedeca52a172014-02-07 16:36:40 +0100165
166 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
Hans de Goedeb1034412014-02-07 16:36:42 +0100167 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
168 ohci->flags |= OHCI_QUIRK_BE_MMIO;
169
170 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
171 ohci->flags |= OHCI_QUIRK_BE_DESC;
172
173 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
174 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
175
Hans de Goedeca52a172014-02-07 16:36:40 +0100176 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)
180 goto err_put_hcd;
181 priv->phy = NULL;
182 }
183
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)
189 goto err_put_clks;
190 priv->clks[clk] = NULL;
191 break;
192 }
193 }
194 }
195
Alan Sternadff5292014-02-11 11:26:00 -0500196 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
201#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
202 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
203 dev_err(&dev->dev,
204 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
205 err = -EINVAL;
206 goto err_put_clks;
207 }
208#endif
209#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
210 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
211 dev_err(&dev->dev,
212 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
213 err = -EINVAL;
214 goto err_put_clks;
215 }
216#endif
217
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700218 if (pdata->power_on) {
219 err = pdata->power_on(dev);
220 if (err < 0)
Hans de Goedeca52a172014-02-07 16:36:40 +0100221 goto err_put_clks;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700222 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100223
224 hcd->rsrc_start = res_mem->start;
225 hcd->rsrc_len = resource_size(res_mem);
226
Thierry Reding148e1132013-01-21 11:09:22 +0100227 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
228 if (IS_ERR(hcd->regs)) {
229 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100230 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200231 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100232 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
233 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100234 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100235
Peter Chen3c9740a2013-11-05 10:46:02 +0800236 device_wakeup_enable(hcd->self.controller);
237
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100238 platform_set_drvdata(dev, hcd);
239
240 return err;
241
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700242err_power:
243 if (pdata->power_off)
244 pdata->power_off(dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100245err_put_clks:
246 while (--clk >= 0)
247 clk_put(priv->clks[clk]);
248err_put_hcd:
249 if (pdata == &ohci_platform_defaults)
250 dev->dev.platform_data = NULL;
251
252 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700253
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100254 return err;
255}
256
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500257static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100258{
259 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900260 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100261 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
262 int clk;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100263
264 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100265
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700266 if (pdata->power_off)
267 pdata->power_off(dev);
268
Hans de Goedeca52a172014-02-07 16:36:40 +0100269 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
270 clk_put(priv->clks[clk]);
271
272 usb_put_hcd(hcd);
273
274 if (pdata == &ohci_platform_defaults)
275 dev->dev.platform_data = NULL;
276
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100277 return 0;
278}
279
280#ifdef CONFIG_PM
281
282static int ohci_platform_suspend(struct device *dev)
283{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530284 struct usb_hcd *hcd = dev_get_drvdata(dev);
285 struct usb_ohci_pdata *pdata = dev->platform_data;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700286 struct platform_device *pdev =
287 container_of(dev, struct platform_device, dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530288 bool do_wakeup = device_may_wakeup(dev);
289 int ret;
290
291 ret = ohci_suspend(hcd, do_wakeup);
292 if (ret)
293 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700294
295 if (pdata->power_suspend)
296 pdata->power_suspend(pdev);
297
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530298 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100299}
300
301static int ohci_platform_resume(struct device *dev)
302{
303 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900304 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700305 struct platform_device *pdev =
306 container_of(dev, struct platform_device, dev);
307
308 if (pdata->power_on) {
309 int err = pdata->power_on(pdev);
310 if (err < 0)
311 return err;
312 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100313
Florian Fainellicfa49b42012-10-08 15:11:29 +0200314 ohci_resume(hcd, false);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100315 return 0;
316}
317
318#else /* !CONFIG_PM */
319#define ohci_platform_suspend NULL
320#define ohci_platform_resume NULL
321#endif /* CONFIG_PM */
322
Hans de Goedeca52a172014-02-07 16:36:40 +0100323static const struct of_device_id ohci_platform_ids[] = {
Hans de Goedece149c32014-02-11 17:35:28 +0100324 { .compatible = "generic-ohci", },
Hans de Goedeca52a172014-02-07 16:36:40 +0100325 { }
326};
327MODULE_DEVICE_TABLE(of, ohci_platform_ids);
328
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100329static const struct platform_device_id ohci_platform_table[] = {
330 { "ohci-platform", 0 },
331 { }
332};
333MODULE_DEVICE_TABLE(platform, ohci_platform_table);
334
335static const struct dev_pm_ops ohci_platform_pm_ops = {
336 .suspend = ohci_platform_suspend,
337 .resume = ohci_platform_resume,
338};
339
340static struct platform_driver ohci_platform_driver = {
341 .id_table = ohci_platform_table,
342 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500343 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100344 .shutdown = usb_hcd_platform_shutdown,
345 .driver = {
346 .owner = THIS_MODULE,
347 .name = "ohci-platform",
348 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100349 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100350 }
351};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530352
353static int __init ohci_platform_init(void)
354{
355 if (usb_disabled())
356 return -ENODEV;
357
358 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
359
360 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
361 return platform_driver_register(&ohci_platform_driver);
362}
363module_init(ohci_platform_init);
364
365static void __exit ohci_platform_cleanup(void)
366{
367 platform_driver_unregister(&ohci_platform_driver);
368}
369module_exit(ohci_platform_cleanup);
370
371MODULE_DESCRIPTION(DRIVER_DESC);
372MODULE_AUTHOR("Hauke Mehrtens");
373MODULE_AUTHOR("Alan Stern");
374MODULE_LICENSE("GPL");