blob: 7d938724f841d8f248f5b59ffce5d951d3df45b3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Aneesh Vc0e88522011-07-21 09:10:12 -04002/*
3 * Copyright 2011 Linaro Limited
4 * Aneesh V <aneesh@ti.com>
Aneesh Vc0e88522011-07-21 09:10:12 -04005 */
6#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06007#include <env.h>
Simon Glass655306c2020-05-10 11:39:58 -06008#include <part.h>
Simon Glassd9a766f2017-05-17 08:23:00 -06009#include <asm/setup.h>
Nishanth Menon92adeb62014-03-28 11:00:04 -050010#include <asm/arch/sys_proto.h>
Sam Protsenkobfac4af2018-02-27 23:45:03 +020011#include <asm/omap_common.h>
12
Aneesh Vc0e88522011-07-21 09:10:12 -040013static 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
Semen Protsenkof2817372017-05-22 19:16:40 +030025#ifdef CONFIG_FASTBOOT_FLASH
26static void omap_set_fastboot_cpu(void)
27{
28 char *cpu;
29 u32 cpu_rev = omap_revision();
30
31 switch (cpu_rev) {
Praneeth Bajjuri177c65a2017-10-12 22:47:05 -050032 case DRA762_ES1_0:
Praneeth Bajjuri14811f82018-04-25 15:56:34 -050033 case DRA762_ABZ_ES1_0:
34 case DRA762_ACD_ES1_0:
Praneeth Bajjuri177c65a2017-10-12 22:47:05 -050035 cpu = "DRA762";
36 break;
Semen Protsenkof2817372017-05-22 19:16:40 +030037 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:
Vishal Mahaveer538264d2017-10-12 22:47:04 -050044 case DRA722_ES2_1:
Semen Protsenkof2817372017-05-22 19:16:40 +030045 cpu = "DRA722";
46 break;
47 default:
48 cpu = NULL;
49 printf("Warning: fastboot.cpu: unknown CPU rev: %u\n", cpu_rev);
50 }
51
Simon Glass6a38e412017-08-03 12:22:09 -060052 env_set("fastboot.cpu", cpu);
Semen Protsenkof2817372017-05-22 19:16:40 +030053}
54
55static 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
Simon Glass6a38e412017-08-03 12:22:09 -060075 env_set("fastboot.secure", secure);
Semen Protsenkof2817372017-05-22 19:16:40 +030076}
77
78static void omap_set_fastboot_board_rev(void)
79{
80 const char *board_rev;
81
Simon Glass64b723f2017-08-03 12:22:12 -060082 board_rev = env_get("board_rev");
Semen Protsenkof2817372017-05-22 19:16:40 +030083 if (board_rev == NULL)
84 printf("Warning: fastboot.board_rev: unknown board revision\n");
85
Simon Glass6a38e412017-08-03 12:22:09 -060086 env_set("fastboot.board_rev", board_rev);
Semen Protsenkof2817372017-05-22 19:16:40 +030087}
88
Alex Kiernan8cb1eab2018-05-29 15:30:42 +000089#ifdef CONFIG_FASTBOOT_FLASH_MMC
Semen Protsenkof2817372017-05-22 19:16:40 +030090static u32 omap_mmc_get_part_size(const char *part)
91{
92 int res;
93 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060094 struct disk_partition info;
Semen Protsenkof2817372017-05-22 19:16:40 +030095 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) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090099 pr_err("invalid mmc device\n");
Semen Protsenkof2817372017-05-22 19:16:40 +0300100 return 0;
101 }
102
Heinrich Schuchardt861cb612023-07-16 13:34:44 +0200103 res = part_get_info_by_name(dev_desc, part, &info);
Sam Protsenko77ebe362017-09-21 23:01:00 +0300104 if (res < 0)
Semen Protsenkof2817372017-05-22 19:16:40 +0300105 return 0;
Semen Protsenkof2817372017-05-22 19:16:40 +0300106
107 /* Calculate size in bytes */
108 sz = (info.size * (u64)info.blksz);
109 /* to KiB */
110 sz >>= 10;
111
112 return (u32)sz;
113}
114
115static void omap_set_fastboot_userdata_size(void)
116{
117 char buf[16];
118 u32 sz_kb;
119
120 sz_kb = omap_mmc_get_part_size("userdata");
Sam Protsenko77ebe362017-09-21 23:01:00 +0300121 if (sz_kb == 0)
122 return; /* probably it's not Android partition table */
Semen Protsenkof2817372017-05-22 19:16:40 +0300123
Sam Protsenko77ebe362017-09-21 23:01:00 +0300124 sprintf(buf, "%u", sz_kb);
Simon Glass6a38e412017-08-03 12:22:09 -0600125 env_set("fastboot.userdata_size", buf);
Semen Protsenkof2817372017-05-22 19:16:40 +0300126}
127#else
128static inline void omap_set_fastboot_userdata_size(void)
129{
130}
Alex Kiernan8cb1eab2018-05-29 15:30:42 +0000131#endif /* CONFIG_FASTBOOT_FLASH_MMC */
Sam Protsenkobedcf522019-07-25 20:11:53 +0300132
133static void omap_set_fastboot_product(void)
134{
135 const char *board_name;
136
137 board_name = env_get("board_name");
138 if (board_name == NULL)
139 printf("Warning: fastboot.product: unknown board\n");
140
141 env_set("fastboot.product", board_name);
142}
143
Semen Protsenkof2817372017-05-22 19:16:40 +0300144void omap_set_fastboot_vars(void)
145{
146 omap_set_fastboot_cpu();
147 omap_set_fastboot_secure();
148 omap_set_fastboot_board_rev();
149 omap_set_fastboot_userdata_size();
Sam Protsenkobedcf522019-07-25 20:11:53 +0300150 omap_set_fastboot_product();
Semen Protsenkof2817372017-05-22 19:16:40 +0300151}
152#endif /* CONFIG_FASTBOOT_FLASH */
153
Aneesh Vc0e88522011-07-21 09:10:12 -0400154/*
155 * Cancel out the denominator and numerator of a fraction
156 * to get smaller numerator and denominator.
157 */
158void cancel_out(u32 *num, u32 *den, u32 den_limit)
159{
160 do_cancel_out(num, den, 2);
161 do_cancel_out(num, den, 3);
162 do_cancel_out(num, den, 5);
163 do_cancel_out(num, den, 7);
164 do_cancel_out(num, den, 11);
165 do_cancel_out(num, den, 13);
166 do_cancel_out(num, den, 17);
167 while ((*den) > den_limit) {
168 *num /= 2;
169 /*
170 * Round up the denominator so that the final fraction
171 * (num/den) is always <= the desired value
172 */
173 *den = (*den + 1) / 2;
174 }
175}
Nishanth Menon92adeb62014-03-28 11:00:04 -0500176
Paul Kocialkowskie0cfa452015-08-27 19:37:08 +0200177__weak void omap_die_id(unsigned int *die_id)
178{
179 die_id[0] = die_id[1] = die_id[2] = die_id[3] = 0;
180}
181
Paul Kocialkowski2edadee2015-08-27 19:37:12 +0200182void omap_die_id_serial(void)
Nishanth Menon92adeb62014-03-28 11:00:04 -0500183{
Paul Kocialkowski2edadee2015-08-27 19:37:12 +0200184 unsigned int die_id[4] = { 0 };
185 char serial_string[17] = { 0 };
Nishanth Menon92adeb62014-03-28 11:00:04 -0500186
Paul Kocialkowski2edadee2015-08-27 19:37:12 +0200187 omap_die_id((unsigned int *)&die_id);
Nishanth Menon92adeb62014-03-28 11:00:04 -0500188
Simon Glass64b723f2017-08-03 12:22:12 -0600189 if (!env_get("serial#")) {
Paul Kocialkowski2edadee2015-08-27 19:37:12 +0200190 snprintf(serial_string, sizeof(serial_string),
191 "%08x%08x", die_id[0], die_id[3]);
192
Simon Glass6a38e412017-08-03 12:22:09 -0600193 env_set("serial#", serial_string);
Nishanth Menon92adeb62014-03-28 11:00:04 -0500194 }
195}
Dileep Katta7354dfc2015-03-25 04:04:51 +0530196
Paul Kocialkowskia7267d22015-08-27 19:37:14 +0200197void omap_die_id_get_board_serial(struct tag_serialnr *serialnr)
198{
199 char *serial_string;
200 unsigned long long serial;
201
Simon Glass64b723f2017-08-03 12:22:12 -0600202 serial_string = env_get("serial#");
Paul Kocialkowskia7267d22015-08-27 19:37:14 +0200203
204 if (serial_string) {
205 serial = simple_strtoull(serial_string, NULL, 16);
206
207 serialnr->high = (unsigned int) (serial >> 32);
208 serialnr->low = (unsigned int) (serial & 0xffffffff);
209 } else {
210 serialnr->high = 0;
211 serialnr->low = 0;
212 }
213}
214
Paul Kocialkowski2edadee2015-08-27 19:37:12 +0200215void omap_die_id_usbethaddr(void)
Dileep Katta7354dfc2015-03-25 04:04:51 +0530216{
Paul Kocialkowski2edadee2015-08-27 19:37:12 +0200217 unsigned int die_id[4] = { 0 };
218 unsigned char mac[6] = { 0 };
Dileep Katta7354dfc2015-03-25 04:04:51 +0530219
Paul Kocialkowski2edadee2015-08-27 19:37:12 +0200220 omap_die_id((unsigned int *)&die_id);
221
Simon Glass64b723f2017-08-03 12:22:12 -0600222 if (!env_get("usbethaddr")) {
Paul Kocialkowski2edadee2015-08-27 19:37:12 +0200223 /*
224 * Create a fake MAC address from the processor ID code.
225 * First byte is 0x02 to signify locally administered.
226 */
227 mac[0] = 0x02;
228 mac[1] = die_id[3] & 0xff;
229 mac[2] = die_id[2] & 0xff;
230 mac[3] = die_id[1] & 0xff;
231 mac[4] = die_id[0] & 0xff;
232 mac[5] = (die_id[0] >> 8) & 0xff;
233
Simon Glass8551d552017-08-03 12:22:11 -0600234 eth_env_set_enetaddr("usbethaddr", mac);
Peter Robinson8033a552017-11-23 13:14:17 +0000235
236 if (!env_get("ethaddr"))
237 eth_env_set_enetaddr("ethaddr", mac);
Dileep Katta7354dfc2015-03-25 04:04:51 +0530238 }
239}
Paul Kocialkowski6bc318e2015-08-27 19:37:13 +0200240
241void omap_die_id_display(void)
242{
243 unsigned int die_id[4] = { 0 };
244
245 omap_die_id(die_id);
246
Ladislav Michlc84b9522016-06-02 11:43:16 +0200247 printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2],
248 die_id[1], die_id[0]);
Paul Kocialkowski6bc318e2015-08-27 19:37:13 +0200249}