• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Library to support early TI EVM EEPROM handling
4  *
5  * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
6  *	Lokesh Vutla
7  *	Steve Kipisz
8  */
9 
10 #include <common.h>
11 #include <eeprom.h>
12 #include <asm/arch/hardware.h>
13 #include <asm/omap_common.h>
14 #include <dm/uclass.h>
15 #include <env.h>
16 #include <i2c.h>
17 
18 #include "board_detect.h"
19 
20 #if !defined(CONFIG_DM_I2C)
21 /**
22  * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
23  * @i2c_bus: i2c bus number to initialize
24  * @dev_addr: Device address to probe for
25  *
26  * Return: 0 on success or corresponding error on failure.
27  */
ti_i2c_eeprom_init(int i2c_bus,int dev_addr)28 static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
29 {
30 	int rc;
31 
32 	if (i2c_bus >= 0) {
33 		rc = i2c_set_bus_num(i2c_bus);
34 		if (rc)
35 			return rc;
36 	}
37 
38 	return i2c_probe(dev_addr);
39 }
40 
41 /**
42  * ti_i2c_eeprom_read - Read data from an EEPROM
43  * @dev_addr: The device address of the EEPROM
44  * @offset: Offset to start reading in the EEPROM
45  * @ep: Pointer to a buffer to read into
46  * @epsize: Size of buffer
47  *
48  * Return: 0 on success or corresponding result of i2c_read
49  */
ti_i2c_eeprom_read(int dev_addr,int offset,uchar * ep,int epsize)50 static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
51 					     uchar *ep, int epsize)
52 {
53 	return i2c_read(dev_addr, offset, 2, ep, epsize);
54 }
55 #endif
56 
57 /**
58  * ti_eeprom_string_cleanup() - Handle eeprom programming errors
59  * @s:	eeprom string (should be NULL terminated)
60  *
61  * Some Board manufacturers do not add a NULL termination at the
62  * end of string, instead some binary information is kludged in, hence
63  * convert the string to just printable characters of ASCII chart.
64  */
ti_eeprom_string_cleanup(char * s)65 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
66 {
67 	int i, l;
68 
69 	l = strlen(s);
70 	for (i = 0; i < l; i++, s++)
71 		if (*s < ' ' || *s > '~') {
72 			*s = 0;
73 			break;
74 		}
75 }
76 
gpi2c_init(void)77 __weak void gpi2c_init(void)
78 {
79 }
80 
ti_i2c_eeprom_get(int bus_addr,int dev_addr,u32 header,u32 size,uint8_t * ep)81 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
82 					    u32 header, u32 size, uint8_t *ep)
83 {
84 	u32 hdr_read;
85 	int rc;
86 
87 #if defined(CONFIG_DM_I2C)
88 	struct udevice *dev;
89 	struct udevice *bus;
90 
91 	rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
92 	if (rc)
93 		return rc;
94 	rc = i2c_get_chip(bus, dev_addr, 1, &dev);
95 	if (rc)
96 		return rc;
97 
98 	/*
99 	 * Read the header first then only read the other contents.
100 	 */
101 	rc = i2c_set_chip_offset_len(dev, 2);
102 	if (rc)
103 		return rc;
104 
105 	rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
106 	if (rc)
107 		return rc;
108 
109 	/* Corrupted data??? */
110 	if (hdr_read != header) {
111 		rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
112 		/*
113 		 * read the eeprom header using i2c again, but use only a
114 		 * 1 byte address (some legacy boards need this..)
115 		 */
116 		if (rc) {
117 			rc =  i2c_set_chip_offset_len(dev, 1);
118 			if (rc)
119 				return rc;
120 
121 			rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
122 		}
123 		if (rc)
124 			return rc;
125 	}
126 	if (hdr_read != header)
127 		return -1;
128 
129 	rc = dm_i2c_read(dev, 0, ep, size);
130 	if (rc)
131 		return rc;
132 #else
133 	u32 byte;
134 
135 	gpi2c_init();
136 	rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
137 	if (rc)
138 		return rc;
139 
140 	/*
141 	 * Read the header first then only read the other contents.
142 	 */
143 	byte = 2;
144 
145 	rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
146 	if (rc)
147 		return rc;
148 
149 	/* Corrupted data??? */
150 	if (hdr_read != header) {
151 		rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
152 		/*
153 		 * read the eeprom header using i2c again, but use only a
154 		 * 1 byte address (some legacy boards need this..)
155 		 */
156 		byte = 1;
157 		if (rc) {
158 			rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
159 				      4);
160 		}
161 		if (rc)
162 			return rc;
163 	}
164 	if (hdr_read != header)
165 		return -1;
166 
167 	rc = i2c_read(dev_addr, 0x0, byte, ep, size);
168 	if (rc)
169 		return rc;
170 #endif
171 	return 0;
172 }
173 
ti_i2c_eeprom_am_set(const char * name,const char * rev)174 int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
175 {
176 	struct ti_common_eeprom *ep;
177 
178 	if (!name || !rev)
179 		return -1;
180 
181 	ep = TI_EEPROM_DATA;
182 	if (ep->header == TI_EEPROM_HEADER_MAGIC)
183 		goto already_set;
184 
185 	/* Set to 0 all fields */
186 	memset(ep, 0, sizeof(*ep));
187 	strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
188 	strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
189 	/* Some dummy serial number to identify the platform */
190 	strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
191 	/* Mark it with a valid header */
192 	ep->header = TI_EEPROM_HEADER_MAGIC;
193 
194 already_set:
195 	return 0;
196 }
197 
ti_i2c_eeprom_am_get(int bus_addr,int dev_addr)198 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
199 {
200 	int rc;
201 	struct ti_am_eeprom am_ep;
202 	struct ti_common_eeprom *ep;
203 
204 	ep = TI_EEPROM_DATA;
205 #ifndef CONFIG_SPL_BUILD
206 	if (ep->header == TI_EEPROM_HEADER_MAGIC)
207 		return 0; /* EEPROM has already been read */
208 #endif
209 
210 	/* Initialize with a known bad marker for i2c fails.. */
211 	ep->header = TI_DEAD_EEPROM_MAGIC;
212 	ep->name[0] = 0x0;
213 	ep->version[0] = 0x0;
214 	ep->serial[0] = 0x0;
215 	ep->config[0] = 0x0;
216 
217 	rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
218 			       sizeof(am_ep), (uint8_t *)&am_ep);
219 	if (rc)
220 		return rc;
221 
222 	ep->header = am_ep.header;
223 	strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
224 	ti_eeprom_string_cleanup(ep->name);
225 
226 	/* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
227 	if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
228 	    am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
229 		strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
230 	else
231 		strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
232 	ti_eeprom_string_cleanup(ep->version);
233 	strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
234 	ti_eeprom_string_cleanup(ep->serial);
235 	strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
236 	ti_eeprom_string_cleanup(ep->config);
237 
238 	memcpy(ep->mac_addr, am_ep.mac_addr,
239 	       TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
240 
241 	return 0;
242 }
243 
ti_i2c_eeprom_dra7_get(int bus_addr,int dev_addr)244 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
245 {
246 	int rc, offset = 0;
247 	struct dra7_eeprom dra7_ep;
248 	struct ti_common_eeprom *ep;
249 
250 	ep = TI_EEPROM_DATA;
251 #ifndef CONFIG_SPL_BUILD
252 	if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
253 		return 0; /* EEPROM has already been read */
254 #endif
255 
256 	/* Initialize with a known bad marker for i2c fails.. */
257 	ep->header = TI_DEAD_EEPROM_MAGIC;
258 	ep->name[0] = 0x0;
259 	ep->version[0] = 0x0;
260 	ep->serial[0] = 0x0;
261 	ep->config[0] = 0x0;
262 	ep->emif1_size = 0;
263 	ep->emif2_size = 0;
264 
265 	rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
266 			       sizeof(dra7_ep), (uint8_t *)&dra7_ep);
267 	if (rc)
268 		return rc;
269 
270 	ep->header = dra7_ep.header;
271 	strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
272 	ti_eeprom_string_cleanup(ep->name);
273 
274 	offset = dra7_ep.version_major - 1;
275 
276 	/* Rev F is skipped */
277 	if (offset >= 5)
278 		offset = offset + 1;
279 	snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
280 		 'A' + offset, dra7_ep.version_minor);
281 	ti_eeprom_string_cleanup(ep->version);
282 	ep->emif1_size = (u64)dra7_ep.emif1_size;
283 	ep->emif2_size = (u64)dra7_ep.emif2_size;
284 	strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
285 	ti_eeprom_string_cleanup(ep->config);
286 
287 	return 0;
288 }
289 
ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record * record,struct ti_am6_eeprom * ep,char ** mac_addr,u8 mac_addr_max_cnt,u8 * mac_addr_cnt)290 static int ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record *record,
291 					  struct ti_am6_eeprom *ep,
292 					  char **mac_addr,
293 					  u8 mac_addr_max_cnt,
294 					  u8 *mac_addr_cnt)
295 {
296 	switch (record->header.id) {
297 	case TI_AM6_EEPROM_RECORD_BOARD_INFO:
298 		if (record->header.len != sizeof(record->data.board_info))
299 			return -EINVAL;
300 
301 		if (!ep)
302 			break;
303 
304 		/* Populate (and clean, if needed) the board name */
305 		strlcpy(ep->name, record->data.board_info.name,
306 			sizeof(ep->name));
307 		ti_eeprom_string_cleanup(ep->name);
308 
309 		/* Populate selected other fields from the board info record */
310 		strlcpy(ep->version, record->data.board_info.version,
311 			sizeof(ep->version));
312 		strlcpy(ep->software_revision,
313 			record->data.board_info.software_revision,
314 			sizeof(ep->software_revision));
315 		strlcpy(ep->serial, record->data.board_info.serial,
316 			sizeof(ep->serial));
317 		break;
318 	case TI_AM6_EEPROM_RECORD_MAC_INFO:
319 		if (record->header.len != sizeof(record->data.mac_info))
320 			return -EINVAL;
321 
322 		if (!mac_addr || !mac_addr_max_cnt)
323 			break;
324 
325 		*mac_addr_cnt = ((record->data.mac_info.mac_control &
326 				 TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK) >>
327 				 TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT) + 1;
328 
329 		/*
330 		 * The EEPROM can (but may not) hold a very large amount
331 		 * of MAC addresses, by far exceeding what we want/can store
332 		 * in the common memory array, so only grab what we can fit.
333 		 * Note that a value of 0 means 1 MAC address, and so on.
334 		 */
335 		*mac_addr_cnt = min(*mac_addr_cnt, mac_addr_max_cnt);
336 
337 		memcpy(mac_addr, record->data.mac_info.mac_addr,
338 		       *mac_addr_cnt * TI_EEPROM_HDR_ETH_ALEN);
339 		break;
340 	case 0x00:
341 		/* Illegal value... Fall through... */
342 	case 0xFF:
343 		/* Illegal value... Something went horribly wrong... */
344 		return -EINVAL;
345 	default:
346 		pr_warn("%s: Ignoring record id %u\n", __func__,
347 			record->header.id);
348 	}
349 
350 	return 0;
351 }
352 
ti_i2c_eeprom_am6_get(int bus_addr,int dev_addr,struct ti_am6_eeprom * ep,char ** mac_addr,u8 mac_addr_max_cnt,u8 * mac_addr_cnt)353 int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr,
354 					 struct ti_am6_eeprom *ep,
355 					 char **mac_addr,
356 					 u8 mac_addr_max_cnt,
357 					 u8 *mac_addr_cnt)
358 {
359 	struct udevice *dev;
360 	struct udevice *bus;
361 	unsigned int eeprom_addr;
362 	struct ti_am6_eeprom_record_board_id board_id;
363 	struct ti_am6_eeprom_record record;
364 	int rc;
365 
366 	/* Initialize with a known bad marker for i2c fails.. */
367 	memset(ep, 0, sizeof(*ep));
368 	ep->header = TI_DEAD_EEPROM_MAGIC;
369 
370 	/* Read the board ID record which is always the first EEPROM record */
371 	rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
372 			       sizeof(board_id), (uint8_t *)&board_id);
373 	if (rc)
374 		return rc;
375 
376 	if (board_id.header.id != TI_AM6_EEPROM_RECORD_BOARD_ID) {
377 		pr_err("%s: Invalid board ID record!\n", __func__);
378 		return -EINVAL;
379 	}
380 
381 	/* Establish DM handle to board config EEPROM */
382 	rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
383 	if (rc)
384 		return rc;
385 	rc = i2c_get_chip(bus, dev_addr, 1, &dev);
386 	if (rc)
387 		return rc;
388 
389 	ep->header = TI_EEPROM_HEADER_MAGIC;
390 
391 	/* Ready to parse TLV structure. Initialize variables... */
392 	*mac_addr_cnt = 0;
393 
394 	/*
395 	 * After the all-encompassing board ID record all other records follow
396 	 * a TLV-type scheme. Point to the first such record and then start
397 	 * parsing those one by one.
398 	 */
399 	eeprom_addr = sizeof(board_id);
400 
401 	while (true) {
402 		rc = dm_i2c_read(dev, eeprom_addr, (uint8_t *)&record.header,
403 				 sizeof(record.header));
404 		if (rc)
405 			return rc;
406 
407 		/*
408 		 * Check for end of list marker. If we reached it don't go
409 		 * any further and stop parsing right here.
410 		 */
411 		if (record.header.id == TI_AM6_EEPROM_RECORD_END_LIST)
412 			break;
413 
414 		eeprom_addr += sizeof(record.header);
415 
416 		debug("%s: dev_addr=0x%02x header.id=%u header.len=%u\n",
417 		      __func__, dev_addr, record.header.id,
418 		      record.header.len);
419 
420 		/* Read record into memory if it fits */
421 		if (record.header.len <= sizeof(record.data)) {
422 			rc = dm_i2c_read(dev, eeprom_addr,
423 					 (uint8_t *)&record.data,
424 					 record.header.len);
425 			if (rc)
426 				return rc;
427 
428 			/* Process record */
429 			rc = ti_i2c_eeprom_am6_parse_record(&record, ep,
430 							    mac_addr,
431 							    mac_addr_max_cnt,
432 							    mac_addr_cnt);
433 			if (rc) {
434 				pr_err("%s: EEPROM parsing error!\n", __func__);
435 				return rc;
436 			}
437 		} else {
438 			/*
439 			 * We may get here in case of larger records which
440 			 * are not yet understood.
441 			 */
442 			pr_err("%s: Ignoring record id %u\n", __func__,
443 			       record.header.id);
444 		}
445 
446 		eeprom_addr += record.header.len;
447 	}
448 
449 	return 0;
450 }
451 
ti_i2c_eeprom_am6_get_base(int bus_addr,int dev_addr)452 int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr)
453 {
454 	struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
455 	int ret;
456 
457 	/*
458 	 * Always execute EEPROM read by not allowing to bypass it during the
459 	 * first invocation of SPL which happens on the R5 core.
460 	 */
461 #if !(defined(CONFIG_SPL_BUILD) && defined(CONFIG_CPU_V7R))
462 	if (ep->header == TI_EEPROM_HEADER_MAGIC) {
463 		debug("%s: EEPROM has already been read\n", __func__);
464 		return 0;
465 	}
466 #endif
467 
468 	ret = ti_i2c_eeprom_am6_get(bus_addr, dev_addr, ep,
469 				    (char **)ep->mac_addr,
470 				    AM6_EEPROM_HDR_NO_OF_MAC_ADDR,
471 				    &ep->mac_addr_cnt);
472 	return ret;
473 }
474 
board_ti_is(char * name_tag)475 bool __maybe_unused board_ti_is(char *name_tag)
476 {
477 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
478 
479 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
480 		return false;
481 	return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
482 }
483 
board_ti_rev_is(char * rev_tag,int cmp_len)484 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
485 {
486 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
487 	int l;
488 
489 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
490 		return false;
491 
492 	l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
493 	return !strncmp(ep->version, rev_tag, l);
494 }
495 
board_ti_get_rev(void)496 char * __maybe_unused board_ti_get_rev(void)
497 {
498 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
499 
500 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
501 	return ep->version;
502 }
503 
board_ti_get_config(void)504 char * __maybe_unused board_ti_get_config(void)
505 {
506 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
507 
508 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
509 	return ep->config;
510 }
511 
board_ti_get_name(void)512 char * __maybe_unused board_ti_get_name(void)
513 {
514 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
515 
516 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
517 	return ep->name;
518 }
519 
520 void __maybe_unused
board_ti_get_eth_mac_addr(int index,u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])521 board_ti_get_eth_mac_addr(int index,
522 			  u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
523 {
524 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
525 
526 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
527 		goto fail;
528 
529 	if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
530 		goto fail;
531 
532 	memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
533 	return;
534 
535 fail:
536 	memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
537 }
538 
539 void __maybe_unused
board_ti_am6_get_eth_mac_addr(int index,u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])540 board_ti_am6_get_eth_mac_addr(int index,
541 			      u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
542 {
543 	struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
544 
545 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
546 		goto fail;
547 
548 	if (index < 0 || index >= ep->mac_addr_cnt)
549 		goto fail;
550 
551 	memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
552 	return;
553 
554 fail:
555 	memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
556 }
557 
board_ti_get_emif1_size(void)558 u64 __maybe_unused board_ti_get_emif1_size(void)
559 {
560 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
561 
562 	if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
563 		return 0;
564 
565 	return ep->emif1_size;
566 }
567 
board_ti_get_emif2_size(void)568 u64 __maybe_unused board_ti_get_emif2_size(void)
569 {
570 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
571 
572 	if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
573 		return 0;
574 
575 	return ep->emif2_size;
576 }
577 
set_board_info_env(char * name)578 void __maybe_unused set_board_info_env(char *name)
579 {
580 	char *unknown = "unknown";
581 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
582 
583 	if (name)
584 		env_set("board_name", name);
585 	else if (ep->name)
586 		env_set("board_name", ep->name);
587 	else
588 		env_set("board_name", unknown);
589 
590 	if (ep->version)
591 		env_set("board_rev", ep->version);
592 	else
593 		env_set("board_rev", unknown);
594 
595 	if (ep->serial)
596 		env_set("board_serial", ep->serial);
597 	else
598 		env_set("board_serial", unknown);
599 }
600 
set_board_info_env_am6(char * name)601 void __maybe_unused set_board_info_env_am6(char *name)
602 {
603 	char *unknown = "unknown";
604 	struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
605 
606 	if (name)
607 		env_set("board_name", name);
608 	else if (ep->name)
609 		env_set("board_name", ep->name);
610 	else
611 		env_set("board_name", unknown);
612 
613 	if (ep->version)
614 		env_set("board_rev", ep->version);
615 	else
616 		env_set("board_rev", unknown);
617 
618 	if (ep->software_revision)
619 		env_set("board_software_revision", ep->software_revision);
620 	else
621 		env_set("board_software_revision", unknown);
622 
623 	if (ep->serial)
624 		env_set("board_serial", ep->serial);
625 	else
626 		env_set("board_serial", unknown);
627 }
628 
mac_to_u64(u8 mac[6])629 static u64 mac_to_u64(u8 mac[6])
630 {
631 	int i;
632 	u64 addr = 0;
633 
634 	for (i = 0; i < 6; i++) {
635 		addr <<= 8;
636 		addr |= mac[i];
637 	}
638 
639 	return addr;
640 }
641 
u64_to_mac(u64 addr,u8 mac[6])642 static void u64_to_mac(u64 addr, u8 mac[6])
643 {
644 	mac[5] = addr;
645 	mac[4] = addr >> 8;
646 	mac[3] = addr >> 16;
647 	mac[2] = addr >> 24;
648 	mac[1] = addr >> 32;
649 	mac[0] = addr >> 40;
650 }
651 
board_ti_set_ethaddr(int index)652 void board_ti_set_ethaddr(int index)
653 {
654 	uint8_t mac_addr[6];
655 	int i;
656 	u64 mac1, mac2;
657 	u8 mac_addr1[6], mac_addr2[6];
658 	int num_macs;
659 	/*
660 	 * Export any Ethernet MAC addresses from EEPROM.
661 	 * The 2 MAC addresses in EEPROM define the address range.
662 	 */
663 	board_ti_get_eth_mac_addr(0, mac_addr1);
664 	board_ti_get_eth_mac_addr(1, mac_addr2);
665 
666 	if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
667 		mac1 = mac_to_u64(mac_addr1);
668 		mac2 = mac_to_u64(mac_addr2);
669 
670 		/* must contain an address range */
671 		num_macs = mac2 - mac1 + 1;
672 		if (num_macs <= 0)
673 			return;
674 
675 		if (num_macs > 50) {
676 			printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
677 			       __func__, num_macs);
678 			num_macs = 50;
679 		}
680 
681 		for (i = 0; i < num_macs; i++) {
682 			u64_to_mac(mac1 + i, mac_addr);
683 			if (is_valid_ethaddr(mac_addr)) {
684 				eth_env_set_enetaddr_by_index("eth", i + index,
685 							      mac_addr);
686 			}
687 		}
688 	}
689 }
690 
board_ti_am6_set_ethaddr(int index,int count)691 void board_ti_am6_set_ethaddr(int index, int count)
692 {
693 	u8 mac_addr[6];
694 	int i;
695 
696 	for (i = 0; i < count; i++) {
697 		board_ti_am6_get_eth_mac_addr(i, mac_addr);
698 		if (is_valid_ethaddr(mac_addr))
699 			eth_env_set_enetaddr_by_index("eth", i + index,
700 						      mac_addr);
701 	}
702 }
703 
board_ti_was_eeprom_read(void)704 bool __maybe_unused board_ti_was_eeprom_read(void)
705 {
706 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
707 
708 	if (ep->header == TI_EEPROM_HEADER_MAGIC)
709 		return true;
710 	else
711 		return false;
712 }
713