misc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /* initialize and register the emac */
  74. return designware_initialize(CONFIG_EMAC_BASE,
  75. CONFIG_PHY_INTERFACE_MODE);
  76. }
  77. #endif
  78. #ifdef CONFIG_DWMMC
  79. /*
  80. * Initializes MMC controllers.
  81. * to override, implement board_mmc_init()
  82. */
  83. int cpu_mmc_init(bd_t *bis)
  84. {
  85. return socfpga_dwmmc_init(SOCFPGA_SDMMC_ADDRESS,
  86. CONFIG_HPS_SDMMC_BUSWIDTH, 0);
  87. }
  88. #endif
  89. #if defined(CONFIG_DISPLAY_CPUINFO)
  90. const char * const bsel_str[] = {
  91. "Reserved",
  92. "FPGA (HPS2FPGA Bridge)",
  93. "NAND Flash (1.8V)",
  94. "NAND Flash (3.0V)",
  95. "SD/MMC External Transceiver (1.8V)",
  96. "SD/MMC Internal Transceiver (3.0V)",
  97. "QSPI Flash (1.8V)",
  98. "QSPI Flash (3.0V)",
  99. };
  100. /*
  101. * Print CPU information
  102. */
  103. int print_cpuinfo(void)
  104. {
  105. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  106. puts("CPU: Altera SoCFPGA Platform\n");
  107. printf("BOOT: %s\n", bsel_str[bsel]);
  108. return 0;
  109. }
  110. #endif
  111. #if defined(CONFIG_SYS_CONSOLE_IS_IN_ENV) && \
  112. defined(CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE)
  113. int overwrite_console(void)
  114. {
  115. return 0;
  116. }
  117. #endif
  118. #ifdef CONFIG_FPGA
  119. /*
  120. * FPGA programming support for SoC FPGA Cyclone V
  121. */
  122. static Altera_desc altera_fpga[] = {
  123. {
  124. /* Family */
  125. Altera_SoCFPGA,
  126. /* Interface type */
  127. fast_passive_parallel,
  128. /* No limitation as additional data will be ignored */
  129. -1,
  130. /* No device function table */
  131. NULL,
  132. /* Base interface address specified in driver */
  133. NULL,
  134. /* No cookie implementation */
  135. 0
  136. },
  137. };
  138. /* add device descriptor to FPGA device table */
  139. static void socfpga_fpga_add(void)
  140. {
  141. int i;
  142. fpga_init();
  143. for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
  144. fpga_add(fpga_altera, &altera_fpga[i]);
  145. }
  146. #else
  147. static inline void socfpga_fpga_add(void) {}
  148. #endif
  149. int arch_cpu_init(void)
  150. {
  151. #ifdef CONFIG_HW_WATCHDOG
  152. /*
  153. * In case the watchdog is enabled, make sure to (re-)configure it
  154. * so that the defined timeout is valid. Otherwise the SPL (Perloader)
  155. * timeout value is still active which might too short for Linux
  156. * booting.
  157. */
  158. hw_watchdog_init();
  159. #else
  160. /*
  161. * If the HW watchdog is NOT enabled, make sure it is not running,
  162. * for example because it was enabled in the preloader. This might
  163. * trigger a watchdog-triggered reboot of Linux kernel later.
  164. * Toggle watchdog reset, so watchdog in not running state.
  165. */
  166. socfpga_per_reset(SOCFPGA_RESET(L4WD0), 1);
  167. socfpga_per_reset(SOCFPGA_RESET(L4WD0), 0);
  168. #endif
  169. return 0;
  170. }
  171. /*
  172. * Convert all NIC-301 AMBA slaves from secure to non-secure
  173. */
  174. static void socfpga_nic301_slave_ns(void)
  175. {
  176. writel(0x1, &nic301_regs->lwhps2fpgaregs);
  177. writel(0x1, &nic301_regs->hps2fpgaregs);
  178. writel(0x1, &nic301_regs->acp);
  179. writel(0x1, &nic301_regs->rom);
  180. writel(0x1, &nic301_regs->ocram);
  181. writel(0x1, &nic301_regs->sdrdata);
  182. }
  183. static uint32_t iswgrp_handoff[8];
  184. int arch_early_init_r(void)
  185. {
  186. int i;
  187. /*
  188. * Write magic value into magic register to unlock support for
  189. * issuing warm reset. The ancient kernel code expects this
  190. * value to be written into the register by the bootloader, so
  191. * to support that old code, we write it here instead of in the
  192. * reset_cpu() function just before reseting the CPU.
  193. */
  194. writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
  195. for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
  196. iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
  197. socfpga_bridges_reset(1);
  198. socfpga_nic301_slave_ns();
  199. /*
  200. * Private components security:
  201. * U-Boot : configure private timer, global timer and cpu component
  202. * access as non secure for kernel stage (as required by Linux)
  203. */
  204. setbits_le32(&scu_regs->sacr, 0xfff);
  205. /* Configure the L2 controller to make SDRAM start at 0 */
  206. #ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
  207. writel(0x2, &nic301_regs->remap);
  208. #else
  209. writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
  210. writel(0x1, &pl310->pl310_addr_filter_start);
  211. #endif
  212. /* Add device descriptor to FPGA device table */
  213. socfpga_fpga_add();
  214. #ifdef CONFIG_DESIGNWARE_SPI
  215. /* Get Designware SPI controller out of reset */
  216. socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
  217. socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
  218. #endif
  219. return 0;
  220. }
  221. static void socfpga_sdram_apply_static_cfg(void)
  222. {
  223. const uint32_t staticcfg = SOCFPGA_SDR_ADDRESS + 0x505c;
  224. const uint32_t applymask = 0x8;
  225. uint32_t val = readl(staticcfg) | applymask;
  226. /*
  227. * SDRAM staticcfg register specific:
  228. * When applying the register setting, the CPU must not access
  229. * SDRAM. Luckily for us, we can abuse i-cache here to help us
  230. * circumvent the SDRAM access issue. The idea is to make sure
  231. * that the code is in one full i-cache line by branching past
  232. * it and back. Once it is in the i-cache, we execute the core
  233. * of the code and apply the register settings.
  234. *
  235. * The code below uses 7 instructions, while the Cortex-A9 has
  236. * 32-byte cachelines, thus the limit is 8 instructions total.
  237. */
  238. asm volatile(
  239. ".align 5 \n"
  240. " b 2f \n"
  241. "1: str %0, [%1] \n"
  242. " dsb \n"
  243. " isb \n"
  244. " b 3f \n"
  245. "2: b 1b \n"
  246. "3: nop \n"
  247. : : "r"(val), "r"(staticcfg) : "memory", "cc");
  248. }
  249. int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  250. {
  251. if (argc != 2)
  252. return CMD_RET_USAGE;
  253. argv++;
  254. switch (*argv[0]) {
  255. case 'e': /* Enable */
  256. writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
  257. socfpga_sdram_apply_static_cfg();
  258. writel(iswgrp_handoff[3], SOCFPGA_SDR_ADDRESS + 0x5080);
  259. writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
  260. writel(iswgrp_handoff[1], &nic301_regs->remap);
  261. break;
  262. case 'd': /* Disable */
  263. writel(0, &sysmgr_regs->fpgaintfgrp_module);
  264. writel(0, SOCFPGA_SDR_ADDRESS + 0x5080);
  265. socfpga_sdram_apply_static_cfg();
  266. writel(0, &reset_manager_base->brg_mod_reset);
  267. writel(1, &nic301_regs->remap);
  268. break;
  269. default:
  270. return CMD_RET_USAGE;
  271. }
  272. return 0;
  273. }
  274. U_BOOT_CMD(
  275. bridge, 2, 1, do_bridge,
  276. "SoCFPGA HPS FPGA bridge control",
  277. "enable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  278. "bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  279. ""
  280. );