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