blob: 9fa7b7beb63f6c0f77887daaeafde2d4c66b38a4 [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
447 if (board_id.header.id != TI_AM6_EEPROM_RECORD_BOARD_ID) {
448 pr_err("%s: Invalid board ID record!\n", __func__);
449 return -EINVAL;
450 }
451
452 /* Establish DM handle to board config EEPROM */
453 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
454 if (rc)
455 return rc;
456 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
457 if (rc)
458 return rc;
459
460 ep->header = TI_EEPROM_HEADER_MAGIC;
461
462 /* Ready to parse TLV structure. Initialize variables... */
463 *mac_addr_cnt = 0;
464
465 /*
466 * After the all-encompassing board ID record all other records follow
467 * a TLV-type scheme. Point to the first such record and then start
468 * parsing those one by one.
469 */
470 eeprom_addr = sizeof(board_id);
471
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500472 while (consecutive_bad_records < 10) {
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500473 rc = dm_i2c_read(dev, eeprom_addr, (uint8_t *)&record.header,
474 sizeof(record.header));
475 if (rc)
476 return rc;
477
478 /*
479 * Check for end of list marker. If we reached it don't go
480 * any further and stop parsing right here.
481 */
482 if (record.header.id == TI_AM6_EEPROM_RECORD_END_LIST)
483 break;
484
485 eeprom_addr += sizeof(record.header);
486
487 debug("%s: dev_addr=0x%02x header.id=%u header.len=%u\n",
488 __func__, dev_addr, record.header.id,
489 record.header.len);
490
491 /* Read record into memory if it fits */
492 if (record.header.len <= sizeof(record.data)) {
493 rc = dm_i2c_read(dev, eeprom_addr,
494 (uint8_t *)&record.data,
495 record.header.len);
496 if (rc)
497 return rc;
498
499 /* Process record */
500 rc = ti_i2c_eeprom_am6_parse_record(&record, ep,
501 mac_addr,
502 mac_addr_max_cnt,
503 mac_addr_cnt);
504 if (rc) {
505 pr_err("%s: EEPROM parsing error!\n", __func__);
506 return rc;
507 }
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500508 consecutive_bad_records = 0;
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500509 } else {
510 /*
511 * We may get here in case of larger records which
512 * are not yet understood.
513 */
514 pr_err("%s: Ignoring record id %u\n", __func__,
515 record.header.id);
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500516 consecutive_bad_records++;
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500517 }
518
519 eeprom_addr += record.header.len;
520 }
521
522 return 0;
523}
524
525int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr)
526{
527 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
528 int ret;
529
530 /*
531 * Always execute EEPROM read by not allowing to bypass it during the
532 * first invocation of SPL which happens on the R5 core.
533 */
534#if !(defined(CONFIG_SPL_BUILD) && defined(CONFIG_CPU_V7R))
535 if (ep->header == TI_EEPROM_HEADER_MAGIC) {
536 debug("%s: EEPROM has already been read\n", __func__);
537 return 0;
538 }
539#endif
540
541 ret = ti_i2c_eeprom_am6_get(bus_addr, dev_addr, ep,
542 (char **)ep->mac_addr,
543 AM6_EEPROM_HDR_NO_OF_MAC_ADDR,
544 &ep->mac_addr_cnt);
545 return ret;
546}
547
Andreas Dannenbergd036a212020-01-07 13:15:54 +0530548bool __maybe_unused board_ti_k3_is(char *name_tag)
549{
550 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
551
552 if (ep->header == TI_DEAD_EEPROM_MAGIC)
553 return false;
554 return !strncmp(ep->name, name_tag, AM6_EEPROM_HDR_NAME_LEN);
555}
556
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600557bool __maybe_unused board_ti_is(char *name_tag)
558{
559 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
560
561 if (ep->header == TI_DEAD_EEPROM_MAGIC)
562 return false;
563 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
564}
565
566bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
567{
568 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
569 int l;
570
571 if (ep->header == TI_DEAD_EEPROM_MAGIC)
572 return false;
573
574 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
575 return !strncmp(ep->version, rev_tag, l);
576}
577
578char * __maybe_unused board_ti_get_rev(void)
579{
580 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
581
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500582 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600583 return ep->version;
584}
585
586char * __maybe_unused board_ti_get_config(void)
587{
588 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
589
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500590 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600591 return ep->config;
592}
593
594char * __maybe_unused board_ti_get_name(void)
595{
596 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
597
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500598 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600599 return ep->name;
600}
601
602void __maybe_unused
603board_ti_get_eth_mac_addr(int index,
604 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
605{
606 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
607
608 if (ep->header == TI_DEAD_EEPROM_MAGIC)
609 goto fail;
610
611 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
612 goto fail;
613
614 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
615 return;
616
617fail:
618 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
619}
620
Andreas Dannenberg5a647112019-06-04 18:08:25 -0500621void __maybe_unused
622board_ti_am6_get_eth_mac_addr(int index,
623 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
624{
625 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
626
627 if (ep->header == TI_DEAD_EEPROM_MAGIC)
628 goto fail;
629
630 if (index < 0 || index >= ep->mac_addr_cnt)
631 goto fail;
632
633 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
634 return;
635
636fail:
637 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
638}
639
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530640u64 __maybe_unused board_ti_get_emif1_size(void)
641{
642 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
643
644 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
645 return 0;
646
647 return ep->emif1_size;
648}
649
650u64 __maybe_unused board_ti_get_emif2_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->emif2_size;
658}
659
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600660void __maybe_unused set_board_info_env(char *name)
661{
662 char *unknown = "unknown";
663 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
664
665 if (name)
Simon Glass6a38e412017-08-03 12:22:09 -0600666 env_set("board_name", name);
Tom Rinid1449cb2020-06-04 15:45:08 -0400667 else if (strlen(ep->name) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600668 env_set("board_name", ep->name);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600669 else
Simon Glass6a38e412017-08-03 12:22:09 -0600670 env_set("board_name", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600671
Tom Rinid1449cb2020-06-04 15:45:08 -0400672 if (strlen(ep->version) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600673 env_set("board_rev", ep->version);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600674 else
Simon Glass6a38e412017-08-03 12:22:09 -0600675 env_set("board_rev", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600676
Tom Rinid1449cb2020-06-04 15:45:08 -0400677 if (strlen(ep->serial) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600678 env_set("board_serial", ep->serial);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600679 else
Simon Glass6a38e412017-08-03 12:22:09 -0600680 env_set("board_serial", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600681}
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200682
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500683void __maybe_unused set_board_info_env_am6(char *name)
684{
685 char *unknown = "unknown";
686 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
687
688 if (name)
689 env_set("board_name", name);
Tom Rinid1449cb2020-06-04 15:45:08 -0400690 else if (strlen(ep->name) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500691 env_set("board_name", ep->name);
692 else
693 env_set("board_name", unknown);
694
Tom Rinid1449cb2020-06-04 15:45:08 -0400695 if (strlen(ep->version) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500696 env_set("board_rev", ep->version);
697 else
698 env_set("board_rev", unknown);
699
Tom Rinid1449cb2020-06-04 15:45:08 -0400700 if (strlen(ep->software_revision) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500701 env_set("board_software_revision", ep->software_revision);
702 else
703 env_set("board_software_revision", unknown);
704
Tom Rinid1449cb2020-06-04 15:45:08 -0400705 if (strlen(ep->serial) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500706 env_set("board_serial", ep->serial);
707 else
708 env_set("board_serial", unknown);
709}
710
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200711static u64 mac_to_u64(u8 mac[6])
712{
713 int i;
714 u64 addr = 0;
715
716 for (i = 0; i < 6; i++) {
717 addr <<= 8;
718 addr |= mac[i];
719 }
720
721 return addr;
722}
723
724static void u64_to_mac(u64 addr, u8 mac[6])
725{
726 mac[5] = addr;
727 mac[4] = addr >> 8;
728 mac[3] = addr >> 16;
729 mac[2] = addr >> 24;
730 mac[1] = addr >> 32;
731 mac[0] = addr >> 40;
732}
733
734void board_ti_set_ethaddr(int index)
735{
736 uint8_t mac_addr[6];
737 int i;
738 u64 mac1, mac2;
739 u8 mac_addr1[6], mac_addr2[6];
740 int num_macs;
741 /*
742 * Export any Ethernet MAC addresses from EEPROM.
743 * The 2 MAC addresses in EEPROM define the address range.
744 */
745 board_ti_get_eth_mac_addr(0, mac_addr1);
746 board_ti_get_eth_mac_addr(1, mac_addr2);
747
748 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
749 mac1 = mac_to_u64(mac_addr1);
750 mac2 = mac_to_u64(mac_addr2);
751
752 /* must contain an address range */
753 num_macs = mac2 - mac1 + 1;
754 if (num_macs <= 0)
755 return;
756
757 if (num_macs > 50) {
758 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
759 __func__, num_macs);
760 num_macs = 50;
761 }
762
763 for (i = 0; i < num_macs; i++) {
764 u64_to_mac(mac1 + i, mac_addr);
765 if (is_valid_ethaddr(mac_addr)) {
Simon Glass8551d552017-08-03 12:22:11 -0600766 eth_env_set_enetaddr_by_index("eth", i + index,
767 mac_addr);
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200768 }
769 }
770 }
771}
Cooper Jr., Frankline1328c72017-06-16 17:25:08 -0500772
Andreas Dannenberg5a647112019-06-04 18:08:25 -0500773void board_ti_am6_set_ethaddr(int index, int count)
774{
775 u8 mac_addr[6];
776 int i;
777
778 for (i = 0; i < count; i++) {
779 board_ti_am6_get_eth_mac_addr(i, mac_addr);
780 if (is_valid_ethaddr(mac_addr))
781 eth_env_set_enetaddr_by_index("eth", i + index,
782 mac_addr);
783 }
784}
785
Cooper Jr., Frankline1328c72017-06-16 17:25:08 -0500786bool __maybe_unused board_ti_was_eeprom_read(void)
787{
788 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
789
790 if (ep->header == TI_EEPROM_HEADER_MAGIC)
791 return true;
792 else
793 return false;
794}