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