Skip to contentSkip to conversation

Xilinx ZynqMP USB fixes

PATCH Superseded Robert Hancock 2 participants 7 messages Jan 7, 2022 – Jan 10, 2022

Subsystems (4)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover Xilinx ZynqMP USB fixes

Some fixes related to the DWC3 USB driver and Xilinx ZynqMP DWC3 wrapper driver to allow ZynqMP USB to work properly when the hardware is configured in USB 2.0-only mode.

Robert Hancock (4):
usb: dwc3: xilinx: Fix PIPE clock selection for USB2.0 mode
usb: dwc3: xilinx: Fix error handling when getting USB3 PHY
usb: dwc3: add reference clock FLADJ configuration
arm64: dts: zynqmp: Add DWC3 USB reference clock period configuration

 arch/arm64/boot/dts/xilinx/zynqmp.dtsi |  4 ++++
 drivers/usb/dwc3/core.c                | 32 ++++++++++++++++++++++++++
 drivers/usb/dwc3/core.h                |  3 +++
 drivers/usb/dwc3/dwc3-xilinx.c         | 17 ++++++++------
 4 files changed, 49 insertions(+), 7 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/4 usb: dwc3: xilinx: Fix PIPE clock selection for USB2.0 mode

It appears that the PIPE clock should not be selected when only USB 2.0 is being used in the design and no USB 3.0 reference clock is used. Fix to set the correct value depending on whether a USB3 PHY is present.

Fixes 84770f028fab ("usb: dwc3: Add driver for Xilinx platforms")
---
 drivers/usb/dwc3/dwc3-xilinx.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/dwc3/dwc3-xilinx.c +5 -2
index 9cc3ad701a29..3bc035376394 100644
@@ -167,8 +167,11 @@ static int dwc3_xlnx_init_zynqmp(struct dwc3_xlnx *priv_data)
 	/* Set PIPE Power Present signal in FPD Power Present Register*/
 	writel(FPD_POWER_PRSNT_OPTION, priv_data->regs + XLNX_USB_FPD_POWER_PRSNT);
 
-	/* Set the PIPE Clock Select bit in FPD PIPE Clock register */
-	writel(PIPE_CLK_SELECT, priv_data->regs + XLNX_USB_FPD_PIPE_CLK);
+	/* Set the PIPE Clock Select bit in FPD PIPE Clock register if a USB3
+	 * PHY is in use, deselect otherwise
+	 */
+	writel(usb3_phy ? PIPE_CLK_SELECT : PIPE_CLK_DESELECT,
+	       priv_data->regs + XLNX_USB_FPD_PIPE_CLK);
 
 	ret = reset_control_deassert(crst);
 	if (ret < 0) {
Signature · 1 line
2.31.1
#2 lore raw

PATCH 2/4 usb: dwc3: xilinx: Fix error handling when getting USB3 PHY

In reply to Robert Hancock

The code that looked up the USB3 PHY was ignoring all errors other than EPROBE_DEFER in an attempt to handle the PHY not being present. Fix and simplify the code by using devm_phy_optional_get and dev_err_probe so that a missing PHY is not treated as an error and unexpected errors are handled properly.

Fixes 84770f028fab ("usb: dwc3: Add driver for Xilinx platforms")
---
 drivers/usb/dwc3/dwc3-xilinx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/usb/dwc3/dwc3-xilinx.c +5 -5
index 3bc035376394..3b16e7610009 100644
@@ -102,12 +102,12 @@ static int dwc3_xlnx_init_zynqmp(struct dwc3_xlnx *priv_data)
 	int			ret;
 	u32			reg;
 
-	usb3_phy = devm_phy_get(dev, "usb3-phy");
-	if (PTR_ERR(usb3_phy) == -EPROBE_DEFER) {
-		ret = -EPROBE_DEFER;
+	usb3_phy = devm_phy_optional_get(dev, "usb3-phy");
+	if (IS_ERR(usb3_phy)) {
+		ret = PTR_ERR(usb3_phy);
+		dev_err_probe(dev, ret,
+			      "failed to get USB3 PHY\n");
 		goto err;
-	} else if (IS_ERR(usb3_phy)) {
-		usb3_phy = NULL;
 	}
 
 	crst = devm_reset_control_get_exclusive(dev, "usb_crst");
Signature · 1 line
2.31.1
#3 lore raw

PATCH 3/4 usb: dwc3: add reference clock FLADJ configuration

In reply to Robert Hancock

Previously a device tree property was added to allow overriding the reference clock period parameter if the default value used was incorrect. However, there is another register field, GFLADJ_REFCLK_FLADJ, which reflects the fractional nanosecond portion of the reference clock period. Add a snps,ref-clock-fladj property to allow configuring this as well.

On the Xilinx ZynqMP platform, the reference clock appears to always be 20 MHz, giving a clock period of 50 ns. However, the default value of GFLADJ_REFCLK_FLADJ was 1008 rather than 0 as it should have been, which prevented many USB devices from functioning properly. The psu_init code run by the Xilinx first-stage boot loader sets this value to 0, however when the controller is reset by the dwc3-xilinx layer, the incorrect default value is restored. This configuration property allows ensuring that the correct value is always used.

---
 drivers/usb/dwc3/core.c | 32 ++++++++++++++++++++++++++++++++
 drivers/usb/dwc3/core.h |  3 +++
 2 files changed, 35 insertions(+)
drivers/usb/dwc3/core.c +32 -0
index f4c09951b517..6289fbcbad45 100644
@@ -359,6 +359,34 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc)
 }
 
 
+/*
+ * dwc3_ref_clk_fladj - Reference clock period (fractional portion) configuration
+ * GFLADJ_REFCLK_FLADJ contains the fractional portion of the reference clock
+ * period set in GUCTL_REFCLKPER.
+ * Calculated as: ((125000/ref_clk_period_integer)-(125000/ref_clk_period)) * ref_clk_period
+ * This value can be specified in the device tree if the default value is incorrect.
+ * Note that 0 is a valid value.
+ *
+ * @dwc3: Pointer to our controller context structure
+ */
+static void dwc3_ref_clk_fladj(struct dwc3 *dwc)
+{
+	u32 reg, reg_new;
+
+	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
+		return;
+
+	if (!dwc->ref_clk_fladj_set)
+		return;
+
+	reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
+	reg_new = reg & ~DWC3_GFLADJ_REFCLK_FLADJ_MASK;
+	reg_new |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, dwc->ref_clk_fladj);
+	if (reg_new != reg)
+		dwc3_writel(dwc->regs, DWC3_GFLADJ, reg_new);
+}
+
+
 /**
  * dwc3_free_one_event_buffer - Frees one event buffer
  * @dwc: Pointer to our controller context structure
@@ -1033,6 +1061,7 @@ static int dwc3_core_init(struct dwc3 *dwc)
 
 	/* Adjust Reference Clock Period */
 	dwc3_ref_clk_period(dwc);
+	dwc3_ref_clk_fladj(dwc);
 
 	dwc3_set_incr_burst_type(dwc);
 
@@ -1418,6 +1447,9 @@ static void dwc3_get_properties(struct dwc3 *dwc)
 				 &dwc->fladj);
 	device_property_read_u32(dev, "snps,ref-clock-period-ns",
 				 &dwc->ref_clk_per);
+	if (!device_property_read_u32(dev, "snps,ref-clock-fladj",
+				      &dwc->ref_clk_fladj))
+		dwc->ref_clk_fladj_set = true;
 
 	dwc->dis_metastability_quirk = device_property_read_bool(dev,
 				"snps,dis_metastability_quirk");
drivers/usb/dwc3/core.h +3 -0
index e1cc3f7398fb..650d4c2e7a67 100644
@@ -388,6 +388,7 @@
 /* Global Frame Length Adjustment Register */
 #define DWC3_GFLADJ_30MHZ_SDBND_SEL		BIT(7)
 #define DWC3_GFLADJ_30MHZ_MASK			0x3f
+#define DWC3_GFLADJ_REFCLK_FLADJ_MASK		0x3fff00
 
 /* Global User Control Register*/
 #define DWC3_GUCTL_REFCLKPER_MASK		0xffc00000
@@ -1166,6 +1167,8 @@ struct dwc3 {
 
 	u32			fladj;
 	u32			ref_clk_per;
+	bool			ref_clk_fladj_set;
+	u32			ref_clk_fladj;
 	u32			irq_gadget;
 	u32			otg_irq;
 	u32			current_otg_role;
Signature · 1 line
2.31.1
maintainer
#4 lore raw

Re: [PATCH 3/4] usb: dwc3: add reference clock FLADJ configuration

Quoted 27 lines · Robert Hancock

Previously a device tree property was added to allow overriding the reference clock period parameter if the default value used was incorrect. However, there is another register field, GFLADJ_REFCLK_FLADJ, which reflects the fractional nanosecond portion of the reference clock period. Add a snps,ref-clock-fladj property to allow configuring this as well.

On the Xilinx ZynqMP platform, the reference clock appears to always be 20 MHz, giving a clock period of 50 ns. However, the default value of GFLADJ_REFCLK_FLADJ was 1008 rather than 0 as it should have been, which prevented many USB devices from functioning properly. The psu_init code run by the Xilinx first-stage boot loader sets this value to 0, however when the controller is reset by the dwc3-xilinx layer, the incorrect default value is restored. This configuration property allows ensuring that the correct value is always used.

---
 drivers/usb/dwc3/core.c | 32 ++++++++++++++++++++++++++++++++
 drivers/usb/dwc3/core.h |  3 +++
 2 files changed, 35 insertions(+)
drivers/usb/dwc3/core.c +0 -0
index f4c09951b517..6289fbcbad45 100644

drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 362

Quoted 4 lines
drivers/usb/dwc3/core.c +1 -0
@@ -359,6 +359,34 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc)
 }
 
 
+/*

Use kernel doc style "/**"

drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 365

Quoted 3 lines
drivers/usb/dwc3/core.c +3 -0
+ * dwc3_ref_clk_fladj - Reference clock period (fractional portion) configuration
+ * GFLADJ_REFCLK_FLADJ contains the fractional portion of the reference clock
+ * period set in GUCTL_REFCLKPER.

It's not a direct "fractional portion" the way it's described here. It may cause some confusion. Let's use the word adjustment to account for the fractional portion that's calculated as below.

drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 366

Quoted 1 line
drivers/usb/dwc3/core.c +1 -0
+ * Calculated as: ((125000/ref_clk_period_integer)-(125000/ref_clk_period)) * ref_clk_period

Note that ref_clk_period_integer is the value in GUCTL.REFCLKPER, and the "ref_clk_period" is the period with fractional value.

drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 374

Quoted 8 lines
drivers/usb/dwc3/core.c +8 -0
+ * This value can be specified in the device tree if the default value is incorrect.
+ * Note that 0 is a valid value.
+ *
+ * @dwc3: Pointer to our controller context structure
+ */
+static void dwc3_ref_clk_fladj(struct dwc3 *dwc)
+{
+	u32 reg, reg_new;

I believe Felipe prefers to declare them in separate lines. Let's keep it consistent as how we do it in for this driver.

Quoted 27 lines
drivers/usb/dwc3/core.c +16 -0
+
+	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
+		return;
+
+	if (!dwc->ref_clk_fladj_set)
+		return;
+
+	reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
+	reg_new = reg & ~DWC3_GFLADJ_REFCLK_FLADJ_MASK;
+	reg_new |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, dwc->ref_clk_fladj);
+	if (reg_new != reg)
+		dwc3_writel(dwc->regs, DWC3_GFLADJ, reg_new);
+}
+
+
 /**
  * dwc3_free_one_event_buffer - Frees one event buffer
  * @dwc: Pointer to our controller context structure
@@ -1033,6 +1061,7 @@ static int dwc3_core_init(struct dwc3 *dwc)
 
 	/* Adjust Reference Clock Period */
 	dwc3_ref_clk_period(dwc);
+	dwc3_ref_clk_fladj(dwc);
 
 	dwc3_set_incr_burst_type(dwc);
 

drivers/usb/dwc3/core.c static void dwc3_get_properties(struct dwc3 *dwc) · line 1452

Quoted 6 lines
drivers/usb/dwc3/core.c +3 -0
@@ -1418,6 +1447,9 @@ static void dwc3_get_properties(struct dwc3 *dwc)
 				 &dwc->fladj);
 	device_property_read_u32(dev, "snps,ref-clock-period-ns",
 				 &dwc->ref_clk_per);
+	if (!device_property_read_u32(dev, "snps,ref-clock-fladj",
+				      &dwc->ref_clk_fladj))
+		dwc->ref_clk_fladj_set = true;

Please document it in the dwc3 DT file whenever we introduce a new property.

Also, do we need to add a new dwc->ref_clk_fladj_set? Can we just define some default value for dwc->ref_clk_fladj as unspecified and have the driver check against that (e.g. #define DWC3_REFCLK_FLADJ_UNSPECIFIED 0xffffffff).

Thanks,
Thinh

Quoted 24 lines
drivers/usb/dwc3/core.c +0 -0
 	dwc->dis_metastability_quirk = device_property_read_bool(dev,
 				"snps,dis_metastability_quirk");
drivers/usb/dwc3/core.h +3 -0
index e1cc3f7398fb..650d4c2e7a67 100644
@@ -388,6 +388,7 @@
 /* Global Frame Length Adjustment Register */
 #define DWC3_GFLADJ_30MHZ_SDBND_SEL		BIT(7)
 #define DWC3_GFLADJ_30MHZ_MASK			0x3f
+#define DWC3_GFLADJ_REFCLK_FLADJ_MASK		0x3fff00
 
 /* Global User Control Register*/
 #define DWC3_GUCTL_REFCLKPER_MASK		0xffc00000
@@ -1166,6 +1167,8 @@ struct dwc3 {
 
 	u32			fladj;
 	u32			ref_clk_per;
+	bool			ref_clk_fladj_set;
+	u32			ref_clk_fladj;
 	u32			irq_gadget;
 	u32			otg_irq;
 	u32			current_otg_role;
#5 lore raw

Re: [PATCH 3/4] usb: dwc3: add reference clock FLADJ configuration

Quoted 31 lines · Thinh Nguyen
Quoted 30 lines · Robert Hancock

Previously a device tree property was added to allow overriding the reference clock period parameter if the default value used was incorrect. However, there is another register field, GFLADJ_REFCLK_FLADJ, which reflects the fractional nanosecond portion of the reference clock period. Add a snps,ref-clock-fladj property to allow configuring this as well.

On the Xilinx ZynqMP platform, the reference clock appears to always be 20 MHz, giving a clock period of 50 ns. However, the default value of GFLADJ_REFCLK_FLADJ was 1008 rather than 0 as it should have been, which prevented many USB devices from functioning properly. The psu_init code run by the Xilinx first-stage boot loader sets this value to 0, however when the controller is reset by the dwc3-xilinx layer, the incorrect default value is restored. This configuration property allows ensuring that the correct value is always used.

---
 drivers/usb/dwc3/core.c | 32 ++++++++++++++++++++++++++++++++
 drivers/usb/dwc3/core.h |  3 +++
 2 files changed, 35 insertions(+)
drivers/usb/dwc3/core.c +0 -0
index f4c09951b517..6289fbcbad45 100644
@@ -359,6 +359,34 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc)
 }
 
 
Quoted 3 lines

drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 362

Quoted 1 line
drivers/usb/dwc3/core.c +1 -0
+/*

Use kernel doc style "/**"

Will update in v2.

Quoted 11 lines
Quoted 6 lines
drivers/usb/dwc3/core.c +1 -0
+ * dwc3_ref_clk_fladj - Reference clock period (fractional portion)

configuration
+ * GFLADJ_REFCLK_FLADJ contains the fractional portion of the reference clock
+ * period set in GUCTL_REFCLKPER.

It's not a direct "fractional portion" the way it's described here. It may cause some confusion. Let's use the word adjustment to account for the fractional portion that's calculated as below.

Quoted 5 lines
Quoted 2 lines

+ * Calculated as: ((125000/ref_clk_period_integer)- (125000/ref_clk_period)) * ref_clk_period

Note that ref_clk_period_integer is the value in GUCTL.REFCLKPER, and the "ref_clk_period" is the period with fractional value.

Rewording this for v2.

Quoted 13 lines

drivers/usb/dwc3/core.c static void dwc3_ref_clk_period(struct dwc3 *dwc) · line 374

Quoted 10 lines

+ * This value can be specified in the device tree if the default value is incorrect.
+ * Note that 0 is a valid value.
+ *
+ * @dwc3: Pointer to our controller context structure
+ */

drivers/usb/dwc3/core.c +3 -0
+static void dwc3_ref_clk_fladj(struct dwc3 *dwc)
+{
+	u32 reg, reg_new;

I believe Felipe prefers to declare them in separate lines. Let's keep it consistent as how we do it in for this driver.

Will do.

Quoted 28 lines
Quoted 28 lines
drivers/usb/dwc3/core.c +10 -0
+
+	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
+		return;
+
+	if (!dwc->ref_clk_fladj_set)
+		return;
+
+	reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
+	reg_new = reg & ~DWC3_GFLADJ_REFCLK_FLADJ_MASK;
+	reg_new |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, dwc-
Quoted 1 line

ref_clk_fladj);

drivers/usb/dwc3/core.c +6 -0
+	if (reg_new != reg)
+		dwc3_writel(dwc->regs, DWC3_GFLADJ, reg_new);
+}
+
+
 /**
  * dwc3_free_one_event_buffer - Frees one event buffer
  * @dwc: Pointer to our controller context structure
@@ -1033,6 +1061,7 @@ static int dwc3_core_init(struct dwc3 *dwc)
 
 	/* Adjust Reference Clock Period */
 	dwc3_ref_clk_period(dwc);
+	dwc3_ref_clk_fladj(dwc);
 
 	dwc3_set_incr_burst_type(dwc);
 
Quoted 9 lines

drivers/usb/dwc3/core.c static void dwc3_get_properties(struct dwc3 *dwc) · line 1452

Quoted 7 lines
drivers/usb/dwc3/core.c +3 -0
@@ -1418,6 +1447,9 @@ static void dwc3_get_properties(struct dwc3 *dwc)
 				 &dwc->fladj);
 	device_property_read_u32(dev, "snps,ref-clock-period-ns",
 				 &dwc->ref_clk_per);
+	if (!device_property_read_u32(dev, "snps,ref-clock-fladj",
+				      &dwc->ref_clk_fladj))
+		dwc->ref_clk_fladj_set = true;

Please document it in the dwc3 DT file whenever we introduce a new property.

Whoops, forgot that. Will add as a separate patch in v2.

Quoted 5 lines

Also, do we need to add a new dwc->ref_clk_fladj_set? Can we just define some default value for dwc->ref_clk_fladj as unspecified and have the driver check against that (e.g. #define DWC3_REFCLK_FLADJ_UNSPECIFIED 0xffffffff).

I figured this was the most explicit/cleanest way to do it that would work with the default zero initialization of the structure. Most of these other settings seem to use 0 as the "don't change anything" value but in this case we do sometimes need to override the value with 0.

Quoted 28 lines

Thanks,
Thinh

Quoted 24 lines
drivers/usb/dwc3/core.c +0 -0
 	dwc->dis_metastability_quirk = device_property_read_bool(dev,
 				"snps,dis_metastability_quirk");
drivers/usb/dwc3/core.h +3 -0
index e1cc3f7398fb..650d4c2e7a67 100644
@@ -388,6 +388,7 @@
 /* Global Frame Length Adjustment Register */
 #define DWC3_GFLADJ_30MHZ_SDBND_SEL		BIT(7)
 #define DWC3_GFLADJ_30MHZ_MASK			0x3f
+#define DWC3_GFLADJ_REFCLK_FLADJ_MASK		0x3fff00
 
 /* Global User Control Register*/
 #define DWC3_GUCTL_REFCLKPER_MASK		0xffc00000
@@ -1166,6 +1167,8 @@ struct dwc3 {
 
 	u32			fladj;
 	u32			ref_clk_per;
+	bool			ref_clk_fladj_set;
+	u32			ref_clk_fladj;
 	u32			irq_gadget;
 	u32			otg_irq;
 	u32			current_otg_role;
#6 lore raw

PATCH 4/4 arm64: dts: zynqmp: Add DWC3 USB reference clock period configuration

In reply to Robert Hancock

Set the reference clock period and FLADJ fields in the DWC3 USB driver to 50ns with no fractional portion to match the ZynqMP configuration.

---
 arch/arm64/boot/dts/xilinx/zynqmp.dtsi | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/boot/dts/xilinx/zynqmp.dtsi +4 -0
index 74e66443e4ce..2f531707d5d4 100644
@@ -828,6 +828,8 @@ dwc3_0: usb@fe200000 {
 				#stream-id-cells = <1>;
 				iommus = <&smmu 0x860>;
 				snps,quirk-frame-length-adjustment = <0x20>;
+				snps,ref-clock-period-ns = <50>;
+				snps,ref-clock-fladj = <0>;
 				/* dma-coherent; */
 			};
 		};
@@ -855,6 +857,8 @@ dwc3_1: usb@fe300000 {
 				#stream-id-cells = <1>;
 				iommus = <&smmu 0x861>;
 				snps,quirk-frame-length-adjustment = <0x20>;
+				snps,ref-clock-period-ns = <50>;
+				snps,ref-clock-fladj = <0>;
 				/* dma-coherent; */
 			};
 		};
Signature · 1 line
2.31.1