Skip to contentSkip to conversation

arm: mach-snapdragon: misc: Initialize eMMC if necessary

PATCH Superseded Stephan Gerhold 2 participants 4 messages Aug 2, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 arm: mach-snapdragon: misc: Initialize eMMC if necessary

At the moment U-Boot produces an empty MAC address (02:00:00:00:00:00) if the eMMC is not used by anything in U-Boot (e.g. with CONFIG_ENV_IS_NOWHERE=y instead of having the environment on eMMC). This happens because then there is nothing that actually initializes the eMMC and reads the "cid" that is later accessed.

To fix this, call mmc_init() to ensure the eMMC is initialized. There is no functional difference if the eMMC is already initialized since then mmc_init() will just return without doing anything.

---

 arch/arm/mach-snapdragon/misc.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm/mach-snapdragon/misc.c +3 -0
index aaa561c2c6..5c062e9636 100644
@@ -33,6 +33,9 @@ u32 msm_board_serial(void)
 	if (!mmc_dev)
 		return 0;
 
+	if (mmc_init(mmc_dev))
+		return 0;
+
 	return UNSTUFF_BITS(mmc_dev->cid, 16, 32);
 }
 
Signature · 1 line
2.32.0
#1 lore raw

PATCH 2/2 arm: mach-snapdrgon: misc: Simplify msm_generate_mac_addr()

The logic in msm_generate_mac_addr() was originally taken from the LK bootloader where the serial number is a string and must be parsed first. However, in U-Boot msm_board_serial() returns an u32 and msm_generate_mac_addr() has quite complicated code that will first print it as a hex string and then immediately parse it again.

What this function actually does at the end is to put the serial number encoded as big endian (the order used for the hex string) into the u8 *mac. Use put_unaligned_be32() to do that with bit shifts instead of going through the string format.

This should be slightly more efficient and cleaner but does not result in any functional difference.

---

 arch/arm/mach-snapdragon/misc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
arch/arm/mach-snapdragon/misc.c +3 -10
index 5c062e9636..02edfda5d6 100644
@@ -9,6 +9,7 @@
 #include <common.h>
 #include <mmc.h>
 #include <asm/arch/misc.h>
+#include <asm/unaligned.h>
 
 /* UNSTUFF_BITS macro taken from Linux Kernel: drivers/mmc/core/sd.c */
 #define UNSTUFF_BITS(resp, start, size) \
@@ -41,16 +42,8 @@ u32 msm_board_serial(void)
 
 void msm_generate_mac_addr(u8 *mac)
 {
-	int i;
-	char sn[9];
-
-	snprintf(sn, 9, "%08x", msm_board_serial());
-
 	/* fill in the mac with serialno, use locally adminstrated pool */
 	mac[0] = 0x02;
-	mac[1] = 00;
-	for (i = 3; i >= 0; i--) {
-		mac[i + 2] = simple_strtoul(&sn[2 * i], NULL, 16);
-		sn[2 * i] = 0;
-	}
+	mac[1] = 0x00;
+	put_unaligned_be32(msm_board_serial(), &mac[2]);
 }
Signature · 1 line
2.32.0
#2 lore raw

Re: [PATCH 2/2] arm: mach-snapdrgon: misc: Simplify msm_generate_mac_addr()

Quoted 54 lines · Stephan Gerhold

The logic in msm_generate_mac_addr() was originally taken from the LK bootloader where the serial number is a string and must be parsed first. However, in U-Boot msm_board_serial() returns an u32 and msm_generate_mac_addr() has quite complicated code that will first print it as a hex string and then immediately parse it again.

What this function actually does at the end is to put the serial number encoded as big endian (the order used for the hex string) into the u8 *mac. Use put_unaligned_be32() to do that with bit shifts instead of going through the string format.

This should be slightly more efficient and cleaner but does not result in any functional difference.

---

 arch/arm/mach-snapdragon/misc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
arch/arm/mach-snapdragon/misc.c +3 -11
index 5c062e9636..02edfda5d6 100644
@@ -9,6 +9,7 @@
 #include <common.h>
 #include <mmc.h>
 #include <asm/arch/misc.h>
+#include <asm/unaligned.h>
 
 /* UNSTUFF_BITS macro taken from Linux Kernel: drivers/mmc/core/sd.c */
 #define UNSTUFF_BITS(resp, start, size) \
@@ -41,16 +42,8 @@ u32 msm_board_serial(void)
 
 void msm_generate_mac_addr(u8 *mac)
 {
-       int i;
-       char sn[9];
-
-       snprintf(sn, 9, "%08x", msm_board_serial());
-
        /* fill in the mac with serialno, use locally adminstrated pool */
        mac[0] = 0x02;
-       mac[1] = 00;
-       for (i = 3; i >= 0; i--) {
-               mac[i + 2] = simple_strtoul(&sn[2 * i], NULL, 16);
-               sn[2 * i] = 0;
-       }
+       mac[1] = 0x00;
+       put_unaligned_be32(msm_board_serial(), &mac[2]);
 }
--
Quoted 2 lines

2.32.0

Please add a comment explaining the logic in the code. This is not understandable without explanation.
Thanks,
Ramon.

Reviewed-by on v1 1/2
#3 lore raw
In reply to Stephan Gerhold
Quoted 33 lines · Stephan Gerhold

At the moment U-Boot produces an empty MAC address (02:00:00:00:00:00) if the eMMC is not used by anything in U-Boot (e.g. with CONFIG_ENV_IS_NOWHERE=y instead of having the environment on eMMC). This happens because then there is nothing that actually initializes the eMMC and reads the "cid" that is later accessed.

To fix this, call mmc_init() to ensure the eMMC is initialized. There is no functional difference if the eMMC is already initialized since then mmc_init() will just return without doing anything.

---

 arch/arm/mach-snapdragon/misc.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm/mach-snapdragon/misc.c +3 -1
index aaa561c2c6..5c062e9636 100644
@@ -33,6 +33,9 @@ u32 msm_board_serial(void)
        if (!mmc_dev)
                return 0;
 
+       if (mmc_init(mmc_dev))
+               return 0;
+
        return UNSTUFF_BITS(mmc_dev->cid, 16, 32);
 }
 
--
Quoted 2 lines

2.32.0