mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 00/10] SKOV maintenance
@ 2023-10-02 10:16 Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 01/10] arm: dts: imx8mp-skov: Add reserved-memory for ramoops pstore Oleksij Rempel
                   ` (9 more replies)
  0 siblings, 10 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

changes v2:
- add skov,imx8mp-board-version fixup
- add Ethernet addresses configuration
- drop cell size changes for SD card
- drop DT based partition for SD card

Oleksij Rempel (10):
  arm: dts: imx8mp-skov: Add reserved-memory for ramoops pstore
  arm: dts: imx8mp-skov: Add pins for hardware variant detection
  arm: dts: imx8mp-skov: Switch to GPT for eMMC partitioning
  arm: dts: imx8mp-skov: Switch to GPT for SD partitioning
  arm: dts: imx8mp-skov: Add barebox state backend in DTS
  ARM: i.MX8MP: skov: refactor bootsource and BBU handlers
  ARM: i.MX8MP: skov: Add hardware variant support
  ARM: i.MX8MP: skov: fixup skov,imx8mp-board-version DT property for
    the kernel
  net: lib: add ether_addr_inc() helper
  ARM: i.MX8MP: skov: assign Ethernet addresses to all ports

 arch/arm/boards/skov-imx8mp/board.c | 367 +++++++++++++++++++++++++++-
 arch/arm/dts/imx8mp-skov.dts        | 157 +++++++++---
 include/net.h                       |   1 +
 net/lib.c                           |  25 ++
 4 files changed, 510 insertions(+), 40 deletions(-)

-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 01/10] arm: dts: imx8mp-skov: Add reserved-memory for ramoops pstore
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 02/10] arm: dts: imx8mp-skov: Add pins for hardware variant detection Oleksij Rempel
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Add a reserved-memory node for 'ramoops' in imx8mp-skov device tree to enable
persistent storage of logs.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/dts/imx8mp-skov.dts | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/arm/dts/imx8mp-skov.dts b/arch/arm/dts/imx8mp-skov.dts
index b7f6fed4b7..4c83bb6bc2 100644
--- a/arch/arm/dts/imx8mp-skov.dts
+++ b/arch/arm/dts/imx8mp-skov.dts
@@ -62,6 +62,17 @@ led-2 {
 		};
 	};
 
+	reserved-memory {
+		#address-cells = <1>;
+		#size-cells = <1>;
+		ranges;
+
+		/* Address will be determined by the bootloader */
+		ramoops {
+			compatible = "ramoops";
+		};
+	};
+
 	reg_usdhc2_vmmc: regulator-usdhc2 {
 		compatible = "regulator-fixed";
 		pinctrl-names = "default";
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 02/10] arm: dts: imx8mp-skov: Add pins for hardware variant detection
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 01/10] arm: dts: imx8mp-skov: Add reserved-memory for ramoops pstore Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 03/10] arm: dts: imx8mp-skov: Switch to GPT for eMMC partitioning Oleksij Rempel
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Define pins in imx8mp-skov device tree for detecting hardware variants.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/dts/imx8mp-skov.dts | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/arch/arm/dts/imx8mp-skov.dts b/arch/arm/dts/imx8mp-skov.dts
index 4c83bb6bc2..4b73022938 100644
--- a/arch/arm/dts/imx8mp-skov.dts
+++ b/arch/arm/dts/imx8mp-skov.dts
@@ -369,6 +369,23 @@ &wdog1 {
 };
 
 &iomuxc {
+	pinctrl-names = "default";
+	pinctrl-0 = <&pinctrl_hog>;
+
+	pinctrl_hog: hoggrp {
+		fsl,pins = <
+			/* varaint id */
+			MX8MP_IOMUXC_GPIO1_IO08__GPIO1_IO08				0x100
+			MX8MP_IOMUXC_GPIO1_IO09__GPIO1_IO09				0x100
+			MX8MP_IOMUXC_GPIO1_IO10__GPIO1_IO10				0x100
+			MX8MP_IOMUXC_GPIO1_IO11__GPIO1_IO11				0x100
+			MX8MP_IOMUXC_GPIO1_IO12__GPIO1_IO12				0x100
+			MX8MP_IOMUXC_GPIO1_IO13__GPIO1_IO13				0x100
+			MX8MP_IOMUXC_GPIO1_IO14__GPIO1_IO14				0x100
+			MX8MP_IOMUXC_GPIO1_IO15__GPIO1_IO15				0x100
+		>;
+	};
+
 	pinctrl_eqos: eqosgrp {
 		fsl,pins = <
 			MX8MP_IOMUXC_ENET_RD0__ENET_QOS_RGMII_RD0			0x91
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 03/10] arm: dts: imx8mp-skov: Switch to GPT for eMMC partitioning
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 01/10] arm: dts: imx8mp-skov: Add reserved-memory for ramoops pstore Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 02/10] arm: dts: imx8mp-skov: Add pins for hardware variant detection Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 04/10] arm: dts: imx8mp-skov: Switch to GPT for SD partitioning Oleksij Rempel
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Move away from using device tree to define eMMC partitions for
imx8mp-skov. Adopt GPT based partitioning, specifically for the
barebox-environment. And prepare it for state partition in the next
patch.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/dts/imx8mp-skov.dts | 20 +-------------------
 1 file changed, 1 insertion(+), 19 deletions(-)

diff --git a/arch/arm/dts/imx8mp-skov.dts b/arch/arm/dts/imx8mp-skov.dts
index 4b73022938..1d4738dbd7 100644
--- a/arch/arm/dts/imx8mp-skov.dts
+++ b/arch/arm/dts/imx8mp-skov.dts
@@ -22,7 +22,7 @@ environment-sd {
 
 		environment-emmc {
 			compatible = "barebox,environment";
-			device-path = &env_emmc;
+			device-path = &usdhc3, "partname:barebox-environment";
 			status = "disabled";
 		};
 	};
@@ -103,24 +103,6 @@ env_sd: partition@e0000 {
 	};
 };
 
-&usdhc3 {
-	partitions {
-		compatible = "fixed-partitions";
-		#address-cells = <2>;
-		#size-cells = <2>;
-
-		partition@0 {
-			label = "barebox";
-			reg = <0x0 0x0 0x0 0x100000>;
-		};
-
-		env_emmc: partition@e0000 {
-			label = "barebox-environment";
-			reg = <0x0 0x100000 0x0 0x100000>;
-		};
-	};
-};
-
 &A53_0 {
 	cpu-supply = <&buck2>;
 };
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 04/10] arm: dts: imx8mp-skov: Switch to GPT for SD partitioning
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
                   ` (2 preceding siblings ...)
  2023-10-02 10:16 ` [PATCH v2 03/10] arm: dts: imx8mp-skov: Switch to GPT for eMMC partitioning Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 05/10] arm: dts: imx8mp-skov: Add barebox state backend in DTS Oleksij Rempel
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Same as for eMMC, use GPT based partitioning. In case of SD cards, they
can be used just for storing data without having boot partitions.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/dts/imx8mp-skov.dts | 20 +-------------------
 1 file changed, 1 insertion(+), 19 deletions(-)

diff --git a/arch/arm/dts/imx8mp-skov.dts b/arch/arm/dts/imx8mp-skov.dts
index 1d4738dbd7..accaed44ab 100644
--- a/arch/arm/dts/imx8mp-skov.dts
+++ b/arch/arm/dts/imx8mp-skov.dts
@@ -16,7 +16,7 @@ chosen {
 
 		environment-sd {
 			compatible = "barebox,environment";
-			device-path = &env_sd;
+			device-path = &usdhc2, "partname:barebox-environment";
 			status = "disabled";
 		};
 
@@ -85,24 +85,6 @@ reg_usdhc2_vmmc: regulator-usdhc2 {
 	};
 };
 
-&usdhc2 {
-	partitions {
-		compatible = "fixed-partitions";
-		#address-cells = <2>;
-		#size-cells = <2>;
-
-		partition@0 {
-			label = "barebox";
-			reg = <0x0 0x0 0x0 0x100000>;
-		};
-
-		env_sd: partition@e0000 {
-			label = "barebox-environment";
-			reg = <0x0 0x100000 0x0 0x100000>;
-		};
-	};
-};
-
 &A53_0 {
 	cpu-supply = <&buck2>;
 };
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 05/10] arm: dts: imx8mp-skov: Add barebox state backend in DTS
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
                   ` (3 preceding siblings ...)
  2023-10-02 10:16 ` [PATCH v2 04/10] arm: dts: imx8mp-skov: Switch to GPT for SD partitioning Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 06/10] ARM: i.MX8MP: skov: refactor bootsource and BBU handlers Oleksij Rempel
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Introduce 'state' node to manage device states using barebox state framework.
Set backend to usdhc3 and define storage parameters.

For now, reuse existing layout from Skov i.MX6 variants.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/dts/imx8mp-skov.dts | 105 +++++++++++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)

diff --git a/arch/arm/dts/imx8mp-skov.dts b/arch/arm/dts/imx8mp-skov.dts
index accaed44ab..885ffe6a24 100644
--- a/arch/arm/dts/imx8mp-skov.dts
+++ b/arch/arm/dts/imx8mp-skov.dts
@@ -31,6 +31,7 @@ aliases {
 		ethernet0 = &eqos;
 		ethernet1 = &lan1;
 		ethernet2 = &lan2;
+		state = &state;
 	};
 
 	leds {
@@ -83,6 +84,110 @@ reg_usdhc2_vmmc: regulator-usdhc2 {
 		gpio = <&gpio2 19 GPIO_ACTIVE_HIGH>;
 		enable-active-high;
 	};
+
+	state: state {
+		#address-cells = <1>;
+		#size-cells = <1>;
+		magic = <0x1c5b3f49>;
+		compatible = "barebox,state";
+		backend-type = "raw";
+		backend = <&usdhc3>;
+                /*
+                 * barebox-state partition size: 1 MiB
+                 * nr. of redundant copies:      4
+                 * ==> max. stride size: 1 MiB / 4 = 256 KiB = 262144 Byte
+                 *
+                 * stride size:     262144 Byte
+                 * raw-header:     -    16 Byte
+                 * direct-storage: -     8 Byte
+                 *                 ------------
+                 * max state size:  262120 Byte
+                 *                  ===========
+                 */
+		backend-stridesize = <0x40000>;
+
+		bootstate {
+			#address-cells = <1>;
+			#size-cells = <1>;
+
+			system0 {
+				#address-cells = <1>;
+				#size-cells = <1>;
+
+				remaining_attempts@0 {
+					reg = <0x0 0x4>;
+					type = "uint32";
+					default = <3>;
+				};
+				priority@4 {
+					reg = <0x4 0x4>;
+					type = "uint32";
+					default = <30>;
+				};
+			};
+
+			system1 {
+				#address-cells = <1>;
+				#size-cells = <1>;
+
+				remaining_attempts@8 {
+					reg = <0x8 0x4>;
+					type = "uint32";
+					default = <3>;
+				};
+				priority@C {
+					reg = <0xC 0x4>;
+					type = "uint32";
+					default = <20>;
+				};
+			};
+
+			last_chosen@10 {
+				reg = <0x10 0x4>;
+				type = "uint32";
+			};
+		};
+
+		display {
+			#address-cells = <1>;
+			#size-cells = <1>;
+
+			xres@14 {
+				reg = <0x14 0x4>;
+				type = "uint32";
+				default = <0>;
+			};
+
+			yres@18 {
+				reg = <0x18 0x4>;
+				type = "uint32";
+				default = <0>;
+			};
+
+			brightness@1C {
+				reg = <0x1C 0x1>;
+				type = "uint8";
+				default = <8>;
+			};
+
+			external@1D {
+				reg = <0x1D 0x1>;
+				type = "uint8";
+				default = <0>;
+			};
+		};
+
+		ethaddr {
+			#address-cells = <1>;
+			#size-cells = <1>;
+
+			eth2@1e {
+				reg = <0x1E 0x6>;
+				type = "mac";
+				default = [00 11 22 33 44 55];
+			};
+		};
+	};
 };
 
 &A53_0 {
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 06/10] ARM: i.MX8MP: skov: refactor bootsource and BBU handlers
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
                   ` (4 preceding siblings ...)
  2023-10-02 10:16 ` [PATCH v2 05/10] arm: dts: imx8mp-skov: Add barebox state backend in DTS Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 07/10] ARM: i.MX8MP: skov: Add hardware variant support Oleksij Rempel
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Rework boot source and Barebox Update (BBU) handlers to support
multiple storage options in a scalable way. This commit allows for
easier extension and better maintainability.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/boards/skov-imx8mp/board.c | 105 +++++++++++++++++++++++++---
 1 file changed, 95 insertions(+), 10 deletions(-)

diff --git a/arch/arm/boards/skov-imx8mp/board.c b/arch/arm/boards/skov-imx8mp/board.c
index 398c74677d..58aca69236 100644
--- a/arch/arm/boards/skov-imx8mp/board.c
+++ b/arch/arm/boards/skov-imx8mp/board.c
@@ -9,21 +9,106 @@
 #include <mach/imx/bbu.h>
 #include <mach/imx/iomux-mx8mp.h>
 
-static int skov_imx8mp_probe(struct device *dev)
+struct skov_imx8mp_storage {
+	const char *name;
+	const char *env_path;
+	const char *dev_path;
+	enum bootsource bootsource;
+	int bootsource_ext_id;
+	bool mmc_boot_part;
+};
+
+enum skov_imx8mp_boot_source {
+	SKOV_BOOT_SOURCE_EMMC,
+	SKOV_BOOT_SOURCE_SD,
+	SKOV_BOOT_SOURCE_UNKNOWN,
+};
+
+static const struct skov_imx8mp_storage skov_imx8mp_storages[] = {
+	[SKOV_BOOT_SOURCE_EMMC] = {
+		/* default boot source */
+		.name = "eMMC",
+		.env_path = "/chosen/environment-emmc",
+		.dev_path = "/dev/mmc2",
+		.bootsource = BOOTSOURCE_MMC,
+		.bootsource_ext_id = 2,
+		.mmc_boot_part = true,
+	},
+	[SKOV_BOOT_SOURCE_SD] = {
+		.name = "SD",
+		.env_path = "/chosen/environment-sd",
+		.dev_path = "/dev/mmc1.barebox",
+		.bootsource = BOOTSOURCE_MMC,
+		.bootsource_ext_id = 1,
+	},
+};
+
+static void skov_imx8mp_enable_env(struct device *dev,
+				   const struct skov_imx8mp_storage *st,
+				   bool *enabled)
 {
-	int emmc_bbu_flag = 0;
-	int sd_bbu_flag = 0;
+	int ret;
 
-	if (bootsource_get() == BOOTSOURCE_MMC && bootsource_get_instance() == 1) {
-		of_device_enable_path("/chosen/environment-sd");
-		sd_bbu_flag = BBU_HANDLER_FLAG_DEFAULT;
+	if (bootsource_get() != st->bootsource ||
+	    bootsource_get_instance() != st->bootsource_ext_id)
+		return;
+
+	ret = of_device_enable_path(st->env_path);
+	if (ret) {
+		dev_err(dev, "Failed to enable environment path: %s, %pe\n",
+			st->env_path, ERR_PTR(ret));
+		return;
+	}
+
+	*enabled = true;
+}
+
+static void skov_imx8mp_add_bbu(struct device *dev,
+				const struct skov_imx8mp_storage *st,
+				bool default_env)
+{
+	unsigned long flags = 0;
+	int ret;
+
+	if (default_env)
+		flags |= BBU_HANDLER_FLAG_DEFAULT;
+
+	if (st->mmc_boot_part) {
+		ret = imx8m_bbu_internal_mmcboot_register_handler(st->name,
+								  st->dev_path,
+								  flags);
 	} else {
-		of_device_enable_path("/chosen/environment-emmc");
-		emmc_bbu_flag = BBU_HANDLER_FLAG_DEFAULT;
+		ret = imx8m_bbu_internal_mmc_register_handler(st->name,
+							      st->dev_path,
+							      flags);
+	}
+	if (ret)
+		dev_err(dev, "Failed to register %s BBU handler: %pe\n",
+			st->name, ERR_PTR(ret));
+}
+
+static void skov_imx8mp_init_storage(struct device *dev)
+{
+	int default_boot_src = SKOV_BOOT_SOURCE_EMMC;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(skov_imx8mp_storages); i++) {
+		bool enabled_env = false;
+
+		skov_imx8mp_enable_env(dev, &skov_imx8mp_storages[i],
+				       &enabled_env);
+		if (enabled_env)
+			default_boot_src = i;
 	}
 
-	imx8m_bbu_internal_mmc_register_handler("SD", "/dev/mmc1.barebox", sd_bbu_flag);
-	imx8m_bbu_internal_mmcboot_register_handler("eMMC", "/dev/mmc2", emmc_bbu_flag);
+	for (i = 0; i < ARRAY_SIZE(skov_imx8mp_storages); i++)
+		skov_imx8mp_add_bbu(dev, &skov_imx8mp_storages[i],
+				    i == default_boot_src);
+}
+
+static int skov_imx8mp_probe(struct device *dev)
+{
+	skov_imx8mp_init_storage(dev);
 
 	return 0;
 }
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 07/10] ARM: i.MX8MP: skov: Add hardware variant support
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
                   ` (5 preceding siblings ...)
  2023-10-02 10:16 ` [PATCH v2 06/10] ARM: i.MX8MP: skov: refactor bootsource and BBU handlers Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 08/10] ARM: i.MX8MP: skov: fixup skov,imx8mp-board-version DT property for the kernel Oleksij Rempel
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Introduce hardware variant detection in skov-imx8mp board. This
addition uses GPIOs to identify hardware versions and set the
appropriate device tree compatible strings accordingly.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/boards/skov-imx8mp/board.c | 128 ++++++++++++++++++++++++++++
 1 file changed, 128 insertions(+)

diff --git a/arch/arm/boards/skov-imx8mp/board.c b/arch/arm/boards/skov-imx8mp/board.c
index 58aca69236..3a1cd83bbd 100644
--- a/arch/arm/boards/skov-imx8mp/board.c
+++ b/arch/arm/boards/skov-imx8mp/board.c
@@ -1,14 +1,30 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#include "linux/kernel.h"
 #include <bootsource.h>
 #include <common.h>
 #include <deep-probe.h>
 #include <envfs.h>
+#include <environment.h>
+#include <globalvar.h>
+#include <gpio.h>
 #include <init.h>
 #include <io.h>
 #include <mach/imx/bbu.h>
+#include <mach/imx/generic.h>
 #include <mach/imx/iomux-mx8mp.h>
 
+#define GPIO_HW_VARIANT  {\
+	{IMX_GPIO_NR(1, 8), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var0"}, \
+	{IMX_GPIO_NR(1, 9), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var1"}, \
+	{IMX_GPIO_NR(1, 10), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var2"}, \
+	{IMX_GPIO_NR(1, 11), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var3"}, \
+	{IMX_GPIO_NR(1, 12), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var4"}, \
+	{IMX_GPIO_NR(1, 13), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var5"}, \
+	{IMX_GPIO_NR(1, 14), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var6"}, \
+	{IMX_GPIO_NR(1, 15), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var7"}, \
+}
+
 struct skov_imx8mp_storage {
 	const char *name;
 	const char *env_path;
@@ -43,6 +59,116 @@ static const struct skov_imx8mp_storage skov_imx8mp_storages[] = {
 	},
 };
 
+struct board_description {
+	const char *dts_compatible;
+	const char *dts_compatible_hdmi;
+	unsigned flags;
+};
+
+#define SKOV_IMX8MP_HAS_HDMI	BIT(0)
+
+static const struct board_description imx8mp_variants[] = {
+	[0] = {
+		.dts_compatible = "skov,imx8mp-skov-revb-lt6",
+	},
+	[1] = {
+		.dts_compatible = "skov,imx8mp-skov-revb-mi1010ait-1cp1",
+		.dts_compatible_hdmi = "skov,imx8mp-skov-revb-hdmi",
+		.flags = SKOV_IMX8MP_HAS_HDMI,
+	},
+};
+
+static int skov_imx8mp_get_variant_id(uint *id)
+{
+	struct gpio gpios_rev[] = GPIO_HW_VARIANT;
+	struct device_node *gpio_np;
+	u32 hw_rev;
+	int ret;
+
+	gpio_np = of_find_node_by_name_address(NULL, "gpio@30200000");
+	if (!gpio_np)
+		return -ENODEV;
+
+	ret = of_device_ensure_probed(gpio_np);
+	if (ret)
+		return ret;
+
+	ret = gpio_array_to_id(gpios_rev, ARRAY_SIZE(gpios_rev), &hw_rev);
+	if (ret)
+		goto exit_get_id;
+
+	*id = hw_rev;
+
+	return 0;
+exit_get_id:
+	pr_err("Failed to read gpio ID: %pe\n", ERR_PTR(ret));
+	return ret;
+}
+
+static int skov_imx8mp_get_hdmi(struct device *dev)
+{
+	const char *env = "state.display.external";
+	struct device_node *state_np;
+	unsigned int val = 0;
+	int ret;
+
+	state_np = of_find_node_by_name_address(NULL, "state");
+	if (!state_np) {
+		dev_err(dev, "Failed to find state node\n");
+		return -ENODEV;
+	}
+
+	ret = of_device_ensure_probed(state_np);
+	if (ret) {
+		dev_err(dev, "Failed to probe state node: %pe\n", ERR_PTR(ret));
+		return ret;
+	}
+
+	ret = getenv_uint(env, &val);
+	if (ret) {
+		dev_err(dev, "Failed to read %s: %pe\n", env, ERR_PTR(ret));
+		return ret;
+	}
+
+	return val;
+}
+
+static int skov_imx8mp_init_variant(struct device *dev)
+{
+	const struct board_description *variant;
+	const char *compatible;
+	unsigned int v = 0;
+	int ret;
+
+	ret = skov_imx8mp_get_variant_id(&v);
+	if (ret)
+		return ret;
+
+	if (v >= ARRAY_SIZE(imx8mp_variants)) {
+		dev_err(dev, "Invalid variant %u\n", v);
+		return -EINVAL;
+	}
+
+	variant = &imx8mp_variants[v];
+
+	if (variant->flags & SKOV_IMX8MP_HAS_HDMI) {
+		ret = skov_imx8mp_get_hdmi(dev);
+		if (ret < 0)
+			return ret;
+
+		if (ret)
+			compatible = variant->dts_compatible_hdmi;
+		else
+			compatible = variant->dts_compatible;
+	} else {
+		compatible = variant->dts_compatible;
+	}
+
+	of_prepend_machine_compatible(NULL, compatible);
+
+	return 0;
+}
+
 static void skov_imx8mp_enable_env(struct device *dev,
 				   const struct skov_imx8mp_storage *st,
 				   bool *enabled)
@@ -110,6 +236,8 @@ static int skov_imx8mp_probe(struct device *dev)
 {
 	skov_imx8mp_init_storage(dev);
 
+	skov_imx8mp_init_variant(dev);
+
 	return 0;
 }
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 08/10] ARM: i.MX8MP: skov: fixup skov,imx8mp-board-version DT property for the kernel
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
                   ` (6 preceding siblings ...)
  2023-10-02 10:16 ` [PATCH v2 07/10] ARM: i.MX8MP: skov: Add hardware variant support Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 09/10] net: lib: add ether_addr_inc() helper Oleksij Rempel
  2023-10-02 10:16 ` [PATCH v2 10/10] ARM: i.MX8MP: skov: assign Ethernet addresses to all ports Oleksij Rempel
  9 siblings, 0 replies; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Fixup Skov specific skov,imx8mp-board-version property for the kernel DT,
to keep it compatible with the imx6 version.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/boards/skov-imx8mp/board.c | 41 +++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)

diff --git a/arch/arm/boards/skov-imx8mp/board.c b/arch/arm/boards/skov-imx8mp/board.c
index 3a1cd83bbd..3b6eb7b080 100644
--- a/arch/arm/boards/skov-imx8mp/board.c
+++ b/arch/arm/boards/skov-imx8mp/board.c
@@ -14,6 +14,13 @@
 #include <mach/imx/generic.h>
 #include <mach/imx/iomux-mx8mp.h>
 
+struct skov_imx8mp_priv {
+	struct device *dev;
+	int variant_id;
+};
+
+static struct skov_imx8mp_priv *skov_imx8mp_priv;
+
 #define GPIO_HW_VARIANT  {\
 	{IMX_GPIO_NR(1, 8), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var0"}, \
 	{IMX_GPIO_NR(1, 9), GPIOF_DIR_IN | GPIOF_ACTIVE_HIGH, "var1"}, \
@@ -78,6 +85,22 @@ static const struct board_description imx8mp_variants[] = {
 	},
 };
 
+static int skov_imx8mp_fixup(struct device_node *root, void *data)
+{
+	struct device_node *chosen = of_create_node(root, "/chosen");
+	const char *of_board = "skov,imx8mp-board-version";
+	struct skov_imx8mp_priv *priv = data;
+	struct device *dev = priv->dev;
+	int ret;
+
+	ret = of_property_write_u32(chosen, of_board, priv->variant_id);
+	if (ret)
+		dev_err(dev,  "Failed to fixup %s: %pe\n", of_board,
+			ERR_PTR(ret));
+
+	return 0;
+}
+
 static int skov_imx8mp_get_variant_id(uint *id)
 {
 	struct gpio gpios_rev[] = GPIO_HW_VARIANT;
@@ -133,9 +156,10 @@ static int skov_imx8mp_get_hdmi(struct device *dev)
 	return val;
 }
 
-static int skov_imx8mp_init_variant(struct device *dev)
+static int skov_imx8mp_init_variant(struct skov_imx8mp_priv *priv)
 {
 	const struct board_description *variant;
+	struct device *dev = priv->dev;
 	const char *compatible;
 	unsigned int v = 0;
 	int ret;
@@ -144,6 +168,8 @@ static int skov_imx8mp_init_variant(struct device *dev)
 	if (ret)
 		return ret;
 
+	priv->variant_id = v;
+
 	if (v >= ARRAY_SIZE(imx8mp_variants)) {
 		dev_err(dev, "Invalid variant %u\n", v);
 		return -EINVAL;
@@ -234,9 +260,20 @@ static void skov_imx8mp_init_storage(struct device *dev)
 
 static int skov_imx8mp_probe(struct device *dev)
 {
+	struct skov_imx8mp_priv *priv;
+	int ret;
+
+	priv = xzalloc(sizeof(*priv));
+	priv->dev = dev;
+	skov_imx8mp_priv = priv;
+
 	skov_imx8mp_init_storage(dev);
 
-	skov_imx8mp_init_variant(dev);
+	skov_imx8mp_init_variant(priv);
+
+	ret = of_register_fixup(skov_imx8mp_fixup, priv);
+	if (ret)
+		dev_err(dev, "Failed to register fixup: %pe\n", ERR_PTR(ret));
 
 	return 0;
 }
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 09/10] net: lib: add ether_addr_inc() helper
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
                   ` (7 preceding siblings ...)
  2023-10-02 10:16 ` [PATCH v2 08/10] ARM: i.MX8MP: skov: fixup skov,imx8mp-board-version DT property for the kernel Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-04  6:41   ` Sascha Hauer
  2023-10-02 10:16 ` [PATCH v2 10/10] ARM: i.MX8MP: skov: assign Ethernet addresses to all ports Oleksij Rempel
  9 siblings, 1 reply; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Add helper function to calculate Ethernet address by incriminating it.
This helper can be used for multiport devices like switches to generate
address for each port based on one stored address.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 include/net.h |  1 +
 net/lib.c     | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/include/net.h b/include/net.h
index a0ef8bee04..fe82451cc9 100644
--- a/include/net.h
+++ b/include/net.h
@@ -360,6 +360,7 @@ int setenv_ip(const char *name, IPaddr_t ip);
 
 int string_to_ethaddr(const char *str, u8 enetaddr[6]);
 void ethaddr_to_string(const u8 enetaddr[6], char *str);
+void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment);
 
 #ifdef CONFIG_NET_RESOLV
 int resolv(const char *host, IPaddr_t *ip);
diff --git a/net/lib.c b/net/lib.c
index d4536441bd..dc7d83bc6d 100644
--- a/net/lib.c
+++ b/net/lib.c
@@ -44,6 +44,31 @@ void ethaddr_to_string(const u8 enetaddr[ETH_ALEN], char *str)
 		 enetaddr[4], enetaddr[5]);
 }
 
+/**
+ * ether_addr_inc - Increment an Ethernet address.
+ * @dst_addr: Destination address to store the incremented address.
+ * @src_addr: Source address to be incremented.
+ * @increment: Value by which to increment the source address.
+ *
+ * This function increments the given source Ethernet address by
+ * the specified increment value, storing the result in the
+ * destination address.
+ */
+void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment)
+{
+	u32 value;
+	int i;
+
+	for(i = 0; i < 6; ++i)
+		dst_addr[i] = src_addr[i];
+
+	for(i = 5; i >= 0 && increment; --i) {
+		value = dst_addr[i] + increment;
+		dst_addr[i] = value & 0xFF;
+		increment = value >> 8;
+	}
+}
+
 int string_to_ip(const char *s, IPaddr_t *ip)
 {
 	IPaddr_t addr = 0;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 10/10] ARM: i.MX8MP: skov: assign Ethernet addresses to all ports
  2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
                   ` (8 preceding siblings ...)
  2023-10-02 10:16 ` [PATCH v2 09/10] net: lib: add ether_addr_inc() helper Oleksij Rempel
@ 2023-10-02 10:16 ` Oleksij Rempel
  2023-10-05  6:52   ` Ahmad Fatoum
  9 siblings, 1 reply; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-02 10:16 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Assign Ethernet address to all ports based on the master address stored
in the NVMEM. In case nothing nvmem is clean, generate address based on
vendor OUI with part of i.MX8MP unique ID.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 arch/arm/boards/skov-imx8mp/board.c | 97 +++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/arch/arm/boards/skov-imx8mp/board.c b/arch/arm/boards/skov-imx8mp/board.c
index 3b6eb7b080..6de9ab47ab 100644
--- a/arch/arm/boards/skov-imx8mp/board.c
+++ b/arch/arm/boards/skov-imx8mp/board.c
@@ -12,13 +12,18 @@
 #include <io.h>
 #include <mach/imx/bbu.h>
 #include <mach/imx/generic.h>
+#include <mach/imx/imx8mq.h>
 #include <mach/imx/iomux-mx8mp.h>
+#include <net.h>
+#include <of_net.h>
 
 struct skov_imx8mp_priv {
 	struct device *dev;
 	int variant_id;
 };
 
+#define SKOV_OUI  {0x00, 0x0e, 0xcd}
+
 static struct skov_imx8mp_priv *skov_imx8mp_priv;
 
 #define GPIO_HW_VARIANT  {\
@@ -85,6 +90,98 @@ static const struct board_description imx8mp_variants[] = {
 	},
 };
 
+static struct eth_device *
+skov_imx8mp_init_master_edev(struct skov_imx8mp_priv *priv,
+			     struct device_node *np)
+{
+	u8 oui_mac[ETH_ALEN] = SKOV_OUI;
+	struct eth_device *edev;
+
+	edev = of_find_eth_device_by_node(np);
+	if (!edev) {
+		dev_err(priv->dev, "Failed to find master eth device\n");
+		return NULL;
+	}
+
+	if (is_valid_ether_addr(edev->ethaddr))
+		return edev;
+
+	if (!edev->parent || of_get_mac_addr_nvmem(edev->parent->of_node,
+						   oui_mac)) {
+		char str[sizeof("xx:xx:xx:xx:xx:xx")];
+		u64 unique_id;
+
+		unique_id = imx8m_uid();
+		if (!unique_id)
+			dev_err(priv->dev, "Failed to get i.MX8MP unique ID\n");
+
+		/* Generate MAC address based on i.MX8MP unique ID */
+		oui_mac[3] = (unique_id >> 56) & 0xff;
+		oui_mac[4] = (unique_id >> 48) & 0xff;
+		oui_mac[5] = (unique_id >> 40) & 0xff;
+		ethaddr_to_string(oui_mac, str);
+		dev_warn(priv->dev, "Failed to get master eth addr. Generating based on i.MX8MP unique ID: %s\n",
+			 str);
+	}
+
+	of_eth_register_ethaddr(np, oui_mac);
+
+	return edev;
+}
+
+static void skov_imx8mp_register_ethaddr(struct skov_imx8mp_priv *priv,
+					 struct device_node *root)
+{
+	struct eth_device *master_edev;
+	struct eth_node_info {
+		const char *alias;
+		struct device_node *np;
+		u32 inc;
+	} eth_nodes[] = {
+		{ "ethernet0", NULL, 0 },
+		/* Addresses are assigned in the reverse order
+		 * LAN2 addr < LAN1 addr
+		 */
+		{ "ethernet1", NULL, 1 },
+		{ "ethernet2", NULL, 0 },
+	};
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(eth_nodes); i++) {
+		eth_nodes[i].np = of_find_node_by_alias(root,
+							eth_nodes[i].alias);
+		if (!eth_nodes[i].np) {
+			dev_err(priv->dev, "Failed to find %s alias\n",
+				eth_nodes[i].alias);
+			return;
+		}
+	}
+
+	master_edev = skov_imx8mp_init_master_edev(priv, eth_nodes[0].np);
+	if (!master_edev) {
+		dev_err(priv->dev, "Failed to init master edev\n");
+		return;
+	}
+
+	/* Set ETH addresses for all but master edev */
+	for (i = 1; i < ARRAY_SIZE(eth_nodes); i++) {
+		u8 ethaddr[ETH_ALEN];
+
+		ether_addr_inc(ethaddr, master_edev->ethaddr, eth_nodes[i].inc);
+		of_eth_register_ethaddr(eth_nodes[i].np, ethaddr);
+	}
+}
+
+static int skov_imx8mp_populate_ethaddr(void)
+{
+	struct skov_imx8mp_priv *priv = skov_imx8mp_priv;
+
+	skov_imx8mp_register_ethaddr(priv, NULL);
+
+	return 0;
+}
+postenvironment_initcall(skov_imx8mp_populate_ethaddr);
+
 static int skov_imx8mp_fixup(struct device_node *root, void *data)
 {
 	struct device_node *chosen = of_create_node(root, "/chosen");
-- 
2.39.2




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 09/10] net: lib: add ether_addr_inc() helper
  2023-10-02 10:16 ` [PATCH v2 09/10] net: lib: add ether_addr_inc() helper Oleksij Rempel
@ 2023-10-04  6:41   ` Sascha Hauer
  2023-10-05  6:48     ` Ahmad Fatoum
  0 siblings, 1 reply; 16+ messages in thread
From: Sascha Hauer @ 2023-10-04  6:41 UTC (permalink / raw)
  To: Oleksij Rempel; +Cc: barebox

On Mon, Oct 02, 2023 at 12:16:53PM +0200, Oleksij Rempel wrote:
> Add helper function to calculate Ethernet address by incriminating it.
> This helper can be used for multiport devices like switches to generate
> address for each port based on one stored address.
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  include/net.h |  1 +
>  net/lib.c     | 25 +++++++++++++++++++++++++
>  2 files changed, 26 insertions(+)
> 
> diff --git a/include/net.h b/include/net.h
> index a0ef8bee04..fe82451cc9 100644
> --- a/include/net.h
> +++ b/include/net.h
> @@ -360,6 +360,7 @@ int setenv_ip(const char *name, IPaddr_t ip);
>  
>  int string_to_ethaddr(const char *str, u8 enetaddr[6]);
>  void ethaddr_to_string(const u8 enetaddr[6], char *str);
> +void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment);
>  
>  #ifdef CONFIG_NET_RESOLV
>  int resolv(const char *host, IPaddr_t *ip);
> diff --git a/net/lib.c b/net/lib.c
> index d4536441bd..dc7d83bc6d 100644
> --- a/net/lib.c
> +++ b/net/lib.c
> @@ -44,6 +44,31 @@ void ethaddr_to_string(const u8 enetaddr[ETH_ALEN], char *str)
>  		 enetaddr[4], enetaddr[5]);
>  }
>  
> +/**
> + * ether_addr_inc - Increment an Ethernet address.
> + * @dst_addr: Destination address to store the incremented address.
> + * @src_addr: Source address to be incremented.
> + * @increment: Value by which to increment the source address.
> + *
> + * This function increments the given source Ethernet address by
> + * the specified increment value, storing the result in the
> + * destination address.
> + */
> +void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment)
> +{
> +	u32 value;
> +	int i;
> +
> +	for(i = 0; i < 6; ++i)
> +		dst_addr[i] = src_addr[i];
> +
> +	for(i = 5; i >= 0 && increment; --i) {
> +		value = dst_addr[i] + increment;
> +		dst_addr[i] = value & 0xFF;
> +		increment = value >> 8;
> +	}
> +}

The Kernel already has eth_addr_add() and more helpers. How about
adopting these instead?

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 09/10] net: lib: add ether_addr_inc() helper
  2023-10-04  6:41   ` Sascha Hauer
@ 2023-10-05  6:48     ` Ahmad Fatoum
  2023-10-05  7:07       ` Oleksij Rempel
  0 siblings, 1 reply; 16+ messages in thread
From: Ahmad Fatoum @ 2023-10-05  6:48 UTC (permalink / raw)
  To: Sascha Hauer, Oleksij Rempel; +Cc: barebox

On 04.10.23 08:41, Sascha Hauer wrote:
> On Mon, Oct 02, 2023 at 12:16:53PM +0200, Oleksij Rempel wrote:
>> Add helper function to calculate Ethernet address by incriminating it.
>> This helper can be used for multiport devices like switches to generate
>> address for each port based on one stored address.
>>
>> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
>> ---
>>  include/net.h |  1 +
>>  net/lib.c     | 25 +++++++++++++++++++++++++
>>  2 files changed, 26 insertions(+)
>>
>> diff --git a/include/net.h b/include/net.h
>> index a0ef8bee04..fe82451cc9 100644
>> --- a/include/net.h
>> +++ b/include/net.h
>> @@ -360,6 +360,7 @@ int setenv_ip(const char *name, IPaddr_t ip);
>>  
>>  int string_to_ethaddr(const char *str, u8 enetaddr[6]);
>>  void ethaddr_to_string(const u8 enetaddr[6], char *str);
>> +void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment);
>>  
>>  #ifdef CONFIG_NET_RESOLV
>>  int resolv(const char *host, IPaddr_t *ip);
>> diff --git a/net/lib.c b/net/lib.c
>> index d4536441bd..dc7d83bc6d 100644
>> --- a/net/lib.c
>> +++ b/net/lib.c
>> @@ -44,6 +44,31 @@ void ethaddr_to_string(const u8 enetaddr[ETH_ALEN], char *str)
>>  		 enetaddr[4], enetaddr[5]);
>>  }
>>  
>> +/**
>> + * ether_addr_inc - Increment an Ethernet address.
>> + * @dst_addr: Destination address to store the incremented address.
>> + * @src_addr: Source address to be incremented.
>> + * @increment: Value by which to increment the source address.
>> + *
>> + * This function increments the given source Ethernet address by
>> + * the specified increment value, storing the result in the
>> + * destination address.
>> + */
>> +void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment)
>> +{
>> +	u32 value;
>> +	int i;
>> +
>> +	for(i = 0; i < 6; ++i)
>> +		dst_addr[i] = src_addr[i];
>> +
>> +	for(i = 5; i >= 0 && increment; --i) {
>> +		value = dst_addr[i] + increment;
>> +		dst_addr[i] = value & 0xFF;
>> +		increment = value >> 8;
>> +	}
>> +}
> 
> The Kernel already has eth_addr_add() and more helpers. How about
> adopting these instead?

I had copied them over here: https://lore.barebox.org/barebox/20230911155927.3786335-2-a.fatoum@pengutronix.de/
They got reverted along with patch 1/3 in that series, but they have no dependency, so could be reinstated.


> 
> Sascha
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 10/10] ARM: i.MX8MP: skov: assign Ethernet addresses to all ports
  2023-10-02 10:16 ` [PATCH v2 10/10] ARM: i.MX8MP: skov: assign Ethernet addresses to all ports Oleksij Rempel
@ 2023-10-05  6:52   ` Ahmad Fatoum
  0 siblings, 0 replies; 16+ messages in thread
From: Ahmad Fatoum @ 2023-10-05  6:52 UTC (permalink / raw)
  To: Oleksij Rempel, barebox

Hello Oleksij,

On 02.10.23 12:16, Oleksij Rempel wrote:
> Assign Ethernet address to all ports based on the master address stored
> in the NVMEM. In case nothing nvmem is clean, generate address based on
> vendor OUI with part of i.MX8MP unique ID.
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  arch/arm/boards/skov-imx8mp/board.c | 97 +++++++++++++++++++++++++++++
>  1 file changed, 97 insertions(+)
> 
> diff --git a/arch/arm/boards/skov-imx8mp/board.c b/arch/arm/boards/skov-imx8mp/board.c
> index 3b6eb7b080..6de9ab47ab 100644
> --- a/arch/arm/boards/skov-imx8mp/board.c
> +++ b/arch/arm/boards/skov-imx8mp/board.c
> @@ -12,13 +12,18 @@
>  #include <io.h>
>  #include <mach/imx/bbu.h>
>  #include <mach/imx/generic.h>
> +#include <mach/imx/imx8mq.h>
>  #include <mach/imx/iomux-mx8mp.h>
> +#include <net.h>
> +#include <of_net.h>
>  
>  struct skov_imx8mp_priv {
>  	struct device *dev;
>  	int variant_id;
>  };
>  
> +#define SKOV_OUI  {0x00, 0x0e, 0xcd}
> +
>  static struct skov_imx8mp_priv *skov_imx8mp_priv;
>  
>  #define GPIO_HW_VARIANT  {\
> @@ -85,6 +90,98 @@ static const struct board_description imx8mp_variants[] = {
>  	},
>  };
>  
> +static struct eth_device *
> +skov_imx8mp_init_master_edev(struct skov_imx8mp_priv *priv,
> +			     struct device_node *np)
> +{
> +	u8 oui_mac[ETH_ALEN] = SKOV_OUI;
> +	struct eth_device *edev;
> +
> +	edev = of_find_eth_device_by_node(np);
> +	if (!edev) {
> +		dev_err(priv->dev, "Failed to find master eth device\n");
> +		return NULL;
> +	}
> +
> +	if (is_valid_ether_addr(edev->ethaddr))
> +		return edev;
> +
> +	if (!edev->parent || of_get_mac_addr_nvmem(edev->parent->of_node,
> +						   oui_mac)) {
> +		char str[sizeof("xx:xx:xx:xx:xx:xx")];
> +		u64 unique_id;
> +
> +		unique_id = imx8m_uid();
> +		if (!unique_id)
> +			dev_err(priv->dev, "Failed to get i.MX8MP unique ID\n");
> +
> +		/* Generate MAC address based on i.MX8MP unique ID */
> +		oui_mac[3] = (unique_id >> 56) & 0xff;
> +		oui_mac[4] = (unique_id >> 48) & 0xff;
> +		oui_mac[5] = (unique_id >> 40) & 0xff;
> +		ethaddr_to_string(oui_mac, str);
> +		dev_warn(priv->dev, "Failed to get master eth addr. Generating based on i.MX8MP unique ID: %s\n",
> +			 str);

I attempted to solve this generically in [1]. I'd prefer to get that in instead of
board-specific measures. I also think that it might be a bad idea to expose the
unique ID like that as this allows easy brute forcing of the machine ID that's
generated by default out of the UID.

[1]: https://lore.barebox.org/barebox/20230911155927.3786335-1-a.fatoum@pengutronix.de/

> +	}
> +
> +	of_eth_register_ethaddr(np, oui_mac);
> +
> +	return edev;
> +}
> +
> +static void skov_imx8mp_register_ethaddr(struct skov_imx8mp_priv *priv,
> +					 struct device_node *root)
> +{
> +	struct eth_device *master_edev;
> +	struct eth_node_info {
> +		const char *alias;
> +		struct device_node *np;
> +		u32 inc;
> +	} eth_nodes[] = {
> +		{ "ethernet0", NULL, 0 },
> +		/* Addresses are assigned in the reverse order
> +		 * LAN2 addr < LAN1 addr
> +		 */
> +		{ "ethernet1", NULL, 1 },
> +		{ "ethernet2", NULL, 0 },
> +	};
> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(eth_nodes); i++) {
> +		eth_nodes[i].np = of_find_node_by_alias(root,
> +							eth_nodes[i].alias);
> +		if (!eth_nodes[i].np) {
> +			dev_err(priv->dev, "Failed to find %s alias\n",
> +				eth_nodes[i].alias);
> +			return;
> +		}
> +	}
> +
> +	master_edev = skov_imx8mp_init_master_edev(priv, eth_nodes[0].np);
> +	if (!master_edev) {
> +		dev_err(priv->dev, "Failed to init master edev\n");
> +		return;
> +	}
> +
> +	/* Set ETH addresses for all but master edev */
> +	for (i = 1; i < ARRAY_SIZE(eth_nodes); i++) {
> +		u8 ethaddr[ETH_ALEN];
> +
> +		ether_addr_inc(ethaddr, master_edev->ethaddr, eth_nodes[i].inc);
> +		of_eth_register_ethaddr(eth_nodes[i].np, ethaddr);
> +	}
> +}
> +
> +static int skov_imx8mp_populate_ethaddr(void)
> +{
> +	struct skov_imx8mp_priv *priv = skov_imx8mp_priv;
> +
> +	skov_imx8mp_register_ethaddr(priv, NULL);
> +
> +	return 0;
> +}
> +postenvironment_initcall(skov_imx8mp_populate_ethaddr);
> +
>  static int skov_imx8mp_fixup(struct device_node *root, void *data)
>  {
>  	struct device_node *chosen = of_create_node(root, "/chosen");

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 09/10] net: lib: add ether_addr_inc() helper
  2023-10-05  6:48     ` Ahmad Fatoum
@ 2023-10-05  7:07       ` Oleksij Rempel
  2023-10-06 11:44         ` Sascha Hauer
  0 siblings, 1 reply; 16+ messages in thread
From: Oleksij Rempel @ 2023-10-05  7:07 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: Sascha Hauer, barebox

On Thu, Oct 05, 2023 at 08:48:46AM +0200, Ahmad Fatoum wrote:
> >> +void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment)
> >> +{
> >> +	u32 value;
> >> +	int i;
> >> +
> >> +	for(i = 0; i < 6; ++i)
> >> +		dst_addr[i] = src_addr[i];
> >> +
> >> +	for(i = 5; i >= 0 && increment; --i) {
> >> +		value = dst_addr[i] + increment;
> >> +		dst_addr[i] = value & 0xFF;
> >> +		increment = value >> 8;
> >> +	}
> >> +}
> > 
> > The Kernel already has eth_addr_add() and more helpers. How about
> > adopting these instead?
> 
> I had copied them over here: https://lore.barebox.org/barebox/20230911155927.3786335-2-a.fatoum@pengutronix.de/
> They got reverted along with patch 1/3 in that series, but they have no dependency, so could be reinstated.

Ok, it looks like two last patches need some more work. I'll drop them
for now and respin it in a next time slot.

@Sascha, should I resend first 8 patches separately?

Regards,
Oleskij
-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 09/10] net: lib: add ether_addr_inc() helper
  2023-10-05  7:07       ` Oleksij Rempel
@ 2023-10-06 11:44         ` Sascha Hauer
  0 siblings, 0 replies; 16+ messages in thread
From: Sascha Hauer @ 2023-10-06 11:44 UTC (permalink / raw)
  To: Oleksij Rempel; +Cc: Ahmad Fatoum, barebox

On Thu, Oct 05, 2023 at 09:07:17AM +0200, Oleksij Rempel wrote:
> On Thu, Oct 05, 2023 at 08:48:46AM +0200, Ahmad Fatoum wrote:
> > >> +void ether_addr_inc(u8 *dst_addr, const u8 *src_addr, u32 increment)
> > >> +{
> > >> +	u32 value;
> > >> +	int i;
> > >> +
> > >> +	for(i = 0; i < 6; ++i)
> > >> +		dst_addr[i] = src_addr[i];
> > >> +
> > >> +	for(i = 5; i >= 0 && increment; --i) {
> > >> +		value = dst_addr[i] + increment;
> > >> +		dst_addr[i] = value & 0xFF;
> > >> +		increment = value >> 8;
> > >> +	}
> > >> +}
> > > 
> > > The Kernel already has eth_addr_add() and more helpers. How about
> > > adopting these instead?
> > 
> > I had copied them over here: https://lore.barebox.org/barebox/20230911155927.3786335-2-a.fatoum@pengutronix.de/
> > They got reverted along with patch 1/3 in that series, but they have no dependency, so could be reinstated.
> 
> Ok, it looks like two last patches need some more work. I'll drop them
> for now and respin it in a next time slot.
> 
> @Sascha, should I resend first 8 patches separately?

No, I just applied 1-8

Sascha


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2023-10-06 11:46 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 01/10] arm: dts: imx8mp-skov: Add reserved-memory for ramoops pstore Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 02/10] arm: dts: imx8mp-skov: Add pins for hardware variant detection Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 03/10] arm: dts: imx8mp-skov: Switch to GPT for eMMC partitioning Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 04/10] arm: dts: imx8mp-skov: Switch to GPT for SD partitioning Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 05/10] arm: dts: imx8mp-skov: Add barebox state backend in DTS Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 06/10] ARM: i.MX8MP: skov: refactor bootsource and BBU handlers Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 07/10] ARM: i.MX8MP: skov: Add hardware variant support Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 08/10] ARM: i.MX8MP: skov: fixup skov,imx8mp-board-version DT property for the kernel Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 09/10] net: lib: add ether_addr_inc() helper Oleksij Rempel
2023-10-04  6:41   ` Sascha Hauer
2023-10-05  6:48     ` Ahmad Fatoum
2023-10-05  7:07       ` Oleksij Rempel
2023-10-06 11:44         ` Sascha Hauer
2023-10-02 10:16 ` [PATCH v2 10/10] ARM: i.MX8MP: skov: assign Ethernet addresses to all ports Oleksij Rempel
2023-10-05  6:52   ` Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox