blob: 6c87c1b11accafd6f1af101d162dc06e2de3ed1a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mingkai Hu0e58b512015-10-26 19:47:50 +08002/*
Pankaj Bansal6c772772019-10-31 05:41:09 +00003 * Copyright 2017-2019 NXP
Mingkai Hu0e58b512015-10-26 19:47:50 +08004 * Copyright 2014-2015 Freescale Semiconductor, Inc.
Mingkai Hu0e58b512015-10-26 19:47:50 +08005 */
6
7#include <common.h>
Simon Glass33d1e702019-11-14 12:57:32 -07008#include <cpu_func.h>
Simon Glassdb229612019-08-01 09:46:42 -06009#include <env.h>
Simon Glass0e0ac202017-04-06 12:47:04 -060010#include <fsl_ddr_sdram.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070011#include <vsprintf.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080012#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080014#include <asm/system.h>
Joe Hershberger8f454d92018-07-16 15:33:51 -050015#include <fm_eth.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080016#include <asm/armv8/mmu.h>
17#include <asm/io.h>
18#include <asm/arch/fsl_serdes.h>
19#include <asm/arch/soc.h>
20#include <asm/arch/cpu.h>
21#include <asm/arch/speed.h>
Ashish Kumar11234062017-08-11 11:09:14 +053022#include <fsl_immap.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080023#include <asm/arch/mp.h>
Alexander Graf12be31c2016-11-17 01:03:01 +010024#include <efi_loader.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080025#include <fsl-mc/fsl_mc.h>
26#ifdef CONFIG_FSL_ESDHC
27#include <fsl_esdhc.h>
28#endif
Hou Zhiqiang21c4d552016-06-28 20:18:15 +080029#include <asm/armv8/sec_firmware.h>
Shengzhou Liu15875a52016-11-21 11:36:48 +080030#ifdef CONFIG_SYS_FSL_DDR
31#include <fsl_ddr.h>
32#endif
Simon Glass243182c2017-05-17 08:23:06 -060033#include <asm/arch/clock.h>
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +053034#include <hwconfig.h>
Ahmed Mansouraa270b42017-12-15 16:01:00 -050035#include <fsl_qbman.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080036
Rajesh Bhagat583da8b2018-11-05 18:01:42 +000037#ifdef CONFIG_TFABOOT
Simon Glass9d1f6192019-08-02 09:44:25 -060038#include <env_internal.h>
Pankit Gargbdbf84f2018-11-05 18:01:52 +000039#ifdef CONFIG_CHAIN_OF_TRUST
40#include <fsl_validate.h>
41#endif
Rajesh Bhagat583da8b2018-11-05 18:01:42 +000042#endif
Simon Glasscaefa322019-11-14 12:57:31 -070043#include <linux/mii.h>
Rajesh Bhagat583da8b2018-11-05 18:01:42 +000044
Mingkai Hu0e58b512015-10-26 19:47:50 +080045DECLARE_GLOBAL_DATA_PTR;
46
York Sunef4cef92018-11-05 18:01:06 +000047static struct cpu_type cpu_type_list[] = {
48 CPU_TYPE_ENTRY(LS2080A, LS2080A, 8),
49 CPU_TYPE_ENTRY(LS2085A, LS2085A, 8),
50 CPU_TYPE_ENTRY(LS2045A, LS2045A, 4),
51 CPU_TYPE_ENTRY(LS2088A, LS2088A, 8),
52 CPU_TYPE_ENTRY(LS2084A, LS2084A, 8),
53 CPU_TYPE_ENTRY(LS2048A, LS2048A, 4),
54 CPU_TYPE_ENTRY(LS2044A, LS2044A, 4),
55 CPU_TYPE_ENTRY(LS2081A, LS2081A, 8),
56 CPU_TYPE_ENTRY(LS2041A, LS2041A, 4),
57 CPU_TYPE_ENTRY(LS1043A, LS1043A, 4),
Hou Zhiqiangb9aedf92018-12-20 06:31:17 +000058 CPU_TYPE_ENTRY(LS1043A, LS1043A_P23, 4),
York Sunef4cef92018-11-05 18:01:06 +000059 CPU_TYPE_ENTRY(LS1023A, LS1023A, 2),
Hou Zhiqiangb9aedf92018-12-20 06:31:17 +000060 CPU_TYPE_ENTRY(LS1023A, LS1023A_P23, 2),
York Sunef4cef92018-11-05 18:01:06 +000061 CPU_TYPE_ENTRY(LS1046A, LS1046A, 4),
62 CPU_TYPE_ENTRY(LS1026A, LS1026A, 2),
63 CPU_TYPE_ENTRY(LS2040A, LS2040A, 4),
64 CPU_TYPE_ENTRY(LS1012A, LS1012A, 1),
Yuantian Tangf463d752019-09-18 16:50:52 +080065 CPU_TYPE_ENTRY(LS1017A, LS1017A, 1),
66 CPU_TYPE_ENTRY(LS1018A, LS1018A, 1),
67 CPU_TYPE_ENTRY(LS1027A, LS1027A, 2),
Yuantian Tang4aefa162019-04-10 16:43:33 +080068 CPU_TYPE_ENTRY(LS1028A, LS1028A, 2),
York Sunef4cef92018-11-05 18:01:06 +000069 CPU_TYPE_ENTRY(LS1088A, LS1088A, 8),
70 CPU_TYPE_ENTRY(LS1084A, LS1084A, 8),
71 CPU_TYPE_ENTRY(LS1048A, LS1048A, 4),
72 CPU_TYPE_ENTRY(LS1044A, LS1044A, 4),
Priyanka Jainef76b2e2018-10-29 09:17:09 +000073 CPU_TYPE_ENTRY(LX2160A, LX2160A, 16),
74 CPU_TYPE_ENTRY(LX2120A, LX2120A, 12),
75 CPU_TYPE_ENTRY(LX2080A, LX2080A, 8),
York Sunef4cef92018-11-05 18:01:06 +000076};
77
78#define EARLY_PGTABLE_SIZE 0x5000
79static struct mm_region early_map[] = {
80#ifdef CONFIG_FSL_LSCH3
81 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
82 CONFIG_SYS_FSL_CCSR_SIZE,
83 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
84 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
85 },
86 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
87 SYS_FSL_OCRAM_SPACE_SIZE,
88 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
89 },
90 { CONFIG_SYS_FSL_QSPI_BASE1, CONFIG_SYS_FSL_QSPI_BASE1,
91 CONFIG_SYS_FSL_QSPI_SIZE1,
92 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE},
93#ifdef CONFIG_FSL_IFC
94 /* For IFC Region #1, only the first 4MB is cache-enabled */
95 { CONFIG_SYS_FSL_IFC_BASE1, CONFIG_SYS_FSL_IFC_BASE1,
96 CONFIG_SYS_FSL_IFC_SIZE1_1,
97 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
98 },
99 { CONFIG_SYS_FSL_IFC_BASE1 + CONFIG_SYS_FSL_IFC_SIZE1_1,
100 CONFIG_SYS_FSL_IFC_BASE1 + CONFIG_SYS_FSL_IFC_SIZE1_1,
101 CONFIG_SYS_FSL_IFC_SIZE1 - CONFIG_SYS_FSL_IFC_SIZE1_1,
102 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
103 },
104 { CONFIG_SYS_FLASH_BASE, CONFIG_SYS_FSL_IFC_BASE1,
105 CONFIG_SYS_FSL_IFC_SIZE1,
106 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
107 },
108#endif
109 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
110 CONFIG_SYS_FSL_DRAM_SIZE1,
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +0000111#if defined(CONFIG_TFABOOT) || \
112 (defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD))
York Sunef4cef92018-11-05 18:01:06 +0000113 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
114#else /* Start with nGnRnE and PXN and UXN to prevent speculative access */
115 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
116#endif
117 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
118 },
119#ifdef CONFIG_FSL_IFC
120 /* Map IFC region #2 up to CONFIG_SYS_FLASH_BASE for NAND boot */
121 { CONFIG_SYS_FSL_IFC_BASE2, CONFIG_SYS_FSL_IFC_BASE2,
122 CONFIG_SYS_FLASH_BASE - CONFIG_SYS_FSL_IFC_BASE2,
123 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
124 },
125#endif
126 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
127 CONFIG_SYS_FSL_DCSR_SIZE,
128 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
129 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
130 },
131 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
132 CONFIG_SYS_FSL_DRAM_SIZE2,
133 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
134 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
135 },
Priyanka Jain88c25662018-10-29 09:11:29 +0000136#ifdef CONFIG_SYS_FSL_DRAM_BASE3
137 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
138 CONFIG_SYS_FSL_DRAM_SIZE3,
139 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
140 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
141 },
142#endif
York Sunef4cef92018-11-05 18:01:06 +0000143#elif defined(CONFIG_FSL_LSCH2)
144 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
145 CONFIG_SYS_FSL_CCSR_SIZE,
146 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
147 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
148 },
149 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
150 SYS_FSL_OCRAM_SPACE_SIZE,
151 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
152 },
153 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
154 CONFIG_SYS_FSL_DCSR_SIZE,
155 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
156 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
157 },
158 { CONFIG_SYS_FSL_QSPI_BASE, CONFIG_SYS_FSL_QSPI_BASE,
159 CONFIG_SYS_FSL_QSPI_SIZE,
160 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
161 },
162#ifdef CONFIG_FSL_IFC
163 { CONFIG_SYS_FSL_IFC_BASE, CONFIG_SYS_FSL_IFC_BASE,
164 CONFIG_SYS_FSL_IFC_SIZE,
165 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
166 },
167#endif
168 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
169 CONFIG_SYS_FSL_DRAM_SIZE1,
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +0000170#if defined(CONFIG_TFABOOT) || \
171 (defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD))
York Sunef4cef92018-11-05 18:01:06 +0000172 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
173#else /* Start with nGnRnE and PXN and UXN to prevent speculative access */
174 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
175#endif
176 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
177 },
178 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
179 CONFIG_SYS_FSL_DRAM_SIZE2,
180 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
181 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
182 },
183#endif
184 {}, /* list terminator */
185};
186
187static struct mm_region final_map[] = {
188#ifdef CONFIG_FSL_LSCH3
189 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
190 CONFIG_SYS_FSL_CCSR_SIZE,
191 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
192 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
193 },
194 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
195 SYS_FSL_OCRAM_SPACE_SIZE,
196 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
197 },
198 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
199 CONFIG_SYS_FSL_DRAM_SIZE1,
200 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
201 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
202 },
203 { CONFIG_SYS_FSL_QSPI_BASE1, CONFIG_SYS_FSL_QSPI_BASE1,
204 CONFIG_SYS_FSL_QSPI_SIZE1,
205 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
206 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
207 },
208 { CONFIG_SYS_FSL_QSPI_BASE2, CONFIG_SYS_FSL_QSPI_BASE2,
209 CONFIG_SYS_FSL_QSPI_SIZE2,
210 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
211 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
212 },
213#ifdef CONFIG_FSL_IFC
214 { CONFIG_SYS_FSL_IFC_BASE2, CONFIG_SYS_FSL_IFC_BASE2,
215 CONFIG_SYS_FSL_IFC_SIZE2,
216 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
217 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
218 },
219#endif
220 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
221 CONFIG_SYS_FSL_DCSR_SIZE,
222 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
223 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
224 },
225 { CONFIG_SYS_FSL_MC_BASE, CONFIG_SYS_FSL_MC_BASE,
226 CONFIG_SYS_FSL_MC_SIZE,
227 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
228 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
229 },
230 { CONFIG_SYS_FSL_NI_BASE, CONFIG_SYS_FSL_NI_BASE,
231 CONFIG_SYS_FSL_NI_SIZE,
232 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
233 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
234 },
235 /* For QBMAN portal, only the first 64MB is cache-enabled */
236 { CONFIG_SYS_FSL_QBMAN_BASE, CONFIG_SYS_FSL_QBMAN_BASE,
237 CONFIG_SYS_FSL_QBMAN_SIZE_1,
238 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
239 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN | PTE_BLOCK_NS
240 },
241 { CONFIG_SYS_FSL_QBMAN_BASE + CONFIG_SYS_FSL_QBMAN_SIZE_1,
242 CONFIG_SYS_FSL_QBMAN_BASE + CONFIG_SYS_FSL_QBMAN_SIZE_1,
243 CONFIG_SYS_FSL_QBMAN_SIZE - CONFIG_SYS_FSL_QBMAN_SIZE_1,
244 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
245 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
246 },
247 { CONFIG_SYS_PCIE1_PHYS_ADDR, CONFIG_SYS_PCIE1_PHYS_ADDR,
248 CONFIG_SYS_PCIE1_PHYS_SIZE,
249 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
250 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
251 },
252 { CONFIG_SYS_PCIE2_PHYS_ADDR, CONFIG_SYS_PCIE2_PHYS_ADDR,
253 CONFIG_SYS_PCIE2_PHYS_SIZE,
254 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
255 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
256 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800257#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000258 { CONFIG_SYS_PCIE3_PHYS_ADDR, CONFIG_SYS_PCIE3_PHYS_ADDR,
259 CONFIG_SYS_PCIE3_PHYS_SIZE,
260 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
261 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
262 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800263#endif
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000264#ifdef CONFIG_SYS_PCIE4_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000265 { CONFIG_SYS_PCIE4_PHYS_ADDR, CONFIG_SYS_PCIE4_PHYS_ADDR,
266 CONFIG_SYS_PCIE4_PHYS_SIZE,
267 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
268 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
269 },
270#endif
Hou Zhiqiangd08f9702019-04-08 10:15:41 +0000271#ifdef SYS_PCIE5_PHYS_ADDR
272 { SYS_PCIE5_PHYS_ADDR, SYS_PCIE5_PHYS_ADDR,
273 SYS_PCIE5_PHYS_SIZE,
274 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
275 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
276 },
277#endif
278#ifdef SYS_PCIE6_PHYS_ADDR
279 { SYS_PCIE6_PHYS_ADDR, SYS_PCIE6_PHYS_ADDR,
280 SYS_PCIE6_PHYS_SIZE,
281 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
282 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
283 },
284#endif
York Sunef4cef92018-11-05 18:01:06 +0000285 { CONFIG_SYS_FSL_WRIOP1_BASE, CONFIG_SYS_FSL_WRIOP1_BASE,
286 CONFIG_SYS_FSL_WRIOP1_SIZE,
287 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
288 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
289 },
290 { CONFIG_SYS_FSL_AIOP1_BASE, CONFIG_SYS_FSL_AIOP1_BASE,
291 CONFIG_SYS_FSL_AIOP1_SIZE,
292 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
293 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
294 },
295 { CONFIG_SYS_FSL_PEBUF_BASE, CONFIG_SYS_FSL_PEBUF_BASE,
296 CONFIG_SYS_FSL_PEBUF_SIZE,
297 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
298 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
299 },
300 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
301 CONFIG_SYS_FSL_DRAM_SIZE2,
302 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
303 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
304 },
Priyanka Jain88c25662018-10-29 09:11:29 +0000305#ifdef CONFIG_SYS_FSL_DRAM_BASE3
306 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
307 CONFIG_SYS_FSL_DRAM_SIZE3,
308 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
309 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
310 },
311#endif
York Sunef4cef92018-11-05 18:01:06 +0000312#elif defined(CONFIG_FSL_LSCH2)
313 { CONFIG_SYS_FSL_BOOTROM_BASE, CONFIG_SYS_FSL_BOOTROM_BASE,
314 CONFIG_SYS_FSL_BOOTROM_SIZE,
315 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
316 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
317 },
318 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
319 CONFIG_SYS_FSL_CCSR_SIZE,
320 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
321 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
322 },
323 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
324 SYS_FSL_OCRAM_SPACE_SIZE,
325 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
326 },
327 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
328 CONFIG_SYS_FSL_DCSR_SIZE,
329 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
330 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
331 },
332 { CONFIG_SYS_FSL_QSPI_BASE, CONFIG_SYS_FSL_QSPI_BASE,
333 CONFIG_SYS_FSL_QSPI_SIZE,
334 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
335 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
336 },
337#ifdef CONFIG_FSL_IFC
338 { CONFIG_SYS_FSL_IFC_BASE, CONFIG_SYS_FSL_IFC_BASE,
339 CONFIG_SYS_FSL_IFC_SIZE,
340 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
341 },
342#endif
343 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
344 CONFIG_SYS_FSL_DRAM_SIZE1,
345 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
346 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
347 },
348 { CONFIG_SYS_FSL_QBMAN_BASE, CONFIG_SYS_FSL_QBMAN_BASE,
349 CONFIG_SYS_FSL_QBMAN_SIZE,
350 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
351 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
352 },
353 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
354 CONFIG_SYS_FSL_DRAM_SIZE2,
355 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
356 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
357 },
358 { CONFIG_SYS_PCIE1_PHYS_ADDR, CONFIG_SYS_PCIE1_PHYS_ADDR,
359 CONFIG_SYS_PCIE1_PHYS_SIZE,
360 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
361 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
362 },
363 { CONFIG_SYS_PCIE2_PHYS_ADDR, CONFIG_SYS_PCIE2_PHYS_ADDR,
364 CONFIG_SYS_PCIE2_PHYS_SIZE,
365 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
366 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
367 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800368#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000369 { CONFIG_SYS_PCIE3_PHYS_ADDR, CONFIG_SYS_PCIE3_PHYS_ADDR,
370 CONFIG_SYS_PCIE3_PHYS_SIZE,
371 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
372 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
373 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800374#endif
York Sunef4cef92018-11-05 18:01:06 +0000375 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
376 CONFIG_SYS_FSL_DRAM_SIZE3,
377 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
378 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
379 },
380#endif
381#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
382 {}, /* space holder for secure mem */
383#endif
384 {},
385};
386
York Sun9da8f502016-06-24 16:46:23 -0700387struct mm_region *mem_map = early_map;
Alexander Grafce0a64e2016-03-04 01:09:54 +0100388
Mingkai Hu0e58b512015-10-26 19:47:50 +0800389void cpu_name(char *name)
390{
391 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
392 unsigned int i, svr, ver;
393
394 svr = gur_in32(&gur->svr);
395 ver = SVR_SOC_VER(svr);
396
397 for (i = 0; i < ARRAY_SIZE(cpu_type_list); i++)
398 if ((cpu_type_list[i].soc_ver & SVR_WO_E) == ver) {
399 strcpy(name, cpu_type_list[i].name);
Priyanka Jainef76b2e2018-10-29 09:17:09 +0000400#ifdef CONFIG_ARCH_LX2160A
401 if (IS_C_PROCESSOR(svr))
402 strcat(name, "C");
403#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800404
405 if (IS_E_PROCESSOR(svr))
406 strcat(name, "E");
Wenbin Song863a33a2016-09-13 16:13:54 +0800407
408 sprintf(name + strlen(name), " Rev%d.%d",
409 SVR_MAJ(svr), SVR_MIN(svr));
Mingkai Hu0e58b512015-10-26 19:47:50 +0800410 break;
411 }
412
413 if (i == ARRAY_SIZE(cpu_type_list))
414 strcpy(name, "unknown");
415}
416
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400417#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Mingkai Hu0e58b512015-10-26 19:47:50 +0800418/*
419 * To start MMU before DDR is available, we create MMU table in SRAM.
420 * The base address of SRAM is CONFIG_SYS_FSL_OCRAM_BASE. We use three
421 * levels of translation tables here to cover 40-bit address space.
422 * We use 4KB granule size, with 40 bits physical address, T0SZ=24
York Sun9da8f502016-06-24 16:46:23 -0700423 * Address above EARLY_PGTABLE_SIZE (0x5000) is free for other purpose.
424 * Note, the debug print in cache_v8.c is not usable for debugging
425 * these early MMU tables because UART is not yet available.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800426 */
427static inline void early_mmu_setup(void)
428{
York Sun9da8f502016-06-24 16:46:23 -0700429 unsigned int el = current_el();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800430
York Sun9da8f502016-06-24 16:46:23 -0700431 /* global data is already setup, no allocation yet */
Pankit Gargc4d39eb2018-11-05 18:01:28 +0000432 if (el == 3)
433 gd->arch.tlb_addr = CONFIG_SYS_FSL_OCRAM_BASE;
434 else
435 gd->arch.tlb_addr = CONFIG_SYS_DDR_SDRAM_BASE;
York Sun9da8f502016-06-24 16:46:23 -0700436 gd->arch.tlb_fillptr = gd->arch.tlb_addr;
437 gd->arch.tlb_size = EARLY_PGTABLE_SIZE;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800438
York Sun9da8f502016-06-24 16:46:23 -0700439 /* Create early page tables */
440 setup_pgtables();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800441
York Sun9da8f502016-06-24 16:46:23 -0700442 /* point TTBR to the new table */
443 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
444 get_tcr(el, NULL, NULL) &
445 ~(TCR_ORGN_MASK | TCR_IRGN_MASK),
Mingkai Hu0e58b512015-10-26 19:47:50 +0800446 MEMORY_ATTRIBUTES);
York Sun9da8f502016-06-24 16:46:23 -0700447
Mingkai Hu0e58b512015-10-26 19:47:50 +0800448 set_sctlr(get_sctlr() | CR_M);
449}
450
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800451static void fix_pcie_mmu_map(void)
452{
York Sun4ce6fbf2017-03-27 11:41:01 -0700453#ifdef CONFIG_ARCH_LS2080A
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800454 unsigned int i;
455 u32 svr, ver;
456 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
457
458 svr = gur_in32(&gur->svr);
459 ver = SVR_SOC_VER(svr);
460
461 /* Fix PCIE base and size for LS2088A */
462 if ((ver == SVR_LS2088A) || (ver == SVR_LS2084A) ||
Priyanka Jain2b361782017-04-27 15:08:06 +0530463 (ver == SVR_LS2048A) || (ver == SVR_LS2044A) ||
464 (ver == SVR_LS2081A) || (ver == SVR_LS2041A)) {
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800465 for (i = 0; i < ARRAY_SIZE(final_map); i++) {
466 switch (final_map[i].phys) {
467 case CONFIG_SYS_PCIE1_PHYS_ADDR:
468 final_map[i].phys = 0x2000000000ULL;
469 final_map[i].virt = 0x2000000000ULL;
470 final_map[i].size = 0x800000000ULL;
471 break;
472 case CONFIG_SYS_PCIE2_PHYS_ADDR:
473 final_map[i].phys = 0x2800000000ULL;
474 final_map[i].virt = 0x2800000000ULL;
475 final_map[i].size = 0x800000000ULL;
476 break;
Yuantian Tang4aefa162019-04-10 16:43:33 +0800477#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800478 case CONFIG_SYS_PCIE3_PHYS_ADDR:
479 final_map[i].phys = 0x3000000000ULL;
480 final_map[i].virt = 0x3000000000ULL;
481 final_map[i].size = 0x800000000ULL;
482 break;
Yuantian Tang4aefa162019-04-10 16:43:33 +0800483#endif
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000484#ifdef CONFIG_SYS_PCIE4_PHYS_ADDR
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800485 case CONFIG_SYS_PCIE4_PHYS_ADDR:
486 final_map[i].phys = 0x3800000000ULL;
487 final_map[i].virt = 0x3800000000ULL;
488 final_map[i].size = 0x800000000ULL;
489 break;
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000490#endif
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800491 default:
492 break;
493 }
494 }
495 }
496#endif
497}
498
Mingkai Hu0e58b512015-10-26 19:47:50 +0800499/*
500 * The final tables look similar to early tables, but different in detail.
501 * These tables are in DRAM. Sub tables are added to enable cache for
502 * QBMan and OCRAM.
503 *
York Sun1ef95cc2016-06-24 16:46:18 -0700504 * Put the MMU table in secure memory if gd->arch.secure_ram is valid.
505 * OCRAM will be not used for this purpose so gd->arch.secure_ram can't be 0.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800506 */
507static inline void final_mmu_setup(void)
508{
York Sun9da8f502016-06-24 16:46:23 -0700509 u64 tlb_addr_save = gd->arch.tlb_addr;
York Sun0804d562015-12-04 11:57:08 -0800510 unsigned int el = current_el();
York Sun9da8f502016-06-24 16:46:23 -0700511 int index;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800512
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800513 /* fix the final_map before filling in the block entries */
514 fix_pcie_mmu_map();
515
York Sun9da8f502016-06-24 16:46:23 -0700516 mem_map = final_map;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800517
York Sun75488ed2017-03-06 09:02:30 -0800518 /* Update mapping for DDR to actual size */
519 for (index = 0; index < ARRAY_SIZE(final_map) - 2; index++) {
520 /*
521 * Find the entry for DDR mapping and update the address and
522 * size. Zero-sized mapping will be skipped when creating MMU
523 * table.
524 */
525 switch (final_map[index].virt) {
526 case CONFIG_SYS_FSL_DRAM_BASE1:
527 final_map[index].virt = gd->bd->bi_dram[0].start;
528 final_map[index].phys = gd->bd->bi_dram[0].start;
529 final_map[index].size = gd->bd->bi_dram[0].size;
530 break;
531#ifdef CONFIG_SYS_FSL_DRAM_BASE2
532 case CONFIG_SYS_FSL_DRAM_BASE2:
533#if (CONFIG_NR_DRAM_BANKS >= 2)
534 final_map[index].virt = gd->bd->bi_dram[1].start;
535 final_map[index].phys = gd->bd->bi_dram[1].start;
536 final_map[index].size = gd->bd->bi_dram[1].size;
537#else
538 final_map[index].size = 0;
539#endif
540 break;
541#endif
542#ifdef CONFIG_SYS_FSL_DRAM_BASE3
543 case CONFIG_SYS_FSL_DRAM_BASE3:
544#if (CONFIG_NR_DRAM_BANKS >= 3)
545 final_map[index].virt = gd->bd->bi_dram[2].start;
546 final_map[index].phys = gd->bd->bi_dram[2].start;
547 final_map[index].size = gd->bd->bi_dram[2].size;
548#else
549 final_map[index].size = 0;
550#endif
551 break;
552#endif
553 default:
554 break;
555 }
556 }
557
York Sun0804d562015-12-04 11:57:08 -0800558#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
York Sun9da8f502016-06-24 16:46:23 -0700559 if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
560 if (el == 3) {
561 /*
562 * Only use gd->arch.secure_ram if the address is
563 * recalculated. Align to 4KB for MMU table.
564 */
565 /* put page tables in secure ram */
566 index = ARRAY_SIZE(final_map) - 2;
567 gd->arch.tlb_addr = gd->arch.secure_ram & ~0xfff;
568 final_map[index].virt = gd->arch.secure_ram & ~0x3;
569 final_map[index].phys = final_map[index].virt;
570 final_map[index].size = CONFIG_SYS_MEM_RESERVE_SECURE;
571 final_map[index].attrs = PTE_BLOCK_OUTER_SHARE;
York Sun1ef95cc2016-06-24 16:46:18 -0700572 gd->arch.secure_ram |= MEM_RESERVE_SECURE_SECURED;
York Sun9da8f502016-06-24 16:46:23 -0700573 tlb_addr_save = gd->arch.tlb_addr;
York Sun0804d562015-12-04 11:57:08 -0800574 } else {
York Sun9da8f502016-06-24 16:46:23 -0700575 /* Use allocated (board_f.c) memory for TLB */
576 tlb_addr_save = gd->arch.tlb_allocated;
577 gd->arch.tlb_addr = tlb_addr_save;
York Sun0804d562015-12-04 11:57:08 -0800578 }
579 }
580#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800581
York Sun9da8f502016-06-24 16:46:23 -0700582 /* Reset the fill ptr */
583 gd->arch.tlb_fillptr = tlb_addr_save;
584
585 /* Create normal system page tables */
586 setup_pgtables();
587
588 /* Create emergency page tables */
589 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
590 gd->arch.tlb_emerg = gd->arch.tlb_addr;
591 setup_pgtables();
592 gd->arch.tlb_addr = tlb_addr_save;
593
York Suncf64ced2017-03-06 09:02:31 -0800594 /* Disable cache and MMU */
595 dcache_disable(); /* TLBs are invalidated */
596 invalidate_icache_all();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800597
598 /* point TTBR to the new table */
York Sun9da8f502016-06-24 16:46:23 -0700599 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
Mingkai Hu0e58b512015-10-26 19:47:50 +0800600 MEMORY_ATTRIBUTES);
York Suncf64ced2017-03-06 09:02:31 -0800601
York Suneb6eac12016-07-22 10:52:23 -0700602 set_sctlr(get_sctlr() | CR_M);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800603}
604
Alexander Grafbc78b922016-03-21 20:26:12 +0100605u64 get_page_table_size(void)
606{
607 return 0x10000;
608}
609
Mingkai Hu0e58b512015-10-26 19:47:50 +0800610int arch_cpu_init(void)
611{
York Sune6b871e2017-05-15 08:51:59 -0700612 /*
613 * This function is called before U-Boot relocates itself to speed up
614 * on system running. It is not necessary to run if performance is not
615 * critical. Skip if MMU is already enabled by SPL or other means.
616 */
617 if (get_sctlr() & CR_M)
618 return 0;
619
Mingkai Hu0e58b512015-10-26 19:47:50 +0800620 icache_enable();
621 __asm_invalidate_dcache_all();
622 __asm_invalidate_tlb_all();
623 early_mmu_setup();
624 set_sctlr(get_sctlr() | CR_C);
625 return 0;
626}
627
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800628void mmu_setup(void)
629{
630 final_mmu_setup();
631}
632
Mingkai Hu0e58b512015-10-26 19:47:50 +0800633/*
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800634 * This function is called from common/board_r.c.
635 * It recreates MMU table in main memory.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800636 */
637void enable_caches(void)
638{
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800639 mmu_setup();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800640 __asm_invalidate_tlb_all();
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800641 icache_enable();
642 dcache_enable();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800643}
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400644#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000645
646#ifdef CONFIG_TFABOOT
647enum boot_src __get_boot_src(u32 porsr1)
648{
649 enum boot_src src = BOOT_SOURCE_RESERVED;
650 u32 rcw_src = (porsr1 & RCW_SRC_MASK) >> RCW_SRC_BIT;
Priyanka Jain88c25662018-10-29 09:11:29 +0000651#if !defined(CONFIG_NXP_LSCH3_2)
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000652 u32 val;
653#endif
654 debug("%s: rcw_src 0x%x\n", __func__, rcw_src);
655
656#if defined(CONFIG_FSL_LSCH3)
Priyanka Jain88c25662018-10-29 09:11:29 +0000657#if defined(CONFIG_NXP_LSCH3_2)
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000658 switch (rcw_src) {
659 case RCW_SRC_SDHC1_VAL:
660 src = BOOT_SOURCE_SD_MMC;
661 break;
662 case RCW_SRC_SDHC2_VAL:
663 src = BOOT_SOURCE_SD_MMC2;
664 break;
665 case RCW_SRC_I2C1_VAL:
666 src = BOOT_SOURCE_I2C1_EXTENDED;
667 break;
668 case RCW_SRC_FLEXSPI_NAND2K_VAL:
669 src = BOOT_SOURCE_XSPI_NAND;
670 break;
671 case RCW_SRC_FLEXSPI_NAND4K_VAL:
672 src = BOOT_SOURCE_XSPI_NAND;
673 break;
674 case RCW_SRC_RESERVED_1_VAL:
675 src = BOOT_SOURCE_RESERVED;
676 break;
677 case RCW_SRC_FLEXSPI_NOR_24B:
678 src = BOOT_SOURCE_XSPI_NOR;
679 break;
680 default:
681 src = BOOT_SOURCE_RESERVED;
682 }
683#else
684 val = rcw_src & RCW_SRC_TYPE_MASK;
685 if (val == RCW_SRC_NOR_VAL) {
686 val = rcw_src & NOR_TYPE_MASK;
687
688 switch (val) {
689 case NOR_16B_VAL:
690 case NOR_32B_VAL:
691 src = BOOT_SOURCE_IFC_NOR;
692 break;
693 default:
694 src = BOOT_SOURCE_RESERVED;
695 }
696 } else {
697 /* RCW SRC Serial Flash */
698 val = rcw_src & RCW_SRC_SERIAL_MASK;
699 switch (val) {
700 case RCW_SRC_QSPI_VAL:
701 /* RCW SRC Serial NOR (QSPI) */
702 src = BOOT_SOURCE_QSPI_NOR;
703 break;
704 case RCW_SRC_SD_CARD_VAL:
705 /* RCW SRC SD Card */
706 src = BOOT_SOURCE_SD_MMC;
707 break;
708 case RCW_SRC_EMMC_VAL:
709 /* RCW SRC EMMC */
Rajesh Bhagat5b73c902018-12-27 04:37:49 +0000710 src = BOOT_SOURCE_SD_MMC;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000711 break;
712 case RCW_SRC_I2C1_VAL:
713 /* RCW SRC I2C1 Extended */
714 src = BOOT_SOURCE_I2C1_EXTENDED;
715 break;
716 default:
717 src = BOOT_SOURCE_RESERVED;
718 }
719 }
720#endif
721#elif defined(CONFIG_FSL_LSCH2)
722 /* RCW SRC NAND */
723 val = rcw_src & RCW_SRC_NAND_MASK;
724 if (val == RCW_SRC_NAND_VAL) {
725 val = rcw_src & NAND_RESERVED_MASK;
726 if (val != NAND_RESERVED_1 && val != NAND_RESERVED_2)
727 src = BOOT_SOURCE_IFC_NAND;
728
729 } else {
730 /* RCW SRC NOR */
731 val = rcw_src & RCW_SRC_NOR_MASK;
732 if (val == NOR_8B_VAL || val == NOR_16B_VAL) {
733 src = BOOT_SOURCE_IFC_NOR;
734 } else {
735 switch (rcw_src) {
736 case QSPI_VAL1:
737 case QSPI_VAL2:
738 src = BOOT_SOURCE_QSPI_NOR;
739 break;
740 case SD_VAL:
741 src = BOOT_SOURCE_SD_MMC;
742 break;
743 default:
744 src = BOOT_SOURCE_RESERVED;
745 }
746 }
747 }
748#endif
York Sun8f3f4ef2018-11-05 18:02:09 +0000749
750 if (CONFIG_IS_ENABLED(SYS_FSL_ERRATUM_A010539) && !rcw_src)
751 src = BOOT_SOURCE_QSPI_NOR;
752
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000753 debug("%s: src 0x%x\n", __func__, src);
754 return src;
755}
756
757enum boot_src get_boot_src(void)
758{
York Sun8f3f4ef2018-11-05 18:02:09 +0000759 struct pt_regs regs;
760 u32 porsr1 = 0;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000761
762#if defined(CONFIG_FSL_LSCH3)
763 u32 __iomem *dcfg_ccsr = (u32 __iomem *)DCFG_BASE;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000764#elif defined(CONFIG_FSL_LSCH2)
765 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
York Sun8f3f4ef2018-11-05 18:02:09 +0000766#endif
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000767
York Sun8f3f4ef2018-11-05 18:02:09 +0000768 if (current_el() == 2) {
769 regs.regs[0] = SIP_SVC_RCW;
770
771 smc_call(&regs);
772 if (!regs.regs[0])
773 porsr1 = regs.regs[1];
774 }
775
776 if (current_el() == 3 || !porsr1) {
777#ifdef CONFIG_FSL_LSCH3
778 porsr1 = in_le32(dcfg_ccsr + DCFG_PORSR1 / 4);
779#elif defined(CONFIG_FSL_LSCH2)
780 porsr1 = in_be32(&gur->porsr1);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800781#endif
York Sun8f3f4ef2018-11-05 18:02:09 +0000782 }
783
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000784 debug("%s: porsr1 0x%x\n", __func__, porsr1);
785
786 return __get_boot_src(porsr1);
787}
788
789#ifdef CONFIG_ENV_IS_IN_MMC
790int mmc_get_env_dev(void)
791{
792 enum boot_src src = get_boot_src();
793 int dev = CONFIG_SYS_MMC_ENV_DEV;
794
795 switch (src) {
796 case BOOT_SOURCE_SD_MMC:
797 dev = 0;
798 break;
799 case BOOT_SOURCE_SD_MMC2:
800 dev = 1;
801 break;
802 default:
803 break;
804 }
805
806 return dev;
807}
808#endif
809
810enum env_location env_get_location(enum env_operation op, int prio)
811{
812 enum boot_src src = get_boot_src();
813 enum env_location env_loc = ENVL_NOWHERE;
814
815 if (prio)
816 return ENVL_UNKNOWN;
817
Udit Agarwal5e9720c2019-04-23 06:06:04 +0000818#ifdef CONFIG_ENV_IS_NOWHERE
819 return env_loc;
Pankit Gargbdbf84f2018-11-05 18:01:52 +0000820#endif
821
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000822 switch (src) {
823 case BOOT_SOURCE_IFC_NOR:
824 env_loc = ENVL_FLASH;
825 break;
826 case BOOT_SOURCE_QSPI_NOR:
827 /* FALLTHROUGH */
828 case BOOT_SOURCE_XSPI_NOR:
829 env_loc = ENVL_SPI_FLASH;
830 break;
831 case BOOT_SOURCE_IFC_NAND:
832 /* FALLTHROUGH */
833 case BOOT_SOURCE_QSPI_NAND:
834 /* FALLTHROUGH */
835 case BOOT_SOURCE_XSPI_NAND:
836 env_loc = ENVL_NAND;
837 break;
838 case BOOT_SOURCE_SD_MMC:
839 /* FALLTHROUGH */
840 case BOOT_SOURCE_SD_MMC2:
841 env_loc = ENVL_MMC;
842 break;
843 case BOOT_SOURCE_I2C1_EXTENDED:
844 /* FALLTHROUGH */
845 default:
846 break;
847 }
848
849 return env_loc;
850}
851#endif /* CONFIG_TFABOOT */
Mingkai Hu0e58b512015-10-26 19:47:50 +0800852
Priyanka Jain9a276702016-11-17 12:29:56 +0530853u32 initiator_type(u32 cluster, int init_id)
Mingkai Hu0e58b512015-10-26 19:47:50 +0800854{
855 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
856 u32 idx = (cluster >> (init_id * 8)) & TP_CLUSTER_INIT_MASK;
857 u32 type = 0;
858
859 type = gur_in32(&gur->tp_ityp[idx]);
860 if (type & TP_ITYP_AV)
861 return type;
862
863 return 0;
864}
865
York Suned7fbe32016-09-13 12:40:30 -0700866u32 cpu_pos_mask(void)
867{
868 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
869 int i = 0;
870 u32 cluster, type, mask = 0;
871
872 do {
873 int j;
874
875 cluster = gur_in32(&gur->tp_cluster[i].lower);
876 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
877 type = initiator_type(cluster, j);
878 if (type && (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM))
879 mask |= 1 << (i * TP_INIT_PER_CLUSTER + j);
880 }
881 i++;
882 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
883
884 return mask;
885}
886
Mingkai Hu0e58b512015-10-26 19:47:50 +0800887u32 cpu_mask(void)
888{
889 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
890 int i = 0, count = 0;
891 u32 cluster, type, mask = 0;
892
893 do {
894 int j;
895
896 cluster = gur_in32(&gur->tp_cluster[i].lower);
897 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
898 type = initiator_type(cluster, j);
899 if (type) {
900 if (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
901 mask |= 1 << count;
902 count++;
903 }
904 }
905 i++;
906 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
907
908 return mask;
909}
910
911/*
912 * Return the number of cores on this SOC.
913 */
914int cpu_numcores(void)
915{
916 return hweight32(cpu_mask());
917}
918
919int fsl_qoriq_core_to_cluster(unsigned int core)
920{
921 struct ccsr_gur __iomem *gur =
922 (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
923 int i = 0, count = 0;
924 u32 cluster;
925
926 do {
927 int j;
928
929 cluster = gur_in32(&gur->tp_cluster[i].lower);
930 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
931 if (initiator_type(cluster, j)) {
932 if (count == core)
933 return i;
934 count++;
935 }
936 }
937 i++;
938 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
939
940 return -1; /* cannot identify the cluster */
941}
942
943u32 fsl_qoriq_core_to_type(unsigned int core)
944{
945 struct ccsr_gur __iomem *gur =
946 (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
947 int i = 0, count = 0;
948 u32 cluster, type;
949
950 do {
951 int j;
952
953 cluster = gur_in32(&gur->tp_cluster[i].lower);
954 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
955 type = initiator_type(cluster, j);
956 if (type) {
957 if (count == core)
958 return type;
959 count++;
960 }
961 }
962 i++;
963 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
964
965 return -1; /* cannot identify the cluster */
966}
967
Priyanka Jain96b001f2016-11-17 12:29:51 +0530968#ifndef CONFIG_FSL_LSCH3
Sriram Dash9282d262016-06-13 09:58:32 +0530969uint get_svr(void)
970{
971 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
972
973 return gur_in32(&gur->svr);
974}
Priyanka Jain96b001f2016-11-17 12:29:51 +0530975#endif
Sriram Dash9282d262016-06-13 09:58:32 +0530976
Mingkai Hu0e58b512015-10-26 19:47:50 +0800977#ifdef CONFIG_DISPLAY_CPUINFO
978int print_cpuinfo(void)
979{
980 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
981 struct sys_info sysinfo;
982 char buf[32];
983 unsigned int i, core;
York Suncbe8e1c2016-04-04 11:41:26 -0700984 u32 type, rcw, svr = gur_in32(&gur->svr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800985
986 puts("SoC: ");
987
988 cpu_name(buf);
York Suncbe8e1c2016-04-04 11:41:26 -0700989 printf(" %s (0x%x)\n", buf, svr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800990 memset((u8 *)buf, 0x00, ARRAY_SIZE(buf));
991 get_sys_info(&sysinfo);
992 puts("Clock Configuration:");
993 for_each_cpu(i, core, cpu_numcores(), cpu_mask()) {
994 if (!(i % 3))
995 puts("\n ");
996 type = TP_ITYP_VER(fsl_qoriq_core_to_type(core));
997 printf("CPU%d(%s):%-4s MHz ", core,
998 type == TY_ITYP_VER_A7 ? "A7 " :
999 (type == TY_ITYP_VER_A53 ? "A53" :
Alison Wang79808392016-07-05 16:01:52 +08001000 (type == TY_ITYP_VER_A57 ? "A57" :
1001 (type == TY_ITYP_VER_A72 ? "A72" : " "))),
Mingkai Hu0e58b512015-10-26 19:47:50 +08001002 strmhz(buf, sysinfo.freq_processor[core]));
1003 }
Hou Zhiqiang3f91cda2017-01-10 16:44:15 +08001004 /* Display platform clock as Bus frequency. */
Mingkai Hu0e58b512015-10-26 19:47:50 +08001005 printf("\n Bus: %-4s MHz ",
Hou Zhiqiang3f91cda2017-01-10 16:44:15 +08001006 strmhz(buf, sysinfo.freq_systembus / CONFIG_SYS_FSL_PCLK_DIV));
Mingkai Hu0e58b512015-10-26 19:47:50 +08001007 printf("DDR: %-4s MT/s", strmhz(buf, sysinfo.freq_ddrbus));
Shaohui Xie04643262015-10-26 19:47:54 +08001008#ifdef CONFIG_SYS_DPAA_FMAN
1009 printf(" FMAN: %-4s MHz", strmhz(buf, sysinfo.freq_fman[0]));
1010#endif
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301011#ifdef CONFIG_SYS_FSL_HAS_DP_DDR
York Suncbe8e1c2016-04-04 11:41:26 -07001012 if (soc_has_dp_ddr()) {
1013 printf(" DP-DDR: %-4s MT/s",
1014 strmhz(buf, sysinfo.freq_ddrbus2));
1015 }
Mingkai Hu0e58b512015-10-26 19:47:50 +08001016#endif
1017 puts("\n");
1018
1019 /*
1020 * Display the RCW, so that no one gets confused as to what RCW
1021 * we're actually using for this boot.
1022 */
1023 puts("Reset Configuration Word (RCW):");
1024 for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
1025 rcw = gur_in32(&gur->rcwsr[i]);
1026 if ((i % 4) == 0)
1027 printf("\n %08x:", i * 4);
1028 printf(" %08x", rcw);
1029 }
1030 puts("\n");
1031
1032 return 0;
1033}
1034#endif
1035
1036#ifdef CONFIG_FSL_ESDHC
1037int cpu_mmc_init(bd_t *bis)
1038{
1039 return fsl_esdhc_mmc_init(bis);
1040}
1041#endif
1042
1043int cpu_eth_init(bd_t *bis)
1044{
1045 int error = 0;
1046
Santan Kumar1afa9002017-05-05 15:42:29 +05301047#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001048 error = fsl_mc_ldpaa_init(bis);
1049#endif
Shaohui Xie04643262015-10-26 19:47:54 +08001050#ifdef CONFIG_FMAN_ENET
1051 fm_standard_init(bis);
1052#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001053 return error;
1054}
1055
Yuantian Tangaec3b142017-04-19 13:27:39 +08001056static inline int check_psci(void)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001057{
Yuantian Tangaec3b142017-04-19 13:27:39 +08001058 unsigned int psci_ver;
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301059
Yuantian Tangaec3b142017-04-19 13:27:39 +08001060 psci_ver = sec_firmware_support_psci_version();
1061 if (psci_ver == PSCI_INVALID_VER)
1062 return 1;
1063
1064 return 0;
1065}
1066
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301067static void config_core_prefetch(void)
1068{
1069 char *buf = NULL;
1070 char buffer[HWCONFIG_BUFFER_SIZE];
1071 const char *prefetch_arg = NULL;
1072 size_t arglen;
1073 unsigned int mask;
1074 struct pt_regs regs;
1075
1076 if (env_get_f("hwconfig", buffer, sizeof(buffer)) > 0)
1077 buf = buffer;
Pankaj Bansal6c772772019-10-31 05:41:09 +00001078 else
1079 return;
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301080
1081 prefetch_arg = hwconfig_subarg_f("core_prefetch", "disable",
1082 &arglen, buf);
1083
1084 if (prefetch_arg) {
1085 mask = simple_strtoul(prefetch_arg, NULL, 0) & 0xff;
1086 if (mask & 0x1) {
1087 printf("Core0 prefetch can't be disabled\n");
1088 return;
1089 }
1090
1091#define SIP_PREFETCH_DISABLE_64 0xC200FF13
1092 regs.regs[0] = SIP_PREFETCH_DISABLE_64;
1093 regs.regs[1] = mask;
1094 smc_call(&regs);
1095
1096 if (regs.regs[0])
1097 printf("Prefetch disable config failed for mask ");
1098 else
1099 printf("Prefetch disable config passed for mask ");
1100 printf("0x%x\n", mask);
1101 }
1102}
1103
Yuantian Tangaec3b142017-04-19 13:27:39 +08001104int arch_early_init_r(void)
1105{
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301106#ifdef CONFIG_SYS_FSL_ERRATUM_A009635
Priyanka Jain823e0422017-02-14 10:34:31 +05301107 u32 svr_dev_id;
1108 /*
1109 * erratum A009635 is valid only for LS2080A SoC and
1110 * its personalitiesi
1111 */
Wenbin song5d8a61c2017-12-04 12:18:28 +08001112 svr_dev_id = get_svr();
1113 if (IS_SVR_DEV(svr_dev_id, SVR_DEV(SVR_LS2080A)))
Priyanka Jain823e0422017-02-14 10:34:31 +05301114 erratum_a009635();
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301115#endif
Shengzhou Liu15875a52016-11-21 11:36:48 +08001116#if defined(CONFIG_SYS_FSL_ERRATUM_A009942) && defined(CONFIG_SYS_FSL_DDR)
1117 erratum_a009942_check_cpo();
1118#endif
Yuantian Tangaec3b142017-04-19 13:27:39 +08001119 if (check_psci()) {
1120 debug("PSCI: PSCI does not exist.\n");
1121
1122 /* if PSCI does not exist, boot secondary cores here */
1123 if (fsl_layerscape_wake_seconday_cores())
Hou Zhiqiang21c4d552016-06-28 20:18:15 +08001124 printf("Did not wake secondary cores\n");
1125 }
Mingkai Hu0e58b512015-10-26 19:47:50 +08001126
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301127 config_core_prefetch();
1128
Mingkai Hu0e58b512015-10-26 19:47:50 +08001129#ifdef CONFIG_SYS_HAS_SERDES
1130 fsl_serdes_init();
1131#endif
Pankaj Bansalcc840622018-10-29 11:28:26 +00001132#ifdef CONFIG_SYS_FSL_HAS_RGMII
1133 /* some dpmacs in armv8a based freescale layerscape SOCs can be
1134 * configured via both serdes(sgmii, xfi, xlaui etc) bits and via
1135 * EC*_PMUX(rgmii) bits in RCW.
1136 * e.g. dpmac 17 and 18 in LX2160A can be configured as SGMII from
1137 * serdes bits and as RGMII via EC1_PMUX/EC2_PMUX bits
1138 * Now if a dpmac is enabled by serdes bits then it takes precedence
1139 * over EC*_PMUX bits. i.e. in LX2160A if we select serdes protocol
1140 * that configures dpmac17 as SGMII and set the EC1_PMUX as RGMII,
1141 * then the dpmac is SGMII and not RGMII.
1142 *
1143 * Therefore, move the fsl_rgmii_init after fsl_serdes_init. in
1144 * fsl_rgmii_init function of SOC, we will check if the dpmac is enabled
1145 * or not? if it is (fsl_serdes_init has already enabled the dpmac),
1146 * then don't enable it.
1147 */
1148 fsl_rgmii_init();
1149#endif
Shaohui Xie04643262015-10-26 19:47:54 +08001150#ifdef CONFIG_FMAN_ENET
1151 fman_enet_init();
1152#endif
Ahmed Mansouraa270b42017-12-15 16:01:00 -05001153#ifdef CONFIG_SYS_DPAA_QBMAN
1154 setup_qbman_portals();
1155#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001156 return 0;
1157}
1158
1159int timer_init(void)
1160{
1161 u32 __iomem *cntcr = (u32 *)CONFIG_SYS_FSL_TIMER_ADDR;
1162#ifdef CONFIG_FSL_LSCH3
1163 u32 __iomem *cltbenr = (u32 *)CONFIG_SYS_FSL_PMU_CLTBENR;
1164#endif
Thomas Schaefercbec2b82019-08-08 16:00:30 +08001165#if defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
1166 defined(CONFIG_ARCH_LS1028A)
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001167 u32 __iomem *pctbenr = (u32 *)FSL_PMU_PCTBENR_OFFSET;
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301168 u32 svr_dev_id;
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001169#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001170#ifdef COUNTER_FREQUENCY_REAL
1171 unsigned long cntfrq = COUNTER_FREQUENCY_REAL;
1172
1173 /* Update with accurate clock frequency */
York Sune6b871e2017-05-15 08:51:59 -07001174 if (current_el() == 3)
1175 asm volatile("msr cntfrq_el0, %0" : : "r" (cntfrq) : "memory");
Mingkai Hu0e58b512015-10-26 19:47:50 +08001176#endif
1177
1178#ifdef CONFIG_FSL_LSCH3
1179 /* Enable timebase for all clusters.
1180 * It is safe to do so even some clusters are not enabled.
1181 */
1182 out_le32(cltbenr, 0xf);
1183#endif
1184
Thomas Schaefercbec2b82019-08-08 16:00:30 +08001185#if defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
1186 defined(CONFIG_ARCH_LS1028A)
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001187 /*
1188 * In certain Layerscape SoCs, the clock for each core's
1189 * has an enable bit in the PMU Physical Core Time Base Enable
1190 * Register (PCTBENR), which allows the watchdog to operate.
1191 */
1192 setbits_le32(pctbenr, 0xff);
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301193 /*
1194 * For LS2080A SoC and its personalities, timer controller
1195 * offset is different
1196 */
Wenbin song5d8a61c2017-12-04 12:18:28 +08001197 svr_dev_id = get_svr();
1198 if (IS_SVR_DEV(svr_dev_id, SVR_DEV(SVR_LS2080A)))
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301199 cntcr = (u32 *)SYS_FSL_LS2080A_LS2085A_TIMER_ADDR;
1200
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001201#endif
1202
Mingkai Hu0e58b512015-10-26 19:47:50 +08001203 /* Enable clock for timer
1204 * This is a global setting.
1205 */
1206 out_le32(cntcr, 0x1);
1207
1208 return 0;
1209}
1210
Alexander Graf12be31c2016-11-17 01:03:01 +01001211__efi_runtime_data u32 __iomem *rstcr = (u32 *)CONFIG_SYS_FSL_RST_ADDR;
1212
1213void __efi_runtime reset_cpu(ulong addr)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001214{
Mingkai Hu0e58b512015-10-26 19:47:50 +08001215 u32 val;
1216
Priyanka Jainef76b2e2018-10-29 09:17:09 +00001217#ifdef CONFIG_ARCH_LX2160A
1218 val = in_le32(rstcr);
1219 val |= 0x01;
1220 out_le32(rstcr, val);
1221#else
Mingkai Hu0e58b512015-10-26 19:47:50 +08001222 /* Raise RESET_REQ_B */
1223 val = scfg_in32(rstcr);
1224 val |= 0x02;
1225 scfg_out32(rstcr, val);
Priyanka Jainef76b2e2018-10-29 09:17:09 +00001226#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001227}
York Sun928b6812015-12-07 11:08:58 -08001228
Mathew McBride8f137732019-10-18 14:27:54 +11001229#if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_PSCI_RESET)
Alexander Graf12be31c2016-11-17 01:03:01 +01001230
1231void __efi_runtime EFIAPI efi_reset_system(
1232 enum efi_reset_type reset_type,
1233 efi_status_t reset_status,
1234 unsigned long data_size, void *reset_data)
1235{
1236 switch (reset_type) {
1237 case EFI_RESET_COLD:
1238 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +01001239 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf12be31c2016-11-17 01:03:01 +01001240 reset_cpu(0);
1241 break;
1242 case EFI_RESET_SHUTDOWN:
1243 /* Nothing we can do */
1244 break;
1245 }
1246
1247 while (1) { }
1248}
1249
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +01001250efi_status_t efi_reset_system_init(void)
Alexander Graf12be31c2016-11-17 01:03:01 +01001251{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +01001252 return efi_add_runtime_mmio(&rstcr, sizeof(*rstcr));
Alexander Graf12be31c2016-11-17 01:03:01 +01001253}
1254
1255#endif
1256
York Sun2db54082017-09-07 10:12:32 -07001257/*
1258 * Calculate reserved memory with given memory bank
1259 * Return aligned memory size on success
1260 * Return (ram_size + needed size) for failure
1261 */
York Sun928b6812015-12-07 11:08:58 -08001262phys_size_t board_reserve_ram_top(phys_size_t ram_size)
1263{
1264 phys_size_t ram_top = ram_size;
1265
Santan Kumar1afa9002017-05-05 15:42:29 +05301266#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
York Sun2db54082017-09-07 10:12:32 -07001267 ram_top = mc_get_dram_block_size();
1268 if (ram_top > ram_size)
1269 return ram_size + ram_top;
1270
1271 ram_top = ram_size - ram_top;
York Sun4de24ef2017-03-06 09:02:28 -08001272 /* The start address of MC reserved memory needs to be aligned. */
York Sun928b6812015-12-07 11:08:58 -08001273 ram_top &= ~(CONFIG_SYS_MC_RSV_MEM_ALIGN - 1);
1274#endif
York Sun4de24ef2017-03-06 09:02:28 -08001275
1276 return ram_size - ram_top;
1277}
1278
1279phys_size_t get_effective_memsize(void)
1280{
1281 phys_size_t ea_size, rem = 0;
1282
1283 /*
1284 * For ARMv8 SoCs, DDR memory is split into two or three regions. The
Sumit Garged0deea2017-10-04 03:20:49 +05301285 * first region is 2GB space at 0x8000_0000. Secure memory needs to
1286 * allocated from first region. If the memory extends to the second
1287 * region (or the third region if applicable), Management Complex (MC)
1288 * memory should be put into the highest region, i.e. the end of DDR
1289 * memory. CONFIG_MAX_MEM_MAPPED is set to the size of first region so
1290 * U-Boot doesn't relocate itself into higher address. Should DDR be
1291 * configured to skip the first region, this function needs to be
1292 * adjusted.
York Sun4de24ef2017-03-06 09:02:28 -08001293 */
1294 if (gd->ram_size > CONFIG_MAX_MEM_MAPPED) {
1295 ea_size = CONFIG_MAX_MEM_MAPPED;
1296 rem = gd->ram_size - ea_size;
1297 } else {
1298 ea_size = gd->ram_size;
1299 }
1300
1301#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1302 /* Check if we have enough space for secure memory */
Sumit Garged0deea2017-10-04 03:20:49 +05301303 if (ea_size > CONFIG_SYS_MEM_RESERVE_SECURE)
1304 ea_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
1305 else
1306 printf("Error: No enough space for secure memory.\n");
York Sun4de24ef2017-03-06 09:02:28 -08001307#endif
1308 /* Check if we have enough memory for MC */
1309 if (rem < board_reserve_ram_top(rem)) {
1310 /* Not enough memory in high region to reserve */
York Sun2db54082017-09-07 10:12:32 -07001311 if (ea_size > board_reserve_ram_top(ea_size))
1312 ea_size -= board_reserve_ram_top(ea_size);
York Sun4de24ef2017-03-06 09:02:28 -08001313 else
1314 printf("Error: No enough space for reserved memory.\n");
1315 }
1316
1317 return ea_size;
1318}
1319
Rajesh Bhagat1dde2d22018-11-05 18:01:58 +00001320#ifdef CONFIG_TFABOOT
1321phys_size_t tfa_get_dram_size(void)
1322{
1323 struct pt_regs regs;
1324 phys_size_t dram_size = 0;
1325
1326 regs.regs[0] = SMC_DRAM_BANK_INFO;
1327 regs.regs[1] = -1;
1328
1329 smc_call(&regs);
1330 if (regs.regs[0])
1331 return 0;
1332
1333 dram_size = regs.regs[1];
1334 return dram_size;
1335}
1336
1337static int tfa_dram_init_banksize(void)
1338{
1339 int i = 0, ret = 0;
1340 struct pt_regs regs;
1341 phys_size_t dram_size = tfa_get_dram_size();
1342
1343 debug("dram_size %llx\n", dram_size);
1344
1345 if (!dram_size)
1346 return -EINVAL;
1347
1348 do {
1349 regs.regs[0] = SMC_DRAM_BANK_INFO;
1350 regs.regs[1] = i;
1351
1352 smc_call(&regs);
1353 if (regs.regs[0]) {
1354 ret = -EINVAL;
1355 break;
1356 }
1357
1358 debug("bank[%d]: start %lx, size %lx\n", i, regs.regs[1],
1359 regs.regs[2]);
1360 gd->bd->bi_dram[i].start = regs.regs[1];
1361 gd->bd->bi_dram[i].size = regs.regs[2];
1362
1363 dram_size -= gd->bd->bi_dram[i].size;
1364
1365 i++;
1366 } while (dram_size);
1367
1368 if (i > 0)
1369 ret = 0;
1370
1371#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
1372 /* Assign memory for MC */
1373#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1374 if (gd->bd->bi_dram[2].size >=
1375 board_reserve_ram_top(gd->bd->bi_dram[2].size)) {
1376 gd->arch.resv_ram = gd->bd->bi_dram[2].start +
1377 gd->bd->bi_dram[2].size -
1378 board_reserve_ram_top(gd->bd->bi_dram[2].size);
1379 } else
1380#endif
1381 {
1382 if (gd->bd->bi_dram[1].size >=
1383 board_reserve_ram_top(gd->bd->bi_dram[1].size)) {
1384 gd->arch.resv_ram = gd->bd->bi_dram[1].start +
1385 gd->bd->bi_dram[1].size -
1386 board_reserve_ram_top(gd->bd->bi_dram[1].size);
1387 } else if (gd->bd->bi_dram[0].size >
1388 board_reserve_ram_top(gd->bd->bi_dram[0].size)) {
1389 gd->arch.resv_ram = gd->bd->bi_dram[0].start +
1390 gd->bd->bi_dram[0].size -
1391 board_reserve_ram_top(gd->bd->bi_dram[0].size);
1392 }
1393 }
1394#endif /* CONFIG_FSL_MC_ENET */
1395
1396 return ret;
1397}
1398#endif
1399
Simon Glass2f949c32017-03-31 08:40:32 -06001400int dram_init_banksize(void)
York Sun4de24ef2017-03-06 09:02:28 -08001401{
1402#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1403 phys_size_t dp_ddr_size;
1404#endif
1405
Rajesh Bhagat1dde2d22018-11-05 18:01:58 +00001406#ifdef CONFIG_TFABOOT
1407 if (!tfa_dram_init_banksize())
1408 return 0;
1409#endif
York Sun4de24ef2017-03-06 09:02:28 -08001410 /*
1411 * gd->ram_size has the total size of DDR memory, less reserved secure
1412 * memory. The DDR extends from low region to high region(s) presuming
1413 * no hole is created with DDR configuration. gd->arch.secure_ram tracks
1414 * the location of secure memory. gd->arch.resv_ram tracks the location
York Sunc9e3e042017-09-28 08:42:12 -07001415 * of reserved memory for Management Complex (MC). Because gd->ram_size
1416 * is reduced by this function if secure memory is reserved, checking
1417 * gd->arch.secure_ram should be done to avoid running it repeatedly.
York Sun4de24ef2017-03-06 09:02:28 -08001418 */
York Sunc9e3e042017-09-28 08:42:12 -07001419
1420#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1421 if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
1422 debug("No need to run again, skip %s\n", __func__);
1423
1424 return 0;
1425 }
1426#endif
1427
York Sun4de24ef2017-03-06 09:02:28 -08001428 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
1429 if (gd->ram_size > CONFIG_SYS_DDR_BLOCK1_SIZE) {
1430 gd->bd->bi_dram[0].size = CONFIG_SYS_DDR_BLOCK1_SIZE;
1431 gd->bd->bi_dram[1].start = CONFIG_SYS_DDR_BLOCK2_BASE;
1432 gd->bd->bi_dram[1].size = gd->ram_size -
1433 CONFIG_SYS_DDR_BLOCK1_SIZE;
1434#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1435 if (gd->bi_dram[1].size > CONFIG_SYS_DDR_BLOCK2_SIZE) {
1436 gd->bd->bi_dram[2].start = CONFIG_SYS_DDR_BLOCK3_BASE;
1437 gd->bd->bi_dram[2].size = gd->bd->bi_dram[1].size -
1438 CONFIG_SYS_DDR_BLOCK2_SIZE;
1439 gd->bd->bi_dram[1].size = CONFIG_SYS_DDR_BLOCK2_SIZE;
1440 }
1441#endif
1442 } else {
1443 gd->bd->bi_dram[0].size = gd->ram_size;
1444 }
1445#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
Sumit Garged0deea2017-10-04 03:20:49 +05301446 if (gd->bd->bi_dram[0].size >
1447 CONFIG_SYS_MEM_RESERVE_SECURE) {
1448 gd->bd->bi_dram[0].size -=
1449 CONFIG_SYS_MEM_RESERVE_SECURE;
1450 gd->arch.secure_ram = gd->bd->bi_dram[0].start +
1451 gd->bd->bi_dram[0].size;
York Sun4de24ef2017-03-06 09:02:28 -08001452 gd->arch.secure_ram |= MEM_RESERVE_SECURE_MAINTAINED;
1453 gd->ram_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
York Sun4de24ef2017-03-06 09:02:28 -08001454 }
1455#endif /* CONFIG_SYS_MEM_RESERVE_SECURE */
1456
Santan Kumar1afa9002017-05-05 15:42:29 +05301457#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
York Sun4de24ef2017-03-06 09:02:28 -08001458 /* Assign memory for MC */
1459#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1460 if (gd->bd->bi_dram[2].size >=
1461 board_reserve_ram_top(gd->bd->bi_dram[2].size)) {
1462 gd->arch.resv_ram = gd->bd->bi_dram[2].start +
1463 gd->bd->bi_dram[2].size -
1464 board_reserve_ram_top(gd->bd->bi_dram[2].size);
1465 } else
1466#endif
1467 {
1468 if (gd->bd->bi_dram[1].size >=
1469 board_reserve_ram_top(gd->bd->bi_dram[1].size)) {
1470 gd->arch.resv_ram = gd->bd->bi_dram[1].start +
1471 gd->bd->bi_dram[1].size -
1472 board_reserve_ram_top(gd->bd->bi_dram[1].size);
1473 } else if (gd->bd->bi_dram[0].size >
1474 board_reserve_ram_top(gd->bd->bi_dram[0].size)) {
1475 gd->arch.resv_ram = gd->bd->bi_dram[0].start +
1476 gd->bd->bi_dram[0].size -
1477 board_reserve_ram_top(gd->bd->bi_dram[0].size);
1478 }
1479 }
1480#endif /* CONFIG_FSL_MC_ENET */
1481
1482#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1483#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1484#error "This SoC shouldn't have DP DDR"
1485#endif
1486 if (soc_has_dp_ddr()) {
1487 /* initialize DP-DDR here */
1488 puts("DP-DDR: ");
1489 /*
1490 * DDR controller use 0 as the base address for binding.
1491 * It is mapped to CONFIG_SYS_DP_DDR_BASE for core to access.
1492 */
1493 dp_ddr_size = fsl_other_ddr_sdram(CONFIG_SYS_DP_DDR_BASE_PHY,
1494 CONFIG_DP_DDR_CTRL,
1495 CONFIG_DP_DDR_NUM_CTRLS,
1496 CONFIG_DP_DDR_DIMM_SLOTS_PER_CTLR,
1497 NULL, NULL, NULL);
1498 if (dp_ddr_size) {
1499 gd->bd->bi_dram[2].start = CONFIG_SYS_DP_DDR_BASE;
1500 gd->bd->bi_dram[2].size = dp_ddr_size;
1501 } else {
1502 puts("Not detected");
1503 }
1504 }
1505#endif
Simon Glass2f949c32017-03-31 08:40:32 -06001506
York Sunc9e3e042017-09-28 08:42:12 -07001507#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1508 debug("%s is called. gd->ram_size is reduced to %lu\n",
1509 __func__, (ulong)gd->ram_size);
1510#endif
1511
Simon Glass2f949c32017-03-31 08:40:32 -06001512 return 0;
York Sun4de24ef2017-03-06 09:02:28 -08001513}
1514
Stephen Warrend0de8062018-08-30 15:43:43 -06001515#if CONFIG_IS_ENABLED(EFI_LOADER)
York Sun4de24ef2017-03-06 09:02:28 -08001516void efi_add_known_memory(void)
1517{
1518 int i;
1519 phys_addr_t ram_start, start;
1520 phys_size_t ram_size;
1521 u64 pages;
York Sun928b6812015-12-07 11:08:58 -08001522
York Sun4de24ef2017-03-06 09:02:28 -08001523 /* Add RAM */
1524 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
1525#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1526#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1527#error "This SoC shouldn't have DP DDR"
1528#endif
1529 if (i == 2)
1530 continue; /* skip DP-DDR */
1531#endif
1532 ram_start = gd->bd->bi_dram[i].start;
1533 ram_size = gd->bd->bi_dram[i].size;
1534#ifdef CONFIG_RESV_RAM
1535 if (gd->arch.resv_ram >= ram_start &&
1536 gd->arch.resv_ram < ram_start + ram_size)
1537 ram_size = gd->arch.resv_ram - ram_start;
1538#endif
1539 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
1540 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
1541
1542 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
1543 false);
1544 }
York Sun928b6812015-12-07 11:08:58 -08001545}
York Sun4de24ef2017-03-06 09:02:28 -08001546#endif
York Sun729f2d12017-03-06 09:02:34 -08001547
1548/*
1549 * Before DDR size is known, early MMU table have DDR mapped as device memory
1550 * to avoid speculative access. To relocate U-Boot to DDR, "normal memory"
1551 * needs to be set for these mappings.
1552 * If a special case configures DDR with holes in the mapping, the holes need
1553 * to be marked as invalid. This is not implemented in this function.
1554 */
1555void update_early_mmu_table(void)
1556{
1557 if (!gd->arch.tlb_addr)
1558 return;
1559
1560 if (gd->ram_size <= CONFIG_SYS_FSL_DRAM_SIZE1) {
1561 mmu_change_region_attr(
1562 CONFIG_SYS_SDRAM_BASE,
1563 gd->ram_size,
1564 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1565 PTE_BLOCK_OUTER_SHARE |
1566 PTE_BLOCK_NS |
1567 PTE_TYPE_VALID);
1568 } else {
1569 mmu_change_region_attr(
1570 CONFIG_SYS_SDRAM_BASE,
1571 CONFIG_SYS_DDR_BLOCK1_SIZE,
1572 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1573 PTE_BLOCK_OUTER_SHARE |
1574 PTE_BLOCK_NS |
1575 PTE_TYPE_VALID);
1576#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1577#ifndef CONFIG_SYS_DDR_BLOCK2_SIZE
1578#error "Missing CONFIG_SYS_DDR_BLOCK2_SIZE"
1579#endif
1580 if (gd->ram_size - CONFIG_SYS_DDR_BLOCK1_SIZE >
1581 CONFIG_SYS_DDR_BLOCK2_SIZE) {
1582 mmu_change_region_attr(
1583 CONFIG_SYS_DDR_BLOCK2_BASE,
1584 CONFIG_SYS_DDR_BLOCK2_SIZE,
1585 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1586 PTE_BLOCK_OUTER_SHARE |
1587 PTE_BLOCK_NS |
1588 PTE_TYPE_VALID);
1589 mmu_change_region_attr(
1590 CONFIG_SYS_DDR_BLOCK3_BASE,
1591 gd->ram_size -
1592 CONFIG_SYS_DDR_BLOCK1_SIZE -
1593 CONFIG_SYS_DDR_BLOCK2_SIZE,
1594 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1595 PTE_BLOCK_OUTER_SHARE |
1596 PTE_BLOCK_NS |
1597 PTE_TYPE_VALID);
1598 } else
1599#endif
1600 {
1601 mmu_change_region_attr(
1602 CONFIG_SYS_DDR_BLOCK2_BASE,
1603 gd->ram_size -
1604 CONFIG_SYS_DDR_BLOCK1_SIZE,
1605 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1606 PTE_BLOCK_OUTER_SHARE |
1607 PTE_BLOCK_NS |
1608 PTE_TYPE_VALID);
1609 }
1610 }
1611}
1612
1613__weak int dram_init(void)
1614{
Simon Glass0e0ac202017-04-06 12:47:04 -06001615 fsl_initdram();
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +00001616#if (!defined(CONFIG_SPL) && !defined(CONFIG_TFABOOT)) || \
1617 defined(CONFIG_SPL_BUILD)
York Sun729f2d12017-03-06 09:02:34 -08001618 /* This will break-before-make MMU for DDR */
1619 update_early_mmu_table();
1620#endif
1621
1622 return 0;
1623}