1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
5 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
6 * Igor Grinberg <grinberg@compulab.co.il>
12 #include <eeprom_layout.h>
13 #include <eeprom_field.h>
14 #include <asm/setup.h>
15 #include <linux/kernel.h>
18 #define EEPROM_LAYOUT_VER_OFFSET 44
19 #define BOARD_SERIAL_OFFSET 20
20 #define BOARD_SERIAL_OFFSET_LEGACY 8
21 #define BOARD_REV_OFFSET 0
22 #define BOARD_REV_OFFSET_LEGACY 6
23 #define BOARD_REV_SIZE 2
24 #define PRODUCT_NAME_OFFSET 128
25 #define PRODUCT_NAME_SIZE 16
26 #define MAC_ADDR_OFFSET 4
27 #define MAC_ADDR_OFFSET_LEGACY 0
29 #define LAYOUT_INVALID 0
30 #define LAYOUT_LEGACY 0xff
32 static int cl_eeprom_bus;
33 static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
35 static int cl_eeprom_read(uint offset, uchar *buf, int len)
38 unsigned int current_i2c_bus = i2c_get_bus_num();
40 res = i2c_set_bus_num(cl_eeprom_bus);
44 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
45 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
47 i2c_set_bus_num(current_i2c_bus);
52 static int cl_eeprom_setup(uint eeprom_bus)
57 * We know the setup was already done when the layout is set to a valid
58 * value and we're using the same bus as before.
60 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
63 cl_eeprom_bus = eeprom_bus;
64 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
65 (uchar *)&cl_eeprom_layout, 1);
67 cl_eeprom_layout = LAYOUT_INVALID;
71 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
72 cl_eeprom_layout = LAYOUT_LEGACY;
77 void get_board_serial(struct tag_serialnr *serialnr)
82 memset(serialnr, 0, sizeof(*serialnr));
84 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
87 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
88 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
90 if (cl_eeprom_read(offset, (uchar *)serial, 8))
93 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
94 serialnr->low = serial[0];
95 serialnr->high = serial[1];
100 * Routine: cl_eeprom_read_mac_addr
101 * Description: read mac address and store it in buf.
103 int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
108 err = cl_eeprom_setup(eeprom_bus);
112 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
113 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
115 return cl_eeprom_read(offset, buf, 6);
118 static u32 board_rev;
121 * Routine: cl_eeprom_get_board_rev
122 * Description: read system revision from eeprom
124 u32 cl_eeprom_get_board_rev(uint eeprom_bus)
126 char str[5]; /* Legacy representation can contain at most 4 digits */
127 uint offset = BOARD_REV_OFFSET_LEGACY;
132 if (cl_eeprom_setup(eeprom_bus))
135 if (cl_eeprom_layout != LAYOUT_LEGACY)
136 offset = BOARD_REV_OFFSET;
138 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
142 * Convert legacy syntactic representation to semantic
143 * representation. i.e. for rev 1.00: 0x100 --> 0x64
145 if (cl_eeprom_layout == LAYOUT_LEGACY) {
146 sprintf(str, "%x", board_rev);
147 board_rev = dectoul(str, NULL);
154 * Routine: cl_eeprom_get_board_rev
155 * Description: read system revision from eeprom
157 * @buf: buffer to store the product name
158 * @eeprom_bus: i2c bus num of the eeprom
160 * @return: 0 on success, < 0 on failure
162 int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
169 err = cl_eeprom_setup(eeprom_bus);
173 err = cl_eeprom_read(PRODUCT_NAME_OFFSET, buf, PRODUCT_NAME_SIZE);
174 if (!err) /* Protect ourselves from invalid data (unterminated str) */
175 buf[PRODUCT_NAME_SIZE - 1] = '\0';
180 #ifdef CONFIG_CMD_EEPROM_LAYOUT
182 * eeprom_field_print_bin_ver() - print a "version field" which contains binary
185 * Treat the field data as simple binary data, and print it formatted as a
186 * version number (2 digits after decimal point).
187 * The field size must be exactly 2 bytes.
192 * @field: an initialized field to print
194 void eeprom_field_print_bin_ver(const struct eeprom_field *field)
196 if ((field->buf[0] == 0xff) && (field->buf[1] == 0xff)) {
201 printf(PRINT_FIELD_SEGMENT, field->name);
202 int major = (field->buf[1] << 8 | field->buf[0]) / 100;
203 int minor = (field->buf[1] << 8 | field->buf[0]) - major * 100;
204 printf("%d.%02d\n", major, minor);
208 * eeprom_field_update_bin_ver() - update a "version field" which contains
211 * This function takes a version string in the form of x.y (x and y are both
212 * decimal values, y is limited to two digits), translates it to the binary
213 * form, then writes it to the field. The field size must be exactly 2 bytes.
215 * This function strictly enforces the data syntax, and will not update the
216 * field if there's any deviation from it. It also protects from overflow.
218 * @field: an initialized field
219 * @value: a version string
221 * Returns 0 on success, -1 on failure.
223 int eeprom_field_update_bin_ver(struct eeprom_field *field, char *value)
226 char *tok = strtok(value, ".");
230 int num = simple_strtol(tok, &endptr, 0);
234 tok = strtok(NULL, "");
238 int remainder = simple_strtol(tok, &endptr, 0);
242 num = num * 100 + remainder;
246 field->buf[0] = (unsigned char)num;
247 field->buf[1] = num >> 8;
252 char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
253 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
256 * eeprom_field_print_date() - print a field which contains date data
258 * Treat the field data as simple binary data, and print it formatted as a date.
260 * Field Name 07/Feb/2014
261 * Field Name 56/BAD/9999
263 * @field: an initialized field to print
265 void eeprom_field_print_date(const struct eeprom_field *field)
267 printf(PRINT_FIELD_SEGMENT, field->name);
268 printf("%02d/", field->buf[0]);
269 if (field->buf[1] >= 1 && field->buf[1] <= 12)
270 printf("%s", months[field->buf[1] - 1]);
274 printf("/%d\n", field->buf[3] << 8 | field->buf[2]);
277 static int validate_date(unsigned char day, unsigned char month,
280 int days_in_february;
301 days_in_february = 28;
304 days_in_february = 29;
305 else if (year % 400 == 0)
306 days_in_february = 29;
309 if (day > days_in_february)
321 * eeprom_field_update_date() - update a date field which contains binary data
323 * This function takes a date string in the form of x/Mon/y (x and y are both
324 * decimal values), translates it to the binary representation, then writes it
327 * This function strictly enforces the data syntax, and will not update the
328 * field if there's any deviation from it. It also protects from overflow in the
329 * year value, and checks the validity of the date.
331 * @field: an initialized field
332 * @value: a date string
334 * Returns 0 on success, -1 on failure.
336 int eeprom_field_update_date(struct eeprom_field *field, char *value)
339 char *tok1 = strtok(value, "/");
340 char *tok2 = strtok(NULL, "/");
341 char *tok3 = strtok(NULL, "/");
343 if (tok1 == NULL || tok2 == NULL || tok3 == NULL) {
344 printf("%s: syntax error\n", field->name);
348 unsigned char day = (unsigned char)simple_strtol(tok1, &endptr, 0);
349 if (*endptr != '\0' || day == 0) {
350 printf("%s: invalid day\n", field->name);
355 for (month = 1; month <= 12; month++)
356 if (!strcmp(tok2, months[month - 1]))
359 unsigned int year = simple_strtol(tok3, &endptr, 0);
360 if (*endptr != '\0') {
361 printf("%s: invalid year\n", field->name);
365 if (validate_date(day, month - 1, year)) {
366 printf("%s: invalid date\n", field->name);
371 printf("%s: year overflow\n", field->name);
376 field->buf[1] = month;
377 field->buf[2] = (unsigned char)year;
378 field->buf[3] = (unsigned char)(year >> 8);
383 #define LAYOUT_VERSION_LEGACY 1
384 #define LAYOUT_VERSION_VER1 2
385 #define LAYOUT_VERSION_VER2 3
386 #define LAYOUT_VERSION_VER3 4
388 #define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x
390 struct eeprom_field layout_v2[15] = {
391 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
392 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
393 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
394 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
395 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
396 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
397 { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
398 { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
399 { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
400 { RESERVED_FIELDS, 83, NULL, DEFINE_PRINT_UPDATE(reserved) },
401 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
402 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
403 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
404 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
405 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
406 eeprom_field_update_ascii },
409 struct eeprom_field layout_v3[16] = {
410 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
411 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
412 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
413 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
414 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
415 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
416 { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
417 { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
418 { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
419 { "CompuLab EEPROM ID", 3, NULL, DEFINE_PRINT_UPDATE(bin) },
420 { RESERVED_FIELDS, 80, NULL, DEFINE_PRINT_UPDATE(reserved) },
421 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
422 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
423 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
424 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
425 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
426 eeprom_field_update_ascii },
429 void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version)
431 switch (layout->layout_version) {
432 case LAYOUT_VERSION_VER2:
433 layout->fields = layout_v2;
434 layout->num_of_fields = ARRAY_SIZE(layout_v2);
436 case LAYOUT_VERSION_VER3:
437 layout->fields = layout_v3;
438 layout->num_of_fields = ARRAY_SIZE(layout_v3);
441 __eeprom_layout_assign(layout, layout_version);
445 int eeprom_parse_layout_version(char *str)
447 if (!strcmp(str, "legacy"))
448 return LAYOUT_VERSION_LEGACY;
449 else if (!strcmp(str, "v1"))
450 return LAYOUT_VERSION_VER1;
451 else if (!strcmp(str, "v2"))
452 return LAYOUT_VERSION_VER2;
453 else if (!strcmp(str, "v3"))
454 return LAYOUT_VERSION_VER3;
456 return LAYOUT_VERSION_UNRECOGNIZED;
459 int eeprom_layout_detect(unsigned char *data)
461 switch (data[EEPROM_LAYOUT_VER_OFFSET]) {
464 return LAYOUT_VERSION_VER1;
466 return LAYOUT_VERSION_VER2;
468 return LAYOUT_VERSION_VER3;
471 if (data[EEPROM_LAYOUT_VER_OFFSET] >= 0x20)
472 return LAYOUT_VERSION_LEGACY;
474 return LAYOUT_VERSION_UNRECOGNIZED;