Skip to contentSkip to conversation

phy: Add USB3 PHY support to Google Tensor SoC USB PHY driver

PATCH Superseded RD Babiera 2 participants 3 messages Sep 18 – Sep 25

Subsystems (3)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

RD Babiera

author
#0 lore raw

PATCH v7 1/1 phy: Add USB3 PHY support to Google Tensor SoC USB PHY driver

Add USB3 PHY support for the Google Tensor G5 USB PHY driver. This patch adds functionality for the usb3_tca register, usb3 clock, and usb3 reset as defined in google,lga-usb-phy.yaml. Kconfig now lists USB SuperSpeed support.

Refactor the probe sequence to initialize the USB2 and USB3 PHYs, and then initialize clocks and resets for both PHYs afterwards.

Refactor set_vbus_valid to reduce duplicated code.

Implement USB3 phy_ops for phy_init, phy_exit, and phy_power_on. combo_phy_state enum is added to track PHY bringup state across PHY API calls. google_usb_set_orientation can reprogram the TCA when the PHY is ready.

google_usb_set_orientation now calls pm_runtime_get_if_active to guarantee register access. Setting orientation and pm_runtime management are now handled within phy_mutex.

---
Changes since v1:
* Removed mix of goto-based and scope-based cleanup from usb3 phy_init
* Removed unused usb3_core resource from probe
* Added combo_phy_state enum to interally track ComboPHY bringup state to allow google_usb_set_orientation() to change TCA orientation.
* Modify Kconfig documentation to reflect SuperSpeed support

Changes since v2:
* google_usb3_phy_init now sets USBDP_TOP_CFG_REG_PMGT_REF_CLK_REQ_N to false if phy_init fails elsewhere.
* google_usb3_phy_init errors are now handled via DEFINE_FREE structures. This affects set_pmgt_ref_clk_req_n, clk_bulk_prepare_enable, and reset_control_bulk_deassert.
* google_usb2_phy_init also handles undoing clk_bulk_prepare_enable via DEFINE_FREE structure.
* google_usb3_phy_power_on allows program_tca_locked in the COMBO_PHY_TCA_READY state. Waiting for PoR=>NC is only performed once.
* Note: there are checkpatch errors for the DEFINE_FREE macros resulting in "ERROR: trailing statements should be on next line". Other cases of DEFINE_FREE where the line limit would otherwise exceed 100 columns have the indentation done the same way.

Changes since v3:
* set_pmgt_ref_clk_req_n(false) in google_usb3_phy_exit() comes after reset assertion and clock disable to match phy_init() sequence.
* program_tca_locked in usb3 power_on() now requires a valid orientation to match google_usb_set_orientation requirements.

Changes since v4:
* google_usb_set_orientation sets gphy->orientation while holding phy_mutex.
* google_usb_set_orientation now holds a pm_runtime reference before checking for suspend, and calls put_autosuspend prior to normal exit.

Changes since v5:
* google_usb_set_orientation replaces pm_runtime_suspended check with pm_runtime_get_if_active call.

Changes since v6:
* google_usb_set_orientation uses pm_runtime_put() instead of pm_runtime_put_autosuspend().
* Added newline to all dev_* logs
* xa_ack sw poll changed to 100ms, and xa_timeout_val is programmed to match during usb3 phy init
* google_usb3_phy_power_on returns -EINVAL when PHY is idle and only performs PoR->NC wait in COMBO_PHY_INIT_DONE

---
 drivers/phy/Kconfig          |   2 +-
 drivers/phy/phy-google-usb.c | 434 +++++++++++++++++++++++++++++++----
 2 files changed, 393 insertions(+), 43 deletions(-)
drivers/phy/Kconfig +1 -1
index 19f3b7d12b7d..d2d401129af7 100644
@@ -100,7 +100,7 @@ config PHY_GOOGLE_USB
 	  the G5 generation (Laguna). This driver provides the PHY interfaces
 	  to interact with the SNPS eUSB2 and USB 3.2/DisplayPort Combo PHY,
 	  both of which are integrated with the DWC3 USB DRD controller.
-	  This driver currently supports USB high-speed.
+	  This driver currently supports USB high-speed and SuperSpeed.
 
 config USB_LGM_PHY
 	tristate "INTEL Lightning Mountain USB PHY Driver"
drivers/phy/phy-google-usb.c +392 -42

538 lines not on this page · raw message

base-commit: cee9395acd8043be0644b25c34bfa86623f2b935

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#1 lore raw

Hi RD,

Thanks for sending v7. I've reviewed the changes and identified a few functional issues, and a couple minor items as seen below:

Quoted 1 line · RD Babiera

Add USB3 PHY support for the Google Tensor G5 USB PHY driver.

...

drivers/phy/phy-google-usb.c line 21

Quoted 5 lines
drivers/phy/phy-google-usb.c +0 -0
@@ -20,6 +20,7 @@
 #include <linux/reset.h>
 #include <linux/usb/typec_mux.h>

The driver is now using readl_poll_timeout() and pm_runtime_get_if_active(), we should be including linux/iopoll.h and linux/pm_runtime.h explicitly.

drivers/phy/phy-google-usb.c line 65

Quoted 6 lines
drivers/phy/phy-google-usb.c +6 -0
+#define TCA_CTRLSYNCMODE_CFG1_XA_TIMEOUT_VAL_100MS 0x1e85
+#define TCA_PSTATE_0_OFFSET 0x50
+#define TCA_PSTATE_0_UPCS_LANE0_PHYSTATUS BIT(8)
+
+#define GPHY_TCA_DELAY_US 10
+#define GPHY_TCA_TIMEOUT_US 100000

With the addition of TCA_CTRLSYNCMODE_CFG1_XA_TIMEOUT_VAL_100MS, we should consider bumping GPHY_TCA_TIMEOUT_US to be slightly larger (e.g. 110000us) to ensure the hardware timeout is guaranteed to expire before the software poll timeout.

Quoted 4 lines
drivers/phy/phy-google-usb.c +4 -0
+static const char * const u2phy_clk_names[] = {
+	"usb2",
+	"usb2_apb",
+};

drivers/phy/phy-google-usb.c struct google_usb_phy_instance { · line 103

Quoted 10 lines
drivers/phy/phy-google-usb.c +10 -0
+static const char * const u3phy_clk_names[] = {
+	"usb3"
+};
+static const char * const u2phy_rst_names[] = {
+	"usb2",
+	"usb2_apb",
+};
+static const char * const u3phy_rst_names[] = {
+	"usb3"
+};

nit: checkpatch.pl --strict flags missing blank lines between these array declarations (and the inline helper functions + DEFINE__FREE macros below).

Quoted 30 lines
drivers/phy/phy-google-usb.c +30 -0
+
+static const struct google_usb_phy_config phy_configs[GOOGLE_USB_PHY_NUM] = {
+	[GOOGLE_USB2_PHY] = {
+		.clk_names = u2phy_clk_names,
+		.num_clks = ARRAY_SIZE(u2phy_clk_names),
+		.rst_names = u2phy_rst_names,
+		.num_rsts = ARRAY_SIZE(u2phy_rst_names),
+	},
+	[GOOGLE_USB3_PHY] = {
+		.clk_names = u3phy_clk_names,
+		.num_clks = ARRAY_SIZE(u3phy_clk_names),
+		.rst_names = u3phy_rst_names,
+		.num_rsts = ARRAY_SIZE(u3phy_rst_names),
+	},
+};
+
+static inline void google_usb_phy_clk_disable(struct google_usb_phy_instance *inst)
+{
+	clk_bulk_disable_unprepare(inst->num_clks, inst->clks);
+}
+DEFINE_FREE(inst_clk_disable, struct google_usb_phy_instance *,
+	    if (_T) google_usb_phy_clk_disable(_T))
+
+static inline void google_usb_phy_rst_disable(struct google_usb_phy_instance *inst)
+{
+	reset_control_bulk_assert(inst->num_rsts, inst->rsts);
+}
+DEFINE_FREE(inst_rst_disable, struct google_usb_phy_instance *,
+	    if (_T) google_usb_phy_rst_disable(_T))
+

...

Quoted 22 lines

static int google_usb_set_orientation(struct typec_switch_dev *sw,

drivers/phy/phy-google-usb.c +7 -4
 				      enum typec_orientation orientation)
 {
 	struct google_usb_phy *gphy = typec_switch_get_drvdata(sw);
+	int ret = 0;
 
 	dev_dbg(gphy->dev, "set orientation %d\n", orientation);
 
-	gphy->orientation = orientation;
+	guard(mutex)(&gphy->phy_mutex);
 
-	if (pm_runtime_suspended(gphy->dev))
-		return 0;
+	gphy->orientation = orientation;
 
-	guard(mutex)(&gphy->phy_mutex);
+	if (IS_ENABLED(CONFIG_PM)) {
+		if (pm_runtime_get_if_active(gphy->dev) <= 0)
+			return 0;
+	}
 

drivers/phy/phy-google-usb.c struct google_usb_phy_instance { · line 276

Quoted 10 lines
drivers/phy/phy-google-usb.c +6 -1
 	set_vbus_valid(gphy);
 
-	return 0;
+	if (gphy->phy_state == COMBO_PHY_TCA_READY && orientation != TYPEC_ORIENTATION_NONE)
+		ret = program_tca_locked(gphy);
+
+	pm_runtime_put(gphy->dev);
+
+	return ret;
 }

Previously, sashiko recommended moving to pm_runtime_get_if_active(), which was done in v6. However I think this may have changed the behavior of google_usb_set_orientation() and potentially introduced a regression due to the pre-existing ordering of calls in gooogle_usb_phy_probe(), causing this function to always take the early 'return 0' path.

In google_usb_phy_probe(), we call devm_phy_create() prior to calling pm_runtime_enable(dev).

In drivers/phy/phy-core.c, devm_phy_create() calls phy_create(), which has the following check:

    if (pm_runtime_enabled(dev)) {
        pm_runtime_enable(&phy->dev);
        pm_runtime_no_callbacks(&phy->dev);
    }

Therefore, the phy device never has pm_runtime_enabled, causing this call to pm_runtime_get_if_active() to always return 0, and the function exits prior to calling set_vbus_valid().

I think moving the pm_runtime_enable(dev) call prior to devm_phy_create() will resolve the issue, but we should audit power managment in this driver to verify.

Quoted 12 lines
drivers/phy/phy-google-usb.c +11 -0
+static int google_usb3_phy_init(struct phy *_phy)
+{
+	struct google_usb_phy_instance *inst = phy_get_drvdata(_phy);
+	struct google_usb_phy *gphy = inst->parent;
+	int ret = 0;
+	u32 reg;
+
+	dev_dbg(gphy->dev, "initializing usb3 phy\n");
+
+	guard(mutex)(&gphy->phy_mutex);
+

drivers/phy/phy-google-usb.c static const struct phy_ops google_usb2_phy_ops = { · line 365

Quoted 10 lines
drivers/phy/phy-google-usb.c +10 -0
+	if (gphy->phy_state != COMBO_PHY_IDLE) {
+		dev_warn(gphy->dev, "usb3 phy init called when combo phy state is not idle\n");
+		return 0;
+	}
+
+	reg = readl(gphy->usb3_tca_base + TCA_CTRLSYNCMODE_CFG1_OFFSET);
+	reg &= ~TCA_CTRLSYNCMODE_CFG1_XA_TIMEOUT_VAL;
+	reg |= FIELD_PREP(TCA_CTRLSYNCMODE_CFG1_XA_TIMEOUT_VAL,
+			  TCA_CTRLSYNCMODE_CFG1_XA_TIMEOUT_VAL_100MS);
+	writel(reg, gphy->usb3_tca_base + TCA_CTRLSYNCMODE_CFG1_OFFSET);

I think this introduces a regression between v6 and v7, as usb3_tca_base may be accessed prior to the 'usb3' clock being enabled, and furthermore, the call to reset_control_bulk_deassert() will clear this value.

Therefore, I think we need to this after the call to reset_control_bulk_deassert().

Quoted 40 lines
drivers/phy/phy-google-usb.c +38 -0
+
+	reg = readl(gphy->usbdp_top_base + PHY_POWER_CONFIG_REG1_OFFSET);
+	reg |= PHY_POWER_CONFIG_REG1_PG_MODE_EN;
+	reg &= ~PHY_POWER_CONFIG_REG1_UPCS_PIPE_CONFIG;
+	reg |= FIELD_PREP(PHY_POWER_CONFIG_REG1_UPCS_PIPE_CONFIG,
+			  (UPCS_PIPE_CONFIG_ISO_CPM |
+			   UPCS_PIPE_CONFIG_PG_MODE_STATIC |
+			   UPCS_PIPE_CONFIG_LANE_RESET_NO_PG_EXIT));
+	writel(reg, gphy->usbdp_top_base + PHY_POWER_CONFIG_REG1_OFFSET);
+
+	set_vbus_valid(gphy);
+
+	reg = readl(gphy->usbdp_top_base + USBCS_PHY_CFG1_OFFSET);
+	reg |= USBCS_PHY_CFG1_PHY0_MPLLA_SSC_EN;
+	writel(reg, gphy->usbdp_top_base + USBCS_PHY_CFG1_OFFSET);
+
+	set_sram_bypass(gphy, SRAM_BYPASS_MODE_BYPASS_FIRMWARE |
+			SRAM_BYPASS_MODE_BYPASS_CONTEXT);
+	set_pmgt_ref_clk_req_n(gphy, true);
+	struct google_usb_phy *pmgt_ref_clk_req_dev __free(pmgt_ref_clk_req_n) = gphy;
+
+	ret = clk_bulk_prepare_enable(inst->num_clks, inst->clks);
+	if (ret)
+		return ret;
+	struct google_usb_phy_instance *clk_dev __free(inst_clk_disable) = inst;
+
+	ret = reset_control_bulk_deassert(inst->num_rsts, inst->rsts);
+	if (ret)
+		return ret;
+	struct google_usb_phy_instance *rst_dev __free(inst_rst_disable) = inst;
+
+	ret = readl_poll_timeout(gphy->usb3_tca_base + TCA_PSTATE_0_OFFSET,
+				 reg, !(reg & TCA_PSTATE_0_UPCS_LANE0_PHYSTATUS),
+				 GPHY_TCA_DELAY_US, GPHY_TCA_TIMEOUT_US);
+	if (ret) {
+		dev_err(gphy->dev, "wait for lane0 phystatus timed out\n");
+		return ret;
+	}

drivers/phy/phy-google-usb.c static const struct phy_ops google_usb2_phy_ops = { · line 413

Quoted 10 lines
drivers/phy/phy-google-usb.c +10 -0
+
+	gphy->phy_state = COMBO_PHY_INIT_DONE;
+
+	retain_and_null_ptr(rst_dev);
+	retain_and_null_ptr(clk_dev);
+	retain_and_null_ptr(pmgt_ref_clk_req_dev);
+
+	return 0;
+}
+

Thanks,
Neill

author
#2 lore raw

Hi Neill, thanks for the thorough review. Will send v8 out shortly.

Quoted 3 lines · Neill Kapron

The driver is now using readl_poll_timeout() and pm_runtime_get_if_active(), we should be including linux/iopoll.h and linux/pm_runtime.h explicitly.

Acknowledged.

Quoted 11 lines

drivers/phy/phy-google-usb.c line 65

Quoted 6 lines
drivers/phy/phy-google-usb.c +6 -0
+#define TCA_CTRLSYNCMODE_CFG1_XA_TIMEOUT_VAL_100MS 0x1e85
+#define TCA_PSTATE_0_OFFSET 0x50
+#define TCA_PSTATE_0_UPCS_LANE0_PHYSTATUS BIT(8)
+
+#define GPHY_TCA_DELAY_US 10
+#define GPHY_TCA_TIMEOUT_US 100000

With the addition of TCA_CTRLSYNCMODE_CFG1_XA_TIMEOUT_VAL_100MS, we should consider bumping GPHY_TCA_TIMEOUT_US to be slightly larger (e.g. 110000us) to ensure the hardware timeout is guaranteed to expire before the software poll timeout.

Will change here.

Quoted 10 lines
Quoted 10 lines
drivers/phy/phy-google-usb.c +10 -0
+static const char * const u2phy_clk_names[] = {
+     "usb2",
+     "usb2_apb",
+};
+static const char * const u3phy_clk_names[] = {
+     "usb3"
+};
+static const char * const u2phy_rst_names[] = {
+     "usb2",
+     "usb2_apb",
Quoted 8 lines

drivers/phy/phy-google-usb.c struct google_usb_phy_instance { · line 103

Quoted 4 lines
drivers/phy/phy-google-usb.c +4 -0
+};
+static const char * const u3phy_rst_names[] = {
+     "usb3"
+};

nit: checkpatch.pl --strict flags missing blank lines between these array declarations (and the inline helper functions + DEFINE__FREE macros below).

Will check.

Quoted 49 lines
Quoted 21 lines

static int google_usb_set_orientation(struct typec_switch_dev *sw,
enum typec_orientation orientation)

 {
      struct google_usb_phy *gphy = typec_switch_get_drvdata(sw);
drivers/phy/phy-google-usb.c +7 -4
+     int ret = 0;
 
      dev_dbg(gphy->dev, "set orientation %d\n", orientation);
 
-     gphy->orientation = orientation;
+     guard(mutex)(&gphy->phy_mutex);
 
-     if (pm_runtime_suspended(gphy->dev))
-             return 0;
+     gphy->orientation = orientation;
 
-     guard(mutex)(&gphy->phy_mutex);
+     if (IS_ENABLED(CONFIG_PM)) {
+             if (pm_runtime_get_if_active(gphy->dev) <= 0)
+                     return 0;
+     }
 

drivers/phy/phy-google-usb.c struct google_usb_phy_instance { · line 276

Quoted 10 lines
drivers/phy/phy-google-usb.c +6 -1
      set_vbus_valid(gphy);
 
-     return 0;
+     if (gphy->phy_state == COMBO_PHY_TCA_READY && orientation != TYPEC_ORIENTATION_NONE)
+             ret = program_tca_locked(gphy);
+
+     pm_runtime_put(gphy->dev);
+
+     return ret;
 }

Previously, sashiko recommended moving to pm_runtime_get_if_active(), which was done in v6. However I think this may have changed the behavior of google_usb_set_orientation() and potentially introduced a regression due to the pre-existing ordering of calls in gooogle_usb_phy_probe(), causing this function to always take the early 'return 0' path.

In google_usb_phy_probe(), we call devm_phy_create() prior to calling pm_runtime_enable(dev).

In drivers/phy/phy-core.c, devm_phy_create() calls phy_create(), which has the following check:

    if (pm_runtime_enabled(dev)) {
        pm_runtime_enable(&phy->dev);
        pm_runtime_no_callbacks(&phy->dev);
    }
Quoted 7 lines

Therefore, the phy device never has pm_runtime_enabled, causing this call to pm_runtime_get_if_active() to always return 0, and the function exits prior to calling set_vbus_valid().

I think moving the pm_runtime_enable(dev) call prior to devm_phy_create() will resolve the issue, but we should audit power managment in this driver to verify.

The check here is called on gphy->dev as opposed to phy->dev. fw_devlink uses FW_DEVLINK_FLAGS_RPM by default, so the dwc3 consumer controller holds DL_FLAG_PM_RUNTIME on the phy platform device. Every resume call on the dwc3 controller results in the supplier resuming as well under this model, so the runtime_get() call here passes.

Quoted 7 lines

I think this introduces a regression between v6 and v7, as usb3_tca_base may be accessed prior to the 'usb3' clock being enabled, and furthermore, the call to reset_control_bulk_deassert() will clear this value.

Therefore, I think we need to this after the call to reset_control_bulk_deassert().

usb3_tca_base is powered by a different clock as opposed to the 'usb3' one, so the register access will be safe as long as the platform driver's power domain is on. Register reads in either order both result in the same behavior during testing, but I would argue that writing before the call to reset_control_bulk_deassert() ensures that the parameter is applied on PoR.

Best,
RD