blob: c4b257f851d664ae451c9fad5f6c5a8bf9f5d19b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nikita Kiryanov82309252012-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 Kiryanov82309252012-01-12 03:26:30 +00007 */
8
9#include <common.h>
Simon Glasscb3ef682019-11-14 12:57:50 -070010#include <eeprom.h>
Nikita Kiryanov82309252012-01-12 03:26:30 +000011#include <i2c.h>
Nikita Kiryanov8af57342016-04-16 17:55:04 +030012#include <eeprom_layout.h>
13#include <eeprom_field.h>
Simon Glass5d982852017-05-17 08:23:00 -060014#include <asm/setup.h>
Nikita Kiryanov8af57342016-04-16 17:55:04 +030015#include <linux/kernel.h>
Nikita Kiryanov53af8772015-09-06 11:48:37 +030016#include "eeprom.h"
Nikita Kiryanov82309252012-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 Kiryanov7d3c97d2012-01-02 04:01:34 +000021#define BOARD_REV_OFFSET 0
22#define BOARD_REV_OFFSET_LEGACY 6
Nikita Kiryanov6f3b3002012-05-24 04:01:22 +000023#define BOARD_REV_SIZE 2
Nikita Kiryanov53af8772015-09-06 11:48:37 +030024#define PRODUCT_NAME_OFFSET 128
25#define PRODUCT_NAME_SIZE 16
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000026#define MAC_ADDR_OFFSET 4
27#define MAC_ADDR_OFFSET_LEGACY 0
Nikita Kiryanov82309252012-01-12 03:26:30 +000028
29#define LAYOUT_INVALID 0
30#define LAYOUT_LEGACY 0xff
31
Nikita Kiryanove7a24472015-01-14 10:42:43 +020032static int cl_eeprom_bus;
Igor Grinberg689be5f2013-09-16 21:49:58 +030033static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
Nikita Kiryanov82309252012-01-12 03:26:30 +000034
Igor Grinberg689be5f2013-09-16 21:49:58 +030035static int cl_eeprom_read(uint offset, uchar *buf, int len)
Nikita Kiryanov82309252012-01-12 03:26:30 +000036{
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030037 int res;
38 unsigned int current_i2c_bus = i2c_get_bus_num();
39
Nikita Kiryanove7a24472015-01-14 10:42:43 +020040 res = i2c_set_bus_num(cl_eeprom_bus);
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030041 if (res < 0)
42 return res;
43
44 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
Nikita Kiryanov82309252012-01-12 03:26:30 +000045 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030046
47 i2c_set_bus_num(current_i2c_bus);
48
49 return res;
Nikita Kiryanov82309252012-01-12 03:26:30 +000050}
51
Nikita Kiryanove7a24472015-01-14 10:42:43 +020052static int cl_eeprom_setup(uint eeprom_bus)
Nikita Kiryanov82309252012-01-12 03:26:30 +000053{
54 int res;
55
Nikita Kiryanove7a24472015-01-14 10:42:43 +020056 /*
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.
59 */
60 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
Nikita Kiryanov82309252012-01-12 03:26:30 +000061 return 0;
62
Nikita Kiryanove7a24472015-01-14 10:42:43 +020063 cl_eeprom_bus = eeprom_bus;
Igor Grinberg689be5f2013-09-16 21:49:58 +030064 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
65 (uchar *)&cl_eeprom_layout, 1);
Nikita Kiryanov82309252012-01-12 03:26:30 +000066 if (res) {
Igor Grinberg689be5f2013-09-16 21:49:58 +030067 cl_eeprom_layout = LAYOUT_INVALID;
Nikita Kiryanov82309252012-01-12 03:26:30 +000068 return res;
69 }
70
Igor Grinberg689be5f2013-09-16 21:49:58 +030071 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
72 cl_eeprom_layout = LAYOUT_LEGACY;
Nikita Kiryanov82309252012-01-12 03:26:30 +000073
74 return 0;
75}
76
77void get_board_serial(struct tag_serialnr *serialnr)
78{
79 u32 serial[2];
80 uint offset;
81
82 memset(serialnr, 0, sizeof(*serialnr));
Igor Grinberg689be5f2013-09-16 21:49:58 +030083
Nikita Kiryanove7a24472015-01-14 10:42:43 +020084 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
Nikita Kiryanov82309252012-01-12 03:26:30 +000085 return;
86
Igor Grinberg689be5f2013-09-16 21:49:58 +030087 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
88 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
89
90 if (cl_eeprom_read(offset, (uchar *)serial, 8))
Nikita Kiryanov82309252012-01-12 03:26:30 +000091 return;
92
93 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
94 serialnr->low = serial[0];
95 serialnr->high = serial[1];
96 }
97}
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000098
99/*
Igor Grinberg689be5f2013-09-16 21:49:58 +0300100 * Routine: cl_eeprom_read_mac_addr
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000101 * Description: read mac address and store it in buf.
102 */
Nikita Kiryanove7a24472015-01-14 10:42:43 +0200103int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000104{
105 uint offset;
Nikita Kiryanove93e8092015-09-06 11:48:36 +0300106 int err;
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000107
Nikita Kiryanove93e8092015-09-06 11:48:36 +0300108 err = cl_eeprom_setup(eeprom_bus);
109 if (err)
110 return err;
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000111
Igor Grinberg689be5f2013-09-16 21:49:58 +0300112 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000113 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
Igor Grinberg689be5f2013-09-16 21:49:58 +0300114
115 return cl_eeprom_read(offset, buf, 6);
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000116}
117
Igor Grinberga937fd12014-11-03 11:32:18 +0200118static u32 board_rev;
119
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000120/*
Igor Grinberg689be5f2013-09-16 21:49:58 +0300121 * Routine: cl_eeprom_get_board_rev
Nikita Kiryanov8c318eb2012-05-24 04:01:24 +0000122 * Description: read system revision from eeprom
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000123 */
Nikita Kiryanov72898ac2015-09-06 11:48:35 +0300124u32 cl_eeprom_get_board_rev(uint eeprom_bus)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000125{
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000126 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000127 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000128
Igor Grinberga937fd12014-11-03 11:32:18 +0200129 if (board_rev)
130 return board_rev;
131
Nikita Kiryanov72898ac2015-09-06 11:48:35 +0300132 if (cl_eeprom_setup(eeprom_bus))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000133 return 0;
134
Igor Grinberg689be5f2013-09-16 21:49:58 +0300135 if (cl_eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000136 offset = BOARD_REV_OFFSET;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000137
Igor Grinberga937fd12014-11-03 11:32:18 +0200138 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000139 return 0;
140
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000141 /*
142 * Convert legacy syntactic representation to semantic
143 * representation. i.e. for rev 1.00: 0x100 --> 0x64
144 */
Igor Grinberg689be5f2013-09-16 21:49:58 +0300145 if (cl_eeprom_layout == LAYOUT_LEGACY) {
Igor Grinberga937fd12014-11-03 11:32:18 +0200146 sprintf(str, "%x", board_rev);
Simon Glass0b1284e2021-07-24 09:03:30 -0600147 board_rev = dectoul(str, NULL);
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000148 }
149
Igor Grinberga937fd12014-11-03 11:32:18 +0200150 return board_rev;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000151};
Nikita Kiryanov53af8772015-09-06 11:48:37 +0300152
153/*
154 * Routine: cl_eeprom_get_board_rev
155 * Description: read system revision from eeprom
156 *
157 * @buf: buffer to store the product name
158 * @eeprom_bus: i2c bus num of the eeprom
159 *
160 * @return: 0 on success, < 0 on failure
161 */
162int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
163{
164 int err;
165
166 if (buf == NULL)
167 return -EINVAL;
168
169 err = cl_eeprom_setup(eeprom_bus);
170 if (err)
171 return err;
172
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';
176
177 return err;
178}
Nikita Kiryanov8af57342016-04-16 17:55:04 +0300179
180#ifdef CONFIG_CMD_EEPROM_LAYOUT
181/**
182 * eeprom_field_print_bin_ver() - print a "version field" which contains binary
183 * data
184 *
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.
188 *
189 * Sample output:
190 * Field Name 123.45
191 *
192 * @field: an initialized field to print
193 */
194void eeprom_field_print_bin_ver(const struct eeprom_field *field)
195{
196 if ((field->buf[0] == 0xff) && (field->buf[1] == 0xff)) {
197 field->buf[0] = 0;
198 field->buf[1] = 0;
199 }
200
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);
205}
206
207/**
208 * eeprom_field_update_bin_ver() - update a "version field" which contains
209 * binary data
210 *
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.
214 *
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.
217 *
218 * @field: an initialized field
219 * @value: a version string
220 *
221 * Returns 0 on success, -1 on failure.
222 */
223int eeprom_field_update_bin_ver(struct eeprom_field *field, char *value)
224{
225 char *endptr;
226 char *tok = strtok(value, ".");
227 if (tok == NULL)
228 return -1;
229
230 int num = simple_strtol(tok, &endptr, 0);
231 if (*endptr != '\0')
232 return -1;
233
234 tok = strtok(NULL, "");
235 if (tok == NULL)
236 return -1;
237
238 int remainder = simple_strtol(tok, &endptr, 0);
239 if (*endptr != '\0')
240 return -1;
241
242 num = num * 100 + remainder;
243 if (num >> 16)
244 return -1;
245
246 field->buf[0] = (unsigned char)num;
247 field->buf[1] = num >> 8;
248
249 return 0;
250}
251
252char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
253 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
254
255/**
256 * eeprom_field_print_date() - print a field which contains date data
257 *
258 * Treat the field data as simple binary data, and print it formatted as a date.
259 * Sample output:
260 * Field Name 07/Feb/2014
261 * Field Name 56/BAD/9999
262 *
263 * @field: an initialized field to print
264 */
265void eeprom_field_print_date(const struct eeprom_field *field)
266{
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]);
271 else
272 printf("BAD");
273
274 printf("/%d\n", field->buf[3] << 8 | field->buf[2]);
275}
276
277static int validate_date(unsigned char day, unsigned char month,
278 unsigned int year)
279{
280 int days_in_february;
281
282 switch (month) {
283 case 0:
284 case 2:
285 case 4:
286 case 6:
287 case 7:
288 case 9:
289 case 11:
290 if (day > 31)
291 return -1;
292 break;
293 case 3:
294 case 5:
295 case 8:
296 case 10:
297 if (day > 30)
298 return -1;
299 break;
300 case 1:
301 days_in_february = 28;
302 if (year % 4 == 0) {
303 if (year % 100 != 0)
304 days_in_february = 29;
305 else if (year % 400 == 0)
306 days_in_february = 29;
307 }
308
309 if (day > days_in_february)
310 return -1;
311
312 break;
313 default:
314 return -1;
315 }
316
317 return 0;
318}
319
320/**
321 * eeprom_field_update_date() - update a date field which contains binary data
322 *
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
325 * to the field.
326 *
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.
330 *
331 * @field: an initialized field
332 * @value: a date string
333 *
334 * Returns 0 on success, -1 on failure.
335 */
336int eeprom_field_update_date(struct eeprom_field *field, char *value)
337{
338 char *endptr;
339 char *tok1 = strtok(value, "/");
340 char *tok2 = strtok(NULL, "/");
341 char *tok3 = strtok(NULL, "/");
342
343 if (tok1 == NULL || tok2 == NULL || tok3 == NULL) {
344 printf("%s: syntax error\n", field->name);
345 return -1;
346 }
347
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);
351 return -1;
352 }
353
354 unsigned char month;
355 for (month = 1; month <= 12; month++)
356 if (!strcmp(tok2, months[month - 1]))
357 break;
358
359 unsigned int year = simple_strtol(tok3, &endptr, 0);
360 if (*endptr != '\0') {
361 printf("%s: invalid year\n", field->name);
362 return -1;
363 }
364
365 if (validate_date(day, month - 1, year)) {
366 printf("%s: invalid date\n", field->name);
367 return -1;
368 }
369
370 if (year >> 16) {
371 printf("%s: year overflow\n", field->name);
372 return -1;
373 }
374
375 field->buf[0] = day;
376 field->buf[1] = month;
377 field->buf[2] = (unsigned char)year;
378 field->buf[3] = (unsigned char)(year >> 8);
379
380 return 0;
381}
382
383#define LAYOUT_VERSION_LEGACY 1
384#define LAYOUT_VERSION_VER1 2
385#define LAYOUT_VERSION_VER2 3
386#define LAYOUT_VERSION_VER3 4
387
Nikita Kiryanov8af57342016-04-16 17:55:04 +0300388#define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x
389
Nikita Kiryanov8af57342016-04-16 17:55:04 +0300390struct 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 },
407};
408
409struct 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 },
427};
428
429void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version)
430{
431 switch (layout->layout_version) {
Nikita Kiryanov8af57342016-04-16 17:55:04 +0300432 case LAYOUT_VERSION_VER2:
433 layout->fields = layout_v2;
434 layout->num_of_fields = ARRAY_SIZE(layout_v2);
435 break;
436 case LAYOUT_VERSION_VER3:
437 layout->fields = layout_v3;
438 layout->num_of_fields = ARRAY_SIZE(layout_v3);
439 break;
440 default:
441 __eeprom_layout_assign(layout, layout_version);
442 }
443}
444
445int eeprom_parse_layout_version(char *str)
446{
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;
455 else
456 return LAYOUT_VERSION_UNRECOGNIZED;
457}
458
459int eeprom_layout_detect(unsigned char *data)
460{
461 switch (data[EEPROM_LAYOUT_VER_OFFSET]) {
462 case 0xff:
463 case 0:
464 return LAYOUT_VERSION_VER1;
465 case 2:
466 return LAYOUT_VERSION_VER2;
467 case 3:
468 return LAYOUT_VERSION_VER3;
469 }
470
471 if (data[EEPROM_LAYOUT_VER_OFFSET] >= 0x20)
472 return LAYOUT_VERSION_LEGACY;
473
474 return LAYOUT_VERSION_UNRECOGNIZED;
475}
476#endif