board_detect.c 10 KB

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