1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 - 2022, Xilinx, Inc.
4 * (C) Copyright 2022 - 2023, Advanced Micro Devices, Inc.
6 * Michal Simek <michal.simek@amd.com>
11 #include <efi_loader.h>
17 #include <asm/global_data.h>
18 #include <asm/sections.h>
19 #include <dm/uclass.h>
21 #include <linux/sizes.h>
25 #include <i2c_eeprom.h>
27 #include <generated/dt.h>
30 #include <linux/ctype.h>
31 #include <linux/kernel.h>
36 #if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
37 struct efi_fw_image fw_images[] = {
38 #if defined(XILINX_BOOT_IMAGE_GUID)
40 .image_type_id = XILINX_BOOT_IMAGE_GUID,
41 .fw_name = u"XILINX-BOOT",
45 #if defined(XILINX_UBOOT_IMAGE_GUID)
47 .image_type_id = XILINX_UBOOT_IMAGE_GUID,
48 .fw_name = u"XILINX-UBOOT",
54 struct efi_capsule_update_info update_info = {
55 .num_images = ARRAY_SIZE(fw_images),
59 #endif /* EFI_HAVE_CAPSULE_SUPPORT */
61 #define EEPROM_HEADER_MAGIC 0xdaaddeed
62 #define EEPROM_HDR_MANUFACTURER_LEN 16
63 #define EEPROM_HDR_NAME_LEN 16
64 #define EEPROM_HDR_REV_LEN 8
65 #define EEPROM_HDR_SERIAL_LEN 20
66 #define EEPROM_HDR_NO_OF_MAC_ADDR 4
67 #define EEPROM_HDR_ETH_ALEN ETH_ALEN
68 #define EEPROM_HDR_UUID_LEN 16
70 struct xilinx_board_description {
72 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
73 char name[EEPROM_HDR_NAME_LEN + 1];
74 char revision[EEPROM_HDR_REV_LEN + 1];
75 char serial[EEPROM_HDR_SERIAL_LEN + 1];
76 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
77 char uuid[EEPROM_HDR_UUID_LEN + 1];
80 static int highest_id = -1;
81 static struct xilinx_board_description *board_info;
83 #define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
85 /* Variable which stores pointer to array which stores eeprom content */
86 struct xilinx_legacy_format {
87 char board_sn[18]; /* 0x0 */
88 char unused0[14]; /* 0x12 */
89 char eth_mac[ETH_ALEN]; /* 0x20 */
90 char unused1[170]; /* 0x26 */
91 char board_name[11]; /* 0xd0 */
92 char unused2[5]; /* 0xdc */
93 char board_revision[3]; /* 0xe0 */
94 char unused3[29]; /* 0xe3 */
97 static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
102 for (i = 0; i < size; i++) {
105 /* Remove all non printable chars but ignore MAC address */
106 if ((i < offsetof(struct xilinx_legacy_format, eth_mac) ||
107 i >= offsetof(struct xilinx_legacy_format, unused1)) &&
108 (byte < '!' || byte > '~')) {
113 /* Convert strings to lower case */
114 if (byte >= 'A' && byte <= 'Z')
115 eeprom[i] = byte + 'a' - 'A';
119 static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
120 struct xilinx_board_description *desc)
123 struct xilinx_legacy_format *eeprom_content;
124 bool eth_valid = false;
126 size = sizeof(*eeprom_content);
128 eeprom_content = calloc(1, size);
132 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
135 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
137 debug("%s: I2C EEPROM read failed\n", __func__);
138 free(eeprom_content);
142 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
144 /* Terminating \0 chars are the part of desc fields already */
145 strlcpy(desc->name, eeprom_content->board_name,
146 sizeof(eeprom_content->board_name) + 1);
147 strlcpy(desc->revision, eeprom_content->board_revision,
148 sizeof(eeprom_content->board_revision) + 1);
149 strlcpy(desc->serial, eeprom_content->board_sn,
150 sizeof(eeprom_content->board_sn) + 1);
152 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
154 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
156 printf("Xilinx I2C Legacy format at %s:\n", name);
157 printf(" Board name:\t%s\n", desc->name);
158 printf(" Board rev:\t%s\n", desc->revision);
159 printf(" Board SN:\t%s\n", desc->serial);
162 printf(" Ethernet mac:\t%pM\n", desc->mac_addr);
164 desc->header = EEPROM_HEADER_MAGIC;
166 free(eeprom_content);
171 static bool xilinx_detect_legacy(u8 *buffer)
176 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
179 if (c < '0' || c > '9')
186 static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
187 struct xilinx_board_description *desc)
189 int i, ret, eeprom_size;
193 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
194 eeprom_size = i2c_eeprom_size(dev);
196 fru_content = calloc(1, eeprom_size);
200 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
203 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
206 debug("%s: I2C EEPROM read failed\n", __func__);
210 fru_capture((unsigned long)fru_content);
211 if (gd->flags & GD_FLG_RELOC || (_DEBUG && IS_ENABLED(CONFIG_DTB_RESELECT))) {
212 printf("Xilinx I2C FRU format at %s:\n", name);
213 ret = fru_display(0);
215 printf("FRU format decoding failed.\n");
220 if (desc->header == EEPROM_HEADER_MAGIC) {
221 debug("Information already filled\n");
226 /* It is clear that FRU was captured and structures were filled */
227 strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
228 sizeof(desc->manufacturer));
229 strlcpy(desc->uuid, (char *)fru_data.brd.uuid,
231 strlcpy(desc->name, (char *)fru_data.brd.product_name,
233 for (i = 0; i < sizeof(desc->name); i++) {
234 if (desc->name[i] == ' ')
235 desc->name[i] = '\0';
237 strlcpy(desc->revision, (char *)fru_data.brd.rev,
238 sizeof(desc->revision));
239 for (i = 0; i < sizeof(desc->revision); i++) {
240 if (desc->revision[i] == ' ')
241 desc->revision[i] = '\0';
243 strlcpy(desc->serial, (char *)fru_data.brd.serial_number,
244 sizeof(desc->serial));
246 while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
247 if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
248 memcpy(&desc->mac_addr[id],
249 (char *)fru_data.mac.macid[id], ETH_ALEN);
253 desc->header = EEPROM_HEADER_MAGIC;
260 static bool xilinx_detect_fru(u8 *buffer)
265 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
267 debug("%s Common header CRC FAIL\n", __func__);
271 bool all_zeros = true;
272 /* Checksum over all zeros is also zero that's why detect this case */
273 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
281 debug("%s Common header CRC PASS\n", __func__);
285 static int xilinx_read_eeprom_single(char *name,
286 struct xilinx_board_description *desc)
291 u8 buffer[XILINX_I2C_DETECTION_BITS];
293 eeprom = ofnode_get_aliases_node(name);
294 if (!ofnode_valid(eeprom))
297 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
301 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
303 debug("%s: I2C EEPROM read failed\n", __func__);
307 debug("%s: i2c memory detected: %s\n", __func__, name);
309 if (IS_ENABLED(CONFIG_CMD_FRU) && xilinx_detect_fru(buffer))
310 return xilinx_read_eeprom_fru(dev, name, desc);
312 if (xilinx_detect_legacy(buffer))
313 return xilinx_read_eeprom_legacy(dev, name, desc);
318 __maybe_unused int xilinx_read_eeprom(void)
321 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
322 struct xilinx_board_description *desc;
324 highest_id = dev_read_alias_highest_id("nvmem");
325 /* No nvmem aliases present */
329 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
333 debug("%s: Highest ID %d, board_info %p\n", __func__,
334 highest_id, board_info);
336 for (id = 0; id <= highest_id; id++) {
337 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
339 /* Alloc structure */
340 desc = &board_info[id];
342 /* Ignoring return value for supporting multiple chips */
343 xilinx_read_eeprom_single(name_buf, desc);
347 * Consider to clean board_info structure when board/cards are not
354 #if defined(CONFIG_OF_BOARD)
355 void *board_fdt_blob_setup(int *err)
360 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
361 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
362 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
363 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
365 if (fdt_magic(fdt_blob) == FDT_MAGIC)
368 debug("DTB is not passed via %p\n", fdt_blob);
371 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
373 * FDT is at end of BSS unless it is in a different memory
376 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
377 fdt_blob = (ulong *)_image_binary_end;
379 fdt_blob = (ulong *)__bss_end;
381 /* FDT is at end of image */
382 fdt_blob = (ulong *)_end;
385 if (fdt_magic(fdt_blob) == FDT_MAGIC)
388 debug("DTB is also not passed via %p\n", fdt_blob);
395 #if defined(CONFIG_BOARD_LATE_INIT)
396 static int env_set_by_index(const char *name, int index, char *data)
401 sprintf(var, "board_%s", name);
403 sprintf(var, "card%d_%s", index, name);
405 return env_set(var, data);
408 int board_late_init_xilinx(void)
411 int i, id, macid = 0;
412 struct xilinx_board_description *desc;
413 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
415 if (!IS_ENABLED(CONFIG_MICROBLAZE)) {
418 scriptaddr = env_get_hex("scriptaddr", 0);
419 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
422 if (IS_ENABLED(CONFIG_ARCH_ZYNQ) || IS_ENABLED(CONFIG_MICROBLAZE))
423 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
425 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
427 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
428 ret |= env_set_addr("bootm_size", (void *)bootm_size);
430 for (id = 0; id <= highest_id; id++) {
431 desc = &board_info[id];
432 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
433 if (desc->manufacturer[0])
434 ret |= env_set_by_index("manufacturer", id,
437 ret |= env_set_by_index("name", id,
439 if (desc->revision[0])
440 ret |= env_set_by_index("rev", id,
443 ret |= env_set_by_index("serial", id,
447 unsigned char uuid[UUID_STR_LEN + 1];
448 unsigned char *t = desc->uuid;
450 memset(uuid, 0, UUID_STR_LEN + 1);
452 sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
453 t[0], t[1], t[2], t[3], t[4], t[5],
454 t[6], t[7], t[8], t[9], t[10], t[11],
455 t[12], t[13], t[14], t[15]);
456 ret |= env_set_by_index("uuid", id, uuid);
459 if (!CONFIG_IS_ENABLED(NET))
462 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
463 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
464 ret |= eth_env_set_enetaddr_by_index("eth",
465 macid++, desc->mac_addr[i]);
471 printf("%s: Saving run time variables FAILED\n", __func__);
477 static char *board_name = DEVICE_TREE;
479 int __maybe_unused board_fit_config_name_match(const char *name)
481 debug("%s: Check %s, default %s\n", __func__, name, board_name);
483 #if !defined(CONFIG_SPL_BUILD)
484 if (IS_ENABLED(CONFIG_REGEX)) {
488 ret = slre_compile(&slre, name);
490 ret = slre_match(&slre, board_name, strlen(board_name),
492 debug("%s: name match ret = %d\n", __func__, ret);
498 if (!strcmp(name, board_name))
504 #if IS_ENABLED(CONFIG_DTB_RESELECT)
505 #define MAX_NAME_LENGTH 50
507 char * __maybe_unused __weak board_name_decode(void)
509 char *board_local_name;
510 struct xilinx_board_description *desc;
513 board_local_name = calloc(1, MAX_NAME_LENGTH);
517 for (id = 0; id <= highest_id; id++) {
518 desc = &board_info[id];
520 /* No board description */
524 /* Board is not detected */
525 if (desc->header != EEPROM_HEADER_MAGIC)
528 /* The first string should be soc name */
530 strcat(board_local_name, CONFIG_SYS_BOARD);
533 * For two purpose here:
534 * soc_name- eg: zynqmp-
535 * and between base board and CC eg: ..revA-sck...
537 strcat(board_local_name, "-");
540 /* For DT composition name needs to be lowercase */
541 for (i = 0; i < sizeof(desc->name); i++)
542 desc->name[i] = tolower(desc->name[i]);
544 strcat(board_local_name, desc->name);
546 if (desc->revision[0]) {
547 strcat(board_local_name, "-rev");
549 /* And revision needs to be uppercase */
550 for (i = 0; i < sizeof(desc->revision); i++)
551 desc->revision[i] = toupper(desc->revision[i]);
553 strcat(board_local_name, desc->revision);
558 * Longer strings will end up with buffer overflow and potential
559 * attacks that's why check it
561 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
562 panic("Board name can't be determined\n");
564 if (strlen(board_local_name))
565 return board_local_name;
568 free(board_local_name);
572 bool __maybe_unused __weak board_detection(void)
574 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
577 ret = xilinx_read_eeprom();
578 return !ret ? true : false;
584 bool __maybe_unused __weak soc_detection(void)
589 char * __maybe_unused __weak soc_name_decode(void)
594 int embedded_dtb_select(void)
596 if (soc_detection()) {
597 char *soc_local_name;
599 soc_local_name = soc_name_decode();
600 if (soc_local_name) {
601 board_name = soc_local_name;
602 printf("Detected SOC name: %s\n", board_name);
604 /* Time to change DTB on fly */
605 /* Both ways should work here */
606 /* fdtdec_resetup(&rescan); */
607 return fdtdec_setup();
611 if (board_detection()) {
612 char *board_local_name;
614 board_local_name = board_name_decode();
615 if (board_local_name) {
616 board_name = board_local_name;
617 printf("Detected name: %s\n", board_name);
619 /* Time to change DTB on fly */
620 /* Both ways should work here */
621 /* fdtdec_resetup(&rescan); */
629 #if defined(CONFIG_LMB)
630 phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
639 if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
640 panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
642 /* found enough not-reserved memory to relocated U-Boot */
644 lmb_add(&lmb, gd->ram_base, gd->ram_size);
645 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
646 size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
647 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
650 reg = gd->ram_top - size;