board_detect.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 <asm/omap_common.h>
  11. #include <dm/uclass.h>
  12. #include <i2c.h>
  13. #include "board_detect.h"
  14. #if defined(CONFIG_DM_I2C_COMPAT)
  15. /**
  16. * ti_i2c_set_alen - Set chip's i2c address length
  17. * @bus_addr - I2C bus number
  18. * @dev_addr - I2C eeprom id
  19. * @alen - I2C address length in bytes
  20. *
  21. * DM_I2C by default sets the address length to be used to 1. This
  22. * function allows this address length to be changed to match the
  23. * eeprom used for board detection.
  24. */
  25. int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
  26. {
  27. struct udevice *dev;
  28. struct udevice *bus;
  29. int rc;
  30. rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
  31. if (rc)
  32. return rc;
  33. rc = i2c_get_chip(bus, dev_addr, 1, &dev);
  34. if (rc)
  35. return rc;
  36. rc = i2c_set_chip_offset_len(dev, alen);
  37. if (rc)
  38. return rc;
  39. return 0;
  40. }
  41. #else
  42. int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
  43. {
  44. return 0;
  45. }
  46. #endif
  47. /**
  48. * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
  49. * @i2c_bus: i2c bus number to initialize
  50. * @dev_addr: Device address to probe for
  51. *
  52. * Return: 0 on success or corresponding error on failure.
  53. */
  54. static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
  55. {
  56. int rc;
  57. if (i2c_bus >= 0) {
  58. rc = i2c_set_bus_num(i2c_bus);
  59. if (rc)
  60. return rc;
  61. }
  62. return i2c_probe(dev_addr);
  63. }
  64. /**
  65. * ti_i2c_eeprom_read - Read data from an EEPROM
  66. * @dev_addr: The device address of the EEPROM
  67. * @offset: Offset to start reading in the EEPROM
  68. * @ep: Pointer to a buffer to read into
  69. * @epsize: Size of buffer
  70. *
  71. * Return: 0 on success or corresponding result of i2c_read
  72. */
  73. static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
  74. uchar *ep, int epsize)
  75. {
  76. int bus_num, rc, alen;
  77. bus_num = i2c_get_bus_num();
  78. alen = 2;
  79. rc = ti_i2c_set_alen(bus_num, dev_addr, alen);
  80. if (rc)
  81. return rc;
  82. return i2c_read(dev_addr, offset, alen, ep, epsize);
  83. }
  84. /**
  85. * ti_eeprom_string_cleanup() - Handle eeprom programming errors
  86. * @s: eeprom string (should be NULL terminated)
  87. *
  88. * Some Board manufacturers do not add a NULL termination at the
  89. * end of string, instead some binary information is kludged in, hence
  90. * convert the string to just printable characters of ASCII chart.
  91. */
  92. static void __maybe_unused ti_eeprom_string_cleanup(char *s)
  93. {
  94. int i, l;
  95. l = strlen(s);
  96. for (i = 0; i < l; i++, s++)
  97. if (*s < ' ' || *s > '~') {
  98. *s = 0;
  99. break;
  100. }
  101. }
  102. __weak void gpi2c_init(void)
  103. {
  104. }
  105. static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
  106. u32 header, u32 size, uint8_t *ep)
  107. {
  108. u32 byte, hdr_read;
  109. int rc;
  110. gpi2c_init();
  111. rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
  112. if (rc)
  113. return rc;
  114. /*
  115. * Read the header first then only read the other contents.
  116. */
  117. byte = 2;
  118. rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
  119. if (rc)
  120. return rc;
  121. rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
  122. if (rc)
  123. return rc;
  124. /* Corrupted data??? */
  125. if (hdr_read != header) {
  126. rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
  127. /*
  128. * read the eeprom header using i2c again, but use only a
  129. * 1 byte address (some legacy boards need this..)
  130. */
  131. byte = 1;
  132. if (rc) {
  133. rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
  134. if (rc)
  135. return rc;
  136. rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
  137. 4);
  138. }
  139. if (rc)
  140. return rc;
  141. }
  142. if (hdr_read != header)
  143. return -1;
  144. rc = i2c_read(dev_addr, 0x0, byte, ep, size);
  145. if (rc)
  146. return rc;
  147. return 0;
  148. }
  149. int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
  150. {
  151. struct ti_common_eeprom *ep;
  152. if (!name || !rev)
  153. return -1;
  154. ep = TI_EEPROM_DATA;
  155. if (ep->header == TI_EEPROM_HEADER_MAGIC)
  156. goto already_set;
  157. /* Set to 0 all fields */
  158. memset(ep, 0, sizeof(*ep));
  159. strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
  160. strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
  161. /* Some dummy serial number to identify the platform */
  162. strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
  163. /* Mark it with a valid header */
  164. ep->header = TI_EEPROM_HEADER_MAGIC;
  165. already_set:
  166. return 0;
  167. }
  168. int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
  169. {
  170. int rc;
  171. struct ti_am_eeprom am_ep;
  172. struct ti_common_eeprom *ep;
  173. ep = TI_EEPROM_DATA;
  174. #ifndef CONFIG_SPL_BUILD
  175. if (ep->header == TI_EEPROM_HEADER_MAGIC)
  176. return 0; /* EEPROM has already been read */
  177. #endif
  178. /* Initialize with a known bad marker for i2c fails.. */
  179. ep->header = TI_DEAD_EEPROM_MAGIC;
  180. ep->name[0] = 0x0;
  181. ep->version[0] = 0x0;
  182. ep->serial[0] = 0x0;
  183. ep->config[0] = 0x0;
  184. rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
  185. sizeof(am_ep), (uint8_t *)&am_ep);
  186. if (rc)
  187. return rc;
  188. ep->header = am_ep.header;
  189. strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
  190. ti_eeprom_string_cleanup(ep->name);
  191. /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
  192. if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
  193. am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
  194. strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
  195. else
  196. strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
  197. ti_eeprom_string_cleanup(ep->version);
  198. strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
  199. ti_eeprom_string_cleanup(ep->serial);
  200. strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
  201. ti_eeprom_string_cleanup(ep->config);
  202. memcpy(ep->mac_addr, am_ep.mac_addr,
  203. TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
  204. return 0;
  205. }
  206. int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
  207. {
  208. int rc, offset = 0;
  209. struct dra7_eeprom dra7_ep;
  210. struct ti_common_eeprom *ep;
  211. ep = TI_EEPROM_DATA;
  212. #ifndef CONFIG_SPL_BUILD
  213. if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
  214. return 0; /* EEPROM has already been read */
  215. #endif
  216. /* Initialize with a known bad marker for i2c fails.. */
  217. ep->header = TI_DEAD_EEPROM_MAGIC;
  218. ep->name[0] = 0x0;
  219. ep->version[0] = 0x0;
  220. ep->serial[0] = 0x0;
  221. ep->config[0] = 0x0;
  222. ep->emif1_size = 0;
  223. ep->emif2_size = 0;
  224. rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
  225. sizeof(dra7_ep), (uint8_t *)&dra7_ep);
  226. if (rc)
  227. return rc;
  228. ep->header = dra7_ep.header;
  229. strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
  230. ti_eeprom_string_cleanup(ep->name);
  231. offset = dra7_ep.version_major - 1;
  232. /* Rev F is skipped */
  233. if (offset >= 5)
  234. offset = offset + 1;
  235. snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
  236. 'A' + offset, dra7_ep.version_minor);
  237. ti_eeprom_string_cleanup(ep->version);
  238. ep->emif1_size = (u64)dra7_ep.emif1_size;
  239. ep->emif2_size = (u64)dra7_ep.emif2_size;
  240. strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
  241. ti_eeprom_string_cleanup(ep->config);
  242. return 0;
  243. }
  244. bool __maybe_unused board_ti_is(char *name_tag)
  245. {
  246. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  247. if (ep->header == TI_DEAD_EEPROM_MAGIC)
  248. return false;
  249. return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
  250. }
  251. bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
  252. {
  253. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  254. int l;
  255. if (ep->header == TI_DEAD_EEPROM_MAGIC)
  256. return false;
  257. l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
  258. return !strncmp(ep->version, rev_tag, l);
  259. }
  260. char * __maybe_unused board_ti_get_rev(void)
  261. {
  262. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  263. /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
  264. return ep->version;
  265. }
  266. char * __maybe_unused board_ti_get_config(void)
  267. {
  268. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  269. /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
  270. return ep->config;
  271. }
  272. char * __maybe_unused board_ti_get_name(void)
  273. {
  274. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  275. /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
  276. return ep->name;
  277. }
  278. void __maybe_unused
  279. board_ti_get_eth_mac_addr(int index,
  280. u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
  281. {
  282. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  283. if (ep->header == TI_DEAD_EEPROM_MAGIC)
  284. goto fail;
  285. if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
  286. goto fail;
  287. memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
  288. return;
  289. fail:
  290. memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
  291. }
  292. u64 __maybe_unused board_ti_get_emif1_size(void)
  293. {
  294. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  295. if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
  296. return 0;
  297. return ep->emif1_size;
  298. }
  299. u64 __maybe_unused board_ti_get_emif2_size(void)
  300. {
  301. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  302. if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
  303. return 0;
  304. return ep->emif2_size;
  305. }
  306. void __maybe_unused set_board_info_env(char *name)
  307. {
  308. char *unknown = "unknown";
  309. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  310. if (name)
  311. env_set("board_name", name);
  312. else if (ep->name)
  313. env_set("board_name", ep->name);
  314. else
  315. env_set("board_name", unknown);
  316. if (ep->version)
  317. env_set("board_rev", ep->version);
  318. else
  319. env_set("board_rev", unknown);
  320. if (ep->serial)
  321. env_set("board_serial", ep->serial);
  322. else
  323. env_set("board_serial", unknown);
  324. }
  325. static u64 mac_to_u64(u8 mac[6])
  326. {
  327. int i;
  328. u64 addr = 0;
  329. for (i = 0; i < 6; i++) {
  330. addr <<= 8;
  331. addr |= mac[i];
  332. }
  333. return addr;
  334. }
  335. static void u64_to_mac(u64 addr, u8 mac[6])
  336. {
  337. mac[5] = addr;
  338. mac[4] = addr >> 8;
  339. mac[3] = addr >> 16;
  340. mac[2] = addr >> 24;
  341. mac[1] = addr >> 32;
  342. mac[0] = addr >> 40;
  343. }
  344. void board_ti_set_ethaddr(int index)
  345. {
  346. uint8_t mac_addr[6];
  347. int i;
  348. u64 mac1, mac2;
  349. u8 mac_addr1[6], mac_addr2[6];
  350. int num_macs;
  351. /*
  352. * Export any Ethernet MAC addresses from EEPROM.
  353. * The 2 MAC addresses in EEPROM define the address range.
  354. */
  355. board_ti_get_eth_mac_addr(0, mac_addr1);
  356. board_ti_get_eth_mac_addr(1, mac_addr2);
  357. if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
  358. mac1 = mac_to_u64(mac_addr1);
  359. mac2 = mac_to_u64(mac_addr2);
  360. /* must contain an address range */
  361. num_macs = mac2 - mac1 + 1;
  362. if (num_macs <= 0)
  363. return;
  364. if (num_macs > 50) {
  365. printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
  366. __func__, num_macs);
  367. num_macs = 50;
  368. }
  369. for (i = 0; i < num_macs; i++) {
  370. u64_to_mac(mac1 + i, mac_addr);
  371. if (is_valid_ethaddr(mac_addr)) {
  372. eth_env_set_enetaddr_by_index("eth", i + index,
  373. mac_addr);
  374. }
  375. }
  376. }
  377. }
  378. bool __maybe_unused board_ti_was_eeprom_read(void)
  379. {
  380. struct ti_common_eeprom *ep = TI_EEPROM_DATA;
  381. if (ep->header == TI_EEPROM_HEADER_MAGIC)
  382. return true;
  383. else
  384. return false;
  385. }