blob: bcdd3ae4f14a689b606f2e4c1c641e4b30379eff [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 Simek014d3042019-01-21 15:25:02 +010019
Michal Simek207e0622020-08-03 16:14:23 +020020#include "fru.h"
21
Michal Simekaee22b32020-08-03 12:59:28 +020022#if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
Michal Simek014d3042019-01-21 15:25:02 +010023int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
24{
Michal Simek13db6162019-01-21 16:29:07 +010025 int ret = -EINVAL;
Michal Simek13db6162019-01-21 16:29:07 +010026 struct udevice *dev;
27 ofnode eeprom;
28
29 eeprom = ofnode_get_chosen_node("xlnx,eeprom");
30 if (!ofnode_valid(eeprom))
31 return -ENODEV;
32
33 debug("%s: Path to EEPROM %s\n", __func__,
Simon Glassf3455962020-01-27 08:49:43 -070034 ofnode_read_chosen_string("xlnx,eeprom"));
Michal Simek13db6162019-01-21 16:29:07 +010035
36 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
37 if (ret)
38 return ret;
39
40 ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
41 if (ret)
42 debug("%s: I2C EEPROM MAC address read failed\n", __func__);
43 else
44 debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
Michal Simek13db6162019-01-21 16:29:07 +010045
46 return ret;
47}
Michal Simekaee22b32020-08-03 12:59:28 +020048#endif
Ibai Erkiaga6f658202019-10-02 15:57:36 +010049
Michal Simek394ee242020-08-03 13:01:45 +020050#define EEPROM_HEADER_MAGIC 0xdaaddeed
51#define EEPROM_HDR_MANUFACTURER_LEN 16
52#define EEPROM_HDR_NAME_LEN 16
53#define EEPROM_HDR_REV_LEN 8
54#define EEPROM_HDR_SERIAL_LEN 20
55#define EEPROM_HDR_NO_OF_MAC_ADDR 4
56#define EEPROM_HDR_ETH_ALEN ETH_ALEN
57
58struct xilinx_board_description {
59 u32 header;
60 char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
61 char name[EEPROM_HDR_NAME_LEN + 1];
62 char revision[EEPROM_HDR_REV_LEN + 1];
63 char serial[EEPROM_HDR_SERIAL_LEN + 1];
64 u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
65};
66
Michal Simek0bb24e12020-08-03 12:57:05 +020067static int highest_id = -1;
68static struct xilinx_board_description **board_info;
Michal Simek394ee242020-08-03 13:01:45 +020069
Michal Simek207e0622020-08-03 16:14:23 +020070#define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
Michal Simek394ee242020-08-03 13:01:45 +020071
72/* Variable which stores pointer to array which stores eeprom content */
73struct xilinx_legacy_format {
74 char board_sn[18]; /* 0x0 */
75 char unused0[14]; /* 0x12 */
76 char eth_mac[6]; /* 0x20 */
77 char unused1[170]; /* 0x26 */
78 char board_name[11]; /* 0xd0 */
79 char unused2[5]; /* 0xdc */
80 char board_revision[3]; /* 0xe0 */
81 char unused3[29]; /* 0xe3 */
82};
83
84static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
85{
86 int i;
87 char byte;
88
89 for (i = 0; i < size; i++) {
90 byte = eeprom[i];
91
92 /* Remove all ffs and spaces */
93 if (byte == 0xff || byte == ' ')
94 eeprom[i] = 0;
95
96 /* Convert strings to lower case */
97 if (byte >= 'A' && byte <= 'Z')
98 eeprom[i] = byte + 'a' - 'A';
99 }
100}
101
102static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
103 struct xilinx_board_description *desc)
104{
105 int ret, size;
106 struct xilinx_legacy_format *eeprom_content;
107 bool eth_valid = false;
108
109 size = sizeof(*eeprom_content);
110
111 eeprom_content = calloc(1, size);
112 if (!eeprom_content)
113 return -ENOMEM;
114
115 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
116 eeprom_content);
117
118 ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
119 if (ret) {
120 debug("%s: I2C EEPROM read failed\n", __func__);
121 free(eeprom_content);
122 return ret;
123 }
124
125 xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
126
127 printf("Xilinx I2C Legacy format at %s:\n", name);
128 printf(" Board name:\t%s\n", eeprom_content->board_name);
129 printf(" Board rev:\t%s\n", eeprom_content->board_revision);
130 printf(" Board SN:\t%s\n", eeprom_content->board_sn);
131
132 eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
133 if (eth_valid)
134 printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
135
136 /* Terminating \0 chars ensure end of string */
137 strcpy(desc->name, eeprom_content->board_name);
138 strcpy(desc->revision, eeprom_content->board_revision);
139 strcpy(desc->serial, eeprom_content->board_sn);
140 if (eth_valid)
141 memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
142
143 desc->header = EEPROM_HEADER_MAGIC;
144
145 free(eeprom_content);
146
147 return ret;
148}
149
150static bool xilinx_detect_legacy(u8 *buffer)
151{
152 int i;
153 char c;
154
155 for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
156 c = buffer[i];
157
158 if (c < '0' || c > '9')
159 return false;
160 }
161
162 return true;
163}
164
Michal Simek207e0622020-08-03 16:14:23 +0200165static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
166 struct xilinx_board_description *desc)
167{
168 int ret, eeprom_size;
169 u8 *fru_content;
170
171 /* FIXME this is shortcut - if eeprom type is wrong it will fail */
172 eeprom_size = i2c_eeprom_size(dev);
173
174 fru_content = calloc(1, eeprom_size);
175 if (!fru_content)
176 return -ENOMEM;
177
178 debug("%s: I2C EEPROM read pass data at %p\n", __func__,
179 fru_content);
180
181 ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
182 eeprom_size);
183 if (ret) {
184 debug("%s: I2C EEPROM read failed\n", __func__);
185 free(fru_content);
186 return ret;
187 }
188
189 printf("Xilinx I2C FRU format at %s:\n", name);
190 fru_capture((unsigned long)fru_content);
191 ret = fru_display(0);
192 if (ret) {
193 printf("FRU format decoding failed.\n");
194 return ret;
195 }
196
197 if (desc->header == EEPROM_HEADER_MAGIC) {
198 debug("Information already filled\n");
199 return -EINVAL;
200 }
201
202 /* It is clear that FRU was captured and structures were filled */
203 strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
204 sizeof(desc->manufacturer));
205 strncpy(desc->name, (char *)fru_data.brd.product_name,
206 sizeof(desc->name));
207 strncpy(desc->revision, (char *)fru_data.brd.rev,
208 sizeof(desc->revision));
209 strncpy(desc->serial, (char *)fru_data.brd.serial_number,
210 sizeof(desc->serial));
211 desc->header = EEPROM_HEADER_MAGIC;
212
213 return 0;
214}
215
216static bool xilinx_detect_fru(u8 *buffer)
217{
218 u8 checksum = 0;
219 int i;
220
221 checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
222 if (checksum) {
223 debug("%s Common header CRC FAIL\n", __func__);
224 return false;
225 }
226
227 bool all_zeros = true;
228 /* Checksum over all zeros is also zero that's why detect this case */
229 for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
230 if (buffer[i] != 0)
231 all_zeros = false;
232 }
233
234 if (all_zeros)
235 return false;
236
237 debug("%s Common header CRC PASS\n", __func__);
238 return true;
239}
240
Michal Simek394ee242020-08-03 13:01:45 +0200241static int xilinx_read_eeprom_single(char *name,
242 struct xilinx_board_description *desc)
243{
244 int ret;
245 struct udevice *dev;
246 ofnode eeprom;
247 u8 buffer[XILINX_I2C_DETECTION_BITS];
248
249 eeprom = ofnode_get_aliases_node(name);
250 if (!ofnode_valid(eeprom))
251 return -ENODEV;
252
253 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
254 if (ret)
255 return ret;
256
257 ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
258 if (ret) {
259 debug("%s: I2C EEPROM read failed\n", __func__);
260 return ret;
261 }
262
263 debug("%s: i2c memory detected: %s\n", __func__, name);
264
Michal Simek207e0622020-08-03 16:14:23 +0200265 if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
266 return xilinx_read_eeprom_fru(dev, name, desc);
267
Michal Simek394ee242020-08-03 13:01:45 +0200268 if (xilinx_detect_legacy(buffer))
269 return xilinx_read_eeprom_legacy(dev, name, desc);
270
271 return -ENODEV;
272}
273
274__maybe_unused int xilinx_read_eeprom(void)
275{
Michal Simek0bb24e12020-08-03 12:57:05 +0200276 int id, ret;
Michal Simek394ee242020-08-03 13:01:45 +0200277 char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
Michal Simek0bb24e12020-08-03 12:57:05 +0200278 struct xilinx_board_description *desc;
Michal Simek394ee242020-08-03 13:01:45 +0200279
280 highest_id = dev_read_alias_highest_id("nvmem");
281 /* No nvmem aliases present */
282 if (highest_id < 0)
283 return -EINVAL;
284
Michal Simek0bb24e12020-08-03 12:57:05 +0200285 board_info = calloc(1, sizeof(desc) * highest_id);
Michal Simek394ee242020-08-03 13:01:45 +0200286 if (!board_info)
287 return -ENOMEM;
288
289 debug("%s: Highest ID %d, board_info %p\n", __func__,
290 highest_id, board_info);
291
292 for (id = 0; id <= highest_id; id++) {
293 snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
294
Michal Simek0bb24e12020-08-03 12:57:05 +0200295 /* Alloc structure */
296 desc = board_info[id];
297 if (!desc) {
298 desc = calloc(1, sizeof(*desc));
299 if (!desc)
300 return -ENOMEM;
301
302 board_info[id] = desc;
303 }
304
Michal Simek394ee242020-08-03 13:01:45 +0200305 /* Ignoring return value for supporting multiple chips */
Michal Simek0bb24e12020-08-03 12:57:05 +0200306 ret = xilinx_read_eeprom_single(name_buf, desc);
307 if (ret) {
308 free(desc);
309 board_info[id] = NULL;
310 }
Michal Simek394ee242020-08-03 13:01:45 +0200311 }
312
Michal Simek0bb24e12020-08-03 12:57:05 +0200313 /*
314 * Consider to clean board_info structure when board/cards are not
315 * detected.
316 */
Michal Simek394ee242020-08-03 13:01:45 +0200317
318 return 0;
319}
320
Michal Simek878ba362019-12-19 17:45:15 +0100321#if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100322void *board_fdt_blob_setup(void)
323{
Michal Simekddf69ae2020-09-04 16:21:47 +0200324 void *fdt_blob;
Michal Simekc89f4292020-03-19 10:23:56 +0100325
326#if !defined(CONFIG_VERSAL_NO_DDR) && !defined(CONFIG_ZYNQMP_NO_DDR)
327 fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100328
Michal Simek878ba362019-12-19 17:45:15 +0100329 if (fdt_magic(fdt_blob) == FDT_MAGIC)
330 return fdt_blob;
331
332 debug("DTB is not passed via %p\n", fdt_blob);
Michal Simekc89f4292020-03-19 10:23:56 +0100333#endif
Michal Simek878ba362019-12-19 17:45:15 +0100334
335#ifdef CONFIG_SPL_BUILD
336 /* FDT is at end of BSS unless it is in a different memory region */
337 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
338 fdt_blob = (ulong *)&_image_binary_end;
339 else
340 fdt_blob = (ulong *)&__bss_end;
341#else
342 /* FDT is at end of image */
343 fdt_blob = (ulong *)&_end;
344#endif
345
346 if (fdt_magic(fdt_blob) == FDT_MAGIC)
347 return fdt_blob;
348
349 debug("DTB is also not passed via %p\n", fdt_blob);
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100350
Michal Simek878ba362019-12-19 17:45:15 +0100351 return NULL;
Ibai Erkiaga6f658202019-10-02 15:57:36 +0100352}
353#endif
Michal Simek705d44a2020-03-31 12:39:37 +0200354
Michal Simek67ed85d2020-10-22 11:14:20 +0200355#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek0bb24e12020-08-03 12:57:05 +0200356static int env_set_by_index(const char *name, int index, char *data)
357{
358 char var[32];
359
360 if (!index)
361 sprintf(var, "board_%s", name);
362 else
363 sprintf(var, "card%d_%s", index, name);
364
365 return env_set(var, data);
366}
367
Michal Simek705d44a2020-03-31 12:39:37 +0200368int board_late_init_xilinx(void)
369{
Michal Simekbd07b5d2020-08-12 12:16:49 +0200370 u32 ret = 0;
Michal Simek0bb24e12020-08-03 12:57:05 +0200371 int i, id, macid = 0;
372 struct xilinx_board_description *desc;
Michal Simekc86ce0c2020-08-12 12:17:53 +0200373 phys_size_t bootm_size = gd->ram_size;
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600374 struct bd_info *bd = gd->bd;
375
Michal Simek9ccfcae2020-10-23 07:54:18 +0200376 if (!CONFIG_IS_ENABLED(MICROBLAZE) && bd->bi_dram[0].start) {
T Karthik Reddy17b7f662020-04-01 06:01:21 -0600377 ulong scriptaddr;
378
379 scriptaddr = env_get_hex("scriptaddr", 0);
380 ret |= env_set_hex("scriptaddr",
381 bd->bi_dram[0].start + scriptaddr);
382 }
Michal Simekc86ce0c2020-08-12 12:17:53 +0200383
Michal Simek9ccfcae2020-10-23 07:54:18 +0200384 if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
Michal Simekc86ce0c2020-08-12 12:17:53 +0200385 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
Michal Simekbd07b5d2020-08-12 12:16:49 +0200386
387 ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
388
389 ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
Michal Simekc86ce0c2020-08-12 12:17:53 +0200390 ret |= env_set_addr("bootm_size", (void *)bootm_size);
Michal Simek705d44a2020-03-31 12:39:37 +0200391
Michal Simek0bb24e12020-08-03 12:57:05 +0200392 for (id = 0; id <= highest_id; id++) {
393 desc = board_info[id];
394 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
395 if (desc->manufacturer[0])
396 ret |= env_set_by_index("manufacturer", id,
397 desc->manufacturer);
398 if (desc->name[0])
399 ret |= env_set_by_index("name", id,
400 desc->name);
401 if (desc->revision[0])
402 ret |= env_set_by_index("rev", id,
403 desc->revision);
404 if (desc->serial[0])
405 ret |= env_set_by_index("serial", id,
406 desc->serial);
Michal Simek394ee242020-08-03 13:01:45 +0200407
408 if (!CONFIG_IS_ENABLED(NET))
409 continue;
410
Michal Simek0bb24e12020-08-03 12:57:05 +0200411 for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
412 if (!desc->mac_addr[i])
413 continue;
414
415 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
416 ret |= eth_env_set_enetaddr_by_index("eth",
417 macid++, desc->mac_addr[i]);
418 }
Michal Simek394ee242020-08-03 13:01:45 +0200419 }
420 }
421
Michal Simekbd07b5d2020-08-12 12:16:49 +0200422 if (ret)
423 printf("%s: Saving run time variables FAILED\n", __func__);
Michal Simek8ba62d22020-07-09 15:57:56 +0200424
Michal Simek705d44a2020-03-31 12:39:37 +0200425 return 0;
426}
Michal Simek67ed85d2020-10-22 11:14:20 +0200427#endif