Skip to contentSkip to conversation

usb: dwc2: Fix the write to W1C fields in HPRT register

PATCH Superseded [email protected] 2 participants 6 messages Jun 20, 2023 – Jun 21, 2023 USB

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: dwc2: Fix the write to W1C fields in HPRT register

From: Teik Heng Chong <[email protected]>

Fix the write to the HPRT register which treat W1C fields
as if they were mere RW. This leads to unintended clearing of such fields

---

V1->V2
- update subject tags to usb:dwc2

---
 drivers/usb/host/dwc2.c | 34 ++++++++--------------------------
 drivers/usb/host/dwc2.h |  4 ++++
 2 files changed, 12 insertions(+), 26 deletions(-)
drivers/usb/host/dwc2.c +8 -26
index 23060fc369..9818f9be94 100644
@@ -315,9 +315,7 @@ static void dwc_otg_core_host_init(struct udevice *dev,
 
 	/* Turn on the vbus power. */
 	if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
-		hprt0 = readl(&regs->hprt0);
-		hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
-		hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
+		hprt0 = readl(&regs->hprt0) & ~DWC2_HPRT0_W1C_MASK;
 		if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
 			hprt0 |= DWC2_HPRT0_PRTPWR;
 			writel(hprt0, &regs->hprt0);
@@ -748,7 +746,7 @@ static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
 	case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
 		switch (wValue) {
 		case USB_PORT_FEAT_C_CONNECTION:
-			setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTCONNDET);
 			break;
 		}
 		break;
@@ -759,21 +757,13 @@ static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
 			break;
 
 		case USB_PORT_FEAT_RESET:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
 			mdelay(50);
-			clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
+			clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
 			break;
 
 		case USB_PORT_FEAT_POWER:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
 			break;
 
 		case USB_PORT_FEAT_ENABLE:
@@ -1213,14 +1203,9 @@ static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
 		dwc_otg_core_host_init(dev, regs);
 	}
 
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
 	mdelay(50);
-	clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
-		     DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
-		     DWC2_HPRT0_PRTRST);
+	clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
 
 	for (i = 0; i < MAX_DEVICE; i++) {
 		for (j = 0; j < MAX_ENDPOINT; j++) {
@@ -1246,10 +1231,7 @@ static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
 static void dwc2_uninit_common(struct dwc2_core_regs *regs)
 {
 	/* Put everything in reset. */
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
 }
 
 #if !CONFIG_IS_ENABLED(DM_USB)
drivers/usb/host/dwc2.h +4 -0
index a6f562fe60..6f022e33a1 100644
@@ -543,6 +543,10 @@ struct dwc2_core_regs {
 #define DWC2_HPRT0_PRTSPD_LOW				(2 << 17)
 #define DWC2_HPRT0_PRTSPD_MASK				(0x3 << 17)
 #define DWC2_HPRT0_PRTSPD_OFFSET			17
+#define DWC2_HPRT0_W1C_MASK				(DWC2_HPRT0_PRTCONNDET | \
+							DWC2_HPRT0_PRTENA | \
+							DWC2_HPRT0_PRTENCHNG | \
+							DWC2_HPRT0_PRTOVRCURRCHNG)
 #define DWC2_HAINT_CH0					(1 << 0)
 #define DWC2_HAINT_CH0_OFFSET				0
 #define DWC2_HAINT_CH1					(1 << 1)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 99 lines · On 6/20/23 15:52, [email protected] wrote:

From: Teik Heng Chong <[email protected]>

Fix the write to the HPRT register which treat W1C fields
as if they were mere RW. This leads to unintended clearing of such fields

---

V1->V2
- update subject tags to usb:dwc2

---
  drivers/usb/host/dwc2.c | 34 ++++++++--------------------------
  drivers/usb/host/dwc2.h |  4 ++++
  2 files changed, 12 insertions(+), 26 deletions(-)
drivers/usb/host/dwc2.c +8 -26
index 23060fc369..9818f9be94 100644
@@ -315,9 +315,7 @@ static void dwc_otg_core_host_init(struct udevice *dev,
 
  	/* Turn on the vbus power. */
  	if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
-		hprt0 = readl(&regs->hprt0);
-		hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
-		hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
+		hprt0 = readl(&regs->hprt0) & ~DWC2_HPRT0_W1C_MASK;
  		if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
  			hprt0 |= DWC2_HPRT0_PRTPWR;
  			writel(hprt0, &regs->hprt0);
@@ -748,7 +746,7 @@ static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
  	case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
  		switch (wValue) {
  		case USB_PORT_FEAT_C_CONNECTION:
-			setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTCONNDET);
  			break;
  		}
  		break;
@@ -759,21 +757,13 @@ static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
  			break;
 
  		case USB_PORT_FEAT_RESET:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
  			mdelay(50);
-			clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
+			clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
  			break;
 
  		case USB_PORT_FEAT_POWER:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
  			break;
 
  		case USB_PORT_FEAT_ENABLE:
@@ -1213,14 +1203,9 @@ static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
  		dwc_otg_core_host_init(dev, regs);
  	}
 
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
  	mdelay(50);
-	clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
-		     DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
-		     DWC2_HPRT0_PRTRST);
+	clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
 
  	for (i = 0; i < MAX_DEVICE; i++) {
  		for (j = 0; j < MAX_ENDPOINT; j++) {
@@ -1246,10 +1231,7 @@ static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
  static void dwc2_uninit_common(struct dwc2_core_regs *regs)
  {
  	/* Put everything in reset. */
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
  }
 
  #if !CONFIG_IS_ENABLED(DM_USB)
drivers/usb/host/dwc2.h +0 -0
index a6f562fe60..6f022e33a1 100644

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 552

Quoted 10 lines
drivers/usb/host/dwc2.h +4 -0
@@ -543,6 +543,10 @@ struct dwc2_core_regs {
  #define DWC2_HPRT0_PRTSPD_LOW				(2 << 17)
  #define DWC2_HPRT0_PRTSPD_MASK				(0x3 << 17)
  #define DWC2_HPRT0_PRTSPD_OFFSET			17
+#define DWC2_HPRT0_W1C_MASK				(DWC2_HPRT0_PRTCONNDET | \
+							DWC2_HPRT0_PRTENA | \
+							DWC2_HPRT0_PRTENCHNG | \
+							DWC2_HPRT0_PRTOVRCURRCHNG)
  #define DWC2_HAINT_CH0					(1 << 0)
  #define DWC2_HAINT_CH0_OFFSET				0
  #define DWC2_HAINT_CH1					(1 << 1)

+CC ST

Is there an actual bug this is solving ? Details please ?

#2 lore raw

-----Original Message-----
From: Marek Vasut <[email protected]>
Sent: Wednesday, 21 June, 2023 5:38 AM
To: Chong, Teik Heng <[email protected]>; [email protected]
Cc: Jagan Teki <[email protected]>; Vignesh R <[email protected]>; Simon <[email protected]>; Kris <[email protected]>; Chee, Tien Fong <[email protected]>; Hea, Kok Kiang <[email protected]>; Lokanathan, Raaj <[email protected]>; Maniyam, Dinesh <[email protected]>; Ng, Boon Khai <[email protected]>; Yuslaimi, Alif Zakuan <[email protected]>; Zamri, Muhammad Hazim Izzat <[email protected]>; Lim, Jit Loon <[email protected]>; Tang, Sieu Mun <[email protected]>; Patrice CHOTARD <[email protected]>; Patrick DELAUNAY <[email protected]>
Subject: Re: [PATCH v2 1/1] usb: dwc2: Fix the write to W1C fields in HPRT register

Quoted 106 lines · On 6/20/23 15:52, [email protected] wrote:

From: Teik Heng Chong <[email protected]>

Fix the write to the HPRT register which treat W1C fields as if they were mere RW. This leads to unintended clearing of such fields

---

V1->V2
- update subject tags to usb:dwc2

---
  drivers/usb/host/dwc2.c | 34 ++++++++--------------------------
  drivers/usb/host/dwc2.h |  4 ++++
  2 files changed, 12 insertions(+), 26 deletions(-)
drivers/usb/host/dwc2.c index renamed from drivers/usb/host/dwc2.c +0 -0

23060fc369..9818f9be94 100644

drivers/usb/host/dwc2.c +1 -3
@@ -315,9 +315,7 @@ static void dwc_otg_core_host_init(struct udevice 
 *dev,
 
  	/* Turn on the vbus power. */
  	if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
-		hprt0 = readl(&regs->hprt0);
-		hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
-		hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
+		hprt0 = readl(&regs->hprt0) & ~DWC2_HPRT0_W1C_MASK;
  		if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
  			hprt0 |= DWC2_HPRT0_PRTPWR;
  			writel(hprt0, &regs->hprt0);
drivers/usb/host/dwc2.c +11 -19
@@ -748,7 +746,7 @@ static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
  	case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
  		switch (wValue) {
  		case USB_PORT_FEAT_C_CONNECTION:
-			setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, 
+DWC2_HPRT0_PRTCONNDET);
  			break;
  		}
  		break;
@@ -759,21 +757,13 @@ static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
  			break;
 
  		case USB_PORT_FEAT_RESET:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, 
+DWC2_HPRT0_PRTRST);
  			mdelay(50);
-			clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
+			clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | 
+DWC2_HPRT0_PRTRST);
  			break;
 
  		case USB_PORT_FEAT_POWER:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, 
+DWC2_HPRT0_PRTRST);
  			break;
 
  		case USB_PORT_FEAT_ENABLE:
@@ -1213,14 +1203,9 @@ static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
  		dwc_otg_core_host_init(dev, regs);
  	}
 
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, 
+DWC2_HPRT0_PRTRST);
  	mdelay(50);
-	clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
-		     DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
-		     DWC2_HPRT0_PRTRST);
+	clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
 
  	for (i = 0; i < MAX_DEVICE; i++) {
  		for (j = 0; j < MAX_ENDPOINT; j++) { @@ -1246,10 +1231,7 @@ static 

int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
static void dwc2_uninit_common(struct dwc2_core_regs *regs) {
/* Put everything in reset. */

drivers/usb/host/dwc2.c +2 -4
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, 
+DWC2_HPRT0_PRTRST);
  }
 
  #if !CONFIG_IS_ENABLED(DM_USB)
drivers/usb/host/dwc2.h index renamed from drivers/usb/host/dwc2.h +0 -0

a6f562fe60..6f022e33a1 100644

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 552

Quoted 10 lines
drivers/usb/host/dwc2.h +4 -0
@@ -543,6 +543,10 @@ struct dwc2_core_regs {
  #define DWC2_HPRT0_PRTSPD_LOW				(2 << 17)
  #define DWC2_HPRT0_PRTSPD_MASK				(0x3 << 17)
  #define DWC2_HPRT0_PRTSPD_OFFSET			17
+#define DWC2_HPRT0_W1C_MASK				(DWC2_HPRT0_PRTCONNDET | \
+							DWC2_HPRT0_PRTENA | \
+							DWC2_HPRT0_PRTENCHNG | \
+							DWC2_HPRT0_PRTOVRCURRCHNG)
  #define DWC2_HAINT_CH0					(1 << 0)
  #define DWC2_HAINT_CH0_OFFSET				0
  #define DWC2_HAINT_CH1					(1 << 1)

+CC ST

Is there an actual bug this is solving ? Details please ?

This bug was found during the testing on Simics model. We read the specification DesignWare Cores USB 2.0 Hi-Speed On-The-Go (OTG) Databook (3.30a) "5.3.4.8 Host Port Control and Status Register (HPRT)" and verified what every write to HPRT is intended to do in U-Boot dw2 driver. Then, we realized HPRT.PrtPwr is cleared by this mistake. Furthermore, we compared U-Boot driver source code and the Linux driver source code (which handles HPRT correctly). In the Linux driver (contrary to U-Boot), HPRT is always read using dwc2_read_hprt0 helper function https://elixir.bootlin.com/linux/latest/source/drivers/usb/dwc2/hcd.h#L462 which clears W1C bits. So after write back those bits are zeroes.

#3 lore raw
Quoted 127 lines · Teik Heng

-----Original Message-----
From: Marek Vasut <[email protected]>
Sent: Wednesday, 21 June, 2023 5:38 AM
To: Chong, Teik Heng <[email protected]>; [email protected]
Cc: Jagan Teki <[email protected]>; Vignesh R <[email protected]>; Simon <[email protected]>; Kris <[email protected]>; Chee, Tien Fong <[email protected]>; Hea, Kok Kiang <[email protected]>; Lokanathan, Raaj <[email protected]>; Maniyam, Dinesh <[email protected]>; Ng, Boon Khai <[email protected]>; Yuslaimi, Alif Zakuan <[email protected]>; Zamri, Muhammad Hazim Izzat <[email protected]>; Lim, Jit Loon <[email protected]>; Tang, Sieu Mun <[email protected]>; Patrice CHOTARD <[email protected]>; Patrick DELAUNAY <[email protected]>
Subject: Re: [PATCH v2 1/1] usb: dwc2: Fix the write to W1C fields in HPRT register

Quoted 106 lines · On 6/20/23 15:52, [email protected] wrote:

From: Teik Heng Chong <[email protected]>

Fix the write to the HPRT register which treat W1C fields as if they were mere RW. This leads to unintended clearing of such fields

---

V1->V2
- update subject tags to usb:dwc2

---
   drivers/usb/host/dwc2.c | 34 ++++++++--------------------------
   drivers/usb/host/dwc2.h |  4 ++++
   2 files changed, 12 insertions(+), 26 deletions(-)
drivers/usb/host/dwc2.c index renamed from drivers/usb/host/dwc2.c +0 -0

23060fc369..9818f9be94 100644

drivers/usb/host/dwc2.c +1 -3
@@ -315,9 +315,7 @@ static void dwc_otg_core_host_init(struct udevice
 *dev,
 
   	/* Turn on the vbus power. */
   	if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
-		hprt0 = readl(&regs->hprt0);
-		hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
-		hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
+		hprt0 = readl(&regs->hprt0) & ~DWC2_HPRT0_W1C_MASK;
   		if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
   			hprt0 |= DWC2_HPRT0_PRTPWR;
   			writel(hprt0, &regs->hprt0);
drivers/usb/host/dwc2.c +11 -19
@@ -748,7 +746,7 @@ static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
   	case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
   		switch (wValue) {
   		case USB_PORT_FEAT_C_CONNECTION:
-			setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTCONNDET);
   			break;
   		}
   		break;
@@ -759,21 +757,13 @@ static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
   			break;
 
   		case USB_PORT_FEAT_RESET:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTRST);
   			mdelay(50);
-			clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
+			clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK |
+DWC2_HPRT0_PRTRST);
   			break;
 
   		case USB_PORT_FEAT_POWER:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTRST);
   			break;
 
   		case USB_PORT_FEAT_ENABLE:
@@ -1213,14 +1203,9 @@ static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
   		dwc_otg_core_host_init(dev, regs);
   	}
 
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTRST);
   	mdelay(50);
-	clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
-		     DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
-		     DWC2_HPRT0_PRTRST);
+	clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
 
   	for (i = 0; i < MAX_DEVICE; i++) {
   		for (j = 0; j < MAX_ENDPOINT; j++) { @@ -1246,10 +1231,7 @@ static

int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
static void dwc2_uninit_common(struct dwc2_core_regs *regs)

   {
   	/* Put everything in reset. */
drivers/usb/host/dwc2.c +2 -4
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTRST);
   }
 
   #if !CONFIG_IS_ENABLED(DM_USB)
drivers/usb/host/dwc2.h index renamed from drivers/usb/host/dwc2.h +0 -0

a6f562fe60..6f022e33a1 100644

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 552

Quoted 10 lines
drivers/usb/host/dwc2.h +4 -0
@@ -543,6 +543,10 @@ struct dwc2_core_regs {
   #define DWC2_HPRT0_PRTSPD_LOW				(2 << 17)
   #define DWC2_HPRT0_PRTSPD_MASK				(0x3 << 17)
   #define DWC2_HPRT0_PRTSPD_OFFSET			17
+#define DWC2_HPRT0_W1C_MASK				(DWC2_HPRT0_PRTCONNDET | \
+							DWC2_HPRT0_PRTENA | \
+							DWC2_HPRT0_PRTENCHNG | \
+							DWC2_HPRT0_PRTOVRCURRCHNG)
   #define DWC2_HAINT_CH0					(1 << 0)
   #define DWC2_HAINT_CH0_OFFSET				0
   #define DWC2_HAINT_CH1					(1 << 1)

+CC ST

Quoted 4 lines

Is there an actual bug this is solving ? Details please ?

This bug was found during the testing on Simics model. We read the specification DesignWare Cores USB 2.0 Hi-Speed On-The-Go (OTG) Databook (3.30a) "5.3.4.8 Host Port Control and Status Register (HPRT)" and verified what every write to HPRT is intended to do in U-Boot dw2 driver. Then, we realized HPRT.PrtPwr is cleared by this mistake. Furthermore, we compared U-Boot driver source code and the Linux driver source code (which handles HPRT correctly). In the Linux driver (contrary to U-Boot), HPRT is always read using dwc2_read_hprt0 helper function https://elixir.bootlin.com/linux/latest/source/drivers/usb/dwc2/hcd.h#L462 which clears W1C bits. So after write back those bits are zeroes.

Please do add this to the commit message, I'll pick V3 if ST has no objection.

#4 lore raw
Quoted 193 lines

-----Original Message-----
From: Marek Vasut <[email protected]>
Sent: Wednesday, 21 June, 2023 9:20 AM
To: Chong, Teik Heng <[email protected]>; [email protected]
Cc: Jagan Teki <[email protected]>; Vignesh R <[email protected]>; Simon <[email protected]>; Kris <[email protected]>; Chee, Tien Fong <[email protected]>; Hea, Kok Kiang <[email protected]>; Lokanathan, Raaj <[email protected]>; Maniyam, Dinesh <[email protected]>; Ng, Boon Khai <[email protected]>; Yuslaimi, Alif Zakuan <[email protected]>; Zamri, Muhammad Hazim Izzat <[email protected]>; Lim, Jit Loon <[email protected]>; Tang, Sieu Mun <[email protected]>; Patrice CHOTARD <[email protected]>; Patrick DELAUNAY <[email protected]>
Subject: Re: [PATCH v2 1/1] usb: dwc2: Fix the write to W1C fields in HPRT register

Quoted 4 lines · Teik Heng

-----Original Message-----
From: Marek Vasut <[email protected]>
Sent: Wednesday, 21 June, 2023 5:38 AM
To: Chong, Teik Heng <[email protected]>; [email protected]

Quoted 8 lines

Cc: Jagan Teki <[email protected]>; Vignesh R <[email protected]>; Simon <[email protected]>; Kris <[email protected]>; Chee, Tien Fong <[email protected]>; Hea, Kok Kiang <[email protected]>; Lokanathan, Raaj <[email protected]>; Maniyam, Dinesh <[email protected]>; Ng, Boon Khai <[email protected]>; Yuslaimi, Alif Zakuan <[email protected]>; Zamri, Muhammad Hazim Izzat <[email protected]>; Lim,

Jit

Quoted 31 lines

Loon <[email protected]>; Tang, Sieu Mun <[email protected]>; Patrice CHOTARD <[email protected]>; Patrick DELAUNAY <[email protected]>
Subject: Re: [PATCH v2 1/1] usb: dwc2: Fix the write to W1C fields in HPRT register

Quoted 23 lines · On 6/20/23 15:52, [email protected] wrote:

From: Teik Heng Chong <[email protected]>

Fix the write to the HPRT register which treat W1C fields as if they were mere RW. This leads to unintended clearing of such fields

---

V1->V2
- update subject tags to usb:dwc2

---
   drivers/usb/host/dwc2.c | 34 ++++++++--------------------------
   drivers/usb/host/dwc2.h |  4 ++++
   2 files changed, 12 insertions(+), 26 deletions(-)
drivers/usb/host/dwc2.c index renamed from drivers/usb/host/dwc2.c +0 -0

23060fc369..9818f9be94 100644

drivers/usb/host/dwc2.c +0 -0
@@ -315,9 +315,7 @@ static void dwc_otg_core_host_init(struct udevice
 *dev,
 
Quoted 4 lines

drivers/usb/host/dwc2.c static void dwc_otg_core_host_init(struct udevice *dev,

Quoted 4 lines
drivers/usb/host/dwc2.c +0 -2
   	/* Turn on the vbus power. */
   	if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
-		hprt0 = readl(&regs->hprt0);
-		hprt0 &= ~(DWC2_HPRT0_PRTENA |

DWC2_HPRT0_PRTCONNDET);

Quoted 1 line

drivers/usb/host/dwc2.c static void dwc_otg_core_host_init(struct udevice · line 321

Quoted 1 line
drivers/usb/host/dwc2.c +0 -1
-		hprt0 &= ~(DWC2_HPRT0_PRTENCHNG |

DWC2_HPRT0_PRTOVRCURRCHNG);

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -0
+		hprt0 = readl(&regs->hprt0) & ~DWC2_HPRT0_W1C_MASK;
   		if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
   			hprt0 |= DWC2_HPRT0_PRTPWR;
   			writel(hprt0, &regs->hprt0);
drivers/usb/host/dwc2.c +0 -0
@@ -748,7 +746,7 @@ static int dwc_otg_submit_rh_msg_out(struct

dwc2_priv *priv,

Quoted 1 line

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct · line 746

Quoted 1 line
drivers/usb/host/dwc2.c +0 -0
   	case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER |

USB_TYPE_CLASS:

Quoted 3 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 3 lines
drivers/usb/host/dwc2.c +0 -1
   		switch (wValue) {
   		case USB_PORT_FEAT_C_CONNECTION:
-			setbits_le32(&regs->hprt0,

DWC2_HPRT0_PRTCONNDET);

Quoted 1 line

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct · line 749

Quoted 1 line
drivers/usb/host/dwc2.c +1 -0
+			clrsetbits_le32(&regs->hprt0,

DWC2_HPRT0_W1C_MASK,

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -0
+DWC2_HPRT0_PRTCONNDET);
   			break;
   		}
   		break;
@@ -759,21 +757,13 @@ static int dwc_otg_submit_rh_msg_out(struct

dwc2_priv *priv,

Quoted 4 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 4 lines
drivers/usb/host/dwc2.c +0 -1
   			break;
 
   		case USB_PORT_FEAT_RESET:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA

|

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -4
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0,

DWC2_HPRT0_W1C_MASK,

Quoted 4 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 4 lines
drivers/usb/host/dwc2.c +2 -1
+DWC2_HPRT0_PRTRST);
   			mdelay(50);
-			clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
+			clrbits_le32(&regs->hprt0,

DWC2_HPRT0_W1C_MASK |

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -1
+DWC2_HPRT0_PRTRST);
   			break;
 
   		case USB_PORT_FEAT_POWER:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA

|

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -4
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0,

DWC2_HPRT0_W1C_MASK,

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc2_init_common(struct udevice

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -0
+DWC2_HPRT0_PRTRST);
   			break;
 
   		case USB_PORT_FEAT_ENABLE:
@@ -1213,14 +1203,9 @@ static int dwc2_init_common(struct udevice

*dev, struct dwc2_priv *priv)

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)

Quoted 5 lines
drivers/usb/host/dwc2.c +0 -2
   		dwc_otg_core_host_init(dev, regs);
   	}
 
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET |

DWC2_HPRT0_PRTENCHNG |

Quoted 6 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 6 lines
drivers/usb/host/dwc2.c +2 -3
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTRST);
   	mdelay(50);
-	clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |

DWC2_HPRT0_PRTCONNDET |

Quoted 1 line

drivers/usb/host/dwc2.c static int dwc2_init_common(struct udevice · line 1222

Quoted 1 line
drivers/usb/host/dwc2.c +0 -1
-		     DWC2_HPRT0_PRTENCHNG |

DWC2_HPRT0_PRTOVRCURRCHNG |

Quoted 6 lines

drivers/usb/host/dwc2.c static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)

Quoted 6 lines
drivers/usb/host/dwc2.c +2 -1
-		     DWC2_HPRT0_PRTRST);
+	clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK |
+DWC2_HPRT0_PRTRST);
 
   	for (i = 0; i < MAX_DEVICE; i++) {
   		for (j = 0; j < MAX_ENDPOINT; j++) { @@ -1246,10 +1231,7

@@

Quoted 6 lines

drivers/usb/host/dwc2.c

Quoted 6 lines

static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
static void dwc2_uninit_common(struct dwc2_core_regs *regs)

   {
   	/* Put everything in reset. */
drivers/usb/host/dwc2.c +0 -2
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET |

DWC2_HPRT0_PRTENCHNG |

Quoted 6 lines
Quoted 6 lines
drivers/usb/host/dwc2.c +2 -2
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTRST);
   }
Quoted 7 lines

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 543

Quoted 7 lines
   #if !CONFIG_IS_ENABLED(DM_USB)
drivers/usb/host/dwc2.h index renamed from drivers/usb/host/dwc2.h +0 -0

a6f562fe60..6f022e33a1 100644

drivers/usb/host/dwc2.h +0 -0
@@ -543,6 +543,10 @@ struct dwc2_core_regs {
   #define DWC2_HPRT0_PRTSPD_LOW				(2 <<

17)

Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 544

Quoted 1 line
drivers/usb/host/dwc2.h +0 -0
   #define DWC2_HPRT0_PRTSPD_MASK				(0x3

<< 17)

Quoted 2 lines

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 546

Quoted 2 lines
drivers/usb/host/dwc2.h +1 -0
   #define DWC2_HPRT0_PRTSPD_OFFSET			17
+#define DWC2_HPRT0_W1C_MASK
	(DWC2_HPRT0_PRTCONNDET | \
Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 547

Quoted 1 line
drivers/usb/host/dwc2.h +1 -0
+
	DWC2_HPRT0_PRTENA | \
Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 548

Quoted 1 line
drivers/usb/host/dwc2.h +1 -0
+
	DWC2_HPRT0_PRTENCHNG | \
Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 549

Quoted 1 line
drivers/usb/host/dwc2.h +1 -0
+

DWC2_HPRT0_PRTOVRCURRCHNG)

Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 550

Quoted 1 line
drivers/usb/host/dwc2.h +0 -0
   #define DWC2_HAINT_CH0					(1 <<

0)

Quoted 2 lines

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 552

Quoted 2 lines
drivers/usb/host/dwc2.h +0 -0
   #define DWC2_HAINT_CH0_OFFSET				0
   #define DWC2_HAINT_CH1					(1 <<

1)

Quoted 6 lines

+CC ST

Is there an actual bug this is solving ? Details please ?

This bug was found during the testing on Simics model. We read the

specification DesignWare Cores USB 2.0 Hi-Speed On-The-Go (OTG) Databook (3.30a) "5.3.4.8 Host Port Control and Status Register (HPRT)" and verified what every write to HPRT is intended to do in U-Boot dw2 driver. Then, we realized HPRT.PrtPwr is cleared by this mistake.

Quoted 7 lines
Quoted 1 line

Furthermore, we compared U-Boot driver source code and the Linux driver

source code (which handles HPRT correctly). In the Linux driver (contrary to U-Boot), HPRT is always read using dwc2_read_hprt0 helper function
https://elixir.bootlin.com/linux/latest/source/drivers/usb/dwc2/hcd.h#L462
which clears W1C bits. So after write back those bits are zeroes.

Please do add this to the commit message, I'll pick V3 if ST has no objection.

Ok I will add this.

#5 lore raw
Quoted 194 lines · Teik Heng
Quoted 194 lines

-----Original Message-----
From: Marek Vasut <[email protected]>
Sent: Wednesday, 21 June, 2023 9:20 AM
To: Chong, Teik Heng <[email protected]>; [email protected]
Cc: Jagan Teki <[email protected]>; Vignesh R <[email protected]>; Simon <[email protected]>; Kris <[email protected]>; Chee, Tien Fong <[email protected]>; Hea, Kok Kiang <[email protected]>; Lokanathan, Raaj <[email protected]>; Maniyam, Dinesh <[email protected]>; Ng, Boon Khai <[email protected]>; Yuslaimi, Alif Zakuan <[email protected]>; Zamri, Muhammad Hazim Izzat <[email protected]>; Lim, Jit Loon <[email protected]>; Tang, Sieu Mun <[email protected]>; Patrice CHOTARD <[email protected]>; Patrick DELAUNAY <[email protected]>
Subject: Re: [PATCH v2 1/1] usb: dwc2: Fix the write to W1C fields in HPRT register

Quoted 4 lines · Teik Heng

-----Original Message-----
From: Marek Vasut <[email protected]>
Sent: Wednesday, 21 June, 2023 5:38 AM
To: Chong, Teik Heng <[email protected]>; [email protected]

Quoted 8 lines

Cc: Jagan Teki <[email protected]>; Vignesh R <[email protected]>; Simon <[email protected]>; Kris <[email protected]>; Chee, Tien Fong <[email protected]>; Hea, Kok Kiang <[email protected]>; Lokanathan, Raaj <[email protected]>; Maniyam, Dinesh <[email protected]>; Ng, Boon Khai <[email protected]>; Yuslaimi, Alif Zakuan <[email protected]>; Zamri, Muhammad Hazim Izzat <[email protected]>; Lim,

Jit

Quoted 31 lines

Loon <[email protected]>; Tang, Sieu Mun <[email protected]>; Patrice CHOTARD <[email protected]>; Patrick DELAUNAY <[email protected]>
Subject: Re: [PATCH v2 1/1] usb: dwc2: Fix the write to W1C fields in HPRT register

Quoted 23 lines · On 6/20/23 15:52, [email protected] wrote:

From: Teik Heng Chong <[email protected]>

Fix the write to the HPRT register which treat W1C fields as if they were mere RW. This leads to unintended clearing of such fields

---

V1->V2
- update subject tags to usb:dwc2

---
    drivers/usb/host/dwc2.c | 34 ++++++++--------------------------
    drivers/usb/host/dwc2.h |  4 ++++
    2 files changed, 12 insertions(+), 26 deletions(-)
drivers/usb/host/dwc2.c index renamed from drivers/usb/host/dwc2.c +0 -0

23060fc369..9818f9be94 100644

drivers/usb/host/dwc2.c +0 -0
@@ -315,9 +315,7 @@ static void dwc_otg_core_host_init(struct udevice
 *dev,
 
Quoted 4 lines

drivers/usb/host/dwc2.c static void dwc_otg_core_host_init(struct udevice *dev,

Quoted 4 lines
drivers/usb/host/dwc2.c +0 -2
    	/* Turn on the vbus power. */
    	if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
-		hprt0 = readl(&regs->hprt0);
-		hprt0 &= ~(DWC2_HPRT0_PRTENA |

DWC2_HPRT0_PRTCONNDET);

Quoted 1 line

drivers/usb/host/dwc2.c static void dwc_otg_core_host_init(struct udevice · line 321

Quoted 1 line
drivers/usb/host/dwc2.c +0 -1
-		hprt0 &= ~(DWC2_HPRT0_PRTENCHNG |

DWC2_HPRT0_PRTOVRCURRCHNG);

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -0
+		hprt0 = readl(&regs->hprt0) & ~DWC2_HPRT0_W1C_MASK;
    		if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
    			hprt0 |= DWC2_HPRT0_PRTPWR;
    			writel(hprt0, &regs->hprt0);
drivers/usb/host/dwc2.c +0 -0
@@ -748,7 +746,7 @@ static int dwc_otg_submit_rh_msg_out(struct

dwc2_priv *priv,

Quoted 1 line

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct · line 746

Quoted 1 line
drivers/usb/host/dwc2.c +0 -0
    	case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER |

USB_TYPE_CLASS:

Quoted 3 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 3 lines
drivers/usb/host/dwc2.c +0 -1
    		switch (wValue) {
    		case USB_PORT_FEAT_C_CONNECTION:
-			setbits_le32(&regs->hprt0,

DWC2_HPRT0_PRTCONNDET);

Quoted 1 line

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct · line 749

Quoted 1 line
drivers/usb/host/dwc2.c +1 -0
+			clrsetbits_le32(&regs->hprt0,

DWC2_HPRT0_W1C_MASK,

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -0
+DWC2_HPRT0_PRTCONNDET);
    			break;
    		}
    		break;
@@ -759,21 +757,13 @@ static int dwc_otg_submit_rh_msg_out(struct

dwc2_priv *priv,

Quoted 4 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 4 lines
drivers/usb/host/dwc2.c +0 -1
    			break;
 
    		case USB_PORT_FEAT_RESET:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA

|

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -4
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0,

DWC2_HPRT0_W1C_MASK,

Quoted 4 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 4 lines
drivers/usb/host/dwc2.c +2 -1
+DWC2_HPRT0_PRTRST);
    			mdelay(50);
-			clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
+			clrbits_le32(&regs->hprt0,

DWC2_HPRT0_W1C_MASK |

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -1
+DWC2_HPRT0_PRTRST);
    			break;
 
    		case USB_PORT_FEAT_POWER:
-			clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA

|

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -4
-					DWC2_HPRT0_PRTCONNDET |
-					DWC2_HPRT0_PRTENCHNG |
-					DWC2_HPRT0_PRTOVRCURRCHNG,
-					DWC2_HPRT0_PRTRST);
+			clrsetbits_le32(&regs->hprt0,

DWC2_HPRT0_W1C_MASK,

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc2_init_common(struct udevice

Quoted 5 lines
drivers/usb/host/dwc2.c +1 -0
+DWC2_HPRT0_PRTRST);
    			break;
 
    		case USB_PORT_FEAT_ENABLE:
@@ -1213,14 +1203,9 @@ static int dwc2_init_common(struct udevice

*dev, struct dwc2_priv *priv)

Quoted 5 lines

drivers/usb/host/dwc2.c static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)

Quoted 5 lines
drivers/usb/host/dwc2.c +0 -2
    		dwc_otg_core_host_init(dev, regs);
    	}
 
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET |

DWC2_HPRT0_PRTENCHNG |

Quoted 6 lines

drivers/usb/host/dwc2.c static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,

Quoted 6 lines
drivers/usb/host/dwc2.c +2 -3
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTRST);
    	mdelay(50);
-	clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |

DWC2_HPRT0_PRTCONNDET |

Quoted 1 line

drivers/usb/host/dwc2.c static int dwc2_init_common(struct udevice · line 1222

Quoted 1 line
drivers/usb/host/dwc2.c +0 -1
-		     DWC2_HPRT0_PRTENCHNG |

DWC2_HPRT0_PRTOVRCURRCHNG |

Quoted 6 lines

drivers/usb/host/dwc2.c static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)

Quoted 6 lines
drivers/usb/host/dwc2.c +2 -1
-		     DWC2_HPRT0_PRTRST);
+	clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK |
+DWC2_HPRT0_PRTRST);
 
    	for (i = 0; i < MAX_DEVICE; i++) {
    		for (j = 0; j < MAX_ENDPOINT; j++) { @@ -1246,10 +1231,7

@@

Quoted 6 lines

drivers/usb/host/dwc2.c

Quoted 6 lines

static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)

    static void dwc2_uninit_common(struct dwc2_core_regs *regs)
    {
    	/* Put everything in reset. */
drivers/usb/host/dwc2.c +0 -2
-	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
-			DWC2_HPRT0_PRTCONNDET |

DWC2_HPRT0_PRTENCHNG |

Quoted 6 lines
Quoted 6 lines
drivers/usb/host/dwc2.c +2 -2
-			DWC2_HPRT0_PRTOVRCURRCHNG,
-			DWC2_HPRT0_PRTRST);
+	clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK,
+DWC2_HPRT0_PRTRST);
    }
Quoted 7 lines

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 543

Quoted 7 lines
    #if !CONFIG_IS_ENABLED(DM_USB)
drivers/usb/host/dwc2.h index renamed from drivers/usb/host/dwc2.h +0 -0

a6f562fe60..6f022e33a1 100644

drivers/usb/host/dwc2.h +0 -0
@@ -543,6 +543,10 @@ struct dwc2_core_regs {
    #define DWC2_HPRT0_PRTSPD_LOW				(2 <<

17)

Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 544

Quoted 1 line
drivers/usb/host/dwc2.h +0 -0
    #define DWC2_HPRT0_PRTSPD_MASK				(0x3

<< 17)

Quoted 2 lines

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 546

Quoted 2 lines
drivers/usb/host/dwc2.h +1 -0
    #define DWC2_HPRT0_PRTSPD_OFFSET			17
+#define DWC2_HPRT0_W1C_MASK
	(DWC2_HPRT0_PRTCONNDET | \
Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 547

Quoted 1 line
drivers/usb/host/dwc2.h +1 -0
+
	DWC2_HPRT0_PRTENA | \
Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 548

Quoted 1 line
drivers/usb/host/dwc2.h +1 -0
+
	DWC2_HPRT0_PRTENCHNG | \
Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 549

Quoted 1 line
drivers/usb/host/dwc2.h +1 -0
+

DWC2_HPRT0_PRTOVRCURRCHNG)

Quoted 1 line

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 550

Quoted 1 line
drivers/usb/host/dwc2.h +0 -0
    #define DWC2_HAINT_CH0					(1 <<

0)

Quoted 2 lines

drivers/usb/host/dwc2.h struct dwc2_core_regs { · line 552

Quoted 2 lines
drivers/usb/host/dwc2.h +0 -0
    #define DWC2_HAINT_CH0_OFFSET				0
    #define DWC2_HAINT_CH1					(1 <<

1)

Quoted 6 lines

+CC ST

Is there an actual bug this is solving ? Details please ?

This bug was found during the testing on Simics model. We read the

specification DesignWare Cores USB 2.0 Hi-Speed On-The-Go (OTG) Databook (3.30a) "5.3.4.8 Host Port Control and Status Register (HPRT)" and verified what every write to HPRT is intended to do in U-Boot dw2 driver. Then, we realized HPRT.PrtPwr is cleared by this mistake.

Quoted 1 line

Furthermore, we compared U-Boot driver source code and the Linux driver

Quoted 8 lines
Quoted 6 lines

source code (which handles HPRT correctly). In the Linux driver (contrary to U-Boot), HPRT is always read using dwc2_read_hprt0 helper function
https://elixir.bootlin.com/linux/latest/source/drivers/usb/dwc2/hcd.h#L462
which clears W1C bits. So after write back those bits are zeroes.

Please do add this to the commit message, I'll pick V3 if ST has no objection.

Ok I will add this.

Thanks