board.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2014 - 2020 Xilinx, Inc.
  4. * Michal Simek <michal.simek@xilinx.com>
  5. */
  6. #include <common.h>
  7. #include <env.h>
  8. #include <log.h>
  9. #include <asm/global_data.h>
  10. #include <asm/sections.h>
  11. #include <dm/uclass.h>
  12. #include <i2c.h>
  13. #include <linux/sizes.h>
  14. #include <malloc.h>
  15. #include "board.h"
  16. #include <dm.h>
  17. #include <i2c_eeprom.h>
  18. #include <net.h>
  19. #include <generated/dt.h>
  20. #include <soc.h>
  21. #include <linux/ctype.h>
  22. #include "fru.h"
  23. #if defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET)
  24. int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
  25. {
  26. int ret = -EINVAL;
  27. struct udevice *dev;
  28. ofnode eeprom;
  29. eeprom = ofnode_get_chosen_node("xlnx,eeprom");
  30. if (!ofnode_valid(eeprom))
  31. return -ENODEV;
  32. debug("%s: Path to EEPROM %s\n", __func__,
  33. ofnode_read_chosen_string("xlnx,eeprom"));
  34. ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
  35. if (ret)
  36. return ret;
  37. ret = dm_i2c_read(dev, CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, ethaddr, 6);
  38. if (ret)
  39. debug("%s: I2C EEPROM MAC address read failed\n", __func__);
  40. else
  41. debug("%s: I2C EEPROM MAC %pM\n", __func__, ethaddr);
  42. return ret;
  43. }
  44. #endif
  45. #define EEPROM_HEADER_MAGIC 0xdaaddeed
  46. #define EEPROM_HDR_MANUFACTURER_LEN 16
  47. #define EEPROM_HDR_NAME_LEN 16
  48. #define EEPROM_HDR_REV_LEN 8
  49. #define EEPROM_HDR_SERIAL_LEN 20
  50. #define EEPROM_HDR_NO_OF_MAC_ADDR 4
  51. #define EEPROM_HDR_ETH_ALEN ETH_ALEN
  52. struct xilinx_board_description {
  53. u32 header;
  54. char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
  55. char name[EEPROM_HDR_NAME_LEN + 1];
  56. char revision[EEPROM_HDR_REV_LEN + 1];
  57. char serial[EEPROM_HDR_SERIAL_LEN + 1];
  58. u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
  59. };
  60. static int highest_id = -1;
  61. static struct xilinx_board_description *board_info;
  62. #define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr)
  63. /* Variable which stores pointer to array which stores eeprom content */
  64. struct xilinx_legacy_format {
  65. char board_sn[18]; /* 0x0 */
  66. char unused0[14]; /* 0x12 */
  67. char eth_mac[6]; /* 0x20 */
  68. char unused1[170]; /* 0x26 */
  69. char board_name[11]; /* 0xd0 */
  70. char unused2[5]; /* 0xdc */
  71. char board_revision[3]; /* 0xe0 */
  72. char unused3[29]; /* 0xe3 */
  73. };
  74. static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
  75. {
  76. int i;
  77. char byte;
  78. for (i = 0; i < size; i++) {
  79. byte = eeprom[i];
  80. /* Remove all ffs and spaces */
  81. if (byte == 0xff || byte == ' ')
  82. eeprom[i] = 0;
  83. /* Convert strings to lower case */
  84. if (byte >= 'A' && byte <= 'Z')
  85. eeprom[i] = byte + 'a' - 'A';
  86. }
  87. }
  88. static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
  89. struct xilinx_board_description *desc)
  90. {
  91. int ret, size;
  92. struct xilinx_legacy_format *eeprom_content;
  93. bool eth_valid = false;
  94. size = sizeof(*eeprom_content);
  95. eeprom_content = calloc(1, size);
  96. if (!eeprom_content)
  97. return -ENOMEM;
  98. debug("%s: I2C EEPROM read pass data at %p\n", __func__,
  99. eeprom_content);
  100. ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
  101. if (ret) {
  102. debug("%s: I2C EEPROM read failed\n", __func__);
  103. free(eeprom_content);
  104. return ret;
  105. }
  106. xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
  107. printf("Xilinx I2C Legacy format at %s:\n", name);
  108. printf(" Board name:\t%s\n", eeprom_content->board_name);
  109. printf(" Board rev:\t%s\n", eeprom_content->board_revision);
  110. printf(" Board SN:\t%s\n", eeprom_content->board_sn);
  111. eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
  112. if (eth_valid)
  113. printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac);
  114. /* Terminating \0 chars ensure end of string */
  115. strcpy(desc->name, eeprom_content->board_name);
  116. strcpy(desc->revision, eeprom_content->board_revision);
  117. strcpy(desc->serial, eeprom_content->board_sn);
  118. if (eth_valid)
  119. memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
  120. desc->header = EEPROM_HEADER_MAGIC;
  121. free(eeprom_content);
  122. return ret;
  123. }
  124. static bool xilinx_detect_legacy(u8 *buffer)
  125. {
  126. int i;
  127. char c;
  128. for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
  129. c = buffer[i];
  130. if (c < '0' || c > '9')
  131. return false;
  132. }
  133. return true;
  134. }
  135. static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
  136. struct xilinx_board_description *desc)
  137. {
  138. int i, ret, eeprom_size;
  139. u8 *fru_content;
  140. /* FIXME this is shortcut - if eeprom type is wrong it will fail */
  141. eeprom_size = i2c_eeprom_size(dev);
  142. fru_content = calloc(1, eeprom_size);
  143. if (!fru_content)
  144. return -ENOMEM;
  145. debug("%s: I2C EEPROM read pass data at %p\n", __func__,
  146. fru_content);
  147. ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
  148. eeprom_size);
  149. if (ret) {
  150. debug("%s: I2C EEPROM read failed\n", __func__);
  151. goto end;
  152. }
  153. fru_capture((unsigned long)fru_content);
  154. if (gd->flags & GD_FLG_RELOC || (_DEBUG && CONFIG_IS_ENABLED(DTB_RESELECT))) {
  155. printf("Xilinx I2C FRU format at %s:\n", name);
  156. ret = fru_display(0);
  157. if (ret) {
  158. printf("FRU format decoding failed.\n");
  159. goto end;
  160. }
  161. }
  162. if (desc->header == EEPROM_HEADER_MAGIC) {
  163. debug("Information already filled\n");
  164. ret = -EINVAL;
  165. goto end;
  166. }
  167. /* It is clear that FRU was captured and structures were filled */
  168. strncpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
  169. sizeof(desc->manufacturer));
  170. strncpy(desc->name, (char *)fru_data.brd.product_name,
  171. sizeof(desc->name));
  172. for (i = 0; i < sizeof(desc->name); i++) {
  173. if (desc->name[i] == ' ')
  174. desc->name[i] = '\0';
  175. }
  176. strncpy(desc->revision, (char *)fru_data.brd.rev,
  177. sizeof(desc->revision));
  178. strncpy(desc->serial, (char *)fru_data.brd.serial_number,
  179. sizeof(desc->serial));
  180. desc->header = EEPROM_HEADER_MAGIC;
  181. end:
  182. free(fru_content);
  183. return ret;
  184. }
  185. static bool xilinx_detect_fru(u8 *buffer)
  186. {
  187. u8 checksum = 0;
  188. int i;
  189. checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
  190. if (checksum) {
  191. debug("%s Common header CRC FAIL\n", __func__);
  192. return false;
  193. }
  194. bool all_zeros = true;
  195. /* Checksum over all zeros is also zero that's why detect this case */
  196. for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
  197. if (buffer[i] != 0)
  198. all_zeros = false;
  199. }
  200. if (all_zeros)
  201. return false;
  202. debug("%s Common header CRC PASS\n", __func__);
  203. return true;
  204. }
  205. static int xilinx_read_eeprom_single(char *name,
  206. struct xilinx_board_description *desc)
  207. {
  208. int ret;
  209. struct udevice *dev;
  210. ofnode eeprom;
  211. u8 buffer[XILINX_I2C_DETECTION_BITS];
  212. eeprom = ofnode_get_aliases_node(name);
  213. if (!ofnode_valid(eeprom))
  214. return -ENODEV;
  215. ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
  216. if (ret)
  217. return ret;
  218. ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
  219. if (ret) {
  220. debug("%s: I2C EEPROM read failed\n", __func__);
  221. return ret;
  222. }
  223. debug("%s: i2c memory detected: %s\n", __func__, name);
  224. if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer))
  225. return xilinx_read_eeprom_fru(dev, name, desc);
  226. if (xilinx_detect_legacy(buffer))
  227. return xilinx_read_eeprom_legacy(dev, name, desc);
  228. return -ENODEV;
  229. }
  230. __maybe_unused int xilinx_read_eeprom(void)
  231. {
  232. int id;
  233. char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
  234. struct xilinx_board_description *desc;
  235. highest_id = dev_read_alias_highest_id("nvmem");
  236. /* No nvmem aliases present */
  237. if (highest_id < 0)
  238. return -EINVAL;
  239. board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
  240. if (!board_info)
  241. return -ENOMEM;
  242. debug("%s: Highest ID %d, board_info %p\n", __func__,
  243. highest_id, board_info);
  244. for (id = 0; id <= highest_id; id++) {
  245. snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
  246. /* Alloc structure */
  247. desc = &board_info[id];
  248. /* Ignoring return value for supporting multiple chips */
  249. xilinx_read_eeprom_single(name_buf, desc);
  250. }
  251. /*
  252. * Consider to clean board_info structure when board/cards are not
  253. * detected.
  254. */
  255. return 0;
  256. }
  257. #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
  258. void *board_fdt_blob_setup(void)
  259. {
  260. void *fdt_blob;
  261. if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
  262. !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
  263. !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
  264. fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
  265. if (fdt_magic(fdt_blob) == FDT_MAGIC)
  266. return fdt_blob;
  267. debug("DTB is not passed via %p\n", fdt_blob);
  268. }
  269. if (IS_ENABLED(CONFIG_SPL_BUILD)) {
  270. /*
  271. * FDT is at end of BSS unless it is in a different memory
  272. * region
  273. */
  274. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  275. fdt_blob = (ulong *)&_image_binary_end;
  276. else
  277. fdt_blob = (ulong *)&__bss_end;
  278. } else {
  279. /* FDT is at end of image */
  280. fdt_blob = (ulong *)&_end;
  281. }
  282. if (fdt_magic(fdt_blob) == FDT_MAGIC)
  283. return fdt_blob;
  284. debug("DTB is also not passed via %p\n", fdt_blob);
  285. return NULL;
  286. }
  287. #endif
  288. #if defined(CONFIG_BOARD_LATE_INIT)
  289. static int env_set_by_index(const char *name, int index, char *data)
  290. {
  291. char var[32];
  292. if (!index)
  293. sprintf(var, "board_%s", name);
  294. else
  295. sprintf(var, "card%d_%s", index, name);
  296. return env_set(var, data);
  297. }
  298. int board_late_init_xilinx(void)
  299. {
  300. u32 ret = 0;
  301. int i, id, macid = 0;
  302. struct xilinx_board_description *desc;
  303. phys_size_t bootm_size = gd->ram_size;
  304. if (!CONFIG_IS_ENABLED(MICROBLAZE)) {
  305. ulong scriptaddr;
  306. scriptaddr = env_get_hex("scriptaddr", 0);
  307. ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
  308. }
  309. if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE))
  310. bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
  311. ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
  312. ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
  313. ret |= env_set_addr("bootm_size", (void *)bootm_size);
  314. for (id = 0; id <= highest_id; id++) {
  315. desc = &board_info[id];
  316. if (desc && desc->header == EEPROM_HEADER_MAGIC) {
  317. if (desc->manufacturer[0])
  318. ret |= env_set_by_index("manufacturer", id,
  319. desc->manufacturer);
  320. if (desc->name[0])
  321. ret |= env_set_by_index("name", id,
  322. desc->name);
  323. if (desc->revision[0])
  324. ret |= env_set_by_index("rev", id,
  325. desc->revision);
  326. if (desc->serial[0])
  327. ret |= env_set_by_index("serial", id,
  328. desc->serial);
  329. if (!CONFIG_IS_ENABLED(NET))
  330. continue;
  331. for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
  332. if (!desc->mac_addr[i])
  333. continue;
  334. if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
  335. ret |= eth_env_set_enetaddr_by_index("eth",
  336. macid++, desc->mac_addr[i]);
  337. }
  338. }
  339. }
  340. if (ret)
  341. printf("%s: Saving run time variables FAILED\n", __func__);
  342. return 0;
  343. }
  344. #endif
  345. static char *board_name = DEVICE_TREE;
  346. int __maybe_unused board_fit_config_name_match(const char *name)
  347. {
  348. debug("%s: Check %s, default %s\n", __func__, name, board_name);
  349. if (!strcmp(name, board_name))
  350. return 0;
  351. return -1;
  352. }
  353. #if defined(CONFIG_DISPLAY_CPUINFO) && !defined(CONFIG_ARCH_ZYNQ)
  354. int print_cpuinfo(void)
  355. {
  356. struct udevice *soc;
  357. char name[SOC_MAX_STR_SIZE];
  358. int ret;
  359. ret = soc_get(&soc);
  360. if (ret) {
  361. printf("CPU: UNKNOWN\n");
  362. return 0;
  363. }
  364. ret = soc_get_family(soc, name, SOC_MAX_STR_SIZE);
  365. if (ret)
  366. printf("CPU: %s\n", name);
  367. ret = soc_get_revision(soc, name, SOC_MAX_STR_SIZE);
  368. if (ret)
  369. printf("Silicon: %s\n", name);
  370. return 0;
  371. }
  372. #endif
  373. #if CONFIG_IS_ENABLED(DTB_RESELECT)
  374. #define MAX_NAME_LENGTH 50
  375. char * __maybe_unused __weak board_name_decode(void)
  376. {
  377. char *board_local_name;
  378. struct xilinx_board_description *desc;
  379. int i, id;
  380. board_local_name = calloc(1, MAX_NAME_LENGTH);
  381. if (!board_info)
  382. return NULL;
  383. for (id = 0; id <= highest_id; id++) {
  384. desc = &board_info[id];
  385. /* No board description */
  386. if (!desc)
  387. goto error;
  388. /* Board is not detected */
  389. if (desc->header != EEPROM_HEADER_MAGIC)
  390. continue;
  391. /* The first string should be soc name */
  392. if (!id)
  393. strcat(board_local_name, CONFIG_SYS_BOARD);
  394. /*
  395. * For two purpose here:
  396. * soc_name- eg: zynqmp-
  397. * and between base board and CC eg: ..revA-sck...
  398. */
  399. strcat(board_local_name, "-");
  400. if (desc->name[0]) {
  401. /* For DT composition name needs to be lowercase */
  402. for (i = 0; i < sizeof(desc->name); i++)
  403. desc->name[i] = tolower(desc->name[i]);
  404. strcat(board_local_name, desc->name);
  405. }
  406. if (desc->revision[0]) {
  407. strcat(board_local_name, "-rev");
  408. /* And revision needs to be uppercase */
  409. for (i = 0; i < sizeof(desc->revision); i++)
  410. desc->revision[i] = toupper(desc->revision[i]);
  411. strcat(board_local_name, desc->revision);
  412. }
  413. }
  414. /*
  415. * Longer strings will end up with buffer overflow and potential
  416. * attacks that's why check it
  417. */
  418. if (strlen(board_local_name) >= MAX_NAME_LENGTH)
  419. panic("Board name can't be determined\n");
  420. if (strlen(board_local_name))
  421. return board_local_name;
  422. error:
  423. free(board_local_name);
  424. return NULL;
  425. }
  426. bool __maybe_unused __weak board_detection(void)
  427. {
  428. if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
  429. int ret;
  430. ret = xilinx_read_eeprom();
  431. return !ret ? true : false;
  432. }
  433. return false;
  434. }
  435. int embedded_dtb_select(void)
  436. {
  437. if (board_detection()) {
  438. char *board_local_name;
  439. board_local_name = board_name_decode();
  440. if (board_local_name) {
  441. board_name = board_local_name;
  442. printf("Detected name: %s\n", board_name);
  443. /* Time to change DTB on fly */
  444. /* Both ways should work here */
  445. /* fdtdec_resetup(&rescan); */
  446. fdtdec_setup();
  447. }
  448. }
  449. return 0;
  450. }
  451. #endif