Merge https://gitlab.denx.de/u-boot/custodians/u-boot-marvell
[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 #include <generated/dt.h>
20
21 #include "fru.h"
22
23 #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
24 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
25 {
26         int ret = -EINVAL;
27         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__,
35               ofnode_read_chosen_string("xlnx,eeprom"));
36
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);
46
47         return ret;
48 }
49 #endif
50
51 #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
59 struct 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
68 static int highest_id = -1;
69 static struct xilinx_board_description **board_info;
70
71 #define XILINX_I2C_DETECTION_BITS       sizeof(struct fru_common_hdr)
72
73 /* Variable which stores pointer to array which stores eeprom content */
74 struct 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
85 static 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
103 static 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
151 static 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
166 static 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
217 static 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
242 static 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
266         if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
267                 return xilinx_read_eeprom_fru(dev, name, desc);
268
269         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 {
277         int id, ret;
278         char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
279         struct xilinx_board_description *desc;
280
281         highest_id = dev_read_alias_highest_id("nvmem");
282         /* No nvmem aliases present */
283         if (highest_id < 0)
284                 return -EINVAL;
285
286         board_info = calloc(1, sizeof(desc) * highest_id);
287         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
296                 /* 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
306                 /* Ignoring return value for supporting multiple chips */
307                 ret = xilinx_read_eeprom_single(name_buf, desc);
308                 if (ret) {
309                         free(desc);
310                         board_info[id] = NULL;
311                 }
312         }
313
314         /*
315          * Consider to clean board_info structure when board/cards are not
316          * detected.
317          */
318
319         return 0;
320 }
321
322 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
323 void *board_fdt_blob_setup(void)
324 {
325         void *fdt_blob;
326
327 #if !defined(CONFIG_VERSAL_NO_DDR) && !defined(CONFIG_ZYNQMP_NO_DDR)
328         fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
329
330         if (fdt_magic(fdt_blob) == FDT_MAGIC)
331                 return fdt_blob;
332
333         debug("DTB is not passed via %p\n", fdt_blob);
334 #endif
335
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);
351
352         return NULL;
353 }
354 #endif
355
356 #if defined(CONFIG_BOARD_LATE_INIT)
357 static 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
369 int board_late_init_xilinx(void)
370 {
371         u32 ret = 0;
372         int i, id, macid = 0;
373         struct xilinx_board_description *desc;
374         phys_size_t bootm_size = gd->ram_size;
375         struct bd_info *bd = gd->bd;
376
377         if (!CONFIG_IS_ENABLED(MICROBLAZE) && bd->bi_dram[0].start) {
378                 ulong scriptaddr;
379
380                 scriptaddr = env_get_hex("scriptaddr", 0);
381                 ret |= env_set_hex("scriptaddr",
382                                    bd->bi_dram[0].start + scriptaddr);
383         }
384
385         if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
386                 bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
387
388         ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
389
390         ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
391         ret |= env_set_addr("bootm_size", (void *)bootm_size);
392
393         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);
408
409                         if (!CONFIG_IS_ENABLED(NET))
410                                 continue;
411
412                         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                         }
420                 }
421         }
422
423         if (ret)
424                 printf("%s: Saving run time variables FAILED\n", __func__);
425
426         return 0;
427 }
428 #endif
429
430 int __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 }