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 <eeprom.h> 11#include <i2c.h> 12#include <eeprom_layout.h> 13#include <eeprom_field.h> 14#include <asm/setup.h> 15#include <linux/kernel.h> 16#include "eeprom.h" 17 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 28 29#define LAYOUT_INVALID 0 30#define LAYOUT_LEGACY 0xff 31 32static int cl_eeprom_bus; 33static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */ 34 35static int cl_eeprom_read(uint offset, uchar *buf, int len) 36{ 37 int res; 38 unsigned int current_i2c_bus = i2c_get_bus_num(); 39 40 res = i2c_set_bus_num(cl_eeprom_bus); 41 if (res < 0) 42 return res; 43 44 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset, 45 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len); 46 47 i2c_set_bus_num(current_i2c_bus); 48 49 return res; 50} 51 52static int cl_eeprom_setup(uint eeprom_bus) 53{ 54 int res; 55 56 /* 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) 61 return 0; 62 63 cl_eeprom_bus = eeprom_bus; 64 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET, 65 (uchar *)&cl_eeprom_layout, 1); 66 if (res) { 67 cl_eeprom_layout = LAYOUT_INVALID; 68 return res; 69 } 70 71 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20) 72 cl_eeprom_layout = LAYOUT_LEGACY; 73 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)); 83 84 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS)) 85 return; 86 87 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ? 88 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY; 89 90 if (cl_eeprom_read(offset, (uchar *)serial, 8)) 91 return; 92 93 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) { 94 serialnr->low = serial[0]; 95 serialnr->high = serial[1]; 96 } 97} 98 99/* 100 * Routine: cl_eeprom_read_mac_addr 101 * Description: read mac address and store it in buf. 102 */ 103int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus) 104{ 105 uint offset; 106 int err; 107 108 err = cl_eeprom_setup(eeprom_bus); 109 if (err) 110 return err; 111 112 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ? 113 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY; 114 115 return cl_eeprom_read(offset, buf, 6); 116} 117 118static u32 board_rev; 119 120/* 121 * Routine: cl_eeprom_get_board_rev 122 * Description: read system revision from eeprom 123 */ 124u32 cl_eeprom_get_board_rev(uint eeprom_bus) 125{ 126 char str[5]; /* Legacy representation can contain at most 4 digits */ 127 uint offset = BOARD_REV_OFFSET_LEGACY; 128 129 if (board_rev) 130 return board_rev; 131 132 if (cl_eeprom_setup(eeprom_bus)) 133 return 0; 134 135 if (cl_eeprom_layout != LAYOUT_LEGACY) 136 offset = BOARD_REV_OFFSET; 137 138 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE)) 139 return 0; 140 141 /* 142 * Convert legacy syntactic representation to semantic 143 * representation. i.e. for rev 1.00: 0x100 --> 0x64 144 */ 145 if (cl_eeprom_layout == LAYOUT_LEGACY) { 146 sprintf(str, "%x", board_rev); 147 board_rev = dectoul(str, NULL); 148 } 149 150 return board_rev; 151}; 152 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} 179 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 388#define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x 389 390struct 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) { 432 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 477