eeprom.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. #include <common.h>
  9. #include <eeprom.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. #ifndef CONFIG_SYS_I2C_EEPROM_ADDR
  17. # define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
  18. # define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
  19. #endif
  20. #ifndef CONFIG_SYS_I2C_EEPROM_BUS
  21. #define CONFIG_SYS_I2C_EEPROM_BUS 0
  22. #endif
  23. #define EEPROM_LAYOUT_VER_OFFSET 44
  24. #define BOARD_SERIAL_OFFSET 20
  25. #define BOARD_SERIAL_OFFSET_LEGACY 8
  26. #define BOARD_REV_OFFSET 0
  27. #define BOARD_REV_OFFSET_LEGACY 6
  28. #define BOARD_REV_SIZE 2
  29. #define PRODUCT_NAME_OFFSET 128
  30. #define PRODUCT_NAME_SIZE 16
  31. #define MAC_ADDR_OFFSET 4
  32. #define MAC_ADDR_OFFSET_LEGACY 0
  33. #define LAYOUT_INVALID 0
  34. #define LAYOUT_LEGACY 0xff
  35. static int cl_eeprom_bus;
  36. static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
  37. static int cl_eeprom_read(uint offset, uchar *buf, int len)
  38. {
  39. int res;
  40. unsigned int current_i2c_bus = i2c_get_bus_num();
  41. res = i2c_set_bus_num(cl_eeprom_bus);
  42. if (res < 0)
  43. return res;
  44. res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
  45. CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
  46. i2c_set_bus_num(current_i2c_bus);
  47. return res;
  48. }
  49. static int cl_eeprom_setup(uint eeprom_bus)
  50. {
  51. int res;
  52. /*
  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)
  57. return 0;
  58. cl_eeprom_bus = eeprom_bus;
  59. res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
  60. (uchar *)&cl_eeprom_layout, 1);
  61. if (res) {
  62. cl_eeprom_layout = LAYOUT_INVALID;
  63. return res;
  64. }
  65. if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
  66. cl_eeprom_layout = LAYOUT_LEGACY;
  67. return 0;
  68. }
  69. void get_board_serial(struct tag_serialnr *serialnr)
  70. {
  71. u32 serial[2];
  72. uint offset;
  73. memset(serialnr, 0, sizeof(*serialnr));
  74. if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
  75. return;
  76. offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
  77. BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
  78. if (cl_eeprom_read(offset, (uchar *)serial, 8))
  79. return;
  80. if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
  81. serialnr->low = serial[0];
  82. serialnr->high = serial[1];
  83. }
  84. }
  85. /*
  86. * Routine: cl_eeprom_read_mac_addr
  87. * Description: read mac address and store it in buf.
  88. */
  89. int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
  90. {
  91. uint offset;
  92. int err;
  93. err = cl_eeprom_setup(eeprom_bus);
  94. if (err)
  95. return err;
  96. offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
  97. MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
  98. return cl_eeprom_read(offset, buf, 6);
  99. }
  100. static u32 board_rev;
  101. /*
  102. * Routine: cl_eeprom_get_board_rev
  103. * Description: read system revision from eeprom
  104. */
  105. u32 cl_eeprom_get_board_rev(uint eeprom_bus)
  106. {
  107. char str[5]; /* Legacy representation can contain at most 4 digits */
  108. uint offset = BOARD_REV_OFFSET_LEGACY;
  109. if (board_rev)
  110. return board_rev;
  111. if (cl_eeprom_setup(eeprom_bus))
  112. return 0;
  113. if (cl_eeprom_layout != LAYOUT_LEGACY)
  114. offset = BOARD_REV_OFFSET;
  115. if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
  116. return 0;
  117. /*
  118. * Convert legacy syntactic representation to semantic
  119. * representation. i.e. for rev 1.00: 0x100 --> 0x64
  120. */
  121. if (cl_eeprom_layout == LAYOUT_LEGACY) {
  122. sprintf(str, "%x", board_rev);
  123. board_rev = dectoul(str, NULL);
  124. }
  125. return board_rev;
  126. };
  127. /*
  128. * Routine: cl_eeprom_get_board_rev
  129. * Description: read system revision from eeprom
  130. *
  131. * @buf: buffer to store the product name
  132. * @eeprom_bus: i2c bus num of the eeprom
  133. *
  134. * @return: 0 on success, < 0 on failure
  135. */
  136. int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
  137. {
  138. int err;
  139. if (buf == NULL)
  140. return -EINVAL;
  141. err = cl_eeprom_setup(eeprom_bus);
  142. if (err)
  143. return err;
  144. err = cl_eeprom_read(PRODUCT_NAME_OFFSET, buf, PRODUCT_NAME_SIZE);
  145. if (!err) /* Protect ourselves from invalid data (unterminated str) */
  146. buf[PRODUCT_NAME_SIZE - 1] = '\0';
  147. return err;
  148. }
  149. #ifdef CONFIG_CMD_EEPROM_LAYOUT
  150. /**
  151. * eeprom_field_print_bin_ver() - print a "version field" which contains binary
  152. * data
  153. *
  154. * Treat the field data as simple binary data, and print it formatted as a
  155. * version number (2 digits after decimal point).
  156. * The field size must be exactly 2 bytes.
  157. *
  158. * Sample output:
  159. * Field Name 123.45
  160. *
  161. * @field: an initialized field to print
  162. */
  163. void eeprom_field_print_bin_ver(const struct eeprom_field *field)
  164. {
  165. if ((field->buf[0] == 0xff) && (field->buf[1] == 0xff)) {
  166. field->buf[0] = 0;
  167. field->buf[1] = 0;
  168. }
  169. printf(PRINT_FIELD_SEGMENT, field->name);
  170. int major = (field->buf[1] << 8 | field->buf[0]) / 100;
  171. int minor = (field->buf[1] << 8 | field->buf[0]) - major * 100;
  172. printf("%d.%02d\n", major, minor);
  173. }
  174. /**
  175. * eeprom_field_update_bin_ver() - update a "version field" which contains
  176. * binary data
  177. *
  178. * This function takes a version string in the form of x.y (x and y are both
  179. * decimal values, y is limited to two digits), translates it to the binary
  180. * form, then writes it to the field. The field size must be exactly 2 bytes.
  181. *
  182. * This function strictly enforces the data syntax, and will not update the
  183. * field if there's any deviation from it. It also protects from overflow.
  184. *
  185. * @field: an initialized field
  186. * @value: a version string
  187. *
  188. * Returns 0 on success, -1 on failure.
  189. */
  190. int eeprom_field_update_bin_ver(struct eeprom_field *field, char *value)
  191. {
  192. char *endptr;
  193. char *tok = strtok(value, ".");
  194. if (tok == NULL)
  195. return -1;
  196. int num = simple_strtol(tok, &endptr, 0);
  197. if (*endptr != '\0')
  198. return -1;
  199. tok = strtok(NULL, "");
  200. if (tok == NULL)
  201. return -1;
  202. int remainder = simple_strtol(tok, &endptr, 0);
  203. if (*endptr != '\0')
  204. return -1;
  205. num = num * 100 + remainder;
  206. if (num >> 16)
  207. return -1;
  208. field->buf[0] = (unsigned char)num;
  209. field->buf[1] = num >> 8;
  210. return 0;
  211. }
  212. char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  213. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  214. /**
  215. * eeprom_field_print_date() - print a field which contains date data
  216. *
  217. * Treat the field data as simple binary data, and print it formatted as a date.
  218. * Sample output:
  219. * Field Name 07/Feb/2014
  220. * Field Name 56/BAD/9999
  221. *
  222. * @field: an initialized field to print
  223. */
  224. void eeprom_field_print_date(const struct eeprom_field *field)
  225. {
  226. printf(PRINT_FIELD_SEGMENT, field->name);
  227. printf("%02d/", field->buf[0]);
  228. if (field->buf[1] >= 1 && field->buf[1] <= 12)
  229. printf("%s", months[field->buf[1] - 1]);
  230. else
  231. printf("BAD");
  232. printf("/%d\n", field->buf[3] << 8 | field->buf[2]);
  233. }
  234. static int validate_date(unsigned char day, unsigned char month,
  235. unsigned int year)
  236. {
  237. int days_in_february;
  238. switch (month) {
  239. case 0:
  240. case 2:
  241. case 4:
  242. case 6:
  243. case 7:
  244. case 9:
  245. case 11:
  246. if (day > 31)
  247. return -1;
  248. break;
  249. case 3:
  250. case 5:
  251. case 8:
  252. case 10:
  253. if (day > 30)
  254. return -1;
  255. break;
  256. case 1:
  257. days_in_february = 28;
  258. if (year % 4 == 0) {
  259. if (year % 100 != 0)
  260. days_in_february = 29;
  261. else if (year % 400 == 0)
  262. days_in_february = 29;
  263. }
  264. if (day > days_in_february)
  265. return -1;
  266. break;
  267. default:
  268. return -1;
  269. }
  270. return 0;
  271. }
  272. /**
  273. * eeprom_field_update_date() - update a date field which contains binary data
  274. *
  275. * This function takes a date string in the form of x/Mon/y (x and y are both
  276. * decimal values), translates it to the binary representation, then writes it
  277. * to the field.
  278. *
  279. * This function strictly enforces the data syntax, and will not update the
  280. * field if there's any deviation from it. It also protects from overflow in the
  281. * year value, and checks the validity of the date.
  282. *
  283. * @field: an initialized field
  284. * @value: a date string
  285. *
  286. * Returns 0 on success, -1 on failure.
  287. */
  288. int eeprom_field_update_date(struct eeprom_field *field, char *value)
  289. {
  290. char *endptr;
  291. char *tok1 = strtok(value, "/");
  292. char *tok2 = strtok(NULL, "/");
  293. char *tok3 = strtok(NULL, "/");
  294. if (tok1 == NULL || tok2 == NULL || tok3 == NULL) {
  295. printf("%s: syntax error\n", field->name);
  296. return -1;
  297. }
  298. unsigned char day = (unsigned char)simple_strtol(tok1, &endptr, 0);
  299. if (*endptr != '\0' || day == 0) {
  300. printf("%s: invalid day\n", field->name);
  301. return -1;
  302. }
  303. unsigned char month;
  304. for (month = 1; month <= 12; month++)
  305. if (!strcmp(tok2, months[month - 1]))
  306. break;
  307. unsigned int year = simple_strtol(tok3, &endptr, 0);
  308. if (*endptr != '\0') {
  309. printf("%s: invalid year\n", field->name);
  310. return -1;
  311. }
  312. if (validate_date(day, month - 1, year)) {
  313. printf("%s: invalid date\n", field->name);
  314. return -1;
  315. }
  316. if (year >> 16) {
  317. printf("%s: year overflow\n", field->name);
  318. return -1;
  319. }
  320. field->buf[0] = day;
  321. field->buf[1] = month;
  322. field->buf[2] = (unsigned char)year;
  323. field->buf[3] = (unsigned char)(year >> 8);
  324. return 0;
  325. }
  326. #define LAYOUT_VERSION_LEGACY 1
  327. #define LAYOUT_VERSION_VER1 2
  328. #define LAYOUT_VERSION_VER2 3
  329. #define LAYOUT_VERSION_VER3 4
  330. extern struct eeprom_field layout_unknown[1];
  331. #define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x
  332. #ifdef CONFIG_CM_T3X
  333. struct eeprom_field layout_legacy[5] = {
  334. { "MAC address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  335. { "Board Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin) },
  336. { "Serial Number", 8, NULL, DEFINE_PRINT_UPDATE(bin) },
  337. { "Board Configuration", 64, NULL, DEFINE_PRINT_UPDATE(ascii) },
  338. { RESERVED_FIELDS, 176, NULL, eeprom_field_print_reserved,
  339. eeprom_field_update_ascii },
  340. };
  341. #else
  342. #define layout_legacy layout_unknown
  343. #endif
  344. #if defined(CONFIG_CM_T3X)
  345. struct eeprom_field layout_v1[12] = {
  346. { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
  347. { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
  348. { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  349. { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  350. { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
  351. { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
  352. { RESERVED_FIELDS, 96, NULL, DEFINE_PRINT_UPDATE(reserved) },
  353. { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  354. { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  355. { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  356. { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  357. { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
  358. eeprom_field_update_ascii },
  359. };
  360. #else
  361. #define layout_v1 layout_unknown
  362. #endif
  363. struct eeprom_field layout_v2[15] = {
  364. { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
  365. { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
  366. { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  367. { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  368. { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
  369. { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
  370. { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  371. { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  372. { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
  373. { RESERVED_FIELDS, 83, NULL, DEFINE_PRINT_UPDATE(reserved) },
  374. { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  375. { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  376. { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  377. { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  378. { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
  379. eeprom_field_update_ascii },
  380. };
  381. struct eeprom_field layout_v3[16] = {
  382. { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
  383. { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
  384. { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  385. { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  386. { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
  387. { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
  388. { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  389. { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
  390. { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
  391. { "CompuLab EEPROM ID", 3, NULL, DEFINE_PRINT_UPDATE(bin) },
  392. { RESERVED_FIELDS, 80, NULL, DEFINE_PRINT_UPDATE(reserved) },
  393. { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  394. { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  395. { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  396. { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
  397. { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
  398. eeprom_field_update_ascii },
  399. };
  400. void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version)
  401. {
  402. switch (layout->layout_version) {
  403. case LAYOUT_VERSION_LEGACY:
  404. layout->fields = layout_legacy;
  405. layout->num_of_fields = ARRAY_SIZE(layout_legacy);
  406. break;
  407. case LAYOUT_VERSION_VER1:
  408. layout->fields = layout_v1;
  409. layout->num_of_fields = ARRAY_SIZE(layout_v1);
  410. break;
  411. case LAYOUT_VERSION_VER2:
  412. layout->fields = layout_v2;
  413. layout->num_of_fields = ARRAY_SIZE(layout_v2);
  414. break;
  415. case LAYOUT_VERSION_VER3:
  416. layout->fields = layout_v3;
  417. layout->num_of_fields = ARRAY_SIZE(layout_v3);
  418. break;
  419. default:
  420. __eeprom_layout_assign(layout, layout_version);
  421. }
  422. }
  423. int eeprom_parse_layout_version(char *str)
  424. {
  425. if (!strcmp(str, "legacy"))
  426. return LAYOUT_VERSION_LEGACY;
  427. else if (!strcmp(str, "v1"))
  428. return LAYOUT_VERSION_VER1;
  429. else if (!strcmp(str, "v2"))
  430. return LAYOUT_VERSION_VER2;
  431. else if (!strcmp(str, "v3"))
  432. return LAYOUT_VERSION_VER3;
  433. else
  434. return LAYOUT_VERSION_UNRECOGNIZED;
  435. }
  436. int eeprom_layout_detect(unsigned char *data)
  437. {
  438. switch (data[EEPROM_LAYOUT_VER_OFFSET]) {
  439. case 0xff:
  440. case 0:
  441. return LAYOUT_VERSION_VER1;
  442. case 2:
  443. return LAYOUT_VERSION_VER2;
  444. case 3:
  445. return LAYOUT_VERSION_VER3;
  446. }
  447. if (data[EEPROM_LAYOUT_VER_OFFSET] >= 0x20)
  448. return LAYOUT_VERSION_LEGACY;
  449. return LAYOUT_VERSION_UNRECOGNIZED;
  450. }
  451. #endif