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