Yanhong Wang | 39331e4 | 2023-06-15 17:36:48 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2023 StarFive Technology Co., Ltd. |
| 4 | * Author: Yanhong Wang<yanhong.wang@starfivetech.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <env.h> |
| 10 | #include <i2c.h> |
| 11 | #include <init.h> |
| 12 | #include <u-boot/crc.h> |
| 13 | #include <linux/delay.h> |
| 14 | |
| 15 | #define FORMAT_VERSION 0x2 |
| 16 | #define PCB_VERSION 0xB1 |
| 17 | #define BOM_VERSION 'A' |
| 18 | /* |
| 19 | * BYTES_PER_EEPROM_PAGE: the 24FC04H datasheet says that data can |
| 20 | * only be written in page mode, which means 16 bytes at a time: |
| 21 | * 16-Byte Page Write Buffer |
| 22 | */ |
| 23 | #define BYTES_PER_EEPROM_PAGE 16 |
| 24 | |
| 25 | /* |
| 26 | * EEPROM_WRITE_DELAY_MS: the 24FC04H datasheet says it takes up to |
| 27 | * 5ms to complete a given write: |
| 28 | * Write Cycle Time (byte or page) ro Page Write Time 5 ms, Maximum |
| 29 | */ |
| 30 | #define EEPROM_WRITE_DELAY_MS 5000 |
| 31 | /* |
| 32 | * StarFive OUI. Registration Date is 20xx-xx-xx |
| 33 | */ |
| 34 | #define STARFIVE_OUI_PREFIX "6C:CF:39:" |
| 35 | #define STARFIVE_DEFAULT_MAC0 "6C:CF:39:6C:DE:AD" |
| 36 | #define STARFIVE_DEFAULT_MAC1 "6C:CF:39:6C:DE:AE" |
| 37 | |
| 38 | /* Magic number at the first four bytes of EEPROM HATs */ |
| 39 | #define STARFIVE_EEPROM_HATS_SIG "SFVF" /* StarFive VisionFive */ |
| 40 | |
| 41 | #define STARFIVE_EEPROM_HATS_SIZE_MAX 256 /* Header + Atom1&4(v1) */ |
| 42 | #define STARFIVE_EEPROM_WP_OFFSET 0 /* Read only field */ |
| 43 | #define STARFIVE_EEPROM_ATOM1_PSTR "VF7110A1-2228-D008E000-00000001\0" |
| 44 | #define STARFIVE_EEPROM_ATOM1_PSTR_SIZE 32 |
| 45 | #define STARFIVE_EEPROM_ATOM1_SN_OFFSET 23 |
| 46 | #define STARFIVE_EEPROM_ATOM1_VSTR "StarFive Technology Co., Ltd.\0\0\0" |
| 47 | #define STARFIVE_EEPROM_ATOM1_VSTR_SIZE 32 |
| 48 | |
| 49 | #define MAGIC_NUMBER_BYTES 4 |
| 50 | #define MAC_ADDR_BYTES 6 |
| 51 | #define MAC_ADDR_STRLEN 17 |
| 52 | |
| 53 | /* |
| 54 | * Atom Types |
| 55 | * 0x0000 = invalid |
| 56 | * 0x0001 = vendor info |
| 57 | * 0x0002 = GPIO map |
| 58 | * 0x0003 = Linux device tree blob |
| 59 | * 0x0004 = manufacturer custom data |
| 60 | * 0x0005-0xfffe = reserved for future use |
| 61 | * 0xffff = invalid |
| 62 | */ |
| 63 | |
| 64 | #define HATS_ATOM_INVALID 0x0000 |
| 65 | #define HATS_ATOM_VENDOR 0x0001 |
| 66 | #define HATS_ATOM_GPIO 0x0002 |
| 67 | #define HATS_ATOM_DTB 0x0003 |
| 68 | #define HATS_ATOM_CUSTOM 0x0004 |
| 69 | #define HATS_ATOM_INVALID_END 0xffff |
| 70 | |
| 71 | struct eeprom_header { |
| 72 | char signature[MAGIC_NUMBER_BYTES]; /* ASCII table signature */ |
| 73 | u8 version; /* EEPROM data format version */ |
| 74 | /* (0x00 reserved, 0x01 = first version) */ |
| 75 | u8 reversed; /* 0x00, Reserved field */ |
| 76 | u16 numatoms; /* total atoms in EEPROM */ |
| 77 | u32 eeplen; /* total length in bytes of all eeprom data */ |
| 78 | /* (including this header) */ |
| 79 | }; |
| 80 | |
| 81 | struct eeprom_atom_header { |
| 82 | u16 type; |
| 83 | u16 count; |
| 84 | u32 dlen; |
| 85 | }; |
| 86 | |
| 87 | struct eeprom_atom1_data { |
| 88 | u8 uuid[16]; |
| 89 | u16 pid; |
| 90 | u16 pver; |
| 91 | u8 vslen; |
| 92 | u8 pslen; |
| 93 | uchar vstr[STARFIVE_EEPROM_ATOM1_VSTR_SIZE]; |
| 94 | uchar pstr[STARFIVE_EEPROM_ATOM1_PSTR_SIZE]; /* product SN */ |
| 95 | }; |
| 96 | |
| 97 | struct starfive_eeprom_atom1 { |
| 98 | struct eeprom_atom_header header; |
| 99 | struct eeprom_atom1_data data; |
| 100 | u16 crc; |
| 101 | }; |
| 102 | |
| 103 | struct eeprom_atom4_data { |
| 104 | u16 version; |
| 105 | u8 pcb_revision; /* PCB version */ |
| 106 | u8 bom_revision; /* BOM version */ |
| 107 | u8 mac0_addr[MAC_ADDR_BYTES]; /* Ethernet0 MAC */ |
| 108 | u8 mac1_addr[MAC_ADDR_BYTES]; /* Ethernet1 MAC */ |
| 109 | u8 reserved[2]; |
| 110 | }; |
| 111 | |
| 112 | struct starfive_eeprom_atom4 { |
| 113 | struct eeprom_atom_header header; |
| 114 | struct eeprom_atom4_data data; |
| 115 | u16 crc; |
| 116 | }; |
| 117 | |
| 118 | struct starfive_eeprom { |
| 119 | struct eeprom_header header; |
| 120 | struct starfive_eeprom_atom1 atom1; |
| 121 | struct starfive_eeprom_atom4 atom4; |
| 122 | }; |
| 123 | |
| 124 | static union { |
| 125 | struct starfive_eeprom eeprom; |
| 126 | uchar buf[STARFIVE_EEPROM_HATS_SIZE_MAX]; |
| 127 | } pbuf __section(".data"); |
| 128 | |
| 129 | /* Set to 1 if we've read EEPROM into memory */ |
| 130 | static int has_been_read __section(".data"); |
| 131 | |
| 132 | static inline int is_match_magic(void) |
| 133 | { |
| 134 | return strncmp(pbuf.eeprom.header.signature, STARFIVE_EEPROM_HATS_SIG, |
| 135 | MAGIC_NUMBER_BYTES); |
| 136 | } |
| 137 | |
| 138 | /* Calculate the current CRC */ |
| 139 | static inline u32 calculate_crc16(struct eeprom_atom_header *head) |
| 140 | { |
| 141 | uint len = sizeof(struct eeprom_atom_header) + head->dlen - sizeof(u16); |
| 142 | |
| 143 | return crc16(0, (void *)head, len); |
| 144 | } |
| 145 | |
| 146 | /* This function should be called after each update to the EEPROM structure */ |
| 147 | static inline void update_crc(void) |
| 148 | { |
| 149 | pbuf.eeprom.atom1.crc = calculate_crc16(&pbuf.eeprom.atom1.header); |
| 150 | pbuf.eeprom.atom4.crc = calculate_crc16(&pbuf.eeprom.atom4.header); |
| 151 | } |
| 152 | |
| 153 | static void dump_raw_eeprom(void) |
| 154 | { |
| 155 | unsigned int i; |
| 156 | u32 len; |
| 157 | |
| 158 | len = sizeof(struct starfive_eeprom); |
| 159 | for (i = 0; i < len; i++) { |
| 160 | if ((i % 16) == 0) |
| 161 | printf("%02X: ", i); |
| 162 | printf("%02X ", ((u8 *)pbuf.buf)[i]); |
| 163 | if (((i % 16) == 15) || (i == len - 1)) |
| 164 | printf("\n"); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * show_eeprom - display the contents of the EEPROM |
| 170 | */ |
| 171 | static void show_eeprom(void) |
| 172 | { |
| 173 | if (has_been_read != 1) |
| 174 | return; |
| 175 | |
| 176 | printf("\n--------EEPROM INFO--------\n"); |
| 177 | printf("Vendor : %s\n", pbuf.eeprom.atom1.data.vstr); |
| 178 | printf("Product full SN: %s\n", pbuf.eeprom.atom1.data.pstr); |
| 179 | printf("data version: 0x%x\n", pbuf.eeprom.atom4.data.version); |
| 180 | if (pbuf.eeprom.atom4.data.version == 2) { |
| 181 | printf("PCB revision: 0x%x\n", pbuf.eeprom.atom4.data.pcb_revision); |
| 182 | printf("BOM revision: %c\n", pbuf.eeprom.atom4.data.bom_revision); |
| 183 | printf("Ethernet MAC0 address: %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 184 | pbuf.eeprom.atom4.data.mac0_addr[0], pbuf.eeprom.atom4.data.mac0_addr[1], |
| 185 | pbuf.eeprom.atom4.data.mac0_addr[2], pbuf.eeprom.atom4.data.mac0_addr[3], |
| 186 | pbuf.eeprom.atom4.data.mac0_addr[4], pbuf.eeprom.atom4.data.mac0_addr[5]); |
| 187 | printf("Ethernet MAC1 address: %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 188 | pbuf.eeprom.atom4.data.mac1_addr[0], pbuf.eeprom.atom4.data.mac1_addr[1], |
| 189 | pbuf.eeprom.atom4.data.mac1_addr[2], pbuf.eeprom.atom4.data.mac1_addr[3], |
| 190 | pbuf.eeprom.atom4.data.mac1_addr[4], pbuf.eeprom.atom4.data.mac1_addr[5]); |
| 191 | } else { |
| 192 | printf("Custom data v%d is not Supported\n", pbuf.eeprom.atom4.data.version); |
| 193 | } |
| 194 | printf("--------EEPROM INFO--------\n\n"); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * set_mac_address() - stores a MAC address into the local EEPROM copy |
| 199 | * |
| 200 | * This function takes a pointer to MAC address string |
| 201 | * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number), |
| 202 | * stores it in the MAC address field of the EEPROM local copy, and |
| 203 | * updates the local copy of the CRC. |
| 204 | */ |
| 205 | static void set_mac_address(char *string, int index) |
| 206 | { |
| 207 | u8 i; |
| 208 | u8 *mac; |
| 209 | |
| 210 | if (strncasecmp(STARFIVE_OUI_PREFIX, string, |
| 211 | strlen(STARFIVE_OUI_PREFIX))) { |
| 212 | printf("The MAC address doesn't match StarFive OUI %s\n", |
| 213 | STARFIVE_OUI_PREFIX); |
| 214 | return; |
| 215 | } |
| 216 | mac = (index == 0) ? pbuf.eeprom.atom4.data.mac0_addr : |
| 217 | pbuf.eeprom.atom4.data.mac1_addr; |
| 218 | |
| 219 | for (i = 0; *string && (i < MAC_ADDR_BYTES); i++) { |
| 220 | mac[i] = hextoul(string, &string); |
| 221 | |
| 222 | if (*string == ':') |
| 223 | string++; |
| 224 | } |
| 225 | |
| 226 | update_crc(); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * init_local_copy() - initialize the in-memory EEPROM copy |
| 231 | * |
| 232 | * Initialize the in-memory EEPROM copy with the magic number. Must |
| 233 | * be done when preparing to initialize a blank EEPROM, or overwrite |
| 234 | * one with a corrupted magic number. |
| 235 | */ |
| 236 | static void init_local_copy(void) |
| 237 | { |
| 238 | memset((void *)pbuf.buf, 0, sizeof(struct starfive_eeprom)); |
| 239 | memcpy(pbuf.eeprom.header.signature, STARFIVE_EEPROM_HATS_SIG, |
| 240 | strlen(STARFIVE_EEPROM_HATS_SIG)); |
| 241 | pbuf.eeprom.header.version = FORMAT_VERSION; |
| 242 | pbuf.eeprom.header.numatoms = 2; |
| 243 | pbuf.eeprom.header.eeplen = sizeof(struct starfive_eeprom); |
| 244 | |
| 245 | pbuf.eeprom.atom1.header.type = HATS_ATOM_VENDOR; |
| 246 | pbuf.eeprom.atom1.header.count = 1; |
| 247 | pbuf.eeprom.atom1.header.dlen = sizeof(struct eeprom_atom1_data) + sizeof(u16); |
| 248 | pbuf.eeprom.atom1.data.vslen = STARFIVE_EEPROM_ATOM1_VSTR_SIZE; |
| 249 | pbuf.eeprom.atom1.data.pslen = STARFIVE_EEPROM_ATOM1_PSTR_SIZE; |
| 250 | memcpy(pbuf.eeprom.atom1.data.vstr, STARFIVE_EEPROM_ATOM1_VSTR, |
| 251 | strlen(STARFIVE_EEPROM_ATOM1_VSTR)); |
| 252 | memcpy(pbuf.eeprom.atom1.data.pstr, STARFIVE_EEPROM_ATOM1_PSTR, |
| 253 | strlen(STARFIVE_EEPROM_ATOM1_PSTR)); |
| 254 | |
| 255 | pbuf.eeprom.atom4.header.type = HATS_ATOM_CUSTOM; |
| 256 | pbuf.eeprom.atom4.header.count = 2; |
| 257 | pbuf.eeprom.atom4.header.dlen = sizeof(struct eeprom_atom4_data) + sizeof(u16); |
| 258 | pbuf.eeprom.atom4.data.version = FORMAT_VERSION; |
| 259 | pbuf.eeprom.atom4.data.pcb_revision = PCB_VERSION; |
| 260 | pbuf.eeprom.atom4.data.bom_revision = BOM_VERSION; |
| 261 | set_mac_address(STARFIVE_DEFAULT_MAC0, 0); |
| 262 | set_mac_address(STARFIVE_DEFAULT_MAC1, 1); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * prog_eeprom() - write the EEPROM from memory |
| 267 | */ |
| 268 | static int prog_eeprom(unsigned int size) |
| 269 | { |
| 270 | unsigned int i; |
| 271 | void *p; |
| 272 | uchar tmp_buff[STARFIVE_EEPROM_HATS_SIZE_MAX]; |
| 273 | struct udevice *dev; |
| 274 | int ret; |
| 275 | |
| 276 | if (is_match_magic()) { |
| 277 | printf("MAGIC ERROR, Please check the data@%p.\n", pbuf.buf); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, |
| 282 | CONFIG_SYS_I2C_EEPROM_ADDR, |
| 283 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 284 | &dev); |
| 285 | if (ret) { |
| 286 | printf("Get i2c bus:%d addr:%d fail.\n", CONFIG_SYS_EEPROM_BUS_NUM, |
| 287 | CONFIG_SYS_I2C_EEPROM_ADDR); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | for (i = 0, p = (u8 *)pbuf.buf; i < size; ) { |
| 292 | if (!ret) |
| 293 | ret = dm_i2c_write(dev, i, p, min((int)(size - i), |
| 294 | BYTES_PER_EEPROM_PAGE)); |
| 295 | if (ret) |
| 296 | break; |
| 297 | |
| 298 | udelay(EEPROM_WRITE_DELAY_MS); |
| 299 | i += BYTES_PER_EEPROM_PAGE; |
| 300 | p += BYTES_PER_EEPROM_PAGE; |
| 301 | } |
| 302 | |
| 303 | if (!ret) { |
| 304 | /* Verify the write by reading back the EEPROM and comparing */ |
| 305 | ret = dm_i2c_read(dev, |
| 306 | STARFIVE_EEPROM_WP_OFFSET, |
| 307 | tmp_buff, |
| 308 | STARFIVE_EEPROM_HATS_SIZE_MAX); |
| 309 | if (!ret && memcmp((void *)pbuf.buf, (void *)tmp_buff, |
| 310 | STARFIVE_EEPROM_HATS_SIZE_MAX)) |
| 311 | ret = -1; |
| 312 | } |
| 313 | |
| 314 | if (ret) { |
| 315 | has_been_read = -1; |
| 316 | printf("Programming failed.\n"); |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | printf("Programming passed.\n"); |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * read_eeprom() - read the EEPROM into memory, if it hasn't been read already |
| 326 | */ |
| 327 | static int read_eeprom(void) |
| 328 | { |
| 329 | int ret; |
| 330 | struct udevice *dev; |
| 331 | |
| 332 | if (has_been_read == 1) |
| 333 | return 0; |
| 334 | |
| 335 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, |
| 336 | CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev); |
| 337 | if (!ret) |
| 338 | ret = dm_i2c_read(dev, 0, (u8 *)pbuf.buf, |
| 339 | STARFIVE_EEPROM_HATS_SIZE_MAX); |
| 340 | |
| 341 | has_been_read = (ret == 0) ? 1 : 0; |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * set_pcb_revision() - stores a StarFive PCB revision into the local EEPROM copy |
| 348 | * |
| 349 | * Takes a pointer to a string representing the numeric PCB revision in |
| 350 | * decimal ("0" - "255"), stores it in the pcb_revision field of the |
| 351 | * EEPROM local copy, and updates the CRC of the local copy. |
| 352 | */ |
| 353 | static void set_pcb_revision(char *string) |
| 354 | { |
| 355 | u32 p; |
| 356 | |
| 357 | p = simple_strtoul(string, &string, 16); |
| 358 | if (p > U8_MAX) { |
| 359 | printf("%s must not be greater than %d\n", "PCB revision", |
| 360 | U8_MAX); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | pbuf.eeprom.atom4.data.pcb_revision = p; |
| 365 | |
| 366 | update_crc(); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * set_bom_revision() - stores a StarFive BOM revision into the local EEPROM copy |
| 371 | * |
| 372 | * Takes a pointer to a uppercase ASCII character representing the BOM |
| 373 | * revision ("A" - "Z"), stores it in the bom_revision field of the |
| 374 | * EEPROM local copy, and updates the CRC of the local copy. |
| 375 | */ |
| 376 | static void set_bom_revision(char *string) |
| 377 | { |
| 378 | if (string[0] < 'A' || string[0] > 'Z') { |
| 379 | printf("BOM revision must be an uppercase letter between A and Z\n"); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | pbuf.eeprom.atom4.data.bom_revision = string[0]; |
| 384 | |
| 385 | update_crc(); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * set_product_id() - stores a StarFive product ID into the local EEPROM copy |
| 390 | * |
| 391 | * Takes a pointer to a string representing the numeric product ID in |
| 392 | * string ("VF7100A1-2150-D008E000-00000001\0"), stores it in the product string |
| 393 | * field of the EEPROM local copy, and updates the CRC of the local copy. |
| 394 | */ |
| 395 | static void set_product_id(char *string) |
| 396 | { |
| 397 | u32 len; |
| 398 | |
| 399 | len = (strlen(string) > STARFIVE_EEPROM_ATOM1_PSTR_SIZE) ? |
| 400 | STARFIVE_EEPROM_ATOM1_PSTR_SIZE : strlen(string); |
| 401 | |
| 402 | memcpy((void *)pbuf.eeprom.atom1.data.pstr, (void *)string, len); |
| 403 | |
| 404 | update_crc(); |
| 405 | } |
| 406 | |
| 407 | static int print_usage(void) |
| 408 | { |
| 409 | printf("display and program the system ID and MAC addresses in EEPROM\n" |
| 410 | "[read_eeprom|initialize|write_eeprom|mac_address|pcb_revision|bom_revision|product_id]\n" |
| 411 | "mac read_eeprom\n" |
| 412 | " - read EEPROM content into memory data structure\n" |
| 413 | "mac write_eeprom\n" |
| 414 | " - save memory data structure to the EEPROM\n" |
| 415 | "mac initialize\n" |
| 416 | " - initialize the in-memory EEPROM copy with default data\n" |
| 417 | "mac mac0_address <xx:xx:xx:xx:xx:xx>\n" |
| 418 | " - stores a MAC0 address into the local EEPROM copy\n" |
| 419 | "mac mac1_address <xx:xx:xx:xx:xx:xx>\n" |
| 420 | " - stores a MAC1 address into the local EEPROM copy\n" |
| 421 | "mac pcb_revision <?>\n" |
| 422 | " - stores a StarFive PCB revision into the local EEPROM copy\n" |
| 423 | "mac bom_revision <A>\n" |
| 424 | " - stores a StarFive BOM revision into the local EEPROM copy\n" |
| 425 | "mac product_id <VF7110A1-2228-D008E000-xxxxxxxx>\n" |
| 426 | " - stores a StarFive product ID into the local EEPROM copy\n"); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 431 | { |
| 432 | char *cmd; |
| 433 | |
| 434 | if (argc == 1) { |
| 435 | show_eeprom(); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | if (argc > 3) |
| 440 | return print_usage(); |
| 441 | |
| 442 | cmd = argv[1]; |
| 443 | |
| 444 | /* Commands with no argument */ |
| 445 | if (!strcmp(cmd, "read_eeprom")) { |
| 446 | has_been_read = 0; |
| 447 | return read_eeprom(); |
| 448 | } else if (!strcmp(cmd, "initialize")) { |
| 449 | init_local_copy(); |
| 450 | return 0; |
| 451 | } else if (!strcmp(cmd, "write_eeprom")) { |
| 452 | return prog_eeprom(STARFIVE_EEPROM_HATS_SIZE_MAX); |
| 453 | } |
| 454 | |
| 455 | if (argc != 3) |
| 456 | return print_usage(); |
| 457 | |
| 458 | if (is_match_magic()) { |
| 459 | printf("Please read the EEPROM ('read_eeprom') and/or initialize the EEPROM ('initialize') first.\n"); |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | if (!strcmp(cmd, "mac0_address")) { |
| 464 | set_mac_address(argv[2], 0); |
| 465 | return 0; |
| 466 | } else if (!strcmp(cmd, "mac1_address")) { |
| 467 | set_mac_address(argv[2], 1); |
| 468 | return 0; |
| 469 | } else if (!strcmp(cmd, "pcb_revision")) { |
| 470 | set_pcb_revision(argv[2]); |
| 471 | return 0; |
| 472 | } else if (!strcmp(cmd, "bom_revision")) { |
| 473 | set_bom_revision(argv[2]); |
| 474 | return 0; |
| 475 | } else if (!strcmp(cmd, "product_id")) { |
| 476 | set_product_id(argv[2]); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | return print_usage(); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * mac_read_from_eeprom() - read the MAC address & the serial number in EEPROM |
| 485 | * |
| 486 | * This function reads the MAC address and the serial number from EEPROM and |
| 487 | * sets the appropriate environment variables for each one read. |
| 488 | * |
| 489 | * The environment variables are only set if they haven't been set already. |
| 490 | * This ensures that any user-saved variables are never overwritten. |
| 491 | * |
| 492 | * If CONFIG_ID_EEPROM is enabled, this function will be called in |
| 493 | * "static init_fnc_t init_sequence_r[]" of u-boot/common/board_r.c. |
| 494 | */ |
| 495 | int mac_read_from_eeprom(void) |
| 496 | { |
| 497 | /** |
| 498 | * try to fill the buff from EEPROM, |
| 499 | * always return SUCCESS, even some error happens. |
| 500 | */ |
| 501 | if (read_eeprom()) { |
| 502 | dump_raw_eeprom(); |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | // 1, setup ethaddr env |
| 507 | eth_env_set_enetaddr("eth0addr", pbuf.eeprom.atom4.data.mac0_addr); |
| 508 | eth_env_set_enetaddr("eth1addr", pbuf.eeprom.atom4.data.mac1_addr); |
| 509 | |
| 510 | /** |
| 511 | * 2, setup serial# env, reference to hifive-platform-i2c-eeprom.c, |
| 512 | * serial# can be a ASCII string, but not just a hex number, so we |
| 513 | * setup serial# in the 32Byte format: |
| 514 | * "VF7100A1-2201-D008E000-00000001;" |
| 515 | * "<product>-<date>-<DDR&eMMC>-<serial_number>" |
| 516 | * <date>: 4Byte, should be the output of `date +%y%W` |
| 517 | * <DDR&eMMC>: 8Byte, "D008" means 8GB, "D01T" means 1TB; |
| 518 | * "E000" means no eMMC,"E032" means 32GB, "E01T" means 1TB. |
| 519 | * <serial_number>: 8Byte, the Unique Identifier of board in hex. |
| 520 | */ |
| 521 | if (!env_get("serial#")) |
| 522 | env_set("serial#", pbuf.eeprom.atom1.data.pstr); |
| 523 | |
| 524 | printf("StarFive EEPROM format v%u\n", pbuf.eeprom.header.version); |
| 525 | show_eeprom(); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * get_pcb_revision_from_eeprom - get the PCB revision |
| 531 | * |
| 532 | * 1.2A return 'A'/'a', 1.3B return 'B'/'b',other values are illegal |
| 533 | */ |
| 534 | u8 get_pcb_revision_from_eeprom(void) |
| 535 | { |
| 536 | u8 pv = 0xFF; |
| 537 | |
| 538 | if (read_eeprom()) |
| 539 | return pv; |
| 540 | |
| 541 | return pbuf.eeprom.atom1.data.pstr[6]; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * get_ddr_size_from_eeprom - get the DDR size |
| 546 | * pstr: VF7110A1-2228-D008E000-00000001 |
| 547 | * VF7110A1/VF7110B1 : VisionFive JH7110A /VisionFive JH7110B |
| 548 | * D008: 8GB LPDDR4 |
| 549 | * E000: No emmc device, ECxx: include emmc device, xx: Capacity size[GB] |
| 550 | * return: the field of 'D008E000' |
| 551 | */ |
| 552 | |
| 553 | u32 get_ddr_size_from_eeprom(void) |
| 554 | { |
| 555 | u32 pv = 0xFFFFFFFF; |
| 556 | |
| 557 | if (read_eeprom()) |
| 558 | return pv; |
| 559 | |
| 560 | return hextoul(&pbuf.eeprom.atom1.data.pstr[14], NULL); |
| 561 | } |