cpu.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <asm/arch/clock.h>
  7. #include <asm/io.h>
  8. #include <asm/arch/immap_ls102xa.h>
  9. #include <asm/cache.h>
  10. #include <asm/system.h>
  11. #include <tsec.h>
  12. #include <netdev.h>
  13. #include <fsl_esdhc.h>
  14. #include <config.h>
  15. #include <fsl_wdog.h>
  16. #include "fsl_epu.h"
  17. #define DCSR_RCPM2_BLOCK_OFFSET 0x223000
  18. #define DCSR_RCPM2_CPMFSMCR0 0x400
  19. #define DCSR_RCPM2_CPMFSMSR0 0x404
  20. #define DCSR_RCPM2_CPMFSMCR1 0x414
  21. #define DCSR_RCPM2_CPMFSMSR1 0x418
  22. #define CPMFSMSR_FSM_STATE_MASK 0x7f
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #ifndef CONFIG_SYS_DCACHE_OFF
  25. /*
  26. * Bit[1] of the descriptor indicates the descriptor type,
  27. * and bit[0] indicates whether the descriptor is valid.
  28. */
  29. #define PMD_TYPE_TABLE 0x3
  30. #define PMD_TYPE_SECT 0x1
  31. /* AttrIndx[2:0] */
  32. #define PMD_ATTRINDX(t) ((t) << 2)
  33. /* Section */
  34. #define PMD_SECT_AF (1 << 10)
  35. #define BLOCK_SIZE_L1 (1UL << 30)
  36. #define BLOCK_SIZE_L2 (1UL << 21)
  37. /* TTBCR flags */
  38. #define TTBCR_EAE (1 << 31)
  39. #define TTBCR_T0SZ(x) ((x) << 0)
  40. #define TTBCR_T1SZ(x) ((x) << 16)
  41. #define TTBCR_USING_TTBR0 (TTBCR_T0SZ(0) | TTBCR_T1SZ(0))
  42. #define TTBCR_IRGN0_NC (0 << 8)
  43. #define TTBCR_IRGN0_WBWA (1 << 8)
  44. #define TTBCR_IRGN0_WT (2 << 8)
  45. #define TTBCR_IRGN0_WBNWA (3 << 8)
  46. #define TTBCR_IRGN0_MASK (3 << 8)
  47. #define TTBCR_ORGN0_NC (0 << 10)
  48. #define TTBCR_ORGN0_WBWA (1 << 10)
  49. #define TTBCR_ORGN0_WT (2 << 10)
  50. #define TTBCR_ORGN0_WBNWA (3 << 10)
  51. #define TTBCR_ORGN0_MASK (3 << 10)
  52. #define TTBCR_SHARED_NON (0 << 12)
  53. #define TTBCR_SHARED_OUTER (2 << 12)
  54. #define TTBCR_SHARED_INNER (3 << 12)
  55. #define TTBCR_EPD0 (0 << 7)
  56. #define TTBCR (TTBCR_SHARED_NON | \
  57. TTBCR_ORGN0_NC | \
  58. TTBCR_IRGN0_NC | \
  59. TTBCR_USING_TTBR0 | \
  60. TTBCR_EAE)
  61. /*
  62. * Memory region attributes for LPAE (defined in pgtable):
  63. *
  64. * n = AttrIndx[2:0]
  65. *
  66. * n MAIR
  67. * UNCACHED 000 00000000
  68. * BUFFERABLE 001 01000100
  69. * DEV_WC 001 01000100
  70. * WRITETHROUGH 010 10101010
  71. * WRITEBACK 011 11101110
  72. * DEV_CACHED 011 11101110
  73. * DEV_SHARED 100 00000100
  74. * DEV_NONSHARED 100 00000100
  75. * unused 101
  76. * unused 110
  77. * WRITEALLOC 111 11111111
  78. */
  79. #define MT_MAIR0 0xeeaa4400
  80. #define MT_MAIR1 0xff000004
  81. #define MT_STRONLY_ORDER 0
  82. #define MT_NORMAL_NC 1
  83. #define MT_DEVICE_MEM 4
  84. #define MT_NORMAL 7
  85. /* The phy_addr must be aligned to 4KB */
  86. static inline void set_pgtable(u32 *page_table, u32 index, u32 phy_addr)
  87. {
  88. u32 value = phy_addr | PMD_TYPE_TABLE;
  89. page_table[2 * index] = value;
  90. page_table[2 * index + 1] = 0;
  91. }
  92. /* The phy_addr must be aligned to 4KB */
  93. static inline void set_pgsection(u32 *page_table, u32 index, u64 phy_addr,
  94. u32 memory_type)
  95. {
  96. u64 value;
  97. value = phy_addr | PMD_TYPE_SECT | PMD_SECT_AF;
  98. value |= PMD_ATTRINDX(memory_type);
  99. page_table[2 * index] = value & 0xFFFFFFFF;
  100. page_table[2 * index + 1] = (value >> 32) & 0xFFFFFFFF;
  101. }
  102. /*
  103. * Start MMU after DDR is available, we create MMU table in DRAM.
  104. * The base address of TTLB is gd->arch.tlb_addr. We use two
  105. * levels of translation tables here to cover 40-bit address space.
  106. *
  107. * The TTLBs are located at PHY 2G~4G.
  108. *
  109. * VA mapping:
  110. *
  111. * ------- <---- 0GB
  112. * | |
  113. * | |
  114. * |-------| <---- 0x24000000
  115. * |///////| ===> 192MB VA map for PCIe1 with offset 0x40_0000_0000
  116. * |-------| <---- 0x300000000
  117. * | |
  118. * |-------| <---- 0x34000000
  119. * |///////| ===> 192MB VA map for PCIe2 with offset 0x48_0000_0000
  120. * |-------| <---- 0x40000000
  121. * | |
  122. * |-------| <---- 0x80000000 DDR0 space start
  123. * |\\\\\\\|
  124. *.|\\\\\\\| ===> 2GB VA map for 2GB DDR0 Memory space
  125. * |\\\\\\\|
  126. * ------- <---- 4GB DDR0 space end
  127. */
  128. static void mmu_setup(void)
  129. {
  130. u32 *level0_table = (u32 *)gd->arch.tlb_addr;
  131. u32 *level1_table = (u32 *)(gd->arch.tlb_addr + 0x1000);
  132. u64 va_start = 0;
  133. u32 reg;
  134. int i;
  135. /* Level 0 Table 2-3 are used to map DDR */
  136. set_pgsection(level0_table, 3, 3 * BLOCK_SIZE_L1, MT_NORMAL);
  137. set_pgsection(level0_table, 2, 2 * BLOCK_SIZE_L1, MT_NORMAL);
  138. /* Level 0 Table 1 is used to map device */
  139. set_pgsection(level0_table, 1, 1 * BLOCK_SIZE_L1, MT_DEVICE_MEM);
  140. /* Level 0 Table 0 is used to map device including PCIe MEM */
  141. set_pgtable(level0_table, 0, (u32)level1_table);
  142. /* Level 1 has 512 entries */
  143. for (i = 0; i < 512; i++) {
  144. /* Mapping for PCIe 1 */
  145. if (va_start >= CONFIG_SYS_PCIE1_VIRT_ADDR &&
  146. va_start < (CONFIG_SYS_PCIE1_VIRT_ADDR +
  147. CONFIG_SYS_PCIE_MMAP_SIZE))
  148. set_pgsection(level1_table, i,
  149. CONFIG_SYS_PCIE1_PHYS_BASE + va_start,
  150. MT_DEVICE_MEM);
  151. /* Mapping for PCIe 2 */
  152. else if (va_start >= CONFIG_SYS_PCIE2_VIRT_ADDR &&
  153. va_start < (CONFIG_SYS_PCIE2_VIRT_ADDR +
  154. CONFIG_SYS_PCIE_MMAP_SIZE))
  155. set_pgsection(level1_table, i,
  156. CONFIG_SYS_PCIE2_PHYS_BASE + va_start,
  157. MT_DEVICE_MEM);
  158. else
  159. set_pgsection(level1_table, i,
  160. va_start,
  161. MT_DEVICE_MEM);
  162. va_start += BLOCK_SIZE_L2;
  163. }
  164. asm volatile("dsb sy;isb");
  165. asm volatile("mcr p15, 0, %0, c2, c0, 2" /* Write RT to TTBCR */
  166. : : "r" (TTBCR) : "memory");
  167. asm volatile("mcrr p15, 0, %0, %1, c2" /* TTBR 0 */
  168. : : "r" ((u32)level0_table), "r" (0) : "memory");
  169. asm volatile("mcr p15, 0, %0, c10, c2, 0" /* write MAIR 0 */
  170. : : "r" (MT_MAIR0) : "memory");
  171. asm volatile("mcr p15, 0, %0, c10, c2, 1" /* write MAIR 1 */
  172. : : "r" (MT_MAIR1) : "memory");
  173. /* Set the access control to all-supervisor */
  174. asm volatile("mcr p15, 0, %0, c3, c0, 0"
  175. : : "r" (~0));
  176. /* Enable the mmu */
  177. reg = get_cr();
  178. set_cr(reg | CR_M);
  179. }
  180. /*
  181. * This function is called from lib/board.c. It recreates MMU
  182. * table in main memory. MMU and i/d-cache are enabled here.
  183. */
  184. void enable_caches(void)
  185. {
  186. /* Invalidate all TLB */
  187. mmu_page_table_flush(gd->arch.tlb_addr,
  188. gd->arch.tlb_addr + gd->arch.tlb_size);
  189. /* Set up and enable mmu */
  190. mmu_setup();
  191. /* Invalidate & Enable d-cache */
  192. invalidate_dcache_all();
  193. set_cr(get_cr() | CR_C);
  194. }
  195. #endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
  196. uint get_svr(void)
  197. {
  198. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  199. return in_be32(&gur->svr);
  200. }
  201. #if defined(CONFIG_DISPLAY_CPUINFO)
  202. int print_cpuinfo(void)
  203. {
  204. char buf1[32], buf2[32];
  205. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  206. unsigned int svr, major, minor, ver, i;
  207. svr = in_be32(&gur->svr);
  208. major = SVR_MAJ(svr);
  209. minor = SVR_MIN(svr);
  210. puts("CPU: Freescale LayerScape ");
  211. ver = SVR_SOC_VER(svr);
  212. switch (ver) {
  213. case SOC_VER_SLS1020:
  214. puts("SLS1020");
  215. break;
  216. case SOC_VER_LS1020:
  217. puts("LS1020");
  218. break;
  219. case SOC_VER_LS1021:
  220. puts("LS1021");
  221. break;
  222. case SOC_VER_LS1022:
  223. puts("LS1022");
  224. break;
  225. default:
  226. puts("Unknown");
  227. break;
  228. }
  229. if (IS_E_PROCESSOR(svr) && (ver != SOC_VER_SLS1020))
  230. puts("E");
  231. printf(", Version: %d.%d, (0x%08x)\n", major, minor, svr);
  232. puts("Clock Configuration:");
  233. printf("\n CPU0(ARMV7):%-4s MHz, ", strmhz(buf1, gd->cpu_clk));
  234. printf("\n Bus:%-4s MHz, ", strmhz(buf1, gd->bus_clk));
  235. printf("DDR:%-4s MHz (%s MT/s data rate), ",
  236. strmhz(buf1, gd->mem_clk/2), strmhz(buf2, gd->mem_clk));
  237. puts("\n");
  238. /* Display the RCW, so that no one gets confused as to what RCW
  239. * we're actually using for this boot.
  240. */
  241. puts("Reset Configuration Word (RCW):");
  242. for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
  243. u32 rcw = in_be32(&gur->rcwsr[i]);
  244. if ((i % 4) == 0)
  245. printf("\n %08x:", i * 4);
  246. printf(" %08x", rcw);
  247. }
  248. puts("\n");
  249. return 0;
  250. }
  251. #endif
  252. #ifdef CONFIG_FSL_ESDHC
  253. int cpu_mmc_init(bd_t *bis)
  254. {
  255. return fsl_esdhc_mmc_init(bis);
  256. }
  257. #endif
  258. int cpu_eth_init(bd_t *bis)
  259. {
  260. #ifdef CONFIG_TSEC_ENET
  261. tsec_standard_init(bis);
  262. #endif
  263. return 0;
  264. }
  265. int arch_cpu_init(void)
  266. {
  267. void *epu_base = (void *)(CONFIG_SYS_DCSRBAR + EPU_BLOCK_OFFSET);
  268. void *rcpm2_base =
  269. (void *)(CONFIG_SYS_DCSRBAR + DCSR_RCPM2_BLOCK_OFFSET);
  270. struct ccsr_scfg *scfg = (void *)CONFIG_SYS_FSL_SCFG_ADDR;
  271. u32 state;
  272. /*
  273. * The RCPM FSM state may not be reset after power-on.
  274. * So, reset them.
  275. */
  276. state = in_be32(rcpm2_base + DCSR_RCPM2_CPMFSMSR0) &
  277. CPMFSMSR_FSM_STATE_MASK;
  278. if (state != 0) {
  279. out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR0, 0x80);
  280. out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR0, 0x0);
  281. }
  282. state = in_be32(rcpm2_base + DCSR_RCPM2_CPMFSMSR1) &
  283. CPMFSMSR_FSM_STATE_MASK;
  284. if (state != 0) {
  285. out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR1, 0x80);
  286. out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR1, 0x0);
  287. }
  288. /*
  289. * After wakeup from deep sleep, Clear EPU registers
  290. * as early as possible to prevent from possible issue.
  291. * It's also safe to clear at normal boot.
  292. */
  293. fsl_epu_clean(epu_base);
  294. setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SEC_RD_WR);
  295. return 0;
  296. }
  297. #ifdef CONFIG_ARMV7_NONSEC
  298. /* Set the address at which the secondary core starts from.*/
  299. void smp_set_core_boot_addr(unsigned long addr, int corenr)
  300. {
  301. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  302. out_be32(&gur->scratchrw[0], addr);
  303. }
  304. /* Release the secondary core from holdoff state and kick it */
  305. void smp_kick_all_cpus(void)
  306. {
  307. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  308. out_be32(&gur->brrl, 0x2);
  309. /*
  310. * LS1 STANDBYWFE is not captured outside the ARM module in the soc.
  311. * So add a delay to wait bootrom execute WFE.
  312. */
  313. udelay(1);
  314. asm volatile("sev");
  315. }
  316. #endif
  317. void reset_cpu(ulong addr)
  318. {
  319. struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
  320. clrbits_be16(&wdog->wcr, WCR_SRS);
  321. while (1) {
  322. /*
  323. * Let the watchdog trigger
  324. */
  325. }
  326. }
  327. void arch_preboot_os(void)
  328. {
  329. unsigned long ctrl;
  330. /* Disable PL1 Physical Timer */
  331. asm("mrc p15, 0, %0, c14, c2, 1" : "=r" (ctrl));
  332. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  333. asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl));
  334. }