Skip to contentSkip to conversation

stmmac: tegra: Add MGBE support

PATCH RESEND In review Thierry Reding 6 participants 10 messages Sep 23, 2022 – Nov 25, 2022

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND v4 1/1 stmmac: tegra: Add MGBE support

From: Bhadram Varka <[email protected]>

Add support for the Multi-Gigabit Ethernet (MGBE/XPCS) IP found on NVIDIA Tegra234 SoCs.

---
 drivers/net/ethernet/stmicro/stmmac/Kconfig   |   6 +
 drivers/net/ethernet/stmicro/stmmac/Makefile  |   1 +
 .../net/ethernet/stmicro/stmmac/dwmac-tegra.c | 290 ++++++++++++++++++
 3 files changed, 297 insertions(+)
 create mode 100644 drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c
drivers/net/ethernet/stmicro/stmmac/Kconfig +6 -0
index 31ff35174034..e9f61bdaf7c4 100644
@@ -235,6 +235,12 @@ config DWMAC_INTEL_PLAT
 	  the stmmac device driver. This driver is used for the Intel Keem Bay
 	  SoC.
 
+config DWMAC_TEGRA
+	tristate "NVIDIA Tegra MGBE support"
+	depends on ARCH_TEGRA || COMPILE_TEST
+	help
+	  Support for the MGBE controller found on Tegra SoCs.
+
 config DWMAC_VISCONTI
 	tristate "Toshiba Visconti DWMAC support"
 	default ARCH_VISCONTI
drivers/net/ethernet/stmicro/stmmac/Makefile +1 -0
index d4e12e9ace4f..057e4bab5c08 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_DWMAC_DWC_QOS_ETH)	+= dwmac-dwc-qos-eth.o
 obj-$(CONFIG_DWMAC_INTEL_PLAT)	+= dwmac-intel-plat.o
 obj-$(CONFIG_DWMAC_GENERIC)	+= dwmac-generic.o
 obj-$(CONFIG_DWMAC_IMX8)	+= dwmac-imx.o
+obj-$(CONFIG_DWMAC_TEGRA)	+= dwmac-tegra.o
 obj-$(CONFIG_DWMAC_VISCONTI)	+= dwmac-visconti.o
 stmmac-platform-objs:= stmmac_platform.o
 dwmac-altr-socfpga-objs := altr_tse_pcs.o dwmac-socfpga.o
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +290 -0

290 lines not on this page · raw message

Signature · 1 line
2.37.3
maintainer
#1 lore raw
Quoted 111 lines · Thierry Reding

From: Bhadram Varka <[email protected]>

Add support for the Multi-Gigabit Ethernet (MGBE/XPCS) IP found on NVIDIA Tegra234 SoCs.

---
 drivers/net/ethernet/stmicro/stmmac/Kconfig   |   6 +
 drivers/net/ethernet/stmicro/stmmac/Makefile  |   1 +
 .../net/ethernet/stmicro/stmmac/dwmac-tegra.c | 290 ++++++++++++++++++
 3 files changed, 297 insertions(+)
 create mode 100644 drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c
drivers/net/ethernet/stmicro/stmmac/Kconfig +6 -0
index 31ff35174034..e9f61bdaf7c4 100644
@@ -235,6 +235,12 @@ config DWMAC_INTEL_PLAT
 	  the stmmac device driver. This driver is used for the Intel Keem Bay
 	  SoC.
 
+config DWMAC_TEGRA
+	tristate "NVIDIA Tegra MGBE support"
+	depends on ARCH_TEGRA || COMPILE_TEST
+	help
+	  Support for the MGBE controller found on Tegra SoCs.
+
 config DWMAC_VISCONTI
 	tristate "Toshiba Visconti DWMAC support"
 	default ARCH_VISCONTI
drivers/net/ethernet/stmicro/stmmac/Makefile +1 -0
index d4e12e9ace4f..057e4bab5c08 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_DWMAC_DWC_QOS_ETH)	+= dwmac-dwc-qos-eth.o
 obj-$(CONFIG_DWMAC_INTEL_PLAT)	+= dwmac-intel-plat.o
 obj-$(CONFIG_DWMAC_GENERIC)	+= dwmac-generic.o
 obj-$(CONFIG_DWMAC_IMX8)	+= dwmac-imx.o
+obj-$(CONFIG_DWMAC_TEGRA)	+= dwmac-tegra.o
 obj-$(CONFIG_DWMAC_VISCONTI)	+= dwmac-visconti.o
 stmmac-platform-objs:= stmmac_platform.o
 dwmac-altr-socfpga-objs := altr_tse_pcs.o dwmac-socfpga.o
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +62 -0
new file mode 100644
index 000000000000..bb4b540820fa
@@ -0,0 +1,290 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/platform_device.h>
+#include <linux/of_device.h>
+#include <linux/module.h>
+#include <linux/stmmac.h>
+#include <linux/clk.h>
+
+#include "stmmac_platform.h"
+
+static const char *const mgbe_clks[] = {
+	"rx-pcs", "tx", "tx-pcs", "mac-divider", "mac", "mgbe", "ptp-ref", "mac"
+};
+
+struct tegra_mgbe {
+	struct device *dev;
+
+	struct clk_bulk_data *clks;
+
+	struct reset_control *rst_mac;
+	struct reset_control *rst_pcs;
+
+	void __iomem *hv;
+	void __iomem *regs;
+	void __iomem *xpcs;
+
+	struct mii_bus *mii;
+};
+
+#define XPCS_WRAP_UPHY_RX_CONTROL 0x801c
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_SW_OVRD BIT(31)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_PCS_PHY_RDY BIT(10)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_CDR_RESET BIT(9)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_CAL_EN BIT(8)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_SLEEP (BIT(7) | BIT(6))
+#define XPCS_WRAP_UPHY_RX_CONTROL_AUX_RX_IDDQ BIT(5)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_IDDQ BIT(4)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_DATA_EN BIT(0)
+#define XPCS_WRAP_UPHY_HW_INIT_CTRL 0x8020
+#define XPCS_WRAP_UPHY_HW_INIT_CTRL_TX_EN BIT(0)
+#define XPCS_WRAP_UPHY_HW_INIT_CTRL_RX_EN BIT(2)
+#define XPCS_WRAP_UPHY_STATUS 0x8044
+#define XPCS_WRAP_UPHY_STATUS_TX_P_UP BIT(0)
+#define XPCS_WRAP_IRQ_STATUS 0x8050
+#define XPCS_WRAP_IRQ_STATUS_PCS_LINK_STS BIT(6)
+
+#define XPCS_REG_ADDR_SHIFT 10
+#define XPCS_REG_ADDR_MASK 0x1fff
+#define XPCS_ADDR 0x3fc
+
+#define MGBE_WRAP_COMMON_INTR_ENABLE	0x8704
+#define MAC_SBD_INTR			BIT(2)
+#define MGBE_WRAP_AXI_ASID0_CTRL	0x8400
+#define MGBE_SID			0x6
+
+static void mgbe_uphy_lane_bringup(struct tegra_mgbe *mgbe)
+{
+	unsigned int retry = 300;
+	u32 value;
+	int err;
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_STATUS);
+	if ((value & XPCS_WRAP_UPHY_STATUS_TX_P_UP) == 0) {

drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c line 72

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +10 -0
+		value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_HW_INIT_CTRL);
+		value |= XPCS_WRAP_UPHY_HW_INIT_CTRL_TX_EN;
+		writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_HW_INIT_CTRL);
+	}
+
+	err = readl_poll_timeout(mgbe->xpcs + XPCS_WRAP_UPHY_HW_INIT_CTRL, value,
+				 (value & XPCS_WRAP_UPHY_HW_INIT_CTRL_TX_EN) == 0,
+				 500, 500 * 2000);
+	if (err < 0)
+		dev_err(mgbe->dev, "timeout waiting for TX lane to become enabled\n");

Why you don't need to propagate this error to the caller?

Same question for more error cases below.

Quoted 46 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +46 -0
+
+	usleep_range(10000, 20000);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_SW_OVRD;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value &= ~XPCS_WRAP_UPHY_RX_CONTROL_RX_IDDQ;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value &= ~XPCS_WRAP_UPHY_RX_CONTROL_AUX_RX_IDDQ;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value &= ~XPCS_WRAP_UPHY_RX_CONTROL_RX_SLEEP;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_CAL_EN;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	err = readl_poll_timeout(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL, value,
+				 (value & XPCS_WRAP_UPHY_RX_CONTROL_RX_CAL_EN) == 0,
+				 1000, 1000 * 2000);
+	if (err < 0)
+		dev_err(mgbe->dev, "timeout waiting for RX calibration to become enabled\n");
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_DATA_EN;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_CDR_RESET;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value &= ~XPCS_WRAP_UPHY_RX_CONTROL_RX_CDR_RESET;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_PCS_PHY_RDY;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	while (--retry) {

drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c line 128

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +10 -0
+		err = readl_poll_timeout(mgbe->xpcs + XPCS_WRAP_IRQ_STATUS, value,
+					 value & XPCS_WRAP_IRQ_STATUS_PCS_LINK_STS,
+					 500, 500 * 2000);
+		if (err < 0) {
+			dev_err(mgbe->dev, "timeout waiting for link to become ready\n");
+			usleep_range(10000, 20000);
+			continue;
+		}
+		break;
+	}

It looks like the above loop can take up to 150 seconds (300 iterations, 500000usec each), can it be shortned?

Quoted 137 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +137 -0
+
+	/* clear status */
+	writel(value, mgbe->xpcs + XPCS_WRAP_IRQ_STATUS);
+}
+
+static int tegra_mgbe_probe(struct platform_device *pdev)
+{
+	struct plat_stmmacenet_data *plat;
+	struct stmmac_resources res;
+	struct tegra_mgbe *mgbe;
+	int irq, err, i;
+
+	mgbe = devm_kzalloc(&pdev->dev, sizeof(*mgbe), GFP_KERNEL);
+	if (!mgbe)
+		return -ENOMEM;
+
+	mgbe->dev = &pdev->dev;
+
+	memset(&res, 0, sizeof(res));
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
+	mgbe->hv = devm_platform_ioremap_resource_byname(pdev, "hypervisor");
+	if (IS_ERR(mgbe->hv))
+		return PTR_ERR(mgbe->hv);
+
+	mgbe->regs = devm_platform_ioremap_resource_byname(pdev, "mac");
+	if (IS_ERR(mgbe->regs))
+		return PTR_ERR(mgbe->regs);
+
+	mgbe->xpcs = devm_platform_ioremap_resource_byname(pdev, "xpcs");
+	if (IS_ERR(mgbe->xpcs))
+		return PTR_ERR(mgbe->xpcs);
+
+	res.addr = mgbe->regs;
+	res.irq = irq;
+
+	mgbe->clks = devm_kzalloc(&pdev->dev, sizeof(*mgbe->clks), GFP_KERNEL);
+	if (!mgbe->clks)
+		return -ENOMEM;
+
+	for (i = 0; i <  ARRAY_SIZE(mgbe_clks); i++)
+		mgbe->clks[i].id = mgbe_clks[i];
+
+	err = devm_clk_bulk_get(mgbe->dev, ARRAY_SIZE(mgbe_clks), mgbe->clks);
+	if (err < 0)
+		return err;
+
+	err = clk_bulk_prepare_enable(ARRAY_SIZE(mgbe_clks), mgbe->clks);
+	if (err < 0)
+		return err;
+
+	/* Perform MAC reset */
+	mgbe->rst_mac = devm_reset_control_get(&pdev->dev, "mac");
+	if (IS_ERR(mgbe->rst_mac))
+		return PTR_ERR(mgbe->rst_mac);
+
+	err = reset_control_assert(mgbe->rst_mac);
+	if (err < 0)
+		return err;
+
+	usleep_range(2000, 4000);
+
+	err = reset_control_deassert(mgbe->rst_mac);
+	if (err < 0)
+		return err;
+
+	/* Perform PCS reset */
+	mgbe->rst_pcs = devm_reset_control_get(&pdev->dev, "pcs");
+	if (IS_ERR(mgbe->rst_pcs))
+		return PTR_ERR(mgbe->rst_pcs);
+
+	err = reset_control_assert(mgbe->rst_pcs);
+	if (err < 0)
+		return err;
+
+	usleep_range(2000, 4000);
+
+	err = reset_control_deassert(mgbe->rst_pcs);
+	if (err < 0)
+		return err;
+
+	plat = stmmac_probe_config_dt(pdev, res.mac);
+	if (IS_ERR(plat))
+		return PTR_ERR(plat);
+
+	plat->has_xgmac = 1;
+	plat->tso_en = 1;
+	plat->pmt = 1;
+	plat->bsp_priv = mgbe;
+
+	if (!plat->mdio_node)
+		plat->mdio_node = of_get_child_by_name(pdev->dev.of_node, "mdio");
+
+	if (!plat->mdio_bus_data) {
+		plat->mdio_bus_data = devm_kzalloc(&pdev->dev, sizeof(*plat->mdio_bus_data),
+						   GFP_KERNEL);
+		if (!plat->mdio_bus_data) {
+			err = -ENOMEM;
+			goto remove;
+		}
+	}
+
+	plat->mdio_bus_data->needs_reset = true;
+
+	mgbe_uphy_lane_bringup(mgbe);
+
+	/* Tx FIFO Size - 128KB */
+	plat->tx_fifo_size = 131072;
+	/* Rx FIFO Size - 192KB */
+	plat->rx_fifo_size = 196608;
+
+	/* Enable common interrupt at wrapper level */
+	writel(MAC_SBD_INTR, mgbe->regs + MGBE_WRAP_COMMON_INTR_ENABLE);
+
+	/* Program SID */
+	writel(MGBE_SID, mgbe->hv + MGBE_WRAP_AXI_ASID0_CTRL);
+
+	err = stmmac_dvr_probe(&pdev->dev, plat, &res);
+	if (err < 0)
+		goto remove;
+
+	return 0;
+
+remove:
+	stmmac_remove_config_dt(pdev, plat);
+	return err;
+}
+
+static int tegra_mgbe_remove(struct platform_device *pdev)
+{
+	struct tegra_mgbe *mgbe = get_stmmac_bsp_priv(&pdev->dev);
+
+	clk_bulk_disable_unprepare(ARRAY_SIZE(mgbe_clks), mgbe->clks);
+

drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c line 275

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +10 -0
+	stmmac_pltfr_remove(pdev);
+
+	return 0;
+}
+
+static const struct of_device_id tegra_mgbe_match[] = {
+	{ .compatible = "nvidia,tegra234-mgbe", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, tegra_mgbe_match);

The DT bindings will land in 6.1, right?

Thanks!

Paolo

#2 lore raw
Quoted 125 lines · Paolo Abeni
Quoted 111 lines · Thierry Reding

From: Bhadram Varka <[email protected]>

Add support for the Multi-Gigabit Ethernet (MGBE/XPCS) IP found on NVIDIA Tegra234 SoCs.

---
 drivers/net/ethernet/stmicro/stmmac/Kconfig   |   6 +
 drivers/net/ethernet/stmicro/stmmac/Makefile  |   1 +
 .../net/ethernet/stmicro/stmmac/dwmac-tegra.c | 290 ++++++++++++++++++
 3 files changed, 297 insertions(+)
 create mode 100644 drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c
drivers/net/ethernet/stmicro/stmmac/Kconfig +6 -0
index 31ff35174034..e9f61bdaf7c4 100644
@@ -235,6 +235,12 @@ config DWMAC_INTEL_PLAT
 	  the stmmac device driver. This driver is used for the Intel Keem Bay
 	  SoC.
 
+config DWMAC_TEGRA
+	tristate "NVIDIA Tegra MGBE support"
+	depends on ARCH_TEGRA || COMPILE_TEST
+	help
+	  Support for the MGBE controller found on Tegra SoCs.
+
 config DWMAC_VISCONTI
 	tristate "Toshiba Visconti DWMAC support"
 	default ARCH_VISCONTI
drivers/net/ethernet/stmicro/stmmac/Makefile +1 -0
index d4e12e9ace4f..057e4bab5c08 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_DWMAC_DWC_QOS_ETH)	+= dwmac-dwc-qos-eth.o
 obj-$(CONFIG_DWMAC_INTEL_PLAT)	+= dwmac-intel-plat.o
 obj-$(CONFIG_DWMAC_GENERIC)	+= dwmac-generic.o
 obj-$(CONFIG_DWMAC_IMX8)	+= dwmac-imx.o
+obj-$(CONFIG_DWMAC_TEGRA)	+= dwmac-tegra.o
 obj-$(CONFIG_DWMAC_VISCONTI)	+= dwmac-visconti.o
 stmmac-platform-objs:= stmmac_platform.o
 dwmac-altr-socfpga-objs := altr_tse_pcs.o dwmac-socfpga.o
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +62 -0
new file mode 100644
index 000000000000..bb4b540820fa
@@ -0,0 +1,290 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/platform_device.h>
+#include <linux/of_device.h>
+#include <linux/module.h>
+#include <linux/stmmac.h>
+#include <linux/clk.h>
+
+#include "stmmac_platform.h"
+
+static const char *const mgbe_clks[] = {
+	"rx-pcs", "tx", "tx-pcs", "mac-divider", "mac", "mgbe", "ptp-ref", "mac"
+};
+
+struct tegra_mgbe {
+	struct device *dev;
+
+	struct clk_bulk_data *clks;
+
+	struct reset_control *rst_mac;
+	struct reset_control *rst_pcs;
+
+	void __iomem *hv;
+	void __iomem *regs;
+	void __iomem *xpcs;
+
+	struct mii_bus *mii;
+};
+
+#define XPCS_WRAP_UPHY_RX_CONTROL 0x801c
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_SW_OVRD BIT(31)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_PCS_PHY_RDY BIT(10)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_CDR_RESET BIT(9)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_CAL_EN BIT(8)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_SLEEP (BIT(7) | BIT(6))
+#define XPCS_WRAP_UPHY_RX_CONTROL_AUX_RX_IDDQ BIT(5)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_IDDQ BIT(4)
+#define XPCS_WRAP_UPHY_RX_CONTROL_RX_DATA_EN BIT(0)
+#define XPCS_WRAP_UPHY_HW_INIT_CTRL 0x8020
+#define XPCS_WRAP_UPHY_HW_INIT_CTRL_TX_EN BIT(0)
+#define XPCS_WRAP_UPHY_HW_INIT_CTRL_RX_EN BIT(2)
+#define XPCS_WRAP_UPHY_STATUS 0x8044
+#define XPCS_WRAP_UPHY_STATUS_TX_P_UP BIT(0)
+#define XPCS_WRAP_IRQ_STATUS 0x8050
+#define XPCS_WRAP_IRQ_STATUS_PCS_LINK_STS BIT(6)
+
+#define XPCS_REG_ADDR_SHIFT 10
+#define XPCS_REG_ADDR_MASK 0x1fff
+#define XPCS_ADDR 0x3fc
+
+#define MGBE_WRAP_COMMON_INTR_ENABLE	0x8704
+#define MAC_SBD_INTR			BIT(2)
+#define MGBE_WRAP_AXI_ASID0_CTRL	0x8400
+#define MGBE_SID			0x6
+
+static void mgbe_uphy_lane_bringup(struct tegra_mgbe *mgbe)
+{
+	unsigned int retry = 300;
+	u32 value;
+	int err;
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_STATUS);
+	if ((value & XPCS_WRAP_UPHY_STATUS_TX_P_UP) == 0) {

drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c line 72

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +10 -0
+		value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_HW_INIT_CTRL);
+		value |= XPCS_WRAP_UPHY_HW_INIT_CTRL_TX_EN;
+		writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_HW_INIT_CTRL);
+	}
+
+	err = readl_poll_timeout(mgbe->xpcs + XPCS_WRAP_UPHY_HW_INIT_CTRL, value,
+				 (value & XPCS_WRAP_UPHY_HW_INIT_CTRL_TX_EN) == 0,
+				 500, 500 * 2000);
+	if (err < 0)
+		dev_err(mgbe->dev, "timeout waiting for TX lane to become enabled\n");

Why you don't need to propagate this error to the caller?

Quoted 1 line

Same question for more error cases below.

I suspect that we can simply propagate the error in these cases. We never run into these issues in practice, so it went unnoticed.

Quoted 52 lines
Quoted 52 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +51 -0
+
+	usleep_range(10000, 20000);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_SW_OVRD;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value &= ~XPCS_WRAP_UPHY_RX_CONTROL_RX_IDDQ;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value &= ~XPCS_WRAP_UPHY_RX_CONTROL_AUX_RX_IDDQ;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value &= ~XPCS_WRAP_UPHY_RX_CONTROL_RX_SLEEP;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_CAL_EN;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	err = readl_poll_timeout(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL, value,
+				 (value & XPCS_WRAP_UPHY_RX_CONTROL_RX_CAL_EN) == 0,
+				 1000, 1000 * 2000);
+	if (err < 0)
+		dev_err(mgbe->dev, "timeout waiting for RX calibration to become enabled\n");
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_DATA_EN;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_CDR_RESET;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value &= ~XPCS_WRAP_UPHY_RX_CONTROL_RX_CDR_RESET;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	value = readl(mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+	value |= XPCS_WRAP_UPHY_RX_CONTROL_RX_PCS_PHY_RDY;
+	writel(value, mgbe->xpcs + XPCS_WRAP_UPHY_RX_CONTROL);
+
+	while (--retry) {
+		err = readl_poll_timeout(mgbe->xpcs + XPCS_WRAP_IRQ_STATUS, value,
+					 value & XPCS_WRAP_IRQ_STATUS_PCS_LINK_STS,
+					 500, 500 * 2000);
+		if (err < 0) {
+			dev_err(mgbe->dev, "timeout waiting for link to become ready\n");
Quoted 8 lines

drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c line 128

Quoted 5 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +5 -0
+			usleep_range(10000, 20000);
+			continue;
+		}
+		break;
+	}

It looks like the above loop can take up to 150 seconds (300 iterations, 500000usec each), can it be shortned?

This is likely left-over from debugging. It might be possible to get rid of the loop altogether and just use the built-in retry mechanism from readl_poll_timeout().

Bhadram, do you have any concerns with removing the outer while loop here? In your experience, if the link doesn't become ready within the 1 second timeout of the readl_poll_timeout() call, is it at all likely to succeed subsequently or can we safely assume that something has gone wrong?

Quoted 142 lines
Quoted 142 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +141 -0
+
+	/* clear status */
+	writel(value, mgbe->xpcs + XPCS_WRAP_IRQ_STATUS);
+}
+
+static int tegra_mgbe_probe(struct platform_device *pdev)
+{
+	struct plat_stmmacenet_data *plat;
+	struct stmmac_resources res;
+	struct tegra_mgbe *mgbe;
+	int irq, err, i;
+
+	mgbe = devm_kzalloc(&pdev->dev, sizeof(*mgbe), GFP_KERNEL);
+	if (!mgbe)
+		return -ENOMEM;
+
+	mgbe->dev = &pdev->dev;
+
+	memset(&res, 0, sizeof(res));
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
+	mgbe->hv = devm_platform_ioremap_resource_byname(pdev, "hypervisor");
+	if (IS_ERR(mgbe->hv))
+		return PTR_ERR(mgbe->hv);
+
+	mgbe->regs = devm_platform_ioremap_resource_byname(pdev, "mac");
+	if (IS_ERR(mgbe->regs))
+		return PTR_ERR(mgbe->regs);
+
+	mgbe->xpcs = devm_platform_ioremap_resource_byname(pdev, "xpcs");
+	if (IS_ERR(mgbe->xpcs))
+		return PTR_ERR(mgbe->xpcs);
+
+	res.addr = mgbe->regs;
+	res.irq = irq;
+
+	mgbe->clks = devm_kzalloc(&pdev->dev, sizeof(*mgbe->clks), GFP_KERNEL);
+	if (!mgbe->clks)
+		return -ENOMEM;
+
+	for (i = 0; i <  ARRAY_SIZE(mgbe_clks); i++)
+		mgbe->clks[i].id = mgbe_clks[i];
+
+	err = devm_clk_bulk_get(mgbe->dev, ARRAY_SIZE(mgbe_clks), mgbe->clks);
+	if (err < 0)
+		return err;
+
+	err = clk_bulk_prepare_enable(ARRAY_SIZE(mgbe_clks), mgbe->clks);
+	if (err < 0)
+		return err;
+
+	/* Perform MAC reset */
+	mgbe->rst_mac = devm_reset_control_get(&pdev->dev, "mac");
+	if (IS_ERR(mgbe->rst_mac))
+		return PTR_ERR(mgbe->rst_mac);
+
+	err = reset_control_assert(mgbe->rst_mac);
+	if (err < 0)
+		return err;
+
+	usleep_range(2000, 4000);
+
+	err = reset_control_deassert(mgbe->rst_mac);
+	if (err < 0)
+		return err;
+
+	/* Perform PCS reset */
+	mgbe->rst_pcs = devm_reset_control_get(&pdev->dev, "pcs");
+	if (IS_ERR(mgbe->rst_pcs))
+		return PTR_ERR(mgbe->rst_pcs);
+
+	err = reset_control_assert(mgbe->rst_pcs);
+	if (err < 0)
+		return err;
+
+	usleep_range(2000, 4000);
+
+	err = reset_control_deassert(mgbe->rst_pcs);
+	if (err < 0)
+		return err;
+
+	plat = stmmac_probe_config_dt(pdev, res.mac);
+	if (IS_ERR(plat))
+		return PTR_ERR(plat);
+
+	plat->has_xgmac = 1;
+	plat->tso_en = 1;
+	plat->pmt = 1;
+	plat->bsp_priv = mgbe;
+
+	if (!plat->mdio_node)
+		plat->mdio_node = of_get_child_by_name(pdev->dev.of_node, "mdio");
+
+	if (!plat->mdio_bus_data) {
+		plat->mdio_bus_data = devm_kzalloc(&pdev->dev, sizeof(*plat->mdio_bus_data),
+						   GFP_KERNEL);
+		if (!plat->mdio_bus_data) {
+			err = -ENOMEM;
+			goto remove;
+		}
+	}
+
+	plat->mdio_bus_data->needs_reset = true;
+
+	mgbe_uphy_lane_bringup(mgbe);
+
+	/* Tx FIFO Size - 128KB */
+	plat->tx_fifo_size = 131072;
+	/* Rx FIFO Size - 192KB */
+	plat->rx_fifo_size = 196608;
+
+	/* Enable common interrupt at wrapper level */
+	writel(MAC_SBD_INTR, mgbe->regs + MGBE_WRAP_COMMON_INTR_ENABLE);
+
+	/* Program SID */
+	writel(MGBE_SID, mgbe->hv + MGBE_WRAP_AXI_ASID0_CTRL);
+
+	err = stmmac_dvr_probe(&pdev->dev, plat, &res);
+	if (err < 0)
+		goto remove;
+
+	return 0;
+
+remove:
+	stmmac_remove_config_dt(pdev, plat);
+	return err;
+}
+
+static int tegra_mgbe_remove(struct platform_device *pdev)
+{
+	struct tegra_mgbe *mgbe = get_stmmac_bsp_priv(&pdev->dev);
+
+	clk_bulk_disable_unprepare(ARRAY_SIZE(mgbe_clks), mgbe->clks);
+
+	stmmac_pltfr_remove(pdev);
+
+	return 0;
+}
Quoted 8 lines

drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c line 275

Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-tegra.c new file +6 -0
+
+static const struct of_device_id tegra_mgbe_match[] = {
+	{ .compatible = "nvidia,tegra234-mgbe", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, tegra_mgbe_match);

The DT bindings will land in 6.1, right?

Yes, the DT bindings and the device tree changes for Jetson AGX Orin are currently targetted for 6.1.

Thierry

#3 lore raw
In reply to Thierry Reding

+Russell, Andrew, Vladimir,

Quoted 11 lines · Thierry Reding

From: Bhadram Varka <[email protected]>

Add support for the Multi-Gigabit Ethernet (MGBE/XPCS) IP found on NVIDIA Tegra234 SoCs.

---
  drivers/net/ethernet/stmicro/stmmac/Kconfig   |   6 +
  drivers/net/ethernet/stmicro/stmmac/Makefile  |   1 +
  .../net/ethernet/stmicro/stmmac/dwmac-tegra.c | 290 ++++++++++++++++++

You should be modeling this as a proper PCS driver and have a 'pcs-handle' property pointing to it in your Device Tree.

The configuration you are doing here is probably working the first time you bring-up the network device but I doubt it works across system suspend/resume states where power to the GMAC and PCS is lost, it also begs the question of which mediums this was tested with and whether dynamic switching of speeds and so on is working? --
Florian

#4 lore raw

Hi @Florian Fainelli,
Thanks for the review.

Quoted 35 lines

-----Original Message-----
From: Florian Fainelli <[email protected]>
Sent: 27 September 2022 10:53 PM
To: Thierry Reding <[email protected]>; David S . Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Russell King <[email protected]>; Andrew Lunn <[email protected]>; Vladimir Oltean <[email protected]>
Cc: Jonathan Hunter <[email protected]>; Bhadram Varka <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH net-next v4 RESEND] stmmac: tegra: Add MGBE support

External email: Use caution opening links or attachments

+Russell, Andrew, Vladimir,

Quoted 5 lines · Thierry Reding

From: Bhadram Varka <[email protected]>

Add support for the Multi-Gigabit Ethernet (MGBE/XPCS) IP found on NVIDIA Tegra234 SoCs.

Quoted 7 lines
---
  drivers/net/ethernet/stmicro/stmmac/Kconfig   |   6 +
  drivers/net/ethernet/stmicro/stmmac/Makefile  |   1 +
  .../net/ethernet/stmicro/stmmac/dwmac-tegra.c | 290

++++++++++++++++++

You should be modeling this as a proper PCS driver and have a 'pcs-handle' property pointing to it in your Device Tree.

Quoted 6 lines

The configuration you are doing here is probably working the first time you bring-up the network device but I doubt it works across system suspend/resume states where power to the GMAC and PCS is lost, it also begs the question of which mediums this was tested with and whether dynamic switching of speeds and so on is working?
--

For Tegra234, there is UPHY lanes control logic inside XPCS IP which is memory-mapped IP (not part of the MAC IP). mgbe_uphy_lane_bringup performs UPHY lane bring up here. Here MGBE/XPCS works in XFI mode.

Agree that lane bring down logic is not present interface down/suspend paths. Will update the changes accordingly. One more thing is that UPHY lane bring should happen only after the line side link is up. This also will make the changes. Please let me know if I miss anything here.

Thanks,
Bhadram.

maintainer
#5 lore raw

Hi Florian,

On 12/10/2022 05:56, Bhadram Varka wrote:

...

Quoted 10 lines
Quoted 9 lines

You should be modeling this as a proper PCS driver and have a 'pcs-handle' property pointing to it in your Device Tree.

The configuration you are doing here is probably working the first time you bring-up the network device but I doubt it works across system suspend/resume states where power to the GMAC and PCS is lost, it also begs the question of which mediums this was tested with and whether dynamic switching of speeds and so on is working?
--

Quoted 6 lines

For Tegra234, there is UPHY lanes control logic inside XPCS IP which is memory-mapped IP (not part of the MAC IP). mgbe_uphy_lane_bringup performs UPHY lane bring up here. Here MGBE/XPCS works in XFI mode.

Agree that lane bring down logic is not present interface down/suspend paths. Will update the changes accordingly. One more thing is that UPHY lane bring should happen only after the line side link is up. This also will make the changes. Please let me know if I miss anything here.

An updated version of this has now been posted [0]. It should have been marked as 'V5'. We have tested suspend/resume and verified that it is working. We are hoping to get this into Linux v6.2 if not too late. Let me know if you have any concerns.

Thanks
Jon

[0] https://lore.kernel.org/linux-tegra/[email protected]/T/#t

Signature · 1 line
nvpublic
#6 lore raw
In reply to Bhadram Varka

Hi Bhadram,

Quoted 10 lines · Bhadram Varka
Quoted 9 lines

You should be modeling this as a proper PCS driver and have a 'pcs-handle' property pointing to it in your Device Tree.

The configuration you are doing here is probably working the first time you bring-up the network device but I doubt it works across system suspend/resume states where power to the GMAC and PCS is lost, it also begs the question of which mediums this was tested with and whether dynamic switching of speeds and so on is working?
--

Quoted 6 lines

For Tegra234, there is UPHY lanes control logic inside XPCS IP which is memory-mapped IP (not part of the MAC IP). mgbe_uphy_lane_bringup performs UPHY lane bring up here. Here MGBE/XPCS works in XFI mode.

Agree that lane bring down logic is not present interface down/suspend paths. Will update the changes accordingly. One more thing is that UPHY lane bring should happen only after the line side link is up. This also will make the changes. Please let me know if I miss anything here.

What about the non-UPHY part of the XPCS IP, how does the dwmac-tegra.c driver control it/see the status it reports?

#7 lore raw

Hi Vladimir,

Quoted 35 lines

-----Original Message-----
From: Vladimir Oltean <[email protected]>
Sent: 18 November 2022 06:32 PM
To: Bhadram Varka <[email protected]>
Cc: Florian Fainelli <[email protected]>; Thierry Reding <[email protected]>; David S . Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Russell King <[email protected]>; Andrew Lunn <[email protected]>; Revanth Kumar Uppala <[email protected]>; Jonathan Hunter <[email protected]>; linux- [email protected]; [email protected]
Subject: Re: [PATCH net-next v4 RESEND] stmmac: tegra: Add MGBE support

External email: Use caution opening links or attachments

Hi Bhadram,

Quoted 1 line · Bhadram Varka
Quoted 1 line

You should be modeling this as a proper PCS driver and have a 'pcs-

handle'

Quoted 10 lines
Quoted 8 lines

property pointing to it in your Device Tree.

The configuration you are doing here is probably working the first time you bring-up the network device but I doubt it works across system suspend/resume states where power to the GMAC and PCS is lost, it also begs the question of which mediums this was tested with and whether dynamic switching of speeds and so on is working? --

For Tegra234, there is UPHY lanes control logic inside XPCS IP which is

memory-mapped IP (not part of the MAC IP).

Quoted 1 line

mgbe_uphy_lane_bringup performs UPHY lane bring up here. Here

MGBE/XPCS works in XFI mode.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

Agree that lane bring down logic is not present interface down/suspend

paths. Will update the changes accordingly.

Quoted 1 line

One more thing is that UPHY lane bring should happen only after the line

side link is up. This also will make the changes.

Quoted 1 line

Please let me know if I miss anything here.

What about the non-UPHY part of the XPCS IP, how does the dwmac-tegra.c driver control it/see the status it reports?

Reset values of XPCS IP take care of configuring the IP in 10G mode. No need for extra register programming is required from the driver side. The only status that the driver expects from XPCS IP is RLU to be up which will be done by serdes_up in recent posted changes. Please let me know if any other queries on recent changes [0]

Thank You!

[0]: https://patchwork.ozlabs.org/project/linux-tegra/patch/[email protected]/

#8 lore raw
Quoted 9 lines · Bhadram Varka

Reset values of XPCS IP take care of configuring the IP in 10G mode. No need for extra register programming is required from the driver side. The only status that the driver expects from XPCS IP is RLU to be up which will be done by serdes_up in recent posted changes. Please let me know if any other queries on recent changes [0]

Thank You!

[0]: https://patchwork.ozlabs.org/project/linux-tegra/patch/[email protected]/

What about link status reporting, if the XPCS is connected to an SFP cage?

What I'm trying to get at is that maybe it would be useful to consider the pcs-xpcs.c phylink pcs driver, even if your XPCS IP is memory mapped, that is not a problem. Using mdiobus_register(), you can create your own "MDIO" controller with custom bus read() and write() operations which translate C45 accesses as seen by the xpcs driver into proper MMIO accesses at the right address.

If I understand the hardware model right, the XPCS MDIO bus could be exported by a common, top-level SERDES driver. In addition to the XPCS MDIO bus, it would also model the lanes as generic PHY devices, on which you could call phy_set_mode_ext(serdes, PHY_MODE_ETHERNET, phy_mode), and phy_power_on()/phy_power_off().

Can your SERDES lanes also operate in PCIe mode? If yes, how is the selection between PCIe and Ethernet/XPCS done?

#9 lore raw

Hi Vladimir,

Quoted 29 lines

-----Original Message-----
From: Vladimir Oltean <[email protected]>
Sent: 22 November 2022 06:56 PM
To: Bhadram Varka <[email protected]>
Cc: Florian Fainelli <[email protected]>; Thierry Reding <[email protected]>; David S . Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Russell King <[email protected]>; Andrew Lunn <[email protected]>; Revanth Kumar Uppala <[email protected]>; Jonathan Hunter <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH net-next v4 RESEND] stmmac: tegra: Add MGBE support

External email: Use caution opening links or attachments

Quoted 11 lines · Bhadram Varka

Reset values of XPCS IP take care of configuring the IP in 10G mode. No need for extra register programming is required from the driver side. The only status that the driver expects from XPCS IP is RLU to be up which will be done by serdes_up in recent posted changes. Please let me know if any other queries on recent changes [0]

Thank You!

[0]:
https://patchwork.ozlabs.org/project/linux-tegra/patch/20221118075744.
[email protected]/

Quoted 9 lines

What about link status reporting, if the XPCS is connected to an SFP cage?

What I'm trying to get at is that maybe it would be useful to consider the pcs- xpcs.c phylink pcs driver, even if your XPCS IP is memory mapped, that is not a problem. Using mdiobus_register(), you can create your own "MDIO" controller with custom bus read() and write() operations which translate C45 accesses as seen by the xpcs driver into proper MMIO accesses at the right address.

Except UPHY lane bring up through XPCS IP wrapper, nothing extra done from driver. I think serdes_up/down function pointers gave the feasibility to do the same.

Quoted 5 lines

If I understand the hardware model right, the XPCS MDIO bus could be exported by a common, top-level SERDES driver. In addition to the XPCS MDIO bus, it would also model the lanes as generic PHY devices, on which you could call phy_set_mode_ext(serdes, PHY_MODE_ETHERNET, phy_mode), and phy_power_on()/phy_power_off().

There is no MDIO bus in XPCS IP.

Quoted 2 lines

Can your SERDES lanes also operate in PCIe mode? If yes, how is the selection between PCIe and Ethernet/XPCS done?

No. It only operates in XFI.

Please let me know if there are any comments.

Thanks!