blob: 5f2afb9def4eef68441198bfc161c4c6f52dae9d [file] [log] [blame]
Michal Simek014d3042019-01-21 15:25:02 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Michal Simek394ee242020-08-03 13:01:45 +02003 * (C) Copyright 2014 - 2020 Xilinx, Inc.
Michal Simek014d3042019-01-21 15:25:02 +01004 * Michal Simek <michal.simek@xilinx.com>
5 */
6
7#include <common.h>
Sughosh Ganuccb36462022-04-15 11:29:34 +05308#include <efi.h>
9#include <efi_loader.h>
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Michal Simek878ba362019-12-19 17:45:15 +010013#include <asm/sections.h>
Michal Simek014d3042019-01-21 15:25:02 +010014#include <dm/uclass.h>
15#include <i2c.h>
Michal Simekb90b97e2020-04-08 10:51:36 +020016#include <linux/sizes.h>
Michal Simek394ee242020-08-03 13:01:45 +020017#include <malloc.h>
Michal Simek705d44a2020-03-31 12:39:37 +020018#include "board.h"
Michal Simek394ee242020-08-03 13:01:45 +020019#include <dm.h>
20#include <i2c_eeprom.h>
21#include <net.h>
Michal Simekfe49df92020-10-26 12:26:13 +010022#include <generated/dt.h>
T Karthik Reddyc8fa40a2021-08-10 06:50:20 -060023#include <soc.h>
Michal Simeke0453512021-08-11 14:23:54 +020024#include <linux/ctype.h>
Sughosh Ganuccb36462022-04-15 11:29:34 +053025#include <linux/kernel.h>
Michal Simek014d3042019-01-21 15:25:02 +010026
Michal Simek207e0622020-08-03 16:14:23 +020027#include "fru.h"
28
Sughosh Ganuccb36462022-04-15 11:29:34 +053029#if CONFIG_IS_ENABLED(EFI_HAVE_CAPSULE_SUPPORT)
30struct efi_fw_image fw_images[] = {
31#if defined(XILINX_BOOT_IMAGE_GUID)
32 {
33 .image_type_id = XILINX_BOOT_IMAGE_GUID,
34 .fw_name = u"XILINX-BOOT",
35 .image_index = 1,
36 },
37#endif
38#if defined(XILINX_UBOOT_IMAGE_GUID)
39 {
40 .image_type_id = XILINX_UBOOT_IMAGE_GUID,
41 .fw_name = u"XILINX-UBOOT",
42 .image_index = 2,
43 },
44#endif
45};
46
47struct efi_capsule_update_info update_info = {
48 .images = fw_images,
49};
50
51u8 num_image_type_guids = ARRAY_SIZE(fw_images);
52#endif /* EFI_HAVE_CAPSULE_SUPPORT */
53
Michal Simekaee22b32020-08-03 12:59:28 +020054#if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
Michal Simek014d3042019-01-21 15:25:02 +010055int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
56{
Michal Simek13db6162019-01-21 16:29:07 +010057 int ret = -EINVAL;
Michal Simek13db6162019-01-21 16:29:07 +010058 struct udevice *dev;
59 ofnode eeprom;
60
61 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
62 if (!ofnode_valid(eeprom))
63 return -ENODEV;
64
65 debug("%s: Path to EEPROM %s\n", __func__,
Simon Glassf3455962020-01-27 08:49:43 -070066 ofnode_read_chosen_string("xlnx,eeprom"));
Michal Simek13db6162019-01-21 16:29:07 +010067
68 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
69 if (ret)
70 return ret;
71
72 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
73 if (ret)
74 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
75 else
76 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
Michal Simek13db6162019-01-21 16:29:07 +010077
78 return ret;
79}
Michal Simekaee22b32020-08-03 12:59:28 +020080#endif
Ibai Erkiaga6f658202019-10-02 15:57:36 +010081
Michal Simek394ee242020-08-03 13:01:45 +020082#define EEPROM_HEADER_MAGIC 0xdaaddeed
83#define EEPROM_HDR_MANUFACTURER_LEN 16
84#define EEPROM_HDR_NAME_LEN 16
85#define EEPROM_HDR_REV_LEN 8
86#define EEPROM_HDR_SERIAL_LEN 20
87#define EEPROM_HDR_NO_OF_MAC_ADDR 4
88#define EEPROM_HDR_ETH_ALEN ETH_ALEN
89
90struct xilinx_board_description {
91 u32 header;
92 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
93 char name[EEPROM_HDR_NAME_LEN + 1];
94 char revision[EEPROM_HDR_REV_LEN + 1];
95 char serial[EEPROM_HDR_SERIAL_LEN + 1];
96 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
97};
98
Michal Simek0bb24e12020-08-03 12:57:05 +020099static int highest_id = -1;
Michal Simeke98ae1d2021-08-12 12:30:36 +0200100static struct xilinx_board_description *board_info;
Michal Simek394ee242020-08-03 13:01:45 +0200101
Michal Simek207e0622020-08-03 16:14:23 +0200102#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simek394ee242020-08-03 13:01:45 +0200103
104/* Variable which stores pointer to array which stores eeprom content */
105struct xilinx_legacy_format {
106 char board_sn[18]; /* 0x0 */
107 char unused0[14]; /* 0x12 */
108 char eth_mac[6]; /* 0x20 */
109 char unused1[170]; /* 0x26 */
110 char board_name[11]; /* 0xd0 */
111 char unused2[5]; /* 0xdc */
112 char board_revision[3]; /* 0xe0 */
113 char unused3[29]; /* 0xe3 */
114};
115
116static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
117{
118 int i;
119 char byte;
120
121 for (i = 0; i < size; i++) {
122 byte = eeprom[i];
123
124 /* Remove all ffs and spaces */
125 if (byte == 0xff || byte == ' ')
126 eeprom[i] = 0;
127
128 /* Convert strings to lower case */
129 if (byte >= 'A' && byte <= 'Z')
130 eeprom[i] = byte + 'a' - 'A';
131 }
132}
133
134static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
135 struct xilinx_board_description *desc)
136{
137 int ret, size;
138 struct xilinx_legacy_format *eeprom_content;
139 bool eth_valid = false;
140
141 size = sizeof(*eeprom_content);
142
143 eeprom_content = calloc(1, size);
144 if (!eeprom_content)
145 return -ENOMEM;
146
147 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
148 eeprom_content);
149
150 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
151 if (ret) {
152 debug("%s: I2C EEPROM read failed\n", __func__);
153 free(eeprom_content);
154 return ret;
155 }
156
157 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
158
159 printf("Xilinx I2C Legacy format at %s:\n", name);
160 printf(" Board name:\t%s\n", eeprom_content->board_name);
161 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
162 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
163
164 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
165 if (eth_valid)
166 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
167
168 /* Terminating \0 chars ensure end of string */
169 strcpy(desc->name, eeprom_content->board_name);
170 strcpy(desc->revision, eeprom_content->board_revision);
171 strcpy(desc->serial, eeprom_content->board_sn);
172 if (eth_valid)
173 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
174
175 desc->header = EEPROM_HEADER_MAGIC;
176
177 free(eeprom_content);
178
179 return ret;
180}
181
182static bool xilinx_detect_legacy(u8 *buffer)
183{
184 int i;
185 char c;
186
187 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
188 c = buffer[i];
189
190 if (c < '0' || c > '9')
191 return false;
192 }
193
194 return true;
195}
196
Michal Simek207e0622020-08-03 16:14:23 +0200197static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
198 struct xilinx_board_description *desc)
199{
Michal Simekbe5f5722021-08-12 11:03:49 +0200200 int i, ret, eeprom_size;
Michal Simek207e0622020-08-03 16:14:23 +0200201 u8 *fru_content;
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100202 u8 id = 0;
Michal Simek207e0622020-08-03 16:14:23 +0200203
204 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
205 eeprom_size = i2c_eeprom_size(dev);
206
207 fru_content = calloc(1, eeprom_size);
208 if (!fru_content)
209 return -ENOMEM;
210
211 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
212 fru_content);
213
214 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
215 eeprom_size);
216 if (ret) {
217 debug("%s: I2C EEPROM read failed\n", __func__);
Michal Simekaada8a62021-08-13 09:17:10 +0200218 goto end;
Michal Simek207e0622020-08-03 16:14:23 +0200219 }
220
Michal Simek207e0622020-08-03 16:14:23 +0200221 fru_capture((unsigned long)fru_content);
Michal Simeke0453512021-08-11 14:23:54 +0200222 if (gd->flags & GD_FLG_RELOC || (_DEBUG && CONFIG_IS_ENABLED(DTB_RESELECT))) {
223 printf("Xilinx I2C FRU format at %s:\n", name);
224 ret = fru_display(0);
225 if (ret) {
226 printf("FRU format decoding failed.\n");
227 goto end;
228 }
Michal Simek207e0622020-08-03 16:14:23 +0200229 }
230
231 if (desc->header == EEPROM_HEADER_MAGIC) {
232 debug("Information already filled\n");
Michal Simekaada8a62021-08-13 09:17:10 +0200233 ret = -EINVAL;
234 goto end;
Michal Simek207e0622020-08-03 16:14:23 +0200235 }
236
237 /* It is clear that FRU was captured and structures were filled */
238 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
239 sizeof(desc->manufacturer));
240 strncpy(desc->name, (char *)fru_data.brd.product_name,
241 sizeof(desc->name));
Michal Simekbe5f5722021-08-12 11:03:49 +0200242 for (i = 0; i < sizeof(desc->name); i++) {
243 if (desc->name[i] == ' ')
244 desc->name[i] = '\0';
245 }
Michal Simek207e0622020-08-03 16:14:23 +0200246 strncpy(desc->revision, (char *)fru_data.brd.rev,
247 sizeof(desc->revision));
Michal Simek904fb972022-06-06 09:31:27 +0200248 for (i = 0; i < sizeof(desc->revision); i++) {
249 if (desc->revision[i] == ' ')
250 desc->revision[i] = '\0';
251 }
Michal Simek207e0622020-08-03 16:14:23 +0200252 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
253 sizeof(desc->serial));
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100254
255 while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
256 if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
257 memcpy(&desc->mac_addr[id],
258 (char *)fru_data.mac.macid[id], ETH_ALEN);
259 id++;
260 }
261
Michal Simek207e0622020-08-03 16:14:23 +0200262 desc->header = EEPROM_HEADER_MAGIC;
263
Michal Simekaada8a62021-08-13 09:17:10 +0200264end:
265 free(fru_content);
266 return ret;
Michal Simek207e0622020-08-03 16:14:23 +0200267}
268
269static bool xilinx_detect_fru(u8 *buffer)
270{
271 u8 checksum = 0;
272 int i;
273
274 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
275 if (checksum) {
276 debug("%s Common header CRC FAIL\n", __func__);
277 return false;
278 }
279
280 bool all_zeros = true;
281 /* Checksum over all zeros is also zero that's why detect this case */
282 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
283 if (buffer[i] != 0)
284 all_zeros = false;
285 }
286
287 if (all_zeros)
288 return false;
289
290 debug("%s Common header CRC PASS\n", __func__);
291 return true;
292}
293
Michal Simek394ee242020-08-03 13:01:45 +0200294static int xilinx_read_eeprom_single(char *name,
295 struct xilinx_board_description *desc)
296{
297 int ret;
298 struct udevice *dev;
299 ofnode eeprom;
300 u8 buffer[XILINX_I2C_DETECTION_BITS];
301
302 eeprom = ofnode_get_aliases_node(name);
303 if (!ofnode_valid(eeprom))
304 return -ENODEV;
305
306 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
307 if (ret)
308 return ret;
309
310 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
311 if (ret) {
312 debug("%s: I2C EEPROM read failed\n", __func__);
313 return ret;
314 }
315
316 debug("%s: i2c memory detected: %s\n", __func__, name);
317
Michal Simek207e0622020-08-03 16:14:23 +0200318 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
319 return xilinx_read_eeprom_fru(dev, name, desc);
320
Michal Simek394ee242020-08-03 13:01:45 +0200321 if (xilinx_detect_legacy(buffer))
322 return xilinx_read_eeprom_legacy(dev, name, desc);
323
324 return -ENODEV;
325}
326
327__maybe_unused int xilinx_read_eeprom(void)
328{
Michal Simeke98ae1d2021-08-12 12:30:36 +0200329 int id;
Michal Simek394ee242020-08-03 13:01:45 +0200330 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simek0bb24e12020-08-03 12:57:05 +0200331 struct xilinx_board_description *desc;
Michal Simek394ee242020-08-03 13:01:45 +0200332
333 highest_id = dev_read_alias_highest_id("nvmem");
334 /* No nvmem aliases present */
335 if (highest_id < 0)
336 return -EINVAL;
337
Michal Simeke98ae1d2021-08-12 12:30:36 +0200338 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
Michal Simek394ee242020-08-03 13:01:45 +0200339 if (!board_info)
340 return -ENOMEM;
341
342 debug("%s: Highest ID %d, board_info %p\n", __func__,
343 highest_id, board_info);
344
345 for (id = 0; id <= highest_id; id++) {
346 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
347
Michal Simek0bb24e12020-08-03 12:57:05 +0200348 /* Alloc structure */
Michal Simeke98ae1d2021-08-12 12:30:36 +0200349 desc = &board_info[id];
Michal Simek0bb24e12020-08-03 12:57:05 +0200350
Michal Simek394ee242020-08-03 13:01:45 +0200351 /* Ignoring return value for supporting multiple chips */
Michal Simeke98ae1d2021-08-12 12:30:36 +0200352 xilinx_read_eeprom_single(name_buf, desc);
Michal Simek394ee242020-08-03 13:01:45 +0200353 }
354
Michal Simek0bb24e12020-08-03 12:57:05 +0200355 /*
356 * Consider to clean board_info structure when board/cards are not
357 * detected.
358 */
Michal Simek394ee242020-08-03 13:01:45 +0200359
360 return 0;
361}
362
Michal Simek1a505292022-02-17 14:28:38 +0100363#if defined(CONFIG_OF_BOARD)
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300364void *board_fdt_blob_setup(int *err)
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100365{
Michal Simekddf69ae2020-09-04 16:21:47 +0200366 void *fdt_blob;
Michal Simekc89f4292020-03-19 10:23:56 +0100367
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300368 *err = 0;
Michal Simek663c1622021-01-04 11:07:28 +0100369 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
370 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simek6d4317b2021-02-02 13:33:22 +0100371 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek9b9d01e2021-01-04 11:03:36 +0100372 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100373
Michal Simek9b9d01e2021-01-04 11:03:36 +0100374 if (fdt_magic(fdt_blob) == FDT_MAGIC)
375 return fdt_blob;
Michal Simek878ba362019-12-19 17:45:15 +0100376
Michal Simek9b9d01e2021-01-04 11:03:36 +0100377 debug("DTB is not passed via %p\n", fdt_blob);
378 }
Michal Simek878ba362019-12-19 17:45:15 +0100379
Michal Simek9b9d01e2021-01-04 11:03:36 +0100380 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
381 /*
382 * FDT is at end of BSS unless it is in a different memory
383 * region
384 */
385 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
386 fdt_blob = (ulong *)&_image_binary_end;
387 else
388 fdt_blob = (ulong *)&__bss_end;
389 } else {
390 /* FDT is at end of image */
391 fdt_blob = (ulong *)&_end;
392 }
Michal Simek878ba362019-12-19 17:45:15 +0100393
394 if (fdt_magic(fdt_blob) == FDT_MAGIC)
395 return fdt_blob;
396
397 debug("DTB is also not passed via %p\n", fdt_blob);
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100398
Michal Simek1a505292022-02-17 14:28:38 +0100399 *err = -EINVAL;
Michal Simek878ba362019-12-19 17:45:15 +0100400 return NULL;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100401}
402#endif
Michal Simek705d44a2020-03-31 12:39:37 +0200403
Michal Simek67ed85d2020-10-22 11:14:20 +0200404#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek0bb24e12020-08-03 12:57:05 +0200405static int env_set_by_index(const char *name, int index, char *data)
406{
407 char var[32];
408
409 if (!index)
410 sprintf(var, "board_%s", name);
411 else
412 sprintf(var, "card%d_%s", index, name);
413
414 return env_set(var, data);
415}
416
Michal Simek705d44a2020-03-31 12:39:37 +0200417int board_late_init_xilinx(void)
418{
Michal Simekbd07b5d2020-08-12 12:16:49 +0200419 u32 ret = 0;
Michal Simek0bb24e12020-08-03 12:57:05 +0200420 int i, id, macid = 0;
421 struct xilinx_board_description *desc;
Ricardo Salvetid35ccf22022-01-20 16:17:30 -0300422 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600423
T Karthik Reddy466cdde2021-04-02 01:49:17 -0600424 if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600425 ulong scriptaddr;
426
427 scriptaddr = env_get_hex("scriptaddr", 0);
T Karthik Reddy466cdde2021-04-02 01:49:17 -0600428 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600429 }
Michal Simekc86ce0c2020-08-12 12:17:53 +0200430
Michal Simek9ccfcae2020-10-23 07:54:18 +0200431 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simekc86ce0c2020-08-12 12:17:53 +0200432 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simekbd07b5d2020-08-12 12:16:49 +0200433
434 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
435
436 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simekc86ce0c2020-08-12 12:17:53 +0200437 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simek705d44a2020-03-31 12:39:37 +0200438
Michal Simek0bb24e12020-08-03 12:57:05 +0200439 for (id = 0; id <= highest_id; id++) {
Michal Simeke98ae1d2021-08-12 12:30:36 +0200440 desc = &board_info[id];
Michal Simek0bb24e12020-08-03 12:57:05 +0200441 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
442 if (desc->manufacturer[0])
443 ret |= env_set_by_index("manufacturer", id,
444 desc->manufacturer);
445 if (desc->name[0])
446 ret |= env_set_by_index("name", id,
447 desc->name);
448 if (desc->revision[0])
449 ret |= env_set_by_index("rev", id,
450 desc->revision);
451 if (desc->serial[0])
452 ret |= env_set_by_index("serial", id,
453 desc->serial);
Michal Simek394ee242020-08-03 13:01:45 +0200454
455 if (!CONFIG_IS_ENABLED(NET))
456 continue;
457
Michal Simek0bb24e12020-08-03 12:57:05 +0200458 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
459 if (!desc->mac_addr[i])
Ashok Reddy Soma2d3b1b72022-02-23 15:00:58 +0100460 break;
Michal Simek0bb24e12020-08-03 12:57:05 +0200461
462 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
463 ret |= eth_env_set_enetaddr_by_index("eth",
464 macid++, desc->mac_addr[i]);
465 }
Michal Simek394ee242020-08-03 13:01:45 +0200466 }
467 }
468
Michal Simekbd07b5d2020-08-12 12:16:49 +0200469 if (ret)
470 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simek8ba62d22020-07-09 15:57:56 +0200471
Michal Simek705d44a2020-03-31 12:39:37 +0200472 return 0;
473}
Michal Simek67ed85d2020-10-22 11:14:20 +0200474#endif
Michal Simekfe49df92020-10-26 12:26:13 +0100475
Michal Simekc88975e2021-07-23 09:55:59 +0200476static char *board_name = DEVICE_TREE;
477
Michal Simekfe49df92020-10-26 12:26:13 +0100478int __maybe_unused board_fit_config_name_match(const char *name)
479{
Michal Simekc88975e2021-07-23 09:55:59 +0200480 debug("%s: Check %s, default %s\n", __func__, name, board_name);
Michal Simekfe49df92020-10-26 12:26:13 +0100481
Michal Simekc88975e2021-07-23 09:55:59 +0200482 if (!strcmp(name, board_name))
Michal Simekfe49df92020-10-26 12:26:13 +0100483 return 0;
484
485 return -1;
486}
T Karthik Reddyc8fa40a2021-08-10 06:50:20 -0600487
Michal Simekafb28572021-07-23 09:59:59 +0200488#if CONFIG_IS_ENABLED(DTB_RESELECT)
Michal Simeke0453512021-08-11 14:23:54 +0200489#define MAX_NAME_LENGTH 50
490
Michal Simekafb28572021-07-23 09:59:59 +0200491char * __maybe_unused __weak board_name_decode(void)
492{
Michal Simeke0453512021-08-11 14:23:54 +0200493 char *board_local_name;
494 struct xilinx_board_description *desc;
495 int i, id;
496
497 board_local_name = calloc(1, MAX_NAME_LENGTH);
498 if (!board_info)
499 return NULL;
500
501 for (id = 0; id <= highest_id; id++) {
502 desc = &board_info[id];
503
504 /* No board description */
505 if (!desc)
506 goto error;
507
508 /* Board is not detected */
509 if (desc->header != EEPROM_HEADER_MAGIC)
510 continue;
511
512 /* The first string should be soc name */
513 if (!id)
514 strcat(board_local_name, CONFIG_SYS_BOARD);
515
516 /*
517 * For two purpose here:
518 * soc_name- eg: zynqmp-
519 * and between base board and CC eg: ..revA-sck...
520 */
521 strcat(board_local_name, "-");
522
523 if (desc->name[0]) {
524 /* For DT composition name needs to be lowercase */
525 for (i = 0; i < sizeof(desc->name); i++)
526 desc->name[i] = tolower(desc->name[i]);
527
528 strcat(board_local_name, desc->name);
529 }
530 if (desc->revision[0]) {
531 strcat(board_local_name, "-rev");
532
533 /* And revision needs to be uppercase */
534 for (i = 0; i < sizeof(desc->revision); i++)
535 desc->revision[i] = toupper(desc->revision[i]);
536
537 strcat(board_local_name, desc->revision);
538 }
539 }
540
541 /*
542 * Longer strings will end up with buffer overflow and potential
543 * attacks that's why check it
544 */
545 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
546 panic("Board name can't be determined\n");
547
548 if (strlen(board_local_name))
549 return board_local_name;
550
551error:
552 free(board_local_name);
Michal Simekafb28572021-07-23 09:59:59 +0200553 return NULL;
554}
555
556bool __maybe_unused __weak board_detection(void)
557{
Michal Simeke0453512021-08-11 14:23:54 +0200558 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
559 int ret;
560
561 ret = xilinx_read_eeprom();
562 return !ret ? true : false;
563 }
564
Michal Simekafb28572021-07-23 09:59:59 +0200565 return false;
566}
567
568int embedded_dtb_select(void)
569{
570 if (board_detection()) {
571 char *board_local_name;
572
573 board_local_name = board_name_decode();
574 if (board_local_name) {
575 board_name = board_local_name;
Michal Simeke0453512021-08-11 14:23:54 +0200576 printf("Detected name: %s\n", board_name);
Michal Simekafb28572021-07-23 09:59:59 +0200577
578 /* Time to change DTB on fly */
579 /* Both ways should work here */
580 /* fdtdec_resetup(&rescan); */
581 fdtdec_setup();
582 }
583 }
584 return 0;
585}
586#endif