blob: 4a8456f12a73dba3f461d6001f9569c67c3e0384 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +01002/*
3 * Generic platform ohci driver
4 *
5 * Copyright 2007 Michael Buesch <m@bues.ch>
6 * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
Hans de Goedeca52a172014-02-07 16:36:40 +01007 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +01008 *
9 * Derived from the OCHI-SSB driver
10 * Derived from the OHCI-PCI driver
11 * Copyright 1999 Roman Weissgaerber
12 * Copyright 2000-2002 David Brownell
13 * Copyright 1999 Linus Torvalds
14 * Copyright 1999 Gregory P. Smith
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010015 */
Manjunath Goudar928fb682013-06-03 20:46:08 +053016
Hans de Goedeca52a172014-02-07 16:36:40 +010017#include <linux/clk.h>
18#include <linux/dma-mapping.h>
Manjunath Goudar928fb682013-06-03 20:46:08 +053019#include <linux/hrtimer.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
Thierry Reding148e1132013-01-21 11:09:22 +010023#include <linux/err.h>
Martin Blumenstingl1255dfd2018-03-03 22:43:08 +010024#include <linux/of.h>
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010025#include <linux/platform_device.h>
Tony Lindgren0aa0b932017-05-25 09:13:31 -070026#include <linux/pm_runtime.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];
Masahiro Yamada20a7f3a2017-11-02 01:01:59 +090040 struct reset_control *resets;
Hans de Goedeca52a172014-02-07 16:36:40 +010041};
Manjunath Goudar928fb682013-06-03 20:46:08 +053042
43static const char hcd_name[] = "ohci-platform";
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010044
Hans de Goedeca52a172014-02-07 16:36:40 +010045static int ohci_platform_power_on(struct platform_device *dev)
46{
47 struct usb_hcd *hcd = platform_get_drvdata(dev);
48 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
Martin Blumenstingl1255dfd2018-03-03 22:43:08 +010049 int clk, ret;
Hans de Goedeca52a172014-02-07 16:36:40 +010050
51 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
52 ret = clk_prepare_enable(priv->clks[clk]);
53 if (ret)
54 goto err_disable_clks;
55 }
56
Hans de Goedeca52a172014-02-07 16:36:40 +010057 return 0;
58
Hans de Goedeca52a172014-02-07 16:36:40 +010059err_disable_clks:
60 while (--clk >= 0)
61 clk_disable_unprepare(priv->clks[clk]);
62
63 return ret;
64}
65
66static void ohci_platform_power_off(struct platform_device *dev)
67{
68 struct usb_hcd *hcd = platform_get_drvdata(dev);
69 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
Martin Blumenstingl1255dfd2018-03-03 22:43:08 +010070 int clk;
Hans de Goedeca52a172014-02-07 16:36:40 +010071
72 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
73 if (priv->clks[clk])
74 clk_disable_unprepare(priv->clks[clk]);
75}
76
Manjunath Goudar928fb682013-06-03 20:46:08 +053077static struct hc_driver __read_mostly ohci_platform_hc_driver;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010078
Manjunath Goudar928fb682013-06-03 20:46:08 +053079static const struct ohci_driver_overrides platform_overrides __initconst = {
Hans de Goedeca52a172014-02-07 16:36:40 +010080 .product_desc = "Generic Platform OHCI controller",
Hans de Goedeca52a172014-02-07 16:36:40 +010081 .extra_priv_size = sizeof(struct ohci_platform_priv),
82};
83
84static struct usb_ohci_pdata ohci_platform_defaults = {
85 .power_on = ohci_platform_power_on,
86 .power_suspend = ohci_platform_power_off,
87 .power_off = ohci_platform_power_off,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010088};
89
Bill Pemberton41ac7b32012-11-19 13:21:48 -050090static int ohci_platform_probe(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010091{
92 struct usb_hcd *hcd;
93 struct resource *res_mem;
Jingoo Hand4f09e22013-07-30 19:59:40 +090094 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +010095 struct ohci_platform_priv *priv;
Hans de Goedeb1034412014-02-07 16:36:42 +010096 struct ohci_hcd *ohci;
Martin Blumenstingl1255dfd2018-03-03 22:43:08 +010097 int err, irq, clk = 0;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +010098
99 if (usb_disabled())
100 return -ENODEV;
101
Hans de Goedeca52a172014-02-07 16:36:40 +0100102 /*
103 * Use reasonable defaults so platforms don't have to provide these
104 * with DT probing on ARM.
105 */
106 if (!pdata)
107 pdata = &ohci_platform_defaults;
108
109 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
110 if (err)
111 return err;
112
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100113 irq = platform_get_irq(dev, 0);
Stephen Boydb33f3702019-07-30 11:15:46 -0700114 if (irq < 0)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100115 return irq;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100116
Hans de Goedeca52a172014-02-07 16:36:40 +0100117 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
118 dev_name(&dev->dev));
119 if (!hcd)
120 return -ENOMEM;
121
122 platform_set_drvdata(dev, hcd);
123 dev->dev.platform_data = pdata;
124 priv = hcd_to_ohci_priv(hcd);
Hans de Goedeb1034412014-02-07 16:36:42 +0100125 ohci = hcd_to_ohci(hcd);
Hans de Goedeca52a172014-02-07 16:36:40 +0100126
127 if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
Hans de Goedeb1034412014-02-07 16:36:42 +0100128 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
129 ohci->flags |= OHCI_QUIRK_BE_MMIO;
130
131 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
132 ohci->flags |= OHCI_QUIRK_BE_DESC;
133
134 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
135 ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
136
Kevin Cernekeec4b86922014-10-11 11:10:48 -0700137 if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
138 ohci->flags |= OHCI_QUIRK_FRAME_NO;
139
Tony Lindgren2545d852017-05-25 09:13:32 -0700140 if (of_property_read_bool(dev->dev.of_node,
141 "remote-wakeup-connected"))
142 ohci->hc_control = OHCI_CTRL_RWC;
143
Kevin Cernekeec4b86922014-10-11 11:10:48 -0700144 of_property_read_u32(dev->dev.of_node, "num-ports",
145 &ohci->num_ports);
146
Hans de Goedeca52a172014-02-07 16:36:40 +0100147 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
148 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
149 if (IS_ERR(priv->clks[clk])) {
150 err = PTR_ERR(priv->clks[clk]);
151 if (err == -EPROBE_DEFER)
152 goto err_put_clks;
153 priv->clks[clk] = NULL;
154 break;
155 }
156 }
Masahiro Yamada20a7f3a2017-11-02 01:01:59 +0900157
158 priv->resets = devm_reset_control_array_get_optional_shared(
159 &dev->dev);
160 if (IS_ERR(priv->resets)) {
161 err = PTR_ERR(priv->resets);
162 goto err_put_clks;
Hans de Goede62d96942016-06-02 17:14:06 +0200163 }
Masahiro Yamada20a7f3a2017-11-02 01:01:59 +0900164
165 err = reset_control_deassert(priv->resets);
166 if (err)
167 goto err_put_clks;
Hans de Goedeca52a172014-02-07 16:36:40 +0100168 }
169
Alan Sternadff5292014-02-11 11:26:00 -0500170 if (pdata->big_endian_desc)
171 ohci->flags |= OHCI_QUIRK_BE_DESC;
172 if (pdata->big_endian_mmio)
173 ohci->flags |= OHCI_QUIRK_BE_MMIO;
Kevin Cernekee46f21942014-10-11 11:10:49 -0700174 if (pdata->no_big_frame_no)
175 ohci->flags |= OHCI_QUIRK_FRAME_NO;
176 if (pdata->num_ports)
177 ohci->num_ports = pdata->num_ports;
Alan Sternadff5292014-02-11 11:26:00 -0500178
179#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
180 if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
181 dev_err(&dev->dev,
182 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
183 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200184 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500185 }
186#endif
187#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
188 if (ohci->flags & OHCI_QUIRK_BE_DESC) {
189 dev_err(&dev->dev,
190 "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
191 err = -EINVAL;
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200192 goto err_reset;
Alan Sternadff5292014-02-11 11:26:00 -0500193 }
194#endif
195
Tony Lindgren0aa0b932017-05-25 09:13:31 -0700196 pm_runtime_set_active(&dev->dev);
197 pm_runtime_enable(&dev->dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700198 if (pdata->power_on) {
199 err = pdata->power_on(dev);
200 if (err < 0)
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200201 goto err_reset;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700202 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100203
Varka Bhadram7b519292014-11-04 07:51:16 +0530204 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100205 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
206 if (IS_ERR(hcd->regs)) {
207 err = PTR_ERR(hcd->regs);
Hans de Goedeca52a172014-02-07 16:36:40 +0100208 goto err_power;
Julia Lawallaece3892012-08-14 08:47:37 +0200209 }
Varka Bhadram7b519292014-11-04 07:51:16 +0530210 hcd->rsrc_start = res_mem->start;
211 hcd->rsrc_len = resource_size(res_mem);
212
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100213 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
214 if (err)
Hans de Goedeca52a172014-02-07 16:36:40 +0100215 goto err_power;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100216
Peter Chen3c9740a2013-11-05 10:46:02 +0800217 device_wakeup_enable(hcd->self.controller);
218
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100219 platform_set_drvdata(dev, hcd);
220
221 return err;
222
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700223err_power:
224 if (pdata->power_off)
225 pdata->power_off(dev);
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200226err_reset:
Tony Lindgren0aa0b932017-05-25 09:13:31 -0700227 pm_runtime_disable(&dev->dev);
Masahiro Yamada20a7f3a2017-11-02 01:01:59 +0900228 reset_control_assert(priv->resets);
Hans de Goedeca52a172014-02-07 16:36:40 +0100229err_put_clks:
230 while (--clk >= 0)
231 clk_put(priv->clks[clk]);
Martin Blumenstingl1255dfd2018-03-03 22:43:08 +0100232
Hans de Goedeca52a172014-02-07 16:36:40 +0100233 if (pdata == &ohci_platform_defaults)
234 dev->dev.platform_data = NULL;
235
236 usb_put_hcd(hcd);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700237
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100238 return err;
239}
240
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500241static int ohci_platform_remove(struct platform_device *dev)
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100242{
243 struct usb_hcd *hcd = platform_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900244 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
Hans de Goedeca52a172014-02-07 16:36:40 +0100245 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
Masahiro Yamada20a7f3a2017-11-02 01:01:59 +0900246 int clk;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100247
Tony Lindgren0aa0b932017-05-25 09:13:31 -0700248 pm_runtime_get_sync(&dev->dev);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100249 usb_remove_hcd(hcd);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100250
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700251 if (pdata->power_off)
252 pdata->power_off(dev);
253
Masahiro Yamada20a7f3a2017-11-02 01:01:59 +0900254 reset_control_assert(priv->resets);
Maxime Ripard4615f3b2014-05-13 17:44:20 +0200255
Hans de Goedeca52a172014-02-07 16:36:40 +0100256 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
257 clk_put(priv->clks[clk]);
258
259 usb_put_hcd(hcd);
260
Tony Lindgren0aa0b932017-05-25 09:13:31 -0700261 pm_runtime_put_sync(&dev->dev);
262 pm_runtime_disable(&dev->dev);
263
Hans de Goedeca52a172014-02-07 16:36:40 +0100264 if (pdata == &ohci_platform_defaults)
265 dev->dev.platform_data = NULL;
266
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100267 return 0;
268}
269
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900270#ifdef CONFIG_PM_SLEEP
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100271static int ohci_platform_suspend(struct device *dev)
272{
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530273 struct usb_hcd *hcd = dev_get_drvdata(dev);
274 struct usb_ohci_pdata *pdata = dev->platform_data;
Geliang Tang20db5512015-12-23 21:26:52 +0800275 struct platform_device *pdev = to_platform_device(dev);
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530276 bool do_wakeup = device_may_wakeup(dev);
277 int ret;
278
279 ret = ohci_suspend(hcd, do_wakeup);
280 if (ret)
281 return ret;
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700282
283 if (pdata->power_suspend)
284 pdata->power_suspend(pdev);
285
Manjunath Goudar39dbd7d2013-10-04 09:58:14 +0530286 return ret;
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100287}
288
289static int ohci_platform_resume(struct device *dev)
290{
291 struct usb_hcd *hcd = dev_get_drvdata(dev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900292 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
Geliang Tang20db5512015-12-23 21:26:52 +0800293 struct platform_device *pdev = to_platform_device(dev);
Kuninori Morimotoe4d37ae2012-08-06 18:09:10 -0700294
295 if (pdata->power_on) {
296 int err = pdata->power_on(pdev);
297 if (err < 0)
298 return err;
299 }
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100300
Florian Fainellicfa49b42012-10-08 15:11:29 +0200301 ohci_resume(hcd, false);
Qais Yousef1cb3b002020-05-18 16:49:29 +0100302
303 pm_runtime_disable(dev);
304 pm_runtime_set_active(dev);
305 pm_runtime_enable(dev);
306
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100307 return 0;
308}
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900309#endif /* CONFIG_PM_SLEEP */
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100310
Hans de Goedeca52a172014-02-07 16:36:40 +0100311static const struct of_device_id ohci_platform_ids[] = {
Hans de Goedece149c32014-02-11 17:35:28 +0100312 { .compatible = "generic-ohci", },
Andreas Herrmanna95cfa62015-01-06 13:48:56 +0100313 { .compatible = "cavium,octeon-6335-ohci", },
Tony Lindgren2545d852017-05-25 09:13:32 -0700314 { .compatible = "ti,ohci-omap3", },
Hans de Goedeca52a172014-02-07 16:36:40 +0100315 { }
316};
317MODULE_DEVICE_TABLE(of, ohci_platform_ids);
318
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100319static const struct platform_device_id ohci_platform_table[] = {
320 { "ohci-platform", 0 },
321 { }
322};
323MODULE_DEVICE_TABLE(platform, ohci_platform_table);
324
Wonhong Kwon5e4ccd92014-10-24 13:45:47 +0900325static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
326 ohci_platform_resume);
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100327
328static struct platform_driver ohci_platform_driver = {
329 .id_table = ohci_platform_table,
330 .probe = ohci_platform_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500331 .remove = ohci_platform_remove,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100332 .shutdown = usb_hcd_platform_shutdown,
333 .driver = {
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100334 .name = "ohci-platform",
335 .pm = &ohci_platform_pm_ops,
Hans de Goedeca52a172014-02-07 16:36:40 +0100336 .of_match_table = ohci_platform_ids,
Hauke Mehrtensfa3364b2012-03-13 01:04:47 +0100337 }
338};
Manjunath Goudar928fb682013-06-03 20:46:08 +0530339
340static int __init ohci_platform_init(void)
341{
342 if (usb_disabled())
343 return -ENODEV;
344
345 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
346
347 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
348 return platform_driver_register(&ohci_platform_driver);
349}
350module_init(ohci_platform_init);
351
352static void __exit ohci_platform_cleanup(void)
353{
354 platform_driver_unregister(&ohci_platform_driver);
355}
356module_exit(ohci_platform_cleanup);
357
358MODULE_DESCRIPTION(DRIVER_DESC);
359MODULE_AUTHOR("Hauke Mehrtens");
360MODULE_AUTHOR("Alan Stern");
361MODULE_LICENSE("GPL");