Home | History | Annotate | Line # | Download | only in sunxi
sunxi_musb.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: sunxi_musb.c,v 1.1 2017/09/09 12:01:04 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_musb.c,v 1.1 2017/09/09 12:01:04 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.1  jmcneill 
     45  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #define	MUSB2_REG_AWIN_VEND0	0x43
     48  1.1  jmcneill #define	MUSB2_REG_INTTX		0x44
     49  1.1  jmcneill #define	MUSB2_REG_INTRX		0x46
     50  1.1  jmcneill #define	MUSB2_REG_INTUSB	0x4c
     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.1  jmcneill CFATTACH_DECL_NEW(sunxi_musb, sizeof(struct motg_softc),
     56  1.1  jmcneill 	sunxi_musb_match, sunxi_musb_attach, NULL, NULL);
     57  1.1  jmcneill 
     58  1.1  jmcneill static const struct of_compat_data compat_data[] = {
     59  1.1  jmcneill 	{ "allwinner,sun4i-a10-musb",		5 },
     60  1.1  jmcneill 	{ "allwinner,sun6i-a13-musb",		5 },
     61  1.1  jmcneill 	{ "allwinner,sun8i-h3-musb",		4 },
     62  1.1  jmcneill 	{ "allwinner,sun8i-a33-musb",		5 },
     63  1.1  jmcneill 	{ NULL }
     64  1.1  jmcneill };
     65  1.1  jmcneill 
     66  1.1  jmcneill static int
     67  1.1  jmcneill sunxi_musb_intr(void *priv)
     68  1.1  jmcneill {
     69  1.1  jmcneill 	struct motg_softc * const sc = priv;
     70  1.1  jmcneill 	uint16_t inttx, intrx;
     71  1.1  jmcneill 	uint8_t intusb;
     72  1.1  jmcneill 
     73  1.1  jmcneill 	mutex_enter(&sc->sc_intr_lock);
     74  1.1  jmcneill 
     75  1.1  jmcneill 	intusb = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTUSB);
     76  1.1  jmcneill 	inttx = bus_space_read_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTTX);
     77  1.1  jmcneill 	intrx = bus_space_read_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTRX);
     78  1.1  jmcneill 	if (!intusb && !inttx && !intrx) {
     79  1.1  jmcneill 		mutex_exit(&sc->sc_intr_lock);
     80  1.1  jmcneill 		return 0;
     81  1.1  jmcneill 	}
     82  1.1  jmcneill 
     83  1.1  jmcneill 	if (intusb)
     84  1.1  jmcneill 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTUSB, intusb);
     85  1.1  jmcneill 	if (inttx)
     86  1.1  jmcneill 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTTX, inttx);
     87  1.1  jmcneill 	if (intrx)
     88  1.1  jmcneill 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTRX, intrx);
     89  1.1  jmcneill 
     90  1.1  jmcneill 	motg_intr(sc, intrx, inttx, intusb);
     91  1.1  jmcneill 
     92  1.1  jmcneill 	mutex_exit(&sc->sc_intr_lock);
     93  1.1  jmcneill 
     94  1.1  jmcneill 	return 1;
     95  1.1  jmcneill }
     96  1.1  jmcneill 
     97  1.1  jmcneill static void
     98  1.1  jmcneill sunxi_musb_poll(void *priv)
     99  1.1  jmcneill {
    100  1.1  jmcneill 	sunxi_musb_intr(priv);
    101  1.1  jmcneill }
    102  1.1  jmcneill 
    103  1.1  jmcneill static int
    104  1.1  jmcneill sunxi_musb_match(device_t parent, cfdata_t cf, void *aux)
    105  1.1  jmcneill {
    106  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    107  1.1  jmcneill 
    108  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    109  1.1  jmcneill }
    110  1.1  jmcneill 
    111  1.1  jmcneill static void
    112  1.1  jmcneill sunxi_musb_attach(device_t parent, device_t self, void *aux)
    113  1.1  jmcneill {
    114  1.1  jmcneill 	struct motg_softc * const sc = device_private(self);
    115  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    116  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    117  1.1  jmcneill 	struct fdtbus_reset *rst;
    118  1.1  jmcneill 	struct fdtbus_phy *phy;
    119  1.1  jmcneill 	struct clk *clk;
    120  1.1  jmcneill 	char intrstr[128];
    121  1.1  jmcneill 	const char *dr_mode;
    122  1.1  jmcneill 	bus_addr_t addr;
    123  1.1  jmcneill 	bus_size_t size;
    124  1.1  jmcneill 	void *ih;
    125  1.1  jmcneill 	u_int n;
    126  1.1  jmcneill 
    127  1.1  jmcneill 	/* Only "host" mode is supported */
    128  1.1  jmcneill 	dr_mode = fdtbus_get_string(phandle, "dr_mode");
    129  1.1  jmcneill 	if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
    130  1.1  jmcneill 		aprint_normal(": '%s' mode not supported\n", dr_mode);
    131  1.1  jmcneill 		return;
    132  1.1  jmcneill 	}
    133  1.1  jmcneill 
    134  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    135  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    136  1.1  jmcneill 		return;
    137  1.1  jmcneill 	}
    138  1.1  jmcneill 
    139  1.1  jmcneill 	/* Enable clocks */
    140  1.1  jmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
    141  1.1  jmcneill 		if (clk_enable(clk) != 0) {
    142  1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
    143  1.1  jmcneill 			return;
    144  1.1  jmcneill 		}
    145  1.1  jmcneill 	/* De-assert resets */
    146  1.1  jmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
    147  1.1  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    148  1.1  jmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
    149  1.1  jmcneill 			return;
    150  1.1  jmcneill 		}
    151  1.1  jmcneill 
    152  1.1  jmcneill 	/* Enable optional phy */
    153  1.1  jmcneill 	phy = fdtbus_phy_get(phandle, "usb");
    154  1.1  jmcneill 	if (phy && fdtbus_phy_enable(phy, true) != 0) {
    155  1.1  jmcneill 		aprint_error(": couldn't enable phy\n");
    156  1.1  jmcneill 		return;
    157  1.1  jmcneill 	}
    158  1.1  jmcneill 
    159  1.1  jmcneill 	sc->sc_dev = self;
    160  1.1  jmcneill 	sc->sc_bus.ub_hcpriv = sc;
    161  1.1  jmcneill 	sc->sc_bus.ub_dmatag = faa->faa_dmat;
    162  1.1  jmcneill 	strlcpy(sc->sc_vendor, "Allwinner", sizeof(sc->sc_vendor));
    163  1.1  jmcneill 	sc->sc_size = size;
    164  1.1  jmcneill 	sc->sc_iot = faa->faa_bst;
    165  1.1  jmcneill 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
    166  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    167  1.1  jmcneill 		return;
    168  1.1  jmcneill 	}
    169  1.1  jmcneill 	sc->sc_intr_poll = sunxi_musb_poll;
    170  1.1  jmcneill 	sc->sc_intr_poll_arg = sc;
    171  1.1  jmcneill 	sc->sc_mode = MOTG_MODE_HOST;
    172  1.1  jmcneill 	sc->sc_ep_max = of_search_compatible(phandle, compat_data)->data;
    173  1.1  jmcneill 	sc->sc_ep_fifosize = 512;
    174  1.1  jmcneill 
    175  1.1  jmcneill 	aprint_naive("\n");
    176  1.1  jmcneill 	aprint_normal(": USB OTG\n");
    177  1.1  jmcneill 
    178  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    179  1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    180  1.1  jmcneill 		return;
    181  1.1  jmcneill 	}
    182  1.1  jmcneill 
    183  1.1  jmcneill 	ih = fdtbus_intr_establish(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
    184  1.1  jmcneill 	    sunxi_musb_intr, sc);
    185  1.1  jmcneill 	if (ih == NULL) {
    186  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    187  1.1  jmcneill 		    intrstr);
    188  1.1  jmcneill 		return;
    189  1.1  jmcneill 	}
    190  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    191  1.1  jmcneill 
    192  1.1  jmcneill 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_AWIN_VEND0, 0);
    193  1.1  jmcneill 
    194  1.1  jmcneill 	motg_init(sc);
    195  1.1  jmcneill }
    196