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>
24 #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
25 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
31 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
32 if (!ofnode_valid(eeprom))
35 debug("%s: Path to EEPROM %s\n", __func__,
36 ofnode_read_chosen_string("xlnx,eeprom"));
38 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
42 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
44 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
46 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
52 #define EEPROM_HEADER_MAGIC 0xdaaddeed
53 #define EEPROM_HDR_MANUFACTURER_LEN 16
54 #define EEPROM_HDR_NAME_LEN 16
55 #define EEPROM_HDR_REV_LEN 8
56 #define EEPROM_HDR_SERIAL_LEN 20
57 #define EEPROM_HDR_NO_OF_MAC_ADDR 4
58 #define EEPROM_HDR_ETH_ALEN ETH_ALEN
60 struct xilinx_board_description {
62 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
63 char name[EEPROM_HDR_NAME_LEN + 1];
64 char revision[EEPROM_HDR_REV_LEN + 1];
65 char serial[EEPROM_HDR_SERIAL_LEN + 1];
66 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
69 static int highest_id = -1;
70 static struct xilinx_board_description **board_info;
72 #define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
74 /* Variable which stores pointer to array which stores eeprom content */
75 struct xilinx_legacy_format {
76 char board_sn[18]; /* 0x0 */
77 char unused0[14]; /* 0x12 */
78 char eth_mac[6]; /* 0x20 */
79 char unused1[170]; /* 0x26 */
80 char board_name[11]; /* 0xd0 */
81 char unused2[5]; /* 0xdc */
82 char board_revision[3]; /* 0xe0 */
83 char unused3[29]; /* 0xe3 */
86 static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
91 for (i = 0; i < size; i++) {
94 /* Remove all ffs and spaces */
95 if (byte == 0xff || byte == ' ')
98 /* Convert strings to lower case */
99 if (byte >= 'A' && byte <= 'Z')
100 eeprom[i] = byte + 'a' - 'A';
104 static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
105 struct xilinx_board_description *desc)
108 struct xilinx_legacy_format *eeprom_content;
109 bool eth_valid = false;
111 size = sizeof(*eeprom_content);
113 eeprom_content = calloc(1, size);
117 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
120 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
122 debug("%s: I2C EEPROM read failed\n", __func__);
123 free(eeprom_content);
127 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
129 printf("Xilinx I2C Legacy format at %s:\n", name);
130 printf(" Board name:\t%s\n", eeprom_content->board_name);
131 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
132 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
134 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
136 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
138 /* Terminating \0 chars ensure end of string */
139 strcpy(desc->name, eeprom_content->board_name);
140 strcpy(desc->revision, eeprom_content->board_revision);
141 strcpy(desc->serial, eeprom_content->board_sn);
143 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
145 desc->header = EEPROM_HEADER_MAGIC;
147 free(eeprom_content);
152 static bool xilinx_detect_legacy(u8 *buffer)
157 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
160 if (c < '0' || c > '9')
167 static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
168 struct xilinx_board_description *desc)
170 int ret, eeprom_size;
173 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
174 eeprom_size = i2c_eeprom_size(dev);
176 fru_content = calloc(1, eeprom_size);
180 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
183 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
186 debug("%s: I2C EEPROM read failed\n", __func__);
191 printf("Xilinx I2C FRU format at %s:\n", name);
192 fru_capture((unsigned long)fru_content);
193 ret = fru_display(0);
195 printf("FRU format decoding failed.\n");
199 if (desc->header == EEPROM_HEADER_MAGIC) {
200 debug("Information already filled\n");
204 /* It is clear that FRU was captured and structures were filled */
205 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
206 sizeof(desc->manufacturer));
207 strncpy(desc->name, (char *)fru_data.brd.product_name,
209 strncpy(desc->revision, (char *)fru_data.brd.rev,
210 sizeof(desc->revision));
211 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
212 sizeof(desc->serial));
213 desc->header = EEPROM_HEADER_MAGIC;
218 static bool xilinx_detect_fru(u8 *buffer)
223 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
225 debug("%s Common header CRC FAIL\n", __func__);
229 bool all_zeros = true;
230 /* Checksum over all zeros is also zero that's why detect this case */
231 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
239 debug("%s Common header CRC PASS\n", __func__);
243 static int xilinx_read_eeprom_single(char *name,
244 struct xilinx_board_description *desc)
249 u8 buffer[XILINX_I2C_DETECTION_BITS];
251 eeprom = ofnode_get_aliases_node(name);
252 if (!ofnode_valid(eeprom))
255 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
259 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
261 debug("%s: I2C EEPROM read failed\n", __func__);
265 debug("%s: i2c memory detected: %s\n", __func__, name);
267 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
268 return xilinx_read_eeprom_fru(dev, name, desc);
270 if (xilinx_detect_legacy(buffer))
271 return xilinx_read_eeprom_legacy(dev, name, desc);
276 __maybe_unused int xilinx_read_eeprom(void)
279 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
280 struct xilinx_board_description *desc;
282 highest_id = dev_read_alias_highest_id("nvmem");
283 /* No nvmem aliases present */
287 board_info = calloc(1, sizeof(desc) * highest_id);
291 debug("%s: Highest ID %d, board_info %p\n", __func__,
292 highest_id, board_info);
294 for (id = 0; id <= highest_id; id++) {
295 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
297 /* Alloc structure */
298 desc = board_info[id];
300 desc = calloc(1, sizeof(*desc));
304 board_info[id] = desc;
307 /* Ignoring return value for supporting multiple chips */
308 ret = xilinx_read_eeprom_single(name_buf, desc);
311 board_info[id] = NULL;
316 * Consider to clean board_info structure when board/cards are not
323 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
324 void *board_fdt_blob_setup(void)
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_size;
381 struct bd_info *bd = gd->bd;
383 if (!CONFIG_IS_ENABLED(MICROBLAZE) && bd->bi_dram[0].start) {
386 scriptaddr = env_get_hex("scriptaddr", 0);
387 ret |= env_set_hex("scriptaddr",
388 bd->bi_dram[0].start + scriptaddr);
391 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
392 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
394 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
396 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
397 ret |= env_set_addr("bootm_size", (void *)bootm_size);
399 for (id = 0; id <= highest_id; id++) {
400 desc = board_info[id];
401 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
402 if (desc->manufacturer[0])
403 ret |= env_set_by_index("manufacturer", id,
406 ret |= env_set_by_index("name", id,
408 if (desc->revision[0])
409 ret |= env_set_by_index("rev", id,
412 ret |= env_set_by_index("serial", id,
415 if (!CONFIG_IS_ENABLED(NET))
418 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
419 if (!desc->mac_addr[i])
422 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
423 ret |= eth_env_set_enetaddr_by_index("eth",
424 macid++, desc->mac_addr[i]);
430 printf("%s: Saving run time variables FAILED\n", __func__);
436 int __maybe_unused board_fit_config_name_match(const char *name)
438 debug("%s: Check %s, default %s\n", __func__, name, DEVICE_TREE);
440 if (!strcmp(name, DEVICE_TREE))