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/
11 #include <asm/omap_common.h>
12 #include <dm/uclass.h>
15 #include "board_detect.h"
17 #if defined(CONFIG_DM_I2C_COMPAT)
19 * ti_i2c_set_alen - Set chip's i2c address length
20 * @bus_addr - I2C bus number
21 * @dev_addr - I2C eeprom id
22 * @alen - I2C address length in bytes
24 * DM_I2C by default sets the address length to be used to 1. This
25 * function allows this address length to be changed to match the
26 * eeprom used for board detection.
28 int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
34 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
37 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
40 rc = i2c_set_chip_offset_len(dev, alen);
47 int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
53 #if !defined(CONFIG_DM_I2C) || defined(CONFIG_DM_I2C_COMPAT)
55 * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
56 * @i2c_bus: i2c bus number to initialize
57 * @dev_addr: Device address to probe for
59 * Return: 0 on success or corresponding error on failure.
61 static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
66 rc = i2c_set_bus_num(i2c_bus);
71 return i2c_probe(dev_addr);
75 * ti_i2c_eeprom_read - Read data from an EEPROM
76 * @dev_addr: The device address of the EEPROM
77 * @offset: Offset to start reading in the EEPROM
78 * @ep: Pointer to a buffer to read into
79 * @epsize: Size of buffer
81 * Return: 0 on success or corresponding result of i2c_read
83 static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
84 uchar *ep, int epsize)
86 int bus_num, rc, alen;
88 bus_num = i2c_get_bus_num();
92 rc = ti_i2c_set_alen(bus_num, dev_addr, alen);
96 return i2c_read(dev_addr, offset, alen, ep, epsize);
101 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
102 * @s: eeprom string (should be NULL terminated)
104 * Some Board manufacturers do not add a NULL termination at the
105 * end of string, instead some binary information is kludged in, hence
106 * convert the string to just printable characters of ASCII chart.
108 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
113 for (i = 0; i < l; i++, s++)
114 if (*s < ' ' || *s > '~') {
120 __weak void gpi2c_init(void)
124 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
125 u32 header, u32 size, uint8_t *ep)
130 #if defined(CONFIG_DM_I2C) && !defined(CONFIG_DM_I2C_COMPAT)
134 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
137 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
142 * Read the header first then only read the other contents.
144 rc = i2c_set_chip_offset_len(dev, 2);
148 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
152 /* Corrupted data??? */
153 if (hdr_read != header) {
154 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
156 * read the eeprom header using i2c again, but use only a
157 * 1 byte address (some legacy boards need this..)
160 rc = i2c_set_chip_offset_len(dev, 1);
164 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
169 if (hdr_read != header)
172 rc = dm_i2c_read(dev, 0, ep, size);
179 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
184 * Read the header first then only read the other contents.
188 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
192 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
196 /* Corrupted data??? */
197 if (hdr_read != header) {
198 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
200 * read the eeprom header using i2c again, but use only a
201 * 1 byte address (some legacy boards need this..)
205 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
209 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
215 if (hdr_read != header)
218 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
225 int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
227 struct ti_common_eeprom *ep;
233 if (ep->header == TI_EEPROM_HEADER_MAGIC)
236 /* Set to 0 all fields */
237 memset(ep, 0, sizeof(*ep));
238 strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
239 strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
240 /* Some dummy serial number to identify the platform */
241 strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
242 /* Mark it with a valid header */
243 ep->header = TI_EEPROM_HEADER_MAGIC;
249 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
252 struct ti_am_eeprom am_ep;
253 struct ti_common_eeprom *ep;
256 #ifndef CONFIG_SPL_BUILD
257 if (ep->header == TI_EEPROM_HEADER_MAGIC)
258 return 0; /* EEPROM has already been read */
261 /* Initialize with a known bad marker for i2c fails.. */
262 ep->header = TI_DEAD_EEPROM_MAGIC;
264 ep->version[0] = 0x0;
268 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
269 sizeof(am_ep), (uint8_t *)&am_ep);
273 ep->header = am_ep.header;
274 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
275 ti_eeprom_string_cleanup(ep->name);
277 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
278 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
279 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
280 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
282 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
283 ti_eeprom_string_cleanup(ep->version);
284 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
285 ti_eeprom_string_cleanup(ep->serial);
286 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
287 ti_eeprom_string_cleanup(ep->config);
289 memcpy(ep->mac_addr, am_ep.mac_addr,
290 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
295 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
298 struct dra7_eeprom dra7_ep;
299 struct ti_common_eeprom *ep;
302 #ifndef CONFIG_SPL_BUILD
303 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
304 return 0; /* EEPROM has already been read */
307 /* Initialize with a known bad marker for i2c fails.. */
308 ep->header = TI_DEAD_EEPROM_MAGIC;
310 ep->version[0] = 0x0;
316 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
317 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
321 ep->header = dra7_ep.header;
322 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
323 ti_eeprom_string_cleanup(ep->name);
325 offset = dra7_ep.version_major - 1;
327 /* Rev F is skipped */
330 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
331 'A' + offset, dra7_ep.version_minor);
332 ti_eeprom_string_cleanup(ep->version);
333 ep->emif1_size = (u64)dra7_ep.emif1_size;
334 ep->emif2_size = (u64)dra7_ep.emif2_size;
335 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
336 ti_eeprom_string_cleanup(ep->config);
341 bool __maybe_unused board_ti_is(char *name_tag)
343 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
345 if (ep->header == TI_DEAD_EEPROM_MAGIC)
347 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
350 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
352 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
355 if (ep->header == TI_DEAD_EEPROM_MAGIC)
358 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
359 return !strncmp(ep->version, rev_tag, l);
362 char * __maybe_unused board_ti_get_rev(void)
364 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
366 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
370 char * __maybe_unused board_ti_get_config(void)
372 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
374 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
378 char * __maybe_unused board_ti_get_name(void)
380 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
382 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
387 board_ti_get_eth_mac_addr(int index,
388 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
390 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
392 if (ep->header == TI_DEAD_EEPROM_MAGIC)
395 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
398 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
402 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
405 u64 __maybe_unused board_ti_get_emif1_size(void)
407 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
409 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
412 return ep->emif1_size;
415 u64 __maybe_unused board_ti_get_emif2_size(void)
417 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
419 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
422 return ep->emif2_size;
425 void __maybe_unused set_board_info_env(char *name)
427 char *unknown = "unknown";
428 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
431 env_set("board_name", name);
433 env_set("board_name", ep->name);
435 env_set("board_name", unknown);
438 env_set("board_rev", ep->version);
440 env_set("board_rev", unknown);
443 env_set("board_serial", ep->serial);
445 env_set("board_serial", unknown);
448 static u64 mac_to_u64(u8 mac[6])
453 for (i = 0; i < 6; i++) {
461 static void u64_to_mac(u64 addr, u8 mac[6])
471 void board_ti_set_ethaddr(int index)
476 u8 mac_addr1[6], mac_addr2[6];
479 * Export any Ethernet MAC addresses from EEPROM.
480 * The 2 MAC addresses in EEPROM define the address range.
482 board_ti_get_eth_mac_addr(0, mac_addr1);
483 board_ti_get_eth_mac_addr(1, mac_addr2);
485 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
486 mac1 = mac_to_u64(mac_addr1);
487 mac2 = mac_to_u64(mac_addr2);
489 /* must contain an address range */
490 num_macs = mac2 - mac1 + 1;
495 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
500 for (i = 0; i < num_macs; i++) {
501 u64_to_mac(mac1 + i, mac_addr);
502 if (is_valid_ethaddr(mac_addr)) {
503 eth_env_set_enetaddr_by_index("eth", i + index,
510 bool __maybe_unused board_ti_was_eeprom_read(void)
512 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
514 if (ep->header == TI_EEPROM_HEADER_MAGIC)