blob: b44389445369c852a6be3b2bcd63c29956221fad [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 Glass8e16b1e2019-12-28 10:45:05 -070011#include <init.h>
Simon Glassf11478f2019-12-28 10:45:07 -070012#include <hang.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070013#include <vsprintf.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080014#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090015#include <linux/errno.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080016#include <asm/system.h>
Joe Hershberger8f454d92018-07-16 15:33:51 -050017#include <fm_eth.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080018#include <asm/armv8/mmu.h>
19#include <asm/io.h>
20#include <asm/arch/fsl_serdes.h>
21#include <asm/arch/soc.h>
22#include <asm/arch/cpu.h>
23#include <asm/arch/speed.h>
Ashish Kumar11234062017-08-11 11:09:14 +053024#include <fsl_immap.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080025#include <asm/arch/mp.h>
Alexander Graf12be31c2016-11-17 01:03:01 +010026#include <efi_loader.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080027#include <fsl-mc/fsl_mc.h>
28#ifdef CONFIG_FSL_ESDHC
29#include <fsl_esdhc.h>
30#endif
Hou Zhiqiang21c4d552016-06-28 20:18:15 +080031#include <asm/armv8/sec_firmware.h>
Shengzhou Liu15875a52016-11-21 11:36:48 +080032#ifdef CONFIG_SYS_FSL_DDR
33#include <fsl_ddr.h>
34#endif
Simon Glass243182c2017-05-17 08:23:06 -060035#include <asm/arch/clock.h>
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +053036#include <hwconfig.h>
Ahmed Mansouraa270b42017-12-15 16:01:00 -050037#include <fsl_qbman.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080038
Rajesh Bhagat583da8b2018-11-05 18:01:42 +000039#ifdef CONFIG_TFABOOT
Simon Glass9d1f6192019-08-02 09:44:25 -060040#include <env_internal.h>
Pankit Gargbdbf84f2018-11-05 18:01:52 +000041#ifdef CONFIG_CHAIN_OF_TRUST
42#include <fsl_validate.h>
43#endif
Rajesh Bhagat583da8b2018-11-05 18:01:42 +000044#endif
Simon Glasscaefa322019-11-14 12:57:31 -070045#include <linux/mii.h>
Rajesh Bhagat583da8b2018-11-05 18:01:42 +000046
Mingkai Hu0e58b512015-10-26 19:47:50 +080047DECLARE_GLOBAL_DATA_PTR;
48
York Sunef4cef92018-11-05 18:01:06 +000049static struct cpu_type cpu_type_list[] = {
50 CPU_TYPE_ENTRY(LS2080A, LS2080A, 8),
51 CPU_TYPE_ENTRY(LS2085A, LS2085A, 8),
52 CPU_TYPE_ENTRY(LS2045A, LS2045A, 4),
53 CPU_TYPE_ENTRY(LS2088A, LS2088A, 8),
54 CPU_TYPE_ENTRY(LS2084A, LS2084A, 8),
55 CPU_TYPE_ENTRY(LS2048A, LS2048A, 4),
56 CPU_TYPE_ENTRY(LS2044A, LS2044A, 4),
57 CPU_TYPE_ENTRY(LS2081A, LS2081A, 8),
58 CPU_TYPE_ENTRY(LS2041A, LS2041A, 4),
59 CPU_TYPE_ENTRY(LS1043A, LS1043A, 4),
Hou Zhiqiangb9aedf92018-12-20 06:31:17 +000060 CPU_TYPE_ENTRY(LS1043A, LS1043A_P23, 4),
York Sunef4cef92018-11-05 18:01:06 +000061 CPU_TYPE_ENTRY(LS1023A, LS1023A, 2),
Hou Zhiqiangb9aedf92018-12-20 06:31:17 +000062 CPU_TYPE_ENTRY(LS1023A, LS1023A_P23, 2),
York Sunef4cef92018-11-05 18:01:06 +000063 CPU_TYPE_ENTRY(LS1046A, LS1046A, 4),
64 CPU_TYPE_ENTRY(LS1026A, LS1026A, 2),
65 CPU_TYPE_ENTRY(LS2040A, LS2040A, 4),
66 CPU_TYPE_ENTRY(LS1012A, LS1012A, 1),
Yuantian Tangf463d752019-09-18 16:50:52 +080067 CPU_TYPE_ENTRY(LS1017A, LS1017A, 1),
68 CPU_TYPE_ENTRY(LS1018A, LS1018A, 1),
69 CPU_TYPE_ENTRY(LS1027A, LS1027A, 2),
Yuantian Tang4aefa162019-04-10 16:43:33 +080070 CPU_TYPE_ENTRY(LS1028A, LS1028A, 2),
York Sunef4cef92018-11-05 18:01:06 +000071 CPU_TYPE_ENTRY(LS1088A, LS1088A, 8),
72 CPU_TYPE_ENTRY(LS1084A, LS1084A, 8),
73 CPU_TYPE_ENTRY(LS1048A, LS1048A, 4),
74 CPU_TYPE_ENTRY(LS1044A, LS1044A, 4),
Priyanka Jainef76b2e2018-10-29 09:17:09 +000075 CPU_TYPE_ENTRY(LX2160A, LX2160A, 16),
76 CPU_TYPE_ENTRY(LX2120A, LX2120A, 12),
77 CPU_TYPE_ENTRY(LX2080A, LX2080A, 8),
York Sunef4cef92018-11-05 18:01:06 +000078};
79
80#define EARLY_PGTABLE_SIZE 0x5000
81static struct mm_region early_map[] = {
82#ifdef CONFIG_FSL_LSCH3
83 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
84 CONFIG_SYS_FSL_CCSR_SIZE,
85 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
86 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
87 },
88 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
89 SYS_FSL_OCRAM_SPACE_SIZE,
90 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
91 },
92 { CONFIG_SYS_FSL_QSPI_BASE1, CONFIG_SYS_FSL_QSPI_BASE1,
93 CONFIG_SYS_FSL_QSPI_SIZE1,
94 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE},
95#ifdef CONFIG_FSL_IFC
96 /* For IFC Region #1, only the first 4MB is cache-enabled */
97 { CONFIG_SYS_FSL_IFC_BASE1, CONFIG_SYS_FSL_IFC_BASE1,
98 CONFIG_SYS_FSL_IFC_SIZE1_1,
99 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
100 },
101 { CONFIG_SYS_FSL_IFC_BASE1 + CONFIG_SYS_FSL_IFC_SIZE1_1,
102 CONFIG_SYS_FSL_IFC_BASE1 + CONFIG_SYS_FSL_IFC_SIZE1_1,
103 CONFIG_SYS_FSL_IFC_SIZE1 - CONFIG_SYS_FSL_IFC_SIZE1_1,
104 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
105 },
106 { CONFIG_SYS_FLASH_BASE, CONFIG_SYS_FSL_IFC_BASE1,
107 CONFIG_SYS_FSL_IFC_SIZE1,
108 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
109 },
110#endif
111 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
112 CONFIG_SYS_FSL_DRAM_SIZE1,
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +0000113#if defined(CONFIG_TFABOOT) || \
114 (defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD))
York Sunef4cef92018-11-05 18:01:06 +0000115 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
116#else /* Start with nGnRnE and PXN and UXN to prevent speculative access */
117 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
118#endif
119 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
120 },
121#ifdef CONFIG_FSL_IFC
122 /* Map IFC region #2 up to CONFIG_SYS_FLASH_BASE for NAND boot */
123 { CONFIG_SYS_FSL_IFC_BASE2, CONFIG_SYS_FSL_IFC_BASE2,
124 CONFIG_SYS_FLASH_BASE - CONFIG_SYS_FSL_IFC_BASE2,
125 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
126 },
127#endif
128 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
129 CONFIG_SYS_FSL_DCSR_SIZE,
130 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
131 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
132 },
133 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
134 CONFIG_SYS_FSL_DRAM_SIZE2,
135 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
136 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
137 },
Priyanka Jain88c25662018-10-29 09:11:29 +0000138#ifdef CONFIG_SYS_FSL_DRAM_BASE3
139 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
140 CONFIG_SYS_FSL_DRAM_SIZE3,
141 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
142 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
143 },
144#endif
York Sunef4cef92018-11-05 18:01:06 +0000145#elif defined(CONFIG_FSL_LSCH2)
146 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
147 CONFIG_SYS_FSL_CCSR_SIZE,
148 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
149 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
150 },
151 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
152 SYS_FSL_OCRAM_SPACE_SIZE,
153 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
154 },
155 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
156 CONFIG_SYS_FSL_DCSR_SIZE,
157 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
158 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
159 },
160 { CONFIG_SYS_FSL_QSPI_BASE, CONFIG_SYS_FSL_QSPI_BASE,
161 CONFIG_SYS_FSL_QSPI_SIZE,
162 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
163 },
164#ifdef CONFIG_FSL_IFC
165 { CONFIG_SYS_FSL_IFC_BASE, CONFIG_SYS_FSL_IFC_BASE,
166 CONFIG_SYS_FSL_IFC_SIZE,
167 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
168 },
169#endif
170 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
171 CONFIG_SYS_FSL_DRAM_SIZE1,
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +0000172#if defined(CONFIG_TFABOOT) || \
173 (defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD))
York Sunef4cef92018-11-05 18:01:06 +0000174 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
175#else /* Start with nGnRnE and PXN and UXN to prevent speculative access */
176 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
177#endif
178 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
179 },
180 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
181 CONFIG_SYS_FSL_DRAM_SIZE2,
182 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
183 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
184 },
185#endif
186 {}, /* list terminator */
187};
188
189static struct mm_region final_map[] = {
190#ifdef CONFIG_FSL_LSCH3
191 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
192 CONFIG_SYS_FSL_CCSR_SIZE,
193 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
194 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
195 },
196 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
197 SYS_FSL_OCRAM_SPACE_SIZE,
198 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
199 },
200 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
201 CONFIG_SYS_FSL_DRAM_SIZE1,
202 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
203 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
204 },
205 { CONFIG_SYS_FSL_QSPI_BASE1, CONFIG_SYS_FSL_QSPI_BASE1,
206 CONFIG_SYS_FSL_QSPI_SIZE1,
207 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
208 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
209 },
210 { CONFIG_SYS_FSL_QSPI_BASE2, CONFIG_SYS_FSL_QSPI_BASE2,
211 CONFIG_SYS_FSL_QSPI_SIZE2,
212 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
213 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
214 },
215#ifdef CONFIG_FSL_IFC
216 { CONFIG_SYS_FSL_IFC_BASE2, CONFIG_SYS_FSL_IFC_BASE2,
217 CONFIG_SYS_FSL_IFC_SIZE2,
218 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
219 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
220 },
221#endif
222 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
223 CONFIG_SYS_FSL_DCSR_SIZE,
224 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
225 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
226 },
227 { CONFIG_SYS_FSL_MC_BASE, CONFIG_SYS_FSL_MC_BASE,
228 CONFIG_SYS_FSL_MC_SIZE,
229 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
230 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
231 },
232 { CONFIG_SYS_FSL_NI_BASE, CONFIG_SYS_FSL_NI_BASE,
233 CONFIG_SYS_FSL_NI_SIZE,
234 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
235 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
236 },
237 /* For QBMAN portal, only the first 64MB is cache-enabled */
238 { CONFIG_SYS_FSL_QBMAN_BASE, CONFIG_SYS_FSL_QBMAN_BASE,
239 CONFIG_SYS_FSL_QBMAN_SIZE_1,
240 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
241 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN | PTE_BLOCK_NS
242 },
243 { CONFIG_SYS_FSL_QBMAN_BASE + CONFIG_SYS_FSL_QBMAN_SIZE_1,
244 CONFIG_SYS_FSL_QBMAN_BASE + CONFIG_SYS_FSL_QBMAN_SIZE_1,
245 CONFIG_SYS_FSL_QBMAN_SIZE - CONFIG_SYS_FSL_QBMAN_SIZE_1,
246 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
247 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
248 },
249 { CONFIG_SYS_PCIE1_PHYS_ADDR, CONFIG_SYS_PCIE1_PHYS_ADDR,
250 CONFIG_SYS_PCIE1_PHYS_SIZE,
251 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
252 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
253 },
254 { CONFIG_SYS_PCIE2_PHYS_ADDR, CONFIG_SYS_PCIE2_PHYS_ADDR,
255 CONFIG_SYS_PCIE2_PHYS_SIZE,
256 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
257 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
258 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800259#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000260 { CONFIG_SYS_PCIE3_PHYS_ADDR, CONFIG_SYS_PCIE3_PHYS_ADDR,
261 CONFIG_SYS_PCIE3_PHYS_SIZE,
262 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
263 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
264 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800265#endif
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000266#ifdef CONFIG_SYS_PCIE4_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000267 { CONFIG_SYS_PCIE4_PHYS_ADDR, CONFIG_SYS_PCIE4_PHYS_ADDR,
268 CONFIG_SYS_PCIE4_PHYS_SIZE,
269 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
270 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
271 },
272#endif
Hou Zhiqiangd08f9702019-04-08 10:15:41 +0000273#ifdef SYS_PCIE5_PHYS_ADDR
274 { SYS_PCIE5_PHYS_ADDR, SYS_PCIE5_PHYS_ADDR,
275 SYS_PCIE5_PHYS_SIZE,
276 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
277 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
278 },
279#endif
280#ifdef SYS_PCIE6_PHYS_ADDR
281 { SYS_PCIE6_PHYS_ADDR, SYS_PCIE6_PHYS_ADDR,
282 SYS_PCIE6_PHYS_SIZE,
283 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
284 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
285 },
286#endif
York Sunef4cef92018-11-05 18:01:06 +0000287 { CONFIG_SYS_FSL_WRIOP1_BASE, CONFIG_SYS_FSL_WRIOP1_BASE,
288 CONFIG_SYS_FSL_WRIOP1_SIZE,
289 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
290 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
291 },
292 { CONFIG_SYS_FSL_AIOP1_BASE, CONFIG_SYS_FSL_AIOP1_BASE,
293 CONFIG_SYS_FSL_AIOP1_SIZE,
294 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
295 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
296 },
297 { CONFIG_SYS_FSL_PEBUF_BASE, CONFIG_SYS_FSL_PEBUF_BASE,
298 CONFIG_SYS_FSL_PEBUF_SIZE,
299 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
300 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
301 },
302 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
303 CONFIG_SYS_FSL_DRAM_SIZE2,
304 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
305 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
306 },
Priyanka Jain88c25662018-10-29 09:11:29 +0000307#ifdef CONFIG_SYS_FSL_DRAM_BASE3
308 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
309 CONFIG_SYS_FSL_DRAM_SIZE3,
310 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
311 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
312 },
313#endif
York Sunef4cef92018-11-05 18:01:06 +0000314#elif defined(CONFIG_FSL_LSCH2)
315 { CONFIG_SYS_FSL_BOOTROM_BASE, CONFIG_SYS_FSL_BOOTROM_BASE,
316 CONFIG_SYS_FSL_BOOTROM_SIZE,
317 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
318 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
319 },
320 { CONFIG_SYS_FSL_CCSR_BASE, CONFIG_SYS_FSL_CCSR_BASE,
321 CONFIG_SYS_FSL_CCSR_SIZE,
322 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
323 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
324 },
325 { CONFIG_SYS_FSL_OCRAM_BASE, CONFIG_SYS_FSL_OCRAM_BASE,
326 SYS_FSL_OCRAM_SPACE_SIZE,
327 PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_NON_SHARE
328 },
329 { CONFIG_SYS_FSL_DCSR_BASE, CONFIG_SYS_FSL_DCSR_BASE,
330 CONFIG_SYS_FSL_DCSR_SIZE,
331 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
332 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
333 },
334 { CONFIG_SYS_FSL_QSPI_BASE, CONFIG_SYS_FSL_QSPI_BASE,
335 CONFIG_SYS_FSL_QSPI_SIZE,
336 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
337 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
338 },
339#ifdef CONFIG_FSL_IFC
340 { CONFIG_SYS_FSL_IFC_BASE, CONFIG_SYS_FSL_IFC_BASE,
341 CONFIG_SYS_FSL_IFC_SIZE,
342 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE
343 },
344#endif
345 { CONFIG_SYS_FSL_DRAM_BASE1, CONFIG_SYS_FSL_DRAM_BASE1,
346 CONFIG_SYS_FSL_DRAM_SIZE1,
347 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
348 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
349 },
350 { CONFIG_SYS_FSL_QBMAN_BASE, CONFIG_SYS_FSL_QBMAN_BASE,
351 CONFIG_SYS_FSL_QBMAN_SIZE,
352 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
353 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
354 },
355 { CONFIG_SYS_FSL_DRAM_BASE2, CONFIG_SYS_FSL_DRAM_BASE2,
356 CONFIG_SYS_FSL_DRAM_SIZE2,
357 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
358 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
359 },
360 { CONFIG_SYS_PCIE1_PHYS_ADDR, CONFIG_SYS_PCIE1_PHYS_ADDR,
361 CONFIG_SYS_PCIE1_PHYS_SIZE,
362 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
363 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
364 },
365 { CONFIG_SYS_PCIE2_PHYS_ADDR, CONFIG_SYS_PCIE2_PHYS_ADDR,
366 CONFIG_SYS_PCIE2_PHYS_SIZE,
367 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
368 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
369 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800370#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
York Sunef4cef92018-11-05 18:01:06 +0000371 { CONFIG_SYS_PCIE3_PHYS_ADDR, CONFIG_SYS_PCIE3_PHYS_ADDR,
372 CONFIG_SYS_PCIE3_PHYS_SIZE,
373 PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
374 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN
375 },
Yuantian Tang4aefa162019-04-10 16:43:33 +0800376#endif
York Sunef4cef92018-11-05 18:01:06 +0000377 { CONFIG_SYS_FSL_DRAM_BASE3, CONFIG_SYS_FSL_DRAM_BASE3,
378 CONFIG_SYS_FSL_DRAM_SIZE3,
379 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
380 PTE_BLOCK_OUTER_SHARE | PTE_BLOCK_NS
381 },
382#endif
383#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
384 {}, /* space holder for secure mem */
385#endif
386 {},
387};
388
York Sun9da8f502016-06-24 16:46:23 -0700389struct mm_region *mem_map = early_map;
Alexander Grafce0a64e2016-03-04 01:09:54 +0100390
Mingkai Hu0e58b512015-10-26 19:47:50 +0800391void cpu_name(char *name)
392{
393 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
394 unsigned int i, svr, ver;
395
396 svr = gur_in32(&gur->svr);
397 ver = SVR_SOC_VER(svr);
398
399 for (i = 0; i < ARRAY_SIZE(cpu_type_list); i++)
400 if ((cpu_type_list[i].soc_ver & SVR_WO_E) == ver) {
401 strcpy(name, cpu_type_list[i].name);
Priyanka Jainef76b2e2018-10-29 09:17:09 +0000402#ifdef CONFIG_ARCH_LX2160A
403 if (IS_C_PROCESSOR(svr))
404 strcat(name, "C");
405#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800406
407 if (IS_E_PROCESSOR(svr))
408 strcat(name, "E");
Wenbin Song863a33a2016-09-13 16:13:54 +0800409
410 sprintf(name + strlen(name), " Rev%d.%d",
411 SVR_MAJ(svr), SVR_MIN(svr));
Mingkai Hu0e58b512015-10-26 19:47:50 +0800412 break;
413 }
414
415 if (i == ARRAY_SIZE(cpu_type_list))
416 strcpy(name, "unknown");
417}
418
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400419#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Mingkai Hu0e58b512015-10-26 19:47:50 +0800420/*
421 * To start MMU before DDR is available, we create MMU table in SRAM.
422 * The base address of SRAM is CONFIG_SYS_FSL_OCRAM_BASE. We use three
423 * levels of translation tables here to cover 40-bit address space.
424 * We use 4KB granule size, with 40 bits physical address, T0SZ=24
York Sun9da8f502016-06-24 16:46:23 -0700425 * Address above EARLY_PGTABLE_SIZE (0x5000) is free for other purpose.
426 * Note, the debug print in cache_v8.c is not usable for debugging
427 * these early MMU tables because UART is not yet available.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800428 */
429static inline void early_mmu_setup(void)
430{
York Sun9da8f502016-06-24 16:46:23 -0700431 unsigned int el = current_el();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800432
York Sun9da8f502016-06-24 16:46:23 -0700433 /* global data is already setup, no allocation yet */
Pankit Gargc4d39eb2018-11-05 18:01:28 +0000434 if (el == 3)
435 gd->arch.tlb_addr = CONFIG_SYS_FSL_OCRAM_BASE;
436 else
437 gd->arch.tlb_addr = CONFIG_SYS_DDR_SDRAM_BASE;
York Sun9da8f502016-06-24 16:46:23 -0700438 gd->arch.tlb_fillptr = gd->arch.tlb_addr;
439 gd->arch.tlb_size = EARLY_PGTABLE_SIZE;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800440
York Sun9da8f502016-06-24 16:46:23 -0700441 /* Create early page tables */
442 setup_pgtables();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800443
York Sun9da8f502016-06-24 16:46:23 -0700444 /* point TTBR to the new table */
445 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
446 get_tcr(el, NULL, NULL) &
447 ~(TCR_ORGN_MASK | TCR_IRGN_MASK),
Mingkai Hu0e58b512015-10-26 19:47:50 +0800448 MEMORY_ATTRIBUTES);
York Sun9da8f502016-06-24 16:46:23 -0700449
Mingkai Hu0e58b512015-10-26 19:47:50 +0800450 set_sctlr(get_sctlr() | CR_M);
451}
452
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800453static void fix_pcie_mmu_map(void)
454{
York Sun4ce6fbf2017-03-27 11:41:01 -0700455#ifdef CONFIG_ARCH_LS2080A
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800456 unsigned int i;
457 u32 svr, ver;
458 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
459
460 svr = gur_in32(&gur->svr);
461 ver = SVR_SOC_VER(svr);
462
463 /* Fix PCIE base and size for LS2088A */
464 if ((ver == SVR_LS2088A) || (ver == SVR_LS2084A) ||
Priyanka Jain2b361782017-04-27 15:08:06 +0530465 (ver == SVR_LS2048A) || (ver == SVR_LS2044A) ||
466 (ver == SVR_LS2081A) || (ver == SVR_LS2041A)) {
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800467 for (i = 0; i < ARRAY_SIZE(final_map); i++) {
468 switch (final_map[i].phys) {
469 case CONFIG_SYS_PCIE1_PHYS_ADDR:
470 final_map[i].phys = 0x2000000000ULL;
471 final_map[i].virt = 0x2000000000ULL;
472 final_map[i].size = 0x800000000ULL;
473 break;
474 case CONFIG_SYS_PCIE2_PHYS_ADDR:
475 final_map[i].phys = 0x2800000000ULL;
476 final_map[i].virt = 0x2800000000ULL;
477 final_map[i].size = 0x800000000ULL;
478 break;
Yuantian Tang4aefa162019-04-10 16:43:33 +0800479#ifdef CONFIG_SYS_PCIE3_PHYS_ADDR
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800480 case CONFIG_SYS_PCIE3_PHYS_ADDR:
481 final_map[i].phys = 0x3000000000ULL;
482 final_map[i].virt = 0x3000000000ULL;
483 final_map[i].size = 0x800000000ULL;
484 break;
Yuantian Tang4aefa162019-04-10 16:43:33 +0800485#endif
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000486#ifdef CONFIG_SYS_PCIE4_PHYS_ADDR
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800487 case CONFIG_SYS_PCIE4_PHYS_ADDR:
488 final_map[i].phys = 0x3800000000ULL;
489 final_map[i].virt = 0x3800000000ULL;
490 final_map[i].size = 0x800000000ULL;
491 break;
Hou Zhiqiangd5d1ce32019-04-08 10:15:32 +0000492#endif
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800493 default:
494 break;
495 }
496 }
497 }
498#endif
499}
500
Mingkai Hu0e58b512015-10-26 19:47:50 +0800501/*
502 * The final tables look similar to early tables, but different in detail.
503 * These tables are in DRAM. Sub tables are added to enable cache for
504 * QBMan and OCRAM.
505 *
York Sun1ef95cc2016-06-24 16:46:18 -0700506 * Put the MMU table in secure memory if gd->arch.secure_ram is valid.
507 * OCRAM will be not used for this purpose so gd->arch.secure_ram can't be 0.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800508 */
509static inline void final_mmu_setup(void)
510{
York Sun9da8f502016-06-24 16:46:23 -0700511 u64 tlb_addr_save = gd->arch.tlb_addr;
York Sun0804d562015-12-04 11:57:08 -0800512 unsigned int el = current_el();
York Sun9da8f502016-06-24 16:46:23 -0700513 int index;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800514
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800515 /* fix the final_map before filling in the block entries */
516 fix_pcie_mmu_map();
517
York Sun9da8f502016-06-24 16:46:23 -0700518 mem_map = final_map;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800519
York Sun75488ed2017-03-06 09:02:30 -0800520 /* Update mapping for DDR to actual size */
521 for (index = 0; index < ARRAY_SIZE(final_map) - 2; index++) {
522 /*
523 * Find the entry for DDR mapping and update the address and
524 * size. Zero-sized mapping will be skipped when creating MMU
525 * table.
526 */
527 switch (final_map[index].virt) {
528 case CONFIG_SYS_FSL_DRAM_BASE1:
529 final_map[index].virt = gd->bd->bi_dram[0].start;
530 final_map[index].phys = gd->bd->bi_dram[0].start;
531 final_map[index].size = gd->bd->bi_dram[0].size;
532 break;
533#ifdef CONFIG_SYS_FSL_DRAM_BASE2
534 case CONFIG_SYS_FSL_DRAM_BASE2:
535#if (CONFIG_NR_DRAM_BANKS >= 2)
536 final_map[index].virt = gd->bd->bi_dram[1].start;
537 final_map[index].phys = gd->bd->bi_dram[1].start;
538 final_map[index].size = gd->bd->bi_dram[1].size;
539#else
540 final_map[index].size = 0;
541#endif
542 break;
543#endif
544#ifdef CONFIG_SYS_FSL_DRAM_BASE3
545 case CONFIG_SYS_FSL_DRAM_BASE3:
546#if (CONFIG_NR_DRAM_BANKS >= 3)
547 final_map[index].virt = gd->bd->bi_dram[2].start;
548 final_map[index].phys = gd->bd->bi_dram[2].start;
549 final_map[index].size = gd->bd->bi_dram[2].size;
550#else
551 final_map[index].size = 0;
552#endif
553 break;
554#endif
555 default:
556 break;
557 }
558 }
559
York Sun0804d562015-12-04 11:57:08 -0800560#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
York Sun9da8f502016-06-24 16:46:23 -0700561 if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
562 if (el == 3) {
563 /*
564 * Only use gd->arch.secure_ram if the address is
565 * recalculated. Align to 4KB for MMU table.
566 */
567 /* put page tables in secure ram */
568 index = ARRAY_SIZE(final_map) - 2;
569 gd->arch.tlb_addr = gd->arch.secure_ram & ~0xfff;
570 final_map[index].virt = gd->arch.secure_ram & ~0x3;
571 final_map[index].phys = final_map[index].virt;
572 final_map[index].size = CONFIG_SYS_MEM_RESERVE_SECURE;
573 final_map[index].attrs = PTE_BLOCK_OUTER_SHARE;
York Sun1ef95cc2016-06-24 16:46:18 -0700574 gd->arch.secure_ram |= MEM_RESERVE_SECURE_SECURED;
York Sun9da8f502016-06-24 16:46:23 -0700575 tlb_addr_save = gd->arch.tlb_addr;
York Sun0804d562015-12-04 11:57:08 -0800576 } else {
York Sun9da8f502016-06-24 16:46:23 -0700577 /* Use allocated (board_f.c) memory for TLB */
578 tlb_addr_save = gd->arch.tlb_allocated;
579 gd->arch.tlb_addr = tlb_addr_save;
York Sun0804d562015-12-04 11:57:08 -0800580 }
581 }
582#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800583
York Sun9da8f502016-06-24 16:46:23 -0700584 /* Reset the fill ptr */
585 gd->arch.tlb_fillptr = tlb_addr_save;
586
587 /* Create normal system page tables */
588 setup_pgtables();
589
590 /* Create emergency page tables */
591 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
592 gd->arch.tlb_emerg = gd->arch.tlb_addr;
593 setup_pgtables();
594 gd->arch.tlb_addr = tlb_addr_save;
595
York Suncf64ced2017-03-06 09:02:31 -0800596 /* Disable cache and MMU */
597 dcache_disable(); /* TLBs are invalidated */
598 invalidate_icache_all();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800599
600 /* point TTBR to the new table */
York Sun9da8f502016-06-24 16:46:23 -0700601 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
Mingkai Hu0e58b512015-10-26 19:47:50 +0800602 MEMORY_ATTRIBUTES);
York Suncf64ced2017-03-06 09:02:31 -0800603
York Suneb6eac12016-07-22 10:52:23 -0700604 set_sctlr(get_sctlr() | CR_M);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800605}
606
Alexander Grafbc78b922016-03-21 20:26:12 +0100607u64 get_page_table_size(void)
608{
609 return 0x10000;
610}
611
Mingkai Hu0e58b512015-10-26 19:47:50 +0800612int arch_cpu_init(void)
613{
York Sune6b871e2017-05-15 08:51:59 -0700614 /*
615 * This function is called before U-Boot relocates itself to speed up
616 * on system running. It is not necessary to run if performance is not
617 * critical. Skip if MMU is already enabled by SPL or other means.
618 */
619 if (get_sctlr() & CR_M)
620 return 0;
621
Mingkai Hu0e58b512015-10-26 19:47:50 +0800622 icache_enable();
623 __asm_invalidate_dcache_all();
624 __asm_invalidate_tlb_all();
625 early_mmu_setup();
626 set_sctlr(get_sctlr() | CR_C);
627 return 0;
628}
629
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800630void mmu_setup(void)
631{
632 final_mmu_setup();
633}
634
Mingkai Hu0e58b512015-10-26 19:47:50 +0800635/*
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800636 * This function is called from common/board_r.c.
637 * It recreates MMU table in main memory.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800638 */
639void enable_caches(void)
640{
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800641 mmu_setup();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800642 __asm_invalidate_tlb_all();
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800643 icache_enable();
644 dcache_enable();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800645}
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400646#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000647
648#ifdef CONFIG_TFABOOT
649enum boot_src __get_boot_src(u32 porsr1)
650{
651 enum boot_src src = BOOT_SOURCE_RESERVED;
652 u32 rcw_src = (porsr1 & RCW_SRC_MASK) >> RCW_SRC_BIT;
Priyanka Jain88c25662018-10-29 09:11:29 +0000653#if !defined(CONFIG_NXP_LSCH3_2)
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000654 u32 val;
655#endif
656 debug("%s: rcw_src 0x%x\n", __func__, rcw_src);
657
658#if defined(CONFIG_FSL_LSCH3)
Priyanka Jain88c25662018-10-29 09:11:29 +0000659#if defined(CONFIG_NXP_LSCH3_2)
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000660 switch (rcw_src) {
661 case RCW_SRC_SDHC1_VAL:
662 src = BOOT_SOURCE_SD_MMC;
663 break;
664 case RCW_SRC_SDHC2_VAL:
665 src = BOOT_SOURCE_SD_MMC2;
666 break;
667 case RCW_SRC_I2C1_VAL:
668 src = BOOT_SOURCE_I2C1_EXTENDED;
669 break;
670 case RCW_SRC_FLEXSPI_NAND2K_VAL:
671 src = BOOT_SOURCE_XSPI_NAND;
672 break;
673 case RCW_SRC_FLEXSPI_NAND4K_VAL:
674 src = BOOT_SOURCE_XSPI_NAND;
675 break;
676 case RCW_SRC_RESERVED_1_VAL:
677 src = BOOT_SOURCE_RESERVED;
678 break;
679 case RCW_SRC_FLEXSPI_NOR_24B:
680 src = BOOT_SOURCE_XSPI_NOR;
681 break;
682 default:
683 src = BOOT_SOURCE_RESERVED;
684 }
685#else
686 val = rcw_src & RCW_SRC_TYPE_MASK;
687 if (val == RCW_SRC_NOR_VAL) {
688 val = rcw_src & NOR_TYPE_MASK;
689
690 switch (val) {
691 case NOR_16B_VAL:
692 case NOR_32B_VAL:
693 src = BOOT_SOURCE_IFC_NOR;
694 break;
695 default:
696 src = BOOT_SOURCE_RESERVED;
697 }
698 } else {
699 /* RCW SRC Serial Flash */
700 val = rcw_src & RCW_SRC_SERIAL_MASK;
701 switch (val) {
702 case RCW_SRC_QSPI_VAL:
703 /* RCW SRC Serial NOR (QSPI) */
704 src = BOOT_SOURCE_QSPI_NOR;
705 break;
706 case RCW_SRC_SD_CARD_VAL:
707 /* RCW SRC SD Card */
708 src = BOOT_SOURCE_SD_MMC;
709 break;
710 case RCW_SRC_EMMC_VAL:
711 /* RCW SRC EMMC */
Rajesh Bhagat5b73c902018-12-27 04:37:49 +0000712 src = BOOT_SOURCE_SD_MMC;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000713 break;
714 case RCW_SRC_I2C1_VAL:
715 /* RCW SRC I2C1 Extended */
716 src = BOOT_SOURCE_I2C1_EXTENDED;
717 break;
718 default:
719 src = BOOT_SOURCE_RESERVED;
720 }
721 }
722#endif
723#elif defined(CONFIG_FSL_LSCH2)
724 /* RCW SRC NAND */
725 val = rcw_src & RCW_SRC_NAND_MASK;
726 if (val == RCW_SRC_NAND_VAL) {
727 val = rcw_src & NAND_RESERVED_MASK;
728 if (val != NAND_RESERVED_1 && val != NAND_RESERVED_2)
729 src = BOOT_SOURCE_IFC_NAND;
730
731 } else {
732 /* RCW SRC NOR */
733 val = rcw_src & RCW_SRC_NOR_MASK;
734 if (val == NOR_8B_VAL || val == NOR_16B_VAL) {
735 src = BOOT_SOURCE_IFC_NOR;
736 } else {
737 switch (rcw_src) {
738 case QSPI_VAL1:
739 case QSPI_VAL2:
740 src = BOOT_SOURCE_QSPI_NOR;
741 break;
742 case SD_VAL:
743 src = BOOT_SOURCE_SD_MMC;
744 break;
745 default:
746 src = BOOT_SOURCE_RESERVED;
747 }
748 }
749 }
750#endif
York Sun8f3f4ef2018-11-05 18:02:09 +0000751
752 if (CONFIG_IS_ENABLED(SYS_FSL_ERRATUM_A010539) && !rcw_src)
753 src = BOOT_SOURCE_QSPI_NOR;
754
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000755 debug("%s: src 0x%x\n", __func__, src);
756 return src;
757}
758
759enum boot_src get_boot_src(void)
760{
York Sun8f3f4ef2018-11-05 18:02:09 +0000761 struct pt_regs regs;
762 u32 porsr1 = 0;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000763
764#if defined(CONFIG_FSL_LSCH3)
765 u32 __iomem *dcfg_ccsr = (u32 __iomem *)DCFG_BASE;
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000766#elif defined(CONFIG_FSL_LSCH2)
767 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
York Sun8f3f4ef2018-11-05 18:02:09 +0000768#endif
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000769
York Sun8f3f4ef2018-11-05 18:02:09 +0000770 if (current_el() == 2) {
771 regs.regs[0] = SIP_SVC_RCW;
772
773 smc_call(&regs);
774 if (!regs.regs[0])
775 porsr1 = regs.regs[1];
776 }
777
778 if (current_el() == 3 || !porsr1) {
779#ifdef CONFIG_FSL_LSCH3
780 porsr1 = in_le32(dcfg_ccsr + DCFG_PORSR1 / 4);
781#elif defined(CONFIG_FSL_LSCH2)
782 porsr1 = in_be32(&gur->porsr1);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800783#endif
York Sun8f3f4ef2018-11-05 18:02:09 +0000784 }
785
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000786 debug("%s: porsr1 0x%x\n", __func__, porsr1);
787
788 return __get_boot_src(porsr1);
789}
790
791#ifdef CONFIG_ENV_IS_IN_MMC
792int mmc_get_env_dev(void)
793{
794 enum boot_src src = get_boot_src();
795 int dev = CONFIG_SYS_MMC_ENV_DEV;
796
797 switch (src) {
798 case BOOT_SOURCE_SD_MMC:
799 dev = 0;
800 break;
801 case BOOT_SOURCE_SD_MMC2:
802 dev = 1;
803 break;
804 default:
805 break;
806 }
807
808 return dev;
809}
810#endif
811
812enum env_location env_get_location(enum env_operation op, int prio)
813{
814 enum boot_src src = get_boot_src();
815 enum env_location env_loc = ENVL_NOWHERE;
816
817 if (prio)
818 return ENVL_UNKNOWN;
819
Udit Agarwal5e9720c2019-04-23 06:06:04 +0000820#ifdef CONFIG_ENV_IS_NOWHERE
821 return env_loc;
Pankit Gargbdbf84f2018-11-05 18:01:52 +0000822#endif
823
Rajesh Bhagat583da8b2018-11-05 18:01:42 +0000824 switch (src) {
825 case BOOT_SOURCE_IFC_NOR:
826 env_loc = ENVL_FLASH;
827 break;
828 case BOOT_SOURCE_QSPI_NOR:
829 /* FALLTHROUGH */
830 case BOOT_SOURCE_XSPI_NOR:
831 env_loc = ENVL_SPI_FLASH;
832 break;
833 case BOOT_SOURCE_IFC_NAND:
834 /* FALLTHROUGH */
835 case BOOT_SOURCE_QSPI_NAND:
836 /* FALLTHROUGH */
837 case BOOT_SOURCE_XSPI_NAND:
838 env_loc = ENVL_NAND;
839 break;
840 case BOOT_SOURCE_SD_MMC:
841 /* FALLTHROUGH */
842 case BOOT_SOURCE_SD_MMC2:
843 env_loc = ENVL_MMC;
844 break;
845 case BOOT_SOURCE_I2C1_EXTENDED:
846 /* FALLTHROUGH */
847 default:
848 break;
849 }
850
851 return env_loc;
852}
853#endif /* CONFIG_TFABOOT */
Mingkai Hu0e58b512015-10-26 19:47:50 +0800854
Priyanka Jain9a276702016-11-17 12:29:56 +0530855u32 initiator_type(u32 cluster, int init_id)
Mingkai Hu0e58b512015-10-26 19:47:50 +0800856{
857 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
858 u32 idx = (cluster >> (init_id * 8)) & TP_CLUSTER_INIT_MASK;
859 u32 type = 0;
860
861 type = gur_in32(&gur->tp_ityp[idx]);
862 if (type & TP_ITYP_AV)
863 return type;
864
865 return 0;
866}
867
York Suned7fbe32016-09-13 12:40:30 -0700868u32 cpu_pos_mask(void)
869{
870 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
871 int i = 0;
872 u32 cluster, type, mask = 0;
873
874 do {
875 int j;
876
877 cluster = gur_in32(&gur->tp_cluster[i].lower);
878 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
879 type = initiator_type(cluster, j);
880 if (type && (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM))
881 mask |= 1 << (i * TP_INIT_PER_CLUSTER + j);
882 }
883 i++;
884 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
885
886 return mask;
887}
888
Mingkai Hu0e58b512015-10-26 19:47:50 +0800889u32 cpu_mask(void)
890{
891 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
892 int i = 0, count = 0;
893 u32 cluster, type, mask = 0;
894
895 do {
896 int j;
897
898 cluster = gur_in32(&gur->tp_cluster[i].lower);
899 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
900 type = initiator_type(cluster, j);
901 if (type) {
902 if (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
903 mask |= 1 << count;
904 count++;
905 }
906 }
907 i++;
908 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
909
910 return mask;
911}
912
913/*
914 * Return the number of cores on this SOC.
915 */
916int cpu_numcores(void)
917{
918 return hweight32(cpu_mask());
919}
920
921int fsl_qoriq_core_to_cluster(unsigned int core)
922{
923 struct ccsr_gur __iomem *gur =
924 (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
925 int i = 0, count = 0;
926 u32 cluster;
927
928 do {
929 int j;
930
931 cluster = gur_in32(&gur->tp_cluster[i].lower);
932 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
933 if (initiator_type(cluster, j)) {
934 if (count == core)
935 return i;
936 count++;
937 }
938 }
939 i++;
940 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
941
942 return -1; /* cannot identify the cluster */
943}
944
945u32 fsl_qoriq_core_to_type(unsigned int core)
946{
947 struct ccsr_gur __iomem *gur =
948 (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
949 int i = 0, count = 0;
950 u32 cluster, type;
951
952 do {
953 int j;
954
955 cluster = gur_in32(&gur->tp_cluster[i].lower);
956 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
957 type = initiator_type(cluster, j);
958 if (type) {
959 if (count == core)
960 return type;
961 count++;
962 }
963 }
964 i++;
965 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
966
967 return -1; /* cannot identify the cluster */
968}
969
Priyanka Jain96b001f2016-11-17 12:29:51 +0530970#ifndef CONFIG_FSL_LSCH3
Sriram Dash9282d262016-06-13 09:58:32 +0530971uint get_svr(void)
972{
973 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
974
975 return gur_in32(&gur->svr);
976}
Priyanka Jain96b001f2016-11-17 12:29:51 +0530977#endif
Sriram Dash9282d262016-06-13 09:58:32 +0530978
Mingkai Hu0e58b512015-10-26 19:47:50 +0800979#ifdef CONFIG_DISPLAY_CPUINFO
980int print_cpuinfo(void)
981{
982 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
983 struct sys_info sysinfo;
984 char buf[32];
985 unsigned int i, core;
York Suncbe8e1c2016-04-04 11:41:26 -0700986 u32 type, rcw, svr = gur_in32(&gur->svr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800987
988 puts("SoC: ");
989
990 cpu_name(buf);
York Suncbe8e1c2016-04-04 11:41:26 -0700991 printf(" %s (0x%x)\n", buf, svr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800992 memset((u8 *)buf, 0x00, ARRAY_SIZE(buf));
993 get_sys_info(&sysinfo);
994 puts("Clock Configuration:");
995 for_each_cpu(i, core, cpu_numcores(), cpu_mask()) {
996 if (!(i % 3))
997 puts("\n ");
998 type = TP_ITYP_VER(fsl_qoriq_core_to_type(core));
999 printf("CPU%d(%s):%-4s MHz ", core,
1000 type == TY_ITYP_VER_A7 ? "A7 " :
1001 (type == TY_ITYP_VER_A53 ? "A53" :
Alison Wang79808392016-07-05 16:01:52 +08001002 (type == TY_ITYP_VER_A57 ? "A57" :
1003 (type == TY_ITYP_VER_A72 ? "A72" : " "))),
Mingkai Hu0e58b512015-10-26 19:47:50 +08001004 strmhz(buf, sysinfo.freq_processor[core]));
1005 }
Hou Zhiqiang3f91cda2017-01-10 16:44:15 +08001006 /* Display platform clock as Bus frequency. */
Mingkai Hu0e58b512015-10-26 19:47:50 +08001007 printf("\n Bus: %-4s MHz ",
Hou Zhiqiang3f91cda2017-01-10 16:44:15 +08001008 strmhz(buf, sysinfo.freq_systembus / CONFIG_SYS_FSL_PCLK_DIV));
Mingkai Hu0e58b512015-10-26 19:47:50 +08001009 printf("DDR: %-4s MT/s", strmhz(buf, sysinfo.freq_ddrbus));
Shaohui Xie04643262015-10-26 19:47:54 +08001010#ifdef CONFIG_SYS_DPAA_FMAN
1011 printf(" FMAN: %-4s MHz", strmhz(buf, sysinfo.freq_fman[0]));
1012#endif
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301013#ifdef CONFIG_SYS_FSL_HAS_DP_DDR
York Suncbe8e1c2016-04-04 11:41:26 -07001014 if (soc_has_dp_ddr()) {
1015 printf(" DP-DDR: %-4s MT/s",
1016 strmhz(buf, sysinfo.freq_ddrbus2));
1017 }
Mingkai Hu0e58b512015-10-26 19:47:50 +08001018#endif
1019 puts("\n");
1020
1021 /*
1022 * Display the RCW, so that no one gets confused as to what RCW
1023 * we're actually using for this boot.
1024 */
1025 puts("Reset Configuration Word (RCW):");
1026 for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
1027 rcw = gur_in32(&gur->rcwsr[i]);
1028 if ((i % 4) == 0)
1029 printf("\n %08x:", i * 4);
1030 printf(" %08x", rcw);
1031 }
1032 puts("\n");
1033
1034 return 0;
1035}
1036#endif
1037
1038#ifdef CONFIG_FSL_ESDHC
1039int cpu_mmc_init(bd_t *bis)
1040{
1041 return fsl_esdhc_mmc_init(bis);
1042}
1043#endif
1044
1045int cpu_eth_init(bd_t *bis)
1046{
1047 int error = 0;
1048
Santan Kumar1afa9002017-05-05 15:42:29 +05301049#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001050 error = fsl_mc_ldpaa_init(bis);
1051#endif
Shaohui Xie04643262015-10-26 19:47:54 +08001052#ifdef CONFIG_FMAN_ENET
1053 fm_standard_init(bis);
1054#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001055 return error;
1056}
1057
Yuantian Tangaec3b142017-04-19 13:27:39 +08001058static inline int check_psci(void)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001059{
Yuantian Tangaec3b142017-04-19 13:27:39 +08001060 unsigned int psci_ver;
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301061
Yuantian Tangaec3b142017-04-19 13:27:39 +08001062 psci_ver = sec_firmware_support_psci_version();
1063 if (psci_ver == PSCI_INVALID_VER)
1064 return 1;
1065
1066 return 0;
1067}
1068
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301069static void config_core_prefetch(void)
1070{
1071 char *buf = NULL;
1072 char buffer[HWCONFIG_BUFFER_SIZE];
1073 const char *prefetch_arg = NULL;
1074 size_t arglen;
1075 unsigned int mask;
1076 struct pt_regs regs;
1077
1078 if (env_get_f("hwconfig", buffer, sizeof(buffer)) > 0)
1079 buf = buffer;
Pankaj Bansal6c772772019-10-31 05:41:09 +00001080 else
1081 return;
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301082
1083 prefetch_arg = hwconfig_subarg_f("core_prefetch", "disable",
1084 &arglen, buf);
1085
1086 if (prefetch_arg) {
1087 mask = simple_strtoul(prefetch_arg, NULL, 0) & 0xff;
1088 if (mask & 0x1) {
1089 printf("Core0 prefetch can't be disabled\n");
1090 return;
1091 }
1092
1093#define SIP_PREFETCH_DISABLE_64 0xC200FF13
1094 regs.regs[0] = SIP_PREFETCH_DISABLE_64;
1095 regs.regs[1] = mask;
1096 smc_call(&regs);
1097
1098 if (regs.regs[0])
1099 printf("Prefetch disable config failed for mask ");
1100 else
1101 printf("Prefetch disable config passed for mask ");
1102 printf("0x%x\n", mask);
1103 }
1104}
1105
Alex Marginean762a2682019-11-27 17:19:32 +02001106#ifdef CONFIG_PCIE_ECAM_GENERIC
1107__weak void set_ecam_icids(void)
1108{
1109}
1110#endif
1111
Yuantian Tangaec3b142017-04-19 13:27:39 +08001112int arch_early_init_r(void)
1113{
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301114#ifdef CONFIG_SYS_FSL_ERRATUM_A009635
Priyanka Jain823e0422017-02-14 10:34:31 +05301115 u32 svr_dev_id;
1116 /*
1117 * erratum A009635 is valid only for LS2080A SoC and
1118 * its personalitiesi
1119 */
Wenbin song5d8a61c2017-12-04 12:18:28 +08001120 svr_dev_id = get_svr();
1121 if (IS_SVR_DEV(svr_dev_id, SVR_DEV(SVR_LS2080A)))
Priyanka Jain823e0422017-02-14 10:34:31 +05301122 erratum_a009635();
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +05301123#endif
Shengzhou Liu15875a52016-11-21 11:36:48 +08001124#if defined(CONFIG_SYS_FSL_ERRATUM_A009942) && defined(CONFIG_SYS_FSL_DDR)
1125 erratum_a009942_check_cpo();
1126#endif
Yuantian Tangaec3b142017-04-19 13:27:39 +08001127 if (check_psci()) {
1128 debug("PSCI: PSCI does not exist.\n");
1129
1130 /* if PSCI does not exist, boot secondary cores here */
1131 if (fsl_layerscape_wake_seconday_cores())
Hou Zhiqiang21c4d552016-06-28 20:18:15 +08001132 printf("Did not wake secondary cores\n");
1133 }
Mingkai Hu0e58b512015-10-26 19:47:50 +08001134
Prabhakar Kushwaha0acce842017-11-10 11:32:52 +05301135 config_core_prefetch();
1136
Mingkai Hu0e58b512015-10-26 19:47:50 +08001137#ifdef CONFIG_SYS_HAS_SERDES
1138 fsl_serdes_init();
1139#endif
Pankaj Bansalcc840622018-10-29 11:28:26 +00001140#ifdef CONFIG_SYS_FSL_HAS_RGMII
1141 /* some dpmacs in armv8a based freescale layerscape SOCs can be
1142 * configured via both serdes(sgmii, xfi, xlaui etc) bits and via
1143 * EC*_PMUX(rgmii) bits in RCW.
1144 * e.g. dpmac 17 and 18 in LX2160A can be configured as SGMII from
1145 * serdes bits and as RGMII via EC1_PMUX/EC2_PMUX bits
1146 * Now if a dpmac is enabled by serdes bits then it takes precedence
1147 * over EC*_PMUX bits. i.e. in LX2160A if we select serdes protocol
1148 * that configures dpmac17 as SGMII and set the EC1_PMUX as RGMII,
1149 * then the dpmac is SGMII and not RGMII.
1150 *
1151 * Therefore, move the fsl_rgmii_init after fsl_serdes_init. in
1152 * fsl_rgmii_init function of SOC, we will check if the dpmac is enabled
1153 * or not? if it is (fsl_serdes_init has already enabled the dpmac),
1154 * then don't enable it.
1155 */
1156 fsl_rgmii_init();
1157#endif
Shaohui Xie04643262015-10-26 19:47:54 +08001158#ifdef CONFIG_FMAN_ENET
1159 fman_enet_init();
1160#endif
Ahmed Mansouraa270b42017-12-15 16:01:00 -05001161#ifdef CONFIG_SYS_DPAA_QBMAN
1162 setup_qbman_portals();
1163#endif
Alex Marginean762a2682019-11-27 17:19:32 +02001164#ifdef CONFIG_PCIE_ECAM_GENERIC
1165 set_ecam_icids();
1166#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001167 return 0;
1168}
1169
1170int timer_init(void)
1171{
1172 u32 __iomem *cntcr = (u32 *)CONFIG_SYS_FSL_TIMER_ADDR;
1173#ifdef CONFIG_FSL_LSCH3
1174 u32 __iomem *cltbenr = (u32 *)CONFIG_SYS_FSL_PMU_CLTBENR;
1175#endif
Thomas Schaefercbec2b82019-08-08 16:00:30 +08001176#if defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
1177 defined(CONFIG_ARCH_LS1028A)
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001178 u32 __iomem *pctbenr = (u32 *)FSL_PMU_PCTBENR_OFFSET;
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301179 u32 svr_dev_id;
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001180#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001181#ifdef COUNTER_FREQUENCY_REAL
1182 unsigned long cntfrq = COUNTER_FREQUENCY_REAL;
1183
1184 /* Update with accurate clock frequency */
York Sune6b871e2017-05-15 08:51:59 -07001185 if (current_el() == 3)
1186 asm volatile("msr cntfrq_el0, %0" : : "r" (cntfrq) : "memory");
Mingkai Hu0e58b512015-10-26 19:47:50 +08001187#endif
1188
1189#ifdef CONFIG_FSL_LSCH3
1190 /* Enable timebase for all clusters.
1191 * It is safe to do so even some clusters are not enabled.
1192 */
1193 out_le32(cltbenr, 0xf);
1194#endif
1195
Thomas Schaefercbec2b82019-08-08 16:00:30 +08001196#if defined(CONFIG_ARCH_LS2080A) || defined(CONFIG_ARCH_LS1088A) || \
1197 defined(CONFIG_ARCH_LS1028A)
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001198 /*
1199 * In certain Layerscape SoCs, the clock for each core's
1200 * has an enable bit in the PMU Physical Core Time Base Enable
1201 * Register (PCTBENR), which allows the watchdog to operate.
1202 */
1203 setbits_le32(pctbenr, 0xff);
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301204 /*
1205 * For LS2080A SoC and its personalities, timer controller
1206 * offset is different
1207 */
Wenbin song5d8a61c2017-12-04 12:18:28 +08001208 svr_dev_id = get_svr();
1209 if (IS_SVR_DEV(svr_dev_id, SVR_DEV(SVR_LS2080A)))
Priyanka Jain3d31ec72016-11-17 12:29:52 +05301210 cntcr = (u32 *)SYS_FSL_LS2080A_LS2085A_TIMER_ADDR;
1211
Yunhui Cui3dfb82a2016-06-08 10:31:42 +08001212#endif
1213
Mingkai Hu0e58b512015-10-26 19:47:50 +08001214 /* Enable clock for timer
1215 * This is a global setting.
1216 */
1217 out_le32(cntcr, 0x1);
1218
1219 return 0;
1220}
1221
Alexander Graf12be31c2016-11-17 01:03:01 +01001222__efi_runtime_data u32 __iomem *rstcr = (u32 *)CONFIG_SYS_FSL_RST_ADDR;
1223
1224void __efi_runtime reset_cpu(ulong addr)
Mingkai Hu0e58b512015-10-26 19:47:50 +08001225{
Mingkai Hu0e58b512015-10-26 19:47:50 +08001226 u32 val;
1227
Priyanka Jainef76b2e2018-10-29 09:17:09 +00001228#ifdef CONFIG_ARCH_LX2160A
1229 val = in_le32(rstcr);
1230 val |= 0x01;
1231 out_le32(rstcr, val);
1232#else
Mingkai Hu0e58b512015-10-26 19:47:50 +08001233 /* Raise RESET_REQ_B */
1234 val = scfg_in32(rstcr);
1235 val |= 0x02;
1236 scfg_out32(rstcr, val);
Priyanka Jainef76b2e2018-10-29 09:17:09 +00001237#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +08001238}
York Sun928b6812015-12-07 11:08:58 -08001239
Mathew McBride8f137732019-10-18 14:27:54 +11001240#if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_PSCI_RESET)
Alexander Graf12be31c2016-11-17 01:03:01 +01001241
1242void __efi_runtime EFIAPI efi_reset_system(
1243 enum efi_reset_type reset_type,
1244 efi_status_t reset_status,
1245 unsigned long data_size, void *reset_data)
1246{
1247 switch (reset_type) {
1248 case EFI_RESET_COLD:
1249 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +01001250 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf12be31c2016-11-17 01:03:01 +01001251 reset_cpu(0);
1252 break;
1253 case EFI_RESET_SHUTDOWN:
1254 /* Nothing we can do */
1255 break;
1256 }
1257
1258 while (1) { }
1259}
1260
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +01001261efi_status_t efi_reset_system_init(void)
Alexander Graf12be31c2016-11-17 01:03:01 +01001262{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +01001263 return efi_add_runtime_mmio(&rstcr, sizeof(*rstcr));
Alexander Graf12be31c2016-11-17 01:03:01 +01001264}
1265
1266#endif
1267
York Sun2db54082017-09-07 10:12:32 -07001268/*
1269 * Calculate reserved memory with given memory bank
1270 * Return aligned memory size on success
1271 * Return (ram_size + needed size) for failure
1272 */
York Sun928b6812015-12-07 11:08:58 -08001273phys_size_t board_reserve_ram_top(phys_size_t ram_size)
1274{
1275 phys_size_t ram_top = ram_size;
1276
Santan Kumar1afa9002017-05-05 15:42:29 +05301277#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
York Sun2db54082017-09-07 10:12:32 -07001278 ram_top = mc_get_dram_block_size();
1279 if (ram_top > ram_size)
1280 return ram_size + ram_top;
1281
1282 ram_top = ram_size - ram_top;
York Sun4de24ef2017-03-06 09:02:28 -08001283 /* The start address of MC reserved memory needs to be aligned. */
York Sun928b6812015-12-07 11:08:58 -08001284 ram_top &= ~(CONFIG_SYS_MC_RSV_MEM_ALIGN - 1);
1285#endif
York Sun4de24ef2017-03-06 09:02:28 -08001286
1287 return ram_size - ram_top;
1288}
1289
1290phys_size_t get_effective_memsize(void)
1291{
1292 phys_size_t ea_size, rem = 0;
1293
1294 /*
1295 * For ARMv8 SoCs, DDR memory is split into two or three regions. The
Sumit Garged0deea2017-10-04 03:20:49 +05301296 * first region is 2GB space at 0x8000_0000. Secure memory needs to
1297 * allocated from first region. If the memory extends to the second
1298 * region (or the third region if applicable), Management Complex (MC)
1299 * memory should be put into the highest region, i.e. the end of DDR
1300 * memory. CONFIG_MAX_MEM_MAPPED is set to the size of first region so
1301 * U-Boot doesn't relocate itself into higher address. Should DDR be
1302 * configured to skip the first region, this function needs to be
1303 * adjusted.
York Sun4de24ef2017-03-06 09:02:28 -08001304 */
1305 if (gd->ram_size > CONFIG_MAX_MEM_MAPPED) {
1306 ea_size = CONFIG_MAX_MEM_MAPPED;
1307 rem = gd->ram_size - ea_size;
1308 } else {
1309 ea_size = gd->ram_size;
1310 }
1311
1312#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1313 /* Check if we have enough space for secure memory */
Sumit Garged0deea2017-10-04 03:20:49 +05301314 if (ea_size > CONFIG_SYS_MEM_RESERVE_SECURE)
1315 ea_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
1316 else
1317 printf("Error: No enough space for secure memory.\n");
York Sun4de24ef2017-03-06 09:02:28 -08001318#endif
1319 /* Check if we have enough memory for MC */
1320 if (rem < board_reserve_ram_top(rem)) {
1321 /* Not enough memory in high region to reserve */
York Sun2db54082017-09-07 10:12:32 -07001322 if (ea_size > board_reserve_ram_top(ea_size))
1323 ea_size -= board_reserve_ram_top(ea_size);
York Sun4de24ef2017-03-06 09:02:28 -08001324 else
1325 printf("Error: No enough space for reserved memory.\n");
1326 }
1327
1328 return ea_size;
1329}
1330
Rajesh Bhagat1dde2d22018-11-05 18:01:58 +00001331#ifdef CONFIG_TFABOOT
1332phys_size_t tfa_get_dram_size(void)
1333{
1334 struct pt_regs regs;
1335 phys_size_t dram_size = 0;
1336
1337 regs.regs[0] = SMC_DRAM_BANK_INFO;
1338 regs.regs[1] = -1;
1339
1340 smc_call(&regs);
1341 if (regs.regs[0])
1342 return 0;
1343
1344 dram_size = regs.regs[1];
1345 return dram_size;
1346}
1347
1348static int tfa_dram_init_banksize(void)
1349{
1350 int i = 0, ret = 0;
1351 struct pt_regs regs;
1352 phys_size_t dram_size = tfa_get_dram_size();
1353
1354 debug("dram_size %llx\n", dram_size);
1355
1356 if (!dram_size)
1357 return -EINVAL;
1358
1359 do {
1360 regs.regs[0] = SMC_DRAM_BANK_INFO;
1361 regs.regs[1] = i;
1362
1363 smc_call(&regs);
1364 if (regs.regs[0]) {
1365 ret = -EINVAL;
1366 break;
1367 }
1368
1369 debug("bank[%d]: start %lx, size %lx\n", i, regs.regs[1],
1370 regs.regs[2]);
1371 gd->bd->bi_dram[i].start = regs.regs[1];
1372 gd->bd->bi_dram[i].size = regs.regs[2];
1373
1374 dram_size -= gd->bd->bi_dram[i].size;
1375
1376 i++;
1377 } while (dram_size);
1378
1379 if (i > 0)
1380 ret = 0;
1381
1382#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
1383 /* Assign memory for MC */
1384#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1385 if (gd->bd->bi_dram[2].size >=
1386 board_reserve_ram_top(gd->bd->bi_dram[2].size)) {
1387 gd->arch.resv_ram = gd->bd->bi_dram[2].start +
1388 gd->bd->bi_dram[2].size -
1389 board_reserve_ram_top(gd->bd->bi_dram[2].size);
1390 } else
1391#endif
1392 {
1393 if (gd->bd->bi_dram[1].size >=
1394 board_reserve_ram_top(gd->bd->bi_dram[1].size)) {
1395 gd->arch.resv_ram = gd->bd->bi_dram[1].start +
1396 gd->bd->bi_dram[1].size -
1397 board_reserve_ram_top(gd->bd->bi_dram[1].size);
1398 } else if (gd->bd->bi_dram[0].size >
1399 board_reserve_ram_top(gd->bd->bi_dram[0].size)) {
1400 gd->arch.resv_ram = gd->bd->bi_dram[0].start +
1401 gd->bd->bi_dram[0].size -
1402 board_reserve_ram_top(gd->bd->bi_dram[0].size);
1403 }
1404 }
1405#endif /* CONFIG_FSL_MC_ENET */
1406
1407 return ret;
1408}
1409#endif
1410
Simon Glass2f949c32017-03-31 08:40:32 -06001411int dram_init_banksize(void)
York Sun4de24ef2017-03-06 09:02:28 -08001412{
1413#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1414 phys_size_t dp_ddr_size;
1415#endif
1416
Rajesh Bhagat1dde2d22018-11-05 18:01:58 +00001417#ifdef CONFIG_TFABOOT
1418 if (!tfa_dram_init_banksize())
1419 return 0;
1420#endif
York Sun4de24ef2017-03-06 09:02:28 -08001421 /*
1422 * gd->ram_size has the total size of DDR memory, less reserved secure
1423 * memory. The DDR extends from low region to high region(s) presuming
1424 * no hole is created with DDR configuration. gd->arch.secure_ram tracks
1425 * the location of secure memory. gd->arch.resv_ram tracks the location
York Sunc9e3e042017-09-28 08:42:12 -07001426 * of reserved memory for Management Complex (MC). Because gd->ram_size
1427 * is reduced by this function if secure memory is reserved, checking
1428 * gd->arch.secure_ram should be done to avoid running it repeatedly.
York Sun4de24ef2017-03-06 09:02:28 -08001429 */
York Sunc9e3e042017-09-28 08:42:12 -07001430
1431#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1432 if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
1433 debug("No need to run again, skip %s\n", __func__);
1434
1435 return 0;
1436 }
1437#endif
1438
York Sun4de24ef2017-03-06 09:02:28 -08001439 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
1440 if (gd->ram_size > CONFIG_SYS_DDR_BLOCK1_SIZE) {
1441 gd->bd->bi_dram[0].size = CONFIG_SYS_DDR_BLOCK1_SIZE;
1442 gd->bd->bi_dram[1].start = CONFIG_SYS_DDR_BLOCK2_BASE;
1443 gd->bd->bi_dram[1].size = gd->ram_size -
1444 CONFIG_SYS_DDR_BLOCK1_SIZE;
1445#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1446 if (gd->bi_dram[1].size > CONFIG_SYS_DDR_BLOCK2_SIZE) {
1447 gd->bd->bi_dram[2].start = CONFIG_SYS_DDR_BLOCK3_BASE;
1448 gd->bd->bi_dram[2].size = gd->bd->bi_dram[1].size -
1449 CONFIG_SYS_DDR_BLOCK2_SIZE;
1450 gd->bd->bi_dram[1].size = CONFIG_SYS_DDR_BLOCK2_SIZE;
1451 }
1452#endif
1453 } else {
1454 gd->bd->bi_dram[0].size = gd->ram_size;
1455 }
1456#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
Sumit Garged0deea2017-10-04 03:20:49 +05301457 if (gd->bd->bi_dram[0].size >
1458 CONFIG_SYS_MEM_RESERVE_SECURE) {
1459 gd->bd->bi_dram[0].size -=
1460 CONFIG_SYS_MEM_RESERVE_SECURE;
1461 gd->arch.secure_ram = gd->bd->bi_dram[0].start +
1462 gd->bd->bi_dram[0].size;
York Sun4de24ef2017-03-06 09:02:28 -08001463 gd->arch.secure_ram |= MEM_RESERVE_SECURE_MAINTAINED;
1464 gd->ram_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
York Sun4de24ef2017-03-06 09:02:28 -08001465 }
1466#endif /* CONFIG_SYS_MEM_RESERVE_SECURE */
1467
Santan Kumar1afa9002017-05-05 15:42:29 +05301468#if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
York Sun4de24ef2017-03-06 09:02:28 -08001469 /* Assign memory for MC */
1470#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1471 if (gd->bd->bi_dram[2].size >=
1472 board_reserve_ram_top(gd->bd->bi_dram[2].size)) {
1473 gd->arch.resv_ram = gd->bd->bi_dram[2].start +
1474 gd->bd->bi_dram[2].size -
1475 board_reserve_ram_top(gd->bd->bi_dram[2].size);
1476 } else
1477#endif
1478 {
1479 if (gd->bd->bi_dram[1].size >=
1480 board_reserve_ram_top(gd->bd->bi_dram[1].size)) {
1481 gd->arch.resv_ram = gd->bd->bi_dram[1].start +
1482 gd->bd->bi_dram[1].size -
1483 board_reserve_ram_top(gd->bd->bi_dram[1].size);
1484 } else if (gd->bd->bi_dram[0].size >
1485 board_reserve_ram_top(gd->bd->bi_dram[0].size)) {
1486 gd->arch.resv_ram = gd->bd->bi_dram[0].start +
1487 gd->bd->bi_dram[0].size -
1488 board_reserve_ram_top(gd->bd->bi_dram[0].size);
1489 }
1490 }
1491#endif /* CONFIG_FSL_MC_ENET */
1492
1493#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1494#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1495#error "This SoC shouldn't have DP DDR"
1496#endif
1497 if (soc_has_dp_ddr()) {
1498 /* initialize DP-DDR here */
1499 puts("DP-DDR: ");
1500 /*
1501 * DDR controller use 0 as the base address for binding.
1502 * It is mapped to CONFIG_SYS_DP_DDR_BASE for core to access.
1503 */
1504 dp_ddr_size = fsl_other_ddr_sdram(CONFIG_SYS_DP_DDR_BASE_PHY,
1505 CONFIG_DP_DDR_CTRL,
1506 CONFIG_DP_DDR_NUM_CTRLS,
1507 CONFIG_DP_DDR_DIMM_SLOTS_PER_CTLR,
1508 NULL, NULL, NULL);
1509 if (dp_ddr_size) {
1510 gd->bd->bi_dram[2].start = CONFIG_SYS_DP_DDR_BASE;
1511 gd->bd->bi_dram[2].size = dp_ddr_size;
1512 } else {
1513 puts("Not detected");
1514 }
1515 }
1516#endif
Simon Glass2f949c32017-03-31 08:40:32 -06001517
York Sunc9e3e042017-09-28 08:42:12 -07001518#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
1519 debug("%s is called. gd->ram_size is reduced to %lu\n",
1520 __func__, (ulong)gd->ram_size);
1521#endif
1522
Simon Glass2f949c32017-03-31 08:40:32 -06001523 return 0;
York Sun4de24ef2017-03-06 09:02:28 -08001524}
1525
Stephen Warrend0de8062018-08-30 15:43:43 -06001526#if CONFIG_IS_ENABLED(EFI_LOADER)
York Sun4de24ef2017-03-06 09:02:28 -08001527void efi_add_known_memory(void)
1528{
1529 int i;
1530 phys_addr_t ram_start, start;
1531 phys_size_t ram_size;
1532 u64 pages;
York Sun928b6812015-12-07 11:08:58 -08001533
York Sun4de24ef2017-03-06 09:02:28 -08001534 /* Add RAM */
1535 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
1536#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
1537#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1538#error "This SoC shouldn't have DP DDR"
1539#endif
1540 if (i == 2)
1541 continue; /* skip DP-DDR */
1542#endif
1543 ram_start = gd->bd->bi_dram[i].start;
1544 ram_size = gd->bd->bi_dram[i].size;
1545#ifdef CONFIG_RESV_RAM
1546 if (gd->arch.resv_ram >= ram_start &&
1547 gd->arch.resv_ram < ram_start + ram_size)
1548 ram_size = gd->arch.resv_ram - ram_start;
1549#endif
1550 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
1551 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
1552
1553 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
1554 false);
1555 }
York Sun928b6812015-12-07 11:08:58 -08001556}
York Sun4de24ef2017-03-06 09:02:28 -08001557#endif
York Sun729f2d12017-03-06 09:02:34 -08001558
1559/*
1560 * Before DDR size is known, early MMU table have DDR mapped as device memory
1561 * to avoid speculative access. To relocate U-Boot to DDR, "normal memory"
1562 * needs to be set for these mappings.
1563 * If a special case configures DDR with holes in the mapping, the holes need
1564 * to be marked as invalid. This is not implemented in this function.
1565 */
1566void update_early_mmu_table(void)
1567{
1568 if (!gd->arch.tlb_addr)
1569 return;
1570
1571 if (gd->ram_size <= CONFIG_SYS_FSL_DRAM_SIZE1) {
1572 mmu_change_region_attr(
1573 CONFIG_SYS_SDRAM_BASE,
1574 gd->ram_size,
1575 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1576 PTE_BLOCK_OUTER_SHARE |
1577 PTE_BLOCK_NS |
1578 PTE_TYPE_VALID);
1579 } else {
1580 mmu_change_region_attr(
1581 CONFIG_SYS_SDRAM_BASE,
1582 CONFIG_SYS_DDR_BLOCK1_SIZE,
1583 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1584 PTE_BLOCK_OUTER_SHARE |
1585 PTE_BLOCK_NS |
1586 PTE_TYPE_VALID);
1587#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
1588#ifndef CONFIG_SYS_DDR_BLOCK2_SIZE
1589#error "Missing CONFIG_SYS_DDR_BLOCK2_SIZE"
1590#endif
1591 if (gd->ram_size - CONFIG_SYS_DDR_BLOCK1_SIZE >
1592 CONFIG_SYS_DDR_BLOCK2_SIZE) {
1593 mmu_change_region_attr(
1594 CONFIG_SYS_DDR_BLOCK2_BASE,
1595 CONFIG_SYS_DDR_BLOCK2_SIZE,
1596 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1597 PTE_BLOCK_OUTER_SHARE |
1598 PTE_BLOCK_NS |
1599 PTE_TYPE_VALID);
1600 mmu_change_region_attr(
1601 CONFIG_SYS_DDR_BLOCK3_BASE,
1602 gd->ram_size -
1603 CONFIG_SYS_DDR_BLOCK1_SIZE -
1604 CONFIG_SYS_DDR_BLOCK2_SIZE,
1605 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1606 PTE_BLOCK_OUTER_SHARE |
1607 PTE_BLOCK_NS |
1608 PTE_TYPE_VALID);
1609 } else
1610#endif
1611 {
1612 mmu_change_region_attr(
1613 CONFIG_SYS_DDR_BLOCK2_BASE,
1614 gd->ram_size -
1615 CONFIG_SYS_DDR_BLOCK1_SIZE,
1616 PTE_BLOCK_MEMTYPE(MT_NORMAL) |
1617 PTE_BLOCK_OUTER_SHARE |
1618 PTE_BLOCK_NS |
1619 PTE_TYPE_VALID);
1620 }
1621 }
1622}
1623
1624__weak int dram_init(void)
1625{
Simon Glass0e0ac202017-04-06 12:47:04 -06001626 fsl_initdram();
Rajesh Bhagat5efbecf2018-11-05 18:01:37 +00001627#if (!defined(CONFIG_SPL) && !defined(CONFIG_TFABOOT)) || \
1628 defined(CONFIG_SPL_BUILD)
York Sun729f2d12017-03-06 09:02:34 -08001629 /* This will break-before-make MMU for DDR */
1630 update_early_mmu_table();
1631#endif
1632
1633 return 0;
1634}
Alex Marginean47568ce2020-01-11 01:05:40 +02001635
1636#ifdef CONFIG_ARCH_MISC_INIT
1637__weak int serdes_misc_init(void)
1638{
1639 return 0;
1640}
1641
1642int arch_misc_init(void)
1643{
1644 serdes_misc_init();
1645
1646 return 0;
1647}
1648#endif