Skip to contentSkip to conversation

xilinx: board: Add support for board eeproms

PATCH New Michal Simek 3 participants 6 messages Oct 14, 2020 – Oct 27, 2020 Arm/Zynq architecture

lore
Share & tools
lore
#0 lore raw

cover xilinx: board: Add support for board eeproms

Hi,

Based on discussion with Rob we should be using nvmem alias to pointing to eeprom which stores information about device. ZynqMP boards are using format which is based on offset. The code is reading it and setup environment variables based on it. They can be used within scripts to automate different behavior for specific boards.

Thanks,
Michal

Michal Simek (3):
dm: core: Add support for getting node from aliases
xilinx: board: Read the whole eeprom not just offset
xilinx: board: Add support for additional card detection

 board/xilinx/common/board.c  | 242 ++++++++++++++++++++++++++++++++++-
 board/xilinx/common/board.h  |   2 +
 board/xilinx/versal/board.c  |   3 +
 board/xilinx/zynqmp/zynqmp.c |   3 +
 drivers/core/ofnode.c        |  22 ++++
 include/dm/ofnode.h          |  22 ++++
 test/dm/ofnode.c             |  22 ++++
 7 files changed, 315 insertions(+), 1 deletion(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/3 dm: core: Add support for getting node from aliases

Add support for getting a node/property from aliases. The similar functionality is provided for chosen node and this implemenatation is copy of it.

---

 drivers/core/ofnode.c | 22 ++++++++++++++++++++++
 include/dm/ofnode.h   | 22 ++++++++++++++++++++++
 test/dm/ofnode.c      | 22 ++++++++++++++++++++++
 3 files changed, 66 insertions(+)
drivers/core/ofnode.c +22 -0
index 7d1b89514c7d..a68076bf3517 100644
@@ -476,6 +476,28 @@ ofnode ofnode_get_chosen_node(const char *name)
 	return ofnode_path(prop);
 }
 
+const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
+{
+	ofnode node;
+
+	node = ofnode_path("/aliases");
+
+	return ofnode_read_prop(node, propname, sizep);
+}
+
+ofnode ofnode_get_aliases_node(const char *name)
+{
+	const char *prop;
+
+	prop = ofnode_read_aliases_prop(name, NULL);
+	if (!prop)
+		return ofnode_null();
+
+	debug("%s: node_path: %s\n", __func__, prop);
+
+	return ofnode_path(prop);
+}
+
 int ofnode_get_child_count(ofnode parent)
 {
 	ofnode child;
include/dm/ofnode.h +22 -0
index 4b7af3705601..ced7f6ffb250 100644
@@ -605,6 +605,28 @@ const char *ofnode_read_chosen_string(const char *propname);
  */
 ofnode ofnode_get_chosen_node(const char *propname);
 
+/**
+ * ofnode_read_aliases_prop() - get the value of a aliases property
+ *
+ * This looks for a property within the /aliases node and returns its value
+ *
+ * @propname: Property name to look for
+ * @sizep: Returns size of property, or FDT_ERR_... error code if function
+ *	returns NULL
+ * @return property value if found, else NULL
+ */
+const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
+
+/**
+ * ofnode_get_aliases_node() - get a referenced node from the aliases node
+ *
+ * This looks up a named property in the aliases node and uses that as a path to
+ * look up a code.
+ *
+ * @return the referenced node if present, else ofnode_null()
+ */
+ofnode ofnode_get_aliases_node(const char *propname);
+
 struct display_timing;
 /**
  * ofnode_decode_display_timing() - decode display timings
test/dm/ofnode.c +22 -0
index 01ac3c2094c8..fb1ceb131805 100644
@@ -207,6 +207,28 @@ static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
 
+static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
+{
+	const void *val;
+	ofnode node;
+	int size;
+
+	node = ofnode_get_aliases_node("eth3");
+	ut_assert(ofnode_valid(node));
+	ut_asserteq_str("sbe5", ofnode_get_name(node));
+
+	node = ofnode_get_aliases_node("unknown");
+	ut_assert(!ofnode_valid(node));
+
+	val = ofnode_read_aliases_prop("spi0", &size);
+	ut_assertnonnull(val);
+	ut_asserteq(7, size);
+	ut_asserteq_str("/spi at 0", (const char *)val);
+
+	return 0;
+}
+DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
 {
 	ofnode node, child_node;
Signature · 1 line
2.28.0
#2 lore raw

PATCH 1/3 dm: core: Add support for getting node from aliases

Quoted 5 lines · Michal Simek

Add support for getting a node/property from aliases. The similar functionality is provided for chosen node and this implemenatation is copy of it.

Quoted 8 lines

---

 drivers/core/ofnode.c | 22 ++++++++++++++++++++++
 include/dm/ofnode.h   | 22 ++++++++++++++++++++++
 test/dm/ofnode.c      | 22 ++++++++++++++++++++++
 3 files changed, 66 insertions(+)
#3 lore raw

PATCH 2/3 xilinx: board: Read the whole eeprom not just offset

In reply to Michal Simek

Starts to use new way how eeproms should be referenced. Reference is done via nvmem alias nodes. When this new way is specified code itself read the eeprom and decode xilinx legacy format and fill struct xilinx_board_description. Then based on information present there board_* variables are setup.
If variables are saved and content can't be changed information is just shown on console.

---

 board/xilinx/common/board.c  | 205 ++++++++++++++++++++++++++++++++++-
 board/xilinx/common/board.h  |   2 +
 board/xilinx/versal/board.c  |   3 +
 board/xilinx/zynqmp/zynqmp.c |   3 +
 4 files changed, 212 insertions(+), 1 deletion(-)
board/xilinx/common/board.c +204 -1
index b0f60c40a5c8..0aed84546d41 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
- * (C) Copyright 2014 - 2019 Xilinx, Inc.
+ * (C) Copyright 2014 - 2020 Xilinx, Inc.
  * Michal Simek <[email protected]>
  */
 
@@ -11,7 +11,11 @@
 #include <dm/uclass.h>
 #include <i2c.h>
 #include <linux/sizes.h>
+#include <malloc.h>
 #include "board.h"
+#include <dm.h>
+#include <i2c_eeprom.h>
+#include <net.h>
 
 #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
@@ -41,6 +45,180 @@ int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
 }
 #endif
 
+#define EEPROM_HEADER_MAGIC		0xdaaddeed
+#define EEPROM_HDR_MANUFACTURER_LEN	16
+#define EEPROM_HDR_NAME_LEN		16
+#define EEPROM_HDR_REV_LEN		8
+#define EEPROM_HDR_SERIAL_LEN		20
+#define EEPROM_HDR_NO_OF_MAC_ADDR	4
+#define EEPROM_HDR_ETH_ALEN		ETH_ALEN
+
+struct xilinx_board_description {
+	u32 header;
+	char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
+	char name[EEPROM_HDR_NAME_LEN + 1];
+	char revision[EEPROM_HDR_REV_LEN + 1];
+	char serial[EEPROM_HDR_SERIAL_LEN + 1];
+	u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
+};
+
+static struct xilinx_board_description *board_info;
+
+#define XILINX_I2C_DETECTION_BITS	8
+
+/* Variable which stores pointer to array which stores eeprom content */
+struct xilinx_legacy_format {
+	char board_sn[18]; /* 0x0 */
+	char unused0[14]; /* 0x12 */
+	char eth_mac[6]; /* 0x20 */
+	char unused1[170]; /* 0x26 */
+	char board_name[11]; /* 0xd0 */
+	char unused2[5]; /* 0xdc */
+	char board_revision[3]; /* 0xe0 */
+	char unused3[29]; /* 0xe3 */
+};
+
+static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
+{
+	int i;
+	char byte;
+
+	for (i = 0; i < size; i++) {
+		byte = eeprom[i];
+
+		/* Remove all ffs and spaces */
+		if (byte == 0xff || byte == ' ')
+			eeprom[i] = 0;
+
+		/* Convert strings to lower case */
+		if (byte >= 'A' && byte <= 'Z')
+			eeprom[i] = byte + 'a' - 'A';
+	}
+}
+
+static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
+				     struct xilinx_board_description *desc)
+{
+	int ret, size;
+	struct xilinx_legacy_format *eeprom_content;
+	bool eth_valid = false;
+
+	size = sizeof(*eeprom_content);
+
+	eeprom_content = calloc(1, size);
+	if (!eeprom_content)
+		return -ENOMEM;
+
+	debug("%s: I2C EEPROM read pass data at %p\n", __func__,
+	      eeprom_content);
+
+	ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
+	if (ret) {
+		debug("%s: I2C EEPROM read failed\n", __func__);
+		free(eeprom_content);
+		return ret;
+	}
+
+	xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
+
+	printf("Xilinx I2C Legacy format@%s:\n", name);
+	printf(" Board name:\t%s\n", eeprom_content->board_name);
+	printf(" Board rev:\t%s\n", eeprom_content->board_revision);
+	printf(" Board SN:\t%s\n", eeprom_content->board_sn);
+
+	eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
+	if (eth_valid)
+		printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
+
+	/* Terminating \0 chars ensure end of string */
+	strcpy(desc->name, eeprom_content->board_name);
+	strcpy(desc->revision, eeprom_content->board_revision);
+	strcpy(desc->serial, eeprom_content->board_sn);
+	if (eth_valid)
+		memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
+
+	desc->header = EEPROM_HEADER_MAGIC;
+
+	free(eeprom_content);
+
+	return ret;
+}
+
+static bool xilinx_detect_legacy(u8 *buffer)
+{
+	int i;
+	char c;
+
+	for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
+		c = buffer[i];
+
+		if (c < '0' || c > '9')
+			return false;
+	}
+
+	return true;
+}
+
+static int xilinx_read_eeprom_single(char *name,
+				     struct xilinx_board_description *desc)
+{
+	int ret;
+	struct udevice *dev;
+	ofnode eeprom;
+	u8 buffer[XILINX_I2C_DETECTION_BITS];
+
+	eeprom = ofnode_get_aliases_node(name);
+	if (!ofnode_valid(eeprom))
+		return -ENODEV;
+
+	ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
+	if (ret)
+		return ret;
+
+	ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
+	if (ret) {
+		debug("%s: I2C EEPROM read failed\n", __func__);
+		return ret;
+	}
+
+	debug("%s: i2c memory detected: %s\n", __func__, name);
+
+	if (xilinx_detect_legacy(buffer))
+		return xilinx_read_eeprom_legacy(dev, name, desc);
+
+	return -ENODEV;
+}
+
+__maybe_unused int xilinx_read_eeprom(void)
+{
+	int id, highest_id;
+	char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
+
+	highest_id = dev_read_alias_highest_id("nvmem");
+	/* No nvmem aliases present */
+	if (highest_id < 0)
+		return -EINVAL;
+
+	board_info = calloc(1, sizeof(*board_info));
+	if (!board_info)
+		return -ENOMEM;
+
+	debug("%s: Highest ID %d, board_info %p\n", __func__,
+	      highest_id, board_info);
+
+	for (id = 0; id <= highest_id; id++) {
+		snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
+
+		/* Ignoring return value for supporting multiple chips */
+		xilinx_read_eeprom_single(name_buf, board_info);
+	}
+
+	if (board_info->header != EEPROM_HEADER_MAGIC)
+		free(board_info);
+
+	return 0;
+}
+
 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
 void *board_fdt_blob_setup(void)
 {
@@ -78,6 +256,7 @@ void *board_fdt_blob_setup(void)
 int board_late_init_xilinx(void)
 {
 	u32 ret = 0;
+	int i;
 	phys_size_t bootm_size = gd->ram_size;
 
 	if (CONFIG_IS_ENABLED(ARCH_ZYNQ))
@@ -88,6 +267,30 @@ int board_late_init_xilinx(void)
 	ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
 	ret |= env_set_addr("bootm_size", (void *)bootm_size);
 
+	if (board_info) {
+		if (board_info->manufacturer)
+			ret |= env_set("manufacturer",
+				       board_info->manufacturer);
+		if (board_info->name)
+			ret |= env_set("board_name", board_info->name);
+		if (board_info->revision)
+			ret |= env_set("board_rev", board_info->revision);
+		if (board_info->serial)
+			ret |= env_set("board_serial", board_info->serial);
+
+		for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
+			if (!board_info->mac_addr[i])
+				continue;
+
+			if (!CONFIG_IS_ENABLED(NET))
+				continue;
+
+			if (is_valid_ethaddr((const u8 *)board_info->mac_addr[i]))
+				ret |= eth_env_set_enetaddr_by_index("eth", i,
+						board_info->mac_addr[i]);
+		}
+	}
+
 	if (ret)
 		printf("%s: Saving run time variables FAILED\n", __func__);
 
board/xilinx/common/board.h +2 -0
index 180dfbca1082..69e642429b85 100644
@@ -9,4 +9,6 @@
 
 int board_late_init_xilinx(void);
 
+int xilinx_read_eeprom(void);
+
 #endif /* BOARD_XILINX_COMMON_BOARD_H */
board/xilinx/versal/board.c +3 -0
index a5ca4ca87401..912c1143a8ad 100644
@@ -36,6 +36,9 @@ int board_init(void)
 	fpga_add(fpga_xilinx, &versalpl);
 #endif
 
+	if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM))
+		xilinx_read_eeprom();
+
 	return 0;
 }
 
board/xilinx/zynqmp/zynqmp.c +3 -0
index 362c6e3dd6fb..db3f5978a928 100644
@@ -324,6 +324,9 @@ int board_init(void)
 	if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
 		zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
 						zynqmp_pm_cfg_obj_size);
+#else
+	if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM))
+		xilinx_read_eeprom();
 #endif
 
 	printf("EL Level:\tEL%d\n", current_el());
Signature · 1 line
2.28.0
#4 lore raw

PATCH 3/3 xilinx: board: Add support for additional card detection

In reply to Michal Simek

The most of Xilinx evaluation boards have FMC connectors which contain small eeprom for card identification. That's why read content of eeprom and record it.
Also generate cardX_ variables for easier script handling.

---

 board/xilinx/common/board.c | 85 ++++++++++++++++++++++++++-----------
 1 file changed, 61 insertions(+), 24 deletions(-)
board/xilinx/common/board.c +61 -24
index 0aed84546d41..0d0c21ca3d40 100644
@@ -62,7 +62,8 @@ struct xilinx_board_description {
 	u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
 };
 
-static struct xilinx_board_description *board_info;
+static int highest_id = -1;
+static struct xilinx_board_description **board_info;
 
 #define XILINX_I2C_DETECTION_BITS	8
 
@@ -191,15 +192,16 @@ static int xilinx_read_eeprom_single(char *name,
 
 __maybe_unused int xilinx_read_eeprom(void)
 {
-	int id, highest_id;
+	int id, ret;
 	char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
+	struct xilinx_board_description *desc;
 
 	highest_id = dev_read_alias_highest_id("nvmem");
 	/* No nvmem aliases present */
 	if (highest_id < 0)
 		return -EINVAL;
 
-	board_info = calloc(1, sizeof(*board_info));
+	board_info = calloc(1, sizeof(desc) * highest_id);
 	if (!board_info)
 		return -ENOMEM;
 
@@ -209,12 +211,28 @@ __maybe_unused int xilinx_read_eeprom(void)
 	for (id = 0; id <= highest_id; id++) {
 		snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
 
+		/* Alloc structure */
+		desc = board_info[id];
+		if (!desc) {
+			desc = calloc(1, sizeof(*desc));
+			if (!desc)
+				return -ENOMEM;
+
+			board_info[id] = desc;
+		}
+
 		/* Ignoring return value for supporting multiple chips */
-		xilinx_read_eeprom_single(name_buf, board_info);
+		ret = xilinx_read_eeprom_single(name_buf, desc);
+		if (ret) {
+			free(desc);
+			board_info[id] = NULL;
+		}
 	}
 
-	if (board_info->header != EEPROM_HEADER_MAGIC)
-		free(board_info);
+	/*
+	 * Consider to clean board_info structure when board/cards are not
+	 * detected.
+	 */
 
 	return 0;
 }
@@ -253,10 +271,23 @@ void *board_fdt_blob_setup(void)
 }
 #endif
 
+static int env_set_by_index(const char *name, int index, char *data)
+{
+	char var[32];
+
+	if (!index)
+		sprintf(var, "board_%s", name);
+	else
+		sprintf(var, "card%d_%s", index, name);
+
+	return env_set(var, data);
+}
+
 int board_late_init_xilinx(void)
 {
 	u32 ret = 0;
-	int i;
+	int i, id, macid = 0;
+	struct xilinx_board_description *desc;
 	phys_size_t bootm_size = gd->ram_size;
 
 	if (CONFIG_IS_ENABLED(ARCH_ZYNQ))
@@ -267,27 +298,33 @@ int board_late_init_xilinx(void)
 	ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
 	ret |= env_set_addr("bootm_size", (void *)bootm_size);
 
-	if (board_info) {
-		if (board_info->manufacturer)
-			ret |= env_set("manufacturer",
-				       board_info->manufacturer);
-		if (board_info->name)
-			ret |= env_set("board_name", board_info->name);
-		if (board_info->revision)
-			ret |= env_set("board_rev", board_info->revision);
-		if (board_info->serial)
-			ret |= env_set("board_serial", board_info->serial);
-
-		for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
-			if (!board_info->mac_addr[i])
-				continue;
+	for (id = 0; id <= highest_id; id++) {
+		desc = board_info[id];
+		if (desc && desc->header == EEPROM_HEADER_MAGIC) {
+			if (desc->manufacturer[0])
+				ret |= env_set_by_index("manufacturer", id,
+							desc->manufacturer);
+			if (desc->name[0])
+				ret |= env_set_by_index("name", id,
+							desc->name);
+			if (desc->revision[0])
+				ret |= env_set_by_index("rev", id,
+							desc->revision);
+			if (desc->serial[0])
+				ret |= env_set_by_index("serial", id,
+							desc->serial);
 
 			if (!CONFIG_IS_ENABLED(NET))
 				continue;
 
-			if (is_valid_ethaddr((const u8 *)board_info->mac_addr[i]))
-				ret |= eth_env_set_enetaddr_by_index("eth", i,
-						board_info->mac_addr[i]);
+			for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
+				if (!desc->mac_addr[i])
+					continue;
+
+				if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
+					ret |= eth_env_set_enetaddr_by_index("eth",
+							macid++, desc->mac_addr[i]);
+			}
 		}
 	}
 
Signature · 1 line
2.28.0
Applied
#5 lore raw

cover xilinx: board: Add support for board eeproms

In reply to Michal Simek

st 14. 10. 2020 v 15:55 odes?latel Michal Simek
<[email protected]> napsal:

Quoted 27 lines

Hi,

Based on discussion with Rob we should be using nvmem alias to pointing to eeprom which stores information about device. ZynqMP boards are using format which is based on offset. The code is reading it and setup environment variables based on it. They can be used within scripts to automate different behavior for specific boards.

Thanks,
Michal

Michal Simek (3):
dm: core: Add support for getting node from aliases
xilinx: board: Read the whole eeprom not just offset
xilinx: board: Add support for additional card detection

 board/xilinx/common/board.c  | 242 ++++++++++++++++++++++++++++++++++-
 board/xilinx/common/board.h  |   2 +
 board/xilinx/versal/board.c  |   3 +
 board/xilinx/zynqmp/zynqmp.c |   3 +
 drivers/core/ofnode.c        |  22 ++++
 include/dm/ofnode.h          |  22 ++++
 test/dm/ofnode.c             |  22 ++++
 7 files changed, 315 insertions(+), 1 deletion(-)
Quoted 3 lines

--
2.28.0

Applied.
M

Signature · 5 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze
Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs
U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs