1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2011 Linaro Limited
4  * Aneesh V <aneesh@ti.com>
5  */
6 #include <common.h>
7 #include <env.h>
8 #include <part.h>
9 #include <asm/setup.h>
10 #include <asm/arch/sys_proto.h>
11 #include <asm/omap_common.h>
12 
do_cancel_out(u32 * num,u32 * den,u32 factor)13 static void do_cancel_out(u32 *num, u32 *den, u32 factor)
14 {
15 	while (1) {
16 		if (((*num)/factor*factor == (*num)) &&
17 		   ((*den)/factor*factor == (*den))) {
18 			(*num) /= factor;
19 			(*den) /= factor;
20 		} else
21 			break;
22 	}
23 }
24 
25 #ifdef CONFIG_FASTBOOT_FLASH
omap_set_fastboot_cpu(void)26 static void omap_set_fastboot_cpu(void)
27 {
28 	char *cpu;
29 	u32 cpu_rev = omap_revision();
30 
31 	switch (cpu_rev) {
32 	case DRA762_ES1_0:
33 	case DRA762_ABZ_ES1_0:
34 	case DRA762_ACD_ES1_0:
35 		cpu = "DRA762";
36 		break;
37 	case DRA752_ES1_0:
38 	case DRA752_ES1_1:
39 	case DRA752_ES2_0:
40 		cpu = "DRA752";
41 		break;
42 	case DRA722_ES1_0:
43 	case DRA722_ES2_0:
44 	case DRA722_ES2_1:
45 		cpu = "DRA722";
46 		break;
47 	default:
48 		cpu = NULL;
49 		printf("Warning: fastboot.cpu: unknown CPU rev: %u\n", cpu_rev);
50 	}
51 
52 	env_set("fastboot.cpu", cpu);
53 }
54 
omap_set_fastboot_secure(void)55 static void omap_set_fastboot_secure(void)
56 {
57 	const char *secure;
58 	u32 dev = get_device_type();
59 
60 	switch (dev) {
61 	case EMU_DEVICE:
62 		secure = "EMU";
63 		break;
64 	case HS_DEVICE:
65 		secure = "HS";
66 		break;
67 	case GP_DEVICE:
68 		secure = "GP";
69 		break;
70 	default:
71 		secure = NULL;
72 		printf("Warning: fastboot.secure: unknown CPU sec: %u\n", dev);
73 	}
74 
75 	env_set("fastboot.secure", secure);
76 }
77 
omap_set_fastboot_board_rev(void)78 static void omap_set_fastboot_board_rev(void)
79 {
80 	const char *board_rev;
81 
82 	board_rev = env_get("board_rev");
83 	if (board_rev == NULL)
84 		printf("Warning: fastboot.board_rev: unknown board revision\n");
85 
86 	env_set("fastboot.board_rev", board_rev);
87 }
88 
89 #ifdef CONFIG_FASTBOOT_FLASH_MMC
omap_mmc_get_part_size(const char * part)90 static u32 omap_mmc_get_part_size(const char *part)
91 {
92 	int res;
93 	struct blk_desc *dev_desc;
94 	struct disk_partition info;
95 	u64 sz = 0;
96 
97 	dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
98 	if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
99 		pr_err("invalid mmc device\n");
100 		return 0;
101 	}
102 
103 	/* Check only for EFI (GPT) partition table */
104 	res = part_get_info_by_name_type(dev_desc, part, &info, PART_TYPE_EFI);
105 	if (res < 0)
106 		return 0;
107 
108 	/* Calculate size in bytes */
109 	sz = (info.size * (u64)info.blksz);
110 	/* to KiB */
111 	sz >>= 10;
112 
113 	return (u32)sz;
114 }
115 
omap_set_fastboot_userdata_size(void)116 static void omap_set_fastboot_userdata_size(void)
117 {
118 	char buf[16];
119 	u32 sz_kb;
120 
121 	sz_kb = omap_mmc_get_part_size("userdata");
122 	if (sz_kb == 0)
123 		return; /* probably it's not Android partition table */
124 
125 	sprintf(buf, "%u", sz_kb);
126 	env_set("fastboot.userdata_size", buf);
127 }
128 #else
omap_set_fastboot_userdata_size(void)129 static inline void omap_set_fastboot_userdata_size(void)
130 {
131 }
132 #endif /* CONFIG_FASTBOOT_FLASH_MMC */
133 
omap_set_fastboot_product(void)134 static void omap_set_fastboot_product(void)
135 {
136 	const char *board_name;
137 
138 	board_name = env_get("board_name");
139 	if (board_name == NULL)
140 		printf("Warning: fastboot.product: unknown board\n");
141 
142 	env_set("fastboot.product", board_name);
143 }
144 
omap_set_fastboot_vars(void)145 void omap_set_fastboot_vars(void)
146 {
147 	omap_set_fastboot_cpu();
148 	omap_set_fastboot_secure();
149 	omap_set_fastboot_board_rev();
150 	omap_set_fastboot_userdata_size();
151 	omap_set_fastboot_product();
152 }
153 #endif /* CONFIG_FASTBOOT_FLASH */
154 
155 /*
156  * Cancel out the denominator and numerator of a fraction
157  * to get smaller numerator and denominator.
158  */
cancel_out(u32 * num,u32 * den,u32 den_limit)159 void cancel_out(u32 *num, u32 *den, u32 den_limit)
160 {
161 	do_cancel_out(num, den, 2);
162 	do_cancel_out(num, den, 3);
163 	do_cancel_out(num, den, 5);
164 	do_cancel_out(num, den, 7);
165 	do_cancel_out(num, den, 11);
166 	do_cancel_out(num, den, 13);
167 	do_cancel_out(num, den, 17);
168 	while ((*den) > den_limit) {
169 		*num /= 2;
170 		/*
171 		 * Round up the denominator so that the final fraction
172 		 * (num/den) is always <= the desired value
173 		 */
174 		*den = (*den + 1) / 2;
175 	}
176 }
177 
omap_die_id(unsigned int * die_id)178 __weak void omap_die_id(unsigned int *die_id)
179 {
180 	die_id[0] = die_id[1] = die_id[2] = die_id[3] = 0;
181 }
182 
omap_die_id_serial(void)183 void omap_die_id_serial(void)
184 {
185 	unsigned int die_id[4] = { 0 };
186 	char serial_string[17] = { 0 };
187 
188 	omap_die_id((unsigned int *)&die_id);
189 
190 	if (!env_get("serial#")) {
191 		snprintf(serial_string, sizeof(serial_string),
192 			"%08x%08x", die_id[0], die_id[3]);
193 
194 		env_set("serial#", serial_string);
195 	}
196 }
197 
omap_die_id_get_board_serial(struct tag_serialnr * serialnr)198 void omap_die_id_get_board_serial(struct tag_serialnr *serialnr)
199 {
200 	char *serial_string;
201 	unsigned long long serial;
202 
203 	serial_string = env_get("serial#");
204 
205 	if (serial_string) {
206 		serial = simple_strtoull(serial_string, NULL, 16);
207 
208 		serialnr->high = (unsigned int) (serial >> 32);
209 		serialnr->low = (unsigned int) (serial & 0xffffffff);
210 	} else {
211 		serialnr->high = 0;
212 		serialnr->low = 0;
213 	}
214 }
215 
omap_die_id_usbethaddr(void)216 void omap_die_id_usbethaddr(void)
217 {
218 	unsigned int die_id[4] = { 0 };
219 	unsigned char mac[6] = { 0 };
220 
221 	omap_die_id((unsigned int *)&die_id);
222 
223 	if (!env_get("usbethaddr")) {
224 		/*
225 		 * Create a fake MAC address from the processor ID code.
226 		 * First byte is 0x02 to signify locally administered.
227 		 */
228 		mac[0] = 0x02;
229 		mac[1] = die_id[3] & 0xff;
230 		mac[2] = die_id[2] & 0xff;
231 		mac[3] = die_id[1] & 0xff;
232 		mac[4] = die_id[0] & 0xff;
233 		mac[5] = (die_id[0] >> 8) & 0xff;
234 
235 		eth_env_set_enetaddr("usbethaddr", mac);
236 
237 		if (!env_get("ethaddr"))
238 			eth_env_set_enetaddr("ethaddr", mac);
239 	}
240 }
241 
omap_die_id_display(void)242 void omap_die_id_display(void)
243 {
244 	unsigned int die_id[4] = { 0 };
245 
246 	omap_die_id(die_id);
247 
248 	printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2],
249 		die_id[1], die_id[0]);
250 }
251