arm64: imx8mp: Read values from M24C32-D write-lockable page on DHCOM i.MX8MP
Subsystems (4)
Christoph Niedermaier
maintainerv5 cover arm64: imx8mp: Read values from M24C32-D write-lockable page on DHCOM i.MX8MP
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.
This patch series prepare the device tree of the DHCOM i.MX8MP for reading the EEPROM ID page. Add the reading of the content and set the correspondent environment variable. It also synchronize two serial number environment variable via callback functions and fixing recursive calling of them.
To ensure that the environment callback functions work, this patch series depends on patch [1].
[1] https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
Christoph Niedermaier (4):
arm64: dts: imx8mp: Add aliases for the access to the EEPROM ID page
node
arm64: imx8mp: Read values from M24C32-D write-lockable page on DHCOM
i.MX8MP
lib: hashtable: Prevent recursive calling of callback functions
board: dhelectronics: Sync env variable dh_som_serial_number with SN
arch/arm/dts/imx8mp-dhcom-u-boot.dtsi | 2 + board/dhelectronics/common/dh_common.c | 166 +++++++++++++++++- board/dhelectronics/common/dh_common.h | 79 ++++++++- .../dh_imx8mp/imx8mp_dhcom_pdk2.c | 75 +++++++- configs/dhsom.config | 1 + lib/hashtable.c | 27 ++- 6 files changed, 339 insertions(+), 11 deletions(-)
---
Cc: "NXP i.MX U-Boot Team" <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Stefano Babic <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
Signature · 1 line
2.30.2
Christoph Niedermaier
maintainerPATCH v5 1/4 arm64: dts: imx8mp: Add aliases for the access to the EEPROM ID page node
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.
---
Cc: "NXP i.MX U-Boot Team" <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Stefano Babic <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
---
V3: - Add this patch to the series
V4: - Typo in subject and commit message corrected
V5: - Add Reviewed-by tag
--- arch/arm/dts/imx8mp-dhcom-u-boot.dtsi | 2 ++ 1 file changed, 2 insertions(+)
arch/arm/dts/imx8mp-dhcom-u-boot.dtsi +2 -0
@@ -9,6 +9,8 @@ aliases { eeprom0 = &eeprom0; eeprom1 = &eeprom1; + eeprom0wl = &eeprom0wl; + eeprom1wl = &eeprom1wl; mmc0 = &usdhc2; /* MicroSD */ mmc1 = &usdhc3; /* eMMC */ mmc2 = &usdhc1; /* SDIO */
Signature · 1 line
2.30.2
Christoph Niedermaier
maintainerPATCH v5 2/4 arm64: imx8mp: Read values from M24C32-D write-lockable page on DHCOM i.MX8MP
In reply to Christoph NiedermaierThe 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.
---
Cc: "NXP i.MX U-Boot Team" <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Stefano Babic <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
---
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
--- board/dhelectronics/common/dh_common.c | 149 +++++++++++++++++- board/dhelectronics/common/dh_common.h | 79 +++++++++- .../dh_imx8mp/imx8mp_dhcom_pdk2.c | 75 ++++++++- 3 files changed, 295 insertions(+), 8 deletions(-)
board/dhelectronics/common/dh_common.c +148 -1
@@ -7,6 +7,7 @@#include <dm.h> #include <i2c_eeprom.h> #include <net.h> +#include <u-boot/crc.h> #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);
board/dhelectronics/common/dh_common.h +78 -1
93 lines not on this page · raw message
board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c +69 -6
123 lines not on this page · raw message
Signature · 1 line
2.30.2
Quoted 21 lines · Christoph Niedermaier
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
Quoted 5 lines
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 <[email protected]>
Christoph Niedermaier
maintainerPATCH v5 3/4 lib: hashtable: Prevent recursive calling of callback functions
In reply to Christoph NiedermaierIn 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);
---
Cc: Tom Rini <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: [email protected]
---
V4: - Add this patch to the series
v5: - No functional changes, but changed the order of the patches
--- lib/hashtable.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
lib/hashtable.c +24 -3
@@ -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; }/*
Signature · 1 line
2.30.2
Christoph Niedermaier
maintainerPATCH v5 4/4 board: dhelectronics: Sync env variable dh_som_serial_number with SN
In reply to Christoph NiedermaierThe 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.
---
Cc: "NXP i.MX U-Boot Team" <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Stefano Babic <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
---
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
--- board/dhelectronics/common/dh_common.c | 17 +++++++++++++++++ configs/dhsom.config | 1 + 2 files changed, 18 insertions(+)
board/dhelectronics/common/dh_common.c +17 -0
@@ -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 ];
configs/dhsom.config +1 -0
@@ -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
Signature · 1 line
2.30.2
Re: [PATCH V5 4/4] board: dhelectronics: Sync env variable dh_som_serial_number with SN
Quoted 6 lines · Christoph Niedermaier
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 <[email protected]>