blob: 6368fce43197c9bdfe80d5fd5718eedbea50e9fb [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
Hans de Goede62d96942016-06-02 17:14:06 +020036#define OHCI_MAX_RESETS 2
Hans de Goedeca52a172014-02-07 16:36:40 +010037#define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
38
39struct ohci_platform_priv {
40 struct clk *clks[OHCI_MAX_CLKS];
Hans de Goede62d96942016-06-02 17:14:06 +020041 struct reset_control *resets[OHCI_MAX_RESETS];
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080042 struct phy **phys;
43 int num_phys;
Hans de Goedeca52a172014-02-07 16:36:40 +010044};
Manjunath Goudar928fb682013-06-03 20:46:08 +053045
46static const char hcd_name[] = "ohci-platform";
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010047
Hans de Goedeca52a172014-02-07 16:36:40 +010048static int ohci_platform_power_on(struct platform_device *dev)
49{
50 struct usb_hcd *hcd = platform_get_drvdata(dev);
51 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080052 int clk, ret, phy_num;
Hans de Goedeca52a172014-02-07 16:36:40 +010053
54 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
55 ret = clk_prepare_enable(priv->clks[clk]);
56 if (ret)
57 goto err_disable_clks;
58 }
59
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080060 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
Arun Ramamurthya666f7d2015-04-22 16:04:13 -070061 ret = phy_init(priv->phys[phy_num]);
62 if (ret)
63 goto err_exit_phy;
64 ret = phy_power_on(priv->phys[phy_num]);
65 if (ret) {
66 phy_exit(priv->phys[phy_num]);
67 goto err_exit_phy;
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080068 }
Hans de Goedeca52a172014-02-07 16:36:40 +010069 }
70
71 return 0;
72
73err_exit_phy:
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080074 while (--phy_num >= 0) {
Arun Ramamurthya666f7d2015-04-22 16:04:13 -070075 phy_power_off(priv->phys[phy_num]);
76 phy_exit(priv->phys[phy_num]);
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080077 }
Hans de Goedeca52a172014-02-07 16:36:40 +010078err_disable_clks:
79 while (--clk >= 0)
80 clk_disable_unprepare(priv->clks[clk]);
81
82 return ret;
83}
84
85static void ohci_platform_power_off(struct platform_device *dev)
86{
87 struct usb_hcd *hcd = platform_get_drvdata(dev);
88 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080089 int clk, phy_num;
Hans de Goedeca52a172014-02-07 16:36:40 +010090
Arun Ramamurthy85cd6902015-01-19 16:05:30 -080091 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
Arun Ramamurthya666f7d2015-04-22 16:04:13 -070092 phy_power_off(priv->phys[phy_num]);
93 phy_exit(priv->phys[phy_num]);
Hans de Goedeca52a172014-02-07 16:36:40 +010094 }
95
96 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
97 if (priv->clks[clk])
98 clk_disable_unprepare(priv->clks[clk]);
99}
100
Manjunath Goudar928fb682013-06-03 20:46:08 +0530101static struct hc_driver __read_mostly ohci_platform_hc_driver;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100102
Manjunath Goudar928fb682013-06-03 20:46:08 +0530103static const struct ohci_driver_overrides platform_overrides __initconst = {
Hans de Goedeca52a172014-02-07 16:36:40 +0100104 .product_desc = "Generic Platform OHCI controller",
Hans de Goedeca52a172014-02-07 16:36:40 +0100105 .extra_priv_size = sizeof(struct ohci_platform_priv),
106};
107
108static struct usb_ohci_pdata ohci_platform_defaults = {
109 .power_on = ohci_platform_power_on,
110 .power_suspend = ohci_platform_power_off,
111 .power_off = ohci_platform_power_off,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100112};
113
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500114static int ohci_platform_probe(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100115{
116 struct usb_hcd *hcd;
117 struct resource *res_mem;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900118 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100119 struct ohci_platform_priv *priv;
Hans de Goedeb1034412014-02-07 16:36:42 +0100120 struct ohci_hcd *ohci;
Hans de Goede62d96942016-06-02 17:14:06 +0200121 int err, irq, phy_num, clk = 0, rst = 0;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100122
123 if (usb_disabled())
124 return -ENODEV;
125
Hans de Goedeca52a172014-02-07 16:36:40 +0100126 /*
127 * Use reasonable defaults so platforms don't have to provide these
128 * with DT probing on ARM.
129 */
130 if (!pdata)
131 pdata = &ohci_platform_defaults;
132
133 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
134 if (err)
135 return err;
136
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100137 irq = platform_get_irq(dev, 0);
138 if (irq < 0) {
Florian Fainelli976baf62012-10-08 15:11:42 +0200139 dev_err(&dev->dev, "no irq provided");
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100140 return irq;
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
Arun Ramamurthy85cd6902015-01-19 16:05:30 -0800169 priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
170 "phys", "#phy-cells");
Arun Ramamurthy85cd6902015-01-19 16:05:30 -0800171
Arun Ramamurthya666f7d2015-04-22 16:04:13 -0700172 if (priv->num_phys > 0) {
173 priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
174 sizeof(struct phy *), GFP_KERNEL);
175 if (!priv->phys)
176 return -ENOMEM;
177 } else
178 priv->num_phys = 0;
Arun Ramamurthy85cd6902015-01-19 16:05:30 -0800179
180 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
Arun Ramamurthya666f7d2015-04-22 16:04:13 -0700181 priv->phys[phy_num] = devm_of_phy_get_by_index(
182 &dev->dev, dev->dev.of_node, phy_num);
183 if (IS_ERR(priv->phys[phy_num])) {
184 err = PTR_ERR(priv->phys[phy_num]);
185 goto err_put_hcd;
Yoshihiro Shimodad3d6ef12017-03-13 15:25:24 +0900186 } else if (!hcd->phy) {
187 /* Avoiding phy_get() in usb_add_hcd() */
188 hcd->phy = priv->phys[phy_num];
Arun Ramamurthya666f7d2015-04-22 16:04:13 -0700189 }
Hans de Goedeca52a172014-02-07 16:36:40 +0100190 }
191
192 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
193 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
194 if (IS_ERR(priv->clks[clk])) {
195 err = PTR_ERR(priv->clks[clk]);
196 if (err == -EPROBE_DEFER)
197 goto err_put_clks;
198 priv->clks[clk] = NULL;
199 break;
200 }
201 }
Hans de Goede62d96942016-06-02 17:14:06 +0200202 for (rst = 0; rst < OHCI_MAX_RESETS; rst++) {
203 priv->resets[rst] =
204 devm_reset_control_get_shared_by_index(
205 &dev->dev, rst);
206 if (IS_ERR(priv->resets[rst])) {
207 err = PTR_ERR(priv->resets[rst]);
208 if (err == -EPROBE_DEFER)
209 goto err_reset;
210 priv->resets[rst] = NULL;
211 break;
212 }
213 err = reset_control_deassert(priv->resets[rst]);
214 if (err)
215 goto err_reset;
216 }
Hans de Goedeca52a172014-02-07 16:36:40 +0100217 }
218
Alan Sternadff5292014-02-11 11:26:00 -0500219 if (pdata->big_endian_desc)
220 ohci->flags |= OHCI_QUIRK_BE_DESC;
221 if (pdata->big_endian_mmio)
222 ohci->flags |= OHCI_QUIRK_BE_MMIO;
Kevin Cernekee46f21942014-10-11 11:10:49 -0700223 if (pdata->no_big_frame_no)
224 ohci->flags |= OHCI_QUIRK_FRAME_NO;
225 if (pdata->num_ports)
226 ohci->num_ports = pdata->num_ports;
Alan Sternadff5292014-02-11 11:26:00 -0500227
228#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
229 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
230 dev_err(&dev->dev,
231 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
232 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200233 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500234 }
235#endif
236#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
237 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
238 dev_err(&dev->dev,
239 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
240 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200241 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500242 }
243#endif
244
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700245 if (pdata->power_on) {
246 err = pdata->power_on(dev);
247 if (err < 0)
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200248 goto err_reset;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700249 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100250
Varka Bhadram7b519292014-11-04 07:51:16 +0530251 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100252 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
253 if (IS_ERR(hcd->regs)) {
254 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100255 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200256 }
Varka Bhadram7b519292014-11-04 07:51:16 +0530257 hcd->rsrc_start = res_mem->start;
258 hcd->rsrc_len = resource_size(res_mem);
259
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100260 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
261 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100262 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100263
Peter Chen3c9740a2013-11-05 10:46:02 +0800264 device_wakeup_enable(hcd->self.controller);
265
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100266 platform_set_drvdata(dev, hcd);
267
268 return err;
269
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700270err_power:
271 if (pdata->power_off)
272 pdata->power_off(dev);
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200273err_reset:
Hans de Goede62d96942016-06-02 17:14:06 +0200274 while (--rst >= 0)
275 reset_control_assert(priv->resets[rst]);
Hans de Goedeca52a172014-02-07 16:36:40 +0100276err_put_clks:
277 while (--clk >= 0)
278 clk_put(priv->clks[clk]);
279err_put_hcd:
280 if (pdata == &ohci_platform_defaults)
281 dev->dev.platform_data = NULL;
282
283 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700284
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100285 return err;
286}
287
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500288static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100289{
290 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900291 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100292 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
Hans de Goede62d96942016-06-02 17:14:06 +0200293 int clk, rst;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100294
295 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100296
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700297 if (pdata->power_off)
298 pdata->power_off(dev);
299
Hans de Goede62d96942016-06-02 17:14:06 +0200300 for (rst = 0; rst < OHCI_MAX_RESETS && priv->resets[rst]; rst++)
301 reset_control_assert(priv->resets[rst]);
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200302
Hans de Goedeca52a172014-02-07 16:36:40 +0100303 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
304 clk_put(priv->clks[clk]);
305
306 usb_put_hcd(hcd);
307
308 if (pdata == &ohci_platform_defaults)
309 dev->dev.platform_data = NULL;
310
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100311 return 0;
312}
313
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900314#ifdef CONFIG_PM_SLEEP
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100315static int ohci_platform_suspend(struct device *dev)
316{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530317 struct usb_hcd *hcd = dev_get_drvdata(dev);
318 struct usb_ohci_pdata *pdata = dev->platform_data;
Geliang Tang20db5512015-12-23 21:26:52 +0800319 struct platform_device *pdev = to_platform_device(dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530320 bool do_wakeup = device_may_wakeup(dev);
321 int ret;
322
323 ret = ohci_suspend(hcd, do_wakeup);
324 if (ret)
325 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700326
327 if (pdata->power_suspend)
328 pdata->power_suspend(pdev);
329
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530330 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100331}
332
333static int ohci_platform_resume(struct device *dev)
334{
335 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900336 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Geliang Tang20db5512015-12-23 21:26:52 +0800337 struct platform_device *pdev = to_platform_device(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700338
339 if (pdata->power_on) {
340 int err = pdata->power_on(pdev);
341 if (err < 0)
342 return err;
343 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100344
Florian Fainellicfa49b42012-10-08 15:11:29 +0200345 ohci_resume(hcd, false);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100346 return 0;
347}
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900348#endif /* CONFIG_PM_SLEEP */
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100349
Hans de Goedeca52a172014-02-07 16:36:40 +0100350static const struct of_device_id ohci_platform_ids[] = {
Hans de Goedece149c32014-02-11 17:35:28 +0100351 { .compatible = "generic-ohci", },
Andreas Herrmanna95cfa62015-01-06 13:48:56 +0100352 { .compatible = "cavium,octeon-6335-ohci", },
Hans de Goedeca52a172014-02-07 16:36:40 +0100353 { }
354};
355MODULE_DEVICE_TABLE(of, ohci_platform_ids);
356
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100357static const struct platform_device_id ohci_platform_table[] = {
358 { "ohci-platform", 0 },
359 { }
360};
361MODULE_DEVICE_TABLE(platform, ohci_platform_table);
362
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900363static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
364 ohci_platform_resume);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100365
366static struct platform_driver ohci_platform_driver = {
367 .id_table = ohci_platform_table,
368 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500369 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100370 .shutdown = usb_hcd_platform_shutdown,
371 .driver = {
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100372 .name = "ohci-platform",
373 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100374 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100375 }
376};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530377
378static int __init ohci_platform_init(void)
379{
380 if (usb_disabled())
381 return -ENODEV;
382
383 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
384
385 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
386 return platform_driver_register(&ohci_platform_driver);
387}
388module_init(ohci_platform_init);
389
390static void __exit ohci_platform_cleanup(void)
391{
392 platform_driver_unregister(&ohci_platform_driver);
393}
394module_exit(ohci_platform_cleanup);
395
396MODULE_DESCRIPTION(DRIVER_DESC);
397MODULE_AUTHOR("Hauke Mehrtens");
398MODULE_AUTHOR("Alan Stern");
399MODULE_LICENSE("GPL");