blob: adb691d3087ca14b873ee2978fc3f26cd8383b66 [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
Hans de Goedeca52a172014-02-07 16:36:40 +010046static int ohci_platform_power_on(struct platform_device *dev)
47{
48 struct usb_hcd *hcd = platform_get_drvdata(dev);
49 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
50 int clk, ret;
51
52 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
53 ret = clk_prepare_enable(priv->clks[clk]);
54 if (ret)
55 goto err_disable_clks;
56 }
57
58 if (priv->phy) {
59 ret = phy_init(priv->phy);
60 if (ret)
61 goto err_disable_clks;
62
63 ret = phy_power_on(priv->phy);
64 if (ret)
65 goto err_exit_phy;
66 }
67
68 return 0;
69
70err_exit_phy:
71 phy_exit(priv->phy);
72err_disable_clks:
73 while (--clk >= 0)
74 clk_disable_unprepare(priv->clks[clk]);
75
76 return ret;
77}
78
79static void ohci_platform_power_off(struct platform_device *dev)
80{
81 struct usb_hcd *hcd = platform_get_drvdata(dev);
82 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
83 int clk;
84
85 if (priv->phy) {
86 phy_power_off(priv->phy);
87 phy_exit(priv->phy);
88 }
89
90 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
91 if (priv->clks[clk])
92 clk_disable_unprepare(priv->clks[clk]);
93}
94
Manjunath Goudar928fb682013-06-03 20:46:08 +053095static struct hc_driver __read_mostly ohci_platform_hc_driver;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010096
Manjunath Goudar928fb682013-06-03 20:46:08 +053097static const struct ohci_driver_overrides platform_overrides __initconst = {
Hans de Goedeca52a172014-02-07 16:36:40 +010098 .product_desc = "Generic Platform OHCI controller",
Hans de Goedeca52a172014-02-07 16:36:40 +010099 .extra_priv_size = sizeof(struct ohci_platform_priv),
100};
101
102static struct usb_ohci_pdata ohci_platform_defaults = {
103 .power_on = ohci_platform_power_on,
104 .power_suspend = ohci_platform_power_off,
105 .power_off = ohci_platform_power_off,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100106};
107
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500108static int ohci_platform_probe(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100109{
110 struct usb_hcd *hcd;
111 struct resource *res_mem;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900112 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100113 struct ohci_platform_priv *priv;
Hans de Goedeb1034412014-02-07 16:36:42 +0100114 struct ohci_hcd *ohci;
Hans de Goedeca52a172014-02-07 16:36:40 +0100115 int err, irq, clk = 0;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100116
117 if (usb_disabled())
118 return -ENODEV;
119
Hans de Goedeca52a172014-02-07 16:36:40 +0100120 /*
121 * Use reasonable defaults so platforms don't have to provide these
122 * with DT probing on ARM.
123 */
124 if (!pdata)
125 pdata = &ohci_platform_defaults;
126
127 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
128 if (err)
129 return err;
130
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100131 irq = platform_get_irq(dev, 0);
132 if (irq < 0) {
Florian Fainelli976baf6e2012-10-08 15:11:42 +0200133 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100134 return irq;
135 }
136
137 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
138 if (!res_mem) {
Florian Fainelliac0e3c02012-10-08 15:11:44 +0200139 dev_err(&dev->dev, "no memory resource provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100140 return -ENXIO;
141 }
142
Hans de Goedeca52a172014-02-07 16:36:40 +0100143 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
144 dev_name(&dev->dev));
145 if (!hcd)
146 return -ENOMEM;
147
148 platform_set_drvdata(dev, hcd);
149 dev->dev.platform_data = pdata;
150 priv = hcd_to_ohci_priv(hcd);
Hans de Goedeb1034412014-02-07 16:36:42 +0100151 ohci = hcd_to_ohci(hcd);
Hans de Goedeca52a172014-02-07 16:36:40 +0100152
153 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
Hans de Goedeb1034412014-02-07 16:36:42 +0100154 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
155 ohci->flags |= OHCI_QUIRK_BE_MMIO;
156
157 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
158 ohci->flags |= OHCI_QUIRK_BE_DESC;
159
160 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
161 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
162
Kevin Cernekeec4b86922014-10-11 11:10:48 -0700163 if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
164 ohci->flags |= OHCI_QUIRK_FRAME_NO;
165
166 of_property_read_u32(dev->dev.of_node, "num-ports",
167 &ohci->num_ports);
168
Hans de Goedeca52a172014-02-07 16:36:40 +0100169 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 }
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200187
188 }
189
190 priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
191 if (IS_ERR(priv->rst)) {
192 err = PTR_ERR(priv->rst);
193 if (err == -EPROBE_DEFER)
194 goto err_put_clks;
195 priv->rst = NULL;
196 } else {
197 err = reset_control_deassert(priv->rst);
198 if (err)
199 goto err_put_clks;
Hans de Goedeca52a172014-02-07 16:36:40 +0100200 }
201
Alan Sternadff5292014-02-11 11:26:00 -0500202 if (pdata->big_endian_desc)
203 ohci->flags |= OHCI_QUIRK_BE_DESC;
204 if (pdata->big_endian_mmio)
205 ohci->flags |= OHCI_QUIRK_BE_MMIO;
Kevin Cernekee46f21942014-10-11 11:10:49 -0700206 if (pdata->no_big_frame_no)
207 ohci->flags |= OHCI_QUIRK_FRAME_NO;
208 if (pdata->num_ports)
209 ohci->num_ports = pdata->num_ports;
Alan Sternadff5292014-02-11 11:26:00 -0500210
211#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
212 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
213 dev_err(&dev->dev,
214 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
215 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200216 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500217 }
218#endif
219#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
220 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
221 dev_err(&dev->dev,
222 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
223 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200224 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500225 }
226#endif
227
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700228 if (pdata->power_on) {
229 err = pdata->power_on(dev);
230 if (err < 0)
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200231 goto err_reset;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700232 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100233
234 hcd->rsrc_start = res_mem->start;
235 hcd->rsrc_len = resource_size(res_mem);
236
Thierry Reding148e1132013-01-21 11:09:22 +0100237 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
238 if (IS_ERR(hcd->regs)) {
239 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100240 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200241 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100242 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
243 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100244 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100245
Peter Chen3c9740a2013-11-05 10:46:02 +0800246 device_wakeup_enable(hcd->self.controller);
247
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100248 platform_set_drvdata(dev, hcd);
249
250 return err;
251
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700252err_power:
253 if (pdata->power_off)
254 pdata->power_off(dev);
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200255err_reset:
256 if (priv->rst)
257 reset_control_assert(priv->rst);
Hans de Goedeca52a172014-02-07 16:36:40 +0100258err_put_clks:
259 while (--clk >= 0)
260 clk_put(priv->clks[clk]);
261err_put_hcd:
262 if (pdata == &ohci_platform_defaults)
263 dev->dev.platform_data = NULL;
264
265 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700266
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100267 return err;
268}
269
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500270static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100271{
272 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900273 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100274 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
275 int clk;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100276
277 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100278
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700279 if (pdata->power_off)
280 pdata->power_off(dev);
281
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200282 if (priv->rst)
283 reset_control_assert(priv->rst);
284
Hans de Goedeca52a172014-02-07 16:36:40 +0100285 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
286 clk_put(priv->clks[clk]);
287
288 usb_put_hcd(hcd);
289
290 if (pdata == &ohci_platform_defaults)
291 dev->dev.platform_data = NULL;
292
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100293 return 0;
294}
295
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900296#ifdef CONFIG_PM_SLEEP
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100297static int ohci_platform_suspend(struct device *dev)
298{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530299 struct usb_hcd *hcd = dev_get_drvdata(dev);
300 struct usb_ohci_pdata *pdata = dev->platform_data;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700301 struct platform_device *pdev =
302 container_of(dev, struct platform_device, dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530303 bool do_wakeup = device_may_wakeup(dev);
304 int ret;
305
306 ret = ohci_suspend(hcd, do_wakeup);
307 if (ret)
308 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700309
310 if (pdata->power_suspend)
311 pdata->power_suspend(pdev);
312
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530313 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100314}
315
316static int ohci_platform_resume(struct device *dev)
317{
318 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900319 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700320 struct platform_device *pdev =
321 container_of(dev, struct platform_device, dev);
322
323 if (pdata->power_on) {
324 int err = pdata->power_on(pdev);
325 if (err < 0)
326 return err;
327 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100328
Florian Fainellicfa49b42012-10-08 15:11:29 +0200329 ohci_resume(hcd, false);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100330 return 0;
331}
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900332#endif /* CONFIG_PM_SLEEP */
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100333
Hans de Goedeca52a172014-02-07 16:36:40 +0100334static const struct of_device_id ohci_platform_ids[] = {
Hans de Goedece149c32014-02-11 17:35:28 +0100335 { .compatible = "generic-ohci", },
Hans de Goedeca52a172014-02-07 16:36:40 +0100336 { }
337};
338MODULE_DEVICE_TABLE(of, ohci_platform_ids);
339
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100340static const struct platform_device_id ohci_platform_table[] = {
341 { "ohci-platform", 0 },
342 { }
343};
344MODULE_DEVICE_TABLE(platform, ohci_platform_table);
345
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900346static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
347 ohci_platform_resume);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100348
349static struct platform_driver ohci_platform_driver = {
350 .id_table = ohci_platform_table,
351 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500352 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100353 .shutdown = usb_hcd_platform_shutdown,
354 .driver = {
355 .owner = THIS_MODULE,
356 .name = "ohci-platform",
357 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100358 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100359 }
360};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530361
362static int __init ohci_platform_init(void)
363{
364 if (usb_disabled())
365 return -ENODEV;
366
367 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
368
369 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
370 return platform_driver_register(&ohci_platform_driver);
371}
372module_init(ohci_platform_init);
373
374static void __exit ohci_platform_cleanup(void)
375{
376 platform_driver_unregister(&ohci_platform_driver);
377}
378module_exit(ohci_platform_cleanup);
379
380MODULE_DESCRIPTION(DRIVER_DESC);
381MODULE_AUTHOR("Hauke Mehrtens");
382MODULE_AUTHOR("Alan Stern");
383MODULE_LICENSE("GPL");