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