sunxi: add support for 4.1
[openwrt.git] / target / linux / sunxi / patches-4.1 / 170-musb-add-driver.patch
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
5  controller
6
7 This is based on initial code to get the Allwinner sunxi musb controller
8 supported by Chen-Yu Tsai and Roman Byshko.
9
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.
12
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.
17
18 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
19 Signed-off-by: Felipe Balbi <balbi@ti.com>
20 ---
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
28
29 diff --git a/Documentation/devicetree/bindings/usb/allwinner,sun4i-a10-musb.txt b/Documentation/devicetree/bindings/usb/allwinner,sun4i-a10-musb.txt
30 new file mode 100644
31 index 0000000..9254a6c
32 --- /dev/null
33 +++ b/Documentation/devicetree/bindings/usb/allwinner,sun4i-a10-musb.txt
34 @@ -0,0 +1,27 @@
35 +Allwinner sun4i A10 musb DRC/OTG controller
36 +-------------------------------------------
37 +
38 +Required properties:
39 + - compatible      : "allwinner,sun4i-a10-musb"
40 + - reg             : mmio address range of the musb controller
41 + - clocks          : clock specifier for the musb controller ahb gate clock
42 + - interrupts      : interrupt to which the musb controller is connected
43 + - interrupt-names : must be "mc"
44 + - phys            : phy specifier for the otg phy
45 + - phy-names       : must be "usb"
46 + - dr_mode         : Dual-Role mode must be "host" or "otg"
47 + - extcon          : extcon specifier for the otg phy
48 +
49 +Example:
50 +
51 +       usb_otg: usb@01c13000 {
52 +               compatible = "allwinner,sun4i-a10-musb";
53 +               reg = <0x01c13000 0x0400>;
54 +               clocks = <&ahb_gates 0>;
55 +               interrupts = <38>;
56 +               interrupt-names = "mc";
57 +               phys = <&usbphy 0>;
58 +               phy-names = "usb";
59 +               extcon = <&usbphy 0>;
60 +               status = "disabled";
61 +       };
62 diff --git a/drivers/usb/musb/Kconfig b/drivers/usb/musb/Kconfig
63 index 39db8b6..37081ed 100644
64 --- a/drivers/usb/musb/Kconfig
65 +++ b/drivers/usb/musb/Kconfig
66 @@ -5,7 +5,7 @@
67  
68  # (M)HDRC = (Multipoint) Highspeed Dual-Role Controller
69  config USB_MUSB_HDRC
70 -       tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)'
71 +       tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, AW, ...)'
72         depends on (USB || USB_GADGET)
73         help
74           Say Y here if your system has a dual role high speed USB
75 @@ -20,6 +20,8 @@ config USB_MUSB_HDRC
76           Analog Devices parts using this IP include Blackfin BF54x,
77           BF525 and BF527.
78  
79 +         Allwinner SoCs using this IP include A10, A13, A20, ...
80 +
81           If you do not know what this is, please say N.
82  
83           To compile this driver as a module, choose M here; the
84 @@ -60,6 +62,15 @@ endchoice
85  
86  comment "Platform Glue Layer"
87  
88 +config USB_MUSB_SUNXI
89 +       tristate "Allwinner (sunxi)"
90 +       depends on ARCH_SUNXI
91 +       depends on NOP_USB_XCEIV
92 +       depends on PHY_SUN4I_USB
93 +       depends on EXTCON
94 +       depends on GENERIC_PHY
95 +       select SUNXI_SRAM
96 +
97  config USB_MUSB_DAVINCI
98         tristate "DaVinci"
99         depends on ARCH_DAVINCI_DMx
100 diff --git a/drivers/usb/musb/Makefile b/drivers/usb/musb/Makefile
101 index ba49501..f95befe 100644
102 --- a/drivers/usb/musb/Makefile
103 +++ b/drivers/usb/musb/Makefile
104 @@ -20,6 +20,7 @@ obj-$(CONFIG_USB_MUSB_DA8XX)                  += da8xx.o
105  obj-$(CONFIG_USB_MUSB_BLACKFIN)                        += blackfin.o
106  obj-$(CONFIG_USB_MUSB_UX500)                   += ux500.o
107  obj-$(CONFIG_USB_MUSB_JZ4740)                  += jz4740.o
108 +obj-$(CONFIG_USB_MUSB_SUNXI)                   += sunxi.o
109  
110  
111  obj-$(CONFIG_USB_MUSB_AM335X_CHILD)            += musb_am335x.o
112 diff --git a/drivers/usb/musb/sunxi.c b/drivers/usb/musb/sunxi.c
113 new file mode 100644
114 index 0000000..00d7248
115 --- /dev/null
116 +++ b/drivers/usb/musb/sunxi.c
117 @@ -0,0 +1,703 @@
118 +/*
119 + * Allwinner sun4i MUSB Glue Layer
120 + *
121 + * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
122 + *
123 + * Based on code from
124 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
125 + *
126 + * This program is free software; you can redistribute it and/or modify
127 + * it under the terms of the GNU General Public License as published by
128 + * the Free Software Foundation; either version 2 of the License, or
129 + * (at your option) any later version.
130 + *
131 + * This program is distributed in the hope that it will be useful,
132 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
133 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
134 + * GNU General Public License for more details.
135 + */
136 +
137 +#include <linux/clk.h>
138 +#include <linux/err.h>
139 +#include <linux/extcon.h>
140 +#include <linux/io.h>
141 +#include <linux/kernel.h>
142 +#include <linux/module.h>
143 +#include <linux/of.h>
144 +#include <linux/phy/phy-sun4i-usb.h>
145 +#include <linux/platform_device.h>
146 +#include <linux/soc/sunxi/sunxi_sram.h>
147 +#include <linux/usb/musb.h>
148 +#include <linux/usb/of.h>
149 +#include <linux/usb/usb_phy_generic.h>
150 +#include <linux/workqueue.h>
151 +#include "musb_core.h"
152 +
153 +/*
154 + * Register offsets, note sunxi musb has a different layout then most
155 + * musb implementations, we translate the layout in musb_readb & friends.
156 + */
157 +#define SUNXI_MUSB_POWER                       0x0040
158 +#define SUNXI_MUSB_DEVCTL                      0x0041
159 +#define SUNXI_MUSB_INDEX                       0x0042
160 +#define SUNXI_MUSB_VEND0                       0x0043
161 +#define SUNXI_MUSB_INTRTX                      0x0044
162 +#define SUNXI_MUSB_INTRRX                      0x0046
163 +#define SUNXI_MUSB_INTRTXE                     0x0048
164 +#define SUNXI_MUSB_INTRRXE                     0x004a
165 +#define SUNXI_MUSB_INTRUSB                     0x004c
166 +#define SUNXI_MUSB_INTRUSBE                    0x0050
167 +#define SUNXI_MUSB_FRAME                       0x0054
168 +#define SUNXI_MUSB_TXFIFOSZ                    0x0090
169 +#define SUNXI_MUSB_TXFIFOADD                   0x0092
170 +#define SUNXI_MUSB_RXFIFOSZ                    0x0094
171 +#define SUNXI_MUSB_RXFIFOADD                   0x0096
172 +#define SUNXI_MUSB_FADDR                       0x0098
173 +#define SUNXI_MUSB_TXFUNCADDR                  0x0098
174 +#define SUNXI_MUSB_TXHUBADDR                   0x009a
175 +#define SUNXI_MUSB_TXHUBPORT                   0x009b
176 +#define SUNXI_MUSB_RXFUNCADDR                  0x009c
177 +#define SUNXI_MUSB_RXHUBADDR                   0x009e
178 +#define SUNXI_MUSB_RXHUBPORT                   0x009f
179 +#define SUNXI_MUSB_CONFIGDATA                  0x00c0
180 +
181 +/* VEND0 bits */
182 +#define SUNXI_MUSB_VEND0_PIO_MODE              0
183 +
184 +/* flags */
185 +#define SUNXI_MUSB_FL_ENABLED                  0
186 +#define SUNXI_MUSB_FL_HOSTMODE                 1
187 +#define SUNXI_MUSB_FL_HOSTMODE_PEND            2
188 +#define SUNXI_MUSB_FL_VBUS_ON                  3
189 +#define SUNXI_MUSB_FL_PHY_ON                   4
190 +
191 +/* Our read/write methods need access and do not get passed in a musb ref :| */
192 +static struct musb *sunxi_musb;
193 +
194 +struct sunxi_glue {
195 +       struct device           *dev;
196 +       struct platform_device  *musb;
197 +       struct clk              *clk;
198 +       struct phy              *phy;
199 +       struct platform_device  *usb_phy;
200 +       struct usb_phy          *xceiv;
201 +       unsigned long           flags;
202 +       struct work_struct      work;
203 +       struct extcon_dev       *extcon;
204 +       struct notifier_block   host_nb;
205 +};
206 +
207 +/* phy_power_on / off may sleep, so we use a workqueue  */
208 +static void sunxi_musb_work(struct work_struct *work)
209 +{
210 +       struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
211 +       bool vbus_on, phy_on;
212 +
213 +       if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
214 +               return;
215 +
216 +       if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
217 +               struct musb *musb = platform_get_drvdata(glue->musb);
218 +               unsigned long flags;
219 +               u8 devctl;
220 +
221 +               spin_lock_irqsave(&musb->lock, flags);
222 +
223 +               devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
224 +               if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
225 +                       set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
226 +                       musb->xceiv->otg->default_a = 1;
227 +                       musb->xceiv->otg->state = OTG_STATE_A_IDLE;
228 +                       MUSB_HST_MODE(musb);
229 +                       devctl |= MUSB_DEVCTL_SESSION;
230 +               } else {
231 +                       clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
232 +                       musb->xceiv->otg->default_a = 0;
233 +                       musb->xceiv->otg->state = OTG_STATE_B_IDLE;
234 +                       MUSB_DEV_MODE(musb);
235 +                       devctl &= ~MUSB_DEVCTL_SESSION;
236 +               }
237 +               writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
238 +
239 +               spin_unlock_irqrestore(&musb->lock, flags);
240 +       }
241 +
242 +       vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
243 +       phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
244 +
245 +       if (phy_on != vbus_on) {
246 +               if (vbus_on) {
247 +                       phy_power_on(glue->phy);
248 +                       set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
249 +               } else {
250 +                       phy_power_off(glue->phy);
251 +                       clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
252 +               }
253 +       }
254 +}
255 +
256 +static void sunxi_musb_set_vbus(struct musb *musb, int is_on)
257 +{
258 +       struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
259 +
260 +       if (is_on)
261 +               set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
262 +       else
263 +               clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
264 +
265 +       schedule_work(&glue->work);
266 +}
267 +
268 +static void sunxi_musb_pre_root_reset_end(struct musb *musb)
269 +{
270 +       struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
271 +
272 +       sun4i_usb_phy_set_squelch_detect(glue->phy, false);
273 +}
274 +
275 +static void sunxi_musb_post_root_reset_end(struct musb *musb)
276 +{
277 +       struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
278 +
279 +       sun4i_usb_phy_set_squelch_detect(glue->phy, true);
280 +}
281 +
282 +static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
283 +{
284 +       struct musb *musb = __hci;
285 +       unsigned long flags;
286 +
287 +       spin_lock_irqsave(&musb->lock, flags);
288 +
289 +       musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
290 +       if (musb->int_usb)
291 +               writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
292 +
293 +       /*
294 +        * sunxi musb often signals babble on low / full speed device
295 +        * disconnect, without ever raising MUSB_INTR_DISCONNECT, since
296 +        * normally babble never happens treat it as disconnect.
297 +        */
298 +       if ((musb->int_usb & MUSB_INTR_BABBLE) && is_host_active(musb)) {
299 +               musb->int_usb &= ~MUSB_INTR_BABBLE;
300 +               musb->int_usb |= MUSB_INTR_DISCONNECT;
301 +       }
302 +
303 +       if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
304 +               /* ep0 FADDR must be 0 when (re)entering peripheral mode */
305 +               musb_ep_select(musb->mregs, 0);
306 +               musb_writeb(musb->mregs, MUSB_FADDR, 0);
307 +       }
308 +
309 +       musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
310 +       if (musb->int_tx)
311 +               writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
312 +
313 +       musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
314 +       if (musb->int_rx)
315 +               writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
316 +
317 +       musb_interrupt(musb);
318 +
319 +       spin_unlock_irqrestore(&musb->lock, flags);
320 +
321 +       return IRQ_HANDLED;
322 +}
323 +
324 +static int sunxi_musb_host_notifier(struct notifier_block *nb,
325 +                                   unsigned long event, void *ptr)
326 +{
327 +       struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
328 +
329 +       if (event)
330 +               set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
331 +       else
332 +               clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
333 +
334 +       set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
335 +       schedule_work(&glue->work);
336 +
337 +       return NOTIFY_DONE;
338 +}
339 +
340 +static int sunxi_musb_init(struct musb *musb)
341 +{
342 +       struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
343 +       int ret;
344 +
345 +       sunxi_musb = musb;
346 +       musb->phy = glue->phy;
347 +       musb->xceiv = glue->xceiv;
348 +
349 +       ret = sunxi_sram_claim(musb->controller->parent);
350 +       if (ret)
351 +               return ret;
352 +
353 +       ret = clk_prepare_enable(glue->clk);
354 +       if (ret)
355 +               goto error_sram_release;
356 +
357 +       writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
358 +
359 +       /* Register notifier before calling phy_init() */
360 +       if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) {
361 +               ret = extcon_register_notifier(glue->extcon, EXTCON_USB_HOST,
362 +                                              &glue->host_nb);
363 +               if (ret)
364 +                       goto error_clk_disable;
365 +       }
366 +
367 +       ret = phy_init(glue->phy);
368 +       if (ret)
369 +               goto error_unregister_notifier;
370 +
371 +       if (musb->port_mode == MUSB_PORT_MODE_HOST) {
372 +               ret = phy_power_on(glue->phy);
373 +               if (ret)
374 +                       goto error_phy_exit;
375 +               set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
376 +               /* Stop musb work from turning vbus off again */
377 +               set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
378 +       }
379 +
380 +       musb->isr = sunxi_musb_interrupt;
381 +
382 +       /* Stop the musb-core from doing runtime pm (not supported on sunxi) */
383 +       pm_runtime_get(musb->controller);
384 +
385 +       return 0;
386 +
387 +error_phy_exit:
388 +       phy_exit(glue->phy);
389 +error_unregister_notifier:
390 +       if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
391 +               extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
392 +                                          &glue->host_nb);
393 +error_clk_disable:
394 +       clk_disable_unprepare(glue->clk);
395 +error_sram_release:
396 +       sunxi_sram_release(musb->controller->parent);
397 +       return ret;
398 +}
399 +
400 +static int sunxi_musb_exit(struct musb *musb)
401 +{
402 +       struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
403 +
404 +       pm_runtime_put(musb->controller);
405 +
406 +       cancel_work_sync(&glue->work);
407 +       if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
408 +               phy_power_off(glue->phy);
409 +
410 +       phy_exit(glue->phy);
411 +
412 +       if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
413 +               extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
414 +                                          &glue->host_nb);
415 +
416 +       clk_disable_unprepare(glue->clk);
417 +       sunxi_sram_release(musb->controller->parent);
418 +
419 +       return 0;
420 +}
421 +
422 +static void sunxi_musb_enable(struct musb *musb)
423 +{
424 +       struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
425 +
426 +       /* musb_core does not call us in a balanced manner */
427 +       if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
428 +               return;
429 +
430 +       schedule_work(&glue->work);
431 +}
432 +
433 +static void sunxi_musb_disable(struct musb *musb)
434 +{
435 +       struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
436 +
437 +       clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
438 +}
439 +
440 +/*
441 + * sunxi musb register layout
442 + * 0x00 - 0x17 fifo regs, 1 long per fifo
443 + * 0x40 - 0x57 generic control regs (power - frame)
444 + * 0x80 - 0x8f ep control regs (addressed through hw_ep->regs, indexed)
445 + * 0x90 - 0x97 fifo control regs (indexed)
446 + * 0x98 - 0x9f multipoint / busctl regs (indexed)
447 + * 0xc0                configdata reg
448 + */
449 +
450 +static u32 sunxi_musb_fifo_offset(u8 epnum)
451 +{
452 +       return (epnum * 4);
453 +}
454 +
455 +static u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
456 +{
457 +       WARN_ONCE(offset != 0,
458 +                 "sunxi_musb_ep_offset called with non 0 offset\n");
459 +
460 +       return 0x80; /* indexed, so ignore epnum */
461 +}
462 +
463 +static u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
464 +{
465 +       return SUNXI_MUSB_TXFUNCADDR + offset;
466 +}
467 +
468 +static u8 sunxi_musb_readb(const void __iomem *addr, unsigned offset)
469 +{
470 +       if (addr == sunxi_musb->mregs) {
471 +               /* generic control or fifo control reg access */
472 +               switch (offset) {
473 +               case MUSB_FADDR:
474 +                       return readb(addr + SUNXI_MUSB_FADDR);
475 +               case MUSB_POWER:
476 +                       return readb(addr + SUNXI_MUSB_POWER);
477 +               case MUSB_INTRUSB:
478 +                       return readb(addr + SUNXI_MUSB_INTRUSB);
479 +               case MUSB_INTRUSBE:
480 +                       return readb(addr + SUNXI_MUSB_INTRUSBE);
481 +               case MUSB_INDEX:
482 +                       return readb(addr + SUNXI_MUSB_INDEX);
483 +               case MUSB_TESTMODE:
484 +                       return 0; /* No testmode on sunxi */
485 +               case MUSB_DEVCTL:
486 +                       return readb(addr + SUNXI_MUSB_DEVCTL);
487 +               case MUSB_TXFIFOSZ:
488 +                       return readb(addr + SUNXI_MUSB_TXFIFOSZ);
489 +               case MUSB_RXFIFOSZ:
490 +                       return readb(addr + SUNXI_MUSB_RXFIFOSZ);
491 +               case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
492 +                       return readb(addr + SUNXI_MUSB_CONFIGDATA);
493 +               /* Offset for these is fixed by sunxi_musb_busctl_offset() */
494 +               case SUNXI_MUSB_TXFUNCADDR:
495 +               case SUNXI_MUSB_TXHUBADDR:
496 +               case SUNXI_MUSB_TXHUBPORT:
497 +               case SUNXI_MUSB_RXFUNCADDR:
498 +               case SUNXI_MUSB_RXHUBADDR:
499 +               case SUNXI_MUSB_RXHUBPORT:
500 +                       /* multipoint / busctl reg access */
501 +                       return readb(addr + offset);
502 +               default:
503 +                       dev_err(sunxi_musb->controller->parent,
504 +                               "Error unknown readb offset %u\n", offset);
505 +                       return 0;
506 +               }
507 +       } else if (addr == (sunxi_musb->mregs + 0x80)) {
508 +               /* ep control reg access */
509 +               /* sunxi has a 2 byte hole before the txtype register */
510 +               if (offset >= MUSB_TXTYPE)
511 +                       offset += 2;
512 +               return readb(addr + offset);
513 +       }
514 +
515 +       dev_err(sunxi_musb->controller->parent,
516 +               "Error unknown readb at 0x%x bytes offset\n",
517 +               (int)(addr - sunxi_musb->mregs));
518 +       return 0;
519 +}
520 +
521 +static void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
522 +{
523 +       if (addr == sunxi_musb->mregs) {
524 +               /* generic control or fifo control reg access */
525 +               switch (offset) {
526 +               case MUSB_FADDR:
527 +                       return writeb(data, addr + SUNXI_MUSB_FADDR);
528 +               case MUSB_POWER:
529 +                       return writeb(data, addr + SUNXI_MUSB_POWER);
530 +               case MUSB_INTRUSB:
531 +                       return writeb(data, addr + SUNXI_MUSB_INTRUSB);
532 +               case MUSB_INTRUSBE:
533 +                       return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
534 +               case MUSB_INDEX:
535 +                       return writeb(data, addr + SUNXI_MUSB_INDEX);
536 +               case MUSB_TESTMODE:
537 +                       if (data)
538 +                               dev_warn(sunxi_musb->controller->parent,
539 +                                       "sunxi-musb does not have testmode\n");
540 +                       return;
541 +               case MUSB_DEVCTL:
542 +                       return writeb(data, addr + SUNXI_MUSB_DEVCTL);
543 +               case MUSB_TXFIFOSZ:
544 +                       return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
545 +               case MUSB_RXFIFOSZ:
546 +                       return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
547 +               /* Offset for these is fixed by sunxi_musb_busctl_offset() */
548 +               case SUNXI_MUSB_TXFUNCADDR:
549 +               case SUNXI_MUSB_TXHUBADDR:
550 +               case SUNXI_MUSB_TXHUBPORT:
551 +               case SUNXI_MUSB_RXFUNCADDR:
552 +               case SUNXI_MUSB_RXHUBADDR:
553 +               case SUNXI_MUSB_RXHUBPORT:
554 +                       /* multipoint / busctl reg access */
555 +                       return writeb(data, addr + offset);
556 +               default:
557 +                       dev_err(sunxi_musb->controller->parent,
558 +                               "Error unknown writeb offset %u\n", offset);
559 +                       return;
560 +               }
561 +       } else if (addr == (sunxi_musb->mregs + 0x80)) {
562 +               /* ep control reg access */
563 +               if (offset >= MUSB_TXTYPE)
564 +                       offset += 2;
565 +               return writeb(data, addr + offset);
566 +       }
567 +
568 +       dev_err(sunxi_musb->controller->parent,
569 +               "Error unknown writeb at 0x%x bytes offset\n",
570 +               (int)(addr - sunxi_musb->mregs));
571 +}
572 +
573 +static u16 sunxi_musb_readw(const void __iomem *addr, unsigned offset)
574 +{
575 +       if (addr == sunxi_musb->mregs) {
576 +               /* generic control or fifo control reg access */
577 +               switch (offset) {
578 +               case MUSB_INTRTX:
579 +                       return readw(addr + SUNXI_MUSB_INTRTX);
580 +               case MUSB_INTRRX:
581 +                       return readw(addr + SUNXI_MUSB_INTRRX);
582 +               case MUSB_INTRTXE:
583 +                       return readw(addr + SUNXI_MUSB_INTRTXE);
584 +               case MUSB_INTRRXE:
585 +                       return readw(addr + SUNXI_MUSB_INTRRXE);
586 +               case MUSB_FRAME:
587 +                       return readw(addr + SUNXI_MUSB_FRAME);
588 +               case MUSB_TXFIFOADD:
589 +                       return readw(addr + SUNXI_MUSB_TXFIFOADD);
590 +               case MUSB_RXFIFOADD:
591 +                       return readw(addr + SUNXI_MUSB_RXFIFOADD);
592 +               case MUSB_HWVERS:
593 +                       return 0; /* sunxi musb version is not known */
594 +               default:
595 +                       dev_err(sunxi_musb->controller->parent,
596 +                               "Error unknown readw offset %u\n", offset);
597 +                       return 0;
598 +               }
599 +       } else if (addr == (sunxi_musb->mregs + 0x80)) {
600 +               /* ep control reg access */
601 +               return readw(addr + offset);
602 +       }
603 +
604 +       dev_err(sunxi_musb->controller->parent,
605 +               "Error unknown readw at 0x%x bytes offset\n",
606 +               (int)(addr - sunxi_musb->mregs));
607 +       return 0;
608 +}
609 +
610 +static void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
611 +{
612 +       if (addr == sunxi_musb->mregs) {
613 +               /* generic control or fifo control reg access */
614 +               switch (offset) {
615 +               case MUSB_INTRTX:
616 +                       return writew(data, addr + SUNXI_MUSB_INTRTX);
617 +               case MUSB_INTRRX:
618 +                       return writew(data, addr + SUNXI_MUSB_INTRRX);
619 +               case MUSB_INTRTXE:
620 +                       return writew(data, addr + SUNXI_MUSB_INTRTXE);
621 +               case MUSB_INTRRXE:
622 +                       return writew(data, addr + SUNXI_MUSB_INTRRXE);
623 +               case MUSB_FRAME:
624 +                       return writew(data, addr + SUNXI_MUSB_FRAME);
625 +               case MUSB_TXFIFOADD:
626 +                       return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
627 +               case MUSB_RXFIFOADD:
628 +                       return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
629 +               default:
630 +                       dev_err(sunxi_musb->controller->parent,
631 +                               "Error unknown writew offset %u\n", offset);
632 +                       return;
633 +               }
634 +       } else if (addr == (sunxi_musb->mregs + 0x80)) {
635 +               /* ep control reg access */
636 +               return writew(data, addr + offset);
637 +       }
638 +
639 +       dev_err(sunxi_musb->controller->parent,
640 +               "Error unknown writew at 0x%x bytes offset\n",
641 +               (int)(addr - sunxi_musb->mregs));
642 +}
643 +
644 +static const struct musb_platform_ops sunxi_musb_ops = {
645 +       .quirks         = MUSB_INDEXED_EP,
646 +       .init           = sunxi_musb_init,
647 +       .exit           = sunxi_musb_exit,
648 +       .enable         = sunxi_musb_enable,
649 +       .disable        = sunxi_musb_disable,
650 +       .fifo_offset    = sunxi_musb_fifo_offset,
651 +       .ep_offset      = sunxi_musb_ep_offset,
652 +       .busctl_offset  = sunxi_musb_busctl_offset,
653 +       .readb          = sunxi_musb_readb,
654 +       .writeb         = sunxi_musb_writeb,
655 +       .readw          = sunxi_musb_readw,
656 +       .writew         = sunxi_musb_writew,
657 +       .set_vbus       = sunxi_musb_set_vbus,
658 +       .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
659 +       .post_root_reset_end = sunxi_musb_post_root_reset_end,
660 +};
661 +
662 +/* Allwinner OTG supports up to 5 endpoints */
663 +#define SUNXI_MUSB_MAX_EP_NUM  6
664 +#define SUNXI_MUSB_RAM_BITS    11
665 +
666 +static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
667 +       MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
668 +       MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
669 +       MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
670 +       MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
671 +       MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
672 +       MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
673 +       MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
674 +       MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
675 +       MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
676 +       MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
677 +};
678 +
679 +static struct musb_hdrc_config sunxi_musb_hdrc_config = {
680 +       .fifo_cfg       = sunxi_musb_mode_cfg,
681 +       .fifo_cfg_size  = ARRAY_SIZE(sunxi_musb_mode_cfg),
682 +       .multipoint     = true,
683 +       .dyn_fifo       = true,
684 +       .soft_con       = true,
685 +       .num_eps        = SUNXI_MUSB_MAX_EP_NUM,
686 +       .ram_bits       = SUNXI_MUSB_RAM_BITS,
687 +       .dma            = 0,
688 +};
689 +
690 +static int sunxi_musb_probe(struct platform_device *pdev)
691 +{
692 +       struct musb_hdrc_platform_data  pdata;
693 +       struct platform_device_info     pinfo;
694 +       struct sunxi_glue               *glue;
695 +       struct device_node              *np = pdev->dev.of_node;
696 +       int ret;
697 +
698 +       if (!np) {
699 +               dev_err(&pdev->dev, "Error no device tree node found\n");
700 +               return -EINVAL;
701 +       }
702 +
703 +       glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
704 +       if (!glue)
705 +               return -ENOMEM;
706 +
707 +       memset(&pdata, 0, sizeof(pdata));
708 +       switch (of_usb_get_dr_mode(np)) {
709 +#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
710 +       case USB_DR_MODE_HOST:
711 +               pdata.mode = MUSB_PORT_MODE_HOST;
712 +               break;
713 +#endif
714 +#ifdef CONFIG_USB_MUSB_DUAL_ROLE
715 +       case USB_DR_MODE_OTG:
716 +               glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
717 +               if (IS_ERR(glue->extcon)) {
718 +                       if (PTR_ERR(glue->extcon) == -EPROBE_DEFER)
719 +                               return -EPROBE_DEFER;
720 +                       dev_err(&pdev->dev, "Invalid or missing extcon\n");
721 +                       return PTR_ERR(glue->extcon);
722 +               }
723 +               pdata.mode = MUSB_PORT_MODE_DUAL_ROLE;
724 +               break;
725 +#endif
726 +       default:
727 +               dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
728 +               return -EINVAL;
729 +       }
730 +       pdata.platform_ops      = &sunxi_musb_ops;
731 +       pdata.config            = &sunxi_musb_hdrc_config;
732 +
733 +       glue->dev = &pdev->dev;
734 +       INIT_WORK(&glue->work, sunxi_musb_work);
735 +       glue->host_nb.notifier_call = sunxi_musb_host_notifier;
736 +
737 +       glue->clk = devm_clk_get(&pdev->dev, NULL);
738 +       if (IS_ERR(glue->clk)) {
739 +               dev_err(&pdev->dev, "Error getting clock: %ld\n",
740 +                       PTR_ERR(glue->clk));
741 +               return PTR_ERR(glue->clk);
742 +       }
743 +
744 +       glue->phy = devm_phy_get(&pdev->dev, "usb");
745 +       if (IS_ERR(glue->phy)) {
746 +               if (PTR_ERR(glue->phy) == -EPROBE_DEFER)
747 +                       return -EPROBE_DEFER;
748 +               dev_err(&pdev->dev, "Error getting phy %ld\n",
749 +                       PTR_ERR(glue->phy));
750 +               return PTR_ERR(glue->phy);
751 +       }
752 +
753 +       glue->usb_phy = usb_phy_generic_register();
754 +       if (IS_ERR(glue->usb_phy)) {
755 +               dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
756 +                       PTR_ERR(glue->usb_phy));
757 +               return PTR_ERR(glue->usb_phy);
758 +       }
759 +
760 +       glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
761 +       if (IS_ERR(glue->xceiv)) {
762 +               ret = PTR_ERR(glue->xceiv);
763 +               dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
764 +               goto err_unregister_usb_phy;
765 +       }
766 +
767 +       platform_set_drvdata(pdev, glue);
768 +
769 +       memset(&pinfo, 0, sizeof(pinfo));
770 +       pinfo.name       = "musb-hdrc";
771 +       pinfo.id        = PLATFORM_DEVID_AUTO;
772 +       pinfo.parent    = &pdev->dev;
773 +       pinfo.res       = pdev->resource;
774 +       pinfo.num_res   = pdev->num_resources;
775 +       pinfo.data      = &pdata;
776 +       pinfo.size_data = sizeof(pdata);
777 +
778 +       glue->musb = platform_device_register_full(&pinfo);
779 +       if (IS_ERR(glue->musb)) {
780 +               ret = PTR_ERR(glue->musb);
781 +               dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
782 +               goto err_unregister_usb_phy;
783 +       }
784 +
785 +       return 0;
786 +
787 +err_unregister_usb_phy:
788 +       usb_phy_generic_unregister(glue->usb_phy);
789 +       return ret;
790 +}
791 +
792 +static int sunxi_musb_remove(struct platform_device *pdev)
793 +{
794 +       struct sunxi_glue *glue = platform_get_drvdata(pdev);
795 +       struct platform_device *usb_phy = glue->usb_phy;
796 +
797 +       platform_device_unregister(glue->musb); /* Frees glue ! */
798 +       usb_phy_generic_unregister(usb_phy);
799 +
800 +       return 0;
801 +}
802 +
803 +static const struct of_device_id sunxi_musb_match[] = {
804 +       { .compatible = "allwinner,sun4i-a10-musb", },
805 +       {}
806 +};
807 +
808 +static struct platform_driver sunxi_musb_driver = {
809 +       .probe = sunxi_musb_probe,
810 +       .remove = sunxi_musb_remove,
811 +       .driver = {
812 +               .name = "musb-sunxi",
813 +               .of_match_table = sunxi_musb_match,
814 +       },
815 +};
816 +module_platform_driver(sunxi_musb_driver);
817 +
818 +MODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
819 +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
820 +MODULE_LICENSE("GPL v2");