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