blob: 0b0bf1837c9cb89ac074356f9c3e081b858a63ff [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>
Aneesh V095aea22011-07-21 09:10:12 -040010static 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
Semen Protsenkofa24eca2017-05-22 19:16:40 +030022#ifdef CONFIG_FASTBOOT_FLASH
23static 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
Simon Glass382bee52017-08-03 12:22:09 -060043 env_set("fastboot.cpu", cpu);
Semen Protsenkofa24eca2017-05-22 19:16:40 +030044}
45
46static 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
Simon Glass382bee52017-08-03 12:22:09 -060066 env_set("fastboot.secure", secure);
Semen Protsenkofa24eca2017-05-22 19:16:40 +030067}
68
69static void omap_set_fastboot_board_rev(void)
70{
71 const char *board_rev;
72
Simon Glass00caae62017-08-03 12:22:12 -060073 board_rev = env_get("board_rev");
Semen Protsenkofa24eca2017-05-22 19:16:40 +030074 if (board_rev == NULL)
75 printf("Warning: fastboot.board_rev: unknown board revision\n");
76
Simon Glass382bee52017-08-03 12:22:09 -060077 env_set("fastboot.board_rev", board_rev);
Semen Protsenkofa24eca2017-05-22 19:16:40 +030078}
79
80#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
81static 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 error("invalid mmc device\n");
91 return 0;
92 }
93
94 res = part_get_info_by_name(dev_desc, part, &info);
95 if (res < 0) {
96 error("cannot find partition: '%s'\n", part);
97 return 0;
98 }
99
100 /* Calculate size in bytes */
101 sz = (info.size * (u64)info.blksz);
102 /* to KiB */
103 sz >>= 10;
104
105 return (u32)sz;
106}
107
108static void omap_set_fastboot_userdata_size(void)
109{
110 char buf[16];
111 u32 sz_kb;
112
113 sz_kb = omap_mmc_get_part_size("userdata");
114 if (sz_kb == 0) {
115 buf[0] = '\0';
116 printf("Warning: fastboot.userdata_size: unable to calc\n");
117 } else {
118 sprintf(buf, "%u", sz_kb);
119 }
120
Simon Glass382bee52017-08-03 12:22:09 -0600121 env_set("fastboot.userdata_size", buf);
Semen Protsenkofa24eca2017-05-22 19:16:40 +0300122}
123#else
124static inline void omap_set_fastboot_userdata_size(void)
125{
126}
127#endif /* CONFIG_FASTBOOT_FLASH_MMC_DEV */
128void 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
Aneesh V095aea22011-07-21 09:10:12 -0400137/*
138 * Cancel out the denominator and numerator of a fraction
139 * to get smaller numerator and denominator.
140 */
141void 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}
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500159
Paul Kocialkowski72931b12015-08-27 19:37:08 +0200160__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
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200165void omap_die_id_serial(void)
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500166{
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200167 unsigned int die_id[4] = { 0 };
168 char serial_string[17] = { 0 };
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500169
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200170 omap_die_id((unsigned int *)&die_id);
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500171
Simon Glass00caae62017-08-03 12:22:12 -0600172 if (!env_get("serial#")) {
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200173 snprintf(serial_string, sizeof(serial_string),
174 "%08x%08x", die_id[0], die_id[3]);
175
Simon Glass382bee52017-08-03 12:22:09 -0600176 env_set("serial#", serial_string);
Nishanth Menon8a0c6d62014-03-28 11:00:04 -0500177 }
178}
Dileep Kattaf12467d2015-03-25 04:04:51 +0530179
Paul Kocialkowski2da87ab2015-08-27 19:37:14 +0200180void omap_die_id_get_board_serial(struct tag_serialnr *serialnr)
181{
182 char *serial_string;
183 unsigned long long serial;
184
Simon Glass00caae62017-08-03 12:22:12 -0600185 serial_string = env_get("serial#");
Paul Kocialkowski2da87ab2015-08-27 19:37:14 +0200186
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
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200198void omap_die_id_usbethaddr(void)
Dileep Kattaf12467d2015-03-25 04:04:51 +0530199{
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200200 unsigned int die_id[4] = { 0 };
201 unsigned char mac[6] = { 0 };
Dileep Kattaf12467d2015-03-25 04:04:51 +0530202
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200203 omap_die_id((unsigned int *)&die_id);
204
Simon Glass00caae62017-08-03 12:22:12 -0600205 if (!env_get("usbethaddr")) {
Paul Kocialkowski07815eb2015-08-27 19:37:12 +0200206 /*
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
Simon Glassfd1e9592017-08-03 12:22:11 -0600217 eth_env_set_enetaddr("usbethaddr", mac);
Dileep Kattaf12467d2015-03-25 04:04:51 +0530218 }
219}
Paul Kocialkowski679f82c2015-08-27 19:37:13 +0200220
221void omap_die_id_display(void)
222{
223 unsigned int die_id[4] = { 0 };
224
225 omap_die_id(die_id);
226
Ladislav Michle22455f2016-06-02 11:43:16 +0200227 printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2],
228 die_id[1], die_id[0]);
Paul Kocialkowski679f82c2015-08-27 19:37:13 +0200229}