cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 <asm/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 <fm_eth.h>
  20. #include <fsl_debug_server.h>
  21. #include <fsl-mc/fsl_mc.h>
  22. #ifdef CONFIG_FSL_ESDHC
  23. #include <fsl_esdhc.h>
  24. #endif
  25. DECLARE_GLOBAL_DATA_PTR;
  26. void cpu_name(char *name)
  27. {
  28. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  29. unsigned int i, svr, ver;
  30. svr = gur_in32(&gur->svr);
  31. ver = SVR_SOC_VER(svr);
  32. for (i = 0; i < ARRAY_SIZE(cpu_type_list); i++)
  33. if ((cpu_type_list[i].soc_ver & SVR_WO_E) == ver) {
  34. strcpy(name, cpu_type_list[i].name);
  35. if (IS_E_PROCESSOR(svr))
  36. strcat(name, "E");
  37. break;
  38. }
  39. if (i == ARRAY_SIZE(cpu_type_list))
  40. strcpy(name, "unknown");
  41. }
  42. #ifndef CONFIG_SYS_DCACHE_OFF
  43. /*
  44. * Set the block entries according to the information of the table.
  45. */
  46. static int set_block_entry(const struct sys_mmu_table *list,
  47. struct table_info *table)
  48. {
  49. u64 block_size = 0, block_shift = 0;
  50. u64 block_addr, index;
  51. int j;
  52. if (table->entry_size == BLOCK_SIZE_L1) {
  53. block_size = BLOCK_SIZE_L1;
  54. block_shift = SECTION_SHIFT_L1;
  55. } else if (table->entry_size == BLOCK_SIZE_L2) {
  56. block_size = BLOCK_SIZE_L2;
  57. block_shift = SECTION_SHIFT_L2;
  58. } else {
  59. return -EINVAL;
  60. }
  61. block_addr = list->phys_addr;
  62. index = (list->virt_addr - table->table_base) >> block_shift;
  63. for (j = 0; j < (list->size >> block_shift); j++) {
  64. set_pgtable_section(table->ptr,
  65. index,
  66. block_addr,
  67. list->memory_type,
  68. list->share);
  69. block_addr += block_size;
  70. index++;
  71. }
  72. return 0;
  73. }
  74. /*
  75. * Find the corresponding table entry for the list.
  76. */
  77. static int find_table(const struct sys_mmu_table *list,
  78. struct table_info *table, u64 *level0_table)
  79. {
  80. u64 index = 0, level = 0;
  81. u64 *level_table = level0_table;
  82. u64 temp_base = 0, block_size = 0, block_shift = 0;
  83. while (level < 3) {
  84. if (level == 0) {
  85. block_size = BLOCK_SIZE_L0;
  86. block_shift = SECTION_SHIFT_L0;
  87. } else if (level == 1) {
  88. block_size = BLOCK_SIZE_L1;
  89. block_shift = SECTION_SHIFT_L1;
  90. } else if (level == 2) {
  91. block_size = BLOCK_SIZE_L2;
  92. block_shift = SECTION_SHIFT_L2;
  93. }
  94. index = 0;
  95. while (list->virt_addr >= temp_base) {
  96. index++;
  97. temp_base += block_size;
  98. }
  99. temp_base -= block_size;
  100. if ((level_table[index - 1] & PMD_TYPE_MASK) ==
  101. PMD_TYPE_TABLE) {
  102. level_table = (u64 *)(level_table[index - 1] &
  103. ~PMD_TYPE_MASK);
  104. level++;
  105. continue;
  106. } else {
  107. if (level == 0)
  108. return -EINVAL;
  109. if ((list->phys_addr + list->size) >
  110. (temp_base + block_size * NUM_OF_ENTRY))
  111. return -EINVAL;
  112. /*
  113. * Check the address and size of the list member is
  114. * aligned with the block size.
  115. */
  116. if (((list->phys_addr & (block_size - 1)) != 0) ||
  117. ((list->size & (block_size - 1)) != 0))
  118. return -EINVAL;
  119. table->ptr = level_table;
  120. table->table_base = temp_base -
  121. ((index - 1) << block_shift);
  122. table->entry_size = block_size;
  123. return 0;
  124. }
  125. }
  126. return -EINVAL;
  127. }
  128. /*
  129. * To start MMU before DDR is available, we create MMU table in SRAM.
  130. * The base address of SRAM is CONFIG_SYS_FSL_OCRAM_BASE. We use three
  131. * levels of translation tables here to cover 40-bit address space.
  132. * We use 4KB granule size, with 40 bits physical address, T0SZ=24
  133. * Level 0 IA[39], table address @0
  134. * Level 1 IA[38:30], table address @0x1000, 0x2000
  135. * Level 2 IA[29:21], table address @0x3000, 0x4000
  136. * Address above 0x5000 is free for other purpose.
  137. */
  138. static inline void early_mmu_setup(void)
  139. {
  140. unsigned int el, i;
  141. u64 *level0_table = (u64 *)CONFIG_SYS_FSL_OCRAM_BASE;
  142. u64 *level1_table0 = (u64 *)(CONFIG_SYS_FSL_OCRAM_BASE + 0x1000);
  143. u64 *level1_table1 = (u64 *)(CONFIG_SYS_FSL_OCRAM_BASE + 0x2000);
  144. u64 *level2_table0 = (u64 *)(CONFIG_SYS_FSL_OCRAM_BASE + 0x3000);
  145. u64 *level2_table1 = (u64 *)(CONFIG_SYS_FSL_OCRAM_BASE + 0x4000);
  146. struct table_info table = {level0_table, 0, BLOCK_SIZE_L0};
  147. /* Invalidate all table entries */
  148. memset(level0_table, 0, 0x5000);
  149. /* Fill in the table entries */
  150. set_pgtable_table(level0_table, 0, level1_table0);
  151. set_pgtable_table(level0_table, 1, level1_table1);
  152. set_pgtable_table(level1_table0, 0, level2_table0);
  153. #ifdef CONFIG_FSL_LSCH3
  154. set_pgtable_table(level1_table0,
  155. CONFIG_SYS_FLASH_BASE >> SECTION_SHIFT_L1,
  156. level2_table1);
  157. #endif
  158. /* Find the table and fill in the block entries */
  159. for (i = 0; i < ARRAY_SIZE(early_mmu_table); i++) {
  160. if (find_table(&early_mmu_table[i],
  161. &table, level0_table) == 0) {
  162. /*
  163. * If find_table() returns error, it cannot be dealt
  164. * with here. Breakpoint can be added for debugging.
  165. */
  166. set_block_entry(&early_mmu_table[i], &table);
  167. /*
  168. * If set_block_entry() returns error, it cannot be
  169. * dealt with here too.
  170. */
  171. }
  172. }
  173. el = current_el();
  174. set_ttbr_tcr_mair(el, (u64)level0_table, LAYERSCAPE_TCR,
  175. MEMORY_ATTRIBUTES);
  176. set_sctlr(get_sctlr() | CR_M);
  177. }
  178. /*
  179. * The final tables look similar to early tables, but different in detail.
  180. * These tables are in DRAM. Sub tables are added to enable cache for
  181. * QBMan and OCRAM.
  182. *
  183. * Level 1 table 0 contains 512 entries for each 1GB from 0 to 512GB.
  184. * Level 1 table 1 contains 512 entries for each 1GB from 512GB to 1TB.
  185. * Level 2 table 0 contains 512 entries for each 2MB from 0 to 1GB.
  186. *
  187. * For LSCH3:
  188. * Level 2 table 1 contains 512 entries for each 2MB from 32GB to 33GB.
  189. */
  190. static inline void final_mmu_setup(void)
  191. {
  192. unsigned int el, i;
  193. u64 *level0_table = (u64 *)gd->arch.tlb_addr;
  194. u64 *level1_table0 = (u64 *)(gd->arch.tlb_addr + 0x1000);
  195. u64 *level1_table1 = (u64 *)(gd->arch.tlb_addr + 0x2000);
  196. u64 *level2_table0 = (u64 *)(gd->arch.tlb_addr + 0x3000);
  197. #ifdef CONFIG_FSL_LSCH3
  198. u64 *level2_table1 = (u64 *)(gd->arch.tlb_addr + 0x4000);
  199. #endif
  200. struct table_info table = {level0_table, 0, BLOCK_SIZE_L0};
  201. /* Invalidate all table entries */
  202. memset(level0_table, 0, PGTABLE_SIZE);
  203. /* Fill in the table entries */
  204. set_pgtable_table(level0_table, 0, level1_table0);
  205. set_pgtable_table(level0_table, 1, level1_table1);
  206. set_pgtable_table(level1_table0, 0, level2_table0);
  207. #ifdef CONFIG_FSL_LSCH3
  208. set_pgtable_table(level1_table0,
  209. CONFIG_SYS_FSL_QBMAN_BASE >> SECTION_SHIFT_L1,
  210. level2_table1);
  211. #endif
  212. /* Find the table and fill in the block entries */
  213. for (i = 0; i < ARRAY_SIZE(final_mmu_table); i++) {
  214. if (find_table(&final_mmu_table[i],
  215. &table, level0_table) == 0) {
  216. if (set_block_entry(&final_mmu_table[i],
  217. &table) != 0) {
  218. printf("MMU error: could not set block entry for %p\n",
  219. &final_mmu_table[i]);
  220. }
  221. } else {
  222. printf("MMU error: could not find the table for %p\n",
  223. &final_mmu_table[i]);
  224. }
  225. }
  226. /* flush new MMU table */
  227. flush_dcache_range(gd->arch.tlb_addr,
  228. gd->arch.tlb_addr + gd->arch.tlb_size);
  229. /* point TTBR to the new table */
  230. el = current_el();
  231. set_ttbr_tcr_mair(el, (u64)level0_table, LAYERSCAPE_TCR_FINAL,
  232. MEMORY_ATTRIBUTES);
  233. /*
  234. * MMU is already enabled, just need to invalidate TLB to load the
  235. * new table. The new table is compatible with the current table, if
  236. * MMU somehow walks through the new table before invalidation TLB,
  237. * it still works. So we don't need to turn off MMU here.
  238. */
  239. }
  240. int arch_cpu_init(void)
  241. {
  242. icache_enable();
  243. __asm_invalidate_dcache_all();
  244. __asm_invalidate_tlb_all();
  245. early_mmu_setup();
  246. set_sctlr(get_sctlr() | CR_C);
  247. return 0;
  248. }
  249. /*
  250. * This function is called from lib/board.c.
  251. * It recreates MMU table in main memory. MMU and d-cache are enabled earlier.
  252. * There is no need to disable d-cache for this operation.
  253. */
  254. void enable_caches(void)
  255. {
  256. final_mmu_setup();
  257. __asm_invalidate_tlb_all();
  258. }
  259. #endif
  260. static inline u32 initiator_type(u32 cluster, int init_id)
  261. {
  262. struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  263. u32 idx = (cluster >> (init_id * 8)) & TP_CLUSTER_INIT_MASK;
  264. u32 type = 0;
  265. type = gur_in32(&gur->tp_ityp[idx]);
  266. if (type & TP_ITYP_AV)
  267. return type;
  268. return 0;
  269. }
  270. u32 cpu_mask(void)
  271. {
  272. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  273. int i = 0, count = 0;
  274. u32 cluster, type, mask = 0;
  275. do {
  276. int j;
  277. cluster = gur_in32(&gur->tp_cluster[i].lower);
  278. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  279. type = initiator_type(cluster, j);
  280. if (type) {
  281. if (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
  282. mask |= 1 << count;
  283. count++;
  284. }
  285. }
  286. i++;
  287. } while ((cluster & TP_CLUSTER_EOC) == 0x0);
  288. return mask;
  289. }
  290. /*
  291. * Return the number of cores on this SOC.
  292. */
  293. int cpu_numcores(void)
  294. {
  295. return hweight32(cpu_mask());
  296. }
  297. int fsl_qoriq_core_to_cluster(unsigned int core)
  298. {
  299. struct ccsr_gur __iomem *gur =
  300. (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
  301. int i = 0, count = 0;
  302. u32 cluster;
  303. do {
  304. int j;
  305. cluster = gur_in32(&gur->tp_cluster[i].lower);
  306. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  307. if (initiator_type(cluster, j)) {
  308. if (count == core)
  309. return i;
  310. count++;
  311. }
  312. }
  313. i++;
  314. } while ((cluster & TP_CLUSTER_EOC) == 0x0);
  315. return -1; /* cannot identify the cluster */
  316. }
  317. u32 fsl_qoriq_core_to_type(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, type;
  323. do {
  324. int j;
  325. cluster = gur_in32(&gur->tp_cluster[i].lower);
  326. for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
  327. type = initiator_type(cluster, j);
  328. if (type) {
  329. if (count == core)
  330. return type;
  331. count++;
  332. }
  333. }
  334. i++;
  335. } while ((cluster & TP_CLUSTER_EOC) == 0x0);
  336. return -1; /* cannot identify the cluster */
  337. }
  338. #ifdef CONFIG_DISPLAY_CPUINFO
  339. int print_cpuinfo(void)
  340. {
  341. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  342. struct sys_info sysinfo;
  343. char buf[32];
  344. unsigned int i, core;
  345. u32 type, rcw;
  346. puts("SoC: ");
  347. cpu_name(buf);
  348. printf(" %s (0x%x)\n", buf, gur_in32(&gur->svr));
  349. memset((u8 *)buf, 0x00, ARRAY_SIZE(buf));
  350. get_sys_info(&sysinfo);
  351. puts("Clock Configuration:");
  352. for_each_cpu(i, core, cpu_numcores(), cpu_mask()) {
  353. if (!(i % 3))
  354. puts("\n ");
  355. type = TP_ITYP_VER(fsl_qoriq_core_to_type(core));
  356. printf("CPU%d(%s):%-4s MHz ", core,
  357. type == TY_ITYP_VER_A7 ? "A7 " :
  358. (type == TY_ITYP_VER_A53 ? "A53" :
  359. (type == TY_ITYP_VER_A57 ? "A57" : " ")),
  360. strmhz(buf, sysinfo.freq_processor[core]));
  361. }
  362. printf("\n Bus: %-4s MHz ",
  363. strmhz(buf, sysinfo.freq_systembus));
  364. printf("DDR: %-4s MT/s", strmhz(buf, sysinfo.freq_ddrbus));
  365. #ifdef CONFIG_FSL_LSCH3
  366. printf(" DP-DDR: %-4s MT/s", strmhz(buf, sysinfo.freq_ddrbus2));
  367. #endif
  368. puts("\n");
  369. /*
  370. * Display the RCW, so that no one gets confused as to what RCW
  371. * we're actually using for this boot.
  372. */
  373. puts("Reset Configuration Word (RCW):");
  374. for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
  375. rcw = gur_in32(&gur->rcwsr[i]);
  376. if ((i % 4) == 0)
  377. printf("\n %08x:", i * 4);
  378. printf(" %08x", rcw);
  379. }
  380. puts("\n");
  381. return 0;
  382. }
  383. #endif
  384. #ifdef CONFIG_FSL_ESDHC
  385. int cpu_mmc_init(bd_t *bis)
  386. {
  387. return fsl_esdhc_mmc_init(bis);
  388. }
  389. #endif
  390. int cpu_eth_init(bd_t *bis)
  391. {
  392. int error = 0;
  393. #ifdef CONFIG_FSL_MC_ENET
  394. error = fsl_mc_ldpaa_init(bis);
  395. #endif
  396. return error;
  397. }
  398. int arch_early_init_r(void)
  399. {
  400. #ifdef CONFIG_MP
  401. int rv = 1;
  402. rv = fsl_layerscape_wake_seconday_cores();
  403. if (rv)
  404. printf("Did not wake secondary cores\n");
  405. #endif
  406. #ifdef CONFIG_SYS_HAS_SERDES
  407. fsl_serdes_init();
  408. #endif
  409. return 0;
  410. }
  411. int timer_init(void)
  412. {
  413. u32 __iomem *cntcr = (u32 *)CONFIG_SYS_FSL_TIMER_ADDR;
  414. #ifdef CONFIG_FSL_LSCH3
  415. u32 __iomem *cltbenr = (u32 *)CONFIG_SYS_FSL_PMU_CLTBENR;
  416. #endif
  417. #ifdef COUNTER_FREQUENCY_REAL
  418. unsigned long cntfrq = COUNTER_FREQUENCY_REAL;
  419. /* Update with accurate clock frequency */
  420. asm volatile("msr cntfrq_el0, %0" : : "r" (cntfrq) : "memory");
  421. #endif
  422. #ifdef CONFIG_FSL_LSCH3
  423. /* Enable timebase for all clusters.
  424. * It is safe to do so even some clusters are not enabled.
  425. */
  426. out_le32(cltbenr, 0xf);
  427. #endif
  428. /* Enable clock for timer
  429. * This is a global setting.
  430. */
  431. out_le32(cntcr, 0x1);
  432. return 0;
  433. }
  434. void reset_cpu(ulong addr)
  435. {
  436. u32 __iomem *rstcr = (u32 *)CONFIG_SYS_FSL_RST_ADDR;
  437. u32 val;
  438. /* Raise RESET_REQ_B */
  439. val = scfg_in32(rstcr);
  440. val |= 0x02;
  441. scfg_out32(rstcr, val);
  442. }