]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
scsi: ufs: ufs-qcom: Fixup PAM-4 TX L0_L1_L2_L3 adaptation pattern length
authorCan Guo <can.guo@oss.qualcomm.com>
Wed, 25 Mar 2026 15:21:50 +0000 (08:21 -0700)
committerMartin K. Petersen <martin.petersen@oracle.com>
Fri, 27 Mar 2026 21:20:30 +0000 (17:20 -0400)
If HS-G6 Power Mode change handshake is successful and outbound data Lanes
are expected to transmit ADAPT, M-TX Lanes shall be configured as

  if (Adapt Type == REFRESH)
    TX_HS_ADAPT_LENGTH_L0_L1_L2_L3 = PA_PeerRxHsG6AdaptRefreshL0L1L2L3.
  else if (Adapt Type == INITIAL)
    TX_HS_ADAPT_LENGTH_L0_L1_L2_L3 = PA_PeerRxHsG6AdaptInitialL0L1L2L3.

On some platforms, the ADAPT_L0_L1_L2_L3 duration on Host TX Lanes is only
a half of theoretical ADAPT_L0_L1_L2_L3 duration TADAPT_L0_L1_L2_L3 (in
PAM-4 UI) calculated from TX_HS_ADAPT_LENGTH_L0_L1_L2_L3.

For such platforms, the workaround is to double the ADAPT_L0_L1_L2_L3
duration by uplifting TX_HS_ADAPT_LENGTH_L0_L1_L2_L3. UniPro initializes
TX_HS_ADAPT_LENGTH_L0_L1_L2_L3 during HS-G6 Power Mode change handshake, it
would be too late for SW to update TX_HS_ADAPT_LENGTH_L0_L1_L2_L3 post
HS-G6 Power Mode change. Update PA_PeerRxHsG6AdaptRefreshL0L1L2L3 and
PA_PeerRxHsG6AdaptInitialL0L1L2L3 post Link Startup and before HS-G6 Power
Mode change, so that the UniPro would use the updated value during HS-G6
Power Mode change handshake.

Reviewed-by: Bean Huo <beanhuo@micron.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Can Guo <can.guo@oss.qualcomm.com>
Reviewed-by: Peter Wang <peter.wang@mediatek.com>
Link: https://patch.msgid.link/20260325152154.1604082-9-can.guo@oss.qualcomm.com
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/ufs/host/ufs-qcom.c

index cdc769886e8204b0eb1b95cbd2e8fffdeb914476..b94fe93b830e675fbe9d4eab1c3e5b3bb12114c5 100644 (file)
@@ -1069,10 +1069,188 @@ static void ufs_qcom_override_pa_tx_hsg1_sync_len(struct ufs_hba *hba)
                dev_err(hba->dev, "Failed (%d) set PA_TX_HSG1_SYNC_LENGTH\n", err);
 }
 
+/**
+ * ufs_qcom_double_t_adapt_l0l1l2l3 - Create a new adapt that doubles the
+ * adaptation duration TADAPT_L0_L1_L2_L3 derived from the old adapt.
+ *
+ * @old_adapt: Original ADAPT_L0_L1_L2_L3 capability
+ *
+ * ADAPT_length_L0_L1_L2_L3 formula from M-PHY spec:
+ * if (ADAPT_range_L0_L1_L2_L3 == COARSE) {
+ *   ADAPT_length_L0_L1_L2_L3 = [0, 12]
+ *   ADAPT_L0_L1_L2_L3 = 215 x 2^ADAPT_length_L0_L1_L2_L3
+ * } else if (ADAPT_range_L0_L1_L2_L3 == FINE) {
+ *   ADAPT_length_L0_L1_L2_L3 = [0, 127]
+ *   TADAPT_L0_L1_L2_L3 = 215 x (ADAPT_length_L0_L1_L2_L3 + 1)
+ * }
+ *
+ * To double the adaptation duration TADAPT_L0_L1_L2_L3:
+ * 1. If adapt range is COARSE (1'b1), new adapt = old adapt + 1.
+ * 2. If adapt range is FINE (1'b0):
+ *   a) If old adapt length is < 64, (new adapt + 1) = 2 * (old adapt + 1).
+ *   b) If old adapt length is >= 64, set new adapt to 0x88 using COARSE
+ *      range, because new adapt get from equation in a) shall exceed 127.
+ *
+ * Examples:
+ * ADAPT_range_L0_L1_L2_L3 | ADAPT_length_L0_L1_L2_L3 | TADAPT_L0_L1_L2_L3 (PAM-4 UI)
+ *             0                       3                       131072
+ *             0                       7                       262144
+ *             0                       63                      2097152
+ *             0                       64                      2129920
+ *             0                       127                     4194304
+ *             1                       8                       8388608
+ *             1                       9                       16777216
+ *             1                       10                      33554432
+ *             1                       11                      67108864
+ *             1                       12                      134217728
+ *
+ * Return: new adapt.
+ */
+static u32 ufs_qcom_double_t_adapt_l0l1l2l3(u32 old_adapt)
+{
+       u32 adapt_length = old_adapt & ADAPT_LENGTH_MASK;
+       u32 new_adapt;
+
+       if (IS_ADAPT_RANGE_COARSE(old_adapt)) {
+               new_adapt = (adapt_length + 1) | ADAPT_RANGE_BIT;
+       } else {
+               if (adapt_length < 64)
+                       new_adapt = (adapt_length << 1) + 1;
+               else
+                       /*
+                        * 0x88 is the very coarse Adapt value which is two
+                        * times of the largest fine Adapt value (0x7F)
+                        */
+                       new_adapt = 0x88;
+       }
+
+       return new_adapt;
+}
+
+static void ufs_qcom_limit_max_gear(struct ufs_hba *hba,
+                                   enum ufs_hs_gear_tag gear)
+{
+       struct ufs_qcom_host *host = ufshcd_get_variant(hba);
+       struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
+       struct ufs_host_params *host_params = &host->host_params;
+
+       host_params->hs_tx_gear = gear;
+       host_params->hs_rx_gear = gear;
+       pwr_info->gear_tx = gear;
+       pwr_info->gear_rx = gear;
+
+       dev_warn(hba->dev, "Limited max gear of host and device to HS-G%d\n", gear);
+}
+
+static void ufs_qcom_fixup_tx_adapt_l0l1l2l3(struct ufs_hba *hba)
+{
+       struct ufs_qcom_host *host = ufshcd_get_variant(hba);
+       struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
+       struct ufs_host_params *host_params = &host->host_params;
+       u32 old_adapt, new_adapt, actual_adapt;
+       bool limit_speed = false;
+       int err;
+
+       if (host->hw_ver.major != 0x7 || host->hw_ver.minor > 0x1 ||
+           host_params->hs_tx_gear <= UFS_HS_G5 ||
+           pwr_info->gear_tx <= UFS_HS_G5)
+               return;
+
+       err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PEERRXHSG6ADAPTINITIALL0L1L2L3), &old_adapt);
+       if (err)
+               goto out;
+
+       if (old_adapt > ADAPT_L0L1L2L3_LENGTH_MAX) {
+               dev_err(hba->dev, "PA_PeerRxHsG6AdaptInitialL0L1L2L3 value (0x%x) exceeds MAX\n",
+                       old_adapt);
+               err = -ERANGE;
+               goto out;
+       }
+
+       new_adapt = ufs_qcom_double_t_adapt_l0l1l2l3(old_adapt);
+       dev_dbg(hba->dev, "Original PA_PeerRxHsG6AdaptInitialL0L1L2L3 = 0x%x, new value = 0x%x\n",
+               old_adapt, new_adapt);
+
+       /*
+        * 0x8C is the max possible value allowed by UniPro v3.0 spec, some HWs
+        * can accept 0x8D but some cannot.
+        */
+       if (new_adapt <= ADAPT_L0L1L2L3_LENGTH_MAX ||
+           (new_adapt == ADAPT_L0L1L2L3_LENGTH_MAX + 1 && host->hw_ver.minor == 0x1)) {
+               err = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PEERRXHSG6ADAPTINITIALL0L1L2L3),
+                                    new_adapt);
+               if (err)
+                       goto out;
+
+               err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PEERRXHSG6ADAPTINITIALL0L1L2L3),
+                                    &actual_adapt);
+               if (err)
+                       goto out;
+
+               if (actual_adapt != new_adapt) {
+                       limit_speed = true;
+                       dev_warn(hba->dev, "PA_PeerRxHsG6AdaptInitialL0L1L2L3 0x%x, expect 0x%x\n",
+                                actual_adapt, new_adapt);
+               }
+       } else {
+               limit_speed = true;
+               dev_warn(hba->dev, "New PA_PeerRxHsG6AdaptInitialL0L1L2L3 (0x%x) is too large!\n",
+                        new_adapt);
+       }
+
+       err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PEERRXHSG6ADAPTREFRESHL0L1L2L3), &old_adapt);
+       if (err)
+               goto out;
+
+       if (old_adapt > ADAPT_L0L1L2L3_LENGTH_MAX) {
+               dev_err(hba->dev, "PA_PeerRxHsG6AdaptRefreshL0L1L2L3 value (0x%x) exceeds MAX\n",
+                       old_adapt);
+               err = -ERANGE;
+               goto out;
+       }
+
+       new_adapt = ufs_qcom_double_t_adapt_l0l1l2l3(old_adapt);
+       dev_dbg(hba->dev, "Original PA_PeerRxHsG6AdaptRefreshL0L1L2L3 = 0x%x, new value = 0x%x\n",
+               old_adapt, new_adapt);
+
+       /*
+        * 0x8C is the max possible value allowed by UniPro v3.0 spec, some HWs
+        * can accept 0x8D but some cannot.
+        */
+       if (new_adapt <= ADAPT_L0L1L2L3_LENGTH_MAX ||
+           (new_adapt == ADAPT_L0L1L2L3_LENGTH_MAX + 1 && host->hw_ver.minor == 0x1)) {
+               err = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PEERRXHSG6ADAPTREFRESHL0L1L2L3),
+                                    new_adapt);
+               if (err)
+                       goto out;
+
+               err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PEERRXHSG6ADAPTREFRESHL0L1L2L3),
+                                    &actual_adapt);
+               if (err)
+                       goto out;
+
+               if (actual_adapt != new_adapt) {
+                       limit_speed = true;
+                       dev_warn(hba->dev, "PA_PeerRxHsG6AdaptRefreshL0L1L2L3 0x%x, expect 0x%x\n",
+                                new_adapt, actual_adapt);
+               }
+       } else {
+               limit_speed = true;
+               dev_warn(hba->dev, "New PA_PeerRxHsG6AdaptRefreshL0L1L2L3 (0x%x) is too large!\n",
+                        new_adapt);
+       }
+
+out:
+       if (limit_speed || err)
+               ufs_qcom_limit_max_gear(hba, UFS_HS_G5);
+}
+
 static int ufs_qcom_apply_dev_quirks(struct ufs_hba *hba)
 {
        int err = 0;
 
+       ufs_qcom_fixup_tx_adapt_l0l1l2l3(hba);
+
        if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME)
                err = ufs_qcom_quirk_host_pa_saveconfigtime(hba);