eeprom.c 14 KB

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