blob: 99fdbac639b7dd2e5d9c6487de5a7b46d27dbbbd [file] [log] [blame]
Michal Simek9755e3d2019-01-21 15:25:02 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Michal Simekd61728c2020-08-03 13:01:45 +02003 * (C) Copyright 2014 - 2020 Xilinx, Inc.
Michal Simek9755e3d2019-01-21 15:25:02 +01004 * Michal Simek <michal.simek@xilinx.com>
5 */
6
7#include <common.h>
Sughosh Ganu741ef862022-04-15 11:29:34 +05308#include <efi.h>
9#include <efi_loader.h>
Simon Glass09140112020-05-10 11:40:03 -060010#include <env.h>
Michal Simeka32c3e92022-08-25 14:23:10 +020011#include <image.h>
12#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Michal Simekfc274a52019-12-19 17:45:15 +010015#include <asm/sections.h>
Michal Simek9755e3d2019-01-21 15:25:02 +010016#include <dm/uclass.h>
17#include <i2c.h>
Michal Simeka29511e2020-04-08 10:51:36 +020018#include <linux/sizes.h>
Michal Simekd61728c2020-08-03 13:01:45 +020019#include <malloc.h>
Michal Simek80fdef12020-03-31 12:39:37 +020020#include "board.h"
Michal Simekd61728c2020-08-03 13:01:45 +020021#include <dm.h>
22#include <i2c_eeprom.h>
23#include <net.h>
Michal Simek05af4832020-10-26 12:26:13 +010024#include <generated/dt.h>
T Karthik Reddy80c0d382021-08-10 06:50:20 -060025#include <soc.h>
Michal Simek52ff1622021-08-11 14:23:54 +020026#include <linux/ctype.h>
Sughosh Ganu741ef862022-04-15 11:29:34 +053027#include <linux/kernel.h>
Michal Simekee5a4b82022-07-21 16:19:17 +020028#include <uuid.h>
Michal Simek9755e3d2019-01-21 15:25:02 +010029
Michal Simekf149b392020-08-03 16:14:23 +020030#include "fru.h"
31
Sughosh Ganu741ef862022-04-15 11:29:34 +053032#if CONFIG_IS_ENABLED(EFI_HAVE_CAPSULE_SUPPORT)
33struct efi_fw_image fw_images[] = {
34#if defined(XILINX_BOOT_IMAGE_GUID)
35 {
36 .image_type_id = XILINX_BOOT_IMAGE_GUID,
37 .fw_name = u"XILINX-BOOT",
38 .image_index = 1,
39 },
40#endif
41#if defined(XILINX_UBOOT_IMAGE_GUID)
42 {
43 .image_type_id = XILINX_UBOOT_IMAGE_GUID,
44 .fw_name = u"XILINX-UBOOT",
45 .image_index = 2,
46 },
47#endif
48};
49
50struct efi_capsule_update_info update_info = {
51 .images = fw_images,
52};
53
54u8 num_image_type_guids = ARRAY_SIZE(fw_images);
55#endif /* EFI_HAVE_CAPSULE_SUPPORT */
56
Michal Simek9fea3b12020-08-03 12:59:28 +020057#if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
Michal Simek829e8c72019-01-21 16:29:07 +010058int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
59{
60 int ret = -EINVAL;
Michal Simek829e8c72019-01-21 16:29:07 +010061 struct udevice *dev;
62 ofnode eeprom;
63
64 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
65 if (!ofnode_valid(eeprom))
66 return -ENODEV;
67
68 debug("%s: Path to EEPROM %s\n", __func__,
Simon Glass14ca9f72020-01-27 08:49:43 -070069 ofnode_read_chosen_string("xlnx,eeprom"));
Michal Simek829e8c72019-01-21 16:29:07 +010070
71 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
72 if (ret)
73 return ret;
74
75 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
76 if (ret)
77 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
78 else
79 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
Michal Simek829e8c72019-01-21 16:29:07 +010080
81 return ret;
82}
Michal Simek9fea3b12020-08-03 12:59:28 +020083#endif
Ibai Erkiagafec657b2019-10-02 15:57:36 +010084
Michal Simekd61728c2020-08-03 13:01:45 +020085#define EEPROM_HEADER_MAGIC 0xdaaddeed
86#define EEPROM_HDR_MANUFACTURER_LEN 16
87#define EEPROM_HDR_NAME_LEN 16
88#define EEPROM_HDR_REV_LEN 8
89#define EEPROM_HDR_SERIAL_LEN 20
90#define EEPROM_HDR_NO_OF_MAC_ADDR 4
91#define EEPROM_HDR_ETH_ALEN ETH_ALEN
Michal Simekee5a4b82022-07-21 16:19:17 +020092#define EEPROM_HDR_UUID_LEN 16
Michal Simekd61728c2020-08-03 13:01:45 +020093
94struct xilinx_board_description {
95 u32 header;
96 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
97 char name[EEPROM_HDR_NAME_LEN + 1];
98 char revision[EEPROM_HDR_REV_LEN + 1];
99 char serial[EEPROM_HDR_SERIAL_LEN + 1];
100 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
Michal Simekee5a4b82022-07-21 16:19:17 +0200101 char uuid[EEPROM_HDR_UUID_LEN + 1];
Michal Simekd61728c2020-08-03 13:01:45 +0200102};
103
Michal Simeka03b5942020-08-03 12:57:05 +0200104static int highest_id = -1;
Michal Simekd9c93c92021-08-12 12:30:36 +0200105static struct xilinx_board_description *board_info;
Michal Simekd61728c2020-08-03 13:01:45 +0200106
Michal Simekf149b392020-08-03 16:14:23 +0200107#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simekd61728c2020-08-03 13:01:45 +0200108
109/* Variable which stores pointer to array which stores eeprom content */
110struct xilinx_legacy_format {
111 char board_sn[18]; /* 0x0 */
112 char unused0[14]; /* 0x12 */
113 char eth_mac[6]; /* 0x20 */
114 char unused1[170]; /* 0x26 */
115 char board_name[11]; /* 0xd0 */
116 char unused2[5]; /* 0xdc */
117 char board_revision[3]; /* 0xe0 */
118 char unused3[29]; /* 0xe3 */
119};
120
121static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
122{
123 int i;
Venkatesh Yadav Abbarapue1a193b2022-09-26 12:22:42 +0530124 unsigned char byte;
Michal Simekd61728c2020-08-03 13:01:45 +0200125
126 for (i = 0; i < size; i++) {
127 byte = eeprom[i];
128
129 /* Remove all ffs and spaces */
130 if (byte == 0xff || byte == ' ')
131 eeprom[i] = 0;
132
133 /* Convert strings to lower case */
134 if (byte >= 'A' && byte <= 'Z')
135 eeprom[i] = byte + 'a' - 'A';
136 }
137}
138
139static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
140 struct xilinx_board_description *desc)
141{
142 int ret, size;
143 struct xilinx_legacy_format *eeprom_content;
144 bool eth_valid = false;
145
146 size = sizeof(*eeprom_content);
147
148 eeprom_content = calloc(1, size);
149 if (!eeprom_content)
150 return -ENOMEM;
151
152 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
153 eeprom_content);
154
155 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
156 if (ret) {
157 debug("%s: I2C EEPROM read failed\n", __func__);
158 free(eeprom_content);
159 return ret;
160 }
161
162 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
163
164 printf("Xilinx I2C Legacy format at %s:\n", name);
165 printf(" Board name:\t%s\n", eeprom_content->board_name);
166 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
167 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
168
169 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
170 if (eth_valid)
171 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
172
173 /* Terminating \0 chars ensure end of string */
174 strcpy(desc->name, eeprom_content->board_name);
175 strcpy(desc->revision, eeprom_content->board_revision);
176 strcpy(desc->serial, eeprom_content->board_sn);
177 if (eth_valid)
178 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
179
180 desc->header = EEPROM_HEADER_MAGIC;
181
182 free(eeprom_content);
183
184 return ret;
185}
186
187static bool xilinx_detect_legacy(u8 *buffer)
188{
189 int i;
190 char c;
191
192 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
193 c = buffer[i];
194
195 if (c < '0' || c > '9')
196 return false;
197 }
198
199 return true;
200}
201
Michal Simekf149b392020-08-03 16:14:23 +0200202static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
203 struct xilinx_board_description *desc)
204{
Michal Simek530560b2021-08-12 11:03:49 +0200205 int i, ret, eeprom_size;
Michal Simekf149b392020-08-03 16:14:23 +0200206 u8 *fru_content;
Ashok Reddy Soma7a036b62022-02-23 15:00:59 +0100207 u8 id = 0;
Michal Simekf149b392020-08-03 16:14:23 +0200208
209 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
210 eeprom_size = i2c_eeprom_size(dev);
211
212 fru_content = calloc(1, eeprom_size);
213 if (!fru_content)
214 return -ENOMEM;
215
216 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
217 fru_content);
218
219 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
220 eeprom_size);
221 if (ret) {
222 debug("%s: I2C EEPROM read failed\n", __func__);
Michal Simekb2628632021-08-13 09:17:10 +0200223 goto end;
Michal Simekf149b392020-08-03 16:14:23 +0200224 }
225
Michal Simekf149b392020-08-03 16:14:23 +0200226 fru_capture((unsigned long)fru_content);
Michal Simek52ff1622021-08-11 14:23:54 +0200227 if (gd->flags & GD_FLG_RELOC || (_DEBUG && CONFIG_IS_ENABLED(DTB_RESELECT))) {
228 printf("Xilinx I2C FRU format at %s:\n", name);
229 ret = fru_display(0);
230 if (ret) {
231 printf("FRU format decoding failed.\n");
232 goto end;
233 }
Michal Simekf149b392020-08-03 16:14:23 +0200234 }
235
236 if (desc->header == EEPROM_HEADER_MAGIC) {
237 debug("Information already filled\n");
Michal Simekb2628632021-08-13 09:17:10 +0200238 ret = -EINVAL;
239 goto end;
Michal Simekf149b392020-08-03 16:14:23 +0200240 }
241
242 /* It is clear that FRU was captured and structures were filled */
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200243 strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
Michal Simekf149b392020-08-03 16:14:23 +0200244 sizeof(desc->manufacturer));
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200245 strlcpy(desc->uuid, (char *)fru_data.brd.uuid,
Michal Simekee5a4b82022-07-21 16:19:17 +0200246 sizeof(desc->uuid));
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200247 strlcpy(desc->name, (char *)fru_data.brd.product_name,
Michal Simekf149b392020-08-03 16:14:23 +0200248 sizeof(desc->name));
Michal Simek530560b2021-08-12 11:03:49 +0200249 for (i = 0; i < sizeof(desc->name); i++) {
250 if (desc->name[i] == ' ')
251 desc->name[i] = '\0';
252 }
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200253 strlcpy(desc->revision, (char *)fru_data.brd.rev,
Michal Simekf149b392020-08-03 16:14:23 +0200254 sizeof(desc->revision));
Michal Simek93216272022-06-06 09:31:27 +0200255 for (i = 0; i < sizeof(desc->revision); i++) {
256 if (desc->revision[i] == ' ')
257 desc->revision[i] = '\0';
258 }
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200259 strlcpy(desc->serial, (char *)fru_data.brd.serial_number,
Michal Simekf149b392020-08-03 16:14:23 +0200260 sizeof(desc->serial));
Ashok Reddy Soma7a036b62022-02-23 15:00:59 +0100261
262 while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
263 if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
264 memcpy(&desc->mac_addr[id],
265 (char *)fru_data.mac.macid[id], ETH_ALEN);
266 id++;
267 }
268
Michal Simekf149b392020-08-03 16:14:23 +0200269 desc->header = EEPROM_HEADER_MAGIC;
270
Michal Simekb2628632021-08-13 09:17:10 +0200271end:
272 free(fru_content);
273 return ret;
Michal Simekf149b392020-08-03 16:14:23 +0200274}
275
276static bool xilinx_detect_fru(u8 *buffer)
277{
278 u8 checksum = 0;
279 int i;
280
281 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
282 if (checksum) {
283 debug("%s Common header CRC FAIL\n", __func__);
284 return false;
285 }
286
287 bool all_zeros = true;
288 /* Checksum over all zeros is also zero that's why detect this case */
289 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
290 if (buffer[i] != 0)
291 all_zeros = false;
292 }
293
294 if (all_zeros)
295 return false;
296
297 debug("%s Common header CRC PASS\n", __func__);
298 return true;
299}
300
Michal Simekd61728c2020-08-03 13:01:45 +0200301static int xilinx_read_eeprom_single(char *name,
302 struct xilinx_board_description *desc)
303{
304 int ret;
305 struct udevice *dev;
306 ofnode eeprom;
307 u8 buffer[XILINX_I2C_DETECTION_BITS];
308
309 eeprom = ofnode_get_aliases_node(name);
310 if (!ofnode_valid(eeprom))
311 return -ENODEV;
312
313 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
314 if (ret)
315 return ret;
316
317 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
318 if (ret) {
319 debug("%s: I2C EEPROM read failed\n", __func__);
320 return ret;
321 }
322
323 debug("%s: i2c memory detected: %s\n", __func__, name);
324
Michal Simekf149b392020-08-03 16:14:23 +0200325 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
326 return xilinx_read_eeprom_fru(dev, name, desc);
327
Michal Simekd61728c2020-08-03 13:01:45 +0200328 if (xilinx_detect_legacy(buffer))
329 return xilinx_read_eeprom_legacy(dev, name, desc);
330
331 return -ENODEV;
332}
333
334__maybe_unused int xilinx_read_eeprom(void)
335{
Michal Simekd9c93c92021-08-12 12:30:36 +0200336 int id;
Michal Simekd61728c2020-08-03 13:01:45 +0200337 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simeka03b5942020-08-03 12:57:05 +0200338 struct xilinx_board_description *desc;
Michal Simekd61728c2020-08-03 13:01:45 +0200339
340 highest_id = dev_read_alias_highest_id("nvmem");
341 /* No nvmem aliases present */
342 if (highest_id < 0)
343 return -EINVAL;
344
Michal Simekd9c93c92021-08-12 12:30:36 +0200345 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
Michal Simekd61728c2020-08-03 13:01:45 +0200346 if (!board_info)
347 return -ENOMEM;
348
349 debug("%s: Highest ID %d, board_info %p\n", __func__,
350 highest_id, board_info);
351
352 for (id = 0; id <= highest_id; id++) {
353 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
354
Michal Simeka03b5942020-08-03 12:57:05 +0200355 /* Alloc structure */
Michal Simekd9c93c92021-08-12 12:30:36 +0200356 desc = &board_info[id];
Michal Simeka03b5942020-08-03 12:57:05 +0200357
Michal Simekd61728c2020-08-03 13:01:45 +0200358 /* Ignoring return value for supporting multiple chips */
Michal Simekd9c93c92021-08-12 12:30:36 +0200359 xilinx_read_eeprom_single(name_buf, desc);
Michal Simekd61728c2020-08-03 13:01:45 +0200360 }
361
Michal Simeka03b5942020-08-03 12:57:05 +0200362 /*
363 * Consider to clean board_info structure when board/cards are not
364 * detected.
365 */
Michal Simekd61728c2020-08-03 13:01:45 +0200366
367 return 0;
368}
369
Michal Simekf0631002022-02-17 14:28:38 +0100370#if defined(CONFIG_OF_BOARD)
Ilias Apalodimase7fb7892021-10-26 09:12:33 +0300371void *board_fdt_blob_setup(int *err)
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100372{
Michal Simeke2572b52020-09-04 16:21:47 +0200373 void *fdt_blob;
Michal Simek453bb772020-03-19 10:23:56 +0100374
Ilias Apalodimase7fb7892021-10-26 09:12:33 +0300375 *err = 0;
Michal Simeka672b982021-01-04 11:07:28 +0100376 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
377 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simekfc3c6fd2021-02-02 13:33:22 +0100378 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek506009f2021-01-04 11:03:36 +0100379 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100380
Michal Simek506009f2021-01-04 11:03:36 +0100381 if (fdt_magic(fdt_blob) == FDT_MAGIC)
382 return fdt_blob;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100383
Michal Simek506009f2021-01-04 11:03:36 +0100384 debug("DTB is not passed via %p\n", fdt_blob);
385 }
Michal Simekfc274a52019-12-19 17:45:15 +0100386
Michal Simek506009f2021-01-04 11:03:36 +0100387 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
388 /*
389 * FDT is at end of BSS unless it is in a different memory
390 * region
391 */
392 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
393 fdt_blob = (ulong *)&_image_binary_end;
394 else
395 fdt_blob = (ulong *)&__bss_end;
396 } else {
397 /* FDT is at end of image */
398 fdt_blob = (ulong *)&_end;
399 }
Michal Simekfc274a52019-12-19 17:45:15 +0100400
401 if (fdt_magic(fdt_blob) == FDT_MAGIC)
402 return fdt_blob;
403
404 debug("DTB is also not passed via %p\n", fdt_blob);
405
Michal Simekf0631002022-02-17 14:28:38 +0100406 *err = -EINVAL;
Michal Simekfc274a52019-12-19 17:45:15 +0100407 return NULL;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100408}
409#endif
Michal Simek80fdef12020-03-31 12:39:37 +0200410
Michal Simek7c553ac2020-10-22 11:14:20 +0200411#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simeka03b5942020-08-03 12:57:05 +0200412static int env_set_by_index(const char *name, int index, char *data)
413{
414 char var[32];
415
416 if (!index)
417 sprintf(var, "board_%s", name);
418 else
419 sprintf(var, "card%d_%s", index, name);
420
421 return env_set(var, data);
422}
423
Michal Simek80fdef12020-03-31 12:39:37 +0200424int board_late_init_xilinx(void)
425{
Michal Simekca0f6162020-08-12 12:16:49 +0200426 u32 ret = 0;
Michal Simeka03b5942020-08-03 12:57:05 +0200427 int i, id, macid = 0;
428 struct xilinx_board_description *desc;
Ricardo Salvetie6e3b9d2022-01-20 16:17:30 -0300429 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600430
T Karthik Reddy25484d92021-04-02 01:49:17 -0600431 if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600432 ulong scriptaddr;
433
434 scriptaddr = env_get_hex("scriptaddr", 0);
T Karthik Reddy25484d92021-04-02 01:49:17 -0600435 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600436 }
Michal Simek2570cc62020-08-12 12:17:53 +0200437
Michal Simek12305822020-10-23 07:54:18 +0200438 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simek2570cc62020-08-12 12:17:53 +0200439 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simek80fdef12020-03-31 12:39:37 +0200440
Michal Simekca0f6162020-08-12 12:16:49 +0200441 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
442
443 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simek2570cc62020-08-12 12:17:53 +0200444 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simekca0f6162020-08-12 12:16:49 +0200445
Michal Simeka03b5942020-08-03 12:57:05 +0200446 for (id = 0; id <= highest_id; id++) {
Michal Simekd9c93c92021-08-12 12:30:36 +0200447 desc = &board_info[id];
Michal Simeka03b5942020-08-03 12:57:05 +0200448 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
449 if (desc->manufacturer[0])
450 ret |= env_set_by_index("manufacturer", id,
451 desc->manufacturer);
452 if (desc->name[0])
453 ret |= env_set_by_index("name", id,
454 desc->name);
455 if (desc->revision[0])
456 ret |= env_set_by_index("rev", id,
457 desc->revision);
458 if (desc->serial[0])
459 ret |= env_set_by_index("serial", id,
460 desc->serial);
Michal Simekd61728c2020-08-03 13:01:45 +0200461
Michal Simekee5a4b82022-07-21 16:19:17 +0200462 if (desc->uuid[0]) {
Venkatesh Yadav Abbarapue1a193b2022-09-26 12:22:42 +0530463 unsigned char uuid[UUID_STR_LEN + 1];
464 unsigned char *t = desc->uuid;
Michal Simekee5a4b82022-07-21 16:19:17 +0200465
466 memset(uuid, 0, UUID_STR_LEN + 1);
467
468 sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
469 t[0], t[1], t[2], t[3], t[4], t[5],
470 t[6], t[7], t[8], t[9], t[10], t[11],
471 t[12], t[13], t[14], t[15]);
472 ret |= env_set_by_index("uuid", id, uuid);
473 }
474
Michal Simekd61728c2020-08-03 13:01:45 +0200475 if (!CONFIG_IS_ENABLED(NET))
476 continue;
477
Michal Simeka03b5942020-08-03 12:57:05 +0200478 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
Michal Simeka03b5942020-08-03 12:57:05 +0200479 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
480 ret |= eth_env_set_enetaddr_by_index("eth",
481 macid++, desc->mac_addr[i]);
482 }
Michal Simekd61728c2020-08-03 13:01:45 +0200483 }
484 }
485
Michal Simekca0f6162020-08-12 12:16:49 +0200486 if (ret)
487 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simekc8da6512020-07-09 15:57:56 +0200488
Michal Simek80fdef12020-03-31 12:39:37 +0200489 return 0;
490}
Michal Simek7c553ac2020-10-22 11:14:20 +0200491#endif
Michal Simek05af4832020-10-26 12:26:13 +0100492
Michal Simek57f71032021-07-23 09:55:59 +0200493static char *board_name = DEVICE_TREE;
494
Michal Simek05af4832020-10-26 12:26:13 +0100495int __maybe_unused board_fit_config_name_match(const char *name)
496{
Michal Simek57f71032021-07-23 09:55:59 +0200497 debug("%s: Check %s, default %s\n", __func__, name, board_name);
Michal Simek05af4832020-10-26 12:26:13 +0100498
Michal Simek57f71032021-07-23 09:55:59 +0200499 if (!strcmp(name, board_name))
Michal Simek05af4832020-10-26 12:26:13 +0100500 return 0;
501
502 return -1;
503}
T Karthik Reddy80c0d382021-08-10 06:50:20 -0600504
Michal Simek88232532021-07-23 09:59:59 +0200505#if CONFIG_IS_ENABLED(DTB_RESELECT)
Michal Simek52ff1622021-08-11 14:23:54 +0200506#define MAX_NAME_LENGTH 50
507
Michal Simek88232532021-07-23 09:59:59 +0200508char * __maybe_unused __weak board_name_decode(void)
509{
Michal Simek52ff1622021-08-11 14:23:54 +0200510 char *board_local_name;
511 struct xilinx_board_description *desc;
512 int i, id;
513
514 board_local_name = calloc(1, MAX_NAME_LENGTH);
515 if (!board_info)
516 return NULL;
517
518 for (id = 0; id <= highest_id; id++) {
519 desc = &board_info[id];
520
521 /* No board description */
522 if (!desc)
523 goto error;
524
525 /* Board is not detected */
526 if (desc->header != EEPROM_HEADER_MAGIC)
527 continue;
528
529 /* The first string should be soc name */
530 if (!id)
531 strcat(board_local_name, CONFIG_SYS_BOARD);
532
533 /*
534 * For two purpose here:
535 * soc_name- eg: zynqmp-
536 * and between base board and CC eg: ..revA-sck...
537 */
538 strcat(board_local_name, "-");
539
540 if (desc->name[0]) {
541 /* For DT composition name needs to be lowercase */
542 for (i = 0; i < sizeof(desc->name); i++)
543 desc->name[i] = tolower(desc->name[i]);
544
545 strcat(board_local_name, desc->name);
546 }
547 if (desc->revision[0]) {
548 strcat(board_local_name, "-rev");
549
550 /* And revision needs to be uppercase */
551 for (i = 0; i < sizeof(desc->revision); i++)
552 desc->revision[i] = toupper(desc->revision[i]);
553
554 strcat(board_local_name, desc->revision);
555 }
556 }
557
558 /*
559 * Longer strings will end up with buffer overflow and potential
560 * attacks that's why check it
561 */
562 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
563 panic("Board name can't be determined\n");
564
565 if (strlen(board_local_name))
566 return board_local_name;
567
568error:
569 free(board_local_name);
Michal Simek88232532021-07-23 09:59:59 +0200570 return NULL;
571}
572
573bool __maybe_unused __weak board_detection(void)
574{
Michal Simek52ff1622021-08-11 14:23:54 +0200575 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
576 int ret;
577
578 ret = xilinx_read_eeprom();
579 return !ret ? true : false;
580 }
581
Michal Simek88232532021-07-23 09:59:59 +0200582 return false;
583}
584
Michal Simek39d3c3c2022-09-06 12:40:41 +0200585bool __maybe_unused __weak soc_detection(void)
586{
587 return false;
588}
589
590char * __maybe_unused __weak soc_name_decode(void)
591{
592 return NULL;
593}
594
Michal Simek88232532021-07-23 09:59:59 +0200595int embedded_dtb_select(void)
596{
Michal Simek39d3c3c2022-09-06 12:40:41 +0200597 if (soc_detection()) {
598 char *soc_local_name;
599
600 soc_local_name = soc_name_decode();
601 if (soc_local_name) {
602 board_name = soc_local_name;
603 printf("Detected SOC name: %s\n", board_name);
604
605 /* Time to change DTB on fly */
606 /* Both ways should work here */
607 /* fdtdec_resetup(&rescan); */
608 return fdtdec_setup();
609 }
610 }
611
Michal Simek88232532021-07-23 09:59:59 +0200612 if (board_detection()) {
613 char *board_local_name;
614
615 board_local_name = board_name_decode();
616 if (board_local_name) {
617 board_name = board_local_name;
Michal Simek52ff1622021-08-11 14:23:54 +0200618 printf("Detected name: %s\n", board_name);
Michal Simek88232532021-07-23 09:59:59 +0200619
620 /* Time to change DTB on fly */
621 /* Both ways should work here */
622 /* fdtdec_resetup(&rescan); */
623 fdtdec_setup();
624 }
625 }
626 return 0;
627}
628#endif
Michal Simeka32c3e92022-08-25 14:23:10 +0200629
630#if defined(CONFIG_LMB)
Pali Rohár049704f2022-09-09 17:32:40 +0200631phys_size_t board_get_usable_ram_top(phys_size_t total_size)
Michal Simeka32c3e92022-08-25 14:23:10 +0200632{
633 phys_size_t size;
634 phys_addr_t reg;
635 struct lmb lmb;
636
637 if (!total_size)
638 return gd->ram_top;
639
640 if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
641 panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
642
643 /* found enough not-reserved memory to relocated U-Boot */
644 lmb_init(&lmb);
645 lmb_add(&lmb, gd->ram_base, gd->ram_size);
646 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
647 size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
648 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
649
650 if (!reg)
651 reg = gd->ram_top - size;
652
653 return reg + size;
654}
655#endif