1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 - 2020 Xilinx, Inc.
4 * Michal Simek <michal.simek@xilinx.com>
10 #include <asm/global_data.h>
11 #include <asm/sections.h>
12 #include <dm/uclass.h>
14 #include <linux/sizes.h>
18 #include <i2c_eeprom.h>
20 #include <generated/dt.h>
22 #include <linux/ctype.h>
26 #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
27 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
33 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
34 if (!ofnode_valid(eeprom))
37 debug("%s: Path to EEPROM %s\n", __func__,
38 ofnode_read_chosen_string("xlnx,eeprom"));
40 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
44 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
46 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
48 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
54 #define EEPROM_HEADER_MAGIC 0xdaaddeed
55 #define EEPROM_HDR_MANUFACTURER_LEN 16
56 #define EEPROM_HDR_NAME_LEN 16
57 #define EEPROM_HDR_REV_LEN 8
58 #define EEPROM_HDR_SERIAL_LEN 20
59 #define EEPROM_HDR_NO_OF_MAC_ADDR 4
60 #define EEPROM_HDR_ETH_ALEN ETH_ALEN
62 struct xilinx_board_description {
64 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
65 char name[EEPROM_HDR_NAME_LEN + 1];
66 char revision[EEPROM_HDR_REV_LEN + 1];
67 char serial[EEPROM_HDR_SERIAL_LEN + 1];
68 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
71 static int highest_id = -1;
72 static struct xilinx_board_description *board_info;
74 #define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
76 /* Variable which stores pointer to array which stores eeprom content */
77 struct xilinx_legacy_format {
78 char board_sn[18]; /* 0x0 */
79 char unused0[14]; /* 0x12 */
80 char eth_mac[6]; /* 0x20 */
81 char unused1[170]; /* 0x26 */
82 char board_name[11]; /* 0xd0 */
83 char unused2[5]; /* 0xdc */
84 char board_revision[3]; /* 0xe0 */
85 char unused3[29]; /* 0xe3 */
88 static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
93 for (i = 0; i < size; i++) {
96 /* Remove all ffs and spaces */
97 if (byte == 0xff || byte == ' ')
100 /* Convert strings to lower case */
101 if (byte >= 'A' && byte <= 'Z')
102 eeprom[i] = byte + 'a' - 'A';
106 static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
107 struct xilinx_board_description *desc)
110 struct xilinx_legacy_format *eeprom_content;
111 bool eth_valid = false;
113 size = sizeof(*eeprom_content);
115 eeprom_content = calloc(1, size);
119 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
122 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
124 debug("%s: I2C EEPROM read failed\n", __func__);
125 free(eeprom_content);
129 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
131 printf("Xilinx I2C Legacy format at %s:\n", name);
132 printf(" Board name:\t%s\n", eeprom_content->board_name);
133 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
134 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
136 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
138 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
140 /* Terminating \0 chars ensure end of string */
141 strcpy(desc->name, eeprom_content->board_name);
142 strcpy(desc->revision, eeprom_content->board_revision);
143 strcpy(desc->serial, eeprom_content->board_sn);
145 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
147 desc->header = EEPROM_HEADER_MAGIC;
149 free(eeprom_content);
154 static bool xilinx_detect_legacy(u8 *buffer)
159 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
162 if (c < '0' || c > '9')
169 static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
170 struct xilinx_board_description *desc)
172 int i, ret, eeprom_size;
175 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
176 eeprom_size = i2c_eeprom_size(dev);
178 fru_content = calloc(1, eeprom_size);
182 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
185 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
188 debug("%s: I2C EEPROM read failed\n", __func__);
192 fru_capture((unsigned long)fru_content);
193 if (gd->flags & GD_FLG_RELOC || (_DEBUG && CONFIG_IS_ENABLED(DTB_RESELECT))) {
194 printf("Xilinx I2C FRU format at %s:\n", name);
195 ret = fru_display(0);
197 printf("FRU format decoding failed.\n");
202 if (desc->header == EEPROM_HEADER_MAGIC) {
203 debug("Information already filled\n");
208 /* It is clear that FRU was captured and structures were filled */
209 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
210 sizeof(desc->manufacturer));
211 strncpy(desc->name, (char *)fru_data.brd.product_name,
213 for (i = 0; i < sizeof(desc->name); i++) {
214 if (desc->name[i] == ' ')
215 desc->name[i] = '\0';
217 strncpy(desc->revision, (char *)fru_data.brd.rev,
218 sizeof(desc->revision));
219 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
220 sizeof(desc->serial));
221 desc->header = EEPROM_HEADER_MAGIC;
228 static bool xilinx_detect_fru(u8 *buffer)
233 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
235 debug("%s Common header CRC FAIL\n", __func__);
239 bool all_zeros = true;
240 /* Checksum over all zeros is also zero that's why detect this case */
241 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
249 debug("%s Common header CRC PASS\n", __func__);
253 static int xilinx_read_eeprom_single(char *name,
254 struct xilinx_board_description *desc)
259 u8 buffer[XILINX_I2C_DETECTION_BITS];
261 eeprom = ofnode_get_aliases_node(name);
262 if (!ofnode_valid(eeprom))
265 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
269 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
271 debug("%s: I2C EEPROM read failed\n", __func__);
275 debug("%s: i2c memory detected: %s\n", __func__, name);
277 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
278 return xilinx_read_eeprom_fru(dev, name, desc);
280 if (xilinx_detect_legacy(buffer))
281 return xilinx_read_eeprom_legacy(dev, name, desc);
286 __maybe_unused int xilinx_read_eeprom(void)
289 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
290 struct xilinx_board_description *desc;
292 highest_id = dev_read_alias_highest_id("nvmem");
293 /* No nvmem aliases present */
297 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
301 debug("%s: Highest ID %d, board_info %p\n", __func__,
302 highest_id, board_info);
304 for (id = 0; id <= highest_id; id++) {
305 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
307 /* Alloc structure */
308 desc = &board_info[id];
310 /* Ignoring return value for supporting multiple chips */
311 xilinx_read_eeprom_single(name_buf, desc);
315 * Consider to clean board_info structure when board/cards are not
322 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
323 void *board_fdt_blob_setup(int *err)
328 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
329 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
330 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
331 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
333 if (fdt_magic(fdt_blob) == FDT_MAGIC)
336 debug("DTB is not passed via %p\n", fdt_blob);
339 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
341 * FDT is at end of BSS unless it is in a different memory
344 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
345 fdt_blob = (ulong *)&_image_binary_end;
347 fdt_blob = (ulong *)&__bss_end;
349 /* FDT is at end of image */
350 fdt_blob = (ulong *)&_end;
353 if (fdt_magic(fdt_blob) == FDT_MAGIC)
356 debug("DTB is also not passed via %p\n", fdt_blob);
362 #if defined(CONFIG_BOARD_LATE_INIT)
363 static int env_set_by_index(const char *name, int index, char *data)
368 sprintf(var, "board_%s", name);
370 sprintf(var, "card%d_%s", index, name);
372 return env_set(var, data);
375 int board_late_init_xilinx(void)
378 int i, id, macid = 0;
379 struct xilinx_board_description *desc;
380 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
382 if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
385 scriptaddr = env_get_hex("scriptaddr", 0);
386 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
389 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
390 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
392 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
394 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
395 ret |= env_set_addr("bootm_size", (void *)bootm_size);
397 for (id = 0; id <= highest_id; id++) {
398 desc = &board_info[id];
399 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
400 if (desc->manufacturer[0])
401 ret |= env_set_by_index("manufacturer", id,
404 ret |= env_set_by_index("name", id,
406 if (desc->revision[0])
407 ret |= env_set_by_index("rev", id,
410 ret |= env_set_by_index("serial", id,
413 if (!CONFIG_IS_ENABLED(NET))
416 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
417 if (!desc->mac_addr[i])
420 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
421 ret |= eth_env_set_enetaddr_by_index("eth",
422 macid++, desc->mac_addr[i]);
428 printf("%s: Saving run time variables FAILED\n", __func__);
434 static char *board_name = DEVICE_TREE;
436 int __maybe_unused board_fit_config_name_match(const char *name)
438 debug("%s: Check %s, default %s\n", __func__, name, board_name);
440 if (!strcmp(name, board_name))
446 #if defined(CONFIG_DISPLAY_CPUINFO) && !defined(CONFIG_ARCH_ZYNQ)
447 int print_cpuinfo(void)
450 char name[SOC_MAX_STR_SIZE];
455 printf("CPU: UNKNOWN\n");
459 ret = soc_get_family(soc, name, SOC_MAX_STR_SIZE);
461 printf("CPU: %s\n", name);
463 ret = soc_get_revision(soc, name, SOC_MAX_STR_SIZE);
465 printf("Silicon: %s\n", name);
471 #if CONFIG_IS_ENABLED(DTB_RESELECT)
472 #define MAX_NAME_LENGTH 50
474 char * __maybe_unused __weak board_name_decode(void)
476 char *board_local_name;
477 struct xilinx_board_description *desc;
480 board_local_name = calloc(1, MAX_NAME_LENGTH);
484 for (id = 0; id <= highest_id; id++) {
485 desc = &board_info[id];
487 /* No board description */
491 /* Board is not detected */
492 if (desc->header != EEPROM_HEADER_MAGIC)
495 /* The first string should be soc name */
497 strcat(board_local_name, CONFIG_SYS_BOARD);
500 * For two purpose here:
501 * soc_name- eg: zynqmp-
502 * and between base board and CC eg: ..revA-sck...
504 strcat(board_local_name, "-");
507 /* For DT composition name needs to be lowercase */
508 for (i = 0; i < sizeof(desc->name); i++)
509 desc->name[i] = tolower(desc->name[i]);
511 strcat(board_local_name, desc->name);
513 if (desc->revision[0]) {
514 strcat(board_local_name, "-rev");
516 /* And revision needs to be uppercase */
517 for (i = 0; i < sizeof(desc->revision); i++)
518 desc->revision[i] = toupper(desc->revision[i]);
520 strcat(board_local_name, desc->revision);
525 * Longer strings will end up with buffer overflow and potential
526 * attacks that's why check it
528 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
529 panic("Board name can't be determined\n");
531 if (strlen(board_local_name))
532 return board_local_name;
535 free(board_local_name);
539 bool __maybe_unused __weak board_detection(void)
541 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
544 ret = xilinx_read_eeprom();
545 return !ret ? true : false;
551 int embedded_dtb_select(void)
553 if (board_detection()) {
554 char *board_local_name;
556 board_local_name = board_name_decode();
557 if (board_local_name) {
558 board_name = board_local_name;
559 printf("Detected name: %s\n", board_name);
561 /* Time to change DTB on fly */
562 /* Both ways should work here */
563 /* fdtdec_resetup(&rescan); */