cpu.c 22 KB

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