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