1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Miscellaneous Snapdragon functionality
4  *
5  * (C) Copyright 2018 Ramon Fried <ramon.fried@gmail.com>
6  *
7  */
8 
9 #include <common.h>
10 #include <mmc.h>
11 #include <asm/arch/misc.h>
12 #include <asm/unaligned.h>
13 
14 /* UNSTUFF_BITS macro taken from Linux Kernel: drivers/mmc/core/sd.c */
15 #define UNSTUFF_BITS(resp, start, size) \
16 	({ \
17 		const int __size = size; \
18 		const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
19 		const int __off = 3 - ((start) / 32); \
20 		const int __shft = (start) & 31; \
21 		u32 __res; \
22 					\
23 		__res = resp[__off] >> __shft; \
24 		if (__size + __shft > 32) \
25 			__res |= resp[__off - 1] << ((32 - __shft) % 32); \
26 		__res & __mask;	\
27 	})
28 
msm_board_serial(void)29 u32 msm_board_serial(void)
30 {
31 	struct mmc *mmc_dev;
32 
33 	mmc_dev = find_mmc_device(0);
34 	if (!mmc_dev)
35 		return 0;
36 
37 	if (mmc_init(mmc_dev))
38 		return 0;
39 
40 	return UNSTUFF_BITS(mmc_dev->cid, 16, 32);
41 }
42 
msm_generate_mac_addr(u8 * mac)43 void msm_generate_mac_addr(u8 *mac)
44 {
45 	/* use locally adminstrated pool */
46 	mac[0] = 0x02;
47 	mac[1] = 0x00;
48 
49 	/*
50 	 * Put the 32-bit serial number in the last 32-bit of the MAC address.
51 	 * Use big endian order so it is consistent with the serial number
52 	 * written as a hexadecimal string, e.g. 0x1234abcd -> 02:00:12:34:ab:cd
53 	 */
54 	put_unaligned_be32(msm_board_serial(), &mac[2]);
55 }
56