Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Gateworks Corporation |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <gsc.h> |
| 8 | #include <hexdump.h> |
| 9 | #include <i2c.h> |
| 10 | #include <dm/uclass.h> |
| 11 | |
| 12 | #include "eeprom.h" |
| 13 | |
| 14 | /* I2C */ |
| 15 | #define SOM_EEPROM_BUSNO 0 |
| 16 | #define SOM_EEPROM_ADDR 0x51 |
| 17 | #define BASEBOARD_EEPROM_BUSNO 1 |
| 18 | #define BASEBOARD_EEPROM_ADDR 0x52 |
| 19 | |
| 20 | struct venice_board_info som_info; |
| 21 | struct venice_board_info base_info; |
| 22 | char venice_model[32]; |
| 23 | u32 venice_serial; |
| 24 | |
| 25 | /* return a mac address from EEPROM info */ |
| 26 | int eeprom_getmac(int index, uint8_t *address) |
| 27 | { |
| 28 | int i, j; |
| 29 | u32 maclow, machigh; |
| 30 | u64 mac; |
| 31 | |
| 32 | j = 0; |
| 33 | if (som_info.macno) { |
| 34 | maclow = som_info.mac[5]; |
| 35 | maclow |= som_info.mac[4] << 8; |
| 36 | maclow |= som_info.mac[3] << 16; |
| 37 | maclow |= som_info.mac[2] << 24; |
| 38 | machigh = som_info.mac[1]; |
| 39 | machigh |= som_info.mac[0] << 8; |
| 40 | mac = machigh; |
| 41 | mac <<= 32; |
| 42 | mac |= maclow; |
| 43 | for (i = 0; i < som_info.macno; i++, j++) { |
| 44 | if (index == j) |
| 45 | goto out; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | maclow = base_info.mac[5]; |
| 50 | maclow |= base_info.mac[4] << 8; |
| 51 | maclow |= base_info.mac[3] << 16; |
| 52 | maclow |= base_info.mac[2] << 24; |
| 53 | machigh = base_info.mac[1]; |
| 54 | machigh |= base_info.mac[0] << 8; |
| 55 | mac = machigh; |
| 56 | mac <<= 32; |
| 57 | mac |= maclow; |
| 58 | for (i = 0; i < base_info.macno; i++, j++) { |
| 59 | if (index == j) |
| 60 | goto out; |
| 61 | } |
| 62 | |
| 63 | return -EINVAL; |
| 64 | |
| 65 | out: |
| 66 | mac += i; |
| 67 | address[0] = (mac >> 40) & 0xff; |
| 68 | address[1] = (mac >> 32) & 0xff; |
| 69 | address[2] = (mac >> 24) & 0xff; |
| 70 | address[3] = (mac >> 16) & 0xff; |
| 71 | address[4] = (mac >> 8) & 0xff; |
| 72 | address[5] = (mac >> 0) & 0xff; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int eeprom_read(int busno, int slave, int alen, struct venice_board_info *info) |
| 78 | { |
| 79 | int i; |
| 80 | int chksum; |
| 81 | unsigned char *buf = (unsigned char *)info; |
| 82 | struct udevice *dev, *bus; |
| 83 | int ret; |
| 84 | |
| 85 | /* probe device */ |
| 86 | ret = uclass_get_device_by_seq(UCLASS_I2C, busno, &bus); |
| 87 | if (ret) |
| 88 | return ret; |
| 89 | ret = dm_i2c_probe(bus, slave, 0, &dev); |
| 90 | if (ret) |
| 91 | return ret; |
| 92 | |
| 93 | /* read eeprom config section */ |
| 94 | memset(info, 0, sizeof(*info)); |
| 95 | ret = i2c_set_chip_offset_len(dev, alen); |
| 96 | if (ret) { |
| 97 | puts("EEPROM: Failed to set alen\n"); |
| 98 | return ret; |
| 99 | } |
| 100 | ret = dm_i2c_read(dev, 0x00, buf, sizeof(*info)); |
| 101 | if (ret) { |
| 102 | if (slave == SOM_EEPROM_ADDR) |
| 103 | printf("EEPROM: Failed to read EEPROM\n"); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | /* validate checksum */ |
| 108 | for (chksum = 0, i = 0; i < (int)sizeof(*info) - 2; i++) |
| 109 | chksum += buf[i]; |
| 110 | if ((info->chksum[0] != chksum >> 8) || |
| 111 | (info->chksum[1] != (chksum & 0xff))) { |
| 112 | printf("EEPROM: I2C%d@0x%02x: Invalid Checksum\n", busno, slave); |
| 113 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, sizeof(*info)); |
| 114 | memset(info, 0, sizeof(*info)); |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | |
| 118 | /* sanity check valid model */ |
| 119 | if (info->model[0] != 'G' || info->model[1] != 'W') { |
| 120 | printf("EEPROM: I2C%d@0x%02x: Invalid Model in EEPROM\n", busno, slave); |
| 121 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, sizeof(*info)); |
| 122 | memset(info, 0, sizeof(*info)); |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /* determine BOM revision from model */ |
| 130 | int get_bom_rev(const char *str) |
| 131 | { |
| 132 | int rev_bom = 0; |
| 133 | int i; |
| 134 | |
| 135 | for (i = strlen(str) - 1; i > 0; i--) { |
| 136 | if (str[i] == '-') |
| 137 | break; |
| 138 | if (str[i] >= '1' && str[i] <= '9') { |
| 139 | rev_bom = str[i] - '0'; |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | return rev_bom; |
| 144 | } |
| 145 | |
| 146 | /* determine PCB revision from model */ |
| 147 | char get_pcb_rev(const char *str) |
| 148 | { |
| 149 | char rev_pcb = 'A'; |
| 150 | int i; |
| 151 | |
| 152 | for (i = strlen(str) - 1; i > 0; i--) { |
| 153 | if (str[i] == '-') |
| 154 | break; |
| 155 | if (str[i] >= 'A') { |
| 156 | rev_pcb = str[i]; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | return rev_pcb; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * get dt name based on model and detail level: |
| 165 | * |
| 166 | * For boards that are a combination of a SoM plus a Baseboard: |
| 167 | * Venice SoM part numbers are GW70xx where xx is: |
| 168 | * 7000-7019: same PCB with som dt of '0x' |
| 169 | * 7020-7039: same PCB with som dt of '2x' |
| 170 | * 7040-7059: same PCB with som dt of '4x' |
| 171 | * 7060-7079: same PCB with som dt of '6x' |
| 172 | * 7080-7099: same PCB with som dt of '8x' |
| 173 | * Venice Baseboard part numbers are GW7xxx where xxx is: |
| 174 | * 7100-7199: same PCB with base dt of '71xx' |
| 175 | * 7200-7299: same PCB with base dt of '72xx' |
| 176 | * 7300-7399: same PCB with base dt of '73xx' |
| 177 | * 7400-7499: same PCB with base dt of '74xx' |
| 178 | * 7500-7599: same PCB with base dt of '75xx' |
| 179 | * 7600-7699: same PCB with base dt of '76xx' |
| 180 | * 7700-7799: same PCB with base dt of '77xx' |
| 181 | * 7800-7899: same PCB with base dt of '78xx' |
| 182 | * DT name is comprised of: |
| 183 | * gw<base dt>-<som dt>-[base-pcb-rev][base-bom-rev][som-pcb-rev][som-bom-rev] |
| 184 | * |
| 185 | * For board models from 7900-7999 each PCB is unique with its own dt: |
| 186 | * DT name is comprised: |
| 187 | * gw<model>-[pcb-rev][bom-rev] |
| 188 | * |
| 189 | */ |
| 190 | #define snprintfcat(dest, sz, fmt, ...) \ |
| 191 | snprintf((dest) + strlen(dest), (sz) - strlen(dest), fmt, ##__VA_ARGS__) |
| 192 | const char *eeprom_get_dtb_name(int level, char *buf, int sz) |
| 193 | { |
| 194 | #ifdef CONFIG_IMX8MM |
| 195 | const char *pre = "imx8mm-venice-gw"; |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame^] | 196 | #elif CONFIG_IMX8MN |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 197 | const char *pre = "imx8mn-venice-gw"; |
Tim Harvey | 0f5717f | 2022-04-13 11:31:09 -0700 | [diff] [blame^] | 198 | #elif CONFIG_IMX8MP |
| 199 | const char *pre = "imx8mp-venice-gw"; |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 200 | #endif |
| 201 | int model, rev_pcb, rev_bom; |
| 202 | |
| 203 | model = ((som_info.model[2] - '0') * 1000) |
| 204 | + ((som_info.model[3] - '0') * 100) |
| 205 | + ((som_info.model[4] - '0') * 10) |
| 206 | + (som_info.model[5] - '0'); |
| 207 | rev_pcb = tolower(get_pcb_rev(som_info.model)); |
| 208 | rev_bom = get_bom_rev(som_info.model); |
| 209 | |
| 210 | /* som + baseboard*/ |
| 211 | if (base_info.model[0]) { |
| 212 | /* baseboard id: 7100-7199->71; 7200-7299->72; etc */ |
| 213 | int base = ((base_info.model[2] - '0') * 10) + (base_info.model[3] - '0'); |
| 214 | /* som id: 7000-7019->1; 7020-7039->2; etc */ |
| 215 | int som = ((model % 100) / 20) * 2; |
| 216 | int rev_base_pcb = tolower(get_pcb_rev(base_info.model)); |
| 217 | int rev_base_bom = get_bom_rev(base_info.model); |
| 218 | |
| 219 | snprintf(buf, sz, "%s%2dxx-%dx", pre, base, som); |
| 220 | switch (level) { |
| 221 | case 0: /* full model (ie gw73xx-0x-a1a1) */ |
| 222 | if (rev_base_bom) |
| 223 | snprintfcat(buf, sz, "-%c%d", rev_base_pcb, rev_base_bom); |
| 224 | else |
| 225 | snprintfcat(buf, sz, "-%c", rev_base_pcb); |
| 226 | if (rev_bom) |
| 227 | snprintfcat(buf, sz, "%c%d", rev_pcb, rev_bom); |
| 228 | else |
| 229 | snprintfcat(buf, sz, "%c", rev_pcb); |
| 230 | break; |
| 231 | case 1: /* don't care about SoM revision */ |
| 232 | if (rev_base_bom) |
| 233 | snprintfcat(buf, sz, "-%c%d", rev_base_pcb, rev_base_bom); |
| 234 | else |
| 235 | snprintfcat(buf, sz, "-%c", rev_base_pcb); |
| 236 | snprintfcat(buf, sz, "xx"); |
| 237 | break; |
| 238 | case 2: /* don't care about baseboard revision */ |
| 239 | snprintfcat(buf, sz, "-xx"); |
| 240 | if (rev_bom) |
| 241 | snprintfcat(buf, sz, "%c%d", rev_pcb, rev_bom); |
| 242 | else |
| 243 | snprintfcat(buf, sz, "%c", rev_pcb); |
| 244 | break; |
| 245 | case 3: /* don't care about SoM/baseboard revision */ |
| 246 | break; |
| 247 | default: |
| 248 | return NULL; |
| 249 | } |
| 250 | } else { |
| 251 | snprintf(buf, sz, "%s%04d", pre, model); |
| 252 | switch (level) { |
| 253 | case 0: /* full model wth PCB and BOM revision first (ie gw7901-a1) */ |
| 254 | if (rev_bom) |
| 255 | snprintfcat(buf, sz, "-%c%d", rev_pcb, rev_bom); |
| 256 | else |
| 257 | snprintfcat(buf, sz, "-%c", rev_pcb); |
| 258 | break; |
| 259 | case 1: /* don't care about BOM revision */ |
| 260 | snprintfcat(buf, sz, "-%c", rev_pcb); |
| 261 | break; |
| 262 | case 2: /* don't care about PCB or BOM revision */ |
| 263 | break; |
Tim Harvey | 4efcc64 | 2022-04-13 11:31:08 -0700 | [diff] [blame] | 264 | case 3: /* don't care about last digit of model */ |
| 265 | buf[strlen(buf) - 1] = 'x'; |
| 266 | break; |
| 267 | case 4: /* don't care about last two digits of model */ |
| 268 | buf[strlen(buf) - 1] = 'x'; |
| 269 | buf[strlen(buf) - 2] = 'x'; |
| 270 | break; |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 271 | default: |
| 272 | return NULL; |
Tim Harvey | 4efcc64 | 2022-04-13 11:31:08 -0700 | [diff] [blame] | 273 | break; |
Tim Harvey | d4daeaa | 2022-04-13 08:56:40 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | return buf; |
| 278 | } |
| 279 | |
| 280 | static int eeprom_info(bool verbose) |
| 281 | { |
| 282 | printf("Model : %s\n", venice_model); |
| 283 | printf("Serial : %d\n", som_info.serial); |
| 284 | printf("MFGDate : %02x-%02x-%02x%02x\n", |
| 285 | som_info.mfgdate[0], som_info.mfgdate[1], |
| 286 | som_info.mfgdate[2], som_info.mfgdate[3]); |
| 287 | if (base_info.model[0] && verbose) { |
| 288 | printf("SOM : %s %d %02x-%02x-%02x%02x\n", |
| 289 | som_info.model, som_info.serial, |
| 290 | som_info.mfgdate[0], som_info.mfgdate[1], |
| 291 | som_info.mfgdate[2], som_info.mfgdate[3]); |
| 292 | printf("BASE : %s %d %02x-%02x-%02x%02x\n", |
| 293 | base_info.model, base_info.serial, |
| 294 | base_info.mfgdate[0], base_info.mfgdate[1], |
| 295 | base_info.mfgdate[2], base_info.mfgdate[3]); |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | int eeprom_init(int quiet) |
| 302 | { |
| 303 | char rev_pcb; |
| 304 | int rev_bom; |
| 305 | int ret; |
| 306 | |
| 307 | ret = eeprom_read(SOM_EEPROM_BUSNO, SOM_EEPROM_ADDR, 1, &som_info); |
| 308 | if (ret) { |
| 309 | puts("ERROR: Failed to probe EEPROM\n"); |
| 310 | memset(&som_info, 0, sizeof(som_info)); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* read optional baseboard EEPROM */ |
| 315 | eeprom_read(BASEBOARD_EEPROM_BUSNO, BASEBOARD_EEPROM_ADDR, 2, &base_info); |
| 316 | |
| 317 | /* create model strings */ |
| 318 | if (base_info.model[0]) { |
| 319 | sprintf(venice_model, "GW%c%c%c%c-%c%c-", |
| 320 | som_info.model[2], /* family */ |
| 321 | base_info.model[3], /* baseboard */ |
| 322 | base_info.model[4], base_info.model[5], /* subload of baseboard */ |
| 323 | som_info.model[4], som_info.model[5]); /* last 2digits of SOM */ |
| 324 | |
| 325 | /* baseboard revision */ |
| 326 | rev_pcb = get_pcb_rev(base_info.model); |
| 327 | rev_bom = get_bom_rev(base_info.model); |
| 328 | if (rev_bom) |
| 329 | sprintf(venice_model + strlen(venice_model), "%c%d", rev_pcb, rev_bom); |
| 330 | else |
| 331 | sprintf(venice_model + strlen(venice_model), "%c", rev_pcb); |
| 332 | /* som revision */ |
| 333 | rev_pcb = get_pcb_rev(som_info.model); |
| 334 | rev_bom = get_bom_rev(som_info.model); |
| 335 | if (rev_bom) |
| 336 | sprintf(venice_model + strlen(venice_model), "%c%d", rev_pcb, rev_bom); |
| 337 | else |
| 338 | sprintf(venice_model + strlen(venice_model), "%c", rev_pcb); |
| 339 | } else { |
| 340 | strcpy(venice_model, som_info.model); |
| 341 | } |
| 342 | venice_serial = som_info.serial; |
| 343 | |
| 344 | if (!quiet) |
| 345 | eeprom_info(false); |
| 346 | |
| 347 | return (16 << som_info.sdram_size); |
| 348 | } |
| 349 | |
| 350 | void board_gsc_info(void) |
| 351 | { |
| 352 | eeprom_info(true); |
| 353 | } |
| 354 | |
| 355 | const char *eeprom_get_model(void) |
| 356 | { |
| 357 | return venice_model; |
| 358 | } |
| 359 | |
| 360 | u32 eeprom_get_serial(void) |
| 361 | { |
| 362 | return venice_serial; |
| 363 | } |