Home | History | Annotate | Line # | Download | only in sunxi
sunxi_musb.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: sunxi_musb.c,v 1.2 2017/10/29 16:02:46 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.2  jmcneill #include "opt_motg.h"
     30  1.2  jmcneill #ifdef MOTG_ALLWINNER
     31  1.2  jmcneill # error Do not define MOTG_ALLWINNER when using this driver
     32  1.2  jmcneill #endif
     33  1.2  jmcneill 
     34  1.1  jmcneill #include <sys/cdefs.h>
     35  1.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_musb.c,v 1.2 2017/10/29 16:02:46 jmcneill Exp $");
     36  1.1  jmcneill 
     37  1.1  jmcneill #include <sys/param.h>
     38  1.1  jmcneill #include <sys/bus.h>
     39  1.1  jmcneill #include <sys/device.h>
     40  1.1  jmcneill #include <sys/intr.h>
     41  1.1  jmcneill #include <sys/systm.h>
     42  1.1  jmcneill #include <sys/kernel.h>
     43  1.1  jmcneill #include <sys/pool.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <dev/usb/usb.h>
     46  1.1  jmcneill #include <dev/usb/usbdi.h>
     47  1.1  jmcneill #include <dev/usb/usbdivar.h>
     48  1.1  jmcneill #include <dev/usb/motgvar.h>
     49  1.2  jmcneill #include <dev/usb/motgreg.h>
     50  1.1  jmcneill 
     51  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     52  1.1  jmcneill 
     53  1.2  jmcneill #include <machine/bus_defs.h>
     54  1.2  jmcneill 
     55  1.1  jmcneill #define	MUSB2_REG_AWIN_VEND0	0x43
     56  1.1  jmcneill 
     57  1.1  jmcneill static int	sunxi_musb_match(device_t, cfdata_t, void *);
     58  1.1  jmcneill static void	sunxi_musb_attach(device_t, device_t, void *);
     59  1.1  jmcneill 
     60  1.2  jmcneill struct sunxi_musb_softc {
     61  1.2  jmcneill 	struct motg_softc	sc_otg;
     62  1.2  jmcneill 	struct bus_space	sc_bs;
     63  1.2  jmcneill };
     64  1.2  jmcneill 
     65  1.2  jmcneill CFATTACH_DECL_NEW(sunxi_musb, sizeof(struct sunxi_musb_softc),
     66  1.1  jmcneill 	sunxi_musb_match, sunxi_musb_attach, NULL, NULL);
     67  1.1  jmcneill 
     68  1.1  jmcneill static const struct of_compat_data compat_data[] = {
     69  1.1  jmcneill 	{ "allwinner,sun4i-a10-musb",		5 },
     70  1.1  jmcneill 	{ "allwinner,sun6i-a13-musb",		5 },
     71  1.1  jmcneill 	{ "allwinner,sun8i-h3-musb",		4 },
     72  1.1  jmcneill 	{ "allwinner,sun8i-a33-musb",		5 },
     73  1.1  jmcneill 	{ NULL }
     74  1.1  jmcneill };
     75  1.1  jmcneill 
     76  1.2  jmcneill #define	REMAPFLAG	0x8000
     77  1.2  jmcneill #define	REGDECL(a, b)	[(a)] = ((b) | REMAPFLAG)
     78  1.2  jmcneill 
     79  1.2  jmcneill /* Allwinner USB DRD register mappings */
     80  1.2  jmcneill static const uint16_t sunxi_musb_regmap[] = {
     81  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(0),	0x0000),
     82  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(1),	0x0004),
     83  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(2),	0x0008),
     84  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(3),	0x000c),
     85  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(4),	0x0010),
     86  1.2  jmcneill 	REGDECL(MUSB2_REG_EPFIFO(5),	0x0014),
     87  1.2  jmcneill 	REGDECL(MUSB2_REG_POWER,	0x0040),
     88  1.2  jmcneill 	REGDECL(MUSB2_REG_DEVCTL,	0x0041),
     89  1.2  jmcneill 	REGDECL(MUSB2_REG_EPINDEX,	0x0042),
     90  1.2  jmcneill 	REGDECL(MUSB2_REG_AWIN_VEND0,	0x0043),
     91  1.2  jmcneill 	REGDECL(MUSB2_REG_INTTX,	0x0044),
     92  1.2  jmcneill 	REGDECL(MUSB2_REG_INTRX,	0x0046),
     93  1.2  jmcneill 	REGDECL(MUSB2_REG_INTTXE,	0x0048),
     94  1.2  jmcneill 	REGDECL(MUSB2_REG_INTRXE,	0x004a),
     95  1.2  jmcneill 	REGDECL(MUSB2_REG_INTUSB,	0x004c),
     96  1.2  jmcneill 	REGDECL(MUSB2_REG_INTUSBE,	0x0050),
     97  1.2  jmcneill 	REGDECL(MUSB2_REG_FRAME,	0x0054),
     98  1.2  jmcneill 	REGDECL(MUSB2_REG_TESTMODE,	0x007c),
     99  1.2  jmcneill 	REGDECL(MUSB2_REG_TXMAXP,	0x0080),
    100  1.2  jmcneill 	REGDECL(MUSB2_REG_TXCSRL,	0x0082),
    101  1.2  jmcneill 	REGDECL(MUSB2_REG_TXCSRH,	0x0083),
    102  1.2  jmcneill 	REGDECL(MUSB2_REG_RXMAXP,	0x0084),
    103  1.2  jmcneill 	REGDECL(MUSB2_REG_RXCSRL,	0x0086),
    104  1.2  jmcneill 	REGDECL(MUSB2_REG_RXCSRH,	0x0087),
    105  1.2  jmcneill 	REGDECL(MUSB2_REG_RXCOUNT,	0x0088),
    106  1.2  jmcneill 	REGDECL(MUSB2_REG_TXTI,		0x008c),
    107  1.2  jmcneill 	REGDECL(MUSB2_REG_TXNAKLIMIT,	0x008d),
    108  1.2  jmcneill 	REGDECL(MUSB2_REG_RXNAKLIMIT,	0x008d),
    109  1.2  jmcneill 	REGDECL(MUSB2_REG_RXTI,		0x008e),
    110  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFIFOSZ,	0x0090),
    111  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFIFOADD,	0x0092),
    112  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFIFOSZ,	0x0094),
    113  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFIFOADD,	0x0096),
    114  1.2  jmcneill 	REGDECL(MUSB2_REG_FADDR,	0x0098),
    115  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(0),	0x0098),
    116  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(0),	0x009a),
    117  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(0),	0x009b),
    118  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(0),	0x009c),
    119  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(0),	0x009e),
    120  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(0),	0x009f),
    121  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(1),	0x0098),
    122  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(1),	0x009a),
    123  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(1),	0x009b),
    124  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(1),	0x009c),
    125  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(1),	0x009e),
    126  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(1),	0x009f),
    127  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(2),	0x0098),
    128  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(2),	0x009a),
    129  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(2),	0x009b),
    130  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(2),	0x009c),
    131  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(2),	0x009e),
    132  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(2),	0x009f),
    133  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(3),	0x0098),
    134  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(3),	0x009a),
    135  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(3),	0x009b),
    136  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(3),	0x009c),
    137  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(3),	0x009e),
    138  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(3),	0x009f),
    139  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(4),	0x0098),
    140  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(4),	0x009a),
    141  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(4),	0x009b),
    142  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(4),	0x009c),
    143  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(4),	0x009e),
    144  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(4),	0x009f),
    145  1.2  jmcneill 	REGDECL(MUSB2_REG_TXFADDR(5),	0x0098),
    146  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHADDR(5),	0x009a),
    147  1.2  jmcneill 	REGDECL(MUSB2_REG_TXHUBPORT(5),	0x009b),
    148  1.2  jmcneill 	REGDECL(MUSB2_REG_RXFADDR(5),	0x009c),
    149  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHADDR(5),	0x009e),
    150  1.2  jmcneill 	REGDECL(MUSB2_REG_RXHUBPORT(5),	0x009f),
    151  1.2  jmcneill 	REGDECL(MUSB2_REG_CONFDATA,	0x00c0),
    152  1.2  jmcneill };
    153  1.2  jmcneill 
    154  1.2  jmcneill static bus_size_t
    155  1.2  jmcneill sunxi_musb_reg(bus_size_t o)
    156  1.2  jmcneill {
    157  1.2  jmcneill 	bus_size_t v;
    158  1.2  jmcneill 
    159  1.2  jmcneill 	if (o >= __arraycount(sunxi_musb_regmap))
    160  1.2  jmcneill 		return o;
    161  1.2  jmcneill 
    162  1.2  jmcneill 	v = sunxi_musb_regmap[o];
    163  1.2  jmcneill 	KASSERTMSG((v & REMAPFLAG) != 0, "%s: reg %#lx not in regmap",
    164  1.2  jmcneill 	    __func__, o);
    165  1.2  jmcneill 
    166  1.2  jmcneill 	return v & ~REMAPFLAG;
    167  1.2  jmcneill }
    168  1.2  jmcneill 
    169  1.2  jmcneill static int
    170  1.2  jmcneill sunxi_musb_filt(bus_size_t o)
    171  1.2  jmcneill {
    172  1.2  jmcneill 	switch (o) {
    173  1.2  jmcneill 	case MUSB2_REG_MISC:
    174  1.2  jmcneill 	case MUSB2_REG_RXDBDIS:
    175  1.2  jmcneill 	case MUSB2_REG_TXDBDIS:
    176  1.2  jmcneill 		return 1;
    177  1.2  jmcneill 	default:
    178  1.2  jmcneill 		return 0;
    179  1.2  jmcneill 	}
    180  1.2  jmcneill }
    181  1.2  jmcneill 
    182  1.2  jmcneill static uint8_t
    183  1.2  jmcneill sunxi_musb_bs_r_1(void *t, bus_space_handle_t h, bus_size_t o)
    184  1.2  jmcneill {
    185  1.2  jmcneill 	const struct bus_space *bs = t;
    186  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    187  1.2  jmcneill 
    188  1.2  jmcneill 	switch (o) {
    189  1.2  jmcneill 	case MUSB2_REG_HWVERS:
    190  1.2  jmcneill 		return 0;	/* no known equivalent */
    191  1.2  jmcneill 	}
    192  1.2  jmcneill 
    193  1.2  jmcneill 	return bus_space_read_1(bs_parent, h, sunxi_musb_reg(o));
    194  1.2  jmcneill }
    195  1.2  jmcneill 
    196  1.2  jmcneill static uint16_t
    197  1.2  jmcneill sunxi_musb_bs_r_2(void *t, bus_space_handle_t h, bus_size_t o)
    198  1.2  jmcneill {
    199  1.2  jmcneill 	const struct bus_space *bs = t;
    200  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    201  1.2  jmcneill 
    202  1.2  jmcneill 	return bus_space_read_2(bs_parent, h, sunxi_musb_reg(o));
    203  1.2  jmcneill }
    204  1.2  jmcneill 
    205  1.2  jmcneill static void
    206  1.2  jmcneill sunxi_musb_bs_w_1(void *t, bus_space_handle_t h, bus_size_t o,
    207  1.2  jmcneill     uint8_t v)
    208  1.2  jmcneill {
    209  1.2  jmcneill 	const struct bus_space *bs = t;
    210  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    211  1.2  jmcneill 
    212  1.2  jmcneill 	if (sunxi_musb_filt(o) != 0)
    213  1.2  jmcneill 		return;
    214  1.2  jmcneill 
    215  1.2  jmcneill 	bus_space_write_1(bs_parent, h, sunxi_musb_reg(o), v);
    216  1.2  jmcneill }
    217  1.2  jmcneill 
    218  1.2  jmcneill static void
    219  1.2  jmcneill sunxi_musb_bs_w_2(void *t, bus_space_handle_t h, bus_size_t o,
    220  1.2  jmcneill     uint16_t v)
    221  1.2  jmcneill {
    222  1.2  jmcneill 	const struct bus_space *bs = t;
    223  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    224  1.2  jmcneill 
    225  1.2  jmcneill 	if (sunxi_musb_filt(o) != 0)
    226  1.2  jmcneill 		return;
    227  1.2  jmcneill 
    228  1.2  jmcneill 	bus_space_write_2(bs_parent, h, sunxi_musb_reg(o), v);
    229  1.2  jmcneill }
    230  1.2  jmcneill 
    231  1.2  jmcneill static void
    232  1.2  jmcneill sunxi_musb_bs_rm_1(void *t, bus_space_handle_t h, bus_size_t o,
    233  1.2  jmcneill     uint8_t *d, bus_size_t c)
    234  1.2  jmcneill {
    235  1.2  jmcneill 	const struct bus_space *bs = t;
    236  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    237  1.2  jmcneill 
    238  1.2  jmcneill 	bus_space_read_multi_1(bs_parent, h, sunxi_musb_reg(o), d, c);
    239  1.2  jmcneill }
    240  1.2  jmcneill 
    241  1.2  jmcneill static void
    242  1.2  jmcneill sunxi_musb_bs_rm_4(void *t, bus_space_handle_t h, bus_size_t o,
    243  1.2  jmcneill     uint32_t *d, bus_size_t c)
    244  1.2  jmcneill {
    245  1.2  jmcneill 	const struct bus_space *bs = t;
    246  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    247  1.2  jmcneill 
    248  1.2  jmcneill 	bus_space_read_multi_4(bs_parent, h, sunxi_musb_reg(o), d, c);
    249  1.2  jmcneill }
    250  1.2  jmcneill 
    251  1.2  jmcneill static void
    252  1.2  jmcneill sunxi_musb_bs_wm_1(void *t, bus_space_handle_t h, bus_size_t o,
    253  1.2  jmcneill     const uint8_t *d, bus_size_t c)
    254  1.2  jmcneill {
    255  1.2  jmcneill 	const struct bus_space *bs = t;
    256  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    257  1.2  jmcneill 
    258  1.2  jmcneill 	if (sunxi_musb_filt(o) != 0)
    259  1.2  jmcneill 		return;
    260  1.2  jmcneill 
    261  1.2  jmcneill 	bus_space_write_multi_1(bs_parent, h, sunxi_musb_reg(o), d, c);
    262  1.2  jmcneill }
    263  1.2  jmcneill 
    264  1.2  jmcneill static void
    265  1.2  jmcneill sunxi_musb_bs_wm_4(void *t, bus_space_handle_t h, bus_size_t o,
    266  1.2  jmcneill     const uint32_t *d, bus_size_t c)
    267  1.2  jmcneill {
    268  1.2  jmcneill 	const struct bus_space *bs = t;
    269  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    270  1.2  jmcneill 
    271  1.2  jmcneill 	if (sunxi_musb_filt(o) != 0)
    272  1.2  jmcneill 		return;
    273  1.2  jmcneill 
    274  1.2  jmcneill 	bus_space_write_multi_4(bs_parent, h, sunxi_musb_reg(o), d, c);
    275  1.2  jmcneill }
    276  1.2  jmcneill 
    277  1.2  jmcneill static void
    278  1.2  jmcneill sunxi_musb_bs_barrier(void *t, bus_space_handle_t h, bus_size_t o,
    279  1.2  jmcneill     bus_size_t l, int f)
    280  1.2  jmcneill {
    281  1.2  jmcneill 	const struct bus_space *bs = t;
    282  1.2  jmcneill 	const struct bus_space *bs_parent = bs->bs_cookie;
    283  1.2  jmcneill 
    284  1.2  jmcneill 	bus_space_barrier(bs_parent, h, o, l, f);
    285  1.2  jmcneill }
    286  1.2  jmcneill 
    287  1.1  jmcneill static int
    288  1.1  jmcneill sunxi_musb_intr(void *priv)
    289  1.1  jmcneill {
    290  1.1  jmcneill 	struct motg_softc * const sc = priv;
    291  1.1  jmcneill 	uint16_t inttx, intrx;
    292  1.1  jmcneill 	uint8_t intusb;
    293  1.1  jmcneill 
    294  1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
    295  1.1  jmcneill 
    296  1.1  jmcneill 	intusb = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTUSB);
    297  1.1  jmcneill 	inttx = bus_space_read_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTTX);
    298  1.1  jmcneill 	intrx = bus_space_read_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTRX);
    299  1.1  jmcneill 	if (!intusb && !inttx && !intrx) {
    300  1.1  jmcneill 		mutex_exit(&sc->sc_intr_lock);
    301  1.1  jmcneill 		return 0;
    302  1.1  jmcneill 	}
    303  1.1  jmcneill 
    304  1.1  jmcneill 	if (intusb)
    305  1.1  jmcneill 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTUSB, intusb);
    306  1.1  jmcneill 	if (inttx)
    307  1.1  jmcneill 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTTX, inttx);
    308  1.1  jmcneill 	if (intrx)
    309  1.1  jmcneill 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTRX, intrx);
    310  1.1  jmcneill 
    311  1.1  jmcneill 	motg_intr(sc, intrx, inttx, intusb);
    312  1.1  jmcneill 
    313  1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
    314  1.1  jmcneill 
    315  1.1  jmcneill 	return 1;
    316  1.1  jmcneill }
    317  1.1  jmcneill 
    318  1.1  jmcneill static void
    319  1.1  jmcneill sunxi_musb_poll(void *priv)
    320  1.1  jmcneill {
    321  1.1  jmcneill 	sunxi_musb_intr(priv);
    322  1.1  jmcneill }
    323  1.1  jmcneill 
    324  1.1  jmcneill static int
    325  1.1  jmcneill sunxi_musb_match(device_t parent, cfdata_t cf, void *aux)
    326  1.1  jmcneill {
    327  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    328  1.1  jmcneill 
    329  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    330  1.1  jmcneill }
    331  1.1  jmcneill 
    332  1.1  jmcneill static void
    333  1.1  jmcneill sunxi_musb_attach(device_t parent, device_t self, void *aux)
    334  1.1  jmcneill {
    335  1.2  jmcneill 	struct sunxi_musb_softc * const msc = device_private(self);
    336  1.2  jmcneill 	struct motg_softc * const sc = &msc->sc_otg;
    337  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    338  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    339  1.1  jmcneill 	struct fdtbus_reset *rst;
    340  1.1  jmcneill 	struct fdtbus_phy *phy;
    341  1.1  jmcneill 	struct clk *clk;
    342  1.1  jmcneill 	char intrstr[128];
    343  1.1  jmcneill 	const char *dr_mode;
    344  1.1  jmcneill 	bus_addr_t addr;
    345  1.1  jmcneill 	bus_size_t size;
    346  1.1  jmcneill 	void *ih;
    347  1.1  jmcneill 	u_int n;
    348  1.1  jmcneill 
    349  1.1  jmcneill 	/* Only "host" mode is supported */
    350  1.1  jmcneill 	dr_mode = fdtbus_get_string(phandle, "dr_mode");
    351  1.1  jmcneill 	if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
    352  1.1  jmcneill 		aprint_normal(": '%s' mode not supported\n", dr_mode);
    353  1.1  jmcneill 		return;
    354  1.1  jmcneill 	}
    355  1.1  jmcneill 
    356  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    357  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    358  1.1  jmcneill 		return;
    359  1.1  jmcneill 	}
    360  1.1  jmcneill 
    361  1.1  jmcneill 	/* Enable clocks */
    362  1.1  jmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
    363  1.1  jmcneill 		if (clk_enable(clk) != 0) {
    364  1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
    365  1.1  jmcneill 			return;
    366  1.1  jmcneill 		}
    367  1.1  jmcneill 	/* De-assert resets */
    368  1.1  jmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
    369  1.1  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    370  1.1  jmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
    371  1.1  jmcneill 			return;
    372  1.1  jmcneill 		}
    373  1.1  jmcneill 
    374  1.1  jmcneill 	/* Enable optional phy */
    375  1.1  jmcneill 	phy = fdtbus_phy_get(phandle, "usb");
    376  1.1  jmcneill 	if (phy && fdtbus_phy_enable(phy, true) != 0) {
    377  1.1  jmcneill 		aprint_error(": couldn't enable phy\n");
    378  1.1  jmcneill 		return;
    379  1.1  jmcneill 	}
    380  1.1  jmcneill 
    381  1.2  jmcneill 	/* Create custom bus space tag for remapping registers */
    382  1.2  jmcneill 	msc->sc_bs.bs_cookie = faa->faa_bst;
    383  1.2  jmcneill 	msc->sc_bs.bs_r_1 = sunxi_musb_bs_r_1;
    384  1.2  jmcneill 	msc->sc_bs.bs_r_2 = sunxi_musb_bs_r_2;
    385  1.2  jmcneill 	msc->sc_bs.bs_w_1 = sunxi_musb_bs_w_1;
    386  1.2  jmcneill 	msc->sc_bs.bs_w_2 = sunxi_musb_bs_w_2;
    387  1.2  jmcneill 	msc->sc_bs.bs_rm_1 = sunxi_musb_bs_rm_1;
    388  1.2  jmcneill 	msc->sc_bs.bs_rm_4 = sunxi_musb_bs_rm_4;
    389  1.2  jmcneill 	msc->sc_bs.bs_wm_1 = sunxi_musb_bs_wm_1;
    390  1.2  jmcneill 	msc->sc_bs.bs_wm_4 = sunxi_musb_bs_wm_4;
    391  1.2  jmcneill 	msc->sc_bs.bs_barrier = sunxi_musb_bs_barrier;
    392  1.2  jmcneill 
    393  1.1  jmcneill 	sc->sc_dev = self;
    394  1.1  jmcneill 	sc->sc_bus.ub_hcpriv = sc;
    395  1.1  jmcneill 	sc->sc_bus.ub_dmatag = faa->faa_dmat;
    396  1.1  jmcneill 	strlcpy(sc->sc_vendor, "Allwinner", sizeof(sc->sc_vendor));
    397  1.1  jmcneill 	sc->sc_size = size;
    398  1.2  jmcneill 	sc->sc_iot = &msc->sc_bs;
    399  1.2  jmcneill 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) {
    400  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    401  1.1  jmcneill 		return;
    402  1.1  jmcneill 	}
    403  1.1  jmcneill 	sc->sc_intr_poll = sunxi_musb_poll;
    404  1.1  jmcneill 	sc->sc_intr_poll_arg = sc;
    405  1.1  jmcneill 	sc->sc_mode = MOTG_MODE_HOST;
    406  1.1  jmcneill 	sc->sc_ep_max = of_search_compatible(phandle, compat_data)->data;
    407  1.1  jmcneill 	sc->sc_ep_fifosize = 512;
    408  1.1  jmcneill 
    409  1.1  jmcneill 	aprint_naive("\n");
    410  1.1  jmcneill 	aprint_normal(": USB OTG\n");
    411  1.1  jmcneill 
    412  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    413  1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    414  1.1  jmcneill 		return;
    415  1.1  jmcneill 	}
    416  1.1  jmcneill 
    417  1.1  jmcneill 	ih = fdtbus_intr_establish(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
    418  1.1  jmcneill 	    sunxi_musb_intr, sc);
    419  1.1  jmcneill 	if (ih == NULL) {
    420  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    421  1.1  jmcneill 		    intrstr);
    422  1.1  jmcneill 		return;
    423  1.1  jmcneill 	}
    424  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    425  1.1  jmcneill 
    426  1.1  jmcneill 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_AWIN_VEND0, 0);
    427  1.1  jmcneill 
    428  1.1  jmcneill 	motg_init(sc);
    429  1.1  jmcneill }
    430