3 * Corscience GmbH & Co. KG, <www.corscience.de>
6 * SPDX-License-Identifier: GPL-2.0+
11 #include "tricorder-eeprom.h"
13 static inline void warn_wrong_value(const char *msg, unsigned int a,
16 printf("Expected EEPROM %s %08x, got %08x\n", msg, a, b);
19 static int handle_eeprom_v0(struct tricorder_eeprom *eeprom)
21 struct tricorder_eeprom_v0 {
25 char board_name[TRICORDER_BOARD_NAME_LENGTH];
26 char board_version[TRICORDER_BOARD_VERSION_LENGTH];
27 char board_serial[TRICORDER_BOARD_SERIAL_LENGTH];
32 printf("Old EEPROM (v0), consider rewrite!\n");
34 if (be16_to_cpu(eeprom->length) != sizeof(eepromv0)) {
35 warn_wrong_value("length", sizeof(eepromv0),
36 be16_to_cpu(eeprom->length));
40 memcpy(&eepromv0, eeprom, sizeof(eepromv0));
42 crc = crc32(0L, (unsigned char *)&eepromv0,
43 sizeof(eepromv0) - sizeof(eepromv0.crc32));
44 if (be32_to_cpu(eepromv0.crc32) != crc) {
45 warn_wrong_value("CRC", be32_to_cpu(eepromv0.crc32),
50 /* Ok the content is correct, do the conversion */
51 memset(eeprom->interface_version, 0x0,
52 TRICORDER_INTERFACE_VERSION_LENGTH);
53 crc = crc32(0L, (unsigned char *)eeprom, TRICORDER_EEPROM_CRC_SIZE);
54 eeprom->crc32 = cpu_to_be32(crc);
59 static int handle_eeprom_v1(struct tricorder_eeprom *eeprom)
63 if (be16_to_cpu(eeprom->length) != TRICORDER_EEPROM_SIZE) {
64 warn_wrong_value("length", TRICORDER_EEPROM_SIZE,
65 be16_to_cpu(eeprom->length));
69 crc = crc32(0L, (unsigned char *)eeprom, TRICORDER_EEPROM_CRC_SIZE);
70 if (be32_to_cpu(eeprom->crc32) != crc) {
71 warn_wrong_value("CRC", be32_to_cpu(eeprom->crc32), crc);
78 int tricorder_get_eeprom(int addr, struct tricorder_eeprom *eeprom)
80 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
81 unsigned int bus = i2c_get_bus_num();
82 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
85 memset(eeprom, 0, TRICORDER_EEPROM_SIZE);
87 i2c_read(addr, 0, 2, (unsigned char *)eeprom, TRICORDER_EEPROM_SIZE);
88 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
92 if (be32_to_cpu(eeprom->magic) != TRICORDER_EEPROM_MAGIC) {
93 warn_wrong_value("magic", TRICORDER_EEPROM_MAGIC,
94 be32_to_cpu(eeprom->magic));
98 switch (be16_to_cpu(eeprom->version)) {
100 return handle_eeprom_v0(eeprom);
102 return handle_eeprom_v1(eeprom);
104 warn_wrong_value("version", TRICORDER_EEPROM_VERSION,
105 be16_to_cpu(eeprom->version));
110 #if !defined(CONFIG_SPL)
111 int tricorder_eeprom_read(unsigned devaddr)
113 struct tricorder_eeprom eeprom;
114 int ret = tricorder_get_eeprom(devaddr, &eeprom);
119 printf("Board type: %.*s\n",
120 sizeof(eeprom.board_name), eeprom.board_name);
121 printf("Board version: %.*s\n",
122 sizeof(eeprom.board_version), eeprom.board_version);
123 printf("Board serial: %.*s\n",
124 sizeof(eeprom.board_serial), eeprom.board_serial);
125 printf("Board interface version: %.*s\n",
126 sizeof(eeprom.interface_version),
127 eeprom.interface_version);
132 int tricorder_eeprom_write(unsigned devaddr, const char *name,
133 const char *version, const char *serial, const char *interface)
135 struct tricorder_eeprom eeprom, eeprom_verify;
141 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
145 memset(eeprom, 0, TRICORDER_EEPROM_SIZE);
146 memset(eeprom_verify, 0, TRICORDER_EEPROM_SIZE);
148 eeprom.magic = cpu_to_be32(TRICORDER_EEPROM_MAGIC);
149 eeprom.length = cpu_to_be16(TRICORDER_EEPROM_SIZE);
150 eeprom.version = cpu_to_be16(TRICORDER_EEPROM_VERSION);
152 length = min(sizeof(eeprom.board_name), strlen(name));
153 strncpy(eeprom.board_name, name, length);
155 length = min(sizeof(eeprom.board_version), strlen(version));
156 strncpy(eeprom.board_version, version, length);
158 length = min(sizeof(eeprom.board_serial), strlen(serial));
159 strncpy(eeprom.board_serial, serial, length);
162 length = min(sizeof(eeprom.interface_version),
164 strncpy(eeprom.interface_version, interface, length);
167 crc = crc32(0L, (unsigned char *)&eeprom, TRICORDER_EEPROM_CRC_SIZE);
168 eeprom.crc32 = cpu_to_be32(crc);
171 puts("Tricorder EEPROM content:\n");
172 print_buffer(0, &eeprom, 1, sizeof(eeprom), 16);
175 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
176 bus = i2c_get_bus_num();
177 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
180 /* do page write to the eeprom */
181 for (i = 0, p = (unsigned char *)&eeprom;
184 ret = i2c_write(devaddr, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
185 p, min(sizeof(eeprom) - i, 32));
188 udelay(5000); /* 5ms write cycle timing */
191 ret = i2c_read(devaddr, 0, 2, (unsigned char *)&eeprom_verify,
192 TRICORDER_EEPROM_SIZE);
194 if (memcmp(&eeprom, &eeprom_verify, sizeof(eeprom)) != 0) {
195 printf("Tricorder: Could not verify EEPROM content!\n");
199 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
200 i2c_set_bus_num(bus);
205 int do_tricorder_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
208 ulong dev_addr = simple_strtoul(argv[2], NULL, 16);
210 if (strcmp(argv[1], "read") == 0) {
213 rcode = tricorder_eeprom_read(dev_addr);
217 } else if (argc == 6 || argc == 7) {
218 ulong dev_addr = simple_strtoul(argv[2], NULL, 16);
219 char *name = argv[3];
220 char *version = argv[4];
221 char *serial = argv[5];
222 char *interface = NULL;
228 if (strcmp(argv[1], "write") == 0) {
231 rcode = tricorder_eeprom_write(dev_addr, name, version,
238 return CMD_RET_USAGE;
242 tricordereeprom, 7, 1, do_tricorder_eeprom,
245 " - read Tricorder EEPROM at devaddr and print content\n"
246 "tricordereeprom write devaddr name version serial [interface]\n"
247 " - write Tricorder EEPROM at devaddr with 'name', 'version'"
249 " optional add an HW interface parameter"
251 #endif /* CONFIG_SPL */