]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/host/ehci-platform.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / host / ehci-platform.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
7a7a4a59
HM
2/*
3 * Generic platform ehci driver
4 *
5 * Copyright 2007 Steven Brown <sbrown@cortland.com>
6 * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
a4aeb211 7 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
7a7a4a59
HM
8 *
9 * Derived from the ohci-ssb driver
10 * Copyright 2007 Michael Buesch <m@bues.ch>
11 *
12 * Derived from the EHCI-PCI driver
13 * Copyright (c) 2000-2004 by David Brownell
14 *
15 * Derived from the ohci-pci driver
16 * Copyright 1999 Roman Weissgaerber
17 * Copyright 2000-2002 David Brownell
18 * Copyright 1999 Linus Torvalds
19 * Copyright 1999 Gregory P. Smith
20 *
21 * Licensed under the GNU/GPL. See COPYING for details.
22 */
776c15d0 23#include <linux/acpi.h>
a4aeb211 24#include <linux/clk.h>
f3bc64d6 25#include <linux/dma-mapping.h>
148e1134 26#include <linux/err.h>
99f91934 27#include <linux/kernel.h>
d1bb67a7
AS
28#include <linux/hrtimer.h>
29#include <linux/io.h>
99f91934 30#include <linux/module.h>
f3bc64d6 31#include <linux/of.h>
a4aeb211 32#include <linux/phy/phy.h>
7a7a4a59 33#include <linux/platform_device.h>
2d87bbd6 34#include <linux/reset.h>
99f91934
AS
35#include <linux/usb.h>
36#include <linux/usb/hcd.h>
7a7a4a59 37#include <linux/usb/ehci_pdriver.h>
d4d75128 38#include <linux/usb/of.h>
7a7a4a59 39
99f91934
AS
40#include "ehci.h"
41
42#define DRIVER_DESC "EHCI generic platform driver"
73577d61 43#define EHCI_MAX_CLKS 4
a4aeb211
HG
44#define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
45
46struct ehci_platform_priv {
47 struct clk *clks[EHCI_MAX_CLKS];
8e84f8aa 48 struct reset_control *rsts;
7e7a0e67
AR
49 struct phy **phys;
50 int num_phys;
b4629a7b 51 bool reset_on_resume;
a4aeb211 52};
99f91934
AS
53
54static const char hcd_name[] = "ehci-platform";
55
7a7a4a59
HM
56static int ehci_platform_reset(struct usb_hcd *hcd)
57{
58 struct platform_device *pdev = to_platform_device(hcd->self.controller);
d4f09e28 59 struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
7a7a4a59
HM
60 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
61 int retval;
62
7a7a4a59 63 ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
7a7a4a59 64
743fcce0
SS
65 if (pdata->pre_setup) {
66 retval = pdata->pre_setup(hcd);
67 if (retval < 0)
68 return retval;
69 }
70
7a7a4a59
HM
71 ehci->caps = hcd->regs + pdata->caps_offset;
72 retval = ehci_setup(hcd);
73 if (retval)
74 return retval;
75
4534874a
FF
76 if (pdata->no_io_watchdog)
77 ehci->need_io_watchdog = 0;
7a7a4a59
HM
78 return 0;
79}
80
a4aeb211
HG
81static int ehci_platform_power_on(struct platform_device *dev)
82{
83 struct usb_hcd *hcd = platform_get_drvdata(dev);
84 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
7e7a0e67 85 int clk, ret, phy_num;
a4aeb211
HG
86
87 for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
88 ret = clk_prepare_enable(priv->clks[clk]);
89 if (ret)
90 goto err_disable_clks;
91 }
92
7e7a0e67 93 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
216e2992
AR
94 ret = phy_init(priv->phys[phy_num]);
95 if (ret)
96 goto err_exit_phy;
97 ret = phy_power_on(priv->phys[phy_num]);
98 if (ret) {
99 phy_exit(priv->phys[phy_num]);
100 goto err_exit_phy;
7e7a0e67 101 }
a4aeb211
HG
102 }
103
104 return 0;
105
106err_exit_phy:
7e7a0e67 107 while (--phy_num >= 0) {
216e2992
AR
108 phy_power_off(priv->phys[phy_num]);
109 phy_exit(priv->phys[phy_num]);
7e7a0e67 110 }
a4aeb211
HG
111err_disable_clks:
112 while (--clk >= 0)
113 clk_disable_unprepare(priv->clks[clk]);
114
115 return ret;
116}
117
118static void ehci_platform_power_off(struct platform_device *dev)
119{
120 struct usb_hcd *hcd = platform_get_drvdata(dev);
121 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
7e7a0e67 122 int clk, phy_num;
a4aeb211 123
7e7a0e67 124 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
216e2992
AR
125 phy_power_off(priv->phys[phy_num]);
126 phy_exit(priv->phys[phy_num]);
a4aeb211
HG
127 }
128
129 for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
130 if (priv->clks[clk])
131 clk_disable_unprepare(priv->clks[clk]);
132}
133
99f91934 134static struct hc_driver __read_mostly ehci_platform_hc_driver;
7a7a4a59 135
62d08a11 136static const struct ehci_driver_overrides platform_overrides __initconst = {
a4aeb211
HG
137 .reset = ehci_platform_reset,
138 .extra_priv_size = sizeof(struct ehci_platform_priv),
7a7a4a59
HM
139};
140
a4aeb211
HG
141static struct usb_ehci_pdata ehci_platform_defaults = {
142 .power_on = ehci_platform_power_on,
143 .power_suspend = ehci_platform_power_off,
144 .power_off = ehci_platform_power_off,
145};
f3bc64d6 146
41ac7b3a 147static int ehci_platform_probe(struct platform_device *dev)
7a7a4a59
HM
148{
149 struct usb_hcd *hcd;
150 struct resource *res_mem;
a4aeb211
HG
151 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
152 struct ehci_platform_priv *priv;
ad3db5da 153 struct ehci_hcd *ehci;
8e84f8aa 154 int err, irq, phy_num, clk = 0;
7a7a4a59 155
7a7a4a59
HM
156 if (usb_disabled())
157 return -ENODEV;
158
f3bc64d6 159 /*
a4aeb211
HG
160 * Use reasonable defaults so platforms don't have to provide these
161 * with DT probing on ARM.
f3bc64d6 162 */
a4aeb211
HG
163 if (!pdata)
164 pdata = &ehci_platform_defaults;
e1fd7341 165
c99e76c5
AH
166 err = dma_coerce_mask_and_coherent(&dev->dev,
167 pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
17f69b5f
JL
168 if (err) {
169 dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
22d9d8e8 170 return err;
17f69b5f 171 }
f3bc64d6 172
7a7a4a59
HM
173 irq = platform_get_irq(dev, 0);
174 if (irq < 0) {
2350cb0c 175 dev_err(&dev->dev, "no irq provided");
7a7a4a59
HM
176 return irq;
177 }
7a7a4a59 178
a4aeb211
HG
179 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
180 dev_name(&dev->dev));
181 if (!hcd)
182 return -ENOMEM;
183
184 platform_set_drvdata(dev, hcd);
185 dev->dev.platform_data = pdata;
186 priv = hcd_to_ehci_priv(hcd);
ad3db5da 187 ehci = hcd_to_ehci(hcd);
a4aeb211
HG
188
189 if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
ad3db5da
HG
190 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
191 ehci->big_endian_mmio = 1;
192
193 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
194 ehci->big_endian_desc = 1;
195
196 if (of_property_read_bool(dev->dev.of_node, "big-endian"))
197 ehci->big_endian_mmio = ehci->big_endian_desc = 1;
198
314b41b1
WL
199 if (of_property_read_bool(dev->dev.of_node,
200 "needs-reset-on-resume"))
b4629a7b 201 priv->reset_on_resume = true;
314b41b1 202
40f2f2a3
JE
203 if (of_property_read_bool(dev->dev.of_node,
204 "has-transaction-translator"))
b4629a7b 205 hcd->has_tt = 1;
40f2f2a3 206
7e7a0e67
AR
207 priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
208 "phys", "#phy-cells");
7e7a0e67 209
216e2992
AR
210 if (priv->num_phys > 0) {
211 priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
212 sizeof(struct phy *), GFP_KERNEL);
213 if (!priv->phys)
214 return -ENOMEM;
215 } else
216 priv->num_phys = 0;
7e7a0e67
AR
217
218 for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
216e2992
AR
219 priv->phys[phy_num] = devm_of_phy_get_by_index(
220 &dev->dev, dev->dev.of_node, phy_num);
221 if (IS_ERR(priv->phys[phy_num])) {
222 err = PTR_ERR(priv->phys[phy_num]);
223 goto err_put_hcd;
42a58c99
YS
224 } else if (!hcd->phy) {
225 /* Avoiding phy_get() in usb_add_hcd() */
226 hcd->phy = priv->phys[phy_num];
216e2992 227 }
a4aeb211
HG
228 }
229
230 for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
231 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
232 if (IS_ERR(priv->clks[clk])) {
233 err = PTR_ERR(priv->clks[clk]);
234 if (err == -EPROBE_DEFER)
235 goto err_put_clks;
236 priv->clks[clk] = NULL;
237 break;
238 }
239 }
240 }
241
8e84f8aa
MY
242 priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev);
243 if (IS_ERR(priv->rsts)) {
244 err = PTR_ERR(priv->rsts);
245 goto err_put_clks;
2d87bbd6
BB
246 }
247
8e84f8aa
MY
248 err = reset_control_deassert(priv->rsts);
249 if (err)
250 goto err_put_clks;
251
843d5e03
AS
252 if (pdata->big_endian_desc)
253 ehci->big_endian_desc = 1;
254 if (pdata->big_endian_mmio)
255 ehci->big_endian_mmio = 1;
b4629a7b
AB
256 if (pdata->has_tt)
257 hcd->has_tt = 1;
258 if (pdata->reset_on_resume)
259 priv->reset_on_resume = true;
843d5e03
AS
260
261#ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
262 if (ehci->big_endian_mmio) {
263 dev_err(&dev->dev,
264 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
265 err = -EINVAL;
2d87bbd6 266 goto err_reset;
843d5e03
AS
267 }
268#endif
269#ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
270 if (ehci->big_endian_desc) {
271 dev_err(&dev->dev,
272 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
273 err = -EINVAL;
2d87bbd6 274 goto err_reset;
843d5e03
AS
275 }
276#endif
277
04216bed
KM
278 if (pdata->power_on) {
279 err = pdata->power_on(dev);
280 if (err < 0)
2d87bbd6 281 goto err_reset;
04216bed 282 }
7a7a4a59 283
2062ff48 284 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
148e1134
TR
285 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
286 if (IS_ERR(hcd->regs)) {
287 err = PTR_ERR(hcd->regs);
a4aeb211 288 goto err_power;
ec03ad85 289 }
2062ff48
VB
290 hcd->rsrc_start = res_mem->start;
291 hcd->rsrc_len = resource_size(res_mem);
292
7a7a4a59
HM
293 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
294 if (err)
a4aeb211 295 goto err_power;
7a7a4a59 296
3c9740a1 297 device_wakeup_enable(hcd->self.controller);
d4d75128 298 device_enable_async_suspend(hcd->self.controller);
7a7a4a59
HM
299 platform_set_drvdata(dev, hcd);
300
301 return err;
302
04216bed
KM
303err_power:
304 if (pdata->power_off)
305 pdata->power_off(dev);
2d87bbd6 306err_reset:
8e84f8aa 307 reset_control_assert(priv->rsts);
a4aeb211
HG
308err_put_clks:
309 while (--clk >= 0)
310 clk_put(priv->clks[clk]);
311err_put_hcd:
312 if (pdata == &ehci_platform_defaults)
313 dev->dev.platform_data = NULL;
314
315 usb_put_hcd(hcd);
04216bed 316
7a7a4a59
HM
317 return err;
318}
319
fb4e98ab 320static int ehci_platform_remove(struct platform_device *dev)
7a7a4a59
HM
321{
322 struct usb_hcd *hcd = platform_get_drvdata(dev);
d4f09e28 323 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
a4aeb211 324 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
8e84f8aa 325 int clk;
7a7a4a59
HM
326
327 usb_remove_hcd(hcd);
7a7a4a59 328
04216bed
KM
329 if (pdata->power_off)
330 pdata->power_off(dev);
331
8e84f8aa 332 reset_control_assert(priv->rsts);
2d87bbd6 333
a4aeb211
HG
334 for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
335 clk_put(priv->clks[clk]);
336
337 usb_put_hcd(hcd);
338
f3bc64d6
AB
339 if (pdata == &ehci_platform_defaults)
340 dev->dev.platform_data = NULL;
341
7a7a4a59
HM
342 return 0;
343}
344
5e4ccd9e 345#ifdef CONFIG_PM_SLEEP
7a7a4a59
HM
346static int ehci_platform_suspend(struct device *dev)
347{
348 struct usb_hcd *hcd = dev_get_drvdata(dev);
d4f09e28 349 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
20db5513 350 struct platform_device *pdev = to_platform_device(dev);
c5cf9212 351 bool do_wakeup = device_may_wakeup(dev);
04216bed
KM
352 int ret;
353
354 ret = ehci_suspend(hcd, do_wakeup);
e155b5b8
VG
355 if (ret)
356 return ret;
7a7a4a59 357
04216bed
KM
358 if (pdata->power_suspend)
359 pdata->power_suspend(pdev);
360
361 return ret;
7a7a4a59
HM
362}
363
364static int ehci_platform_resume(struct device *dev)
365{
366 struct usb_hcd *hcd = dev_get_drvdata(dev);
d4f09e28 367 struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
20db5513 368 struct platform_device *pdev = to_platform_device(dev);
b4629a7b 369 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
d4d75128 370 struct device *companion_dev;
04216bed
KM
371
372 if (pdata->power_on) {
373 int err = pdata->power_on(pdev);
374 if (err < 0)
375 return err;
376 }
7a7a4a59 377
d4d75128 378 companion_dev = usb_of_get_companion_dev(hcd->self.controller);
a7415477 379 if (companion_dev) {
d4d75128 380 device_pm_wait_for_dev(hcd->self.controller, companion_dev);
a7415477
JH
381 put_device(companion_dev);
382 }
d4d75128 383
b4629a7b 384 ehci_resume(hcd, priv->reset_on_resume);
7a7a4a59
HM
385 return 0;
386}
5e4ccd9e 387#endif /* CONFIG_PM_SLEEP */
7a7a4a59 388
f3bc64d6
AB
389static const struct of_device_id vt8500_ehci_ids[] = {
390 { .compatible = "via,vt8500-ehci", },
391 { .compatible = "wm,prizm-ehci", },
915974c3 392 { .compatible = "generic-ehci", },
a95cfa6b 393 { .compatible = "cavium,octeon-6335-ehci", },
f3bc64d6
AB
394 {}
395};
a4aeb211 396MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
f3bc64d6 397
776c15d0
JL
398static const struct acpi_device_id ehci_acpi_match[] = {
399 { "PNP0D20", 0 }, /* EHCI controller without debug */
400 { }
401};
402MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
403
7a7a4a59
HM
404static const struct platform_device_id ehci_platform_table[] = {
405 { "ehci-platform", 0 },
406 { }
407};
408MODULE_DEVICE_TABLE(platform, ehci_platform_table);
409
5e4ccd9e
WK
410static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
411 ehci_platform_resume);
7a7a4a59
HM
412
413static struct platform_driver ehci_platform_driver = {
414 .id_table = ehci_platform_table,
415 .probe = ehci_platform_probe,
7690417d 416 .remove = ehci_platform_remove,
7a7a4a59
HM
417 .shutdown = usb_hcd_platform_shutdown,
418 .driver = {
7a7a4a59
HM
419 .name = "ehci-platform",
420 .pm = &ehci_platform_pm_ops,
ae5e5f7b 421 .of_match_table = vt8500_ehci_ids,
776c15d0 422 .acpi_match_table = ACPI_PTR(ehci_acpi_match),
7a7a4a59
HM
423 }
424};
99f91934
AS
425
426static int __init ehci_platform_init(void)
427{
428 if (usb_disabled())
429 return -ENODEV;
430
431 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
432
433 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
434 return platform_driver_register(&ehci_platform_driver);
435}
436module_init(ehci_platform_init);
437
438static void __exit ehci_platform_cleanup(void)
439{
440 platform_driver_unregister(&ehci_platform_driver);
441}
442module_exit(ehci_platform_cleanup);
443
444MODULE_DESCRIPTION(DRIVER_DESC);
445MODULE_AUTHOR("Hauke Mehrtens");
446MODULE_AUTHOR("Alan Stern");
447MODULE_LICENSE("GPL");