Open FFBoard
Open source force feedback firmware
dwc2_common.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2024 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#include "tusb_option.h"
28
29#define DWC2_COMMON_DEBUG 2
30
31#if defined(TUP_USBIP_DWC2) && (CFG_TUH_ENABLED || CFG_TUD_ENABLED)
32
33#if CFG_TUD_ENABLED
34#include "device/dcd.h"
35#endif
36
37#if CFG_TUH_ENABLED
38#include "host/hcd.h"
39#endif
40
41#include "dwc2_common.h"
42
43//--------------------------------------------------------------------
44//
45//--------------------------------------------------------------------
46static void reset_core(dwc2_regs_t* dwc2) {
47 // reset core
48 dwc2->grstctl |= GRSTCTL_CSRST;
49
50 if ((dwc2->gsnpsid & DWC2_CORE_REV_MASK) < (DWC2_CORE_REV_4_20a & DWC2_CORE_REV_MASK)) {
51 // prior v42.0 CSRST is self-clearing
52 while (dwc2->grstctl & GRSTCTL_CSRST) {}
53 } else {
54 // From v4.20a CSRST bit is write only, CSRT_DONE (w1c) is introduced for checking.
55 // CSRST must also be explicitly cleared
56 while (!(dwc2->grstctl & GRSTCTL_CSRST_DONE)) {}
57 dwc2->grstctl = (dwc2->grstctl & ~GRSTCTL_CSRST) | GRSTCTL_CSRST_DONE;
58 }
59
60 while (!(dwc2->grstctl & GRSTCTL_AHBIDL)) {} // wait for AHB master IDLE
61}
62
63static void phy_fs_init(dwc2_regs_t* dwc2) {
64 TU_LOG(DWC2_COMMON_DEBUG, "Fullspeed PHY init\r\n");
65
66 uint32_t gusbcfg = dwc2->gusbcfg;
67
68 // Select FS PHY
69 gusbcfg |= GUSBCFG_PHYSEL;
70 dwc2->gusbcfg = gusbcfg;
71
72 // MCU specific PHY init before reset
74
75 // Reset core after selecting PHY
76 reset_core(dwc2);
77
78 // USB turnaround time is critical for certification where long cables and 5-Hubs are used.
79 // So if you need the AHB to run at less than 30 MHz, and if USB turnaround time is not critical,
80 // these bits can be programmed to a larger value. Default is 5
81 gusbcfg &= ~GUSBCFG_TRDT_Msk;
82 gusbcfg |= 5u << GUSBCFG_TRDT_Pos;
83 dwc2->gusbcfg = gusbcfg;
84
85 // MCU specific PHY update post reset
87}
88
89static void phy_hs_init(dwc2_regs_t* dwc2) {
90 uint32_t gusbcfg = dwc2->gusbcfg;
91
92 // De-select FS PHY
93 gusbcfg &= ~GUSBCFG_PHYSEL;
94
96 TU_LOG(DWC2_COMMON_DEBUG, "Highspeed ULPI PHY init\r\n");
97
98 // Select ULPI PHY (external)
99 gusbcfg |= GUSBCFG_ULPI_UTMI_SEL;
100
101 // ULPI is always 8-bit interface
102 gusbcfg &= ~GUSBCFG_PHYIF16;
103
104 // ULPI select single data rate
105 gusbcfg &= ~GUSBCFG_DDRSEL;
106
107 // default internal VBUS Indicator and Drive
108 gusbcfg &= ~(GUSBCFG_ULPIEVBUSD | GUSBCFG_ULPIEVBUSI);
109
110 // Disable FS/LS ULPI
111 gusbcfg &= ~(GUSBCFG_ULPIFSLS | GUSBCFG_ULPICSM);
112 } else {
113 TU_LOG(DWC2_COMMON_DEBUG, "Highspeed UTMI+ PHY init\r\n");
114
115 // Select UTMI+ PHY (internal)
116 gusbcfg &= ~GUSBCFG_ULPI_UTMI_SEL;
117
118 // Set 16-bit interface if supported
119 if (dwc2->ghwcfg4_bm.phy_data_width) {
120 gusbcfg |= GUSBCFG_PHYIF16; // 16 bit
121 } else {
122 gusbcfg &= ~GUSBCFG_PHYIF16; // 8 bit
123 }
124 }
125
126 // Apply config
127 dwc2->gusbcfg = gusbcfg;
128
129 // mcu specific phy init
131
132 // Reset core after selecting PHY
133 reset_core(dwc2);
134
135 // Set turn-around, must after core reset otherwise it will be clear
136 // - 9 if using 8-bit PHY interface
137 // - 5 if using 16-bit PHY interface
138 gusbcfg &= ~GUSBCFG_TRDT_Msk;
139 gusbcfg |= (dwc2->ghwcfg4_bm.phy_data_width ? 5u : 9u) << GUSBCFG_TRDT_Pos;
140 dwc2->gusbcfg = gusbcfg;
141
142 // MCU specific PHY update post reset
144}
145
146static bool check_dwc2(dwc2_regs_t* dwc2) {
147#if CFG_TUSB_DEBUG >= DWC2_COMMON_DEBUG
148 // print guid, gsnpsid, ghwcfg1, ghwcfg2, ghwcfg3, ghwcfg4
149 // Run 'python dwc2_info.py' and check dwc2_info.md for bit-field value and comparison with other ports
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]);
154 }
155 TU_LOG1("0x%08" PRIX32 "\r\n", p[5]);
156#endif
157
158 // For some reason: GD32VF103 gsnpsid and all hwcfg register are always zero (skip it)
159 (void)dwc2;
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);
164#endif
165
166 return true;
167}
168
169//--------------------------------------------------------------------
170//
171//--------------------------------------------------------------------
172bool dwc2_core_is_highspeed(dwc2_regs_t* dwc2, tusb_role_t role) {
173 (void)dwc2;
174
175#if CFG_TUD_ENABLED
176 if (role == TUSB_ROLE_DEVICE && !TUD_OPT_HIGH_SPEED) {
177 return false;
178 }
179#endif
180#if CFG_TUH_ENABLED
181 if (role == TUSB_ROLE_HOST && !TUH_OPT_HIGH_SPEED) {
182 return false;
183 }
184#endif
185
187}
188
189/* dwc2 has several PHYs option
190 * - UTMI+ is internal highspeed PHY, clock can be 30 Mhz (8-bit) or 60 Mhz (16-bit)
191 * - ULPI is external highspeed PHY, clock is 60Mhz with only 8-bit interface
192 * - Dedicated FS PHY is internal with clock 48Mhz.
193 *
194 * In addition, UTMI+/ULPI can be shared to run at fullspeed mode with 48Mhz
195 *
196*/
197bool dwc2_core_init(uint8_t rhport, bool is_highspeed) {
198 dwc2_regs_t* dwc2 = DWC2_REG(rhport);
199
200 // Check Synopsys ID register, failed if controller clock/power is not enabled
201 TU_ASSERT(check_dwc2(dwc2));
202
203 // disable global interrupt
204 dwc2->gahbcfg &= ~GAHBCFG_GINT;
205
206 if (is_highspeed) {
207 phy_hs_init(dwc2);
208 } else {
209 phy_fs_init(dwc2);
210 }
211
212 /* Set HS/FS Timeout Calibration to 7 (max available value).
213 * The number of PHY clocks that the application programs in
214 * this field is added to the high/full speed interpacket timeout
215 * duration in the core to account for any additional delays
216 * introduced by the PHY. This can be required, because the delay
217 * introduced by the PHY in generating the linestate condition
218 * can vary from one PHY to another. */
219 dwc2->gusbcfg |= (7ul << GUSBCFG_TOCAL_Pos);
220
221 // Enable PHY clock TODO stop/gate clock when suspended mode
222 dwc2->pcgcctl &= ~(PCGCCTL_STOPPCLK | PCGCCTL_GATEHCLK | PCGCCTL_PWRCLMP | PCGCCTL_RSTPDWNMODULE);
223
224 dfifo_flush_tx(dwc2, 0x10); // all tx fifo
225 dfifo_flush_rx(dwc2);
226
227 // Clear pending and disable all interrupts
228 dwc2->gintsts = 0xFFFFFFFFU;
229 dwc2->gotgint = 0xFFFFFFFFU;
230 dwc2->gintmsk = 0;
231
232 return true;
233}
234
235// void dwc2_core_handle_common_irq(uint8_t rhport, bool in_isr) {
236// (void) in_isr;
237// dwc2_regs_t * const dwc2 = DWC2_REG(rhport);
238// const uint32_t int_mask = dwc2->gintmsk;
239// const uint32_t int_status = dwc2->gintsts & int_mask;
240//
241// // Device disconnect
242// if (int_status & GINTSTS_DISCINT) {
243// dwc2->gintsts = GINTSTS_DISCINT;
244// }
245//
246// }
247
248//--------------------------------------------------------------------
249// DFIFO
250//--------------------------------------------------------------------
251// Read a single data packet from receive DFIFO
252void dfifo_read_packet(dwc2_regs_t* dwc2, uint8_t* dst, uint16_t len) {
253 const volatile uint32_t* rx_fifo = dwc2->fifo[0];
254
255 // Reading full available 32 bit words from fifo
256 uint16_t word_count = len >> 2;
257 while (word_count--) {
258 tu_unaligned_write32(dst, *rx_fifo);
259 dst += 4;
260 }
261
262 // Read the remaining 1-3 bytes from fifo
263 const uint8_t bytes_rem = len & 0x03;
264 if (bytes_rem != 0) {
265 const uint32_t tmp = *rx_fifo;
266 dst[0] = tu_u32_byte0(tmp);
267 if (bytes_rem > 1) {
268 dst[1] = tu_u32_byte1(tmp);
269 }
270 if (bytes_rem > 2) {
271 dst[2] = tu_u32_byte2(tmp);
272 }
273 }
274}
275
276// Write a single data packet to DFIFO
277void dfifo_write_packet(dwc2_regs_t* dwc2, uint8_t fifo_num, const uint8_t* src, uint16_t len) {
278 volatile uint32_t* tx_fifo = dwc2->fifo[fifo_num];
279
280 // Pushing full available 32 bit words to fifo
281 uint16_t word_count = len >> 2;
282 while (word_count--) {
283 *tx_fifo = tu_unaligned_read32(src);
284 src += 4;
285 }
286
287 // Write the remaining 1-3 bytes into fifo
288 const uint8_t bytes_rem = len & 0x03;
289 if (bytes_rem) {
290 uint32_t tmp_word = src[0];
291 if (bytes_rem > 1) {
292 tmp_word |= (src[1] << 8);
293 }
294 if (bytes_rem > 2) {
295 tmp_word |= (src[2] << 16);
296 }
297
298 *tx_fifo = tmp_word;
299 }
300}
301
302#endif
static void dwc2_phy_init(dwc2_regs_t *dwc2, uint8_t hs_phy_type)
Definition: dwc2_bcm.h:68
static void dwc2_phy_update(dwc2_regs_t *dwc2, uint8_t hs_phy_type)
Definition: dwc2_bcm.h:77
static void phy_hs_init(dwc2_regs_t *dwc2)
Definition: dwc2_common.c:89
void dfifo_read_packet(dwc2_regs_t *dwc2, uint8_t *dst, uint16_t len)
Definition: dwc2_common.c:252
bool dwc2_core_is_highspeed(dwc2_regs_t *dwc2, tusb_role_t role)
Definition: dwc2_common.c:172
bool dwc2_core_init(uint8_t rhport, bool is_highspeed)
Definition: dwc2_common.c:197
static bool check_dwc2(dwc2_regs_t *dwc2)
Definition: dwc2_common.c:146
static void phy_fs_init(dwc2_regs_t *dwc2)
Definition: dwc2_common.c:63
static void reset_core(dwc2_regs_t *dwc2)
Definition: dwc2_common.c:46
void dfifo_write_packet(dwc2_regs_t *dwc2, uint8_t fifo_num, const uint8_t *src, uint16_t len)
Definition: dwc2_common.c:277
static TU_ATTR_ALWAYS_INLINE dwc2_regs_t * DWC2_REG(uint8_t rhport)
Definition: dwc2_common.h:67
static TU_ATTR_ALWAYS_INLINE void dfifo_flush_tx(dwc2_regs_t *dwc2, uint8_t fnum)
Definition: dwc2_common.h:82
static TU_ATTR_ALWAYS_INLINE void dfifo_flush_rx(dwc2_regs_t *dwc2)
Definition: dwc2_common.h:88
@ GHWCFG2_HSPHY_ULPI
Definition: dwc2_type.h:112
@ GHWCFG2_HSPHY_NOT_SUPPORTED
Definition: dwc2_type.h:110
uint32_t phy_data_width
Definition: dwc2_type.h:350
uint32_t hs_phy_type
Definition: dwc2_type.h:307
volatile uint32_t gahbcfg
Definition: dwc2_type.h:549
volatile uint32_t gotgint
Definition: dwc2_type.h:545
volatile uint32_t gusbcfg
Definition: dwc2_type.h:553
volatile uint32_t gsnpsid
Definition: dwc2_type.h:584
volatile dwc2_ghwcfg2_t ghwcfg2_bm
Definition: dwc2_type.h:588
volatile dwc2_ghwcfg4_t ghwcfg4_bm
Definition: dwc2_type.h:596
volatile uint32_t gintmsk
Definition: dwc2_type.h:561
volatile uint32_t fifo[16][0x400]
Definition: dwc2_type.h:674
volatile uint32_t grstctl
Definition: dwc2_type.h:557
volatile uint32_t gintsts
Definition: dwc2_type.h:560
volatile uint32_t guid
Definition: dwc2_type.h:583
volatile uint32_t pcgcctl
Definition: dwc2_type.h:668
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte1(uint32_t ui32)
Definition: tusb_common.h:139
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte0(uint32_t ui32)
Definition: tusb_common.h:140
static TU_ATTR_ALWAYS_INLINE void tu_unaligned_write32(void *mem, uint32_t value)
Definition: tusb_common.h:213
static TU_ATTR_ALWAYS_INLINE uint32_t tu_unaligned_read32(const void *mem)
Definition: tusb_common.h:207
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte2(uint32_t ui32)
Definition: tusb_common.h:138