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 return i2c_read(dev_addr, offset, 2, ep, epsize);
90 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
91 * @s: eeprom string (should be NULL terminated)
93 * Some Board manufacturers do not add a NULL termination at the
94 * end of string, instead some binary information is kludged in, hence
95 * convert the string to just printable characters of ASCII chart.
97 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
102 for (i = 0; i < l; i++, s++)
103 if (*s < ' ' || *s > '~') {
109 __weak void gpi2c_init(void)
113 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
114 u32 header, u32 size, uint8_t *ep)
120 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
125 * Read the header first then only read the other contents.
128 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
132 /* Corrupted data??? */
133 if (hdr_read != header) {
134 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
136 * read the eeprom header using i2c again, but use only a
137 * 1 byte address (some legacy boards need this..)
141 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
146 if (hdr_read != header)
149 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
156 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
159 struct ti_am_eeprom am_ep;
160 struct ti_common_eeprom *ep;
163 #ifndef CONFIG_SPL_BUILD
164 if (ep->header == TI_EEPROM_HEADER_MAGIC)
165 return 0; /* EEPROM has already been read */
168 /* Initialize with a known bad marker for i2c fails.. */
169 ep->header = TI_DEAD_EEPROM_MAGIC;
171 ep->version[0] = 0x0;
175 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
176 sizeof(am_ep), (uint8_t *)&am_ep);
180 ep->header = am_ep.header;
181 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
182 ti_eeprom_string_cleanup(ep->name);
184 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
185 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
186 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
187 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
189 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
190 ti_eeprom_string_cleanup(ep->version);
191 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
192 ti_eeprom_string_cleanup(ep->serial);
193 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
194 ti_eeprom_string_cleanup(ep->config);
196 memcpy(ep->mac_addr, am_ep.mac_addr,
197 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
202 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
205 struct dra7_eeprom dra7_ep;
206 struct ti_common_eeprom *ep;
209 #ifndef CONFIG_SPL_BUILD
210 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
211 return 0; /* EEPROM has already been read */
214 /* Initialize with a known bad marker for i2c fails.. */
215 ep->header = TI_DEAD_EEPROM_MAGIC;
217 ep->version[0] = 0x0;
223 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
224 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
228 ep->header = dra7_ep.header;
229 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
230 ti_eeprom_string_cleanup(ep->name);
232 offset = dra7_ep.version_major - 1;
234 /* Rev F is skipped */
237 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
238 'A' + offset, dra7_ep.version_minor);
239 ti_eeprom_string_cleanup(ep->version);
240 ep->emif1_size = (u64)dra7_ep.emif1_size;
241 ep->emif2_size = (u64)dra7_ep.emif2_size;
242 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
243 ti_eeprom_string_cleanup(ep->config);
248 bool __maybe_unused board_ti_is(char *name_tag)
250 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
252 if (ep->header == TI_DEAD_EEPROM_MAGIC)
254 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
257 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
259 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
262 if (ep->header == TI_DEAD_EEPROM_MAGIC)
265 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
266 return !strncmp(ep->version, rev_tag, l);
269 char * __maybe_unused board_ti_get_rev(void)
271 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
273 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
277 char * __maybe_unused board_ti_get_config(void)
279 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
281 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
285 char * __maybe_unused board_ti_get_name(void)
287 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
289 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
294 board_ti_get_eth_mac_addr(int index,
295 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
297 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
299 if (ep->header == TI_DEAD_EEPROM_MAGIC)
302 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
305 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
309 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
312 u64 __maybe_unused board_ti_get_emif1_size(void)
314 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
316 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
319 return ep->emif1_size;
322 u64 __maybe_unused board_ti_get_emif2_size(void)
324 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
326 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
329 return ep->emif2_size;
332 void __maybe_unused set_board_info_env(char *name)
334 char *unknown = "unknown";
335 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
338 setenv("board_name", name);
340 setenv("board_name", ep->name);
342 setenv("board_name", unknown);
345 setenv("board_rev", ep->version);
347 setenv("board_rev", unknown);
350 setenv("board_serial", ep->serial);
352 setenv("board_serial", unknown);
355 static u64 mac_to_u64(u8 mac[6])
360 for (i = 0; i < 6; i++) {
368 static void u64_to_mac(u64 addr, u8 mac[6])
378 void board_ti_set_ethaddr(int index)
383 u8 mac_addr1[6], mac_addr2[6];
386 * Export any Ethernet MAC addresses from EEPROM.
387 * The 2 MAC addresses in EEPROM define the address range.
389 board_ti_get_eth_mac_addr(0, mac_addr1);
390 board_ti_get_eth_mac_addr(1, mac_addr2);
392 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
393 mac1 = mac_to_u64(mac_addr1);
394 mac2 = mac_to_u64(mac_addr2);
396 /* must contain an address range */
397 num_macs = mac2 - mac1 + 1;
402 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
407 for (i = 0; i < num_macs; i++) {
408 u64_to_mac(mac1 + i, mac_addr);
409 if (is_valid_ethaddr(mac_addr)) {
410 eth_setenv_enetaddr_by_index("eth", i + index,