blob: 59d03b167c46e25e10f68a53869ab2b79cdcac9c [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 Glassdb229612019-08-01 09:46:42 -06008#include <env.h>
Simon Glass0e0ac202017-04-06 12:47:04 -06009#include <fsl_ddr_sdram.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070010#include <vsprintf.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080011#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080013#include <asm/system.h>
Joe Hershberger8f454d92018-07-16 15:33:51 -050014#include <fm_eth.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080015#include <asm/armv8/mmu.h>
16#include <asm/io.h>
17#include <asm/arch/fsl_serdes.h>
18#include <asm/arch/soc.h>
19#include <asm/arch/cpu.h>
20#include <asm/arch/speed.h>
Ashish Kumar11234062017-08-11 11:09:14 +053021#include <fsl_immap.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080022#include <asm/arch/mp.h>
Alexander Graf12be31c2016-11-17 01:03:01 +010023#include <efi_loader.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080024#include <fsl-mc/fsl_mc.h>
25#ifdef CONFIG_FSL_ESDHC
26#include <fsl_esdhc.h>
27#endif
Hou Zhiqiang21c4d552016-06-28 20:18:15 +080028#include <asm/armv8/sec_firmware.h>
Shengzhou Liu15875a52016-11-21 11:36:48 +080029#ifdef CONFIG_SYS_FSL_DDR
30#include <fsl_ddr.h>
31#endif
Simon Glass243182c2017-05-17 08:23:06 -060032#include <asm/arch/clock.h>
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +053033#include <hwconfig.h>
Ahmed Mansouraa270b42017-12-15 16:01:00 -050034#include <fsl_qbman.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080035
Rajesh Bhagat583da8b2018-11-05 18:01:42 +000036#ifdef CONFIG_TFABOOT
Simon Glass9d1f6192019-08-02 09:44:25 -060037#include <env_internal.h>
Pankit Gargbdbf84f2018-11-05 18:01:52 +000038#ifdef CONFIG_CHAIN_OF_TRUST
39#include <fsl_validate.h>
40#endif
Rajesh Bhagat583da8b2018-11-05 18:01:42 +000041#endif
42
Mingkai Hu0e58b512015-10-26 19:47:50 +080043DECLARE_GLOBAL_DATA_PTR;
44
York Sunef4cef92018-11-05 18:01:06 +000045static struct cpu_type cpu_type_list[] = {
46 CPU_TYPE_ENTRY(LS2080A, LS2080A, 8),
47 CPU_TYPE_ENTRY(LS2085A, LS2085A, 8),
48 CPU_TYPE_ENTRY(LS2045A, LS2045A, 4),
49 CPU_TYPE_ENTRY(LS2088A, LS2088A, 8),
50 CPU_TYPE_ENTRY(LS2084A, LS2084A, 8),
51 CPU_TYPE_ENTRY(LS2048A, LS2048A, 4),
52 CPU_TYPE_ENTRY(LS2044A, LS2044A, 4),
53 CPU_TYPE_ENTRY(LS2081A, LS2081A, 8),
54 CPU_TYPE_ENTRY(LS2041A, LS2041A, 4),
55 CPU_TYPE_ENTRY(LS1043A, LS1043A, 4),
Hou Zhiqiangb9aedf92018-12-20 06:31:17 +000056 CPU_TYPE_ENTRY(LS1043A, LS1043A_P23, 4),
York Sunef4cef92018-11-05 18:01:06 +000057 CPU_TYPE_ENTRY(LS1023A, LS1023A, 2),
Hou Zhiqiangb9aedf92018-12-20 06:31:17 +000058 CPU_TYPE_ENTRY(LS1023A, LS1023A_P23, 2),
York Sunef4cef92018-11-05 18:01:06 +000059 CPU_TYPE_ENTRY(LS1046A, LS1046A, 4),
60 CPU_TYPE_ENTRY(LS1026A, LS1026A, 2),
61 CPU_TYPE_ENTRY(LS2040A, LS2040A, 4),
62 CPU_TYPE_ENTRY(LS1012A, LS1012A, 1),
Yuantian Tangf463d752019-09-18 16:50:52 +080063 CPU_TYPE_ENTRY(LS1017A, LS1017A, 1),
64 CPU_TYPE_ENTRY(LS1018A, LS1018A, 1),
65 CPU_TYPE_ENTRY(LS1027A, LS1027A, 2),
Yuantian Tang4aefa162019-04-10 16:43:33 +080066 CPU_TYPE_ENTRY(LS1028A, LS1028A, 2),
York Sunef4cef92018-11-05 18:01:06 +000067 CPU_TYPE_ENTRY(LS1088A, LS1088A, 8),
68 CPU_TYPE_ENTRY(LS1084A, LS1084A, 8),
69 CPU_TYPE_ENTRY(LS1048A, LS1048A, 4),
70 CPU_TYPE_ENTRY(LS1044A, LS1044A, 4),
Priyanka Jainef76b2e2018-10-29 09:17:09 +000071 CPU_TYPE_ENTRY(LX2160A, LX2160A, 16),
72 CPU_TYPE_ENTRY(LX2120A, LX2120A, 12),
73 CPU_TYPE_ENTRY(LX2080A, LX2080A, 8),
York Sunef4cef92018-11-05 18:01:06 +000074};
75
76#define EARLY_PGTABLE_SIZE 0x5000
77static struct mm_region early_map[] = {
78#ifdef CONFIG_FSL_LSCH3
79 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
80 CONFIG_SYS_FSL_CCSR_SIZE,
81 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
82 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
83 },
84 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
85 SYS_FSL_OCRAM_SPACE_SIZE,
86 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
87 },
88 { CONFIG_SYS_FSL_QSPI_BASE1, CONFIG_SYS_FSL_QSPI_BASE1,
89 CONFIG_SYS_FSL_QSPI_SIZE1,
90 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE},
91#ifdef CONFIG_FSL_IFC
92 /* For IFC Region #1, only the first 4MB is cache-enabled */
93 { CONFIG_SYS_FSL_IFC_BASE1, CONFIG_SYS_FSL_IFC_BASE1,
94 CONFIG_SYS_FSL_IFC_SIZE1_1,
95 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
96 },
97 { CONFIG_SYS_FSL_IFC_BASE1 + CONFIG_SYS_FSL_IFC_SIZE1_1,
98 CONFIG_SYS_FSL_IFC_BASE1 + CONFIG_SYS_FSL_IFC_SIZE1_1,
99 CONFIG_SYS_FSL_IFC_SIZE1 - CONFIG_SYS_FSL_IFC_SIZE1_1,
100 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
101 },
102 { CONFIG_SYS_FLASH_BASE, CONFIG_SYS_FSL_IFC_BASE1,
103 CONFIG_SYS_FSL_IFC_SIZE1,
104 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
105 },
106#endif
107 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
108 CONFIG_SYS_FSL_DRAM_SIZE1,
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +0000109#if defined(CONFIG_TFABOOT) || \
110 (defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD))
York Sunef4cef92018-11-05 18:01:06 +0000111 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
112#else /* Start with nGnRnE and PXN and UXN to prevent speculative access */
113 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
114#endif
115 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
116 },
117#ifdef CONFIG_FSL_IFC
118 /* Map IFC region #2 up to CONFIG_SYS_FLASH_BASE for NAND boot */
119 { CONFIG_SYS_FSL_IFC_BASE2, CONFIG_SYS_FSL_IFC_BASE2,
120 CONFIG_SYS_FLASH_BASE - CONFIG_SYS_FSL_IFC_BASE2,
121 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
122 },
123#endif
124 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
125 CONFIG_SYS_FSL_DCSR_SIZE,
126 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
127 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
128 },
129 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
130 CONFIG_SYS_FSL_DRAM_SIZE2,
131 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
132 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
133 },
Priyanka Jain88c25662018-10-29 09:11:29 +0000134#ifdef CONFIG_SYS_FSL_DRAM_BASE3
135 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
136 CONFIG_SYS_FSL_DRAM_SIZE3,
137 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
138 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
139 },
140#endif
York Sunef4cef92018-11-05 18:01:06 +0000141#elif defined(CONFIG_FSL_LSCH2)
142 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
143 CONFIG_SYS_FSL_CCSR_SIZE,
144 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
145 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
146 },
147 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
148 SYS_FSL_OCRAM_SPACE_SIZE,
149 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
150 },
151 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
152 CONFIG_SYS_FSL_DCSR_SIZE,
153 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
154 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
155 },
156 { CONFIG_SYS_FSL_QSPI_BASE, CONFIG_SYS_FSL_QSPI_BASE,
157 CONFIG_SYS_FSL_QSPI_SIZE,
158 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
159 },
160#ifdef CONFIG_FSL_IFC
161 { CONFIG_SYS_FSL_IFC_BASE, CONFIG_SYS_FSL_IFC_BASE,
162 CONFIG_SYS_FSL_IFC_SIZE,
163 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
164 },
165#endif
166 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
167 CONFIG_SYS_FSL_DRAM_SIZE1,
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +0000168#if defined(CONFIG_TFABOOT) || \
169 (defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD))
York Sunef4cef92018-11-05 18:01:06 +0000170 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
171#else /* Start with nGnRnE and PXN and UXN to prevent speculative access */
172 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
173#endif
174 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
175 },
176 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
177 CONFIG_SYS_FSL_DRAM_SIZE2,
178 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
179 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
180 },
181#endif
182 {}, /* list terminator */
183};
184
185static struct mm_region final_map[] = {
186#ifdef CONFIG_FSL_LSCH3
187 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
188 CONFIG_SYS_FSL_CCSR_SIZE,
189 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
190 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
191 },
192 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
193 SYS_FSL_OCRAM_SPACE_SIZE,
194 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
195 },
196 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
197 CONFIG_SYS_FSL_DRAM_SIZE1,
198 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
199 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
200 },
201 { CONFIG_SYS_FSL_QSPI_BASE1, CONFIG_SYS_FSL_QSPI_BASE1,
202 CONFIG_SYS_FSL_QSPI_SIZE1,
203 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
204 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
205 },
206 { CONFIG_SYS_FSL_QSPI_BASE2, CONFIG_SYS_FSL_QSPI_BASE2,
207 CONFIG_SYS_FSL_QSPI_SIZE2,
208 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
209 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
210 },
211#ifdef CONFIG_FSL_IFC
212 { CONFIG_SYS_FSL_IFC_BASE2, CONFIG_SYS_FSL_IFC_BASE2,
213 CONFIG_SYS_FSL_IFC_SIZE2,
214 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
215 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
216 },
217#endif
218 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
219 CONFIG_SYS_FSL_DCSR_SIZE,
220 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
221 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
222 },
223 { CONFIG_SYS_FSL_MC_BASE, CONFIG_SYS_FSL_MC_BASE,
224 CONFIG_SYS_FSL_MC_SIZE,
225 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
226 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
227 },
228 { CONFIG_SYS_FSL_NI_BASE, CONFIG_SYS_FSL_NI_BASE,
229 CONFIG_SYS_FSL_NI_SIZE,
230 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
231 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
232 },
233 /* For QBMAN portal, only the first 64MB is cache-enabled */
234 { CONFIG_SYS_FSL_QBMAN_BASE, CONFIG_SYS_FSL_QBMAN_BASE,
235 CONFIG_SYS_FSL_QBMAN_SIZE_1,
236 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
237 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN | PTE_BLOCK_NS
238 },
239 { CONFIG_SYS_FSL_QBMAN_BASE + CONFIG_SYS_FSL_QBMAN_SIZE_1,
240 CONFIG_SYS_FSL_QBMAN_BASE + CONFIG_SYS_FSL_QBMAN_SIZE_1,
241 CONFIG_SYS_FSL_QBMAN_SIZE - CONFIG_SYS_FSL_QBMAN_SIZE_1,
242 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
243 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
244 },
245 { CONFIG_SYS_PCIE1_PHYS_ADDR, CONFIG_SYS_PCIE1_PHYS_ADDR,
246 CONFIG_SYS_PCIE1_PHYS_SIZE,
247 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
248 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
249 },
250 { CONFIG_SYS_PCIE2_PHYS_ADDR, CONFIG_SYS_PCIE2_PHYS_ADDR,
251 CONFIG_SYS_PCIE2_PHYS_SIZE,
252 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
253 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
254 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800255#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000256 { CONFIG_SYS_PCIE3_PHYS_ADDR, CONFIG_SYS_PCIE3_PHYS_ADDR,
257 CONFIG_SYS_PCIE3_PHYS_SIZE,
258 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
259 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
260 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800261#endif
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000262#ifdef CONFIG_SYS_PCIE4_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000263 { CONFIG_SYS_PCIE4_PHYS_ADDR, CONFIG_SYS_PCIE4_PHYS_ADDR,
264 CONFIG_SYS_PCIE4_PHYS_SIZE,
265 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
266 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
267 },
268#endif
Hou Zhiqiangd08f9702019-04-08 10:15:41 +0000269#ifdef SYS_PCIE5_PHYS_ADDR
270 { SYS_PCIE5_PHYS_ADDR, SYS_PCIE5_PHYS_ADDR,
271 SYS_PCIE5_PHYS_SIZE,
272 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
273 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
274 },
275#endif
276#ifdef SYS_PCIE6_PHYS_ADDR
277 { SYS_PCIE6_PHYS_ADDR, SYS_PCIE6_PHYS_ADDR,
278 SYS_PCIE6_PHYS_SIZE,
279 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
280 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
281 },
282#endif
York Sunef4cef92018-11-05 18:01:06 +0000283 { CONFIG_SYS_FSL_WRIOP1_BASE, CONFIG_SYS_FSL_WRIOP1_BASE,
284 CONFIG_SYS_FSL_WRIOP1_SIZE,
285 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
286 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
287 },
288 { CONFIG_SYS_FSL_AIOP1_BASE, CONFIG_SYS_FSL_AIOP1_BASE,
289 CONFIG_SYS_FSL_AIOP1_SIZE,
290 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
291 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
292 },
293 { CONFIG_SYS_FSL_PEBUF_BASE, CONFIG_SYS_FSL_PEBUF_BASE,
294 CONFIG_SYS_FSL_PEBUF_SIZE,
295 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
296 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
297 },
298 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
299 CONFIG_SYS_FSL_DRAM_SIZE2,
300 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
301 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
302 },
Priyanka Jain88c25662018-10-29 09:11:29 +0000303#ifdef CONFIG_SYS_FSL_DRAM_BASE3
304 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
305 CONFIG_SYS_FSL_DRAM_SIZE3,
306 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
307 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
308 },
309#endif
York Sunef4cef92018-11-05 18:01:06 +0000310#elif defined(CONFIG_FSL_LSCH2)
311 { CONFIG_SYS_FSL_BOOTROM_BASE, CONFIG_SYS_FSL_BOOTROM_BASE,
312 CONFIG_SYS_FSL_BOOTROM_SIZE,
313 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
314 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
315 },
316 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
317 CONFIG_SYS_FSL_CCSR_SIZE,
318 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
319 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
320 },
321 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
322 SYS_FSL_OCRAM_SPACE_SIZE,
323 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
324 },
325 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
326 CONFIG_SYS_FSL_DCSR_SIZE,
327 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
328 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
329 },
330 { CONFIG_SYS_FSL_QSPI_BASE, CONFIG_SYS_FSL_QSPI_BASE,
331 CONFIG_SYS_FSL_QSPI_SIZE,
332 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
333 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
334 },
335#ifdef CONFIG_FSL_IFC
336 { CONFIG_SYS_FSL_IFC_BASE, CONFIG_SYS_FSL_IFC_BASE,
337 CONFIG_SYS_FSL_IFC_SIZE,
338 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
339 },
340#endif
341 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
342 CONFIG_SYS_FSL_DRAM_SIZE1,
343 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
344 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
345 },
346 { CONFIG_SYS_FSL_QBMAN_BASE, CONFIG_SYS_FSL_QBMAN_BASE,
347 CONFIG_SYS_FSL_QBMAN_SIZE,
348 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
349 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
350 },
351 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
352 CONFIG_SYS_FSL_DRAM_SIZE2,
353 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
354 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
355 },
356 { CONFIG_SYS_PCIE1_PHYS_ADDR, CONFIG_SYS_PCIE1_PHYS_ADDR,
357 CONFIG_SYS_PCIE1_PHYS_SIZE,
358 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
359 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
360 },
361 { CONFIG_SYS_PCIE2_PHYS_ADDR, CONFIG_SYS_PCIE2_PHYS_ADDR,
362 CONFIG_SYS_PCIE2_PHYS_SIZE,
363 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
364 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
365 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800366#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000367 { CONFIG_SYS_PCIE3_PHYS_ADDR, CONFIG_SYS_PCIE3_PHYS_ADDR,
368 CONFIG_SYS_PCIE3_PHYS_SIZE,
369 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
370 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
371 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800372#endif
York Sunef4cef92018-11-05 18:01:06 +0000373 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
374 CONFIG_SYS_FSL_DRAM_SIZE3,
375 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
376 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
377 },
378#endif
379#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
380 {}, /* space holder for secure mem */
381#endif
382 {},
383};
384
York Sun9da8f502016-06-24 16:46:23 -0700385struct mm_region *mem_map = early_map;
Alexander Grafce0a64e2016-03-04 01:09:54 +0100386
Mingkai Hu0e58b512015-10-26 19:47:50 +0800387void cpu_name(char *name)
388{
389 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
390 unsigned int i, svr, ver;
391
392 svr = gur_in32(&gur->svr);
393 ver = SVR_SOC_VER(svr);
394
395 for (i = 0; i < ARRAY_SIZE(cpu_type_list); i++)
396 if ((cpu_type_list[i].soc_ver & SVR_WO_E) == ver) {
397 strcpy(name, cpu_type_list[i].name);
Priyanka Jainef76b2e2018-10-29 09:17:09 +0000398#ifdef CONFIG_ARCH_LX2160A
399 if (IS_C_PROCESSOR(svr))
400 strcat(name, "C");
401#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800402
403 if (IS_E_PROCESSOR(svr))
404 strcat(name, "E");
Wenbin Song863a33a2016-09-13 16:13:54 +0800405
406 sprintf(name + strlen(name), " Rev%d.%d",
407 SVR_MAJ(svr), SVR_MIN(svr));
Mingkai Hu0e58b512015-10-26 19:47:50 +0800408 break;
409 }
410
411 if (i == ARRAY_SIZE(cpu_type_list))
412 strcpy(name, "unknown");
413}
414
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400415#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Mingkai Hu0e58b512015-10-26 19:47:50 +0800416/*
417 * To start MMU before DDR is available, we create MMU table in SRAM.
418 * The base address of SRAM is CONFIG_SYS_FSL_OCRAM_BASE. We use three
419 * levels of translation tables here to cover 40-bit address space.
420 * We use 4KB granule size, with 40 bits physical address, T0SZ=24
York Sun9da8f502016-06-24 16:46:23 -0700421 * Address above EARLY_PGTABLE_SIZE (0x5000) is free for other purpose.
422 * Note, the debug print in cache_v8.c is not usable for debugging
423 * these early MMU tables because UART is not yet available.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800424 */
425static inline void early_mmu_setup(void)
426{
York Sun9da8f502016-06-24 16:46:23 -0700427 unsigned int el = current_el();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800428
York Sun9da8f502016-06-24 16:46:23 -0700429 /* global data is already setup, no allocation yet */
Pankit Gargc4d39eb2018-11-05 18:01:28 +0000430 if (el == 3)
431 gd->arch.tlb_addr = CONFIG_SYS_FSL_OCRAM_BASE;
432 else
433 gd->arch.tlb_addr = CONFIG_SYS_DDR_SDRAM_BASE;
York Sun9da8f502016-06-24 16:46:23 -0700434 gd->arch.tlb_fillptr = gd->arch.tlb_addr;
435 gd->arch.tlb_size = EARLY_PGTABLE_SIZE;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800436
York Sun9da8f502016-06-24 16:46:23 -0700437 /* Create early page tables */
438 setup_pgtables();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800439
York Sun9da8f502016-06-24 16:46:23 -0700440 /* point TTBR to the new table */
441 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
442 get_tcr(el, NULL, NULL) &
443 ~(TCR_ORGN_MASK | TCR_IRGN_MASK),
Mingkai Hu0e58b512015-10-26 19:47:50 +0800444 MEMORY_ATTRIBUTES);
York Sun9da8f502016-06-24 16:46:23 -0700445
Mingkai Hu0e58b512015-10-26 19:47:50 +0800446 set_sctlr(get_sctlr() | CR_M);
447}
448
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800449static void fix_pcie_mmu_map(void)
450{
York Sun4ce6fbf2017-03-27 11:41:01 -0700451#ifdef CONFIG_ARCH_LS2080A
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800452 unsigned int i;
453 u32 svr, ver;
454 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
455
456 svr = gur_in32(&gur->svr);
457 ver = SVR_SOC_VER(svr);
458
459 /* Fix PCIE base and size for LS2088A */
460 if ((ver == SVR_LS2088A) || (ver == SVR_LS2084A) ||
Priyanka Jain2b361782017-04-27 15:08:06 +0530461 (ver == SVR_LS2048A) || (ver == SVR_LS2044A) ||
462 (ver == SVR_LS2081A) || (ver == SVR_LS2041A)) {
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800463 for (i = 0; i < ARRAY_SIZE(final_map); i++) {
464 switch (final_map[i].phys) {
465 case CONFIG_SYS_PCIE1_PHYS_ADDR:
466 final_map[i].phys = 0x2000000000ULL;
467 final_map[i].virt = 0x2000000000ULL;
468 final_map[i].size = 0x800000000ULL;
469 break;
470 case CONFIG_SYS_PCIE2_PHYS_ADDR:
471 final_map[i].phys = 0x2800000000ULL;
472 final_map[i].virt = 0x2800000000ULL;
473 final_map[i].size = 0x800000000ULL;
474 break;
Yuantian Tang4aefa162019-04-10 16:43:33 +0800475#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800476 case CONFIG_SYS_PCIE3_PHYS_ADDR:
477 final_map[i].phys = 0x3000000000ULL;
478 final_map[i].virt = 0x3000000000ULL;
479 final_map[i].size = 0x800000000ULL;
480 break;
Yuantian Tang4aefa162019-04-10 16:43:33 +0800481#endif
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000482#ifdef CONFIG_SYS_PCIE4_PHYS_ADDR
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800483 case CONFIG_SYS_PCIE4_PHYS_ADDR:
484 final_map[i].phys = 0x3800000000ULL;
485 final_map[i].virt = 0x3800000000ULL;
486 final_map[i].size = 0x800000000ULL;
487 break;
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000488#endif
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800489 default:
490 break;
491 }
492 }
493 }
494#endif
495}
496
Mingkai Hu0e58b512015-10-26 19:47:50 +0800497/*
498 * The final tables look similar to early tables, but different in detail.
499 * These tables are in DRAM. Sub tables are added to enable cache for
500 * QBMan and OCRAM.
501 *
York Sun1ef95cc2016-06-24 16:46:18 -0700502 * Put the MMU table in secure memory if gd->arch.secure_ram is valid.
503 * OCRAM will be not used for this purpose so gd->arch.secure_ram can't be 0.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800504 */
505static inline void final_mmu_setup(void)
506{
York Sun9da8f502016-06-24 16:46:23 -0700507 u64 tlb_addr_save = gd->arch.tlb_addr;
York Sun0804d562015-12-04 11:57:08 -0800508 unsigned int el = current_el();
York Sun9da8f502016-06-24 16:46:23 -0700509 int index;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800510
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800511 /* fix the final_map before filling in the block entries */
512 fix_pcie_mmu_map();
513
York Sun9da8f502016-06-24 16:46:23 -0700514 mem_map = final_map;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800515
York Sun75488ed2017-03-06 09:02:30 -0800516 /* Update mapping for DDR to actual size */
517 for (index = 0; index < ARRAY_SIZE(final_map) - 2; index++) {
518 /*
519 * Find the entry for DDR mapping and update the address and
520 * size. Zero-sized mapping will be skipped when creating MMU
521 * table.
522 */
523 switch (final_map[index].virt) {
524 case CONFIG_SYS_FSL_DRAM_BASE1:
525 final_map[index].virt = gd->bd->bi_dram[0].start;
526 final_map[index].phys = gd->bd->bi_dram[0].start;
527 final_map[index].size = gd->bd->bi_dram[0].size;
528 break;
529#ifdef CONFIG_SYS_FSL_DRAM_BASE2
530 case CONFIG_SYS_FSL_DRAM_BASE2:
531#if (CONFIG_NR_DRAM_BANKS >= 2)
532 final_map[index].virt = gd->bd->bi_dram[1].start;
533 final_map[index].phys = gd->bd->bi_dram[1].start;
534 final_map[index].size = gd->bd->bi_dram[1].size;
535#else
536 final_map[index].size = 0;
537#endif
538 break;
539#endif
540#ifdef CONFIG_SYS_FSL_DRAM_BASE3
541 case CONFIG_SYS_FSL_DRAM_BASE3:
542#if (CONFIG_NR_DRAM_BANKS >= 3)
543 final_map[index].virt = gd->bd->bi_dram[2].start;
544 final_map[index].phys = gd->bd->bi_dram[2].start;
545 final_map[index].size = gd->bd->bi_dram[2].size;
546#else
547 final_map[index].size = 0;
548#endif
549 break;
550#endif
551 default:
552 break;
553 }
554 }
555
York Sun0804d562015-12-04 11:57:08 -0800556#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
York Sun9da8f502016-06-24 16:46:23 -0700557 if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
558 if (el == 3) {
559 /*
560 * Only use gd->arch.secure_ram if the address is
561 * recalculated. Align to 4KB for MMU table.
562 */
563 /* put page tables in secure ram */
564 index = ARRAY_SIZE(final_map) - 2;
565 gd->arch.tlb_addr = gd->arch.secure_ram & ~0xfff;
566 final_map[index].virt = gd->arch.secure_ram & ~0x3;
567 final_map[index].phys = final_map[index].virt;
568 final_map[index].size = CONFIG_SYS_MEM_RESERVE_SECURE;
569 final_map[index].attrs = PTE_BLOCK_OUTER_SHARE;
York Sun1ef95cc2016-06-24 16:46:18 -0700570 gd->arch.secure_ram |= MEM_RESERVE_SECURE_SECURED;
York Sun9da8f502016-06-24 16:46:23 -0700571 tlb_addr_save = gd->arch.tlb_addr;
York Sun0804d562015-12-04 11:57:08 -0800572 } else {
York Sun9da8f502016-06-24 16:46:23 -0700573 /* Use allocated (board_f.c) memory for TLB */
574 tlb_addr_save = gd->arch.tlb_allocated;
575 gd->arch.tlb_addr = tlb_addr_save;
York Sun0804d562015-12-04 11:57:08 -0800576 }
577 }
578#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800579
York Sun9da8f502016-06-24 16:46:23 -0700580 /* Reset the fill ptr */
581 gd->arch.tlb_fillptr = tlb_addr_save;
582
583 /* Create normal system page tables */
584 setup_pgtables();
585
586 /* Create emergency page tables */
587 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
588 gd->arch.tlb_emerg = gd->arch.tlb_addr;
589 setup_pgtables();
590 gd->arch.tlb_addr = tlb_addr_save;
591
York Suncf64ced2017-03-06 09:02:31 -0800592 /* Disable cache and MMU */
593 dcache_disable(); /* TLBs are invalidated */
594 invalidate_icache_all();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800595
596 /* point TTBR to the new table */
York Sun9da8f502016-06-24 16:46:23 -0700597 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
Mingkai Hu0e58b512015-10-26 19:47:50 +0800598 MEMORY_ATTRIBUTES);
York Suncf64ced2017-03-06 09:02:31 -0800599
York Suneb6eac12016-07-22 10:52:23 -0700600 set_sctlr(get_sctlr() | CR_M);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800601}
602
Alexander Grafbc78b922016-03-21 20:26:12 +0100603u64 get_page_table_size(void)
604{
605 return 0x10000;
606}
607
Mingkai Hu0e58b512015-10-26 19:47:50 +0800608int arch_cpu_init(void)
609{
York Sune6b871e2017-05-15 08:51:59 -0700610 /*
611 * This function is called before U-Boot relocates itself to speed up
612 * on system running. It is not necessary to run if performance is not
613 * critical. Skip if MMU is already enabled by SPL or other means.
614 */
615 if (get_sctlr() & CR_M)
616 return 0;
617
Mingkai Hu0e58b512015-10-26 19:47:50 +0800618 icache_enable();
619 __asm_invalidate_dcache_all();
620 __asm_invalidate_tlb_all();
621 early_mmu_setup();
622 set_sctlr(get_sctlr() | CR_C);
623 return 0;
624}
625
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800626void mmu_setup(void)
627{
628 final_mmu_setup();
629}
630
Mingkai Hu0e58b512015-10-26 19:47:50 +0800631/*
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800632 * This function is called from common/board_r.c.
633 * It recreates MMU table in main memory.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800634 */
635void enable_caches(void)
636{
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800637 mmu_setup();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800638 __asm_invalidate_tlb_all();
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800639 icache_enable();
640 dcache_enable();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800641}
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400642#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000643
644#ifdef CONFIG_TFABOOT
645enum boot_src __get_boot_src(u32 porsr1)
646{
647 enum boot_src src = BOOT_SOURCE_RESERVED;
648 u32 rcw_src = (porsr1 & RCW_SRC_MASK) >> RCW_SRC_BIT;
Priyanka Jain88c25662018-10-29 09:11:29 +0000649#if !defined(CONFIG_NXP_LSCH3_2)
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000650 u32 val;
651#endif
652 debug("%s: rcw_src 0x%x\n", __func__, rcw_src);
653
654#if defined(CONFIG_FSL_LSCH3)
Priyanka Jain88c25662018-10-29 09:11:29 +0000655#if defined(CONFIG_NXP_LSCH3_2)
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000656 switch (rcw_src) {
657 case RCW_SRC_SDHC1_VAL:
658 src = BOOT_SOURCE_SD_MMC;
659 break;
660 case RCW_SRC_SDHC2_VAL:
661 src = BOOT_SOURCE_SD_MMC2;
662 break;
663 case RCW_SRC_I2C1_VAL:
664 src = BOOT_SOURCE_I2C1_EXTENDED;
665 break;
666 case RCW_SRC_FLEXSPI_NAND2K_VAL:
667 src = BOOT_SOURCE_XSPI_NAND;
668 break;
669 case RCW_SRC_FLEXSPI_NAND4K_VAL:
670 src = BOOT_SOURCE_XSPI_NAND;
671 break;
672 case RCW_SRC_RESERVED_1_VAL:
673 src = BOOT_SOURCE_RESERVED;
674 break;
675 case RCW_SRC_FLEXSPI_NOR_24B:
676 src = BOOT_SOURCE_XSPI_NOR;
677 break;
678 default:
679 src = BOOT_SOURCE_RESERVED;
680 }
681#else
682 val = rcw_src & RCW_SRC_TYPE_MASK;
683 if (val == RCW_SRC_NOR_VAL) {
684 val = rcw_src & NOR_TYPE_MASK;
685
686 switch (val) {
687 case NOR_16B_VAL:
688 case NOR_32B_VAL:
689 src = BOOT_SOURCE_IFC_NOR;
690 break;
691 default:
692 src = BOOT_SOURCE_RESERVED;
693 }
694 } else {
695 /* RCW SRC Serial Flash */
696 val = rcw_src & RCW_SRC_SERIAL_MASK;
697 switch (val) {
698 case RCW_SRC_QSPI_VAL:
699 /* RCW SRC Serial NOR (QSPI) */
700 src = BOOT_SOURCE_QSPI_NOR;
701 break;
702 case RCW_SRC_SD_CARD_VAL:
703 /* RCW SRC SD Card */
704 src = BOOT_SOURCE_SD_MMC;
705 break;
706 case RCW_SRC_EMMC_VAL:
707 /* RCW SRC EMMC */
Rajesh Bhagat5b73c902018-12-27 04:37:49 +0000708 src = BOOT_SOURCE_SD_MMC;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000709 break;
710 case RCW_SRC_I2C1_VAL:
711 /* RCW SRC I2C1 Extended */
712 src = BOOT_SOURCE_I2C1_EXTENDED;
713 break;
714 default:
715 src = BOOT_SOURCE_RESERVED;
716 }
717 }
718#endif
719#elif defined(CONFIG_FSL_LSCH2)
720 /* RCW SRC NAND */
721 val = rcw_src & RCW_SRC_NAND_MASK;
722 if (val == RCW_SRC_NAND_VAL) {
723 val = rcw_src & NAND_RESERVED_MASK;
724 if (val != NAND_RESERVED_1 && val != NAND_RESERVED_2)
725 src = BOOT_SOURCE_IFC_NAND;
726
727 } else {
728 /* RCW SRC NOR */
729 val = rcw_src & RCW_SRC_NOR_MASK;
730 if (val == NOR_8B_VAL || val == NOR_16B_VAL) {
731 src = BOOT_SOURCE_IFC_NOR;
732 } else {
733 switch (rcw_src) {
734 case QSPI_VAL1:
735 case QSPI_VAL2:
736 src = BOOT_SOURCE_QSPI_NOR;
737 break;
738 case SD_VAL:
739 src = BOOT_SOURCE_SD_MMC;
740 break;
741 default:
742 src = BOOT_SOURCE_RESERVED;
743 }
744 }
745 }
746#endif
York Sun8f3f4ef2018-11-05 18:02:09 +0000747
748 if (CONFIG_IS_ENABLED(SYS_FSL_ERRATUM_A010539) && !rcw_src)
749 src = BOOT_SOURCE_QSPI_NOR;
750
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000751 debug("%s: src 0x%x\n", __func__, src);
752 return src;
753}
754
755enum boot_src get_boot_src(void)
756{
York Sun8f3f4ef2018-11-05 18:02:09 +0000757 struct pt_regs regs;
758 u32 porsr1 = 0;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000759
760#if defined(CONFIG_FSL_LSCH3)
761 u32 __iomem *dcfg_ccsr = (u32 __iomem *)DCFG_BASE;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000762#elif defined(CONFIG_FSL_LSCH2)
763 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
York Sun8f3f4ef2018-11-05 18:02:09 +0000764#endif
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000765
York Sun8f3f4ef2018-11-05 18:02:09 +0000766 if (current_el() == 2) {
767 regs.regs[0] = SIP_SVC_RCW;
768
769 smc_call(&regs);
770 if (!regs.regs[0])
771 porsr1 = regs.regs[1];
772 }
773
774 if (current_el() == 3 || !porsr1) {
775#ifdef CONFIG_FSL_LSCH3
776 porsr1 = in_le32(dcfg_ccsr + DCFG_PORSR1 / 4);
777#elif defined(CONFIG_FSL_LSCH2)
778 porsr1 = in_be32(&gur->porsr1);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800779#endif
York Sun8f3f4ef2018-11-05 18:02:09 +0000780 }
781
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000782 debug("%s: porsr1 0x%x\n", __func__, porsr1);
783
784 return __get_boot_src(porsr1);
785}
786
787#ifdef CONFIG_ENV_IS_IN_MMC
788int mmc_get_env_dev(void)
789{
790 enum boot_src src = get_boot_src();
791 int dev = CONFIG_SYS_MMC_ENV_DEV;
792
793 switch (src) {
794 case BOOT_SOURCE_SD_MMC:
795 dev = 0;
796 break;
797 case BOOT_SOURCE_SD_MMC2:
798 dev = 1;
799 break;
800 default:
801 break;
802 }
803
804 return dev;
805}
806#endif
807
808enum env_location env_get_location(enum env_operation op, int prio)
809{
810 enum boot_src src = get_boot_src();
811 enum env_location env_loc = ENVL_NOWHERE;
812
813 if (prio)
814 return ENVL_UNKNOWN;
815
Udit Agarwal5e9720c2019-04-23 06:06:04 +0000816#ifdef CONFIG_ENV_IS_NOWHERE
817 return env_loc;
Pankit Gargbdbf84f2018-11-05 18:01:52 +0000818#endif
819
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000820 switch (src) {
821 case BOOT_SOURCE_IFC_NOR:
822 env_loc = ENVL_FLASH;
823 break;
824 case BOOT_SOURCE_QSPI_NOR:
825 /* FALLTHROUGH */
826 case BOOT_SOURCE_XSPI_NOR:
827 env_loc = ENVL_SPI_FLASH;
828 break;
829 case BOOT_SOURCE_IFC_NAND:
830 /* FALLTHROUGH */
831 case BOOT_SOURCE_QSPI_NAND:
832 /* FALLTHROUGH */
833 case BOOT_SOURCE_XSPI_NAND:
834 env_loc = ENVL_NAND;
835 break;
836 case BOOT_SOURCE_SD_MMC:
837 /* FALLTHROUGH */
838 case BOOT_SOURCE_SD_MMC2:
839 env_loc = ENVL_MMC;
840 break;
841 case BOOT_SOURCE_I2C1_EXTENDED:
842 /* FALLTHROUGH */
843 default:
844 break;
845 }
846
847 return env_loc;
848}
849#endif /* CONFIG_TFABOOT */
Mingkai Hu0e58b512015-10-26 19:47:50 +0800850
Priyanka Jain9a276702016-11-17 12:29:56 +0530851u32 initiator_type(u32 cluster, int init_id)
Mingkai Hu0e58b512015-10-26 19:47:50 +0800852{
853 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
854 u32 idx = (cluster >> (init_id * 8)) & TP_CLUSTER_INIT_MASK;
855 u32 type = 0;
856
857 type = gur_in32(&gur->tp_ityp[idx]);
858 if (type & TP_ITYP_AV)
859 return type;
860
861 return 0;
862}
863
York Suned7fbe32016-09-13 12:40:30 -0700864u32 cpu_pos_mask(void)
865{
866 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
867 int i = 0;
868 u32 cluster, type, mask = 0;
869
870 do {
871 int j;
872
873 cluster = gur_in32(&gur->tp_cluster[i].lower);
874 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
875 type = initiator_type(cluster, j);
876 if (type && (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM))
877 mask |= 1 << (i * TP_INIT_PER_CLUSTER + j);
878 }
879 i++;
880 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
881
882 return mask;
883}
884
Mingkai Hu0e58b512015-10-26 19:47:50 +0800885u32 cpu_mask(void)
886{
887 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
888 int i = 0, count = 0;
889 u32 cluster, type, mask = 0;
890
891 do {
892 int j;
893
894 cluster = gur_in32(&gur->tp_cluster[i].lower);
895 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
896 type = initiator_type(cluster, j);
897 if (type) {
898 if (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
899 mask |= 1 << count;
900 count++;
901 }
902 }
903 i++;
904 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
905
906 return mask;
907}
908
909/*
910 * Return the number of cores on this SOC.
911 */
912int cpu_numcores(void)
913{
914 return hweight32(cpu_mask());
915}
916
917int fsl_qoriq_core_to_cluster(unsigned int core)
918{
919 struct ccsr_gur __iomem *gur =
920 (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
921 int i = 0, count = 0;
922 u32 cluster;
923
924 do {
925 int j;
926
927 cluster = gur_in32(&gur->tp_cluster[i].lower);
928 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
929 if (initiator_type(cluster, j)) {
930 if (count == core)
931 return i;
932 count++;
933 }
934 }
935 i++;
936 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
937
938 return -1; /* cannot identify the cluster */
939}
940
941u32 fsl_qoriq_core_to_type(unsigned int core)
942{
943 struct ccsr_gur __iomem *gur =
944 (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
945 int i = 0, count = 0;
946 u32 cluster, type;
947
948 do {
949 int j;
950
951 cluster = gur_in32(&gur->tp_cluster[i].lower);
952 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
953 type = initiator_type(cluster, j);
954 if (type) {
955 if (count == core)
956 return type;
957 count++;
958 }
959 }
960 i++;
961 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
962
963 return -1; /* cannot identify the cluster */
964}
965
Priyanka Jain96b001f2016-11-17 12:29:51 +0530966#ifndef CONFIG_FSL_LSCH3
Sriram Dash9282d262016-06-13 09:58:32 +0530967uint get_svr(void)
968{
969 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
970
971 return gur_in32(&gur->svr);
972}
Priyanka Jain96b001f2016-11-17 12:29:51 +0530973#endif
Sriram Dash9282d262016-06-13 09:58:32 +0530974
Mingkai Hu0e58b512015-10-26 19:47:50 +0800975#ifdef CONFIG_DISPLAY_CPUINFO
976int print_cpuinfo(void)
977{
978 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
979 struct sys_info sysinfo;
980 char buf[32];
981 unsigned int i, core;
York Suncbe8e1c2016-04-04 11:41:26 -0700982 u32 type, rcw, svr = gur_in32(&gur->svr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800983
984 puts("SoC: ");
985
986 cpu_name(buf);
York Suncbe8e1c2016-04-04 11:41:26 -0700987 printf(" %s (0x%x)\n", buf, svr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800988 memset((u8 *)buf, 0x00, ARRAY_SIZE(buf));
989 get_sys_info(&sysinfo);
990 puts("Clock Configuration:");
991 for_each_cpu(i, core, cpu_numcores(), cpu_mask()) {
992 if (!(i % 3))
993 puts("\n ");
994 type = TP_ITYP_VER(fsl_qoriq_core_to_type(core));
995 printf("CPU%d(%s):%-4s MHz ", core,
996 type == TY_ITYP_VER_A7 ? "A7 " :
997 (type == TY_ITYP_VER_A53 ? "A53" :
Alison Wang79808392016-07-05 16:01:52 +0800998 (type == TY_ITYP_VER_A57 ? "A57" :
999 (type == TY_ITYP_VER_A72 ? "A72" : " "))),
Mingkai Hu0e58b512015-10-26 19:47:50 +08001000 strmhz(buf, sysinfo.freq_processor[core]));
1001 }
Hou Zhiqiang3f91cda2017-01-10 16:44:15 +08001002 /* Display platform clock as Bus frequency. */
Mingkai Hu0e58b512015-10-26 19:47:50 +08001003 printf("\n Bus: %-4s MHz ",
Hou Zhiqiang3f91cda2017-01-10 16:44:15 +08001004 strmhz(buf, sysinfo.freq_systembus / CONFIG_SYS_FSL_PCLK_DIV));
Mingkai Hu0e58b512015-10-26 19:47:50 +08001005 printf("DDR: %-4s MT/s", strmhz(buf, sysinfo.freq_ddrbus));
Shaohui Xie04643262015-10-26 19:47:54 +08001006#ifdef CONFIG_SYS_DPAA_FMAN
1007 printf(" FMAN: %-4s MHz", strmhz(buf, sysinfo.freq_fman[0]));
1008#endif
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301009#ifdef CONFIG_SYS_FSL_HAS_DP_DDR
York Suncbe8e1c2016-04-04 11:41:26 -07001010 if (soc_has_dp_ddr()) {
1011 printf(" DP-DDR: %-4s MT/s",
1012 strmhz(buf, sysinfo.freq_ddrbus2));
1013 }
Mingkai Hu0e58b512015-10-26 19:47:50 +08001014#endif
1015 puts("\n");
1016
1017 /*
1018 * Display the RCW, so that no one gets confused as to what RCW
1019 * we're actually using for this boot.
1020 */
1021 puts("Reset Configuration Word (RCW):");
1022 for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
1023 rcw = gur_in32(&gur->rcwsr[i]);
1024 if ((i % 4) == 0)
1025 printf("\n %08x:", i * 4);
1026 printf(" %08x", rcw);
1027 }
1028 puts("\n");
1029
1030 return 0;
1031}
1032#endif
1033
1034#ifdef CONFIG_FSL_ESDHC
1035int cpu_mmc_init(bd_t *bis)
1036{
1037 return fsl_esdhc_mmc_init(bis);
1038}
1039#endif
1040
1041int cpu_eth_init(bd_t *bis)
1042{
1043 int error = 0;
1044
Santan Kumar1afa9002017-05-05 15:42:29 +05301045#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001046 error = fsl_mc_ldpaa_init(bis);
1047#endif
Shaohui Xie04643262015-10-26 19:47:54 +08001048#ifdef CONFIG_FMAN_ENET
1049 fm_standard_init(bis);
1050#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001051 return error;
1052}
1053
Yuantian Tangaec3b142017-04-19 13:27:39 +08001054static inline int check_psci(void)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001055{
Yuantian Tangaec3b142017-04-19 13:27:39 +08001056 unsigned int psci_ver;
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301057
Yuantian Tangaec3b142017-04-19 13:27:39 +08001058 psci_ver = sec_firmware_support_psci_version();
1059 if (psci_ver == PSCI_INVALID_VER)
1060 return 1;
1061
1062 return 0;
1063}
1064
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301065static void config_core_prefetch(void)
1066{
1067 char *buf = NULL;
1068 char buffer[HWCONFIG_BUFFER_SIZE];
1069 const char *prefetch_arg = NULL;
1070 size_t arglen;
1071 unsigned int mask;
1072 struct pt_regs regs;
1073
1074 if (env_get_f("hwconfig", buffer, sizeof(buffer)) > 0)
1075 buf = buffer;
Pankaj Bansal6c772772019-10-31 05:41:09 +00001076 else
1077 return;
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301078
1079 prefetch_arg = hwconfig_subarg_f("core_prefetch", "disable",
1080 &arglen, buf);
1081
1082 if (prefetch_arg) {
1083 mask = simple_strtoul(prefetch_arg, NULL, 0) & 0xff;
1084 if (mask & 0x1) {
1085 printf("Core0 prefetch can't be disabled\n");
1086 return;
1087 }
1088
1089#define SIP_PREFETCH_DISABLE_64 0xC200FF13
1090 regs.regs[0] = SIP_PREFETCH_DISABLE_64;
1091 regs.regs[1] = mask;
1092 smc_call(&regs);
1093
1094 if (regs.regs[0])
1095 printf("Prefetch disable config failed for mask ");
1096 else
1097 printf("Prefetch disable config passed for mask ");
1098 printf("0x%x\n", mask);
1099 }
1100}
1101
Yuantian Tangaec3b142017-04-19 13:27:39 +08001102int arch_early_init_r(void)
1103{
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301104#ifdef CONFIG_SYS_FSL_ERRATUM_A009635
Priyanka Jain823e0422017-02-14 10:34:31 +05301105 u32 svr_dev_id;
1106 /*
1107 * erratum A009635 is valid only for LS2080A SoC and
1108 * its personalitiesi
1109 */
Wenbin song5d8a61c2017-12-04 12:18:28 +08001110 svr_dev_id = get_svr();
1111 if (IS_SVR_DEV(svr_dev_id, SVR_DEV(SVR_LS2080A)))
Priyanka Jain823e0422017-02-14 10:34:31 +05301112 erratum_a009635();
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301113#endif
Shengzhou Liu15875a52016-11-21 11:36:48 +08001114#if defined(CONFIG_SYS_FSL_ERRATUM_A009942) && defined(CONFIG_SYS_FSL_DDR)
1115 erratum_a009942_check_cpo();
1116#endif
Yuantian Tangaec3b142017-04-19 13:27:39 +08001117 if (check_psci()) {
1118 debug("PSCI: PSCI does not exist.\n");
1119
1120 /* if PSCI does not exist, boot secondary cores here */
1121 if (fsl_layerscape_wake_seconday_cores())
Hou Zhiqiang21c4d552016-06-28 20:18:15 +08001122 printf("Did not wake secondary cores\n");
1123 }
Mingkai Hu0e58b512015-10-26 19:47:50 +08001124
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301125 config_core_prefetch();
1126
Mingkai Hu0e58b512015-10-26 19:47:50 +08001127#ifdef CONFIG_SYS_HAS_SERDES
1128 fsl_serdes_init();
1129#endif
Pankaj Bansalcc840622018-10-29 11:28:26 +00001130#ifdef CONFIG_SYS_FSL_HAS_RGMII
1131 /* some dpmacs in armv8a based freescale layerscape SOCs can be
1132 * configured via both serdes(sgmii, xfi, xlaui etc) bits and via
1133 * EC*_PMUX(rgmii) bits in RCW.
1134 * e.g. dpmac 17 and 18 in LX2160A can be configured as SGMII from
1135 * serdes bits and as RGMII via EC1_PMUX/EC2_PMUX bits
1136 * Now if a dpmac is enabled by serdes bits then it takes precedence
1137 * over EC*_PMUX bits. i.e. in LX2160A if we select serdes protocol
1138 * that configures dpmac17 as SGMII and set the EC1_PMUX as RGMII,
1139 * then the dpmac is SGMII and not RGMII.
1140 *
1141 * Therefore, move the fsl_rgmii_init after fsl_serdes_init. in
1142 * fsl_rgmii_init function of SOC, we will check if the dpmac is enabled
1143 * or not? if it is (fsl_serdes_init has already enabled the dpmac),
1144 * then don't enable it.
1145 */
1146 fsl_rgmii_init();
1147#endif
Shaohui Xie04643262015-10-26 19:47:54 +08001148#ifdef CONFIG_FMAN_ENET
1149 fman_enet_init();
1150#endif
Ahmed Mansouraa270b42017-12-15 16:01:00 -05001151#ifdef CONFIG_SYS_DPAA_QBMAN
1152 setup_qbman_portals();
1153#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001154 return 0;
1155}
1156
1157int timer_init(void)
1158{
1159 u32 __iomem *cntcr = (u32 *)CONFIG_SYS_FSL_TIMER_ADDR;
1160#ifdef CONFIG_FSL_LSCH3
1161 u32 __iomem *cltbenr = (u32 *)CONFIG_SYS_FSL_PMU_CLTBENR;
1162#endif
Thomas Schaefercbec2b82019-08-08 16:00:30 +08001163#if defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
1164 defined(CONFIG_ARCH_LS1028A)
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001165 u32 __iomem *pctbenr = (u32 *)FSL_PMU_PCTBENR_OFFSET;
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301166 u32 svr_dev_id;
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001167#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001168#ifdef COUNTER_FREQUENCY_REAL
1169 unsigned long cntfrq = COUNTER_FREQUENCY_REAL;
1170
1171 /* Update with accurate clock frequency */
York Sune6b871e2017-05-15 08:51:59 -07001172 if (current_el() == 3)
1173 asm volatile("msr cntfrq_el0, %0" : : "r" (cntfrq) : "memory");
Mingkai Hu0e58b512015-10-26 19:47:50 +08001174#endif
1175
1176#ifdef CONFIG_FSL_LSCH3
1177 /* Enable timebase for all clusters.
1178 * It is safe to do so even some clusters are not enabled.
1179 */
1180 out_le32(cltbenr, 0xf);
1181#endif
1182
Thomas Schaefercbec2b82019-08-08 16:00:30 +08001183#if defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
1184 defined(CONFIG_ARCH_LS1028A)
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001185 /*
1186 * In certain Layerscape SoCs, the clock for each core's
1187 * has an enable bit in the PMU Physical Core Time Base Enable
1188 * Register (PCTBENR), which allows the watchdog to operate.
1189 */
1190 setbits_le32(pctbenr, 0xff);
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301191 /*
1192 * For LS2080A SoC and its personalities, timer controller
1193 * offset is different
1194 */
Wenbin song5d8a61c2017-12-04 12:18:28 +08001195 svr_dev_id = get_svr();
1196 if (IS_SVR_DEV(svr_dev_id, SVR_DEV(SVR_LS2080A)))
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301197 cntcr = (u32 *)SYS_FSL_LS2080A_LS2085A_TIMER_ADDR;
1198
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001199#endif
1200
Mingkai Hu0e58b512015-10-26 19:47:50 +08001201 /* Enable clock for timer
1202 * This is a global setting.
1203 */
1204 out_le32(cntcr, 0x1);
1205
1206 return 0;
1207}
1208
Alexander Graf12be31c2016-11-17 01:03:01 +01001209__efi_runtime_data u32 __iomem *rstcr = (u32 *)CONFIG_SYS_FSL_RST_ADDR;
1210
1211void __efi_runtime reset_cpu(ulong addr)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001212{
Mingkai Hu0e58b512015-10-26 19:47:50 +08001213 u32 val;
1214
Priyanka Jainef76b2e2018-10-29 09:17:09 +00001215#ifdef CONFIG_ARCH_LX2160A
1216 val = in_le32(rstcr);
1217 val |= 0x01;
1218 out_le32(rstcr, val);
1219#else
Mingkai Hu0e58b512015-10-26 19:47:50 +08001220 /* Raise RESET_REQ_B */
1221 val = scfg_in32(rstcr);
1222 val |= 0x02;
1223 scfg_out32(rstcr, val);
Priyanka Jainef76b2e2018-10-29 09:17:09 +00001224#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001225}
York Sun928b6812015-12-07 11:08:58 -08001226
Mathew McBride8f137732019-10-18 14:27:54 +11001227#if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_PSCI_RESET)
Alexander Graf12be31c2016-11-17 01:03:01 +01001228
1229void __efi_runtime EFIAPI efi_reset_system(
1230 enum efi_reset_type reset_type,
1231 efi_status_t reset_status,
1232 unsigned long data_size, void *reset_data)
1233{
1234 switch (reset_type) {
1235 case EFI_RESET_COLD:
1236 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +01001237 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf12be31c2016-11-17 01:03:01 +01001238 reset_cpu(0);
1239 break;
1240 case EFI_RESET_SHUTDOWN:
1241 /* Nothing we can do */
1242 break;
1243 }
1244
1245 while (1) { }
1246}
1247
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +01001248efi_status_t efi_reset_system_init(void)
Alexander Graf12be31c2016-11-17 01:03:01 +01001249{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +01001250 return efi_add_runtime_mmio(&rstcr, sizeof(*rstcr));
Alexander Graf12be31c2016-11-17 01:03:01 +01001251}
1252
1253#endif
1254
York Sun2db54082017-09-07 10:12:32 -07001255/*
1256 * Calculate reserved memory with given memory bank
1257 * Return aligned memory size on success
1258 * Return (ram_size + needed size) for failure
1259 */
York Sun928b6812015-12-07 11:08:58 -08001260phys_size_t board_reserve_ram_top(phys_size_t ram_size)
1261{
1262 phys_size_t ram_top = ram_size;
1263
Santan Kumar1afa9002017-05-05 15:42:29 +05301264#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
York Sun2db54082017-09-07 10:12:32 -07001265 ram_top = mc_get_dram_block_size();
1266 if (ram_top > ram_size)
1267 return ram_size + ram_top;
1268
1269 ram_top = ram_size - ram_top;
York Sun4de24ef2017-03-06 09:02:28 -08001270 /* The start address of MC reserved memory needs to be aligned. */
York Sun928b6812015-12-07 11:08:58 -08001271 ram_top &= ~(CONFIG_SYS_MC_RSV_MEM_ALIGN - 1);
1272#endif
York Sun4de24ef2017-03-06 09:02:28 -08001273
1274 return ram_size - ram_top;
1275}
1276
1277phys_size_t get_effective_memsize(void)
1278{
1279 phys_size_t ea_size, rem = 0;
1280
1281 /*
1282 * For ARMv8 SoCs, DDR memory is split into two or three regions. The
Sumit Garged0deea2017-10-04 03:20:49 +05301283 * first region is 2GB space at 0x8000_0000. Secure memory needs to
1284 * allocated from first region. If the memory extends to the second
1285 * region (or the third region if applicable), Management Complex (MC)
1286 * memory should be put into the highest region, i.e. the end of DDR
1287 * memory. CONFIG_MAX_MEM_MAPPED is set to the size of first region so
1288 * U-Boot doesn't relocate itself into higher address. Should DDR be
1289 * configured to skip the first region, this function needs to be
1290 * adjusted.
York Sun4de24ef2017-03-06 09:02:28 -08001291 */
1292 if (gd->ram_size > CONFIG_MAX_MEM_MAPPED) {
1293 ea_size = CONFIG_MAX_MEM_MAPPED;
1294 rem = gd->ram_size - ea_size;
1295 } else {
1296 ea_size = gd->ram_size;
1297 }
1298
1299#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1300 /* Check if we have enough space for secure memory */
Sumit Garged0deea2017-10-04 03:20:49 +05301301 if (ea_size > CONFIG_SYS_MEM_RESERVE_SECURE)
1302 ea_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
1303 else
1304 printf("Error: No enough space for secure memory.\n");
York Sun4de24ef2017-03-06 09:02:28 -08001305#endif
1306 /* Check if we have enough memory for MC */
1307 if (rem < board_reserve_ram_top(rem)) {
1308 /* Not enough memory in high region to reserve */
York Sun2db54082017-09-07 10:12:32 -07001309 if (ea_size > board_reserve_ram_top(ea_size))
1310 ea_size -= board_reserve_ram_top(ea_size);
York Sun4de24ef2017-03-06 09:02:28 -08001311 else
1312 printf("Error: No enough space for reserved memory.\n");
1313 }
1314
1315 return ea_size;
1316}
1317
Rajesh Bhagat1dde2d22018-11-05 18:01:58 +00001318#ifdef CONFIG_TFABOOT
1319phys_size_t tfa_get_dram_size(void)
1320{
1321 struct pt_regs regs;
1322 phys_size_t dram_size = 0;
1323
1324 regs.regs[0] = SMC_DRAM_BANK_INFO;
1325 regs.regs[1] = -1;
1326
1327 smc_call(&regs);
1328 if (regs.regs[0])
1329 return 0;
1330
1331 dram_size = regs.regs[1];
1332 return dram_size;
1333}
1334
1335static int tfa_dram_init_banksize(void)
1336{
1337 int i = 0, ret = 0;
1338 struct pt_regs regs;
1339 phys_size_t dram_size = tfa_get_dram_size();
1340
1341 debug("dram_size %llx\n", dram_size);
1342
1343 if (!dram_size)
1344 return -EINVAL;
1345
1346 do {
1347 regs.regs[0] = SMC_DRAM_BANK_INFO;
1348 regs.regs[1] = i;
1349
1350 smc_call(&regs);
1351 if (regs.regs[0]) {
1352 ret = -EINVAL;
1353 break;
1354 }
1355
1356 debug("bank[%d]: start %lx, size %lx\n", i, regs.regs[1],
1357 regs.regs[2]);
1358 gd->bd->bi_dram[i].start = regs.regs[1];
1359 gd->bd->bi_dram[i].size = regs.regs[2];
1360
1361 dram_size -= gd->bd->bi_dram[i].size;
1362
1363 i++;
1364 } while (dram_size);
1365
1366 if (i > 0)
1367 ret = 0;
1368
1369#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
1370 /* Assign memory for MC */
1371#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1372 if (gd->bd->bi_dram[2].size >=
1373 board_reserve_ram_top(gd->bd->bi_dram[2].size)) {
1374 gd->arch.resv_ram = gd->bd->bi_dram[2].start +
1375 gd->bd->bi_dram[2].size -
1376 board_reserve_ram_top(gd->bd->bi_dram[2].size);
1377 } else
1378#endif
1379 {
1380 if (gd->bd->bi_dram[1].size >=
1381 board_reserve_ram_top(gd->bd->bi_dram[1].size)) {
1382 gd->arch.resv_ram = gd->bd->bi_dram[1].start +
1383 gd->bd->bi_dram[1].size -
1384 board_reserve_ram_top(gd->bd->bi_dram[1].size);
1385 } else if (gd->bd->bi_dram[0].size >
1386 board_reserve_ram_top(gd->bd->bi_dram[0].size)) {
1387 gd->arch.resv_ram = gd->bd->bi_dram[0].start +
1388 gd->bd->bi_dram[0].size -
1389 board_reserve_ram_top(gd->bd->bi_dram[0].size);
1390 }
1391 }
1392#endif /* CONFIG_FSL_MC_ENET */
1393
1394 return ret;
1395}
1396#endif
1397
Simon Glass2f949c32017-03-31 08:40:32 -06001398int dram_init_banksize(void)
York Sun4de24ef2017-03-06 09:02:28 -08001399{
1400#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1401 phys_size_t dp_ddr_size;
1402#endif
1403
Rajesh Bhagat1dde2d22018-11-05 18:01:58 +00001404#ifdef CONFIG_TFABOOT
1405 if (!tfa_dram_init_banksize())
1406 return 0;
1407#endif
York Sun4de24ef2017-03-06 09:02:28 -08001408 /*
1409 * gd->ram_size has the total size of DDR memory, less reserved secure
1410 * memory. The DDR extends from low region to high region(s) presuming
1411 * no hole is created with DDR configuration. gd->arch.secure_ram tracks
1412 * the location of secure memory. gd->arch.resv_ram tracks the location
York Sunc9e3e042017-09-28 08:42:12 -07001413 * of reserved memory for Management Complex (MC). Because gd->ram_size
1414 * is reduced by this function if secure memory is reserved, checking
1415 * gd->arch.secure_ram should be done to avoid running it repeatedly.
York Sun4de24ef2017-03-06 09:02:28 -08001416 */
York Sunc9e3e042017-09-28 08:42:12 -07001417
1418#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1419 if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
1420 debug("No need to run again, skip %s\n", __func__);
1421
1422 return 0;
1423 }
1424#endif
1425
York Sun4de24ef2017-03-06 09:02:28 -08001426 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
1427 if (gd->ram_size > CONFIG_SYS_DDR_BLOCK1_SIZE) {
1428 gd->bd->bi_dram[0].size = CONFIG_SYS_DDR_BLOCK1_SIZE;
1429 gd->bd->bi_dram[1].start = CONFIG_SYS_DDR_BLOCK2_BASE;
1430 gd->bd->bi_dram[1].size = gd->ram_size -
1431 CONFIG_SYS_DDR_BLOCK1_SIZE;
1432#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1433 if (gd->bi_dram[1].size > CONFIG_SYS_DDR_BLOCK2_SIZE) {
1434 gd->bd->bi_dram[2].start = CONFIG_SYS_DDR_BLOCK3_BASE;
1435 gd->bd->bi_dram[2].size = gd->bd->bi_dram[1].size -
1436 CONFIG_SYS_DDR_BLOCK2_SIZE;
1437 gd->bd->bi_dram[1].size = CONFIG_SYS_DDR_BLOCK2_SIZE;
1438 }
1439#endif
1440 } else {
1441 gd->bd->bi_dram[0].size = gd->ram_size;
1442 }
1443#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
Sumit Garged0deea2017-10-04 03:20:49 +05301444 if (gd->bd->bi_dram[0].size >
1445 CONFIG_SYS_MEM_RESERVE_SECURE) {
1446 gd->bd->bi_dram[0].size -=
1447 CONFIG_SYS_MEM_RESERVE_SECURE;
1448 gd->arch.secure_ram = gd->bd->bi_dram[0].start +
1449 gd->bd->bi_dram[0].size;
York Sun4de24ef2017-03-06 09:02:28 -08001450 gd->arch.secure_ram |= MEM_RESERVE_SECURE_MAINTAINED;
1451 gd->ram_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
York Sun4de24ef2017-03-06 09:02:28 -08001452 }
1453#endif /* CONFIG_SYS_MEM_RESERVE_SECURE */
1454
Santan Kumar1afa9002017-05-05 15:42:29 +05301455#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
York Sun4de24ef2017-03-06 09:02:28 -08001456 /* Assign memory for MC */
1457#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1458 if (gd->bd->bi_dram[2].size >=
1459 board_reserve_ram_top(gd->bd->bi_dram[2].size)) {
1460 gd->arch.resv_ram = gd->bd->bi_dram[2].start +
1461 gd->bd->bi_dram[2].size -
1462 board_reserve_ram_top(gd->bd->bi_dram[2].size);
1463 } else
1464#endif
1465 {
1466 if (gd->bd->bi_dram[1].size >=
1467 board_reserve_ram_top(gd->bd->bi_dram[1].size)) {
1468 gd->arch.resv_ram = gd->bd->bi_dram[1].start +
1469 gd->bd->bi_dram[1].size -
1470 board_reserve_ram_top(gd->bd->bi_dram[1].size);
1471 } else if (gd->bd->bi_dram[0].size >
1472 board_reserve_ram_top(gd->bd->bi_dram[0].size)) {
1473 gd->arch.resv_ram = gd->bd->bi_dram[0].start +
1474 gd->bd->bi_dram[0].size -
1475 board_reserve_ram_top(gd->bd->bi_dram[0].size);
1476 }
1477 }
1478#endif /* CONFIG_FSL_MC_ENET */
1479
1480#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1481#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1482#error "This SoC shouldn't have DP DDR"
1483#endif
1484 if (soc_has_dp_ddr()) {
1485 /* initialize DP-DDR here */
1486 puts("DP-DDR: ");
1487 /*
1488 * DDR controller use 0 as the base address for binding.
1489 * It is mapped to CONFIG_SYS_DP_DDR_BASE for core to access.
1490 */
1491 dp_ddr_size = fsl_other_ddr_sdram(CONFIG_SYS_DP_DDR_BASE_PHY,
1492 CONFIG_DP_DDR_CTRL,
1493 CONFIG_DP_DDR_NUM_CTRLS,
1494 CONFIG_DP_DDR_DIMM_SLOTS_PER_CTLR,
1495 NULL, NULL, NULL);
1496 if (dp_ddr_size) {
1497 gd->bd->bi_dram[2].start = CONFIG_SYS_DP_DDR_BASE;
1498 gd->bd->bi_dram[2].size = dp_ddr_size;
1499 } else {
1500 puts("Not detected");
1501 }
1502 }
1503#endif
Simon Glass2f949c32017-03-31 08:40:32 -06001504
York Sunc9e3e042017-09-28 08:42:12 -07001505#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1506 debug("%s is called. gd->ram_size is reduced to %lu\n",
1507 __func__, (ulong)gd->ram_size);
1508#endif
1509
Simon Glass2f949c32017-03-31 08:40:32 -06001510 return 0;
York Sun4de24ef2017-03-06 09:02:28 -08001511}
1512
Stephen Warrend0de8062018-08-30 15:43:43 -06001513#if CONFIG_IS_ENABLED(EFI_LOADER)
York Sun4de24ef2017-03-06 09:02:28 -08001514void efi_add_known_memory(void)
1515{
1516 int i;
1517 phys_addr_t ram_start, start;
1518 phys_size_t ram_size;
1519 u64 pages;
York Sun928b6812015-12-07 11:08:58 -08001520
York Sun4de24ef2017-03-06 09:02:28 -08001521 /* Add RAM */
1522 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
1523#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1524#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1525#error "This SoC shouldn't have DP DDR"
1526#endif
1527 if (i == 2)
1528 continue; /* skip DP-DDR */
1529#endif
1530 ram_start = gd->bd->bi_dram[i].start;
1531 ram_size = gd->bd->bi_dram[i].size;
1532#ifdef CONFIG_RESV_RAM
1533 if (gd->arch.resv_ram >= ram_start &&
1534 gd->arch.resv_ram < ram_start + ram_size)
1535 ram_size = gd->arch.resv_ram - ram_start;
1536#endif
1537 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
1538 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
1539
1540 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
1541 false);
1542 }
York Sun928b6812015-12-07 11:08:58 -08001543}
York Sun4de24ef2017-03-06 09:02:28 -08001544#endif
York Sun729f2d12017-03-06 09:02:34 -08001545
1546/*
1547 * Before DDR size is known, early MMU table have DDR mapped as device memory
1548 * to avoid speculative access. To relocate U-Boot to DDR, "normal memory"
1549 * needs to be set for these mappings.
1550 * If a special case configures DDR with holes in the mapping, the holes need
1551 * to be marked as invalid. This is not implemented in this function.
1552 */
1553void update_early_mmu_table(void)
1554{
1555 if (!gd->arch.tlb_addr)
1556 return;
1557
1558 if (gd->ram_size <= CONFIG_SYS_FSL_DRAM_SIZE1) {
1559 mmu_change_region_attr(
1560 CONFIG_SYS_SDRAM_BASE,
1561 gd->ram_size,
1562 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1563 PTE_BLOCK_OUTER_SHARE |
1564 PTE_BLOCK_NS |
1565 PTE_TYPE_VALID);
1566 } else {
1567 mmu_change_region_attr(
1568 CONFIG_SYS_SDRAM_BASE,
1569 CONFIG_SYS_DDR_BLOCK1_SIZE,
1570 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1571 PTE_BLOCK_OUTER_SHARE |
1572 PTE_BLOCK_NS |
1573 PTE_TYPE_VALID);
1574#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1575#ifndef CONFIG_SYS_DDR_BLOCK2_SIZE
1576#error "Missing CONFIG_SYS_DDR_BLOCK2_SIZE"
1577#endif
1578 if (gd->ram_size - CONFIG_SYS_DDR_BLOCK1_SIZE >
1579 CONFIG_SYS_DDR_BLOCK2_SIZE) {
1580 mmu_change_region_attr(
1581 CONFIG_SYS_DDR_BLOCK2_BASE,
1582 CONFIG_SYS_DDR_BLOCK2_SIZE,
1583 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1584 PTE_BLOCK_OUTER_SHARE |
1585 PTE_BLOCK_NS |
1586 PTE_TYPE_VALID);
1587 mmu_change_region_attr(
1588 CONFIG_SYS_DDR_BLOCK3_BASE,
1589 gd->ram_size -
1590 CONFIG_SYS_DDR_BLOCK1_SIZE -
1591 CONFIG_SYS_DDR_BLOCK2_SIZE,
1592 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1593 PTE_BLOCK_OUTER_SHARE |
1594 PTE_BLOCK_NS |
1595 PTE_TYPE_VALID);
1596 } else
1597#endif
1598 {
1599 mmu_change_region_attr(
1600 CONFIG_SYS_DDR_BLOCK2_BASE,
1601 gd->ram_size -
1602 CONFIG_SYS_DDR_BLOCK1_SIZE,
1603 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1604 PTE_BLOCK_OUTER_SHARE |
1605 PTE_BLOCK_NS |
1606 PTE_TYPE_VALID);
1607 }
1608 }
1609}
1610
1611__weak int dram_init(void)
1612{
Simon Glass0e0ac202017-04-06 12:47:04 -06001613 fsl_initdram();
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +00001614#if (!defined(CONFIG_SPL) && !defined(CONFIG_TFABOOT)) || \
1615 defined(CONFIG_SPL_BUILD)
York Sun729f2d12017-03-06 09:02:34 -08001616 /* This will break-before-make MMU for DDR */
1617 update_early_mmu_table();
1618#endif
1619
1620 return 0;
1621}