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/global_data.h>
11 #include <asm/sections.h>
12 #include <dm/uclass.h>
13 #include <i2c.h>
14 #include <linux/sizes.h>
15 #include <malloc.h>
16 #include "board.h"
17 #include <dm.h>
18 #include <i2c_eeprom.h>
19 #include <net.h>
20 #include <generated/dt.h>
21 
22 #include "fru.h"
23 
24 #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
zynq_board_read_rom_ethaddr(unsigned char * ethaddr)25 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
26 {
27 	int ret = -EINVAL;
28 	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__,
36 	      ofnode_read_chosen_string("xlnx,eeprom"));
37 
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);
47 
48 	return ret;
49 }
50 #endif
51 
52 #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 
60 struct 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 
69 static int highest_id = -1;
70 static struct xilinx_board_description **board_info;
71 
72 #define XILINX_I2C_DETECTION_BITS	sizeof(struct fru_common_hdr)
73 
74 /* Variable which stores pointer to array which stores eeprom content */
75 struct 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 
xilinx_eeprom_legacy_cleanup(char * eeprom,int size)86 static 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 
xilinx_read_eeprom_legacy(struct udevice * dev,char * name,struct xilinx_board_description * desc)104 static 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 
xilinx_detect_legacy(u8 * buffer)152 static 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 
xilinx_read_eeprom_fru(struct udevice * dev,char * name,struct xilinx_board_description * desc)167 static 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 
xilinx_detect_fru(u8 * buffer)218 static 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 
xilinx_read_eeprom_single(char * name,struct xilinx_board_description * desc)243 static 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 
267 	if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
268 		return xilinx_read_eeprom_fru(dev, name, desc);
269 
270 	if (xilinx_detect_legacy(buffer))
271 		return xilinx_read_eeprom_legacy(dev, name, desc);
272 
273 	return -ENODEV;
274 }
275 
xilinx_read_eeprom(void)276 __maybe_unused int xilinx_read_eeprom(void)
277 {
278 	int id, ret;
279 	char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
280 	struct xilinx_board_description *desc;
281 
282 	highest_id = dev_read_alias_highest_id("nvmem");
283 	/* No nvmem aliases present */
284 	if (highest_id < 0)
285 		return -EINVAL;
286 
287 	board_info = calloc(1, sizeof(desc) * highest_id);
288 	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 
297 		/* 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 
307 		/* Ignoring return value for supporting multiple chips */
308 		ret = xilinx_read_eeprom_single(name_buf, desc);
309 		if (ret) {
310 			free(desc);
311 			board_info[id] = NULL;
312 		}
313 	}
314 
315 	/*
316 	 * Consider to clean board_info structure when board/cards are not
317 	 * detected.
318 	 */
319 
320 	return 0;
321 }
322 
323 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
board_fdt_blob_setup(void)324 void *board_fdt_blob_setup(void)
325 {
326 	void *fdt_blob;
327 
328 	if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
329 	    !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
330 	    !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
331 		fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
332 
333 		if (fdt_magic(fdt_blob) == FDT_MAGIC)
334 			return fdt_blob;
335 
336 		debug("DTB is not passed via %p\n", fdt_blob);
337 	}
338 
339 	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 	}
352 
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;
359 }
360 #endif
361 
362 #if defined(CONFIG_BOARD_LATE_INIT)
env_set_by_index(const char * name,int index,char * data)363 static 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 
board_late_init_xilinx(void)375 int board_late_init_xilinx(void)
376 {
377 	u32 ret = 0;
378 	int i, id, macid = 0;
379 	struct xilinx_board_description *desc;
380 	phys_size_t bootm_size = gd->ram_size;
381 	struct bd_info *bd = gd->bd;
382 
383 	if (!CONFIG_IS_ENABLED(MICROBLAZE) && bd->bi_dram[0].start) {
384 		ulong scriptaddr;
385 
386 		scriptaddr = env_get_hex("scriptaddr", 0);
387 		ret |= env_set_hex("scriptaddr",
388 				   bd->bi_dram[0].start + scriptaddr);
389 	}
390 
391 	if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
392 		bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
393 
394 	ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
395 
396 	ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
397 	ret |= env_set_addr("bootm_size", (void *)bootm_size);
398 
399 	for (id = 0; id <= highest_id; id++) {
400 		desc = board_info[id];
401 		if (desc && desc->header == EEPROM_HEADER_MAGIC) {
402 			if (desc->manufacturer[0])
403 				ret |= env_set_by_index("manufacturer", id,
404 							desc->manufacturer);
405 			if (desc->name[0])
406 				ret |= env_set_by_index("name", id,
407 							desc->name);
408 			if (desc->revision[0])
409 				ret |= env_set_by_index("rev", id,
410 							desc->revision);
411 			if (desc->serial[0])
412 				ret |= env_set_by_index("serial", id,
413 							desc->serial);
414 
415 			if (!CONFIG_IS_ENABLED(NET))
416 				continue;
417 
418 			for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
419 				if (!desc->mac_addr[i])
420 					continue;
421 
422 				if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
423 					ret |= eth_env_set_enetaddr_by_index("eth",
424 							macid++, desc->mac_addr[i]);
425 			}
426 		}
427 	}
428 
429 	if (ret)
430 		printf("%s: Saving run time variables FAILED\n", __func__);
431 
432 	return 0;
433 }
434 #endif
435 
board_fit_config_name_match(const char * name)436 int __maybe_unused board_fit_config_name_match(const char *name)
437 {
438 	debug("%s: Check %s, default %s\n", __func__, name, DEVICE_TREE);
439 
440 	if (!strcmp(name, DEVICE_TREE))
441 		return 0;
442 
443 	return -1;
444 }
445