1 // SPDX-License-Identifier: GPL-2.0+
3 * Library to support early TI EVM EEPROM handling
5 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
14 #include <asm/arch/hardware.h>
15 #include <asm/omap_common.h>
16 #include <dm/uclass.h>
23 #include "board_detect.h"
25 #if !CONFIG_IS_ENABLED(DM_I2C)
27 * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
28 * @i2c_bus: i2c bus number to initialize
29 * @dev_addr: Device address to probe for
31 * Return: 0 on success or corresponding error on failure.
33 static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
38 rc = i2c_set_bus_num(i2c_bus);
43 return i2c_probe(dev_addr);
47 * ti_i2c_eeprom_read - Read data from an EEPROM
48 * @dev_addr: The device address of the EEPROM
49 * @offset: Offset to start reading in the EEPROM
50 * @ep: Pointer to a buffer to read into
51 * @epsize: Size of buffer
53 * Return: 0 on success or corresponding result of i2c_read
55 static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
56 uchar *ep, int epsize)
58 return i2c_read(dev_addr, offset, 2, ep, epsize);
63 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
64 * @s: eeprom string (should be NULL terminated)
66 * Some Board manufacturers do not add a NULL termination at the
67 * end of string, instead some binary information is kludged in, hence
68 * convert the string to just printable characters of ASCII chart.
70 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
75 for (i = 0; i < l; i++, s++)
76 if (*s < ' ' || *s > '~') {
82 __weak void gpi2c_init(void)
86 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
87 u32 header, u32 size, uint8_t *ep)
91 bool one_byte_addressing = true;
93 #if CONFIG_IS_ENABLED(DM_I2C)
97 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
100 rc = dm_i2c_probe(bus, dev_addr, 0, &dev);
105 * Read the header first then only read the other contents.
107 rc = i2c_set_chip_offset_len(dev, 1);
112 * Skip checking result here since this could be a valid i2c read fail
113 * on some boards that use 2 byte addressing.
114 * We must allow for fall through to check the data if 2 byte
117 (void)dm_i2c_read(dev, 0, ep, size);
119 if (*((u32 *)ep) != header)
120 one_byte_addressing = false;
123 * Handle case of bad 2 byte eeproms that responds to 1 byte addressing
124 * but gets stuck in const addressing when read requests are performed
125 * on offsets. We perform an offset test to make sure it is not a 2 byte
126 * eeprom that works with 1 byte addressing but just without an offset
129 rc = dm_i2c_read(dev, 0x1, &offset_test, sizeof(offset_test));
131 if (*((u32 *)ep) != (header & 0xFF))
132 one_byte_addressing = false;
134 /* Corrupted data??? */
135 if (!one_byte_addressing) {
137 * read the eeprom header using i2c again, but use only a
138 * 2 byte address (some newer boards need this..)
140 rc = i2c_set_chip_offset_len(dev, 2);
144 rc = dm_i2c_read(dev, 0, ep, size);
148 if (*((u32 *)ep) != header)
154 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
159 * Read the header first then only read the other contents.
164 * Skip checking result here since this could be a valid i2c read fail
165 * on some boards that use 2 byte addressing.
166 * We must allow for fall through to check the data if 2 byte
169 (void)i2c_read(dev_addr, 0x0, byte, ep, size);
171 if (*((u32 *)ep) != header)
172 one_byte_addressing = false;
175 * Handle case of bad 2 byte eeproms that responds to 1 byte addressing
176 * but gets stuck in const addressing when read requests are performed
177 * on offsets. We perform an offset test to make sure it is not a 2 byte
178 * eeprom that works with 1 byte addressing but just without an offset
181 rc = i2c_read(dev_addr, 0x1, byte, &offset_test, sizeof(offset_test));
183 if (*((u32 *)ep) != (header & 0xFF))
184 one_byte_addressing = false;
186 /* Corrupted data??? */
187 if (!one_byte_addressing) {
189 * read the eeprom header using i2c again, but use only a
190 * 2 byte address (some newer boards need this..)
193 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
197 if (*((u32 *)ep) != header)
203 int __maybe_unused ti_emmc_boardid_get(void)
208 struct ti_common_eeprom *ep;
209 struct ti_am_eeprom brdid;
210 struct blk_desc *bdesc;
214 if (ep->header == TI_EEPROM_HEADER_MAGIC)
215 return 0; /* EEPROM has already been read */
217 /* Initialize with a known bad marker for emmc fails.. */
218 ep->header = TI_DEAD_EEPROM_MAGIC;
220 ep->version[0] = 0x0;
224 /* uclass object initialization */
225 rc = mmc_initialize(NULL);
229 /* Set device to /dev/mmcblk1 */
230 rc = uclass_get_device(UCLASS_MMC, 1, &dev);
234 /* Grab the mmc device */
235 mmc = mmc_get_mmc_dev(dev);
239 /* mmc hardware initialization routine */
242 /* Set partition to /dev/mmcblk1boot1 */
243 rc = mmc_switch_part(mmc, 2);
247 buffer = malloc(mmc->read_bl_len);
251 bdesc = mmc_get_blk_desc(mmc);
253 /* blk_dread returns the number of blocks read*/
254 if (blk_dread(bdesc, 0L, 1, buffer) != 1) {
259 memcpy(&brdid, buffer, sizeof(brdid));
261 /* Write out the ep struct values */
262 ep->header = brdid.header;
263 strlcpy(ep->name, brdid.name, TI_EEPROM_HDR_NAME_LEN + 1);
264 ti_eeprom_string_cleanup(ep->name);
265 strlcpy(ep->version, brdid.version, TI_EEPROM_HDR_REV_LEN + 1);
266 ti_eeprom_string_cleanup(ep->version);
267 strlcpy(ep->serial, brdid.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
268 ti_eeprom_string_cleanup(ep->serial);
276 int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
278 struct ti_common_eeprom *ep;
284 if (ep->header == TI_EEPROM_HEADER_MAGIC)
287 /* Set to 0 all fields */
288 memset(ep, 0, sizeof(*ep));
289 strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
290 strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
291 /* Some dummy serial number to identify the platform */
292 strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
293 /* Mark it with a valid header */
294 ep->header = TI_EEPROM_HEADER_MAGIC;
300 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
303 struct ti_am_eeprom am_ep;
304 struct ti_common_eeprom *ep;
307 #ifndef CONFIG_SPL_BUILD
308 if (ep->header == TI_EEPROM_HEADER_MAGIC)
309 return 0; /* EEPROM has already been read */
312 /* Initialize with a known bad marker for i2c fails.. */
313 ep->header = TI_DEAD_EEPROM_MAGIC;
315 ep->version[0] = 0x0;
319 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
320 sizeof(am_ep), (uint8_t *)&am_ep);
324 ep->header = am_ep.header;
325 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
326 ti_eeprom_string_cleanup(ep->name);
328 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
329 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
330 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
331 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
333 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
334 ti_eeprom_string_cleanup(ep->version);
335 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
336 ti_eeprom_string_cleanup(ep->serial);
337 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
338 ti_eeprom_string_cleanup(ep->config);
340 memcpy(ep->mac_addr, am_ep.mac_addr,
341 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
346 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
349 struct dra7_eeprom dra7_ep;
350 struct ti_common_eeprom *ep;
353 #ifndef CONFIG_SPL_BUILD
354 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
355 return 0; /* EEPROM has already been read */
358 /* Initialize with a known bad marker for i2c fails.. */
359 ep->header = TI_DEAD_EEPROM_MAGIC;
361 ep->version[0] = 0x0;
367 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
368 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
372 ep->header = dra7_ep.header;
373 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
374 ti_eeprom_string_cleanup(ep->name);
376 offset = dra7_ep.version_major - 1;
378 /* Rev F is skipped */
381 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
382 'A' + offset, dra7_ep.version_minor);
383 ti_eeprom_string_cleanup(ep->version);
384 ep->emif1_size = (u64)dra7_ep.emif1_size;
385 ep->emif2_size = (u64)dra7_ep.emif2_size;
386 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
387 ti_eeprom_string_cleanup(ep->config);
392 static int ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record *record,
393 struct ti_am6_eeprom *ep,
398 switch (record->header.id) {
399 case TI_AM6_EEPROM_RECORD_BOARD_INFO:
400 if (record->header.len != sizeof(record->data.board_info))
406 /* Populate (and clean, if needed) the board name */
407 strlcpy(ep->name, record->data.board_info.name,
409 ti_eeprom_string_cleanup(ep->name);
411 /* Populate selected other fields from the board info record */
412 strlcpy(ep->version, record->data.board_info.version,
413 sizeof(ep->version));
414 strlcpy(ep->software_revision,
415 record->data.board_info.software_revision,
416 sizeof(ep->software_revision));
417 strlcpy(ep->serial, record->data.board_info.serial,
420 case TI_AM6_EEPROM_RECORD_MAC_INFO:
421 if (record->header.len != sizeof(record->data.mac_info))
424 if (!mac_addr || !mac_addr_max_cnt)
427 *mac_addr_cnt = ((record->data.mac_info.mac_control &
428 TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK) >>
429 TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT) + 1;
432 * The EEPROM can (but may not) hold a very large amount
433 * of MAC addresses, by far exceeding what we want/can store
434 * in the common memory array, so only grab what we can fit.
435 * Note that a value of 0 means 1 MAC address, and so on.
437 *mac_addr_cnt = min(*mac_addr_cnt, mac_addr_max_cnt);
439 memcpy(mac_addr, record->data.mac_info.mac_addr,
440 *mac_addr_cnt * TI_EEPROM_HDR_ETH_ALEN);
443 /* Illegal value... Fall through... */
445 /* Illegal value... Something went horribly wrong... */
448 pr_warn("%s: Ignoring record id %u\n", __func__,
455 int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr,
456 struct ti_am6_eeprom *ep,
463 unsigned int eeprom_addr;
464 struct ti_am6_eeprom_record_board_id board_id;
465 struct ti_am6_eeprom_record record;
467 int consecutive_bad_records = 0;
469 /* Initialize with a known bad marker for i2c fails.. */
470 memset(ep, 0, sizeof(*ep));
471 ep->header = TI_DEAD_EEPROM_MAGIC;
473 /* Read the board ID record which is always the first EEPROM record */
474 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
475 sizeof(board_id), (uint8_t *)&board_id);
479 if (board_id.header.id != TI_AM6_EEPROM_RECORD_BOARD_ID) {
480 pr_err("%s: Invalid board ID record!\n", __func__);
484 /* Establish DM handle to board config EEPROM */
485 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
488 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
492 ep->header = TI_EEPROM_HEADER_MAGIC;
494 /* Ready to parse TLV structure. Initialize variables... */
498 * After the all-encompassing board ID record all other records follow
499 * a TLV-type scheme. Point to the first such record and then start
500 * parsing those one by one.
502 eeprom_addr = sizeof(board_id);
504 while (consecutive_bad_records < 10) {
505 rc = dm_i2c_read(dev, eeprom_addr, (uint8_t *)&record.header,
506 sizeof(record.header));
511 * Check for end of list marker. If we reached it don't go
512 * any further and stop parsing right here.
514 if (record.header.id == TI_AM6_EEPROM_RECORD_END_LIST)
517 eeprom_addr += sizeof(record.header);
519 debug("%s: dev_addr=0x%02x header.id=%u header.len=%u\n",
520 __func__, dev_addr, record.header.id,
523 /* Read record into memory if it fits */
524 if (record.header.len <= sizeof(record.data)) {
525 rc = dm_i2c_read(dev, eeprom_addr,
526 (uint8_t *)&record.data,
532 rc = ti_i2c_eeprom_am6_parse_record(&record, ep,
537 pr_err("%s: EEPROM parsing error!\n", __func__);
540 consecutive_bad_records = 0;
543 * We may get here in case of larger records which
544 * are not yet understood.
546 pr_err("%s: Ignoring record id %u\n", __func__,
548 consecutive_bad_records++;
551 eeprom_addr += record.header.len;
557 int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr)
559 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
563 * Always execute EEPROM read by not allowing to bypass it during the
564 * first invocation of SPL which happens on the R5 core.
566 #if !(defined(CONFIG_SPL_BUILD) && defined(CONFIG_CPU_V7R))
567 if (ep->header == TI_EEPROM_HEADER_MAGIC) {
568 debug("%s: EEPROM has already been read\n", __func__);
573 ret = ti_i2c_eeprom_am6_get(bus_addr, dev_addr, ep,
574 (char **)ep->mac_addr,
575 AM6_EEPROM_HDR_NO_OF_MAC_ADDR,
580 bool __maybe_unused board_ti_k3_is(char *name_tag)
582 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
584 if (ep->header == TI_DEAD_EEPROM_MAGIC)
586 return !strncmp(ep->name, name_tag, AM6_EEPROM_HDR_NAME_LEN);
589 bool __maybe_unused board_ti_is(char *name_tag)
591 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
593 if (ep->header == TI_DEAD_EEPROM_MAGIC)
595 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
598 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
600 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
603 if (ep->header == TI_DEAD_EEPROM_MAGIC)
606 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
607 return !strncmp(ep->version, rev_tag, l);
610 char * __maybe_unused board_ti_get_rev(void)
612 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
614 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
618 char * __maybe_unused board_ti_get_config(void)
620 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
622 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
626 char * __maybe_unused board_ti_get_name(void)
628 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
630 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
635 board_ti_get_eth_mac_addr(int index,
636 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
638 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
640 if (ep->header == TI_DEAD_EEPROM_MAGIC)
643 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
646 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
650 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
654 board_ti_am6_get_eth_mac_addr(int index,
655 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
657 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
659 if (ep->header == TI_DEAD_EEPROM_MAGIC)
662 if (index < 0 || index >= ep->mac_addr_cnt)
665 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
669 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
672 u64 __maybe_unused board_ti_get_emif1_size(void)
674 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
676 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
679 return ep->emif1_size;
682 u64 __maybe_unused board_ti_get_emif2_size(void)
684 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
686 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
689 return ep->emif2_size;
692 void __maybe_unused set_board_info_env(char *name)
694 char *unknown = "unknown";
695 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
698 env_set("board_name", name);
699 else if (strlen(ep->name) != 0)
700 env_set("board_name", ep->name);
702 env_set("board_name", unknown);
704 if (strlen(ep->version) != 0)
705 env_set("board_rev", ep->version);
707 env_set("board_rev", unknown);
709 if (strlen(ep->serial) != 0)
710 env_set("board_serial", ep->serial);
712 env_set("board_serial", unknown);
715 void __maybe_unused set_board_info_env_am6(char *name)
717 char *unknown = "unknown";
718 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
721 env_set("board_name", name);
722 else if (strlen(ep->name) != 0)
723 env_set("board_name", ep->name);
725 env_set("board_name", unknown);
727 if (strlen(ep->version) != 0)
728 env_set("board_rev", ep->version);
730 env_set("board_rev", unknown);
732 if (strlen(ep->software_revision) != 0)
733 env_set("board_software_revision", ep->software_revision);
735 env_set("board_software_revision", unknown);
737 if (strlen(ep->serial) != 0)
738 env_set("board_serial", ep->serial);
740 env_set("board_serial", unknown);
743 static u64 mac_to_u64(u8 mac[6])
748 for (i = 0; i < 6; i++) {
756 static void u64_to_mac(u64 addr, u8 mac[6])
766 void board_ti_set_ethaddr(int index)
771 u8 mac_addr1[6], mac_addr2[6];
774 * Export any Ethernet MAC addresses from EEPROM.
775 * The 2 MAC addresses in EEPROM define the address range.
777 board_ti_get_eth_mac_addr(0, mac_addr1);
778 board_ti_get_eth_mac_addr(1, mac_addr2);
780 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
781 mac1 = mac_to_u64(mac_addr1);
782 mac2 = mac_to_u64(mac_addr2);
784 /* must contain an address range */
785 num_macs = mac2 - mac1 + 1;
790 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
795 for (i = 0; i < num_macs; i++) {
796 u64_to_mac(mac1 + i, mac_addr);
797 if (is_valid_ethaddr(mac_addr)) {
798 eth_env_set_enetaddr_by_index("eth", i + index,
805 void board_ti_am6_set_ethaddr(int index, int count)
810 for (i = 0; i < count; i++) {
811 board_ti_am6_get_eth_mac_addr(i, mac_addr);
812 if (is_valid_ethaddr(mac_addr))
813 eth_env_set_enetaddr_by_index("eth", i + index,
818 bool __maybe_unused board_ti_was_eeprom_read(void)
820 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
822 if (ep->header == TI_EEPROM_HEADER_MAGIC)