Home | History | Annotate | Line # | Download | only in sunxi
sunxi_musb.c revision 1.6
      1  1.6  jmcneill /* $NetBSD: sunxi_musb.c,v 1.6 2021/01/15 22:47:32 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.6  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_musb.c,v 1.6 2021/01/15 22:47:32 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill #include <sys/pool.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <dev/usb/usb.h>
     41  1.1  jmcneill #include <dev/usb/usbdi.h>
     42  1.1  jmcneill #include <dev/usb/usbdivar.h>
     43  1.1  jmcneill #include <dev/usb/motgvar.h>
     44  1.2  jmcneill #include <dev/usb/motgreg.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     47  1.1  jmcneill 
     48  1.2  jmcneill #include <machine/bus_defs.h>
     49  1.2  jmcneill 
     50  1.1  jmcneill #define	MUSB2_REG_AWIN_VEND0	0x43
     51  1.1  jmcneill 
     52  1.1  jmcneill static int	sunxi_musb_match(device_t, cfdata_t, void *);
     53  1.1  jmcneill static void	sunxi_musb_attach(device_t, device_t, void *);
     54  1.1  jmcneill 
     55  1.2  jmcneill struct sunxi_musb_softc {
     56  1.2  jmcneill 	struct motg_softc	sc_otg;
     57  1.2  jmcneill 	struct bus_space	sc_bs;
     58  1.2  jmcneill };
     59  1.2  jmcneill 
     60  1.2  jmcneill CFATTACH_DECL_NEW(sunxi_musb, sizeof(struct sunxi_musb_softc),
     61  1.1  jmcneill 	sunxi_musb_match, sunxi_musb_attach, NULL, NULL);
     62  1.1  jmcneill 
     63  1.1  jmcneill static const struct of_compat_data compat_data[] = {
     64  1.1  jmcneill 	{ "allwinner,sun4i-a10-musb",		5 },
     65  1.1  jmcneill 	{ "allwinner,sun6i-a13-musb",		5 },
     66  1.1  jmcneill 	{ "allwinner,sun8i-h3-musb",		4 },
     67  1.1  jmcneill 	{ "allwinner,sun8i-a33-musb",		5 },
     68  1.1  jmcneill 	{ NULL }
     69  1.1  jmcneill };
     70  1.1  jmcneill 
     71  1.2  jmcneill #define	REMAPFLAG	0x8000
     72  1.2  jmcneill #define	REGDECL(a, b)	[(a)] = ((b) | REMAPFLAG)
     73  1.2  jmcneill 
     74  1.2  jmcneill /* Allwinner USB DRD register mappings */
     75  1.2  jmcneill static const uint16_t sunxi_musb_regmap[] = {
     76  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(0),	0x0000),
     77  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(1),	0x0004),
     78  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(2),	0x0008),
     79  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(3),	0x000c),
     80  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(4),	0x0010),
     81  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(5),	0x0014),
     82  1.2  jmcneill 	REGDECL(MUSB2_REG_POWER,	0x0040),
     83  1.2  jmcneill 	REGDECL(MUSB2_REG_DEVCTL,	0x0041),
     84  1.2  jmcneill 	REGDECL(MUSB2_REG_EPINDEX,	0x0042),
     85  1.2  jmcneill 	REGDECL(MUSB2_REG_AWIN_VEND0,	0x0043),
     86  1.2  jmcneill 	REGDECL(MUSB2_REG_INTTX,	0x0044),
     87  1.2  jmcneill 	REGDECL(MUSB2_REG_INTRX,	0x0046),
     88  1.2  jmcneill 	REGDECL(MUSB2_REG_INTTXE,	0x0048),
     89  1.2  jmcneill 	REGDECL(MUSB2_REG_INTRXE,	0x004a),
     90  1.2  jmcneill 	REGDECL(MUSB2_REG_INTUSB,	0x004c),
     91  1.2  jmcneill 	REGDECL(MUSB2_REG_INTUSBE,	0x0050),
     92  1.2  jmcneill 	REGDECL(MUSB2_REG_FRAME,	0x0054),
     93  1.2  jmcneill 	REGDECL(MUSB2_REG_TESTMODE,	0x007c),
     94  1.2  jmcneill 	REGDECL(MUSB2_REG_TXMAXP,	0x0080),
     95  1.2  jmcneill 	REGDECL(MUSB2_REG_TXCSRL,	0x0082),
     96  1.2  jmcneill 	REGDECL(MUSB2_REG_TXCSRH,	0x0083),
     97  1.2  jmcneill 	REGDECL(MUSB2_REG_RXMAXP,	0x0084),
     98  1.2  jmcneill 	REGDECL(MUSB2_REG_RXCSRL,	0x0086),
     99  1.2  jmcneill 	REGDECL(MUSB2_REG_RXCSRH,	0x0087),
    100  1.2  jmcneill 	REGDECL(MUSB2_REG_RXCOUNT,	0x0088),
    101  1.2  jmcneill 	REGDECL(MUSB2_REG_TXTI,		0x008c),
    102  1.2  jmcneill 	REGDECL(MUSB2_REG_TXNAKLIMIT,	0x008d),
    103  1.2  jmcneill 	REGDECL(MUSB2_REG_RXNAKLIMIT,	0x008d),
    104  1.2  jmcneill 	REGDECL(MUSB2_REG_RXTI,		0x008e),
    105  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFIFOSZ,	0x0090),
    106  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFIFOADD,	0x0092),
    107  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFIFOSZ,	0x0094),
    108  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFIFOADD,	0x0096),
    109  1.2  jmcneill 	REGDECL(MUSB2_REG_FADDR,	0x0098),
    110  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(0),	0x0098),
    111  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(0),	0x009a),
    112  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(0),	0x009b),
    113  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(0),	0x009c),
    114  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(0),	0x009e),
    115  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(0),	0x009f),
    116  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(1),	0x0098),
    117  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(1),	0x009a),
    118  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(1),	0x009b),
    119  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(1),	0x009c),
    120  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(1),	0x009e),
    121  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(1),	0x009f),
    122  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(2),	0x0098),
    123  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(2),	0x009a),
    124  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(2),	0x009b),
    125  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(2),	0x009c),
    126  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(2),	0x009e),
    127  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(2),	0x009f),
    128  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(3),	0x0098),
    129  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(3),	0x009a),
    130  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(3),	0x009b),
    131  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(3),	0x009c),
    132  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(3),	0x009e),
    133  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(3),	0x009f),
    134  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(4),	0x0098),
    135  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(4),	0x009a),
    136  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(4),	0x009b),
    137  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(4),	0x009c),
    138  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(4),	0x009e),
    139  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(4),	0x009f),
    140  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(5),	0x0098),
    141  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(5),	0x009a),
    142  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(5),	0x009b),
    143  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(5),	0x009c),
    144  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(5),	0x009e),
    145  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(5),	0x009f),
    146  1.2  jmcneill 	REGDECL(MUSB2_REG_CONFDATA,	0x00c0),
    147  1.2  jmcneill };
    148  1.2  jmcneill 
    149  1.2  jmcneill static bus_size_t
    150  1.2  jmcneill sunxi_musb_reg(bus_size_t o)
    151  1.2  jmcneill {
    152  1.2  jmcneill 	bus_size_t v;
    153  1.2  jmcneill 
    154  1.2  jmcneill 	if (o >= __arraycount(sunxi_musb_regmap))
    155  1.2  jmcneill 		return o;
    156  1.2  jmcneill 
    157  1.2  jmcneill 	v = sunxi_musb_regmap[o];
    158  1.2  jmcneill 	KASSERTMSG((v & REMAPFLAG) != 0, "%s: reg %#lx not in regmap",
    159  1.2  jmcneill 	    __func__, o);
    160  1.2  jmcneill 
    161  1.2  jmcneill 	return v & ~REMAPFLAG;
    162  1.2  jmcneill }
    163  1.2  jmcneill 
    164  1.2  jmcneill static int
    165  1.2  jmcneill sunxi_musb_filt(bus_size_t o)
    166  1.2  jmcneill {
    167  1.2  jmcneill 	switch (o) {
    168  1.2  jmcneill 	case MUSB2_REG_MISC:
    169  1.2  jmcneill 	case MUSB2_REG_RXDBDIS:
    170  1.2  jmcneill 	case MUSB2_REG_TXDBDIS:
    171  1.2  jmcneill 		return 1;
    172  1.2  jmcneill 	default:
    173  1.2  jmcneill 		return 0;
    174  1.2  jmcneill 	}
    175  1.2  jmcneill }
    176  1.2  jmcneill 
    177  1.2  jmcneill static uint8_t
    178  1.2  jmcneill sunxi_musb_bs_r_1(void *t, bus_space_handle_t h, bus_size_t o)
    179  1.2  jmcneill {
    180  1.2  jmcneill 	switch (o) {
    181  1.2  jmcneill 	case MUSB2_REG_HWVERS:
    182  1.2  jmcneill 		return 0;	/* no known equivalent */
    183  1.2  jmcneill 	}
    184  1.2  jmcneill 
    185  1.3  jmcneill 	return bus_space_read_1((bus_space_tag_t)t, h, sunxi_musb_reg(o));
    186  1.2  jmcneill }
    187  1.2  jmcneill 
    188  1.2  jmcneill static uint16_t
    189  1.2  jmcneill sunxi_musb_bs_r_2(void *t, bus_space_handle_t h, bus_size_t o)
    190  1.2  jmcneill {
    191  1.3  jmcneill 	return bus_space_read_2((bus_space_tag_t)t, h, sunxi_musb_reg(o));
    192  1.2  jmcneill }
    193  1.2  jmcneill 
    194  1.2  jmcneill static void
    195  1.2  jmcneill sunxi_musb_bs_w_1(void *t, bus_space_handle_t h, bus_size_t o,
    196  1.2  jmcneill     uint8_t v)
    197  1.2  jmcneill {
    198  1.2  jmcneill 	if (sunxi_musb_filt(o) != 0)
    199  1.2  jmcneill 		return;
    200  1.2  jmcneill 
    201  1.3  jmcneill 	bus_space_write_1((bus_space_tag_t)t, h, sunxi_musb_reg(o), v);
    202  1.2  jmcneill }
    203  1.2  jmcneill 
    204  1.2  jmcneill static void
    205  1.2  jmcneill sunxi_musb_bs_w_2(void *t, bus_space_handle_t h, bus_size_t o,
    206  1.2  jmcneill     uint16_t v)
    207  1.2  jmcneill {
    208  1.2  jmcneill 	if (sunxi_musb_filt(o) != 0)
    209  1.2  jmcneill 		return;
    210  1.2  jmcneill 
    211  1.3  jmcneill 	bus_space_write_2((bus_space_tag_t)t, h, sunxi_musb_reg(o), v);
    212  1.2  jmcneill }
    213  1.2  jmcneill 
    214  1.2  jmcneill static void
    215  1.2  jmcneill sunxi_musb_bs_rm_1(void *t, bus_space_handle_t h, bus_size_t o,
    216  1.2  jmcneill     uint8_t *d, bus_size_t c)
    217  1.2  jmcneill {
    218  1.3  jmcneill 	bus_space_read_multi_1((bus_space_tag_t)t, h, sunxi_musb_reg(o), d, c);
    219  1.2  jmcneill }
    220  1.2  jmcneill 
    221  1.2  jmcneill static void
    222  1.2  jmcneill sunxi_musb_bs_rm_4(void *t, bus_space_handle_t h, bus_size_t o,
    223  1.2  jmcneill     uint32_t *d, bus_size_t c)
    224  1.2  jmcneill {
    225  1.3  jmcneill 	bus_space_read_multi_4((bus_space_tag_t)t, h, sunxi_musb_reg(o), d, c);
    226  1.2  jmcneill }
    227  1.2  jmcneill 
    228  1.2  jmcneill static void
    229  1.2  jmcneill sunxi_musb_bs_wm_1(void *t, bus_space_handle_t h, bus_size_t o,
    230  1.2  jmcneill     const uint8_t *d, bus_size_t c)
    231  1.2  jmcneill {
    232  1.2  jmcneill 	if (sunxi_musb_filt(o) != 0)
    233  1.2  jmcneill 		return;
    234  1.2  jmcneill 
    235  1.3  jmcneill 	bus_space_write_multi_1((bus_space_tag_t)t, h, sunxi_musb_reg(o), d, c);
    236  1.2  jmcneill }
    237  1.2  jmcneill 
    238  1.2  jmcneill static void
    239  1.2  jmcneill sunxi_musb_bs_wm_4(void *t, bus_space_handle_t h, bus_size_t o,
    240  1.2  jmcneill     const uint32_t *d, bus_size_t c)
    241  1.2  jmcneill {
    242  1.2  jmcneill 	if (sunxi_musb_filt(o) != 0)
    243  1.2  jmcneill 		return;
    244  1.2  jmcneill 
    245  1.3  jmcneill 	bus_space_write_multi_4((bus_space_tag_t)t, h, sunxi_musb_reg(o), d, c);
    246  1.2  jmcneill }
    247  1.2  jmcneill 
    248  1.2  jmcneill static void
    249  1.2  jmcneill sunxi_musb_bs_barrier(void *t, bus_space_handle_t h, bus_size_t o,
    250  1.2  jmcneill     bus_size_t l, int f)
    251  1.2  jmcneill {
    252  1.3  jmcneill 	bus_space_barrier((bus_space_tag_t)t, h, o, l, f);
    253  1.2  jmcneill }
    254  1.2  jmcneill 
    255  1.1  jmcneill static int
    256  1.1  jmcneill sunxi_musb_intr(void *priv)
    257  1.1  jmcneill {
    258  1.1  jmcneill 	struct motg_softc * const sc = priv;
    259  1.1  jmcneill 	uint16_t inttx, intrx;
    260  1.1  jmcneill 	uint8_t intusb;
    261  1.1  jmcneill 
    262  1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    263  1.1  jmcneill 
    264  1.1  jmcneill 	intusb = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTUSB);
    265  1.1  jmcneill 	inttx = bus_space_read_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTTX);
    266  1.1  jmcneill 	intrx = bus_space_read_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTRX);
    267  1.1  jmcneill 	if (!intusb && !inttx && !intrx) {
    268  1.1  jmcneill 		mutex_exit(&sc->sc_intr_lock);
    269  1.1  jmcneill 		return 0;
    270  1.1  jmcneill 	}
    271  1.1  jmcneill 
    272  1.1  jmcneill 	if (intusb)
    273  1.1  jmcneill 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTUSB, intusb);
    274  1.1  jmcneill 	if (inttx)
    275  1.1  jmcneill 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTTX, inttx);
    276  1.1  jmcneill 	if (intrx)
    277  1.1  jmcneill 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTRX, intrx);
    278  1.1  jmcneill 
    279  1.1  jmcneill 	motg_intr(sc, intrx, inttx, intusb);
    280  1.1  jmcneill 
    281  1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    282  1.1  jmcneill 
    283  1.1  jmcneill 	return 1;
    284  1.1  jmcneill }
    285  1.1  jmcneill 
    286  1.1  jmcneill static void
    287  1.1  jmcneill sunxi_musb_poll(void *priv)
    288  1.1  jmcneill {
    289  1.1  jmcneill 	sunxi_musb_intr(priv);
    290  1.1  jmcneill }
    291  1.1  jmcneill 
    292  1.1  jmcneill static int
    293  1.1  jmcneill sunxi_musb_match(device_t parent, cfdata_t cf, void *aux)
    294  1.1  jmcneill {
    295  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    296  1.1  jmcneill 
    297  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    298  1.1  jmcneill }
    299  1.1  jmcneill 
    300  1.1  jmcneill static void
    301  1.1  jmcneill sunxi_musb_attach(device_t parent, device_t self, void *aux)
    302  1.1  jmcneill {
    303  1.2  jmcneill 	struct sunxi_musb_softc * const msc = device_private(self);
    304  1.2  jmcneill 	struct motg_softc * const sc = &msc->sc_otg;
    305  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    306  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    307  1.1  jmcneill 	struct fdtbus_reset *rst;
    308  1.1  jmcneill 	struct fdtbus_phy *phy;
    309  1.1  jmcneill 	struct clk *clk;
    310  1.1  jmcneill 	char intrstr[128];
    311  1.1  jmcneill 	const char *dr_mode;
    312  1.1  jmcneill 	bus_addr_t addr;
    313  1.1  jmcneill 	bus_size_t size;
    314  1.1  jmcneill 	void *ih;
    315  1.1  jmcneill 	u_int n;
    316  1.1  jmcneill 
    317  1.1  jmcneill 	/* Only "host" mode is supported */
    318  1.1  jmcneill 	dr_mode = fdtbus_get_string(phandle, "dr_mode");
    319  1.1  jmcneill 	if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
    320  1.1  jmcneill 		aprint_normal(": '%s' mode not supported\n", dr_mode);
    321  1.1  jmcneill 		return;
    322  1.1  jmcneill 	}
    323  1.1  jmcneill 
    324  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    325  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    326  1.1  jmcneill 		return;
    327  1.1  jmcneill 	}
    328  1.1  jmcneill 
    329  1.1  jmcneill 	/* Enable clocks */
    330  1.1  jmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
    331  1.1  jmcneill 		if (clk_enable(clk) != 0) {
    332  1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
    333  1.1  jmcneill 			return;
    334  1.1  jmcneill 		}
    335  1.1  jmcneill 	/* De-assert resets */
    336  1.1  jmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
    337  1.1  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    338  1.1  jmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
    339  1.1  jmcneill 			return;
    340  1.1  jmcneill 		}
    341  1.1  jmcneill 
    342  1.1  jmcneill 	/* Enable optional phy */
    343  1.1  jmcneill 	phy = fdtbus_phy_get(phandle, "usb");
    344  1.1  jmcneill 	if (phy && fdtbus_phy_enable(phy, true) != 0) {
    345  1.1  jmcneill 		aprint_error(": couldn't enable phy\n");
    346  1.1  jmcneill 		return;
    347  1.1  jmcneill 	}
    348  1.1  jmcneill 
    349  1.2  jmcneill 	/* Create custom bus space tag for remapping registers */
    350  1.2  jmcneill 	msc->sc_bs.bs_cookie = faa->faa_bst;
    351  1.2  jmcneill 	msc->sc_bs.bs_r_1 = sunxi_musb_bs_r_1;
    352  1.2  jmcneill 	msc->sc_bs.bs_r_2 = sunxi_musb_bs_r_2;
    353  1.2  jmcneill 	msc->sc_bs.bs_w_1 = sunxi_musb_bs_w_1;
    354  1.2  jmcneill 	msc->sc_bs.bs_w_2 = sunxi_musb_bs_w_2;
    355  1.2  jmcneill 	msc->sc_bs.bs_rm_1 = sunxi_musb_bs_rm_1;
    356  1.2  jmcneill 	msc->sc_bs.bs_rm_4 = sunxi_musb_bs_rm_4;
    357  1.2  jmcneill 	msc->sc_bs.bs_wm_1 = sunxi_musb_bs_wm_1;
    358  1.2  jmcneill 	msc->sc_bs.bs_wm_4 = sunxi_musb_bs_wm_4;
    359  1.2  jmcneill 	msc->sc_bs.bs_barrier = sunxi_musb_bs_barrier;
    360  1.2  jmcneill 
    361  1.1  jmcneill 	sc->sc_dev = self;
    362  1.1  jmcneill 	sc->sc_bus.ub_hcpriv = sc;
    363  1.1  jmcneill 	sc->sc_bus.ub_dmatag = faa->faa_dmat;
    364  1.1  jmcneill 	sc->sc_size = size;
    365  1.2  jmcneill 	sc->sc_iot = &msc->sc_bs;
    366  1.2  jmcneill 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) {
    367  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    368  1.1  jmcneill 		return;
    369  1.1  jmcneill 	}
    370  1.1  jmcneill 	sc->sc_intr_poll = sunxi_musb_poll;
    371  1.1  jmcneill 	sc->sc_intr_poll_arg = sc;
    372  1.1  jmcneill 	sc->sc_mode = MOTG_MODE_HOST;
    373  1.1  jmcneill 	sc->sc_ep_max = of_search_compatible(phandle, compat_data)->data;
    374  1.1  jmcneill 	sc->sc_ep_fifosize = 512;
    375  1.1  jmcneill 
    376  1.1  jmcneill 	aprint_naive("\n");
    377  1.1  jmcneill 	aprint_normal(": USB OTG\n");
    378  1.1  jmcneill 
    379  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    380  1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    381  1.1  jmcneill 		return;
    382  1.1  jmcneill 	}
    383  1.1  jmcneill 
    384  1.6  jmcneill 	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
    385  1.6  jmcneill 	    sunxi_musb_intr, sc, device_xname(self));
    386  1.1  jmcneill 	if (ih == NULL) {
    387  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    388  1.1  jmcneill 		    intrstr);
    389  1.1  jmcneill 		return;
    390  1.1  jmcneill 	}
    391  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    392  1.1  jmcneill 
    393  1.1  jmcneill 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_AWIN_VEND0, 0);
    394  1.1  jmcneill 
    395  1.1  jmcneill 	motg_init(sc);
    396  1.1  jmcneill }
    397