The dp->lanes field of struct phy_configure_opts_dp is only valid when
dp->set_lanes is set, but it was used unconditionally when programming
the PLL clock enable, the PCS lane count, the power-state registers and
the lane-reset bits. A rate-only or voltage-only reconfiguration could be
realized based on an invalid lane count.

Split the link lane count into two fields of struct tp_link:

  - max_lanes: the number of lanes available on the link (from
    cdns,num-lanes, defaulted to DEFAULT_NUM_LANES);
  - num_lanes: the number of lanes currently in use.

tp_dp_verify_config() now validates dp->lanes against max_lanes only
when set_lanes is set, and otherwise keeps the current num_lanes.
tp_dp_set_lanes() receives the requested lane count, updates
link->num_lanes once the lanes are programmed.

All outgoing per-lane register values are also shifted by the link
master lane: the PLL enable bits, the PHY_PMA_XCVR_POWER_STATE_REQ lane
fields, the pma_tx_elec_idle mask and the PHY_RESET lane-reset bits are
now relative to link->mlane instead of lane 0. The per-lane loops in
tp_dp_set_lanes() are replaced with single mask compute/apply steps.

Signed-off-by: Benjamin Robin <[email protected]>
---
 drivers/phy/cadence/phy-cadence-torrent.c | 222 ++++++++++++++++--------------
 1 file changed, 115 insertions(+), 107 deletions(-)

diff --git a/drivers/phy/cadence/phy-cadence-torrent.c 
b/drivers/phy/cadence/phy-cadence-torrent.c
index 28627bd23445..6e48c315e6b1 100644
--- a/drivers/phy/cadence/phy-cadence-torrent.c
+++ b/drivers/phy/cadence/phy-cadence-torrent.c
@@ -344,8 +344,9 @@ struct tp_link {
        struct phy *phy;
        struct reset_control *lnk_rst;
        enum cdns_torrent_phy_type phy_type;
-       u32 mlane;
-       u32 num_lanes;
+       u8 mlane;
+       u8 num_lanes;
+       u8 max_lanes;
        enum cdns_torrent_ssc_mode ssc_mode;
 };
 
@@ -610,6 +611,25 @@ static const char *cdns_torrent_get_phy_type(enum 
cdns_torrent_phy_type phy_type
        }
 }
 
+static bool tp_dp_is_valid_bit_rate(u32 bit_rate)
+{
+       switch (bit_rate) {
+       case 1620:
+       case 2430:
+       case 3240:
+       case 2160:
+       case 4320:
+       case 2700:
+       case 5400:
+       case 8100:
+               return true;
+       default:
+               break;
+       }
+
+       return false;
+}
+
 /*
  * Set registers responsible for enabling and configuring SSC, with second and
  * third register values provided by parameters.
@@ -972,30 +992,26 @@ static int cdns_torrent_dp_get_pll(struct tp_data 
*tp_data,
 /*
  * Enable or disable PLL for selected lanes.
  */
-static int tp_dptx_set_pll_en(struct tp_data *tp_data, struct tp_link *link,
-                             struct phy_configure_opts_dp *dp, bool enable)
+static int tp_dptx_set_pll_en(const struct tp_data *tp_data, u32 mlane, u32 
num_lanes,
+                             bool enable)
 {
        void __iomem *addr;
-       u32 rd_val, pll_ack_val;
+       u32 rd_val;
        int ret;
 
-       /*
-        * Used to determine, which bits to check for or enable in
-        * PHY_PMA_XCVR_PLLCLK_EN register.
-        */
-       u32 pll_bits;
+       /* Used to determine which bits to check for or enable in 
PHY_PMA_XCVR_PLLCLK_EN reg */
+       u32 pll_bits = ((1U << num_lanes) - 1) << mlane;
        /* Used to enable or disable lanes. */
        u32 pll_val;
+       u32 pll_ack_val;
 
-       /* Select values of registers and mask, depending on enabled lane 
count. */
+       /* Select reg value and mask, depending on enabled lane count. */
        pll_val = tp_reg_dptx_read(tp_data, PHY_PMA_XCVR_PLLCLK_EN);
 
        if (enable) {
-               pll_bits = ((1 << dp->lanes) - 1);
                pll_val |= pll_bits;
                pll_ack_val = pll_bits;
        } else {
-               pll_bits = ((1 << link->num_lanes) - 1);
                pll_val &= (~pll_bits);
                pll_ack_val = 0;
        }
@@ -1006,39 +1022,42 @@ static int tp_dptx_set_pll_en(struct tp_data *tp_data, 
struct tp_link *link,
        addr = tp_data->base + PHY_PMA_XCVR_PLLCLK_EN_ACK;
        ret = readl_poll_timeout(addr, rd_val, (rd_val & pll_bits) == 
pll_ack_val, 0,
                                 POLL_TIMEOUT_US);
+       if (ret < 0) {
+               dev_err(tp_data->dev, "timeout waiting for link PLL clock 
enable ack\n");
+               return ret;
+       }
+
        ndelay(100);
-       return ret;
+       return 0;
 }
 
-static int tp_dptx_set_power_state(struct tp_data *tp_data, struct tp_link 
*link,
-                                  u32 num_lanes, enum phy_powerstate 
powerstate)
+static int tp_dptx_set_power_state(const struct tp_data *tp_data, u32 mlane, 
u32 num_lanes,
+                                  enum phy_powerstate powerstate)
 {
-       /* Register value for power state for a single byte. */
        void __iomem *addr;
-       u32 value_part, i;
+       u32 value_part;
        u32 value = 0;
        u32 mask = 0;
        u32 read_val;
        int ret;
 
        switch (powerstate) {
-       case (POWERSTATE_A0):
+       case POWERSTATE_A0:
                value_part = 0x01U;
                break;
-       case (POWERSTATE_A2):
+       case POWERSTATE_A2:
                value_part = 0x04U;
                break;
+       case POWERSTATE_A3:
        default:
-               /* Powerstate A3 */
                value_part = 0x08U;
                break;
        }
 
        /* Select values of registers and mask, depending on enabled lane 
count. */
-
-       for (i = 0; i < num_lanes; i++) {
-               value |= (value_part << PHY_POWER_STATE_LN(i));
-               mask |= (PMA_XCVR_POWER_STATE_REQ_LN_MASK << 
PHY_POWER_STATE_LN(i));
+       for (u32 i = 0; i < num_lanes; ++i) {
+               value |= value_part << PHY_POWER_STATE_LN(mlane + i);
+               mask |= PMA_XCVR_POWER_STATE_REQ_LN_MASK << 
PHY_POWER_STATE_LN(mlane + i);
        }
 
        /* Set power state A<n>. */
@@ -1073,7 +1092,7 @@ static int tp_dptx_wait_pma_cmn_ready(const struct 
tp_data *tp_data)
        return 0;
 }
 
-static int tp_dp_run(struct tp_data *tp_data, struct tp_link *link, u32 
num_lanes)
+static int tp_dp_run(struct tp_data *tp_data, struct tp_link *link)
 {
        void __iomem *addr;
        unsigned int read_val;
@@ -1092,11 +1111,11 @@ static int tp_dp_run(struct tp_data *tp_data, struct 
tp_link *link, u32 num_lane
 
        ndelay(100);
 
-       ret = tp_dptx_set_power_state(tp_data, link, num_lanes, POWERSTATE_A2);
+       ret = tp_dptx_set_power_state(tp_data, link->mlane, link->num_lanes, 
POWERSTATE_A2);
        if (ret)
                return ret;
 
-       ret = tp_dptx_set_power_state(tp_data, link, num_lanes, POWERSTATE_A0);
+       ret = tp_dptx_set_power_state(tp_data, link->mlane, link->num_lanes, 
POWERSTATE_A0);
 
        return ret;
 }
@@ -1190,7 +1209,7 @@ static int tp_dp_configure_rate(struct tp_data *tp_data, 
struct tp_link *link,
                /* PMA common configuration 100MHz */
                cdns_torrent_dp_pma_cmn_vco_cfg_100mhz(tp_data, dp->link_rate, 
dp->ssc);
 
-       cdns_torrent_dp_pma_cmn_rate(tp_data, link, dp->link_rate, dp->lanes);
+       cdns_torrent_dp_pma_cmn_rate(tp_data, link, dp->link_rate, 
link->num_lanes);
 
        /* Enable the associated PLL (cmn_pll0_en or cmn_pll1_en) */
        tp_reg_set_bits(reg_base, PHY_PMA_PLL_RAW_CTRL, pll_mask);
@@ -1207,56 +1226,46 @@ static int tp_dp_configure_rate(struct tp_data 
*tp_data, struct tp_link *link,
 /*
  * Verify, that parameters to configure PHY with are correct.
  */
-static int tp_dp_verify_config(struct tp_link *link, struct 
phy_configure_opts_dp *dp)
+static int tp_dp_verify_config(const struct tp_link *link,
+                              const struct phy_configure_opts_dp *dp)
 {
-       u8 i;
+       u32 num_lanes;
 
        /* If changing link rate was required, verify it's supported. */
        if (dp->set_rate) {
-               switch (dp->link_rate) {
-               case 1620:
-               case 2160:
-               case 2430:
-               case 2700:
-               case 3240:
-               case 4320:
-               case 5400:
-               case 8100:
-                       /* valid bit rate */
-                       break;
-               default:
+               if (!tp_dp_is_valid_bit_rate(dp->link_rate))
                        return -EINVAL;
-               }
        }
 
        /* Verify lane count. */
-       switch (dp->lanes) {
-       case 1:
-       case 2:
-       case 4:
-               /* valid lane count. */
-               break;
-       default:
-               return -EINVAL;
+       if (dp->set_lanes) {
+               switch (dp->lanes) {
+               case 1:
+               case 2:
+               case 4:
+                       /* valid lane count. */
+                       break;
+               default:
+                       return -EINVAL;
+               }
+
+               /* Check against actual number of PHY's lanes. */
+               if (dp->lanes > link->max_lanes)
+                       return -EINVAL;
+
+               num_lanes = dp->lanes;
+       } else {
+               num_lanes = link->num_lanes;
        }
 
-       /* Check against actual number of PHY's lanes. */
-       if (dp->lanes > link->num_lanes)
-               return -EINVAL;
-
-       /*
-        * If changing voltages is required, check swing and pre-emphasis
-        * levels, per-lane.
-        */
+       /* If changing voltages is required, check swing and pre-emphasis 
levels, per-lane. */
        if (dp->set_voltages) {
                /* Lane count verified previously. */
-               for (i = 0; i < dp->lanes; i++) {
+               for (u32 i = 0; i < num_lanes; ++i) {
                        if (dp->voltage[i] > 3 || dp->pre[i] > 3)
                                return -EINVAL;
 
-                       /* Sum of voltage swing and pre-emphasis levels cannot
-                        * exceed 3.
-                        */
+                       /* Sum of voltage swing and pre-emphasis levels cannot 
exceed 3. */
                        if (dp->voltage[i] + dp->pre[i] > 3)
                                return -EINVAL;
                }
@@ -1286,58 +1295,53 @@ static void cdns_torrent_dp_set_a0_pll(struct tp_data 
*tp_data,
 }
 
 /* Configure lane count as required. */
-static int tp_dp_set_lanes(struct tp_data *tp_data, struct tp_link *link,
-                          struct phy_configure_opts_dp *dp)
+static int tp_dp_set_lanes(struct tp_data *tp_data, struct tp_link *link, u32 
num_lanes)
 {
-       u32 value, i;
+       u32 pma_tx_elec_idle_mask;
+       u32 used_lane_mask;
+       u32 all_lane_mask;
+       u32 value;
+       u32 mlane = link->mlane;
        int ret;
-       u8 lane_mask = (1 << dp->lanes) - 1;
-       u8 pma_tx_elec_idle_mask = 0;
-       u32 clane = link->mlane;
 
-       lane_mask <<= clane;
+       used_lane_mask = ((1U << num_lanes) - 1) << mlane;
+       all_lane_mask = ((1U << link->max_lanes) - 1) << mlane;
 
        value = tp_reg_dptx_read(tp_data, PHY_RESET);
+
        /* clear pma_tx_elec_idle_ln_* bits. */
-       pma_tx_elec_idle_mask = ((1 << link->num_lanes) - 1) << clane;
-
+       pma_tx_elec_idle_mask = ((1U << link->max_lanes) - 1) << mlane;
        pma_tx_elec_idle_mask <<= PMA_TX_ELEC_IDLE_SHIFT;
 
        value &= ~pma_tx_elec_idle_mask;
 
        /* Assert pma_tx_elec_idle_ln_* for disabled lanes. */
-       value |= ((~lane_mask) << PMA_TX_ELEC_IDLE_SHIFT) &
-                pma_tx_elec_idle_mask;
+       value |= ((~used_lane_mask) << PMA_TX_ELEC_IDLE_SHIFT) & 
pma_tx_elec_idle_mask;
 
        tp_reg_dptx_write(tp_data, PHY_RESET, value);
 
        /* reset the link by asserting master lane phy_l0*_reset_n low */
-       tp_reg_dptx_write(tp_data, PHY_RESET,
-                             value & (~(1 << clane)));
+       value &= ~(1U << mlane);
+       tp_reg_dptx_write(tp_data, PHY_RESET, value);
 
        /*
-        * Assert lane reset on unused lanes and master lane so they remain in 
reset
-        * and powered down when re-enabling the link
+        * Assert lane reset on unused lanes and master lane so they remain in
+        * reset and powered down when re-enabling the link
         */
-       for (i = 0; i < link->num_lanes; i++)
-               value &= (~(1 << (clane + i)));
-
-       for (i = 1; i < link->num_lanes; i++)
-               value |= ((1 << (clane + i)) & lane_mask);
-
+       value &= ~all_lane_mask;
+       value |= used_lane_mask;
+       value &= ~(1U << mlane);
        tp_reg_dptx_write(tp_data, PHY_RESET, value);
 
-       cdns_torrent_dp_set_a0_pll(tp_data, link, dp->lanes);
-
-       /* release phy_l0*_reset_n based on used laneCount */
-       for (i = 0; i < link->num_lanes; i++)
-               value &= (~(1 << (clane + i)));
-
-       for (i = 0; i < link->num_lanes; i++)
-               value |= ((1 << (clane + i)) & lane_mask);
+       cdns_torrent_dp_set_a0_pll(tp_data, link, link->max_lanes);
 
+       /* release master lane reset */
+       value |= 1 << mlane;
        tp_reg_dptx_write(tp_data, PHY_RESET, value);
 
+       /* update the number of used lanes */
+       link->num_lanes = num_lanes;
+
        /* Wait, until PHY gets ready after releasing PHY reset signal. */
        ret = tp_dptx_wait_pma_cmn_ready(tp_data);
        if (ret)
@@ -1347,10 +1351,10 @@ static int tp_dp_set_lanes(struct tp_data *tp_data, 
struct tp_link *link,
 
        /* release pma_xcvr_pllclk_en_ln_*, only for the master lane */
        value = tp_reg_dptx_read(tp_data, PHY_PMA_XCVR_PLLCLK_EN);
-       value |= (1 << clane);
+       value |= 1 << mlane;
        tp_reg_dptx_write(tp_data, PHY_PMA_XCVR_PLLCLK_EN, value);
 
-       ret = tp_dp_run(tp_data, link, dp->lanes);
+       ret = tp_dp_run(tp_data, link);
 
        return ret;
 }
@@ -1361,10 +1365,10 @@ static int tp_dp_set_rate(struct tp_data *tp_data, 
struct tp_link *link,
 {
        int ret;
 
-       ret = tp_dptx_set_power_state(tp_data, link, dp->lanes, POWERSTATE_A3);
+       ret = tp_dptx_set_power_state(tp_data, link->mlane, link->num_lanes, 
POWERSTATE_A3);
        if (ret)
                return ret;
-       ret = tp_dptx_set_pll_en(tp_data, link, dp, false);
+       ret = tp_dptx_set_pll_en(tp_data, link->mlane, link->max_lanes, false);
        if (ret)
                return ret;
        ndelay(200);
@@ -1374,13 +1378,13 @@ static int tp_dp_set_rate(struct tp_data *tp_data, 
struct tp_link *link,
                return ret;
        ndelay(200);
 
-       ret = tp_dptx_set_pll_en(tp_data, link, dp, true);
+       ret = tp_dptx_set_pll_en(tp_data, link->mlane, link->num_lanes, true);
        if (ret)
                return ret;
-       ret = tp_dptx_set_power_state(tp_data, link, dp->lanes, POWERSTATE_A2);
+       ret = tp_dptx_set_power_state(tp_data, link->mlane, link->num_lanes, 
POWERSTATE_A2);
        if (ret)
                return ret;
-       ret = tp_dptx_set_power_state(tp_data, link, dp->lanes, POWERSTATE_A0);
+       ret = tp_dptx_set_power_state(tp_data, link->mlane, link->num_lanes, 
POWERSTATE_A0);
        if (ret)
                return ret;
        ndelay(900);
@@ -1435,7 +1439,7 @@ static int tp_phy_configure(struct phy *phy, union 
phy_configure_opts *opts)
        }
 
        if (opts->dp.set_lanes) {
-               ret = tp_dp_set_lanes(tp_data, link, &opts->dp);
+               ret = tp_dp_set_lanes(tp_data, link, opts->dp.lanes);
                if (ret) {
                        dev_err(&phy->dev, "tp_dp_set_lanes failed\n");
                        return ret;
@@ -1587,7 +1591,7 @@ static int cdns_torrent_dp_start(struct tp_data *tp_data, 
struct tp_link *link,
        if (ret)
                return ret;
 
-       ret = tp_dp_run(tp_data, link, link->num_lanes);
+       ret = tp_dp_run(tp_data, link);
 
        return ret;
 }
@@ -2525,6 +2529,7 @@ static int cdns_torrent_phy_probe(struct platform_device 
*pdev)
        struct tp_data *tp_data;
        struct device *dev = &pdev->dev;
        struct phy_provider *phy_provider;
+       u32 num_lanes = DEFAULT_NUM_LANES;
        const struct cdns_torrent_data *data;
        struct device_node *child;
        int ret, subnodes, node = 0, i;
@@ -2582,6 +2587,7 @@ static int cdns_torrent_phy_probe(struct platform_device 
*pdev)
 
        for_each_available_child_of_node(dev->of_node, child) {
                struct phy *gphy;
+               u32 mlane;
 
                /* PHY subnode name must be 'phy'. */
                if (!(of_node_name_eq(child, "phy")))
@@ -2596,14 +2602,15 @@ static int cdns_torrent_phy_probe(struct 
platform_device *pdev)
                        goto put_lnk_rst;
                }
 
-               if (of_property_read_u32(child, "reg",
-                                        &tp_data->links[node].mlane)) {
+               if (of_property_read_u32(child, "reg", &mlane)) {
                        dev_err(dev, "%s: No \"reg\"-property.\n",
                                child->full_name);
                        ret = -EINVAL;
                        goto put_child;
                }
 
+               tp_data->links[node].mlane = mlane;
+
                if (of_property_read_u32(child, "cdns,phy-type", &phy_type)) {
                        dev_err(dev, "%s: No \"cdns,phy-type\"-property.\n",
                                child->full_name);
@@ -2639,10 +2646,11 @@ static int cdns_torrent_phy_probe(struct 
platform_device *pdev)
                        goto put_child;
                }
 
-               tp_data->links[node].num_lanes = DEFAULT_NUM_LANES;
-               of_property_read_u32(child, "cdns,num-lanes", 
&tp_data->links[node].num_lanes);
+               of_property_read_u32(child, "cdns,num-lanes", &num_lanes);
+               tp_data->links[node].num_lanes = num_lanes;
+               tp_data->links[node].max_lanes = num_lanes;
 
-               total_num_lanes += tp_data->links[node].num_lanes;
+               total_num_lanes += num_lanes;
 
                /* Get SSC mode */
                tp_data->links[node].ssc_mode = NO_SSC;

-- 
2.56.1

Reply via email to