2 * Library to support early TI EVM EEPROM handling
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/omap_common.h>
13 #include <dm/uclass.h>
16 #include "board_detect.h"
18 #if defined(CONFIG_DM_I2C_COMPAT)
20 * ti_i2c_set_alen - Set chip's i2c address length
21 * @bus_addr - I2C bus number
22 * @dev_addr - I2C eeprom id
23 * @alen - I2C address length in bytes
25 * DM_I2C by default sets the address length to be used to 1. This
26 * function allows this address length to be changed to match the
27 * eeprom used for board detection.
29 int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
35 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
38 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
41 rc = i2c_set_chip_offset_len(dev, alen);
48 int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
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);
100 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
101 * @s: eeprom string (should be NULL terminated)
103 * Some Board manufacturers do not add a NULL termination at the
104 * end of string, instead some binary information is kludged in, hence
105 * convert the string to just printable characters of ASCII chart.
107 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
112 for (i = 0; i < l; i++, s++)
113 if (*s < ' ' || *s > '~') {
119 __weak void gpi2c_init(void)
123 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
124 u32 header, u32 size, uint8_t *ep)
130 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
135 * Read the header first then only read the other contents.
139 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
143 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
147 /* Corrupted data??? */
148 if (hdr_read != header) {
149 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
151 * read the eeprom header using i2c again, but use only a
152 * 1 byte address (some legacy boards need this..)
156 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
160 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
166 if (hdr_read != header)
169 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
176 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
179 struct ti_am_eeprom am_ep;
180 struct ti_common_eeprom *ep;
183 #ifndef CONFIG_SPL_BUILD
184 if (ep->header == TI_EEPROM_HEADER_MAGIC)
185 return 0; /* EEPROM has already been read */
188 /* Initialize with a known bad marker for i2c fails.. */
189 ep->header = TI_DEAD_EEPROM_MAGIC;
191 ep->version[0] = 0x0;
195 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
196 sizeof(am_ep), (uint8_t *)&am_ep);
200 ep->header = am_ep.header;
201 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
202 ti_eeprom_string_cleanup(ep->name);
204 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
205 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
206 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
207 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
209 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
210 ti_eeprom_string_cleanup(ep->version);
211 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
212 ti_eeprom_string_cleanup(ep->serial);
213 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
214 ti_eeprom_string_cleanup(ep->config);
216 memcpy(ep->mac_addr, am_ep.mac_addr,
217 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
222 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
225 struct dra7_eeprom dra7_ep;
226 struct ti_common_eeprom *ep;
229 #ifndef CONFIG_SPL_BUILD
230 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
231 return 0; /* EEPROM has already been read */
234 /* Initialize with a known bad marker for i2c fails.. */
235 ep->header = TI_DEAD_EEPROM_MAGIC;
237 ep->version[0] = 0x0;
243 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
244 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
248 ep->header = dra7_ep.header;
249 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
250 ti_eeprom_string_cleanup(ep->name);
252 offset = dra7_ep.version_major - 1;
254 /* Rev F is skipped */
257 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
258 'A' + offset, dra7_ep.version_minor);
259 ti_eeprom_string_cleanup(ep->version);
260 ep->emif1_size = (u64)dra7_ep.emif1_size;
261 ep->emif2_size = (u64)dra7_ep.emif2_size;
262 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
263 ti_eeprom_string_cleanup(ep->config);
268 bool __maybe_unused board_ti_is(char *name_tag)
270 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
272 if (ep->header == TI_DEAD_EEPROM_MAGIC)
274 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
277 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
279 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
282 if (ep->header == TI_DEAD_EEPROM_MAGIC)
285 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
286 return !strncmp(ep->version, rev_tag, l);
289 char * __maybe_unused board_ti_get_rev(void)
291 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
293 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
297 char * __maybe_unused board_ti_get_config(void)
299 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
301 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
305 char * __maybe_unused board_ti_get_name(void)
307 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
309 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
314 board_ti_get_eth_mac_addr(int index,
315 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
317 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
319 if (ep->header == TI_DEAD_EEPROM_MAGIC)
322 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
325 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
329 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
332 u64 __maybe_unused board_ti_get_emif1_size(void)
334 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
336 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
339 return ep->emif1_size;
342 u64 __maybe_unused board_ti_get_emif2_size(void)
344 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
346 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
349 return ep->emif2_size;
352 void __maybe_unused set_board_info_env(char *name)
354 char *unknown = "unknown";
355 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
358 setenv("board_name", name);
360 setenv("board_name", ep->name);
362 setenv("board_name", unknown);
365 setenv("board_rev", ep->version);
367 setenv("board_rev", unknown);
370 setenv("board_serial", ep->serial);
372 setenv("board_serial", unknown);
375 static u64 mac_to_u64(u8 mac[6])
380 for (i = 0; i < 6; i++) {
388 static void u64_to_mac(u64 addr, u8 mac[6])
398 void board_ti_set_ethaddr(int index)
403 u8 mac_addr1[6], mac_addr2[6];
406 * Export any Ethernet MAC addresses from EEPROM.
407 * The 2 MAC addresses in EEPROM define the address range.
409 board_ti_get_eth_mac_addr(0, mac_addr1);
410 board_ti_get_eth_mac_addr(1, mac_addr2);
412 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
413 mac1 = mac_to_u64(mac_addr1);
414 mac2 = mac_to_u64(mac_addr2);
416 /* must contain an address range */
417 num_macs = mac2 - mac1 + 1;
422 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
427 for (i = 0; i < num_macs; i++) {
428 u64_to_mac(mac1 + i, mac_addr);
429 if (is_valid_ethaddr(mac_addr)) {
430 eth_setenv_enetaddr_by_index("eth", i + index,