PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/cpu/armv8/fsl-layerscape/cpu.c

https://bitbucket.org/hldspb/uboot-sbc8600
C | 903 lines | 665 code | 109 blank | 129 comment | 103 complexity | 8524c3544221105e3ec350f1ae0f0609 MD5 | raw file
  1. /*
  2. * Copyright 2017 NXP
  3. * Copyright 2014-2015 Freescale Semiconductor, Inc.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <fsl_ddr_sdram.h>
  9. #include <asm/io.h>
  10. #include <linux/errno.h>
  11. #include <asm/system.h>
  12. #include <asm/armv8/mmu.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/fsl_serdes.h>
  15. #include <asm/arch/soc.h>
  16. #include <asm/arch/cpu.h>
  17. #include <asm/arch/speed.h>
  18. #include <fsl_immap.h>
  19. #include <asm/arch/mp.h>
  20. #include <efi_loader.h>
  21. #include <fm_eth.h>
  22. #include <fsl-mc/fsl_mc.h>
  23. #ifdef CONFIG_FSL_ESDHC
  24. #include <fsl_esdhc.h>
  25. #endif
  26. #include <asm/armv8/sec_firmware.h>
  27. #ifdef CONFIG_SYS_FSL_DDR
  28. #include <fsl_ddr.h>
  29. #endif
  30. #include <asm/arch/clock.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. struct mm_region *mem_map = early_map;
  33. void cpu_name(char *name)
  34. {
  35. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  36. unsigned int i, svr, ver;
  37. svr = gur_in32(&gur->svr);
  38. ver = SVR_SOC_VER(svr);
  39. for (i = 0; i < ARRAY_SIZE(cpu_type_list); i++)
  40. if ((cpu_type_list[i].soc_ver & SVR_WO_E) == ver) {
  41. strcpy(name, cpu_type_list[i].name);
  42. if (IS_E_PROCESSOR(svr))
  43. strcat(name, "E");
  44. sprintf(name + strlen(name), " Rev%d.%d",
  45. SVR_MAJ(svr), SVR_MIN(svr));
  46. break;
  47. }
  48. if (i == ARRAY_SIZE(cpu_type_list))
  49. strcpy(name, "unknown");
  50. }
  51. #ifndef CONFIG_SYS_DCACHE_OFF
  52. /*
  53. * To start MMU before DDR is available, we create MMU table in SRAM.
  54. * The base address of SRAM is CONFIG_SYS_FSL_OCRAM_BASE. We use three
  55. * levels of translation tables here to cover 40-bit address space.
  56. * We use 4KB granule size, with 40 bits physical address, T0SZ=24
  57. * Address above EARLY_PGTABLE_SIZE (0x5000) is free for other purpose.
  58. * Note, the debug print in cache_v8.c is not usable for debugging
  59. * these early MMU tables because UART is not yet available.
  60. */
  61. static inline void early_mmu_setup(void)
  62. {
  63. unsigned int el = current_el();
  64. /* global data is already setup, no allocation yet */
  65. gd->arch.tlb_addr = CONFIG_SYS_FSL_OCRAM_BASE;
  66. gd->arch.tlb_fillptr = gd->arch.tlb_addr;
  67. gd->arch.tlb_size = EARLY_PGTABLE_SIZE;
  68. /* Create early page tables */
  69. setup_pgtables();
  70. /* point TTBR to the new table */
  71. set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
  72. get_tcr(el, NULL, NULL) &
  73. ~(TCR_ORGN_MASK | TCR_IRGN_MASK),
  74. MEMORY_ATTRIBUTES);
  75. set_sctlr(get_sctlr() | CR_M);
  76. }
  77. static void fix_pcie_mmu_map(void)
  78. {
  79. #ifdef CONFIG_ARCH_LS2080A
  80. unsigned int i;
  81. u32 svr, ver;
  82. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  83. svr = gur_in32(&gur->svr);
  84. ver = SVR_SOC_VER(svr);
  85. /* Fix PCIE base and size for LS2088A */
  86. if ((ver == SVR_LS2088A) || (ver == SVR_LS2084A) ||
  87. (ver == SVR_LS2048A) || (ver == SVR_LS2044A) ||
  88. (ver == SVR_LS2081A) || (ver == SVR_LS2041A)) {
  89. for (i = 0; i < ARRAY_SIZE(final_map); i++) {
  90. switch (final_map[i].phys) {
  91. case CONFIG_SYS_PCIE1_PHYS_ADDR:
  92. final_map[i].phys = 0x2000000000ULL;
  93. final_map[i].virt = 0x2000000000ULL;
  94. final_map[i].size = 0x800000000ULL;
  95. break;
  96. case CONFIG_SYS_PCIE2_PHYS_ADDR:
  97. final_map[i].phys = 0x2800000000ULL;
  98. final_map[i].virt = 0x2800000000ULL;
  99. final_map[i].size = 0x800000000ULL;
  100. break;
  101. case CONFIG_SYS_PCIE3_PHYS_ADDR:
  102. final_map[i].phys = 0x3000000000ULL;
  103. final_map[i].virt = 0x3000000000ULL;
  104. final_map[i].size = 0x800000000ULL;
  105. break;
  106. case CONFIG_SYS_PCIE4_PHYS_ADDR:
  107. final_map[i].phys = 0x3800000000ULL;
  108. final_map[i].virt = 0x3800000000ULL;
  109. final_map[i].size = 0x800000000ULL;
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. }
  116. #endif
  117. }
  118. /*
  119. * The final tables look similar to early tables, but different in detail.
  120. * These tables are in DRAM. Sub tables are added to enable cache for
  121. * QBMan and OCRAM.
  122. *
  123. * Put the MMU table in secure memory if gd->arch.secure_ram is valid.
  124. * OCRAM will be not used for this purpose so gd->arch.secure_ram can't be 0.
  125. */
  126. static inline void final_mmu_setup(void)
  127. {
  128. u64 tlb_addr_save = gd->arch.tlb_addr;
  129. unsigned int el = current_el();
  130. int index;
  131. /* fix the final_map before filling in the block entries */
  132. fix_pcie_mmu_map();
  133. mem_map = final_map;
  134. /* Update mapping for DDR to actual size */
  135. for (index = 0; index < ARRAY_SIZE(final_map) - 2; index++) {
  136. /*
  137. * Find the entry for DDR mapping and update the address and
  138. * size. Zero-sized mapping will be skipped when creating MMU
  139. * table.
  140. */
  141. switch (final_map[index].virt) {
  142. case CONFIG_SYS_FSL_DRAM_BASE1:
  143. final_map[index].virt = gd->bd->bi_dram[0].start;
  144. final_map[index].phys = gd->bd->bi_dram[0].start;
  145. final_map[index].size = gd->bd->bi_dram[0].size;
  146. break;
  147. #ifdef CONFIG_SYS_FSL_DRAM_BASE2
  148. case CONFIG_SYS_FSL_DRAM_BASE2:
  149. #if (CONFIG_NR_DRAM_BANKS >= 2)
  150. final_map[index].virt = gd->bd->bi_dram[1].start;
  151. final_map[index].phys = gd->bd->bi_dram[1].start;
  152. final_map[index].size = gd->bd->bi_dram[1].size;
  153. #else
  154. final_map[index].size = 0;
  155. #endif
  156. break;
  157. #endif
  158. #ifdef CONFIG_SYS_FSL_DRAM_BASE3
  159. case CONFIG_SYS_FSL_DRAM_BASE3:
  160. #if (CONFIG_NR_DRAM_BANKS >= 3)
  161. final_map[index].virt = gd->bd->bi_dram[2].start;
  162. final_map[index].phys = gd->bd->bi_dram[2].start;
  163. final_map[index].size = gd->bd->bi_dram[2].size;
  164. #else
  165. final_map[index].size = 0;
  166. #endif
  167. break;
  168. #endif
  169. default:
  170. break;
  171. }
  172. }
  173. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  174. if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
  175. if (el == 3) {
  176. /*
  177. * Only use gd->arch.secure_ram if the address is
  178. * recalculated. Align to 4KB for MMU table.
  179. */
  180. /* put page tables in secure ram */
  181. index = ARRAY_SIZE(final_map) - 2;
  182. gd->arch.tlb_addr = gd->arch.secure_ram & ~0xfff;
  183. final_map[index].virt = gd->arch.secure_ram & ~0x3;
  184. final_map[index].phys = final_map[index].virt;
  185. final_map[index].size = CONFIG_SYS_MEM_RESERVE_SECURE;
  186. final_map[index].attrs = PTE_BLOCK_OUTER_SHARE;
  187. gd->arch.secure_ram |= MEM_RESERVE_SECURE_SECURED;
  188. tlb_addr_save = gd->arch.tlb_addr;
  189. } else {
  190. /* Use allocated (board_f.c) memory for TLB */
  191. tlb_addr_save = gd->arch.tlb_allocated;
  192. gd->arch.tlb_addr = tlb_addr_save;
  193. }
  194. }
  195. #endif
  196. /* Reset the fill ptr */
  197. gd->arch.tlb_fillptr = tlb_addr_save;
  198. /* Create normal system page tables */
  199. setup_pgtables();
  200. /* Create emergency page tables */
  201. gd->arch.tlb_addr = gd->arch.tlb_fillptr;
  202. gd->arch.tlb_emerg = gd->arch.tlb_addr;
  203. setup_pgtables();
  204. gd->arch.tlb_addr = tlb_addr_save;
  205. /* Disable cache and MMU */
  206. dcache_disable(); /* TLBs are invalidated */
  207. invalidate_icache_all();
  208. /* point TTBR to the new table */
  209. set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
  210. MEMORY_ATTRIBUTES);
  211. set_sctlr(get_sctlr() | CR_M);
  212. }
  213. u64 get_page_table_size(void)
  214. {
  215. return 0x10000;
  216. }
  217. int arch_cpu_init(void)
  218. {
  219. /*
  220. * This function is called before U-Boot relocates itself to speed up
  221. * on system running. It is not necessary to run if performance is not
  222. * critical. Skip if MMU is already enabled by SPL or other means.
  223. */
  224. if (get_sctlr() & CR_M)
  225. return 0;
  226. icache_enable();
  227. __asm_invalidate_dcache_all();
  228. __asm_invalidate_tlb_all();
  229. early_mmu_setup();
  230. set_sctlr(get_sctlr() | CR_C);
  231. return 0;
  232. }
  233. void mmu_setup(void)
  234. {
  235. final_mmu_setup();
  236. }
  237. /*
  238. * This function is called from common/board_r.c.
  239. * It recreates MMU table in main memory.
  240. */
  241. void enable_caches(void)
  242. {
  243. mmu_setup();
  244. __asm_invalidate_tlb_all();
  245. icache_enable();
  246. dcache_enable();
  247. }
  248. #endif
  249. u32 initiator_type(u32 cluster, int init_id)
  250. {
  251. struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  252. u32 idx = (cluster >> (init_id * 8)) & TP_CLUSTER_INIT_MASK;
  253. u32 type = 0;
  254. type = gur_in32(&gur->tp_ityp[idx]);
  255. if (type & TP_ITYP_AV)
  256. return type;
  257. return 0;
  258. }
  259. u32 cpu_pos_mask(void)
  260. {
  261. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  262. int i = 0;
  263. u32 cluster, type, mask = 0;
  264. do {
  265. int j;
  266. cluster = gur_in32(&gur->tp_cluster[i].lower);
  267. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  268. type = initiator_type(cluster, j);
  269. if (type && (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM))
  270. mask |= 1 << (i * TP_INIT_PER_CLUSTER + j);
  271. }
  272. i++;
  273. } while ((cluster & TP_CLUSTER_EOC) == 0x0);
  274. return mask;
  275. }
  276. u32 cpu_mask(void)
  277. {
  278. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  279. int i = 0, count = 0;
  280. u32 cluster, type, mask = 0;
  281. do {
  282. int j;
  283. cluster = gur_in32(&gur->tp_cluster[i].lower);
  284. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  285. type = initiator_type(cluster, j);
  286. if (type) {
  287. if (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
  288. mask |= 1 << count;
  289. count++;
  290. }
  291. }
  292. i++;
  293. } while ((cluster & TP_CLUSTER_EOC) == 0x0);
  294. return mask;
  295. }
  296. /*
  297. * Return the number of cores on this SOC.
  298. */
  299. int cpu_numcores(void)
  300. {
  301. return hweight32(cpu_mask());
  302. }
  303. int fsl_qoriq_core_to_cluster(unsigned int core)
  304. {
  305. struct ccsr_gur __iomem *gur =
  306. (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
  307. int i = 0, count = 0;
  308. u32 cluster;
  309. do {
  310. int j;
  311. cluster = gur_in32(&gur->tp_cluster[i].lower);
  312. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  313. if (initiator_type(cluster, j)) {
  314. if (count == core)
  315. return i;
  316. count++;
  317. }
  318. }
  319. i++;
  320. } while ((cluster & TP_CLUSTER_EOC) == 0x0);
  321. return -1; /* cannot identify the cluster */
  322. }
  323. u32 fsl_qoriq_core_to_type(unsigned int core)
  324. {
  325. struct ccsr_gur __iomem *gur =
  326. (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
  327. int i = 0, count = 0;
  328. u32 cluster, type;
  329. do {
  330. int j;
  331. cluster = gur_in32(&gur->tp_cluster[i].lower);
  332. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  333. type = initiator_type(cluster, j);
  334. if (type) {
  335. if (count == core)
  336. return type;
  337. count++;
  338. }
  339. }
  340. i++;
  341. } while ((cluster & TP_CLUSTER_EOC) == 0x0);
  342. return -1; /* cannot identify the cluster */
  343. }
  344. #ifndef CONFIG_FSL_LSCH3
  345. uint get_svr(void)
  346. {
  347. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  348. return gur_in32(&gur->svr);
  349. }
  350. #endif
  351. #ifdef CONFIG_DISPLAY_CPUINFO
  352. int print_cpuinfo(void)
  353. {
  354. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  355. struct sys_info sysinfo;
  356. char buf[32];
  357. unsigned int i, core;
  358. u32 type, rcw, svr = gur_in32(&gur->svr);
  359. puts("SoC: ");
  360. cpu_name(buf);
  361. printf(" %s (0x%x)\n", buf, svr);
  362. memset((u8 *)buf, 0x00, ARRAY_SIZE(buf));
  363. get_sys_info(&sysinfo);
  364. puts("Clock Configuration:");
  365. for_each_cpu(i, core, cpu_numcores(), cpu_mask()) {
  366. if (!(i % 3))
  367. puts("\n ");
  368. type = TP_ITYP_VER(fsl_qoriq_core_to_type(core));
  369. printf("CPU%d(%s):%-4s MHz ", core,
  370. type == TY_ITYP_VER_A7 ? "A7 " :
  371. (type == TY_ITYP_VER_A53 ? "A53" :
  372. (type == TY_ITYP_VER_A57 ? "A57" :
  373. (type == TY_ITYP_VER_A72 ? "A72" : " "))),
  374. strmhz(buf, sysinfo.freq_processor[core]));
  375. }
  376. /* Display platform clock as Bus frequency. */
  377. printf("\n Bus: %-4s MHz ",
  378. strmhz(buf, sysinfo.freq_systembus / CONFIG_SYS_FSL_PCLK_DIV));
  379. printf("DDR: %-4s MT/s", strmhz(buf, sysinfo.freq_ddrbus));
  380. #ifdef CONFIG_SYS_DPAA_FMAN
  381. printf(" FMAN: %-4s MHz", strmhz(buf, sysinfo.freq_fman[0]));
  382. #endif
  383. #ifdef CONFIG_SYS_FSL_HAS_DP_DDR
  384. if (soc_has_dp_ddr()) {
  385. printf(" DP-DDR: %-4s MT/s",
  386. strmhz(buf, sysinfo.freq_ddrbus2));
  387. }
  388. #endif
  389. puts("\n");
  390. /*
  391. * Display the RCW, so that no one gets confused as to what RCW
  392. * we're actually using for this boot.
  393. */
  394. puts("Reset Configuration Word (RCW):");
  395. for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
  396. rcw = gur_in32(&gur->rcwsr[i]);
  397. if ((i % 4) == 0)
  398. printf("\n %08x:", i * 4);
  399. printf(" %08x", rcw);
  400. }
  401. puts("\n");
  402. return 0;
  403. }
  404. #endif
  405. #ifdef CONFIG_FSL_ESDHC
  406. int cpu_mmc_init(bd_t *bis)
  407. {
  408. return fsl_esdhc_mmc_init(bis);
  409. }
  410. #endif
  411. int cpu_eth_init(bd_t *bis)
  412. {
  413. int error = 0;
  414. #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
  415. error = fsl_mc_ldpaa_init(bis);
  416. #endif
  417. #ifdef CONFIG_FMAN_ENET
  418. fm_standard_init(bis);
  419. #endif
  420. return error;
  421. }
  422. static inline int check_psci(void)
  423. {
  424. unsigned int psci_ver;
  425. psci_ver = sec_firmware_support_psci_version();
  426. if (psci_ver == PSCI_INVALID_VER)
  427. return 1;
  428. return 0;
  429. }
  430. int arch_early_init_r(void)
  431. {
  432. #ifdef CONFIG_SYS_FSL_ERRATUM_A009635
  433. u32 svr_dev_id;
  434. /*
  435. * erratum A009635 is valid only for LS2080A SoC and
  436. * its personalitiesi
  437. */
  438. svr_dev_id = get_svr() >> 16;
  439. if (svr_dev_id == SVR_DEV_LS2080A)
  440. erratum_a009635();
  441. #endif
  442. #if defined(CONFIG_SYS_FSL_ERRATUM_A009942) && defined(CONFIG_SYS_FSL_DDR)
  443. erratum_a009942_check_cpo();
  444. #endif
  445. if (check_psci()) {
  446. debug("PSCI: PSCI does not exist.\n");
  447. /* if PSCI does not exist, boot secondary cores here */
  448. if (fsl_layerscape_wake_seconday_cores())
  449. printf("Did not wake secondary cores\n");
  450. }
  451. #ifdef CONFIG_SYS_FSL_HAS_RGMII
  452. fsl_rgmii_init();
  453. #endif
  454. #ifdef CONFIG_SYS_HAS_SERDES
  455. fsl_serdes_init();
  456. #endif
  457. #ifdef CONFIG_FMAN_ENET
  458. fman_enet_init();
  459. #endif
  460. return 0;
  461. }
  462. int timer_init(void)
  463. {
  464. u32 __iomem *cntcr = (u32 *)CONFIG_SYS_FSL_TIMER_ADDR;
  465. #ifdef CONFIG_FSL_LSCH3
  466. u32 __iomem *cltbenr = (u32 *)CONFIG_SYS_FSL_PMU_CLTBENR;
  467. #endif
  468. #ifdef CONFIG_ARCH_LS2080A
  469. u32 __iomem *pctbenr = (u32 *)FSL_PMU_PCTBENR_OFFSET;
  470. u32 svr_dev_id;
  471. #endif
  472. #ifdef COUNTER_FREQUENCY_REAL
  473. unsigned long cntfrq = COUNTER_FREQUENCY_REAL;
  474. /* Update with accurate clock frequency */
  475. if (current_el() == 3)
  476. asm volatile("msr cntfrq_el0, %0" : : "r" (cntfrq) : "memory");
  477. #endif
  478. #ifdef CONFIG_FSL_LSCH3
  479. /* Enable timebase for all clusters.
  480. * It is safe to do so even some clusters are not enabled.
  481. */
  482. out_le32(cltbenr, 0xf);
  483. #endif
  484. #ifdef CONFIG_ARCH_LS2080A
  485. /*
  486. * In certain Layerscape SoCs, the clock for each core's
  487. * has an enable bit in the PMU Physical Core Time Base Enable
  488. * Register (PCTBENR), which allows the watchdog to operate.
  489. */
  490. setbits_le32(pctbenr, 0xff);
  491. /*
  492. * For LS2080A SoC and its personalities, timer controller
  493. * offset is different
  494. */
  495. svr_dev_id = get_svr() >> 16;
  496. if (svr_dev_id == SVR_DEV_LS2080A)
  497. cntcr = (u32 *)SYS_FSL_LS2080A_LS2085A_TIMER_ADDR;
  498. #endif
  499. /* Enable clock for timer
  500. * This is a global setting.
  501. */
  502. out_le32(cntcr, 0x1);
  503. return 0;
  504. }
  505. __efi_runtime_data u32 __iomem *rstcr = (u32 *)CONFIG_SYS_FSL_RST_ADDR;
  506. void __efi_runtime reset_cpu(ulong addr)
  507. {
  508. u32 val;
  509. /* Raise RESET_REQ_B */
  510. val = scfg_in32(rstcr);
  511. val |= 0x02;
  512. scfg_out32(rstcr, val);
  513. }
  514. #ifdef CONFIG_EFI_LOADER
  515. void __efi_runtime EFIAPI efi_reset_system(
  516. enum efi_reset_type reset_type,
  517. efi_status_t reset_status,
  518. unsigned long data_size, void *reset_data)
  519. {
  520. switch (reset_type) {
  521. case EFI_RESET_COLD:
  522. case EFI_RESET_WARM:
  523. reset_cpu(0);
  524. break;
  525. case EFI_RESET_SHUTDOWN:
  526. /* Nothing we can do */
  527. break;
  528. }
  529. while (1) { }
  530. }
  531. void efi_reset_system_init(void)
  532. {
  533. efi_add_runtime_mmio(&rstcr, sizeof(*rstcr));
  534. }
  535. #endif
  536. /*
  537. * Calculate reserved memory with given memory bank
  538. * Return aligned memory size on success
  539. * Return (ram_size + needed size) for failure
  540. */
  541. phys_size_t board_reserve_ram_top(phys_size_t ram_size)
  542. {
  543. phys_size_t ram_top = ram_size;
  544. #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
  545. ram_top = mc_get_dram_block_size();
  546. if (ram_top > ram_size)
  547. return ram_size + ram_top;
  548. ram_top = ram_size - ram_top;
  549. /* The start address of MC reserved memory needs to be aligned. */
  550. ram_top &= ~(CONFIG_SYS_MC_RSV_MEM_ALIGN - 1);
  551. #endif
  552. return ram_size - ram_top;
  553. }
  554. phys_size_t get_effective_memsize(void)
  555. {
  556. phys_size_t ea_size, rem = 0;
  557. /*
  558. * For ARMv8 SoCs, DDR memory is split into two or three regions. The
  559. * first region is 2GB space at 0x8000_0000. Secure memory needs to
  560. * allocated from first region. If the memory extends to the second
  561. * region (or the third region if applicable), Management Complex (MC)
  562. * memory should be put into the highest region, i.e. the end of DDR
  563. * memory. CONFIG_MAX_MEM_MAPPED is set to the size of first region so
  564. * U-Boot doesn't relocate itself into higher address. Should DDR be
  565. * configured to skip the first region, this function needs to be
  566. * adjusted.
  567. */
  568. if (gd->ram_size > CONFIG_MAX_MEM_MAPPED) {
  569. ea_size = CONFIG_MAX_MEM_MAPPED;
  570. rem = gd->ram_size - ea_size;
  571. } else {
  572. ea_size = gd->ram_size;
  573. }
  574. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  575. /* Check if we have enough space for secure memory */
  576. if (ea_size > CONFIG_SYS_MEM_RESERVE_SECURE)
  577. ea_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
  578. else
  579. printf("Error: No enough space for secure memory.\n");
  580. #endif
  581. /* Check if we have enough memory for MC */
  582. if (rem < board_reserve_ram_top(rem)) {
  583. /* Not enough memory in high region to reserve */
  584. if (ea_size > board_reserve_ram_top(ea_size))
  585. ea_size -= board_reserve_ram_top(ea_size);
  586. else
  587. printf("Error: No enough space for reserved memory.\n");
  588. }
  589. return ea_size;
  590. }
  591. int dram_init_banksize(void)
  592. {
  593. #ifdef CONFIG_SYS_DP_DDR_BASE_PHY
  594. phys_size_t dp_ddr_size;
  595. #endif
  596. /*
  597. * gd->ram_size has the total size of DDR memory, less reserved secure
  598. * memory. The DDR extends from low region to high region(s) presuming
  599. * no hole is created with DDR configuration. gd->arch.secure_ram tracks
  600. * the location of secure memory. gd->arch.resv_ram tracks the location
  601. * of reserved memory for Management Complex (MC). Because gd->ram_size
  602. * is reduced by this function if secure memory is reserved, checking
  603. * gd->arch.secure_ram should be done to avoid running it repeatedly.
  604. */
  605. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  606. if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
  607. debug("No need to run again, skip %s\n", __func__);
  608. return 0;
  609. }
  610. #endif
  611. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  612. if (gd->ram_size > CONFIG_SYS_DDR_BLOCK1_SIZE) {
  613. gd->bd->bi_dram[0].size = CONFIG_SYS_DDR_BLOCK1_SIZE;
  614. gd->bd->bi_dram[1].start = CONFIG_SYS_DDR_BLOCK2_BASE;
  615. gd->bd->bi_dram[1].size = gd->ram_size -
  616. CONFIG_SYS_DDR_BLOCK1_SIZE;
  617. #ifdef CONFIG_SYS_DDR_BLOCK3_BASE
  618. if (gd->bi_dram[1].size > CONFIG_SYS_DDR_BLOCK2_SIZE) {
  619. gd->bd->bi_dram[2].start = CONFIG_SYS_DDR_BLOCK3_BASE;
  620. gd->bd->bi_dram[2].size = gd->bd->bi_dram[1].size -
  621. CONFIG_SYS_DDR_BLOCK2_SIZE;
  622. gd->bd->bi_dram[1].size = CONFIG_SYS_DDR_BLOCK2_SIZE;
  623. }
  624. #endif
  625. } else {
  626. gd->bd->bi_dram[0].size = gd->ram_size;
  627. }
  628. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  629. if (gd->bd->bi_dram[0].size >
  630. CONFIG_SYS_MEM_RESERVE_SECURE) {
  631. gd->bd->bi_dram[0].size -=
  632. CONFIG_SYS_MEM_RESERVE_SECURE;
  633. gd->arch.secure_ram = gd->bd->bi_dram[0].start +
  634. gd->bd->bi_dram[0].size;
  635. gd->arch.secure_ram |= MEM_RESERVE_SECURE_MAINTAINED;
  636. gd->ram_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
  637. }
  638. #endif /* CONFIG_SYS_MEM_RESERVE_SECURE */
  639. #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
  640. /* Assign memory for MC */
  641. #ifdef CONFIG_SYS_DDR_BLOCK3_BASE
  642. if (gd->bd->bi_dram[2].size >=
  643. board_reserve_ram_top(gd->bd->bi_dram[2].size)) {
  644. gd->arch.resv_ram = gd->bd->bi_dram[2].start +
  645. gd->bd->bi_dram[2].size -
  646. board_reserve_ram_top(gd->bd->bi_dram[2].size);
  647. } else
  648. #endif
  649. {
  650. if (gd->bd->bi_dram[1].size >=
  651. board_reserve_ram_top(gd->bd->bi_dram[1].size)) {
  652. gd->arch.resv_ram = gd->bd->bi_dram[1].start +
  653. gd->bd->bi_dram[1].size -
  654. board_reserve_ram_top(gd->bd->bi_dram[1].size);
  655. } else if (gd->bd->bi_dram[0].size >
  656. board_reserve_ram_top(gd->bd->bi_dram[0].size)) {
  657. gd->arch.resv_ram = gd->bd->bi_dram[0].start +
  658. gd->bd->bi_dram[0].size -
  659. board_reserve_ram_top(gd->bd->bi_dram[0].size);
  660. }
  661. }
  662. #endif /* CONFIG_FSL_MC_ENET */
  663. #ifdef CONFIG_SYS_DP_DDR_BASE_PHY
  664. #ifdef CONFIG_SYS_DDR_BLOCK3_BASE
  665. #error "This SoC shouldn't have DP DDR"
  666. #endif
  667. if (soc_has_dp_ddr()) {
  668. /* initialize DP-DDR here */
  669. puts("DP-DDR: ");
  670. /*
  671. * DDR controller use 0 as the base address for binding.
  672. * It is mapped to CONFIG_SYS_DP_DDR_BASE for core to access.
  673. */
  674. dp_ddr_size = fsl_other_ddr_sdram(CONFIG_SYS_DP_DDR_BASE_PHY,
  675. CONFIG_DP_DDR_CTRL,
  676. CONFIG_DP_DDR_NUM_CTRLS,
  677. CONFIG_DP_DDR_DIMM_SLOTS_PER_CTLR,
  678. NULL, NULL, NULL);
  679. if (dp_ddr_size) {
  680. gd->bd->bi_dram[2].start = CONFIG_SYS_DP_DDR_BASE;
  681. gd->bd->bi_dram[2].size = dp_ddr_size;
  682. } else {
  683. puts("Not detected");
  684. }
  685. }
  686. #endif
  687. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  688. debug("%s is called. gd->ram_size is reduced to %lu\n",
  689. __func__, (ulong)gd->ram_size);
  690. #endif
  691. return 0;
  692. }
  693. #if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_SPL_BUILD)
  694. void efi_add_known_memory(void)
  695. {
  696. int i;
  697. phys_addr_t ram_start, start;
  698. phys_size_t ram_size;
  699. u64 pages;
  700. /* Add RAM */
  701. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  702. #ifdef CONFIG_SYS_DP_DDR_BASE_PHY
  703. #ifdef CONFIG_SYS_DDR_BLOCK3_BASE
  704. #error "This SoC shouldn't have DP DDR"
  705. #endif
  706. if (i == 2)
  707. continue; /* skip DP-DDR */
  708. #endif
  709. ram_start = gd->bd->bi_dram[i].start;
  710. ram_size = gd->bd->bi_dram[i].size;
  711. #ifdef CONFIG_RESV_RAM
  712. if (gd->arch.resv_ram >= ram_start &&
  713. gd->arch.resv_ram < ram_start + ram_size)
  714. ram_size = gd->arch.resv_ram - ram_start;
  715. #endif
  716. start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
  717. pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  718. efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
  719. false);
  720. }
  721. }
  722. #endif
  723. /*
  724. * Before DDR size is known, early MMU table have DDR mapped as device memory
  725. * to avoid speculative access. To relocate U-Boot to DDR, "normal memory"
  726. * needs to be set for these mappings.
  727. * If a special case configures DDR with holes in the mapping, the holes need
  728. * to be marked as invalid. This is not implemented in this function.
  729. */
  730. void update_early_mmu_table(void)
  731. {
  732. if (!gd->arch.tlb_addr)
  733. return;
  734. if (gd->ram_size <= CONFIG_SYS_FSL_DRAM_SIZE1) {
  735. mmu_change_region_attr(
  736. CONFIG_SYS_SDRAM_BASE,
  737. gd->ram_size,
  738. PTE_BLOCK_MEMTYPE(MT_NORMAL) |
  739. PTE_BLOCK_OUTER_SHARE |
  740. PTE_BLOCK_NS |
  741. PTE_TYPE_VALID);
  742. } else {
  743. mmu_change_region_attr(
  744. CONFIG_SYS_SDRAM_BASE,
  745. CONFIG_SYS_DDR_BLOCK1_SIZE,
  746. PTE_BLOCK_MEMTYPE(MT_NORMAL) |
  747. PTE_BLOCK_OUTER_SHARE |
  748. PTE_BLOCK_NS |
  749. PTE_TYPE_VALID);
  750. #ifdef CONFIG_SYS_DDR_BLOCK3_BASE
  751. #ifndef CONFIG_SYS_DDR_BLOCK2_SIZE
  752. #error "Missing CONFIG_SYS_DDR_BLOCK2_SIZE"
  753. #endif
  754. if (gd->ram_size - CONFIG_SYS_DDR_BLOCK1_SIZE >
  755. CONFIG_SYS_DDR_BLOCK2_SIZE) {
  756. mmu_change_region_attr(
  757. CONFIG_SYS_DDR_BLOCK2_BASE,
  758. CONFIG_SYS_DDR_BLOCK2_SIZE,
  759. PTE_BLOCK_MEMTYPE(MT_NORMAL) |
  760. PTE_BLOCK_OUTER_SHARE |
  761. PTE_BLOCK_NS |
  762. PTE_TYPE_VALID);
  763. mmu_change_region_attr(
  764. CONFIG_SYS_DDR_BLOCK3_BASE,
  765. gd->ram_size -
  766. CONFIG_SYS_DDR_BLOCK1_SIZE -
  767. CONFIG_SYS_DDR_BLOCK2_SIZE,
  768. PTE_BLOCK_MEMTYPE(MT_NORMAL) |
  769. PTE_BLOCK_OUTER_SHARE |
  770. PTE_BLOCK_NS |
  771. PTE_TYPE_VALID);
  772. } else
  773. #endif
  774. {
  775. mmu_change_region_attr(
  776. CONFIG_SYS_DDR_BLOCK2_BASE,
  777. gd->ram_size -
  778. CONFIG_SYS_DDR_BLOCK1_SIZE,
  779. PTE_BLOCK_MEMTYPE(MT_NORMAL) |
  780. PTE_BLOCK_OUTER_SHARE |
  781. PTE_BLOCK_NS |
  782. PTE_TYPE_VALID);
  783. }
  784. }
  785. }
  786. __weak int dram_init(void)
  787. {
  788. fsl_initdram();
  789. #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
  790. /* This will break-before-make MMU for DDR */
  791. update_early_mmu_table();
  792. #endif
  793. return 0;
  794. }