blob: e258e22f3714c6c8052d8ddf80956cc7a89f779e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lokesh Vutla0bea8132016-02-24 12:30:54 -06002/*
3 * Library to support early TI EVM EEPROM handling
4 *
5 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla
7 * Steve Kipisz
Lokesh Vutla0bea8132016-02-24 12:30:54 -06008 */
9
10#include <common.h>
11#include <asm/omap_common.h>
Cooper Jr., Franklinc6b80b12017-04-20 10:25:44 -050012#include <dm/uclass.h>
Lokesh Vutla0bea8132016-02-24 12:30:54 -060013#include <i2c.h>
14
15#include "board_detect.h"
16
Jean-Jacques Hiblot15142442018-12-07 14:50:49 +010017#if !defined(CONFIG_DM_I2C)
Lokesh Vutla0bea8132016-02-24 12:30:54 -060018/**
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
22 *
23 * Return: 0 on success or corresponding error on failure.
24 */
25static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
26{
27 int rc;
28
29 if (i2c_bus >= 0) {
30 rc = i2c_set_bus_num(i2c_bus);
31 if (rc)
32 return rc;
33 }
34
35 return i2c_probe(dev_addr);
36}
37
38/**
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
44 *
45 * Return: 0 on success or corresponding result of i2c_read
46 */
47static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
48 uchar *ep, int epsize)
49{
Jean-Jacques Hiblot15142442018-12-07 14:50:49 +010050 return i2c_read(dev_addr, offset, 2, ep, epsize);
Lokesh Vutla0bea8132016-02-24 12:30:54 -060051}
Andreas Dannenberg2463f672018-12-07 14:50:47 +010052#endif
Lokesh Vutla0bea8132016-02-24 12:30:54 -060053
54/**
55 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
56 * @s: eeprom string (should be NULL terminated)
57 *
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.
61 */
62static void __maybe_unused ti_eeprom_string_cleanup(char *s)
63{
64 int i, l;
65
66 l = strlen(s);
67 for (i = 0; i < l; i++, s++)
68 if (*s < ' ' || *s > '~') {
69 *s = 0;
70 break;
71 }
72}
73
74__weak void gpi2c_init(void)
75{
76}
77
78static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
79 u32 header, u32 size, uint8_t *ep)
80{
Andreas Dannenberg2463f672018-12-07 14:50:47 +010081 u32 hdr_read;
Lokesh Vutla0bea8132016-02-24 12:30:54 -060082 int rc;
83
Jean-Jacques Hiblot15142442018-12-07 14:50:49 +010084#if defined(CONFIG_DM_I2C)
Andreas Dannenberg2463f672018-12-07 14:50:47 +010085 struct udevice *dev;
86 struct udevice *bus;
87
88 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
89 if (rc)
90 return rc;
91 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
92 if (rc)
93 return rc;
94
95 /*
96 * Read the header first then only read the other contents.
97 */
98 rc = i2c_set_chip_offset_len(dev, 2);
99 if (rc)
100 return rc;
101
102 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
103 if (rc)
104 return rc;
105
106 /* Corrupted data??? */
107 if (hdr_read != header) {
108 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
109 /*
110 * read the eeprom header using i2c again, but use only a
111 * 1 byte address (some legacy boards need this..)
112 */
113 if (rc) {
114 rc = i2c_set_chip_offset_len(dev, 1);
115 if (rc)
116 return rc;
117
118 rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4);
119 }
120 if (rc)
121 return rc;
122 }
123 if (hdr_read != header)
124 return -1;
125
126 rc = dm_i2c_read(dev, 0, ep, size);
127 if (rc)
128 return rc;
129#else
130 u32 byte;
131
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600132 gpi2c_init();
133 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
134 if (rc)
135 return rc;
136
137 /*
138 * Read the header first then only read the other contents.
139 */
140 byte = 2;
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500141
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600142 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
143 if (rc)
144 return rc;
145
146 /* Corrupted data??? */
147 if (hdr_read != header) {
148 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
149 /*
150 * read the eeprom header using i2c again, but use only a
151 * 1 byte address (some legacy boards need this..)
152 */
153 byte = 1;
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500154 if (rc) {
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600155 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
156 4);
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500157 }
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600158 if (rc)
159 return rc;
160 }
161 if (hdr_read != header)
162 return -1;
163
164 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
165 if (rc)
166 return rc;
Andreas Dannenberg2463f672018-12-07 14:50:47 +0100167#endif
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600168 return 0;
169}
170
Nishanth Menone936f992017-06-16 17:25:04 -0500171int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
172{
173 struct ti_common_eeprom *ep;
174
175 if (!name || !rev)
176 return -1;
177
178 ep = TI_EEPROM_DATA;
179 if (ep->header == TI_EEPROM_HEADER_MAGIC)
180 goto already_set;
181
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;
190
191already_set:
192 return 0;
193}
194
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600195int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
196{
197 int rc;
198 struct ti_am_eeprom am_ep;
199 struct ti_common_eeprom *ep;
200
201 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100202#ifndef CONFIG_SPL_BUILD
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600203 if (ep->header == TI_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100204 return 0; /* EEPROM has already been read */
205#endif
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600206
207 /* Initialize with a known bad marker for i2c fails.. */
208 ep->header = TI_DEAD_EEPROM_MAGIC;
209 ep->name[0] = 0x0;
210 ep->version[0] = 0x0;
211 ep->serial[0] = 0x0;
Nishanth Menon2a78c9e2016-10-11 12:39:04 -0500212 ep->config[0] = 0x0;
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600213
214 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
215 sizeof(am_ep), (uint8_t *)&am_ep);
216 if (rc)
217 return rc;
218
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);
222
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);
227 else
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);
234
235 memcpy(ep->mac_addr, am_ep.mac_addr,
236 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
237
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600238 return 0;
239}
240
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530241int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
242{
243 int rc, offset = 0;
244 struct dra7_eeprom dra7_ep;
245 struct ti_common_eeprom *ep;
246
247 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100248#ifndef CONFIG_SPL_BUILD
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530249 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100250 return 0; /* EEPROM has already been read */
251#endif
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530252
253 /* Initialize with a known bad marker for i2c fails.. */
Nishanth Menon28d624b2016-10-11 12:39:03 -0500254 ep->header = TI_DEAD_EEPROM_MAGIC;
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530255 ep->name[0] = 0x0;
256 ep->version[0] = 0x0;
257 ep->serial[0] = 0x0;
Nishanth Menon2a78c9e2016-10-11 12:39:04 -0500258 ep->config[0] = 0x0;
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530259 ep->emif1_size = 0;
260 ep->emif2_size = 0;
261
262 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
263 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
264 if (rc)
265 return rc;
266
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);
270
271 offset = dra7_ep.version_major - 1;
272
273 /* Rev F is skipped */
274 if (offset >= 5)
275 offset = offset + 1;
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);
283
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530284 return 0;
285}
286
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600287bool __maybe_unused board_ti_is(char *name_tag)
288{
289 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
290
291 if (ep->header == TI_DEAD_EEPROM_MAGIC)
292 return false;
293 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
294}
295
296bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
297{
298 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
299 int l;
300
301 if (ep->header == TI_DEAD_EEPROM_MAGIC)
302 return false;
303
304 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
305 return !strncmp(ep->version, rev_tag, l);
306}
307
308char * __maybe_unused board_ti_get_rev(void)
309{
310 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
311
Nishanth Menon7774e972016-10-11 12:39:05 -0500312 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600313 return ep->version;
314}
315
316char * __maybe_unused board_ti_get_config(void)
317{
318 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
319
Nishanth Menon7774e972016-10-11 12:39:05 -0500320 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600321 return ep->config;
322}
323
324char * __maybe_unused board_ti_get_name(void)
325{
326 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
327
Nishanth Menon7774e972016-10-11 12:39:05 -0500328 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600329 return ep->name;
330}
331
332void __maybe_unused
333board_ti_get_eth_mac_addr(int index,
334 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
335{
336 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
337
338 if (ep->header == TI_DEAD_EEPROM_MAGIC)
339 goto fail;
340
341 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
342 goto fail;
343
344 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
345 return;
346
347fail:
348 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
349}
350
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530351u64 __maybe_unused board_ti_get_emif1_size(void)
352{
353 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
354
355 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
356 return 0;
357
358 return ep->emif1_size;
359}
360
361u64 __maybe_unused board_ti_get_emif2_size(void)
362{
363 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
364
365 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
366 return 0;
367
368 return ep->emif2_size;
369}
370
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600371void __maybe_unused set_board_info_env(char *name)
372{
373 char *unknown = "unknown";
374 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
375
376 if (name)
Simon Glass382bee52017-08-03 12:22:09 -0600377 env_set("board_name", name);
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600378 else if (ep->name)
Simon Glass382bee52017-08-03 12:22:09 -0600379 env_set("board_name", ep->name);
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600380 else
Simon Glass382bee52017-08-03 12:22:09 -0600381 env_set("board_name", unknown);
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600382
383 if (ep->version)
Simon Glass382bee52017-08-03 12:22:09 -0600384 env_set("board_rev", ep->version);
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600385 else
Simon Glass382bee52017-08-03 12:22:09 -0600386 env_set("board_rev", unknown);
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600387
388 if (ep->serial)
Simon Glass382bee52017-08-03 12:22:09 -0600389 env_set("board_serial", ep->serial);
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600390 else
Simon Glass382bee52017-08-03 12:22:09 -0600391 env_set("board_serial", unknown);
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600392}
Roger Quadros38f719e2017-03-14 15:04:19 +0200393
394static u64 mac_to_u64(u8 mac[6])
395{
396 int i;
397 u64 addr = 0;
398
399 for (i = 0; i < 6; i++) {
400 addr <<= 8;
401 addr |= mac[i];
402 }
403
404 return addr;
405}
406
407static void u64_to_mac(u64 addr, u8 mac[6])
408{
409 mac[5] = addr;
410 mac[4] = addr >> 8;
411 mac[3] = addr >> 16;
412 mac[2] = addr >> 24;
413 mac[1] = addr >> 32;
414 mac[0] = addr >> 40;
415}
416
417void board_ti_set_ethaddr(int index)
418{
419 uint8_t mac_addr[6];
420 int i;
421 u64 mac1, mac2;
422 u8 mac_addr1[6], mac_addr2[6];
423 int num_macs;
424 /*
425 * Export any Ethernet MAC addresses from EEPROM.
426 * The 2 MAC addresses in EEPROM define the address range.
427 */
428 board_ti_get_eth_mac_addr(0, mac_addr1);
429 board_ti_get_eth_mac_addr(1, mac_addr2);
430
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);
434
435 /* must contain an address range */
436 num_macs = mac2 - mac1 + 1;
437 if (num_macs <= 0)
438 return;
439
440 if (num_macs > 50) {
441 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
442 __func__, num_macs);
443 num_macs = 50;
444 }
445
446 for (i = 0; i < num_macs; i++) {
447 u64_to_mac(mac1 + i, mac_addr);
448 if (is_valid_ethaddr(mac_addr)) {
Simon Glassfd1e9592017-08-03 12:22:11 -0600449 eth_env_set_enetaddr_by_index("eth", i + index,
450 mac_addr);
Roger Quadros38f719e2017-03-14 15:04:19 +0200451 }
452 }
453 }
454}
Cooper Jr., Franklin69e8d4b2017-06-16 17:25:08 -0500455
456bool __maybe_unused board_ti_was_eeprom_read(void)
457{
458 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
459
460 if (ep->header == TI_EEPROM_HEADER_MAGIC)
461 return true;
462 else
463 return false;
464}