misc.c 12 KB

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