board_detect.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Library to support early TI EVM EEPROM handling
  4. *
  5. * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
  6. * Lokesh Vutla
  7. * Steve Kipisz
  8. */
  9. #include <common.h>
  10. #include <eeprom.h>
  11. #include <log.h>
  12. #include <net.h>
  13. #include <asm/arch/hardware.h>
  14. #include <asm/omap_common.h>
  15. #include <dm/uclass.h>
  16. #include <env.h>
  17. #include <i2c.h>
  18. #include <mmc.h>
  19. #include <errno.h>
  20. #include <malloc.h>
  21. #include "board_detect.h"
  22. #if !CONFIG_IS_ENABLED(DM_I2C)
  23. /**
  24. * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
  25. * @i2c_bus: i2c bus number to initialize
  26. * @dev_addr: Device address to probe for
  27. *
  28. * Return: 0 on success or corresponding error on failure.
  29. */
  30. static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
  31. {
  32. int rc;
  33. if (i2c_bus >= 0) {
  34. rc = i2c_set_bus_num(i2c_bus);
  35. if (rc)
  36. return rc;
  37. }
  38. return i2c_probe(dev_addr);
  39. }
  40. /**
  41. * ti_i2c_eeprom_read - Read data from an EEPROM
  42. * @dev_addr: The device address of the EEPROM
  43. * @offset: Offset to start reading in the EEPROM
  44. * @ep: Pointer to a buffer to read into
  45. * @epsize: Size of buffer
  46. *
  47. * Return: 0 on success or corresponding result of i2c_read
  48. */
  49. static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
  50. uchar *ep, int epsize)
  51. {
  52. return i2c_read(dev_addr, offset, 2, ep, epsize);
  53. }
  54. #endif
  55. /**
  56. * ti_eeprom_string_cleanup() - Handle eeprom programming errors
  57. * @s: eeprom string (should be NULL terminated)
  58. *
  59. * Some Board manufacturers do not add a NULL termination at the
  60. * end of string, instead some binary information is kludged in, hence
  61. * convert the string to just printable characters of ASCII chart.
  62. */
  63. static void __maybe_unused ti_eeprom_string_cleanup(char *s)
  64. {
  65. int i, l;
  66. l = strlen(s);
  67. for (i = 0; i < l; i++, s++)
  68. if (*s < ' ' || *s > '~') {
  69. *s = 0;
  70. break;
  71. }
  72. }
  73. __weak void gpi2c_init(void)
  74. {
  75. }
  76. static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
  77. u32 header, u32 size, uint8_t *ep)
  78. {
  79. int rc;
  80. uint8_t offset_test;
  81. bool one_byte_addressing = true;
  82. #if CONFIG_IS_ENABLED(DM_I2C)
  83. struct udevice *dev;
  84. struct udevice *bus;
  85. rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
  86. if (rc)
  87. return rc;
  88. rc = dm_i2c_probe(bus, dev_addr, 0, &dev);
  89. if (rc)
  90. return rc;
  91. /*
  92. * Read the header first then only read the other contents.
  93. */
  94. rc = i2c_set_chip_offset_len(dev, 1);
  95. if (rc)
  96. return rc;
  97. /*
  98. * Skip checking result here since this could be a valid i2c read fail
  99. * on some boards that use 2 byte addressing.
  100. * We must allow for fall through to check the data if 2 byte
  101. * addressing works
  102. */
  103. (void)dm_i2c_read(dev, 0, ep, size);
  104. if (*((u32 *)ep) != header)
  105. one_byte_addressing = false;
  106. /*
  107. * Handle case of bad 2 byte eeproms that responds to 1 byte addressing
  108. * but gets stuck in const addressing when read requests are performed
  109. * on offsets. We perform an offset test to make sure it is not a 2 byte
  110. * eeprom that works with 1 byte addressing but just without an offset
  111. */
  112. rc = dm_i2c_read(dev, 0x1, &offset_test, sizeof(offset_test));
  113. if (*((u32 *)ep) != (header & 0xFF))
  114. one_byte_addressing = false;
  115. /* Corrupted data??? */
  116. if (!one_byte_addressing) {
  117. /*
  118. * read the eeprom header using i2c again, but use only a
  119. * 2 byte address (some newer boards need this..)
  120. */
  121. rc = i2c_set_chip_offset_len(dev, 2);
  122. if (rc)
  123. return rc;
  124. rc = dm_i2c_read(dev, 0, ep, size);
  125. if (rc)
  126. return rc;
  127. }
  128. if (*((u32 *)ep) != header)
  129. return -1;
  130. #else
  131. u32 byte;
  132. gpi2c_init();
  133. rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
  134. if (rc)
  135. return rc;
  136. /*
  137. * Read the header first then only read the other contents.
  138. */
  139. byte = 1;
  140. /*
  141. * Skip checking result here since this could be a valid i2c read fail
  142. * on some boards that use 2 byte addressing.
  143. * We must allow for fall through to check the data if 2 byte
  144. * addressing works
  145. */
  146. (void)i2c_read(dev_addr, 0x0, byte, ep, size);
  147. if (*((u32 *)ep) != header)
  148. one_byte_addressing = false;
  149. /*
  150. * Handle case of bad 2 byte eeproms that responds to 1 byte addressing
  151. * but gets stuck in const addressing when read requests are performed
  152. * on offsets. We perform an offset test to make sure it is not a 2 byte
  153. * eeprom that works with 1 byte addressing but just without an offset
  154. */
  155. rc = i2c_read(dev_addr, 0x1, byte, &offset_test, sizeof(offset_test));
  156. if (*((u32 *)ep) != (header & 0xFF))
  157. one_byte_addressing = false;
  158. /* Corrupted data??? */
  159. if (!one_byte_addressing) {
  160. /*
  161. * read the eeprom header using i2c again, but use only a
  162. * 2 byte address (some newer boards need this..)
  163. */
  164. byte = 2;
  165. rc = i2c_read(dev_addr, 0x0, byte, ep, size);
  166. if (rc)
  167. return rc;
  168. }
  169. if (*((u32 *)ep) != header)
  170. return -1;
  171. #endif
  172. return 0;
  173. }
  174. int __maybe_unused ti_emmc_boardid_get(void)
  175. {
  176. int rc;
  177. struct udevice *dev;
  178. struct mmc *mmc;
  179. struct ti_common_eeprom *ep;
  180. struct ti_am_eeprom brdid;
  181. struct blk_desc *bdesc;
  182. uchar *buffer;
  183. ep = TI_EEPROM_DATA;
  184. if (ep->header == TI_EEPROM_HEADER_MAGIC)
  185. return 0; /* EEPROM has already been read */
  186. /* Initialize with a known bad marker for emmc fails.. */
  187. ep->header = TI_DEAD_EEPROM_MAGIC;
  188. ep->name[0] = 0x0;
  189. ep->version[0] = 0x0;
  190. ep->serial[0] = 0x0;
  191. ep->config[0] = 0x0;
  192. /* uclass object initialization */
  193. rc = mmc_initialize(NULL);
  194. if (rc)
  195. return rc;
  196. /* Set device to /dev/mmcblk1 */
  197. rc = uclass_get_device(UCLASS_MMC, 1, &dev);
  198. if (rc)
  199. return rc;
  200. /* Grab the mmc device */
  201. mmc = mmc_get_mmc_dev(dev);
  202. if (!mmc)
  203. return -ENODEV;
  204. /* mmc hardware initialization routine */
  205. mmc_init(mmc);
  206. /* Set partition to /dev/mmcblk1boot1 */
  207. rc = mmc_switch_part(mmc, 2);
  208. if (rc)
  209. return rc;
  210. buffer = malloc(mmc->read_bl_len);
  211. if (!buffer)
  212. return -ENOMEM;
  213. bdesc = mmc_get_blk_desc(mmc);
  214. /* blk_dread returns the number of blocks read*/
  215. if (blk_dread(bdesc, 0L, 1, buffer) != 1) {
  216. rc = -EIO;
  217. goto cleanup;
  218. }
  219. memcpy(&brdid, buffer, sizeof(brdid));
  220. /* Write out the ep struct values */
  221. ep->header = brdid.header;
  222. strlcpy(ep->name, brdid.name, TI_EEPROM_HDR_NAME_LEN + 1);
  223. ti_eeprom_string_cleanup(ep->name);
  224. strlcpy(ep->version, brdid.version, TI_EEPROM_HDR_REV_LEN + 1);
  225. ti_eeprom_string_cleanup(ep->version);
  226. strlcpy(ep->serial, brdid.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
  227. ti_eeprom_string_cleanup(ep->serial);
  228. cleanup:
  229. free(buffer);
  230. return rc;
  231. }
  232. int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
  233. {
  234. struct ti_common_eeprom *ep;
  235. if (!name || !rev)
  236. return -1;
  237. ep = TI_EEPROM_DATA;
  238. if (ep->header == TI_EEPROM_HEADER_MAGIC)
  239. goto already_set;
  240. /* Set to 0 all fields */
  241. memset(ep, 0, sizeof(*ep));
  242. strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
  243. strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
  244. /* Some dummy serial number to identify the platform */
  245. strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
  246. /* Mark it with a valid header */
  247. ep->header = TI_EEPROM_HEADER_MAGIC;
  248. already_set:
  249. return 0;
  250. }
  251. int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
  252. {
  253. int rc;
  254. struct ti_am_eeprom am_ep;
  255. struct ti_common_eeprom *ep;
  256. ep = TI_EEPROM_DATA;
  257. #ifndef CONFIG_SPL_BUILD
  258. if (ep->header == TI_EEPROM_HEADER_MAGIC)
  259. return 0; /* EEPROM has already been read */
  260. #endif
  261. /* Initialize with a known bad marker for i2c fails.. */
  262. ep->header = TI_DEAD_EEPROM_MAGIC;
  263. ep->name[0] = 0x0;
  264. ep->version[0] = 0x0;
  265. ep->serial[0] = 0x0;
  266. ep->config[0] = 0x0;
  267. rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
  268. sizeof(am_ep), (uint8_t *)&am_ep);
  269. if (rc)
  270. return rc;
  271. ep->header = am_ep.header;
  272. strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
  273. ti_eeprom_string_cleanup(ep->name);
  274. /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
  275. if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
  276. am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
  277. strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
  278. else
  279. strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
  280. ti_eeprom_string_cleanup(ep->version);
  281. strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
  282. ti_eeprom_string_cleanup(ep->serial);
  283. strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
  284. ti_eeprom_string_cleanup(ep->config);
  285. memcpy(ep->mac_addr, am_ep.mac_addr,
  286. TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
  287. return 0;
  288. }
  289. int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
  290. {
  291. int rc, offset = 0;
  292. struct dra7_eeprom dra7_ep;
  293. struct ti_common_eeprom *ep;
  294. ep = TI_EEPROM_DATA;
  295. #ifndef CONFIG_SPL_BUILD
  296. if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
  297. return 0; /* EEPROM has already been read */
  298. #endif
  299. /* Initialize with a known bad marker for i2c fails.. */
  300. ep->header = TI_DEAD_EEPROM_MAGIC;
  301. ep->name[0] = 0x0;
  302. ep->version[0] = 0x0;
  303. ep->serial[0] = 0x0;
  304. ep->config[0] = 0x0;
  305. ep->emif1_size = 0;
  306. ep->emif2_size = 0;
  307. rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
  308. sizeof(dra7_ep), (uint8_t *)&dra7_ep);
  309. if (rc)
  310. return rc;
  311. ep->header = dra7_ep.header;
  312. strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
  313. ti_eeprom_string_cleanup(ep->name);
  314. offset = dra7_ep.version_major - 1;
  315. /* Rev F is skipped */
  316. if (offset >= 5)
  317. offset = offset + 1;
  318. snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
  319. 'A' + offset, dra7_ep.version_minor);
  320. ti_eeprom_string_cleanup(ep->version);
  321. ep->emif1_size = (u64)dra7_ep.emif1_size;
  322. ep->emif2_size = (u64)dra7_ep.emif2_size;
  323. strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
  324. ti_eeprom_string_cleanup(ep->config);
  325. return 0;
  326. }
  327. static int ti_i2c_eeprom_am6_parse_record(struct ti_am6_eeprom_record *record,
  328. struct ti_am6_eeprom *ep,
  329. char **mac_addr,
  330. u8 mac_addr_max_cnt,
  331. u8 *mac_addr_cnt)
  332. {
  333. switch (record->header.id) {
  334. case TI_AM6_EEPROM_RECORD_BOARD_INFO:
  335. if (record->header.len != sizeof(record->data.board_info))
  336. return -EINVAL;
  337. if (!ep)
  338. break;
  339. /* Populate (and clean, if needed) the board name */
  340. strlcpy(ep->name, record->data.board_info.name,
  341. sizeof(ep->name));
  342. ti_eeprom_string_cleanup(ep->name);
  343. /* Populate selected other fields from the board info record */
  344. strlcpy(ep->version, record->data.board_info.version,
  345. sizeof(ep->version));
  346. strlcpy(ep->software_revision,
  347. record->data.board_info.software_revision,
  348. sizeof(ep->software_revision));
  349. strlcpy(ep->serial, record->data.board_info.serial,
  350. sizeof(ep->serial));
  351. break;
  352. case TI_AM6_EEPROM_RECORD_MAC_INFO:
  353. if (record->header.len != sizeof(record->data.mac_info))
  354. return -EINVAL;
  355. if (!mac_addr || !mac_addr_max_cnt)
  356. break;
  357. *mac_addr_cnt = ((record->data.mac_info.mac_control &
  358. TI_AM6_EEPROM_MAC_ADDR_COUNT_MASK) >>
  359. TI_AM6_EEPROM_MAC_ADDR_COUNT_SHIFT) + 1;
  360. /*
  361. * The EEPROM can (but may not) hold a very large amount
  362. * of MAC addresses, by far exceeding what we want/can store
  363. * in the common memory array, so only grab what we can fit.
  364. * Note that a value of 0 means 1 MAC address, and so on.
  365. */
  366. *mac_addr_cnt = min(*mac_addr_cnt, mac_addr_max_cnt);
  367. memcpy(mac_addr, record->data.mac_info.mac_addr,
  368. *mac_addr_cnt * TI_EEPROM_HDR_ETH_ALEN);
  369. break;
  370. case 0x00:
  371. /* Illegal value... Fall through... */
  372. case 0xFF:
  373. /* Illegal value... Something went horribly wrong... */
  374. return -EINVAL;
  375. default:
  376. pr_warn("%s: Ignoring record id %u\n", __func__,
  377. record->header.id);
  378. }
  379. return 0;
  380. }
  381. int __maybe_unused ti_i2c_eeprom_am6_get(int bus_addr, int dev_addr,
  382. struct ti_am6_eeprom *ep,
  383. char **mac_addr,
  384. u8 mac_addr_max_cnt,
  385. u8 *mac_addr_cnt)
  386. {
  387. struct udevice *dev;
  388. struct udevice *bus;
  389. unsigned int eeprom_addr;
  390. struct ti_am6_eeprom_record_board_id board_id;
  391. struct ti_am6_eeprom_record record;
  392. int rc;
  393. int consecutive_bad_records = 0;
  394. /* Initialize with a known bad marker for i2c fails.. */
  395. memset(ep, 0, sizeof(*ep));
  396. ep->header = TI_DEAD_EEPROM_MAGIC;
  397. /* Read the board ID record which is always the first EEPROM record */
  398. rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
  399. sizeof(board_id), (uint8_t *)&board_id);
  400. if (rc)
  401. return rc;
  402. if (board_id.header.id != TI_AM6_EEPROM_RECORD_BOARD_ID) {
  403. pr_err("%s: Invalid board ID record!\n", __func__);
  404. return -EINVAL;
  405. }
  406. /* Establish DM handle to board config EEPROM */
  407. rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
  408. if (rc)
  409. return rc;
  410. rc = i2c_get_chip(bus, dev_addr, 1, &dev);
  411. if (rc)
  412. return rc;
  413. ep->header = TI_EEPROM_HEADER_MAGIC;
  414. /* Ready to parse TLV structure. Initialize variables... */
  415. *mac_addr_cnt = 0;
  416. /*
  417. * After the all-encompassing board ID record all other records follow
  418. * a TLV-type scheme. Point to the first such record and then start
  419. * parsing those one by one.
  420. */
  421. eeprom_addr = sizeof(board_id);
  422. while (consecutive_bad_records < 10) {
  423. rc = dm_i2c_read(dev, eeprom_addr, (uint8_t *)&record.header,
  424. sizeof(record.header));
  425. if (rc)
  426. return rc;
  427. /*
  428. * Check for end of list marker. If we reached it don't go
  429. * any further and stop parsing right here.
  430. */
  431. if (record.header.id == TI_AM6_EEPROM_RECORD_END_LIST)
  432. break;
  433. eeprom_addr += sizeof(record.header);
  434. debug("%s: dev_addr=0x%02x header.id=%u header.len=%u\n",
  435. __func__, dev_addr, record.header.id,
  436. record.header.len);
  437. /* Read record into memory if it fits */
  438. if (record.header.len <= sizeof(record.data)) {
  439. rc = dm_i2c_read(dev, eeprom_addr,
  440. (uint8_t *)&record.data,
  441. record.header.len);
  442. if (rc)
  443. return rc;
  444. /* Process record */
  445. rc = ti_i2c_eeprom_am6_parse_record(&record, ep,
  446. mac_addr,
  447. mac_addr_max_cnt,
  448. mac_addr_cnt);
  449. if (rc) {
  450. pr_err("%s: EEPROM parsing error!\n", __func__);
  451. return rc;
  452. }
  453. consecutive_bad_records = 0;
  454. } else {
  455. /*
  456. * We may get here in case of larger records which
  457. * are not yet understood.
  458. */
  459. pr_err("%s: Ignoring record id %u\n", __func__,
  460. record.header.id);
  461. consecutive_bad_records++;
  462. }
  463. eeprom_addr += record.header.len;
  464. }
  465. return 0;
  466. }
  467. int __maybe_unused ti_i2c_eeprom_am6_get_base(int bus_addr, int dev_addr)
  468. {
  469. struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
  470. int ret;
  471. /*
  472. * Always execute EEPROM read by not allowing to bypass it during the
  473. * first invocation of SPL which happens on the R5 core.
  474. */
  475. #if !(defined(CONFIG_SPL_BUILD) && defined(CONFIG_CPU_V7R))
  476. if (ep->header == TI_EEPROM_HEADER_MAGIC) {
  477. debug("%s: EEPROM has already been read\n", __func__);
  478. return 0;
  479. }
  480. #endif
  481. ret = ti_i2c_eeprom_am6_get(bus_addr, dev_addr, ep,
  482. (char **)ep->mac_addr,
  483. AM6_EEPROM_HDR_NO_OF_MAC_ADDR,
  484. &ep->mac_addr_cnt);
  485. return ret;
  486. }
  487. bool __maybe_unused board_ti_k3_is(char *name_tag)
  488. {
  489. struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
  490. if (ep->header == TI_DEAD_EEPROM_MAGIC)
  491. return false;
  492. return !strncmp(ep->name, name_tag, AM6_EEPROM_HDR_NAME_LEN);
  493. }
  494. bool __maybe_unused board_ti_is(char *name_tag)
  495. {
  496. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  497. if (ep->header == TI_DEAD_EEPROM_MAGIC)
  498. return false;
  499. return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
  500. }
  501. bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
  502. {
  503. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  504. int l;
  505. if (ep->header == TI_DEAD_EEPROM_MAGIC)
  506. return false;
  507. l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
  508. return !strncmp(ep->version, rev_tag, l);
  509. }
  510. char * __maybe_unused board_ti_get_rev(void)
  511. {
  512. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  513. /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
  514. return ep->version;
  515. }
  516. char * __maybe_unused board_ti_get_config(void)
  517. {
  518. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  519. /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
  520. return ep->config;
  521. }
  522. char * __maybe_unused board_ti_get_name(void)
  523. {
  524. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  525. /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
  526. return ep->name;
  527. }
  528. void __maybe_unused
  529. board_ti_get_eth_mac_addr(int index,
  530. u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
  531. {
  532. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  533. if (ep->header == TI_DEAD_EEPROM_MAGIC)
  534. goto fail;
  535. if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
  536. goto fail;
  537. memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
  538. return;
  539. fail:
  540. memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
  541. }
  542. void __maybe_unused
  543. board_ti_am6_get_eth_mac_addr(int index,
  544. u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
  545. {
  546. struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
  547. if (ep->header == TI_DEAD_EEPROM_MAGIC)
  548. goto fail;
  549. if (index < 0 || index >= ep->mac_addr_cnt)
  550. goto fail;
  551. memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
  552. return;
  553. fail:
  554. memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
  555. }
  556. u64 __maybe_unused board_ti_get_emif1_size(void)
  557. {
  558. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  559. if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
  560. return 0;
  561. return ep->emif1_size;
  562. }
  563. u64 __maybe_unused board_ti_get_emif2_size(void)
  564. {
  565. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  566. if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
  567. return 0;
  568. return ep->emif2_size;
  569. }
  570. void __maybe_unused set_board_info_env(char *name)
  571. {
  572. char *unknown = "unknown";
  573. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  574. if (name)
  575. env_set("board_name", name);
  576. else if (strlen(ep->name) != 0)
  577. env_set("board_name", ep->name);
  578. else
  579. env_set("board_name", unknown);
  580. if (strlen(ep->version) != 0)
  581. env_set("board_rev", ep->version);
  582. else
  583. env_set("board_rev", unknown);
  584. if (strlen(ep->serial) != 0)
  585. env_set("board_serial", ep->serial);
  586. else
  587. env_set("board_serial", unknown);
  588. }
  589. void __maybe_unused set_board_info_env_am6(char *name)
  590. {
  591. char *unknown = "unknown";
  592. struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
  593. if (name)
  594. env_set("board_name", name);
  595. else if (strlen(ep->name) != 0)
  596. env_set("board_name", ep->name);
  597. else
  598. env_set("board_name", unknown);
  599. if (strlen(ep->version) != 0)
  600. env_set("board_rev", ep->version);
  601. else
  602. env_set("board_rev", unknown);
  603. if (strlen(ep->software_revision) != 0)
  604. env_set("board_software_revision", ep->software_revision);
  605. else
  606. env_set("board_software_revision", unknown);
  607. if (strlen(ep->serial) != 0)
  608. env_set("board_serial", ep->serial);
  609. else
  610. env_set("board_serial", unknown);
  611. }
  612. static u64 mac_to_u64(u8 mac[6])
  613. {
  614. int i;
  615. u64 addr = 0;
  616. for (i = 0; i < 6; i++) {
  617. addr <<= 8;
  618. addr |= mac[i];
  619. }
  620. return addr;
  621. }
  622. static void u64_to_mac(u64 addr, u8 mac[6])
  623. {
  624. mac[5] = addr;
  625. mac[4] = addr >> 8;
  626. mac[3] = addr >> 16;
  627. mac[2] = addr >> 24;
  628. mac[1] = addr >> 32;
  629. mac[0] = addr >> 40;
  630. }
  631. void board_ti_set_ethaddr(int index)
  632. {
  633. uint8_t mac_addr[6];
  634. int i;
  635. u64 mac1, mac2;
  636. u8 mac_addr1[6], mac_addr2[6];
  637. int num_macs;
  638. /*
  639. * Export any Ethernet MAC addresses from EEPROM.
  640. * The 2 MAC addresses in EEPROM define the address range.
  641. */
  642. board_ti_get_eth_mac_addr(0, mac_addr1);
  643. board_ti_get_eth_mac_addr(1, mac_addr2);
  644. if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
  645. mac1 = mac_to_u64(mac_addr1);
  646. mac2 = mac_to_u64(mac_addr2);
  647. /* must contain an address range */
  648. num_macs = mac2 - mac1 + 1;
  649. if (num_macs <= 0)
  650. return;
  651. if (num_macs > 50) {
  652. printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
  653. __func__, num_macs);
  654. num_macs = 50;
  655. }
  656. for (i = 0; i < num_macs; i++) {
  657. u64_to_mac(mac1 + i, mac_addr);
  658. if (is_valid_ethaddr(mac_addr)) {
  659. eth_env_set_enetaddr_by_index("eth", i + index,
  660. mac_addr);
  661. }
  662. }
  663. }
  664. }
  665. void board_ti_am6_set_ethaddr(int index, int count)
  666. {
  667. u8 mac_addr[6];
  668. int i;
  669. for (i = 0; i < count; i++) {
  670. board_ti_am6_get_eth_mac_addr(i, mac_addr);
  671. if (is_valid_ethaddr(mac_addr))
  672. eth_env_set_enetaddr_by_index("eth", i + index,
  673. mac_addr);
  674. }
  675. }
  676. bool __maybe_unused board_ti_was_eeprom_read(void)
  677. {
  678. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  679. if (ep->header == TI_EEPROM_HEADER_MAGIC)
  680. return true;
  681. else
  682. return false;
  683. }