misc_gen5.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012-2017 Altera Corporation <www.altera.com>
  4. */
  5. #include <common.h>
  6. #include <asm/io.h>
  7. #include <errno.h>
  8. #include <fdtdec.h>
  9. #include <linux/libfdt.h>
  10. #include <altera.h>
  11. #include <miiphy.h>
  12. #include <netdev.h>
  13. #include <watchdog.h>
  14. #include <asm/arch/misc.h>
  15. #include <asm/arch/reset_manager.h>
  16. #include <asm/arch/scan_manager.h>
  17. #include <asm/arch/sdram.h>
  18. #include <asm/arch/system_manager.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 nic301_registers *nic301_regs =
  29. (struct nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
  30. static struct scu_registers *scu_regs =
  31. (struct scu_registers *)SOCFPGA_MPUSCU_ADDRESS;
  32. /*
  33. * DesignWare Ethernet initialization
  34. */
  35. #ifdef CONFIG_ETH_DESIGNWARE
  36. static void gen5_dwmac_reset(const u8 of_reset_id, const u8 phymode)
  37. {
  38. u32 physhift, reset;
  39. if (of_reset_id == EMAC0_RESET) {
  40. physhift = SYSMGR_EMACGRP_CTRL_PHYSEL0_LSB;
  41. reset = SOCFPGA_RESET(EMAC0);
  42. } else if (of_reset_id == EMAC1_RESET) {
  43. physhift = SYSMGR_EMACGRP_CTRL_PHYSEL1_LSB;
  44. reset = SOCFPGA_RESET(EMAC1);
  45. } else {
  46. printf("GMAC: Invalid reset ID (%i)!\n", of_reset_id);
  47. return;
  48. }
  49. /* configure to PHY interface select choosed */
  50. clrsetbits_le32(&sysmgr_regs->emacgrp_ctrl,
  51. SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift,
  52. phymode << physhift);
  53. /* Release the EMAC controller from reset */
  54. socfpga_per_reset(reset, 0);
  55. }
  56. static int socfpga_eth_reset(void)
  57. {
  58. /* Put all GMACs into RESET state. */
  59. socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
  60. socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
  61. return socfpga_eth_reset_common(gen5_dwmac_reset);
  62. };
  63. #else
  64. static int socfpga_eth_reset(void)
  65. {
  66. return 0;
  67. };
  68. #endif
  69. static const struct {
  70. const u16 pn;
  71. const char *name;
  72. const char *var;
  73. } socfpga_fpga_model[] = {
  74. /* Cyclone V E */
  75. { 0x2b15, "Cyclone V, E/A2", "cv_e_a2" },
  76. { 0x2b05, "Cyclone V, E/A4", "cv_e_a4" },
  77. { 0x2b22, "Cyclone V, E/A5", "cv_e_a5" },
  78. { 0x2b13, "Cyclone V, E/A7", "cv_e_a7" },
  79. { 0x2b14, "Cyclone V, E/A9", "cv_e_a9" },
  80. /* Cyclone V GX/GT */
  81. { 0x2b01, "Cyclone V, GX/C3", "cv_gx_c3" },
  82. { 0x2b12, "Cyclone V, GX/C4", "cv_gx_c4" },
  83. { 0x2b02, "Cyclone V, GX/C5 or GT/D5", "cv_gx_c5" },
  84. { 0x2b03, "Cyclone V, GX/C7 or GT/D7", "cv_gx_c7" },
  85. { 0x2b04, "Cyclone V, GX/C9 or GT/D9", "cv_gx_c9" },
  86. /* Cyclone V SE/SX/ST */
  87. { 0x2d11, "Cyclone V, SE/A2 or SX/C2", "cv_se_a2" },
  88. { 0x2d01, "Cyclone V, SE/A4 or SX/C4", "cv_se_a4" },
  89. { 0x2d12, "Cyclone V, SE/A5 or SX/C5 or ST/D5", "cv_se_a5" },
  90. { 0x2d02, "Cyclone V, SE/A6 or SX/C6 or ST/D6", "cv_se_a6" },
  91. /* Arria V */
  92. { 0x2d03, "Arria V, D5", "av_d5" },
  93. };
  94. static int socfpga_fpga_id(const bool print_id)
  95. {
  96. const u32 altera_mi = 0x6e;
  97. const u32 id = scan_mgr_get_fpga_id();
  98. const u32 lsb = id & 0x00000001;
  99. const u32 mi = (id >> 1) & 0x000007ff;
  100. const u32 pn = (id >> 12) & 0x0000ffff;
  101. const u32 version = (id >> 28) & 0x0000000f;
  102. int i;
  103. if ((mi != altera_mi) || (lsb != 1)) {
  104. printf("FPGA: Not Altera chip ID\n");
  105. return -EINVAL;
  106. }
  107. for (i = 0; i < ARRAY_SIZE(socfpga_fpga_model); i++)
  108. if (pn == socfpga_fpga_model[i].pn)
  109. break;
  110. if (i == ARRAY_SIZE(socfpga_fpga_model)) {
  111. printf("FPGA: Unknown Altera chip, ID 0x%08x\n", id);
  112. return -EINVAL;
  113. }
  114. if (print_id)
  115. printf("FPGA: Altera %s, version 0x%01x\n",
  116. socfpga_fpga_model[i].name, version);
  117. return i;
  118. }
  119. /*
  120. * Print CPU information
  121. */
  122. #if defined(CONFIG_DISPLAY_CPUINFO)
  123. int print_cpuinfo(void)
  124. {
  125. const u32 bsel =
  126. SYSMGR_GET_BOOTINFO_BSEL(readl(&sysmgr_regs->bootinfo));
  127. puts("CPU: Altera SoCFPGA Platform\n");
  128. socfpga_fpga_id(1);
  129. printf("BOOT: %s\n", bsel_str[bsel].name);
  130. return 0;
  131. }
  132. #endif
  133. #ifdef CONFIG_ARCH_MISC_INIT
  134. int arch_misc_init(void)
  135. {
  136. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  137. const int fpga_id = socfpga_fpga_id(0);
  138. env_set("bootmode", bsel_str[bsel].mode);
  139. if (fpga_id >= 0)
  140. env_set("fpgatype", socfpga_fpga_model[fpga_id].var);
  141. return socfpga_eth_reset();
  142. }
  143. #endif
  144. /*
  145. * Convert all NIC-301 AMBA slaves from secure to non-secure
  146. */
  147. static void socfpga_nic301_slave_ns(void)
  148. {
  149. writel(0x1, &nic301_regs->lwhps2fpgaregs);
  150. writel(0x1, &nic301_regs->hps2fpgaregs);
  151. writel(0x1, &nic301_regs->acp);
  152. writel(0x1, &nic301_regs->rom);
  153. writel(0x1, &nic301_regs->ocram);
  154. writel(0x1, &nic301_regs->sdrdata);
  155. }
  156. static u32 iswgrp_handoff[8];
  157. int arch_early_init_r(void)
  158. {
  159. int i;
  160. /*
  161. * Write magic value into magic register to unlock support for
  162. * issuing warm reset. The ancient kernel code expects this
  163. * value to be written into the register by the bootloader, so
  164. * to support that old code, we write it here instead of in the
  165. * reset_cpu() function just before resetting the CPU.
  166. */
  167. writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
  168. for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
  169. iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
  170. socfpga_bridges_reset(1);
  171. socfpga_nic301_slave_ns();
  172. /*
  173. * Private components security:
  174. * U-Boot : configure private timer, global timer and cpu component
  175. * access as non secure for kernel stage (as required by Linux)
  176. */
  177. setbits_le32(&scu_regs->sacr, 0xfff);
  178. /* Configure the L2 controller to make SDRAM start at 0 */
  179. writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
  180. writel(0x1, &pl310->pl310_addr_filter_start);
  181. /* Add device descriptor to FPGA device table */
  182. socfpga_fpga_add();
  183. #ifdef CONFIG_DESIGNWARE_SPI
  184. /* Get Designware SPI controller out of reset */
  185. socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
  186. socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
  187. #endif
  188. #ifdef CONFIG_NAND_DENALI
  189. socfpga_per_reset(SOCFPGA_RESET(NAND), 0);
  190. #endif
  191. return 0;
  192. }
  193. #ifndef CONFIG_SPL_BUILD
  194. static struct socfpga_reset_manager *reset_manager_base =
  195. (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_ADDRESS;
  196. static struct socfpga_sdr_ctrl *sdr_ctrl =
  197. (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
  198. static void socfpga_sdram_apply_static_cfg(void)
  199. {
  200. const u32 applymask = 0x8;
  201. u32 val = readl(&sdr_ctrl->static_cfg) | applymask;
  202. /*
  203. * SDRAM staticcfg register specific:
  204. * When applying the register setting, the CPU must not access
  205. * SDRAM. Luckily for us, we can abuse i-cache here to help us
  206. * circumvent the SDRAM access issue. The idea is to make sure
  207. * that the code is in one full i-cache line by branching past
  208. * it and back. Once it is in the i-cache, we execute the core
  209. * of the code and apply the register settings.
  210. *
  211. * The code below uses 7 instructions, while the Cortex-A9 has
  212. * 32-byte cachelines, thus the limit is 8 instructions total.
  213. */
  214. asm volatile(
  215. ".align 5 \n"
  216. " b 2f \n"
  217. "1: str %0, [%1] \n"
  218. " dsb \n"
  219. " isb \n"
  220. " b 3f \n"
  221. "2: b 1b \n"
  222. "3: nop \n"
  223. : : "r"(val), "r"(&sdr_ctrl->static_cfg) : "memory", "cc");
  224. }
  225. void do_bridge_reset(int enable)
  226. {
  227. if (enable) {
  228. writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
  229. socfpga_sdram_apply_static_cfg();
  230. writel(iswgrp_handoff[3], &sdr_ctrl->fpgaport_rst);
  231. writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
  232. writel(iswgrp_handoff[1], &nic301_regs->remap);
  233. } else {
  234. writel(0, &sysmgr_regs->fpgaintfgrp_module);
  235. writel(0, &sdr_ctrl->fpgaport_rst);
  236. socfpga_sdram_apply_static_cfg();
  237. writel(0, &reset_manager_base->brg_mod_reset);
  238. writel(1, &nic301_regs->remap);
  239. }
  240. }
  241. #endif