blob: a12dccd4c518a443ecc4495f6ba2ca72a6660ea1 [file] [log] [blame]
Michal Simek014d3042019-01-21 15:25:02 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Michal Simek53f44362023-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 Simek014d3042019-01-21 15:25:02 +01007 */
8
Sughosh Ganuccb36462022-04-15 11:29:34 +05309#include <efi.h>
10#include <efi_loader.h>
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <env.h>
Michal Simekaa975612022-08-25 14:23:10 +020012#include <image.h>
Algapally Santosh Sagard1c1b1e2023-01-19 22:36:14 -070013#include <init.h>
James Hilliard2c234642024-03-31 17:28:59 -060014#include <jffs2/load_kernel.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Michal Simek878ba362019-12-19 17:45:15 +010017#include <asm/sections.h>
Michal Simek014d3042019-01-21 15:25:02 +010018#include <dm/uclass.h>
19#include <i2c.h>
Michal Simekb90b97e2020-04-08 10:51:36 +020020#include <linux/sizes.h>
Michal Simek394ee242020-08-03 13:01:45 +020021#include <malloc.h>
Sughosh Ganud8abe432024-10-25 22:57:24 +053022#include <memtop.h>
James Hilliard2c234642024-03-31 17:28:59 -060023#include <mtd_node.h>
Michal Simek705d44a2020-03-31 12:39:37 +020024#include "board.h"
Michal Simek394ee242020-08-03 13:01:45 +020025#include <dm.h>
26#include <i2c_eeprom.h>
27#include <net.h>
Michal Simekfe49df92020-10-26 12:26:13 +010028#include <generated/dt.h>
Venkatesh Yadav Abbarapue8e3c902024-01-17 08:50:13 +053029#include <rng.h>
Michal Simek53f44362023-01-06 09:38:52 +010030#include <slre.h>
T Karthik Reddyc8fa40a2021-08-10 06:50:20 -060031#include <soc.h>
Michal Simeke0453512021-08-11 14:23:54 +020032#include <linux/ctype.h>
Sughosh Ganuccb36462022-04-15 11:29:34 +053033#include <linux/kernel.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010034#include <u-boot/uuid.h>
Michal Simek014d3042019-01-21 15:25:02 +010035
Michal Simek207e0622020-08-03 16:14:23 +020036#include "fru.h"
37
Simon Glassb8196212023-02-05 15:39:42 -070038#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
Sughosh Ganuccb36462022-04-15 11:29:34 +053039struct efi_fw_image fw_images[] = {
40#if defined(XILINX_BOOT_IMAGE_GUID)
41 {
42 .image_type_id = XILINX_BOOT_IMAGE_GUID,
43 .fw_name = u"XILINX-BOOT",
44 .image_index = 1,
45 },
46#endif
Michal Simekcd038612024-02-23 17:18:42 +010047#if defined(XILINX_UBOOT_IMAGE_GUID) && defined(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME)
Sughosh Ganuccb36462022-04-15 11:29:34 +053048 {
49 .image_type_id = XILINX_UBOOT_IMAGE_GUID,
50 .fw_name = u"XILINX-UBOOT",
51 .image_index = 2,
52 },
53#endif
54};
55
56struct efi_capsule_update_info update_info = {
Masahisa Kojima5d2438b2023-06-07 14:41:51 +090057 .num_images = ARRAY_SIZE(fw_images),
Sughosh Ganuccb36462022-04-15 11:29:34 +053058 .images = fw_images,
59};
60
Sughosh Ganuccb36462022-04-15 11:29:34 +053061#endif /* EFI_HAVE_CAPSULE_SUPPORT */
62
Michal Simek394ee242020-08-03 13:01:45 +020063#define EEPROM_HEADER_MAGIC 0xdaaddeed
64#define EEPROM_HDR_MANUFACTURER_LEN 16
65#define EEPROM_HDR_NAME_LEN 16
66#define EEPROM_HDR_REV_LEN 8
67#define EEPROM_HDR_SERIAL_LEN 20
68#define EEPROM_HDR_NO_OF_MAC_ADDR 4
69#define EEPROM_HDR_ETH_ALEN ETH_ALEN
Michal Simek7459d4a2022-07-21 16:19:17 +020070#define EEPROM_HDR_UUID_LEN 16
Michal Simek394ee242020-08-03 13:01:45 +020071
72struct xilinx_board_description {
73 u32 header;
74 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
75 char name[EEPROM_HDR_NAME_LEN + 1];
76 char revision[EEPROM_HDR_REV_LEN + 1];
77 char serial[EEPROM_HDR_SERIAL_LEN + 1];
78 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
Michal Simek7459d4a2022-07-21 16:19:17 +020079 char uuid[EEPROM_HDR_UUID_LEN + 1];
Michal Simek394ee242020-08-03 13:01:45 +020080};
81
Michal Simek0bb24e12020-08-03 12:57:05 +020082static int highest_id = -1;
Michal Simeke98ae1d2021-08-12 12:30:36 +020083static struct xilinx_board_description *board_info;
Michal Simek394ee242020-08-03 13:01:45 +020084
Michal Simek207e0622020-08-03 16:14:23 +020085#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simek394ee242020-08-03 13:01:45 +020086
87/* Variable which stores pointer to array which stores eeprom content */
88struct xilinx_legacy_format {
89 char board_sn[18]; /* 0x0 */
90 char unused0[14]; /* 0x12 */
Michal Simekb2df1df2023-01-24 16:19:26 +010091 char eth_mac[ETH_ALEN]; /* 0x20 */
Michal Simek394ee242020-08-03 13:01:45 +020092 char unused1[170]; /* 0x26 */
93 char board_name[11]; /* 0xd0 */
94 char unused2[5]; /* 0xdc */
95 char board_revision[3]; /* 0xe0 */
96 char unused3[29]; /* 0xe3 */
97};
98
99static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
100{
101 int i;
Venkatesh Yadav Abbarapu3be370d2022-09-26 12:22:42 +0530102 unsigned char byte;
Michal Simek394ee242020-08-03 13:01:45 +0200103
104 for (i = 0; i < size; i++) {
105 byte = eeprom[i];
106
Petr Zejdldf8123b2024-04-04 13:44:22 +0200107 /* Ignore MAC address */
108 if (i >= offsetof(struct xilinx_legacy_format, eth_mac) &&
109 i < offsetof(struct xilinx_legacy_format, unused1)) {
110 continue;
111 }
112
113 /* Remove all non printable chars */
114 if (byte < '!' || byte > '~') {
Michal Simek394ee242020-08-03 13:01:45 +0200115 eeprom[i] = 0;
Michal Simekd432cbb2023-01-24 16:19:27 +0100116 continue;
117 }
Michal Simek394ee242020-08-03 13:01:45 +0200118
119 /* Convert strings to lower case */
120 if (byte >= 'A' && byte <= 'Z')
121 eeprom[i] = byte + 'a' - 'A';
122 }
123}
124
125static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
126 struct xilinx_board_description *desc)
127{
128 int ret, size;
129 struct xilinx_legacy_format *eeprom_content;
130 bool eth_valid = false;
131
132 size = sizeof(*eeprom_content);
133
134 eeprom_content = calloc(1, size);
135 if (!eeprom_content)
136 return -ENOMEM;
137
138 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
139 eeprom_content);
140
141 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
142 if (ret) {
143 debug("%s: I2C EEPROM read failed\n", __func__);
144 free(eeprom_content);
145 return ret;
146 }
147
148 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
149
Michal Simekdaccab22023-01-24 16:19:28 +0100150 /* Terminating \0 chars are the part of desc fields already */
151 strlcpy(desc->name, eeprom_content->board_name,
152 sizeof(eeprom_content->board_name) + 1);
153 strlcpy(desc->revision, eeprom_content->board_revision,
154 sizeof(eeprom_content->board_revision) + 1);
155 strlcpy(desc->serial, eeprom_content->board_sn,
156 sizeof(eeprom_content->board_sn) + 1);
Michal Simek394ee242020-08-03 13:01:45 +0200157
158 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
159 if (eth_valid)
Michal Simekdaccab22023-01-24 16:19:28 +0100160 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
Michal Simek394ee242020-08-03 13:01:45 +0200161
Michal Simekdaccab22023-01-24 16:19:28 +0100162 printf("Xilinx I2C Legacy format at %s:\n", name);
163 printf(" Board name:\t%s\n", desc->name);
164 printf(" Board rev:\t%s\n", desc->revision);
165 printf(" Board SN:\t%s\n", desc->serial);
166
Michal Simek394ee242020-08-03 13:01:45 +0200167 if (eth_valid)
Michal Simekdaccab22023-01-24 16:19:28 +0100168 printf(" Ethernet mac:\t%pM\n", desc->mac_addr);
Michal Simek394ee242020-08-03 13:01:45 +0200169
170 desc->header = EEPROM_HEADER_MAGIC;
171
172 free(eeprom_content);
173
174 return ret;
175}
176
177static bool xilinx_detect_legacy(u8 *buffer)
178{
179 int i;
180 char c;
181
182 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
183 c = buffer[i];
184
185 if (c < '0' || c > '9')
186 return false;
187 }
188
189 return true;
190}
191
Michal Simek207e0622020-08-03 16:14:23 +0200192static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
193 struct xilinx_board_description *desc)
194{
Michal Simekbe5f5722021-08-12 11:03:49 +0200195 int i, ret, eeprom_size;
Michal Simek207e0622020-08-03 16:14:23 +0200196 u8 *fru_content;
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100197 u8 id = 0;
Michal Simek207e0622020-08-03 16:14:23 +0200198
199 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
200 eeprom_size = i2c_eeprom_size(dev);
201
202 fru_content = calloc(1, eeprom_size);
203 if (!fru_content)
204 return -ENOMEM;
205
206 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
207 fru_content);
208
209 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
210 eeprom_size);
211 if (ret) {
212 debug("%s: I2C EEPROM read failed\n", __func__);
Michal Simekaada8a62021-08-13 09:17:10 +0200213 goto end;
Michal Simek207e0622020-08-03 16:14:23 +0200214 }
215
Michal Simek207e0622020-08-03 16:14:23 +0200216 fru_capture((unsigned long)fru_content);
Simon Glassd2c5a3d2023-02-05 15:39:38 -0700217 if (gd->flags & GD_FLG_RELOC || (_DEBUG && IS_ENABLED(CONFIG_DTB_RESELECT))) {
Michal Simeke0453512021-08-11 14:23:54 +0200218 printf("Xilinx I2C FRU format at %s:\n", name);
219 ret = fru_display(0);
220 if (ret) {
221 printf("FRU format decoding failed.\n");
222 goto end;
223 }
Michal Simek207e0622020-08-03 16:14:23 +0200224 }
225
226 if (desc->header == EEPROM_HEADER_MAGIC) {
227 debug("Information already filled\n");
Michal Simekaada8a62021-08-13 09:17:10 +0200228 ret = -EINVAL;
229 goto end;
Michal Simek207e0622020-08-03 16:14:23 +0200230 }
231
232 /* It is clear that FRU was captured and structures were filled */
Michal Simekb5f206e2022-07-21 16:19:18 +0200233 strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
Michal Simek207e0622020-08-03 16:14:23 +0200234 sizeof(desc->manufacturer));
Michal Simekb5f206e2022-07-21 16:19:18 +0200235 strlcpy(desc->uuid, (char *)fru_data.brd.uuid,
Michal Simek7459d4a2022-07-21 16:19:17 +0200236 sizeof(desc->uuid));
Michal Simekb5f206e2022-07-21 16:19:18 +0200237 strlcpy(desc->name, (char *)fru_data.brd.product_name,
Michal Simek207e0622020-08-03 16:14:23 +0200238 sizeof(desc->name));
Michal Simekbe5f5722021-08-12 11:03:49 +0200239 for (i = 0; i < sizeof(desc->name); i++) {
240 if (desc->name[i] == ' ')
241 desc->name[i] = '\0';
242 }
Michal Simekb5f206e2022-07-21 16:19:18 +0200243 strlcpy(desc->revision, (char *)fru_data.brd.rev,
Michal Simek207e0622020-08-03 16:14:23 +0200244 sizeof(desc->revision));
Michal Simek904fb972022-06-06 09:31:27 +0200245 for (i = 0; i < sizeof(desc->revision); i++) {
246 if (desc->revision[i] == ' ')
247 desc->revision[i] = '\0';
248 }
Michal Simekb5f206e2022-07-21 16:19:18 +0200249 strlcpy(desc->serial, (char *)fru_data.brd.serial_number,
Michal Simek207e0622020-08-03 16:14:23 +0200250 sizeof(desc->serial));
Ashok Reddy Soma9f60e442022-02-23 15:00:59 +0100251
252 while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
253 if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
254 memcpy(&desc->mac_addr[id],
255 (char *)fru_data.mac.macid[id], ETH_ALEN);
256 id++;
257 }
258
Michal Simek207e0622020-08-03 16:14:23 +0200259 desc->header = EEPROM_HEADER_MAGIC;
260
Michal Simekaada8a62021-08-13 09:17:10 +0200261end:
262 free(fru_content);
263 return ret;
Michal Simek207e0622020-08-03 16:14:23 +0200264}
265
266static bool xilinx_detect_fru(u8 *buffer)
267{
268 u8 checksum = 0;
269 int i;
270
271 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
272 if (checksum) {
273 debug("%s Common header CRC FAIL\n", __func__);
274 return false;
275 }
276
277 bool all_zeros = true;
278 /* Checksum over all zeros is also zero that's why detect this case */
279 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
280 if (buffer[i] != 0)
281 all_zeros = false;
282 }
283
284 if (all_zeros)
285 return false;
286
287 debug("%s Common header CRC PASS\n", __func__);
288 return true;
289}
290
Michal Simek394ee242020-08-03 13:01:45 +0200291static int xilinx_read_eeprom_single(char *name,
292 struct xilinx_board_description *desc)
293{
294 int ret;
295 struct udevice *dev;
296 ofnode eeprom;
297 u8 buffer[XILINX_I2C_DETECTION_BITS];
298
299 eeprom = ofnode_get_aliases_node(name);
300 if (!ofnode_valid(eeprom))
301 return -ENODEV;
302
303 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
304 if (ret)
305 return ret;
306
307 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
308 if (ret) {
309 debug("%s: I2C EEPROM read failed\n", __func__);
310 return ret;
311 }
312
313 debug("%s: i2c memory detected: %s\n", __func__, name);
314
Simon Glass631bec22023-02-05 15:36:31 -0700315 if (IS_ENABLED(CONFIG_CMD_FRU) && xilinx_detect_fru(buffer))
Michal Simek207e0622020-08-03 16:14:23 +0200316 return xilinx_read_eeprom_fru(dev, name, desc);
317
Michal Simek394ee242020-08-03 13:01:45 +0200318 if (xilinx_detect_legacy(buffer))
319 return xilinx_read_eeprom_legacy(dev, name, desc);
320
321 return -ENODEV;
322}
323
324__maybe_unused int xilinx_read_eeprom(void)
325{
Michal Simeke98ae1d2021-08-12 12:30:36 +0200326 int id;
Michal Simek394ee242020-08-03 13:01:45 +0200327 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simek0bb24e12020-08-03 12:57:05 +0200328 struct xilinx_board_description *desc;
Michal Simek394ee242020-08-03 13:01:45 +0200329
330 highest_id = dev_read_alias_highest_id("nvmem");
331 /* No nvmem aliases present */
332 if (highest_id < 0)
333 return -EINVAL;
334
Michal Simeke98ae1d2021-08-12 12:30:36 +0200335 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
Michal Simek394ee242020-08-03 13:01:45 +0200336 if (!board_info)
337 return -ENOMEM;
338
339 debug("%s: Highest ID %d, board_info %p\n", __func__,
340 highest_id, board_info);
341
342 for (id = 0; id <= highest_id; id++) {
343 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
344
Michal Simek0bb24e12020-08-03 12:57:05 +0200345 /* Alloc structure */
Michal Simeke98ae1d2021-08-12 12:30:36 +0200346 desc = &board_info[id];
Michal Simek0bb24e12020-08-03 12:57:05 +0200347
Michal Simek394ee242020-08-03 13:01:45 +0200348 /* Ignoring return value for supporting multiple chips */
Michal Simeke98ae1d2021-08-12 12:30:36 +0200349 xilinx_read_eeprom_single(name_buf, desc);
Michal Simek394ee242020-08-03 13:01:45 +0200350 }
351
Michal Simek0bb24e12020-08-03 12:57:05 +0200352 /*
353 * Consider to clean board_info structure when board/cards are not
354 * detected.
355 */
Michal Simek394ee242020-08-03 13:01:45 +0200356
357 return 0;
358}
359
Michal Simek1a505292022-02-17 14:28:38 +0100360#if defined(CONFIG_OF_BOARD)
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300361void *board_fdt_blob_setup(int *err)
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100362{
Michal Simekddf69ae2020-09-04 16:21:47 +0200363 void *fdt_blob;
Michal Simekc89f4292020-03-19 10:23:56 +0100364
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300365 *err = 0;
Michal Simek54a898c2024-02-14 12:52:33 +0100366
367 if (IS_ENABLED(CONFIG_TARGET_XILINX_MBV)) {
368 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
369
370 if (fdt_magic(fdt_blob) == FDT_MAGIC)
371 return fdt_blob;
372 }
373
Simon Glass49c24a82024-09-29 19:49:47 -0600374 if (!IS_ENABLED(CONFIG_XPL_BUILD) &&
Michal Simek663c1622021-01-04 11:07:28 +0100375 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simek6d4317b2021-02-02 13:33:22 +0100376 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek9b9d01e2021-01-04 11:03:36 +0100377 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100378
Michal Simek9b9d01e2021-01-04 11:03:36 +0100379 if (fdt_magic(fdt_blob) == FDT_MAGIC)
380 return fdt_blob;
Michal Simek878ba362019-12-19 17:45:15 +0100381
Michal Simek9b9d01e2021-01-04 11:03:36 +0100382 debug("DTB is not passed via %p\n", fdt_blob);
383 }
Michal Simek878ba362019-12-19 17:45:15 +0100384
Simon Glass49c24a82024-09-29 19:49:47 -0600385 if (IS_ENABLED(CONFIG_XPL_BUILD)) {
Michal Simek9b9d01e2021-01-04 11:03:36 +0100386 /*
387 * FDT is at end of BSS unless it is in a different memory
388 * region
389 */
390 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
Shiji Yangeff11fa2023-08-03 09:47:17 +0800391 fdt_blob = (ulong *)_image_binary_end;
Michal Simek9b9d01e2021-01-04 11:03:36 +0100392 else
Shiji Yangeff11fa2023-08-03 09:47:17 +0800393 fdt_blob = (ulong *)__bss_end;
Michal Simek9b9d01e2021-01-04 11:03:36 +0100394 } else {
395 /* FDT is at end of image */
Shiji Yangeff11fa2023-08-03 09:47:17 +0800396 fdt_blob = (ulong *)_end;
Michal Simek9b9d01e2021-01-04 11:03:36 +0100397 }
Michal Simek878ba362019-12-19 17:45:15 +0100398
399 if (fdt_magic(fdt_blob) == FDT_MAGIC)
400 return fdt_blob;
401
402 debug("DTB is also not passed via %p\n", fdt_blob);
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100403
Michal Simek1a505292022-02-17 14:28:38 +0100404 *err = -EINVAL;
Michal Simek878ba362019-12-19 17:45:15 +0100405 return NULL;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100406}
407#endif
Michal Simek705d44a2020-03-31 12:39:37 +0200408
Michal Simek67ed85d2020-10-22 11:14:20 +0200409#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek0bb24e12020-08-03 12:57:05 +0200410static int env_set_by_index(const char *name, int index, char *data)
411{
412 char var[32];
413
414 if (!index)
415 sprintf(var, "board_%s", name);
416 else
417 sprintf(var, "card%d_%s", index, name);
418
419 return env_set(var, data);
420}
421
Michal Simek705d44a2020-03-31 12:39:37 +0200422int board_late_init_xilinx(void)
423{
Michal Simekbd07b5d2020-08-12 12:16:49 +0200424 u32 ret = 0;
Michal Simek0bb24e12020-08-03 12:57:05 +0200425 int i, id, macid = 0;
426 struct xilinx_board_description *desc;
Ricardo Salvetid35ccf22022-01-20 16:17:30 -0300427 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
Michal Simekbb70b772023-08-31 09:04:28 +0200428 u64 bootscr_flash_offset, bootscr_flash_size;
Padmarao Begaric1399622024-09-13 15:02:31 +0530429 ulong scriptaddr;
430 u64 bootscr_address;
431 u64 bootscr_offset;
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600432
Padmarao Begaric1399622024-09-13 15:02:31 +0530433 /* Fetch bootscr_address/bootscr_offset from DT and update */
434 if (!ofnode_read_bootscript_address(&bootscr_address,
435 &bootscr_offset)) {
436 if (bootscr_offset)
437 ret |= env_set_hex("scriptaddr",
438 gd->ram_base +
439 bootscr_offset);
440 else
Algapally Santosh Sagar473705e2023-08-31 08:59:06 +0200441 ret |= env_set_hex("scriptaddr",
Padmarao Begaric1399622024-09-13 15:02:31 +0530442 bootscr_address);
443 } else {
444 /* Update scriptaddr(bootscr offset) from env */
445 scriptaddr = env_get_hex("scriptaddr", 0);
446 ret |= env_set_hex("scriptaddr",
447 gd->ram_base + scriptaddr);
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600448 }
Michal Simekc86ce0c2020-08-12 12:17:53 +0200449
Michal Simekbb70b772023-08-31 09:04:28 +0200450 if (!ofnode_read_bootscript_flash(&bootscr_flash_offset,
451 &bootscr_flash_size)) {
452 ret |= env_set_hex("script_offset_f", bootscr_flash_offset);
453 ret |= env_set_hex("script_size_f", bootscr_flash_size);
454 } else {
455 debug("!!! Please define bootscr-flash-offset via DT !!!\n");
456 ret |= env_set_hex("script_offset_f",
457 CONFIG_BOOT_SCRIPT_OFFSET);
458 }
459
Simon Glass117038a2023-02-05 15:40:14 -0700460 if (IS_ENABLED(CONFIG_ARCH_ZYNQ) || IS_ENABLED(CONFIG_MICROBLAZE))
Michal Simekc86ce0c2020-08-12 12:17:53 +0200461 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simekbd07b5d2020-08-12 12:16:49 +0200462
Michal Simekbd07b5d2020-08-12 12:16:49 +0200463 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simekc86ce0c2020-08-12 12:17:53 +0200464 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simek705d44a2020-03-31 12:39:37 +0200465
Michal Simek0bb24e12020-08-03 12:57:05 +0200466 for (id = 0; id <= highest_id; id++) {
Michal Simeke98ae1d2021-08-12 12:30:36 +0200467 desc = &board_info[id];
Michal Simek0bb24e12020-08-03 12:57:05 +0200468 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
469 if (desc->manufacturer[0])
470 ret |= env_set_by_index("manufacturer", id,
471 desc->manufacturer);
472 if (desc->name[0])
473 ret |= env_set_by_index("name", id,
474 desc->name);
475 if (desc->revision[0])
476 ret |= env_set_by_index("rev", id,
477 desc->revision);
478 if (desc->serial[0])
479 ret |= env_set_by_index("serial", id,
480 desc->serial);
Michal Simek394ee242020-08-03 13:01:45 +0200481
Michal Simek7459d4a2022-07-21 16:19:17 +0200482 if (desc->uuid[0]) {
Venkatesh Yadav Abbarapu3be370d2022-09-26 12:22:42 +0530483 unsigned char uuid[UUID_STR_LEN + 1];
484 unsigned char *t = desc->uuid;
Michal Simek7459d4a2022-07-21 16:19:17 +0200485
486 memset(uuid, 0, UUID_STR_LEN + 1);
487
488 sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
489 t[0], t[1], t[2], t[3], t[4], t[5],
490 t[6], t[7], t[8], t[9], t[10], t[11],
491 t[12], t[13], t[14], t[15]);
492 ret |= env_set_by_index("uuid", id, uuid);
493 }
494
Jerome Forissier1ff00362024-10-16 12:04:03 +0200495 if (!(CONFIG_IS_ENABLED(NET) ||
496 CONFIG_IS_ENABLED(NET_LWIP)))
Michal Simek394ee242020-08-03 13:01:45 +0200497 continue;
498
Michal Simek0bb24e12020-08-03 12:57:05 +0200499 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
Michal Simek0bb24e12020-08-03 12:57:05 +0200500 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
501 ret |= eth_env_set_enetaddr_by_index("eth",
502 macid++, desc->mac_addr[i]);
503 }
Michal Simek394ee242020-08-03 13:01:45 +0200504 }
505 }
506
Michal Simekbd07b5d2020-08-12 12:16:49 +0200507 if (ret)
508 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simek8ba62d22020-07-09 15:57:56 +0200509
Michal Simek705d44a2020-03-31 12:39:37 +0200510 return 0;
511}
Michal Simek67ed85d2020-10-22 11:14:20 +0200512#endif
Michal Simekfe49df92020-10-26 12:26:13 +0100513
Michal Simekc88975e2021-07-23 09:55:59 +0200514static char *board_name = DEVICE_TREE;
515
Michal Simekfe49df92020-10-26 12:26:13 +0100516int __maybe_unused board_fit_config_name_match(const char *name)
517{
Michal Simekc88975e2021-07-23 09:55:59 +0200518 debug("%s: Check %s, default %s\n", __func__, name, board_name);
Michal Simekfe49df92020-10-26 12:26:13 +0100519
Simon Glass49c24a82024-09-29 19:49:47 -0600520#if !defined(CONFIG_XPL_BUILD)
Simon Glassf34ccae2023-02-05 15:40:37 -0700521 if (IS_ENABLED(CONFIG_REGEX)) {
Michal Simek53f44362023-01-06 09:38:52 +0100522 struct slre slre;
523 int ret;
524
525 ret = slre_compile(&slre, name);
526 if (ret) {
527 ret = slre_match(&slre, board_name, strlen(board_name),
528 NULL);
529 debug("%s: name match ret = %d\n", __func__, ret);
530 return !ret;
531 }
532 }
533#endif
534
Michal Simekc88975e2021-07-23 09:55:59 +0200535 if (!strcmp(name, board_name))
Michal Simekfe49df92020-10-26 12:26:13 +0100536 return 0;
537
538 return -1;
539}
T Karthik Reddyc8fa40a2021-08-10 06:50:20 -0600540
Simon Glassd2c5a3d2023-02-05 15:39:38 -0700541#if IS_ENABLED(CONFIG_DTB_RESELECT)
Michal Simeke0453512021-08-11 14:23:54 +0200542#define MAX_NAME_LENGTH 50
543
Michal Simekafb28572021-07-23 09:59:59 +0200544char * __maybe_unused __weak board_name_decode(void)
545{
Michal Simeke0453512021-08-11 14:23:54 +0200546 char *board_local_name;
547 struct xilinx_board_description *desc;
548 int i, id;
549
550 board_local_name = calloc(1, MAX_NAME_LENGTH);
551 if (!board_info)
552 return NULL;
553
554 for (id = 0; id <= highest_id; id++) {
555 desc = &board_info[id];
556
557 /* No board description */
558 if (!desc)
559 goto error;
560
561 /* Board is not detected */
562 if (desc->header != EEPROM_HEADER_MAGIC)
563 continue;
564
565 /* The first string should be soc name */
566 if (!id)
567 strcat(board_local_name, CONFIG_SYS_BOARD);
568
569 /*
570 * For two purpose here:
571 * soc_name- eg: zynqmp-
572 * and between base board and CC eg: ..revA-sck...
573 */
574 strcat(board_local_name, "-");
575
576 if (desc->name[0]) {
577 /* For DT composition name needs to be lowercase */
578 for (i = 0; i < sizeof(desc->name); i++)
579 desc->name[i] = tolower(desc->name[i]);
580
581 strcat(board_local_name, desc->name);
582 }
583 if (desc->revision[0]) {
584 strcat(board_local_name, "-rev");
585
586 /* And revision needs to be uppercase */
587 for (i = 0; i < sizeof(desc->revision); i++)
588 desc->revision[i] = toupper(desc->revision[i]);
589
590 strcat(board_local_name, desc->revision);
591 }
592 }
593
594 /*
595 * Longer strings will end up with buffer overflow and potential
596 * attacks that's why check it
597 */
598 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
599 panic("Board name can't be determined\n");
600
601 if (strlen(board_local_name))
602 return board_local_name;
603
604error:
605 free(board_local_name);
Michal Simekafb28572021-07-23 09:59:59 +0200606 return NULL;
607}
608
609bool __maybe_unused __weak board_detection(void)
610{
Michal Simeke0453512021-08-11 14:23:54 +0200611 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
612 int ret;
613
614 ret = xilinx_read_eeprom();
615 return !ret ? true : false;
616 }
617
Michal Simekafb28572021-07-23 09:59:59 +0200618 return false;
619}
620
Michal Simek4c8e4ee2022-09-06 12:40:41 +0200621bool __maybe_unused __weak soc_detection(void)
622{
623 return false;
624}
625
626char * __maybe_unused __weak soc_name_decode(void)
627{
628 return NULL;
629}
630
Michal Simekafb28572021-07-23 09:59:59 +0200631int embedded_dtb_select(void)
632{
Michal Simek4c8e4ee2022-09-06 12:40:41 +0200633 if (soc_detection()) {
634 char *soc_local_name;
635
636 soc_local_name = soc_name_decode();
637 if (soc_local_name) {
638 board_name = soc_local_name;
639 printf("Detected SOC name: %s\n", board_name);
640
641 /* Time to change DTB on fly */
642 /* Both ways should work here */
643 /* fdtdec_resetup(&rescan); */
644 return fdtdec_setup();
645 }
646 }
647
Michal Simekafb28572021-07-23 09:59:59 +0200648 if (board_detection()) {
649 char *board_local_name;
650
651 board_local_name = board_name_decode();
652 if (board_local_name) {
653 board_name = board_local_name;
Michal Simeke0453512021-08-11 14:23:54 +0200654 printf("Detected name: %s\n", board_name);
Michal Simekafb28572021-07-23 09:59:59 +0200655
656 /* Time to change DTB on fly */
657 /* Both ways should work here */
658 /* fdtdec_resetup(&rescan); */
659 fdtdec_setup();
660 }
661 }
662 return 0;
663}
664#endif
Michal Simekaa975612022-08-25 14:23:10 +0200665
Venkatesh Yadav Abbarapue8e3c902024-01-17 08:50:13 +0530666#ifdef CONFIG_OF_BOARD_SETUP
667#define MAX_RAND_SIZE 8
668int ft_board_setup(void *blob, struct bd_info *bd)
669{
James Hilliard2c234642024-03-31 17:28:59 -0600670 static const struct node_info nodes[] = {
671 { "arm,pl353-nand-r2p1", MTD_DEV_TYPE_NAND, },
672 };
673
674 if (IS_ENABLED(CONFIG_FDT_FIXUP_PARTITIONS) && IS_ENABLED(CONFIG_NAND_ZYNQ))
675 fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
676
Venkatesh Yadav Abbarapue8e3c902024-01-17 08:50:13 +0530677 return 0;
678}
679#endif
Sughosh Ganud8abe432024-10-25 22:57:24 +0530680
Michal Simek730fcb52024-11-15 15:31:02 +0100681#ifndef CONFIG_XILINX_MINI
682
Sughosh Ganud8abe432024-10-25 22:57:24 +0530683#ifndef MMU_SECTION_SIZE
684#define MMU_SECTION_SIZE (1 * 1024 * 1024)
685#endif
686
687phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
688{
689 phys_size_t size;
690 phys_addr_t reg;
691
692 if (!total_size)
693 return gd->ram_top;
694
695 if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
696 panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
697
698 size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
699 reg = get_mem_top(gd->ram_base, gd->ram_size, size,
700 (void *)gd->fdt_blob);
701 if (!reg)
702 reg = gd->ram_top - size;
703
704 return reg + size;
705}
Michal Simek730fcb52024-11-15 15:31:02 +0100706
707#endif