From patchwork Fri Dec 6 23:04:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Niedermaier X-Patchwork-Id: 2019576 X-Patchwork-Delegate: festevam@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.a=rsa-sha256 header.s=dhelectronicscom header.b=aNjJ3yOX; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Y4n1Q4QlDz1yRL for ; Sat, 7 Dec 2024 10:06:02 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 478FD88E00; Sat, 7 Dec 2024 00:06:00 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.b="aNjJ3yOX"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2E125895E3; Sat, 7 Dec 2024 00:05:59 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mx3.securetransport.de (mx3.securetransport.de [116.203.31.6]) by phobos.denx.de (Postfix) with ESMTP id 406838973B for ; Sat, 7 Dec 2024 00:05:55 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=cniedermaier@dh-electronics.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dh-electronics.com; s=dhelectronicscom; t=1733526343; bh=sCZ5mEtcDwNkz4DS6V6DPwgtI3/n3poNedYtjmAmdZw=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=aNjJ3yOXUhudAq3VgKnpMLy9MBkOeXU41s0eJFtdxt065n7Cj/OEen3eY4ZLnPNfD RXr/VNXijGUP8fq3N8X4MTHAFLz44ubjaH3++7+I8lCDHEmDcPiXQg8Ns4sLEXK2QI AAS8LtectYUuJLZc8CJA4pBVFNXoz0An1D0rVNuXjUlcFeaSWkpXdp8PQwz4EY+GPL sxsLlximSIRmwj6WN97IVoezEvR5HoBENGXW2jS4wBcsED5APvt/ySR7yFsHssSca4 64Y/iI0bch8qrrbavfOK9T5nAfyrEUixiSdES2P8+Icu/jwxiVrTn5GGEKm06dFZOp lkvu/VtVrk4TQ== From: Christoph Niedermaier To: CC: Christoph Niedermaier , Marek Vasut , NXP i.MX U-Boot Team , Fabio Estevam , Stefano Babic , Tom Rini , Subject: [PATCH V6 1/4] arm64: dts: imx8mp: Add aliases for the access to the EEPROM ID page node Date: Sat, 7 Dec 2024 00:04:17 +0100 Message-ID: <20241206230420.54704-2-cniedermaier@dh-electronics.com> X-klartext: yes In-Reply-To: <20241206230420.54704-1-cniedermaier@dh-electronics.com> References: <20241206230420.54704-1-cniedermaier@dh-electronics.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean The new i.MX8M Plus DHCOM rev.200 is populated with M24C32-D EEPROM that contains an additional write-lockable page called ID page. Add aliases eeprom0wl and eeprom1wl for the access to the EEPROM ID page node. Signed-off-by: Christoph Niedermaier Reviewed-by: Marek Vasut --- Cc: "NXP i.MX U-Boot Team" Cc: Marek Vasut Cc: Fabio Estevam Cc: Stefano Babic Cc: Tom Rini Cc: u-boot@dh-electronics.com --- V3: - Add this patch to the series V4: - Typo in subject and commit message corrected V5: - Add Reviewed-by tag V6: - No changes --- arch/arm/dts/imx8mp-dhcom-u-boot.dtsi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/arm/dts/imx8mp-dhcom-u-boot.dtsi b/arch/arm/dts/imx8mp-dhcom-u-boot.dtsi index c065fb8299..546490a4a8 100644 --- a/arch/arm/dts/imx8mp-dhcom-u-boot.dtsi +++ b/arch/arm/dts/imx8mp-dhcom-u-boot.dtsi @@ -9,6 +9,8 @@ aliases { eeprom0 = &eeprom0; eeprom1 = &eeprom1; + eeprom0wl = &eeprom0wl; + eeprom1wl = &eeprom1wl; mmc0 = &usdhc2; /* MicroSD */ mmc1 = &usdhc3; /* eMMC */ mmc2 = &usdhc1; /* SDIO */ From patchwork Fri Dec 6 23:04:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Niedermaier X-Patchwork-Id: 2019577 X-Patchwork-Delegate: festevam@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.a=rsa-sha256 header.s=dhelectronicscom header.b=vOcc6zPS; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Y4n1Z6QVcz1yRL for ; Sat, 7 Dec 2024 10:06:10 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A919F895F1; Sat, 7 Dec 2024 00:06:05 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.b="vOcc6zPS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DE37F895F1; Sat, 7 Dec 2024 00:06:04 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mx3.securetransport.de (mx3.securetransport.de [IPv6:2a01:4f8:c0c:92be::1]) by phobos.denx.de (Postfix) with ESMTP id 6AE8A88D72 for ; Sat, 7 Dec 2024 00:06:02 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=cniedermaier@dh-electronics.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dh-electronics.com; s=dhelectronicscom; t=1733526344; bh=+dLWvJFp2+gGQexi43PfznmBwhdXiSiqbqGB79dRkJE=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=vOcc6zPSjuUfNl57Nt34gGqDGhD0wXxuN+ivI9pUGsUn3QRdnLRfcPfa/mn2ZyJqK 3H0tW2qT3qEnpjyNDDvs531lOPmY8VhbtU6YUtGgd2JtaHriOe9FvQsAfVJ8W1NUW2 2oLigB9nFa8XVWYvZczKP4pBnR3ZU7dXO/ZAuMNlNf90IAifWIqHXTr0TcFSLekDwD Uo8Cz8xb30iV4pQHCtAv5dfAvJQM6l8R6gCtQLepThQZz+5tZ4as7eeKFaWJJMUOcs jquogd6I7B8/8CuSgIM/82lSLmxPVqWmp2Tx1DcuYBnmTOn0Zfv6agG1fYL0hD/Suq Spf1SM91ahrNQ== From: Christoph Niedermaier To: CC: Christoph Niedermaier , Marek Vasut , NXP i.MX U-Boot Team , Fabio Estevam , Stefano Babic , Tom Rini , Subject: [PATCH V6 2/4] arm64: imx8mp: Read values from M24C32-D write-lockable page on DHCOM i.MX8MP Date: Sat, 7 Dec 2024 00:04:18 +0100 Message-ID: <20241206230420.54704-3-cniedermaier@dh-electronics.com> X-klartext: yes In-Reply-To: <20241206230420.54704-1-cniedermaier@dh-electronics.com> References: <20241206230420.54704-1-cniedermaier@dh-electronics.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean The new i.MX8M Plus DHCOM rev.200 is populated with M24C32-D EEPROM that contains an additional write-lockable page called ID page, which is populated with a structure containing ethernet MAC addresses, DH item number and DH serial number. Because the write-lockable page is not present on rev.100 i.MX8MP DHCOM SoM, test whether EEPROM ID page exists by setting up the i2c driver. There may be multiple EEPROMs with an ID page on this platform, always use the first one. The evaluation of the EEPROM ID page is done in two steps. First, the content is read and checked. This is done to cache the content of the EEPROM ID page. Second, the content is extracted from the EEPROM buffer by requesting it. For the ethernet MAC address the i.MX8M Plus DHCOM currently supports parsing address from multiple sources in the following priority order: 1) U-Boot environment 'ethaddr'/'eth1addr' environment variable 2) SoC OTP fuses 3) On-SoM EEPROM Add support for parsing the content of this new EEPROM ID page and place it between 2) and 3) on the priority list. The new entry is 2.5) On-SoM EEPROM write-lockable page. Signed-off-by: Christoph Niedermaier Reviewed-by: Marek Vasut --- Cc: "NXP i.MX U-Boot Team" Cc: Marek Vasut Cc: Fabio Estevam Cc: Stefano Babic Cc: Tom Rini Cc: u-boot@dh-electronics.com --- V2: - Merging the two previous patches "arm64: imx8mp: Read MAC address from M24C32-D write-lockable page on DH i.MX8MP DHCOM if available" and "arm64: imx8mp: Read item and serial number from EEPROM ID page on DH i.MX8MP DHCOM" - Split function for reading the EEPROM ID page - One for reading the content and check the header to cache the EEPROM ID page - Second for requesting values from the content buffer - Use an EEPROM buffer array in function board_late_init() to cache the EEPROM ID page content - Add namespaces for defines - Improve request values names (enum) - Add function for reading the hardware coding - Adjust the mac address function to use the EEPROM ID page content buffer - Arrange valiables in reverse xmas tree order V3: - Add #defines for the version of the EEPROM ID page data located in the header - Reference EEPROM ID page directly by new aliases - Read EEPROM content directly to the buffer by the given parameter - Give some variables a more meaningful name - Improve determination of the EEPROM ID page size - Validation of the EEPROM ID page data in the reading function - Shorten the reading function - Add namespace to all defines and enums - Use more local variable in the evaluation function for a better reading - Fix wrong return of the function __weak int dh_setup_mac_address() - Remove function for reading the hardware revision - Use printf for output throughout - After EEPROM ID page reading error call the function dh_setup_mac_address() with parameter NULL - Suppress error message for the hardware rev. 100 in case of return value -ENODEV V4: - Divide main structure into header and payload - Use of a pointer of the main structure for the handover to the functions - Improve error handling for EEPROM ID page size determination - Extend error message for the validation of the header ID - Extend error message for calculating the header checksum - Extend error message for calculating the payload checksum - Place variables fin_chr and soc_coded at the beginning of the function - Move main structure definition and defines to the header file V5: - Remove unnecessary else if when checking the EEPROM size - Inversion of the version check V6: - Fix parameter on function dh_setup_mac_address() for the DH imx6 SoM - Add Reviewed-by tag --- board/dhelectronics/common/dh_common.c | 149 +++++++++++++++++- board/dhelectronics/common/dh_common.h | 79 +++++++++- board/dhelectronics/dh_imx6/dh_imx6.c | 4 +- .../dh_imx8mp/imx8mp_dhcom_pdk2.c | 75 ++++++++- 4 files changed, 297 insertions(+), 10 deletions(-) diff --git a/board/dhelectronics/common/dh_common.c b/board/dhelectronics/common/dh_common.c index 32c50b4f0f..6a942909cc 100644 --- a/board/dhelectronics/common/dh_common.c +++ b/board/dhelectronics/common/dh_common.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "dh_common.h" @@ -30,6 +31,149 @@ int dh_get_mac_is_enabled(const char *alias) return 0; } +int dh_read_eeprom_id_page(u8 *eeprom_buffer, const char *alias) +{ + struct eeprom_id_page *eip = (struct eeprom_id_page *)eeprom_buffer; + struct udevice *dev; + size_t payload_len; + int eeprom_size; + u16 crc16_calc; + u16 crc16_eip; + u8 crc8_calc; + ofnode node; + int ret; + + node = ofnode_path(alias); + + ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, node, &dev); + if (ret) + return ret; + + eeprom_size = i2c_eeprom_size(dev); + if (eeprom_size < 0) { + printf("%s: Error getting EEPROM ID page size! ret = %d\n", __func__, ret); + return eeprom_size; + } + + if (eeprom_size == 0 || eeprom_size > DH_EEPROM_ID_PAGE_MAX_SIZE) { + eeprom_size = DH_EEPROM_ID_PAGE_MAX_SIZE; + printf("Get invalid EEPROM ID page size %d bytes! Try to read %d bytes.\n", + eeprom_size, DH_EEPROM_ID_PAGE_MAX_SIZE); + } + + ret = i2c_eeprom_read(dev, 0x0, eeprom_buffer, eeprom_size); + if (ret) { + printf("%s: Error reading EEPROM ID page! ret = %d\n", __func__, ret); + return ret; + } + + /* Validate header ID */ + if (eip->hdr.id[0] != 'D' || eip->hdr.id[1] != 'H' || eip->hdr.id[2] != 'E') { + printf("%s: Error validating header ID! (got %c%c%c (0x%02x 0x%02x 0x%02x) != expected DHE)\n", + __func__, isprint(eip->hdr.id[0]) ? eip->hdr.id[0] : '.', + isprint(eip->hdr.id[1]) ? eip->hdr.id[1] : '.', + isprint(eip->hdr.id[2]) ? eip->hdr.id[2] : '.', + eip->hdr.id[0], eip->hdr.id[1], eip->hdr.id[2]); + return -EINVAL; + } + + /* Validate header checksum */ + crc8_calc = crc8(0xff, eeprom_buffer, offsetof(struct eeprom_id_page, hdr.crc8_hdr)); + if (eip->hdr.crc8_hdr != crc8_calc) { + printf("%s: Error validating header checksum! (got 0x%02x != calc 0x%02x)\n", + __func__, eip->hdr.crc8_hdr, crc8_calc); + return -EINVAL; + } + + /* + * Validate header version + * The payload is defined by the version specified in the header. + * Currently only version 0x10 is defined, so take the length of + * the only defined payload as the payload length. + */ + if (eip->hdr.version != DH_EEPROM_ID_PAGE_V1_0) { + printf("%s: Error validating version! (0x%02X is not supported)\n", + __func__, eip->hdr.version); + return -EINVAL; + } + payload_len = sizeof(eip->pl); + + /* Validate payload checksum */ + crc16_eip = (eip->hdr.crc16_pl[1] << 8) | eip->hdr.crc16_pl[0]; + crc16_calc = crc16(0xffff, eeprom_buffer + sizeof(eip->hdr), payload_len); + if (crc16_eip != crc16_calc) { + printf("%s: Error validating data checksum! (got 0x%02x != calc 0x%02x)\n", + __func__, crc16_eip, crc16_calc); + return -EINVAL; + } + + return 0; +} + +int dh_get_value_from_eeprom_buffer(enum eip_request_values request, u8 *data, int data_len, + struct eeprom_id_page *eip) +{ + const char fin_chr = (eip->pl.item_prefix & DH_ITEM_PREFIX_FIN_BIT) ? + DH_ITEM_PREFIX_FIN_FLASHED_CHR : DH_ITEM_PREFIX_FIN_HALF_CHR; + const u8 soc_coded = eip->pl.item_prefix & 0xf; + char soc_chr; + + if (!eip) + return -EINVAL; + + /* Copy requested data */ + switch (request) { + case DH_MAC0: + if (!is_valid_ethaddr(eip->pl.mac0)) + return -EINVAL; + + if (data_len >= sizeof(eip->pl.mac0)) + memcpy(data, eip->pl.mac0, sizeof(eip->pl.mac0)); + else + return -EINVAL; + break; + case DH_MAC1: + if (!is_valid_ethaddr(eip->pl.mac1)) + return -EINVAL; + + if (data_len >= sizeof(eip->pl.mac1)) + memcpy(data, eip->pl.mac1, sizeof(eip->pl.mac1)); + else + return -EINVAL; + break; + case DH_ITEM_NUMBER: + if (data_len < 8) /* String length must be 7 characters + string termination */ + return -EINVAL; + + if (soc_coded == DH_ITEM_PREFIX_NXP) + soc_chr = DH_ITEM_PREFIX_NXP_CHR; + else if (soc_coded == DH_ITEM_PREFIX_ST) + soc_chr = DH_ITEM_PREFIX_ST_CHR; + else + return -EINVAL; + + snprintf(data, data_len, "%c%c%05d", fin_chr, soc_chr, + (eip->pl.item_num[0] << 16) | (eip->pl.item_num[1] << 8) | + eip->pl.item_num[2]); + break; + case DH_SERIAL_NUMBER: + /* + * data_len must be greater than the size of eip->pl.serial, + * because there is a string termination needed. + */ + if (data_len <= sizeof(eip->pl.serial)) + return -EINVAL; + + data[sizeof(eip->pl.serial)] = 0; + memcpy(data, eip->pl.serial, sizeof(eip->pl.serial)); + break; + default: + return -EINVAL; + } + + return 0; +} + int dh_get_mac_from_eeprom(unsigned char *enetaddr, const char *alias) { struct udevice *dev; @@ -62,7 +206,7 @@ int dh_get_mac_from_eeprom(unsigned char *enetaddr, const char *alias) return 0; } -__weak int dh_setup_mac_address(void) +__weak int dh_setup_mac_address(struct eeprom_id_page *eip) { unsigned char enetaddr[6]; @@ -72,6 +216,9 @@ __weak int dh_setup_mac_address(void) if (dh_get_mac_is_enabled("ethernet0")) return 0; + if (!dh_get_value_from_eeprom_buffer(DH_MAC0, enetaddr, sizeof(enetaddr), eip)) + return eth_env_set_enetaddr("ethaddr", enetaddr); + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) return eth_env_set_enetaddr("ethaddr", enetaddr); diff --git a/board/dhelectronics/common/dh_common.h b/board/dhelectronics/common/dh_common.h index a2de5b1553..c4693c6061 100644 --- a/board/dhelectronics/common/dh_common.h +++ b/board/dhelectronics/common/dh_common.h @@ -3,6 +3,52 @@ * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner */ +#define DH_EEPROM_ID_PAGE_MAX_SIZE 64 + +enum eip_request_values { + DH_MAC0, + DH_MAC1, + DH_ITEM_NUMBER, + DH_SERIAL_NUMBER, +}; + +/* DH item: Vendor coding */ +#define DH_ITEM_PREFIX_NXP 0x01 +#define DH_ITEM_PREFIX_NXP_CHR 'I' +#define DH_ITEM_PREFIX_ST 0x02 +#define DH_ITEM_PREFIX_ST_CHR 'S' + +/* + * DH item: Finished state coding + * Bit = 0 means half finished + * Prefix is 'H' + * Bit = 1 means finished with a customer image flashed + * Prefix is 'F' + */ +#define DH_ITEM_PREFIX_FIN_BIT BIT(7) +#define DH_ITEM_PREFIX_FIN_HALF_CHR 'H' +#define DH_ITEM_PREFIX_FIN_FLASHED_CHR 'F' + +struct eeprom_id_page { + /* Header */ + struct { + u8 id[3]; /* Identifier 'D', 'H', 'E' - 'D' is at index 0 */ + u8 version; /* 0x10 -- Version 1.0 */ + u8 crc16_pl[2]; /* Checksum payload, [1] is MSbyte */ + u8 crc8_hdr; /* Checksum header */ + } hdr; + /* Payload */ + struct { + u8 mac0[6]; + u8 mac1[6]; + u8 item_prefix; /* H/F is coded in MSbits, Vendor coding starts at LSbits */ + u8 item_num[3]; /* [2] is MSbyte */ + u8 serial[9]; /* [8] is MSbyte */ + } pl; +}; + +#define DH_EEPROM_ID_PAGE_V1_0 0x10 + /* * dh_mac_is_in_env - Check if MAC address is already set * @@ -28,9 +74,40 @@ int dh_get_mac_is_enabled(const char *alias); */ int dh_get_mac_from_eeprom(unsigned char *enetaddr, const char *alias); +/* + * dh_read_eeprom_id_page() - Read EEPROM ID page content into given buffer + * @eeprom_buffer: Buffer for EEPROM ID page content + * @alias: Alias for EEPROM ID page device tree node + * + * Read the content of the EEPROM ID page into the given buffer (parameter + * eeprom_buffer). The EEPROM ID page device is selected via alias device + * tree name (parameter alias). The data of the EEPROM ID page is verified. + * An error is returned for reading failures and invalid data. + * + * Return: 0 if OK, other value on error + */ +int dh_read_eeprom_id_page(u8 *eeprom_buffer, const char *alias); + +/* + * dh_get_value_from_eeprom_buffer() - Get value from EEPROM buffer + * @eip_request_values: Requested value as enum + * @data: Buffer where value is to be stored + * @data_len: Length of the value buffer + * @eip: Pointer to EEPROM ID page struct from which the data is parsed + * + * Gets the value specified by the parameter eip_request_values from the EEPROM + * data struct (parameter eip). The data is written to the specified data + * buffer (parameter data). If the length of the data (parameter data_len) is + * not sufficient to copy the data into the buffer, an error is returned. + * + * Return: 0 if OK, other value on error + */ +int dh_get_value_from_eeprom_buffer(enum eip_request_values request, u8 *data, int data_len, + struct eeprom_id_page *eip); + /* * dh_setup_mac_address - Try to get MAC address from various locations and write it to env * * Return: 0 if OK, other value on error */ -int dh_setup_mac_address(void); +int dh_setup_mac_address(struct eeprom_id_page *eip); diff --git a/board/dhelectronics/dh_imx6/dh_imx6.c b/board/dhelectronics/dh_imx6/dh_imx6.c index f2b14bf701..234824b38c 100644 --- a/board/dhelectronics/dh_imx6/dh_imx6.c +++ b/board/dhelectronics/dh_imx6/dh_imx6.c @@ -84,7 +84,7 @@ int board_usb_phy_mode(int port) } #endif -int dh_setup_mac_address(void) +int dh_setup_mac_address(struct eeprom_id_page *eip) { unsigned char enetaddr[6]; @@ -171,7 +171,7 @@ int board_late_init(void) u32 hw_code; char buf[16]; - dh_setup_mac_address(); + dh_setup_mac_address(NULL); hw_code = board_get_hwcode(); diff --git a/board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c b/board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c index 78aae41235..4af3cbe9fe 100644 --- a/board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c +++ b/board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c @@ -40,7 +40,7 @@ int board_phys_sdram_size(phys_size_t *size) return 0; } -static int dh_imx8_setup_ethaddr(void) +static int dh_imx8_setup_ethaddr(struct eeprom_id_page *eip) { unsigned char enetaddr[6]; @@ -53,6 +53,9 @@ static int dh_imx8_setup_ethaddr(void) if (!dh_imx_get_mac_from_fuse(enetaddr)) goto out; + if (!dh_get_value_from_eeprom_buffer(DH_MAC0, enetaddr, sizeof(enetaddr), eip)) + goto out; + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) goto out; @@ -62,7 +65,7 @@ out: return eth_env_set_enetaddr("ethaddr", enetaddr); } -static int dh_imx8_setup_eth1addr(void) +static int dh_imx8_setup_eth1addr(struct eeprom_id_page *eip) { unsigned char enetaddr[6]; @@ -75,6 +78,9 @@ static int dh_imx8_setup_eth1addr(void) if (!dh_imx_get_mac_from_fuse(enetaddr)) goto increment_out; + if (!dh_get_value_from_eeprom_buffer(DH_MAC1, enetaddr, sizeof(enetaddr), eip)) + goto out; + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom1")) goto out; @@ -95,21 +101,58 @@ out: return eth_env_set_enetaddr("eth1addr", enetaddr); } -int dh_setup_mac_address(void) +int dh_setup_mac_address(struct eeprom_id_page *eip) { int ret; - ret = dh_imx8_setup_ethaddr(); + ret = dh_imx8_setup_ethaddr(eip); if (ret) printf("%s: Unable to setup ethaddr! ret = %d\n", __func__, ret); - ret = dh_imx8_setup_eth1addr(); + ret = dh_imx8_setup_eth1addr(eip); if (ret) printf("%s: Unable to setup eth1addr! ret = %d\n", __func__, ret); return ret; } +void dh_add_item_number_and_serial_to_env(struct eeprom_id_page *eip) +{ + char *item_number_env; + char item_number[8]; /* String with 7 characters + string termination */ + char *serial_env; + char serial[10]; /* String with 9 characters + string termination */ + int ret; + + ret = dh_get_value_from_eeprom_buffer(DH_ITEM_NUMBER, item_number, sizeof(item_number), + eip); + if (ret) { + printf("%s: Unable to get DHSOM item number from EEPROM ID page! ret = %d\n", + __func__, ret); + } else { + item_number_env = env_get("dh_som_item_number"); + if (!item_number_env) + env_set("dh_som_item_number", item_number); + else if (strcmp(item_number_env, item_number)) + printf("Warning: Environment dh_som_item_number differs from EEPROM ID page value (%s != %s)\n", + item_number_env, item_number); + } + + ret = dh_get_value_from_eeprom_buffer(DH_SERIAL_NUMBER, serial, sizeof(serial), + eip); + if (ret) { + printf("%s: Unable to get DHSOM serial number from EEPROM ID page! ret = %d\n", + __func__, ret); + } else { + serial_env = env_get("dh_som_serial_number"); + if (!serial_env) + env_set("dh_som_serial_number", serial); + else if (strcmp(serial_env, serial)) + printf("Warning: Environment dh_som_serial_number differs from EEPROM ID page value (%s != %s)\n", + serial_env, serial); + } +} + int board_init(void) { return 0; @@ -117,7 +160,27 @@ int board_init(void) int board_late_init(void) { - dh_setup_mac_address(); + u8 eeprom_buffer[DH_EEPROM_ID_PAGE_MAX_SIZE] = { 0 }; + struct eeprom_id_page *eip = (struct eeprom_id_page *)eeprom_buffer; + int ret; + + ret = dh_read_eeprom_id_page(eeprom_buffer, "eeprom0wl"); + if (ret) { + /* + * The EEPROM ID page is available on SoM rev. 200 and greater. + * For SoM rev. 100 the return value will be -ENODEV. Suppress + * the error message for that, because the absence cannot be + * treated as an error. + */ + if (ret != -ENODEV) + printf("%s: Cannot read valid data from EEPROM ID page! ret = %d\n", + __func__, ret); + dh_setup_mac_address(NULL); + } else { + dh_setup_mac_address(eip); + dh_add_item_number_and_serial_to_env(eip); + } + return 0; } From patchwork Fri Dec 6 23:04:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Niedermaier X-Patchwork-Id: 2019578 X-Patchwork-Delegate: festevam@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.a=rsa-sha256 header.s=dhelectronicscom header.b=nOyYEfmV; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Y4n1l2dhtz1yRL for ; Sat, 7 Dec 2024 10:06:19 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4051489434; Sat, 7 Dec 2024 00:06:12 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.b="nOyYEfmV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 067B68953F; Sat, 7 Dec 2024 00:06:11 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mx3.securetransport.de (mx3.securetransport.de [IPv6:2a01:4f8:c0c:92be::1]) by phobos.denx.de (Postfix) with ESMTP id 0D27388D72 for ; Sat, 7 Dec 2024 00:06:09 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=cniedermaier@dh-electronics.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dh-electronics.com; s=dhelectronicscom; t=1733526345; bh=mSyoN8W2umN1lLZer/D39+AJzFgMzqhHMA0Co+iDa3s=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=nOyYEfmVVIC53ojcuwBHllZ/+Re5740wl3F1s0qEJFMCHvvvx6pqO3K+Lp0GiJko7 r4SRNJxngOW/63WlyFHNlhbuj8G9sMSZHWXTTRZlPuZmdClDm3uJNLtqd0nKPliGHD n2M4QvWbfY6VWMVO2oiTlSiNvNiYWWcruowN4CVZc+v7lpNEyeBMjn2fYAuLPBJKyF 5BEHrZAx8BXAA7TEtjgOSNRLcoegTDFoQRPilXSP6n2UudMsDTrOShSoJsOk9z67Ji Esatg9bhUlIP2vnYLlZWpqOHF19ST41/K52KmHTp07dpIWKWZc6aUcXDq/xu9mN6VG xUAs9cvNK1Cnw== From: Christoph Niedermaier To: CC: Christoph Niedermaier , Marek Vasut , Tom Rini , Simon Glass , Subject: [PATCH V6 3/4] lib: hashtable: Prevent recursive calling of callback functions Date: Sat, 7 Dec 2024 00:04:19 +0100 Message-ID: <20241206230420.54704-4-cniedermaier@dh-electronics.com> X-klartext: yes In-Reply-To: <20241206230420.54704-1-cniedermaier@dh-electronics.com> References: <20241206230420.54704-1-cniedermaier@dh-electronics.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean In case there are two variables which each implement env callback that performs env_set() on the other variable, the callbacks will call each other recursively until the stack runs out. Prevent such a recursion from happening. Example which triggers this behavior: static int on_foo(...) { env_set("bar", 0); ... } static int on_bar(...) { env_set("foo", 0); ... } U_BOOT_ENV_CALLBACK(foo, on_foo); U_BOOT_ENV_CALLBACK(bar, on_bar); Signed-off-by: Christoph Niedermaier Suggested-by: Marek Vasut --- Cc: Tom Rini Cc: Simon Glass Cc: Marek Vasut Cc: u-boot@dh-electronics.com --- V4: - Add this patch to the series v5: - No functional changes, but changed the order of the patches V6: - No changes --- lib/hashtable.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/hashtable.c b/lib/hashtable.c index e8a59e2dca..75c263b505 100644 --- a/lib/hashtable.c +++ b/lib/hashtable.c @@ -221,11 +221,32 @@ static int do_callback(const struct env_entry *e, const char *name, const char *value, enum env_op op, int flags) { + int ret = 0; + #ifndef CONFIG_XPL_BUILD - if (e->callback) - return e->callback(name, value, op, flags); + static bool in_callback; + + if (!e->callback || in_callback) + return 0; + + /* + * In case there are two variables which each implement env callback + * that performs env_set() on the other variable, the callbacks will + * call each other recursively until the stack runs out. Prevent such + * a recursion from happening. + * + * Example which triggers this behavior: + * static int on_foo(...) { env_set("bar", 0); ... } + * static int on_bar(...) { env_set("foo", 0); ... } + * U_BOOT_ENV_CALLBACK(foo, on_foo); + * U_BOOT_ENV_CALLBACK(bar, on_bar); + */ + in_callback = true; + ret = e->callback(name, value, op, flags); + in_callback = false; #endif - return 0; + + return ret; } /* From patchwork Fri Dec 6 23:04:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Niedermaier X-Patchwork-Id: 2019579 X-Patchwork-Delegate: festevam@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.a=rsa-sha256 header.s=dhelectronicscom header.b=OBnK3yVB; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Y4n1v4qkfz1yRL for ; Sat, 7 Dec 2024 10:06:27 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A8E6088DFA; Sat, 7 Dec 2024 00:06:17 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=dh-electronics.com header.i=@dh-electronics.com header.b="OBnK3yVB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9E66B88D72; Sat, 7 Dec 2024 00:06:16 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mx3.securetransport.de (mx3.securetransport.de [116.203.31.6]) by phobos.denx.de (Postfix) with ESMTP id DD4AC8974C for ; Sat, 7 Dec 2024 00:06:13 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=dh-electronics.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=cniedermaier@dh-electronics.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dh-electronics.com; s=dhelectronicscom; t=1733526346; bh=JVzB0Goe2csNr6aC41CkhmwFTVcPoebxIWkOdKB2nPM=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=OBnK3yVBYJAFQvLmgmNq1GOf+a7nUuN+xIVXk4okJExQdY0yKba6+PxjhHQ2kgzmt DVEAOw40khA1kJmtagJ4SqljWDLTmL/uK3CPAKwQ/RodMX5mZBwgBHyuJhzjsmPZV+ Pi3mz0MJaBwfXFDi6XH/vBHR28Sd81o9cFlnl9V6GN+C8FAYirbfzvhY5xmR/0el5W yJuygIGzx0DSNzLB/0+gChbnCUSyIupBFcjcBoIeZD7oL58zzUajeYpO4QztoS7u3n hU8NfrLnbmJW3/BTCRN03DO53p6SoJreb85twqrQT2STTb9YZGOyqBtkYjXYJBIDlh 5gPOgi/rK3XKw== From: Christoph Niedermaier To: CC: Christoph Niedermaier , Marek Vasut , NXP i.MX U-Boot Team , Fabio Estevam , Stefano Babic , Tom Rini , Subject: [PATCH V6 4/4] board: dhelectronics: Sync env variable dh_som_serial_number with SN Date: Sat, 7 Dec 2024 00:04:20 +0100 Message-ID: <20241206230420.54704-5-cniedermaier@dh-electronics.com> X-klartext: yes In-Reply-To: <20241206230420.54704-1-cniedermaier@dh-electronics.com> References: <20241206230420.54704-1-cniedermaier@dh-electronics.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean The env variable "SN" is used to store the serial number on DH electronics SoMs. New SoMs will use the variable "dh_som_serial_number". To ensure compatibility, these env variables are synchronized. This is achieved using callback functions. Signed-off-by: Christoph Niedermaier Reviewed-by: Marek Vasut --- Cc: "NXP i.MX U-Boot Team" Cc: Marek Vasut Cc: Fabio Estevam Cc: Stefano Babic Cc: Tom Rini Cc: u-boot@dh-electronics.com --- V3: - Add this patch to the series V4: - Remove the recursion prevention from the callback functions => The recursion prevention will be generally solved in the callback function call in a separate patch v5: - No functional changes, but changed the order of the patches V6: - Add Reviewed-by tag --- board/dhelectronics/common/dh_common.c | 17 +++++++++++++++++ configs/dhsom.config | 1 + 2 files changed, 18 insertions(+) diff --git a/board/dhelectronics/common/dh_common.c b/board/dhelectronics/common/dh_common.c index 6a942909cc..71010803f5 100644 --- a/board/dhelectronics/common/dh_common.c +++ b/board/dhelectronics/common/dh_common.c @@ -11,6 +11,23 @@ #include "dh_common.h" +static int on_dh_som_serial_number(const char *name, const char *value, enum env_op op, + int flags) +{ + env_set("SN", value); + return 0; +} + +U_BOOT_ENV_CALLBACK(dh_som_serial_number, on_dh_som_serial_number); + +static int on_SN(const char *name, const char *value, enum env_op op, int flags) +{ + env_set("dh_som_serial_number", value); + return 0; +} + +U_BOOT_ENV_CALLBACK(SN, on_SN); + bool dh_mac_is_in_env(const char *env) { unsigned char enetaddr[6]; diff --git a/configs/dhsom.config b/configs/dhsom.config index fc76632630..380bedaa5d 100644 --- a/configs/dhsom.config +++ b/configs/dhsom.config @@ -49,6 +49,7 @@ CONFIG_CMD_UUID=y CONFIG_CMD_WGET=y CONFIG_CMD_XXD=y CONFIG_CRC32_VERIFY=y +CONFIG_ENV_CALLBACK_LIST_STATIC="dh_som_serial_number:dh_som_serial_number,SN:SN," CONFIG_HASH_VERIFY=y CONFIG_MD5SUM_VERIFY=y CONFIG_SHA1SUM_VERIFY=y