cpu.c 24 KB

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