xilinx: board: Add FRU decoder support
[platform/kernel/u-boot.git] / board / xilinx / common / board.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014 - 2020 Xilinx, Inc.
4  * Michal Simek <michal.simek@xilinx.com>
5  */
6
7 #include <common.h>
8 #include <env.h>
9 #include <log.h>
10 #include <asm/sections.h>
11 #include <dm/uclass.h>
12 #include <i2c.h>
13 #include <linux/sizes.h>
14 #include <malloc.h>
15 #include "board.h"
16 #include <dm.h>
17 #include <i2c_eeprom.h>
18 #include <net.h>
19
20 #include "fru.h"
21
22 #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
23 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
24 {
25         int ret = -EINVAL;
26         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__,
34               ofnode_read_chosen_string("xlnx,eeprom"));
35
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);
45
46         return ret;
47 }
48 #endif
49
50 #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
58 struct 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
67 static int highest_id = -1;
68 static struct xilinx_board_description **board_info;
69
70 #define XILINX_I2C_DETECTION_BITS       sizeof(struct fru_common_hdr)
71
72 /* Variable which stores pointer to array which stores eeprom content */
73 struct 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
84 static 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
102 static 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
150 static 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
165 static 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
216 static 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
241 static 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
265         if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
266                 return xilinx_read_eeprom_fru(dev, name, desc);
267
268         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 {
276         int id, ret;
277         char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
278         struct xilinx_board_description *desc;
279
280         highest_id = dev_read_alias_highest_id("nvmem");
281         /* No nvmem aliases present */
282         if (highest_id < 0)
283                 return -EINVAL;
284
285         board_info = calloc(1, sizeof(desc) * highest_id);
286         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
295                 /* 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
305                 /* Ignoring return value for supporting multiple chips */
306                 ret = xilinx_read_eeprom_single(name_buf, desc);
307                 if (ret) {
308                         free(desc);
309                         board_info[id] = NULL;
310                 }
311         }
312
313         /*
314          * Consider to clean board_info structure when board/cards are not
315          * detected.
316          */
317
318         return 0;
319 }
320
321 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
322 void *board_fdt_blob_setup(void)
323 {
324         void *fdt_blob;
325
326 #if !defined(CONFIG_VERSAL_NO_DDR) && !defined(CONFIG_ZYNQMP_NO_DDR)
327         fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
328
329         if (fdt_magic(fdt_blob) == FDT_MAGIC)
330                 return fdt_blob;
331
332         debug("DTB is not passed via %p\n", fdt_blob);
333 #endif
334
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);
350
351         return NULL;
352 }
353 #endif
354
355 #if defined(CONFIG_BOARD_LATE_INIT)
356 static 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
368 int board_late_init_xilinx(void)
369 {
370         u32 ret = 0;
371         int i, id, macid = 0;
372         struct xilinx_board_description *desc;
373         phys_size_t bootm_size = gd->ram_size;
374
375         if (CONFIG_IS_ENABLED(ARCH_ZYNQ))
376                 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
377
378         ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
379
380         ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
381         ret |= env_set_addr("bootm_size", (void *)bootm_size);
382
383         for (id = 0; id <= highest_id; id++) {
384                 desc = board_info[id];
385                 if (desc && desc->header == EEPROM_HEADER_MAGIC) {
386                         if (desc->manufacturer[0])
387                                 ret |= env_set_by_index("manufacturer", id,
388                                                         desc->manufacturer);
389                         if (desc->name[0])
390                                 ret |= env_set_by_index("name", id,
391                                                         desc->name);
392                         if (desc->revision[0])
393                                 ret |= env_set_by_index("rev", id,
394                                                         desc->revision);
395                         if (desc->serial[0])
396                                 ret |= env_set_by_index("serial", id,
397                                                         desc->serial);
398
399                         if (!CONFIG_IS_ENABLED(NET))
400                                 continue;
401
402                         for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
403                                 if (!desc->mac_addr[i])
404                                         continue;
405
406                                 if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
407                                         ret |= eth_env_set_enetaddr_by_index("eth",
408                                                         macid++, desc->mac_addr[i]);
409                         }
410                 }
411         }
412
413         if (ret)
414                 printf("%s: Saving run time variables FAILED\n", __func__);
415
416         return 0;
417 }
418 #endif