[PATCH v2 13/22] phy: cadence-torrent: rework subnode parsing and lane validation

Benjamin Robin benjamin.robin at bootlin.com
Wed Dec 2 07:08:58 PST 2026


Extract the per-subnode handling from the probe function into a new
tp_add_phy_from_of_subnode() helper that fully sets up a single PHY link:
it reads the various properties and validates the lane mapping. The probe
now just iterates over the device-tree children and calls the helper.

Lane assignment is checked by a new tp_set_used_lanes() helper instead
of the previous whole-device lane count. It validates the requested
(mlane, num_lanes) range against the lanes available on the PHY
(mask_available_lanes) and rejects configurations that overlap with
lanes already claimed by another link (mask_used_lanes).

max_bit_rate moves from struct tp_data to struct tp_link: it is a
per-link attribute (parsed from the cdns,max-bit-rate property), not a
Torrent PHY-wide value.

Signed-off-by: Benjamin Robin <benjamin.robin at bootlin.com>
---
 drivers/phy/cadence/phy-cadence-torrent.c | 363 +++++++++++++++++-------------
 1 file changed, 201 insertions(+), 162 deletions(-)

diff --git a/drivers/phy/cadence/phy-cadence-torrent.c b/drivers/phy/cadence/phy-cadence-torrent.c
index 59b61dd8e54a..69e5323b1d68 100644
--- a/drivers/phy/cadence/phy-cadence-torrent.c
+++ b/drivers/phy/cadence/phy-cadence-torrent.c
@@ -347,13 +347,13 @@ struct tp_link {
 	u8 mlane;
 	u8 num_lanes;
 	u8 max_lanes;
+	u16 max_bit_rate; /* Maximum link bit rate to use (in Mbps) */
 	enum cdns_torrent_ssc_mode ssc_mode;
 };
 
 struct tp_data {
 	void __iomem *base; /* DPTX registers base */
 	void __iomem *sd_base; /* SD0801 registers base */
-	u32 max_bit_rate; /* Maximum link bit rate to use (in Mbps) */
 	u32 dp_pll;
 	u32 protocol_bitmask;
 	struct reset_control *phy_rst;
@@ -363,6 +363,8 @@ struct tp_data {
 	struct clk *clk1;
 	enum cdns_torrent_ref_clk ref_clk_rate;
 	enum cdns_torrent_ref_clk ref_clk1_rate;
+	u8 mask_used_lanes;
+
 	struct tp_link links[MAX_NUM_LANES];
 	int nb_links;
 	int already_configured;
@@ -414,6 +416,7 @@ struct cdns_torrent_vals_table {
 struct cdns_torrent_data {
 	u8 block_offset_shift;
 	u8 reg_offset_shift;
+	u8 mask_available_lanes;
 	struct cdns_torrent_vals_table link_cmn_vals_tbl;
 	struct cdns_torrent_vals_table xcvr_diag_vals_tbl;
 	struct cdns_torrent_vals_table pcs_cmn_vals_tbl;
@@ -1560,20 +1563,13 @@ static void cdns_torrent_dp_common_init(struct tp_data *tp_data,
 	 * Initialize PHY with max supported link rate, without SSC.
 	 */
 	if (tp_data->ref_clk_rate == CLK_19_2_MHZ)
-		cdns_torrent_dp_pma_cmn_vco_cfg_19_2mhz(tp_data,
-							tp_data->max_bit_rate,
-							false);
+		cdns_torrent_dp_pma_cmn_vco_cfg_19_2mhz(tp_data, link->max_bit_rate, false);
 	else if (tp_data->ref_clk_rate == CLK_25_MHZ)
-		cdns_torrent_dp_pma_cmn_vco_cfg_25mhz(tp_data,
-						      tp_data->max_bit_rate,
-						      false);
+		cdns_torrent_dp_pma_cmn_vco_cfg_25mhz(tp_data, link->max_bit_rate, false);
 	else if (tp_data->ref_clk_rate == CLK_100_MHZ)
-		cdns_torrent_dp_pma_cmn_vco_cfg_100mhz(tp_data,
-						       tp_data->max_bit_rate,
-						       false);
+		cdns_torrent_dp_pma_cmn_vco_cfg_100mhz(tp_data, link->max_bit_rate, false);
 
-	cdns_torrent_dp_pma_cmn_rate(tp_data, link, tp_data->max_bit_rate,
-				     link->num_lanes);
+	cdns_torrent_dp_pma_cmn_rate(tp_data, link, link->max_bit_rate, link->num_lanes);
 
 	/* take out of reset */
 	tp_dptx_deassert_phy_reset(tp_data);
@@ -2509,6 +2505,173 @@ static int cdns_torrent_clk(struct tp_data *tp_data)
 	return ret;
 }
 
+static int tp_set_used_lanes(struct tp_data *tp_data, u32 mlane, u32 num_lanes)
+{
+	u8 mask_available_lanes;
+	u8 mask_lanes;
+
+	if (((mlane + num_lanes) > MAX_NUM_LANES) || num_lanes == 0)
+		goto invalid_lanes;
+
+	mask_lanes = GENMASK_U8(mlane + num_lanes - 1, mlane);
+	mask_available_lanes = tp_data->init_data->mask_available_lanes;
+
+	if ((mask_lanes & mask_available_lanes) != mask_lanes)
+		goto invalid_lanes;
+
+	if (mask_lanes & tp_data->mask_used_lanes)
+		goto invalid_lanes;
+
+	tp_data->mask_used_lanes |= mask_lanes;
+	return 0;
+
+invalid_lanes:
+	dev_err(tp_data->dev, "Invalid lane configuration\n");
+	return -EINVAL;
+}
+
+static int tp_add_phy_from_of_subnode(struct tp_data *tp_data, struct device_node *child)
+{
+	enum cdns_torrent_phy_type std_type = TYPE_NONE;
+	u32 ssc_mode = CDNS_SERDES_NO_SSC;
+	u32 num_lanes = DEFAULT_NUM_LANES;
+	struct device *dev = tp_data->dev;
+	struct tp_link *link = NULL;
+	struct reset_control *lnk_rst;
+	u32 phy_type = PHY_NONE;
+	int ret = -EINVAL;
+	struct phy *gphy;
+	u32 mlane = 0;
+
+	/* First check the master lane and the number of lanes */
+	if (of_property_read_u32(child, "reg", &mlane)) {
+		dev_err(dev, "%s: No \"reg\"-property.\n", child->full_name);
+		return -EINVAL;
+	}
+
+	of_property_read_u32(child, "cdns,num-lanes", &num_lanes);
+
+	ret = tp_set_used_lanes(tp_data, mlane, num_lanes);
+	if (ret)
+		return ret;
+
+	/* Then retrieve associated standard */
+	if (of_property_read_u32(child, "cdns,phy-type", &phy_type)) {
+		dev_err(dev, "%s: No \"cdns,phy-type\"-property.\n", child->full_name);
+		return -EINVAL;
+	}
+
+	/* Get SSC mode */
+	of_property_read_u32(child, "cdns,ssc-mode", &ssc_mode);
+
+	switch (phy_type) {
+	case PHY_TYPE_PCIE:
+		std_type = TYPE_PCIE;
+		break;
+	case PHY_TYPE_DP:
+		std_type = TYPE_DP;
+		break;
+	case PHY_TYPE_SGMII:
+		std_type = TYPE_SGMII;
+		break;
+	case PHY_TYPE_QSGMII:
+		std_type = TYPE_QSGMII;
+		break;
+	case PHY_TYPE_USB3:
+		std_type = TYPE_USB;
+		break;
+	case PHY_TYPE_USXGMII:
+		std_type = TYPE_USXGMII;
+		break;
+	case PHY_TYPE_XAUI:
+		std_type = TYPE_XAUI;
+		break;
+	default:
+		dev_err(dev, "Unsupported protocol: %u\n", phy_type);
+		return -EINVAL;
+	}
+
+	if (tp_data->nb_links >= MAX_NUM_LANES)
+		return -EINVAL;
+
+	/* Get the PHY link */
+	link = &tp_data->links[tp_data->nb_links];
+	tp_data->nb_links += 1;
+
+	lnk_rst = of_reset_control_array_get_exclusive(child);
+	if (IS_ERR(lnk_rst)) {
+		dev_err(dev, "%s: failed to get reset\n", child->full_name);
+		return PTR_ERR(lnk_rst);
+	}
+
+	link->ssc_mode = ssc_mode;
+	link->lnk_rst = lnk_rst;
+	link->phy_type = std_type;
+	link->mlane = mlane;
+	link->num_lanes = num_lanes;
+	link->max_lanes = num_lanes;
+
+	gphy = devm_phy_create(dev, child, &cdns_torrent_phy_ops);
+	if (IS_ERR(gphy))
+		return PTR_ERR(gphy);
+
+	if (phy_type == PHY_TYPE_DP) {
+		u32 max_bit_rate = DEFAULT_MAX_BIT_RATE;
+
+		switch (num_lanes) {
+		case 1:
+		case 2:
+		case 4:
+			/* valid number of lanes */
+			break;
+		default:
+			dev_err(dev, "unsupported number of lanes: %u\n", num_lanes);
+			return -EINVAL;
+		}
+
+		of_property_read_u32(child, "cdns,max-bit-rate", &max_bit_rate);
+		if (!tp_dp_is_valid_bit_rate(max_bit_rate)) {
+			dev_err(dev, "unsupported max bit rate: %u Mbps\n", max_bit_rate);
+			return -EINVAL;
+		}
+
+		link->max_bit_rate = max_bit_rate;
+		gphy->attrs.bus_width = num_lanes;
+		gphy->attrs.max_link_rate = max_bit_rate;
+		gphy->attrs.mode = PHY_MODE_DP;
+	}
+
+	link->phy = gphy;
+	phy_set_drvdata(gphy, link);
+	tp_data->protocol_bitmask |= BIT(std_type);
+
+	return 0;
+}
+
+static int tp_dp_reg_base_init(struct platform_device *pdev, struct tp_data *tp_data)
+{
+	bool has_dp = false;
+
+	for (u8 i = 0; i < tp_data->nb_links; ++i) {
+		if (tp_data->links[i].phy_type == TYPE_DP) {
+			has_dp = true;
+			break;
+		}
+	}
+
+	if (has_dp) {
+		/* DPTX registers */
+		void __iomem *base = devm_platform_ioremap_resource(pdev, 1);
+
+		if (IS_ERR(base))
+			return PTR_ERR(base);
+
+		tp_data->base = base;
+	}
+
+	return 0;
+}
+
 static bool tp_is_already_configured(const struct tp_data *tp_data)
 {
 	u16 val = tp_reg_read(&tp_data->reg_phy_pma_common_cdb, PHY_PMA_CMN_CTRL1);
@@ -2521,12 +2684,9 @@ 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;
-	u32 total_num_lanes = 0;
-	u32 phy_type;
+	struct device_node *child = NULL;
+	int ret;
 
 	/* Get init data for this PHY */
 	data = of_device_get_match_data(dev);
@@ -2546,8 +2706,8 @@ static int cdns_torrent_phy_probe(struct platform_device *pdev)
 	if (IS_ERR(tp_data->sd_base))
 		return PTR_ERR(tp_data->sd_base);
 
-	subnodes = of_get_available_child_count(dev->of_node);
-	if (subnodes == 0) {
+	ret = of_get_available_child_count(dev->of_node);
+	if (ret == 0) {
 		dev_err(dev, "No available link subnodes found\n");
 		return -EINVAL;
 	}
@@ -2561,166 +2721,42 @@ static int cdns_torrent_phy_probe(struct platform_device *pdev)
 
 	ret = tp_of_get_reset(tp_data);
 	if (ret)
-		goto clk_cleanup;
+		return ret;
 
 	ret = tp_of_get_clk(tp_data);
 	if (ret)
-		goto clk_cleanup;
+		return ret;
 
 	tp_data->already_configured = tp_is_already_configured(tp_data);
 	if (!tp_data->already_configured) {
 		ret = cdns_torrent_clk(tp_data);
 		if (ret)
-			goto clk_cleanup;
+			return ret;
 
 		/* Enable APB */
 		reset_control_deassert(tp_data->apb_rst);
 	}
 
 	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")))
+		if (!of_node_name_eq(child, "phy"))
 			continue;
 
-		tp_data->links[node].lnk_rst =
-				of_reset_control_array_get_exclusive(child);
-		if (IS_ERR(tp_data->links[node].lnk_rst)) {
-			dev_err(dev, "%s: failed to get reset\n",
-				child->full_name);
-			ret = PTR_ERR(tp_data->links[node].lnk_rst);
+		ret = tp_add_phy_from_of_subnode(tp_data, child);
+		if (ret)
 			goto put_lnk_rst;
-		}
-
-		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);
-			ret = -EINVAL;
-			goto put_child;
-		}
-
-		switch (phy_type) {
-		case PHY_TYPE_PCIE:
-			tp_data->links[node].phy_type = TYPE_PCIE;
-			break;
-		case PHY_TYPE_DP:
-			tp_data->links[node].phy_type = TYPE_DP;
-			break;
-		case PHY_TYPE_SGMII:
-			tp_data->links[node].phy_type = TYPE_SGMII;
-			break;
-		case PHY_TYPE_QSGMII:
-			tp_data->links[node].phy_type = TYPE_QSGMII;
-			break;
-		case PHY_TYPE_USB3:
-			tp_data->links[node].phy_type = TYPE_USB;
-			break;
-		case PHY_TYPE_USXGMII:
-			tp_data->links[node].phy_type = TYPE_USXGMII;
-			break;
-		case PHY_TYPE_XAUI:
-			tp_data->links[node].phy_type = TYPE_XAUI;
-			break;
-		default:
-			dev_err(dev, "Unsupported protocol\n");
-			ret = -EINVAL;
-			goto put_child;
-		}
-
-		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 += num_lanes;
-
-		/* Get SSC mode */
-		tp_data->links[node].ssc_mode = NO_SSC;
-		of_property_read_u32(child, "cdns,ssc-mode",
-				     &tp_data->links[node].ssc_mode);
-
-		gphy = devm_phy_create(dev, child, &cdns_torrent_phy_ops);
-		if (IS_ERR(gphy)) {
-			ret = PTR_ERR(gphy);
-			goto put_child;
-		}
-
-		if (tp_data->links[node].phy_type == TYPE_DP) {
-			switch (tp_data->links[node].num_lanes) {
-			case 1:
-			case 2:
-			case 4:
-			/* valid number of lanes */
-				break;
-			default:
-				dev_err(dev, "unsupported number of lanes: %d\n",
-					tp_data->links[node].num_lanes);
-				ret = -EINVAL;
-				goto put_child;
-			}
-
-			tp_data->max_bit_rate = DEFAULT_MAX_BIT_RATE;
-			of_property_read_u32(child, "cdns,max-bit-rate",
-					     &tp_data->max_bit_rate);
-
-			switch (tp_data->max_bit_rate) {
-			case 1620:
-			case 2160:
-			case 2430:
-			case 2700:
-			case 3240:
-			case 4320:
-			case 5400:
-			case 8100:
-			/* valid bit rate */
-				break;
-			default:
-				dev_err(dev, "unsupported max bit rate: %dMbps\n",
-					tp_data->max_bit_rate);
-				ret = -EINVAL;
-				goto put_child;
-			}
-
-			/* DPTX registers */
-			tp_data->base = devm_platform_ioremap_resource(pdev, 1);
-			if (IS_ERR(tp_data->base)) {
-				ret = PTR_ERR(tp_data->base);
-				goto put_child;
-			}
-
-			dev_dbg(dev, "DP max bit rate %d.%03d Gbps\n",
-				tp_data->max_bit_rate / 1000,
-				tp_data->max_bit_rate % 1000);
-
-			gphy->attrs.bus_width = tp_data->links[node].num_lanes;
-			gphy->attrs.max_link_rate = tp_data->max_bit_rate;
-			gphy->attrs.mode = PHY_MODE_DP;
-		}
-
-		tp_data->links[node].phy = gphy;
-		tp_data->protocol_bitmask |= BIT(tp_data->links[node].phy_type);
-		phy_set_drvdata(gphy, &tp_data->links[node]);
-
-		node++;
 	}
-	tp_data->nb_links = node;
+	child = NULL;
 
-	if (total_num_lanes > MAX_NUM_LANES) {
-		dev_err(dev, "Invalid lane configuration\n");
-		ret = -EINVAL;
+	if (tp_data->nb_links == 0) {
+		ret = -ENODEV;
 		goto put_lnk_rst;
 	}
 
+	ret = tp_dp_reg_base_init(pdev, tp_data);
+	if (ret)
+		goto put_lnk_rst;
+
 	if (tp_data->nb_links > 1 && !tp_data->already_configured) {
 		ret = cdns_torrent_phy_configure_multilink(tp_data);
 		if (ret)
@@ -2738,23 +2774,23 @@ static int cdns_torrent_phy_probe(struct platform_device *pdev)
 	else
 		dev_dbg(dev, "Single link configuration:\n");
 
-	for (i = 0; i < tp_data->nb_links; i++)
+	for (u8 i = 0; i < tp_data->nb_links; i++)
 		dev_dbg(dev, "%s (%d lanes)",
 			cdns_torrent_get_phy_type(tp_data->links[i].phy_type),
 			tp_data->links[i].num_lanes);
 
 	return 0;
 
-put_child:
-	node++;
 put_lnk_rst:
-	for (i = 0; i < node; i++)
+	if (child)
+		of_node_put(child);
+
+	for (u8 i = 0; i < tp_data->nb_links; i++)
 		reset_control_put(tp_data->links[i].lnk_rst);
-	of_node_put(child);
+
 	reset_control_assert(tp_data->apb_rst);
 	clk_disable_unprepare(tp_data->clk1);
 	clk_disable_unprepare(tp_data->clk);
-clk_cleanup:
 	return ret;
 }
 
@@ -4937,6 +4973,7 @@ static const struct cdns_torrent_vals_entry cdns_rx_ln_vals_entries[] = {
 static const struct cdns_torrent_data cdns_map_torrent = {
 	.block_offset_shift = 0x2,
 	.reg_offset_shift = 0x2,
+	.mask_available_lanes = 0x0F,
 	.link_cmn_vals_tbl = {
 		.entries = link_cmn_vals_entries,
 		.num_entries = ARRAY_SIZE(link_cmn_vals_entries),
@@ -5071,6 +5108,7 @@ static const struct cdns_torrent_vals_entry ti_tx_ln_vals_entries[] = {
 static const struct cdns_torrent_data ti_j721e_map_torrent = {
 	.block_offset_shift = 0x0,
 	.reg_offset_shift = 0x1,
+	.mask_available_lanes = 0x0F,
 	.link_cmn_vals_tbl = {
 		.entries = link_cmn_vals_entries,
 		.num_entries = ARRAY_SIZE(link_cmn_vals_entries),
@@ -5399,6 +5437,7 @@ static const struct cdns_torrent_vals_entry ti_j7200_rx_ln_vals_entries[] = {
 static const struct cdns_torrent_data ti_j7200_map_torrent = {
 	.block_offset_shift = 0x0,
 	.reg_offset_shift = 0x1,
+	.mask_available_lanes = 0x0F,
 	.link_cmn_vals_tbl = {
 		.entries = link_cmn_vals_entries,
 		.num_entries = ARRAY_SIZE(link_cmn_vals_entries),

-- 
2.56.1




More information about the linux-arm-kernel mailing list