cpu.c 22 KB

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