misc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <altera.h>
  9. #include <miiphy.h>
  10. #include <netdev.h>
  11. #include <watchdog.h>
  12. #include <asm/arch/reset_manager.h>
  13. #include <asm/arch/system_manager.h>
  14. #include <asm/arch/dwmmc.h>
  15. #include <asm/arch/nic301.h>
  16. #include <asm/arch/scu.h>
  17. #include <asm/pl310.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. static struct pl310_regs *const pl310 =
  20. (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
  21. static struct socfpga_system_manager *sysmgr_regs =
  22. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  23. static struct socfpga_reset_manager *reset_manager_base =
  24. (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_ADDRESS;
  25. static struct nic301_registers *nic301_regs =
  26. (struct nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
  27. static struct scu_registers *scu_regs =
  28. (struct scu_registers *)SOCFPGA_MPUSCU_ADDRESS;
  29. int dram_init(void)
  30. {
  31. gd->ram_size = get_ram_size((long *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE);
  32. return 0;
  33. }
  34. void enable_caches(void)
  35. {
  36. #ifndef CONFIG_SYS_ICACHE_OFF
  37. icache_enable();
  38. #endif
  39. #ifndef CONFIG_SYS_DCACHE_OFF
  40. dcache_enable();
  41. #endif
  42. }
  43. /*
  44. * DesignWare Ethernet initialization
  45. */
  46. #ifdef CONFIG_ETH_DESIGNWARE
  47. int cpu_eth_init(bd_t *bis)
  48. {
  49. #if CONFIG_EMAC_BASE == SOCFPGA_EMAC0_ADDRESS
  50. const int physhift = SYSMGR_EMACGRP_CTRL_PHYSEL0_LSB;
  51. const u32 reset = SOCFPGA_RESET(EMAC0);
  52. #elif CONFIG_EMAC_BASE == SOCFPGA_EMAC1_ADDRESS
  53. const int physhift = SYSMGR_EMACGRP_CTRL_PHYSEL1_LSB;
  54. const u32 reset = SOCFPGA_RESET(EMAC1);
  55. #else
  56. #error "Incorrect CONFIG_EMAC_BASE value!"
  57. #endif
  58. /* Initialize EMAC. This needs to be done at least once per boot. */
  59. /*
  60. * Putting the EMAC controller to reset when configuring the PHY
  61. * interface select at System Manager
  62. */
  63. socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
  64. socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
  65. /* Clearing emac0 PHY interface select to 0 */
  66. clrbits_le32(&sysmgr_regs->emacgrp_ctrl,
  67. SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift);
  68. /* configure to PHY interface select choosed */
  69. setbits_le32(&sysmgr_regs->emacgrp_ctrl,
  70. SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII << physhift);
  71. /* Release the EMAC controller from reset */
  72. socfpga_per_reset(reset, 0);
  73. return 0;
  74. }
  75. #endif
  76. #ifdef CONFIG_DWMMC
  77. /*
  78. * Initializes MMC controllers.
  79. * to override, implement board_mmc_init()
  80. */
  81. int cpu_mmc_init(bd_t *bis)
  82. {
  83. return socfpga_dwmmc_init(SOCFPGA_SDMMC_ADDRESS,
  84. CONFIG_HPS_SDMMC_BUSWIDTH, 0);
  85. }
  86. #endif
  87. struct {
  88. const char *mode;
  89. const char *name;
  90. } bsel_str[] = {
  91. { "rsvd", "Reserved", },
  92. { "fpga", "FPGA (HPS2FPGA Bridge)", },
  93. { "nand", "NAND Flash (1.8V)", },
  94. { "nand", "NAND Flash (3.0V)", },
  95. { "sd", "SD/MMC External Transceiver (1.8V)", },
  96. { "sd", "SD/MMC Internal Transceiver (3.0V)", },
  97. { "qspi", "QSPI Flash (1.8V)", },
  98. { "qspi", "QSPI Flash (3.0V)", },
  99. };
  100. /*
  101. * Print CPU information
  102. */
  103. #if defined(CONFIG_DISPLAY_CPUINFO)
  104. int print_cpuinfo(void)
  105. {
  106. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  107. puts("CPU: Altera SoCFPGA Platform\n");
  108. printf("BOOT: %s\n", bsel_str[bsel].name);
  109. return 0;
  110. }
  111. #endif
  112. #ifdef CONFIG_ARCH_MISC_INIT
  113. int arch_misc_init(void)
  114. {
  115. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  116. setenv("bootmode", bsel_str[bsel].mode);
  117. return 0;
  118. }
  119. #endif
  120. #if defined(CONFIG_SYS_CONSOLE_IS_IN_ENV) && \
  121. defined(CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE)
  122. int overwrite_console(void)
  123. {
  124. return 0;
  125. }
  126. #endif
  127. #ifdef CONFIG_FPGA
  128. /*
  129. * FPGA programming support for SoC FPGA Cyclone V
  130. */
  131. static Altera_desc altera_fpga[] = {
  132. {
  133. /* Family */
  134. Altera_SoCFPGA,
  135. /* Interface type */
  136. fast_passive_parallel,
  137. /* No limitation as additional data will be ignored */
  138. -1,
  139. /* No device function table */
  140. NULL,
  141. /* Base interface address specified in driver */
  142. NULL,
  143. /* No cookie implementation */
  144. 0
  145. },
  146. };
  147. /* add device descriptor to FPGA device table */
  148. static void socfpga_fpga_add(void)
  149. {
  150. int i;
  151. fpga_init();
  152. for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
  153. fpga_add(fpga_altera, &altera_fpga[i]);
  154. }
  155. #else
  156. static inline void socfpga_fpga_add(void) {}
  157. #endif
  158. int arch_cpu_init(void)
  159. {
  160. #ifdef CONFIG_HW_WATCHDOG
  161. /*
  162. * In case the watchdog is enabled, make sure to (re-)configure it
  163. * so that the defined timeout is valid. Otherwise the SPL (Perloader)
  164. * timeout value is still active which might too short for Linux
  165. * booting.
  166. */
  167. hw_watchdog_init();
  168. #else
  169. /*
  170. * If the HW watchdog is NOT enabled, make sure it is not running,
  171. * for example because it was enabled in the preloader. This might
  172. * trigger a watchdog-triggered reboot of Linux kernel later.
  173. * Toggle watchdog reset, so watchdog in not running state.
  174. */
  175. socfpga_per_reset(SOCFPGA_RESET(L4WD0), 1);
  176. socfpga_per_reset(SOCFPGA_RESET(L4WD0), 0);
  177. #endif
  178. return 0;
  179. }
  180. /*
  181. * Convert all NIC-301 AMBA slaves from secure to non-secure
  182. */
  183. static void socfpga_nic301_slave_ns(void)
  184. {
  185. writel(0x1, &nic301_regs->lwhps2fpgaregs);
  186. writel(0x1, &nic301_regs->hps2fpgaregs);
  187. writel(0x1, &nic301_regs->acp);
  188. writel(0x1, &nic301_regs->rom);
  189. writel(0x1, &nic301_regs->ocram);
  190. writel(0x1, &nic301_regs->sdrdata);
  191. }
  192. static uint32_t iswgrp_handoff[8];
  193. int arch_early_init_r(void)
  194. {
  195. int i;
  196. /*
  197. * Write magic value into magic register to unlock support for
  198. * issuing warm reset. The ancient kernel code expects this
  199. * value to be written into the register by the bootloader, so
  200. * to support that old code, we write it here instead of in the
  201. * reset_cpu() function just before reseting the CPU.
  202. */
  203. writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
  204. for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
  205. iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
  206. socfpga_bridges_reset(1);
  207. socfpga_nic301_slave_ns();
  208. /*
  209. * Private components security:
  210. * U-Boot : configure private timer, global timer and cpu component
  211. * access as non secure for kernel stage (as required by Linux)
  212. */
  213. setbits_le32(&scu_regs->sacr, 0xfff);
  214. /* Configure the L2 controller to make SDRAM start at 0 */
  215. #ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
  216. writel(0x2, &nic301_regs->remap);
  217. #else
  218. writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
  219. writel(0x1, &pl310->pl310_addr_filter_start);
  220. #endif
  221. /* Add device descriptor to FPGA device table */
  222. socfpga_fpga_add();
  223. #ifdef CONFIG_DESIGNWARE_SPI
  224. /* Get Designware SPI controller out of reset */
  225. socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
  226. socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
  227. #endif
  228. return 0;
  229. }
  230. static void socfpga_sdram_apply_static_cfg(void)
  231. {
  232. const uint32_t staticcfg = SOCFPGA_SDR_ADDRESS + 0x505c;
  233. const uint32_t applymask = 0x8;
  234. uint32_t val = readl(staticcfg) | applymask;
  235. /*
  236. * SDRAM staticcfg register specific:
  237. * When applying the register setting, the CPU must not access
  238. * SDRAM. Luckily for us, we can abuse i-cache here to help us
  239. * circumvent the SDRAM access issue. The idea is to make sure
  240. * that the code is in one full i-cache line by branching past
  241. * it and back. Once it is in the i-cache, we execute the core
  242. * of the code and apply the register settings.
  243. *
  244. * The code below uses 7 instructions, while the Cortex-A9 has
  245. * 32-byte cachelines, thus the limit is 8 instructions total.
  246. */
  247. asm volatile(
  248. ".align 5 \n"
  249. " b 2f \n"
  250. "1: str %0, [%1] \n"
  251. " dsb \n"
  252. " isb \n"
  253. " b 3f \n"
  254. "2: b 1b \n"
  255. "3: nop \n"
  256. : : "r"(val), "r"(staticcfg) : "memory", "cc");
  257. }
  258. int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  259. {
  260. if (argc != 2)
  261. return CMD_RET_USAGE;
  262. argv++;
  263. switch (*argv[0]) {
  264. case 'e': /* Enable */
  265. writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
  266. socfpga_sdram_apply_static_cfg();
  267. writel(iswgrp_handoff[3], SOCFPGA_SDR_ADDRESS + 0x5080);
  268. writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
  269. writel(iswgrp_handoff[1], &nic301_regs->remap);
  270. break;
  271. case 'd': /* Disable */
  272. writel(0, &sysmgr_regs->fpgaintfgrp_module);
  273. writel(0, SOCFPGA_SDR_ADDRESS + 0x5080);
  274. socfpga_sdram_apply_static_cfg();
  275. writel(0, &reset_manager_base->brg_mod_reset);
  276. writel(1, &nic301_regs->remap);
  277. break;
  278. default:
  279. return CMD_RET_USAGE;
  280. }
  281. return 0;
  282. }
  283. U_BOOT_CMD(
  284. bridge, 2, 1, do_bridge,
  285. "SoCFPGA HPS FPGA bridge control",
  286. "enable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  287. "bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  288. ""
  289. );