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>
22 #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
23 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
29 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
30 if (!ofnode_valid(eeprom))
33 debug("%s: Path to EEPROM %s\n", __func__,
34 ofnode_read_chosen_string("xlnx,eeprom"));
36 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
40 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
42 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
44 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
50 #define EEPROM_HEADER_MAGIC 0xdaaddeed
51 #define EEPROM_HDR_MANUFACTURER_LEN 16
52 #define EEPROM_HDR_NAME_LEN 16
53 #define EEPROM_HDR_REV_LEN 8
54 #define EEPROM_HDR_SERIAL_LEN 20
55 #define EEPROM_HDR_NO_OF_MAC_ADDR 4
56 #define EEPROM_HDR_ETH_ALEN ETH_ALEN
58 struct xilinx_board_description {
60 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
61 char name[EEPROM_HDR_NAME_LEN + 1];
62 char revision[EEPROM_HDR_REV_LEN + 1];
63 char serial[EEPROM_HDR_SERIAL_LEN + 1];
64 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
67 static int highest_id = -1;
68 static struct xilinx_board_description **board_info;
70 #define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
72 /* Variable which stores pointer to array which stores eeprom content */
73 struct xilinx_legacy_format {
74 char board_sn[18]; /* 0x0 */
75 char unused0[14]; /* 0x12 */
76 char eth_mac[6]; /* 0x20 */
77 char unused1[170]; /* 0x26 */
78 char board_name[11]; /* 0xd0 */
79 char unused2[5]; /* 0xdc */
80 char board_revision[3]; /* 0xe0 */
81 char unused3[29]; /* 0xe3 */
84 static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
89 for (i = 0; i < size; i++) {
92 /* Remove all ffs and spaces */
93 if (byte == 0xff || byte == ' ')
96 /* Convert strings to lower case */
97 if (byte >= 'A' && byte <= 'Z')
98 eeprom[i] = byte + 'a' - 'A';
102 static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
103 struct xilinx_board_description *desc)
106 struct xilinx_legacy_format *eeprom_content;
107 bool eth_valid = false;
109 size = sizeof(*eeprom_content);
111 eeprom_content = calloc(1, size);
115 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
118 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
120 debug("%s: I2C EEPROM read failed\n", __func__);
121 free(eeprom_content);
125 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
127 printf("Xilinx I2C Legacy format at %s:\n", name);
128 printf(" Board name:\t%s\n", eeprom_content->board_name);
129 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
130 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
132 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
134 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
136 /* Terminating \0 chars ensure end of string */
137 strcpy(desc->name, eeprom_content->board_name);
138 strcpy(desc->revision, eeprom_content->board_revision);
139 strcpy(desc->serial, eeprom_content->board_sn);
141 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
143 desc->header = EEPROM_HEADER_MAGIC;
145 free(eeprom_content);
150 static bool xilinx_detect_legacy(u8 *buffer)
155 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
158 if (c < '0' || c > '9')
165 static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
166 struct xilinx_board_description *desc)
168 int ret, eeprom_size;
171 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
172 eeprom_size = i2c_eeprom_size(dev);
174 fru_content = calloc(1, eeprom_size);
178 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
181 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
184 debug("%s: I2C EEPROM read failed\n", __func__);
189 printf("Xilinx I2C FRU format at %s:\n", name);
190 fru_capture((unsigned long)fru_content);
191 ret = fru_display(0);
193 printf("FRU format decoding failed.\n");
197 if (desc->header == EEPROM_HEADER_MAGIC) {
198 debug("Information already filled\n");
202 /* It is clear that FRU was captured and structures were filled */
203 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
204 sizeof(desc->manufacturer));
205 strncpy(desc->name, (char *)fru_data.brd.product_name,
207 strncpy(desc->revision, (char *)fru_data.brd.rev,
208 sizeof(desc->revision));
209 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
210 sizeof(desc->serial));
211 desc->header = EEPROM_HEADER_MAGIC;
216 static bool xilinx_detect_fru(u8 *buffer)
221 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
223 debug("%s Common header CRC FAIL\n", __func__);
227 bool all_zeros = true;
228 /* Checksum over all zeros is also zero that's why detect this case */
229 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
237 debug("%s Common header CRC PASS\n", __func__);
241 static int xilinx_read_eeprom_single(char *name,
242 struct xilinx_board_description *desc)
247 u8 buffer[XILINX_I2C_DETECTION_BITS];
249 eeprom = ofnode_get_aliases_node(name);
250 if (!ofnode_valid(eeprom))
253 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
257 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
259 debug("%s: I2C EEPROM read failed\n", __func__);
263 debug("%s: i2c memory detected: %s\n", __func__, name);
265 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
266 return xilinx_read_eeprom_fru(dev, name, desc);
268 if (xilinx_detect_legacy(buffer))
269 return xilinx_read_eeprom_legacy(dev, name, desc);
274 __maybe_unused int xilinx_read_eeprom(void)
277 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
278 struct xilinx_board_description *desc;
280 highest_id = dev_read_alias_highest_id("nvmem");
281 /* No nvmem aliases present */
285 board_info = calloc(1, sizeof(desc) * highest_id);
289 debug("%s: Highest ID %d, board_info %p\n", __func__,
290 highest_id, board_info);
292 for (id = 0; id <= highest_id; id++) {
293 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
295 /* Alloc structure */
296 desc = board_info[id];
298 desc = calloc(1, sizeof(*desc));
302 board_info[id] = desc;
305 /* Ignoring return value for supporting multiple chips */
306 ret = xilinx_read_eeprom_single(name_buf, desc);
309 board_info[id] = NULL;
314 * Consider to clean board_info structure when board/cards are not
321 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
322 void *board_fdt_blob_setup(void)
326 #if !defined(CONFIG_VERSAL_NO_DDR) && !defined(CONFIG_ZYNQMP_NO_DDR)
327 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
329 if (fdt_magic(fdt_blob) == FDT_MAGIC)
332 debug("DTB is not passed via %p\n", fdt_blob);
335 #ifdef CONFIG_SPL_BUILD
336 /* FDT is at end of BSS unless it is in a different memory region */
337 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
338 fdt_blob = (ulong *)&_image_binary_end;
340 fdt_blob = (ulong *)&__bss_end;
342 /* FDT is at end of image */
343 fdt_blob = (ulong *)&_end;
346 if (fdt_magic(fdt_blob) == FDT_MAGIC)
349 debug("DTB is also not passed via %p\n", fdt_blob);
355 #if defined(CONFIG_BOARD_LATE_INIT)
356 static int env_set_by_index(const char *name, int index, char *data)
361 sprintf(var, "board_%s", name);
363 sprintf(var, "card%d_%s", index, name);
365 return env_set(var, data);
368 int board_late_init_xilinx(void)
371 int i, id, macid = 0;
372 struct xilinx_board_description *desc;
373 phys_size_t bootm_size = gd->ram_size;
375 if (CONFIG_IS_ENABLED(ARCH_ZYNQ))
376 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
378 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
380 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
381 ret |= env_set_addr("bootm_size", (void *)bootm_size);
383 for (id = 0; id <= highest_id; id++) {
384 desc = board_info[id];
385 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
386 if (desc->manufacturer[0])
387 ret |= env_set_by_index("manufacturer", id,
390 ret |= env_set_by_index("name", id,
392 if (desc->revision[0])
393 ret |= env_set_by_index("rev", id,
396 ret |= env_set_by_index("serial", id,
399 if (!CONFIG_IS_ENABLED(NET))
402 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
403 if (!desc->mac_addr[i])
406 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
407 ret |= eth_env_set_enetaddr_by_index("eth",
408 macid++, desc->mac_addr[i]);
414 printf("%s: Saving run time variables FAILED\n", __func__);