1 From 744543c599c420bcddca08cd2e2713b82a008328 Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Wed, 8 Jul 2015 16:41:38 +0200
4 Subject: [PATCH] usb: musb: sunxi: Add support for the Allwinner sunxi musb
7 This is based on initial code to get the Allwinner sunxi musb controller
8 supported by Chen-Yu Tsai and Roman Byshko.
10 This adds support for the Allwinner sunxi musb controller in both host only
11 and otg mode. Peripheral only mode is not supported, as no boards use that.
13 This has been tested on a cubietruck (A20 SoC) and an UTOO P66 tablet
14 (A13 SoC) with a variety of devices in host mode and with the g_serial gadget
15 driver in peripheral mode, plugging otg / host cables in/out a lot of times
16 in all possible imaginable plug orders.
18 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
19 Signed-off-by: Felipe Balbi <balbi@ti.com>
21 .../bindings/usb/allwinner,sun4i-a10-musb.txt | 27 +
22 drivers/usb/musb/Kconfig | 13 +-
23 drivers/usb/musb/Makefile | 1 +
24 drivers/usb/musb/sunxi.c | 703 +++++++++++++++++++++
25 4 files changed, 743 insertions(+), 1 deletion(-)
26 create mode 100644 Documentation/devicetree/bindings/usb/allwinner,sun4i-a10-musb.txt
27 create mode 100644 drivers/usb/musb/sunxi.c
30 +++ b/Documentation/devicetree/bindings/usb/allwinner,sun4i-a10-musb.txt
32 +Allwinner sun4i A10 musb DRC/OTG controller
33 +-------------------------------------------
36 + - compatible : "allwinner,sun4i-a10-musb"
37 + - reg : mmio address range of the musb controller
38 + - clocks : clock specifier for the musb controller ahb gate clock
39 + - interrupts : interrupt to which the musb controller is connected
40 + - interrupt-names : must be "mc"
41 + - phys : phy specifier for the otg phy
42 + - phy-names : must be "usb"
43 + - dr_mode : Dual-Role mode must be "host" or "otg"
44 + - extcon : extcon specifier for the otg phy
48 + usb_otg: usb@01c13000 {
49 + compatible = "allwinner,sun4i-a10-musb";
50 + reg = <0x01c13000 0x0400>;
51 + clocks = <&ahb_gates 0>;
53 + interrupt-names = "mc";
56 + extcon = <&usbphy 0>;
57 + status = "disabled";
59 --- a/drivers/usb/musb/Kconfig
60 +++ b/drivers/usb/musb/Kconfig
63 # (M)HDRC = (Multipoint) Highspeed Dual-Role Controller
65 - tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)'
66 + tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, AW, ...)'
67 depends on (USB || USB_GADGET)
69 Say Y here if your system has a dual role high speed USB
70 @@ -20,6 +20,8 @@ config USB_MUSB_HDRC
71 Analog Devices parts using this IP include Blackfin BF54x,
74 + Allwinner SoCs using this IP include A10, A13, A20, ...
76 If you do not know what this is, please say N.
78 To compile this driver as a module, choose M here; the
79 @@ -60,6 +62,15 @@ endchoice
81 comment "Platform Glue Layer"
83 +config USB_MUSB_SUNXI
84 + tristate "Allwinner (sunxi)"
85 + depends on ARCH_SUNXI
86 + depends on NOP_USB_XCEIV
87 + depends on PHY_SUN4I_USB
89 + depends on GENERIC_PHY
92 config USB_MUSB_DAVINCI
94 depends on ARCH_DAVINCI_DMx
95 --- a/drivers/usb/musb/Makefile
96 +++ b/drivers/usb/musb/Makefile
97 @@ -20,6 +20,7 @@ obj-$(CONFIG_USB_MUSB_DA8XX) += da8xx.
98 obj-$(CONFIG_USB_MUSB_BLACKFIN) += blackfin.o
99 obj-$(CONFIG_USB_MUSB_UX500) += ux500.o
100 obj-$(CONFIG_USB_MUSB_JZ4740) += jz4740.o
101 +obj-$(CONFIG_USB_MUSB_SUNXI) += sunxi.o
104 obj-$(CONFIG_USB_MUSB_AM335X_CHILD) += musb_am335x.o
106 +++ b/drivers/usb/musb/sunxi.c
109 + * Allwinner sun4i MUSB Glue Layer
111 + * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
113 + * Based on code from
114 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
116 + * This program is free software; you can redistribute it and/or modify
117 + * it under the terms of the GNU General Public License as published by
118 + * the Free Software Foundation; either version 2 of the License, or
119 + * (at your option) any later version.
121 + * This program is distributed in the hope that it will be useful,
122 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
123 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124 + * GNU General Public License for more details.
127 +#include <linux/clk.h>
128 +#include <linux/err.h>
129 +#include <linux/extcon.h>
130 +#include <linux/io.h>
131 +#include <linux/kernel.h>
132 +#include <linux/module.h>
133 +#include <linux/of.h>
134 +#include <linux/phy/phy-sun4i-usb.h>
135 +#include <linux/platform_device.h>
136 +#include <linux/soc/sunxi/sunxi_sram.h>
137 +#include <linux/usb/musb.h>
138 +#include <linux/usb/of.h>
139 +#include <linux/usb/usb_phy_generic.h>
140 +#include <linux/workqueue.h>
141 +#include "musb_core.h"
144 + * Register offsets, note sunxi musb has a different layout then most
145 + * musb implementations, we translate the layout in musb_readb & friends.
147 +#define SUNXI_MUSB_POWER 0x0040
148 +#define SUNXI_MUSB_DEVCTL 0x0041
149 +#define SUNXI_MUSB_INDEX 0x0042
150 +#define SUNXI_MUSB_VEND0 0x0043
151 +#define SUNXI_MUSB_INTRTX 0x0044
152 +#define SUNXI_MUSB_INTRRX 0x0046
153 +#define SUNXI_MUSB_INTRTXE 0x0048
154 +#define SUNXI_MUSB_INTRRXE 0x004a
155 +#define SUNXI_MUSB_INTRUSB 0x004c
156 +#define SUNXI_MUSB_INTRUSBE 0x0050
157 +#define SUNXI_MUSB_FRAME 0x0054
158 +#define SUNXI_MUSB_TXFIFOSZ 0x0090
159 +#define SUNXI_MUSB_TXFIFOADD 0x0092
160 +#define SUNXI_MUSB_RXFIFOSZ 0x0094
161 +#define SUNXI_MUSB_RXFIFOADD 0x0096
162 +#define SUNXI_MUSB_FADDR 0x0098
163 +#define SUNXI_MUSB_TXFUNCADDR 0x0098
164 +#define SUNXI_MUSB_TXHUBADDR 0x009a
165 +#define SUNXI_MUSB_TXHUBPORT 0x009b
166 +#define SUNXI_MUSB_RXFUNCADDR 0x009c
167 +#define SUNXI_MUSB_RXHUBADDR 0x009e
168 +#define SUNXI_MUSB_RXHUBPORT 0x009f
169 +#define SUNXI_MUSB_CONFIGDATA 0x00c0
172 +#define SUNXI_MUSB_VEND0_PIO_MODE 0
175 +#define SUNXI_MUSB_FL_ENABLED 0
176 +#define SUNXI_MUSB_FL_HOSTMODE 1
177 +#define SUNXI_MUSB_FL_HOSTMODE_PEND 2
178 +#define SUNXI_MUSB_FL_VBUS_ON 3
179 +#define SUNXI_MUSB_FL_PHY_ON 4
181 +/* Our read/write methods need access and do not get passed in a musb ref :| */
182 +static struct musb *sunxi_musb;
185 + struct device *dev;
186 + struct platform_device *musb;
189 + struct platform_device *usb_phy;
190 + struct usb_phy *xceiv;
191 + unsigned long flags;
192 + struct work_struct work;
193 + struct extcon_dev *extcon;
194 + struct notifier_block host_nb;
197 +/* phy_power_on / off may sleep, so we use a workqueue */
198 +static void sunxi_musb_work(struct work_struct *work)
200 + struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
201 + bool vbus_on, phy_on;
203 + if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
206 + if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
207 + struct musb *musb = platform_get_drvdata(glue->musb);
208 + unsigned long flags;
211 + spin_lock_irqsave(&musb->lock, flags);
213 + devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
214 + if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
215 + set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
216 + musb->xceiv->otg->default_a = 1;
217 + musb->xceiv->otg->state = OTG_STATE_A_IDLE;
218 + MUSB_HST_MODE(musb);
219 + devctl |= MUSB_DEVCTL_SESSION;
221 + clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
222 + musb->xceiv->otg->default_a = 0;
223 + musb->xceiv->otg->state = OTG_STATE_B_IDLE;
224 + MUSB_DEV_MODE(musb);
225 + devctl &= ~MUSB_DEVCTL_SESSION;
227 + writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
229 + spin_unlock_irqrestore(&musb->lock, flags);
232 + vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
233 + phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
235 + if (phy_on != vbus_on) {
237 + phy_power_on(glue->phy);
238 + set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
240 + phy_power_off(glue->phy);
241 + clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
246 +static void sunxi_musb_set_vbus(struct musb *musb, int is_on)
248 + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
251 + set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
253 + clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
255 + schedule_work(&glue->work);
258 +static void sunxi_musb_pre_root_reset_end(struct musb *musb)
260 + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
262 + sun4i_usb_phy_set_squelch_detect(glue->phy, false);
265 +static void sunxi_musb_post_root_reset_end(struct musb *musb)
267 + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
269 + sun4i_usb_phy_set_squelch_detect(glue->phy, true);
272 +static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
274 + struct musb *musb = __hci;
275 + unsigned long flags;
277 + spin_lock_irqsave(&musb->lock, flags);
279 + musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
281 + writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
284 + * sunxi musb often signals babble on low / full speed device
285 + * disconnect, without ever raising MUSB_INTR_DISCONNECT, since
286 + * normally babble never happens treat it as disconnect.
288 + if ((musb->int_usb & MUSB_INTR_BABBLE) && is_host_active(musb)) {
289 + musb->int_usb &= ~MUSB_INTR_BABBLE;
290 + musb->int_usb |= MUSB_INTR_DISCONNECT;
293 + if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
294 + /* ep0 FADDR must be 0 when (re)entering peripheral mode */
295 + musb_ep_select(musb->mregs, 0);
296 + musb_writeb(musb->mregs, MUSB_FADDR, 0);
299 + musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
301 + writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
303 + musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
305 + writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
307 + musb_interrupt(musb);
309 + spin_unlock_irqrestore(&musb->lock, flags);
311 + return IRQ_HANDLED;
314 +static int sunxi_musb_host_notifier(struct notifier_block *nb,
315 + unsigned long event, void *ptr)
317 + struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
320 + set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
322 + clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
324 + set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
325 + schedule_work(&glue->work);
327 + return NOTIFY_DONE;
330 +static int sunxi_musb_init(struct musb *musb)
332 + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
336 + musb->phy = glue->phy;
337 + musb->xceiv = glue->xceiv;
339 + ret = sunxi_sram_claim(musb->controller->parent);
343 + ret = clk_prepare_enable(glue->clk);
345 + goto error_sram_release;
347 + writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
349 + /* Register notifier before calling phy_init() */
350 + if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) {
351 + ret = extcon_register_notifier(glue->extcon, EXTCON_USB_HOST,
354 + goto error_clk_disable;
357 + ret = phy_init(glue->phy);
359 + goto error_unregister_notifier;
361 + if (musb->port_mode == MUSB_PORT_MODE_HOST) {
362 + ret = phy_power_on(glue->phy);
364 + goto error_phy_exit;
365 + set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
366 + /* Stop musb work from turning vbus off again */
367 + set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
370 + musb->isr = sunxi_musb_interrupt;
372 + /* Stop the musb-core from doing runtime pm (not supported on sunxi) */
373 + pm_runtime_get(musb->controller);
378 + phy_exit(glue->phy);
379 +error_unregister_notifier:
380 + if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
381 + extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
384 + clk_disable_unprepare(glue->clk);
386 + sunxi_sram_release(musb->controller->parent);
390 +static int sunxi_musb_exit(struct musb *musb)
392 + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
394 + pm_runtime_put(musb->controller);
396 + cancel_work_sync(&glue->work);
397 + if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
398 + phy_power_off(glue->phy);
400 + phy_exit(glue->phy);
402 + if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
403 + extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
406 + clk_disable_unprepare(glue->clk);
407 + sunxi_sram_release(musb->controller->parent);
412 +static void sunxi_musb_enable(struct musb *musb)
414 + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
416 + /* musb_core does not call us in a balanced manner */
417 + if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
420 + schedule_work(&glue->work);
423 +static void sunxi_musb_disable(struct musb *musb)
425 + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
427 + clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
431 + * sunxi musb register layout
432 + * 0x00 - 0x17 fifo regs, 1 long per fifo
433 + * 0x40 - 0x57 generic control regs (power - frame)
434 + * 0x80 - 0x8f ep control regs (addressed through hw_ep->regs, indexed)
435 + * 0x90 - 0x97 fifo control regs (indexed)
436 + * 0x98 - 0x9f multipoint / busctl regs (indexed)
437 + * 0xc0 configdata reg
440 +static u32 sunxi_musb_fifo_offset(u8 epnum)
442 + return (epnum * 4);
445 +static u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
447 + WARN_ONCE(offset != 0,
448 + "sunxi_musb_ep_offset called with non 0 offset\n");
450 + return 0x80; /* indexed, so ignore epnum */
453 +static u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
455 + return SUNXI_MUSB_TXFUNCADDR + offset;
458 +static u8 sunxi_musb_readb(const void __iomem *addr, unsigned offset)
460 + if (addr == sunxi_musb->mregs) {
461 + /* generic control or fifo control reg access */
464 + return readb(addr + SUNXI_MUSB_FADDR);
466 + return readb(addr + SUNXI_MUSB_POWER);
468 + return readb(addr + SUNXI_MUSB_INTRUSB);
469 + case MUSB_INTRUSBE:
470 + return readb(addr + SUNXI_MUSB_INTRUSBE);
472 + return readb(addr + SUNXI_MUSB_INDEX);
473 + case MUSB_TESTMODE:
474 + return 0; /* No testmode on sunxi */
476 + return readb(addr + SUNXI_MUSB_DEVCTL);
477 + case MUSB_TXFIFOSZ:
478 + return readb(addr + SUNXI_MUSB_TXFIFOSZ);
479 + case MUSB_RXFIFOSZ:
480 + return readb(addr + SUNXI_MUSB_RXFIFOSZ);
481 + case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
482 + return readb(addr + SUNXI_MUSB_CONFIGDATA);
483 + /* Offset for these is fixed by sunxi_musb_busctl_offset() */
484 + case SUNXI_MUSB_TXFUNCADDR:
485 + case SUNXI_MUSB_TXHUBADDR:
486 + case SUNXI_MUSB_TXHUBPORT:
487 + case SUNXI_MUSB_RXFUNCADDR:
488 + case SUNXI_MUSB_RXHUBADDR:
489 + case SUNXI_MUSB_RXHUBPORT:
490 + /* multipoint / busctl reg access */
491 + return readb(addr + offset);
493 + dev_err(sunxi_musb->controller->parent,
494 + "Error unknown readb offset %u\n", offset);
497 + } else if (addr == (sunxi_musb->mregs + 0x80)) {
498 + /* ep control reg access */
499 + /* sunxi has a 2 byte hole before the txtype register */
500 + if (offset >= MUSB_TXTYPE)
502 + return readb(addr + offset);
505 + dev_err(sunxi_musb->controller->parent,
506 + "Error unknown readb at 0x%x bytes offset\n",
507 + (int)(addr - sunxi_musb->mregs));
511 +static void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
513 + if (addr == sunxi_musb->mregs) {
514 + /* generic control or fifo control reg access */
517 + return writeb(data, addr + SUNXI_MUSB_FADDR);
519 + return writeb(data, addr + SUNXI_MUSB_POWER);
521 + return writeb(data, addr + SUNXI_MUSB_INTRUSB);
522 + case MUSB_INTRUSBE:
523 + return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
525 + return writeb(data, addr + SUNXI_MUSB_INDEX);
526 + case MUSB_TESTMODE:
528 + dev_warn(sunxi_musb->controller->parent,
529 + "sunxi-musb does not have testmode\n");
532 + return writeb(data, addr + SUNXI_MUSB_DEVCTL);
533 + case MUSB_TXFIFOSZ:
534 + return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
535 + case MUSB_RXFIFOSZ:
536 + return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
537 + /* Offset for these is fixed by sunxi_musb_busctl_offset() */
538 + case SUNXI_MUSB_TXFUNCADDR:
539 + case SUNXI_MUSB_TXHUBADDR:
540 + case SUNXI_MUSB_TXHUBPORT:
541 + case SUNXI_MUSB_RXFUNCADDR:
542 + case SUNXI_MUSB_RXHUBADDR:
543 + case SUNXI_MUSB_RXHUBPORT:
544 + /* multipoint / busctl reg access */
545 + return writeb(data, addr + offset);
547 + dev_err(sunxi_musb->controller->parent,
548 + "Error unknown writeb offset %u\n", offset);
551 + } else if (addr == (sunxi_musb->mregs + 0x80)) {
552 + /* ep control reg access */
553 + if (offset >= MUSB_TXTYPE)
555 + return writeb(data, addr + offset);
558 + dev_err(sunxi_musb->controller->parent,
559 + "Error unknown writeb at 0x%x bytes offset\n",
560 + (int)(addr - sunxi_musb->mregs));
563 +static u16 sunxi_musb_readw(const void __iomem *addr, unsigned offset)
565 + if (addr == sunxi_musb->mregs) {
566 + /* generic control or fifo control reg access */
569 + return readw(addr + SUNXI_MUSB_INTRTX);
571 + return readw(addr + SUNXI_MUSB_INTRRX);
573 + return readw(addr + SUNXI_MUSB_INTRTXE);
575 + return readw(addr + SUNXI_MUSB_INTRRXE);
577 + return readw(addr + SUNXI_MUSB_FRAME);
578 + case MUSB_TXFIFOADD:
579 + return readw(addr + SUNXI_MUSB_TXFIFOADD);
580 + case MUSB_RXFIFOADD:
581 + return readw(addr + SUNXI_MUSB_RXFIFOADD);
583 + return 0; /* sunxi musb version is not known */
585 + dev_err(sunxi_musb->controller->parent,
586 + "Error unknown readw offset %u\n", offset);
589 + } else if (addr == (sunxi_musb->mregs + 0x80)) {
590 + /* ep control reg access */
591 + return readw(addr + offset);
594 + dev_err(sunxi_musb->controller->parent,
595 + "Error unknown readw at 0x%x bytes offset\n",
596 + (int)(addr - sunxi_musb->mregs));
600 +static void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
602 + if (addr == sunxi_musb->mregs) {
603 + /* generic control or fifo control reg access */
606 + return writew(data, addr + SUNXI_MUSB_INTRTX);
608 + return writew(data, addr + SUNXI_MUSB_INTRRX);
610 + return writew(data, addr + SUNXI_MUSB_INTRTXE);
612 + return writew(data, addr + SUNXI_MUSB_INTRRXE);
614 + return writew(data, addr + SUNXI_MUSB_FRAME);
615 + case MUSB_TXFIFOADD:
616 + return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
617 + case MUSB_RXFIFOADD:
618 + return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
620 + dev_err(sunxi_musb->controller->parent,
621 + "Error unknown writew offset %u\n", offset);
624 + } else if (addr == (sunxi_musb->mregs + 0x80)) {
625 + /* ep control reg access */
626 + return writew(data, addr + offset);
629 + dev_err(sunxi_musb->controller->parent,
630 + "Error unknown writew at 0x%x bytes offset\n",
631 + (int)(addr - sunxi_musb->mregs));
634 +static const struct musb_platform_ops sunxi_musb_ops = {
635 + .quirks = MUSB_INDEXED_EP,
636 + .init = sunxi_musb_init,
637 + .exit = sunxi_musb_exit,
638 + .enable = sunxi_musb_enable,
639 + .disable = sunxi_musb_disable,
640 + .fifo_offset = sunxi_musb_fifo_offset,
641 + .ep_offset = sunxi_musb_ep_offset,
642 + .busctl_offset = sunxi_musb_busctl_offset,
643 + .readb = sunxi_musb_readb,
644 + .writeb = sunxi_musb_writeb,
645 + .readw = sunxi_musb_readw,
646 + .writew = sunxi_musb_writew,
647 + .set_vbus = sunxi_musb_set_vbus,
648 + .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
649 + .post_root_reset_end = sunxi_musb_post_root_reset_end,
652 +/* Allwinner OTG supports up to 5 endpoints */
653 +#define SUNXI_MUSB_MAX_EP_NUM 6
654 +#define SUNXI_MUSB_RAM_BITS 11
656 +static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
657 + MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
658 + MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
659 + MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
660 + MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
661 + MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
662 + MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
663 + MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
664 + MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
665 + MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
666 + MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
669 +static struct musb_hdrc_config sunxi_musb_hdrc_config = {
670 + .fifo_cfg = sunxi_musb_mode_cfg,
671 + .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
672 + .multipoint = true,
675 + .num_eps = SUNXI_MUSB_MAX_EP_NUM,
676 + .ram_bits = SUNXI_MUSB_RAM_BITS,
680 +static int sunxi_musb_probe(struct platform_device *pdev)
682 + struct musb_hdrc_platform_data pdata;
683 + struct platform_device_info pinfo;
684 + struct sunxi_glue *glue;
685 + struct device_node *np = pdev->dev.of_node;
689 + dev_err(&pdev->dev, "Error no device tree node found\n");
693 + glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
697 + memset(&pdata, 0, sizeof(pdata));
698 + switch (of_usb_get_dr_mode(np)) {
699 +#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
700 + case USB_DR_MODE_HOST:
701 + pdata.mode = MUSB_PORT_MODE_HOST;
704 +#ifdef CONFIG_USB_MUSB_DUAL_ROLE
705 + case USB_DR_MODE_OTG:
706 + glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
707 + if (IS_ERR(glue->extcon)) {
708 + if (PTR_ERR(glue->extcon) == -EPROBE_DEFER)
709 + return -EPROBE_DEFER;
710 + dev_err(&pdev->dev, "Invalid or missing extcon\n");
711 + return PTR_ERR(glue->extcon);
713 + pdata.mode = MUSB_PORT_MODE_DUAL_ROLE;
717 + dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
720 + pdata.platform_ops = &sunxi_musb_ops;
721 + pdata.config = &sunxi_musb_hdrc_config;
723 + glue->dev = &pdev->dev;
724 + INIT_WORK(&glue->work, sunxi_musb_work);
725 + glue->host_nb.notifier_call = sunxi_musb_host_notifier;
727 + glue->clk = devm_clk_get(&pdev->dev, NULL);
728 + if (IS_ERR(glue->clk)) {
729 + dev_err(&pdev->dev, "Error getting clock: %ld\n",
730 + PTR_ERR(glue->clk));
731 + return PTR_ERR(glue->clk);
734 + glue->phy = devm_phy_get(&pdev->dev, "usb");
735 + if (IS_ERR(glue->phy)) {
736 + if (PTR_ERR(glue->phy) == -EPROBE_DEFER)
737 + return -EPROBE_DEFER;
738 + dev_err(&pdev->dev, "Error getting phy %ld\n",
739 + PTR_ERR(glue->phy));
740 + return PTR_ERR(glue->phy);
743 + glue->usb_phy = usb_phy_generic_register();
744 + if (IS_ERR(glue->usb_phy)) {
745 + dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
746 + PTR_ERR(glue->usb_phy));
747 + return PTR_ERR(glue->usb_phy);
750 + glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
751 + if (IS_ERR(glue->xceiv)) {
752 + ret = PTR_ERR(glue->xceiv);
753 + dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
754 + goto err_unregister_usb_phy;
757 + platform_set_drvdata(pdev, glue);
759 + memset(&pinfo, 0, sizeof(pinfo));
760 + pinfo.name = "musb-hdrc";
761 + pinfo.id = PLATFORM_DEVID_AUTO;
762 + pinfo.parent = &pdev->dev;
763 + pinfo.res = pdev->resource;
764 + pinfo.num_res = pdev->num_resources;
765 + pinfo.data = &pdata;
766 + pinfo.size_data = sizeof(pdata);
768 + glue->musb = platform_device_register_full(&pinfo);
769 + if (IS_ERR(glue->musb)) {
770 + ret = PTR_ERR(glue->musb);
771 + dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
772 + goto err_unregister_usb_phy;
777 +err_unregister_usb_phy:
778 + usb_phy_generic_unregister(glue->usb_phy);
782 +static int sunxi_musb_remove(struct platform_device *pdev)
784 + struct sunxi_glue *glue = platform_get_drvdata(pdev);
785 + struct platform_device *usb_phy = glue->usb_phy;
787 + platform_device_unregister(glue->musb); /* Frees glue ! */
788 + usb_phy_generic_unregister(usb_phy);
793 +static const struct of_device_id sunxi_musb_match[] = {
794 + { .compatible = "allwinner,sun4i-a10-musb", },
798 +static struct platform_driver sunxi_musb_driver = {
799 + .probe = sunxi_musb_probe,
800 + .remove = sunxi_musb_remove,
802 + .name = "musb-sunxi",
803 + .of_match_table = sunxi_musb_match,
806 +module_platform_driver(sunxi_musb_driver);
808 +MODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
809 +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
810 +MODULE_LICENSE("GPL v2");