sunxi: initial 3.13 support
[openwrt.git] / target / linux / sunxi / patches-3.13 / 172-usb-add-ehci-driver.patch
1 From 825ce97e1faa39bfd30c3dca95fba5eb021cb534 Mon Sep 17 00:00:00 2001
2 From: arokux <arokux@gmail.com>
3 Date: Wed, 18 Sep 2013 21:45:03 +0200
4 Subject: [PATCH] ARM: sunxi: usb: Add Allwinner sunXi EHCI driver
5
6 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
7
8 Conflicts:
9         drivers/usb/host/Makefile
10 ---
11  drivers/usb/host/Kconfig      |   9 +
12  drivers/usb/host/Makefile     |   1 +
13  drivers/usb/host/ehci-sunxi.c | 446 ++++++++++++++++++++++++++++++++++++++++++
14  3 files changed, 456 insertions(+)
15  create mode 100644 drivers/usb/host/ehci-sunxi.c
16
17 diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
18 index a9707da..db94dba 100644
19 --- a/drivers/usb/host/Kconfig
20 +++ b/drivers/usb/host/Kconfig
21 @@ -273,6 +273,15 @@ config USB_OCTEON_EHCI
22           USB 2.0 device support.  All CN6XXX based chips with USB are
23           supported.
24  
25 +config USB_SUNXI_EHCI
26 +       tristate "Allwinner sunXi EHCI support"
27 +       depends on ARCH_SUNXI
28 +       default n
29 +       help
30 +         Enable support for the Allwinner sunXi on-chip EHCI
31 +         controller. It is needed for high-speed (480Mbit/sec)
32 +         USB 2.0 device support.
33 +
34  endif # USB_EHCI_HCD
35  
36  config USB_OXU210HP_HCD
37 diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile
38 index 01e879e..d96053d 100644
39 --- a/drivers/usb/host/Makefile
40 +++ b/drivers/usb/host/Makefile
41 @@ -39,6 +39,7 @@ obj-$(CONFIG_USB_EHCI_HCD_AT91) += ehci-atmel.o
42  obj-$(CONFIG_USB_EHCI_MSM)     += ehci-msm.o
43  obj-$(CONFIG_USB_EHCI_TEGRA)   += ehci-tegra.o
44  obj-$(CONFIG_USB_W90X900_EHCI) += ehci-w90x900.o
45 +obj-$(CONFIG_USB_SUNXI_EHCI)   += ehci-sunxi.o
46  
47  obj-$(CONFIG_USB_OXU210HP_HCD) += oxu210hp-hcd.o
48  obj-$(CONFIG_USB_ISP116X_HCD)  += isp116x-hcd.o
49 diff --git a/drivers/usb/host/ehci-sunxi.c b/drivers/usb/host/ehci-sunxi.c
50 new file mode 100644
51 index 0000000..e7e15cc
52 --- /dev/null
53 +++ b/drivers/usb/host/ehci-sunxi.c
54 @@ -0,0 +1,446 @@
55 +/*
56 + * Copyright (C) 2013 Roman Byshko
57 + *
58 + * Roman Byshko <rbyshko@gmail.com>
59 + *
60 + * Based on code from
61 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
62 + *
63 + * This file is licensed under the terms of the GNU General Public
64 + * License version 2.  This program is licensed "as is" without any
65 + * warranty of any kind, whether express or implied.
66 + */
67 +
68 +#include <linux/bitops.h>
69 +#include <linux/clk.h>
70 +#include <linux/dma-mapping.h>
71 +#include <linux/io.h>
72 +#include <linux/irq.h>
73 +#include <linux/module.h>
74 +#include <linux/of.h>
75 +#include <linux/platform_device.h>
76 +#include <linux/regulator/consumer.h>
77 +#include <linux/reset.h>
78 +#include <linux/usb.h>
79 +#include <linux/usb/hcd.h>
80 +
81 +#include "ehci.h"
82 +
83 +#define DRV_DESC       "Allwinner sunXi EHCI driver"
84 +#define DRV_NAME       "sunxi-ehci"
85 +
86 +#define SUNXI_USB_PASSBY_EN    1
87 +
88 +#define SUNXI_EHCI_AHB_ICHR8_EN                BIT(10)
89 +#define SUNXI_EHCI_AHB_INCR4_BURST_EN  BIT(9)
90 +#define SUNXI_EHCI_AHB_INCRX_ALIGN_EN  BIT(8)
91 +#define SUNXI_EHCI_ULPI_BYPASS_EN      BIT(0)
92 +
93 +struct sunxi_ehci_hcd {
94 +       struct clk *phy_clk;
95 +       struct clk *ahb_ehci_clk;
96 +       struct reset_control *reset;
97 +       struct regulator *vbus_reg;
98 +       void __iomem *csr;
99 +       void __iomem *pmuirq;
100 +       int irq;
101 +       int id;
102 +};
103 +
104 +
105 +static void usb_phy_write(struct sunxi_ehci_hcd *sunxi_ehci,u32 addr, u32 data, u32 len)
106 +{
107 +       u32 j = 0;
108 +       u32 temp = 0;
109 +       u32 usbc_bit = 0;
110 +       void __iomem *dest = sunxi_ehci->csr;
111 +
112 +       usbc_bit = BIT(sunxi_ehci->id << 1);
113 +
114 +       for (j = 0; j < len; j++) {
115 +               temp = readl(dest);
116 +
117 +               /* clear the address portion */
118 +               temp &= ~(0xff << 8);
119 +
120 +               /* set the address */
121 +               temp |= ((addr + j) << 8);
122 +               writel(temp, dest);
123 +
124 +               /* set the data bit and clear usbc bit*/
125 +               temp = readb(dest);
126 +               if (data & 0x1)
127 +                       temp |= BIT(7);
128 +               else
129 +                       temp &= ~BIT(7);
130 +               temp &= ~usbc_bit;
131 +               writeb(temp, dest);
132 +
133 +               /* flip usbc_bit */
134 +               __set_bit(usbc_bit, dest);
135 +               __clear_bit(usbc_bit, dest);
136 +
137 +               data >>= 1;
138 +       }
139 +}
140 +
141 +/* FIXME: should this function be protected by a lock?
142 + * ehci1 and ehci0 could call it concurrently with same csr.
143 + */
144 +static void sunxi_usb_phy_init(struct sunxi_ehci_hcd *sunxi_ehci)
145 +{
146 +       /* The following comments are machine
147 +        * translated from Chinese, you have been warned!
148 +        */
149 +
150 +       /* adjust PHY's magnitude and rate */
151 +       usb_phy_write(sunxi_ehci, 0x20, 0x14, 5);
152 +
153 +       /* threshold adjustment disconnect */
154 +       usb_phy_write(sunxi_ehci, 0x2a, 3, 2);
155 +
156 +       return;
157 +}
158 +
159 +static void sunxi_usb_passby(struct sunxi_ehci_hcd *sunxi_ehci, int enable)
160 +{
161 +       unsigned long reg_value = 0;
162 +       unsigned long bits = 0;
163 +       static DEFINE_SPINLOCK(lock);
164 +       unsigned long flags = 0;
165 +       void __iomem *addr = sunxi_ehci->pmuirq;
166 +
167 +       bits = SUNXI_EHCI_AHB_ICHR8_EN |
168 +               SUNXI_EHCI_AHB_INCR4_BURST_EN |
169 +               SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
170 +               SUNXI_EHCI_ULPI_BYPASS_EN;
171 +
172 +       spin_lock_irqsave(&lock, flags);
173 +
174 +       reg_value = readl(addr);
175 +
176 +       if (enable)
177 +               reg_value |= bits;
178 +       else
179 +               reg_value &= ~bits;
180 +
181 +       writel(reg_value, addr);
182 +
183 +       spin_unlock_irqrestore(&lock, flags);
184 +
185 +       return;
186 +}
187 +
188 +static void sunxi_ehci_disable(struct sunxi_ehci_hcd *sunxi_ehci)
189 +{
190 +       regulator_disable(sunxi_ehci->vbus_reg);
191 +
192 +       sunxi_usb_passby(sunxi_ehci, !SUNXI_USB_PASSBY_EN);
193 +
194 +       clk_disable_unprepare(sunxi_ehci->ahb_ehci_clk);
195 +       clk_disable_unprepare(sunxi_ehci->phy_clk);
196 +
197 +       reset_control_assert(sunxi_ehci->reset);
198 +}
199 +
200 +static int sunxi_ehci_enable(struct sunxi_ehci_hcd *sunxi_ehci)
201 +{
202 +       int ret;
203 +
204 +       ret = clk_prepare_enable(sunxi_ehci->phy_clk);
205 +       if (ret)
206 +               return ret;
207 +
208 +       ret = reset_control_deassert(sunxi_ehci->reset);
209 +       if (ret)
210 +               goto fail1;
211 +
212 +       ret = clk_prepare_enable(sunxi_ehci->ahb_ehci_clk);
213 +       if (ret)
214 +               goto fail2;
215 +
216 +       sunxi_usb_phy_init(sunxi_ehci);
217 +
218 +       sunxi_usb_passby(sunxi_ehci, SUNXI_USB_PASSBY_EN);
219 +
220 +       ret = regulator_enable(sunxi_ehci->vbus_reg);
221 +       if (ret)
222 +               goto fail3;
223 +
224 +       return 0;
225 +
226 +fail3:
227 +       clk_disable_unprepare(sunxi_ehci->ahb_ehci_clk);
228 +fail2:
229 +       reset_control_assert(sunxi_ehci->reset);
230 +fail1:
231 +       clk_disable_unprepare(sunxi_ehci->phy_clk);
232 +
233 +       return ret;
234 +}
235 +
236 +#ifdef CONFIG_PM
237 +static int sunxi_ehci_suspend(struct device *dev)
238 +{
239 +       struct sunxi_ehci_hcd *sunxi_ehci = NULL;
240 +       struct usb_hcd *hcd = dev_get_drvdata(dev);
241 +       int ret;
242 +
243 +       bool do_wakeup = device_may_wakeup(dev);
244 +
245 +       ret = ehci_suspend(hcd, do_wakeup);
246 +
247 +       sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
248 +
249 +       sunxi_ehci_disable(sunxi_ehci);
250 +
251 +       return ret;
252 +}
253 +
254 +static int sunxi_ehci_resume(struct device *dev)
255 +{
256 +       struct sunxi_ehci_hcd *sunxi_ehci = NULL;
257 +       struct usb_hcd *hcd = dev_get_drvdata(dev);
258 +       int ret;
259 +
260 +       sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
261 +
262 +       ret = sunxi_ehci_enable(sunxi_ehci);
263 +       if (ret)
264 +               return ret;
265 +
266 +       return ehci_resume(hcd, false);
267 +}
268 +
269 +
270 +static const struct dev_pm_ops sunxi_ehci_pmops = {
271 +       .suspend        = sunxi_ehci_suspend,
272 +       .resume         = sunxi_ehci_resume,
273 +};
274 +
275 +#define SUNXI_EHCI_PMOPS (&sunxi_ehci_pmops)
276 +#else /* !CONFIG_PM */
277 +#define SUNXI_EHCI_PMOPS NULL
278 +#endif /* CONFIG_PM */
279 +
280 +static const struct ehci_driver_overrides sunxi_overrides __initconst = {
281 +       .reset =        NULL,
282 +       .extra_priv_size        = sizeof(struct sunxi_ehci_hcd),
283 +};
284 +
285 +/* FIXME: Should there be two instances of hc_driver,
286 + * or one is enough to handle two EHCI controllers? */
287 +static struct hc_driver __read_mostly sunxi_ehci_hc_driver;
288 +
289 +static int sunxi_ehci_init(struct platform_device *pdev, struct usb_hcd *hcd,
290 +                          struct sunxi_ehci_hcd *sunxi_ehci)
291 +{
292 +       void __iomem *ehci_regs = NULL;
293 +       struct resource *res = NULL;
294 +
295 +       sunxi_ehci->vbus_reg = devm_regulator_get(&pdev->dev, "vbus");
296 +       if (IS_ERR(sunxi_ehci->vbus_reg)) {
297 +               if (PTR_ERR(sunxi_ehci->vbus_reg) == -EPROBE_DEFER)
298 +                       return -EPROBE_DEFER;
299 +
300 +               dev_info(&pdev->dev, "no USB VBUS power supply found\n");
301 +       }
302 +
303 +       sunxi_ehci->id = of_alias_get_id(pdev->dev.of_node, "ehci");
304 +       if (sunxi_ehci->id < 0)
305 +               return sunxi_ehci->id;
306 +
307 +       /* FIXME: should res be freed on some failure? */
308 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
309 +       if (!res) {
310 +               dev_err(&pdev->dev, "failed to get I/O memory\n");
311 +               return -ENXIO;
312 +       }
313 +       ehci_regs = devm_ioremap_resource(&pdev->dev, res);
314 +       if (IS_ERR(ehci_regs))
315 +               return PTR_ERR(ehci_regs);
316 +
317 +       hcd->rsrc_start = res->start;
318 +       hcd->rsrc_len = resource_size(res);
319 +       hcd->regs = ehci_regs;
320 +       hcd_to_ehci(hcd)->caps = ehci_regs;
321 +
322 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
323 +       if (!res) {
324 +               dev_err(&pdev->dev, "failed to get I/O memory\n");
325 +               return -ENXIO;
326 +       }
327 +       sunxi_ehci->pmuirq = devm_ioremap_resource(&pdev->dev, res);
328 +       if (IS_ERR(sunxi_ehci->pmuirq))
329 +               return PTR_ERR(sunxi_ehci->pmuirq);
330 +
331 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
332 +       if (!res) {
333 +               dev_err(&pdev->dev, "failed to get I/O memory\n");
334 +               return -ENXIO;
335 +       }
336 +
337 +       /* FIXME: this one byte needs to be shared between both EHCIs,
338 +        * that is why ioremap instead of devm_ioremap_resource,
339 +        * memory is not unmaped back for now.
340 +        */
341 +       sunxi_ehci->csr = ioremap(res->start, resource_size(res));
342 +       if (IS_ERR(sunxi_ehci->csr)) {
343 +               dev_err(&pdev->dev, "failed to remap memory\n");
344 +               return PTR_ERR(sunxi_ehci->csr);
345 +       }
346 +
347 +       sunxi_ehci->irq = platform_get_irq(pdev, 0);
348 +       if (!sunxi_ehci->irq) {
349 +               dev_err(&pdev->dev, "failed to get IRQ\n");
350 +               return -ENODEV;
351 +       }
352 +
353 +       sunxi_ehci->phy_clk = devm_clk_get(&pdev->dev, "usb_phy");
354 +       if (IS_ERR(sunxi_ehci->phy_clk)) {
355 +               dev_err(&pdev->dev, "failed to get usb_phy clock\n");
356 +               return PTR_ERR(sunxi_ehci->phy_clk);
357 +       }
358 +       sunxi_ehci->ahb_ehci_clk = devm_clk_get(&pdev->dev, "ahb_ehci");
359 +       if (IS_ERR(sunxi_ehci->ahb_ehci_clk)) {
360 +               dev_err(&pdev->dev, "failed to get ahb_ehci clock\n");
361 +               return PTR_ERR(sunxi_ehci->ahb_ehci_clk);
362 +       }
363 +
364 +       sunxi_ehci->reset = reset_control_get(&pdev->dev, "ehci_reset");
365 +       if (IS_ERR(sunxi_ehci->reset))
366 +       {
367 +               dev_err(&pdev->dev, "failed to get ehci_reset reset line\n");
368 +               return PTR_ERR(sunxi_ehci->reset);
369 +       }
370 +
371 +       return 0;
372 +}
373 +
374 +static int sunxi_ehci_probe(struct platform_device *pdev)
375 +{
376 +       struct sunxi_ehci_hcd *sunxi_ehci = NULL;
377 +       struct usb_hcd *hcd = NULL;
378 +       int ret;
379 +
380 +       if (pdev->num_resources != 4) {
381 +               dev_err(&pdev->dev, "invalid number of resources: %i\n",
382 +                      pdev->num_resources);
383 +               return -ENODEV;
384 +       }
385 +
386 +       if (pdev->resource[0].flags != IORESOURCE_MEM
387 +                       || pdev->resource[1].flags != IORESOURCE_MEM
388 +                       || pdev->resource[2].flags != IORESOURCE_MEM
389 +                       || pdev->resource[3].flags != IORESOURCE_IRQ) {
390 +               dev_err(&pdev->dev, "invalid resource type\n");
391 +               return -ENODEV;
392 +       }
393 +
394 +       if (!pdev->dev.dma_mask)
395 +               pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
396 +       if (!pdev->dev.coherent_dma_mask)
397 +               pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
398 +
399 +       hcd = usb_create_hcd(&sunxi_ehci_hc_driver, &pdev->dev,
400 +                            dev_name(&pdev->dev));
401 +       if (!hcd) {
402 +               dev_err(&pdev->dev, "unable to create HCD\n");
403 +               return -ENOMEM;
404 +       }
405 +
406 +       platform_set_drvdata(pdev, hcd);
407 +
408 +       sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
409 +       ret = sunxi_ehci_init(pdev, hcd, sunxi_ehci);
410 +       if (ret)
411 +               goto fail1;
412 +
413 +       ret = sunxi_ehci_enable(sunxi_ehci);
414 +       if (ret)
415 +               goto fail1;
416 +
417 +       ret = usb_add_hcd(hcd, sunxi_ehci->irq, IRQF_SHARED | IRQF_DISABLED);
418 +       if (ret) {
419 +               dev_err(&pdev->dev, "failed to add USB HCD\n");
420 +               goto fail2;
421 +       }
422 +
423 +       return 0;
424 +
425 +fail2:
426 +       sunxi_ehci_disable(sunxi_ehci);
427 +
428 +fail1:
429 +       usb_put_hcd(hcd);
430 +       return ret;
431 +}
432 +
433 +static int sunxi_ehci_remove(struct platform_device *pdev)
434 +{
435 +       struct usb_hcd *hcd = platform_get_drvdata(pdev);
436 +       struct sunxi_ehci_hcd *sunxi_ehci = NULL;
437 +
438 +       sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
439 +
440 +       usb_remove_hcd(hcd);
441 +
442 +       sunxi_ehci_disable(sunxi_ehci);
443 +
444 +       usb_put_hcd(hcd);
445 +
446 +       return 0;
447 +}
448 +
449 +static void sunxi_ehci_shutdown(struct platform_device *pdev)
450 +{
451 +       struct usb_hcd *hcd = platform_get_drvdata(pdev);
452 +       struct sunxi_ehci_hcd *sunxi_ehci = NULL;
453 +
454 +       sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
455 +
456 +       usb_hcd_platform_shutdown(pdev);
457 +
458 +       sunxi_ehci_disable(sunxi_ehci);
459 +}
460 +
461 +static const struct of_device_id ehci_of_match[] = {
462 +       {.compatible = "allwinner,sunxi-ehci"},
463 +       {},
464 +};
465 +
466 +static struct platform_driver ehci_sunxi_driver = {
467 +       .driver = {
468 +               .of_match_table = ehci_of_match,
469 +               .name = DRV_NAME,
470 +               .pm = SUNXI_EHCI_PMOPS,
471 +       },
472 +       .probe = sunxi_ehci_probe,
473 +       .remove = sunxi_ehci_remove,
474 +       .shutdown = sunxi_ehci_shutdown,
475 +};
476 +
477 +static int __init sunxi_ehci_init_module(void)
478 +{
479 +       if (usb_disabled())
480 +               return -ENODEV;
481 +
482 +       pr_info(DRV_NAME ": " DRV_DESC "\n");
483 +
484 +       ehci_init_driver(&sunxi_ehci_hc_driver, &sunxi_overrides);
485 +
486 +       return platform_driver_register(&ehci_sunxi_driver);
487 +}
488 +module_init(sunxi_ehci_init_module);
489 +
490 +static void __exit sunxi_ehci_exit_module(void)
491 +{
492 +       platform_driver_unregister(&ehci_sunxi_driver);
493 +}
494 +module_exit(sunxi_ehci_exit_module);
495 +
496 +MODULE_DESCRIPTION(DRIVER_DESC);
497 +MODULE_LICENSE("GPL");
498 +MODULE_ALIAS("platform:" DRV_NAME);
499 +MODULE_DEVICE_TABLE(of, ehci_of_match);
500 +MODULE_AUTHOR("Roman Byshko <rbyshko@gmail.com>");
501 -- 
502 1.8.5.1
503