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