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>
15 #include "board_detect.h"
18 * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
19 * @i2c_bus: i2c bus number to initialize
20 * @dev_addr: Device address to probe for
22 * Return: 0 on success or corresponding error on failure.
24 static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
29 rc = i2c_set_bus_num(i2c_bus);
34 return i2c_probe(dev_addr);
38 * ti_i2c_eeprom_read - Read data from an EEPROM
39 * @dev_addr: The device address of the EEPROM
40 * @offset: Offset to start reading in the EEPROM
41 * @ep: Pointer to a buffer to read into
42 * @epsize: Size of buffer
44 * Return: 0 on success or corresponding result of i2c_read
46 static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
47 uchar *ep, int epsize)
49 return i2c_read(dev_addr, offset, 2, ep, epsize);
53 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
54 * @s: eeprom string (should be NULL terminated)
56 * Some Board manufacturers do not add a NULL termination at the
57 * end of string, instead some binary information is kludged in, hence
58 * convert the string to just printable characters of ASCII chart.
60 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
65 for (i = 0; i < l; i++, s++)
66 if (*s < ' ' || *s > '~') {
72 __weak void gpi2c_init(void)
76 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
77 u32 header, u32 size, uint8_t *ep)
83 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
88 * Read the header first then only read the other contents.
91 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
95 /* Corrupted data??? */
96 if (hdr_read != header) {
97 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
99 * read the eeprom header using i2c again, but use only a
100 * 1 byte address (some legacy boards need this..)
104 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
109 if (hdr_read != header)
112 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
119 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
122 struct ti_am_eeprom am_ep;
123 struct ti_common_eeprom *ep;
126 #ifndef CONFIG_SPL_BUILD
127 if (ep->header == TI_EEPROM_HEADER_MAGIC)
128 return 0; /* EEPROM has already been read */
131 /* Initialize with a known bad marker for i2c fails.. */
132 ep->header = TI_DEAD_EEPROM_MAGIC;
134 ep->version[0] = 0x0;
138 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
139 sizeof(am_ep), (uint8_t *)&am_ep);
143 ep->header = am_ep.header;
144 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
145 ti_eeprom_string_cleanup(ep->name);
147 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
148 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
149 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
150 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
152 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
153 ti_eeprom_string_cleanup(ep->version);
154 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
155 ti_eeprom_string_cleanup(ep->serial);
156 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
157 ti_eeprom_string_cleanup(ep->config);
159 memcpy(ep->mac_addr, am_ep.mac_addr,
160 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
165 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
168 struct dra7_eeprom dra7_ep;
169 struct ti_common_eeprom *ep;
172 #ifndef CONFIG_SPL_BUILD
173 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
174 return 0; /* EEPROM has already been read */
177 /* Initialize with a known bad marker for i2c fails.. */
178 ep->header = TI_DEAD_EEPROM_MAGIC;
180 ep->version[0] = 0x0;
186 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
187 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
191 ep->header = dra7_ep.header;
192 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
193 ti_eeprom_string_cleanup(ep->name);
195 offset = dra7_ep.version_major - 1;
197 /* Rev F is skipped */
200 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
201 'A' + offset, dra7_ep.version_minor);
202 ti_eeprom_string_cleanup(ep->version);
203 ep->emif1_size = (u64)dra7_ep.emif1_size;
204 ep->emif2_size = (u64)dra7_ep.emif2_size;
205 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
206 ti_eeprom_string_cleanup(ep->config);
211 bool __maybe_unused board_ti_is(char *name_tag)
213 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
215 if (ep->header == TI_DEAD_EEPROM_MAGIC)
217 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
220 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
222 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
225 if (ep->header == TI_DEAD_EEPROM_MAGIC)
228 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
229 return !strncmp(ep->version, rev_tag, l);
232 char * __maybe_unused board_ti_get_rev(void)
234 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
236 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
240 char * __maybe_unused board_ti_get_config(void)
242 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
244 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
248 char * __maybe_unused board_ti_get_name(void)
250 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
252 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
257 board_ti_get_eth_mac_addr(int index,
258 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
260 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
262 if (ep->header == TI_DEAD_EEPROM_MAGIC)
265 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
268 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
272 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
275 u64 __maybe_unused board_ti_get_emif1_size(void)
277 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
279 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
282 return ep->emif1_size;
285 u64 __maybe_unused board_ti_get_emif2_size(void)
287 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
289 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
292 return ep->emif2_size;
295 void __maybe_unused set_board_info_env(char *name)
297 char *unknown = "unknown";
298 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
301 setenv("board_name", name);
303 setenv("board_name", ep->name);
305 setenv("board_name", unknown);
308 setenv("board_rev", ep->version);
310 setenv("board_rev", unknown);
313 setenv("board_serial", ep->serial);
315 setenv("board_serial", unknown);
318 static u64 mac_to_u64(u8 mac[6])
323 for (i = 0; i < 6; i++) {
331 static void u64_to_mac(u64 addr, u8 mac[6])
341 void board_ti_set_ethaddr(int index)
346 u8 mac_addr1[6], mac_addr2[6];
349 * Export any Ethernet MAC addresses from EEPROM.
350 * The 2 MAC addresses in EEPROM define the address range.
352 board_ti_get_eth_mac_addr(0, mac_addr1);
353 board_ti_get_eth_mac_addr(1, mac_addr2);
355 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
356 mac1 = mac_to_u64(mac_addr1);
357 mac2 = mac_to_u64(mac_addr2);
359 /* must contain an address range */
360 num_macs = mac2 - mac1 + 1;
365 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
370 for (i = 0; i < num_macs; i++) {
371 u64_to_mac(mac1 + i, mac_addr);
372 if (is_valid_ethaddr(mac_addr)) {
373 eth_setenv_enetaddr_by_index("eth", i + index,