blob: 9b4aded466abfb056704d132aa761448b68a62d1 [file] [log] [blame]
Michal Simek014d3042019-01-21 15:25:02 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Michal Simek394ee242020-08-03 13:01:45 +02003 * (C) Copyright 2014 - 2020 Xilinx, Inc.
Michal Simek014d3042019-01-21 15:25:02 +01004 * Michal Simek <michal.simek@xilinx.com>
5 */
6
7#include <common.h>
Sughosh Ganuccb36462022-04-15 11:29:34 +05308#include <efi.h>
9#include <efi_loader.h>
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Michal Simek878ba362019-12-19 17:45:15 +010013#include <asm/sections.h>
Michal Simek014d3042019-01-21 15:25:02 +010014#include <dm/uclass.h>
15#include <i2c.h>
Michal Simekb90b97e2020-04-08 10:51:36 +020016#include <linux/sizes.h>
Michal Simek394ee242020-08-03 13:01:45 +020017#include <malloc.h>
Michal Simek705d44a2020-03-31 12:39:37 +020018#include "board.h"
Michal Simek394ee242020-08-03 13:01:45 +020019#include <dm.h>
20#include <i2c_eeprom.h>
21#include <net.h>
Michal Simekfe49df92020-10-26 12:26:13 +010022#include <generated/dt.h>
T Karthik Reddyc8fa40a2021-08-10 06:50:20 -060023#include <soc.h>
Michal Simeke0453512021-08-11 14:23:54 +020024#include <linux/ctype.h>
Sughosh Ganuccb36462022-04-15 11:29:34 +053025#include <linux/kernel.h>
Michal Simek7459d4a2022-07-21 16:19:17 +020026#include <uuid.h>
Michal Simek014d3042019-01-21 15:25:02 +010027
Michal Simek207e0622020-08-03 16:14:23 +020028#include "fru.h"
29
Sughosh Ganuccb36462022-04-15 11:29:34 +053030#if CONFIG_IS_ENABLED(EFI_HAVE_CAPSULE_SUPPORT)
31struct efi_fw_image fw_images[] = {
32#if defined(XILINX_BOOT_IMAGE_GUID)
33 {
34 .image_type_id = XILINX_BOOT_IMAGE_GUID,
35 .fw_name = u"XILINX-BOOT",
36 .image_index = 1,
37 },
38#endif
39#if defined(XILINX_UBOOT_IMAGE_GUID)
40 {
41 .image_type_id = XILINX_UBOOT_IMAGE_GUID,
42 .fw_name = u"XILINX-UBOOT",
43 .image_index = 2,
44 },
45#endif
46};
47
48struct efi_capsule_update_info update_info = {
49 .images = fw_images,
50};
51
52u8 num_image_type_guids = ARRAY_SIZE(fw_images);
53#endif /* EFI_HAVE_CAPSULE_SUPPORT */
54
Michal Simekaee22b32020-08-03 12:59:28 +020055#if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
Michal Simek014d3042019-01-21 15:25:02 +010056int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
57{
Michal Simek13db6162019-01-21 16:29:07 +010058 int ret = -EINVAL;
Michal Simek13db6162019-01-21 16:29:07 +010059 struct udevice *dev;
60 ofnode eeprom;
61
62 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
63 if (!ofnode_valid(eeprom))
64 return -ENODEV;
65
66 debug("%s: Path to EEPROM %s\n", __func__,
Simon Glassf3455962020-01-27 08:49:43 -070067 ofnode_read_chosen_string("xlnx,eeprom"));
Michal Simek13db6162019-01-21 16:29:07 +010068
69 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
70 if (ret)
71 return ret;
72
73 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
74 if (ret)
75 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
76 else
77 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
Michal Simek13db6162019-01-21 16:29:07 +010078
79 return ret;
80}
Michal Simekaee22b32020-08-03 12:59:28 +020081#endif
Ibai Erkiaga6f658202019-10-02 15:57:36 +010082
Michal Simek394ee242020-08-03 13:01:45 +020083#define EEPROM_HEADER_MAGIC 0xdaaddeed
84#define EEPROM_HDR_MANUFACTURER_LEN 16
85#define EEPROM_HDR_NAME_LEN 16
86#define EEPROM_HDR_REV_LEN 8
87#define EEPROM_HDR_SERIAL_LEN 20
88#define EEPROM_HDR_NO_OF_MAC_ADDR 4
89#define EEPROM_HDR_ETH_ALEN ETH_ALEN
Michal Simek7459d4a2022-07-21 16:19:17 +020090#define EEPROM_HDR_UUID_LEN 16
Michal Simek394ee242020-08-03 13:01:45 +020091
92struct xilinx_board_description {
93 u32 header;
94 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
95 char name[EEPROM_HDR_NAME_LEN + 1];
96 char revision[EEPROM_HDR_REV_LEN + 1];
97 char serial[EEPROM_HDR_SERIAL_LEN + 1];
98 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
Michal Simek7459d4a2022-07-21 16:19:17 +020099 char uuid[EEPROM_HDR_UUID_LEN + 1];
Michal Simek394ee242020-08-03 13:01:45 +0200100};
101
Michal Simek0bb24e12020-08-03 12:57:05 +0200102static int highest_id = -1;
Michal Simeke98ae1d2021-08-12 12:30:36 +0200103static struct xilinx_board_description *board_info;
Michal Simek394ee242020-08-03 13:01:45 +0200104
Michal Simek207e0622020-08-03 16:14:23 +0200105#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simek394ee242020-08-03 13:01:45 +0200106
107/* Variable which stores pointer to array which stores eeprom content */
108struct xilinx_legacy_format {
109 char board_sn[18]; /* 0x0 */
110 char unused0[14]; /* 0x12 */
111 char eth_mac[6]; /* 0x20 */
112 char unused1[170]; /* 0x26 */
113 char board_name[11]; /* 0xd0 */
114 char unused2[5]; /* 0xdc */
115 char board_revision[3]; /* 0xe0 */
116 char unused3[29]; /* 0xe3 */
117};
118
119static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
120{
121 int i;
122 char byte;
123
124 for (i = 0; i < size; i++) {
125 byte = eeprom[i];
126
127 /* Remove all ffs and spaces */
128 if (byte == 0xff || byte == ' ')
129 eeprom[i] = 0;
130
131 /* Convert strings to lower case */
132 if (byte >= 'A' && byte <= 'Z')
133 eeprom[i] = byte + 'a' - 'A';
134 }
135}
136
137static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
138 struct xilinx_board_description *desc)
139{
140 int ret, size;
141 struct xilinx_legacy_format *eeprom_content;
142 bool eth_valid = false;
143
144 size = sizeof(*eeprom_content);
145
146 eeprom_content = calloc(1, size);
147 if (!eeprom_content)
148 return -ENOMEM;
149
150 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
151 eeprom_content);
152
153 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
154 if (ret) {
155 debug("%s: I2C EEPROM read failed\n", __func__);
156 free(eeprom_content);
157 return ret;
158 }
159
160 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
161
162 printf("Xilinx I2C Legacy format at %s:\n", name);
163 printf(" Board name:\t%s\n", eeprom_content->board_name);
164 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
165 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
166
167 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
168 if (eth_valid)
169 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
170
171 /* Terminating \0 chars ensure end of string */
172 strcpy(desc->name, eeprom_content->board_name);
173 strcpy(desc->revision, eeprom_content->board_revision);
174 strcpy(desc->serial, eeprom_content->board_sn);
175 if (eth_valid)
176 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
177
178 desc->header = EEPROM_HEADER_MAGIC;
179
180 free(eeprom_content);
181
182 return ret;
183}
184
185static bool xilinx_detect_legacy(u8 *buffer)
186{
187 int i;
188 char c;
189
190 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
191 c = buffer[i];
192
193 if (c < '0' || c > '9')
194 return false;
195 }
196
197 return true;
198}
199
Michal Simek207e0622020-08-03 16:14:23 +0200200static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
201 struct xilinx_board_description *desc)
202{
Michal Simekbe5f5722021-08-12 11:03:49 +0200203 int i, ret, eeprom_size;
Michal Simek207e0622020-08-03 16:14:23 +0200204 u8 *fru_content;
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100205 u8 id = 0;
Michal Simek207e0622020-08-03 16:14:23 +0200206
207 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
208 eeprom_size = i2c_eeprom_size(dev);
209
210 fru_content = calloc(1, eeprom_size);
211 if (!fru_content)
212 return -ENOMEM;
213
214 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
215 fru_content);
216
217 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
218 eeprom_size);
219 if (ret) {
220 debug("%s: I2C EEPROM read failed\n", __func__);
Michal Simekaada8a62021-08-13 09:17:10 +0200221 goto end;
Michal Simek207e0622020-08-03 16:14:23 +0200222 }
223
Michal Simek207e0622020-08-03 16:14:23 +0200224 fru_capture((unsigned long)fru_content);
Michal Simeke0453512021-08-11 14:23:54 +0200225 if (gd->flags & GD_FLG_RELOC || (_DEBUG && CONFIG_IS_ENABLED(DTB_RESELECT))) {
226 printf("Xilinx I2C FRU format at %s:\n", name);
227 ret = fru_display(0);
228 if (ret) {
229 printf("FRU format decoding failed.\n");
230 goto end;
231 }
Michal Simek207e0622020-08-03 16:14:23 +0200232 }
233
234 if (desc->header == EEPROM_HEADER_MAGIC) {
235 debug("Information already filled\n");
Michal Simekaada8a62021-08-13 09:17:10 +0200236 ret = -EINVAL;
237 goto end;
Michal Simek207e0622020-08-03 16:14:23 +0200238 }
239
240 /* It is clear that FRU was captured and structures were filled */
Michal Simekb5f206e2022-07-21 16:19:18 +0200241 strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
Michal Simek207e0622020-08-03 16:14:23 +0200242 sizeof(desc->manufacturer));
Michal Simekb5f206e2022-07-21 16:19:18 +0200243 strlcpy(desc->uuid, (char *)fru_data.brd.uuid,
Michal Simek7459d4a2022-07-21 16:19:17 +0200244 sizeof(desc->uuid));
Michal Simekb5f206e2022-07-21 16:19:18 +0200245 strlcpy(desc->name, (char *)fru_data.brd.product_name,
Michal Simek207e0622020-08-03 16:14:23 +0200246 sizeof(desc->name));
Michal Simekbe5f5722021-08-12 11:03:49 +0200247 for (i = 0; i < sizeof(desc->name); i++) {
248 if (desc->name[i] == ' ')
249 desc->name[i] = '\0';
250 }
Michal Simekb5f206e2022-07-21 16:19:18 +0200251 strlcpy(desc->revision, (char *)fru_data.brd.rev,
Michal Simek207e0622020-08-03 16:14:23 +0200252 sizeof(desc->revision));
Michal Simek904fb972022-06-06 09:31:27 +0200253 for (i = 0; i < sizeof(desc->revision); i++) {
254 if (desc->revision[i] == ' ')
255 desc->revision[i] = '\0';
256 }
Michal Simekb5f206e2022-07-21 16:19:18 +0200257 strlcpy(desc->serial, (char *)fru_data.brd.serial_number,
Michal Simek207e0622020-08-03 16:14:23 +0200258 sizeof(desc->serial));
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100259
260 while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
261 if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
262 memcpy(&desc->mac_addr[id],
263 (char *)fru_data.mac.macid[id], ETH_ALEN);
264 id++;
265 }
266
Michal Simek207e0622020-08-03 16:14:23 +0200267 desc->header = EEPROM_HEADER_MAGIC;
268
Michal Simekaada8a62021-08-13 09:17:10 +0200269end:
270 free(fru_content);
271 return ret;
Michal Simek207e0622020-08-03 16:14:23 +0200272}
273
274static bool xilinx_detect_fru(u8 *buffer)
275{
276 u8 checksum = 0;
277 int i;
278
279 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
280 if (checksum) {
281 debug("%s Common header CRC FAIL\n", __func__);
282 return false;
283 }
284
285 bool all_zeros = true;
286 /* Checksum over all zeros is also zero that's why detect this case */
287 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
288 if (buffer[i] != 0)
289 all_zeros = false;
290 }
291
292 if (all_zeros)
293 return false;
294
295 debug("%s Common header CRC PASS\n", __func__);
296 return true;
297}
298
Michal Simek394ee242020-08-03 13:01:45 +0200299static int xilinx_read_eeprom_single(char *name,
300 struct xilinx_board_description *desc)
301{
302 int ret;
303 struct udevice *dev;
304 ofnode eeprom;
305 u8 buffer[XILINX_I2C_DETECTION_BITS];
306
307 eeprom = ofnode_get_aliases_node(name);
308 if (!ofnode_valid(eeprom))
309 return -ENODEV;
310
311 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
312 if (ret)
313 return ret;
314
315 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
316 if (ret) {
317 debug("%s: I2C EEPROM read failed\n", __func__);
318 return ret;
319 }
320
321 debug("%s: i2c memory detected: %s\n", __func__, name);
322
Michal Simek207e0622020-08-03 16:14:23 +0200323 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
324 return xilinx_read_eeprom_fru(dev, name, desc);
325
Michal Simek394ee242020-08-03 13:01:45 +0200326 if (xilinx_detect_legacy(buffer))
327 return xilinx_read_eeprom_legacy(dev, name, desc);
328
329 return -ENODEV;
330}
331
332__maybe_unused int xilinx_read_eeprom(void)
333{
Michal Simeke98ae1d2021-08-12 12:30:36 +0200334 int id;
Michal Simek394ee242020-08-03 13:01:45 +0200335 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simek0bb24e12020-08-03 12:57:05 +0200336 struct xilinx_board_description *desc;
Michal Simek394ee242020-08-03 13:01:45 +0200337
338 highest_id = dev_read_alias_highest_id("nvmem");
339 /* No nvmem aliases present */
340 if (highest_id < 0)
341 return -EINVAL;
342
Michal Simeke98ae1d2021-08-12 12:30:36 +0200343 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
Michal Simek394ee242020-08-03 13:01:45 +0200344 if (!board_info)
345 return -ENOMEM;
346
347 debug("%s: Highest ID %d, board_info %p\n", __func__,
348 highest_id, board_info);
349
350 for (id = 0; id <= highest_id; id++) {
351 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
352
Michal Simek0bb24e12020-08-03 12:57:05 +0200353 /* Alloc structure */
Michal Simeke98ae1d2021-08-12 12:30:36 +0200354 desc = &board_info[id];
Michal Simek0bb24e12020-08-03 12:57:05 +0200355
Michal Simek394ee242020-08-03 13:01:45 +0200356 /* Ignoring return value for supporting multiple chips */
Michal Simeke98ae1d2021-08-12 12:30:36 +0200357 xilinx_read_eeprom_single(name_buf, desc);
Michal Simek394ee242020-08-03 13:01:45 +0200358 }
359
Michal Simek0bb24e12020-08-03 12:57:05 +0200360 /*
361 * Consider to clean board_info structure when board/cards are not
362 * detected.
363 */
Michal Simek394ee242020-08-03 13:01:45 +0200364
365 return 0;
366}
367
Michal Simek1a505292022-02-17 14:28:38 +0100368#if defined(CONFIG_OF_BOARD)
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300369void *board_fdt_blob_setup(int *err)
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100370{
Michal Simekddf69ae2020-09-04 16:21:47 +0200371 void *fdt_blob;
Michal Simekc89f4292020-03-19 10:23:56 +0100372
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300373 *err = 0;
Michal Simek663c1622021-01-04 11:07:28 +0100374 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
375 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simek6d4317b2021-02-02 13:33:22 +0100376 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek9b9d01e2021-01-04 11:03:36 +0100377 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100378
Michal Simek9b9d01e2021-01-04 11:03:36 +0100379 if (fdt_magic(fdt_blob) == FDT_MAGIC)
380 return fdt_blob;
Michal Simek878ba362019-12-19 17:45:15 +0100381
Michal Simek9b9d01e2021-01-04 11:03:36 +0100382 debug("DTB is not passed via %p\n", fdt_blob);
383 }
Michal Simek878ba362019-12-19 17:45:15 +0100384
Michal Simek9b9d01e2021-01-04 11:03:36 +0100385 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
386 /*
387 * FDT is at end of BSS unless it is in a different memory
388 * region
389 */
390 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
391 fdt_blob = (ulong *)&_image_binary_end;
392 else
393 fdt_blob = (ulong *)&__bss_end;
394 } else {
395 /* FDT is at end of image */
396 fdt_blob = (ulong *)&_end;
397 }
Michal Simek878ba362019-12-19 17:45:15 +0100398
399 if (fdt_magic(fdt_blob) == FDT_MAGIC)
400 return fdt_blob;
401
402 debug("DTB is also not passed via %p\n", fdt_blob);
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100403
Michal Simek1a505292022-02-17 14:28:38 +0100404 *err = -EINVAL;
Michal Simek878ba362019-12-19 17:45:15 +0100405 return NULL;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100406}
407#endif
Michal Simek705d44a2020-03-31 12:39:37 +0200408
Michal Simek67ed85d2020-10-22 11:14:20 +0200409#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek0bb24e12020-08-03 12:57:05 +0200410static int env_set_by_index(const char *name, int index, char *data)
411{
412 char var[32];
413
414 if (!index)
415 sprintf(var, "board_%s", name);
416 else
417 sprintf(var, "card%d_%s", index, name);
418
419 return env_set(var, data);
420}
421
Michal Simek705d44a2020-03-31 12:39:37 +0200422int board_late_init_xilinx(void)
423{
Michal Simekbd07b5d2020-08-12 12:16:49 +0200424 u32 ret = 0;
Michal Simek0bb24e12020-08-03 12:57:05 +0200425 int i, id, macid = 0;
426 struct xilinx_board_description *desc;
Ricardo Salvetid35ccf22022-01-20 16:17:30 -0300427 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600428
T Karthik Reddy466cdde2021-04-02 01:49:17 -0600429 if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600430 ulong scriptaddr;
431
432 scriptaddr = env_get_hex("scriptaddr", 0);
T Karthik Reddy466cdde2021-04-02 01:49:17 -0600433 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600434 }
Michal Simekc86ce0c2020-08-12 12:17:53 +0200435
Michal Simek9ccfcae2020-10-23 07:54:18 +0200436 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simekc86ce0c2020-08-12 12:17:53 +0200437 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simekbd07b5d2020-08-12 12:16:49 +0200438
439 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
440
441 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simekc86ce0c2020-08-12 12:17:53 +0200442 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simek705d44a2020-03-31 12:39:37 +0200443
Michal Simek0bb24e12020-08-03 12:57:05 +0200444 for (id = 0; id <= highest_id; id++) {
Michal Simeke98ae1d2021-08-12 12:30:36 +0200445 desc = &board_info[id];
Michal Simek0bb24e12020-08-03 12:57:05 +0200446 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
447 if (desc->manufacturer[0])
448 ret |= env_set_by_index("manufacturer", id,
449 desc->manufacturer);
450 if (desc->name[0])
451 ret |= env_set_by_index("name", id,
452 desc->name);
453 if (desc->revision[0])
454 ret |= env_set_by_index("rev", id,
455 desc->revision);
456 if (desc->serial[0])
457 ret |= env_set_by_index("serial", id,
458 desc->serial);
Michal Simek394ee242020-08-03 13:01:45 +0200459
Michal Simek7459d4a2022-07-21 16:19:17 +0200460 if (desc->uuid[0]) {
461 char uuid[UUID_STR_LEN + 1];
462 char *t = desc->uuid;
463
464 memset(uuid, 0, UUID_STR_LEN + 1);
465
466 sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
467 t[0], t[1], t[2], t[3], t[4], t[5],
468 t[6], t[7], t[8], t[9], t[10], t[11],
469 t[12], t[13], t[14], t[15]);
470 ret |= env_set_by_index("uuid", id, uuid);
471 }
472
Michal Simek394ee242020-08-03 13:01:45 +0200473 if (!CONFIG_IS_ENABLED(NET))
474 continue;
475
Michal Simek0bb24e12020-08-03 12:57:05 +0200476 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
477 if (!desc->mac_addr[i])
Ashok Reddy Soma2d3b1b72022-02-23 15:00:58 +0100478 break;
Michal Simek0bb24e12020-08-03 12:57:05 +0200479
480 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
481 ret |= eth_env_set_enetaddr_by_index("eth",
482 macid++, desc->mac_addr[i]);
483 }
Michal Simek394ee242020-08-03 13:01:45 +0200484 }
485 }
486
Michal Simekbd07b5d2020-08-12 12:16:49 +0200487 if (ret)
488 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simek8ba62d22020-07-09 15:57:56 +0200489
Michal Simek705d44a2020-03-31 12:39:37 +0200490 return 0;
491}
Michal Simek67ed85d2020-10-22 11:14:20 +0200492#endif
Michal Simekfe49df92020-10-26 12:26:13 +0100493
Michal Simekc88975e2021-07-23 09:55:59 +0200494static char *board_name = DEVICE_TREE;
495
Michal Simekfe49df92020-10-26 12:26:13 +0100496int __maybe_unused board_fit_config_name_match(const char *name)
497{
Michal Simekc88975e2021-07-23 09:55:59 +0200498 debug("%s: Check %s, default %s\n", __func__, name, board_name);
Michal Simekfe49df92020-10-26 12:26:13 +0100499
Michal Simekc88975e2021-07-23 09:55:59 +0200500 if (!strcmp(name, board_name))
Michal Simekfe49df92020-10-26 12:26:13 +0100501 return 0;
502
503 return -1;
504}
T Karthik Reddyc8fa40a2021-08-10 06:50:20 -0600505
Michal Simekafb28572021-07-23 09:59:59 +0200506#if CONFIG_IS_ENABLED(DTB_RESELECT)
Michal Simeke0453512021-08-11 14:23:54 +0200507#define MAX_NAME_LENGTH 50
508
Michal Simekafb28572021-07-23 09:59:59 +0200509char * __maybe_unused __weak board_name_decode(void)
510{
Michal Simeke0453512021-08-11 14:23:54 +0200511 char *board_local_name;
512 struct xilinx_board_description *desc;
513 int i, id;
514
515 board_local_name = calloc(1, MAX_NAME_LENGTH);
516 if (!board_info)
517 return NULL;
518
519 for (id = 0; id <= highest_id; id++) {
520 desc = &board_info[id];
521
522 /* No board description */
523 if (!desc)
524 goto error;
525
526 /* Board is not detected */
527 if (desc->header != EEPROM_HEADER_MAGIC)
528 continue;
529
530 /* The first string should be soc name */
531 if (!id)
532 strcat(board_local_name, CONFIG_SYS_BOARD);
533
534 /*
535 * For two purpose here:
536 * soc_name- eg: zynqmp-
537 * and between base board and CC eg: ..revA-sck...
538 */
539 strcat(board_local_name, "-");
540
541 if (desc->name[0]) {
542 /* For DT composition name needs to be lowercase */
543 for (i = 0; i < sizeof(desc->name); i++)
544 desc->name[i] = tolower(desc->name[i]);
545
546 strcat(board_local_name, desc->name);
547 }
548 if (desc->revision[0]) {
549 strcat(board_local_name, "-rev");
550
551 /* And revision needs to be uppercase */
552 for (i = 0; i < sizeof(desc->revision); i++)
553 desc->revision[i] = toupper(desc->revision[i]);
554
555 strcat(board_local_name, desc->revision);
556 }
557 }
558
559 /*
560 * Longer strings will end up with buffer overflow and potential
561 * attacks that's why check it
562 */
563 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
564 panic("Board name can't be determined\n");
565
566 if (strlen(board_local_name))
567 return board_local_name;
568
569error:
570 free(board_local_name);
Michal Simekafb28572021-07-23 09:59:59 +0200571 return NULL;
572}
573
574bool __maybe_unused __weak board_detection(void)
575{
Michal Simeke0453512021-08-11 14:23:54 +0200576 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
577 int ret;
578
579 ret = xilinx_read_eeprom();
580 return !ret ? true : false;
581 }
582
Michal Simekafb28572021-07-23 09:59:59 +0200583 return false;
584}
585
586int embedded_dtb_select(void)
587{
588 if (board_detection()) {
589 char *board_local_name;
590
591 board_local_name = board_name_decode();
592 if (board_local_name) {
593 board_name = board_local_name;
Michal Simeke0453512021-08-11 14:23:54 +0200594 printf("Detected name: %s\n", board_name);
Michal Simekafb28572021-07-23 09:59:59 +0200595
596 /* Time to change DTB on fly */
597 /* Both ways should work here */
598 /* fdtdec_resetup(&rescan); */
599 fdtdec_setup();
600 }
601 }
602 return 0;
603}
604#endif