blob: aa9f3ef071c2a83bb8ecaefc86d3f4fd4ce11e22 [file] [log] [blame]
Michal Simek9755e3d2019-01-21 15:25:02 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Michal Simek8174cd22023-01-06 09:38:52 +01003 * (C) Copyright 2014 - 2022, Xilinx, Inc.
4 * (C) Copyright 2022 - 2023, Advanced Micro Devices, Inc.
5 *
6 * Michal Simek <michal.simek@amd.com>
Michal Simek9755e3d2019-01-21 15:25:02 +01007 */
8
9#include <common.h>
Sughosh Ganu741ef862022-04-15 11:29:34 +053010#include <efi.h>
11#include <efi_loader.h>
Simon Glass09140112020-05-10 11:40:03 -060012#include <env.h>
Michal Simeka32c3e92022-08-25 14:23:10 +020013#include <image.h>
Algapally Santosh Sagarfb737f12023-01-19 22:36:14 -070014#include <init.h>
Michal Simeka32c3e92022-08-25 14:23:10 +020015#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Michal Simekfc274a52019-12-19 17:45:15 +010018#include <asm/sections.h>
Michal Simek9755e3d2019-01-21 15:25:02 +010019#include <dm/uclass.h>
20#include <i2c.h>
Michal Simeka29511e2020-04-08 10:51:36 +020021#include <linux/sizes.h>
Michal Simekd61728c2020-08-03 13:01:45 +020022#include <malloc.h>
Michal Simek80fdef12020-03-31 12:39:37 +020023#include "board.h"
Michal Simekd61728c2020-08-03 13:01:45 +020024#include <dm.h>
25#include <i2c_eeprom.h>
26#include <net.h>
Michal Simek05af4832020-10-26 12:26:13 +010027#include <generated/dt.h>
Michal Simek8174cd22023-01-06 09:38:52 +010028#include <slre.h>
T Karthik Reddy80c0d382021-08-10 06:50:20 -060029#include <soc.h>
Michal Simek52ff1622021-08-11 14:23:54 +020030#include <linux/ctype.h>
Sughosh Ganu741ef862022-04-15 11:29:34 +053031#include <linux/kernel.h>
Michal Simekee5a4b82022-07-21 16:19:17 +020032#include <uuid.h>
Michal Simek9755e3d2019-01-21 15:25:02 +010033
Michal Simekf149b392020-08-03 16:14:23 +020034#include "fru.h"
35
Simon Glass71aa8062023-02-05 15:39:42 -070036#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
Sughosh Ganu741ef862022-04-15 11:29:34 +053037struct efi_fw_image fw_images[] = {
38#if defined(XILINX_BOOT_IMAGE_GUID)
39 {
40 .image_type_id = XILINX_BOOT_IMAGE_GUID,
41 .fw_name = u"XILINX-BOOT",
42 .image_index = 1,
43 },
44#endif
45#if defined(XILINX_UBOOT_IMAGE_GUID)
46 {
47 .image_type_id = XILINX_UBOOT_IMAGE_GUID,
48 .fw_name = u"XILINX-UBOOT",
49 .image_index = 2,
50 },
51#endif
52};
53
54struct efi_capsule_update_info update_info = {
55 .images = fw_images,
56};
57
58u8 num_image_type_guids = ARRAY_SIZE(fw_images);
59#endif /* EFI_HAVE_CAPSULE_SUPPORT */
60
Michal Simekd61728c2020-08-03 13:01:45 +020061#define EEPROM_HEADER_MAGIC 0xdaaddeed
62#define EEPROM_HDR_MANUFACTURER_LEN 16
63#define EEPROM_HDR_NAME_LEN 16
64#define EEPROM_HDR_REV_LEN 8
65#define EEPROM_HDR_SERIAL_LEN 20
66#define EEPROM_HDR_NO_OF_MAC_ADDR 4
67#define EEPROM_HDR_ETH_ALEN ETH_ALEN
Michal Simekee5a4b82022-07-21 16:19:17 +020068#define EEPROM_HDR_UUID_LEN 16
Michal Simekd61728c2020-08-03 13:01:45 +020069
70struct xilinx_board_description {
71 u32 header;
72 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
73 char name[EEPROM_HDR_NAME_LEN + 1];
74 char revision[EEPROM_HDR_REV_LEN + 1];
75 char serial[EEPROM_HDR_SERIAL_LEN + 1];
76 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
Michal Simekee5a4b82022-07-21 16:19:17 +020077 char uuid[EEPROM_HDR_UUID_LEN + 1];
Michal Simekd61728c2020-08-03 13:01:45 +020078};
79
Michal Simeka03b5942020-08-03 12:57:05 +020080static int highest_id = -1;
Michal Simekd9c93c92021-08-12 12:30:36 +020081static struct xilinx_board_description *board_info;
Michal Simekd61728c2020-08-03 13:01:45 +020082
Michal Simekf149b392020-08-03 16:14:23 +020083#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simekd61728c2020-08-03 13:01:45 +020084
85/* Variable which stores pointer to array which stores eeprom content */
86struct xilinx_legacy_format {
87 char board_sn[18]; /* 0x0 */
88 char unused0[14]; /* 0x12 */
Michal Simekb86b1352023-01-24 16:19:26 +010089 char eth_mac[ETH_ALEN]; /* 0x20 */
Michal Simekd61728c2020-08-03 13:01:45 +020090 char unused1[170]; /* 0x26 */
91 char board_name[11]; /* 0xd0 */
92 char unused2[5]; /* 0xdc */
93 char board_revision[3]; /* 0xe0 */
94 char unused3[29]; /* 0xe3 */
95};
96
97static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
98{
99 int i;
Venkatesh Yadav Abbarapue1a193b2022-09-26 12:22:42 +0530100 unsigned char byte;
Michal Simekd61728c2020-08-03 13:01:45 +0200101
102 for (i = 0; i < size; i++) {
103 byte = eeprom[i];
104
Michal Simeke6c62532023-01-24 16:19:27 +0100105 /* Remove all non printable chars but ignore MAC address */
106 if ((i < offsetof(struct xilinx_legacy_format, eth_mac) ||
107 i >= offsetof(struct xilinx_legacy_format, unused1)) &&
108 (byte < '!' || byte > '~')) {
Michal Simekd61728c2020-08-03 13:01:45 +0200109 eeprom[i] = 0;
Michal Simeke6c62532023-01-24 16:19:27 +0100110 continue;
111 }
Michal Simekd61728c2020-08-03 13:01:45 +0200112
113 /* Convert strings to lower case */
114 if (byte >= 'A' && byte <= 'Z')
115 eeprom[i] = byte + 'a' - 'A';
116 }
117}
118
119static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
120 struct xilinx_board_description *desc)
121{
122 int ret, size;
123 struct xilinx_legacy_format *eeprom_content;
124 bool eth_valid = false;
125
126 size = sizeof(*eeprom_content);
127
128 eeprom_content = calloc(1, size);
129 if (!eeprom_content)
130 return -ENOMEM;
131
132 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
133 eeprom_content);
134
135 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
136 if (ret) {
137 debug("%s: I2C EEPROM read failed\n", __func__);
138 free(eeprom_content);
139 return ret;
140 }
141
142 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
143
Michal Simek55631672023-01-24 16:19:28 +0100144 /* Terminating \0 chars are the part of desc fields already */
145 strlcpy(desc->name, eeprom_content->board_name,
146 sizeof(eeprom_content->board_name) + 1);
147 strlcpy(desc->revision, eeprom_content->board_revision,
148 sizeof(eeprom_content->board_revision) + 1);
149 strlcpy(desc->serial, eeprom_content->board_sn,
150 sizeof(eeprom_content->board_sn) + 1);
Michal Simekd61728c2020-08-03 13:01:45 +0200151
152 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
153 if (eth_valid)
Michal Simekd61728c2020-08-03 13:01:45 +0200154 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
155
Michal Simek55631672023-01-24 16:19:28 +0100156 printf("Xilinx I2C Legacy format at %s:\n", name);
157 printf(" Board name:\t%s\n", desc->name);
158 printf(" Board rev:\t%s\n", desc->revision);
159 printf(" Board SN:\t%s\n", desc->serial);
160
161 if (eth_valid)
162 printf(" Ethernet mac:\t%pM\n", desc->mac_addr);
163
Michal Simekd61728c2020-08-03 13:01:45 +0200164 desc->header = EEPROM_HEADER_MAGIC;
165
166 free(eeprom_content);
167
168 return ret;
169}
170
171static bool xilinx_detect_legacy(u8 *buffer)
172{
173 int i;
174 char c;
175
176 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
177 c = buffer[i];
178
179 if (c < '0' || c > '9')
180 return false;
181 }
182
183 return true;
184}
185
Michal Simekf149b392020-08-03 16:14:23 +0200186static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
187 struct xilinx_board_description *desc)
188{
Michal Simek530560b2021-08-12 11:03:49 +0200189 int i, ret, eeprom_size;
Michal Simekf149b392020-08-03 16:14:23 +0200190 u8 *fru_content;
Ashok Reddy Soma7a036b62022-02-23 15:00:59 +0100191 u8 id = 0;
Michal Simekf149b392020-08-03 16:14:23 +0200192
193 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
194 eeprom_size = i2c_eeprom_size(dev);
195
196 fru_content = calloc(1, eeprom_size);
197 if (!fru_content)
198 return -ENOMEM;
199
200 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
201 fru_content);
202
203 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
204 eeprom_size);
205 if (ret) {
206 debug("%s: I2C EEPROM read failed\n", __func__);
Michal Simekb2628632021-08-13 09:17:10 +0200207 goto end;
Michal Simekf149b392020-08-03 16:14:23 +0200208 }
209
Michal Simekf149b392020-08-03 16:14:23 +0200210 fru_capture((unsigned long)fru_content);
Simon Glass866ec872023-02-05 15:39:38 -0700211 if (gd->flags & GD_FLG_RELOC || (_DEBUG && IS_ENABLED(CONFIG_DTB_RESELECT))) {
Michal Simek52ff1622021-08-11 14:23:54 +0200212 printf("Xilinx I2C FRU format at %s:\n", name);
213 ret = fru_display(0);
214 if (ret) {
215 printf("FRU format decoding failed.\n");
216 goto end;
217 }
Michal Simekf149b392020-08-03 16:14:23 +0200218 }
219
220 if (desc->header == EEPROM_HEADER_MAGIC) {
221 debug("Information already filled\n");
Michal Simekb2628632021-08-13 09:17:10 +0200222 ret = -EINVAL;
223 goto end;
Michal Simekf149b392020-08-03 16:14:23 +0200224 }
225
226 /* It is clear that FRU was captured and structures were filled */
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200227 strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
Michal Simekf149b392020-08-03 16:14:23 +0200228 sizeof(desc->manufacturer));
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200229 strlcpy(desc->uuid, (char *)fru_data.brd.uuid,
Michal Simekee5a4b82022-07-21 16:19:17 +0200230 sizeof(desc->uuid));
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200231 strlcpy(desc->name, (char *)fru_data.brd.product_name,
Michal Simekf149b392020-08-03 16:14:23 +0200232 sizeof(desc->name));
Michal Simek530560b2021-08-12 11:03:49 +0200233 for (i = 0; i < sizeof(desc->name); i++) {
234 if (desc->name[i] == ' ')
235 desc->name[i] = '\0';
236 }
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200237 strlcpy(desc->revision, (char *)fru_data.brd.rev,
Michal Simekf149b392020-08-03 16:14:23 +0200238 sizeof(desc->revision));
Michal Simek93216272022-06-06 09:31:27 +0200239 for (i = 0; i < sizeof(desc->revision); i++) {
240 if (desc->revision[i] == ' ')
241 desc->revision[i] = '\0';
242 }
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200243 strlcpy(desc->serial, (char *)fru_data.brd.serial_number,
Michal Simekf149b392020-08-03 16:14:23 +0200244 sizeof(desc->serial));
Ashok Reddy Soma7a036b62022-02-23 15:00:59 +0100245
246 while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
247 if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
248 memcpy(&desc->mac_addr[id],
249 (char *)fru_data.mac.macid[id], ETH_ALEN);
250 id++;
251 }
252
Michal Simekf149b392020-08-03 16:14:23 +0200253 desc->header = EEPROM_HEADER_MAGIC;
254
Michal Simekb2628632021-08-13 09:17:10 +0200255end:
256 free(fru_content);
257 return ret;
Michal Simekf149b392020-08-03 16:14:23 +0200258}
259
260static bool xilinx_detect_fru(u8 *buffer)
261{
262 u8 checksum = 0;
263 int i;
264
265 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
266 if (checksum) {
267 debug("%s Common header CRC FAIL\n", __func__);
268 return false;
269 }
270
271 bool all_zeros = true;
272 /* Checksum over all zeros is also zero that's why detect this case */
273 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
274 if (buffer[i] != 0)
275 all_zeros = false;
276 }
277
278 if (all_zeros)
279 return false;
280
281 debug("%s Common header CRC PASS\n", __func__);
282 return true;
283}
284
Michal Simekd61728c2020-08-03 13:01:45 +0200285static int xilinx_read_eeprom_single(char *name,
286 struct xilinx_board_description *desc)
287{
288 int ret;
289 struct udevice *dev;
290 ofnode eeprom;
291 u8 buffer[XILINX_I2C_DETECTION_BITS];
292
293 eeprom = ofnode_get_aliases_node(name);
294 if (!ofnode_valid(eeprom))
295 return -ENODEV;
296
297 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
298 if (ret)
299 return ret;
300
301 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
302 if (ret) {
303 debug("%s: I2C EEPROM read failed\n", __func__);
304 return ret;
305 }
306
307 debug("%s: i2c memory detected: %s\n", __func__, name);
308
Simon Glassf8daba42023-02-05 15:36:31 -0700309 if (IS_ENABLED(CONFIG_CMD_FRU) && xilinx_detect_fru(buffer))
Michal Simekf149b392020-08-03 16:14:23 +0200310 return xilinx_read_eeprom_fru(dev, name, desc);
311
Michal Simekd61728c2020-08-03 13:01:45 +0200312 if (xilinx_detect_legacy(buffer))
313 return xilinx_read_eeprom_legacy(dev, name, desc);
314
315 return -ENODEV;
316}
317
318__maybe_unused int xilinx_read_eeprom(void)
319{
Michal Simekd9c93c92021-08-12 12:30:36 +0200320 int id;
Michal Simekd61728c2020-08-03 13:01:45 +0200321 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simeka03b5942020-08-03 12:57:05 +0200322 struct xilinx_board_description *desc;
Michal Simekd61728c2020-08-03 13:01:45 +0200323
324 highest_id = dev_read_alias_highest_id("nvmem");
325 /* No nvmem aliases present */
326 if (highest_id < 0)
327 return -EINVAL;
328
Michal Simekd9c93c92021-08-12 12:30:36 +0200329 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
Michal Simekd61728c2020-08-03 13:01:45 +0200330 if (!board_info)
331 return -ENOMEM;
332
333 debug("%s: Highest ID %d, board_info %p\n", __func__,
334 highest_id, board_info);
335
336 for (id = 0; id <= highest_id; id++) {
337 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
338
Michal Simeka03b5942020-08-03 12:57:05 +0200339 /* Alloc structure */
Michal Simekd9c93c92021-08-12 12:30:36 +0200340 desc = &board_info[id];
Michal Simeka03b5942020-08-03 12:57:05 +0200341
Michal Simekd61728c2020-08-03 13:01:45 +0200342 /* Ignoring return value for supporting multiple chips */
Michal Simekd9c93c92021-08-12 12:30:36 +0200343 xilinx_read_eeprom_single(name_buf, desc);
Michal Simekd61728c2020-08-03 13:01:45 +0200344 }
345
Michal Simeka03b5942020-08-03 12:57:05 +0200346 /*
347 * Consider to clean board_info structure when board/cards are not
348 * detected.
349 */
Michal Simekd61728c2020-08-03 13:01:45 +0200350
351 return 0;
352}
353
Michal Simekf0631002022-02-17 14:28:38 +0100354#if defined(CONFIG_OF_BOARD)
Ilias Apalodimase7fb7892021-10-26 09:12:33 +0300355void *board_fdt_blob_setup(int *err)
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100356{
Michal Simeke2572b52020-09-04 16:21:47 +0200357 void *fdt_blob;
Michal Simek453bb772020-03-19 10:23:56 +0100358
Ilias Apalodimase7fb7892021-10-26 09:12:33 +0300359 *err = 0;
Michal Simeka672b982021-01-04 11:07:28 +0100360 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
361 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simekfc3c6fd2021-02-02 13:33:22 +0100362 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek506009f2021-01-04 11:03:36 +0100363 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100364
Michal Simek506009f2021-01-04 11:03:36 +0100365 if (fdt_magic(fdt_blob) == FDT_MAGIC)
366 return fdt_blob;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100367
Michal Simek506009f2021-01-04 11:03:36 +0100368 debug("DTB is not passed via %p\n", fdt_blob);
369 }
Michal Simekfc274a52019-12-19 17:45:15 +0100370
Michal Simek506009f2021-01-04 11:03:36 +0100371 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
372 /*
373 * FDT is at end of BSS unless it is in a different memory
374 * region
375 */
376 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
377 fdt_blob = (ulong *)&_image_binary_end;
378 else
379 fdt_blob = (ulong *)&__bss_end;
380 } else {
381 /* FDT is at end of image */
382 fdt_blob = (ulong *)&_end;
383 }
Michal Simekfc274a52019-12-19 17:45:15 +0100384
385 if (fdt_magic(fdt_blob) == FDT_MAGIC)
386 return fdt_blob;
387
388 debug("DTB is also not passed via %p\n", fdt_blob);
389
Michal Simekf0631002022-02-17 14:28:38 +0100390 *err = -EINVAL;
Michal Simekfc274a52019-12-19 17:45:15 +0100391 return NULL;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100392}
393#endif
Michal Simek80fdef12020-03-31 12:39:37 +0200394
Michal Simek7c553ac2020-10-22 11:14:20 +0200395#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simeka03b5942020-08-03 12:57:05 +0200396static int env_set_by_index(const char *name, int index, char *data)
397{
398 char var[32];
399
400 if (!index)
401 sprintf(var, "board_%s", name);
402 else
403 sprintf(var, "card%d_%s", index, name);
404
405 return env_set(var, data);
406}
407
Michal Simek80fdef12020-03-31 12:39:37 +0200408int board_late_init_xilinx(void)
409{
Michal Simekca0f6162020-08-12 12:16:49 +0200410 u32 ret = 0;
Michal Simeka03b5942020-08-03 12:57:05 +0200411 int i, id, macid = 0;
412 struct xilinx_board_description *desc;
Ricardo Salvetie6e3b9d2022-01-20 16:17:30 -0300413 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600414
T Karthik Reddy25484d92021-04-02 01:49:17 -0600415 if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600416 ulong scriptaddr;
417
418 scriptaddr = env_get_hex("scriptaddr", 0);
T Karthik Reddy25484d92021-04-02 01:49:17 -0600419 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600420 }
Michal Simek2570cc62020-08-12 12:17:53 +0200421
Simon Glassc0ef5a32023-02-05 15:36:13 -0700422 if (IS_ENABLED(CONFIG_ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simek2570cc62020-08-12 12:17:53 +0200423 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simek80fdef12020-03-31 12:39:37 +0200424
Michal Simekca0f6162020-08-12 12:16:49 +0200425 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
426
427 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simek2570cc62020-08-12 12:17:53 +0200428 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simekca0f6162020-08-12 12:16:49 +0200429
Michal Simeka03b5942020-08-03 12:57:05 +0200430 for (id = 0; id <= highest_id; id++) {
Michal Simekd9c93c92021-08-12 12:30:36 +0200431 desc = &board_info[id];
Michal Simeka03b5942020-08-03 12:57:05 +0200432 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
433 if (desc->manufacturer[0])
434 ret |= env_set_by_index("manufacturer", id,
435 desc->manufacturer);
436 if (desc->name[0])
437 ret |= env_set_by_index("name", id,
438 desc->name);
439 if (desc->revision[0])
440 ret |= env_set_by_index("rev", id,
441 desc->revision);
442 if (desc->serial[0])
443 ret |= env_set_by_index("serial", id,
444 desc->serial);
Michal Simekd61728c2020-08-03 13:01:45 +0200445
Michal Simekee5a4b82022-07-21 16:19:17 +0200446 if (desc->uuid[0]) {
Venkatesh Yadav Abbarapue1a193b2022-09-26 12:22:42 +0530447 unsigned char uuid[UUID_STR_LEN + 1];
448 unsigned char *t = desc->uuid;
Michal Simekee5a4b82022-07-21 16:19:17 +0200449
450 memset(uuid, 0, UUID_STR_LEN + 1);
451
452 sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
453 t[0], t[1], t[2], t[3], t[4], t[5],
454 t[6], t[7], t[8], t[9], t[10], t[11],
455 t[12], t[13], t[14], t[15]);
456 ret |= env_set_by_index("uuid", id, uuid);
457 }
458
Michal Simekd61728c2020-08-03 13:01:45 +0200459 if (!CONFIG_IS_ENABLED(NET))
460 continue;
461
Michal Simeka03b5942020-08-03 12:57:05 +0200462 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
Michal Simeka03b5942020-08-03 12:57:05 +0200463 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
464 ret |= eth_env_set_enetaddr_by_index("eth",
465 macid++, desc->mac_addr[i]);
466 }
Michal Simekd61728c2020-08-03 13:01:45 +0200467 }
468 }
469
Michal Simekca0f6162020-08-12 12:16:49 +0200470 if (ret)
471 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simekc8da6512020-07-09 15:57:56 +0200472
Michal Simek80fdef12020-03-31 12:39:37 +0200473 return 0;
474}
Michal Simek7c553ac2020-10-22 11:14:20 +0200475#endif
Michal Simek05af4832020-10-26 12:26:13 +0100476
Michal Simek57f71032021-07-23 09:55:59 +0200477static char *board_name = DEVICE_TREE;
478
Michal Simek05af4832020-10-26 12:26:13 +0100479int __maybe_unused board_fit_config_name_match(const char *name)
480{
Michal Simek57f71032021-07-23 09:55:59 +0200481 debug("%s: Check %s, default %s\n", __func__, name, board_name);
Michal Simek05af4832020-10-26 12:26:13 +0100482
Michal Simek8174cd22023-01-06 09:38:52 +0100483#if !defined(CONFIG_SPL_BUILD)
484 if (CONFIG_IS_ENABLED(REGEX)) {
485 struct slre slre;
486 int ret;
487
488 ret = slre_compile(&slre, name);
489 if (ret) {
490 ret = slre_match(&slre, board_name, strlen(board_name),
491 NULL);
492 debug("%s: name match ret = %d\n", __func__, ret);
493 return !ret;
494 }
495 }
496#endif
497
Michal Simek57f71032021-07-23 09:55:59 +0200498 if (!strcmp(name, board_name))
Michal Simek05af4832020-10-26 12:26:13 +0100499 return 0;
500
501 return -1;
502}
T Karthik Reddy80c0d382021-08-10 06:50:20 -0600503
Simon Glass866ec872023-02-05 15:39:38 -0700504#if IS_ENABLED(CONFIG_DTB_RESELECT)
Michal Simek52ff1622021-08-11 14:23:54 +0200505#define MAX_NAME_LENGTH 50
506
Michal Simek88232532021-07-23 09:59:59 +0200507char * __maybe_unused __weak board_name_decode(void)
508{
Michal Simek52ff1622021-08-11 14:23:54 +0200509 char *board_local_name;
510 struct xilinx_board_description *desc;
511 int i, id;
512
513 board_local_name = calloc(1, MAX_NAME_LENGTH);
514 if (!board_info)
515 return NULL;
516
517 for (id = 0; id <= highest_id; id++) {
518 desc = &board_info[id];
519
520 /* No board description */
521 if (!desc)
522 goto error;
523
524 /* Board is not detected */
525 if (desc->header != EEPROM_HEADER_MAGIC)
526 continue;
527
528 /* The first string should be soc name */
529 if (!id)
530 strcat(board_local_name, CONFIG_SYS_BOARD);
531
532 /*
533 * For two purpose here:
534 * soc_name- eg: zynqmp-
535 * and between base board and CC eg: ..revA-sck...
536 */
537 strcat(board_local_name, "-");
538
539 if (desc->name[0]) {
540 /* For DT composition name needs to be lowercase */
541 for (i = 0; i < sizeof(desc->name); i++)
542 desc->name[i] = tolower(desc->name[i]);
543
544 strcat(board_local_name, desc->name);
545 }
546 if (desc->revision[0]) {
547 strcat(board_local_name, "-rev");
548
549 /* And revision needs to be uppercase */
550 for (i = 0; i < sizeof(desc->revision); i++)
551 desc->revision[i] = toupper(desc->revision[i]);
552
553 strcat(board_local_name, desc->revision);
554 }
555 }
556
557 /*
558 * Longer strings will end up with buffer overflow and potential
559 * attacks that's why check it
560 */
561 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
562 panic("Board name can't be determined\n");
563
564 if (strlen(board_local_name))
565 return board_local_name;
566
567error:
568 free(board_local_name);
Michal Simek88232532021-07-23 09:59:59 +0200569 return NULL;
570}
571
572bool __maybe_unused __weak board_detection(void)
573{
Michal Simek52ff1622021-08-11 14:23:54 +0200574 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
575 int ret;
576
577 ret = xilinx_read_eeprom();
578 return !ret ? true : false;
579 }
580
Michal Simek88232532021-07-23 09:59:59 +0200581 return false;
582}
583
Michal Simek39d3c3c2022-09-06 12:40:41 +0200584bool __maybe_unused __weak soc_detection(void)
585{
586 return false;
587}
588
589char * __maybe_unused __weak soc_name_decode(void)
590{
591 return NULL;
592}
593
Michal Simek88232532021-07-23 09:59:59 +0200594int embedded_dtb_select(void)
595{
Michal Simek39d3c3c2022-09-06 12:40:41 +0200596 if (soc_detection()) {
597 char *soc_local_name;
598
599 soc_local_name = soc_name_decode();
600 if (soc_local_name) {
601 board_name = soc_local_name;
602 printf("Detected SOC name: %s\n", board_name);
603
604 /* Time to change DTB on fly */
605 /* Both ways should work here */
606 /* fdtdec_resetup(&rescan); */
607 return fdtdec_setup();
608 }
609 }
610
Michal Simek88232532021-07-23 09:59:59 +0200611 if (board_detection()) {
612 char *board_local_name;
613
614 board_local_name = board_name_decode();
615 if (board_local_name) {
616 board_name = board_local_name;
Michal Simek52ff1622021-08-11 14:23:54 +0200617 printf("Detected name: %s\n", board_name);
Michal Simek88232532021-07-23 09:59:59 +0200618
619 /* Time to change DTB on fly */
620 /* Both ways should work here */
621 /* fdtdec_resetup(&rescan); */
622 fdtdec_setup();
623 }
624 }
625 return 0;
626}
627#endif
Michal Simeka32c3e92022-08-25 14:23:10 +0200628
629#if defined(CONFIG_LMB)
Pali Rohár049704f2022-09-09 17:32:40 +0200630phys_size_t board_get_usable_ram_top(phys_size_t total_size)
Michal Simeka32c3e92022-08-25 14:23:10 +0200631{
632 phys_size_t size;
633 phys_addr_t reg;
634 struct lmb lmb;
635
636 if (!total_size)
637 return gd->ram_top;
638
639 if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
640 panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
641
642 /* found enough not-reserved memory to relocated U-Boot */
643 lmb_init(&lmb);
644 lmb_add(&lmb, gd->ram_base, gd->ram_size);
645 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
646 size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
647 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
648
649 if (!reg)
650 reg = gd->ram_top - size;
651
652 return reg + size;
653}
654#endif