blob: 59d87f2352021d053722051286f021d94e45dce9 [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 Simekd61728c2020-08-03 13:01:45 +020057#define EEPROM_HEADER_MAGIC 0xdaaddeed
58#define EEPROM_HDR_MANUFACTURER_LEN 16
59#define EEPROM_HDR_NAME_LEN 16
60#define EEPROM_HDR_REV_LEN 8
61#define EEPROM_HDR_SERIAL_LEN 20
62#define EEPROM_HDR_NO_OF_MAC_ADDR 4
63#define EEPROM_HDR_ETH_ALEN ETH_ALEN
Michal Simekee5a4b82022-07-21 16:19:17 +020064#define EEPROM_HDR_UUID_LEN 16
Michal Simekd61728c2020-08-03 13:01:45 +020065
66struct xilinx_board_description {
67 u32 header;
68 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
69 char name[EEPROM_HDR_NAME_LEN + 1];
70 char revision[EEPROM_HDR_REV_LEN + 1];
71 char serial[EEPROM_HDR_SERIAL_LEN + 1];
72 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
Michal Simekee5a4b82022-07-21 16:19:17 +020073 char uuid[EEPROM_HDR_UUID_LEN + 1];
Michal Simekd61728c2020-08-03 13:01:45 +020074};
75
Michal Simeka03b5942020-08-03 12:57:05 +020076static int highest_id = -1;
Michal Simekd9c93c92021-08-12 12:30:36 +020077static struct xilinx_board_description *board_info;
Michal Simekd61728c2020-08-03 13:01:45 +020078
Michal Simekf149b392020-08-03 16:14:23 +020079#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simekd61728c2020-08-03 13:01:45 +020080
81/* Variable which stores pointer to array which stores eeprom content */
82struct xilinx_legacy_format {
83 char board_sn[18]; /* 0x0 */
84 char unused0[14]; /* 0x12 */
85 char eth_mac[6]; /* 0x20 */
86 char unused1[170]; /* 0x26 */
87 char board_name[11]; /* 0xd0 */
88 char unused2[5]; /* 0xdc */
89 char board_revision[3]; /* 0xe0 */
90 char unused3[29]; /* 0xe3 */
91};
92
93static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
94{
95 int i;
Venkatesh Yadav Abbarapue1a193b2022-09-26 12:22:42 +053096 unsigned char byte;
Michal Simekd61728c2020-08-03 13:01:45 +020097
98 for (i = 0; i < size; i++) {
99 byte = eeprom[i];
100
101 /* Remove all ffs and spaces */
102 if (byte == 0xff || byte == ' ')
103 eeprom[i] = 0;
104
105 /* Convert strings to lower case */
106 if (byte >= 'A' && byte <= 'Z')
107 eeprom[i] = byte + 'a' - 'A';
108 }
109}
110
111static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
112 struct xilinx_board_description *desc)
113{
114 int ret, size;
115 struct xilinx_legacy_format *eeprom_content;
116 bool eth_valid = false;
117
118 size = sizeof(*eeprom_content);
119
120 eeprom_content = calloc(1, size);
121 if (!eeprom_content)
122 return -ENOMEM;
123
124 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
125 eeprom_content);
126
127 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
128 if (ret) {
129 debug("%s: I2C EEPROM read failed\n", __func__);
130 free(eeprom_content);
131 return ret;
132 }
133
134 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
135
136 printf("Xilinx I2C Legacy format at %s:\n", name);
137 printf(" Board name:\t%s\n", eeprom_content->board_name);
138 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
139 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
140
141 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
142 if (eth_valid)
143 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
144
145 /* Terminating \0 chars ensure end of string */
146 strcpy(desc->name, eeprom_content->board_name);
147 strcpy(desc->revision, eeprom_content->board_revision);
148 strcpy(desc->serial, eeprom_content->board_sn);
149 if (eth_valid)
150 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
151
152 desc->header = EEPROM_HEADER_MAGIC;
153
154 free(eeprom_content);
155
156 return ret;
157}
158
159static bool xilinx_detect_legacy(u8 *buffer)
160{
161 int i;
162 char c;
163
164 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
165 c = buffer[i];
166
167 if (c < '0' || c > '9')
168 return false;
169 }
170
171 return true;
172}
173
Michal Simekf149b392020-08-03 16:14:23 +0200174static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
175 struct xilinx_board_description *desc)
176{
Michal Simek530560b2021-08-12 11:03:49 +0200177 int i, ret, eeprom_size;
Michal Simekf149b392020-08-03 16:14:23 +0200178 u8 *fru_content;
Ashok Reddy Soma7a036b62022-02-23 15:00:59 +0100179 u8 id = 0;
Michal Simekf149b392020-08-03 16:14:23 +0200180
181 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
182 eeprom_size = i2c_eeprom_size(dev);
183
184 fru_content = calloc(1, eeprom_size);
185 if (!fru_content)
186 return -ENOMEM;
187
188 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
189 fru_content);
190
191 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
192 eeprom_size);
193 if (ret) {
194 debug("%s: I2C EEPROM read failed\n", __func__);
Michal Simekb2628632021-08-13 09:17:10 +0200195 goto end;
Michal Simekf149b392020-08-03 16:14:23 +0200196 }
197
Michal Simekf149b392020-08-03 16:14:23 +0200198 fru_capture((unsigned long)fru_content);
Michal Simek52ff1622021-08-11 14:23:54 +0200199 if (gd->flags & GD_FLG_RELOC || (_DEBUG && CONFIG_IS_ENABLED(DTB_RESELECT))) {
200 printf("Xilinx I2C FRU format at %s:\n", name);
201 ret = fru_display(0);
202 if (ret) {
203 printf("FRU format decoding failed.\n");
204 goto end;
205 }
Michal Simekf149b392020-08-03 16:14:23 +0200206 }
207
208 if (desc->header == EEPROM_HEADER_MAGIC) {
209 debug("Information already filled\n");
Michal Simekb2628632021-08-13 09:17:10 +0200210 ret = -EINVAL;
211 goto end;
Michal Simekf149b392020-08-03 16:14:23 +0200212 }
213
214 /* It is clear that FRU was captured and structures were filled */
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200215 strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
Michal Simekf149b392020-08-03 16:14:23 +0200216 sizeof(desc->manufacturer));
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200217 strlcpy(desc->uuid, (char *)fru_data.brd.uuid,
Michal Simekee5a4b82022-07-21 16:19:17 +0200218 sizeof(desc->uuid));
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200219 strlcpy(desc->name, (char *)fru_data.brd.product_name,
Michal Simekf149b392020-08-03 16:14:23 +0200220 sizeof(desc->name));
Michal Simek530560b2021-08-12 11:03:49 +0200221 for (i = 0; i < sizeof(desc->name); i++) {
222 if (desc->name[i] == ' ')
223 desc->name[i] = '\0';
224 }
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200225 strlcpy(desc->revision, (char *)fru_data.brd.rev,
Michal Simekf149b392020-08-03 16:14:23 +0200226 sizeof(desc->revision));
Michal Simek93216272022-06-06 09:31:27 +0200227 for (i = 0; i < sizeof(desc->revision); i++) {
228 if (desc->revision[i] == ' ')
229 desc->revision[i] = '\0';
230 }
Michal Simek4a1bfcd2022-07-21 16:19:18 +0200231 strlcpy(desc->serial, (char *)fru_data.brd.serial_number,
Michal Simekf149b392020-08-03 16:14:23 +0200232 sizeof(desc->serial));
Ashok Reddy Soma7a036b62022-02-23 15:00:59 +0100233
234 while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
235 if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
236 memcpy(&desc->mac_addr[id],
237 (char *)fru_data.mac.macid[id], ETH_ALEN);
238 id++;
239 }
240
Michal Simekf149b392020-08-03 16:14:23 +0200241 desc->header = EEPROM_HEADER_MAGIC;
242
Michal Simekb2628632021-08-13 09:17:10 +0200243end:
244 free(fru_content);
245 return ret;
Michal Simekf149b392020-08-03 16:14:23 +0200246}
247
248static bool xilinx_detect_fru(u8 *buffer)
249{
250 u8 checksum = 0;
251 int i;
252
253 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
254 if (checksum) {
255 debug("%s Common header CRC FAIL\n", __func__);
256 return false;
257 }
258
259 bool all_zeros = true;
260 /* Checksum over all zeros is also zero that's why detect this case */
261 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
262 if (buffer[i] != 0)
263 all_zeros = false;
264 }
265
266 if (all_zeros)
267 return false;
268
269 debug("%s Common header CRC PASS\n", __func__);
270 return true;
271}
272
Michal Simekd61728c2020-08-03 13:01:45 +0200273static int xilinx_read_eeprom_single(char *name,
274 struct xilinx_board_description *desc)
275{
276 int ret;
277 struct udevice *dev;
278 ofnode eeprom;
279 u8 buffer[XILINX_I2C_DETECTION_BITS];
280
281 eeprom = ofnode_get_aliases_node(name);
282 if (!ofnode_valid(eeprom))
283 return -ENODEV;
284
285 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
286 if (ret)
287 return ret;
288
289 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
290 if (ret) {
291 debug("%s: I2C EEPROM read failed\n", __func__);
292 return ret;
293 }
294
295 debug("%s: i2c memory detected: %s\n", __func__, name);
296
Michal Simekf149b392020-08-03 16:14:23 +0200297 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
298 return xilinx_read_eeprom_fru(dev, name, desc);
299
Michal Simekd61728c2020-08-03 13:01:45 +0200300 if (xilinx_detect_legacy(buffer))
301 return xilinx_read_eeprom_legacy(dev, name, desc);
302
303 return -ENODEV;
304}
305
306__maybe_unused int xilinx_read_eeprom(void)
307{
Michal Simekd9c93c92021-08-12 12:30:36 +0200308 int id;
Michal Simekd61728c2020-08-03 13:01:45 +0200309 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simeka03b5942020-08-03 12:57:05 +0200310 struct xilinx_board_description *desc;
Michal Simekd61728c2020-08-03 13:01:45 +0200311
312 highest_id = dev_read_alias_highest_id("nvmem");
313 /* No nvmem aliases present */
314 if (highest_id < 0)
315 return -EINVAL;
316
Michal Simekd9c93c92021-08-12 12:30:36 +0200317 board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
Michal Simekd61728c2020-08-03 13:01:45 +0200318 if (!board_info)
319 return -ENOMEM;
320
321 debug("%s: Highest ID %d, board_info %p\n", __func__,
322 highest_id, board_info);
323
324 for (id = 0; id <= highest_id; id++) {
325 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
326
Michal Simeka03b5942020-08-03 12:57:05 +0200327 /* Alloc structure */
Michal Simekd9c93c92021-08-12 12:30:36 +0200328 desc = &board_info[id];
Michal Simeka03b5942020-08-03 12:57:05 +0200329
Michal Simekd61728c2020-08-03 13:01:45 +0200330 /* Ignoring return value for supporting multiple chips */
Michal Simekd9c93c92021-08-12 12:30:36 +0200331 xilinx_read_eeprom_single(name_buf, desc);
Michal Simekd61728c2020-08-03 13:01:45 +0200332 }
333
Michal Simeka03b5942020-08-03 12:57:05 +0200334 /*
335 * Consider to clean board_info structure when board/cards are not
336 * detected.
337 */
Michal Simekd61728c2020-08-03 13:01:45 +0200338
339 return 0;
340}
341
Michal Simekf0631002022-02-17 14:28:38 +0100342#if defined(CONFIG_OF_BOARD)
Ilias Apalodimase7fb7892021-10-26 09:12:33 +0300343void *board_fdt_blob_setup(int *err)
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100344{
Michal Simeke2572b52020-09-04 16:21:47 +0200345 void *fdt_blob;
Michal Simek453bb772020-03-19 10:23:56 +0100346
Ilias Apalodimase7fb7892021-10-26 09:12:33 +0300347 *err = 0;
Michal Simeka672b982021-01-04 11:07:28 +0100348 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
349 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simekfc3c6fd2021-02-02 13:33:22 +0100350 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek506009f2021-01-04 11:03:36 +0100351 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100352
Michal Simek506009f2021-01-04 11:03:36 +0100353 if (fdt_magic(fdt_blob) == FDT_MAGIC)
354 return fdt_blob;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100355
Michal Simek506009f2021-01-04 11:03:36 +0100356 debug("DTB is not passed via %p\n", fdt_blob);
357 }
Michal Simekfc274a52019-12-19 17:45:15 +0100358
Michal Simek506009f2021-01-04 11:03:36 +0100359 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
360 /*
361 * FDT is at end of BSS unless it is in a different memory
362 * region
363 */
364 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
365 fdt_blob = (ulong *)&_image_binary_end;
366 else
367 fdt_blob = (ulong *)&__bss_end;
368 } else {
369 /* FDT is at end of image */
370 fdt_blob = (ulong *)&_end;
371 }
Michal Simekfc274a52019-12-19 17:45:15 +0100372
373 if (fdt_magic(fdt_blob) == FDT_MAGIC)
374 return fdt_blob;
375
376 debug("DTB is also not passed via %p\n", fdt_blob);
377
Michal Simekf0631002022-02-17 14:28:38 +0100378 *err = -EINVAL;
Michal Simekfc274a52019-12-19 17:45:15 +0100379 return NULL;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100380}
381#endif
Michal Simek80fdef12020-03-31 12:39:37 +0200382
Michal Simek7c553ac2020-10-22 11:14:20 +0200383#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simeka03b5942020-08-03 12:57:05 +0200384static int env_set_by_index(const char *name, int index, char *data)
385{
386 char var[32];
387
388 if (!index)
389 sprintf(var, "board_%s", name);
390 else
391 sprintf(var, "card%d_%s", index, name);
392
393 return env_set(var, data);
394}
395
Michal Simek80fdef12020-03-31 12:39:37 +0200396int board_late_init_xilinx(void)
397{
Michal Simekca0f6162020-08-12 12:16:49 +0200398 u32 ret = 0;
Michal Simeka03b5942020-08-03 12:57:05 +0200399 int i, id, macid = 0;
400 struct xilinx_board_description *desc;
Ricardo Salvetie6e3b9d2022-01-20 16:17:30 -0300401 phys_size_t bootm_size = gd->ram_top - gd->ram_base;
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600402
T Karthik Reddy25484d92021-04-02 01:49:17 -0600403 if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600404 ulong scriptaddr;
405
406 scriptaddr = env_get_hex("scriptaddr", 0);
T Karthik Reddy25484d92021-04-02 01:49:17 -0600407 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600408 }
Michal Simek2570cc62020-08-12 12:17:53 +0200409
Michal Simek12305822020-10-23 07:54:18 +0200410 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simek2570cc62020-08-12 12:17:53 +0200411 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simek80fdef12020-03-31 12:39:37 +0200412
Michal Simekca0f6162020-08-12 12:16:49 +0200413 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
414
415 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simek2570cc62020-08-12 12:17:53 +0200416 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simekca0f6162020-08-12 12:16:49 +0200417
Michal Simeka03b5942020-08-03 12:57:05 +0200418 for (id = 0; id <= highest_id; id++) {
Michal Simekd9c93c92021-08-12 12:30:36 +0200419 desc = &board_info[id];
Michal Simeka03b5942020-08-03 12:57:05 +0200420 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
421 if (desc->manufacturer[0])
422 ret |= env_set_by_index("manufacturer", id,
423 desc->manufacturer);
424 if (desc->name[0])
425 ret |= env_set_by_index("name", id,
426 desc->name);
427 if (desc->revision[0])
428 ret |= env_set_by_index("rev", id,
429 desc->revision);
430 if (desc->serial[0])
431 ret |= env_set_by_index("serial", id,
432 desc->serial);
Michal Simekd61728c2020-08-03 13:01:45 +0200433
Michal Simekee5a4b82022-07-21 16:19:17 +0200434 if (desc->uuid[0]) {
Venkatesh Yadav Abbarapue1a193b2022-09-26 12:22:42 +0530435 unsigned char uuid[UUID_STR_LEN + 1];
436 unsigned char *t = desc->uuid;
Michal Simekee5a4b82022-07-21 16:19:17 +0200437
438 memset(uuid, 0, UUID_STR_LEN + 1);
439
440 sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
441 t[0], t[1], t[2], t[3], t[4], t[5],
442 t[6], t[7], t[8], t[9], t[10], t[11],
443 t[12], t[13], t[14], t[15]);
444 ret |= env_set_by_index("uuid", id, uuid);
445 }
446
Michal Simekd61728c2020-08-03 13:01:45 +0200447 if (!CONFIG_IS_ENABLED(NET))
448 continue;
449
Michal Simeka03b5942020-08-03 12:57:05 +0200450 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
Michal Simeka03b5942020-08-03 12:57:05 +0200451 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
452 ret |= eth_env_set_enetaddr_by_index("eth",
453 macid++, desc->mac_addr[i]);
454 }
Michal Simekd61728c2020-08-03 13:01:45 +0200455 }
456 }
457
Michal Simekca0f6162020-08-12 12:16:49 +0200458 if (ret)
459 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simekc8da6512020-07-09 15:57:56 +0200460
Michal Simek80fdef12020-03-31 12:39:37 +0200461 return 0;
462}
Michal Simek7c553ac2020-10-22 11:14:20 +0200463#endif
Michal Simek05af4832020-10-26 12:26:13 +0100464
Michal Simek57f71032021-07-23 09:55:59 +0200465static char *board_name = DEVICE_TREE;
466
Michal Simek05af4832020-10-26 12:26:13 +0100467int __maybe_unused board_fit_config_name_match(const char *name)
468{
Michal Simek57f71032021-07-23 09:55:59 +0200469 debug("%s: Check %s, default %s\n", __func__, name, board_name);
Michal Simek05af4832020-10-26 12:26:13 +0100470
Michal Simek57f71032021-07-23 09:55:59 +0200471 if (!strcmp(name, board_name))
Michal Simek05af4832020-10-26 12:26:13 +0100472 return 0;
473
474 return -1;
475}
T Karthik Reddy80c0d382021-08-10 06:50:20 -0600476
Michal Simek88232532021-07-23 09:59:59 +0200477#if CONFIG_IS_ENABLED(DTB_RESELECT)
Michal Simek52ff1622021-08-11 14:23:54 +0200478#define MAX_NAME_LENGTH 50
479
Michal Simek88232532021-07-23 09:59:59 +0200480char * __maybe_unused __weak board_name_decode(void)
481{
Michal Simek52ff1622021-08-11 14:23:54 +0200482 char *board_local_name;
483 struct xilinx_board_description *desc;
484 int i, id;
485
486 board_local_name = calloc(1, MAX_NAME_LENGTH);
487 if (!board_info)
488 return NULL;
489
490 for (id = 0; id <= highest_id; id++) {
491 desc = &board_info[id];
492
493 /* No board description */
494 if (!desc)
495 goto error;
496
497 /* Board is not detected */
498 if (desc->header != EEPROM_HEADER_MAGIC)
499 continue;
500
501 /* The first string should be soc name */
502 if (!id)
503 strcat(board_local_name, CONFIG_SYS_BOARD);
504
505 /*
506 * For two purpose here:
507 * soc_name- eg: zynqmp-
508 * and between base board and CC eg: ..revA-sck...
509 */
510 strcat(board_local_name, "-");
511
512 if (desc->name[0]) {
513 /* For DT composition name needs to be lowercase */
514 for (i = 0; i < sizeof(desc->name); i++)
515 desc->name[i] = tolower(desc->name[i]);
516
517 strcat(board_local_name, desc->name);
518 }
519 if (desc->revision[0]) {
520 strcat(board_local_name, "-rev");
521
522 /* And revision needs to be uppercase */
523 for (i = 0; i < sizeof(desc->revision); i++)
524 desc->revision[i] = toupper(desc->revision[i]);
525
526 strcat(board_local_name, desc->revision);
527 }
528 }
529
530 /*
531 * Longer strings will end up with buffer overflow and potential
532 * attacks that's why check it
533 */
534 if (strlen(board_local_name) >= MAX_NAME_LENGTH)
535 panic("Board name can't be determined\n");
536
537 if (strlen(board_local_name))
538 return board_local_name;
539
540error:
541 free(board_local_name);
Michal Simek88232532021-07-23 09:59:59 +0200542 return NULL;
543}
544
545bool __maybe_unused __weak board_detection(void)
546{
Michal Simek52ff1622021-08-11 14:23:54 +0200547 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
548 int ret;
549
550 ret = xilinx_read_eeprom();
551 return !ret ? true : false;
552 }
553
Michal Simek88232532021-07-23 09:59:59 +0200554 return false;
555}
556
Michal Simek39d3c3c2022-09-06 12:40:41 +0200557bool __maybe_unused __weak soc_detection(void)
558{
559 return false;
560}
561
562char * __maybe_unused __weak soc_name_decode(void)
563{
564 return NULL;
565}
566
Michal Simek88232532021-07-23 09:59:59 +0200567int embedded_dtb_select(void)
568{
Michal Simek39d3c3c2022-09-06 12:40:41 +0200569 if (soc_detection()) {
570 char *soc_local_name;
571
572 soc_local_name = soc_name_decode();
573 if (soc_local_name) {
574 board_name = soc_local_name;
575 printf("Detected SOC name: %s\n", board_name);
576
577 /* Time to change DTB on fly */
578 /* Both ways should work here */
579 /* fdtdec_resetup(&rescan); */
580 return fdtdec_setup();
581 }
582 }
583
Michal Simek88232532021-07-23 09:59:59 +0200584 if (board_detection()) {
585 char *board_local_name;
586
587 board_local_name = board_name_decode();
588 if (board_local_name) {
589 board_name = board_local_name;
Michal Simek52ff1622021-08-11 14:23:54 +0200590 printf("Detected name: %s\n", board_name);
Michal Simek88232532021-07-23 09:59:59 +0200591
592 /* Time to change DTB on fly */
593 /* Both ways should work here */
594 /* fdtdec_resetup(&rescan); */
595 fdtdec_setup();
596 }
597 }
598 return 0;
599}
600#endif
Michal Simeka32c3e92022-08-25 14:23:10 +0200601
602#if defined(CONFIG_LMB)
Pali Rohár049704f2022-09-09 17:32:40 +0200603phys_size_t board_get_usable_ram_top(phys_size_t total_size)
Michal Simeka32c3e92022-08-25 14:23:10 +0200604{
605 phys_size_t size;
606 phys_addr_t reg;
607 struct lmb lmb;
608
609 if (!total_size)
610 return gd->ram_top;
611
612 if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
613 panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
614
615 /* found enough not-reserved memory to relocated U-Boot */
616 lmb_init(&lmb);
617 lmb_add(&lmb, gd->ram_base, gd->ram_size);
618 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
619 size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
620 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
621
622 if (!reg)
623 reg = gd->ram_top - size;
624
625 return reg + size;
626}
627#endif