cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2014 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <asm/system.h>
  9. #include <asm/armv8/mmu.h>
  10. #include <asm/io.h>
  11. #include <asm/arch-fsl-lsch3/immap_lsch3.h>
  12. #include "cpu.h"
  13. #include "mp.h"
  14. #include "speed.h"
  15. #include <fsl_mc.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. #ifndef CONFIG_SYS_DCACHE_OFF
  18. /*
  19. * To start MMU before DDR is available, we create MMU table in SRAM.
  20. * The base address of SRAM is CONFIG_SYS_FSL_OCRAM_BASE. We use three
  21. * levels of translation tables here to cover 40-bit address space.
  22. * We use 4KB granule size, with 40 bits physical address, T0SZ=24
  23. * Level 0 IA[39], table address @0
  24. * Level 1 IA[31:30], table address @01000, 0x2000
  25. * Level 2 IA[29:21], table address @0x3000
  26. */
  27. #define SECTION_SHIFT_L0 39UL
  28. #define SECTION_SHIFT_L1 30UL
  29. #define SECTION_SHIFT_L2 21UL
  30. #define BLOCK_SIZE_L0 0x8000000000UL
  31. #define BLOCK_SIZE_L1 (1 << SECTION_SHIFT_L1)
  32. #define BLOCK_SIZE_L2 (1 << SECTION_SHIFT_L2)
  33. #define CONFIG_SYS_IFC_BASE 0x30000000
  34. #define CONFIG_SYS_IFC_SIZE 0x10000000
  35. #define CONFIG_SYS_IFC_BASE2 0x500000000
  36. #define CONFIG_SYS_IFC_SIZE2 0x100000000
  37. #define TCR_EL2_PS_40BIT (2 << 16)
  38. #define LSCH3_VA_BITS (40)
  39. #define LSCH3_TCR (TCR_TG0_4K | \
  40. TCR_EL2_PS_40BIT | \
  41. TCR_SHARED_NON | \
  42. TCR_ORGN_NC | \
  43. TCR_IRGN_NC | \
  44. TCR_T0SZ(LSCH3_VA_BITS))
  45. /*
  46. * Final MMU
  47. * Let's start from the same layout as early MMU and modify as needed.
  48. * IFC regions will be cache-inhibit.
  49. */
  50. #define FINAL_QBMAN_CACHED_MEM 0x818000000UL
  51. #define FINAL_QBMAN_CACHED_SIZE 0x4000000
  52. static inline void early_mmu_setup(void)
  53. {
  54. int el;
  55. u64 i;
  56. u64 section_l1t0, section_l1t1, section_l2;
  57. u64 *level0_table = (u64 *)CONFIG_SYS_FSL_OCRAM_BASE;
  58. u64 *level1_table_0 = (u64 *)(CONFIG_SYS_FSL_OCRAM_BASE + 0x1000);
  59. u64 *level1_table_1 = (u64 *)(CONFIG_SYS_FSL_OCRAM_BASE + 0x2000);
  60. u64 *level2_table = (u64 *)(CONFIG_SYS_FSL_OCRAM_BASE + 0x3000);
  61. level0_table[0] =
  62. (u64)level1_table_0 | PMD_TYPE_TABLE;
  63. level0_table[1] =
  64. (u64)level1_table_1 | PMD_TYPE_TABLE;
  65. /*
  66. * set level 1 table 0 to cache_inhibit, covering 0 to 512GB
  67. * set level 1 table 1 to cache enabled, covering 512GB to 1TB
  68. * set level 2 table to cache-inhibit, covering 0 to 1GB
  69. */
  70. section_l1t0 = 0;
  71. section_l1t1 = BLOCK_SIZE_L0;
  72. section_l2 = 0;
  73. for (i = 0; i < 512; i++) {
  74. set_pgtable_section(level1_table_0, i, section_l1t0,
  75. MT_DEVICE_NGNRNE);
  76. set_pgtable_section(level1_table_1, i, section_l1t1,
  77. MT_NORMAL);
  78. set_pgtable_section(level2_table, i, section_l2,
  79. MT_DEVICE_NGNRNE);
  80. section_l1t0 += BLOCK_SIZE_L1;
  81. section_l1t1 += BLOCK_SIZE_L1;
  82. section_l2 += BLOCK_SIZE_L2;
  83. }
  84. level1_table_0[0] =
  85. (u64)level2_table | PMD_TYPE_TABLE;
  86. level1_table_0[1] =
  87. 0x40000000 | PMD_SECT_AF | PMD_TYPE_SECT |
  88. PMD_ATTRINDX(MT_DEVICE_NGNRNE);
  89. level1_table_0[2] =
  90. 0x80000000 | PMD_SECT_AF | PMD_TYPE_SECT |
  91. PMD_ATTRINDX(MT_NORMAL);
  92. level1_table_0[3] =
  93. 0xc0000000 | PMD_SECT_AF | PMD_TYPE_SECT |
  94. PMD_ATTRINDX(MT_NORMAL);
  95. /* Rewrite table to enable cache */
  96. set_pgtable_section(level2_table,
  97. CONFIG_SYS_FSL_OCRAM_BASE >> SECTION_SHIFT_L2,
  98. CONFIG_SYS_FSL_OCRAM_BASE,
  99. MT_NORMAL);
  100. for (i = CONFIG_SYS_IFC_BASE >> SECTION_SHIFT_L2;
  101. i < (CONFIG_SYS_IFC_BASE + CONFIG_SYS_IFC_SIZE)
  102. >> SECTION_SHIFT_L2; i++) {
  103. section_l2 = i << SECTION_SHIFT_L2;
  104. set_pgtable_section(level2_table, i,
  105. section_l2, MT_NORMAL);
  106. }
  107. el = current_el();
  108. set_ttbr_tcr_mair(el, (u64)level0_table, LSCH3_TCR, MEMORY_ATTRIBUTES);
  109. set_sctlr(get_sctlr() | CR_M);
  110. }
  111. /*
  112. * This final tale looks similar to early table, but different in detail.
  113. * These tables are in regular memory. Cache on IFC is disabled. One sub table
  114. * is added to enable cache for QBMan.
  115. */
  116. static inline void final_mmu_setup(void)
  117. {
  118. int el;
  119. u64 i, tbl_base, tbl_limit, section_base;
  120. u64 section_l1t0, section_l1t1, section_l2;
  121. u64 *level0_table = (u64 *)gd->arch.tlb_addr;
  122. u64 *level1_table_0 = (u64 *)(gd->arch.tlb_addr + 0x1000);
  123. u64 *level1_table_1 = (u64 *)(gd->arch.tlb_addr + 0x2000);
  124. u64 *level2_table_0 = (u64 *)(gd->arch.tlb_addr + 0x3000);
  125. u64 *level2_table_1 = (u64 *)(gd->arch.tlb_addr + 0x4000);
  126. level0_table[0] =
  127. (u64)level1_table_0 | PMD_TYPE_TABLE;
  128. level0_table[1] =
  129. (u64)level1_table_1 | PMD_TYPE_TABLE;
  130. /*
  131. * set level 1 table 0 to cache_inhibit, covering 0 to 512GB
  132. * set level 1 table 1 to cache enabled, covering 512GB to 1TB
  133. * set level 2 table 0 to cache-inhibit, covering 0 to 1GB
  134. */
  135. section_l1t0 = 0;
  136. section_l1t1 = BLOCK_SIZE_L0;
  137. section_l2 = 0;
  138. for (i = 0; i < 512; i++) {
  139. set_pgtable_section(level1_table_0, i, section_l1t0,
  140. MT_DEVICE_NGNRNE);
  141. set_pgtable_section(level1_table_1, i, section_l1t1,
  142. MT_NORMAL);
  143. set_pgtable_section(level2_table_0, i, section_l2,
  144. MT_DEVICE_NGNRNE);
  145. section_l1t0 += BLOCK_SIZE_L1;
  146. section_l1t1 += BLOCK_SIZE_L1;
  147. section_l2 += BLOCK_SIZE_L2;
  148. }
  149. level1_table_0[0] =
  150. (u64)level2_table_0 | PMD_TYPE_TABLE;
  151. level1_table_0[2] =
  152. 0x80000000 | PMD_SECT_AF | PMD_TYPE_SECT |
  153. PMD_ATTRINDX(MT_NORMAL);
  154. level1_table_0[3] =
  155. 0xc0000000 | PMD_SECT_AF | PMD_TYPE_SECT |
  156. PMD_ATTRINDX(MT_NORMAL);
  157. /* Rewrite table to enable cache */
  158. set_pgtable_section(level2_table_0,
  159. CONFIG_SYS_FSL_OCRAM_BASE >> SECTION_SHIFT_L2,
  160. CONFIG_SYS_FSL_OCRAM_BASE,
  161. MT_NORMAL);
  162. /*
  163. * Fill in other part of tables if cache is needed
  164. * If finer granularity than 1GB is needed, sub table
  165. * should be created.
  166. */
  167. section_base = FINAL_QBMAN_CACHED_MEM & ~(BLOCK_SIZE_L1 - 1);
  168. i = section_base >> SECTION_SHIFT_L1;
  169. level1_table_0[i] = (u64)level2_table_1 | PMD_TYPE_TABLE;
  170. section_l2 = section_base;
  171. for (i = 0; i < 512; i++) {
  172. set_pgtable_section(level2_table_1, i, section_l2,
  173. MT_DEVICE_NGNRNE);
  174. section_l2 += BLOCK_SIZE_L2;
  175. }
  176. tbl_base = FINAL_QBMAN_CACHED_MEM & (BLOCK_SIZE_L1 - 1);
  177. tbl_limit = (FINAL_QBMAN_CACHED_MEM + FINAL_QBMAN_CACHED_SIZE) &
  178. (BLOCK_SIZE_L1 - 1);
  179. for (i = tbl_base >> SECTION_SHIFT_L2;
  180. i < tbl_limit >> SECTION_SHIFT_L2; i++) {
  181. section_l2 = section_base + (i << SECTION_SHIFT_L2);
  182. set_pgtable_section(level2_table_1, i,
  183. section_l2, MT_NORMAL);
  184. }
  185. /* flush new MMU table */
  186. flush_dcache_range(gd->arch.tlb_addr,
  187. gd->arch.tlb_addr + gd->arch.tlb_size);
  188. /* point TTBR to the new table */
  189. el = current_el();
  190. asm volatile("dsb sy");
  191. if (el == 1) {
  192. asm volatile("msr ttbr0_el1, %0"
  193. : : "r" ((u64)level0_table) : "memory");
  194. } else if (el == 2) {
  195. asm volatile("msr ttbr0_el2, %0"
  196. : : "r" ((u64)level0_table) : "memory");
  197. } else if (el == 3) {
  198. asm volatile("msr ttbr0_el3, %0"
  199. : : "r" ((u64)level0_table) : "memory");
  200. } else {
  201. hang();
  202. }
  203. asm volatile("isb");
  204. /*
  205. * MMU is already enabled, just need to invalidate TLB to load the
  206. * new table. The new table is compatible with the current table, if
  207. * MMU somehow walks through the new table before invalidation TLB,
  208. * it still works. So we don't need to turn off MMU here.
  209. */
  210. }
  211. int arch_cpu_init(void)
  212. {
  213. icache_enable();
  214. __asm_invalidate_dcache_all();
  215. __asm_invalidate_tlb_all();
  216. early_mmu_setup();
  217. set_sctlr(get_sctlr() | CR_C);
  218. return 0;
  219. }
  220. /*
  221. * flush_l3_cache
  222. * Dickens L3 cache can be flushed by transitioning from FAM to SFONLY power
  223. * state, by writing to HP-F P-state request register.
  224. * Fixme: This function should moved to a common file if other SoCs also use
  225. * the same Dickens.
  226. */
  227. #define HNF0_PSTATE_REQ 0x04200010
  228. #define HNF1_PSTATE_REQ 0x04210010
  229. #define HNF2_PSTATE_REQ 0x04220010
  230. #define HNF3_PSTATE_REQ 0x04230010
  231. #define HNF4_PSTATE_REQ 0x04240010
  232. #define HNF5_PSTATE_REQ 0x04250010
  233. #define HNF6_PSTATE_REQ 0x04260010
  234. #define HNF7_PSTATE_REQ 0x04270010
  235. #define HNFPSTAT_MASK (0xFFFFFFFFFFFFFFFC)
  236. #define HNFPSTAT_FAM 0x3
  237. #define HNFPSTAT_SFONLY 0x01
  238. static void hnf_pstate_req(u64 *ptr, u64 state)
  239. {
  240. int timeout = 1000;
  241. out_le64(ptr, (in_le64(ptr) & HNFPSTAT_MASK) | (state & 0x3));
  242. ptr++;
  243. /* checking if the transition is completed */
  244. while (timeout > 0) {
  245. if (((in_le64(ptr) & 0x0c) >> 2) == (state & 0x3))
  246. break;
  247. udelay(100);
  248. timeout--;
  249. }
  250. }
  251. void flush_l3_cache(void)
  252. {
  253. hnf_pstate_req((u64 *)HNF0_PSTATE_REQ, HNFPSTAT_SFONLY);
  254. hnf_pstate_req((u64 *)HNF1_PSTATE_REQ, HNFPSTAT_SFONLY);
  255. hnf_pstate_req((u64 *)HNF2_PSTATE_REQ, HNFPSTAT_SFONLY);
  256. hnf_pstate_req((u64 *)HNF3_PSTATE_REQ, HNFPSTAT_SFONLY);
  257. hnf_pstate_req((u64 *)HNF4_PSTATE_REQ, HNFPSTAT_SFONLY);
  258. hnf_pstate_req((u64 *)HNF5_PSTATE_REQ, HNFPSTAT_SFONLY);
  259. hnf_pstate_req((u64 *)HNF6_PSTATE_REQ, HNFPSTAT_SFONLY);
  260. hnf_pstate_req((u64 *)HNF7_PSTATE_REQ, HNFPSTAT_SFONLY);
  261. hnf_pstate_req((u64 *)HNF0_PSTATE_REQ, HNFPSTAT_FAM);
  262. hnf_pstate_req((u64 *)HNF1_PSTATE_REQ, HNFPSTAT_FAM);
  263. hnf_pstate_req((u64 *)HNF2_PSTATE_REQ, HNFPSTAT_FAM);
  264. hnf_pstate_req((u64 *)HNF3_PSTATE_REQ, HNFPSTAT_FAM);
  265. hnf_pstate_req((u64 *)HNF4_PSTATE_REQ, HNFPSTAT_FAM);
  266. hnf_pstate_req((u64 *)HNF5_PSTATE_REQ, HNFPSTAT_FAM);
  267. hnf_pstate_req((u64 *)HNF6_PSTATE_REQ, HNFPSTAT_FAM);
  268. hnf_pstate_req((u64 *)HNF7_PSTATE_REQ, HNFPSTAT_FAM);
  269. }
  270. /*
  271. * This function is called from lib/board.c.
  272. * It recreates MMU table in main memory. MMU and d-cache are enabled earlier.
  273. * There is no need to disable d-cache for this operation.
  274. */
  275. void enable_caches(void)
  276. {
  277. final_mmu_setup();
  278. __asm_invalidate_tlb_all();
  279. }
  280. #endif
  281. static inline u32 initiator_type(u32 cluster, int init_id)
  282. {
  283. struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  284. u32 idx = (cluster >> (init_id * 8)) & TP_CLUSTER_INIT_MASK;
  285. u32 type = in_le32(&gur->tp_ityp[idx]);
  286. if (type & TP_ITYP_AV)
  287. return type;
  288. return 0;
  289. }
  290. u32 cpu_mask(void)
  291. {
  292. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  293. int i = 0, count = 0;
  294. u32 cluster, type, mask = 0;
  295. do {
  296. int j;
  297. cluster = in_le32(&gur->tp_cluster[i].lower);
  298. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  299. type = initiator_type(cluster, j);
  300. if (type) {
  301. if (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
  302. mask |= 1 << count;
  303. count++;
  304. }
  305. }
  306. i++;
  307. } while ((cluster & TP_CLUSTER_EOC) != TP_CLUSTER_EOC);
  308. return mask;
  309. }
  310. /*
  311. * Return the number of cores on this SOC.
  312. */
  313. int cpu_numcores(void)
  314. {
  315. return hweight32(cpu_mask());
  316. }
  317. int fsl_qoriq_core_to_cluster(unsigned int core)
  318. {
  319. struct ccsr_gur __iomem *gur =
  320. (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
  321. int i = 0, count = 0;
  322. u32 cluster;
  323. do {
  324. int j;
  325. cluster = in_le32(&gur->tp_cluster[i].lower);
  326. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  327. if (initiator_type(cluster, j)) {
  328. if (count == core)
  329. return i;
  330. count++;
  331. }
  332. }
  333. i++;
  334. } while ((cluster & TP_CLUSTER_EOC) != TP_CLUSTER_EOC);
  335. return -1; /* cannot identify the cluster */
  336. }
  337. u32 fsl_qoriq_core_to_type(unsigned int core)
  338. {
  339. struct ccsr_gur __iomem *gur =
  340. (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
  341. int i = 0, count = 0;
  342. u32 cluster, type;
  343. do {
  344. int j;
  345. cluster = in_le32(&gur->tp_cluster[i].lower);
  346. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  347. type = initiator_type(cluster, j);
  348. if (type) {
  349. if (count == core)
  350. return type;
  351. count++;
  352. }
  353. }
  354. i++;
  355. } while ((cluster & TP_CLUSTER_EOC) != TP_CLUSTER_EOC);
  356. return -1; /* cannot identify the cluster */
  357. }
  358. #ifdef CONFIG_DISPLAY_CPUINFO
  359. int print_cpuinfo(void)
  360. {
  361. struct sys_info sysinfo;
  362. char buf[32];
  363. unsigned int i, core;
  364. u32 type;
  365. get_sys_info(&sysinfo);
  366. puts("Clock Configuration:");
  367. for_each_cpu(i, core, cpu_numcores(), cpu_mask()) {
  368. if (!(i % 3))
  369. puts("\n ");
  370. type = TP_ITYP_VER(fsl_qoriq_core_to_type(core));
  371. printf("CPU%d(%s):%-4s MHz ", core,
  372. type == TY_ITYP_VER_A7 ? "A7 " :
  373. (type == TY_ITYP_VER_A53 ? "A53" :
  374. (type == TY_ITYP_VER_A57 ? "A57" : " ")),
  375. strmhz(buf, sysinfo.freq_processor[core]));
  376. }
  377. printf("\n Bus: %-4s MHz ",
  378. strmhz(buf, sysinfo.freq_systembus));
  379. printf("DDR: %-4s MHz", strmhz(buf, sysinfo.freq_ddrbus));
  380. puts("\n");
  381. return 0;
  382. }
  383. #endif
  384. int cpu_eth_init(bd_t *bis)
  385. {
  386. int error = 0;
  387. #ifdef CONFIG_FSL_MC_ENET
  388. error = mc_init(bis);
  389. #endif
  390. return error;
  391. }
  392. int arch_early_init_r(void)
  393. {
  394. int rv;
  395. rv = fsl_lsch3_wake_seconday_cores();
  396. if (rv)
  397. printf("Did not wake secondary cores\n");
  398. return 0;
  399. }