zynqmp.c 8.6 KB

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