blob: 17291a9d3b940c36aa0623a04976eec16e487812 [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>
Venkatesh Yadav Abbarapua2700992024-01-17 08:50:13 +053028#include <rng.h>
Michal Simek8174cd22023-01-06 09:38:52 +010029#include <slre.h>
T Karthik Reddy80c0d382021-08-10 06:50:20 -060030#include <soc.h>
Michal Simek52ff1622021-08-11 14:23:54 +020031#include <linux/ctype.h>
Sughosh Ganu741ef862022-04-15 11:29:34 +053032#include <linux/kernel.h>
Michal Simekee5a4b82022-07-21 16:19:17 +020033#include <uuid.h>
Michal Simek9755e3d2019-01-21 15:25:02 +010034
Michal Simekf149b392020-08-03 16:14:23 +020035#include "fru.h"
36
Simon Glass71aa8062023-02-05 15:39:42 -070037#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
Sughosh Ganu741ef862022-04-15 11:29:34 +053038struct efi_fw_image fw_images[] = {
39#if defined(XILINX_BOOT_IMAGE_GUID)
40 {
41 .image_type_id = XILINX_BOOT_IMAGE_GUID,
42 .fw_name = u"XILINX-BOOT",
43 .image_index = 1,
44 },
45#endif
Michal Simek3d3cb282024-02-23 17:18:42 +010046#if defined(XILINX_UBOOT_IMAGE_GUID) && defined(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME)
Sughosh Ganu741ef862022-04-15 11:29:34 +053047 {
48 .image_type_id = XILINX_UBOOT_IMAGE_GUID,
49 .fw_name = u"XILINX-UBOOT",
50 .image_index = 2,
51 },
52#endif
53};
54
55struct efi_capsule_update_info update_info = {
Masahisa Kojimacccea182023-06-07 14:41:51 +090056 .num_images = ARRAY_SIZE(fw_images),
Sughosh Ganu741ef862022-04-15 11:29:34 +053057 .images = fw_images,
58};
59
Sughosh Ganu741ef862022-04-15 11:29:34 +053060#endif /* EFI_HAVE_CAPSULE_SUPPORT */
61
Michal Simekd61728c2020-08-03 13:01:45 +020062#define EEPROM_HEADER_MAGIC 0xdaaddeed
63#define EEPROM_HDR_MANUFACTURER_LEN 16
64#define EEPROM_HDR_NAME_LEN 16
65#define EEPROM_HDR_REV_LEN 8
66#define EEPROM_HDR_SERIAL_LEN 20
67#define EEPROM_HDR_NO_OF_MAC_ADDR 4
68#define EEPROM_HDR_ETH_ALEN ETH_ALEN
Michal Simekee5a4b82022-07-21 16:19:17 +020069#define EEPROM_HDR_UUID_LEN 16
Michal Simekd61728c2020-08-03 13:01:45 +020070
71struct xilinx_board_description {
72 u32 header;
73 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
74 char name[EEPROM_HDR_NAME_LEN + 1];
75 char revision[EEPROM_HDR_REV_LEN + 1];
76 char serial[EEPROM_HDR_SERIAL_LEN + 1];
77 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
Michal Simekee5a4b82022-07-21 16:19:17 +020078 char uuid[EEPROM_HDR_UUID_LEN + 1];
Michal Simekd61728c2020-08-03 13:01:45 +020079};
80
Michal Simeka03b5942020-08-03 12:57:05 +020081static int highest_id = -1;
Michal Simekd9c93c92021-08-12 12:30:36 +020082static struct xilinx_board_description *board_info;
Michal Simekd61728c2020-08-03 13:01:45 +020083
Michal Simekf149b392020-08-03 16:14:23 +020084#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simekd61728c2020-08-03 13:01:45 +020085
86/* Variable which stores pointer to array which stores eeprom content */
87struct xilinx_legacy_format {
88 char board_sn[18]; /* 0x0 */
89 char unused0[14]; /* 0x12 */
Michal Simekb86b1352023-01-24 16:19:26 +010090 char eth_mac[ETH_ALEN]; /* 0x20 */
Michal Simekd61728c2020-08-03 13:01:45 +020091 char unused1[170]; /* 0x26 */
92 char board_name[11]; /* 0xd0 */
93 char unused2[5]; /* 0xdc */
94 char board_revision[3]; /* 0xe0 */
95 char unused3[29]; /* 0xe3 */
96};
97
98static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
99{
100 int i;
Venkatesh Yadav Abbarapue1a193b2022-09-26 12:22:42 +0530101 unsigned char byte;
Michal Simekd61728c2020-08-03 13:01:45 +0200102
103 for (i = 0; i < size; i++) {
104 byte = eeprom[i];
105
Michal Simeke6c62532023-01-24 16:19:27 +0100106 /* Remove all non printable chars but ignore MAC address */
107 if ((i < offsetof(struct xilinx_legacy_format, eth_mac) ||
108 i >= offsetof(struct xilinx_legacy_format, unused1)) &&
109 (byte < '!' || byte > '~')) {
Michal Simekd61728c2020-08-03 13:01:45 +0200110 eeprom[i] = 0;
Michal Simeke6c62532023-01-24 16:19:27 +0100111 continue;
112 }
Michal Simekd61728c2020-08-03 13:01:45 +0200113
114 /* Convert strings to lower case */
115 if (byte >= 'A' && byte <= 'Z')
116 eeprom[i] = byte + 'a' - 'A';
117 }
118}
119
120static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
121 struct xilinx_board_description *desc)
122{
123 int ret, size;
124 struct xilinx_legacy_format *eeprom_content;
125 bool eth_valid = false;
126
127 size = sizeof(*eeprom_content);
128
129 eeprom_content = calloc(1, size);
130 if (!eeprom_content)
131 return -ENOMEM;
132
133 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
134 eeprom_content);
135
136 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
137 if (ret) {
138 debug("%s: I2C EEPROM read failed\n", __func__);
139 free(eeprom_content);
140 return ret;
141 }
142
143 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
144
Michal Simek55631672023-01-24 16:19:28 +0100145 /* Terminating \0 chars are the part of desc fields already */
146 strlcpy(desc->name, eeprom_content->board_name,
147 sizeof(eeprom_content->board_name) + 1);
148 strlcpy(desc->revision, eeprom_content->board_revision,
149 sizeof(eeprom_content->board_revision) + 1);
150 strlcpy(desc->serial, eeprom_content->board_sn,
151 sizeof(eeprom_content->board_sn) + 1);
Michal Simekd61728c2020-08-03 13:01:45 +0200152
153 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
154 if (eth_valid)
Michal Simekd61728c2020-08-03 13:01:45 +0200155 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
156
Michal Simek55631672023-01-24 16:19:28 +0100157 printf("Xilinx I2C Legacy format at %s:\n", name);
158 printf(" Board name:\t%s\n", desc->name);
159 printf(" Board rev:\t%s\n", desc->revision);
160 printf(" Board SN:\t%s\n", desc->serial);
161
162 if (eth_valid)
163 printf(" Ethernet mac:\t%pM\n", desc->mac_addr);
164
Michal Simekd61728c2020-08-03 13:01:45 +0200165 desc->header = EEPROM_HEADER_MAGIC;
166
167 free(eeprom_content);
168
169 return ret;
170}
171
172static bool xilinx_detect_legacy(u8 *buffer)
173{
174 int i;
175 char c;
176
177 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
178 c = buffer[i];
179
180 if (c < '0' || c > '9')
181 return false;
182 }
183
184 return true;
185}
186
Michal Simekf149b392020-08-03 16:14:23 +0200187static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
188 struct xilinx_board_description *desc)
189{
Michal Simek530560b2021-08-12 11:03:49 +0200190 int i, ret, eeprom_size;
Michal Simekf149b392020-08-03 16:14:23 +0200191 u8 *fru_content;
Ashok Reddy Soma7a036b62022-02-23 15:00:59 +0100192 u8 id = 0;
Michal Simekf149b392020-08-03 16:14:23 +0200193
194 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
195 eeprom_size = i2c_eeprom_size(dev);
196
197 fru_content = calloc(1, eeprom_size);
198 if (!fru_content)
199 return -ENOMEM;
200
201 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
202 fru_content);
203
204 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
205 eeprom_size);
206 if (ret) {
207 debug("%s: I2C EEPROM read failed\n", __func__);
Michal Simekb2628632021-08-13 09:17:10 +0200208 goto end;
Michal Simekf149b392020-08-03 16:14:23 +0200209 }
210
Michal Simekf149b392020-08-03 16:14:23 +0200211 fru_capture((unsigned long)fru_content);
Simon Glass866ec872023-02-05 15:39:38 -0700212 if (gd->flags & GD_FLG_RELOC || (_DEBUG && IS_ENABLED(CONFIG_DTB_RESELECT))) {
Michal Simek52ff1622021-08-11 14:23:54 +0200213 printf("Xilinx I2C FRU format at %s:\n", name);
214 ret = fru_display(0);
215 if (ret) {
216 printf("FRU format decoding failed.\n");
217 goto end;
218 }
Michal Simekf149b392020-08-03 16:14:23 +0200219 }
220
221 if (desc->header == EEPROM_HEADER_MAGIC) {
222 debug("Information already filled\n");
Michal Simekb2628632021-08-13 09:17:10 +0200223 ret = -EINVAL;
224 goto end;
Michal Simekf149b392020-08-03 16:14:23 +0200225 }
226
227 /* It is clear that FRU was captured and structures were filled */
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200228 strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
Michal Simekf149b392020-08-03 16:14:23 +0200229 sizeof(desc->manufacturer));
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200230 strlcpy(desc->uuid, (char *)fru_data.brd.uuid,
Michal Simekee5a4b82022-07-21 16:19:17 +0200231 sizeof(desc->uuid));
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200232 strlcpy(desc->name, (char *)fru_data.brd.product_name,
Michal Simekf149b392020-08-03 16:14:23 +0200233 sizeof(desc->name));
Michal Simek530560b2021-08-12 11:03:49 +0200234 for (i = 0; i < sizeof(desc->name); i++) {
235 if (desc->name[i] == ' ')
236 desc->name[i] = '\0';
237 }
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200238 strlcpy(desc->revision, (char *)fru_data.brd.rev,
Michal Simekf149b392020-08-03 16:14:23 +0200239 sizeof(desc->revision));
Michal Simek93216272022-06-06 09:31:27 +0200240 for (i = 0; i < sizeof(desc->revision); i++) {
241 if (desc->revision[i] == ' ')
242 desc->revision[i] = '\0';
243 }
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200244 strlcpy(desc->serial, (char *)fru_data.brd.serial_number,
Michal Simekf149b392020-08-03 16:14:23 +0200245 sizeof(desc->serial));
Ashok Reddy Soma7a036b62022-02-23 15:00:59 +0100246
247 while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
248 if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
249 memcpy(&desc->mac_addr[id],
250 (char *)fru_data.mac.macid[id], ETH_ALEN);
251 id++;
252 }
253
Michal Simekf149b392020-08-03 16:14:23 +0200254 desc->header = EEPROM_HEADER_MAGIC;
255
Michal Simekb2628632021-08-13 09:17:10 +0200256end:
257 free(fru_content);
258 return ret;
Michal Simekf149b392020-08-03 16:14:23 +0200259}
260
261static bool xilinx_detect_fru(u8 *buffer)
262{
263 u8 checksum = 0;
264 int i;
265
266 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
267 if (checksum) {
268 debug("%s Common header CRC FAIL\n", __func__);
269 return false;
270 }
271
272 bool all_zeros = true;
273 /* Checksum over all zeros is also zero that's why detect this case */
274 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
275 if (buffer[i] != 0)
276 all_zeros = false;
277 }
278
279 if (all_zeros)
280 return false;
281
282 debug("%s Common header CRC PASS\n", __func__);
283 return true;
284}
285
Michal Simekd61728c2020-08-03 13:01:45 +0200286static int xilinx_read_eeprom_single(char *name,
287 struct xilinx_board_description *desc)
288{
289 int ret;
290 struct udevice *dev;
291 ofnode eeprom;
292 u8 buffer[XILINX_I2C_DETECTION_BITS];
293
294 eeprom = ofnode_get_aliases_node(name);
295 if (!ofnode_valid(eeprom))
296 return -ENODEV;
297
298 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
299 if (ret)
300 return ret;
301
302 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
303 if (ret) {
304 debug("%s: I2C EEPROM read failed\n", __func__);
305 return ret;
306 }
307
308 debug("%s: i2c memory detected: %s\n", __func__, name);
309
Simon Glassf8daba42023-02-05 15:36:31 -0700310 if (IS_ENABLED(CONFIG_CMD_FRU) && xilinx_detect_fru(buffer))
Michal Simekf149b392020-08-03 16:14:23 +0200311 return xilinx_read_eeprom_fru(dev, name, desc);
312
Michal Simekd61728c2020-08-03 13:01:45 +0200313 if (xilinx_detect_legacy(buffer))
314 return xilinx_read_eeprom_legacy(dev, name, desc);
315
316 return -ENODEV;
317}
318
319__maybe_unused int xilinx_read_eeprom(void)
320{
Michal Simekd9c93c92021-08-12 12:30:36 +0200321 int id;
Michal Simekd61728c2020-08-03 13:01:45 +0200322 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simeka03b5942020-08-03 12:57:05 +0200323 struct xilinx_board_description *desc;
Michal Simekd61728c2020-08-03 13:01:45 +0200324
325 highest_id = dev_read_alias_highest_id("nvmem");
326 /* No nvmem aliases present */
327 if (highest_id < 0)
328 return -EINVAL;
329
Michal Simekd9c93c92021-08-12 12:30:36 +0200330 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
Michal Simekd61728c2020-08-03 13:01:45 +0200331 if (!board_info)
332 return -ENOMEM;
333
334 debug("%s: Highest ID %d, board_info %p\n", __func__,
335 highest_id, board_info);
336
337 for (id = 0; id <= highest_id; id++) {
338 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
339
Michal Simeka03b5942020-08-03 12:57:05 +0200340 /* Alloc structure */
Michal Simekd9c93c92021-08-12 12:30:36 +0200341 desc = &board_info[id];
Michal Simeka03b5942020-08-03 12:57:05 +0200342
Michal Simekd61728c2020-08-03 13:01:45 +0200343 /* Ignoring return value for supporting multiple chips */
Michal Simekd9c93c92021-08-12 12:30:36 +0200344 xilinx_read_eeprom_single(name_buf, desc);
Michal Simekd61728c2020-08-03 13:01:45 +0200345 }
346
Michal Simeka03b5942020-08-03 12:57:05 +0200347 /*
348 * Consider to clean board_info structure when board/cards are not
349 * detected.
350 */
Michal Simekd61728c2020-08-03 13:01:45 +0200351
352 return 0;
353}
354
Michal Simekf0631002022-02-17 14:28:38 +0100355#if defined(CONFIG_OF_BOARD)
Ilias Apalodimase7fb7892021-10-26 09:12:33 +0300356void *board_fdt_blob_setup(int *err)
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100357{
Michal Simeke2572b52020-09-04 16:21:47 +0200358 void *fdt_blob;
Michal Simek453bb772020-03-19 10:23:56 +0100359
Ilias Apalodimase7fb7892021-10-26 09:12:33 +0300360 *err = 0;
Michal Simek451b2ea2024-02-14 12:52:33 +0100361
362 if (IS_ENABLED(CONFIG_TARGET_XILINX_MBV)) {
363 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
364
365 if (fdt_magic(fdt_blob) == FDT_MAGIC)
366 return fdt_blob;
367 }
368
Michal Simeka672b982021-01-04 11:07:28 +0100369 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
370 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simekfc3c6fd2021-02-02 13:33:22 +0100371 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek506009f2021-01-04 11:03:36 +0100372 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100373
Michal Simek506009f2021-01-04 11:03:36 +0100374 if (fdt_magic(fdt_blob) == FDT_MAGIC)
375 return fdt_blob;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100376
Michal Simek506009f2021-01-04 11:03:36 +0100377 debug("DTB is not passed via %p\n", fdt_blob);
378 }
Michal Simekfc274a52019-12-19 17:45:15 +0100379
Michal Simek506009f2021-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))
Shiji Yangccea96f2023-08-03 09:47:17 +0800386 fdt_blob = (ulong *)_image_binary_end;
Michal Simek506009f2021-01-04 11:03:36 +0100387 else
Shiji Yangccea96f2023-08-03 09:47:17 +0800388 fdt_blob = (ulong *)__bss_end;
Michal Simek506009f2021-01-04 11:03:36 +0100389 } else {
390 /* FDT is at end of image */
Shiji Yangccea96f2023-08-03 09:47:17 +0800391 fdt_blob = (ulong *)_end;
Michal Simek506009f2021-01-04 11:03:36 +0100392 }
Michal Simekfc274a52019-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);
398
Michal Simekf0631002022-02-17 14:28:38 +0100399 *err = -EINVAL;
Michal Simekfc274a52019-12-19 17:45:15 +0100400 return NULL;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100401}
402#endif
Michal Simek80fdef12020-03-31 12:39:37 +0200403
Michal Simek7c553ac2020-10-22 11:14:20 +0200404#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simeka03b5942020-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 Simek80fdef12020-03-31 12:39:37 +0200417int board_late_init_xilinx(void)
418{
Michal Simekca0f6162020-08-12 12:16:49 +0200419 u32 ret = 0;
Michal Simeka03b5942020-08-03 12:57:05 +0200420 int i, id, macid = 0;
421 struct xilinx_board_description *desc;
Ricardo Salvetie6e3b9d2022-01-20 16:17:30 -0300422 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
Michal Simek0bbc9622023-08-31 09:04:28 +0200423 u64 bootscr_flash_offset, bootscr_flash_size;
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600424
Simon Glass0c22cdc2023-02-05 15:40:14 -0700425 if (!IS_ENABLED(CONFIG_MICROBLAZE)) {
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600426 ulong scriptaddr;
Algapally Santosh Sagar5528f792023-08-31 08:59:06 +0200427 u64 bootscr_address;
428 u64 bootscr_offset;
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600429
Algapally Santosh Sagar5528f792023-08-31 08:59:06 +0200430 /* Fetch bootscr_address/bootscr_offset from DT and update */
431 if (!ofnode_read_bootscript_address(&bootscr_address,
432 &bootscr_offset)) {
433 if (bootscr_offset)
434 ret |= env_set_hex("scriptaddr",
435 gd->ram_base +
436 bootscr_offset);
437 else
438 ret |= env_set_hex("scriptaddr",
439 bootscr_address);
440 } else {
441 /* Update scriptaddr(bootscr offset) from env */
442 scriptaddr = env_get_hex("scriptaddr", 0);
443 ret |= env_set_hex("scriptaddr",
444 gd->ram_base + scriptaddr);
445 }
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600446 }
Michal Simek2570cc62020-08-12 12:17:53 +0200447
Michal Simek0bbc9622023-08-31 09:04:28 +0200448 if (!ofnode_read_bootscript_flash(&bootscr_flash_offset,
449 &bootscr_flash_size)) {
450 ret |= env_set_hex("script_offset_f", bootscr_flash_offset);
451 ret |= env_set_hex("script_size_f", bootscr_flash_size);
452 } else {
453 debug("!!! Please define bootscr-flash-offset via DT !!!\n");
454 ret |= env_set_hex("script_offset_f",
455 CONFIG_BOOT_SCRIPT_OFFSET);
456 }
457
Simon Glass0c22cdc2023-02-05 15:40:14 -0700458 if (IS_ENABLED(CONFIG_ARCH_ZYNQ) || IS_ENABLED(CONFIG_MICROBLAZE))
Michal Simek2570cc62020-08-12 12:17:53 +0200459 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simek80fdef12020-03-31 12:39:37 +0200460
Michal Simekca0f6162020-08-12 12:16:49 +0200461 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simek2570cc62020-08-12 12:17:53 +0200462 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simekca0f6162020-08-12 12:16:49 +0200463
Michal Simeka03b5942020-08-03 12:57:05 +0200464 for (id = 0; id <= highest_id; id++) {
Michal Simekd9c93c92021-08-12 12:30:36 +0200465 desc = &board_info[id];
Michal Simeka03b5942020-08-03 12:57:05 +0200466 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
467 if (desc->manufacturer[0])
468 ret |= env_set_by_index("manufacturer", id,
469 desc->manufacturer);
470 if (desc->name[0])
471 ret |= env_set_by_index("name", id,
472 desc->name);
473 if (desc->revision[0])
474 ret |= env_set_by_index("rev", id,
475 desc->revision);
476 if (desc->serial[0])
477 ret |= env_set_by_index("serial", id,
478 desc->serial);
Michal Simekd61728c2020-08-03 13:01:45 +0200479
Michal Simekee5a4b82022-07-21 16:19:17 +0200480 if (desc->uuid[0]) {
Venkatesh Yadav Abbarapue1a193b2022-09-26 12:22:42 +0530481 unsigned char uuid[UUID_STR_LEN + 1];
482 unsigned char *t = desc->uuid;
Michal Simekee5a4b82022-07-21 16:19:17 +0200483
484 memset(uuid, 0, UUID_STR_LEN + 1);
485
486 sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
487 t[0], t[1], t[2], t[3], t[4], t[5],
488 t[6], t[7], t[8], t[9], t[10], t[11],
489 t[12], t[13], t[14], t[15]);
490 ret |= env_set_by_index("uuid", id, uuid);
491 }
492
Michal Simekd61728c2020-08-03 13:01:45 +0200493 if (!CONFIG_IS_ENABLED(NET))
494 continue;
495
Michal Simeka03b5942020-08-03 12:57:05 +0200496 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
Michal Simeka03b5942020-08-03 12:57:05 +0200497 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
498 ret |= eth_env_set_enetaddr_by_index("eth",
499 macid++, desc->mac_addr[i]);
500 }
Michal Simekd61728c2020-08-03 13:01:45 +0200501 }
502 }
503
Michal Simekca0f6162020-08-12 12:16:49 +0200504 if (ret)
505 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simekc8da6512020-07-09 15:57:56 +0200506
Michal Simek80fdef12020-03-31 12:39:37 +0200507 return 0;
508}
Michal Simek7c553ac2020-10-22 11:14:20 +0200509#endif
Michal Simek05af4832020-10-26 12:26:13 +0100510
Michal Simek57f71032021-07-23 09:55:59 +0200511static char *board_name = DEVICE_TREE;
512
Michal Simek05af4832020-10-26 12:26:13 +0100513int __maybe_unused board_fit_config_name_match(const char *name)
514{
Michal Simek57f71032021-07-23 09:55:59 +0200515 debug("%s: Check %s, default %s\n", __func__, name, board_name);
Michal Simek05af4832020-10-26 12:26:13 +0100516
Michal Simek8174cd22023-01-06 09:38:52 +0100517#if !defined(CONFIG_SPL_BUILD)
Simon Glasseca09862023-02-05 15:40:37 -0700518 if (IS_ENABLED(CONFIG_REGEX)) {
Michal Simek8174cd22023-01-06 09:38:52 +0100519 struct slre slre;
520 int ret;
521
522 ret = slre_compile(&slre, name);
523 if (ret) {
524 ret = slre_match(&slre, board_name, strlen(board_name),
525 NULL);
526 debug("%s: name match ret = %d\n", __func__, ret);
527 return !ret;
528 }
529 }
530#endif
531
Michal Simek57f71032021-07-23 09:55:59 +0200532 if (!strcmp(name, board_name))
Michal Simek05af4832020-10-26 12:26:13 +0100533 return 0;
534
535 return -1;
536}
T Karthik Reddy80c0d382021-08-10 06:50:20 -0600537
Simon Glass866ec872023-02-05 15:39:38 -0700538#if IS_ENABLED(CONFIG_DTB_RESELECT)
Michal Simek52ff1622021-08-11 14:23:54 +0200539#define MAX_NAME_LENGTH 50
540
Michal Simek88232532021-07-23 09:59:59 +0200541char * __maybe_unused __weak board_name_decode(void)
542{
Michal Simek52ff1622021-08-11 14:23:54 +0200543 char *board_local_name;
544 struct xilinx_board_description *desc;
545 int i, id;
546
547 board_local_name = calloc(1, MAX_NAME_LENGTH);
548 if (!board_info)
549 return NULL;
550
551 for (id = 0; id <= highest_id; id++) {
552 desc = &board_info[id];
553
554 /* No board description */
555 if (!desc)
556 goto error;
557
558 /* Board is not detected */
559 if (desc->header != EEPROM_HEADER_MAGIC)
560 continue;
561
562 /* The first string should be soc name */
563 if (!id)
564 strcat(board_local_name, CONFIG_SYS_BOARD);
565
566 /*
567 * For two purpose here:
568 * soc_name- eg: zynqmp-
569 * and between base board and CC eg: ..revA-sck...
570 */
571 strcat(board_local_name, "-");
572
573 if (desc->name[0]) {
574 /* For DT composition name needs to be lowercase */
575 for (i = 0; i < sizeof(desc->name); i++)
576 desc->name[i] = tolower(desc->name[i]);
577
578 strcat(board_local_name, desc->name);
579 }
580 if (desc->revision[0]) {
581 strcat(board_local_name, "-rev");
582
583 /* And revision needs to be uppercase */
584 for (i = 0; i < sizeof(desc->revision); i++)
585 desc->revision[i] = toupper(desc->revision[i]);
586
587 strcat(board_local_name, desc->revision);
588 }
589 }
590
591 /*
592 * Longer strings will end up with buffer overflow and potential
593 * attacks that's why check it
594 */
595 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
596 panic("Board name can't be determined\n");
597
598 if (strlen(board_local_name))
599 return board_local_name;
600
601error:
602 free(board_local_name);
Michal Simek88232532021-07-23 09:59:59 +0200603 return NULL;
604}
605
606bool __maybe_unused __weak board_detection(void)
607{
Michal Simek52ff1622021-08-11 14:23:54 +0200608 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
609 int ret;
610
611 ret = xilinx_read_eeprom();
612 return !ret ? true : false;
613 }
614
Michal Simek88232532021-07-23 09:59:59 +0200615 return false;
616}
617
Michal Simek39d3c3c2022-09-06 12:40:41 +0200618bool __maybe_unused __weak soc_detection(void)
619{
620 return false;
621}
622
623char * __maybe_unused __weak soc_name_decode(void)
624{
625 return NULL;
626}
627
Michal Simek88232532021-07-23 09:59:59 +0200628int embedded_dtb_select(void)
629{
Michal Simek39d3c3c2022-09-06 12:40:41 +0200630 if (soc_detection()) {
631 char *soc_local_name;
632
633 soc_local_name = soc_name_decode();
634 if (soc_local_name) {
635 board_name = soc_local_name;
636 printf("Detected SOC name: %s\n", board_name);
637
638 /* Time to change DTB on fly */
639 /* Both ways should work here */
640 /* fdtdec_resetup(&rescan); */
641 return fdtdec_setup();
642 }
643 }
644
Michal Simek88232532021-07-23 09:59:59 +0200645 if (board_detection()) {
646 char *board_local_name;
647
648 board_local_name = board_name_decode();
649 if (board_local_name) {
650 board_name = board_local_name;
Michal Simek52ff1622021-08-11 14:23:54 +0200651 printf("Detected name: %s\n", board_name);
Michal Simek88232532021-07-23 09:59:59 +0200652
653 /* Time to change DTB on fly */
654 /* Both ways should work here */
655 /* fdtdec_resetup(&rescan); */
656 fdtdec_setup();
657 }
658 }
659 return 0;
660}
661#endif
Michal Simeka32c3e92022-08-25 14:23:10 +0200662
663#if defined(CONFIG_LMB)
Michal Simek7576ab22023-11-06 12:56:47 +0100664
665#ifndef MMU_SECTION_SIZE
666#define MMU_SECTION_SIZE (1 * 1024 * 1024)
667#endif
668
Heinrich Schuchardtd768dd82023-08-12 20:16:58 +0200669phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Michal Simeka32c3e92022-08-25 14:23:10 +0200670{
671 phys_size_t size;
672 phys_addr_t reg;
673 struct lmb lmb;
674
675 if (!total_size)
676 return gd->ram_top;
677
678 if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
679 panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
680
681 /* found enough not-reserved memory to relocated U-Boot */
682 lmb_init(&lmb);
683 lmb_add(&lmb, gd->ram_base, gd->ram_size);
684 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
685 size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
686 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
687
688 if (!reg)
689 reg = gd->ram_top - size;
690
691 return reg + size;
692}
693#endif
Venkatesh Yadav Abbarapua2700992024-01-17 08:50:13 +0530694
695#ifdef CONFIG_OF_BOARD_SETUP
696#define MAX_RAND_SIZE 8
697int ft_board_setup(void *blob, struct bd_info *bd)
698{
699 size_t n = MAX_RAND_SIZE;
700 struct udevice *dev;
701 u8 buf[MAX_RAND_SIZE];
702 int nodeoffset, ret;
703
704 if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
705 debug("No RNG device\n");
706 return 0;
707 }
708
709 if (dm_rng_read(dev, buf, n)) {
710 debug("Reading RNG failed\n");
711 return 0;
712 }
713
714 if (!blob) {
715 debug("No FDT memory address configured. Please configure\n"
716 "the FDT address via \"fdt addr <address>\" command.\n"
717 "Aborting!\n");
718 return 0;
719 }
720
721 ret = fdt_check_header(blob);
722 if (ret < 0) {
723 debug("fdt_chosen: %s\n", fdt_strerror(ret));
724 return ret;
725 }
726
727 nodeoffset = fdt_find_or_add_subnode(blob, 0, "chosen");
728 if (nodeoffset < 0) {
729 debug("Reading chosen node failed\n");
730 return nodeoffset;
731 }
732
733 ret = fdt_setprop(blob, nodeoffset, "kaslr-seed", buf, sizeof(buf));
734 if (ret < 0) {
735 debug("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(ret));
736 return ret;
737 }
738
739 return 0;
740}
741#endif