blob: cdc06a39ce5828adc5bf066652d9342310829670 [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>
Michal Simek878ba362019-12-19 17:45:15 +010010#include <asm/sections.h>
Michal Simek014d3042019-01-21 15:25:02 +010011#include <dm/uclass.h>
12#include <i2c.h>
Michal Simekb90b97e2020-04-08 10:51:36 +020013#include <linux/sizes.h>
Michal Simek394ee242020-08-03 13:01:45 +020014#include <malloc.h>
Michal Simek705d44a2020-03-31 12:39:37 +020015#include "board.h"
Michal Simek394ee242020-08-03 13:01:45 +020016#include <dm.h>
17#include <i2c_eeprom.h>
18#include <net.h>
Michal Simekfe49df92020-10-26 12:26:13 +010019#include <generated/dt.h>
Michal Simek014d3042019-01-21 15:25:02 +010020
Michal Simek207e0622020-08-03 16:14:23 +020021#include "fru.h"
22
Michal Simekaee22b32020-08-03 12:59:28 +020023#if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
Michal Simek014d3042019-01-21 15:25:02 +010024int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
25{
Michal Simek13db6162019-01-21 16:29:07 +010026 int ret = -EINVAL;
Michal Simek13db6162019-01-21 16:29:07 +010027 struct udevice *dev;
28 ofnode eeprom;
29
30 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
31 if (!ofnode_valid(eeprom))
32 return -ENODEV;
33
34 debug("%s: Path to EEPROM %s\n", __func__,
Simon Glassf3455962020-01-27 08:49:43 -070035 ofnode_read_chosen_string("xlnx,eeprom"));
Michal Simek13db6162019-01-21 16:29:07 +010036
37 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
38 if (ret)
39 return ret;
40
41 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
42 if (ret)
43 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
44 else
45 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
Michal Simek13db6162019-01-21 16:29:07 +010046
47 return ret;
48}
Michal Simekaee22b32020-08-03 12:59:28 +020049#endif
Ibai Erkiaga6f658202019-10-02 15:57:36 +010050
Michal Simek394ee242020-08-03 13:01:45 +020051#define EEPROM_HEADER_MAGIC 0xdaaddeed
52#define EEPROM_HDR_MANUFACTURER_LEN 16
53#define EEPROM_HDR_NAME_LEN 16
54#define EEPROM_HDR_REV_LEN 8
55#define EEPROM_HDR_SERIAL_LEN 20
56#define EEPROM_HDR_NO_OF_MAC_ADDR 4
57#define EEPROM_HDR_ETH_ALEN ETH_ALEN
58
59struct xilinx_board_description {
60 u32 header;
61 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
62 char name[EEPROM_HDR_NAME_LEN + 1];
63 char revision[EEPROM_HDR_REV_LEN + 1];
64 char serial[EEPROM_HDR_SERIAL_LEN + 1];
65 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
66};
67
Michal Simek0bb24e12020-08-03 12:57:05 +020068static int highest_id = -1;
69static struct xilinx_board_description **board_info;
Michal Simek394ee242020-08-03 13:01:45 +020070
Michal Simek207e0622020-08-03 16:14:23 +020071#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simek394ee242020-08-03 13:01:45 +020072
73/* Variable which stores pointer to array which stores eeprom content */
74struct xilinx_legacy_format {
75 char board_sn[18]; /* 0x0 */
76 char unused0[14]; /* 0x12 */
77 char eth_mac[6]; /* 0x20 */
78 char unused1[170]; /* 0x26 */
79 char board_name[11]; /* 0xd0 */
80 char unused2[5]; /* 0xdc */
81 char board_revision[3]; /* 0xe0 */
82 char unused3[29]; /* 0xe3 */
83};
84
85static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
86{
87 int i;
88 char byte;
89
90 for (i = 0; i < size; i++) {
91 byte = eeprom[i];
92
93 /* Remove all ffs and spaces */
94 if (byte == 0xff || byte == ' ')
95 eeprom[i] = 0;
96
97 /* Convert strings to lower case */
98 if (byte >= 'A' && byte <= 'Z')
99 eeprom[i] = byte + 'a' - 'A';
100 }
101}
102
103static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
104 struct xilinx_board_description *desc)
105{
106 int ret, size;
107 struct xilinx_legacy_format *eeprom_content;
108 bool eth_valid = false;
109
110 size = sizeof(*eeprom_content);
111
112 eeprom_content = calloc(1, size);
113 if (!eeprom_content)
114 return -ENOMEM;
115
116 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
117 eeprom_content);
118
119 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
120 if (ret) {
121 debug("%s: I2C EEPROM read failed\n", __func__);
122 free(eeprom_content);
123 return ret;
124 }
125
126 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
127
128 printf("Xilinx I2C Legacy format at %s:\n", name);
129 printf(" Board name:\t%s\n", eeprom_content->board_name);
130 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
131 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
132
133 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
134 if (eth_valid)
135 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
136
137 /* Terminating \0 chars ensure end of string */
138 strcpy(desc->name, eeprom_content->board_name);
139 strcpy(desc->revision, eeprom_content->board_revision);
140 strcpy(desc->serial, eeprom_content->board_sn);
141 if (eth_valid)
142 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
143
144 desc->header = EEPROM_HEADER_MAGIC;
145
146 free(eeprom_content);
147
148 return ret;
149}
150
151static bool xilinx_detect_legacy(u8 *buffer)
152{
153 int i;
154 char c;
155
156 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
157 c = buffer[i];
158
159 if (c < '0' || c > '9')
160 return false;
161 }
162
163 return true;
164}
165
Michal Simek207e0622020-08-03 16:14:23 +0200166static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
167 struct xilinx_board_description *desc)
168{
169 int ret, eeprom_size;
170 u8 *fru_content;
171
172 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
173 eeprom_size = i2c_eeprom_size(dev);
174
175 fru_content = calloc(1, eeprom_size);
176 if (!fru_content)
177 return -ENOMEM;
178
179 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
180 fru_content);
181
182 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
183 eeprom_size);
184 if (ret) {
185 debug("%s: I2C EEPROM read failed\n", __func__);
186 free(fru_content);
187 return ret;
188 }
189
190 printf("Xilinx I2C FRU format at %s:\n", name);
191 fru_capture((unsigned long)fru_content);
192 ret = fru_display(0);
193 if (ret) {
194 printf("FRU format decoding failed.\n");
195 return ret;
196 }
197
198 if (desc->header == EEPROM_HEADER_MAGIC) {
199 debug("Information already filled\n");
200 return -EINVAL;
201 }
202
203 /* It is clear that FRU was captured and structures were filled */
204 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
205 sizeof(desc->manufacturer));
206 strncpy(desc->name, (char *)fru_data.brd.product_name,
207 sizeof(desc->name));
208 strncpy(desc->revision, (char *)fru_data.brd.rev,
209 sizeof(desc->revision));
210 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
211 sizeof(desc->serial));
212 desc->header = EEPROM_HEADER_MAGIC;
213
214 return 0;
215}
216
217static bool xilinx_detect_fru(u8 *buffer)
218{
219 u8 checksum = 0;
220 int i;
221
222 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
223 if (checksum) {
224 debug("%s Common header CRC FAIL\n", __func__);
225 return false;
226 }
227
228 bool all_zeros = true;
229 /* Checksum over all zeros is also zero that's why detect this case */
230 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
231 if (buffer[i] != 0)
232 all_zeros = false;
233 }
234
235 if (all_zeros)
236 return false;
237
238 debug("%s Common header CRC PASS\n", __func__);
239 return true;
240}
241
Michal Simek394ee242020-08-03 13:01:45 +0200242static int xilinx_read_eeprom_single(char *name,
243 struct xilinx_board_description *desc)
244{
245 int ret;
246 struct udevice *dev;
247 ofnode eeprom;
248 u8 buffer[XILINX_I2C_DETECTION_BITS];
249
250 eeprom = ofnode_get_aliases_node(name);
251 if (!ofnode_valid(eeprom))
252 return -ENODEV;
253
254 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
255 if (ret)
256 return ret;
257
258 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
259 if (ret) {
260 debug("%s: I2C EEPROM read failed\n", __func__);
261 return ret;
262 }
263
264 debug("%s: i2c memory detected: %s\n", __func__, name);
265
Michal Simek207e0622020-08-03 16:14:23 +0200266 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
267 return xilinx_read_eeprom_fru(dev, name, desc);
268
Michal Simek394ee242020-08-03 13:01:45 +0200269 if (xilinx_detect_legacy(buffer))
270 return xilinx_read_eeprom_legacy(dev, name, desc);
271
272 return -ENODEV;
273}
274
275__maybe_unused int xilinx_read_eeprom(void)
276{
Michal Simek0bb24e12020-08-03 12:57:05 +0200277 int id, ret;
Michal Simek394ee242020-08-03 13:01:45 +0200278 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simek0bb24e12020-08-03 12:57:05 +0200279 struct xilinx_board_description *desc;
Michal Simek394ee242020-08-03 13:01:45 +0200280
281 highest_id = dev_read_alias_highest_id("nvmem");
282 /* No nvmem aliases present */
283 if (highest_id < 0)
284 return -EINVAL;
285
Michal Simek0bb24e12020-08-03 12:57:05 +0200286 board_info = calloc(1, sizeof(desc) * highest_id);
Michal Simek394ee242020-08-03 13:01:45 +0200287 if (!board_info)
288 return -ENOMEM;
289
290 debug("%s: Highest ID %d, board_info %p\n", __func__,
291 highest_id, board_info);
292
293 for (id = 0; id <= highest_id; id++) {
294 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
295
Michal Simek0bb24e12020-08-03 12:57:05 +0200296 /* Alloc structure */
297 desc = board_info[id];
298 if (!desc) {
299 desc = calloc(1, sizeof(*desc));
300 if (!desc)
301 return -ENOMEM;
302
303 board_info[id] = desc;
304 }
305
Michal Simek394ee242020-08-03 13:01:45 +0200306 /* Ignoring return value for supporting multiple chips */
Michal Simek0bb24e12020-08-03 12:57:05 +0200307 ret = xilinx_read_eeprom_single(name_buf, desc);
308 if (ret) {
309 free(desc);
310 board_info[id] = NULL;
311 }
Michal Simek394ee242020-08-03 13:01:45 +0200312 }
313
Michal Simek0bb24e12020-08-03 12:57:05 +0200314 /*
315 * Consider to clean board_info structure when board/cards are not
316 * detected.
317 */
Michal Simek394ee242020-08-03 13:01:45 +0200318
319 return 0;
320}
321
Michal Simek878ba362019-12-19 17:45:15 +0100322#if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100323void *board_fdt_blob_setup(void)
324{
Michal Simekddf69ae2020-09-04 16:21:47 +0200325 void *fdt_blob;
Michal Simekc89f4292020-03-19 10:23:56 +0100326
327#if !defined(CONFIG_VERSAL_NO_DDR) && !defined(CONFIG_ZYNQMP_NO_DDR)
328 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100329
Michal Simek878ba362019-12-19 17:45:15 +0100330 if (fdt_magic(fdt_blob) == FDT_MAGIC)
331 return fdt_blob;
332
333 debug("DTB is not passed via %p\n", fdt_blob);
Michal Simekc89f4292020-03-19 10:23:56 +0100334#endif
Michal Simek878ba362019-12-19 17:45:15 +0100335
336#ifdef CONFIG_SPL_BUILD
337 /* FDT is at end of BSS unless it is in a different memory region */
338 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
339 fdt_blob = (ulong *)&_image_binary_end;
340 else
341 fdt_blob = (ulong *)&__bss_end;
342#else
343 /* FDT is at end of image */
344 fdt_blob = (ulong *)&_end;
345#endif
346
347 if (fdt_magic(fdt_blob) == FDT_MAGIC)
348 return fdt_blob;
349
350 debug("DTB is also not passed via %p\n", fdt_blob);
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100351
Michal Simek878ba362019-12-19 17:45:15 +0100352 return NULL;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100353}
354#endif
Michal Simek705d44a2020-03-31 12:39:37 +0200355
Michal Simek67ed85d2020-10-22 11:14:20 +0200356#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek0bb24e12020-08-03 12:57:05 +0200357static int env_set_by_index(const char *name, int index, char *data)
358{
359 char var[32];
360
361 if (!index)
362 sprintf(var, "board_%s", name);
363 else
364 sprintf(var, "card%d_%s", index, name);
365
366 return env_set(var, data);
367}
368
Michal Simek705d44a2020-03-31 12:39:37 +0200369int board_late_init_xilinx(void)
370{
Michal Simekbd07b5d2020-08-12 12:16:49 +0200371 u32 ret = 0;
Michal Simek0bb24e12020-08-03 12:57:05 +0200372 int i, id, macid = 0;
373 struct xilinx_board_description *desc;
Michal Simekc86ce0c2020-08-12 12:17:53 +0200374 phys_size_t bootm_size = gd->ram_size;
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600375 struct bd_info *bd = gd->bd;
376
Michal Simek9ccfcae2020-10-23 07:54:18 +0200377 if (!CONFIG_IS_ENABLED(MICROBLAZE) && bd->bi_dram[0].start) {
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600378 ulong scriptaddr;
379
380 scriptaddr = env_get_hex("scriptaddr", 0);
381 ret |= env_set_hex("scriptaddr",
382 bd->bi_dram[0].start + scriptaddr);
383 }
Michal Simekc86ce0c2020-08-12 12:17:53 +0200384
Michal Simek9ccfcae2020-10-23 07:54:18 +0200385 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simekc86ce0c2020-08-12 12:17:53 +0200386 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simekbd07b5d2020-08-12 12:16:49 +0200387
388 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
389
390 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simekc86ce0c2020-08-12 12:17:53 +0200391 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simek705d44a2020-03-31 12:39:37 +0200392
Michal Simek0bb24e12020-08-03 12:57:05 +0200393 for (id = 0; id <= highest_id; id++) {
394 desc = board_info[id];
395 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
396 if (desc->manufacturer[0])
397 ret |= env_set_by_index("manufacturer", id,
398 desc->manufacturer);
399 if (desc->name[0])
400 ret |= env_set_by_index("name", id,
401 desc->name);
402 if (desc->revision[0])
403 ret |= env_set_by_index("rev", id,
404 desc->revision);
405 if (desc->serial[0])
406 ret |= env_set_by_index("serial", id,
407 desc->serial);
Michal Simek394ee242020-08-03 13:01:45 +0200408
409 if (!CONFIG_IS_ENABLED(NET))
410 continue;
411
Michal Simek0bb24e12020-08-03 12:57:05 +0200412 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
413 if (!desc->mac_addr[i])
414 continue;
415
416 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
417 ret |= eth_env_set_enetaddr_by_index("eth",
418 macid++, desc->mac_addr[i]);
419 }
Michal Simek394ee242020-08-03 13:01:45 +0200420 }
421 }
422
Michal Simekbd07b5d2020-08-12 12:16:49 +0200423 if (ret)
424 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simek8ba62d22020-07-09 15:57:56 +0200425
Michal Simek705d44a2020-03-31 12:39:37 +0200426 return 0;
427}
Michal Simek67ed85d2020-10-22 11:14:20 +0200428#endif
Michal Simekfe49df92020-10-26 12:26:13 +0100429
430int __maybe_unused board_fit_config_name_match(const char *name)
431{
432 debug("%s: Check %s, default %s\n", __func__, name, DEVICE_TREE);
433
434 if (!strcmp(name, DEVICE_TREE))
435 return 0;
436
437 return -1;
438}