blob: ae1c988da146e556f4b7fe386ad26264d0b20d90 [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;
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080041 struct phy **phys;
42 int num_phys;
Hans de Goedeca52a172014-02-07 16:36:40 +010043};
Manjunath Goudar928fb682013-06-03 20:46:08 +053044
45static const char hcd_name[] = "ohci-platform";
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010046
Hans de Goedeca52a172014-02-07 16:36:40 +010047static int ohci_platform_power_on(struct platform_device *dev)
48{
49 struct usb_hcd *hcd = platform_get_drvdata(dev);
50 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080051 int clk, ret, phy_num;
Hans de Goedeca52a172014-02-07 16:36:40 +010052
53 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
54 ret = clk_prepare_enable(priv->clks[clk]);
55 if (ret)
56 goto err_disable_clks;
57 }
58
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080059 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
Arun Ramamurthya666f7d2015-04-22 16:04:13 -070060 ret = phy_init(priv->phys[phy_num]);
61 if (ret)
62 goto err_exit_phy;
63 ret = phy_power_on(priv->phys[phy_num]);
64 if (ret) {
65 phy_exit(priv->phys[phy_num]);
66 goto err_exit_phy;
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080067 }
Hans de Goedeca52a172014-02-07 16:36:40 +010068 }
69
70 return 0;
71
72err_exit_phy:
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080073 while (--phy_num >= 0) {
Arun Ramamurthya666f7d2015-04-22 16:04:13 -070074 phy_power_off(priv->phys[phy_num]);
75 phy_exit(priv->phys[phy_num]);
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080076 }
Hans de Goedeca52a172014-02-07 16:36:40 +010077err_disable_clks:
78 while (--clk >= 0)
79 clk_disable_unprepare(priv->clks[clk]);
80
81 return ret;
82}
83
84static void ohci_platform_power_off(struct platform_device *dev)
85{
86 struct usb_hcd *hcd = platform_get_drvdata(dev);
87 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080088 int clk, phy_num;
Hans de Goedeca52a172014-02-07 16:36:40 +010089
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080090 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
Arun Ramamurthya666f7d2015-04-22 16:04:13 -070091 phy_power_off(priv->phys[phy_num]);
92 phy_exit(priv->phys[phy_num]);
Hans de Goedeca52a172014-02-07 16:36:40 +010093 }
94
95 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
96 if (priv->clks[clk])
97 clk_disable_unprepare(priv->clks[clk]);
98}
99
Manjunath Goudar928fb682013-06-03 20:46:08 +0530100static struct hc_driver __read_mostly ohci_platform_hc_driver;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100101
Manjunath Goudar928fb682013-06-03 20:46:08 +0530102static const struct ohci_driver_overrides platform_overrides __initconst = {
Hans de Goedeca52a172014-02-07 16:36:40 +0100103 .product_desc = "Generic Platform OHCI controller",
Hans de Goedeca52a172014-02-07 16:36:40 +0100104 .extra_priv_size = sizeof(struct ohci_platform_priv),
105};
106
107static struct usb_ohci_pdata ohci_platform_defaults = {
108 .power_on = ohci_platform_power_on,
109 .power_suspend = ohci_platform_power_off,
110 .power_off = ohci_platform_power_off,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100111};
112
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500113static int ohci_platform_probe(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100114{
115 struct usb_hcd *hcd;
116 struct resource *res_mem;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900117 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100118 struct ohci_platform_priv *priv;
Hans de Goedeb1034412014-02-07 16:36:42 +0100119 struct ohci_hcd *ohci;
Arun Ramamurthy85cd6902015-01-19 16:05:30 -0800120 int err, irq, phy_num, clk = 0;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100121
122 if (usb_disabled())
123 return -ENODEV;
124
Hans de Goedeca52a172014-02-07 16:36:40 +0100125 /*
126 * Use reasonable defaults so platforms don't have to provide these
127 * with DT probing on ARM.
128 */
129 if (!pdata)
130 pdata = &ohci_platform_defaults;
131
132 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
133 if (err)
134 return err;
135
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100136 irq = platform_get_irq(dev, 0);
137 if (irq < 0) {
Florian Fainelli976baf62012-10-08 15:11:42 +0200138 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100139 return irq;
140 }
141
Hans de Goedeca52a172014-02-07 16:36:40 +0100142 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
143 dev_name(&dev->dev));
144 if (!hcd)
145 return -ENOMEM;
146
147 platform_set_drvdata(dev, hcd);
148 dev->dev.platform_data = pdata;
149 priv = hcd_to_ohci_priv(hcd);
Hans de Goedeb1034412014-02-07 16:36:42 +0100150 ohci = hcd_to_ohci(hcd);
Hans de Goedeca52a172014-02-07 16:36:40 +0100151
152 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
Hans de Goedeb1034412014-02-07 16:36:42 +0100153 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
154 ohci->flags |= OHCI_QUIRK_BE_MMIO;
155
156 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
157 ohci->flags |= OHCI_QUIRK_BE_DESC;
158
159 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
160 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
161
Kevin Cernekeec4b86922014-10-11 11:10:48 -0700162 if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
163 ohci->flags |= OHCI_QUIRK_FRAME_NO;
164
165 of_property_read_u32(dev->dev.of_node, "num-ports",
166 &ohci->num_ports);
167
Arun Ramamurthy85cd6902015-01-19 16:05:30 -0800168 priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
169 "phys", "#phy-cells");
Arun Ramamurthy85cd6902015-01-19 16:05:30 -0800170
Arun Ramamurthya666f7d2015-04-22 16:04:13 -0700171 if (priv->num_phys > 0) {
172 priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
173 sizeof(struct phy *), GFP_KERNEL);
174 if (!priv->phys)
175 return -ENOMEM;
176 } else
177 priv->num_phys = 0;
Arun Ramamurthy85cd6902015-01-19 16:05:30 -0800178
179 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
Arun Ramamurthya666f7d2015-04-22 16:04:13 -0700180 priv->phys[phy_num] = devm_of_phy_get_by_index(
181 &dev->dev, dev->dev.of_node, phy_num);
182 if (IS_ERR(priv->phys[phy_num])) {
183 err = PTR_ERR(priv->phys[phy_num]);
184 goto err_put_hcd;
185 }
Hans de Goedeca52a172014-02-07 16:36:40 +0100186 }
187
188 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
189 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
190 if (IS_ERR(priv->clks[clk])) {
191 err = PTR_ERR(priv->clks[clk]);
192 if (err == -EPROBE_DEFER)
193 goto err_put_clks;
194 priv->clks[clk] = NULL;
195 break;
196 }
197 }
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200198
199 }
200
201 priv->rst = devm_reset_control_get_optional(&dev->dev, NULL);
202 if (IS_ERR(priv->rst)) {
203 err = PTR_ERR(priv->rst);
204 if (err == -EPROBE_DEFER)
205 goto err_put_clks;
206 priv->rst = NULL;
207 } else {
208 err = reset_control_deassert(priv->rst);
209 if (err)
210 goto err_put_clks;
Hans de Goedeca52a172014-02-07 16:36:40 +0100211 }
212
Alan Sternadff5292014-02-11 11:26:00 -0500213 if (pdata->big_endian_desc)
214 ohci->flags |= OHCI_QUIRK_BE_DESC;
215 if (pdata->big_endian_mmio)
216 ohci->flags |= OHCI_QUIRK_BE_MMIO;
Kevin Cernekee46f21942014-10-11 11:10:49 -0700217 if (pdata->no_big_frame_no)
218 ohci->flags |= OHCI_QUIRK_FRAME_NO;
219 if (pdata->num_ports)
220 ohci->num_ports = pdata->num_ports;
Alan Sternadff5292014-02-11 11:26:00 -0500221
222#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
223 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
224 dev_err(&dev->dev,
225 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
226 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200227 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500228 }
229#endif
230#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
231 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
232 dev_err(&dev->dev,
233 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
234 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200235 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500236 }
237#endif
238
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700239 if (pdata->power_on) {
240 err = pdata->power_on(dev);
241 if (err < 0)
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200242 goto err_reset;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700243 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100244
Varka Bhadram7b519292014-11-04 07:51:16 +0530245 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100246 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
247 if (IS_ERR(hcd->regs)) {
248 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100249 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200250 }
Varka Bhadram7b519292014-11-04 07:51:16 +0530251 hcd->rsrc_start = res_mem->start;
252 hcd->rsrc_len = resource_size(res_mem);
253
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100254 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
255 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100256 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100257
Peter Chen3c9740a2013-11-05 10:46:02 +0800258 device_wakeup_enable(hcd->self.controller);
259
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100260 platform_set_drvdata(dev, hcd);
261
262 return err;
263
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700264err_power:
265 if (pdata->power_off)
266 pdata->power_off(dev);
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200267err_reset:
268 if (priv->rst)
269 reset_control_assert(priv->rst);
Hans de Goedeca52a172014-02-07 16:36:40 +0100270err_put_clks:
271 while (--clk >= 0)
272 clk_put(priv->clks[clk]);
273err_put_hcd:
274 if (pdata == &ohci_platform_defaults)
275 dev->dev.platform_data = NULL;
276
277 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700278
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100279 return err;
280}
281
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500282static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100283{
284 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900285 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100286 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
287 int clk;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100288
289 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100290
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700291 if (pdata->power_off)
292 pdata->power_off(dev);
293
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200294 if (priv->rst)
295 reset_control_assert(priv->rst);
296
Hans de Goedeca52a172014-02-07 16:36:40 +0100297 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
298 clk_put(priv->clks[clk]);
299
300 usb_put_hcd(hcd);
301
302 if (pdata == &ohci_platform_defaults)
303 dev->dev.platform_data = NULL;
304
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100305 return 0;
306}
307
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900308#ifdef CONFIG_PM_SLEEP
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100309static int ohci_platform_suspend(struct device *dev)
310{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530311 struct usb_hcd *hcd = dev_get_drvdata(dev);
312 struct usb_ohci_pdata *pdata = dev->platform_data;
Geliang Tang20db5512015-12-23 21:26:52 +0800313 struct platform_device *pdev = to_platform_device(dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530314 bool do_wakeup = device_may_wakeup(dev);
315 int ret;
316
317 ret = ohci_suspend(hcd, do_wakeup);
318 if (ret)
319 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700320
321 if (pdata->power_suspend)
322 pdata->power_suspend(pdev);
323
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530324 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100325}
326
327static int ohci_platform_resume(struct device *dev)
328{
329 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900330 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Geliang Tang20db5512015-12-23 21:26:52 +0800331 struct platform_device *pdev = to_platform_device(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700332
333 if (pdata->power_on) {
334 int err = pdata->power_on(pdev);
335 if (err < 0)
336 return err;
337 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100338
Florian Fainellicfa49b42012-10-08 15:11:29 +0200339 ohci_resume(hcd, false);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100340 return 0;
341}
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900342#endif /* CONFIG_PM_SLEEP */
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100343
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", },
Andreas Herrmanna95cfa62015-01-06 13:48:56 +0100346 { .compatible = "cavium,octeon-6335-ohci", },
Hans de Goedeca52a172014-02-07 16:36:40 +0100347 { }
348};
349MODULE_DEVICE_TABLE(of, ohci_platform_ids);
350
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100351static const struct platform_device_id ohci_platform_table[] = {
352 { "ohci-platform", 0 },
353 { }
354};
355MODULE_DEVICE_TABLE(platform, ohci_platform_table);
356
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900357static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
358 ohci_platform_resume);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100359
360static struct platform_driver ohci_platform_driver = {
361 .id_table = ohci_platform_table,
362 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500363 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100364 .shutdown = usb_hcd_platform_shutdown,
365 .driver = {
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100366 .name = "ohci-platform",
367 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100368 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100369 }
370};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530371
372static int __init ohci_platform_init(void)
373{
374 if (usb_disabled())
375 return -ENODEV;
376
377 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
378
379 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
380 return platform_driver_register(&ohci_platform_driver);
381}
382module_init(ohci_platform_init);
383
384static void __exit ohci_platform_cleanup(void)
385{
386 platform_driver_unregister(&ohci_platform_driver);
387}
388module_exit(ohci_platform_cleanup);
389
390MODULE_DESCRIPTION(DRIVER_DESC);
391MODULE_AUTHOR("Hauke Mehrtens");
392MODULE_AUTHOR("Alan Stern");
393MODULE_LICENSE("GPL");