1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
4 *
5 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
6 * Igor Grinberg <grinberg@compulab.co.il>
7 */
8
9 #include <common.h>
10 #include <i2c.h>
11 #include <eeprom_layout.h>
12 #include <eeprom_field.h>
13 #include <asm/setup.h>
14 #include <linux/kernel.h>
15 #include "eeprom.h"
16
17 #ifndef CONFIG_SYS_I2C_EEPROM_ADDR
18 # define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
19 # define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
20 #endif
21
22 #ifndef CONFIG_SYS_I2C_EEPROM_BUS
23 #define CONFIG_SYS_I2C_EEPROM_BUS 0
24 #endif
25
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
36
37 #define LAYOUT_INVALID 0
38 #define LAYOUT_LEGACY 0xff
39
40 static int cl_eeprom_bus;
41 static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
42
cl_eeprom_read(uint offset,uchar * buf,int len)43 static int cl_eeprom_read(uint offset, uchar *buf, int len)
44 {
45 int res;
46 unsigned int current_i2c_bus = i2c_get_bus_num();
47
48 res = i2c_set_bus_num(cl_eeprom_bus);
49 if (res < 0)
50 return res;
51
52 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
53 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
54
55 i2c_set_bus_num(current_i2c_bus);
56
57 return res;
58 }
59
cl_eeprom_setup(uint eeprom_bus)60 static int cl_eeprom_setup(uint eeprom_bus)
61 {
62 int res;
63
64 /*
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.
67 */
68 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
69 return 0;
70
71 cl_eeprom_bus = eeprom_bus;
72 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
73 (uchar *)&cl_eeprom_layout, 1);
74 if (res) {
75 cl_eeprom_layout = LAYOUT_INVALID;
76 return res;
77 }
78
79 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
80 cl_eeprom_layout = LAYOUT_LEGACY;
81
82 return 0;
83 }
84
get_board_serial(struct tag_serialnr * serialnr)85 void get_board_serial(struct tag_serialnr *serialnr)
86 {
87 u32 serial[2];
88 uint offset;
89
90 memset(serialnr, 0, sizeof(*serialnr));
91
92 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
93 return;
94
95 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
96 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
97
98 if (cl_eeprom_read(offset, (uchar *)serial, 8))
99 return;
100
101 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
102 serialnr->low = serial[0];
103 serialnr->high = serial[1];
104 }
105 }
106
107 /*
108 * Routine: cl_eeprom_read_mac_addr
109 * Description: read mac address and store it in buf.
110 */
cl_eeprom_read_mac_addr(uchar * buf,uint eeprom_bus)111 int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
112 {
113 uint offset;
114 int err;
115
116 err = cl_eeprom_setup(eeprom_bus);
117 if (err)
118 return err;
119
120 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
121 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
122
123 return cl_eeprom_read(offset, buf, 6);
124 }
125
126 static u32 board_rev;
127
128 /*
129 * Routine: cl_eeprom_get_board_rev
130 * Description: read system revision from eeprom
131 */
cl_eeprom_get_board_rev(uint eeprom_bus)132 u32 cl_eeprom_get_board_rev(uint eeprom_bus)
133 {
134 char str[5]; /* Legacy representation can contain at most 4 digits */
135 uint offset = BOARD_REV_OFFSET_LEGACY;
136
137 if (board_rev)
138 return board_rev;
139
140 if (cl_eeprom_setup(eeprom_bus))
141 return 0;
142
143 if (cl_eeprom_layout != LAYOUT_LEGACY)
144 offset = BOARD_REV_OFFSET;
145
146 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
147 return 0;
148
149 /*
150 * Convert legacy syntactic representation to semantic
151 * representation. i.e. for rev 1.00: 0x100 --> 0x64
152 */
153 if (cl_eeprom_layout == LAYOUT_LEGACY) {
154 sprintf(str, "%x", board_rev);
155 board_rev = simple_strtoul(str, NULL, 10);
156 }
157
158 return board_rev;
159 };
160
161 /*
162 * Routine: cl_eeprom_get_board_rev
163 * Description: read system revision from eeprom
164 *
165 * @buf: buffer to store the product name
166 * @eeprom_bus: i2c bus num of the eeprom
167 *
168 * @return: 0 on success, < 0 on failure
169 */
cl_eeprom_get_product_name(uchar * buf,uint eeprom_bus)170 int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
171 {
172 int err;
173
174 if (buf == NULL)
175 return -EINVAL;
176
177 err = cl_eeprom_setup(eeprom_bus);
178 if (err)
179 return err;
180
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';
184
185 return err;
186 }
187
188 #ifdef CONFIG_CMD_EEPROM_LAYOUT
189 /**
190 * eeprom_field_print_bin_ver() - print a "version field" which contains binary
191 * data
192 *
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.
196 *
197 * Sample output:
198 * Field Name 123.45
199 *
200 * @field: an initialized field to print
201 */
eeprom_field_print_bin_ver(const struct eeprom_field * field)202 void eeprom_field_print_bin_ver(const struct eeprom_field *field)
203 {
204 if ((field->buf[0] == 0xff) && (field->buf[1] == 0xff)) {
205 field->buf[0] = 0;
206 field->buf[1] = 0;
207 }
208
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);
213 }
214
215 /**
216 * eeprom_field_update_bin_ver() - update a "version field" which contains
217 * binary data
218 *
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.
222 *
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.
225 *
226 * @field: an initialized field
227 * @value: a version string
228 *
229 * Returns 0 on success, -1 on failure.
230 */
eeprom_field_update_bin_ver(struct eeprom_field * field,char * value)231 int eeprom_field_update_bin_ver(struct eeprom_field *field, char *value)
232 {
233 char *endptr;
234 char *tok = strtok(value, ".");
235 if (tok == NULL)
236 return -1;
237
238 int num = simple_strtol(tok, &endptr, 0);
239 if (*endptr != '\0')
240 return -1;
241
242 tok = strtok(NULL, "");
243 if (tok == NULL)
244 return -1;
245
246 int remainder = simple_strtol(tok, &endptr, 0);
247 if (*endptr != '\0')
248 return -1;
249
250 num = num * 100 + remainder;
251 if (num >> 16)
252 return -1;
253
254 field->buf[0] = (unsigned char)num;
255 field->buf[1] = num >> 8;
256
257 return 0;
258 }
259
260 char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
261 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
262
263 /**
264 * eeprom_field_print_date() - print a field which contains date data
265 *
266 * Treat the field data as simple binary data, and print it formatted as a date.
267 * Sample output:
268 * Field Name 07/Feb/2014
269 * Field Name 56/BAD/9999
270 *
271 * @field: an initialized field to print
272 */
eeprom_field_print_date(const struct eeprom_field * field)273 void eeprom_field_print_date(const struct eeprom_field *field)
274 {
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]);
279 else
280 printf("BAD");
281
282 printf("/%d\n", field->buf[3] << 8 | field->buf[2]);
283 }
284
validate_date(unsigned char day,unsigned char month,unsigned int year)285 static int validate_date(unsigned char day, unsigned char month,
286 unsigned int year)
287 {
288 int days_in_february;
289
290 switch (month) {
291 case 0:
292 case 2:
293 case 4:
294 case 6:
295 case 7:
296 case 9:
297 case 11:
298 if (day > 31)
299 return -1;
300 break;
301 case 3:
302 case 5:
303 case 8:
304 case 10:
305 if (day > 30)
306 return -1;
307 break;
308 case 1:
309 days_in_february = 28;
310 if (year % 4 == 0) {
311 if (year % 100 != 0)
312 days_in_february = 29;
313 else if (year % 400 == 0)
314 days_in_february = 29;
315 }
316
317 if (day > days_in_february)
318 return -1;
319
320 break;
321 default:
322 return -1;
323 }
324
325 return 0;
326 }
327
328 /**
329 * eeprom_field_update_date() - update a date field which contains binary data
330 *
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
333 * to the field.
334 *
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.
338 *
339 * @field: an initialized field
340 * @value: a date string
341 *
342 * Returns 0 on success, -1 on failure.
343 */
eeprom_field_update_date(struct eeprom_field * field,char * value)344 int eeprom_field_update_date(struct eeprom_field *field, char *value)
345 {
346 char *endptr;
347 char *tok1 = strtok(value, "/");
348 char *tok2 = strtok(NULL, "/");
349 char *tok3 = strtok(NULL, "/");
350
351 if (tok1 == NULL || tok2 == NULL || tok3 == NULL) {
352 printf("%s: syntax error\n", field->name);
353 return -1;
354 }
355
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);
359 return -1;
360 }
361
362 unsigned char month;
363 for (month = 1; month <= 12; month++)
364 if (!strcmp(tok2, months[month - 1]))
365 break;
366
367 unsigned int year = simple_strtol(tok3, &endptr, 0);
368 if (*endptr != '\0') {
369 printf("%s: invalid year\n", field->name);
370 return -1;
371 }
372
373 if (validate_date(day, month - 1, year)) {
374 printf("%s: invalid date\n", field->name);
375 return -1;
376 }
377
378 if (year >> 16) {
379 printf("%s: year overflow\n", field->name);
380 return -1;
381 }
382
383 field->buf[0] = day;
384 field->buf[1] = month;
385 field->buf[2] = (unsigned char)year;
386 field->buf[3] = (unsigned char)(year >> 8);
387
388 return 0;
389 }
390
391 #define LAYOUT_VERSION_LEGACY 1
392 #define LAYOUT_VERSION_VER1 2
393 #define LAYOUT_VERSION_VER2 3
394 #define LAYOUT_VERSION_VER3 4
395
396 extern struct eeprom_field layout_unknown[1];
397
398 #define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x
399
400 #ifdef CONFIG_CM_T3X
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 },
408 };
409 #else
410 #define layout_legacy layout_unknown
411 #endif
412
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 },
428 };
429 #else
430 #define layout_v1 layout_unknown
431 #endif
432
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 },
450 };
451
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 },
470 };
471
eeprom_layout_assign(struct eeprom_layout * layout,int layout_version)472 void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version)
473 {
474 switch (layout->layout_version) {
475 case LAYOUT_VERSION_LEGACY:
476 layout->fields = layout_legacy;
477 layout->num_of_fields = ARRAY_SIZE(layout_legacy);
478 break;
479 case LAYOUT_VERSION_VER1:
480 layout->fields = layout_v1;
481 layout->num_of_fields = ARRAY_SIZE(layout_v1);
482 break;
483 case LAYOUT_VERSION_VER2:
484 layout->fields = layout_v2;
485 layout->num_of_fields = ARRAY_SIZE(layout_v2);
486 break;
487 case LAYOUT_VERSION_VER3:
488 layout->fields = layout_v3;
489 layout->num_of_fields = ARRAY_SIZE(layout_v3);
490 break;
491 default:
492 __eeprom_layout_assign(layout, layout_version);
493 }
494 }
495
eeprom_parse_layout_version(char * str)496 int eeprom_parse_layout_version(char *str)
497 {
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;
506 else
507 return LAYOUT_VERSION_UNRECOGNIZED;
508 }
509
eeprom_layout_detect(unsigned char * data)510 int eeprom_layout_detect(unsigned char *data)
511 {
512 switch (data[EEPROM_LAYOUT_VER_OFFSET]) {
513 case 0xff:
514 case 0:
515 return LAYOUT_VERSION_VER1;
516 case 2:
517 return LAYOUT_VERSION_VER2;
518 case 3:
519 return LAYOUT_VERSION_VER3;
520 }
521
522 if (data[EEPROM_LAYOUT_VER_OFFSET] >= 0x20)
523 return LAYOUT_VERSION_LEGACY;
524
525 return LAYOUT_VERSION_UNRECOGNIZED;
526 }
527 #endif
528