Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 2 | /* |
| 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 Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | eba6b8d | 2019-11-14 12:57:50 -0700 | [diff] [blame] | 11 | #include <eeprom.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 13 | #include <net.h> |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 14 | #include <asm/arch/hardware.h> |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 15 | #include <asm/omap_common.h> |
Cooper Jr., Franklin | f2598de | 2017-04-20 10:25:44 -0500 | [diff] [blame] | 16 | #include <dm/uclass.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 17 | #include <env.h> |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 18 | #include <i2c.h> |
Caleb Robey | 0dfcc93 | 2020-01-02 08:17:25 -0600 | [diff] [blame] | 19 | #include <mmc.h> |
| 20 | #include <errno.h> |
| 21 | #include <malloc.h> |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 22 | |
| 23 | #include "board_detect.h" |
| 24 | |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 25 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 26 | /** |
| 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 | */ |
| 33 | static 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 | */ |
| 55 | static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset, |
| 56 | uchar *ep, int epsize) |
| 57 | { |
Jean-Jacques Hiblot | 52a5151 | 2018-12-07 14:50:49 +0100 | [diff] [blame] | 58 | return i2c_read(dev_addr, offset, 2, ep, epsize); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 59 | } |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 60 | #endif |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 61 | |
| 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 | */ |
| 70 | static 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 | |
| 86 | static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr, |
| 87 | u32 header, u32 size, uint8_t *ep) |
| 88 | { |
Nishanth Menon | 570c3bb | 2022-06-17 13:26:11 -0500 | [diff] [blame^] | 89 | u32 hdr_read = 0xdeadbeef; |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 90 | int rc; |
| 91 | |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 92 | #if CONFIG_IS_ENABLED(DM_I2C) |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 93 | struct udevice *dev; |
| 94 | struct udevice *bus; |
| 95 | |
| 96 | rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus); |
| 97 | if (rc) |
| 98 | return rc; |
Andreas Dannenberg | 4803492 | 2020-01-07 13:15:53 +0530 | [diff] [blame] | 99 | rc = dm_i2c_probe(bus, dev_addr, 0, &dev); |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 100 | if (rc) |
| 101 | return rc; |
| 102 | |
| 103 | /* |
| 104 | * Read the header first then only read the other contents. |
| 105 | */ |
| 106 | rc = i2c_set_chip_offset_len(dev, 2); |
| 107 | if (rc) |
| 108 | return rc; |
| 109 | |
Nishanth Menon | 570c3bb | 2022-06-17 13:26:11 -0500 | [diff] [blame^] | 110 | /* |
| 111 | * Skip checking result here since this could be a valid i2c read fail |
| 112 | * on some boards that use 1 byte addressing. |
| 113 | * We must allow for fall through to check the data if 1 byte |
| 114 | * addressing works |
| 115 | */ |
| 116 | (void)dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4); |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 117 | |
| 118 | /* Corrupted data??? */ |
| 119 | if (hdr_read != header) { |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 120 | /* |
| 121 | * read the eeprom header using i2c again, but use only a |
| 122 | * 1 byte address (some legacy boards need this..) |
| 123 | */ |
Michal Simek | 888f10d | 2020-08-27 15:43:48 +0200 | [diff] [blame] | 124 | rc = i2c_set_chip_offset_len(dev, 1); |
| 125 | if (rc) |
| 126 | return rc; |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 127 | |
Michal Simek | 888f10d | 2020-08-27 15:43:48 +0200 | [diff] [blame] | 128 | rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4); |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 129 | if (rc) |
| 130 | return rc; |
| 131 | } |
| 132 | if (hdr_read != header) |
| 133 | return -1; |
| 134 | |
| 135 | rc = dm_i2c_read(dev, 0, ep, size); |
| 136 | if (rc) |
| 137 | return rc; |
| 138 | #else |
| 139 | u32 byte; |
| 140 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 141 | gpi2c_init(); |
| 142 | rc = ti_i2c_eeprom_init(bus_addr, dev_addr); |
| 143 | if (rc) |
| 144 | return rc; |
| 145 | |
| 146 | /* |
| 147 | * Read the header first then only read the other contents. |
| 148 | */ |
| 149 | byte = 2; |
Cooper Jr., Franklin | 5b89c97 | 2017-04-20 10:25:45 -0500 | [diff] [blame] | 150 | |
Nishanth Menon | 570c3bb | 2022-06-17 13:26:11 -0500 | [diff] [blame^] | 151 | /* |
| 152 | * Skip checking result here since this could be a valid i2c read fail |
| 153 | * on some boards that use 1 byte addressing. |
| 154 | * We must allow for fall through to check the data if 1 byte |
| 155 | * addressing works |
| 156 | */ |
| 157 | (void)i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 158 | |
| 159 | /* Corrupted data??? */ |
| 160 | if (hdr_read != header) { |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 161 | /* |
| 162 | * read the eeprom header using i2c again, but use only a |
| 163 | * 1 byte address (some legacy boards need this..) |
| 164 | */ |
| 165 | byte = 1; |
Michal Simek | 888f10d | 2020-08-27 15:43:48 +0200 | [diff] [blame] | 166 | rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, |
| 167 | 4); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 168 | if (rc) |
| 169 | return rc; |
| 170 | } |
| 171 | if (hdr_read != header) |
| 172 | return -1; |
| 173 | |
| 174 | rc = i2c_read(dev_addr, 0x0, byte, ep, size); |
| 175 | if (rc) |
| 176 | return rc; |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 177 | #endif |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
Caleb Robey | 0dfcc93 | 2020-01-02 08:17:25 -0600 | [diff] [blame] | 181 | int __maybe_unused ti_emmc_boardid_get(void) |
| 182 | { |
| 183 | int rc; |
| 184 | struct udevice *dev; |
| 185 | struct mmc *mmc; |
| 186 | struct ti_common_eeprom *ep; |
| 187 | struct ti_am_eeprom brdid; |
| 188 | struct blk_desc *bdesc; |
| 189 | uchar *buffer; |
| 190 | |
| 191 | ep = TI_EEPROM_DATA; |
| 192 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
| 193 | return 0; /* EEPROM has already been read */ |
| 194 | |
| 195 | /* Initialize with a known bad marker for emmc fails.. */ |
| 196 | ep->header = TI_DEAD_EEPROM_MAGIC; |
| 197 | ep->name[0] = 0x0; |
| 198 | ep->version[0] = 0x0; |
| 199 | ep->serial[0] = 0x0; |
| 200 | ep->config[0] = 0x0; |
| 201 | |
| 202 | /* uclass object initialization */ |
| 203 | rc = mmc_initialize(NULL); |
| 204 | if (rc) |
| 205 | return rc; |
| 206 | |
| 207 | /* Set device to /dev/mmcblk1 */ |
| 208 | rc = uclass_get_device(UCLASS_MMC, 1, &dev); |
| 209 | if (rc) |
| 210 | return rc; |
| 211 | |
| 212 | /* Grab the mmc device */ |
| 213 | mmc = mmc_get_mmc_dev(dev); |
| 214 | if (!mmc) |
| 215 | return -ENODEV; |
| 216 | |
| 217 | /* mmc hardware initialization routine */ |
| 218 | mmc_init(mmc); |
| 219 | |
| 220 | /* Set partition to /dev/mmcblk1boot1 */ |
| 221 | rc = mmc_switch_part(mmc, 2); |
| 222 | if (rc) |
| 223 | return rc; |
| 224 | |
| 225 | buffer = malloc(mmc->read_bl_len); |
| 226 | if (!buffer) |
| 227 | return -ENOMEM; |
| 228 | |
| 229 | bdesc = mmc_get_blk_desc(mmc); |
| 230 | |
| 231 | /* blk_dread returns the number of blocks read*/ |
| 232 | if (blk_dread(bdesc, 0L, 1, buffer) != 1) { |
| 233 | rc = -EIO; |
| 234 | goto cleanup; |
| 235 | } |
| 236 | |
| 237 | memcpy(&brdid, buffer, sizeof(brdid)); |
| 238 | |
| 239 | /* Write out the ep struct values */ |
| 240 | ep->header = brdid.header; |
| 241 | strlcpy(ep->name, brdid.name, TI_EEPROM_HDR_NAME_LEN + 1); |
| 242 | ti_eeprom_string_cleanup(ep->name); |
| 243 | strlcpy(ep->version, brdid.version, TI_EEPROM_HDR_REV_LEN + 1); |
| 244 | ti_eeprom_string_cleanup(ep->version); |
| 245 | strlcpy(ep->serial, brdid.serial, TI_EEPROM_HDR_SERIAL_LEN + 1); |
| 246 | ti_eeprom_string_cleanup(ep->serial); |
| 247 | |
| 248 | cleanup: |
| 249 | free(buffer); |
| 250 | |
| 251 | return rc; |
| 252 | } |
| 253 | |
Nishanth Menon | 27856ff | 2017-06-16 17:25:04 -0500 | [diff] [blame] | 254 | int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev) |
| 255 | { |
| 256 | struct ti_common_eeprom *ep; |
| 257 | |
| 258 | if (!name || !rev) |
| 259 | return -1; |
| 260 | |
| 261 | ep = TI_EEPROM_DATA; |
| 262 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
| 263 | goto already_set; |
| 264 | |
| 265 | /* Set to 0 all fields */ |
| 266 | memset(ep, 0, sizeof(*ep)); |
| 267 | strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN); |
| 268 | strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN); |
| 269 | /* Some dummy serial number to identify the platform */ |
| 270 | strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN); |
| 271 | /* Mark it with a valid header */ |
| 272 | ep->header = TI_EEPROM_HEADER_MAGIC; |
| 273 | |
| 274 | already_set: |
| 275 | return 0; |
| 276 | } |
| 277 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 278 | int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr) |
| 279 | { |
| 280 | int rc; |
| 281 | struct ti_am_eeprom am_ep; |
| 282 | struct ti_common_eeprom *ep; |
| 283 | |
| 284 | ep = TI_EEPROM_DATA; |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 285 | #ifndef CONFIG_SPL_BUILD |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 286 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 287 | return 0; /* EEPROM has already been read */ |
| 288 | #endif |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 289 | |
| 290 | /* Initialize with a known bad marker for i2c fails.. */ |
| 291 | ep->header = TI_DEAD_EEPROM_MAGIC; |
| 292 | ep->name[0] = 0x0; |
| 293 | ep->version[0] = 0x0; |
| 294 | ep->serial[0] = 0x0; |
Nishanth Menon | e2b0a52 | 2016-10-11 12:39:04 -0500 | [diff] [blame] | 295 | ep->config[0] = 0x0; |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 296 | |
| 297 | rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC, |
| 298 | sizeof(am_ep), (uint8_t *)&am_ep); |
| 299 | if (rc) |
| 300 | return rc; |
| 301 | |
| 302 | ep->header = am_ep.header; |
| 303 | strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1); |
| 304 | ti_eeprom_string_cleanup(ep->name); |
| 305 | |
| 306 | /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */ |
| 307 | if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 && |
| 308 | am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00) |
| 309 | strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1); |
| 310 | else |
| 311 | strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1); |
| 312 | ti_eeprom_string_cleanup(ep->version); |
| 313 | strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1); |
| 314 | ti_eeprom_string_cleanup(ep->serial); |
| 315 | strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1); |
| 316 | ti_eeprom_string_cleanup(ep->config); |
| 317 | |
| 318 | memcpy(ep->mac_addr, am_ep.mac_addr, |
| 319 | TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN); |
| 320 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 324 | int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr) |
| 325 | { |
| 326 | int rc, offset = 0; |
| 327 | struct dra7_eeprom dra7_ep; |
| 328 | struct ti_common_eeprom *ep; |
| 329 | |
| 330 | ep = TI_EEPROM_DATA; |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 331 | #ifndef CONFIG_SPL_BUILD |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 332 | if (ep->header == DRA7_EEPROM_HEADER_MAGIC) |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 333 | return 0; /* EEPROM has already been read */ |
| 334 | #endif |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 335 | |
| 336 | /* Initialize with a known bad marker for i2c fails.. */ |
Nishanth Menon | 21e54c0 | 2016-10-11 12:39:03 -0500 | [diff] [blame] | 337 | ep->header = TI_DEAD_EEPROM_MAGIC; |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 338 | ep->name[0] = 0x0; |
| 339 | ep->version[0] = 0x0; |
| 340 | ep->serial[0] = 0x0; |
Nishanth Menon | e2b0a52 | 2016-10-11 12:39:04 -0500 | [diff] [blame] | 341 | ep->config[0] = 0x0; |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 342 | ep->emif1_size = 0; |
| 343 | ep->emif2_size = 0; |
| 344 | |
| 345 | rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC, |
| 346 | sizeof(dra7_ep), (uint8_t *)&dra7_ep); |
| 347 | if (rc) |
| 348 | return rc; |
| 349 | |
| 350 | ep->header = dra7_ep.header; |
| 351 | strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1); |
| 352 | ti_eeprom_string_cleanup(ep->name); |
| 353 | |
| 354 | offset = dra7_ep.version_major - 1; |
| 355 | |
| 356 | /* Rev F is skipped */ |
| 357 | if (offset >= 5) |
| 358 | offset = offset + 1; |
| 359 | snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d", |
| 360 | 'A' + offset, dra7_ep.version_minor); |
| 361 | ti_eeprom_string_cleanup(ep->version); |
| 362 | ep->emif1_size = (u64)dra7_ep.emif1_size; |
| 363 | ep->emif2_size = (u64)dra7_ep.emif2_size; |
| 364 | strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1); |
| 365 | ti_eeprom_string_cleanup(ep->config); |
| 366 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 370 | static int ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record *record, |
| 371 | struct ti_am6_eeprom *ep, |
| 372 | char **mac_addr, |
| 373 | u8 mac_addr_max_cnt, |
| 374 | u8 *mac_addr_cnt) |
| 375 | { |
| 376 | switch (record->header.id) { |
| 377 | case TI_AM6_EEPROM_RECORD_BOARD_INFO: |
| 378 | if (record->header.len != sizeof(record->data.board_info)) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | if (!ep) |
| 382 | break; |
| 383 | |
| 384 | /* Populate (and clean, if needed) the board name */ |
| 385 | strlcpy(ep->name, record->data.board_info.name, |
| 386 | sizeof(ep->name)); |
| 387 | ti_eeprom_string_cleanup(ep->name); |
| 388 | |
| 389 | /* Populate selected other fields from the board info record */ |
| 390 | strlcpy(ep->version, record->data.board_info.version, |
| 391 | sizeof(ep->version)); |
| 392 | strlcpy(ep->software_revision, |
| 393 | record->data.board_info.software_revision, |
| 394 | sizeof(ep->software_revision)); |
| 395 | strlcpy(ep->serial, record->data.board_info.serial, |
| 396 | sizeof(ep->serial)); |
| 397 | break; |
| 398 | case TI_AM6_EEPROM_RECORD_MAC_INFO: |
| 399 | if (record->header.len != sizeof(record->data.mac_info)) |
| 400 | return -EINVAL; |
| 401 | |
| 402 | if (!mac_addr || !mac_addr_max_cnt) |
| 403 | break; |
| 404 | |
| 405 | *mac_addr_cnt = ((record->data.mac_info.mac_control & |
| 406 | TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK) >> |
| 407 | TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT) + 1; |
| 408 | |
| 409 | /* |
| 410 | * The EEPROM can (but may not) hold a very large amount |
| 411 | * of MAC addresses, by far exceeding what we want/can store |
| 412 | * in the common memory array, so only grab what we can fit. |
| 413 | * Note that a value of 0 means 1 MAC address, and so on. |
| 414 | */ |
| 415 | *mac_addr_cnt = min(*mac_addr_cnt, mac_addr_max_cnt); |
| 416 | |
| 417 | memcpy(mac_addr, record->data.mac_info.mac_addr, |
| 418 | *mac_addr_cnt * TI_EEPROM_HDR_ETH_ALEN); |
| 419 | break; |
| 420 | case 0x00: |
| 421 | /* Illegal value... Fall through... */ |
| 422 | case 0xFF: |
| 423 | /* Illegal value... Something went horribly wrong... */ |
| 424 | return -EINVAL; |
| 425 | default: |
| 426 | pr_warn("%s: Ignoring record id %u\n", __func__, |
| 427 | record->header.id); |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr, |
| 434 | struct ti_am6_eeprom *ep, |
| 435 | char **mac_addr, |
| 436 | u8 mac_addr_max_cnt, |
| 437 | u8 *mac_addr_cnt) |
| 438 | { |
| 439 | struct udevice *dev; |
| 440 | struct udevice *bus; |
| 441 | unsigned int eeprom_addr; |
| 442 | struct ti_am6_eeprom_record_board_id board_id; |
| 443 | struct ti_am6_eeprom_record record; |
| 444 | int rc; |
Nishanth Menon | 77c94ea | 2022-06-17 13:26:10 -0500 | [diff] [blame] | 445 | int consecutive_bad_records = 0; |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 446 | |
| 447 | /* Initialize with a known bad marker for i2c fails.. */ |
| 448 | memset(ep, 0, sizeof(*ep)); |
| 449 | ep->header = TI_DEAD_EEPROM_MAGIC; |
| 450 | |
| 451 | /* Read the board ID record which is always the first EEPROM record */ |
| 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 | |
| 457 | 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 Menon | 77c94ea | 2022-06-17 13:26:10 -0500 | [diff] [blame] | 482 | while (consecutive_bad_records < 10) { |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 483 | 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 Menon | 77c94ea | 2022-06-17 13:26:10 -0500 | [diff] [blame] | 518 | consecutive_bad_records = 0; |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 519 | } 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 Menon | 77c94ea | 2022-06-17 13:26:10 -0500 | [diff] [blame] | 526 | consecutive_bad_records++; |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | eeprom_addr += record.header.len; |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | int __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 Dannenberg | d036a21 | 2020-01-07 13:15:54 +0530 | [diff] [blame] | 558 | bool __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 Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 567 | bool __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 | |
| 576 | bool __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 | |
| 588 | char * __maybe_unused board_ti_get_rev(void) |
| 589 | { |
| 590 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 591 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 592 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 593 | return ep->version; |
| 594 | } |
| 595 | |
| 596 | char * __maybe_unused board_ti_get_config(void) |
| 597 | { |
| 598 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 599 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 600 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 601 | return ep->config; |
| 602 | } |
| 603 | |
| 604 | char * __maybe_unused board_ti_get_name(void) |
| 605 | { |
| 606 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 607 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 608 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 609 | return ep->name; |
| 610 | } |
| 611 | |
| 612 | void __maybe_unused |
| 613 | board_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 | |
| 627 | fail: |
| 628 | memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN); |
| 629 | } |
| 630 | |
Andreas Dannenberg | 5a64711 | 2019-06-04 18:08:25 -0500 | [diff] [blame] | 631 | void __maybe_unused |
| 632 | board_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 | |
| 646 | fail: |
| 647 | memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN); |
| 648 | } |
| 649 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 650 | u64 __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 | |
| 660 | u64 __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 Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 670 | void __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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 676 | env_set("board_name", name); |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 677 | else if (strlen(ep->name) != 0) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 678 | env_set("board_name", ep->name); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 679 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 680 | env_set("board_name", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 681 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 682 | if (strlen(ep->version) != 0) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 683 | env_set("board_rev", ep->version); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 684 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 685 | env_set("board_rev", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 686 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 687 | if (strlen(ep->serial) != 0) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 688 | env_set("board_serial", ep->serial); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 689 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 690 | env_set("board_serial", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 691 | } |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 692 | |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 693 | void __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 Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 700 | else if (strlen(ep->name) != 0) |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 701 | env_set("board_name", ep->name); |
| 702 | else |
| 703 | env_set("board_name", unknown); |
| 704 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 705 | if (strlen(ep->version) != 0) |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 706 | env_set("board_rev", ep->version); |
| 707 | else |
| 708 | env_set("board_rev", unknown); |
| 709 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 710 | if (strlen(ep->software_revision) != 0) |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 711 | env_set("board_software_revision", ep->software_revision); |
| 712 | else |
| 713 | env_set("board_software_revision", unknown); |
| 714 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 715 | if (strlen(ep->serial) != 0) |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 716 | env_set("board_serial", ep->serial); |
| 717 | else |
| 718 | env_set("board_serial", unknown); |
| 719 | } |
| 720 | |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 721 | static 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 | |
| 734 | static 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 | |
| 744 | void 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 Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 776 | eth_env_set_enetaddr_by_index("eth", i + index, |
| 777 | mac_addr); |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
Cooper Jr., Franklin | e1328c7 | 2017-06-16 17:25:08 -0500 | [diff] [blame] | 782 | |
Andreas Dannenberg | 5a64711 | 2019-06-04 18:08:25 -0500 | [diff] [blame] | 783 | void 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., Franklin | e1328c7 | 2017-06-16 17:25:08 -0500 | [diff] [blame] | 796 | bool __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 | } |