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