misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (C) 2012 Altera Corporation <www.altera.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <libfdt.h>
  11. #include <altera.h>
  12. #include <miiphy.h>
  13. #include <netdev.h>
  14. #include <watchdog.h>
  15. #include <asm/arch/reset_manager.h>
  16. #include <asm/arch/scan_manager.h>
  17. #include <asm/arch/system_manager.h>
  18. #include <asm/arch/dwmmc.h>
  19. #include <asm/arch/nic301.h>
  20. #include <asm/arch/scu.h>
  21. #include <asm/pl310.h>
  22. #include <dt-bindings/reset/altr,rst-mgr.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. static struct pl310_regs *const pl310 =
  25. (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
  26. static struct socfpga_system_manager *sysmgr_regs =
  27. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  28. static struct socfpga_reset_manager *reset_manager_base =
  29. (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_ADDRESS;
  30. static struct nic301_registers *nic301_regs =
  31. (struct nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
  32. static struct scu_registers *scu_regs =
  33. (struct scu_registers *)SOCFPGA_MPUSCU_ADDRESS;
  34. int dram_init(void)
  35. {
  36. gd->ram_size = get_ram_size((long *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE);
  37. return 0;
  38. }
  39. void enable_caches(void)
  40. {
  41. #ifndef CONFIG_SYS_ICACHE_OFF
  42. icache_enable();
  43. #endif
  44. #ifndef CONFIG_SYS_DCACHE_OFF
  45. dcache_enable();
  46. #endif
  47. }
  48. void v7_outer_cache_enable(void)
  49. {
  50. /* Disable the L2 cache */
  51. clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
  52. /* enable BRESP, instruction and data prefetch, full line of zeroes */
  53. setbits_le32(&pl310->pl310_aux_ctrl,
  54. L310_AUX_CTRL_DATA_PREFETCH_MASK |
  55. L310_AUX_CTRL_INST_PREFETCH_MASK |
  56. L310_SHARED_ATT_OVERRIDE_ENABLE);
  57. /* Enable the L2 cache */
  58. setbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
  59. }
  60. void v7_outer_cache_disable(void)
  61. {
  62. /* Disable the L2 cache */
  63. clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
  64. }
  65. /*
  66. * DesignWare Ethernet initialization
  67. */
  68. #ifdef CONFIG_ETH_DESIGNWARE
  69. static void dwmac_deassert_reset(const unsigned int of_reset_id)
  70. {
  71. u32 physhift, reset;
  72. if (of_reset_id == EMAC0_RESET) {
  73. physhift = SYSMGR_EMACGRP_CTRL_PHYSEL0_LSB;
  74. reset = SOCFPGA_RESET(EMAC0);
  75. } else if (of_reset_id == EMAC1_RESET) {
  76. physhift = SYSMGR_EMACGRP_CTRL_PHYSEL1_LSB;
  77. reset = SOCFPGA_RESET(EMAC1);
  78. } else {
  79. printf("GMAC: Invalid reset ID (%i)!\n", of_reset_id);
  80. return;
  81. }
  82. /* Clearing emac0 PHY interface select to 0 */
  83. clrbits_le32(&sysmgr_regs->emacgrp_ctrl,
  84. SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift);
  85. /* configure to PHY interface select choosed */
  86. setbits_le32(&sysmgr_regs->emacgrp_ctrl,
  87. SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII << physhift);
  88. /* Release the EMAC controller from reset */
  89. socfpga_per_reset(reset, 0);
  90. }
  91. static int socfpga_eth_reset(void)
  92. {
  93. const void *fdt = gd->fdt_blob;
  94. struct fdtdec_phandle_args args;
  95. int nodes[2]; /* Max. two GMACs */
  96. int ret, count;
  97. int i, node;
  98. /* Put both GMACs into RESET state. */
  99. socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
  100. socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
  101. count = fdtdec_find_aliases_for_id(fdt, "ethernet",
  102. COMPAT_ALTERA_SOCFPGA_DWMAC,
  103. nodes, ARRAY_SIZE(nodes));
  104. for (i = 0; i < count; i++) {
  105. node = nodes[i];
  106. if (node <= 0)
  107. continue;
  108. ret = fdtdec_parse_phandle_with_args(fdt, node, "resets",
  109. "#reset-cells", 1, 0,
  110. &args);
  111. if (ret || (args.args_count != 1)) {
  112. debug("GMAC%i: Failed to parse DT 'resets'!\n", i);
  113. continue;
  114. }
  115. dwmac_deassert_reset(args.args[0]);
  116. }
  117. return 0;
  118. }
  119. #else
  120. static int socfpga_eth_reset(void)
  121. {
  122. return 0
  123. };
  124. #endif
  125. struct {
  126. const char *mode;
  127. const char *name;
  128. } bsel_str[] = {
  129. { "rsvd", "Reserved", },
  130. { "fpga", "FPGA (HPS2FPGA Bridge)", },
  131. { "nand", "NAND Flash (1.8V)", },
  132. { "nand", "NAND Flash (3.0V)", },
  133. { "sd", "SD/MMC External Transceiver (1.8V)", },
  134. { "sd", "SD/MMC Internal Transceiver (3.0V)", },
  135. { "qspi", "QSPI Flash (1.8V)", },
  136. { "qspi", "QSPI Flash (3.0V)", },
  137. };
  138. static const struct {
  139. const u16 pn;
  140. const char *name;
  141. const char *var;
  142. } const socfpga_fpga_model[] = {
  143. /* Cyclone V E */
  144. { 0x2b15, "Cyclone V, E/A2", "cv_e_a2" },
  145. { 0x2b05, "Cyclone V, E/A4", "cv_e_a4" },
  146. { 0x2b22, "Cyclone V, E/A5", "cv_e_a5" },
  147. { 0x2b13, "Cyclone V, E/A7", "cv_e_a7" },
  148. { 0x2b14, "Cyclone V, E/A9", "cv_e_a9" },
  149. /* Cyclone V GX/GT */
  150. { 0x2b01, "Cyclone V, GX/C3", "cv_gx_c3" },
  151. { 0x2b12, "Cyclone V, GX/C4", "cv_gx_c4" },
  152. { 0x2b02, "Cyclone V, GX/C5 or GT/D5", "cv_gx_c5" },
  153. { 0x2b03, "Cyclone V, GX/C7 or GT/D7", "cv_gx_c7" },
  154. { 0x2b04, "Cyclone V, GX/C9 or GT/D9", "cv_gx_c9" },
  155. /* Cyclone V SE/SX/ST */
  156. { 0x2d11, "Cyclone V, SE/A2 or SX/C2", "cv_se_a2" },
  157. { 0x2d01, "Cyclone V, SE/A4 or SX/C4", "cv_se_a4" },
  158. { 0x2d12, "Cyclone V, SE/A5 or SX/C5 or ST/D5", "cv_se_a5" },
  159. { 0x2d02, "Cyclone V, SE/A6 or SX/C6 or ST/D6", "cv_se_a6" },
  160. /* Arria V */
  161. { 0x2d03, "Arria V, D5", "av_d5" },
  162. };
  163. static int socfpga_fpga_id(const bool print_id)
  164. {
  165. const u32 altera_mi = 0x6e;
  166. const u32 id = scan_mgr_get_fpga_id();
  167. const u32 lsb = id & 0x00000001;
  168. const u32 mi = (id >> 1) & 0x000007ff;
  169. const u32 pn = (id >> 12) & 0x0000ffff;
  170. const u32 version = (id >> 28) & 0x0000000f;
  171. int i;
  172. if ((mi != altera_mi) || (lsb != 1)) {
  173. printf("FPGA: Not Altera chip ID\n");
  174. return -EINVAL;
  175. }
  176. for (i = 0; i < ARRAY_SIZE(socfpga_fpga_model); i++)
  177. if (pn == socfpga_fpga_model[i].pn)
  178. break;
  179. if (i == ARRAY_SIZE(socfpga_fpga_model)) {
  180. printf("FPGA: Unknown Altera chip, ID 0x%08x\n", id);
  181. return -EINVAL;
  182. }
  183. if (print_id)
  184. printf("FPGA: Altera %s, version 0x%01x\n",
  185. socfpga_fpga_model[i].name, version);
  186. return i;
  187. }
  188. /*
  189. * Print CPU information
  190. */
  191. #if defined(CONFIG_DISPLAY_CPUINFO)
  192. int print_cpuinfo(void)
  193. {
  194. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  195. puts("CPU: Altera SoCFPGA Platform\n");
  196. socfpga_fpga_id(1);
  197. printf("BOOT: %s\n", bsel_str[bsel].name);
  198. return 0;
  199. }
  200. #endif
  201. #ifdef CONFIG_ARCH_MISC_INIT
  202. int arch_misc_init(void)
  203. {
  204. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  205. const int fpga_id = socfpga_fpga_id(0);
  206. setenv("bootmode", bsel_str[bsel].mode);
  207. if (fpga_id >= 0)
  208. setenv("fpgatype", socfpga_fpga_model[fpga_id].var);
  209. return socfpga_eth_reset();
  210. }
  211. #endif
  212. #if defined(CONFIG_SYS_CONSOLE_IS_IN_ENV) && \
  213. defined(CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE)
  214. int overwrite_console(void)
  215. {
  216. return 0;
  217. }
  218. #endif
  219. #ifdef CONFIG_FPGA
  220. /*
  221. * FPGA programming support for SoC FPGA Cyclone V
  222. */
  223. static Altera_desc altera_fpga[] = {
  224. {
  225. /* Family */
  226. Altera_SoCFPGA,
  227. /* Interface type */
  228. fast_passive_parallel,
  229. /* No limitation as additional data will be ignored */
  230. -1,
  231. /* No device function table */
  232. NULL,
  233. /* Base interface address specified in driver */
  234. NULL,
  235. /* No cookie implementation */
  236. 0
  237. },
  238. };
  239. /* add device descriptor to FPGA device table */
  240. static void socfpga_fpga_add(void)
  241. {
  242. int i;
  243. fpga_init();
  244. for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
  245. fpga_add(fpga_altera, &altera_fpga[i]);
  246. }
  247. #else
  248. static inline void socfpga_fpga_add(void) {}
  249. #endif
  250. int arch_cpu_init(void)
  251. {
  252. #ifdef CONFIG_HW_WATCHDOG
  253. /*
  254. * In case the watchdog is enabled, make sure to (re-)configure it
  255. * so that the defined timeout is valid. Otherwise the SPL (Perloader)
  256. * timeout value is still active which might too short for Linux
  257. * booting.
  258. */
  259. hw_watchdog_init();
  260. #else
  261. /*
  262. * If the HW watchdog is NOT enabled, make sure it is not running,
  263. * for example because it was enabled in the preloader. This might
  264. * trigger a watchdog-triggered reboot of Linux kernel later.
  265. * Toggle watchdog reset, so watchdog in not running state.
  266. */
  267. socfpga_per_reset(SOCFPGA_RESET(L4WD0), 1);
  268. socfpga_per_reset(SOCFPGA_RESET(L4WD0), 0);
  269. #endif
  270. return 0;
  271. }
  272. /*
  273. * Convert all NIC-301 AMBA slaves from secure to non-secure
  274. */
  275. static void socfpga_nic301_slave_ns(void)
  276. {
  277. writel(0x1, &nic301_regs->lwhps2fpgaregs);
  278. writel(0x1, &nic301_regs->hps2fpgaregs);
  279. writel(0x1, &nic301_regs->acp);
  280. writel(0x1, &nic301_regs->rom);
  281. writel(0x1, &nic301_regs->ocram);
  282. writel(0x1, &nic301_regs->sdrdata);
  283. }
  284. static uint32_t iswgrp_handoff[8];
  285. int arch_early_init_r(void)
  286. {
  287. int i;
  288. /*
  289. * Write magic value into magic register to unlock support for
  290. * issuing warm reset. The ancient kernel code expects this
  291. * value to be written into the register by the bootloader, so
  292. * to support that old code, we write it here instead of in the
  293. * reset_cpu() function just before reseting the CPU.
  294. */
  295. writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
  296. for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
  297. iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
  298. socfpga_bridges_reset(1);
  299. socfpga_nic301_slave_ns();
  300. /*
  301. * Private components security:
  302. * U-Boot : configure private timer, global timer and cpu component
  303. * access as non secure for kernel stage (as required by Linux)
  304. */
  305. setbits_le32(&scu_regs->sacr, 0xfff);
  306. /* Configure the L2 controller to make SDRAM start at 0 */
  307. #ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
  308. writel(0x2, &nic301_regs->remap);
  309. #else
  310. writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
  311. writel(0x1, &pl310->pl310_addr_filter_start);
  312. #endif
  313. /* Add device descriptor to FPGA device table */
  314. socfpga_fpga_add();
  315. #ifdef CONFIG_DESIGNWARE_SPI
  316. /* Get Designware SPI controller out of reset */
  317. socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
  318. socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
  319. #endif
  320. #ifdef CONFIG_NAND_DENALI
  321. socfpga_per_reset(SOCFPGA_RESET(NAND), 0);
  322. #endif
  323. return 0;
  324. }
  325. static void socfpga_sdram_apply_static_cfg(void)
  326. {
  327. const uint32_t staticcfg = SOCFPGA_SDR_ADDRESS + 0x505c;
  328. const uint32_t applymask = 0x8;
  329. uint32_t val = readl(staticcfg) | applymask;
  330. /*
  331. * SDRAM staticcfg register specific:
  332. * When applying the register setting, the CPU must not access
  333. * SDRAM. Luckily for us, we can abuse i-cache here to help us
  334. * circumvent the SDRAM access issue. The idea is to make sure
  335. * that the code is in one full i-cache line by branching past
  336. * it and back. Once it is in the i-cache, we execute the core
  337. * of the code and apply the register settings.
  338. *
  339. * The code below uses 7 instructions, while the Cortex-A9 has
  340. * 32-byte cachelines, thus the limit is 8 instructions total.
  341. */
  342. asm volatile(
  343. ".align 5 \n"
  344. " b 2f \n"
  345. "1: str %0, [%1] \n"
  346. " dsb \n"
  347. " isb \n"
  348. " b 3f \n"
  349. "2: b 1b \n"
  350. "3: nop \n"
  351. : : "r"(val), "r"(staticcfg) : "memory", "cc");
  352. }
  353. int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  354. {
  355. if (argc != 2)
  356. return CMD_RET_USAGE;
  357. argv++;
  358. switch (*argv[0]) {
  359. case 'e': /* Enable */
  360. writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
  361. socfpga_sdram_apply_static_cfg();
  362. writel(iswgrp_handoff[3], SOCFPGA_SDR_ADDRESS + 0x5080);
  363. writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
  364. writel(iswgrp_handoff[1], &nic301_regs->remap);
  365. break;
  366. case 'd': /* Disable */
  367. writel(0, &sysmgr_regs->fpgaintfgrp_module);
  368. writel(0, SOCFPGA_SDR_ADDRESS + 0x5080);
  369. socfpga_sdram_apply_static_cfg();
  370. writel(0, &reset_manager_base->brg_mod_reset);
  371. writel(1, &nic301_regs->remap);
  372. break;
  373. default:
  374. return CMD_RET_USAGE;
  375. }
  376. return 0;
  377. }
  378. U_BOOT_CMD(
  379. bridge, 2, 1, do_bridge,
  380. "SoCFPGA HPS FPGA bridge control",
  381. "enable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  382. "bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  383. ""
  384. );