blob: b81d202b15a25da84e8422c37ebc40ceb5ea0032 [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
Hans de Goedeca52a172014-02-07 16:36:40 +0100137 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
138 dev_name(&dev->dev));
139 if (!hcd)
140 return -ENOMEM;
141
142 platform_set_drvdata(dev, hcd);
143 dev->dev.platform_data = pdata;
144 priv = hcd_to_ohci_priv(hcd);
Hans de Goedeb1034412014-02-07 16:36:42 +0100145 ohci = hcd_to_ohci(hcd);
Hans de Goedeca52a172014-02-07 16:36:40 +0100146
147 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
Hans de Goedeb1034412014-02-07 16:36:42 +0100148 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
149 ohci->flags |= OHCI_QUIRK_BE_MMIO;
150
151 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
152 ohci->flags |= OHCI_QUIRK_BE_DESC;
153
154 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
155 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
156
Kevin Cernekeec4b86922014-10-11 11:10:48 -0700157 if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
158 ohci->flags |= OHCI_QUIRK_FRAME_NO;
159
160 of_property_read_u32(dev->dev.of_node, "num-ports",
161 &ohci->num_ports);
162
Hans de Goedeca52a172014-02-07 16:36:40 +0100163 priv->phy = devm_phy_get(&dev->dev, "usb");
164 if (IS_ERR(priv->phy)) {
165 err = PTR_ERR(priv->phy);
166 if (err == -EPROBE_DEFER)
167 goto err_put_hcd;
168 priv->phy = NULL;
169 }
170
171 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
172 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
173 if (IS_ERR(priv->clks[clk])) {
174 err = PTR_ERR(priv->clks[clk]);
175 if (err == -EPROBE_DEFER)
176 goto err_put_clks;
177 priv->clks[clk] = NULL;
178 break;
179 }
180 }
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200181
182 }
183
184 priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
185 if (IS_ERR(priv->rst)) {
186 err = PTR_ERR(priv->rst);
187 if (err == -EPROBE_DEFER)
188 goto err_put_clks;
189 priv->rst = NULL;
190 } else {
191 err = reset_control_deassert(priv->rst);
192 if (err)
193 goto err_put_clks;
Hans de Goedeca52a172014-02-07 16:36:40 +0100194 }
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;
Kevin Cernekee46f21942014-10-11 11:10:49 -0700200 if (pdata->no_big_frame_no)
201 ohci->flags |= OHCI_QUIRK_FRAME_NO;
202 if (pdata->num_ports)
203 ohci->num_ports = pdata->num_ports;
Alan Sternadff5292014-02-11 11:26:00 -0500204
205#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
206 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
207 dev_err(&dev->dev,
208 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
209 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200210 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500211 }
212#endif
213#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
214 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
215 dev_err(&dev->dev,
216 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
217 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200218 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500219 }
220#endif
221
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700222 if (pdata->power_on) {
223 err = pdata->power_on(dev);
224 if (err < 0)
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200225 goto err_reset;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700226 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100227
Varka Bhadram7b519292014-11-04 07:51:16 +0530228 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100229 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
230 if (IS_ERR(hcd->regs)) {
231 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100232 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200233 }
Varka Bhadram7b519292014-11-04 07:51:16 +0530234 hcd->rsrc_start = res_mem->start;
235 hcd->rsrc_len = resource_size(res_mem);
236
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100237 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
238 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100239 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100240
Peter Chen3c9740a2013-11-05 10:46:02 +0800241 device_wakeup_enable(hcd->self.controller);
242
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100243 platform_set_drvdata(dev, hcd);
244
245 return err;
246
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700247err_power:
248 if (pdata->power_off)
249 pdata->power_off(dev);
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200250err_reset:
251 if (priv->rst)
252 reset_control_assert(priv->rst);
Hans de Goedeca52a172014-02-07 16:36:40 +0100253err_put_clks:
254 while (--clk >= 0)
255 clk_put(priv->clks[clk]);
256err_put_hcd:
257 if (pdata == &ohci_platform_defaults)
258 dev->dev.platform_data = NULL;
259
260 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700261
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100262 return err;
263}
264
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500265static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100266{
267 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900268 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100269 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
270 int clk;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100271
272 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100273
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700274 if (pdata->power_off)
275 pdata->power_off(dev);
276
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200277 if (priv->rst)
278 reset_control_assert(priv->rst);
279
Hans de Goedeca52a172014-02-07 16:36:40 +0100280 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
281 clk_put(priv->clks[clk]);
282
283 usb_put_hcd(hcd);
284
285 if (pdata == &ohci_platform_defaults)
286 dev->dev.platform_data = NULL;
287
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100288 return 0;
289}
290
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900291#ifdef CONFIG_PM_SLEEP
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100292static int ohci_platform_suspend(struct device *dev)
293{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530294 struct usb_hcd *hcd = dev_get_drvdata(dev);
295 struct usb_ohci_pdata *pdata = dev->platform_data;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700296 struct platform_device *pdev =
297 container_of(dev, struct platform_device, dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530298 bool do_wakeup = device_may_wakeup(dev);
299 int ret;
300
301 ret = ohci_suspend(hcd, do_wakeup);
302 if (ret)
303 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700304
305 if (pdata->power_suspend)
306 pdata->power_suspend(pdev);
307
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530308 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100309}
310
311static int ohci_platform_resume(struct device *dev)
312{
313 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900314 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700315 struct platform_device *pdev =
316 container_of(dev, struct platform_device, dev);
317
318 if (pdata->power_on) {
319 int err = pdata->power_on(pdev);
320 if (err < 0)
321 return err;
322 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100323
Florian Fainellicfa49b42012-10-08 15:11:29 +0200324 ohci_resume(hcd, false);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100325 return 0;
326}
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900327#endif /* CONFIG_PM_SLEEP */
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100328
Hans de Goedeca52a172014-02-07 16:36:40 +0100329static const struct of_device_id ohci_platform_ids[] = {
Hans de Goedece149c32014-02-11 17:35:28 +0100330 { .compatible = "generic-ohci", },
Hans de Goedeca52a172014-02-07 16:36:40 +0100331 { }
332};
333MODULE_DEVICE_TABLE(of, ohci_platform_ids);
334
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100335static const struct platform_device_id ohci_platform_table[] = {
336 { "ohci-platform", 0 },
337 { }
338};
339MODULE_DEVICE_TABLE(platform, ohci_platform_table);
340
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900341static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
342 ohci_platform_resume);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100343
344static struct platform_driver ohci_platform_driver = {
345 .id_table = ohci_platform_table,
346 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500347 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100348 .shutdown = usb_hcd_platform_shutdown,
349 .driver = {
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100350 .name = "ohci-platform",
351 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100352 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100353 }
354};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530355
356static int __init ohci_platform_init(void)
357{
358 if (usb_disabled())
359 return -ENODEV;
360
361 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
362
363 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
364 return platform_driver_register(&ohci_platform_driver);
365}
366module_init(ohci_platform_init);
367
368static void __exit ohci_platform_cleanup(void)
369{
370 platform_driver_unregister(&ohci_platform_driver);
371}
372module_exit(ohci_platform_cleanup);
373
374MODULE_DESCRIPTION(DRIVER_DESC);
375MODULE_AUTHOR("Hauke Mehrtens");
376MODULE_AUTHOR("Alan Stern");
377MODULE_LICENSE("GPL");