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 | { |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 89 | u32 hdr_read; |
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 | |
| 110 | rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4); |
| 111 | if (rc) |
| 112 | return rc; |
| 113 | |
| 114 | /* Corrupted data??? */ |
| 115 | if (hdr_read != header) { |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 116 | /* |
| 117 | * read the eeprom header using i2c again, but use only a |
| 118 | * 1 byte address (some legacy boards need this..) |
| 119 | */ |
Michal Simek | 888f10d | 2020-08-27 15:43:48 +0200 | [diff] [blame] | 120 | rc = i2c_set_chip_offset_len(dev, 1); |
| 121 | if (rc) |
| 122 | return rc; |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 123 | |
Michal Simek | 888f10d | 2020-08-27 15:43:48 +0200 | [diff] [blame] | 124 | rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4); |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 125 | if (rc) |
| 126 | return rc; |
| 127 | } |
| 128 | if (hdr_read != header) |
| 129 | return -1; |
| 130 | |
| 131 | rc = dm_i2c_read(dev, 0, ep, size); |
| 132 | if (rc) |
| 133 | return rc; |
| 134 | #else |
| 135 | u32 byte; |
| 136 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 137 | gpi2c_init(); |
| 138 | rc = ti_i2c_eeprom_init(bus_addr, dev_addr); |
| 139 | if (rc) |
| 140 | return rc; |
| 141 | |
| 142 | /* |
| 143 | * Read the header first then only read the other contents. |
| 144 | */ |
| 145 | byte = 2; |
Cooper Jr., Franklin | 5b89c97 | 2017-04-20 10:25:45 -0500 | [diff] [blame] | 146 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 147 | rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4); |
| 148 | if (rc) |
| 149 | return rc; |
| 150 | |
| 151 | /* Corrupted data??? */ |
| 152 | if (hdr_read != header) { |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 153 | /* |
| 154 | * read the eeprom header using i2c again, but use only a |
| 155 | * 1 byte address (some legacy boards need this..) |
| 156 | */ |
| 157 | byte = 1; |
Michal Simek | 888f10d | 2020-08-27 15:43:48 +0200 | [diff] [blame] | 158 | rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, |
| 159 | 4); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 160 | if (rc) |
| 161 | return rc; |
| 162 | } |
| 163 | if (hdr_read != header) |
| 164 | return -1; |
| 165 | |
| 166 | rc = i2c_read(dev_addr, 0x0, byte, ep, size); |
| 167 | if (rc) |
| 168 | return rc; |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 169 | #endif |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Caleb Robey | 0dfcc93 | 2020-01-02 08:17:25 -0600 | [diff] [blame] | 173 | int __maybe_unused ti_emmc_boardid_get(void) |
| 174 | { |
| 175 | int rc; |
| 176 | struct udevice *dev; |
| 177 | struct mmc *mmc; |
| 178 | struct ti_common_eeprom *ep; |
| 179 | struct ti_am_eeprom brdid; |
| 180 | struct blk_desc *bdesc; |
| 181 | uchar *buffer; |
| 182 | |
| 183 | ep = TI_EEPROM_DATA; |
| 184 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
| 185 | return 0; /* EEPROM has already been read */ |
| 186 | |
| 187 | /* Initialize with a known bad marker for emmc fails.. */ |
| 188 | ep->header = TI_DEAD_EEPROM_MAGIC; |
| 189 | ep->name[0] = 0x0; |
| 190 | ep->version[0] = 0x0; |
| 191 | ep->serial[0] = 0x0; |
| 192 | ep->config[0] = 0x0; |
| 193 | |
| 194 | /* uclass object initialization */ |
| 195 | rc = mmc_initialize(NULL); |
| 196 | if (rc) |
| 197 | return rc; |
| 198 | |
| 199 | /* Set device to /dev/mmcblk1 */ |
| 200 | rc = uclass_get_device(UCLASS_MMC, 1, &dev); |
| 201 | if (rc) |
| 202 | return rc; |
| 203 | |
| 204 | /* Grab the mmc device */ |
| 205 | mmc = mmc_get_mmc_dev(dev); |
| 206 | if (!mmc) |
| 207 | return -ENODEV; |
| 208 | |
| 209 | /* mmc hardware initialization routine */ |
| 210 | mmc_init(mmc); |
| 211 | |
| 212 | /* Set partition to /dev/mmcblk1boot1 */ |
| 213 | rc = mmc_switch_part(mmc, 2); |
| 214 | if (rc) |
| 215 | return rc; |
| 216 | |
| 217 | buffer = malloc(mmc->read_bl_len); |
| 218 | if (!buffer) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | bdesc = mmc_get_blk_desc(mmc); |
| 222 | |
| 223 | /* blk_dread returns the number of blocks read*/ |
| 224 | if (blk_dread(bdesc, 0L, 1, buffer) != 1) { |
| 225 | rc = -EIO; |
| 226 | goto cleanup; |
| 227 | } |
| 228 | |
| 229 | memcpy(&brdid, buffer, sizeof(brdid)); |
| 230 | |
| 231 | /* Write out the ep struct values */ |
| 232 | ep->header = brdid.header; |
| 233 | strlcpy(ep->name, brdid.name, TI_EEPROM_HDR_NAME_LEN + 1); |
| 234 | ti_eeprom_string_cleanup(ep->name); |
| 235 | strlcpy(ep->version, brdid.version, TI_EEPROM_HDR_REV_LEN + 1); |
| 236 | ti_eeprom_string_cleanup(ep->version); |
| 237 | strlcpy(ep->serial, brdid.serial, TI_EEPROM_HDR_SERIAL_LEN + 1); |
| 238 | ti_eeprom_string_cleanup(ep->serial); |
| 239 | |
| 240 | cleanup: |
| 241 | free(buffer); |
| 242 | |
| 243 | return rc; |
| 244 | } |
| 245 | |
Nishanth Menon | 27856ff | 2017-06-16 17:25:04 -0500 | [diff] [blame] | 246 | int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev) |
| 247 | { |
| 248 | struct ti_common_eeprom *ep; |
| 249 | |
| 250 | if (!name || !rev) |
| 251 | return -1; |
| 252 | |
| 253 | ep = TI_EEPROM_DATA; |
| 254 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
| 255 | goto already_set; |
| 256 | |
| 257 | /* Set to 0 all fields */ |
| 258 | memset(ep, 0, sizeof(*ep)); |
| 259 | strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN); |
| 260 | strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN); |
| 261 | /* Some dummy serial number to identify the platform */ |
| 262 | strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN); |
| 263 | /* Mark it with a valid header */ |
| 264 | ep->header = TI_EEPROM_HEADER_MAGIC; |
| 265 | |
| 266 | already_set: |
| 267 | return 0; |
| 268 | } |
| 269 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 270 | int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr) |
| 271 | { |
| 272 | int rc; |
| 273 | struct ti_am_eeprom am_ep; |
| 274 | struct ti_common_eeprom *ep; |
| 275 | |
| 276 | ep = TI_EEPROM_DATA; |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 277 | #ifndef CONFIG_SPL_BUILD |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 278 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 279 | return 0; /* EEPROM has already been read */ |
| 280 | #endif |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 281 | |
| 282 | /* Initialize with a known bad marker for i2c fails.. */ |
| 283 | ep->header = TI_DEAD_EEPROM_MAGIC; |
| 284 | ep->name[0] = 0x0; |
| 285 | ep->version[0] = 0x0; |
| 286 | ep->serial[0] = 0x0; |
Nishanth Menon | e2b0a52 | 2016-10-11 12:39:04 -0500 | [diff] [blame] | 287 | ep->config[0] = 0x0; |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 288 | |
| 289 | rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC, |
| 290 | sizeof(am_ep), (uint8_t *)&am_ep); |
| 291 | if (rc) |
| 292 | return rc; |
| 293 | |
| 294 | ep->header = am_ep.header; |
| 295 | strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1); |
| 296 | ti_eeprom_string_cleanup(ep->name); |
| 297 | |
| 298 | /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */ |
| 299 | if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 && |
| 300 | am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00) |
| 301 | strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1); |
| 302 | else |
| 303 | strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1); |
| 304 | ti_eeprom_string_cleanup(ep->version); |
| 305 | strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1); |
| 306 | ti_eeprom_string_cleanup(ep->serial); |
| 307 | strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1); |
| 308 | ti_eeprom_string_cleanup(ep->config); |
| 309 | |
| 310 | memcpy(ep->mac_addr, am_ep.mac_addr, |
| 311 | TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN); |
| 312 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 316 | int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr) |
| 317 | { |
| 318 | int rc, offset = 0; |
| 319 | struct dra7_eeprom dra7_ep; |
| 320 | struct ti_common_eeprom *ep; |
| 321 | |
| 322 | ep = TI_EEPROM_DATA; |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 323 | #ifndef CONFIG_SPL_BUILD |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 324 | if (ep->header == DRA7_EEPROM_HEADER_MAGIC) |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 325 | return 0; /* EEPROM has already been read */ |
| 326 | #endif |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 327 | |
| 328 | /* Initialize with a known bad marker for i2c fails.. */ |
Nishanth Menon | 21e54c0 | 2016-10-11 12:39:03 -0500 | [diff] [blame] | 329 | ep->header = TI_DEAD_EEPROM_MAGIC; |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 330 | ep->name[0] = 0x0; |
| 331 | ep->version[0] = 0x0; |
| 332 | ep->serial[0] = 0x0; |
Nishanth Menon | e2b0a52 | 2016-10-11 12:39:04 -0500 | [diff] [blame] | 333 | ep->config[0] = 0x0; |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 334 | ep->emif1_size = 0; |
| 335 | ep->emif2_size = 0; |
| 336 | |
| 337 | rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC, |
| 338 | sizeof(dra7_ep), (uint8_t *)&dra7_ep); |
| 339 | if (rc) |
| 340 | return rc; |
| 341 | |
| 342 | ep->header = dra7_ep.header; |
| 343 | strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1); |
| 344 | ti_eeprom_string_cleanup(ep->name); |
| 345 | |
| 346 | offset = dra7_ep.version_major - 1; |
| 347 | |
| 348 | /* Rev F is skipped */ |
| 349 | if (offset >= 5) |
| 350 | offset = offset + 1; |
| 351 | snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d", |
| 352 | 'A' + offset, dra7_ep.version_minor); |
| 353 | ti_eeprom_string_cleanup(ep->version); |
| 354 | ep->emif1_size = (u64)dra7_ep.emif1_size; |
| 355 | ep->emif2_size = (u64)dra7_ep.emif2_size; |
| 356 | strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1); |
| 357 | ti_eeprom_string_cleanup(ep->config); |
| 358 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 362 | static int ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record *record, |
| 363 | struct ti_am6_eeprom *ep, |
| 364 | char **mac_addr, |
| 365 | u8 mac_addr_max_cnt, |
| 366 | u8 *mac_addr_cnt) |
| 367 | { |
| 368 | switch (record->header.id) { |
| 369 | case TI_AM6_EEPROM_RECORD_BOARD_INFO: |
| 370 | if (record->header.len != sizeof(record->data.board_info)) |
| 371 | return -EINVAL; |
| 372 | |
| 373 | if (!ep) |
| 374 | break; |
| 375 | |
| 376 | /* Populate (and clean, if needed) the board name */ |
| 377 | strlcpy(ep->name, record->data.board_info.name, |
| 378 | sizeof(ep->name)); |
| 379 | ti_eeprom_string_cleanup(ep->name); |
| 380 | |
| 381 | /* Populate selected other fields from the board info record */ |
| 382 | strlcpy(ep->version, record->data.board_info.version, |
| 383 | sizeof(ep->version)); |
| 384 | strlcpy(ep->software_revision, |
| 385 | record->data.board_info.software_revision, |
| 386 | sizeof(ep->software_revision)); |
| 387 | strlcpy(ep->serial, record->data.board_info.serial, |
| 388 | sizeof(ep->serial)); |
| 389 | break; |
| 390 | case TI_AM6_EEPROM_RECORD_MAC_INFO: |
| 391 | if (record->header.len != sizeof(record->data.mac_info)) |
| 392 | return -EINVAL; |
| 393 | |
| 394 | if (!mac_addr || !mac_addr_max_cnt) |
| 395 | break; |
| 396 | |
| 397 | *mac_addr_cnt = ((record->data.mac_info.mac_control & |
| 398 | TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK) >> |
| 399 | TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT) + 1; |
| 400 | |
| 401 | /* |
| 402 | * The EEPROM can (but may not) hold a very large amount |
| 403 | * of MAC addresses, by far exceeding what we want/can store |
| 404 | * in the common memory array, so only grab what we can fit. |
| 405 | * Note that a value of 0 means 1 MAC address, and so on. |
| 406 | */ |
| 407 | *mac_addr_cnt = min(*mac_addr_cnt, mac_addr_max_cnt); |
| 408 | |
| 409 | memcpy(mac_addr, record->data.mac_info.mac_addr, |
| 410 | *mac_addr_cnt * TI_EEPROM_HDR_ETH_ALEN); |
| 411 | break; |
| 412 | case 0x00: |
| 413 | /* Illegal value... Fall through... */ |
| 414 | case 0xFF: |
| 415 | /* Illegal value... Something went horribly wrong... */ |
| 416 | return -EINVAL; |
| 417 | default: |
| 418 | pr_warn("%s: Ignoring record id %u\n", __func__, |
| 419 | record->header.id); |
| 420 | } |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr, |
| 426 | struct ti_am6_eeprom *ep, |
| 427 | char **mac_addr, |
| 428 | u8 mac_addr_max_cnt, |
| 429 | u8 *mac_addr_cnt) |
| 430 | { |
| 431 | struct udevice *dev; |
| 432 | struct udevice *bus; |
| 433 | unsigned int eeprom_addr; |
| 434 | struct ti_am6_eeprom_record_board_id board_id; |
| 435 | struct ti_am6_eeprom_record record; |
| 436 | int rc; |
| 437 | |
| 438 | /* Initialize with a known bad marker for i2c fails.. */ |
| 439 | memset(ep, 0, sizeof(*ep)); |
| 440 | ep->header = TI_DEAD_EEPROM_MAGIC; |
| 441 | |
| 442 | /* Read the board ID record which is always the first EEPROM record */ |
| 443 | rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC, |
| 444 | sizeof(board_id), (uint8_t *)&board_id); |
| 445 | if (rc) |
| 446 | return rc; |
| 447 | |
| 448 | if (board_id.header.id != TI_AM6_EEPROM_RECORD_BOARD_ID) { |
| 449 | pr_err("%s: Invalid board ID record!\n", __func__); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | |
| 453 | /* Establish DM handle to board config EEPROM */ |
| 454 | rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus); |
| 455 | if (rc) |
| 456 | return rc; |
| 457 | rc = i2c_get_chip(bus, dev_addr, 1, &dev); |
| 458 | if (rc) |
| 459 | return rc; |
| 460 | |
| 461 | ep->header = TI_EEPROM_HEADER_MAGIC; |
| 462 | |
| 463 | /* Ready to parse TLV structure. Initialize variables... */ |
| 464 | *mac_addr_cnt = 0; |
| 465 | |
| 466 | /* |
| 467 | * After the all-encompassing board ID record all other records follow |
| 468 | * a TLV-type scheme. Point to the first such record and then start |
| 469 | * parsing those one by one. |
| 470 | */ |
| 471 | eeprom_addr = sizeof(board_id); |
| 472 | |
| 473 | while (true) { |
| 474 | rc = dm_i2c_read(dev, eeprom_addr, (uint8_t *)&record.header, |
| 475 | sizeof(record.header)); |
| 476 | if (rc) |
| 477 | return rc; |
| 478 | |
| 479 | /* |
| 480 | * Check for end of list marker. If we reached it don't go |
| 481 | * any further and stop parsing right here. |
| 482 | */ |
| 483 | if (record.header.id == TI_AM6_EEPROM_RECORD_END_LIST) |
| 484 | break; |
| 485 | |
| 486 | eeprom_addr += sizeof(record.header); |
| 487 | |
| 488 | debug("%s: dev_addr=0x%02x header.id=%u header.len=%u\n", |
| 489 | __func__, dev_addr, record.header.id, |
| 490 | record.header.len); |
| 491 | |
| 492 | /* Read record into memory if it fits */ |
| 493 | if (record.header.len <= sizeof(record.data)) { |
| 494 | rc = dm_i2c_read(dev, eeprom_addr, |
| 495 | (uint8_t *)&record.data, |
| 496 | record.header.len); |
| 497 | if (rc) |
| 498 | return rc; |
| 499 | |
| 500 | /* Process record */ |
| 501 | rc = ti_i2c_eeprom_am6_parse_record(&record, ep, |
| 502 | mac_addr, |
| 503 | mac_addr_max_cnt, |
| 504 | mac_addr_cnt); |
| 505 | if (rc) { |
| 506 | pr_err("%s: EEPROM parsing error!\n", __func__); |
| 507 | return rc; |
| 508 | } |
| 509 | } 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); |
| 516 | } |
| 517 | |
| 518 | eeprom_addr += record.header.len; |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr) |
| 525 | { |
| 526 | struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA; |
| 527 | int ret; |
| 528 | |
| 529 | /* |
| 530 | * Always execute EEPROM read by not allowing to bypass it during the |
| 531 | * first invocation of SPL which happens on the R5 core. |
| 532 | */ |
| 533 | #if !(defined(CONFIG_SPL_BUILD) && defined(CONFIG_CPU_V7R)) |
| 534 | if (ep->header == TI_EEPROM_HEADER_MAGIC) { |
| 535 | debug("%s: EEPROM has already been read\n", __func__); |
| 536 | return 0; |
| 537 | } |
| 538 | #endif |
| 539 | |
| 540 | ret = ti_i2c_eeprom_am6_get(bus_addr, dev_addr, ep, |
| 541 | (char **)ep->mac_addr, |
| 542 | AM6_EEPROM_HDR_NO_OF_MAC_ADDR, |
| 543 | &ep->mac_addr_cnt); |
| 544 | return ret; |
| 545 | } |
| 546 | |
Andreas Dannenberg | d036a21 | 2020-01-07 13:15:54 +0530 | [diff] [blame] | 547 | bool __maybe_unused board_ti_k3_is(char *name_tag) |
| 548 | { |
| 549 | struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA; |
| 550 | |
| 551 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 552 | return false; |
| 553 | return !strncmp(ep->name, name_tag, AM6_EEPROM_HDR_NAME_LEN); |
| 554 | } |
| 555 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 556 | bool __maybe_unused board_ti_is(char *name_tag) |
| 557 | { |
| 558 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 559 | |
| 560 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 561 | return false; |
| 562 | return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN); |
| 563 | } |
| 564 | |
| 565 | bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len) |
| 566 | { |
| 567 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 568 | int l; |
| 569 | |
| 570 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 571 | return false; |
| 572 | |
| 573 | l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len; |
| 574 | return !strncmp(ep->version, rev_tag, l); |
| 575 | } |
| 576 | |
| 577 | char * __maybe_unused board_ti_get_rev(void) |
| 578 | { |
| 579 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 580 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 581 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 582 | return ep->version; |
| 583 | } |
| 584 | |
| 585 | char * __maybe_unused board_ti_get_config(void) |
| 586 | { |
| 587 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 588 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 589 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 590 | return ep->config; |
| 591 | } |
| 592 | |
| 593 | char * __maybe_unused board_ti_get_name(void) |
| 594 | { |
| 595 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 596 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 597 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 598 | return ep->name; |
| 599 | } |
| 600 | |
| 601 | void __maybe_unused |
| 602 | board_ti_get_eth_mac_addr(int index, |
| 603 | u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN]) |
| 604 | { |
| 605 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 606 | |
| 607 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 608 | goto fail; |
| 609 | |
| 610 | if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR) |
| 611 | goto fail; |
| 612 | |
| 613 | memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN); |
| 614 | return; |
| 615 | |
| 616 | fail: |
| 617 | memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN); |
| 618 | } |
| 619 | |
Andreas Dannenberg | 5a64711 | 2019-06-04 18:08:25 -0500 | [diff] [blame] | 620 | void __maybe_unused |
| 621 | board_ti_am6_get_eth_mac_addr(int index, |
| 622 | u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN]) |
| 623 | { |
| 624 | struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA; |
| 625 | |
| 626 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 627 | goto fail; |
| 628 | |
| 629 | if (index < 0 || index >= ep->mac_addr_cnt) |
| 630 | goto fail; |
| 631 | |
| 632 | memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN); |
| 633 | return; |
| 634 | |
| 635 | fail: |
| 636 | memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN); |
| 637 | } |
| 638 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 639 | u64 __maybe_unused board_ti_get_emif1_size(void) |
| 640 | { |
| 641 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 642 | |
| 643 | if (ep->header != DRA7_EEPROM_HEADER_MAGIC) |
| 644 | return 0; |
| 645 | |
| 646 | return ep->emif1_size; |
| 647 | } |
| 648 | |
| 649 | u64 __maybe_unused board_ti_get_emif2_size(void) |
| 650 | { |
| 651 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 652 | |
| 653 | if (ep->header != DRA7_EEPROM_HEADER_MAGIC) |
| 654 | return 0; |
| 655 | |
| 656 | return ep->emif2_size; |
| 657 | } |
| 658 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 659 | void __maybe_unused set_board_info_env(char *name) |
| 660 | { |
| 661 | char *unknown = "unknown"; |
| 662 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 663 | |
| 664 | if (name) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 665 | env_set("board_name", name); |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 666 | else if (strlen(ep->name) != 0) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 667 | env_set("board_name", ep->name); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 668 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 669 | env_set("board_name", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 670 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 671 | if (strlen(ep->version) != 0) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 672 | env_set("board_rev", ep->version); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 673 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 674 | env_set("board_rev", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 675 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 676 | if (strlen(ep->serial) != 0) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 677 | env_set("board_serial", ep->serial); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 678 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 679 | env_set("board_serial", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 680 | } |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 681 | |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 682 | void __maybe_unused set_board_info_env_am6(char *name) |
| 683 | { |
| 684 | char *unknown = "unknown"; |
| 685 | struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA; |
| 686 | |
| 687 | if (name) |
| 688 | env_set("board_name", name); |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 689 | else if (strlen(ep->name) != 0) |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 690 | env_set("board_name", ep->name); |
| 691 | else |
| 692 | env_set("board_name", unknown); |
| 693 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 694 | if (strlen(ep->version) != 0) |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 695 | env_set("board_rev", ep->version); |
| 696 | else |
| 697 | env_set("board_rev", unknown); |
| 698 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 699 | if (strlen(ep->software_revision) != 0) |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 700 | env_set("board_software_revision", ep->software_revision); |
| 701 | else |
| 702 | env_set("board_software_revision", unknown); |
| 703 | |
Tom Rini | d1449cb | 2020-06-04 15:45:08 -0400 | [diff] [blame] | 704 | if (strlen(ep->serial) != 0) |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 705 | env_set("board_serial", ep->serial); |
| 706 | else |
| 707 | env_set("board_serial", unknown); |
| 708 | } |
| 709 | |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 710 | static u64 mac_to_u64(u8 mac[6]) |
| 711 | { |
| 712 | int i; |
| 713 | u64 addr = 0; |
| 714 | |
| 715 | for (i = 0; i < 6; i++) { |
| 716 | addr <<= 8; |
| 717 | addr |= mac[i]; |
| 718 | } |
| 719 | |
| 720 | return addr; |
| 721 | } |
| 722 | |
| 723 | static void u64_to_mac(u64 addr, u8 mac[6]) |
| 724 | { |
| 725 | mac[5] = addr; |
| 726 | mac[4] = addr >> 8; |
| 727 | mac[3] = addr >> 16; |
| 728 | mac[2] = addr >> 24; |
| 729 | mac[1] = addr >> 32; |
| 730 | mac[0] = addr >> 40; |
| 731 | } |
| 732 | |
| 733 | void board_ti_set_ethaddr(int index) |
| 734 | { |
| 735 | uint8_t mac_addr[6]; |
| 736 | int i; |
| 737 | u64 mac1, mac2; |
| 738 | u8 mac_addr1[6], mac_addr2[6]; |
| 739 | int num_macs; |
| 740 | /* |
| 741 | * Export any Ethernet MAC addresses from EEPROM. |
| 742 | * The 2 MAC addresses in EEPROM define the address range. |
| 743 | */ |
| 744 | board_ti_get_eth_mac_addr(0, mac_addr1); |
| 745 | board_ti_get_eth_mac_addr(1, mac_addr2); |
| 746 | |
| 747 | if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) { |
| 748 | mac1 = mac_to_u64(mac_addr1); |
| 749 | mac2 = mac_to_u64(mac_addr2); |
| 750 | |
| 751 | /* must contain an address range */ |
| 752 | num_macs = mac2 - mac1 + 1; |
| 753 | if (num_macs <= 0) |
| 754 | return; |
| 755 | |
| 756 | if (num_macs > 50) { |
| 757 | printf("%s: Too many MAC addresses: %d. Limiting to 50\n", |
| 758 | __func__, num_macs); |
| 759 | num_macs = 50; |
| 760 | } |
| 761 | |
| 762 | for (i = 0; i < num_macs; i++) { |
| 763 | u64_to_mac(mac1 + i, mac_addr); |
| 764 | if (is_valid_ethaddr(mac_addr)) { |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 765 | eth_env_set_enetaddr_by_index("eth", i + index, |
| 766 | mac_addr); |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | } |
| 770 | } |
Cooper Jr., Franklin | e1328c7 | 2017-06-16 17:25:08 -0500 | [diff] [blame] | 771 | |
Andreas Dannenberg | 5a64711 | 2019-06-04 18:08:25 -0500 | [diff] [blame] | 772 | void board_ti_am6_set_ethaddr(int index, int count) |
| 773 | { |
| 774 | u8 mac_addr[6]; |
| 775 | int i; |
| 776 | |
| 777 | for (i = 0; i < count; i++) { |
| 778 | board_ti_am6_get_eth_mac_addr(i, mac_addr); |
| 779 | if (is_valid_ethaddr(mac_addr)) |
| 780 | eth_env_set_enetaddr_by_index("eth", i + index, |
| 781 | mac_addr); |
| 782 | } |
| 783 | } |
| 784 | |
Cooper Jr., Franklin | e1328c7 | 2017-06-16 17:25:08 -0500 | [diff] [blame] | 785 | bool __maybe_unused board_ti_was_eeprom_read(void) |
| 786 | { |
| 787 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 788 | |
| 789 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
| 790 | return true; |
| 791 | else |
| 792 | return false; |
| 793 | } |