zynqmp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2014 - 2015 Xilinx, Inc.
  4. * Michal Simek <michal.simek@xilinx.com>
  5. */
  6. #include <common.h>
  7. #include <sata.h>
  8. #include <ahci.h>
  9. #include <scsi.h>
  10. #include <malloc.h>
  11. #include <wdt.h>
  12. #include <asm/arch/clk.h>
  13. #include <asm/arch/hardware.h>
  14. #include <asm/arch/sys_proto.h>
  15. #include <asm/arch/psu_init_gpl.h>
  16. #include <asm/io.h>
  17. #include <dm/device.h>
  18. #include <dm/uclass.h>
  19. #include <usb.h>
  20. #include <dwc3-uboot.h>
  21. #include <zynqmppl.h>
  22. #include <i2c.h>
  23. #include <g_dnl.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT)
  26. static struct udevice *watchdog_dev;
  27. #endif
  28. #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
  29. !defined(CONFIG_SPL_BUILD)
  30. static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
  31. static const struct {
  32. u32 id;
  33. u32 ver;
  34. char *name;
  35. bool evexists;
  36. } zynqmp_devices[] = {
  37. {
  38. .id = 0x10,
  39. .name = "3eg",
  40. },
  41. {
  42. .id = 0x10,
  43. .ver = 0x2c,
  44. .name = "3cg",
  45. },
  46. {
  47. .id = 0x11,
  48. .name = "2eg",
  49. },
  50. {
  51. .id = 0x11,
  52. .ver = 0x2c,
  53. .name = "2cg",
  54. },
  55. {
  56. .id = 0x20,
  57. .name = "5ev",
  58. .evexists = 1,
  59. },
  60. {
  61. .id = 0x20,
  62. .ver = 0x100,
  63. .name = "5eg",
  64. .evexists = 1,
  65. },
  66. {
  67. .id = 0x20,
  68. .ver = 0x12c,
  69. .name = "5cg",
  70. },
  71. {
  72. .id = 0x21,
  73. .name = "4ev",
  74. .evexists = 1,
  75. },
  76. {
  77. .id = 0x21,
  78. .ver = 0x100,
  79. .name = "4eg",
  80. .evexists = 1,
  81. },
  82. {
  83. .id = 0x21,
  84. .ver = 0x12c,
  85. .name = "4cg",
  86. },
  87. {
  88. .id = 0x30,
  89. .name = "7ev",
  90. .evexists = 1,
  91. },
  92. {
  93. .id = 0x30,
  94. .ver = 0x100,
  95. .name = "7eg",
  96. .evexists = 1,
  97. },
  98. {
  99. .id = 0x30,
  100. .ver = 0x12c,
  101. .name = "7cg",
  102. },
  103. {
  104. .id = 0x38,
  105. .name = "9eg",
  106. },
  107. {
  108. .id = 0x38,
  109. .ver = 0x2c,
  110. .name = "9cg",
  111. },
  112. {
  113. .id = 0x39,
  114. .name = "6eg",
  115. },
  116. {
  117. .id = 0x39,
  118. .ver = 0x2c,
  119. .name = "6cg",
  120. },
  121. {
  122. .id = 0x40,
  123. .name = "11eg",
  124. },
  125. { /* For testing purpose only */
  126. .id = 0x50,
  127. .ver = 0x2c,
  128. .name = "15cg",
  129. },
  130. {
  131. .id = 0x50,
  132. .name = "15eg",
  133. },
  134. {
  135. .id = 0x58,
  136. .name = "19eg",
  137. },
  138. {
  139. .id = 0x59,
  140. .name = "17eg",
  141. },
  142. {
  143. .id = 0x61,
  144. .name = "21dr",
  145. },
  146. {
  147. .id = 0x63,
  148. .name = "23dr",
  149. },
  150. {
  151. .id = 0x65,
  152. .name = "25dr",
  153. },
  154. {
  155. .id = 0x64,
  156. .name = "27dr",
  157. },
  158. {
  159. .id = 0x60,
  160. .name = "28dr",
  161. },
  162. {
  163. .id = 0x62,
  164. .name = "29dr",
  165. },
  166. };
  167. #endif
  168. int chip_id(unsigned char id)
  169. {
  170. struct pt_regs regs;
  171. int val = -EINVAL;
  172. if (current_el() != 3) {
  173. regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
  174. regs.regs[1] = 0;
  175. regs.regs[2] = 0;
  176. regs.regs[3] = 0;
  177. smc_call(&regs);
  178. /*
  179. * SMC returns:
  180. * regs[0][31:0] = status of the operation
  181. * regs[0][63:32] = CSU.IDCODE register
  182. * regs[1][31:0] = CSU.version register
  183. * regs[1][63:32] = CSU.IDCODE2 register
  184. */
  185. switch (id) {
  186. case IDCODE:
  187. regs.regs[0] = upper_32_bits(regs.regs[0]);
  188. regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
  189. ZYNQMP_CSU_IDCODE_SVD_MASK;
  190. regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
  191. val = regs.regs[0];
  192. break;
  193. case VERSION:
  194. regs.regs[1] = lower_32_bits(regs.regs[1]);
  195. regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
  196. val = regs.regs[1];
  197. break;
  198. case IDCODE2:
  199. regs.regs[1] = lower_32_bits(regs.regs[1]);
  200. regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
  201. val = regs.regs[1];
  202. break;
  203. default:
  204. printf("%s, Invalid Req:0x%x\n", __func__, id);
  205. }
  206. } else {
  207. switch (id) {
  208. case IDCODE:
  209. val = readl(ZYNQMP_CSU_IDCODE_ADDR);
  210. val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
  211. ZYNQMP_CSU_IDCODE_SVD_MASK;
  212. val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
  213. break;
  214. case VERSION:
  215. val = readl(ZYNQMP_CSU_VER_ADDR);
  216. val &= ZYNQMP_CSU_SILICON_VER_MASK;
  217. break;
  218. default:
  219. printf("%s, Invalid Req:0x%x\n", __func__, id);
  220. }
  221. }
  222. return val;
  223. }
  224. #define ZYNQMP_VERSION_SIZE 9
  225. #define ZYNQMP_PL_STATUS_BIT 9
  226. #define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
  227. #define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
  228. #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
  229. !defined(CONFIG_SPL_BUILD)
  230. static char *zynqmp_get_silicon_idcode_name(void)
  231. {
  232. u32 i, id, ver;
  233. char *buf;
  234. static char name[ZYNQMP_VERSION_SIZE];
  235. id = chip_id(IDCODE);
  236. ver = chip_id(IDCODE2);
  237. for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
  238. if ((zynqmp_devices[i].id == id) &&
  239. (zynqmp_devices[i].ver == (ver &
  240. ZYNQMP_CSU_VERSION_MASK))) {
  241. strncat(name, "zu", 2);
  242. strncat(name, zynqmp_devices[i].name,
  243. ZYNQMP_VERSION_SIZE - 3);
  244. break;
  245. }
  246. }
  247. if (i >= ARRAY_SIZE(zynqmp_devices))
  248. return "unknown";
  249. if (!zynqmp_devices[i].evexists)
  250. return name;
  251. if (ver & ZYNQMP_PL_STATUS_MASK)
  252. return name;
  253. if (strstr(name, "eg") || strstr(name, "ev")) {
  254. buf = strstr(name, "e");
  255. *buf = '\0';
  256. }
  257. return name;
  258. }
  259. #endif
  260. int board_early_init_f(void)
  261. {
  262. int ret = 0;
  263. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CLK_ZYNQMP)
  264. zynqmp_pmufw_version();
  265. #endif
  266. #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
  267. ret = psu_init();
  268. #endif
  269. #if defined(CONFIG_WDT) && !defined(CONFIG_SPL_BUILD)
  270. /* bss is not cleared at time when watchdog_reset() is called */
  271. watchdog_dev = NULL;
  272. #endif
  273. return ret;
  274. }
  275. int board_init(void)
  276. {
  277. printf("EL Level:\tEL%d\n", current_el());
  278. #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
  279. !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
  280. defined(CONFIG_SPL_BUILD))
  281. if (current_el() != 3) {
  282. zynqmppl.name = zynqmp_get_silicon_idcode_name();
  283. printf("Chip ID:\t%s\n", zynqmppl.name);
  284. fpga_init();
  285. fpga_add(fpga_xilinx, &zynqmppl);
  286. }
  287. #endif
  288. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT)
  289. if (uclass_get_device(UCLASS_WDT, 0, &watchdog_dev)) {
  290. puts("Watchdog: Not found!\n");
  291. } else {
  292. wdt_start(watchdog_dev, 0, 0);
  293. puts("Watchdog: Started\n");
  294. }
  295. #endif
  296. return 0;
  297. }
  298. #ifdef CONFIG_WATCHDOG
  299. /* Called by macro WATCHDOG_RESET */
  300. void watchdog_reset(void)
  301. {
  302. # if !defined(CONFIG_SPL_BUILD)
  303. static ulong next_reset;
  304. ulong now;
  305. if (!watchdog_dev)
  306. return;
  307. now = timer_get_us();
  308. /* Do not reset the watchdog too often */
  309. if (now > next_reset) {
  310. wdt_reset(watchdog_dev);
  311. next_reset = now + 1000;
  312. }
  313. # endif
  314. }
  315. #endif
  316. int board_early_init_r(void)
  317. {
  318. u32 val;
  319. if (current_el() != 3)
  320. return 0;
  321. val = readl(&crlapb_base->timestamp_ref_ctrl);
  322. val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
  323. if (!val) {
  324. val = readl(&crlapb_base->timestamp_ref_ctrl);
  325. val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
  326. writel(val, &crlapb_base->timestamp_ref_ctrl);
  327. /* Program freq register in System counter */
  328. writel(zynqmp_get_system_timer_freq(),
  329. &iou_scntr_secure->base_frequency_id_register);
  330. /* And enable system counter */
  331. writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
  332. &iou_scntr_secure->counter_control_register);
  333. }
  334. return 0;
  335. }
  336. int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
  337. {
  338. #if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \
  339. defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \
  340. defined(CONFIG_ZYNQ_EEPROM_BUS)
  341. i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS);
  342. if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR,
  343. CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET,
  344. ethaddr, 6))
  345. printf("I2C EEPROM MAC address read failed\n");
  346. #endif
  347. return 0;
  348. }
  349. unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
  350. char * const argv[])
  351. {
  352. int ret = 0;
  353. if (current_el() > 1) {
  354. smp_kick_all_cpus();
  355. dcache_disable();
  356. armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
  357. ES_TO_AARCH64);
  358. } else {
  359. printf("FAIL: current EL is not above EL1\n");
  360. ret = EINVAL;
  361. }
  362. return ret;
  363. }
  364. #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
  365. int dram_init_banksize(void)
  366. {
  367. int ret;
  368. ret = fdtdec_setup_memory_banksize();
  369. if (ret)
  370. return ret;
  371. mem_map_fill();
  372. return 0;
  373. }
  374. int dram_init(void)
  375. {
  376. if (fdtdec_setup_memory_size() != 0)
  377. return -EINVAL;
  378. return 0;
  379. }
  380. #else
  381. int dram_init_banksize(void)
  382. {
  383. #if defined(CONFIG_NR_DRAM_BANKS)
  384. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  385. gd->bd->bi_dram[0].size = get_effective_memsize();
  386. #endif
  387. mem_map_fill();
  388. return 0;
  389. }
  390. int dram_init(void)
  391. {
  392. gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
  393. CONFIG_SYS_SDRAM_SIZE);
  394. return 0;
  395. }
  396. #endif
  397. void reset_cpu(ulong addr)
  398. {
  399. }
  400. static const struct {
  401. u32 bit;
  402. const char *name;
  403. } reset_reasons[] = {
  404. { RESET_REASON_DEBUG_SYS, "DEBUG" },
  405. { RESET_REASON_SOFT, "SOFT" },
  406. { RESET_REASON_SRST, "SRST" },
  407. { RESET_REASON_PSONLY, "PS-ONLY" },
  408. { RESET_REASON_PMU, "PMU" },
  409. { RESET_REASON_INTERNAL, "INTERNAL" },
  410. { RESET_REASON_EXTERNAL, "EXTERNAL" },
  411. {}
  412. };
  413. static u32 reset_reason(void)
  414. {
  415. u32 ret;
  416. int i;
  417. const char *reason = NULL;
  418. ret = readl(&crlapb_base->reset_reason);
  419. puts("Reset reason:\t");
  420. for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
  421. if (ret & reset_reasons[i].bit) {
  422. reason = reset_reasons[i].name;
  423. printf("%s ", reset_reasons[i].name);
  424. break;
  425. }
  426. }
  427. puts("\n");
  428. env_set("reset_reason", reason);
  429. writel(~0, &crlapb_base->reset_reason);
  430. return ret;
  431. }
  432. int board_late_init(void)
  433. {
  434. u32 reg = 0;
  435. u8 bootmode;
  436. struct udevice *dev;
  437. int bootseq = -1;
  438. int bootseq_len = 0;
  439. int env_targets_len = 0;
  440. const char *mode;
  441. char *new_targets;
  442. char *env_targets;
  443. int ret;
  444. if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
  445. debug("Saved variables - Skipping\n");
  446. return 0;
  447. }
  448. ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
  449. if (ret)
  450. return -EINVAL;
  451. if (reg >> BOOT_MODE_ALT_SHIFT)
  452. reg >>= BOOT_MODE_ALT_SHIFT;
  453. bootmode = reg & BOOT_MODES_MASK;
  454. puts("Bootmode: ");
  455. switch (bootmode) {
  456. case USB_MODE:
  457. puts("USB_MODE\n");
  458. mode = "usb";
  459. env_set("modeboot", "usb_dfu_spl");
  460. break;
  461. case JTAG_MODE:
  462. puts("JTAG_MODE\n");
  463. mode = "pxe dhcp";
  464. env_set("modeboot", "jtagboot");
  465. break;
  466. case QSPI_MODE_24BIT:
  467. case QSPI_MODE_32BIT:
  468. mode = "qspi0";
  469. puts("QSPI_MODE\n");
  470. env_set("modeboot", "qspiboot");
  471. break;
  472. case EMMC_MODE:
  473. puts("EMMC_MODE\n");
  474. mode = "mmc0";
  475. env_set("modeboot", "emmcboot");
  476. break;
  477. case SD_MODE:
  478. puts("SD_MODE\n");
  479. if (uclass_get_device_by_name(UCLASS_MMC,
  480. "sdhci@ff160000", &dev)) {
  481. puts("Boot from SD0 but without SD0 enabled!\n");
  482. return -1;
  483. }
  484. debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
  485. mode = "mmc";
  486. bootseq = dev->seq;
  487. env_set("modeboot", "sdboot");
  488. break;
  489. case SD1_LSHFT_MODE:
  490. puts("LVL_SHFT_");
  491. /* fall through */
  492. case SD_MODE1:
  493. puts("SD_MODE1\n");
  494. if (uclass_get_device_by_name(UCLASS_MMC,
  495. "sdhci@ff170000", &dev)) {
  496. puts("Boot from SD1 but without SD1 enabled!\n");
  497. return -1;
  498. }
  499. debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
  500. mode = "mmc";
  501. bootseq = dev->seq;
  502. env_set("modeboot", "sdboot");
  503. break;
  504. case NAND_MODE:
  505. puts("NAND_MODE\n");
  506. mode = "nand0";
  507. env_set("modeboot", "nandboot");
  508. break;
  509. default:
  510. mode = "";
  511. printf("Invalid Boot Mode:0x%x\n", bootmode);
  512. break;
  513. }
  514. if (bootseq >= 0) {
  515. bootseq_len = snprintf(NULL, 0, "%i", bootseq);
  516. debug("Bootseq len: %x\n", bootseq_len);
  517. }
  518. /*
  519. * One terminating char + one byte for space between mode
  520. * and default boot_targets
  521. */
  522. env_targets = env_get("boot_targets");
  523. if (env_targets)
  524. env_targets_len = strlen(env_targets);
  525. new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
  526. bootseq_len);
  527. if (!new_targets)
  528. return -ENOMEM;
  529. if (bootseq >= 0)
  530. sprintf(new_targets, "%s%x %s", mode, bootseq,
  531. env_targets ? env_targets : "");
  532. else
  533. sprintf(new_targets, "%s %s", mode,
  534. env_targets ? env_targets : "");
  535. env_set("boot_targets", new_targets);
  536. reset_reason();
  537. return 0;
  538. }
  539. int checkboard(void)
  540. {
  541. puts("Board: Xilinx ZynqMP\n");
  542. return 0;
  543. }