blob: 1da5ace9236d7d8d17e0d2c67d99ad9ad7a8ef5f [file] [log] [blame]
Lokesh Vutla0bea8132016-02-24 12:30:54 -06001/*
2 * Library to support early TI EVM EEPROM handling
3 *
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
5 * Lokesh Vutla
6 * Steve Kipisz
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <common.h>
12#include <asm/omap_common.h>
Cooper Jr., Franklinc6b80b12017-04-20 10:25:44 -050013#include <dm/uclass.h>
Lokesh Vutla0bea8132016-02-24 12:30:54 -060014#include <i2c.h>
15
16#include "board_detect.h"
17
Cooper Jr., Franklinc6b80b12017-04-20 10:25:44 -050018#if defined(CONFIG_DM_I2C_COMPAT)
19/**
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
24 *
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.
28 */
29int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
30{
31 struct udevice *dev;
32 struct udevice *bus;
33 int rc;
34
35 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
36 if (rc)
37 return rc;
38 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
39 if (rc)
40 return rc;
41 rc = i2c_set_chip_offset_len(dev, alen);
42 if (rc)
43 return rc;
44
45 return 0;
46}
47#else
48int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
49{
50 return 0;
51}
52#endif
53
Lokesh Vutla0bea8132016-02-24 12:30:54 -060054/**
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
58 *
59 * Return: 0 on success or corresponding error on failure.
60 */
61static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
62{
63 int rc;
64
65 if (i2c_bus >= 0) {
66 rc = i2c_set_bus_num(i2c_bus);
67 if (rc)
68 return rc;
69 }
70
71 return i2c_probe(dev_addr);
72}
73
74/**
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
80 *
81 * Return: 0 on success or corresponding result of i2c_read
82 */
83static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
84 uchar *ep, int epsize)
85{
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -050086 int bus_num, rc, alen;
87
88 bus_num = i2c_get_bus_num();
89
90 alen = 2;
91
92 rc = ti_i2c_set_alen(bus_num, dev_addr, alen);
93 if (rc)
94 return rc;
95
96 return i2c_read(dev_addr, offset, alen, ep, epsize);
Lokesh Vutla0bea8132016-02-24 12:30:54 -060097}
98
99/**
100 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
101 * @s: eeprom string (should be NULL terminated)
102 *
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.
106 */
107static void __maybe_unused ti_eeprom_string_cleanup(char *s)
108{
109 int i, l;
110
111 l = strlen(s);
112 for (i = 0; i < l; i++, s++)
113 if (*s < ' ' || *s > '~') {
114 *s = 0;
115 break;
116 }
117}
118
119__weak void gpi2c_init(void)
120{
121}
122
123static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
124 u32 header, u32 size, uint8_t *ep)
125{
126 u32 byte, hdr_read;
127 int rc;
128
129 gpi2c_init();
130 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
131 if (rc)
132 return rc;
133
134 /*
135 * Read the header first then only read the other contents.
136 */
137 byte = 2;
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500138
139 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
140 if (rc)
141 return rc;
142
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600143 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
144 if (rc)
145 return rc;
146
147 /* Corrupted data??? */
148 if (hdr_read != header) {
149 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
150 /*
151 * read the eeprom header using i2c again, but use only a
152 * 1 byte address (some legacy boards need this..)
153 */
154 byte = 1;
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500155 if (rc) {
156 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
157 if (rc)
158 return rc;
159
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600160 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
161 4);
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500162 }
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600163 if (rc)
164 return rc;
165 }
166 if (hdr_read != header)
167 return -1;
168
169 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
170 if (rc)
171 return rc;
172
173 return 0;
174}
175
Nishanth Menone936f992017-06-16 17:25:04 -0500176int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
177{
178 struct ti_common_eeprom *ep;
179
180 if (!name || !rev)
181 return -1;
182
183 ep = TI_EEPROM_DATA;
184 if (ep->header == TI_EEPROM_HEADER_MAGIC)
185 goto already_set;
186
187 /* Set to 0 all fields */
188 memset(ep, 0, sizeof(*ep));
189 strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
190 strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
191 /* Some dummy serial number to identify the platform */
192 strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
193 /* Mark it with a valid header */
194 ep->header = TI_EEPROM_HEADER_MAGIC;
195
196already_set:
197 return 0;
198}
199
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600200int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
201{
202 int rc;
203 struct ti_am_eeprom am_ep;
204 struct ti_common_eeprom *ep;
205
206 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100207#ifndef CONFIG_SPL_BUILD
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600208 if (ep->header == TI_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100209 return 0; /* EEPROM has already been read */
210#endif
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600211
212 /* Initialize with a known bad marker for i2c fails.. */
213 ep->header = TI_DEAD_EEPROM_MAGIC;
214 ep->name[0] = 0x0;
215 ep->version[0] = 0x0;
216 ep->serial[0] = 0x0;
Nishanth Menon2a78c9e2016-10-11 12:39:04 -0500217 ep->config[0] = 0x0;
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600218
219 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
220 sizeof(am_ep), (uint8_t *)&am_ep);
221 if (rc)
222 return rc;
223
224 ep->header = am_ep.header;
225 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
226 ti_eeprom_string_cleanup(ep->name);
227
228 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
229 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
230 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
231 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
232 else
233 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
234 ti_eeprom_string_cleanup(ep->version);
235 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
236 ti_eeprom_string_cleanup(ep->serial);
237 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
238 ti_eeprom_string_cleanup(ep->config);
239
240 memcpy(ep->mac_addr, am_ep.mac_addr,
241 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
242
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600243 return 0;
244}
245
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530246int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
247{
248 int rc, offset = 0;
249 struct dra7_eeprom dra7_ep;
250 struct ti_common_eeprom *ep;
251
252 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100253#ifndef CONFIG_SPL_BUILD
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530254 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100255 return 0; /* EEPROM has already been read */
256#endif
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530257
258 /* Initialize with a known bad marker for i2c fails.. */
Nishanth Menon28d624b2016-10-11 12:39:03 -0500259 ep->header = TI_DEAD_EEPROM_MAGIC;
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530260 ep->name[0] = 0x0;
261 ep->version[0] = 0x0;
262 ep->serial[0] = 0x0;
Nishanth Menon2a78c9e2016-10-11 12:39:04 -0500263 ep->config[0] = 0x0;
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530264 ep->emif1_size = 0;
265 ep->emif2_size = 0;
266
267 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
268 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
269 if (rc)
270 return rc;
271
272 ep->header = dra7_ep.header;
273 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
274 ti_eeprom_string_cleanup(ep->name);
275
276 offset = dra7_ep.version_major - 1;
277
278 /* Rev F is skipped */
279 if (offset >= 5)
280 offset = offset + 1;
281 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
282 'A' + offset, dra7_ep.version_minor);
283 ti_eeprom_string_cleanup(ep->version);
284 ep->emif1_size = (u64)dra7_ep.emif1_size;
285 ep->emif2_size = (u64)dra7_ep.emif2_size;
286 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
287 ti_eeprom_string_cleanup(ep->config);
288
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530289 return 0;
290}
291
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600292bool __maybe_unused board_ti_is(char *name_tag)
293{
294 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
295
296 if (ep->header == TI_DEAD_EEPROM_MAGIC)
297 return false;
298 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
299}
300
301bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
302{
303 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
304 int l;
305
306 if (ep->header == TI_DEAD_EEPROM_MAGIC)
307 return false;
308
309 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
310 return !strncmp(ep->version, rev_tag, l);
311}
312
313char * __maybe_unused board_ti_get_rev(void)
314{
315 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
316
Nishanth Menon7774e972016-10-11 12:39:05 -0500317 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600318 return ep->version;
319}
320
321char * __maybe_unused board_ti_get_config(void)
322{
323 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
324
Nishanth Menon7774e972016-10-11 12:39:05 -0500325 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600326 return ep->config;
327}
328
329char * __maybe_unused board_ti_get_name(void)
330{
331 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
332
Nishanth Menon7774e972016-10-11 12:39:05 -0500333 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600334 return ep->name;
335}
336
337void __maybe_unused
338board_ti_get_eth_mac_addr(int index,
339 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
340{
341 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
342
343 if (ep->header == TI_DEAD_EEPROM_MAGIC)
344 goto fail;
345
346 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
347 goto fail;
348
349 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
350 return;
351
352fail:
353 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
354}
355
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530356u64 __maybe_unused board_ti_get_emif1_size(void)
357{
358 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
359
360 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
361 return 0;
362
363 return ep->emif1_size;
364}
365
366u64 __maybe_unused board_ti_get_emif2_size(void)
367{
368 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
369
370 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
371 return 0;
372
373 return ep->emif2_size;
374}
375
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600376void __maybe_unused set_board_info_env(char *name)
377{
378 char *unknown = "unknown";
379 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
380
381 if (name)
382 setenv("board_name", name);
383 else if (ep->name)
384 setenv("board_name", ep->name);
385 else
386 setenv("board_name", unknown);
387
388 if (ep->version)
389 setenv("board_rev", ep->version);
390 else
391 setenv("board_rev", unknown);
392
393 if (ep->serial)
394 setenv("board_serial", ep->serial);
395 else
396 setenv("board_serial", unknown);
397}
Roger Quadros38f719e2017-03-14 15:04:19 +0200398
399static u64 mac_to_u64(u8 mac[6])
400{
401 int i;
402 u64 addr = 0;
403
404 for (i = 0; i < 6; i++) {
405 addr <<= 8;
406 addr |= mac[i];
407 }
408
409 return addr;
410}
411
412static void u64_to_mac(u64 addr, u8 mac[6])
413{
414 mac[5] = addr;
415 mac[4] = addr >> 8;
416 mac[3] = addr >> 16;
417 mac[2] = addr >> 24;
418 mac[1] = addr >> 32;
419 mac[0] = addr >> 40;
420}
421
422void board_ti_set_ethaddr(int index)
423{
424 uint8_t mac_addr[6];
425 int i;
426 u64 mac1, mac2;
427 u8 mac_addr1[6], mac_addr2[6];
428 int num_macs;
429 /*
430 * Export any Ethernet MAC addresses from EEPROM.
431 * The 2 MAC addresses in EEPROM define the address range.
432 */
433 board_ti_get_eth_mac_addr(0, mac_addr1);
434 board_ti_get_eth_mac_addr(1, mac_addr2);
435
436 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
437 mac1 = mac_to_u64(mac_addr1);
438 mac2 = mac_to_u64(mac_addr2);
439
440 /* must contain an address range */
441 num_macs = mac2 - mac1 + 1;
442 if (num_macs <= 0)
443 return;
444
445 if (num_macs > 50) {
446 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
447 __func__, num_macs);
448 num_macs = 50;
449 }
450
451 for (i = 0; i < num_macs; i++) {
452 u64_to_mac(mac1 + i, mac_addr);
453 if (is_valid_ethaddr(mac_addr)) {
454 eth_setenv_enetaddr_by_index("eth", i + index,
455 mac_addr);
456 }
457 }
458 }
459}
Cooper Jr., Franklin69e8d4b2017-06-16 17:25:08 -0500460
461bool __maybe_unused board_ti_was_eeprom_read(void)
462{
463 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
464
465 if (ep->header == TI_EEPROM_HEADER_MAGIC)
466 return true;
467 else
468 return false;
469}