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