blob: 1b12d096041ecd20ec623948abcf1c715dbd4da3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +00002/*
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>
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +00007 */
8
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +00009#include <i2c.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <vsprintf.h>
Nikita Kiryanov2f67a2e2016-04-16 17:55:04 +030011#include <eeprom_layout.h>
12#include <eeprom_field.h>
Simon Glassd9a766f2017-05-17 08:23:00 -060013#include <asm/setup.h>
Nikita Kiryanov2f67a2e2016-04-16 17:55:04 +030014#include <linux/kernel.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060015#include <linux/string.h>
Nikita Kiryanov61f82b72015-09-06 11:48:37 +030016#include "eeprom.h"
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000017
18#define EEPROM_LAYOUT_VER_OFFSET 44
19#define BOARD_SERIAL_OFFSET 20
20#define BOARD_SERIAL_OFFSET_LEGACY 8
Nikita Kiryanov2eff8502012-01-02 04:01:34 +000021#define BOARD_REV_OFFSET 0
22#define BOARD_REV_OFFSET_LEGACY 6
Nikita Kiryanov01b1e282012-05-24 04:01:22 +000023#define BOARD_REV_SIZE 2
Nikita Kiryanov61f82b72015-09-06 11:48:37 +030024#define PRODUCT_NAME_OFFSET 128
25#define PRODUCT_NAME_SIZE 16
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +000026#define MAC_ADDR_OFFSET 4
27#define MAC_ADDR_OFFSET_LEGACY 0
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000028
29#define LAYOUT_INVALID 0
30#define LAYOUT_LEGACY 0xff
31
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020032static int cl_eeprom_bus;
Igor Grinberg3394be82013-09-16 21:49:58 +030033static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000034
Igor Grinberg3394be82013-09-16 21:49:58 +030035static int cl_eeprom_read(uint offset, uchar *buf, int len)
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000036{
Anatolij Gustschin0aeb7482024-08-07 15:09:33 +020037 struct udevice *eeprom;
Nikita Kiryanova8eeecb2014-08-20 15:08:52 +030038 int res;
Nikita Kiryanova8eeecb2014-08-20 15:08:52 +030039
Anatolij Gustschin0aeb7482024-08-07 15:09:33 +020040 res = i2c_get_chip_for_busnum(cl_eeprom_bus, CONFIG_SYS_I2C_EEPROM_ADDR,
41 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &eeprom);
42 if (res)
Nikita Kiryanova8eeecb2014-08-20 15:08:52 +030043 return res;
44
Anatolij Gustschin0aeb7482024-08-07 15:09:33 +020045 return dm_i2c_read(eeprom, offset, (uint8_t *)buf, len);
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000046}
47
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020048static int cl_eeprom_setup(uint eeprom_bus)
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000049{
50 int res;
51
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020052 /*
53 * We know the setup was already done when the layout is set to a valid
54 * value and we're using the same bus as before.
55 */
56 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000057 return 0;
58
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020059 cl_eeprom_bus = eeprom_bus;
Igor Grinberg3394be82013-09-16 21:49:58 +030060 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
61 (uchar *)&cl_eeprom_layout, 1);
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000062 if (res) {
Igor Grinberg3394be82013-09-16 21:49:58 +030063 cl_eeprom_layout = LAYOUT_INVALID;
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000064 return res;
65 }
66
Igor Grinberg3394be82013-09-16 21:49:58 +030067 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
68 cl_eeprom_layout = LAYOUT_LEGACY;
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000069
70 return 0;
71}
72
73void get_board_serial(struct tag_serialnr *serialnr)
74{
75 u32 serial[2];
76 uint offset;
77
78 memset(serialnr, 0, sizeof(*serialnr));
Igor Grinberg3394be82013-09-16 21:49:58 +030079
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020080 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000081 return;
82
Igor Grinberg3394be82013-09-16 21:49:58 +030083 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
84 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
85
86 if (cl_eeprom_read(offset, (uchar *)serial, 8))
Nikita Kiryanovb47cb9d2012-01-12 03:26:30 +000087 return;
88
89 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
90 serialnr->low = serial[0];
91 serialnr->high = serial[1];
92 }
93}
Nikita Kiryanov2eff8502012-01-02 04:01:34 +000094
95/*
Igor Grinberg3394be82013-09-16 21:49:58 +030096 * Routine: cl_eeprom_read_mac_addr
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +000097 * Description: read mac address and store it in buf.
98 */
Nikita Kiryanovb2c55472015-01-14 10:42:43 +020099int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000100{
101 uint offset;
Nikita Kiryanov35e8d6c2015-09-06 11:48:36 +0300102 int err;
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000103
Nikita Kiryanov35e8d6c2015-09-06 11:48:36 +0300104 err = cl_eeprom_setup(eeprom_bus);
105 if (err)
106 return err;
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000107
Igor Grinberg3394be82013-09-16 21:49:58 +0300108 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000109 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
Igor Grinberg3394be82013-09-16 21:49:58 +0300110
111 return cl_eeprom_read(offset, buf, 6);
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000112}
113
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200114static u32 board_rev;
115
Nikita Kiryanovf1ef8692012-01-12 03:28:09 +0000116/*
Igor Grinberg3394be82013-09-16 21:49:58 +0300117 * Routine: cl_eeprom_get_board_rev
Nikita Kiryanovcc935c62012-05-24 04:01:24 +0000118 * Description: read system revision from eeprom
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000119 */
Nikita Kiryanov7fa68352015-09-06 11:48:35 +0300120u32 cl_eeprom_get_board_rev(uint eeprom_bus)
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000121{
Nikita Kiryanov15f26a72012-05-24 04:01:23 +0000122 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000123 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000124
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200125 if (board_rev)
126 return board_rev;
127
Nikita Kiryanov7fa68352015-09-06 11:48:35 +0300128 if (cl_eeprom_setup(eeprom_bus))
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000129 return 0;
130
Igor Grinberg3394be82013-09-16 21:49:58 +0300131 if (cl_eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000132 offset = BOARD_REV_OFFSET;
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000133
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200134 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000135 return 0;
136
Nikita Kiryanov15f26a72012-05-24 04:01:23 +0000137 /*
138 * Convert legacy syntactic representation to semantic
139 * representation. i.e. for rev 1.00: 0x100 --> 0x64
140 */
Igor Grinberg3394be82013-09-16 21:49:58 +0300141 if (cl_eeprom_layout == LAYOUT_LEGACY) {
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200142 sprintf(str, "%x", board_rev);
Simon Glassff9b9032021-07-24 09:03:30 -0600143 board_rev = dectoul(str, NULL);
Nikita Kiryanov15f26a72012-05-24 04:01:23 +0000144 }
145
Igor Grinberg3c5dc282014-11-03 11:32:18 +0200146 return board_rev;
Nikita Kiryanov2eff8502012-01-02 04:01:34 +0000147};
Nikita Kiryanov61f82b72015-09-06 11:48:37 +0300148
149/*
150 * Routine: cl_eeprom_get_board_rev
151 * Description: read system revision from eeprom
152 *
153 * @buf: buffer to store the product name
154 * @eeprom_bus: i2c bus num of the eeprom
155 *
156 * @return: 0 on success, < 0 on failure
157 */
158int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
159{
160 int err;
161
162 if (buf == NULL)
163 return -EINVAL;
164
165 err = cl_eeprom_setup(eeprom_bus);
166 if (err)
167 return err;
168
169 err = cl_eeprom_read(PRODUCT_NAME_OFFSET, buf, PRODUCT_NAME_SIZE);
170 if (!err) /* Protect ourselves from invalid data (unterminated str) */
171 buf[PRODUCT_NAME_SIZE - 1] = '\0';
172
173 return err;
174}
Nikita Kiryanov2f67a2e2016-04-16 17:55:04 +0300175
176#ifdef CONFIG_CMD_EEPROM_LAYOUT
177/**
178 * eeprom_field_print_bin_ver() - print a "version field" which contains binary
179 * data
180 *
181 * Treat the field data as simple binary data, and print it formatted as a
182 * version number (2 digits after decimal point).
183 * The field size must be exactly 2 bytes.
184 *
185 * Sample output:
186 * Field Name 123.45
187 *
188 * @field: an initialized field to print
189 */
190void eeprom_field_print_bin_ver(const struct eeprom_field *field)
191{
192 if ((field->buf[0] == 0xff) && (field->buf[1] == 0xff)) {
193 field->buf[0] = 0;
194 field->buf[1] = 0;
195 }
196
197 printf(PRINT_FIELD_SEGMENT, field->name);
198 int major = (field->buf[1] << 8 | field->buf[0]) / 100;
199 int minor = (field->buf[1] << 8 | field->buf[0]) - major * 100;
200 printf("%d.%02d\n", major, minor);
201}
202
203/**
204 * eeprom_field_update_bin_ver() - update a "version field" which contains
205 * binary data
206 *
207 * This function takes a version string in the form of x.y (x and y are both
208 * decimal values, y is limited to two digits), translates it to the binary
209 * form, then writes it to the field. The field size must be exactly 2 bytes.
210 *
211 * This function strictly enforces the data syntax, and will not update the
212 * field if there's any deviation from it. It also protects from overflow.
213 *
214 * @field: an initialized field
215 * @value: a version string
216 *
217 * Returns 0 on success, -1 on failure.
218 */
219int eeprom_field_update_bin_ver(struct eeprom_field *field, char *value)
220{
221 char *endptr;
222 char *tok = strtok(value, ".");
223 if (tok == NULL)
224 return -1;
225
226 int num = simple_strtol(tok, &endptr, 0);
227 if (*endptr != '\0')
228 return -1;
229
230 tok = strtok(NULL, "");
231 if (tok == NULL)
232 return -1;
233
234 int remainder = simple_strtol(tok, &endptr, 0);
235 if (*endptr != '\0')
236 return -1;
237
238 num = num * 100 + remainder;
239 if (num >> 16)
240 return -1;
241
242 field->buf[0] = (unsigned char)num;
243 field->buf[1] = num >> 8;
244
245 return 0;
246}
247
248char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
249 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
250
251/**
252 * eeprom_field_print_date() - print a field which contains date data
253 *
254 * Treat the field data as simple binary data, and print it formatted as a date.
255 * Sample output:
256 * Field Name 07/Feb/2014
257 * Field Name 56/BAD/9999
258 *
259 * @field: an initialized field to print
260 */
261void eeprom_field_print_date(const struct eeprom_field *field)
262{
263 printf(PRINT_FIELD_SEGMENT, field->name);
264 printf("%02d/", field->buf[0]);
265 if (field->buf[1] >= 1 && field->buf[1] <= 12)
266 printf("%s", months[field->buf[1] - 1]);
267 else
268 printf("BAD");
269
270 printf("/%d\n", field->buf[3] << 8 | field->buf[2]);
271}
272
273static int validate_date(unsigned char day, unsigned char month,
274 unsigned int year)
275{
276 int days_in_february;
277
278 switch (month) {
279 case 0:
280 case 2:
281 case 4:
282 case 6:
283 case 7:
284 case 9:
285 case 11:
286 if (day > 31)
287 return -1;
288 break;
289 case 3:
290 case 5:
291 case 8:
292 case 10:
293 if (day > 30)
294 return -1;
295 break;
296 case 1:
297 days_in_february = 28;
298 if (year % 4 == 0) {
299 if (year % 100 != 0)
300 days_in_february = 29;
301 else if (year % 400 == 0)
302 days_in_february = 29;
303 }
304
305 if (day > days_in_february)
306 return -1;
307
308 break;
309 default:
310 return -1;
311 }
312
313 return 0;
314}
315
316/**
317 * eeprom_field_update_date() - update a date field which contains binary data
318 *
319 * This function takes a date string in the form of x/Mon/y (x and y are both
320 * decimal values), translates it to the binary representation, then writes it
321 * to the field.
322 *
323 * This function strictly enforces the data syntax, and will not update the
324 * field if there's any deviation from it. It also protects from overflow in the
325 * year value, and checks the validity of the date.
326 *
327 * @field: an initialized field
328 * @value: a date string
329 *
330 * Returns 0 on success, -1 on failure.
331 */
332int eeprom_field_update_date(struct eeprom_field *field, char *value)
333{
334 char *endptr;
335 char *tok1 = strtok(value, "/");
336 char *tok2 = strtok(NULL, "/");
337 char *tok3 = strtok(NULL, "/");
338
339 if (tok1 == NULL || tok2 == NULL || tok3 == NULL) {
340 printf("%s: syntax error\n", field->name);
341 return -1;
342 }
343
344 unsigned char day = (unsigned char)simple_strtol(tok1, &endptr, 0);
345 if (*endptr != '\0' || day == 0) {
346 printf("%s: invalid day\n", field->name);
347 return -1;
348 }
349
350 unsigned char month;
351 for (month = 1; month <= 12; month++)
352 if (!strcmp(tok2, months[month - 1]))
353 break;
354
355 unsigned int year = simple_strtol(tok3, &endptr, 0);
356 if (*endptr != '\0') {
357 printf("%s: invalid year\n", field->name);
358 return -1;
359 }
360
361 if (validate_date(day, month - 1, year)) {
362 printf("%s: invalid date\n", field->name);
363 return -1;
364 }
365
366 if (year >> 16) {
367 printf("%s: year overflow\n", field->name);
368 return -1;
369 }
370
371 field->buf[0] = day;
372 field->buf[1] = month;
373 field->buf[2] = (unsigned char)year;
374 field->buf[3] = (unsigned char)(year >> 8);
375
376 return 0;
377}
378
379#define LAYOUT_VERSION_LEGACY 1
380#define LAYOUT_VERSION_VER1 2
381#define LAYOUT_VERSION_VER2 3
382#define LAYOUT_VERSION_VER3 4
383
Nikita Kiryanov2f67a2e2016-04-16 17:55:04 +0300384#define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x
385
Nikita Kiryanov2f67a2e2016-04-16 17:55:04 +0300386struct eeprom_field layout_v2[15] = {
387 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
388 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
389 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
390 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
391 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
392 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
393 { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
394 { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
395 { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
396 { RESERVED_FIELDS, 83, NULL, DEFINE_PRINT_UPDATE(reserved) },
397 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
398 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
399 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
400 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
401 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
402 eeprom_field_update_ascii },
403};
404
405struct eeprom_field layout_v3[16] = {
406 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
407 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
408 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
409 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
410 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
411 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
412 { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
413 { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
414 { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
415 { "CompuLab EEPROM ID", 3, NULL, DEFINE_PRINT_UPDATE(bin) },
416 { RESERVED_FIELDS, 80, NULL, DEFINE_PRINT_UPDATE(reserved) },
417 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
418 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
419 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
420 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
421 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
422 eeprom_field_update_ascii },
423};
424
425void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version)
426{
427 switch (layout->layout_version) {
Nikita Kiryanov2f67a2e2016-04-16 17:55:04 +0300428 case LAYOUT_VERSION_VER2:
429 layout->fields = layout_v2;
430 layout->num_of_fields = ARRAY_SIZE(layout_v2);
431 break;
432 case LAYOUT_VERSION_VER3:
433 layout->fields = layout_v3;
434 layout->num_of_fields = ARRAY_SIZE(layout_v3);
435 break;
436 default:
437 __eeprom_layout_assign(layout, layout_version);
438 }
439}
440
441int eeprom_parse_layout_version(char *str)
442{
443 if (!strcmp(str, "legacy"))
444 return LAYOUT_VERSION_LEGACY;
445 else if (!strcmp(str, "v1"))
446 return LAYOUT_VERSION_VER1;
447 else if (!strcmp(str, "v2"))
448 return LAYOUT_VERSION_VER2;
449 else if (!strcmp(str, "v3"))
450 return LAYOUT_VERSION_VER3;
451 else
452 return LAYOUT_VERSION_UNRECOGNIZED;
453}
454
455int eeprom_layout_detect(unsigned char *data)
456{
457 switch (data[EEPROM_LAYOUT_VER_OFFSET]) {
458 case 0xff:
459 case 0:
460 return LAYOUT_VERSION_VER1;
461 case 2:
462 return LAYOUT_VERSION_VER2;
463 case 3:
464 return LAYOUT_VERSION_VER3;
465 }
466
467 if (data[EEPROM_LAYOUT_VER_OFFSET] >= 0x20)
468 return LAYOUT_VERSION_LEGACY;
469
470 return LAYOUT_VERSION_UNRECOGNIZED;
471}
472#endif