Skip to contentSkip to conversation

usb: dwc2: Refactor and update USB DWC2 driver

PATCH Superseded Junhui Liu 3 participants 21 messages Dec 30, 2024 – Jan 2, 2025 USB

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Junhui Liu

author
#0 lore raw

v2 cover usb: dwc2: Refactor and update USB DWC2 driver

This series improves the USB DWC2 driver by extracting register definitions into a common file for better readability and updating the reset method to reflect changes in version 4.20a, including the new GRSTCTL_CSFTRST_DONE bit for reset completion indication.

This series has been tested on two platforms:
- MK808 with RK3066 SoC and dwc2 v2.91a, tested with USB flash drive and connected the board to the PC through the rockusb command. Test log [1].
- K230-CanMV with K230 SoC [2] and dwc2 v4.30a, tested with the onboard rtl8152 USB to net chip and an external rtl8152 adapter connected to the onboard USB-C interface via USB hub. Test log [3].

[1] https://gist.github.com/Judehahh/878ace2607d201dc49211f2c655f0fe0#file-mk808-md
[2] https://developer.canaan-creative.com/k230/en/dev/CanMV_K230_Tutorial.html
[3] https://gist.github.com/Judehahh/878ace2607d201dc49211f2c655f0fe0#file-k230-canmv-md

---
Changes in v2:
- Reorganized structures, renamed macros, and moved code for better
clarity and maintainability.
- Added a fix for the incorrect ULPI_UTMI_SEL bit setting.
- Added a fix for incorrectly calculating HBstLen for external DMA mode.
- Extend the changes that replace uint32 with u32 to also include u8 and u16.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Junhui Liu (8):
usb: dwc2: Extract register definitions to common header file
usb: dwc2: Fix incorrect ULPI_UTMI_SEL bit setting
USB: dwc2: Fix HBstLen setting for external DMA mode
usb: dwc2: Clean up with bitfield macros
usb: dwc2: Align macros with Linux kernel definitions
usb: dwc2: Extract macro definitions to common header
usb: dwc2: Unify flush and reset logic with v4.20a support
usb: dwc2: Replace uint<x>_t types with u<x>

 drivers/usb/common/Makefile                |   2 +
 drivers/usb/common/dwc2_core.c             | 103 ++++
 drivers/usb/common/dwc2_core.h             | 560 +++++++++++++++++++++
 drivers/usb/gadget/dwc2_udc_otg.c          | 125 ++---
 drivers/usb/gadget/dwc2_udc_otg_regs.h     | 289 ++---------
 drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c | 306 ++++++------
 drivers/usb/host/dwc2.c                    | 494 ++++++++-----------
 drivers/usb/host/dwc2.h                    | 750 +----------------------------
 8 files changed, 1123 insertions(+), 1506 deletions(-)

---
base-commit: cb7410257ac930a6fb05f1c63b291b7d399f19f2
change-id: 20241218-dwc2-dev-955c21a81118

Best regards,

Signature · 1 line
Junhui Liu <[email protected]>
author
#1 lore raw

PATCH v2 1/8 usb: dwc2: Extract register definitions to common header file

From: Kongyang Liu <[email protected]>

The same registers are accessed in both the otg and gatet drivers of dwc2, and these registers are repeatedly defined in these two parts. Extract register definitions into a common header file to reduce redundancy and make the code more maintainable.

---
 drivers/usb/common/dwc2_core.h             | 126 ++++++++++++++++++
 drivers/usb/gadget/dwc2_udc_otg.c          |  94 +++++++-------
 drivers/usb/gadget/dwc2_udc_otg_regs.h     |  68 +---------
 drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c | 201 +++++++++++++++--------------
 drivers/usb/host/dwc2.c                    | 102 ++++++++-------
 drivers/usb/host/dwc2.h                    |  58 ---------
 6 files changed, 332 insertions(+), 317 deletions(-)
drivers/usb/common/dwc2_core.h new file +126 -0
new file mode 100644
index 0000000000000000000000000000000000000000..26483a57e7df58e2b9fe820367e1680b9251af8d
@@ -0,0 +1,126 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2024, Kongyang Liu <[email protected]>
+ *
+ */
+
+#ifndef __DWC2_CORE_H_
+#define __DWC2_CORE_H_
+
+struct dwc2_global_regs {
+	u32 gotgctl;	/* 0x000 */
+	u32 gotgint;
+	u32 gahbcfg;
+	u32 gusbcfg;
+	u32 grstctl;	/* 0x010 */
+	u32 gintsts;
+	u32 gintmsk;
+	u32 grxstsr;
+	u32 grxstsp;	/* 0x020 */
+	u32 grxfsiz;
+	u32 gnptxfsiz;
+	u32 gnptxsts;
+	u32 gi2cctl;	/* 0x030 */
+	u32 gpvndctl;
+	u32 ggpio;
+	u32 guid;
+	u32 gsnpsid;	/* 0x040 */
+	u32 ghwcfg1;
+	u32 ghwcfg2;
+	u32 ghwcfg3;
+	u32 ghwcfg4;	/* 0x050 */
+	u32 glpmcfg;
+	u32 gpwrdn;
+	u32 gdfifocfg;
+	u32 gadpctl;	/* 0x060 */
+	u32 grefclk;
+	u32 gintmsk2;
+	u32 gintsts2;
+	u8  _pad_from_0x70_to_0x100[0x100 - 0x70];
+	u32 hptxfsiz;	/* 0x100 */
+	u32 dptxfsizn[15];
+	u8  _pad_from_0x140_to_0x400[0x400 - 0x140];
+};
+
+struct dwc2_hc_regs {
+	u32 hcchar;	/* 0x500 + 0x20 * ch */
+	u32 hcsplt;
+	u32 hcint;
+	u32 hcintmsk;
+	u32 hctsiz;
+	u32 hcdma;
+	u32 reserved;
+	u32 hcdmab;
+};
+
+struct dwc2_host_regs {
+	u32 hcfg;	/* 0x400 */
+	u32 hfir;
+	u32 hfnum;
+	u32 _pad_0x40c;
+	u32 hptxsts;	/* 0x410 */
+	u32 haint;
+	u32 haintmsk;
+	u32 hflbaddr;
+	u8  _pad_from_0x420_to_0x440[0x440 - 0x420];
+	u32 hprt0;	/* 0x440 */
+	u8  _pad_from_0x444_to_0x500[0x500 - 0x444];
+	struct dwc2_hc_regs hc[16];	/* 0x500 */
+	u8  _pad_from_0x700_to_0x800[0x800 - 0x700];
+};
+
+/* Device Logical IN Endpoint-Specific Registers */
+struct dwc2_dev_in_endp {
+	u32 diepctl;	/* 0x900 + 0x20 * ep */
+	u32 reserved0;
+	u32 diepint;
+	u32 reserved1;
+	u32 dieptsiz;
+	u32 diepdma;
+	u32 reserved2;
+	u32 diepdmab;
+};
+
+/* Device Logical OUT Endpoint-Specific Registers */
+struct dwc2_dev_out_endp {
+	u32 doepctl;	/* 0xB00 + 0x20 * ep */
+	u32 reserved0;
+	u32 doepint;
+	u32 reserved1;
+	u32 doeptsiz;
+	u32 doepdma;
+	u32 reserved2;
+	u32 doepdmab;
+};
+
+struct dwc2_device_regs {
+	u32 dcfg;	/* 0x800 */
+	u32 dctl;
+	u32 dsts;
+	u32 _pad_0x80c;
+	u32 diepmsk;	/* 0x810 */
+	u32 doepmsk;
+	u32 daint;
+	u32 daintmsk;
+	u32 dtknqr1;	/* 0x820 */
+	u32 dtknqr2;
+	u32 dvbusdis;
+	u32 dvbuspulse;
+	u32 dtknqr3;	/* 0x830 */
+	u32 dtknqr4;
+	u8  _pad_from_0x838_to_0x900[0x900 - 0x838];
+	struct dwc2_dev_in_endp  in_endp[16];	/* 0x900 */
+	struct dwc2_dev_out_endp out_endp[16];	/* 0xB00 */
+};
+
+struct dwc2_core_regs {
+	struct dwc2_global_regs  global_regs;	/* 0x000 */
+	struct dwc2_host_regs    host_regs;	/* 0x400 */
+	struct dwc2_device_regs  device_regs;	/* 0x800 */
+	u8  _pad_from_0xd00_to_0xe00[0xe00 - 0xd00];
+	u32 pcgcctl;				/* 0xe00 */
+	u8  _pad_from_0xe04_to_0x1000[0x1000 - 0xe04];
+	u8  ep_fifo[16][0x1000];		/* 0x1000 */
+};
+
+#endif /* __DWC2_CORE_H_ */
drivers/usb/gadget/dwc2_udc_otg.c +49 -45

236 lines not on this page · raw message

drivers/usb/gadget/dwc2_udc_otg_regs.h +1 -67

81 lines not on this page · raw message

drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +102 -99

498 lines not on this page · raw message

drivers/usb/host/dwc2.c +54 -48

318 lines not on this page · raw message

drivers/usb/host/dwc2.h +0 -58

64 lines not on this page · raw message

Signature · 1 line
2.39.2
Reviewed-by on v2 1/8
#2 lore raw

Re: [PATCH v2 1/8] usb: dwc2: Extract register definitions to common header file

Quoted 9 lines · On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

From: Kongyang Liu <[email protected]>

The same registers are accessed in both the otg and gatet drivers of dwc2, and these registers are repeatedly defined in these two parts. Extract register definitions into a common header file to reduce redundancy and make the code more maintainable.

author
#3 lore raw

PATCH v2 2/8 usb: dwc2: Fix incorrect ULPI_UTMI_SEL bit setting

In reply to Junhui Liu

From: Junhui Liu <[email protected]>

The ULPI_UTMI_SEL bit in the DWC2 driver was set incorrectly. According to the datasheet [1], this bit should be set to 0 for UTMI interface and 1 for ULPI interface. The existing code had this logic reversed, causing the interface selection to be incorrect.

This commit corrects the ULPI_UTMI_SEL bit setting to match the datasheet's description. Referencing the kernel's code [2] also confirms this fix.

[1] https://rockchip.fr/RK312X%20TRM/chapter-26-usb-otg-2-0.pdf#page=30
[2] https://github.com/torvalds/linux/blob/v6.13-rc3/drivers/usb/dwc2/core.c#L1106

---
 drivers/usb/host/dwc2.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
drivers/usb/host/dwc2.c +13 -11
index b6c8b3d5a6bdf375f1adeb106d4832d92307f0db..609de18faa3abc5f4ecb0c23cf3590966bad7992 100644
@@ -402,20 +402,22 @@ static void dwc_otg_core_init(struct udevice *dev)
 	 * soft reset so only program the first time. Do a soft reset
 	 * immediately after setting phyif.
 	 */
-	usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
-	usbcfg |= DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
-
-	if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) {	/* ULPI interface */
+#if (DWC2_PHY_TYPE == DWC2_PHY_TYPE_ULPI)
+	usbcfg |= DWC2_GUSBCFG_ULPI_UTMI_SEL;
+	usbcfg &= ~DWC2_GUSBCFG_PHYIF;
 #ifdef DWC2_PHY_ULPI_DDR
-		usbcfg |= DWC2_GUSBCFG_DDRSEL;
+	usbcfg |= DWC2_GUSBCFG_DDRSEL;
 #else
-		usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
-#endif
-	} else {	/* UTMI+ interface */
+	usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
+#endif /* DWC2_PHY_ULPI_DDR */
+#elif (DWC2_PHY_TYPE == DWC2_PHY_TYPE_UTMI)
+	usbcfg &= ~DWC2_GUSBCFG_ULPI_UTMI_SEL;
 #if (DWC2_UTMI_WIDTH == 16)
-		usbcfg |= DWC2_GUSBCFG_PHYIF;
-#endif
-	}
+	usbcfg |= DWC2_GUSBCFG_PHYIF;
+#else
+	usbcfg &= ~DWC2_GUSBCFG_PHYIF;
+#endif /* DWC2_UTMI_WIDTH */
+#endif /* DWC2_PHY_TYPE */
 
 	writel(usbcfg, &regs->global_regs.gusbcfg);
 
Signature · 1 line
2.39.2
Reviewed-by on v2 2/8
#4 lore raw

Re: [PATCH v2 2/8] usb: dwc2: Fix incorrect ULPI_UTMI_SEL bit setting

Quoted 7 lines · On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

From: Junhui Liu <[email protected]>

The ULPI_UTMI_SEL bit in the DWC2 driver was set incorrectly. According to the datasheet [1], this bit should be set to 0 for UTMI interface and 1 for ULPI interface. The existing code had this logic reversed, causing the interface selection to be incorrect.

Quoted 8 lines

This commit corrects the ULPI_UTMI_SEL bit setting to match the datasheet's description. Referencing the kernel's code [2] also confirms this fix.

[1] https://rockchip.fr/RK312X%20TRM/chapter-26-usb-otg-2-0.pdf#page=30
[2] https://github.com/torvalds/linux/blob/v6.13-rc3/drivers/usb/dwc2/core.c#L1106

author
#5 lore raw

PATCH v2 3/8 USB: dwc2: Fix HBstLen setting for external DMA mode

In reply to Junhui Liu

From: Kongyang Liu <[email protected]>

The loop used to calculate HBstLen for extern DMA mode does not produce the correct result according to the datasheet [1]. Replacing that loop with a direct calculation using LOG2 to correctly assign the burst length in the GAHBCFG register for external DMA mode.

[1] https://rockchip.fr/RK312X%20TRM/chapter-26-usb-otg-2-0.pdf#page=24

---
Additionally, the boards I have only use internal DMA mode, and it’s unclear which chips employ external DMA. The testing was performed by comparing against the datasheet, and the results are shown in [2].

[2] https://gist.github.com/Judehahh/34530da390b58728102778406e602cb1

---
 drivers/usb/host/dwc2.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/usb/host/dwc2.c +2 -5
index 609de18faa3abc5f4ecb0c23cf3590966bad7992..954650d856a4f2e95d74e1b5716c0ebe83fa9ba8 100644
@@ -448,11 +448,8 @@ static void dwc_otg_core_init(struct udevice *dev)
 	case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
 		break;
 	case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
-		while (brst_sz > 1) {
-			ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
-			ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
-			brst_sz >>= 1;
-		}
+		ahbcfg |= (LOG2(brst_sz >> 1) << DWC2_GAHBCFG_HBURSTLEN_OFFSET) &
+			  DWC2_GAHBCFG_HBURSTLEN_MASK;
 
 #ifdef DWC2_DMA_ENABLE
 		ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
Signature · 1 line
2.39.2
Reviewed-by on v2 3/8
#6 lore raw

Re: [PATCH v2 3/8] USB: dwc2: Fix HBstLen setting for external DMA mode

Quoted 11 lines · On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

From: Kongyang Liu <[email protected]>

The loop used to calculate HBstLen for extern DMA mode does not produce the correct result according to the datasheet [1]. Replacing that loop with a direct calculation using LOG2 to correctly assign the burst length in the GAHBCFG register for external DMA mode.

[1] https://rockchip.fr/RK312X%20TRM/chapter-26-usb-otg-2-0.pdf#page=24

author
#7 lore raw

PATCH v2 4/8 usb: dwc2: Clean up with bitfield macros

In reply to Junhui Liu

From: Kongyang Liu <[email protected]>

Use FIELD_PREP, FIELD_GET, BIT, and GENMASK macros to standardize bit manipulation across the DWC2 code, improving readability and maintainability without altering functionality.

---
 drivers/usb/gadget/dwc2_udc_otg.c          |   5 +-
 drivers/usb/gadget/dwc2_udc_otg_regs.h     | 180 +++---
 drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c |  21 +-
 drivers/usb/host/dwc2.c                    | 101 ++-
 drivers/usb/host/dwc2.h                    | 993 ++++++++++-------------------
 5 files changed, 482 insertions(+), 818 deletions(-)
drivers/usb/gadget/dwc2_udc_otg.c +3 -2
index c3dee8771e6c98692dd2ed6b50ca3c6ea21782af..2289dcc9c325a793ff86940625794066463b4083 100644
@@ -29,6 +29,7 @@
 #include <linux/delay.h>
 #include <linux/printk.h>
 
+#include <linux/bitfield.h>
 #include <linux/errno.h>
 #include <linux/list.h>
 
@@ -526,8 +527,8 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 	}
 
 	/* 8. Unmask EPO interrupts*/
-	writel(((1 << EP0_CON) << DAINT_OUT_BIT)
-	       | (1 << EP0_CON), &reg->device_regs.daintmsk);
+	writel(FIELD_PREP(DAINT_OUTEP_MASK, BIT(EP0_CON)) |
+	       FIELD_PREP(DAINT_INEP_MASK, BIT(EP0_CON)), &reg->device_regs.daintmsk);
 
 	/* 9. Unmask device OUT EP common interrupts*/
 	writel(DOEPMSK_INIT, &reg->device_regs.doepmsk);
drivers/usb/gadget/dwc2_udc_otg_regs.h +88 -92

253 lines not on this page · raw message

drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +11 -10
index e2a24c53b4c8d632163484d52b4338820dcb1731..76c0b03c355b47e9523a071bd76ab8d6a17cd3ac 100644
@@ -19,6 +19,7 @@
 
 #include <cpu_func.h>
 #include <log.h>
+#include <linux/bitfield.h>
 #include <linux/bug.h>
 
 static u8 clear_feature_num;
@@ -174,11 +175,11 @@ static int setdma_tx(struct dwc2_ep *ep, struct dwc2_request *req)
 	ctrl = readl(&reg->device_regs.in_endp[ep_num].diepctl);
 
 	/* Write the FIFO number to be used for this endpoint */
-	ctrl &= DIEPCTL_TX_FIFO_NUM_MASK;
-	ctrl |= DIEPCTL_TX_FIFO_NUM(ep->fifo_num);
+	ctrl &= ~DIEPCTL_TX_FIFO_NUM_MASK;
+	ctrl |= FIELD_PREP(DIEPCTL_TX_FIFO_NUM_MASK, ep->fifo_num);
 
 	/* Clear reserved (Next EP) bits */
-	ctrl = (ctrl&~(EP_MASK<<DEPCTL_NEXT_EP_BIT));
+	ctrl &= ~DEPCTL_NEXT_EP_MASK;
 
 	writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, &reg->device_regs.in_endp[ep_num].diepctl);
 
@@ -380,7 +381,7 @@ static void process_ep_in_intr(struct dwc2_udc *dev)
 	debug_cond(DEBUG_IN_EP,
 		"*** %s: EP In interrupt : DAINT = 0x%x\n", __func__, ep_intr);
 
-	ep_intr &= DAINT_MASK;
+	ep_intr = FIELD_GET(DAINT_INEP_MASK, ep_intr);
 
 	while (ep_intr) {
 		if (ep_intr & DAINT_IN_EP_INT(1)) {
@@ -431,10 +432,10 @@ static void process_ep_out_intr(struct dwc2_udc *dev)
 		   "*** %s: EP OUT interrupt : DAINT = 0x%x\n",
 		   __func__, ep_intr);
 
-	ep_intr = (ep_intr >> DAINT_OUT_BIT) & DAINT_MASK;
+	ep_intr = FIELD_GET(DAINT_OUTEP_MASK, ep_intr);
 
 	while (ep_intr) {
-		if (ep_intr & 0x1) {
+		if (ep_intr & BIT(EP0_CON)) {
 			ep_intr_status = readl(&reg->device_regs.out_endp[ep_num].doepint);
 			debug_cond(DEBUG_OUT_EP != 0,
 				   "\tEP%d-OUT : DOEPINT = 0x%x\n",
@@ -1114,10 +1115,10 @@ static void dwc2_udc_ep_activate(struct dwc2_ep *ep)
 	/* Read DEPCTLn register */
 	if (ep_is_in(ep)) {
 		ep_ctrl = readl(&reg->device_regs.in_endp[ep_num].diepctl);
-		daintmsk = 1 << ep_num;
+		daintmsk = FIELD_PREP(DAINT_INEP_MASK, BIT(ep_num));
 	} else {
 		ep_ctrl = readl(&reg->device_regs.out_endp[ep_num].doepctl);
-		daintmsk = (1 << ep_num) << DAINT_OUT_BIT;
+		daintmsk = FIELD_PREP(DAINT_OUTEP_MASK, BIT(ep_num));
 	}
 
 	debug("%s: EPCTRL%d = 0x%x, ep_is_in = %d\n",
@@ -1127,9 +1128,9 @@ static void dwc2_udc_ep_activate(struct dwc2_ep *ep)
 	 * register. */
 	if (!(ep_ctrl & DEPCTL_USBACTEP)) {
 		ep_ctrl = (ep_ctrl & ~DEPCTL_TYPE_MASK) |
-			(ep->bmAttributes << DEPCTL_TYPE_BIT);
+			FIELD_PREP(DEPCTL_TYPE_MASK, ep->bmAttributes);
 		ep_ctrl = (ep_ctrl & ~DEPCTL_MPS_MASK) |
-			(ep->ep.maxpacket << DEPCTL_MPS_BIT);
+			FIELD_PREP(DEPCTL_MPS_MASK, ep->ep.maxpacket);
 		ep_ctrl |= (DEPCTL_SETD0PID | DEPCTL_USBACTEP | DEPCTL_SNAK);
 
 		if (ep_is_in(ep)) {
drivers/usb/host/dwc2.c +46 -55

222 lines not on this page · raw message

drivers/usb/host/dwc2.h +334 -659

1,030 lines not on this page · raw message

Signature · 1 line
2.39.2
#8 lore raw

Re: [PATCH v2 4/8] usb: dwc2: Clean up with bitfield macros

On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

[...]

Quoted 4 lines
drivers/usb/gadget/dwc2_udc_otg_regs.h +0 -0
index 198ba7a7c37d05dca084ef017534265f5fb5fd70..33359ba56f7864525fb435ab52f2b21538e4647f 100644

[...]

Quoted 11 lines
drivers/usb/gadget/dwc2_udc_otg_regs.h +3 -3
@@ -78,15 +77,15 @@ struct dwc2_usbotg_phy {
  #define HIGH_SPEED_CONTROL_PKT_SIZE	64
  #define HIGH_SPEED_BULK_PKT_SIZE	512
 
-#define RX_FIFO_SIZE			(1024)
-#define NPTX_FIFO_SIZE			(1024)
-#define PTX_FIFO_SIZE			(384)
+#define RX_FIFO_SIZE			1024
+#define NPTX_FIFO_SIZE			1024
+#define PTX_FIFO_SIZE			384
 

drivers/usb/gadget/dwc2_udc_otg_regs.h struct dwc2_usbotg_phy { · line 88

Quoted 10 lines
drivers/usb/gadget/dwc2_udc_otg_regs.h +5 -5
-#define DEPCTL_TXFNUM_0		(0x0<<22)
-#define DEPCTL_TXFNUM_1		(0x1<<22)
-#define DEPCTL_TXFNUM_2		(0x2<<22)
-#define DEPCTL_TXFNUM_3		(0x3<<22)
-#define DEPCTL_TXFNUM_4		(0x4<<22)
+#define DEPCTL_TXFNUM_0			(0x0<<22)
+#define DEPCTL_TXFNUM_1			(0x1<<22)
+#define DEPCTL_TXFNUM_2			(0x2<<22)
+#define DEPCTL_TXFNUM_3			(0x3<<22)
+#define DEPCTL_TXFNUM_4			(0x4<<22)

Nitpick -- please add spaces around << , I think checkpatch would warn about this.

[...]

Quoted 16 lines
drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +1 -0
index e2a24c53b4c8d632163484d52b4338820dcb1731..76c0b03c355b47e9523a071bd76ab8d6a17cd3ac 100644
@@ -19,6 +19,7 @@
 
  #include <cpu_func.h>
  #include <log.h>
+#include <linux/bitfield.h>
  #include <linux/bug.h>
 
  static u8 clear_feature_num;
@@ -174,11 +175,11 @@ static int setdma_tx(struct dwc2_ep *ep, struct dwc2_request *req)
  	ctrl = readl(&reg->device_regs.in_endp[ep_num].diepctl);
 
  	/* Write the FIFO number to be used for this endpoint */

drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c static int setdma_tx(struct dwc2_ep *ep, struct dwc2_request *req) · line 184

Quoted 10 lines
drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +3 -3
-	ctrl &= DIEPCTL_TX_FIFO_NUM_MASK;
-	ctrl |= DIEPCTL_TX_FIFO_NUM(ep->fifo_num);
+	ctrl &= ~DIEPCTL_TX_FIFO_NUM_MASK;
+	ctrl |= FIELD_PREP(DIEPCTL_TX_FIFO_NUM_MASK, ep->fifo_num);
 
  	/* Clear reserved (Next EP) bits */
-	ctrl = (ctrl&~(EP_MASK<<DEPCTL_NEXT_EP_BIT));
+	ctrl &= ~DEPCTL_NEXT_EP_MASK;
 
  	writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, &reg->device_regs.in_endp[ep_num].diepctl);

Nitpick -- in some follow up separate series, this could also be replaced by clrsetbits_le32() , but that's really for separate patch/series.

author
#9 lore raw

Re: [PATCH v2 4/8] usb: dwc2: Clean up with bitfield macros

Hi Marek,

Thanks for your review.

On 2025/1/1 02:02, Marek Vasut wrote:
On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

[...]

drivers/usb/gadget/dwc2_udc_otg_regs.h +0 -0
index 198ba7a7c37d05dca084ef017534265f5fb5fd70..33359ba56f7864525fb435ab52f2b21538e4647f 100644

[...]

@@ -78,15 +77,15 @@ struct dwc2_usbotg_phy {
#define HIGH_SPEED_CONTROL_PKT_SIZE 64
#define HIGH_SPEED_BULK_PKT_SIZE 512
-#define RX_FIFO_SIZE (1024)
-#define NPTX_FIFO_SIZE (1024)
-#define PTX_FIFO_SIZE (384)
+#define RX_FIFO_SIZE 1024
+#define NPTX_FIFO_SIZE 1024
+#define PTX_FIFO_SIZE 384
-#define DEPCTL_TXFNUM_0 (0x0<<22)

-#define DEPCTL_TXFNUM_1        (0x1<<22)
-#define DEPCTL_TXFNUM_2        (0x2<<22)
-#define DEPCTL_TXFNUM_3        (0x3<<22)
-#define DEPCTL_TXFNUM_4        (0x4<<22)
+#define DEPCTL_TXFNUM_0            (0x0<<22)
+#define DEPCTL_TXFNUM_1            (0x1<<22)
+#define DEPCTL_TXFNUM_2            (0x2<<22)
+#define DEPCTL_TXFNUM_3            (0x3<<22)
+#define DEPCTL_TXFNUM_4            (0x4<<22)

Nitpick -- please add spaces around << , I think checkpatch would warn about this.

Okay, I will fix it.

[...]

drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +4 -3
index e2a24c53b4c8d632163484d52b4338820dcb1731..76c0b03c355b47e9523a071bd76ab8d6a17cd3ac 100644
@@ -19,6 +19,7 @@
    #include <cpu_func.h>
  #include <log.h>
+#include <linux/bitfield.h>
  #include <linux/bug.h>
    static u8 clear_feature_num;
@@ -174,11 +175,11 @@ static int setdma_tx(struct dwc2_ep *ep, struct dwc2_request *req)
      ctrl = readl(&reg->device_regs.in_endp[ep_num].diepctl);
        /* Write the FIFO number to be used for this endpoint */
-    ctrl &= DIEPCTL_TX_FIFO_NUM_MASK;
-    ctrl |= DIEPCTL_TX_FIFO_NUM(ep->fifo_num);
+    ctrl &= ~DIEPCTL_TX_FIFO_NUM_MASK;
+    ctrl |= FIELD_PREP(DIEPCTL_TX_FIFO_NUM_MASK, ep->fifo_num);
        /* Clear reserved (Next EP) bits */
-    ctrl = (ctrl&~(EP_MASK<<DEPCTL_NEXT_EP_BIT));
+    ctrl &= ~DEPCTL_NEXT_EP_MASK;
        writel(DEPCTL_EPENA|DEPCTL_CNAK|ctrl, &reg->device_regs.in_endp[ep_num].diepctl);

Nitpick -- in some follow up separate series, this could also be replaced by clrsetbits_le32() , but that's really for separate patch/series.

Sure, I will start a separate series to deal with this later.

Best regards,
Junhui Liu

author
#10 lore raw

PATCH v2 5/8 usb: dwc2: Align macros with Linux kernel definitions

In reply to Junhui Liu

From: Kongyang Liu <[email protected]>

Update the DWC2 macros to match those used in the Linux kernel, making it easier to synchronize updates with kernel. Also removed some unused macros to cleanup the code.

---
 drivers/usb/gadget/dwc2_udc_otg.c          |  62 +--
 drivers/usb/gadget/dwc2_udc_otg_regs.h     | 245 ++++++-----
 drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c | 130 +++---
 drivers/usb/host/dwc2.c                    | 260 ++++++------
 drivers/usb/host/dwc2.h                    | 638 +++++++++++++----------------
 5 files changed, 633 insertions(+), 702 deletions(-)
drivers/usb/gadget/dwc2_udc_otg.c +33 -29
index 2289dcc9c325a793ff86940625794066463b4083..0fc9ee1b90fa71135083a1a6dae2e74992e51181 100644
@@ -160,7 +160,7 @@ struct dwc2_core_regs *reg;
 
 bool dfu_usb_get_reset(void)
 {
-	return !!(readl(&reg->global_regs.gintsts) & INT_RESET);
+	return !!(readl(&reg->global_regs.gintsts) & GINTSTS_USBRST);
 }
 
 __weak void otg_phy_init(struct dwc2_udc *dev) {}
@@ -240,8 +240,8 @@ static int udc_enable(struct dwc2_udc *dev)
 
 static int dwc2_gadget_pullup(struct usb_gadget *g, int is_on)
 {
-	clrsetbits_le32(&reg->device_regs.dctl, SOFT_DISCONNECT,
-			is_on ? 0 : SOFT_DISCONNECT);
+	clrsetbits_le32(&reg->device_regs.dctl, DCTL_SFTDISCON,
+			is_on ? 0 : DCTL_SFTDISCON);
 
 	return 0;
 }
@@ -471,7 +471,7 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 	u32 max_hw_ep;
 	int pdata_hw_ep;
 
-	writel(CORE_SOFT_RESET, &reg->global_regs.grstctl);
+	writel(GRSTCTL_CSFTRST, &reg->global_regs.grstctl);
 
 	debug("Resetting OTG controller\n");
 
@@ -498,19 +498,20 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 
 	/* 3. Put the OTG device core in the disconnected state.*/
 	uTemp = readl(&reg->device_regs.dctl);
-	uTemp |= SOFT_DISCONNECT;
+	uTemp |= DCTL_SFTDISCON;
 	writel(uTemp, &reg->device_regs.dctl);
 
 	udelay(20);
 
 	/* 4. Make the OTG device core exit from the disconnected state.*/
 	uTemp = readl(&reg->device_regs.dctl);
-	uTemp = uTemp & ~SOFT_DISCONNECT;
+	uTemp = uTemp & ~DCTL_SFTDISCON;
 	writel(uTemp, &reg->device_regs.dctl);
 
 	/* 5. Configure OTG Core to initial settings of device mode.*/
 	/* [][1: full speed(30Mhz) 0:high speed]*/
-	writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, &reg->device_regs.dcfg);
+	writel(FIELD_PREP(DCFG_EPMISCNT_MASK, 1) |
+	       FIELD_PREP(DCFG_DEVSPD_MASK, DCFG_DEVSPD_HS), &reg->device_regs.dcfg);
 
 	mdelay(1);
 
@@ -518,12 +519,12 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 	writel(GINTMSK_INIT, &reg->global_regs.gintmsk);
 
 	/* 7. Set NAK bit of EP0, EP1, EP2*/
-	writel(DEPCTL_EPDIS | DEPCTL_SNAK, &reg->device_regs.out_endp[EP0_CON].doepctl);
-	writel(DEPCTL_EPDIS | DEPCTL_SNAK, &reg->device_regs.in_endp[EP0_CON].diepctl);
+	writel(DXEPCTL_EPDIS | DXEPCTL_SNAK, &reg->device_regs.out_endp[EP0_CON].doepctl);
+	writel(DXEPCTL_EPDIS | DXEPCTL_SNAK, &reg->device_regs.in_endp[EP0_CON].diepctl);
 
 	for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) {
-		writel(DEPCTL_EPDIS | DEPCTL_SNAK, &reg->device_regs.out_endp[i].doepctl);
-		writel(DEPCTL_EPDIS | DEPCTL_SNAK, &reg->device_regs.in_endp[i].diepctl);
+		writel(DXEPCTL_EPDIS | DXEPCTL_SNAK, &reg->device_regs.out_endp[i].doepctl);
+		writel(DXEPCTL_EPDIS | DXEPCTL_SNAK, &reg->device_regs.in_endp[i].diepctl);
 	}
 
 	/* 8. Unmask EPO interrupts*/
@@ -551,12 +552,12 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 	writel(rx_fifo_sz, &reg->global_regs.grxfsiz);
 
 	/* 12. Set Non Periodic Tx FIFO Size */
-	writel((np_tx_fifo_sz << 16) | rx_fifo_sz,
+	writel(FIELD_PREP(FIFOSIZE_DEPTH_MASK, np_tx_fifo_sz) |
+	       FIELD_PREP(FIFOSIZE_STARTADDR_MASK, rx_fifo_sz),
 	       &reg->global_regs.gnptxfsiz);
 
 	/* retrieve the number of IN Endpoints (excluding ep0) */
-	max_hw_ep = (readl(&reg->global_regs.ghwcfg4) & GHWCFG4_NUM_IN_EPS_MASK) >>
-		    GHWCFG4_NUM_IN_EPS_SHIFT;
+	max_hw_ep = FIELD_GET(GHWCFG4_NUM_IN_EPS_MASK, readl(&reg->global_regs.ghwcfg4));
 	pdata_hw_ep = dev->pdata->tx_fifo_sz_nb;
 
 	/* tx_fifo_sz_nb should equal to number of IN Endpoint */
@@ -568,24 +569,27 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 		if (pdata_hw_ep)
 			tx_fifo_sz = dev->pdata->tx_fifo_sz_array[i];
 
-		writel((rx_fifo_sz + np_tx_fifo_sz + (tx_fifo_sz * i)) |
-			tx_fifo_sz << 16, &reg->global_regs.dptxfsizn[i]);
+		writel(FIELD_PREP(FIFOSIZE_DEPTH_MASK, tx_fifo_sz) |
+		       FIELD_PREP(FIFOSIZE_STARTADDR_MASK,
+				  rx_fifo_sz + np_tx_fifo_sz + tx_fifo_sz * i),
+		       &reg->global_regs.dptxfsizn[i]);
 	}
 	/* Flush the RX FIFO */
-	writel(RX_FIFO_FLUSH, &reg->global_regs.grstctl);
-	while (readl(&reg->global_regs.grstctl) & RX_FIFO_FLUSH)
+	writel(GRSTCTL_RXFFLSH, &reg->global_regs.grstctl);
+	while (readl(&reg->global_regs.grstctl) & GRSTCTL_RXFFLSH)
 		debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
 
 	/* Flush all the Tx FIFO's */
-	writel(TX_FIFO_FLUSH_ALL, &reg->global_regs.grstctl);
-	writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->global_regs.grstctl);
-	while (readl(&reg->global_regs.grstctl) & TX_FIFO_FLUSH)
+	writel(FIELD_PREP(GRSTCTL_TXFNUM_MASK, GRSTCTL_TXFNUM_ALL), &reg->global_regs.grstctl);
+	writel(FIELD_PREP(GRSTCTL_TXFNUM_MASK, GRSTCTL_TXFNUM_ALL) | GRSTCTL_TXFFLSH,
+	       &reg->global_regs.grstctl);
+	while (readl(&reg->global_regs.grstctl) & GRSTCTL_TXFFLSH)
 		debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
 
 	/* 13. Clear NAK bit of EP0, EP1, EP2*/
 	/* For Slave mode*/
 	/* EP0: Control OUT */
-	writel(DEPCTL_EPDIS | DEPCTL_CNAK,
+	writel(DXEPCTL_EPDIS | DXEPCTL_CNAK,
 	       &reg->device_regs.out_endp[EP0_CON].doepctl);
 
 	/* 14. Initialize OTG Link Core.*/
@@ -1127,8 +1131,8 @@ static int dwc2_udc_otg_probe(struct udevice *dev)
 		if (plat->force_b_session_valid &&
 		    !plat->force_vbus_detection) {
 			/* Override VBUS detection: enable then value*/
-			setbits_le32(&usbotg_reg->global_regs.gotgctl, VB_VALOEN);
-			setbits_le32(&usbotg_reg->global_regs.gotgctl, VB_VALOVAL);
+			setbits_le32(&usbotg_reg->global_regs.gotgctl, GOTGCTL_VBVALOEN);
+			setbits_le32(&usbotg_reg->global_regs.gotgctl, GOTGCTL_VBVALOVAL);
 		} else {
 			/* Enable VBUS sensing */
 			setbits_le32(&usbotg_reg->global_regs.ggpio,
@@ -1137,9 +1141,9 @@ static int dwc2_udc_otg_probe(struct udevice *dev)
 		if (plat->force_b_session_valid) {
 			/* Override B session bits: enable then value */
 			setbits_le32(&usbotg_reg->global_regs.gotgctl,
-				     A_VALOEN | B_VALOEN);
+				     GOTGCTL_AVALOEN | GOTGCTL_BVALOEN);
 			setbits_le32(&usbotg_reg->global_regs.gotgctl,
-				     A_VALOVAL | B_VALOVAL);
+				     GOTGCTL_AVALOVAL | GOTGCTL_BVALOVAL);
 		} else {
 			/* Enable ID detection */
 			setbits_le32(&usbotg_reg->global_regs.ggpio,
@@ -1205,10 +1209,10 @@ U_BOOT_DRIVER(dwc2_udc_otg) = {
 int dwc2_udc_B_session_valid(struct udevice *dev)
 {
 	struct dwc2_plat_otg_data *plat = dev_get_plat(dev);
-	struct dwc2_usbotg_reg *usbotg_reg =
-		(struct dwc2_usbotg_reg *)plat->regs_otg;
+	struct dwc2_core_reg *usbotg_reg =
+		(struct dwc2_core_reg *)plat->regs_otg;
 
-	return readl(&usbotg_reg->gotgctl) & B_SESSION_VALID;
+	return readl(&usbotg_reg->global_regs.gotgctl) & GOTGCTL_BSESVLD;
 }
 #else
 int dm_usb_gadget_handle_interrupts(struct udevice *dev)
drivers/usb/gadget/dwc2_udc_otg_regs.h +120 -125

293 lines not on this page · raw message

drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +66 -64

346 lines not on this page · raw message

drivers/usb/host/dwc2.c +126 -134

551 lines not on this page · raw message

drivers/usb/host/dwc2.h +288 -350

644 lines not on this page · raw message

Signature · 1 line
2.39.2
Reviewed-by on v2 5/8
#11 lore raw

Re: [PATCH v2 5/8] usb: dwc2: Align macros with Linux kernel definitions

Quoted 8 lines · On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

From: Kongyang Liu <[email protected]>

Update the DWC2 macros to match those used in the Linux kernel, making it easier to synchronize updates with kernel. Also removed some unused macros to cleanup the code.

author
#12 lore raw

PATCH v2 6/8 usb: dwc2: Extract macro definitions to common header

In reply to Junhui Liu

From: Kongyang Liu <[email protected]>

Some macros are shared between host and gadget code, causing duplicated definitions. Move DWC2 macro definitions from host and gadget code into a common header to reduce duplication.

---
 drivers/usb/common/dwc2_core.h         | 430 +++++++++++++++++++++++++++++++++
 drivers/usb/gadget/dwc2_udc_otg_regs.h | 160 +-----------
 drivers/usb/host/dwc2.h                | 291 ----------------------
 3 files changed, 431 insertions(+), 450 deletions(-)
drivers/usb/common/dwc2_core.h +430 -0
index 26483a57e7df58e2b9fe820367e1680b9251af8d..862d3b3691c9caf84590d34960df21117848df0a 100644
@@ -7,6 +7,8 @@
 #ifndef __DWC2_CORE_H_
 #define __DWC2_CORE_H_
 
+#include <linux/bitops.h>
+
 struct dwc2_global_regs {
 	u32 gotgctl;	/* 0x000 */
 	u32 gotgint;
@@ -123,4 +125,432 @@ struct dwc2_core_regs {
 	u8  ep_fifo[16][0x1000];		/* 0x1000 */
 };
 
+/* Core Global Register */
+#define GOTGCTL_CHIRPEN				BIT(27)
+#define GOTGCTL_MULT_VALID_BC_MASK		GENMASK(26, 22)
+#define GOTGCTL_CURMODE_HOST			BIT(21)
+#define GOTGCTL_OTGVER				BIT(20)
+#define GOTGCTL_BSESVLD				BIT(19)
+#define GOTGCTL_ASESVLD				BIT(18)
+#define GOTGCTL_DBNC_SHORT			BIT(17)
+#define GOTGCTL_CONID_B				BIT(16)
+#define GOTGCTL_DBNCE_FLTR_BYPASS		BIT(15)
+#define GOTGCTL_DEVHNPEN			BIT(11)
+#define GOTGCTL_HSTSETHNPEN			BIT(10)
+#define GOTGCTL_HNPREQ				BIT(9)
+#define GOTGCTL_HSTNEGSCS			BIT(8)
+#define GOTGCTL_BVALOVAL			BIT(7)
+#define GOTGCTL_BVALOEN				BIT(6)
+#define GOTGCTL_AVALOVAL			BIT(5)
+#define GOTGCTL_AVALOEN				BIT(4)
+#define GOTGCTL_VBVALOVAL			BIT(3)
+#define GOTGCTL_VBVALOEN			BIT(2)
+#define GOTGCTL_SESREQ				BIT(1)
+#define GOTGCTL_SESREQSCS			BIT(0)
+
+#define GOTGINT_DBNCE_DONE			BIT(19)
+#define GOTGINT_A_DEV_TOUT_CHG			BIT(18)
+#define GOTGINT_HST_NEG_DET			BIT(17)
+#define GOTGINT_HST_NEG_SUC_STS_CHNG		BIT(9)
+#define GOTGINT_SES_REQ_SUC_STS_CHNG		BIT(8)
+#define GOTGINT_SES_END_DET			BIT(2)
+
+#define GAHBCFG_AHB_SINGLE			BIT(23)
+#define GAHBCFG_NOTI_ALL_DMA_WRIT		BIT(22)
+#define GAHBCFG_REM_MEM_SUPP			BIT(21)
+#define GAHBCFG_P_TXF_EMP_LVL			BIT(8)
+#define GAHBCFG_NP_TXF_EMP_LVL			BIT(7)
+#define GAHBCFG_DMA_EN				BIT(5)
+#define GAHBCFG_HBSTLEN_MASK			GENMASK(4, 1)
+#define GAHBCFG_HBSTLEN_SINGLE			0
+#define GAHBCFG_HBSTLEN_INCR			1
+#define GAHBCFG_HBSTLEN_INCR4			3
+#define GAHBCFG_HBSTLEN_INCR8			5
+#define GAHBCFG_HBSTLEN_INCR16			7
+#define GAHBCFG_GLBL_INTR_EN			BIT(0)
+#define GAHBCFG_CTRL_MASK			(GAHBCFG_P_TXF_EMP_LVL | \
+						 GAHBCFG_NP_TXF_EMP_LVL | \
+						 GAHBCFG_DMA_EN | \
+						 GAHBCFG_GLBL_INTR_EN)
+
+#define GUSBCFG_FORCEDEVMODE			BIT(30)
+#define GUSBCFG_FORCEHOSTMODE			BIT(29)
+#define GUSBCFG_TXENDDELAY			BIT(28)
+#define GUSBCFG_ICTRAFFICPULLREMOVE		BIT(27)
+#define GUSBCFG_ICUSBCAP			BIT(26)
+#define GUSBCFG_ULPI_INT_PROT_DIS		BIT(25)
+#define GUSBCFG_INDICATORPASSTHROUGH		BIT(24)
+#define GUSBCFG_INDICATORCOMPLEMENT		BIT(23)
+#define GUSBCFG_TERMSELDLPULSE			BIT(22)
+#define GUSBCFG_ULPI_INT_VBUS_IND		BIT(21)
+#define GUSBCFG_ULPI_EXT_VBUS_DRV		BIT(20)
+#define GUSBCFG_ULPI_CLK_SUSP_M			BIT(19)
+#define GUSBCFG_ULPI_AUTO_RES			BIT(18)
+#define GUSBCFG_ULPI_FS_LS			BIT(17)
+#define GUSBCFG_OTG_UTMI_FS_SEL			BIT(16)
+#define GUSBCFG_PHY_LP_CLK_SEL			BIT(15)
+#define GUSBCFG_USBTRDTIM_MASK			GENMASK(14, 10)
+#define GUSBCFG_HNPCAP				BIT(9)
+#define GUSBCFG_SRPCAP				BIT(8)
+#define GUSBCFG_DDRSEL				BIT(7)
+#define GUSBCFG_PHYSEL				BIT(6)
+#define GUSBCFG_FSINTF				BIT(5)
+#define GUSBCFG_ULPI_UTMI_SEL			BIT(4)
+#define GUSBCFG_PHYIF16				BIT(3)
+#define GUSBCFG_TOUTCAL_MASK			GENMASK(2, 0)
+
+#define GRSTCTL_AHBIDLE				BIT(31)
+#define GRSTCTL_DMAREQ				BIT(30)
+#define GRSTCTL_CSFTRST_DONE			BIT(29)
+#define GRSTCTL_TXFNUM_MASK			GENMASK(10, 6)
+#define GRSTCTL_TXFFLSH				BIT(5)
+#define GRSTCTL_RXFFLSH				BIT(4)
+#define GRSTCTL_IN_TKNQ_FLSH			BIT(3)
+#define GRSTCTL_FRMCNTRRST			BIT(2)
+#define GRSTCTL_HSFTRST				BIT(1)
+#define GRSTCTL_CSFTRST				BIT(0)
+#define GRSTCTL_TXFNUM_ALL			0x10
+
+#define GINTSTS_WKUPINT				BIT(31)
+#define GINTSTS_SESSREQINT			BIT(30)
+#define GINTSTS_DISCONNINT			BIT(29)
+#define GINTSTS_CONIDSTSCHNG			BIT(28)
+#define GINTSTS_LPMTRANRCVD			BIT(27)
+#define GINTSTS_PTXFEMP				BIT(26)
+#define GINTSTS_HCHINT				BIT(25)
+#define GINTSTS_PRTINT				BIT(24)
+#define GINTSTS_RESETDET			BIT(23)
+#define GINTSTS_FET_SUSP			BIT(22)
+#define GINTSTS_INCOMPL_IP			BIT(21)
+#define GINTSTS_INCOMPL_SOOUT			BIT(21)
+#define GINTSTS_INCOMPL_SOIN			BIT(20)
+#define GINTSTS_OEPINT				BIT(19)
+#define GINTSTS_IEPINT				BIT(18)
+#define GINTSTS_EPMIS				BIT(17)
+#define GINTSTS_RESTOREDONE			BIT(16)
+#define GINTSTS_EOPF				BIT(15)
+#define GINTSTS_ISOUTDROP			BIT(14)
+#define GINTSTS_ENUMDONE			BIT(13)
+#define GINTSTS_USBRST				BIT(12)
+#define GINTSTS_USBSUSP				BIT(11)
+#define GINTSTS_ERLYSUSP			BIT(10)
+#define GINTSTS_I2CINT				BIT(9)
+#define GINTSTS_ULPI_CK_INT			BIT(8)
+#define GINTSTS_GOUTNAKEFF			BIT(7)
+#define GINTSTS_GINNAKEFF			BIT(6)
+#define GINTSTS_NPTXFEMP			BIT(5)
+#define GINTSTS_RXFLVL				BIT(4)
+#define GINTSTS_SOF				BIT(3)
+#define GINTSTS_OTGINT				BIT(2)
+#define GINTSTS_MODEMIS				BIT(1)
+#define GINTSTS_CURMODE_HOST			BIT(0)
+
+#define GRXSTS_FN_MASK				GENMASK(31, 25)
+#define GRXSTS_PKTSTS_MASK			GENMASK(20, 17)
+#define GRXSTS_PKTSTS_GLOBALOUTNAK		1
+#define GRXSTS_PKTSTS_OUTRX			2
+#define GRXSTS_PKTSTS_HCHIN			2
+#define GRXSTS_PKTSTS_OUTDONE			3
+#define GRXSTS_PKTSTS_HCHIN_XFER_COMP		3
+#define GRXSTS_PKTSTS_SETUPDONE			4
+#define GRXSTS_PKTSTS_DATATOGGLEERR		5
+#define GRXSTS_PKTSTS_SETUPRX			6
+#define GRXSTS_PKTSTS_HCHHALTED			7
+#define GRXSTS_DPID_MASK			GENMASK(16, 15)
+#define GRXSTS_BYTECNT_MASK			GENMASK(14, 4)
+#define GRXSTS_HCHNUM_MASK			GENMASK(3, 0)
+
+#define GRXFSIZ_DEPTH_MASK			GENMASK(15, 0)
+
+#define GI2CCTL_BSYDNE				BIT(31)
+#define GI2CCTL_RW				BIT(30)
+#define GI2CCTL_I2CDATSE0			BIT(28)
+#define GI2CCTL_I2CDEVADDR_MASK			GENMASK(27, 26)
+#define GI2CCTL_I2CSUSPCTL			BIT(25)
+#define GI2CCTL_ACK				BIT(24)
+#define GI2CCTL_I2CEN				BIT(23)
+#define GI2CCTL_ADDR_MASK			GENMASK(22, 16)
+#define GI2CCTL_REGADDR_MASK			GENMASK(15, 8)
+#define GI2CCTL_RWDATA_MASK			GENMASK(7, 0)
+
+#define GGPIO_STM32_OTG_GCCFG_IDEN		BIT(22)
+#define GGPIO_STM32_OTG_GCCFG_VBDEN		BIT(21)
+#define GGPIO_STM32_OTG_GCCFG_PWRDWN		BIT(16)
+
+#define GSNPSID_ID_MASK				GENMASK(31, 16)
+#define GSNPSID_OTG_ID				0x4f54
+#define GSNPSID_VER_MASK			GENMASK(15, 0)
+
+#define GHWCFG2_OTG_ENABLE_IC_USB		BIT(31)
+#define GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK		GENMASK(30, 26)
+#define GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK	GENMASK(25, 24)
+#define GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK	GENMASK(23, 22)
+#define GHWCFG2_MULTI_PROC_INT			BIT(20)
+#define GHWCFG2_DYNAMIC_FIFO			BIT(19)
+#define GHWCFG2_PERIO_EP_SUPPORTED		BIT(18)
+#define GHWCFG2_NUM_HOST_CHAN_MASK		GENMASK(17, 14)
+#define GHWCFG2_NUM_DEV_EP_MASK			GENMASK(13, 10)
+#define GHWCFG2_FS_PHY_TYPE_MASK		GENMASK(9, 8)
+#define GHWCFG2_FS_PHY_TYPE_NOT_SUPPORTED	0
+#define GHWCFG2_FS_PHY_TYPE_DEDICATED		1
+#define GHWCFG2_FS_PHY_TYPE_SHARED_UTMI		2
+#define GHWCFG2_FS_PHY_TYPE_SHARED_ULPI		3
+#define GHWCFG2_HS_PHY_TYPE_MASK		GENMASK(7, 6)
+#define GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED	0
+#define GHWCFG2_HS_PHY_TYPE_UTMI		1
+#define GHWCFG2_HS_PHY_TYPE_ULPI		2
+#define GHWCFG2_HS_PHY_TYPE_UTMI_ULPI		3
+#define GHWCFG2_POINT2POINT			BIT(5)
+#define GHWCFG2_ARCHITECTURE_MASK		GENMASK(4, 3)
+#define GHWCFG2_SLAVE_ONLY_ARCH			0
+#define GHWCFG2_EXT_DMA_ARCH			1
+#define GHWCFG2_INT_DMA_ARCH			2
+#define GHWCFG2_OP_MODE_MASK			GENMASK(2, 0)
+#define GHWCFG2_OP_MODE_HNP_SRP_CAPABLE		0
+#define GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE	1
+#define GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE	2
+#define GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE	3
+#define GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE	4
+#define GHWCFG2_OP_MODE_SRP_CAPABLE_HOST	5
+#define GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST	6
+#define GHWCFG2_OP_MODE_UNDEFINED		7
+
+#define GHWCFG4_DESC_DMA_DYN			BIT(31)
+#define GHWCFG4_DESC_DMA			BIT(30)
+#define GHWCFG4_NUM_IN_EPS_MASK			GENMASK(29, 26)
+#define GHWCFG4_DED_FIFO_EN			BIT(25)
+#define GHWCFG4_SESSION_END_FILT_EN		BIT(24)
+#define GHWCFG4_B_VALID_FILT_EN			BIT(23)
+#define GHWCFG4_A_VALID_FILT_EN			BIT(22)
+#define GHWCFG4_VBUS_VALID_FILT_EN		BIT(21)
+#define GHWCFG4_IDDIG_FILT_EN			BIT(20)
+#define GHWCFG4_NUM_DEV_MODE_CTRL_EP_MASK	GENMASK(19, 16)
+#define GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK	GENMASK(15, 14)
+#define GHWCFG4_UTMI_PHY_DATA_WIDTH_8		0
+#define GHWCFG4_UTMI_PHY_DATA_WIDTH_16		1
+#define GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16	2
+#define GHWCFG4_ACG_SUPPORTED			BIT(12)
+#define GHWCFG4_IPG_ISOC_SUPPORTED		BIT(11)
+#define GHWCFG4_SERVICE_INTERVAL_SUPPORTED      BIT(10)
+#define GHWCFG4_XHIBER				BIT(7)
+#define GHWCFG4_HIBER				BIT(6)
+#define GHWCFG4_MIN_AHB_FREQ			BIT(5)
+#define GHWCFG4_POWER_OPTIMIZ			BIT(4)
+#define GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK	GENMASK(3, 0)
+
+#define FIFOSIZE_DEPTH_MASK			GENMASK(31, 16)
+#define FIFOSIZE_STARTADDR_MASK			GENMASK(15, 0)
+
+/* Host Register */
+#define HCFG_MODECHTIMEN			BIT(31)
+#define HCFG_PERSCHEDENA			BIT(26)
+#define HCFG_FRLISTEN_MASK			GENMASK(25, 24)
+#define HCFG_FRLISTEN_8				0
+#define HCFG_FRLISTEN_16			1
+#define HCFG_FRLISTEN_32			2
+#define HCFG_FRLISTEN_64			3
+#define HCFG_DESCDMA				BIT(23)
+#define HCFG_RESVALID_MASK			GENMASK(15, 8)
+#define HCFG_ENA32KHZ				BIT(7)
+#define HCFG_FSLSSUPP				BIT(2)
+#define HCFG_FSLSPCLKSEL_MASK			GENMASK(2, 0)
+#define HCFG_FSLSPCLKSEL_30_60_MHZ		0
+#define HCFG_FSLSPCLKSEL_48_MHZ			1
+#define HCFG_FSLSPCLKSEL_6_MHZ			2
+
+#define HFNUM_FRREM_MASK			GENMASK(31, 16)
+#define HFNUM_FRNUM_MASK			GENMASK(15, 0)
+
+#define HPRT0_SPD_MASK				GENMASK(18, 17)
+#define HPRT0_SPD_HIGH_SPEED			0
+#define HPRT0_SPD_FULL_SPEED			1
+#define HPRT0_SPD_LOW_SPEED			2
+#define HPRT0_TSTCTL_MASK			GENMASK(16, 13)
+#define HPRT0_PWR				BIT(12)
+#define HPRT0_LNSTS_MASK			GENMASK(11, 10)
+#define HPRT0_RST				BIT(8)
+#define HPRT0_SUSP				BIT(7)
+#define HPRT0_RES				BIT(6)
+#define HPRT0_OVRCURRCHG			BIT(5)
+#define HPRT0_OVRCURRACT			BIT(4)
+#define HPRT0_ENACHG				BIT(3)
+#define HPRT0_ENA				BIT(2)
+#define HPRT0_CONNDET				BIT(1)
+#define HPRT0_CONNSTS				BIT(0)
+#define HPRT0_W1C_MASK				(HPRT0_CONNDET | \
+						 HPRT0_ENA | \
+						 HPRT0_ENACHG | \
+						 HPRT0_OVRCURRCHG)
+
+#define HCCHAR_CHENA				BIT(31)
+#define HCCHAR_CHDIS				BIT(30)
+#define HCCHAR_ODDFRM				BIT(29)
+#define HCCHAR_DEVADDR_MASK			GENMASK(28, 22)
+#define HCCHAR_MULTICNT_MASK			GENMASK(21, 20)
+#define HCCHAR_EPTYPE_MASK			GENMASK(19, 18)
+#define HCCHAR_EPTYPE_CONTROL			0
+#define HCCHAR_EPTYPE_ISOC			1
+#define HCCHAR_EPTYPE_BULK			2
+#define HCCHAR_EPTYPE_INTR			3
+#define HCCHAR_LSPDDEV				BIT(17)
+#define HCCHAR_EPDIR				BIT(15)
+#define HCCHAR_EPNUM_MASK			GENMASK(14, 11)
+#define HCCHAR_MPS_MASK				GENMASK(10, 0)
+
+#define HCSPLT_SPLTENA				BIT(31)
+#define HCSPLT_COMPSPLT				BIT(16)
+#define HCSPLT_XACTPOS_MASK			GENMASK(15, 14)
+#define HCSPLT_XACTPOS_MID			0
+#define HCSPLT_XACTPOS_END			1
+#define HCSPLT_XACTPOS_BEGIN			2
+#define HCSPLT_XACTPOS_ALL			3
+#define HCSPLT_HUBADDR_MASK			GENMASK(13, 7)
+#define HCSPLT_PRTADDR_MASK			GENMASK(6, 0)
+
+#define HCINTMSK_FRM_LIST_ROLL			BIT(13)
+#define HCINTMSK_XCS_XACT			BIT(12)
+#define HCINTMSK_BNA				BIT(11)
+#define HCINTMSK_DATATGLERR			BIT(10)
+#define HCINTMSK_FRMOVRUN			BIT(9)
+#define HCINTMSK_BBLERR				BIT(8)
+#define HCINTMSK_XACTERR			BIT(7)
+#define HCINTMSK_NYET				BIT(6)
+#define HCINTMSK_ACK				BIT(5)
+#define HCINTMSK_NAK				BIT(4)
+#define HCINTMSK_STALL				BIT(3)
+#define HCINTMSK_AHBERR				BIT(2)
+#define HCINTMSK_CHHLTD				BIT(1)
+#define HCINTMSK_XFERCOMPL			BIT(0)
+
+#define TSIZ_DOPNG				BIT(31)
+#define TSIZ_SC_MC_PID_MASK			GENMASK(30, 29)
+#define TSIZ_SC_MC_PID_DATA0			0
+#define TSIZ_SC_MC_PID_DATA2			1
+#define TSIZ_SC_MC_PID_DATA1			2
+#define TSIZ_SC_MC_PID_MDATA			3
+#define TSIZ_SC_MC_PID_SETUP			3
+#define TSIZ_PKTCNT_MASK			GENMASK(28, 19)
+#define TSIZ_NTD_MASK				GENMASK(15, 8)
+#define TSIZ_SCHINFO_MASK			GENMASK(7, 0)
+#define TSIZ_XFERSIZE_MASK			GENMASK(18, 0)
+
+/* Device Mode Register */
+#define DCFG_DESCDMA_EN				BIT(23)
+#define DCFG_EPMISCNT_MASK			GENMASK(22, 18)
+#define DCFG_IPG_ISOC_SUPPORDED			BIT(17)
+#define DCFG_PERFRINT_MASK			GENMASK(12, 11)
+#define DCFG_DEVADDR_MASK			GENMASK(10, 4)
+#define DCFG_NZ_STS_OUT_HSHK			BIT(2)
+#define DCFG_DEVSPD_MASK			GENMASK(1, 0)
+#define DCFG_DEVSPD_HS				0
+#define DCFG_DEVSPD_FS				1
+#define DCFG_DEVSPD_LS				2
+#define DCFG_DEVSPD_FS48			3
+
+#define DCTL_SERVICE_INTERVAL_SUPPORTED		BIT(19)
+#define DCTL_PWRONPRGDONE			BIT(11)
+#define DCTL_CGOUTNAK				BIT(10)
+#define DCTL_SGOUTNAK				BIT(9)
+#define DCTL_CGNPINNAK				BIT(8)
+#define DCTL_SGNPINNAK				BIT(7)
+#define DCTL_TSTCTL_MASK			GENMASK(6, 4)
+#define DCTL_GOUTNAKSTS				BIT(3)
+#define DCTL_GNPINNAKSTS			BIT(2)
+#define DCTL_SFTDISCON				BIT(1)
+#define DCTL_RMTWKUPSIG				BIT(0)
+
+#define DSTS_SOFFN_MASK				GENMASK(21, 8)
+#define DSTS_ERRATICERR				BIT(3)
+#define DSTS_ENUMSPD_MASK			GENMASK(2, 1)
+#define DSTS_ENUMSPD_HS				0
+#define DSTS_ENUMSPD_FS				1
+#define DSTS_ENUMSPD_LS				2
+#define DSTS_ENUMSPD_FS48			3
+#define DSTS_SUSPSTS				BIT(0)
+
+#define DIEPMSK_NAKMSK				BIT(13)
+#define DIEPMSK_BNAININTRMSK			BIT(9)
+#define DIEPMSK_TXFIFOUNDRNMSK			BIT(8)
+#define DIEPMSK_TXFIFOEMPTY			BIT(7)
+#define DIEPMSK_INEPNAKEFFMSK			BIT(6)
+#define DIEPMSK_INTKNEPMISMSK			BIT(5)
+#define DIEPMSK_INTKNTXFEMPMSK			BIT(4)
+#define DIEPMSK_TIMEOUTMSK			BIT(3)
+#define DIEPMSK_AHBERRMSK			BIT(2)
+#define DIEPMSK_EPDISBLDMSK			BIT(1)
+#define DIEPMSK_XFERCOMPLMSK			BIT(0)
+
+#define DOEPMSK_BNAMSK				BIT(9)
+#define DOEPMSK_BACK2BACKSETUP			BIT(6)
+#define DOEPMSK_STSPHSERCVDMSK			BIT(5)
+#define DOEPMSK_OUTTKNEPDISMSK			BIT(4)
+#define DOEPMSK_SETUPMSK			BIT(3)
+#define DOEPMSK_AHBERRMSK			BIT(2)
+#define DOEPMSK_EPDISBLDMSK			BIT(1)
+#define DOEPMSK_XFERCOMPLMSK			BIT(0)
+
+#define DAINT_OUTEP_MASK			GENMASK(31, 16)
+#define DAINT_INEP_MASK				GENMASK(15, 0)
+
+#define D0EPCTL_MPS_MASK			GENMASK(1, 0)
+#define D0EPCTL_MPS_64				0
+#define D0EPCTL_MPS_32				1
+#define D0EPCTL_MPS_16				2
+#define D0EPCTL_MPS_8				3
+
+#define DXEPCTL_EPENA				BIT(31)
+#define DXEPCTL_EPDIS				BIT(30)
+#define DXEPCTL_SETD1PID			BIT(29)
+#define DXEPCTL_SETODDFR			BIT(29)
+#define DXEPCTL_SETD0PID			BIT(28)
+#define DXEPCTL_SETEVENFR			BIT(28)
+#define DXEPCTL_SNAK				BIT(27)
+#define DXEPCTL_CNAK				BIT(26)
+#define DXEPCTL_TXFNUM_MASK			GENMASK(25, 22)
+#define DXEPCTL_STALL				BIT(21)
+#define DXEPCTL_SNP				BIT(20)
+#define DXEPCTL_EPTYPE_MASK			GENMASK(19, 18)
+#define DXEPCTL_EPTYPE_CONTROL			0
+#define DXEPCTL_EPTYPE_ISO			1
+#define DXEPCTL_EPTYPE_BULK			2
+#define DXEPCTL_EPTYPE_INTERRUPT		3
+#define DXEPCTL_NAKSTS				BIT(17)
+#define DXEPCTL_DPID				BIT(16)
+#define DXEPCTL_EOFRNUM				BIT(16)
+#define DXEPCTL_USBACTEP			BIT(15)
+#define DXEPCTL_NEXTEP_MASK			GENMASK(14, 11)
+#define DXEPCTL_MPS_MASK			GENMASK(10, 0)
+
+#define DXEPINT_SETUP_RCVD			BIT(15)
+#define DXEPINT_NYETINTRPT			BIT(14)
+#define DXEPINT_NAKINTRPT			BIT(13)
+#define DXEPINT_BBLEERRINTRPT			BIT(12)
+#define DXEPINT_PKTDRPSTS			BIT(11)
+#define DXEPINT_BNAINTR				BIT(9)
+#define DXEPINT_TXFIFOUNDRN			BIT(8)
+#define DXEPINT_OUTPKTERR			BIT(8)
+#define DXEPINT_TXFEMP				BIT(7)
+#define DXEPINT_INEPNAKEFF			BIT(6)
+#define DXEPINT_BACK2BACKSETUP			BIT(6)
+#define DXEPINT_INTKNEPMIS			BIT(5)
+#define DXEPINT_STSPHSERCVD			BIT(5)
+#define DXEPINT_INTKNTXFEMP			BIT(4)
+#define DXEPINT_OUTTKNEPDIS			BIT(4)
+#define DXEPINT_TIMEOUT				BIT(3)
+#define DXEPINT_SETUP				BIT(3)
+#define DXEPINT_AHBERR				BIT(2)
+#define DXEPINT_EPDISBLD			BIT(1)
+#define DXEPINT_XFERCOMPL			BIT(0)
+
+#define DIEPTSIZ0_PKTCNT_MASK			GENMASK(20, 19)
+#define DIEPTSIZ0_XFERSIZE_MASK			GENMASK(6, 0)
+
+#define DOEPTSIZ0_SUPCNT_MASK			GENMASK(30, 29)
+#define DOEPTSIZ0_PKTCNT			BIT(19)
+#define DOEPTSIZ0_XFERSIZE_MASK			GENMASK(6, 0)
+
+#define DXEPTSIZ_MC_MASK			GENMASK(30, 29)
+#define DXEPTSIZ_PKTCNT_MASK			GENMASK(28, 19)
+#define DXEPTSIZ_XFERSIZE_MASK			GENMASK(18, 0)
+
 #endif /* __DWC2_CORE_H_ */
drivers/usb/gadget/dwc2_udc_otg_regs.h +1 -159

182 lines not on this page · raw message

drivers/usb/host/dwc2.h +0 -291

297 lines not on this page · raw message

Signature · 1 line
2.39.2
Reviewed-by on v2 6/8
#13 lore raw

Re: [PATCH v2 6/8] usb: dwc2: Extract macro definitions to common header

Quoted 8 lines · On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

From: Kongyang Liu <[email protected]>

Some macros are shared between host and gadget code, causing duplicated definitions. Move DWC2 macro definitions from host and gadget code into a common header to reduce duplication.

author
#14 lore raw

PATCH v2 7/8 usb: dwc2: Unify flush and reset logic with v4.20a support

In reply to Junhui Liu

From: Kongyang Liu <[email protected]>

This patch merges flush and reset logic for both host and gadget code into a common set of functions, reducing duplication. It also adds support for the updated reset logic to compatible with core version since v4.20a.

This patch mainly refers to the patch in the kernel.

---
 drivers/usb/common/Makefile                |   2 +
 drivers/usb/common/dwc2_core.c             | 103 +++++++++++++++++++++++++++++
 drivers/usb/common/dwc2_core.h             |   4 ++
 drivers/usb/gadget/dwc2_udc_otg.c          |  12 +---
 drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c |   6 +-
 drivers/usb/host/dwc2.c                    |  80 ++--------------------
 6 files changed, 117 insertions(+), 90 deletions(-)
drivers/usb/common/Makefile +2 -0
index 11cc4657a0f403b84b1b8336781e1893d9c7a8f1..73e5bc6d7fdca692276e119911b47db4bf03586a 100644
@@ -4,6 +4,8 @@
 #
 
 obj-$(CONFIG_$(XPL_)DM_USB) += common.o
+obj-$(CONFIG_USB_DWC2) += dwc2_core.o
+obj-$(CONFIG_USB_GADGET_DWC2_OTG) += dwc2_core.o
 obj-$(CONFIG_USB_ISP1760) += usb_urb.o
 obj-$(CONFIG_USB_MUSB_HOST) += usb_urb.o
 obj-$(CONFIG_USB_MUSB_GADGET) += usb_urb.o
drivers/usb/common/dwc2_core.c new file +103 -0

103 lines not on this page · raw message

drivers/usb/common/dwc2_core.h +4 -0
index 862d3b3691c9caf84590d34960df21117848df0a..b50748b9f6b5c7a43524bb1b48972f1394bb63d8 100644
@@ -125,6 +125,10 @@ struct dwc2_core_regs {
 	u8  ep_fifo[16][0x1000];		/* 0x1000 */
 };
 
+int dwc2_core_reset(struct dwc2_core_regs *regs);
+void dwc2_flush_tx_fifo(struct dwc2_core_regs *regs, const int num);
+void dwc2_flush_rx_fifo(struct dwc2_core_regs *regs);
+
 /* Core Global Register */
 #define GOTGCTL_CHIRPEN				BIT(27)
 #define GOTGCTL_MULT_VALID_BC_MASK		GENMASK(26, 22)
drivers/usb/gadget/dwc2_udc_otg.c +3 -9
index 0fc9ee1b90fa71135083a1a6dae2e74992e51181..83d63d7f2162d49c387e0042bd33cc7a5856d407 100644
@@ -471,7 +471,7 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 	u32 max_hw_ep;
 	int pdata_hw_ep;
 
-	writel(GRSTCTL_CSFTRST, &reg->global_regs.grstctl);
+	dwc2_core_reset(reg);
 
 	debug("Resetting OTG controller\n");
 
@@ -575,16 +575,10 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 		       &reg->global_regs.dptxfsizn[i]);
 	}
 	/* Flush the RX FIFO */
-	writel(GRSTCTL_RXFFLSH, &reg->global_regs.grstctl);
-	while (readl(&reg->global_regs.grstctl) & GRSTCTL_RXFFLSH)
-		debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
+	dwc2_flush_rx_fifo(reg);
 
 	/* Flush all the Tx FIFO's */
-	writel(FIELD_PREP(GRSTCTL_TXFNUM_MASK, GRSTCTL_TXFNUM_ALL), &reg->global_regs.grstctl);
-	writel(FIELD_PREP(GRSTCTL_TXFNUM_MASK, GRSTCTL_TXFNUM_ALL) | GRSTCTL_TXFFLSH,
-	       &reg->global_regs.grstctl);
-	while (readl(&reg->global_regs.grstctl) & GRSTCTL_TXFFLSH)
-		debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
+	dwc2_flush_tx_fifo(reg, GRSTCTL_TXFNUM_ALL);
 
 	/* 13. Clear NAK bit of EP0, EP1, EP2*/
 	/* For Slave mode*/
drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c +1 -5
index 64d2fe7bbde4494b4cbcdf57032b720901fdd4eb..2be93592c423df7a9acea473b0e84e1f948999be 100644
@@ -164,11 +164,7 @@ static int setdma_tx(struct dwc2_ep *ep, struct dwc2_request *req)
 		pktcnt = (length - 1)/(ep->ep.maxpacket) + 1;
 
 	/* Flush the endpoint's Tx FIFO */
-	writel(FIELD_PREP(GRSTCTL_TXFNUM_MASK, ep->fifo_num), &reg->global_regs.grstctl);
-	writel(FIELD_PREP(GRSTCTL_TXFNUM_MASK, ep->fifo_num) | GRSTCTL_TXFFLSH,
-	       &reg->global_regs.grstctl);
-	while (readl(&reg->global_regs.grstctl) & GRSTCTL_TXFFLSH)
-		;
+	dwc2_flush_tx_fifo(reg, ep->fifo_num);
 
 	writel(phys_to_bus((unsigned long)ep->dma_buf), &reg->device_regs.in_endp[ep_num].diepdma);
 	writel(FIELD_PREP(DXEPTSIZ_PKTCNT_MASK, pktcnt) |
drivers/usb/host/dwc2.c +4 -76

104 lines not on this page · raw message

Signature · 1 line
2.39.2
#15 lore raw

Re: [PATCH v2 7/8] usb: dwc2: Unify flush and reset logic with v4.20a support

Quoted 60 lines · On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

From: Kongyang Liu <[email protected]>

This patch merges flush and reset logic for both host and gadget code into a common set of functions, reducing duplication. It also adds support for the updated reset logic to compatible with core version since v4.20a.

This patch mainly refers to the patch in the kernel.

---
  drivers/usb/common/Makefile                |   2 +
  drivers/usb/common/dwc2_core.c             | 103 +++++++++++++++++++++++++++++
  drivers/usb/common/dwc2_core.h             |   4 ++
  drivers/usb/gadget/dwc2_udc_otg.c          |  12 +---
  drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c |   6 +-
  drivers/usb/host/dwc2.c                    |  80 ++--------------------
  6 files changed, 117 insertions(+), 90 deletions(-)
drivers/usb/common/Makefile +2 -0
index 11cc4657a0f403b84b1b8336781e1893d9c7a8f1..73e5bc6d7fdca692276e119911b47db4bf03586a 100644
@@ -4,6 +4,8 @@
  #
 
  obj-$(CONFIG_$(XPL_)DM_USB) += common.o
+obj-$(CONFIG_USB_DWC2) += dwc2_core.o
+obj-$(CONFIG_USB_GADGET_DWC2_OTG) += dwc2_core.o
  obj-$(CONFIG_USB_ISP1760) += usb_urb.o
  obj-$(CONFIG_USB_MUSB_HOST) += usb_urb.o
  obj-$(CONFIG_USB_MUSB_GADGET) += usb_urb.o
drivers/usb/common/dwc2_core.c new file +21 -0
new file mode 100644
index 0000000000000000000000000000000000000000..323326e05d7b0318d883d4a3b8186b812c42f9ca
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2024, Kongyang Liu <[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <wait_bit.h>
+
+#include "dwc2_core.h"
+
+int dwc2_core_reset(struct dwc2_core_regs *regs)
+{
+	u32 snpsid;
+	int ret;
+	bool host_mode = false;
+
+	if (!(readl(&regs->global_regs.gotgctl) & GOTGCTL_CONID_B) ||
+	    (readl(&regs->global_regs.gusbcfg) & GUSBCFG_FORCEDEVMODE))
+		host_mode = true;

drivers/usb/common/dwc2_core.c line 31

Quoted 10 lines
drivers/usb/common/dwc2_core.c new file +10 -0
+
+	/* Core Soft Reset */
+	snpsid = readl(&regs->global_regs.gsnpsid);
+	writel(GRSTCTL_CSFTRST, &regs->global_regs.grstctl);
+	if (FIELD_GET(GSNPSID_VER_MASK, snpsid) < 0x420a) {
+		ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST,
+					false, 1000, false);
+		if (ret) {
+			log_warning("%s: Waiting for GRSTCTL_CSFTRST timeout\n", __func__);
+			return -EBUSY;

Return -ETIMEDOUT or even better, return ret; to propagate the return value of wait_for_bit_le32() as-is , which is really -ETIMEDOUT if the wait times out.

drivers/usb/common/dwc2_core.c line 38

Quoted 7 lines
drivers/usb/common/dwc2_core.c new file +7 -0
+		}
+	} else {
+		ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST_DONE,
+					true, 1000, false);
+		if (ret) {
+			log_warning("%s: Waiting for GRSTCTL_CSFTRST_DONE timeout\n", __func__);
+			return -EBUSY;

DTTO, return ret;

drivers/usb/common/dwc2_core.c line 48

Quoted 10 lines
drivers/usb/common/dwc2_core.c new file +10 -0
+		}
+		clrsetbits_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST, GRSTCTL_CSFTRST_DONE);
+	}
+
+	/* Wait for AHB master IDLE state. */
+	ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE,
+				true, 1000, false);
+	if (ret) {
+		log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);
+		return -EBUSY;

DTTO, return ret;

drivers/usb/common/dwc2_core.c line 56

Quoted 8 lines
drivers/usb/common/dwc2_core.c new file +8 -0
+	}
+
+	if (host_mode) {
+		ret = wait_for_bit_le32(&regs->global_regs.gintsts, GINTSTS_CURMODE_HOST,
+					host_mode, 1000, false);
+		if (ret) {
+			log_warning("%s: Waiting for GINTSTS_CURMODE_HOST timeout\n", __func__);
+			return -EBUSY;

DTTO, return ret;

Quoted 6 lines
drivers/usb/common/dwc2_core.c new file +6 -0
+		}
+	}
+
+	return 0;
+}
+

drivers/usb/common/dwc2_core.c line 72

Quoted 10 lines
drivers/usb/common/dwc2_core.c new file +10 -0
+void dwc2_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
+{
+	int ret;
+
+	log_debug("Flush Tx FIFO %d\n", num);
+
+	/* Wait for AHB master IDLE state */
+	ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE, true, 1000, false);
+	if (ret)
+		log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);

Should this function return an integer and an error code on failure ?

Quoted 19 lines
drivers/usb/common/dwc2_core.c new file +19 -0
+	writel(GRSTCTL_TXFFLSH | FIELD_PREP(GRSTCTL_TXFNUM_MASK, num), &regs->global_regs.grstctl);
+
+	ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_TXFFLSH, false, 1000, false);
+	if (ret)
+		log_warning("%s: Waiting for GRSTCTL_TXFFLSH timeout\n", __func__);
+
+	/* Wait for 3 PHY Clocks */
+	udelay(1);
+}
+
+void dwc2_flush_rx_fifo(struct dwc2_core_regs *regs)
+{
+	int ret;
+
+	log_debug("Flush Rx FIFO\n");
+
+	/* Wait for AHB master IDLE state */
+	ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE, true, 1000, false);
+	if (ret)

drivers/usb/common/dwc2_core.c line 102

Quoted 10 lines
drivers/usb/common/dwc2_core.c new file +10 -0
+		log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);
+
+	writel(GRSTCTL_RXFFLSH, &regs->global_regs.grstctl);
+
+	ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_RXFFLSH, false, 1000, false);
+	if (ret)
+		log_warning("%s: Waiting for GRSTCTL_RXFFLSH timeout\n", __func__);
+
+	/* Wait for 3 PHY Clocks */
+	udelay(1);

Shouldn't this delay be derived from the PHY clock frequency somehow ? Are we sure 1us is always sufficient ?

Quoted 1 line
drivers/usb/common/dwc2_core.c new file +1 -0
+}

[...]

author
#16 lore raw

Re: [PATCH v2 7/8] usb: dwc2: Unify flush and reset logic with v4.20a support

On 2025/1/1 02:08, Marek Vasut wrote:
On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:
From: Kongyang Liu <[email protected]><mailto:[email protected]>

This patch merges flush and reset logic for both host and gadget code into a common set of functions, reducing duplication. It also adds support for the updated reset logic to compatible with core version since v4.20a.

This patch mainly refers to the patch in the kernel.

---
  drivers/usb/common/Makefile                |   2 +
  drivers/usb/common/dwc2_core.c             | 103 +++++++++++++++++++++++++++++
  drivers/usb/common/dwc2_core.h             |   4 ++
  drivers/usb/gadget/dwc2_udc_otg.c          |  12 +---
  drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c |   6 +-
  drivers/usb/host/dwc2.c                    |  80 ++--------------------
  6 files changed, 117 insertions(+), 90 deletions(-)
drivers/usb/common/Makefile +2 -0
index 11cc4657a0f403b84b1b8336781e1893d9c7a8f1..73e5bc6d7fdca692276e119911b47db4bf03586a 100644
@@ -4,6 +4,8 @@
  #
    obj-$(CONFIG_$(XPL_)DM_USB) += common.o
+obj-$(CONFIG_USB_DWC2) += dwc2_core.o
+obj-$(CONFIG_USB_GADGET_DWC2_OTG) += dwc2_core.o
  obj-$(CONFIG_USB_ISP1760) += usb_urb.o
  obj-$(CONFIG_USB_MUSB_HOST) += usb_urb.o
  obj-$(CONFIG_USB_MUSB_GADGET) += usb_urb.o
drivers/usb/common/dwc2_core.c new file +31 -0
new file mode 100644
index 0000000000000000000000000000000000000000..323326e05d7b0318d883d4a3b8186b812c42f9ca
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2024, Kongyang Liu <[email protected]><mailto:[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <wait_bit.h>
+
+#include "dwc2_core.h"
+
+int dwc2_core_reset(struct dwc2_core_regs *regs)
+{
+    u32 snpsid;
+    int ret;
+    bool host_mode = false;
+
+    if (!(readl(&regs->global_regs.gotgctl) & GOTGCTL_CONID_B) ||
+        (readl(&regs->global_regs.gusbcfg) & GUSBCFG_FORCEDEVMODE))
+        host_mode = true;
+
+    /* Core Soft Reset */
+    snpsid = readl(&regs->global_regs.gsnpsid);
+    writel(GRSTCTL_CSFTRST, &regs->global_regs.grstctl);
+    if (FIELD_GET(GSNPSID_VER_MASK, snpsid) < 0x420a) {
+        ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST,
+                    false, 1000, false);
+        if (ret) {
+            log_warning("%s: Waiting for GRSTCTL_CSFTRST timeout\n", __func__);
+            return -EBUSY;

Return -ETIMEDOUT or even better, return ret; to propagate the return value of wait_for_bit_le32() as-is , which is really -ETIMEDOUT if the wait times out.

Okay, I'll modify it to return ret;

+ }
+ } else {
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST_DONE,
+ true, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_CSFTRST_DONE timeout\n", __func__);
+ return -EBUSY;

DTTO, return ret;

+ }
+ clrsetbits_le32(&regs->global_regs.grstctl, GRSTCTL_CSFTRST, GRSTCTL_CSFTRST_DONE);
+ }
+
+ /* Wait for AHB master IDLE state. */
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE,
+ true, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);
+ return -EBUSY;

DTTO, return ret;

+ }
+
+ if (host_mode) {
+ ret = wait_for_bit_le32(&regs->global_regs.gintsts, GINTSTS_CURMODE_HOST,
+ host_mode, 1000, false);
+ if (ret) {
+ log_warning("%s: Waiting for GINTSTS_CURMODE_HOST timeout\n", __func__);
+ return -EBUSY;

DTTO, return ret;

+ }
+ }
+
+ return 0;
+}
+
+void dwc2_flush_tx_fifo(struct dwc2_core_regs *regs, const int num) +{
+ int ret;
+
+ log_debug("Flush Tx FIFO %d\n", num);
+

+    /* Wait for AHB master IDLE state */
+    ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE, true, 1000, false);
+    if (ret)

+ log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);

Should this function return an integer and an error code on failure ?

Sure, I think I will add return ret; here in the next version,

+ writel(GRSTCTL_TXFFLSH | FIELD_PREP(GRSTCTL_TXFNUM_MASK, num), &regs->global_regs.grstctl);
+
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_TXFFLSH, false, 1000, false);
+ if (ret)
+ log_warning("%s: Waiting for GRSTCTL_TXFFLSH timeout\n", __func__);
+
+ /* Wait for 3 PHY Clocks */
+ udelay(1);
+}
+
+void dwc2_flush_rx_fifo(struct dwc2_core_regs *regs) +{
+ int ret;
+
+ log_debug("Flush Rx FIFO\n");
+

+    /* Wait for AHB master IDLE state */
+    ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_AHBIDLE, true, 1000, false);
+    if (ret)

+ log_warning("%s: Waiting for GRSTCTL_AHBIDLE timeout\n", __func__);
+
+ writel(GRSTCTL_RXFFLSH, &regs->global_regs.grstctl);
+
+ ret = wait_for_bit_le32(&regs->global_regs.grstctl, GRSTCTL_RXFFLSH, false, 1000, false);
+ if (ret)
+ log_warning("%s: Waiting for GRSTCTL_RXFFLSH timeout\n", __func__);
+
+ /* Wait for 3 PHY Clocks */
+ udelay(1);

Shouldn't this delay be derived from the PHY clock frequency somehow ? Are we sure 1us is always sufficient ?

According to the datasheet, the PHY clock can be set to 30/48/60MHz, so 1us is sufficient even for 30MHz. And it is 10 times 3 PHY clocks with slowest clock, so I think 1us is acceptable.

Best regards,
Junhui Liu

+}
[...]

author
#17 lore raw

PATCH v2 8/8 usb: dwc2: Replace uint<x>_t types with u<x>

In reply to Junhui Liu

From: Kongyang Liu <[email protected]>

Updates all instances of uint8_t, uint16_t, and uint32_t to u8, u16, and u32 respectively, ensuring consistent use of kernel-preferred types and resolving checkpatch.pl warnings.

---
 drivers/usb/gadget/dwc2_udc_otg.c |   4 +-
 drivers/usb/host/dwc2.c           | 100 +++++++++++++++++++-------------------
 2 files changed, 52 insertions(+), 52 deletions(-)
drivers/usb/gadget/dwc2_udc_otg.c +2 -2
index 83d63d7f2162d49c387e0042bd33cc7a5856d407..85bf262c103d9b38c7fa655b56e75f6e1b9ce37b 100644
@@ -466,8 +466,8 @@ static void reconfig_usbd(struct dwc2_udc *dev)
 	/* 2. Soft-reset OTG Core and then unreset again. */
 	int i;
 	unsigned int uTemp;
-	uint32_t dflt_gusbcfg;
-	uint32_t rx_fifo_sz, tx_fifo_sz, np_tx_fifo_sz;
+	u32 dflt_gusbcfg;
+	u32 rx_fifo_sz, tx_fifo_sz, np_tx_fifo_sz;
 	u32 max_hw_ep;
 	int pdata_hw_ep;
 
drivers/usb/host/dwc2.c +50 -50

244 lines not on this page · raw message

Signature · 1 line
2.39.2
Reviewed-by on v2 8/8
#18 lore raw

Re: [PATCH v2 8/8] usb: dwc2: Replace uint<x>_t types with u<x>

Quoted 8 lines · On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

From: Kongyang Liu <[email protected]>

Updates all instances of uint8_t, uint16_t, and uint32_t to u8, u16, and u32 respectively, ensuring consistent use of kernel-preferred types and resolving checkpatch.pl warnings.

Tested-by on the v2 series
#19 lore raw
In reply to Junhui Liu
Quoted 12 lines · On Mon, 30 Dec 2024 at 14:35, Junhui Liu via B4 Relay <…

This series improves the USB DWC2 driver by extracting register definitions into a common file for better readability and updating the reset method to reflect changes in version 4.20a, including the new GRSTCTL_CSFTRST_DONE bit for reset completion indication.

This series has been tested on two platforms:
- MK808 with RK3066 SoC and dwc2 v2.91a, tested with USB flash drive and connected the board to the PC through the rockusb command. Test log [1].
- K230-CanMV with K230 SoC [2] and dwc2 v4.30a, tested with the onboard rtl8152 USB to net chip and an external rtl8152 adapter connected to the onboard USB-C interface via USB hub. Test log [3].

Quoted 7 lines

[1]
https://gist.github.com/Judehahh/878ace2607d201dc49211f2c655f0fe0#file-mk808-md
[2]
https://developer.canaan-creative.com/k230/en/dev/CanMV_K230_Tutorial.html
[3]
https://gist.github.com/Judehahh/878ace2607d201dc49211f2c655f0fe0#file-k230-canmv-md

With basic network testing on the Raspberry Pi 3+ (uses a USB attached ethernet NIC)

Quoted 42 lines

---
Changes in v2:
- Reorganized structures, renamed macros, and moved code for better clarity and maintainability.
- Added a fix for the incorrect ULPI_UTMI_SEL bit setting.
- Added a fix for incorrectly calculating HBstLen for external DMA mode.
- Extend the changes that replace uint32 with u32 to also include u8 and u16.
- Link to v1:
https://lore.kernel.org/r/[email protected]

---
Junhui Liu (8):
usb: dwc2: Extract register definitions to common header file
usb: dwc2: Fix incorrect ULPI_UTMI_SEL bit setting
USB: dwc2: Fix HBstLen setting for external DMA mode
usb: dwc2: Clean up with bitfield macros
usb: dwc2: Align macros with Linux kernel definitions
usb: dwc2: Extract macro definitions to common header
usb: dwc2: Unify flush and reset logic with v4.20a support
usb: dwc2: Replace uint<x>_t types with u<x>

 drivers/usb/common/Makefile                |   2 +
 drivers/usb/common/dwc2_core.c             | 103 ++++
 drivers/usb/common/dwc2_core.h             | 560 +++++++++++++++++++++
 drivers/usb/gadget/dwc2_udc_otg.c          | 125 ++---
 drivers/usb/gadget/dwc2_udc_otg_regs.h     | 289 ++---------
 drivers/usb/gadget/dwc2_udc_otg_xfer_dma.c | 306 ++++++------
 drivers/usb/host/dwc2.c                    | 494 ++++++++-----------
 drivers/usb/host/dwc2.h                    | 750
+----------------------------
 8 files changed, 1123 insertions(+), 1506 deletions(-)

---
base-commit: cb7410257ac930a6fb05f1c63b291b7d399f19f2
change-id: 20241218-dwc2-dev-955c21a81118

Best regards,
--
Junhui Liu <[email protected]>

#20 lore raw
In reply to Junhui Liu
Quoted 12 lines · On 12/30/24 4:30 AM, Junhui Liu via B4 Relay wrote:

This series improves the USB DWC2 driver by extracting register definitions into a common file for better readability and updating the reset method to reflect changes in version 4.20a, including the new GRSTCTL_CSFTRST_DONE bit for reset completion indication.

This series has been tested on two platforms:
- MK808 with RK3066 SoC and dwc2 v2.91a, tested with USB flash drive and connected the board to the PC through the rockusb command. Test log [1].
- K230-CanMV with K230 SoC [2] and dwc2 v4.30a, tested with the onboard rtl8152 USB to net chip and an external rtl8152 adapter connected to the onboard USB-C interface via USB hub. Test log [3].

Quoted 3 lines

[1] https://gist.github.com/Judehahh/878ace2607d201dc49211f2c655f0fe0#file-mk808-md
[2] https://developer.canaan-creative.com/k230/en/dev/CanMV_K230_Tutorial.html
[3] https://gist.github.com/Judehahh/878ace2607d201dc49211f2c655f0fe0#file-k230-canmv-md

The series is pretty good, only a few trivial nitpicks here and there.

Thank you for doing this.