blob: 90c87bab5cfff5ca17569eb4531e936e752e8e92 [file] [log] [blame]
Michal Simek014d3042019-01-21 15:25:02 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Michal Simek394ee242020-08-03 13:01:45 +02003 * (C) Copyright 2014 - 2020 Xilinx, Inc.
Michal Simek014d3042019-01-21 15:25:02 +01004 * Michal Simek <michal.simek@xilinx.com>
5 */
6
7#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Michal Simek878ba362019-12-19 17:45:15 +010011#include <asm/sections.h>
Michal Simek014d3042019-01-21 15:25:02 +010012#include <dm/uclass.h>
13#include <i2c.h>
Michal Simekb90b97e2020-04-08 10:51:36 +020014#include <linux/sizes.h>
Michal Simek394ee242020-08-03 13:01:45 +020015#include <malloc.h>
Michal Simek705d44a2020-03-31 12:39:37 +020016#include "board.h"
Michal Simek394ee242020-08-03 13:01:45 +020017#include <dm.h>
18#include <i2c_eeprom.h>
19#include <net.h>
Michal Simekfe49df92020-10-26 12:26:13 +010020#include <generated/dt.h>
T Karthik Reddyc8fa40a2021-08-10 06:50:20 -060021#include <soc.h>
Michal Simek014d3042019-01-21 15:25:02 +010022
Michal Simek207e0622020-08-03 16:14:23 +020023#include "fru.h"
24
Michal Simekaee22b32020-08-03 12:59:28 +020025#if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
Michal Simek014d3042019-01-21 15:25:02 +010026int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
27{
Michal Simek13db6162019-01-21 16:29:07 +010028 int ret = -EINVAL;
Michal Simek13db6162019-01-21 16:29:07 +010029 struct udevice *dev;
30 ofnode eeprom;
31
32 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
33 if (!ofnode_valid(eeprom))
34 return -ENODEV;
35
36 debug("%s: Path to EEPROM %s\n", __func__,
Simon Glassf3455962020-01-27 08:49:43 -070037 ofnode_read_chosen_string("xlnx,eeprom"));
Michal Simek13db6162019-01-21 16:29:07 +010038
39 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
40 if (ret)
41 return ret;
42
43 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
44 if (ret)
45 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
46 else
47 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
Michal Simek13db6162019-01-21 16:29:07 +010048
49 return ret;
50}
Michal Simekaee22b32020-08-03 12:59:28 +020051#endif
Ibai Erkiaga6f658202019-10-02 15:57:36 +010052
Michal Simek394ee242020-08-03 13:01:45 +020053#define EEPROM_HEADER_MAGIC 0xdaaddeed
54#define EEPROM_HDR_MANUFACTURER_LEN 16
55#define EEPROM_HDR_NAME_LEN 16
56#define EEPROM_HDR_REV_LEN 8
57#define EEPROM_HDR_SERIAL_LEN 20
58#define EEPROM_HDR_NO_OF_MAC_ADDR 4
59#define EEPROM_HDR_ETH_ALEN ETH_ALEN
60
61struct xilinx_board_description {
62 u32 header;
63 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
64 char name[EEPROM_HDR_NAME_LEN + 1];
65 char revision[EEPROM_HDR_REV_LEN + 1];
66 char serial[EEPROM_HDR_SERIAL_LEN + 1];
67 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
68};
69
Michal Simek0bb24e12020-08-03 12:57:05 +020070static int highest_id = -1;
71static struct xilinx_board_description **board_info;
Michal Simek394ee242020-08-03 13:01:45 +020072
Michal Simek207e0622020-08-03 16:14:23 +020073#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simek394ee242020-08-03 13:01:45 +020074
75/* Variable which stores pointer to array which stores eeprom content */
76struct xilinx_legacy_format {
77 char board_sn[18]; /* 0x0 */
78 char unused0[14]; /* 0x12 */
79 char eth_mac[6]; /* 0x20 */
80 char unused1[170]; /* 0x26 */
81 char board_name[11]; /* 0xd0 */
82 char unused2[5]; /* 0xdc */
83 char board_revision[3]; /* 0xe0 */
84 char unused3[29]; /* 0xe3 */
85};
86
87static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
88{
89 int i;
90 char byte;
91
92 for (i = 0; i < size; i++) {
93 byte = eeprom[i];
94
95 /* Remove all ffs and spaces */
96 if (byte == 0xff || byte == ' ')
97 eeprom[i] = 0;
98
99 /* Convert strings to lower case */
100 if (byte >= 'A' && byte <= 'Z')
101 eeprom[i] = byte + 'a' - 'A';
102 }
103}
104
105static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
106 struct xilinx_board_description *desc)
107{
108 int ret, size;
109 struct xilinx_legacy_format *eeprom_content;
110 bool eth_valid = false;
111
112 size = sizeof(*eeprom_content);
113
114 eeprom_content = calloc(1, size);
115 if (!eeprom_content)
116 return -ENOMEM;
117
118 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
119 eeprom_content);
120
121 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
122 if (ret) {
123 debug("%s: I2C EEPROM read failed\n", __func__);
124 free(eeprom_content);
125 return ret;
126 }
127
128 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
129
130 printf("Xilinx I2C Legacy format at %s:\n", name);
131 printf(" Board name:\t%s\n", eeprom_content->board_name);
132 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
133 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
134
135 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
136 if (eth_valid)
137 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
138
139 /* Terminating \0 chars ensure end of string */
140 strcpy(desc->name, eeprom_content->board_name);
141 strcpy(desc->revision, eeprom_content->board_revision);
142 strcpy(desc->serial, eeprom_content->board_sn);
143 if (eth_valid)
144 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
145
146 desc->header = EEPROM_HEADER_MAGIC;
147
148 free(eeprom_content);
149
150 return ret;
151}
152
153static bool xilinx_detect_legacy(u8 *buffer)
154{
155 int i;
156 char c;
157
158 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
159 c = buffer[i];
160
161 if (c < '0' || c > '9')
162 return false;
163 }
164
165 return true;
166}
167
Michal Simek207e0622020-08-03 16:14:23 +0200168static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
169 struct xilinx_board_description *desc)
170{
171 int ret, eeprom_size;
172 u8 *fru_content;
173
174 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
175 eeprom_size = i2c_eeprom_size(dev);
176
177 fru_content = calloc(1, eeprom_size);
178 if (!fru_content)
179 return -ENOMEM;
180
181 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
182 fru_content);
183
184 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
185 eeprom_size);
186 if (ret) {
187 debug("%s: I2C EEPROM read failed\n", __func__);
188 free(fru_content);
189 return ret;
190 }
191
192 printf("Xilinx I2C FRU format at %s:\n", name);
193 fru_capture((unsigned long)fru_content);
194 ret = fru_display(0);
195 if (ret) {
196 printf("FRU format decoding failed.\n");
197 return ret;
198 }
199
200 if (desc->header == EEPROM_HEADER_MAGIC) {
201 debug("Information already filled\n");
202 return -EINVAL;
203 }
204
205 /* It is clear that FRU was captured and structures were filled */
206 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
207 sizeof(desc->manufacturer));
208 strncpy(desc->name, (char *)fru_data.brd.product_name,
209 sizeof(desc->name));
210 strncpy(desc->revision, (char *)fru_data.brd.rev,
211 sizeof(desc->revision));
212 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
213 sizeof(desc->serial));
214 desc->header = EEPROM_HEADER_MAGIC;
215
216 return 0;
217}
218
219static bool xilinx_detect_fru(u8 *buffer)
220{
221 u8 checksum = 0;
222 int i;
223
224 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
225 if (checksum) {
226 debug("%s Common header CRC FAIL\n", __func__);
227 return false;
228 }
229
230 bool all_zeros = true;
231 /* Checksum over all zeros is also zero that's why detect this case */
232 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
233 if (buffer[i] != 0)
234 all_zeros = false;
235 }
236
237 if (all_zeros)
238 return false;
239
240 debug("%s Common header CRC PASS\n", __func__);
241 return true;
242}
243
Michal Simek394ee242020-08-03 13:01:45 +0200244static int xilinx_read_eeprom_single(char *name,
245 struct xilinx_board_description *desc)
246{
247 int ret;
248 struct udevice *dev;
249 ofnode eeprom;
250 u8 buffer[XILINX_I2C_DETECTION_BITS];
251
252 eeprom = ofnode_get_aliases_node(name);
253 if (!ofnode_valid(eeprom))
254 return -ENODEV;
255
256 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
257 if (ret)
258 return ret;
259
260 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
261 if (ret) {
262 debug("%s: I2C EEPROM read failed\n", __func__);
263 return ret;
264 }
265
266 debug("%s: i2c memory detected: %s\n", __func__, name);
267
Michal Simek207e0622020-08-03 16:14:23 +0200268 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
269 return xilinx_read_eeprom_fru(dev, name, desc);
270
Michal Simek394ee242020-08-03 13:01:45 +0200271 if (xilinx_detect_legacy(buffer))
272 return xilinx_read_eeprom_legacy(dev, name, desc);
273
274 return -ENODEV;
275}
276
277__maybe_unused int xilinx_read_eeprom(void)
278{
Michal Simek0bb24e12020-08-03 12:57:05 +0200279 int id, ret;
Michal Simek394ee242020-08-03 13:01:45 +0200280 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simek0bb24e12020-08-03 12:57:05 +0200281 struct xilinx_board_description *desc;
Michal Simek394ee242020-08-03 13:01:45 +0200282
283 highest_id = dev_read_alias_highest_id("nvmem");
284 /* No nvmem aliases present */
285 if (highest_id < 0)
286 return -EINVAL;
287
Michal Simek0bb24e12020-08-03 12:57:05 +0200288 board_info = calloc(1, sizeof(desc) * highest_id);
Michal Simek394ee242020-08-03 13:01:45 +0200289 if (!board_info)
290 return -ENOMEM;
291
292 debug("%s: Highest ID %d, board_info %p\n", __func__,
293 highest_id, board_info);
294
295 for (id = 0; id <= highest_id; id++) {
296 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
297
Michal Simek0bb24e12020-08-03 12:57:05 +0200298 /* Alloc structure */
299 desc = board_info[id];
300 if (!desc) {
301 desc = calloc(1, sizeof(*desc));
302 if (!desc)
303 return -ENOMEM;
304
305 board_info[id] = desc;
306 }
307
Michal Simek394ee242020-08-03 13:01:45 +0200308 /* Ignoring return value for supporting multiple chips */
Michal Simek0bb24e12020-08-03 12:57:05 +0200309 ret = xilinx_read_eeprom_single(name_buf, desc);
310 if (ret) {
311 free(desc);
312 board_info[id] = NULL;
313 }
Michal Simek394ee242020-08-03 13:01:45 +0200314 }
315
Michal Simek0bb24e12020-08-03 12:57:05 +0200316 /*
317 * Consider to clean board_info structure when board/cards are not
318 * detected.
319 */
Michal Simek394ee242020-08-03 13:01:45 +0200320
321 return 0;
322}
323
Michal Simek878ba362019-12-19 17:45:15 +0100324#if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100325void *board_fdt_blob_setup(void)
326{
Michal Simekddf69ae2020-09-04 16:21:47 +0200327 void *fdt_blob;
Michal Simekc89f4292020-03-19 10:23:56 +0100328
Michal Simek663c1622021-01-04 11:07:28 +0100329 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
330 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simek6d4317b2021-02-02 13:33:22 +0100331 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek9b9d01e2021-01-04 11:03:36 +0100332 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100333
Michal Simek9b9d01e2021-01-04 11:03:36 +0100334 if (fdt_magic(fdt_blob) == FDT_MAGIC)
335 return fdt_blob;
Michal Simek878ba362019-12-19 17:45:15 +0100336
Michal Simek9b9d01e2021-01-04 11:03:36 +0100337 debug("DTB is not passed via %p\n", fdt_blob);
338 }
Michal Simek878ba362019-12-19 17:45:15 +0100339
Michal Simek9b9d01e2021-01-04 11:03:36 +0100340 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
341 /*
342 * FDT is at end of BSS unless it is in a different memory
343 * region
344 */
345 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
346 fdt_blob = (ulong *)&_image_binary_end;
347 else
348 fdt_blob = (ulong *)&__bss_end;
349 } else {
350 /* FDT is at end of image */
351 fdt_blob = (ulong *)&_end;
352 }
Michal Simek878ba362019-12-19 17:45:15 +0100353
354 if (fdt_magic(fdt_blob) == FDT_MAGIC)
355 return fdt_blob;
356
357 debug("DTB is also not passed via %p\n", fdt_blob);
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100358
Michal Simek878ba362019-12-19 17:45:15 +0100359 return NULL;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100360}
361#endif
Michal Simek705d44a2020-03-31 12:39:37 +0200362
Michal Simek67ed85d2020-10-22 11:14:20 +0200363#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek0bb24e12020-08-03 12:57:05 +0200364static int env_set_by_index(const char *name, int index, char *data)
365{
366 char var[32];
367
368 if (!index)
369 sprintf(var, "board_%s", name);
370 else
371 sprintf(var, "card%d_%s", index, name);
372
373 return env_set(var, data);
374}
375
Michal Simek705d44a2020-03-31 12:39:37 +0200376int board_late_init_xilinx(void)
377{
Michal Simekbd07b5d2020-08-12 12:16:49 +0200378 u32 ret = 0;
Michal Simek0bb24e12020-08-03 12:57:05 +0200379 int i, id, macid = 0;
380 struct xilinx_board_description *desc;
Michal Simekc86ce0c2020-08-12 12:17:53 +0200381 phys_size_t bootm_size = gd->ram_size;
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600382
T Karthik Reddy466cdde2021-04-02 01:49:17 -0600383 if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600384 ulong scriptaddr;
385
386 scriptaddr = env_get_hex("scriptaddr", 0);
T Karthik Reddy466cdde2021-04-02 01:49:17 -0600387 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600388 }
Michal Simekc86ce0c2020-08-12 12:17:53 +0200389
Michal Simek9ccfcae2020-10-23 07:54:18 +0200390 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simekc86ce0c2020-08-12 12:17:53 +0200391 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simekbd07b5d2020-08-12 12:16:49 +0200392
393 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
394
395 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simekc86ce0c2020-08-12 12:17:53 +0200396 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simek705d44a2020-03-31 12:39:37 +0200397
Michal Simek0bb24e12020-08-03 12:57:05 +0200398 for (id = 0; id <= highest_id; id++) {
399 desc = board_info[id];
400 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
401 if (desc->manufacturer[0])
402 ret |= env_set_by_index("manufacturer", id,
403 desc->manufacturer);
404 if (desc->name[0])
405 ret |= env_set_by_index("name", id,
406 desc->name);
407 if (desc->revision[0])
408 ret |= env_set_by_index("rev", id,
409 desc->revision);
410 if (desc->serial[0])
411 ret |= env_set_by_index("serial", id,
412 desc->serial);
Michal Simek394ee242020-08-03 13:01:45 +0200413
414 if (!CONFIG_IS_ENABLED(NET))
415 continue;
416
Michal Simek0bb24e12020-08-03 12:57:05 +0200417 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
418 if (!desc->mac_addr[i])
419 continue;
420
421 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
422 ret |= eth_env_set_enetaddr_by_index("eth",
423 macid++, desc->mac_addr[i]);
424 }
Michal Simek394ee242020-08-03 13:01:45 +0200425 }
426 }
427
Michal Simekbd07b5d2020-08-12 12:16:49 +0200428 if (ret)
429 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simek8ba62d22020-07-09 15:57:56 +0200430
Michal Simek705d44a2020-03-31 12:39:37 +0200431 return 0;
432}
Michal Simek67ed85d2020-10-22 11:14:20 +0200433#endif
Michal Simekfe49df92020-10-26 12:26:13 +0100434
435int __maybe_unused board_fit_config_name_match(const char *name)
436{
437 debug("%s: Check %s, default %s\n", __func__, name, DEVICE_TREE);
438
439 if (!strcmp(name, DEVICE_TREE))
440 return 0;
441
442 return -1;
443}
T Karthik Reddyc8fa40a2021-08-10 06:50:20 -0600444
445#if defined(CONFIG_DISPLAY_CPUINFO) && !defined(CONFIG_ARCH_ZYNQ)
446int print_cpuinfo(void)
447{
448 struct udevice *soc;
449 char name[SOC_MAX_STR_SIZE];
450 int ret;
451
452 ret = soc_get(&soc);
453 if (ret) {
454 printf("CPU: UNKNOWN\n");
455 return 0;
456 }
457
458 ret = soc_get_family(soc, name, SOC_MAX_STR_SIZE);
459 if (ret)
460 printf("CPU: %s\n", name);
461
462 ret = soc_get_revision(soc, name, SOC_MAX_STR_SIZE);
463 if (ret)
464 printf("Silicon: %s\n", name);
465
466 return 0;
467}
468#endif