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)
54 * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
55 * @i2c_bus: i2c bus number to initialize
56 * @dev_addr: Device address to probe for
58 * Return: 0 on success or corresponding error on failure.
60 static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
65 rc = i2c_set_bus_num(i2c_bus);
70 return i2c_probe(dev_addr);
74 * ti_i2c_eeprom_read - Read data from an EEPROM
75 * @dev_addr: The device address of the EEPROM
76 * @offset: Offset to start reading in the EEPROM
77 * @ep: Pointer to a buffer to read into
78 * @epsize: Size of buffer
80 * Return: 0 on success or corresponding result of i2c_read
82 static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
83 uchar *ep, int epsize)
85 int bus_num, rc, alen;
87 bus_num = i2c_get_bus_num();
91 rc = ti_i2c_set_alen(bus_num, dev_addr, alen);
95 return i2c_read(dev_addr, offset, alen, ep, epsize);
99 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
100 * @s: eeprom string (should be NULL terminated)
102 * Some Board manufacturers do not add a NULL termination at the
103 * end of string, instead some binary information is kludged in, hence
104 * convert the string to just printable characters of ASCII chart.
106 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
111 for (i = 0; i < l; i++, s++)
112 if (*s < ' ' || *s > '~') {
118 __weak void gpi2c_init(void)
122 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
123 u32 header, u32 size, uint8_t *ep)
129 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
134 * Read the header first then only read the other contents.
138 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
142 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
146 /* Corrupted data??? */
147 if (hdr_read != header) {
148 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
150 * read the eeprom header using i2c again, but use only a
151 * 1 byte address (some legacy boards need this..)
155 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
159 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
165 if (hdr_read != header)
168 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
175 int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
177 struct ti_common_eeprom *ep;
183 if (ep->header == TI_EEPROM_HEADER_MAGIC)
186 /* Set to 0 all fields */
187 memset(ep, 0, sizeof(*ep));
188 strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
189 strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
190 /* Some dummy serial number to identify the platform */
191 strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
192 /* Mark it with a valid header */
193 ep->header = TI_EEPROM_HEADER_MAGIC;
199 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
202 struct ti_am_eeprom am_ep;
203 struct ti_common_eeprom *ep;
206 #ifndef CONFIG_SPL_BUILD
207 if (ep->header == TI_EEPROM_HEADER_MAGIC)
208 return 0; /* EEPROM has already been read */
211 /* Initialize with a known bad marker for i2c fails.. */
212 ep->header = TI_DEAD_EEPROM_MAGIC;
214 ep->version[0] = 0x0;
218 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
219 sizeof(am_ep), (uint8_t *)&am_ep);
223 ep->header = am_ep.header;
224 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
225 ti_eeprom_string_cleanup(ep->name);
227 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
228 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
229 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
230 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
232 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
233 ti_eeprom_string_cleanup(ep->version);
234 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
235 ti_eeprom_string_cleanup(ep->serial);
236 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
237 ti_eeprom_string_cleanup(ep->config);
239 memcpy(ep->mac_addr, am_ep.mac_addr,
240 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
245 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
248 struct dra7_eeprom dra7_ep;
249 struct ti_common_eeprom *ep;
252 #ifndef CONFIG_SPL_BUILD
253 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
254 return 0; /* EEPROM has already been read */
257 /* Initialize with a known bad marker for i2c fails.. */
258 ep->header = TI_DEAD_EEPROM_MAGIC;
260 ep->version[0] = 0x0;
266 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
267 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
271 ep->header = dra7_ep.header;
272 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
273 ti_eeprom_string_cleanup(ep->name);
275 offset = dra7_ep.version_major - 1;
277 /* Rev F is skipped */
280 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
281 'A' + offset, dra7_ep.version_minor);
282 ti_eeprom_string_cleanup(ep->version);
283 ep->emif1_size = (u64)dra7_ep.emif1_size;
284 ep->emif2_size = (u64)dra7_ep.emif2_size;
285 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
286 ti_eeprom_string_cleanup(ep->config);
291 bool __maybe_unused board_ti_is(char *name_tag)
293 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
295 if (ep->header == TI_DEAD_EEPROM_MAGIC)
297 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
300 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
302 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
305 if (ep->header == TI_DEAD_EEPROM_MAGIC)
308 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
309 return !strncmp(ep->version, rev_tag, l);
312 char * __maybe_unused board_ti_get_rev(void)
314 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
316 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
320 char * __maybe_unused board_ti_get_config(void)
322 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
324 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
328 char * __maybe_unused board_ti_get_name(void)
330 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
332 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
337 board_ti_get_eth_mac_addr(int index,
338 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
340 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
342 if (ep->header == TI_DEAD_EEPROM_MAGIC)
345 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
348 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
352 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
355 u64 __maybe_unused board_ti_get_emif1_size(void)
357 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
359 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
362 return ep->emif1_size;
365 u64 __maybe_unused board_ti_get_emif2_size(void)
367 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
369 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
372 return ep->emif2_size;
375 void __maybe_unused set_board_info_env(char *name)
377 char *unknown = "unknown";
378 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
381 env_set("board_name", name);
383 env_set("board_name", ep->name);
385 env_set("board_name", unknown);
388 env_set("board_rev", ep->version);
390 env_set("board_rev", unknown);
393 env_set("board_serial", ep->serial);
395 env_set("board_serial", unknown);
398 static u64 mac_to_u64(u8 mac[6])
403 for (i = 0; i < 6; i++) {
411 static void u64_to_mac(u64 addr, u8 mac[6])
421 void board_ti_set_ethaddr(int index)
426 u8 mac_addr1[6], mac_addr2[6];
429 * Export any Ethernet MAC addresses from EEPROM.
430 * The 2 MAC addresses in EEPROM define the address range.
432 board_ti_get_eth_mac_addr(0, mac_addr1);
433 board_ti_get_eth_mac_addr(1, mac_addr2);
435 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
436 mac1 = mac_to_u64(mac_addr1);
437 mac2 = mac_to_u64(mac_addr2);
439 /* must contain an address range */
440 num_macs = mac2 - mac1 + 1;
445 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
450 for (i = 0; i < num_macs; i++) {
451 u64_to_mac(mac1 + i, mac_addr);
452 if (is_valid_ethaddr(mac_addr)) {
453 eth_env_set_enetaddr_by_index("eth", i + index,
460 bool __maybe_unused board_ti_was_eeprom_read(void)
462 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
464 if (ep->header == TI_EEPROM_HEADER_MAGIC)