1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 - 2020 Xilinx, Inc.
4 * Michal Simek <michal.simek@xilinx.com>
10 #include <asm/sections.h>
11 #include <dm/uclass.h>
13 #include <linux/sizes.h>
17 #include <i2c_eeprom.h>
19 #include <generated/dt.h>
23 #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
24 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
30 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
31 if (!ofnode_valid(eeprom))
34 debug("%s: Path to EEPROM %s\n", __func__,
35 ofnode_read_chosen_string("xlnx,eeprom"));
37 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
41 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
43 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
45 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
51 #define EEPROM_HEADER_MAGIC 0xdaaddeed
52 #define EEPROM_HDR_MANUFACTURER_LEN 16
53 #define EEPROM_HDR_NAME_LEN 16
54 #define EEPROM_HDR_REV_LEN 8
55 #define EEPROM_HDR_SERIAL_LEN 20
56 #define EEPROM_HDR_NO_OF_MAC_ADDR 4
57 #define EEPROM_HDR_ETH_ALEN ETH_ALEN
59 struct xilinx_board_description {
61 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
62 char name[EEPROM_HDR_NAME_LEN + 1];
63 char revision[EEPROM_HDR_REV_LEN + 1];
64 char serial[EEPROM_HDR_SERIAL_LEN + 1];
65 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
68 static int highest_id = -1;
69 static struct xilinx_board_description **board_info;
71 #define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
73 /* Variable which stores pointer to array which stores eeprom content */
74 struct xilinx_legacy_format {
75 char board_sn[18]; /* 0x0 */
76 char unused0[14]; /* 0x12 */
77 char eth_mac[6]; /* 0x20 */
78 char unused1[170]; /* 0x26 */
79 char board_name[11]; /* 0xd0 */
80 char unused2[5]; /* 0xdc */
81 char board_revision[3]; /* 0xe0 */
82 char unused3[29]; /* 0xe3 */
85 static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
90 for (i = 0; i < size; i++) {
93 /* Remove all ffs and spaces */
94 if (byte == 0xff || byte == ' ')
97 /* Convert strings to lower case */
98 if (byte >= 'A' && byte <= 'Z')
99 eeprom[i] = byte + 'a' - 'A';
103 static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
104 struct xilinx_board_description *desc)
107 struct xilinx_legacy_format *eeprom_content;
108 bool eth_valid = false;
110 size = sizeof(*eeprom_content);
112 eeprom_content = calloc(1, size);
116 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
119 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
121 debug("%s: I2C EEPROM read failed\n", __func__);
122 free(eeprom_content);
126 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
128 printf("Xilinx I2C Legacy format at %s:\n", name);
129 printf(" Board name:\t%s\n", eeprom_content->board_name);
130 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
131 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
133 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
135 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
137 /* Terminating \0 chars ensure end of string */
138 strcpy(desc->name, eeprom_content->board_name);
139 strcpy(desc->revision, eeprom_content->board_revision);
140 strcpy(desc->serial, eeprom_content->board_sn);
142 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
144 desc->header = EEPROM_HEADER_MAGIC;
146 free(eeprom_content);
151 static bool xilinx_detect_legacy(u8 *buffer)
156 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
159 if (c < '0' || c > '9')
166 static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
167 struct xilinx_board_description *desc)
169 int ret, eeprom_size;
172 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
173 eeprom_size = i2c_eeprom_size(dev);
175 fru_content = calloc(1, eeprom_size);
179 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
182 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
185 debug("%s: I2C EEPROM read failed\n", __func__);
190 printf("Xilinx I2C FRU format at %s:\n", name);
191 fru_capture((unsigned long)fru_content);
192 ret = fru_display(0);
194 printf("FRU format decoding failed.\n");
198 if (desc->header == EEPROM_HEADER_MAGIC) {
199 debug("Information already filled\n");
203 /* It is clear that FRU was captured and structures were filled */
204 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
205 sizeof(desc->manufacturer));
206 strncpy(desc->name, (char *)fru_data.brd.product_name,
208 strncpy(desc->revision, (char *)fru_data.brd.rev,
209 sizeof(desc->revision));
210 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
211 sizeof(desc->serial));
212 desc->header = EEPROM_HEADER_MAGIC;
217 static bool xilinx_detect_fru(u8 *buffer)
222 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
224 debug("%s Common header CRC FAIL\n", __func__);
228 bool all_zeros = true;
229 /* Checksum over all zeros is also zero that's why detect this case */
230 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
238 debug("%s Common header CRC PASS\n", __func__);
242 static int xilinx_read_eeprom_single(char *name,
243 struct xilinx_board_description *desc)
248 u8 buffer[XILINX_I2C_DETECTION_BITS];
250 eeprom = ofnode_get_aliases_node(name);
251 if (!ofnode_valid(eeprom))
254 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
258 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
260 debug("%s: I2C EEPROM read failed\n", __func__);
264 debug("%s: i2c memory detected: %s\n", __func__, name);
266 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
267 return xilinx_read_eeprom_fru(dev, name, desc);
269 if (xilinx_detect_legacy(buffer))
270 return xilinx_read_eeprom_legacy(dev, name, desc);
275 __maybe_unused int xilinx_read_eeprom(void)
278 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
279 struct xilinx_board_description *desc;
281 highest_id = dev_read_alias_highest_id("nvmem");
282 /* No nvmem aliases present */
286 board_info = calloc(1, sizeof(desc) * highest_id);
290 debug("%s: Highest ID %d, board_info %p\n", __func__,
291 highest_id, board_info);
293 for (id = 0; id <= highest_id; id++) {
294 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
296 /* Alloc structure */
297 desc = board_info[id];
299 desc = calloc(1, sizeof(*desc));
303 board_info[id] = desc;
306 /* Ignoring return value for supporting multiple chips */
307 ret = xilinx_read_eeprom_single(name_buf, desc);
310 board_info[id] = NULL;
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(void)
327 #if !defined(CONFIG_VERSAL_NO_DDR) && !defined(CONFIG_ZYNQMP_NO_DDR)
328 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
330 if (fdt_magic(fdt_blob) == FDT_MAGIC)
333 debug("DTB is not passed via %p\n", fdt_blob);
336 #ifdef CONFIG_SPL_BUILD
337 /* FDT is at end of BSS unless it is in a different memory region */
338 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
339 fdt_blob = (ulong *)&_image_binary_end;
341 fdt_blob = (ulong *)&__bss_end;
343 /* FDT is at end of image */
344 fdt_blob = (ulong *)&_end;
347 if (fdt_magic(fdt_blob) == FDT_MAGIC)
350 debug("DTB is also not passed via %p\n", fdt_blob);
356 #if defined(CONFIG_BOARD_LATE_INIT)
357 static int env_set_by_index(const char *name, int index, char *data)
362 sprintf(var, "board_%s", name);
364 sprintf(var, "card%d_%s", index, name);
366 return env_set(var, data);
369 int board_late_init_xilinx(void)
372 int i, id, macid = 0;
373 struct xilinx_board_description *desc;
374 phys_size_t bootm_size = gd->ram_size;
375 struct bd_info *bd = gd->bd;
377 if (!CONFIG_IS_ENABLED(MICROBLAZE) && bd->bi_dram[0].start) {
380 scriptaddr = env_get_hex("scriptaddr", 0);
381 ret |= env_set_hex("scriptaddr",
382 bd->bi_dram[0].start + scriptaddr);
385 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
386 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
388 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
390 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
391 ret |= env_set_addr("bootm_size", (void *)bootm_size);
393 for (id = 0; id <= highest_id; id++) {
394 desc = board_info[id];
395 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
396 if (desc->manufacturer[0])
397 ret |= env_set_by_index("manufacturer", id,
400 ret |= env_set_by_index("name", id,
402 if (desc->revision[0])
403 ret |= env_set_by_index("rev", id,
406 ret |= env_set_by_index("serial", id,
409 if (!CONFIG_IS_ENABLED(NET))
412 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
413 if (!desc->mac_addr[i])
416 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
417 ret |= eth_env_set_enetaddr_by_index("eth",
418 macid++, desc->mac_addr[i]);
424 printf("%s: Saving run time variables FAILED\n", __func__);
430 int __maybe_unused board_fit_config_name_match(const char *name)
432 debug("%s: Check %s, default %s\n", __func__, name, DEVICE_TREE);
434 if (!strcmp(name, DEVICE_TREE))