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
6 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
9 drivers/usb/host/Makefile
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
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
25 +config USB_SUNXI_EHCI
26 + tristate "Allwinner sunXi EHCI support"
27 + depends on ARCH_SUNXI
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.
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
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
51 index 0000000..e7e15cc
53 +++ b/drivers/usb/host/ehci-sunxi.c
56 + * Copyright (C) 2013 Roman Byshko
58 + * Roman Byshko <rbyshko@gmail.com>
60 + * Based on code from
61 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
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.
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>
83 +#define DRV_DESC "Allwinner sunXi EHCI driver"
84 +#define DRV_NAME "sunxi-ehci"
86 +#define SUNXI_USB_PASSBY_EN 1
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)
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;
99 + void __iomem *pmuirq;
105 +static void usb_phy_write(struct sunxi_ehci_hcd *sunxi_ehci,u32 addr, u32 data, u32 len)
110 + void __iomem *dest = sunxi_ehci->csr;
112 + usbc_bit = BIT(sunxi_ehci->id << 1);
114 + for (j = 0; j < len; j++) {
115 + temp = readl(dest);
117 + /* clear the address portion */
118 + temp &= ~(0xff << 8);
120 + /* set the address */
121 + temp |= ((addr + j) << 8);
122 + writel(temp, dest);
124 + /* set the data bit and clear usbc bit*/
125 + temp = readb(dest);
131 + writeb(temp, dest);
133 + /* flip usbc_bit */
134 + __set_bit(usbc_bit, dest);
135 + __clear_bit(usbc_bit, dest);
141 +/* FIXME: should this function be protected by a lock?
142 + * ehci1 and ehci0 could call it concurrently with same csr.
144 +static void sunxi_usb_phy_init(struct sunxi_ehci_hcd *sunxi_ehci)
146 + /* The following comments are machine
147 + * translated from Chinese, you have been warned!
150 + /* adjust PHY's magnitude and rate */
151 + usb_phy_write(sunxi_ehci, 0x20, 0x14, 5);
153 + /* threshold adjustment disconnect */
154 + usb_phy_write(sunxi_ehci, 0x2a, 3, 2);
159 +static void sunxi_usb_passby(struct sunxi_ehci_hcd *sunxi_ehci, int enable)
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;
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;
172 + spin_lock_irqsave(&lock, flags);
174 + reg_value = readl(addr);
179 + reg_value &= ~bits;
181 + writel(reg_value, addr);
183 + spin_unlock_irqrestore(&lock, flags);
188 +static void sunxi_ehci_disable(struct sunxi_ehci_hcd *sunxi_ehci)
190 + regulator_disable(sunxi_ehci->vbus_reg);
192 + sunxi_usb_passby(sunxi_ehci, !SUNXI_USB_PASSBY_EN);
194 + clk_disable_unprepare(sunxi_ehci->ahb_ehci_clk);
195 + clk_disable_unprepare(sunxi_ehci->phy_clk);
197 + reset_control_assert(sunxi_ehci->reset);
200 +static int sunxi_ehci_enable(struct sunxi_ehci_hcd *sunxi_ehci)
204 + ret = clk_prepare_enable(sunxi_ehci->phy_clk);
208 + ret = reset_control_deassert(sunxi_ehci->reset);
212 + ret = clk_prepare_enable(sunxi_ehci->ahb_ehci_clk);
216 + sunxi_usb_phy_init(sunxi_ehci);
218 + sunxi_usb_passby(sunxi_ehci, SUNXI_USB_PASSBY_EN);
220 + ret = regulator_enable(sunxi_ehci->vbus_reg);
227 + clk_disable_unprepare(sunxi_ehci->ahb_ehci_clk);
229 + reset_control_assert(sunxi_ehci->reset);
231 + clk_disable_unprepare(sunxi_ehci->phy_clk);
237 +static int sunxi_ehci_suspend(struct device *dev)
239 + struct sunxi_ehci_hcd *sunxi_ehci = NULL;
240 + struct usb_hcd *hcd = dev_get_drvdata(dev);
243 + bool do_wakeup = device_may_wakeup(dev);
245 + ret = ehci_suspend(hcd, do_wakeup);
247 + sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
249 + sunxi_ehci_disable(sunxi_ehci);
254 +static int sunxi_ehci_resume(struct device *dev)
256 + struct sunxi_ehci_hcd *sunxi_ehci = NULL;
257 + struct usb_hcd *hcd = dev_get_drvdata(dev);
260 + sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
262 + ret = sunxi_ehci_enable(sunxi_ehci);
266 + return ehci_resume(hcd, false);
270 +static const struct dev_pm_ops sunxi_ehci_pmops = {
271 + .suspend = sunxi_ehci_suspend,
272 + .resume = sunxi_ehci_resume,
275 +#define SUNXI_EHCI_PMOPS (&sunxi_ehci_pmops)
276 +#else /* !CONFIG_PM */
277 +#define SUNXI_EHCI_PMOPS NULL
278 +#endif /* CONFIG_PM */
280 +static const struct ehci_driver_overrides sunxi_overrides __initconst = {
282 + .extra_priv_size = sizeof(struct sunxi_ehci_hcd),
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;
289 +static int sunxi_ehci_init(struct platform_device *pdev, struct usb_hcd *hcd,
290 + struct sunxi_ehci_hcd *sunxi_ehci)
292 + void __iomem *ehci_regs = NULL;
293 + struct resource *res = NULL;
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;
300 + dev_info(&pdev->dev, "no USB VBUS power supply found\n");
303 + sunxi_ehci->id = of_alias_get_id(pdev->dev.of_node, "ehci");
304 + if (sunxi_ehci->id < 0)
305 + return sunxi_ehci->id;
307 + /* FIXME: should res be freed on some failure? */
308 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
310 + dev_err(&pdev->dev, "failed to get I/O memory\n");
313 + ehci_regs = devm_ioremap_resource(&pdev->dev, res);
314 + if (IS_ERR(ehci_regs))
315 + return PTR_ERR(ehci_regs);
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;
322 + res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
324 + dev_err(&pdev->dev, "failed to get I/O memory\n");
327 + sunxi_ehci->pmuirq = devm_ioremap_resource(&pdev->dev, res);
328 + if (IS_ERR(sunxi_ehci->pmuirq))
329 + return PTR_ERR(sunxi_ehci->pmuirq);
331 + res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
333 + dev_err(&pdev->dev, "failed to get I/O memory\n");
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.
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);
347 + sunxi_ehci->irq = platform_get_irq(pdev, 0);
348 + if (!sunxi_ehci->irq) {
349 + dev_err(&pdev->dev, "failed to get IRQ\n");
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);
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);
364 + sunxi_ehci->reset = reset_control_get(&pdev->dev, "ehci_reset");
365 + if (IS_ERR(sunxi_ehci->reset))
367 + dev_err(&pdev->dev, "failed to get ehci_reset reset line\n");
368 + return PTR_ERR(sunxi_ehci->reset);
374 +static int sunxi_ehci_probe(struct platform_device *pdev)
376 + struct sunxi_ehci_hcd *sunxi_ehci = NULL;
377 + struct usb_hcd *hcd = NULL;
380 + if (pdev->num_resources != 4) {
381 + dev_err(&pdev->dev, "invalid number of resources: %i\n",
382 + pdev->num_resources);
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");
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);
399 + hcd = usb_create_hcd(&sunxi_ehci_hc_driver, &pdev->dev,
400 + dev_name(&pdev->dev));
402 + dev_err(&pdev->dev, "unable to create HCD\n");
406 + platform_set_drvdata(pdev, hcd);
408 + sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
409 + ret = sunxi_ehci_init(pdev, hcd, sunxi_ehci);
413 + ret = sunxi_ehci_enable(sunxi_ehci);
417 + ret = usb_add_hcd(hcd, sunxi_ehci->irq, IRQF_SHARED | IRQF_DISABLED);
419 + dev_err(&pdev->dev, "failed to add USB HCD\n");
426 + sunxi_ehci_disable(sunxi_ehci);
433 +static int sunxi_ehci_remove(struct platform_device *pdev)
435 + struct usb_hcd *hcd = platform_get_drvdata(pdev);
436 + struct sunxi_ehci_hcd *sunxi_ehci = NULL;
438 + sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
440 + usb_remove_hcd(hcd);
442 + sunxi_ehci_disable(sunxi_ehci);
449 +static void sunxi_ehci_shutdown(struct platform_device *pdev)
451 + struct usb_hcd *hcd = platform_get_drvdata(pdev);
452 + struct sunxi_ehci_hcd *sunxi_ehci = NULL;
454 + sunxi_ehci = (struct sunxi_ehci_hcd *)hcd_to_ehci(hcd)->priv;
456 + usb_hcd_platform_shutdown(pdev);
458 + sunxi_ehci_disable(sunxi_ehci);
461 +static const struct of_device_id ehci_of_match[] = {
462 + {.compatible = "allwinner,sunxi-ehci"},
466 +static struct platform_driver ehci_sunxi_driver = {
468 + .of_match_table = ehci_of_match,
470 + .pm = SUNXI_EHCI_PMOPS,
472 + .probe = sunxi_ehci_probe,
473 + .remove = sunxi_ehci_remove,
474 + .shutdown = sunxi_ehci_shutdown,
477 +static int __init sunxi_ehci_init_module(void)
479 + if (usb_disabled())
482 + pr_info(DRV_NAME ": " DRV_DESC "\n");
484 + ehci_init_driver(&sunxi_ehci_hc_driver, &sunxi_overrides);
486 + return platform_driver_register(&ehci_sunxi_driver);
488 +module_init(sunxi_ehci_init_module);
490 +static void __exit sunxi_ehci_exit_module(void)
492 + platform_driver_unregister(&ehci_sunxi_driver);
494 +module_exit(sunxi_ehci_exit_module);
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>");