blob: 7d1ca274bf3a3cd722d754529f494cb449e143ac [file] [log] [blame]
Aneesh V095aea22011-07-21 09:10:12 -04001/*
2 * Copyright 2011 Linaro Limited
3 * Aneesh V <aneesh@ti.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Aneesh V095aea22011-07-21 09:10:12 -04006 */
7#include <common.h>
Simon Glass5d982852017-05-17 08:23:00 -06008#include <asm/setup.h>
Nishanth Menon8a0c6d62014-03-28 11:00:04 -05009#include <asm/arch/sys_proto.h>
Sam Protsenko7a390c02018-02-27 23:45:03 +020010#include <asm/omap_common.h>
11
Aneesh V095aea22011-07-21 09:10:12 -040012static 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
Semen Protsenkofa24eca2017-05-22 19:16:40 +030024#ifdef CONFIG_FASTBOOT_FLASH
25static void omap_set_fastboot_cpu(void)
26{
27 char *cpu;
28 u32 cpu_rev = omap_revision();
29
30 switch (cpu_rev) {
Praneeth Bajjuri9b46ce82017-10-12 22:47:05 -050031 case DRA762_ES1_0:
32 cpu = "DRA762";
33 break;
Semen Protsenkofa24eca2017-05-22 19:16:40 +030034 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:
Vishal Mahaveerb6df9352017-10-12 22:47:04 -050041 case DRA722_ES2_1:
Semen Protsenkofa24eca2017-05-22 19:16:40 +030042 cpu = "DRA722";
43 break;
44 default:
45 cpu = NULL;
46 printf("Warning: fastboot.cpu: unknown CPU rev: %u\n", cpu_rev);
47 }
48
Simon Glass382bee52017-08-03 12:22:09 -060049 env_set("fastboot.cpu", cpu);
Semen Protsenkofa24eca2017-05-22 19:16:40 +030050}
51
52static 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
Simon Glass382bee52017-08-03 12:22:09 -060072 env_set("fastboot.secure", secure);
Semen Protsenkofa24eca2017-05-22 19:16:40 +030073}
74
75static void omap_set_fastboot_board_rev(void)
76{
77 const char *board_rev;
78
Simon Glass00caae62017-08-03 12:22:12 -060079 board_rev = env_get("board_rev");
Semen Protsenkofa24eca2017-05-22 19:16:40 +030080 if (board_rev == NULL)
81 printf("Warning: fastboot.board_rev: unknown board revision\n");
82
Simon Glass382bee52017-08-03 12:22:09 -060083 env_set("fastboot.board_rev", board_rev);
Semen Protsenkofa24eca2017-05-22 19:16:40 +030084}
85
86#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
87static 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) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090096 pr_err("invalid mmc device\n");
Semen Protsenkofa24eca2017-05-22 19:16:40 +030097 return 0;
98 }
99
Sam Protsenko26857762017-09-22 01:51:59 +0300100 /* Check only for EFI (GPT) partition table */
101 res = part_get_info_by_name_type(dev_desc, part, &info, PART_TYPE_EFI);
Sam Protsenko19d141a2017-09-21 23:01:00 +0300102 if (res < 0)
Semen Protsenkofa24eca2017-05-22 19:16:40 +0300103 return 0;
Semen Protsenkofa24eca2017-05-22 19:16:40 +0300104
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
113static 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");
Sam Protsenko19d141a2017-09-21 23:01:00 +0300119 if (sz_kb == 0)
120 return; /* probably it's not Android partition table */
Semen Protsenkofa24eca2017-05-22 19:16:40 +0300121
Sam Protsenko19d141a2017-09-21 23:01:00 +0300122 sprintf(buf, "%u", sz_kb);
Simon Glass382bee52017-08-03 12:22:09 -0600123 env_set("fastboot.userdata_size", buf);
Semen Protsenkofa24eca2017-05-22 19:16:40 +0300124}
125#else
126static inline void omap_set_fastboot_userdata_size(void)
127{
128}
129#endif /* CONFIG_FASTBOOT_FLASH_MMC_DEV */
130void 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
Aneesh V095aea22011-07-21 09:10:12 -0400139/*
140 * Cancel out the denominator and numerator of a fraction
141 * to get smaller numerator and denominator.
142 */
143void 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}
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500161
Paul Kocialkowski72931b12015-08-27 19:37:08 +0200162__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
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200167void omap_die_id_serial(void)
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500168{
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200169 unsigned int die_id[4] = { 0 };
170 char serial_string[17] = { 0 };
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500171
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200172 omap_die_id((unsigned int *)&die_id);
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500173
Simon Glass00caae62017-08-03 12:22:12 -0600174 if (!env_get("serial#")) {
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200175 snprintf(serial_string, sizeof(serial_string),
176 "%08x%08x", die_id[0], die_id[3]);
177
Simon Glass382bee52017-08-03 12:22:09 -0600178 env_set("serial#", serial_string);
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500179 }
180}
Dileep Kattaf12467d2015-03-25 04:04:51 +0530181
Paul Kocialkowski2da87ab2015-08-27 19:37:14 +0200182void omap_die_id_get_board_serial(struct tag_serialnr *serialnr)
183{
184 char *serial_string;
185 unsigned long long serial;
186
Simon Glass00caae62017-08-03 12:22:12 -0600187 serial_string = env_get("serial#");
Paul Kocialkowski2da87ab2015-08-27 19:37:14 +0200188
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
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200200void omap_die_id_usbethaddr(void)
Dileep Kattaf12467d2015-03-25 04:04:51 +0530201{
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200202 unsigned int die_id[4] = { 0 };
203 unsigned char mac[6] = { 0 };
Dileep Kattaf12467d2015-03-25 04:04:51 +0530204
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200205 omap_die_id((unsigned int *)&die_id);
206
Simon Glass00caae62017-08-03 12:22:12 -0600207 if (!env_get("usbethaddr")) {
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200208 /*
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
Simon Glassfd1e9592017-08-03 12:22:11 -0600219 eth_env_set_enetaddr("usbethaddr", mac);
Peter Robinsonb59670f2017-11-23 13:14:17 +0000220
221 if (!env_get("ethaddr"))
222 eth_env_set_enetaddr("ethaddr", mac);
Dileep Kattaf12467d2015-03-25 04:04:51 +0530223 }
224}
Paul Kocialkowski679f82c2015-08-27 19:37:13 +0200225
226void omap_die_id_display(void)
227{
228 unsigned int die_id[4] = { 0 };
229
230 omap_die_id(die_id);
231
Ladislav Michle22455f2016-06-02 11:43:16 +0200232 printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2],
233 die_id[1], die_id[0]);
Paul Kocialkowski679f82c2015-08-27 19:37:13 +0200234}