Skip to contentSkip to conversation

board: amlogic: vim3: fix setup ethernet mac from efuse

PATCH New Artem Lapkin 3 participants 3 messages Jan 12, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 board: amlogic: vim3: fix setup ethernet mac from efuse

Fix reading built-in ethernet MAC address from efuse

NOTE: MAC is stored in ASCII format, 1bytes = 2characters by 0 offset

if mac from efuse not valid we use meson_generate_serial_ethaddr

NOTE: remake odroid-n2.c variant from Neil Armstrong <[email protected]>

---
 board/amlogic/vim3/vim3.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
board/amlogic/vim3/vim3.c +19 -3
index 824fff8262..87d9fe1f02 100644
@@ -139,26 +139,42 @@ int meson_ft_board_setup(void *blob, struct bd_info *bd)
 }
 
 #define EFUSE_MAC_OFFSET	0
-#define EFUSE_MAC_SIZE		6
+#define EFUSE_MAC_SIZE		12
+#define MAC_ADDR_LEN		6
 
 int misc_init_r(void)
 {
-	uint8_t mac_addr[EFUSE_MAC_SIZE];
+	uint8_t mac_addr[MAC_ADDR_LEN];
+	char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3];
 	ssize_t len;
 
 	meson_eth_init(PHY_INTERFACE_MODE_RGMII, 0);
 
 	if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
 		len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
-					  mac_addr, EFUSE_MAC_SIZE);
+					  efuse_mac_addr, EFUSE_MAC_SIZE);
 		if (len != EFUSE_MAC_SIZE)
 			return 0;
 
+		/* MAC is stored in ASCII format, 1bytes = 2characters */
+		for (int i = 0; i < 6; i++) {
+			tmp[0] = efuse_mac_addr[i * 2];
+			tmp[1] = efuse_mac_addr[i * 2 + 1];
+			tmp[2] = '\0';
+			mac_addr[i] = simple_strtoul(tmp, NULL, 16);
+		}
+
 		if (is_valid_ethaddr(mac_addr))
 			eth_env_set_enetaddr("ethaddr", mac_addr);
 		else
 			meson_generate_serial_ethaddr();
+
+		eth_env_get_enetaddr("ethaddr", mac_addr);
+		printf("[i] setup onboard mac %02X:%02X:%02X:%02X:%02X:%02X\n",
+			mac_addr[0],mac_addr[1],mac_addr[2],
+			mac_addr[3],mac_addr[4],mac_addr[5]);
 	}
 
 	return 0;
 }
+
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 board: amlogic: vim3: fix setup ethernet mac from efuse

Quoted 48 lines · Artem Lapkin

Fix reading built-in ethernet MAC address from efuse

NOTE: MAC is stored in ASCII format, 1bytes = 2characters by 0 offset

if mac from efuse not valid we use meson_generate_serial_ethaddr

NOTE: remake odroid-n2.c variant from Neil Armstrong <[email protected]>

---
 board/amlogic/vim3/vim3.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
board/amlogic/vim3/vim3.c +12 -3
index 824fff8262..87d9fe1f02 100644
@@ -139,26 +139,42 @@ int meson_ft_board_setup(void *blob, struct bd_info *bd)
 }
 
 #define EFUSE_MAC_OFFSET	0
-#define EFUSE_MAC_SIZE		6
+#define EFUSE_MAC_SIZE		12
+#define MAC_ADDR_LEN		6
 
 int misc_init_r(void)
 {
-	uint8_t mac_addr[EFUSE_MAC_SIZE];
+	uint8_t mac_addr[MAC_ADDR_LEN];
+	char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3];
 	ssize_t len;
 
 	meson_eth_init(PHY_INTERFACE_MODE_RGMII, 0);
 
 	if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
 		len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
-					  mac_addr, EFUSE_MAC_SIZE);
+					  efuse_mac_addr, EFUSE_MAC_SIZE);
 		if (len != EFUSE_MAC_SIZE)
 			return 0;
 
+		/* MAC is stored in ASCII format, 1bytes = 2characters */
+		for (int i = 0; i < 6; i++) {
+			tmp[0] = efuse_mac_addr[i * 2];
+			tmp[1] = efuse_mac_addr[i * 2 + 1];
+			tmp[2] = '\0';
+			mac_addr[i] = simple_strtoul(tmp, NULL, 16);
+		}

board/amlogic/vim3/vim3.c int meson_ft_board_setup(void *blob, struct bd_info *bd) · line 175

Quoted 10 lines
board/amlogic/vim3/vim3.c +6 -0
+
 		if (is_valid_ethaddr(mac_addr))
 			eth_env_set_enetaddr("ethaddr", mac_addr);
 		else
 			meson_generate_serial_ethaddr();
+
+		eth_env_get_enetaddr("ethaddr", mac_addr);
+		printf("[i] setup onboard mac %02X:%02X:%02X:%02X:%02X:%02X\n",
+			mac_addr[0],mac_addr[1],mac_addr[2],
+			mac_addr[3],mac_addr[4],mac_addr[5]);

Doesn't need to print mac. If you have to print mac address, then fix the checkpatch error, plz.

ERROR: space required after that ',' (ctx:VxV)
#62: FILE: board/amlogic/vim3/vim3.c:174:
+ mac_addr[0],mac_addr[1],mac_addr[2],

                                   ^

..

board/amlogic/vim3/vim3.c int meson_ft_board_setup(void *blob, struct bd_info *bd) · line 180

Quoted 5 lines
board/amlogic/vim3/vim3.c +1 -0
 	}
 
 	return 0;
 }
+

Not need whitespace.

Best Regards,
Jaehoon Chung

Quoted 1 line
#2 lore raw

PATCH 1/1 board: amlogic: vim3: fix setup ethernet mac from efuse

In reply to Artem Lapkin

Hi Artem,

Quoted 9 lines · Artem Lapkin

Fix reading built-in ethernet MAC address from efuse

NOTE: MAC is stored in ASCII format, 1bytes = 2characters by 0 offset

if mac from efuse not valid we use meson_generate_serial_ethaddr

NOTE: remake odroid-n2.c variant from Neil Armstrong <[email protected]>

First of all, thanks pointing again at this issue.

I've implemented reading of MAC address from EFUSE for VIM boards and I was convinced that it is stored in binary format. I've based my assumptions on the vendor code, especially the message from the vendor kernel:

[??? 4.028236] efusekeynum: 1
[??? 4.031072] efusekeyname:???????????? mac??? offset:???? 0 size:???? 6 [??? 4.037542] efuse efuse: probe OK!

and the vendors dts:

https://github.com/khadas/linux/blob/khadas-vims-4.9.y/arch/arm64/boot/dts/amlogic/kvim3_linux.dts#L506

which both points that the EFUSE key has only 6 bytes, what means binary format. I didn't check it further what is really stored there.

Now I checked again and it looks that the DTS (and thus vendor's kernel message) is incorrect, as the MAC is stored in ASCII and occupies 12 bytes in efuse. One can check that with vendor's kernel:

# cat /sys/class/efuse/userdata
0x00: 43 38 36 33 31 34 37 30 41 38 44 37 00 00 00 00
0x10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
...

This means that your patch is correct, although I would change it a bit.

BTW, the Odroid N2/C4 boards don't have MAC stored explicitly in the EFUSE, but the boards UUID. The original code use the last bytes of the UUID as the MAC address.

Quoted 34 lines
---
  board/amlogic/vim3/vim3.c | 22 +++++++++++++++++++---
  1 file changed, 19 insertions(+), 3 deletions(-)
board/amlogic/vim3/vim3.c +7 -3
index 824fff8262..87d9fe1f02 100644
@@ -139,26 +139,42 @@ int meson_ft_board_setup(void *blob, struct bd_info *bd)
  }
 
  #define EFUSE_MAC_OFFSET	0
-#define EFUSE_MAC_SIZE		6
+#define EFUSE_MAC_SIZE		12
+#define MAC_ADDR_LEN		6
 
  int misc_init_r(void)
  {
-	uint8_t mac_addr[EFUSE_MAC_SIZE];
+	uint8_t mac_addr[MAC_ADDR_LEN];
+	char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3];
  	ssize_t len;
 
  	meson_eth_init(PHY_INTERFACE_MODE_RGMII, 0);
 
  	if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
  		len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
-					  mac_addr, EFUSE_MAC_SIZE);
+					  efuse_mac_addr, EFUSE_MAC_SIZE);
  		if (len != EFUSE_MAC_SIZE)
  			return 0;
 
+		/* MAC is stored in ASCII format, 1bytes = 2characters */
+		for (int i = 0; i < 6; i++) {

board/amlogic/vim3/vim3.c int meson_ft_board_setup(void *blob, struct bd_info *bd) · line 170

Quoted 10 lines
board/amlogic/vim3/vim3.c +6 -0
+			tmp[0] = efuse_mac_addr[i * 2];
+			tmp[1] = efuse_mac_addr[i * 2 + 1];
+			tmp[2] = '\0';
+			mac_addr[i] = simple_strtoul(tmp, NULL, 16);
+		}
+
  		if (is_valid_ethaddr(mac_addr))
  			eth_env_set_enetaddr("ethaddr", mac_addr);
  		else
  			meson_generate_serial_ethaddr();

Now the above code is the same as for Odroid N2/C4, so it can be moved to arch/arm/mach-meson/board-common.c, maybe as meson_read_efuse_mac_addr() with the offset as parameter, so it can be directly used by Odroids and VIMs.

board/amlogic/vim3/vim3.c int meson_ft_board_setup(void *blob, struct bd_info *bd) · line 175

Quoted 5 lines
board/amlogic/vim3/vim3.c +5 -0
+
+		eth_env_get_enetaddr("ethaddr", mac_addr);
+		printf("[i] setup onboard mac %02X:%02X:%02X:%02X:%02X:%02X\n",
+			mac_addr[0],mac_addr[1],mac_addr[2],
+			mac_addr[3],mac_addr[4],mac_addr[5]);

I'm not really convinced that this message has to be displayed. If so, then use "%pM" for printing MAC address.

board/amlogic/vim3/vim3.c int meson_ft_board_setup(void *blob, struct bd_info *bd) · line 180

Quoted 5 lines
board/amlogic/vim3/vim3.c +1 -0
  	}
 
  	return 0;
  }
+

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland