29#define DWC2_COMMON_DEBUG 2
31#if defined(TUP_USBIP_DWC2) && (CFG_TUH_ENABLED || CFG_TUD_ENABLED)
50 if ((dwc2->
gsnpsid & DWC2_CORE_REV_MASK) < (DWC2_CORE_REV_4_20a & DWC2_CORE_REV_MASK)) {
52 while (dwc2->
grstctl & GRSTCTL_CSRST) {}
56 while (!(dwc2->
grstctl & GRSTCTL_CSRST_DONE)) {}
57 dwc2->
grstctl = (dwc2->
grstctl & ~GRSTCTL_CSRST) | GRSTCTL_CSRST_DONE;
60 while (!(dwc2->
grstctl & GRSTCTL_AHBIDL)) {}
64 TU_LOG(DWC2_COMMON_DEBUG,
"Fullspeed PHY init\r\n");
66 uint32_t gusbcfg = dwc2->
gusbcfg;
69 gusbcfg |= GUSBCFG_PHYSEL;
81 gusbcfg &= ~GUSBCFG_TRDT_Msk;
82 gusbcfg |= 5u << GUSBCFG_TRDT_Pos;
90 uint32_t gusbcfg = dwc2->
gusbcfg;
93 gusbcfg &= ~GUSBCFG_PHYSEL;
96 TU_LOG(DWC2_COMMON_DEBUG,
"Highspeed ULPI PHY init\r\n");
99 gusbcfg |= GUSBCFG_ULPI_UTMI_SEL;
102 gusbcfg &= ~GUSBCFG_PHYIF16;
105 gusbcfg &= ~GUSBCFG_DDRSEL;
108 gusbcfg &= ~(GUSBCFG_ULPIEVBUSD | GUSBCFG_ULPIEVBUSI);
111 gusbcfg &= ~(GUSBCFG_ULPIFSLS | GUSBCFG_ULPICSM);
113 TU_LOG(DWC2_COMMON_DEBUG,
"Highspeed UTMI+ PHY init\r\n");
116 gusbcfg &= ~GUSBCFG_ULPI_UTMI_SEL;
120 gusbcfg |= GUSBCFG_PHYIF16;
122 gusbcfg &= ~GUSBCFG_PHYIF16;
138 gusbcfg &= ~GUSBCFG_TRDT_Msk;
147#if CFG_TUSB_DEBUG >= DWC2_COMMON_DEBUG
150 volatile uint32_t
const* p = (
volatile uint32_t
const*) &dwc2->
guid;
151 TU_LOG1(
"guid, gsnpsid, ghwcfg1, ghwcfg2, ghwcfg3, ghwcfg4\r\n");
152 for (
size_t i = 0; i < 5; i++) {
153 TU_LOG1(
"0x%08" PRIX32
", ", p[i]);
155 TU_LOG1(
"0x%08" PRIX32
"\r\n", p[5]);
160#if !TU_CHECK_MCU(OPT_MCU_GD32VF103)
161 enum { GSNPSID_ID_MASK = TU_GENMASK(31, 16) };
162 const uint32_t gsnpsid = dwc2->
gsnpsid & GSNPSID_ID_MASK;
163 TU_ASSERT(gsnpsid == DWC2_OTG_ID || gsnpsid == DWC2_FS_IOT_ID || gsnpsid == DWC2_HS_IOT_ID);
176 if (role == TUSB_ROLE_DEVICE && !TUD_OPT_HIGH_SPEED) {
181 if (role == TUSB_ROLE_HOST && !TUH_OPT_HIGH_SPEED) {
204 dwc2->
gahbcfg &= ~GAHBCFG_GINT;
219 dwc2->
gusbcfg |= (7ul << GUSBCFG_TOCAL_Pos);
222 dwc2->
pcgcctl &= ~(PCGCCTL_STOPPCLK | PCGCCTL_GATEHCLK | PCGCCTL_PWRCLMP | PCGCCTL_RSTPDWNMODULE);
253 const volatile uint32_t* rx_fifo = dwc2->
fifo[0];
256 uint16_t word_count = len >> 2;
257 while (word_count--) {
263 const uint8_t bytes_rem = len & 0x03;
264 if (bytes_rem != 0) {
265 const uint32_t tmp = *rx_fifo;
278 volatile uint32_t* tx_fifo = dwc2->
fifo[fifo_num];
281 uint16_t word_count = len >> 2;
282 while (word_count--) {
288 const uint8_t bytes_rem = len & 0x03;
290 uint32_t tmp_word = src[0];
292 tmp_word |= (src[1] << 8);
295 tmp_word |= (src[2] << 16);
static void dwc2_phy_init(dwc2_regs_t *dwc2, uint8_t hs_phy_type)
static void dwc2_phy_update(dwc2_regs_t *dwc2, uint8_t hs_phy_type)
static void phy_hs_init(dwc2_regs_t *dwc2)
void dfifo_read_packet(dwc2_regs_t *dwc2, uint8_t *dst, uint16_t len)
bool dwc2_core_is_highspeed(dwc2_regs_t *dwc2, tusb_role_t role)
bool dwc2_core_init(uint8_t rhport, bool is_highspeed)
static bool check_dwc2(dwc2_regs_t *dwc2)
static void phy_fs_init(dwc2_regs_t *dwc2)
static void reset_core(dwc2_regs_t *dwc2)
void dfifo_write_packet(dwc2_regs_t *dwc2, uint8_t fifo_num, const uint8_t *src, uint16_t len)
static TU_ATTR_ALWAYS_INLINE dwc2_regs_t * DWC2_REG(uint8_t rhport)
static TU_ATTR_ALWAYS_INLINE void dfifo_flush_tx(dwc2_regs_t *dwc2, uint8_t fnum)
static TU_ATTR_ALWAYS_INLINE void dfifo_flush_rx(dwc2_regs_t *dwc2)
@ GHWCFG2_HSPHY_NOT_SUPPORTED
volatile uint32_t gahbcfg
volatile uint32_t gotgint
volatile uint32_t gusbcfg
volatile uint32_t gsnpsid
volatile dwc2_ghwcfg2_t ghwcfg2_bm
volatile dwc2_ghwcfg4_t ghwcfg4_bm
volatile uint32_t gintmsk
volatile uint32_t fifo[16][0x400]
volatile uint32_t grstctl
volatile uint32_t gintsts
volatile uint32_t pcgcctl
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte1(uint32_t ui32)
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte0(uint32_t ui32)
static TU_ATTR_ALWAYS_INLINE void tu_unaligned_write32(void *mem, uint32_t value)
static TU_ATTR_ALWAYS_INLINE uint32_t tu_unaligned_read32(const void *mem)
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte2(uint32_t ui32)