blob: c37629fe8ab31f7008e4db01e705bdb0adb6da3b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lokesh Vutla35f7a972016-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 Vutla35f7a972016-02-24 12:30:54 -06008 */
9
10#include <common.h>
Simon Glasseba6b8d2019-11-14 12:57:50 -070011#include <eeprom.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <net.h>
Andreas Dannenbergf998a822019-06-04 18:08:24 -050014#include <asm/arch/hardware.h>
Lokesh Vutla35f7a972016-02-24 12:30:54 -060015#include <asm/omap_common.h>
Cooper Jr., Franklinf2598de2017-04-20 10:25:44 -050016#include <dm/uclass.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060017#include <env.h>
Lokesh Vutla35f7a972016-02-24 12:30:54 -060018#include <i2c.h>
Caleb Robey0dfcc932020-01-02 08:17:25 -060019#include <mmc.h>
20#include <errno.h>
21#include <malloc.h>
Lokesh Vutla35f7a972016-02-24 12:30:54 -060022
23#include "board_detect.h"
24
Igor Opaniukf7c91762021-02-09 13:52:45 +020025#if !CONFIG_IS_ENABLED(DM_I2C)
Lokesh Vutla35f7a972016-02-24 12:30:54 -060026/**
27 * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
28 * @i2c_bus: i2c bus number to initialize
29 * @dev_addr: Device address to probe for
30 *
31 * Return: 0 on success or corresponding error on failure.
32 */
33static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
34{
35 int rc;
36
37 if (i2c_bus >= 0) {
38 rc = i2c_set_bus_num(i2c_bus);
39 if (rc)
40 return rc;
41 }
42
43 return i2c_probe(dev_addr);
44}
45
46/**
47 * ti_i2c_eeprom_read - Read data from an EEPROM
48 * @dev_addr: The device address of the EEPROM
49 * @offset: Offset to start reading in the EEPROM
50 * @ep: Pointer to a buffer to read into
51 * @epsize: Size of buffer
52 *
53 * Return: 0 on success or corresponding result of i2c_read
54 */
55static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
56 uchar *ep, int epsize)
57{
Jean-Jacques Hiblot52a51512018-12-07 14:50:49 +010058 return i2c_read(dev_addr, offset, 2, ep, epsize);
Lokesh Vutla35f7a972016-02-24 12:30:54 -060059}
Andreas Dannenberge315a662018-12-07 14:50:47 +010060#endif
Lokesh Vutla35f7a972016-02-24 12:30:54 -060061
62/**
63 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
64 * @s: eeprom string (should be NULL terminated)
65 *
66 * Some Board manufacturers do not add a NULL termination at the
67 * end of string, instead some binary information is kludged in, hence
68 * convert the string to just printable characters of ASCII chart.
69 */
70static void __maybe_unused ti_eeprom_string_cleanup(char *s)
71{
72 int i, l;
73
74 l = strlen(s);
75 for (i = 0; i < l; i++, s++)
76 if (*s < ' ' || *s > '~') {
77 *s = 0;
78 break;
79 }
80}
81
82__weak void gpi2c_init(void)
83{
84}
85
86static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
87 u32 header, u32 size, uint8_t *ep)
88{
Lokesh Vutla35f7a972016-02-24 12:30:54 -060089 int rc;
90
Igor Opaniukf7c91762021-02-09 13:52:45 +020091#if CONFIG_IS_ENABLED(DM_I2C)
Andreas Dannenberge315a662018-12-07 14:50:47 +010092 struct udevice *dev;
93 struct udevice *bus;
94
95 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
96 if (rc)
97 return rc;
Andreas Dannenberg48034922020-01-07 13:15:53 +053098 rc = dm_i2c_probe(bus, dev_addr, 0, &dev);
Andreas Dannenberge315a662018-12-07 14:50:47 +010099 if (rc)
100 return rc;
101
102 /*
103 * Read the header first then only read the other contents.
104 */
Nishanth Menon9177b302022-06-17 13:26:12 -0500105 rc = i2c_set_chip_offset_len(dev, 1);
Andreas Dannenberge315a662018-12-07 14:50:47 +0100106 if (rc)
107 return rc;
108
Nishanth Menon570c3bb2022-06-17 13:26:11 -0500109 /*
110 * Skip checking result here since this could be a valid i2c read fail
Nishanth Menon9177b302022-06-17 13:26:12 -0500111 * on some boards that use 2 byte addressing.
112 * We must allow for fall through to check the data if 2 byte
Nishanth Menon570c3bb2022-06-17 13:26:11 -0500113 * addressing works
114 */
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300115 (void)dm_i2c_read(dev, 0, ep, size);
Andreas Dannenberge315a662018-12-07 14:50:47 +0100116
117 /* Corrupted data??? */
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300118 if (*((u32 *)ep) != header) {
Andreas Dannenberge315a662018-12-07 14:50:47 +0100119 /*
120 * read the eeprom header using i2c again, but use only a
Nishanth Menon9177b302022-06-17 13:26:12 -0500121 * 2 byte address (some newer boards need this..)
Andreas Dannenberge315a662018-12-07 14:50:47 +0100122 */
Nishanth Menon9177b302022-06-17 13:26:12 -0500123 rc = i2c_set_chip_offset_len(dev, 2);
Michal Simek888f10d2020-08-27 15:43:48 +0200124 if (rc)
125 return rc;
Andreas Dannenberge315a662018-12-07 14:50:47 +0100126
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300127 rc = dm_i2c_read(dev, 0, ep, size);
Andreas Dannenberge315a662018-12-07 14:50:47 +0100128 if (rc)
129 return rc;
130 }
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300131 if (*((u32 *)ep) != header)
Andreas Dannenberge315a662018-12-07 14:50:47 +0100132 return -1;
Andreas Dannenberge315a662018-12-07 14:50:47 +0100133#else
134 u32 byte;
135
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600136 gpi2c_init();
137 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
138 if (rc)
139 return rc;
140
141 /*
142 * Read the header first then only read the other contents.
143 */
Nishanth Menon9177b302022-06-17 13:26:12 -0500144 byte = 1;
Cooper Jr., Franklin5b89c972017-04-20 10:25:45 -0500145
Nishanth Menon570c3bb2022-06-17 13:26:11 -0500146 /*
147 * Skip checking result here since this could be a valid i2c read fail
Nishanth Menon9177b302022-06-17 13:26:12 -0500148 * on some boards that use 2 byte addressing.
149 * We must allow for fall through to check the data if 2 byte
Nishanth Menon570c3bb2022-06-17 13:26:11 -0500150 * addressing works
151 */
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300152 (void)i2c_read(dev_addr, 0x0, byte, ep, size);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600153
154 /* Corrupted data??? */
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300155 if (*((u32 *)ep) != header) {
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600156 /*
157 * read the eeprom header using i2c again, but use only a
Nishanth Menon9177b302022-06-17 13:26:12 -0500158 * 2 byte address (some newer boards need this..)
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600159 */
Nishanth Menon9177b302022-06-17 13:26:12 -0500160 byte = 2;
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300161 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600162 if (rc)
163 return rc;
164 }
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300165 if (*((u32 *)ep) != header)
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600166 return -1;
Andreas Dannenberge315a662018-12-07 14:50:47 +0100167#endif
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600168 return 0;
169}
170
Caleb Robey0dfcc932020-01-02 08:17:25 -0600171int __maybe_unused ti_emmc_boardid_get(void)
172{
173 int rc;
174 struct udevice *dev;
175 struct mmc *mmc;
176 struct ti_common_eeprom *ep;
177 struct ti_am_eeprom brdid;
178 struct blk_desc *bdesc;
179 uchar *buffer;
180
181 ep = TI_EEPROM_DATA;
182 if (ep->header == TI_EEPROM_HEADER_MAGIC)
183 return 0; /* EEPROM has already been read */
184
185 /* Initialize with a known bad marker for emmc fails.. */
186 ep->header = TI_DEAD_EEPROM_MAGIC;
187 ep->name[0] = 0x0;
188 ep->version[0] = 0x0;
189 ep->serial[0] = 0x0;
190 ep->config[0] = 0x0;
191
192 /* uclass object initialization */
193 rc = mmc_initialize(NULL);
194 if (rc)
195 return rc;
196
197 /* Set device to /dev/mmcblk1 */
198 rc = uclass_get_device(UCLASS_MMC, 1, &dev);
199 if (rc)
200 return rc;
201
202 /* Grab the mmc device */
203 mmc = mmc_get_mmc_dev(dev);
204 if (!mmc)
205 return -ENODEV;
206
207 /* mmc hardware initialization routine */
208 mmc_init(mmc);
209
210 /* Set partition to /dev/mmcblk1boot1 */
211 rc = mmc_switch_part(mmc, 2);
212 if (rc)
213 return rc;
214
215 buffer = malloc(mmc->read_bl_len);
216 if (!buffer)
217 return -ENOMEM;
218
219 bdesc = mmc_get_blk_desc(mmc);
220
221 /* blk_dread returns the number of blocks read*/
222 if (blk_dread(bdesc, 0L, 1, buffer) != 1) {
223 rc = -EIO;
224 goto cleanup;
225 }
226
227 memcpy(&brdid, buffer, sizeof(brdid));
228
229 /* Write out the ep struct values */
230 ep->header = brdid.header;
231 strlcpy(ep->name, brdid.name, TI_EEPROM_HDR_NAME_LEN + 1);
232 ti_eeprom_string_cleanup(ep->name);
233 strlcpy(ep->version, brdid.version, TI_EEPROM_HDR_REV_LEN + 1);
234 ti_eeprom_string_cleanup(ep->version);
235 strlcpy(ep->serial, brdid.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
236 ti_eeprom_string_cleanup(ep->serial);
237
238cleanup:
239 free(buffer);
240
241 return rc;
242}
243
Nishanth Menon27856ff2017-06-16 17:25:04 -0500244int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
245{
246 struct ti_common_eeprom *ep;
247
248 if (!name || !rev)
249 return -1;
250
251 ep = TI_EEPROM_DATA;
252 if (ep->header == TI_EEPROM_HEADER_MAGIC)
253 goto already_set;
254
255 /* Set to 0 all fields */
256 memset(ep, 0, sizeof(*ep));
257 strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
258 strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
259 /* Some dummy serial number to identify the platform */
260 strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
261 /* Mark it with a valid header */
262 ep->header = TI_EEPROM_HEADER_MAGIC;
263
264already_set:
265 return 0;
266}
267
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600268int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
269{
270 int rc;
271 struct ti_am_eeprom am_ep;
272 struct ti_common_eeprom *ep;
273
274 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota096a762016-12-01 10:37:03 +0100275#ifndef CONFIG_SPL_BUILD
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600276 if (ep->header == TI_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota096a762016-12-01 10:37:03 +0100277 return 0; /* EEPROM has already been read */
278#endif
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600279
280 /* Initialize with a known bad marker for i2c fails.. */
281 ep->header = TI_DEAD_EEPROM_MAGIC;
282 ep->name[0] = 0x0;
283 ep->version[0] = 0x0;
284 ep->serial[0] = 0x0;
Nishanth Menone2b0a522016-10-11 12:39:04 -0500285 ep->config[0] = 0x0;
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600286
287 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
288 sizeof(am_ep), (uint8_t *)&am_ep);
289 if (rc)
290 return rc;
291
292 ep->header = am_ep.header;
293 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
294 ti_eeprom_string_cleanup(ep->name);
295
296 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
297 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
298 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
299 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
300 else
301 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
302 ti_eeprom_string_cleanup(ep->version);
303 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
304 ti_eeprom_string_cleanup(ep->serial);
305 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
306 ti_eeprom_string_cleanup(ep->config);
307
308 memcpy(ep->mac_addr, am_ep.mac_addr,
309 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
310
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600311 return 0;
312}
313
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530314int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
315{
316 int rc, offset = 0;
317 struct dra7_eeprom dra7_ep;
318 struct ti_common_eeprom *ep;
319
320 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota096a762016-12-01 10:37:03 +0100321#ifndef CONFIG_SPL_BUILD
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530322 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota096a762016-12-01 10:37:03 +0100323 return 0; /* EEPROM has already been read */
324#endif
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530325
326 /* Initialize with a known bad marker for i2c fails.. */
Nishanth Menon21e54c02016-10-11 12:39:03 -0500327 ep->header = TI_DEAD_EEPROM_MAGIC;
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530328 ep->name[0] = 0x0;
329 ep->version[0] = 0x0;
330 ep->serial[0] = 0x0;
Nishanth Menone2b0a522016-10-11 12:39:04 -0500331 ep->config[0] = 0x0;
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530332 ep->emif1_size = 0;
333 ep->emif2_size = 0;
334
335 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
336 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
337 if (rc)
338 return rc;
339
340 ep->header = dra7_ep.header;
341 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
342 ti_eeprom_string_cleanup(ep->name);
343
344 offset = dra7_ep.version_major - 1;
345
346 /* Rev F is skipped */
347 if (offset >= 5)
348 offset = offset + 1;
349 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
350 'A' + offset, dra7_ep.version_minor);
351 ti_eeprom_string_cleanup(ep->version);
352 ep->emif1_size = (u64)dra7_ep.emif1_size;
353 ep->emif2_size = (u64)dra7_ep.emif2_size;
354 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
355 ti_eeprom_string_cleanup(ep->config);
356
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530357 return 0;
358}
359
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500360static int ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record *record,
361 struct ti_am6_eeprom *ep,
362 char **mac_addr,
363 u8 mac_addr_max_cnt,
364 u8 *mac_addr_cnt)
365{
366 switch (record->header.id) {
367 case TI_AM6_EEPROM_RECORD_BOARD_INFO:
368 if (record->header.len != sizeof(record->data.board_info))
369 return -EINVAL;
370
371 if (!ep)
372 break;
373
374 /* Populate (and clean, if needed) the board name */
375 strlcpy(ep->name, record->data.board_info.name,
376 sizeof(ep->name));
377 ti_eeprom_string_cleanup(ep->name);
378
379 /* Populate selected other fields from the board info record */
380 strlcpy(ep->version, record->data.board_info.version,
381 sizeof(ep->version));
382 strlcpy(ep->software_revision,
383 record->data.board_info.software_revision,
384 sizeof(ep->software_revision));
385 strlcpy(ep->serial, record->data.board_info.serial,
386 sizeof(ep->serial));
387 break;
388 case TI_AM6_EEPROM_RECORD_MAC_INFO:
389 if (record->header.len != sizeof(record->data.mac_info))
390 return -EINVAL;
391
392 if (!mac_addr || !mac_addr_max_cnt)
393 break;
394
395 *mac_addr_cnt = ((record->data.mac_info.mac_control &
396 TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK) >>
397 TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT) + 1;
398
399 /*
400 * The EEPROM can (but may not) hold a very large amount
401 * of MAC addresses, by far exceeding what we want/can store
402 * in the common memory array, so only grab what we can fit.
403 * Note that a value of 0 means 1 MAC address, and so on.
404 */
405 *mac_addr_cnt = min(*mac_addr_cnt, mac_addr_max_cnt);
406
407 memcpy(mac_addr, record->data.mac_info.mac_addr,
408 *mac_addr_cnt * TI_EEPROM_HDR_ETH_ALEN);
409 break;
410 case 0x00:
411 /* Illegal value... Fall through... */
412 case 0xFF:
413 /* Illegal value... Something went horribly wrong... */
414 return -EINVAL;
415 default:
416 pr_warn("%s: Ignoring record id %u\n", __func__,
417 record->header.id);
418 }
419
420 return 0;
421}
422
423int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr,
424 struct ti_am6_eeprom *ep,
425 char **mac_addr,
426 u8 mac_addr_max_cnt,
427 u8 *mac_addr_cnt)
428{
429 struct udevice *dev;
430 struct udevice *bus;
431 unsigned int eeprom_addr;
432 struct ti_am6_eeprom_record_board_id board_id;
433 struct ti_am6_eeprom_record record;
434 int rc;
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500435 int consecutive_bad_records = 0;
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500436
437 /* Initialize with a known bad marker for i2c fails.. */
438 memset(ep, 0, sizeof(*ep));
439 ep->header = TI_DEAD_EEPROM_MAGIC;
440
441 /* Read the board ID record which is always the first EEPROM record */
442 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
443 sizeof(board_id), (uint8_t *)&board_id);
444 if (rc)
445 return rc;
446
Nishanth Menon892d5c02022-09-21 08:38:42 -0500447 /*
448 * Handle case of bad 2 byte eeproms that responds to 1 byte addressing
449 * but gets stuck in const addressing when read requests are performed
450 * on offsets. We re-read the board ID to ensure we have sane data back
451 */
452 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
453 sizeof(board_id), (uint8_t *)&board_id);
454 if (rc)
455 return rc;
456
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500457 if (board_id.header.id != TI_AM6_EEPROM_RECORD_BOARD_ID) {
458 pr_err("%s: Invalid board ID record!\n", __func__);
459 return -EINVAL;
460 }
461
462 /* Establish DM handle to board config EEPROM */
463 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
464 if (rc)
465 return rc;
466 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
467 if (rc)
468 return rc;
469
470 ep->header = TI_EEPROM_HEADER_MAGIC;
471
472 /* Ready to parse TLV structure. Initialize variables... */
473 *mac_addr_cnt = 0;
474
475 /*
476 * After the all-encompassing board ID record all other records follow
477 * a TLV-type scheme. Point to the first such record and then start
478 * parsing those one by one.
479 */
480 eeprom_addr = sizeof(board_id);
481
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500482 while (consecutive_bad_records < 10) {
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500483 rc = dm_i2c_read(dev, eeprom_addr, (uint8_t *)&record.header,
484 sizeof(record.header));
485 if (rc)
486 return rc;
487
488 /*
489 * Check for end of list marker. If we reached it don't go
490 * any further and stop parsing right here.
491 */
492 if (record.header.id == TI_AM6_EEPROM_RECORD_END_LIST)
493 break;
494
495 eeprom_addr += sizeof(record.header);
496
497 debug("%s: dev_addr=0x%02x header.id=%u header.len=%u\n",
498 __func__, dev_addr, record.header.id,
499 record.header.len);
500
501 /* Read record into memory if it fits */
502 if (record.header.len <= sizeof(record.data)) {
503 rc = dm_i2c_read(dev, eeprom_addr,
504 (uint8_t *)&record.data,
505 record.header.len);
506 if (rc)
507 return rc;
508
509 /* Process record */
510 rc = ti_i2c_eeprom_am6_parse_record(&record, ep,
511 mac_addr,
512 mac_addr_max_cnt,
513 mac_addr_cnt);
514 if (rc) {
515 pr_err("%s: EEPROM parsing error!\n", __func__);
516 return rc;
517 }
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500518 consecutive_bad_records = 0;
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500519 } else {
520 /*
521 * We may get here in case of larger records which
522 * are not yet understood.
523 */
524 pr_err("%s: Ignoring record id %u\n", __func__,
525 record.header.id);
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500526 consecutive_bad_records++;
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500527 }
528
529 eeprom_addr += record.header.len;
530 }
531
532 return 0;
533}
534
535int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr)
536{
537 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
538 int ret;
539
540 /*
541 * Always execute EEPROM read by not allowing to bypass it during the
542 * first invocation of SPL which happens on the R5 core.
543 */
544#if !(defined(CONFIG_SPL_BUILD) && defined(CONFIG_CPU_V7R))
545 if (ep->header == TI_EEPROM_HEADER_MAGIC) {
546 debug("%s: EEPROM has already been read\n", __func__);
547 return 0;
548 }
549#endif
550
551 ret = ti_i2c_eeprom_am6_get(bus_addr, dev_addr, ep,
552 (char **)ep->mac_addr,
553 AM6_EEPROM_HDR_NO_OF_MAC_ADDR,
554 &ep->mac_addr_cnt);
555 return ret;
556}
557
Andreas Dannenbergd036a212020-01-07 13:15:54 +0530558bool __maybe_unused board_ti_k3_is(char *name_tag)
559{
560 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
561
562 if (ep->header == TI_DEAD_EEPROM_MAGIC)
563 return false;
564 return !strncmp(ep->name, name_tag, AM6_EEPROM_HDR_NAME_LEN);
565}
566
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600567bool __maybe_unused board_ti_is(char *name_tag)
568{
569 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
570
571 if (ep->header == TI_DEAD_EEPROM_MAGIC)
572 return false;
573 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
574}
575
576bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
577{
578 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
579 int l;
580
581 if (ep->header == TI_DEAD_EEPROM_MAGIC)
582 return false;
583
584 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
585 return !strncmp(ep->version, rev_tag, l);
586}
587
588char * __maybe_unused board_ti_get_rev(void)
589{
590 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
591
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500592 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600593 return ep->version;
594}
595
596char * __maybe_unused board_ti_get_config(void)
597{
598 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
599
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500600 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600601 return ep->config;
602}
603
604char * __maybe_unused board_ti_get_name(void)
605{
606 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
607
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500608 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600609 return ep->name;
610}
611
612void __maybe_unused
613board_ti_get_eth_mac_addr(int index,
614 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
615{
616 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
617
618 if (ep->header == TI_DEAD_EEPROM_MAGIC)
619 goto fail;
620
621 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
622 goto fail;
623
624 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
625 return;
626
627fail:
628 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
629}
630
Andreas Dannenberg5a647112019-06-04 18:08:25 -0500631void __maybe_unused
632board_ti_am6_get_eth_mac_addr(int index,
633 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
634{
635 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
636
637 if (ep->header == TI_DEAD_EEPROM_MAGIC)
638 goto fail;
639
640 if (index < 0 || index >= ep->mac_addr_cnt)
641 goto fail;
642
643 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
644 return;
645
646fail:
647 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
648}
649
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530650u64 __maybe_unused board_ti_get_emif1_size(void)
651{
652 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
653
654 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
655 return 0;
656
657 return ep->emif1_size;
658}
659
660u64 __maybe_unused board_ti_get_emif2_size(void)
661{
662 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
663
664 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
665 return 0;
666
667 return ep->emif2_size;
668}
669
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600670void __maybe_unused set_board_info_env(char *name)
671{
672 char *unknown = "unknown";
673 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
674
675 if (name)
Simon Glass6a38e412017-08-03 12:22:09 -0600676 env_set("board_name", name);
Tom Rinid1449cb2020-06-04 15:45:08 -0400677 else if (strlen(ep->name) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600678 env_set("board_name", ep->name);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600679 else
Simon Glass6a38e412017-08-03 12:22:09 -0600680 env_set("board_name", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600681
Tom Rinid1449cb2020-06-04 15:45:08 -0400682 if (strlen(ep->version) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600683 env_set("board_rev", ep->version);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600684 else
Simon Glass6a38e412017-08-03 12:22:09 -0600685 env_set("board_rev", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600686
Tom Rinid1449cb2020-06-04 15:45:08 -0400687 if (strlen(ep->serial) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600688 env_set("board_serial", ep->serial);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600689 else
Simon Glass6a38e412017-08-03 12:22:09 -0600690 env_set("board_serial", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600691}
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200692
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500693void __maybe_unused set_board_info_env_am6(char *name)
694{
695 char *unknown = "unknown";
696 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
697
698 if (name)
699 env_set("board_name", name);
Tom Rinid1449cb2020-06-04 15:45:08 -0400700 else if (strlen(ep->name) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500701 env_set("board_name", ep->name);
702 else
703 env_set("board_name", unknown);
704
Tom Rinid1449cb2020-06-04 15:45:08 -0400705 if (strlen(ep->version) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500706 env_set("board_rev", ep->version);
707 else
708 env_set("board_rev", unknown);
709
Tom Rinid1449cb2020-06-04 15:45:08 -0400710 if (strlen(ep->software_revision) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500711 env_set("board_software_revision", ep->software_revision);
712 else
713 env_set("board_software_revision", unknown);
714
Tom Rinid1449cb2020-06-04 15:45:08 -0400715 if (strlen(ep->serial) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500716 env_set("board_serial", ep->serial);
717 else
718 env_set("board_serial", unknown);
719}
720
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200721static u64 mac_to_u64(u8 mac[6])
722{
723 int i;
724 u64 addr = 0;
725
726 for (i = 0; i < 6; i++) {
727 addr <<= 8;
728 addr |= mac[i];
729 }
730
731 return addr;
732}
733
734static void u64_to_mac(u64 addr, u8 mac[6])
735{
736 mac[5] = addr;
737 mac[4] = addr >> 8;
738 mac[3] = addr >> 16;
739 mac[2] = addr >> 24;
740 mac[1] = addr >> 32;
741 mac[0] = addr >> 40;
742}
743
744void board_ti_set_ethaddr(int index)
745{
746 uint8_t mac_addr[6];
747 int i;
748 u64 mac1, mac2;
749 u8 mac_addr1[6], mac_addr2[6];
750 int num_macs;
751 /*
752 * Export any Ethernet MAC addresses from EEPROM.
753 * The 2 MAC addresses in EEPROM define the address range.
754 */
755 board_ti_get_eth_mac_addr(0, mac_addr1);
756 board_ti_get_eth_mac_addr(1, mac_addr2);
757
758 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
759 mac1 = mac_to_u64(mac_addr1);
760 mac2 = mac_to_u64(mac_addr2);
761
762 /* must contain an address range */
763 num_macs = mac2 - mac1 + 1;
764 if (num_macs <= 0)
765 return;
766
767 if (num_macs > 50) {
768 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
769 __func__, num_macs);
770 num_macs = 50;
771 }
772
773 for (i = 0; i < num_macs; i++) {
774 u64_to_mac(mac1 + i, mac_addr);
775 if (is_valid_ethaddr(mac_addr)) {
Simon Glass8551d552017-08-03 12:22:11 -0600776 eth_env_set_enetaddr_by_index("eth", i + index,
777 mac_addr);
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200778 }
779 }
780 }
781}
Cooper Jr., Frankline1328c72017-06-16 17:25:08 -0500782
Andreas Dannenberg5a647112019-06-04 18:08:25 -0500783void board_ti_am6_set_ethaddr(int index, int count)
784{
785 u8 mac_addr[6];
786 int i;
787
788 for (i = 0; i < count; i++) {
789 board_ti_am6_get_eth_mac_addr(i, mac_addr);
790 if (is_valid_ethaddr(mac_addr))
791 eth_env_set_enetaddr_by_index("eth", i + index,
792 mac_addr);
793 }
794}
795
Cooper Jr., Frankline1328c72017-06-16 17:25:08 -0500796bool __maybe_unused board_ti_was_eeprom_read(void)
797{
798 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
799
800 if (ep->header == TI_EEPROM_HEADER_MAGIC)
801 return true;
802 else
803 return false;
804}