blob: 92b61d83ca47984253311e8374e2bbdd7584dd31 [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>
Simon Glass09140112020-05-10 11:40:03 -06008#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Michal Simekfc274a52019-12-19 17:45:15 +010011#include <asm/sections.h>
Michal Simek9755e3d2019-01-21 15:25:02 +010012#include <dm/uclass.h>
13#include <i2c.h>
Michal Simeka29511e2020-04-08 10:51:36 +020014#include <linux/sizes.h>
Michal Simekd61728c2020-08-03 13:01:45 +020015#include <malloc.h>
Michal Simek80fdef12020-03-31 12:39:37 +020016#include "board.h"
Michal Simekd61728c2020-08-03 13:01:45 +020017#include <dm.h>
18#include <i2c_eeprom.h>
19#include <net.h>
Michal Simek05af4832020-10-26 12:26:13 +010020#include <generated/dt.h>
Michal Simek9755e3d2019-01-21 15:25:02 +010021
Michal Simekf149b392020-08-03 16:14:23 +020022#include "fru.h"
23
Michal Simek9fea3b12020-08-03 12:59:28 +020024#if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
Michal Simek829e8c72019-01-21 16:29:07 +010025int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
26{
27 int ret = -EINVAL;
Michal Simek829e8c72019-01-21 16:29:07 +010028 struct udevice *dev;
29 ofnode eeprom;
30
31 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
32 if (!ofnode_valid(eeprom))
33 return -ENODEV;
34
35 debug("%s: Path to EEPROM %s\n", __func__,
Simon Glass14ca9f72020-01-27 08:49:43 -070036 ofnode_read_chosen_string("xlnx,eeprom"));
Michal Simek829e8c72019-01-21 16:29:07 +010037
38 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
39 if (ret)
40 return ret;
41
42 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
43 if (ret)
44 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
45 else
46 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
Michal Simek829e8c72019-01-21 16:29:07 +010047
48 return ret;
49}
Michal Simek9fea3b12020-08-03 12:59:28 +020050#endif
Ibai Erkiagafec657b2019-10-02 15:57:36 +010051
Michal Simekd61728c2020-08-03 13:01:45 +020052#define EEPROM_HEADER_MAGIC 0xdaaddeed
53#define EEPROM_HDR_MANUFACTURER_LEN 16
54#define EEPROM_HDR_NAME_LEN 16
55#define EEPROM_HDR_REV_LEN 8
56#define EEPROM_HDR_SERIAL_LEN 20
57#define EEPROM_HDR_NO_OF_MAC_ADDR 4
58#define EEPROM_HDR_ETH_ALEN ETH_ALEN
59
60struct xilinx_board_description {
61 u32 header;
62 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
63 char name[EEPROM_HDR_NAME_LEN + 1];
64 char revision[EEPROM_HDR_REV_LEN + 1];
65 char serial[EEPROM_HDR_SERIAL_LEN + 1];
66 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
67};
68
Michal Simeka03b5942020-08-03 12:57:05 +020069static int highest_id = -1;
70static struct xilinx_board_description **board_info;
Michal Simekd61728c2020-08-03 13:01:45 +020071
Michal Simekf149b392020-08-03 16:14:23 +020072#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simekd61728c2020-08-03 13:01:45 +020073
74/* Variable which stores pointer to array which stores eeprom content */
75struct xilinx_legacy_format {
76 char board_sn[18]; /* 0x0 */
77 char unused0[14]; /* 0x12 */
78 char eth_mac[6]; /* 0x20 */
79 char unused1[170]; /* 0x26 */
80 char board_name[11]; /* 0xd0 */
81 char unused2[5]; /* 0xdc */
82 char board_revision[3]; /* 0xe0 */
83 char unused3[29]; /* 0xe3 */
84};
85
86static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
87{
88 int i;
89 char byte;
90
91 for (i = 0; i < size; i++) {
92 byte = eeprom[i];
93
94 /* Remove all ffs and spaces */
95 if (byte == 0xff || byte == ' ')
96 eeprom[i] = 0;
97
98 /* Convert strings to lower case */
99 if (byte >= 'A' && byte <= 'Z')
100 eeprom[i] = byte + 'a' - 'A';
101 }
102}
103
104static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
105 struct xilinx_board_description *desc)
106{
107 int ret, size;
108 struct xilinx_legacy_format *eeprom_content;
109 bool eth_valid = false;
110
111 size = sizeof(*eeprom_content);
112
113 eeprom_content = calloc(1, size);
114 if (!eeprom_content)
115 return -ENOMEM;
116
117 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
118 eeprom_content);
119
120 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
121 if (ret) {
122 debug("%s: I2C EEPROM read failed\n", __func__);
123 free(eeprom_content);
124 return ret;
125 }
126
127 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
128
129 printf("Xilinx I2C Legacy format at %s:\n", name);
130 printf(" Board name:\t%s\n", eeprom_content->board_name);
131 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
132 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
133
134 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
135 if (eth_valid)
136 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
137
138 /* Terminating \0 chars ensure end of string */
139 strcpy(desc->name, eeprom_content->board_name);
140 strcpy(desc->revision, eeprom_content->board_revision);
141 strcpy(desc->serial, eeprom_content->board_sn);
142 if (eth_valid)
143 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
144
145 desc->header = EEPROM_HEADER_MAGIC;
146
147 free(eeprom_content);
148
149 return ret;
150}
151
152static bool xilinx_detect_legacy(u8 *buffer)
153{
154 int i;
155 char c;
156
157 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
158 c = buffer[i];
159
160 if (c < '0' || c > '9')
161 return false;
162 }
163
164 return true;
165}
166
Michal Simekf149b392020-08-03 16:14:23 +0200167static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
168 struct xilinx_board_description *desc)
169{
170 int ret, eeprom_size;
171 u8 *fru_content;
172
173 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
174 eeprom_size = i2c_eeprom_size(dev);
175
176 fru_content = calloc(1, eeprom_size);
177 if (!fru_content)
178 return -ENOMEM;
179
180 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
181 fru_content);
182
183 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
184 eeprom_size);
185 if (ret) {
186 debug("%s: I2C EEPROM read failed\n", __func__);
187 free(fru_content);
188 return ret;
189 }
190
191 printf("Xilinx I2C FRU format at %s:\n", name);
192 fru_capture((unsigned long)fru_content);
193 ret = fru_display(0);
194 if (ret) {
195 printf("FRU format decoding failed.\n");
196 return ret;
197 }
198
199 if (desc->header == EEPROM_HEADER_MAGIC) {
200 debug("Information already filled\n");
201 return -EINVAL;
202 }
203
204 /* It is clear that FRU was captured and structures were filled */
205 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
206 sizeof(desc->manufacturer));
207 strncpy(desc->name, (char *)fru_data.brd.product_name,
208 sizeof(desc->name));
209 strncpy(desc->revision, (char *)fru_data.brd.rev,
210 sizeof(desc->revision));
211 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
212 sizeof(desc->serial));
213 desc->header = EEPROM_HEADER_MAGIC;
214
215 return 0;
216}
217
218static bool xilinx_detect_fru(u8 *buffer)
219{
220 u8 checksum = 0;
221 int i;
222
223 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
224 if (checksum) {
225 debug("%s Common header CRC FAIL\n", __func__);
226 return false;
227 }
228
229 bool all_zeros = true;
230 /* Checksum over all zeros is also zero that's why detect this case */
231 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
232 if (buffer[i] != 0)
233 all_zeros = false;
234 }
235
236 if (all_zeros)
237 return false;
238
239 debug("%s Common header CRC PASS\n", __func__);
240 return true;
241}
242
Michal Simekd61728c2020-08-03 13:01:45 +0200243static int xilinx_read_eeprom_single(char *name,
244 struct xilinx_board_description *desc)
245{
246 int ret;
247 struct udevice *dev;
248 ofnode eeprom;
249 u8 buffer[XILINX_I2C_DETECTION_BITS];
250
251 eeprom = ofnode_get_aliases_node(name);
252 if (!ofnode_valid(eeprom))
253 return -ENODEV;
254
255 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
256 if (ret)
257 return ret;
258
259 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
260 if (ret) {
261 debug("%s: I2C EEPROM read failed\n", __func__);
262 return ret;
263 }
264
265 debug("%s: i2c memory detected: %s\n", __func__, name);
266
Michal Simekf149b392020-08-03 16:14:23 +0200267 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
268 return xilinx_read_eeprom_fru(dev, name, desc);
269
Michal Simekd61728c2020-08-03 13:01:45 +0200270 if (xilinx_detect_legacy(buffer))
271 return xilinx_read_eeprom_legacy(dev, name, desc);
272
273 return -ENODEV;
274}
275
276__maybe_unused int xilinx_read_eeprom(void)
277{
Michal Simeka03b5942020-08-03 12:57:05 +0200278 int id, ret;
Michal Simekd61728c2020-08-03 13:01:45 +0200279 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simeka03b5942020-08-03 12:57:05 +0200280 struct xilinx_board_description *desc;
Michal Simekd61728c2020-08-03 13:01:45 +0200281
282 highest_id = dev_read_alias_highest_id("nvmem");
283 /* No nvmem aliases present */
284 if (highest_id < 0)
285 return -EINVAL;
286
Michal Simeka03b5942020-08-03 12:57:05 +0200287 board_info = calloc(1, sizeof(desc) * highest_id);
Michal Simekd61728c2020-08-03 13:01:45 +0200288 if (!board_info)
289 return -ENOMEM;
290
291 debug("%s: Highest ID %d, board_info %p\n", __func__,
292 highest_id, board_info);
293
294 for (id = 0; id <= highest_id; id++) {
295 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
296
Michal Simeka03b5942020-08-03 12:57:05 +0200297 /* Alloc structure */
298 desc = board_info[id];
299 if (!desc) {
300 desc = calloc(1, sizeof(*desc));
301 if (!desc)
302 return -ENOMEM;
303
304 board_info[id] = desc;
305 }
306
Michal Simekd61728c2020-08-03 13:01:45 +0200307 /* Ignoring return value for supporting multiple chips */
Michal Simeka03b5942020-08-03 12:57:05 +0200308 ret = xilinx_read_eeprom_single(name_buf, desc);
309 if (ret) {
310 free(desc);
311 board_info[id] = NULL;
312 }
Michal Simekd61728c2020-08-03 13:01:45 +0200313 }
314
Michal Simeka03b5942020-08-03 12:57:05 +0200315 /*
316 * Consider to clean board_info structure when board/cards are not
317 * detected.
318 */
Michal Simekd61728c2020-08-03 13:01:45 +0200319
320 return 0;
321}
322
Michal Simekfc274a52019-12-19 17:45:15 +0100323#if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100324void *board_fdt_blob_setup(void)
325{
Michal Simeke2572b52020-09-04 16:21:47 +0200326 void *fdt_blob;
Michal Simek453bb772020-03-19 10:23:56 +0100327
Michal Simeka672b982021-01-04 11:07:28 +0100328 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
329 !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
Michal Simekfc3c6fd2021-02-02 13:33:22 +0100330 !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
Michal Simek506009f2021-01-04 11:03:36 +0100331 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100332
Michal Simek506009f2021-01-04 11:03:36 +0100333 if (fdt_magic(fdt_blob) == FDT_MAGIC)
334 return fdt_blob;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100335
Michal Simek506009f2021-01-04 11:03:36 +0100336 debug("DTB is not passed via %p\n", fdt_blob);
337 }
Michal Simekfc274a52019-12-19 17:45:15 +0100338
Michal Simek506009f2021-01-04 11:03:36 +0100339 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
340 /*
341 * FDT is at end of BSS unless it is in a different memory
342 * region
343 */
344 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
345 fdt_blob = (ulong *)&_image_binary_end;
346 else
347 fdt_blob = (ulong *)&__bss_end;
348 } else {
349 /* FDT is at end of image */
350 fdt_blob = (ulong *)&_end;
351 }
Michal Simekfc274a52019-12-19 17:45:15 +0100352
353 if (fdt_magic(fdt_blob) == FDT_MAGIC)
354 return fdt_blob;
355
356 debug("DTB is also not passed via %p\n", fdt_blob);
357
358 return NULL;
Ibai Erkiagafec657b2019-10-02 15:57:36 +0100359}
360#endif
Michal Simek80fdef12020-03-31 12:39:37 +0200361
Michal Simek7c553ac2020-10-22 11:14:20 +0200362#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simeka03b5942020-08-03 12:57:05 +0200363static int env_set_by_index(const char *name, int index, char *data)
364{
365 char var[32];
366
367 if (!index)
368 sprintf(var, "board_%s", name);
369 else
370 sprintf(var, "card%d_%s", index, name);
371
372 return env_set(var, data);
373}
374
Michal Simek80fdef12020-03-31 12:39:37 +0200375int board_late_init_xilinx(void)
376{
Michal Simekca0f6162020-08-12 12:16:49 +0200377 u32 ret = 0;
Michal Simeka03b5942020-08-03 12:57:05 +0200378 int i, id, macid = 0;
379 struct xilinx_board_description *desc;
Michal Simek2570cc62020-08-12 12:17:53 +0200380 phys_size_t bootm_size = gd->ram_size;
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600381
T Karthik Reddy25484d92021-04-02 01:49:17 -0600382 if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600383 ulong scriptaddr;
384
385 scriptaddr = env_get_hex("scriptaddr", 0);
T Karthik Reddy25484d92021-04-02 01:49:17 -0600386 ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
T Karthik Reddyd388ced2020-04-01 06:01:21 -0600387 }
Michal Simek2570cc62020-08-12 12:17:53 +0200388
Michal Simek12305822020-10-23 07:54:18 +0200389 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simek2570cc62020-08-12 12:17:53 +0200390 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simek80fdef12020-03-31 12:39:37 +0200391
Michal Simekca0f6162020-08-12 12:16:49 +0200392 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
393
394 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simek2570cc62020-08-12 12:17:53 +0200395 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simekca0f6162020-08-12 12:16:49 +0200396
Michal Simeka03b5942020-08-03 12:57:05 +0200397 for (id = 0; id <= highest_id; id++) {
398 desc = board_info[id];
399 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
400 if (desc->manufacturer[0])
401 ret |= env_set_by_index("manufacturer", id,
402 desc->manufacturer);
403 if (desc->name[0])
404 ret |= env_set_by_index("name", id,
405 desc->name);
406 if (desc->revision[0])
407 ret |= env_set_by_index("rev", id,
408 desc->revision);
409 if (desc->serial[0])
410 ret |= env_set_by_index("serial", id,
411 desc->serial);
Michal Simekd61728c2020-08-03 13:01:45 +0200412
413 if (!CONFIG_IS_ENABLED(NET))
414 continue;
415
Michal Simeka03b5942020-08-03 12:57:05 +0200416 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
417 if (!desc->mac_addr[i])
418 continue;
419
420 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
421 ret |= eth_env_set_enetaddr_by_index("eth",
422 macid++, desc->mac_addr[i]);
423 }
Michal Simekd61728c2020-08-03 13:01:45 +0200424 }
425 }
426
Michal Simekca0f6162020-08-12 12:16:49 +0200427 if (ret)
428 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simekc8da6512020-07-09 15:57:56 +0200429
Michal Simek80fdef12020-03-31 12:39:37 +0200430 return 0;
431}
Michal Simek7c553ac2020-10-22 11:14:20 +0200432#endif
Michal Simek05af4832020-10-26 12:26:13 +0100433
434int __maybe_unused board_fit_config_name_match(const char *name)
435{
436 debug("%s: Check %s, default %s\n", __func__, name, DEVICE_TREE);
437
438 if (!strcmp(name, DEVICE_TREE))
439 return 0;
440
441 return -1;
442}