2 * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
4 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
5 * Igor Grinberg <grinberg@compulab.co.il>
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <eeprom_layout.h>
13 #include <eeprom_field.h>
14 #include <linux/kernel.h>
17 #ifndef CONFIG_SYS_I2C_EEPROM_ADDR
18 # define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
19 # define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
22 #ifndef CONFIG_SYS_I2C_EEPROM_BUS
23 #define CONFIG_SYS_I2C_EEPROM_BUS 0
26 #define EEPROM_LAYOUT_VER_OFFSET 44
27 #define BOARD_SERIAL_OFFSET 20
28 #define BOARD_SERIAL_OFFSET_LEGACY 8
29 #define BOARD_REV_OFFSET 0
30 #define BOARD_REV_OFFSET_LEGACY 6
31 #define BOARD_REV_SIZE 2
32 #define PRODUCT_NAME_OFFSET 128
33 #define PRODUCT_NAME_SIZE 16
34 #define MAC_ADDR_OFFSET 4
35 #define MAC_ADDR_OFFSET_LEGACY 0
37 #define LAYOUT_INVALID 0
38 #define LAYOUT_LEGACY 0xff
40 static int cl_eeprom_bus;
41 static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
43 static int cl_eeprom_read(uint offset, uchar *buf, int len)
46 unsigned int current_i2c_bus = i2c_get_bus_num();
48 res = i2c_set_bus_num(cl_eeprom_bus);
52 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
53 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
55 i2c_set_bus_num(current_i2c_bus);
60 static int cl_eeprom_setup(uint eeprom_bus)
65 * We know the setup was already done when the layout is set to a valid
66 * value and we're using the same bus as before.
68 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
71 cl_eeprom_bus = eeprom_bus;
72 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
73 (uchar *)&cl_eeprom_layout, 1);
75 cl_eeprom_layout = LAYOUT_INVALID;
79 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
80 cl_eeprom_layout = LAYOUT_LEGACY;
85 void get_board_serial(struct tag_serialnr *serialnr)
90 memset(serialnr, 0, sizeof(*serialnr));
92 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
95 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
96 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
98 if (cl_eeprom_read(offset, (uchar *)serial, 8))
101 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
102 serialnr->low = serial[0];
103 serialnr->high = serial[1];
108 * Routine: cl_eeprom_read_mac_addr
109 * Description: read mac address and store it in buf.
111 int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
116 err = cl_eeprom_setup(eeprom_bus);
120 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
121 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
123 return cl_eeprom_read(offset, buf, 6);
126 static u32 board_rev;
129 * Routine: cl_eeprom_get_board_rev
130 * Description: read system revision from eeprom
132 u32 cl_eeprom_get_board_rev(uint eeprom_bus)
134 char str[5]; /* Legacy representation can contain at most 4 digits */
135 uint offset = BOARD_REV_OFFSET_LEGACY;
140 if (cl_eeprom_setup(eeprom_bus))
143 if (cl_eeprom_layout != LAYOUT_LEGACY)
144 offset = BOARD_REV_OFFSET;
146 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
150 * Convert legacy syntactic representation to semantic
151 * representation. i.e. for rev 1.00: 0x100 --> 0x64
153 if (cl_eeprom_layout == LAYOUT_LEGACY) {
154 sprintf(str, "%x", board_rev);
155 board_rev = simple_strtoul(str, NULL, 10);
162 * Routine: cl_eeprom_get_board_rev
163 * Description: read system revision from eeprom
165 * @buf: buffer to store the product name
166 * @eeprom_bus: i2c bus num of the eeprom
168 * @return: 0 on success, < 0 on failure
170 int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
177 err = cl_eeprom_setup(eeprom_bus);
181 err = cl_eeprom_read(PRODUCT_NAME_OFFSET, buf, PRODUCT_NAME_SIZE);
182 if (!err) /* Protect ourselves from invalid data (unterminated str) */
183 buf[PRODUCT_NAME_SIZE - 1] = '\0';
188 #ifdef CONFIG_CMD_EEPROM_LAYOUT
190 * eeprom_field_print_bin_ver() - print a "version field" which contains binary
193 * Treat the field data as simple binary data, and print it formatted as a
194 * version number (2 digits after decimal point).
195 * The field size must be exactly 2 bytes.
200 * @field: an initialized field to print
202 void eeprom_field_print_bin_ver(const struct eeprom_field *field)
204 if ((field->buf[0] == 0xff) && (field->buf[1] == 0xff)) {
209 printf(PRINT_FIELD_SEGMENT, field->name);
210 int major = (field->buf[1] << 8 | field->buf[0]) / 100;
211 int minor = (field->buf[1] << 8 | field->buf[0]) - major * 100;
212 printf("%d.%02d\n", major, minor);
216 * eeprom_field_update_bin_ver() - update a "version field" which contains
219 * This function takes a version string in the form of x.y (x and y are both
220 * decimal values, y is limited to two digits), translates it to the binary
221 * form, then writes it to the field. The field size must be exactly 2 bytes.
223 * This function strictly enforces the data syntax, and will not update the
224 * field if there's any deviation from it. It also protects from overflow.
226 * @field: an initialized field
227 * @value: a version string
229 * Returns 0 on success, -1 on failure.
231 int eeprom_field_update_bin_ver(struct eeprom_field *field, char *value)
234 char *tok = strtok(value, ".");
238 int num = simple_strtol(tok, &endptr, 0);
242 tok = strtok(NULL, "");
246 int remainder = simple_strtol(tok, &endptr, 0);
250 num = num * 100 + remainder;
254 field->buf[0] = (unsigned char)num;
255 field->buf[1] = num >> 8;
260 char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
261 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
264 * eeprom_field_print_date() - print a field which contains date data
266 * Treat the field data as simple binary data, and print it formatted as a date.
268 * Field Name 07/Feb/2014
269 * Field Name 56/BAD/9999
271 * @field: an initialized field to print
273 void eeprom_field_print_date(const struct eeprom_field *field)
275 printf(PRINT_FIELD_SEGMENT, field->name);
276 printf("%02d/", field->buf[0]);
277 if (field->buf[1] >= 1 && field->buf[1] <= 12)
278 printf("%s", months[field->buf[1] - 1]);
282 printf("/%d\n", field->buf[3] << 8 | field->buf[2]);
285 static int validate_date(unsigned char day, unsigned char month,
288 int days_in_february;
309 days_in_february = 28;
312 days_in_february = 29;
313 else if (year % 400 == 0)
314 days_in_february = 29;
317 if (day > days_in_february)
329 * eeprom_field_update_date() - update a date field which contains binary data
331 * This function takes a date string in the form of x/Mon/y (x and y are both
332 * decimal values), translates it to the binary representation, then writes it
335 * This function strictly enforces the data syntax, and will not update the
336 * field if there's any deviation from it. It also protects from overflow in the
337 * year value, and checks the validity of the date.
339 * @field: an initialized field
340 * @value: a date string
342 * Returns 0 on success, -1 on failure.
344 int eeprom_field_update_date(struct eeprom_field *field, char *value)
347 char *tok1 = strtok(value, "/");
348 char *tok2 = strtok(NULL, "/");
349 char *tok3 = strtok(NULL, "/");
351 if (tok1 == NULL || tok2 == NULL || tok3 == NULL) {
352 printf("%s: syntax error\n", field->name);
356 unsigned char day = (unsigned char)simple_strtol(tok1, &endptr, 0);
357 if (*endptr != '\0' || day == 0) {
358 printf("%s: invalid day\n", field->name);
363 for (month = 1; month <= 12; month++)
364 if (!strcmp(tok2, months[month - 1]))
367 unsigned int year = simple_strtol(tok3, &endptr, 0);
368 if (*endptr != '\0') {
369 printf("%s: invalid year\n", field->name);
373 if (validate_date(day, month - 1, year)) {
374 printf("%s: invalid date\n", field->name);
379 printf("%s: year overflow\n", field->name);
384 field->buf[1] = month;
385 field->buf[2] = (unsigned char)year;
386 field->buf[3] = (unsigned char)(year >> 8);
391 #define LAYOUT_VERSION_LEGACY 1
392 #define LAYOUT_VERSION_VER1 2
393 #define LAYOUT_VERSION_VER2 3
394 #define LAYOUT_VERSION_VER3 4
396 extern struct eeprom_field layout_unknown[1];
398 #define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x
401 struct eeprom_field layout_legacy[5] = {
402 { "MAC address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
403 { "Board Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin) },
404 { "Serial Number", 8, NULL, DEFINE_PRINT_UPDATE(bin) },
405 { "Board Configuration", 64, NULL, DEFINE_PRINT_UPDATE(ascii) },
406 { RESERVED_FIELDS, 176, NULL, eeprom_field_print_reserved,
407 eeprom_field_update_ascii },
410 #define layout_legacy layout_unknown
413 #if defined(CONFIG_CM_T3X) || defined(CONFIG_CM_T3517)
414 struct eeprom_field layout_v1[12] = {
415 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
416 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
417 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
418 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
419 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
420 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
421 { RESERVED_FIELDS, 96, NULL, DEFINE_PRINT_UPDATE(reserved) },
422 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
423 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
424 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
425 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
426 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
427 eeprom_field_update_ascii },
430 #define layout_v1 layout_unknown
433 struct eeprom_field layout_v2[15] = {
434 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
435 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
436 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
437 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
438 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
439 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
440 { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
441 { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
442 { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
443 { RESERVED_FIELDS, 83, NULL, DEFINE_PRINT_UPDATE(reserved) },
444 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
445 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
446 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
447 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
448 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
449 eeprom_field_update_ascii },
452 struct eeprom_field layout_v3[16] = {
453 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
454 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
455 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
456 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
457 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
458 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
459 { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
460 { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
461 { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
462 { "CompuLab EEPROM ID", 3, NULL, DEFINE_PRINT_UPDATE(bin) },
463 { RESERVED_FIELDS, 80, NULL, DEFINE_PRINT_UPDATE(reserved) },
464 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
465 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
466 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
467 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
468 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
469 eeprom_field_update_ascii },
472 void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version)
474 switch (layout->layout_version) {
475 case LAYOUT_VERSION_LEGACY:
476 layout->fields = layout_legacy;
477 layout->num_of_fields = ARRAY_SIZE(layout_legacy);
479 case LAYOUT_VERSION_VER1:
480 layout->fields = layout_v1;
481 layout->num_of_fields = ARRAY_SIZE(layout_v1);
483 case LAYOUT_VERSION_VER2:
484 layout->fields = layout_v2;
485 layout->num_of_fields = ARRAY_SIZE(layout_v2);
487 case LAYOUT_VERSION_VER3:
488 layout->fields = layout_v3;
489 layout->num_of_fields = ARRAY_SIZE(layout_v3);
492 __eeprom_layout_assign(layout, layout_version);
496 int eeprom_parse_layout_version(char *str)
498 if (!strcmp(str, "legacy"))
499 return LAYOUT_VERSION_LEGACY;
500 else if (!strcmp(str, "v1"))
501 return LAYOUT_VERSION_VER1;
502 else if (!strcmp(str, "v2"))
503 return LAYOUT_VERSION_VER2;
504 else if (!strcmp(str, "v3"))
505 return LAYOUT_VERSION_VER3;
507 return LAYOUT_VERSION_UNRECOGNIZED;
510 int eeprom_layout_detect(unsigned char *data)
512 switch (data[EEPROM_LAYOUT_VER_OFFSET]) {
515 return LAYOUT_VERSION_VER1;
517 return LAYOUT_VERSION_VER2;
519 return LAYOUT_VERSION_VER3;
522 if (data[EEPROM_LAYOUT_VER_OFFSET] >= 0x20)
523 return LAYOUT_VERSION_LEGACY;
525 return LAYOUT_VERSION_UNRECOGNIZED;