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)
19 * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
20 * @i2c_bus: i2c bus number to initialize
21 * @dev_addr: Device address to probe for
23 * Return: 0 on success or corresponding error on failure.
25 static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
30 rc = i2c_set_bus_num(i2c_bus);
35 return i2c_probe(dev_addr);
39 * ti_i2c_eeprom_read - Read data from an EEPROM
40 * @dev_addr: The device address of the EEPROM
41 * @offset: Offset to start reading in the EEPROM
42 * @ep: Pointer to a buffer to read into
43 * @epsize: Size of buffer
45 * Return: 0 on success or corresponding result of i2c_read
47 static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
48 uchar *ep, int epsize)
50 return i2c_read(dev_addr, offset, 2, ep, epsize);
55 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
56 * @s: eeprom string (should be NULL terminated)
58 * Some Board manufacturers do not add a NULL termination at the
59 * end of string, instead some binary information is kludged in, hence
60 * convert the string to just printable characters of ASCII chart.
62 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
67 for (i = 0; i < l; i++, s++)
68 if (*s < ' ' || *s > '~') {
74 __weak void gpi2c_init(void)
78 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
79 u32 header, u32 size, uint8_t *ep)
84 #if defined(CONFIG_DM_I2C)
88 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
91 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
96 * Read the header first then only read the other contents.
98 rc = i2c_set_chip_offset_len(dev, 2);
102 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
106 /* Corrupted data??? */
107 if (hdr_read != header) {
108 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
110 * read the eeprom header using i2c again, but use only a
111 * 1 byte address (some legacy boards need this..)
114 rc = i2c_set_chip_offset_len(dev, 1);
118 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
123 if (hdr_read != header)
126 rc = dm_i2c_read(dev, 0, ep, size);
133 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
138 * Read the header first then only read the other contents.
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 = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
161 if (hdr_read != header)
164 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
171 int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
173 struct ti_common_eeprom *ep;
179 if (ep->header == TI_EEPROM_HEADER_MAGIC)
182 /* Set to 0 all fields */
183 memset(ep, 0, sizeof(*ep));
184 strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
185 strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
186 /* Some dummy serial number to identify the platform */
187 strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
188 /* Mark it with a valid header */
189 ep->header = TI_EEPROM_HEADER_MAGIC;
195 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
198 struct ti_am_eeprom am_ep;
199 struct ti_common_eeprom *ep;
202 #ifndef CONFIG_SPL_BUILD
203 if (ep->header == TI_EEPROM_HEADER_MAGIC)
204 return 0; /* EEPROM has already been read */
207 /* Initialize with a known bad marker for i2c fails.. */
208 ep->header = TI_DEAD_EEPROM_MAGIC;
210 ep->version[0] = 0x0;
214 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
215 sizeof(am_ep), (uint8_t *)&am_ep);
219 ep->header = am_ep.header;
220 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
221 ti_eeprom_string_cleanup(ep->name);
223 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
224 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
225 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
226 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
228 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
229 ti_eeprom_string_cleanup(ep->version);
230 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
231 ti_eeprom_string_cleanup(ep->serial);
232 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
233 ti_eeprom_string_cleanup(ep->config);
235 memcpy(ep->mac_addr, am_ep.mac_addr,
236 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
241 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
244 struct dra7_eeprom dra7_ep;
245 struct ti_common_eeprom *ep;
248 #ifndef CONFIG_SPL_BUILD
249 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
250 return 0; /* EEPROM has already been read */
253 /* Initialize with a known bad marker for i2c fails.. */
254 ep->header = TI_DEAD_EEPROM_MAGIC;
256 ep->version[0] = 0x0;
262 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
263 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
267 ep->header = dra7_ep.header;
268 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
269 ti_eeprom_string_cleanup(ep->name);
271 offset = dra7_ep.version_major - 1;
273 /* Rev F is skipped */
276 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
277 'A' + offset, dra7_ep.version_minor);
278 ti_eeprom_string_cleanup(ep->version);
279 ep->emif1_size = (u64)dra7_ep.emif1_size;
280 ep->emif2_size = (u64)dra7_ep.emif2_size;
281 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
282 ti_eeprom_string_cleanup(ep->config);
287 bool __maybe_unused board_ti_is(char *name_tag)
289 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
291 if (ep->header == TI_DEAD_EEPROM_MAGIC)
293 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
296 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
298 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
301 if (ep->header == TI_DEAD_EEPROM_MAGIC)
304 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
305 return !strncmp(ep->version, rev_tag, l);
308 char * __maybe_unused board_ti_get_rev(void)
310 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
312 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
316 char * __maybe_unused board_ti_get_config(void)
318 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
320 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
324 char * __maybe_unused board_ti_get_name(void)
326 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
328 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
333 board_ti_get_eth_mac_addr(int index,
334 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
336 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
338 if (ep->header == TI_DEAD_EEPROM_MAGIC)
341 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
344 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
348 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
351 u64 __maybe_unused board_ti_get_emif1_size(void)
353 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
355 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
358 return ep->emif1_size;
361 u64 __maybe_unused board_ti_get_emif2_size(void)
363 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
365 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
368 return ep->emif2_size;
371 void __maybe_unused set_board_info_env(char *name)
373 char *unknown = "unknown";
374 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
377 env_set("board_name", name);
379 env_set("board_name", ep->name);
381 env_set("board_name", unknown);
384 env_set("board_rev", ep->version);
386 env_set("board_rev", unknown);
389 env_set("board_serial", ep->serial);
391 env_set("board_serial", unknown);
394 static u64 mac_to_u64(u8 mac[6])
399 for (i = 0; i < 6; i++) {
407 static void u64_to_mac(u64 addr, u8 mac[6])
417 void board_ti_set_ethaddr(int index)
422 u8 mac_addr1[6], mac_addr2[6];
425 * Export any Ethernet MAC addresses from EEPROM.
426 * The 2 MAC addresses in EEPROM define the address range.
428 board_ti_get_eth_mac_addr(0, mac_addr1);
429 board_ti_get_eth_mac_addr(1, mac_addr2);
431 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
432 mac1 = mac_to_u64(mac_addr1);
433 mac2 = mac_to_u64(mac_addr2);
435 /* must contain an address range */
436 num_macs = mac2 - mac1 + 1;
441 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
446 for (i = 0; i < num_macs; i++) {
447 u64_to_mac(mac1 + i, mac_addr);
448 if (is_valid_ethaddr(mac_addr)) {
449 eth_env_set_enetaddr_by_index("eth", i + index,
456 bool __maybe_unused board_ti_was_eeprom_read(void)
458 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
460 if (ep->header == TI_EEPROM_HEADER_MAGIC)