blob: ac39b25cd426be564e82104119ebdc72c7570402 [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060022#include <linux/printk.h>
Lokesh Vutla35f7a972016-02-24 12:30:54 -060023
24#include "board_detect.h"
25
Igor Opaniukf7c91762021-02-09 13:52:45 +020026#if !CONFIG_IS_ENABLED(DM_I2C)
Lokesh Vutla35f7a972016-02-24 12:30:54 -060027/**
28 * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
29 * @i2c_bus: i2c bus number to initialize
30 * @dev_addr: Device address to probe for
31 *
32 * Return: 0 on success or corresponding error on failure.
33 */
34static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
35{
36 int rc;
37
38 if (i2c_bus >= 0) {
39 rc = i2c_set_bus_num(i2c_bus);
40 if (rc)
41 return rc;
42 }
43
44 return i2c_probe(dev_addr);
45}
46
47/**
48 * ti_i2c_eeprom_read - Read data from an EEPROM
49 * @dev_addr: The device address of the EEPROM
50 * @offset: Offset to start reading in the EEPROM
51 * @ep: Pointer to a buffer to read into
52 * @epsize: Size of buffer
53 *
54 * Return: 0 on success or corresponding result of i2c_read
55 */
56static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
57 uchar *ep, int epsize)
58{
Jean-Jacques Hiblot52a51512018-12-07 14:50:49 +010059 return i2c_read(dev_addr, offset, 2, ep, epsize);
Lokesh Vutla35f7a972016-02-24 12:30:54 -060060}
Andreas Dannenberge315a662018-12-07 14:50:47 +010061#endif
Lokesh Vutla35f7a972016-02-24 12:30:54 -060062
63/**
64 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
65 * @s: eeprom string (should be NULL terminated)
66 *
67 * Some Board manufacturers do not add a NULL termination at the
68 * end of string, instead some binary information is kludged in, hence
69 * convert the string to just printable characters of ASCII chart.
70 */
71static void __maybe_unused ti_eeprom_string_cleanup(char *s)
72{
73 int i, l;
74
75 l = strlen(s);
76 for (i = 0; i < l; i++, s++)
77 if (*s < ' ' || *s > '~') {
78 *s = 0;
79 break;
80 }
81}
82
83__weak void gpi2c_init(void)
84{
85}
86
87static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
88 u32 header, u32 size, uint8_t *ep)
89{
Lokesh Vutla35f7a972016-02-24 12:30:54 -060090 int rc;
Neha Malcom Francis4459ca12022-12-13 11:57:34 +053091 uint8_t offset_test;
92 bool one_byte_addressing = true;
Lokesh Vutla35f7a972016-02-24 12:30:54 -060093
Igor Opaniukf7c91762021-02-09 13:52:45 +020094#if CONFIG_IS_ENABLED(DM_I2C)
Andreas Dannenberge315a662018-12-07 14:50:47 +010095 struct udevice *dev;
96 struct udevice *bus;
97
98 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
99 if (rc)
100 return rc;
Andreas Dannenberg48034922020-01-07 13:15:53 +0530101 rc = dm_i2c_probe(bus, dev_addr, 0, &dev);
Andreas Dannenberge315a662018-12-07 14:50:47 +0100102 if (rc)
103 return rc;
104
105 /*
106 * Read the header first then only read the other contents.
107 */
Nishanth Menon9177b302022-06-17 13:26:12 -0500108 rc = i2c_set_chip_offset_len(dev, 1);
Andreas Dannenberge315a662018-12-07 14:50:47 +0100109 if (rc)
110 return rc;
111
Nishanth Menon570c3bb2022-06-17 13:26:11 -0500112 /*
113 * Skip checking result here since this could be a valid i2c read fail
Nishanth Menon9177b302022-06-17 13:26:12 -0500114 * on some boards that use 2 byte addressing.
115 * We must allow for fall through to check the data if 2 byte
Nishanth Menon570c3bb2022-06-17 13:26:11 -0500116 * addressing works
117 */
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300118 (void)dm_i2c_read(dev, 0, ep, size);
Andreas Dannenberge315a662018-12-07 14:50:47 +0100119
Neha Malcom Francis4459ca12022-12-13 11:57:34 +0530120 if (*((u32 *)ep) != header)
121 one_byte_addressing = false;
122
123 /*
124 * Handle case of bad 2 byte eeproms that responds to 1 byte addressing
125 * but gets stuck in const addressing when read requests are performed
126 * on offsets. We perform an offset test to make sure it is not a 2 byte
127 * eeprom that works with 1 byte addressing but just without an offset
128 */
129
130 rc = dm_i2c_read(dev, 0x1, &offset_test, sizeof(offset_test));
131
132 if (*((u32 *)ep) != (header & 0xFF))
133 one_byte_addressing = false;
134
Andreas Dannenberge315a662018-12-07 14:50:47 +0100135 /* Corrupted data??? */
Neha Malcom Francis4459ca12022-12-13 11:57:34 +0530136 if (!one_byte_addressing) {
Andreas Dannenberge315a662018-12-07 14:50:47 +0100137 /*
138 * read the eeprom header using i2c again, but use only a
Nishanth Menon9177b302022-06-17 13:26:12 -0500139 * 2 byte address (some newer boards need this..)
Andreas Dannenberge315a662018-12-07 14:50:47 +0100140 */
Nishanth Menon9177b302022-06-17 13:26:12 -0500141 rc = i2c_set_chip_offset_len(dev, 2);
Michal Simek888f10d2020-08-27 15:43:48 +0200142 if (rc)
143 return rc;
Andreas Dannenberge315a662018-12-07 14:50:47 +0100144
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300145 rc = dm_i2c_read(dev, 0, ep, size);
Andreas Dannenberge315a662018-12-07 14:50:47 +0100146 if (rc)
147 return rc;
148 }
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300149 if (*((u32 *)ep) != header)
Andreas Dannenberge315a662018-12-07 14:50:47 +0100150 return -1;
Andreas Dannenberge315a662018-12-07 14:50:47 +0100151#else
152 u32 byte;
153
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600154 gpi2c_init();
155 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
156 if (rc)
157 return rc;
158
159 /*
160 * Read the header first then only read the other contents.
161 */
Nishanth Menon9177b302022-06-17 13:26:12 -0500162 byte = 1;
Cooper Jr., Franklin5b89c972017-04-20 10:25:45 -0500163
Nishanth Menon570c3bb2022-06-17 13:26:11 -0500164 /*
165 * Skip checking result here since this could be a valid i2c read fail
Nishanth Menon9177b302022-06-17 13:26:12 -0500166 * on some boards that use 2 byte addressing.
167 * We must allow for fall through to check the data if 2 byte
Nishanth Menon570c3bb2022-06-17 13:26:11 -0500168 * addressing works
169 */
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300170 (void)i2c_read(dev_addr, 0x0, byte, ep, size);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600171
Neha Malcom Francis4459ca12022-12-13 11:57:34 +0530172 if (*((u32 *)ep) != header)
173 one_byte_addressing = false;
174
175 /*
176 * Handle case of bad 2 byte eeproms that responds to 1 byte addressing
177 * but gets stuck in const addressing when read requests are performed
178 * on offsets. We perform an offset test to make sure it is not a 2 byte
179 * eeprom that works with 1 byte addressing but just without an offset
180 */
181
182 rc = i2c_read(dev_addr, 0x1, byte, &offset_test, sizeof(offset_test));
183
184 if (*((u32 *)ep) != (header & 0xFF))
185 one_byte_addressing = false;
186
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600187 /* Corrupted data??? */
Neha Malcom Francis4459ca12022-12-13 11:57:34 +0530188 if (!one_byte_addressing) {
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600189 /*
190 * read the eeprom header using i2c again, but use only a
Nishanth Menon9177b302022-06-17 13:26:12 -0500191 * 2 byte address (some newer boards need this..)
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600192 */
Nishanth Menon9177b302022-06-17 13:26:12 -0500193 byte = 2;
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300194 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600195 if (rc)
196 return rc;
197 }
Matwey V. Kornilovb90ca702022-08-23 19:05:34 +0300198 if (*((u32 *)ep) != header)
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600199 return -1;
Andreas Dannenberge315a662018-12-07 14:50:47 +0100200#endif
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600201 return 0;
202}
203
Caleb Robey0dfcc932020-01-02 08:17:25 -0600204int __maybe_unused ti_emmc_boardid_get(void)
205{
206 int rc;
207 struct udevice *dev;
208 struct mmc *mmc;
209 struct ti_common_eeprom *ep;
210 struct ti_am_eeprom brdid;
211 struct blk_desc *bdesc;
212 uchar *buffer;
213
214 ep = TI_EEPROM_DATA;
215 if (ep->header == TI_EEPROM_HEADER_MAGIC)
216 return 0; /* EEPROM has already been read */
217
218 /* Initialize with a known bad marker for emmc fails.. */
219 ep->header = TI_DEAD_EEPROM_MAGIC;
220 ep->name[0] = 0x0;
221 ep->version[0] = 0x0;
222 ep->serial[0] = 0x0;
223 ep->config[0] = 0x0;
224
225 /* uclass object initialization */
226 rc = mmc_initialize(NULL);
227 if (rc)
228 return rc;
229
230 /* Set device to /dev/mmcblk1 */
231 rc = uclass_get_device(UCLASS_MMC, 1, &dev);
232 if (rc)
233 return rc;
234
235 /* Grab the mmc device */
236 mmc = mmc_get_mmc_dev(dev);
237 if (!mmc)
238 return -ENODEV;
239
240 /* mmc hardware initialization routine */
241 mmc_init(mmc);
242
243 /* Set partition to /dev/mmcblk1boot1 */
244 rc = mmc_switch_part(mmc, 2);
245 if (rc)
246 return rc;
247
248 buffer = malloc(mmc->read_bl_len);
249 if (!buffer)
250 return -ENOMEM;
251
252 bdesc = mmc_get_blk_desc(mmc);
253
254 /* blk_dread returns the number of blocks read*/
255 if (blk_dread(bdesc, 0L, 1, buffer) != 1) {
256 rc = -EIO;
257 goto cleanup;
258 }
259
260 memcpy(&brdid, buffer, sizeof(brdid));
261
262 /* Write out the ep struct values */
263 ep->header = brdid.header;
264 strlcpy(ep->name, brdid.name, TI_EEPROM_HDR_NAME_LEN + 1);
265 ti_eeprom_string_cleanup(ep->name);
266 strlcpy(ep->version, brdid.version, TI_EEPROM_HDR_REV_LEN + 1);
267 ti_eeprom_string_cleanup(ep->version);
268 strlcpy(ep->serial, brdid.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
269 ti_eeprom_string_cleanup(ep->serial);
270
271cleanup:
272 free(buffer);
273
274 return rc;
275}
276
Nishanth Menon27856ff2017-06-16 17:25:04 -0500277int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
278{
279 struct ti_common_eeprom *ep;
280
281 if (!name || !rev)
282 return -1;
283
284 ep = TI_EEPROM_DATA;
285 if (ep->header == TI_EEPROM_HEADER_MAGIC)
286 goto already_set;
287
288 /* Set to 0 all fields */
289 memset(ep, 0, sizeof(*ep));
290 strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
291 strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
292 /* Some dummy serial number to identify the platform */
293 strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
294 /* Mark it with a valid header */
295 ep->header = TI_EEPROM_HEADER_MAGIC;
296
297already_set:
298 return 0;
299}
300
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600301int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
302{
303 int rc;
304 struct ti_am_eeprom am_ep;
305 struct ti_common_eeprom *ep;
306
307 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota096a762016-12-01 10:37:03 +0100308#ifndef CONFIG_SPL_BUILD
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600309 if (ep->header == TI_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota096a762016-12-01 10:37:03 +0100310 return 0; /* EEPROM has already been read */
311#endif
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600312
313 /* Initialize with a known bad marker for i2c fails.. */
314 ep->header = TI_DEAD_EEPROM_MAGIC;
315 ep->name[0] = 0x0;
316 ep->version[0] = 0x0;
317 ep->serial[0] = 0x0;
Nishanth Menone2b0a522016-10-11 12:39:04 -0500318 ep->config[0] = 0x0;
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600319
320 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
321 sizeof(am_ep), (uint8_t *)&am_ep);
322 if (rc)
323 return rc;
324
325 ep->header = am_ep.header;
326 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
327 ti_eeprom_string_cleanup(ep->name);
328
329 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
330 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
331 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
332 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
333 else
334 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
335 ti_eeprom_string_cleanup(ep->version);
336 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
337 ti_eeprom_string_cleanup(ep->serial);
338 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
339 ti_eeprom_string_cleanup(ep->config);
340
341 memcpy(ep->mac_addr, am_ep.mac_addr,
342 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
343
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600344 return 0;
345}
346
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530347int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
348{
349 int rc, offset = 0;
350 struct dra7_eeprom dra7_ep;
351 struct ti_common_eeprom *ep;
352
353 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota096a762016-12-01 10:37:03 +0100354#ifndef CONFIG_SPL_BUILD
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530355 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota096a762016-12-01 10:37:03 +0100356 return 0; /* EEPROM has already been read */
357#endif
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530358
359 /* Initialize with a known bad marker for i2c fails.. */
Nishanth Menon21e54c02016-10-11 12:39:03 -0500360 ep->header = TI_DEAD_EEPROM_MAGIC;
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530361 ep->name[0] = 0x0;
362 ep->version[0] = 0x0;
363 ep->serial[0] = 0x0;
Nishanth Menone2b0a522016-10-11 12:39:04 -0500364 ep->config[0] = 0x0;
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530365 ep->emif1_size = 0;
366 ep->emif2_size = 0;
367
368 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
369 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
370 if (rc)
371 return rc;
372
373 ep->header = dra7_ep.header;
374 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
375 ti_eeprom_string_cleanup(ep->name);
376
377 offset = dra7_ep.version_major - 1;
378
379 /* Rev F is skipped */
380 if (offset >= 5)
381 offset = offset + 1;
382 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
383 'A' + offset, dra7_ep.version_minor);
384 ti_eeprom_string_cleanup(ep->version);
385 ep->emif1_size = (u64)dra7_ep.emif1_size;
386 ep->emif2_size = (u64)dra7_ep.emif2_size;
387 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
388 ti_eeprom_string_cleanup(ep->config);
389
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530390 return 0;
391}
392
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500393static int ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record *record,
394 struct ti_am6_eeprom *ep,
395 char **mac_addr,
396 u8 mac_addr_max_cnt,
397 u8 *mac_addr_cnt)
398{
399 switch (record->header.id) {
400 case TI_AM6_EEPROM_RECORD_BOARD_INFO:
401 if (record->header.len != sizeof(record->data.board_info))
402 return -EINVAL;
403
404 if (!ep)
405 break;
406
407 /* Populate (and clean, if needed) the board name */
408 strlcpy(ep->name, record->data.board_info.name,
409 sizeof(ep->name));
410 ti_eeprom_string_cleanup(ep->name);
411
412 /* Populate selected other fields from the board info record */
413 strlcpy(ep->version, record->data.board_info.version,
414 sizeof(ep->version));
415 strlcpy(ep->software_revision,
416 record->data.board_info.software_revision,
417 sizeof(ep->software_revision));
418 strlcpy(ep->serial, record->data.board_info.serial,
419 sizeof(ep->serial));
420 break;
421 case TI_AM6_EEPROM_RECORD_MAC_INFO:
422 if (record->header.len != sizeof(record->data.mac_info))
423 return -EINVAL;
424
425 if (!mac_addr || !mac_addr_max_cnt)
426 break;
427
428 *mac_addr_cnt = ((record->data.mac_info.mac_control &
429 TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK) >>
430 TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT) + 1;
431
432 /*
433 * The EEPROM can (but may not) hold a very large amount
434 * of MAC addresses, by far exceeding what we want/can store
435 * in the common memory array, so only grab what we can fit.
436 * Note that a value of 0 means 1 MAC address, and so on.
437 */
438 *mac_addr_cnt = min(*mac_addr_cnt, mac_addr_max_cnt);
439
440 memcpy(mac_addr, record->data.mac_info.mac_addr,
441 *mac_addr_cnt * TI_EEPROM_HDR_ETH_ALEN);
442 break;
443 case 0x00:
444 /* Illegal value... Fall through... */
445 case 0xFF:
446 /* Illegal value... Something went horribly wrong... */
447 return -EINVAL;
448 default:
449 pr_warn("%s: Ignoring record id %u\n", __func__,
450 record->header.id);
451 }
452
453 return 0;
454}
455
456int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr,
457 struct ti_am6_eeprom *ep,
458 char **mac_addr,
459 u8 mac_addr_max_cnt,
460 u8 *mac_addr_cnt)
461{
462 struct udevice *dev;
463 struct udevice *bus;
464 unsigned int eeprom_addr;
465 struct ti_am6_eeprom_record_board_id board_id;
466 struct ti_am6_eeprom_record record;
467 int rc;
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500468 int consecutive_bad_records = 0;
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500469
470 /* Initialize with a known bad marker for i2c fails.. */
471 memset(ep, 0, sizeof(*ep));
472 ep->header = TI_DEAD_EEPROM_MAGIC;
473
474 /* Read the board ID record which is always the first EEPROM record */
475 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
476 sizeof(board_id), (uint8_t *)&board_id);
477 if (rc)
478 return rc;
479
480 if (board_id.header.id != TI_AM6_EEPROM_RECORD_BOARD_ID) {
481 pr_err("%s: Invalid board ID record!\n", __func__);
482 return -EINVAL;
483 }
484
485 /* Establish DM handle to board config EEPROM */
486 rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
487 if (rc)
488 return rc;
489 rc = i2c_get_chip(bus, dev_addr, 1, &dev);
490 if (rc)
491 return rc;
492
493 ep->header = TI_EEPROM_HEADER_MAGIC;
494
495 /* Ready to parse TLV structure. Initialize variables... */
496 *mac_addr_cnt = 0;
497
498 /*
499 * After the all-encompassing board ID record all other records follow
500 * a TLV-type scheme. Point to the first such record and then start
501 * parsing those one by one.
502 */
503 eeprom_addr = sizeof(board_id);
504
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500505 while (consecutive_bad_records < 10) {
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500506 rc = dm_i2c_read(dev, eeprom_addr, (uint8_t *)&record.header,
507 sizeof(record.header));
508 if (rc)
509 return rc;
510
511 /*
512 * Check for end of list marker. If we reached it don't go
513 * any further and stop parsing right here.
514 */
515 if (record.header.id == TI_AM6_EEPROM_RECORD_END_LIST)
516 break;
517
518 eeprom_addr += sizeof(record.header);
519
520 debug("%s: dev_addr=0x%02x header.id=%u header.len=%u\n",
521 __func__, dev_addr, record.header.id,
522 record.header.len);
523
524 /* Read record into memory if it fits */
525 if (record.header.len <= sizeof(record.data)) {
526 rc = dm_i2c_read(dev, eeprom_addr,
527 (uint8_t *)&record.data,
528 record.header.len);
529 if (rc)
530 return rc;
531
532 /* Process record */
533 rc = ti_i2c_eeprom_am6_parse_record(&record, ep,
534 mac_addr,
535 mac_addr_max_cnt,
536 mac_addr_cnt);
537 if (rc) {
538 pr_err("%s: EEPROM parsing error!\n", __func__);
539 return rc;
540 }
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500541 consecutive_bad_records = 0;
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500542 } else {
543 /*
544 * We may get here in case of larger records which
545 * are not yet understood.
546 */
547 pr_err("%s: Ignoring record id %u\n", __func__,
548 record.header.id);
Nishanth Menon77c94ea2022-06-17 13:26:10 -0500549 consecutive_bad_records++;
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500550 }
551
552 eeprom_addr += record.header.len;
553 }
554
555 return 0;
556}
557
558int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr)
559{
560 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
561 int ret;
562
563 /*
564 * Always execute EEPROM read by not allowing to bypass it during the
565 * first invocation of SPL which happens on the R5 core.
566 */
567#if !(defined(CONFIG_SPL_BUILD) && defined(CONFIG_CPU_V7R))
568 if (ep->header == TI_EEPROM_HEADER_MAGIC) {
569 debug("%s: EEPROM has already been read\n", __func__);
570 return 0;
571 }
572#endif
573
574 ret = ti_i2c_eeprom_am6_get(bus_addr, dev_addr, ep,
575 (char **)ep->mac_addr,
576 AM6_EEPROM_HDR_NO_OF_MAC_ADDR,
577 &ep->mac_addr_cnt);
578 return ret;
579}
580
Andreas Dannenbergd036a212020-01-07 13:15:54 +0530581bool __maybe_unused board_ti_k3_is(char *name_tag)
582{
583 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
584
585 if (ep->header == TI_DEAD_EEPROM_MAGIC)
586 return false;
587 return !strncmp(ep->name, name_tag, AM6_EEPROM_HDR_NAME_LEN);
588}
589
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600590bool __maybe_unused board_ti_is(char *name_tag)
591{
592 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
593
594 if (ep->header == TI_DEAD_EEPROM_MAGIC)
595 return false;
596 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
597}
598
599bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
600{
601 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
602 int l;
603
604 if (ep->header == TI_DEAD_EEPROM_MAGIC)
605 return false;
606
607 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
608 return !strncmp(ep->version, rev_tag, l);
609}
610
611char * __maybe_unused board_ti_get_rev(void)
612{
613 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
614
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500615 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600616 return ep->version;
617}
618
619char * __maybe_unused board_ti_get_config(void)
620{
621 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
622
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500623 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600624 return ep->config;
625}
626
627char * __maybe_unused board_ti_get_name(void)
628{
629 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
630
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500631 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600632 return ep->name;
633}
634
635void __maybe_unused
636board_ti_get_eth_mac_addr(int index,
637 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
638{
639 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
640
641 if (ep->header == TI_DEAD_EEPROM_MAGIC)
642 goto fail;
643
644 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
645 goto fail;
646
647 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
648 return;
649
650fail:
651 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
652}
653
Andreas Dannenberg5a647112019-06-04 18:08:25 -0500654void __maybe_unused
655board_ti_am6_get_eth_mac_addr(int index,
656 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
657{
658 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
659
660 if (ep->header == TI_DEAD_EEPROM_MAGIC)
661 goto fail;
662
663 if (index < 0 || index >= ep->mac_addr_cnt)
664 goto fail;
665
666 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
667 return;
668
669fail:
670 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
671}
672
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530673u64 __maybe_unused board_ti_get_emif1_size(void)
674{
675 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
676
677 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
678 return 0;
679
680 return ep->emif1_size;
681}
682
683u64 __maybe_unused board_ti_get_emif2_size(void)
684{
685 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
686
687 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
688 return 0;
689
690 return ep->emif2_size;
691}
692
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600693void __maybe_unused set_board_info_env(char *name)
694{
695 char *unknown = "unknown";
696 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
697
698 if (name)
Simon Glass6a38e412017-08-03 12:22:09 -0600699 env_set("board_name", name);
Tom Rinid1449cb2020-06-04 15:45:08 -0400700 else if (strlen(ep->name) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600701 env_set("board_name", ep->name);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600702 else
Simon Glass6a38e412017-08-03 12:22:09 -0600703 env_set("board_name", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600704
Tom Rinid1449cb2020-06-04 15:45:08 -0400705 if (strlen(ep->version) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600706 env_set("board_rev", ep->version);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600707 else
Simon Glass6a38e412017-08-03 12:22:09 -0600708 env_set("board_rev", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600709
Tom Rinid1449cb2020-06-04 15:45:08 -0400710 if (strlen(ep->serial) != 0)
Simon Glass6a38e412017-08-03 12:22:09 -0600711 env_set("board_serial", ep->serial);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600712 else
Simon Glass6a38e412017-08-03 12:22:09 -0600713 env_set("board_serial", unknown);
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600714}
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200715
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500716void __maybe_unused set_board_info_env_am6(char *name)
717{
718 char *unknown = "unknown";
719 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
720
721 if (name)
722 env_set("board_name", name);
Tom Rinid1449cb2020-06-04 15:45:08 -0400723 else if (strlen(ep->name) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500724 env_set("board_name", ep->name);
725 else
726 env_set("board_name", unknown);
727
Tom Rinid1449cb2020-06-04 15:45:08 -0400728 if (strlen(ep->version) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500729 env_set("board_rev", ep->version);
730 else
731 env_set("board_rev", unknown);
732
Tom Rinid1449cb2020-06-04 15:45:08 -0400733 if (strlen(ep->software_revision) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500734 env_set("board_software_revision", ep->software_revision);
735 else
736 env_set("board_software_revision", unknown);
737
Tom Rinid1449cb2020-06-04 15:45:08 -0400738 if (strlen(ep->serial) != 0)
Andreas Dannenbergf998a822019-06-04 18:08:24 -0500739 env_set("board_serial", ep->serial);
740 else
741 env_set("board_serial", unknown);
742}
743
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200744static u64 mac_to_u64(u8 mac[6])
745{
746 int i;
747 u64 addr = 0;
748
749 for (i = 0; i < 6; i++) {
750 addr <<= 8;
751 addr |= mac[i];
752 }
753
754 return addr;
755}
756
757static void u64_to_mac(u64 addr, u8 mac[6])
758{
759 mac[5] = addr;
760 mac[4] = addr >> 8;
761 mac[3] = addr >> 16;
762 mac[2] = addr >> 24;
763 mac[1] = addr >> 32;
764 mac[0] = addr >> 40;
765}
766
767void board_ti_set_ethaddr(int index)
768{
769 uint8_t mac_addr[6];
770 int i;
771 u64 mac1, mac2;
772 u8 mac_addr1[6], mac_addr2[6];
773 int num_macs;
774 /*
775 * Export any Ethernet MAC addresses from EEPROM.
776 * The 2 MAC addresses in EEPROM define the address range.
777 */
778 board_ti_get_eth_mac_addr(0, mac_addr1);
779 board_ti_get_eth_mac_addr(1, mac_addr2);
780
781 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
782 mac1 = mac_to_u64(mac_addr1);
783 mac2 = mac_to_u64(mac_addr2);
784
785 /* must contain an address range */
786 num_macs = mac2 - mac1 + 1;
787 if (num_macs <= 0)
788 return;
789
790 if (num_macs > 50) {
791 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
792 __func__, num_macs);
793 num_macs = 50;
794 }
795
796 for (i = 0; i < num_macs; i++) {
797 u64_to_mac(mac1 + i, mac_addr);
798 if (is_valid_ethaddr(mac_addr)) {
Simon Glass8551d552017-08-03 12:22:11 -0600799 eth_env_set_enetaddr_by_index("eth", i + index,
800 mac_addr);
Roger Quadros44c7d9a2017-03-14 15:04:19 +0200801 }
802 }
803 }
804}
Cooper Jr., Frankline1328c72017-06-16 17:25:08 -0500805
Andreas Dannenberg5a647112019-06-04 18:08:25 -0500806void board_ti_am6_set_ethaddr(int index, int count)
807{
808 u8 mac_addr[6];
809 int i;
810
811 for (i = 0; i < count; i++) {
812 board_ti_am6_get_eth_mac_addr(i, mac_addr);
813 if (is_valid_ethaddr(mac_addr))
814 eth_env_set_enetaddr_by_index("eth", i + index,
815 mac_addr);
816 }
817}
818
Cooper Jr., Frankline1328c72017-06-16 17:25:08 -0500819bool __maybe_unused board_ti_was_eeprom_read(void)
820{
821 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
822
823 if (ep->header == TI_EEPROM_HEADER_MAGIC)
824 return true;
825 else
826 return false;
827}