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