1029e367dSAndriy Gapon /*- 2029e367dSAndriy Gapon * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca> 3029e367dSAndriy Gapon * Copyright (c) 2018 Andrew Turner <andrew@FreeBSD.org> 4029e367dSAndriy Gapon * All rights reserved. 5029e367dSAndriy Gapon * 6029e367dSAndriy Gapon * This software was developed by SRI International and the University of 7029e367dSAndriy Gapon * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237 8029e367dSAndriy Gapon * ("CTSRD"), as part of the DARPA CRASH research programme. 9029e367dSAndriy Gapon * 10029e367dSAndriy Gapon * Redistribution and use in source and binary forms, with or without 11029e367dSAndriy Gapon * modification, are permitted provided that the following conditions 12029e367dSAndriy Gapon * are met: 13029e367dSAndriy Gapon * 1. Redistributions of source code must retain the above copyright 14029e367dSAndriy Gapon * notice, this list of conditions and the following disclaimer. 15029e367dSAndriy Gapon * 2. Redistributions in binary form must reproduce the above copyright 16029e367dSAndriy Gapon * notice, this list of conditions and the following disclaimer in the 17029e367dSAndriy Gapon * documentation and/or other materials provided with the distribution. 18029e367dSAndriy Gapon * 19029e367dSAndriy Gapon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20029e367dSAndriy Gapon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21029e367dSAndriy Gapon * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22029e367dSAndriy Gapon * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23029e367dSAndriy Gapon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24029e367dSAndriy Gapon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25029e367dSAndriy Gapon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26029e367dSAndriy Gapon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27029e367dSAndriy Gapon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28029e367dSAndriy Gapon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29029e367dSAndriy Gapon * SUCH DAMAGE. 30029e367dSAndriy Gapon * 31029e367dSAndriy Gapon * $FreeBSD$ 32029e367dSAndriy Gapon */ 33029e367dSAndriy Gapon 34029e367dSAndriy Gapon /* 35029e367dSAndriy Gapon * Allwinner USB Dual-Role Device (DRD) controller 36029e367dSAndriy Gapon */ 37029e367dSAndriy Gapon 38029e367dSAndriy Gapon #include <sys/cdefs.h> 39029e367dSAndriy Gapon __FBSDID("$FreeBSD$"); 40029e367dSAndriy Gapon 41029e367dSAndriy Gapon #include <sys/param.h> 42029e367dSAndriy Gapon #include <sys/systm.h> 43029e367dSAndriy Gapon #include <sys/bus.h> 44029e367dSAndriy Gapon #include <sys/rman.h> 45029e367dSAndriy Gapon #include <sys/kernel.h> 46029e367dSAndriy Gapon #include <sys/condvar.h> 47029e367dSAndriy Gapon #include <sys/module.h> 48029e367dSAndriy Gapon #include <machine/bus.h> 49029e367dSAndriy Gapon 50029e367dSAndriy Gapon #include <dev/ofw/ofw_bus.h> 51029e367dSAndriy Gapon #include <dev/ofw/ofw_bus_subr.h> 52029e367dSAndriy Gapon 53029e367dSAndriy Gapon #include <dev/usb/usb.h> 54029e367dSAndriy Gapon #include <dev/usb/usbdi.h> 55029e367dSAndriy Gapon 56029e367dSAndriy Gapon #include <dev/usb/usb_core.h> 57029e367dSAndriy Gapon #include <dev/usb/usb_busdma.h> 58029e367dSAndriy Gapon #include <dev/usb/usb_process.h> 59029e367dSAndriy Gapon #include <dev/usb/usb_util.h> 60029e367dSAndriy Gapon 61029e367dSAndriy Gapon #include <dev/usb/usb_controller.h> 62029e367dSAndriy Gapon #include <dev/usb/usb_bus.h> 63029e367dSAndriy Gapon #include <dev/usb/controller/musb_otg.h> 64029e367dSAndriy Gapon 65029e367dSAndriy Gapon #include <dev/extres/clk/clk.h> 66029e367dSAndriy Gapon #include <dev/extres/hwreset/hwreset.h> 67029e367dSAndriy Gapon 68029e367dSAndriy Gapon #ifdef __arm__ 69029e367dSAndriy Gapon #include <arm/allwinner/aw_machdep.h> 70029e367dSAndriy Gapon #include <arm/allwinner/a10_sramc.h> 71029e367dSAndriy Gapon #endif 72029e367dSAndriy Gapon 73029e367dSAndriy Gapon #define DRD_EP_MAX 5 74*f2066952SAndriy Gapon #define DRD_EP_MAX_H3 4 75029e367dSAndriy Gapon 76029e367dSAndriy Gapon #define MUSB2_REG_AWIN_VEND0 0x0043 77029e367dSAndriy Gapon #define VEND0_PIO_MODE 0 78029e367dSAndriy Gapon 79029e367dSAndriy Gapon #if defined(__arm__) 80029e367dSAndriy Gapon #define bs_parent_space(bs) ((bs)->bs_parent) 81029e367dSAndriy Gapon typedef bus_space_tag_t awusb_bs_tag; 82029e367dSAndriy Gapon #elif defined(__aarch64__) 83029e367dSAndriy Gapon #define bs_parent_space(bs) (bs) 84029e367dSAndriy Gapon typedef void * awusb_bs_tag; 85029e367dSAndriy Gapon #endif 86029e367dSAndriy Gapon 87029e367dSAndriy Gapon #define AWUSB_OKAY 0x01 88029e367dSAndriy Gapon #define AWUSB_NO_CONFDATA 0x02 89029e367dSAndriy Gapon static struct ofw_compat_data compat_data[] = { 90029e367dSAndriy Gapon { "allwinner,sun4i-a10-musb", AWUSB_OKAY }, 91029e367dSAndriy Gapon { "allwinner,sun6i-a31-musb", AWUSB_OKAY }, 92029e367dSAndriy Gapon { "allwinner,sun8i-a33-musb", AWUSB_OKAY | AWUSB_NO_CONFDATA }, 93*f2066952SAndriy Gapon { "allwinner,sun8i-h3-musb", AWUSB_OKAY | AWUSB_NO_CONFDATA }, 94029e367dSAndriy Gapon { NULL, 0 } 95029e367dSAndriy Gapon }; 96029e367dSAndriy Gapon 97029e367dSAndriy Gapon static const struct musb_otg_ep_cfg musbotg_ep_allwinner[] = { 98029e367dSAndriy Gapon { 99*f2066952SAndriy Gapon .ep_end = DRD_EP_MAX, 100*f2066952SAndriy Gapon .ep_fifosz_shift = 9, 101*f2066952SAndriy Gapon .ep_fifosz_reg = MUSB2_VAL_FIFOSZ_512, 102*f2066952SAndriy Gapon }, 103*f2066952SAndriy Gapon { 104*f2066952SAndriy Gapon .ep_end = -1, 105*f2066952SAndriy Gapon }, 106*f2066952SAndriy Gapon }; 107*f2066952SAndriy Gapon 108*f2066952SAndriy Gapon static const struct musb_otg_ep_cfg musbotg_ep_allwinner_h3[] = { 109*f2066952SAndriy Gapon { 110*f2066952SAndriy Gapon .ep_end = DRD_EP_MAX_H3, 111029e367dSAndriy Gapon .ep_fifosz_shift = 9, 112029e367dSAndriy Gapon .ep_fifosz_reg = MUSB2_VAL_FIFOSZ_512, 113029e367dSAndriy Gapon }, 114029e367dSAndriy Gapon { 115029e367dSAndriy Gapon .ep_end = -1, 116029e367dSAndriy Gapon }, 117029e367dSAndriy Gapon }; 118029e367dSAndriy Gapon 119029e367dSAndriy Gapon struct awusbdrd_softc { 120029e367dSAndriy Gapon struct musbotg_softc sc; 121029e367dSAndriy Gapon struct resource *res[2]; 122029e367dSAndriy Gapon clk_t clk; 123029e367dSAndriy Gapon hwreset_t reset; 124029e367dSAndriy Gapon struct bus_space bs; 125029e367dSAndriy Gapon int flags; 126029e367dSAndriy Gapon }; 127029e367dSAndriy Gapon 128029e367dSAndriy Gapon static struct resource_spec awusbdrd_spec[] = { 129029e367dSAndriy Gapon { SYS_RES_MEMORY, 0, RF_ACTIVE }, 130029e367dSAndriy Gapon { SYS_RES_IRQ, 0, RF_ACTIVE }, 131029e367dSAndriy Gapon { -1, 0 } 132029e367dSAndriy Gapon }; 133029e367dSAndriy Gapon 134029e367dSAndriy Gapon #define REMAPFLAG 0x8000 135029e367dSAndriy Gapon #define REGDECL(a, b) [(a)] = ((b) | REMAPFLAG) 136029e367dSAndriy Gapon 137029e367dSAndriy Gapon /* Allwinner USB DRD register mappings */ 138029e367dSAndriy Gapon static const uint16_t awusbdrd_regmap[] = { 139029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(0), 0x0000), 140029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(1), 0x0004), 141029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(2), 0x0008), 142029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(3), 0x000c), 143029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(4), 0x0010), 144029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(5), 0x0014), 145029e367dSAndriy Gapon REGDECL(MUSB2_REG_POWER, 0x0040), 146029e367dSAndriy Gapon REGDECL(MUSB2_REG_DEVCTL, 0x0041), 147029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPINDEX, 0x0042), 148029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTTX, 0x0044), 149029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTRX, 0x0046), 150029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTTXE, 0x0048), 151029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTRXE, 0x004a), 152029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTUSB, 0x004c), 153029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTUSBE, 0x0050), 154029e367dSAndriy Gapon REGDECL(MUSB2_REG_FRAME, 0x0054), 155029e367dSAndriy Gapon REGDECL(MUSB2_REG_TESTMODE, 0x007c), 156029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXMAXP, 0x0080), 157029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXCSRL, 0x0082), 158029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXCSRH, 0x0083), 159029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXMAXP, 0x0084), 160029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXCSRL, 0x0086), 161029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXCSRH, 0x0087), 162029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXCOUNT, 0x0088), 163029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXTI, 0x008c), 164029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXNAKLIMIT, 0x008d), 165029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXNAKLIMIT, 0x008f), 166029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXTI, 0x008e), 167029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFIFOSZ, 0x0090), 168029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFIFOADD, 0x0092), 169029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFIFOSZ, 0x0094), 170029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFIFOADD, 0x0096), 171029e367dSAndriy Gapon REGDECL(MUSB2_REG_FADDR, 0x0098), 172029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(0), 0x0098), 173029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(0), 0x009a), 174029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(0), 0x009b), 175029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(0), 0x009c), 176029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(0), 0x009e), 177029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(0), 0x009f), 178029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(1), 0x0098), 179029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(1), 0x009a), 180029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(1), 0x009b), 181029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(1), 0x009c), 182029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(1), 0x009e), 183029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(1), 0x009f), 184029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(2), 0x0098), 185029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(2), 0x009a), 186029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(2), 0x009b), 187029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(2), 0x009c), 188029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(2), 0x009e), 189029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(2), 0x009f), 190029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(3), 0x0098), 191029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(3), 0x009a), 192029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(3), 0x009b), 193029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(3), 0x009c), 194029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(3), 0x009e), 195029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(3), 0x009f), 196029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(4), 0x0098), 197029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(4), 0x009a), 198029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(4), 0x009b), 199029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(4), 0x009c), 200029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(4), 0x009e), 201029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(4), 0x009f), 202029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(5), 0x0098), 203029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(5), 0x009a), 204029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(5), 0x009b), 205029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(5), 0x009c), 206029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(5), 0x009e), 207029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(5), 0x009f), 208029e367dSAndriy Gapon REGDECL(MUSB2_REG_CONFDATA, 0x00c0), 209029e367dSAndriy Gapon }; 210029e367dSAndriy Gapon 211029e367dSAndriy Gapon static bus_size_t 212029e367dSAndriy Gapon awusbdrd_reg(bus_size_t o) 213029e367dSAndriy Gapon { 214029e367dSAndriy Gapon bus_size_t v; 215029e367dSAndriy Gapon 216029e367dSAndriy Gapon KASSERT(o < nitems(awusbdrd_regmap), 217029e367dSAndriy Gapon ("%s: Invalid register %#lx", __func__, o)); 218029e367dSAndriy Gapon if (o >= nitems(awusbdrd_regmap)) 219029e367dSAndriy Gapon return (o); 220029e367dSAndriy Gapon 221029e367dSAndriy Gapon v = awusbdrd_regmap[o]; 222029e367dSAndriy Gapon 223029e367dSAndriy Gapon KASSERT((v & REMAPFLAG) != 0, ("%s: reg %#lx not in regmap", 224029e367dSAndriy Gapon __func__, o)); 225029e367dSAndriy Gapon 226029e367dSAndriy Gapon return (v & ~REMAPFLAG); 227029e367dSAndriy Gapon } 228029e367dSAndriy Gapon 229029e367dSAndriy Gapon static int 230029e367dSAndriy Gapon awusbdrd_filt(bus_size_t o) 231029e367dSAndriy Gapon { 232029e367dSAndriy Gapon switch (o) { 233029e367dSAndriy Gapon case MUSB2_REG_MISC: 234029e367dSAndriy Gapon case MUSB2_REG_RXDBDIS: 235029e367dSAndriy Gapon case MUSB2_REG_TXDBDIS: 236029e367dSAndriy Gapon return (1); 237029e367dSAndriy Gapon default: 238029e367dSAndriy Gapon return (0); 239029e367dSAndriy Gapon } 240029e367dSAndriy Gapon } 241029e367dSAndriy Gapon 242029e367dSAndriy Gapon static uint8_t 243029e367dSAndriy Gapon awusbdrd_bs_r_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o) 244029e367dSAndriy Gapon { 245029e367dSAndriy Gapon const struct bus_space *bs = t; 246029e367dSAndriy Gapon 247029e367dSAndriy Gapon switch (o) { 248029e367dSAndriy Gapon case MUSB2_REG_HWVERS: 249029e367dSAndriy Gapon return (0); /* no known equivalent */ 250029e367dSAndriy Gapon } 251029e367dSAndriy Gapon 252029e367dSAndriy Gapon return (bus_space_read_1(bs_parent_space(bs), h, awusbdrd_reg(o))); 253029e367dSAndriy Gapon } 254029e367dSAndriy Gapon 255029e367dSAndriy Gapon static uint8_t 256029e367dSAndriy Gapon awusbdrd_bs_r_1_noconf(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o) 257029e367dSAndriy Gapon { 258029e367dSAndriy Gapon 259029e367dSAndriy Gapon /* 260029e367dSAndriy Gapon * There is no confdata register on some SoCs, return the same 261029e367dSAndriy Gapon * magic value as Linux. 262029e367dSAndriy Gapon */ 263029e367dSAndriy Gapon if (o == MUSB2_REG_CONFDATA) 264029e367dSAndriy Gapon return (0xde); 265029e367dSAndriy Gapon 266029e367dSAndriy Gapon return (awusbdrd_bs_r_1(t, h, o)); 267029e367dSAndriy Gapon } 268029e367dSAndriy Gapon 269029e367dSAndriy Gapon 270029e367dSAndriy Gapon static uint16_t 271029e367dSAndriy Gapon awusbdrd_bs_r_2(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o) 272029e367dSAndriy Gapon { 273029e367dSAndriy Gapon const struct bus_space *bs = t; 274029e367dSAndriy Gapon 275d7abf6e7SAndriy Gapon if (awusbdrd_filt(o) != 0) 276d7abf6e7SAndriy Gapon return (0); 277029e367dSAndriy Gapon return bus_space_read_2(bs_parent_space(bs), h, awusbdrd_reg(o)); 278029e367dSAndriy Gapon } 279029e367dSAndriy Gapon 280029e367dSAndriy Gapon static void 281029e367dSAndriy Gapon awusbdrd_bs_w_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o, 282029e367dSAndriy Gapon uint8_t v) 283029e367dSAndriy Gapon { 284029e367dSAndriy Gapon const struct bus_space *bs = t; 285029e367dSAndriy Gapon 286029e367dSAndriy Gapon if (awusbdrd_filt(o) != 0) 287029e367dSAndriy Gapon return; 288029e367dSAndriy Gapon 289029e367dSAndriy Gapon bus_space_write_1(bs_parent_space(bs), h, awusbdrd_reg(o), v); 290029e367dSAndriy Gapon } 291029e367dSAndriy Gapon 292029e367dSAndriy Gapon static void 293029e367dSAndriy Gapon awusbdrd_bs_w_2(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o, 294029e367dSAndriy Gapon uint16_t v) 295029e367dSAndriy Gapon { 296029e367dSAndriy Gapon const struct bus_space *bs = t; 297029e367dSAndriy Gapon 298029e367dSAndriy Gapon if (awusbdrd_filt(o) != 0) 299029e367dSAndriy Gapon return; 300029e367dSAndriy Gapon 301029e367dSAndriy Gapon bus_space_write_2(bs_parent_space(bs), h, awusbdrd_reg(o), v); 302029e367dSAndriy Gapon } 303029e367dSAndriy Gapon 304029e367dSAndriy Gapon static void 305029e367dSAndriy Gapon awusbdrd_bs_rm_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o, 306029e367dSAndriy Gapon uint8_t *d, bus_size_t c) 307029e367dSAndriy Gapon { 308029e367dSAndriy Gapon const struct bus_space *bs = t; 309029e367dSAndriy Gapon 310029e367dSAndriy Gapon bus_space_read_multi_1(bs_parent_space(bs), h, awusbdrd_reg(o), d, c); 311029e367dSAndriy Gapon } 312029e367dSAndriy Gapon 313029e367dSAndriy Gapon static void 314029e367dSAndriy Gapon awusbdrd_bs_rm_4(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o, 315029e367dSAndriy Gapon uint32_t *d, bus_size_t c) 316029e367dSAndriy Gapon { 317029e367dSAndriy Gapon const struct bus_space *bs = t; 318029e367dSAndriy Gapon 319029e367dSAndriy Gapon bus_space_read_multi_4(bs_parent_space(bs), h, awusbdrd_reg(o), d, c); 320029e367dSAndriy Gapon } 321029e367dSAndriy Gapon 322029e367dSAndriy Gapon static void 323029e367dSAndriy Gapon awusbdrd_bs_wm_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o, 324029e367dSAndriy Gapon const uint8_t *d, bus_size_t c) 325029e367dSAndriy Gapon { 326029e367dSAndriy Gapon const struct bus_space *bs = t; 327029e367dSAndriy Gapon 328029e367dSAndriy Gapon if (awusbdrd_filt(o) != 0) 329029e367dSAndriy Gapon return; 330029e367dSAndriy Gapon 331029e367dSAndriy Gapon bus_space_write_multi_1(bs_parent_space(bs), h, awusbdrd_reg(o), d, c); 332029e367dSAndriy Gapon } 333029e367dSAndriy Gapon 334029e367dSAndriy Gapon static void 335029e367dSAndriy Gapon awusbdrd_bs_wm_4(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o, 336029e367dSAndriy Gapon const uint32_t *d, bus_size_t c) 337029e367dSAndriy Gapon { 338029e367dSAndriy Gapon const struct bus_space *bs = t; 339029e367dSAndriy Gapon 340029e367dSAndriy Gapon if (awusbdrd_filt(o) != 0) 341029e367dSAndriy Gapon return; 342029e367dSAndriy Gapon 343029e367dSAndriy Gapon bus_space_write_multi_4(bs_parent_space(bs), h, awusbdrd_reg(o), d, c); 344029e367dSAndriy Gapon } 345029e367dSAndriy Gapon 346029e367dSAndriy Gapon static void 347029e367dSAndriy Gapon awusbdrd_intr(void *arg) 348029e367dSAndriy Gapon { 349029e367dSAndriy Gapon struct awusbdrd_softc *sc = arg; 350029e367dSAndriy Gapon uint8_t intusb; 351029e367dSAndriy Gapon uint16_t inttx, intrx; 352029e367dSAndriy Gapon 353029e367dSAndriy Gapon intusb = MUSB2_READ_1(&sc->sc, MUSB2_REG_INTUSB); 354029e367dSAndriy Gapon inttx = MUSB2_READ_2(&sc->sc, MUSB2_REG_INTTX); 355029e367dSAndriy Gapon intrx = MUSB2_READ_2(&sc->sc, MUSB2_REG_INTRX); 356029e367dSAndriy Gapon if (intusb == 0 && inttx == 0 && intrx == 0) 357029e367dSAndriy Gapon return; 358029e367dSAndriy Gapon 359029e367dSAndriy Gapon if (intusb) 360029e367dSAndriy Gapon MUSB2_WRITE_1(&sc->sc, MUSB2_REG_INTUSB, intusb); 361029e367dSAndriy Gapon if (inttx) 362029e367dSAndriy Gapon MUSB2_WRITE_2(&sc->sc, MUSB2_REG_INTTX, inttx); 363029e367dSAndriy Gapon if (intrx) 364029e367dSAndriy Gapon MUSB2_WRITE_2(&sc->sc, MUSB2_REG_INTRX, intrx); 365029e367dSAndriy Gapon 366029e367dSAndriy Gapon musbotg_interrupt(arg, intrx, inttx, intusb); 367029e367dSAndriy Gapon } 368029e367dSAndriy Gapon 369029e367dSAndriy Gapon static int 370029e367dSAndriy Gapon awusbdrd_probe(device_t dev) 371029e367dSAndriy Gapon { 372029e367dSAndriy Gapon if (!ofw_bus_status_okay(dev)) 373029e367dSAndriy Gapon return (ENXIO); 374029e367dSAndriy Gapon 375029e367dSAndriy Gapon if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 376029e367dSAndriy Gapon return (ENXIO); 377029e367dSAndriy Gapon 378029e367dSAndriy Gapon device_set_desc(dev, "Allwinner USB DRD"); 379029e367dSAndriy Gapon return (BUS_PROBE_DEFAULT); 380029e367dSAndriy Gapon } 381029e367dSAndriy Gapon 382029e367dSAndriy Gapon static int 383029e367dSAndriy Gapon awusbdrd_attach(device_t dev) 384029e367dSAndriy Gapon { 385029e367dSAndriy Gapon struct awusbdrd_softc *sc; 386029e367dSAndriy Gapon int error; 387029e367dSAndriy Gapon 388029e367dSAndriy Gapon sc = device_get_softc(dev); 389029e367dSAndriy Gapon sc->flags = ofw_bus_search_compatible(dev, compat_data)->ocd_data; 390029e367dSAndriy Gapon 391029e367dSAndriy Gapon error = bus_alloc_resources(dev, awusbdrd_spec, sc->res); 392029e367dSAndriy Gapon if (error != 0) 393029e367dSAndriy Gapon return (error); 394029e367dSAndriy Gapon 395029e367dSAndriy Gapon /* AHB gate clock is required */ 396029e367dSAndriy Gapon error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk); 397029e367dSAndriy Gapon if (error != 0) 398029e367dSAndriy Gapon goto fail; 399029e367dSAndriy Gapon 400029e367dSAndriy Gapon /* AHB reset is only present on some SoCs */ 401029e367dSAndriy Gapon (void)hwreset_get_by_ofw_idx(dev, 0, 0, &sc->reset); 402029e367dSAndriy Gapon 403029e367dSAndriy Gapon /* Enable clocks */ 404029e367dSAndriy Gapon error = clk_enable(sc->clk); 405029e367dSAndriy Gapon if (error != 0) { 406029e367dSAndriy Gapon device_printf(dev, "failed to enable clock: %d\n", error); 407029e367dSAndriy Gapon goto fail; 408029e367dSAndriy Gapon } 409029e367dSAndriy Gapon if (sc->reset != NULL) { 410029e367dSAndriy Gapon error = hwreset_deassert(sc->reset); 411029e367dSAndriy Gapon if (error != 0) { 412029e367dSAndriy Gapon device_printf(dev, "failed to de-assert reset: %d\n", 413029e367dSAndriy Gapon error); 414029e367dSAndriy Gapon goto fail; 415029e367dSAndriy Gapon } 416029e367dSAndriy Gapon } 417029e367dSAndriy Gapon 418029e367dSAndriy Gapon sc->sc.sc_bus.parent = dev; 419029e367dSAndriy Gapon sc->sc.sc_bus.devices = sc->sc.sc_devices; 420029e367dSAndriy Gapon sc->sc.sc_bus.devices_max = MUSB2_MAX_DEVICES; 421029e367dSAndriy Gapon sc->sc.sc_bus.dma_bits = 32; 422029e367dSAndriy Gapon 423029e367dSAndriy Gapon error = usb_bus_mem_alloc_all(&sc->sc.sc_bus, USB_GET_DMA_TAG(dev), 424029e367dSAndriy Gapon NULL); 425029e367dSAndriy Gapon if (error != 0) { 426029e367dSAndriy Gapon error = ENOMEM; 427029e367dSAndriy Gapon goto fail; 428029e367dSAndriy Gapon } 429029e367dSAndriy Gapon 430029e367dSAndriy Gapon #if defined(__arm__) 431029e367dSAndriy Gapon sc->bs.bs_parent = rman_get_bustag(sc->res[0]); 432029e367dSAndriy Gapon #elif defined(__aarch64__) 433029e367dSAndriy Gapon sc->bs.bs_cookie = rman_get_bustag(sc->res[0]); 434029e367dSAndriy Gapon #endif 435029e367dSAndriy Gapon 436029e367dSAndriy Gapon if ((sc->flags & AWUSB_NO_CONFDATA) == AWUSB_NO_CONFDATA) 437029e367dSAndriy Gapon sc->bs.bs_r_1 = awusbdrd_bs_r_1_noconf; 438029e367dSAndriy Gapon else 439029e367dSAndriy Gapon sc->bs.bs_r_1 = awusbdrd_bs_r_1; 440029e367dSAndriy Gapon sc->bs.bs_r_2 = awusbdrd_bs_r_2; 441029e367dSAndriy Gapon sc->bs.bs_w_1 = awusbdrd_bs_w_1; 442029e367dSAndriy Gapon sc->bs.bs_w_2 = awusbdrd_bs_w_2; 443029e367dSAndriy Gapon sc->bs.bs_rm_1 = awusbdrd_bs_rm_1; 444029e367dSAndriy Gapon sc->bs.bs_rm_4 = awusbdrd_bs_rm_4; 445029e367dSAndriy Gapon sc->bs.bs_wm_1 = awusbdrd_bs_wm_1; 446029e367dSAndriy Gapon sc->bs.bs_wm_4 = awusbdrd_bs_wm_4; 447029e367dSAndriy Gapon 448029e367dSAndriy Gapon sc->sc.sc_io_tag = &sc->bs; 449029e367dSAndriy Gapon sc->sc.sc_io_hdl = rman_get_bushandle(sc->res[0]); 450029e367dSAndriy Gapon sc->sc.sc_io_size = rman_get_size(sc->res[0]); 451029e367dSAndriy Gapon 452029e367dSAndriy Gapon sc->sc.sc_bus.bdev = device_add_child(dev, "usbus", -1); 453029e367dSAndriy Gapon if (sc->sc.sc_bus.bdev == NULL) { 454029e367dSAndriy Gapon error = ENXIO; 455029e367dSAndriy Gapon goto fail; 456029e367dSAndriy Gapon } 457029e367dSAndriy Gapon device_set_ivars(sc->sc.sc_bus.bdev, &sc->sc.sc_bus); 458029e367dSAndriy Gapon sc->sc.sc_id = 0; 459029e367dSAndriy Gapon sc->sc.sc_platform_data = sc; 460029e367dSAndriy Gapon sc->sc.sc_mode = MUSB2_HOST_MODE; /* XXX HOST vs DEVICE mode */ 461*f2066952SAndriy Gapon if (ofw_bus_is_compatible(dev, "allwinner,sun8i-h3-musb")) { 462*f2066952SAndriy Gapon sc->sc.sc_ep_cfg = musbotg_ep_allwinner_h3; 463*f2066952SAndriy Gapon sc->sc.sc_ep_max = DRD_EP_MAX_H3; 464*f2066952SAndriy Gapon } else { 465029e367dSAndriy Gapon sc->sc.sc_ep_cfg = musbotg_ep_allwinner; 466*f2066952SAndriy Gapon sc->sc.sc_ep_max = DRD_EP_MAX; 467*f2066952SAndriy Gapon } 468029e367dSAndriy Gapon 469029e367dSAndriy Gapon error = bus_setup_intr(dev, sc->res[1], INTR_MPSAFE | INTR_TYPE_BIO, 470029e367dSAndriy Gapon NULL, awusbdrd_intr, sc, &sc->sc.sc_intr_hdl); 471029e367dSAndriy Gapon if (error != 0) 472029e367dSAndriy Gapon goto fail; 473029e367dSAndriy Gapon 474029e367dSAndriy Gapon /* Enable PIO mode */ 475029e367dSAndriy Gapon bus_write_1(sc->res[0], MUSB2_REG_AWIN_VEND0, VEND0_PIO_MODE); 476029e367dSAndriy Gapon 477029e367dSAndriy Gapon #ifdef __arm__ 478029e367dSAndriy Gapon /* Map SRAMD area to USB0 (sun4i/sun7i only) */ 479029e367dSAndriy Gapon switch (allwinner_soc_family()) { 480029e367dSAndriy Gapon case ALLWINNERSOC_SUN4I: 481029e367dSAndriy Gapon case ALLWINNERSOC_SUN7I: 482029e367dSAndriy Gapon a10_map_to_otg(); 483029e367dSAndriy Gapon break; 484029e367dSAndriy Gapon } 485029e367dSAndriy Gapon #endif 486029e367dSAndriy Gapon 487029e367dSAndriy Gapon error = musbotg_init(&sc->sc); 488029e367dSAndriy Gapon if (error != 0) 489029e367dSAndriy Gapon goto fail; 490029e367dSAndriy Gapon 491029e367dSAndriy Gapon error = device_probe_and_attach(sc->sc.sc_bus.bdev); 492029e367dSAndriy Gapon if (error != 0) 493029e367dSAndriy Gapon goto fail; 494029e367dSAndriy Gapon 495029e367dSAndriy Gapon musbotg_vbus_interrupt(&sc->sc, 1); /* XXX VBUS */ 496029e367dSAndriy Gapon 497029e367dSAndriy Gapon return (0); 498029e367dSAndriy Gapon 499029e367dSAndriy Gapon fail: 500029e367dSAndriy Gapon if (sc->reset != NULL) 501029e367dSAndriy Gapon hwreset_release(sc->reset); 502029e367dSAndriy Gapon if (sc->clk != NULL) 503029e367dSAndriy Gapon clk_release(sc->clk); 504029e367dSAndriy Gapon bus_release_resources(dev, awusbdrd_spec, sc->res); 505029e367dSAndriy Gapon return (error); 506029e367dSAndriy Gapon } 507029e367dSAndriy Gapon 508029e367dSAndriy Gapon static int 509029e367dSAndriy Gapon awusbdrd_detach(device_t dev) 510029e367dSAndriy Gapon { 511029e367dSAndriy Gapon struct awusbdrd_softc *sc; 512029e367dSAndriy Gapon device_t bdev; 513029e367dSAndriy Gapon int error; 514029e367dSAndriy Gapon 515029e367dSAndriy Gapon sc = device_get_softc(dev); 516029e367dSAndriy Gapon 517029e367dSAndriy Gapon if (sc->sc.sc_bus.bdev != NULL) { 518029e367dSAndriy Gapon bdev = sc->sc.sc_bus.bdev; 519029e367dSAndriy Gapon device_detach(bdev); 520029e367dSAndriy Gapon device_delete_child(dev, bdev); 521029e367dSAndriy Gapon } 522029e367dSAndriy Gapon 523029e367dSAndriy Gapon musbotg_uninit(&sc->sc); 524029e367dSAndriy Gapon error = bus_teardown_intr(dev, sc->res[1], sc->sc.sc_intr_hdl); 525029e367dSAndriy Gapon if (error != 0) 526029e367dSAndriy Gapon return (error); 527029e367dSAndriy Gapon 528029e367dSAndriy Gapon usb_bus_mem_free_all(&sc->sc.sc_bus, NULL); 529029e367dSAndriy Gapon 530029e367dSAndriy Gapon if (sc->reset != NULL) 531029e367dSAndriy Gapon hwreset_release(sc->reset); 532029e367dSAndriy Gapon if (sc->clk != NULL) 533029e367dSAndriy Gapon clk_release(sc->clk); 534029e367dSAndriy Gapon 535029e367dSAndriy Gapon bus_release_resources(dev, awusbdrd_spec, sc->res); 536029e367dSAndriy Gapon 537029e367dSAndriy Gapon device_delete_children(dev); 538029e367dSAndriy Gapon 539029e367dSAndriy Gapon return (0); 540029e367dSAndriy Gapon } 541029e367dSAndriy Gapon 542029e367dSAndriy Gapon static device_method_t awusbdrd_methods[] = { 543029e367dSAndriy Gapon /* Device interface */ 544029e367dSAndriy Gapon DEVMETHOD(device_probe, awusbdrd_probe), 545029e367dSAndriy Gapon DEVMETHOD(device_attach, awusbdrd_attach), 546029e367dSAndriy Gapon DEVMETHOD(device_detach, awusbdrd_detach), 547029e367dSAndriy Gapon DEVMETHOD(device_suspend, bus_generic_suspend), 548029e367dSAndriy Gapon DEVMETHOD(device_resume, bus_generic_resume), 549029e367dSAndriy Gapon DEVMETHOD(device_shutdown, bus_generic_shutdown), 550029e367dSAndriy Gapon 551029e367dSAndriy Gapon DEVMETHOD_END 552029e367dSAndriy Gapon }; 553029e367dSAndriy Gapon 554029e367dSAndriy Gapon static driver_t awusbdrd_driver = { 555029e367dSAndriy Gapon .name = "musbotg", 556029e367dSAndriy Gapon .methods = awusbdrd_methods, 557029e367dSAndriy Gapon .size = sizeof(struct awusbdrd_softc), 558029e367dSAndriy Gapon }; 559029e367dSAndriy Gapon 560029e367dSAndriy Gapon static devclass_t awusbdrd_devclass; 561029e367dSAndriy Gapon 562029e367dSAndriy Gapon DRIVER_MODULE(musbotg, simplebus, awusbdrd_driver, awusbdrd_devclass, 0, 0); 563029e367dSAndriy Gapon MODULE_DEPEND(musbotg, usb, 1, 1, 1); 564