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> |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 11 | #include <asm/arch/hardware.h> |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 12 | #include <asm/omap_common.h> |
Cooper Jr., Franklin | f2598de | 2017-04-20 10:25:44 -0500 | [diff] [blame] | 13 | #include <dm/uclass.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 14 | #include <env.h> |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 15 | #include <i2c.h> |
| 16 | |
| 17 | #include "board_detect.h" |
| 18 | |
Jean-Jacques Hiblot | 52a5151 | 2018-12-07 14:50:49 +0100 | [diff] [blame] | 19 | #if !defined(CONFIG_DM_I2C) |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 20 | /** |
| 21 | * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device |
| 22 | * @i2c_bus: i2c bus number to initialize |
| 23 | * @dev_addr: Device address to probe for |
| 24 | * |
| 25 | * Return: 0 on success or corresponding error on failure. |
| 26 | */ |
| 27 | static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr) |
| 28 | { |
| 29 | int rc; |
| 30 | |
| 31 | if (i2c_bus >= 0) { |
| 32 | rc = i2c_set_bus_num(i2c_bus); |
| 33 | if (rc) |
| 34 | return rc; |
| 35 | } |
| 36 | |
| 37 | return i2c_probe(dev_addr); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * ti_i2c_eeprom_read - Read data from an EEPROM |
| 42 | * @dev_addr: The device address of the EEPROM |
| 43 | * @offset: Offset to start reading in the EEPROM |
| 44 | * @ep: Pointer to a buffer to read into |
| 45 | * @epsize: Size of buffer |
| 46 | * |
| 47 | * Return: 0 on success or corresponding result of i2c_read |
| 48 | */ |
| 49 | static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset, |
| 50 | uchar *ep, int epsize) |
| 51 | { |
Jean-Jacques Hiblot | 52a5151 | 2018-12-07 14:50:49 +0100 | [diff] [blame] | 52 | return i2c_read(dev_addr, offset, 2, ep, epsize); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 53 | } |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 54 | #endif |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * ti_eeprom_string_cleanup() - Handle eeprom programming errors |
| 58 | * @s: eeprom string (should be NULL terminated) |
| 59 | * |
| 60 | * Some Board manufacturers do not add a NULL termination at the |
| 61 | * end of string, instead some binary information is kludged in, hence |
| 62 | * convert the string to just printable characters of ASCII chart. |
| 63 | */ |
| 64 | static void __maybe_unused ti_eeprom_string_cleanup(char *s) |
| 65 | { |
| 66 | int i, l; |
| 67 | |
| 68 | l = strlen(s); |
| 69 | for (i = 0; i < l; i++, s++) |
| 70 | if (*s < ' ' || *s > '~') { |
| 71 | *s = 0; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | __weak void gpi2c_init(void) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr, |
| 81 | u32 header, u32 size, uint8_t *ep) |
| 82 | { |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 83 | u32 hdr_read; |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 84 | int rc; |
| 85 | |
Jean-Jacques Hiblot | 52a5151 | 2018-12-07 14:50:49 +0100 | [diff] [blame] | 86 | #if defined(CONFIG_DM_I2C) |
Andreas Dannenberg | e315a66 | 2018-12-07 14:50:47 +0100 | [diff] [blame] | 87 | struct udevice *dev; |
| 88 | struct udevice *bus; |
| 89 | |
| 90 | rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus); |
| 91 | if (rc) |
| 92 | return rc; |
| 93 | rc = i2c_get_chip(bus, dev_addr, 1, &dev); |
| 94 | if (rc) |
| 95 | return rc; |
| 96 | |
| 97 | /* |
| 98 | * Read the header first then only read the other contents. |
| 99 | */ |
| 100 | rc = i2c_set_chip_offset_len(dev, 2); |
| 101 | if (rc) |
| 102 | return rc; |
| 103 | |
| 104 | rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4); |
| 105 | if (rc) |
| 106 | return rc; |
| 107 | |
| 108 | /* Corrupted data??? */ |
| 109 | if (hdr_read != header) { |
| 110 | rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4); |
| 111 | /* |
| 112 | * read the eeprom header using i2c again, but use only a |
| 113 | * 1 byte address (some legacy boards need this..) |
| 114 | */ |
| 115 | if (rc) { |
| 116 | rc = i2c_set_chip_offset_len(dev, 1); |
| 117 | if (rc) |
| 118 | return rc; |
| 119 | |
| 120 | rc = dm_i2c_read(dev, 0, (uint8_t *)&hdr_read, 4); |
| 121 | } |
| 122 | if (rc) |
| 123 | return rc; |
| 124 | } |
| 125 | if (hdr_read != header) |
| 126 | return -1; |
| 127 | |
| 128 | rc = dm_i2c_read(dev, 0, ep, size); |
| 129 | if (rc) |
| 130 | return rc; |
| 131 | #else |
| 132 | u32 byte; |
| 133 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 134 | gpi2c_init(); |
| 135 | rc = ti_i2c_eeprom_init(bus_addr, dev_addr); |
| 136 | if (rc) |
| 137 | return rc; |
| 138 | |
| 139 | /* |
| 140 | * Read the header first then only read the other contents. |
| 141 | */ |
| 142 | byte = 2; |
Cooper Jr., Franklin | 5b89c97 | 2017-04-20 10:25:45 -0500 | [diff] [blame] | 143 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 144 | rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4); |
| 145 | if (rc) |
| 146 | return rc; |
| 147 | |
| 148 | /* Corrupted data??? */ |
| 149 | if (hdr_read != header) { |
| 150 | rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4); |
| 151 | /* |
| 152 | * read the eeprom header using i2c again, but use only a |
| 153 | * 1 byte address (some legacy boards need this..) |
| 154 | */ |
| 155 | byte = 1; |
Cooper Jr., Franklin | 5b89c97 | 2017-04-20 10:25:45 -0500 | [diff] [blame] | 156 | if (rc) { |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 157 | rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, |
| 158 | 4); |
Cooper Jr., Franklin | 5b89c97 | 2017-04-20 10:25:45 -0500 | [diff] [blame] | 159 | } |
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 | |
Nishanth Menon | 27856ff | 2017-06-16 17:25:04 -0500 | [diff] [blame] | 173 | int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev) |
| 174 | { |
| 175 | struct ti_common_eeprom *ep; |
| 176 | |
| 177 | if (!name || !rev) |
| 178 | return -1; |
| 179 | |
| 180 | ep = TI_EEPROM_DATA; |
| 181 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
| 182 | goto already_set; |
| 183 | |
| 184 | /* Set to 0 all fields */ |
| 185 | memset(ep, 0, sizeof(*ep)); |
| 186 | strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN); |
| 187 | strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN); |
| 188 | /* Some dummy serial number to identify the platform */ |
| 189 | strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN); |
| 190 | /* Mark it with a valid header */ |
| 191 | ep->header = TI_EEPROM_HEADER_MAGIC; |
| 192 | |
| 193 | already_set: |
| 194 | return 0; |
| 195 | } |
| 196 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 197 | int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr) |
| 198 | { |
| 199 | int rc; |
| 200 | struct ti_am_eeprom am_ep; |
| 201 | struct ti_common_eeprom *ep; |
| 202 | |
| 203 | ep = TI_EEPROM_DATA; |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 204 | #ifndef CONFIG_SPL_BUILD |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 205 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 206 | return 0; /* EEPROM has already been read */ |
| 207 | #endif |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 208 | |
| 209 | /* Initialize with a known bad marker for i2c fails.. */ |
| 210 | ep->header = TI_DEAD_EEPROM_MAGIC; |
| 211 | ep->name[0] = 0x0; |
| 212 | ep->version[0] = 0x0; |
| 213 | ep->serial[0] = 0x0; |
Nishanth Menon | e2b0a52 | 2016-10-11 12:39:04 -0500 | [diff] [blame] | 214 | ep->config[0] = 0x0; |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 215 | |
| 216 | rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC, |
| 217 | sizeof(am_ep), (uint8_t *)&am_ep); |
| 218 | if (rc) |
| 219 | return rc; |
| 220 | |
| 221 | ep->header = am_ep.header; |
| 222 | strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1); |
| 223 | ti_eeprom_string_cleanup(ep->name); |
| 224 | |
| 225 | /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */ |
| 226 | if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 && |
| 227 | am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00) |
| 228 | strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1); |
| 229 | else |
| 230 | strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1); |
| 231 | ti_eeprom_string_cleanup(ep->version); |
| 232 | strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1); |
| 233 | ti_eeprom_string_cleanup(ep->serial); |
| 234 | strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1); |
| 235 | ti_eeprom_string_cleanup(ep->config); |
| 236 | |
| 237 | memcpy(ep->mac_addr, am_ep.mac_addr, |
| 238 | TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN); |
| 239 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 243 | int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr) |
| 244 | { |
| 245 | int rc, offset = 0; |
| 246 | struct dra7_eeprom dra7_ep; |
| 247 | struct ti_common_eeprom *ep; |
| 248 | |
| 249 | ep = TI_EEPROM_DATA; |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 250 | #ifndef CONFIG_SPL_BUILD |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 251 | if (ep->header == DRA7_EEPROM_HEADER_MAGIC) |
Jean-Jacques Hiblot | a096a76 | 2016-12-01 10:37:03 +0100 | [diff] [blame] | 252 | return 0; /* EEPROM has already been read */ |
| 253 | #endif |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 254 | |
| 255 | /* Initialize with a known bad marker for i2c fails.. */ |
Nishanth Menon | 21e54c0 | 2016-10-11 12:39:03 -0500 | [diff] [blame] | 256 | ep->header = TI_DEAD_EEPROM_MAGIC; |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 257 | ep->name[0] = 0x0; |
| 258 | ep->version[0] = 0x0; |
| 259 | ep->serial[0] = 0x0; |
Nishanth Menon | e2b0a52 | 2016-10-11 12:39:04 -0500 | [diff] [blame] | 260 | ep->config[0] = 0x0; |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 261 | ep->emif1_size = 0; |
| 262 | ep->emif2_size = 0; |
| 263 | |
| 264 | rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC, |
| 265 | sizeof(dra7_ep), (uint8_t *)&dra7_ep); |
| 266 | if (rc) |
| 267 | return rc; |
| 268 | |
| 269 | ep->header = dra7_ep.header; |
| 270 | strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1); |
| 271 | ti_eeprom_string_cleanup(ep->name); |
| 272 | |
| 273 | offset = dra7_ep.version_major - 1; |
| 274 | |
| 275 | /* Rev F is skipped */ |
| 276 | if (offset >= 5) |
| 277 | offset = offset + 1; |
| 278 | snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d", |
| 279 | 'A' + offset, dra7_ep.version_minor); |
| 280 | ti_eeprom_string_cleanup(ep->version); |
| 281 | ep->emif1_size = (u64)dra7_ep.emif1_size; |
| 282 | ep->emif2_size = (u64)dra7_ep.emif2_size; |
| 283 | strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1); |
| 284 | ti_eeprom_string_cleanup(ep->config); |
| 285 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 289 | static int ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record *record, |
| 290 | struct ti_am6_eeprom *ep, |
| 291 | char **mac_addr, |
| 292 | u8 mac_addr_max_cnt, |
| 293 | u8 *mac_addr_cnt) |
| 294 | { |
| 295 | switch (record->header.id) { |
| 296 | case TI_AM6_EEPROM_RECORD_BOARD_INFO: |
| 297 | if (record->header.len != sizeof(record->data.board_info)) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | if (!ep) |
| 301 | break; |
| 302 | |
| 303 | /* Populate (and clean, if needed) the board name */ |
| 304 | strlcpy(ep->name, record->data.board_info.name, |
| 305 | sizeof(ep->name)); |
| 306 | ti_eeprom_string_cleanup(ep->name); |
| 307 | |
| 308 | /* Populate selected other fields from the board info record */ |
| 309 | strlcpy(ep->version, record->data.board_info.version, |
| 310 | sizeof(ep->version)); |
| 311 | strlcpy(ep->software_revision, |
| 312 | record->data.board_info.software_revision, |
| 313 | sizeof(ep->software_revision)); |
| 314 | strlcpy(ep->serial, record->data.board_info.serial, |
| 315 | sizeof(ep->serial)); |
| 316 | break; |
| 317 | case TI_AM6_EEPROM_RECORD_MAC_INFO: |
| 318 | if (record->header.len != sizeof(record->data.mac_info)) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | if (!mac_addr || !mac_addr_max_cnt) |
| 322 | break; |
| 323 | |
| 324 | *mac_addr_cnt = ((record->data.mac_info.mac_control & |
| 325 | TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK) >> |
| 326 | TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT) + 1; |
| 327 | |
| 328 | /* |
| 329 | * The EEPROM can (but may not) hold a very large amount |
| 330 | * of MAC addresses, by far exceeding what we want/can store |
| 331 | * in the common memory array, so only grab what we can fit. |
| 332 | * Note that a value of 0 means 1 MAC address, and so on. |
| 333 | */ |
| 334 | *mac_addr_cnt = min(*mac_addr_cnt, mac_addr_max_cnt); |
| 335 | |
| 336 | memcpy(mac_addr, record->data.mac_info.mac_addr, |
| 337 | *mac_addr_cnt * TI_EEPROM_HDR_ETH_ALEN); |
| 338 | break; |
| 339 | case 0x00: |
| 340 | /* Illegal value... Fall through... */ |
| 341 | case 0xFF: |
| 342 | /* Illegal value... Something went horribly wrong... */ |
| 343 | return -EINVAL; |
| 344 | default: |
| 345 | pr_warn("%s: Ignoring record id %u\n", __func__, |
| 346 | record->header.id); |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr, |
| 353 | struct ti_am6_eeprom *ep, |
| 354 | char **mac_addr, |
| 355 | u8 mac_addr_max_cnt, |
| 356 | u8 *mac_addr_cnt) |
| 357 | { |
| 358 | struct udevice *dev; |
| 359 | struct udevice *bus; |
| 360 | unsigned int eeprom_addr; |
| 361 | struct ti_am6_eeprom_record_board_id board_id; |
| 362 | struct ti_am6_eeprom_record record; |
| 363 | int rc; |
| 364 | |
| 365 | /* Initialize with a known bad marker for i2c fails.. */ |
| 366 | memset(ep, 0, sizeof(*ep)); |
| 367 | ep->header = TI_DEAD_EEPROM_MAGIC; |
| 368 | |
| 369 | /* Read the board ID record which is always the first EEPROM record */ |
| 370 | rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC, |
| 371 | sizeof(board_id), (uint8_t *)&board_id); |
| 372 | if (rc) |
| 373 | return rc; |
| 374 | |
| 375 | if (board_id.header.id != TI_AM6_EEPROM_RECORD_BOARD_ID) { |
| 376 | pr_err("%s: Invalid board ID record!\n", __func__); |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | |
| 380 | /* Establish DM handle to board config EEPROM */ |
| 381 | rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus); |
| 382 | if (rc) |
| 383 | return rc; |
| 384 | rc = i2c_get_chip(bus, dev_addr, 1, &dev); |
| 385 | if (rc) |
| 386 | return rc; |
| 387 | |
| 388 | ep->header = TI_EEPROM_HEADER_MAGIC; |
| 389 | |
| 390 | /* Ready to parse TLV structure. Initialize variables... */ |
| 391 | *mac_addr_cnt = 0; |
| 392 | |
| 393 | /* |
| 394 | * After the all-encompassing board ID record all other records follow |
| 395 | * a TLV-type scheme. Point to the first such record and then start |
| 396 | * parsing those one by one. |
| 397 | */ |
| 398 | eeprom_addr = sizeof(board_id); |
| 399 | |
| 400 | while (true) { |
| 401 | rc = dm_i2c_read(dev, eeprom_addr, (uint8_t *)&record.header, |
| 402 | sizeof(record.header)); |
| 403 | if (rc) |
| 404 | return rc; |
| 405 | |
| 406 | /* |
| 407 | * Check for end of list marker. If we reached it don't go |
| 408 | * any further and stop parsing right here. |
| 409 | */ |
| 410 | if (record.header.id == TI_AM6_EEPROM_RECORD_END_LIST) |
| 411 | break; |
| 412 | |
| 413 | eeprom_addr += sizeof(record.header); |
| 414 | |
| 415 | debug("%s: dev_addr=0x%02x header.id=%u header.len=%u\n", |
| 416 | __func__, dev_addr, record.header.id, |
| 417 | record.header.len); |
| 418 | |
| 419 | /* Read record into memory if it fits */ |
| 420 | if (record.header.len <= sizeof(record.data)) { |
| 421 | rc = dm_i2c_read(dev, eeprom_addr, |
| 422 | (uint8_t *)&record.data, |
| 423 | record.header.len); |
| 424 | if (rc) |
| 425 | return rc; |
| 426 | |
| 427 | /* Process record */ |
| 428 | rc = ti_i2c_eeprom_am6_parse_record(&record, ep, |
| 429 | mac_addr, |
| 430 | mac_addr_max_cnt, |
| 431 | mac_addr_cnt); |
| 432 | if (rc) { |
| 433 | pr_err("%s: EEPROM parsing error!\n", __func__); |
| 434 | return rc; |
| 435 | } |
| 436 | } else { |
| 437 | /* |
| 438 | * We may get here in case of larger records which |
| 439 | * are not yet understood. |
| 440 | */ |
| 441 | pr_err("%s: Ignoring record id %u\n", __func__, |
| 442 | record.header.id); |
| 443 | } |
| 444 | |
| 445 | eeprom_addr += record.header.len; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr) |
| 452 | { |
| 453 | struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA; |
| 454 | int ret; |
| 455 | |
| 456 | /* |
| 457 | * Always execute EEPROM read by not allowing to bypass it during the |
| 458 | * first invocation of SPL which happens on the R5 core. |
| 459 | */ |
| 460 | #if !(defined(CONFIG_SPL_BUILD) && defined(CONFIG_CPU_V7R)) |
| 461 | if (ep->header == TI_EEPROM_HEADER_MAGIC) { |
| 462 | debug("%s: EEPROM has already been read\n", __func__); |
| 463 | return 0; |
| 464 | } |
| 465 | #endif |
| 466 | |
| 467 | ret = ti_i2c_eeprom_am6_get(bus_addr, dev_addr, ep, |
| 468 | (char **)ep->mac_addr, |
| 469 | AM6_EEPROM_HDR_NO_OF_MAC_ADDR, |
| 470 | &ep->mac_addr_cnt); |
| 471 | return ret; |
| 472 | } |
| 473 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 474 | bool __maybe_unused board_ti_is(char *name_tag) |
| 475 | { |
| 476 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 477 | |
| 478 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 479 | return false; |
| 480 | return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN); |
| 481 | } |
| 482 | |
| 483 | bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len) |
| 484 | { |
| 485 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 486 | int l; |
| 487 | |
| 488 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 489 | return false; |
| 490 | |
| 491 | l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len; |
| 492 | return !strncmp(ep->version, rev_tag, l); |
| 493 | } |
| 494 | |
| 495 | char * __maybe_unused board_ti_get_rev(void) |
| 496 | { |
| 497 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 498 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 499 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 500 | return ep->version; |
| 501 | } |
| 502 | |
| 503 | char * __maybe_unused board_ti_get_config(void) |
| 504 | { |
| 505 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 506 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 507 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 508 | return ep->config; |
| 509 | } |
| 510 | |
| 511 | char * __maybe_unused board_ti_get_name(void) |
| 512 | { |
| 513 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 514 | |
Nishanth Menon | 23afc8a | 2016-10-11 12:39:05 -0500 | [diff] [blame] | 515 | /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */ |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 516 | return ep->name; |
| 517 | } |
| 518 | |
| 519 | void __maybe_unused |
| 520 | board_ti_get_eth_mac_addr(int index, |
| 521 | u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN]) |
| 522 | { |
| 523 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 524 | |
| 525 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 526 | goto fail; |
| 527 | |
| 528 | if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR) |
| 529 | goto fail; |
| 530 | |
| 531 | memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN); |
| 532 | return; |
| 533 | |
| 534 | fail: |
| 535 | memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN); |
| 536 | } |
| 537 | |
Andreas Dannenberg | 5a64711 | 2019-06-04 18:08:25 -0500 | [diff] [blame] | 538 | void __maybe_unused |
| 539 | board_ti_am6_get_eth_mac_addr(int index, |
| 540 | u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN]) |
| 541 | { |
| 542 | struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA; |
| 543 | |
| 544 | if (ep->header == TI_DEAD_EEPROM_MAGIC) |
| 545 | goto fail; |
| 546 | |
| 547 | if (index < 0 || index >= ep->mac_addr_cnt) |
| 548 | goto fail; |
| 549 | |
| 550 | memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN); |
| 551 | return; |
| 552 | |
| 553 | fail: |
| 554 | memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN); |
| 555 | } |
| 556 | |
Lokesh Vutla | 7c08a78 | 2016-03-08 09:18:04 +0530 | [diff] [blame] | 557 | u64 __maybe_unused board_ti_get_emif1_size(void) |
| 558 | { |
| 559 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 560 | |
| 561 | if (ep->header != DRA7_EEPROM_HEADER_MAGIC) |
| 562 | return 0; |
| 563 | |
| 564 | return ep->emif1_size; |
| 565 | } |
| 566 | |
| 567 | u64 __maybe_unused board_ti_get_emif2_size(void) |
| 568 | { |
| 569 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 570 | |
| 571 | if (ep->header != DRA7_EEPROM_HEADER_MAGIC) |
| 572 | return 0; |
| 573 | |
| 574 | return ep->emif2_size; |
| 575 | } |
| 576 | |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 577 | void __maybe_unused set_board_info_env(char *name) |
| 578 | { |
| 579 | char *unknown = "unknown"; |
| 580 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 581 | |
| 582 | if (name) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 583 | env_set("board_name", name); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 584 | else if (ep->name) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 585 | env_set("board_name", ep->name); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 586 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 587 | env_set("board_name", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 588 | |
| 589 | if (ep->version) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 590 | env_set("board_rev", ep->version); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 591 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 592 | env_set("board_rev", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 593 | |
| 594 | if (ep->serial) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 595 | env_set("board_serial", ep->serial); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 596 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 597 | env_set("board_serial", unknown); |
Lokesh Vutla | 35f7a97 | 2016-02-24 12:30:54 -0600 | [diff] [blame] | 598 | } |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 599 | |
Andreas Dannenberg | f998a82 | 2019-06-04 18:08:24 -0500 | [diff] [blame] | 600 | void __maybe_unused set_board_info_env_am6(char *name) |
| 601 | { |
| 602 | char *unknown = "unknown"; |
| 603 | struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA; |
| 604 | |
| 605 | if (name) |
| 606 | env_set("board_name", name); |
| 607 | else if (ep->name) |
| 608 | env_set("board_name", ep->name); |
| 609 | else |
| 610 | env_set("board_name", unknown); |
| 611 | |
| 612 | if (ep->version) |
| 613 | env_set("board_rev", ep->version); |
| 614 | else |
| 615 | env_set("board_rev", unknown); |
| 616 | |
| 617 | if (ep->software_revision) |
| 618 | env_set("board_software_revision", ep->software_revision); |
| 619 | else |
| 620 | env_set("board_software_revision", unknown); |
| 621 | |
| 622 | if (ep->serial) |
| 623 | env_set("board_serial", ep->serial); |
| 624 | else |
| 625 | env_set("board_serial", unknown); |
| 626 | } |
| 627 | |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 628 | static u64 mac_to_u64(u8 mac[6]) |
| 629 | { |
| 630 | int i; |
| 631 | u64 addr = 0; |
| 632 | |
| 633 | for (i = 0; i < 6; i++) { |
| 634 | addr <<= 8; |
| 635 | addr |= mac[i]; |
| 636 | } |
| 637 | |
| 638 | return addr; |
| 639 | } |
| 640 | |
| 641 | static void u64_to_mac(u64 addr, u8 mac[6]) |
| 642 | { |
| 643 | mac[5] = addr; |
| 644 | mac[4] = addr >> 8; |
| 645 | mac[3] = addr >> 16; |
| 646 | mac[2] = addr >> 24; |
| 647 | mac[1] = addr >> 32; |
| 648 | mac[0] = addr >> 40; |
| 649 | } |
| 650 | |
| 651 | void board_ti_set_ethaddr(int index) |
| 652 | { |
| 653 | uint8_t mac_addr[6]; |
| 654 | int i; |
| 655 | u64 mac1, mac2; |
| 656 | u8 mac_addr1[6], mac_addr2[6]; |
| 657 | int num_macs; |
| 658 | /* |
| 659 | * Export any Ethernet MAC addresses from EEPROM. |
| 660 | * The 2 MAC addresses in EEPROM define the address range. |
| 661 | */ |
| 662 | board_ti_get_eth_mac_addr(0, mac_addr1); |
| 663 | board_ti_get_eth_mac_addr(1, mac_addr2); |
| 664 | |
| 665 | if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) { |
| 666 | mac1 = mac_to_u64(mac_addr1); |
| 667 | mac2 = mac_to_u64(mac_addr2); |
| 668 | |
| 669 | /* must contain an address range */ |
| 670 | num_macs = mac2 - mac1 + 1; |
| 671 | if (num_macs <= 0) |
| 672 | return; |
| 673 | |
| 674 | if (num_macs > 50) { |
| 675 | printf("%s: Too many MAC addresses: %d. Limiting to 50\n", |
| 676 | __func__, num_macs); |
| 677 | num_macs = 50; |
| 678 | } |
| 679 | |
| 680 | for (i = 0; i < num_macs; i++) { |
| 681 | u64_to_mac(mac1 + i, mac_addr); |
| 682 | if (is_valid_ethaddr(mac_addr)) { |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 683 | eth_env_set_enetaddr_by_index("eth", i + index, |
| 684 | mac_addr); |
Roger Quadros | 44c7d9a | 2017-03-14 15:04:19 +0200 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
Cooper Jr., Franklin | e1328c7 | 2017-06-16 17:25:08 -0500 | [diff] [blame] | 689 | |
Andreas Dannenberg | 5a64711 | 2019-06-04 18:08:25 -0500 | [diff] [blame] | 690 | void board_ti_am6_set_ethaddr(int index, int count) |
| 691 | { |
| 692 | u8 mac_addr[6]; |
| 693 | int i; |
| 694 | |
| 695 | for (i = 0; i < count; i++) { |
| 696 | board_ti_am6_get_eth_mac_addr(i, mac_addr); |
| 697 | if (is_valid_ethaddr(mac_addr)) |
| 698 | eth_env_set_enetaddr_by_index("eth", i + index, |
| 699 | mac_addr); |
| 700 | } |
| 701 | } |
| 702 | |
Cooper Jr., Franklin | e1328c7 | 2017-06-16 17:25:08 -0500 | [diff] [blame] | 703 | bool __maybe_unused board_ti_was_eeprom_read(void) |
| 704 | { |
| 705 | struct ti_common_eeprom *ep = TI_EEPROM_DATA; |
| 706 | |
| 707 | if (ep->header == TI_EEPROM_HEADER_MAGIC) |
| 708 | return true; |
| 709 | else |
| 710 | return false; |
| 711 | } |