zynqmp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * (C) Copyright 2014 - 2015 Xilinx, Inc.
  3. * Michal Simek <michal.simek@xilinx.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <sata.h>
  9. #include <ahci.h>
  10. #include <scsi.h>
  11. #include <malloc.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 <usb.h>
  18. #include <dwc3-uboot.h>
  19. #include <zynqmppl.h>
  20. #include <i2c.h>
  21. #include <g_dnl.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
  24. !defined(CONFIG_SPL_BUILD)
  25. static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
  26. static const struct {
  27. u32 id;
  28. u32 ver;
  29. char *name;
  30. bool evexists;
  31. } zynqmp_devices[] = {
  32. {
  33. .id = 0x10,
  34. .name = "3eg",
  35. },
  36. {
  37. .id = 0x10,
  38. .ver = 0x2c,
  39. .name = "3cg",
  40. },
  41. {
  42. .id = 0x11,
  43. .name = "2eg",
  44. },
  45. {
  46. .id = 0x11,
  47. .ver = 0x2c,
  48. .name = "2cg",
  49. },
  50. {
  51. .id = 0x20,
  52. .name = "5ev",
  53. .evexists = 1,
  54. },
  55. {
  56. .id = 0x20,
  57. .ver = 0x100,
  58. .name = "5eg",
  59. .evexists = 1,
  60. },
  61. {
  62. .id = 0x20,
  63. .ver = 0x12c,
  64. .name = "5cg",
  65. },
  66. {
  67. .id = 0x21,
  68. .name = "4ev",
  69. .evexists = 1,
  70. },
  71. {
  72. .id = 0x21,
  73. .ver = 0x100,
  74. .name = "4eg",
  75. .evexists = 1,
  76. },
  77. {
  78. .id = 0x21,
  79. .ver = 0x12c,
  80. .name = "4cg",
  81. },
  82. {
  83. .id = 0x30,
  84. .name = "7ev",
  85. .evexists = 1,
  86. },
  87. {
  88. .id = 0x30,
  89. .ver = 0x100,
  90. .name = "7eg",
  91. .evexists = 1,
  92. },
  93. {
  94. .id = 0x30,
  95. .ver = 0x12c,
  96. .name = "7cg",
  97. },
  98. {
  99. .id = 0x38,
  100. .name = "9eg",
  101. },
  102. {
  103. .id = 0x38,
  104. .ver = 0x2c,
  105. .name = "9cg",
  106. },
  107. {
  108. .id = 0x39,
  109. .name = "6eg",
  110. },
  111. {
  112. .id = 0x39,
  113. .ver = 0x2c,
  114. .name = "6cg",
  115. },
  116. {
  117. .id = 0x40,
  118. .name = "11eg",
  119. },
  120. { /* For testing purpose only */
  121. .id = 0x50,
  122. .ver = 0x2c,
  123. .name = "15cg",
  124. },
  125. {
  126. .id = 0x50,
  127. .name = "15eg",
  128. },
  129. {
  130. .id = 0x58,
  131. .name = "19eg",
  132. },
  133. {
  134. .id = 0x59,
  135. .name = "17eg",
  136. },
  137. {
  138. .id = 0x61,
  139. .name = "21dr",
  140. },
  141. {
  142. .id = 0x63,
  143. .name = "23dr",
  144. },
  145. {
  146. .id = 0x65,
  147. .name = "25dr",
  148. },
  149. {
  150. .id = 0x64,
  151. .name = "27dr",
  152. },
  153. {
  154. .id = 0x60,
  155. .name = "28dr",
  156. },
  157. {
  158. .id = 0x62,
  159. .name = "29dr",
  160. },
  161. };
  162. #endif
  163. int chip_id(unsigned char id)
  164. {
  165. struct pt_regs regs;
  166. int val = -EINVAL;
  167. if (current_el() != 3) {
  168. regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
  169. regs.regs[1] = 0;
  170. regs.regs[2] = 0;
  171. regs.regs[3] = 0;
  172. smc_call(&regs);
  173. /*
  174. * SMC returns:
  175. * regs[0][31:0] = status of the operation
  176. * regs[0][63:32] = CSU.IDCODE register
  177. * regs[1][31:0] = CSU.version register
  178. * regs[1][63:32] = CSU.IDCODE2 register
  179. */
  180. switch (id) {
  181. case IDCODE:
  182. regs.regs[0] = upper_32_bits(regs.regs[0]);
  183. regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
  184. ZYNQMP_CSU_IDCODE_SVD_MASK;
  185. regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
  186. val = regs.regs[0];
  187. break;
  188. case VERSION:
  189. regs.regs[1] = lower_32_bits(regs.regs[1]);
  190. regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
  191. val = regs.regs[1];
  192. break;
  193. case IDCODE2:
  194. regs.regs[1] = lower_32_bits(regs.regs[1]);
  195. regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
  196. val = regs.regs[1];
  197. break;
  198. default:
  199. printf("%s, Invalid Req:0x%x\n", __func__, id);
  200. }
  201. } else {
  202. switch (id) {
  203. case IDCODE:
  204. val = readl(ZYNQMP_CSU_IDCODE_ADDR);
  205. val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
  206. ZYNQMP_CSU_IDCODE_SVD_MASK;
  207. val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
  208. break;
  209. case VERSION:
  210. val = readl(ZYNQMP_CSU_VER_ADDR);
  211. val &= ZYNQMP_CSU_SILICON_VER_MASK;
  212. break;
  213. default:
  214. printf("%s, Invalid Req:0x%x\n", __func__, id);
  215. }
  216. }
  217. return val;
  218. }
  219. #define ZYNQMP_VERSION_SIZE 9
  220. #define ZYNQMP_PL_STATUS_BIT 9
  221. #define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
  222. #define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
  223. #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
  224. !defined(CONFIG_SPL_BUILD)
  225. static char *zynqmp_get_silicon_idcode_name(void)
  226. {
  227. u32 i, id, ver;
  228. char *buf;
  229. static char name[ZYNQMP_VERSION_SIZE];
  230. id = chip_id(IDCODE);
  231. ver = chip_id(IDCODE2);
  232. for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
  233. if ((zynqmp_devices[i].id == id) &&
  234. (zynqmp_devices[i].ver == (ver &
  235. ZYNQMP_CSU_VERSION_MASK))) {
  236. strncat(name, "zu", 2);
  237. strncat(name, zynqmp_devices[i].name,
  238. ZYNQMP_VERSION_SIZE - 3);
  239. break;
  240. }
  241. }
  242. if (i >= ARRAY_SIZE(zynqmp_devices))
  243. return "unknown";
  244. if (!zynqmp_devices[i].evexists)
  245. return name;
  246. if (ver & ZYNQMP_PL_STATUS_MASK)
  247. return name;
  248. if (strstr(name, "eg") || strstr(name, "ev")) {
  249. buf = strstr(name, "e");
  250. *buf = '\0';
  251. }
  252. return name;
  253. }
  254. #endif
  255. int board_early_init_f(void)
  256. {
  257. int ret = 0;
  258. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CLK_ZYNQMP)
  259. zynqmp_pmufw_version();
  260. #endif
  261. #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
  262. ret = psu_init();
  263. #endif
  264. return ret;
  265. }
  266. int board_init(void)
  267. {
  268. printf("EL Level:\tEL%d\n", current_el());
  269. #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
  270. !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
  271. defined(CONFIG_SPL_BUILD))
  272. if (current_el() != 3) {
  273. zynqmppl.name = zynqmp_get_silicon_idcode_name();
  274. printf("Chip ID:\t%s\n", zynqmppl.name);
  275. fpga_init();
  276. fpga_add(fpga_xilinx, &zynqmppl);
  277. }
  278. #endif
  279. return 0;
  280. }
  281. int board_early_init_r(void)
  282. {
  283. u32 val;
  284. if (current_el() != 3)
  285. return 0;
  286. val = readl(&crlapb_base->timestamp_ref_ctrl);
  287. val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
  288. if (!val) {
  289. val = readl(&crlapb_base->timestamp_ref_ctrl);
  290. val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
  291. writel(val, &crlapb_base->timestamp_ref_ctrl);
  292. /* Program freq register in System counter */
  293. writel(zynqmp_get_system_timer_freq(),
  294. &iou_scntr_secure->base_frequency_id_register);
  295. /* And enable system counter */
  296. writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
  297. &iou_scntr_secure->counter_control_register);
  298. }
  299. return 0;
  300. }
  301. int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
  302. {
  303. #if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \
  304. defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \
  305. defined(CONFIG_ZYNQ_EEPROM_BUS)
  306. i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS);
  307. if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR,
  308. CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET,
  309. ethaddr, 6))
  310. printf("I2C EEPROM MAC address read failed\n");
  311. #endif
  312. return 0;
  313. }
  314. unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
  315. char * const argv[])
  316. {
  317. int ret = 0;
  318. if (current_el() > 1) {
  319. smp_kick_all_cpus();
  320. dcache_disable();
  321. armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
  322. ES_TO_AARCH64);
  323. } else {
  324. printf("FAIL: current EL is not above EL1\n");
  325. ret = EINVAL;
  326. }
  327. return ret;
  328. }
  329. #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
  330. int dram_init_banksize(void)
  331. {
  332. return fdtdec_setup_memory_banksize();
  333. }
  334. int dram_init(void)
  335. {
  336. if (fdtdec_setup_memory_size() != 0)
  337. return -EINVAL;
  338. return 0;
  339. }
  340. #else
  341. int dram_init(void)
  342. {
  343. gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
  344. return 0;
  345. }
  346. #endif
  347. void reset_cpu(ulong addr)
  348. {
  349. }
  350. int board_late_init(void)
  351. {
  352. u32 reg = 0;
  353. u8 bootmode;
  354. const char *mode;
  355. char *new_targets;
  356. char *env_targets;
  357. int ret;
  358. if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
  359. debug("Saved variables - Skipping\n");
  360. return 0;
  361. }
  362. ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
  363. if (ret)
  364. return -EINVAL;
  365. if (reg >> BOOT_MODE_ALT_SHIFT)
  366. reg >>= BOOT_MODE_ALT_SHIFT;
  367. bootmode = reg & BOOT_MODES_MASK;
  368. puts("Bootmode: ");
  369. switch (bootmode) {
  370. case USB_MODE:
  371. puts("USB_MODE\n");
  372. mode = "usb";
  373. env_set("modeboot", "usb_dfu_spl");
  374. break;
  375. case JTAG_MODE:
  376. puts("JTAG_MODE\n");
  377. mode = "pxe dhcp";
  378. env_set("modeboot", "jtagboot");
  379. break;
  380. case QSPI_MODE_24BIT:
  381. case QSPI_MODE_32BIT:
  382. mode = "qspi0";
  383. puts("QSPI_MODE\n");
  384. env_set("modeboot", "qspiboot");
  385. break;
  386. case EMMC_MODE:
  387. puts("EMMC_MODE\n");
  388. mode = "mmc0";
  389. env_set("modeboot", "emmcboot");
  390. break;
  391. case SD_MODE:
  392. puts("SD_MODE\n");
  393. mode = "mmc0";
  394. env_set("modeboot", "sdboot");
  395. break;
  396. case SD1_LSHFT_MODE:
  397. puts("LVL_SHFT_");
  398. /* fall through */
  399. case SD_MODE1:
  400. puts("SD_MODE1\n");
  401. #if defined(CONFIG_ZYNQ_SDHCI0) && defined(CONFIG_ZYNQ_SDHCI1)
  402. mode = "mmc1";
  403. env_set("sdbootdev", "1");
  404. #else
  405. mode = "mmc0";
  406. #endif
  407. env_set("modeboot", "sdboot");
  408. break;
  409. case NAND_MODE:
  410. puts("NAND_MODE\n");
  411. mode = "nand0";
  412. env_set("modeboot", "nandboot");
  413. break;
  414. default:
  415. mode = "";
  416. printf("Invalid Boot Mode:0x%x\n", bootmode);
  417. break;
  418. }
  419. /*
  420. * One terminating char + one byte for space between mode
  421. * and default boot_targets
  422. */
  423. env_targets = env_get("boot_targets");
  424. if (env_targets) {
  425. new_targets = calloc(1, strlen(mode) +
  426. strlen(env_targets) + 2);
  427. sprintf(new_targets, "%s %s", mode, env_targets);
  428. } else {
  429. new_targets = calloc(1, strlen(mode) + 2);
  430. sprintf(new_targets, "%s", mode);
  431. }
  432. env_set("boot_targets", new_targets);
  433. return 0;
  434. }
  435. int checkboard(void)
  436. {
  437. puts("Board: Xilinx ZynqMP\n");
  438. return 0;
  439. }
  440. #ifdef CONFIG_USB_DWC3
  441. static struct dwc3_device dwc3_device_data0 = {
  442. .maximum_speed = USB_SPEED_HIGH,
  443. .base = ZYNQMP_USB0_XHCI_BASEADDR,
  444. .dr_mode = USB_DR_MODE_PERIPHERAL,
  445. .index = 0,
  446. };
  447. static struct dwc3_device dwc3_device_data1 = {
  448. .maximum_speed = USB_SPEED_HIGH,
  449. .base = ZYNQMP_USB1_XHCI_BASEADDR,
  450. .dr_mode = USB_DR_MODE_PERIPHERAL,
  451. .index = 1,
  452. };
  453. int usb_gadget_handle_interrupts(int index)
  454. {
  455. dwc3_uboot_handle_interrupt(index);
  456. return 0;
  457. }
  458. int board_usb_init(int index, enum usb_init_type init)
  459. {
  460. debug("%s: index %x\n", __func__, index);
  461. #if defined(CONFIG_USB_GADGET_DOWNLOAD)
  462. g_dnl_set_serialnumber(CONFIG_SYS_CONFIG_NAME);
  463. #endif
  464. switch (index) {
  465. case 0:
  466. return dwc3_uboot_init(&dwc3_device_data0);
  467. case 1:
  468. return dwc3_uboot_init(&dwc3_device_data1);
  469. };
  470. return -1;
  471. }
  472. int board_usb_cleanup(int index, enum usb_init_type init)
  473. {
  474. dwc3_uboot_exit(index);
  475. return 0;
  476. }
  477. #endif