blob: d0be335b2ff7b090d5e58e6865aadf7f924d6bfc [file] [log] [blame]
Mingkai Hu0e58b512015-10-26 19:47:50 +08001/*
2 * Copyright 2014-2015 Freescale Semiconductor, Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +09009#include <linux/errno.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080010#include <asm/system.h>
11#include <asm/armv8/mmu.h>
12#include <asm/io.h>
13#include <asm/arch/fsl_serdes.h>
14#include <asm/arch/soc.h>
15#include <asm/arch/cpu.h>
16#include <asm/arch/speed.h>
17#ifdef CONFIG_MP
18#include <asm/arch/mp.h>
19#endif
Alexander Graf12be31c2016-11-17 01:03:01 +010020#include <efi_loader.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080021#include <fm_eth.h>
Mingkai Hu0e58b512015-10-26 19:47:50 +080022#include <fsl-mc/fsl_mc.h>
23#ifdef CONFIG_FSL_ESDHC
24#include <fsl_esdhc.h>
25#endif
Hou Zhiqiang21c4d552016-06-28 20:18:15 +080026#ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
27#include <asm/armv8/sec_firmware.h>
28#endif
Shengzhou Liu15875a52016-11-21 11:36:48 +080029#ifdef CONFIG_SYS_FSL_DDR
30#include <fsl_ddr.h>
31#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +080032
33DECLARE_GLOBAL_DATA_PTR;
34
York Sun9da8f502016-06-24 16:46:23 -070035struct mm_region *mem_map = early_map;
Alexander Grafce0a64e2016-03-04 01:09:54 +010036
Mingkai Hu0e58b512015-10-26 19:47:50 +080037void cpu_name(char *name)
38{
39 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
40 unsigned int i, svr, ver;
41
42 svr = gur_in32(&gur->svr);
43 ver = SVR_SOC_VER(svr);
44
45 for (i = 0; i < ARRAY_SIZE(cpu_type_list); i++)
46 if ((cpu_type_list[i].soc_ver & SVR_WO_E) == ver) {
47 strcpy(name, cpu_type_list[i].name);
48
49 if (IS_E_PROCESSOR(svr))
50 strcat(name, "E");
Wenbin Song863a33a2016-09-13 16:13:54 +080051
52 sprintf(name + strlen(name), " Rev%d.%d",
53 SVR_MAJ(svr), SVR_MIN(svr));
Mingkai Hu0e58b512015-10-26 19:47:50 +080054 break;
55 }
56
57 if (i == ARRAY_SIZE(cpu_type_list))
58 strcpy(name, "unknown");
59}
60
61#ifndef CONFIG_SYS_DCACHE_OFF
Mingkai Hu0e58b512015-10-26 19:47:50 +080062/*
63 * To start MMU before DDR is available, we create MMU table in SRAM.
64 * The base address of SRAM is CONFIG_SYS_FSL_OCRAM_BASE. We use three
65 * levels of translation tables here to cover 40-bit address space.
66 * We use 4KB granule size, with 40 bits physical address, T0SZ=24
York Sun9da8f502016-06-24 16:46:23 -070067 * Address above EARLY_PGTABLE_SIZE (0x5000) is free for other purpose.
68 * Note, the debug print in cache_v8.c is not usable for debugging
69 * these early MMU tables because UART is not yet available.
Mingkai Hu0e58b512015-10-26 19:47:50 +080070 */
71static inline void early_mmu_setup(void)
72{
York Sun9da8f502016-06-24 16:46:23 -070073 unsigned int el = current_el();
Mingkai Hu0e58b512015-10-26 19:47:50 +080074
York Sun9da8f502016-06-24 16:46:23 -070075 /* global data is already setup, no allocation yet */
76 gd->arch.tlb_addr = CONFIG_SYS_FSL_OCRAM_BASE;
77 gd->arch.tlb_fillptr = gd->arch.tlb_addr;
78 gd->arch.tlb_size = EARLY_PGTABLE_SIZE;
Mingkai Hu0e58b512015-10-26 19:47:50 +080079
York Sun9da8f502016-06-24 16:46:23 -070080 /* Create early page tables */
81 setup_pgtables();
Mingkai Hu0e58b512015-10-26 19:47:50 +080082
York Sun9da8f502016-06-24 16:46:23 -070083 /* point TTBR to the new table */
84 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
85 get_tcr(el, NULL, NULL) &
86 ~(TCR_ORGN_MASK | TCR_IRGN_MASK),
Mingkai Hu0e58b512015-10-26 19:47:50 +080087 MEMORY_ATTRIBUTES);
York Sun9da8f502016-06-24 16:46:23 -070088
Mingkai Hu0e58b512015-10-26 19:47:50 +080089 set_sctlr(get_sctlr() | CR_M);
90}
91
92/*
93 * The final tables look similar to early tables, but different in detail.
94 * These tables are in DRAM. Sub tables are added to enable cache for
95 * QBMan and OCRAM.
96 *
York Sun1ef95cc2016-06-24 16:46:18 -070097 * Put the MMU table in secure memory if gd->arch.secure_ram is valid.
98 * OCRAM will be not used for this purpose so gd->arch.secure_ram can't be 0.
Mingkai Hu0e58b512015-10-26 19:47:50 +080099 */
100static inline void final_mmu_setup(void)
101{
York Sun9da8f502016-06-24 16:46:23 -0700102 u64 tlb_addr_save = gd->arch.tlb_addr;
York Sun0804d562015-12-04 11:57:08 -0800103 unsigned int el = current_el();
York Sun0804d562015-12-04 11:57:08 -0800104#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
York Sun9da8f502016-06-24 16:46:23 -0700105 int index;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800106#endif
107
York Sun9da8f502016-06-24 16:46:23 -0700108 mem_map = final_map;
Mingkai Hu0e58b512015-10-26 19:47:50 +0800109
York Sun0804d562015-12-04 11:57:08 -0800110#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
York Sun9da8f502016-06-24 16:46:23 -0700111 if (gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED) {
112 if (el == 3) {
113 /*
114 * Only use gd->arch.secure_ram if the address is
115 * recalculated. Align to 4KB for MMU table.
116 */
117 /* put page tables in secure ram */
118 index = ARRAY_SIZE(final_map) - 2;
119 gd->arch.tlb_addr = gd->arch.secure_ram & ~0xfff;
120 final_map[index].virt = gd->arch.secure_ram & ~0x3;
121 final_map[index].phys = final_map[index].virt;
122 final_map[index].size = CONFIG_SYS_MEM_RESERVE_SECURE;
123 final_map[index].attrs = PTE_BLOCK_OUTER_SHARE;
York Sun1ef95cc2016-06-24 16:46:18 -0700124 gd->arch.secure_ram |= MEM_RESERVE_SECURE_SECURED;
York Sun9da8f502016-06-24 16:46:23 -0700125 tlb_addr_save = gd->arch.tlb_addr;
York Sun0804d562015-12-04 11:57:08 -0800126 } else {
York Sun9da8f502016-06-24 16:46:23 -0700127 /* Use allocated (board_f.c) memory for TLB */
128 tlb_addr_save = gd->arch.tlb_allocated;
129 gd->arch.tlb_addr = tlb_addr_save;
York Sun0804d562015-12-04 11:57:08 -0800130 }
131 }
132#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800133
York Sun9da8f502016-06-24 16:46:23 -0700134 /* Reset the fill ptr */
135 gd->arch.tlb_fillptr = tlb_addr_save;
136
137 /* Create normal system page tables */
138 setup_pgtables();
139
140 /* Create emergency page tables */
141 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
142 gd->arch.tlb_emerg = gd->arch.tlb_addr;
143 setup_pgtables();
144 gd->arch.tlb_addr = tlb_addr_save;
145
Mingkai Hu0e58b512015-10-26 19:47:50 +0800146 /* flush new MMU table */
York Sun9da8f502016-06-24 16:46:23 -0700147 flush_dcache_range(gd->arch.tlb_addr,
148 gd->arch.tlb_addr + gd->arch.tlb_size);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800149
150 /* point TTBR to the new table */
York Sun9da8f502016-06-24 16:46:23 -0700151 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
Mingkai Hu0e58b512015-10-26 19:47:50 +0800152 MEMORY_ATTRIBUTES);
153 /*
York Suneb6eac12016-07-22 10:52:23 -0700154 * EL3 MMU is already enabled, just need to invalidate TLB to load the
Mingkai Hu0e58b512015-10-26 19:47:50 +0800155 * new table. The new table is compatible with the current table, if
156 * MMU somehow walks through the new table before invalidation TLB,
157 * it still works. So we don't need to turn off MMU here.
York Suneb6eac12016-07-22 10:52:23 -0700158 * When EL2 MMU table is created by calling this function, MMU needs
159 * to be enabled.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800160 */
York Suneb6eac12016-07-22 10:52:23 -0700161 set_sctlr(get_sctlr() | CR_M);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800162}
163
Alexander Grafbc78b922016-03-21 20:26:12 +0100164u64 get_page_table_size(void)
165{
166 return 0x10000;
167}
168
Mingkai Hu0e58b512015-10-26 19:47:50 +0800169int arch_cpu_init(void)
170{
171 icache_enable();
172 __asm_invalidate_dcache_all();
173 __asm_invalidate_tlb_all();
174 early_mmu_setup();
175 set_sctlr(get_sctlr() | CR_C);
176 return 0;
177}
178
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800179void mmu_setup(void)
180{
181 final_mmu_setup();
182}
183
Mingkai Hu0e58b512015-10-26 19:47:50 +0800184/*
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800185 * This function is called from common/board_r.c.
186 * It recreates MMU table in main memory.
Mingkai Hu0e58b512015-10-26 19:47:50 +0800187 */
188void enable_caches(void)
189{
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800190 mmu_setup();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800191 __asm_invalidate_tlb_all();
Hou Zhiqianga7befa52016-06-28 20:18:12 +0800192 icache_enable();
193 dcache_enable();
Mingkai Hu0e58b512015-10-26 19:47:50 +0800194}
195#endif
196
Priyanka Jain9a276702016-11-17 12:29:56 +0530197u32 initiator_type(u32 cluster, int init_id)
Mingkai Hu0e58b512015-10-26 19:47:50 +0800198{
199 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
200 u32 idx = (cluster >> (init_id * 8)) & TP_CLUSTER_INIT_MASK;
201 u32 type = 0;
202
203 type = gur_in32(&gur->tp_ityp[idx]);
204 if (type & TP_ITYP_AV)
205 return type;
206
207 return 0;
208}
209
York Suned7fbe32016-09-13 12:40:30 -0700210u32 cpu_pos_mask(void)
211{
212 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
213 int i = 0;
214 u32 cluster, type, mask = 0;
215
216 do {
217 int j;
218
219 cluster = gur_in32(&gur->tp_cluster[i].lower);
220 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
221 type = initiator_type(cluster, j);
222 if (type && (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM))
223 mask |= 1 << (i * TP_INIT_PER_CLUSTER + j);
224 }
225 i++;
226 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
227
228 return mask;
229}
230
Mingkai Hu0e58b512015-10-26 19:47:50 +0800231u32 cpu_mask(void)
232{
233 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
234 int i = 0, count = 0;
235 u32 cluster, type, mask = 0;
236
237 do {
238 int j;
239
240 cluster = gur_in32(&gur->tp_cluster[i].lower);
241 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
242 type = initiator_type(cluster, j);
243 if (type) {
244 if (TP_ITYP_TYPE(type) == TP_ITYP_TYPE_ARM)
245 mask |= 1 << count;
246 count++;
247 }
248 }
249 i++;
250 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
251
252 return mask;
253}
254
255/*
256 * Return the number of cores on this SOC.
257 */
258int cpu_numcores(void)
259{
260 return hweight32(cpu_mask());
261}
262
263int fsl_qoriq_core_to_cluster(unsigned int core)
264{
265 struct ccsr_gur __iomem *gur =
266 (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
267 int i = 0, count = 0;
268 u32 cluster;
269
270 do {
271 int j;
272
273 cluster = gur_in32(&gur->tp_cluster[i].lower);
274 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
275 if (initiator_type(cluster, j)) {
276 if (count == core)
277 return i;
278 count++;
279 }
280 }
281 i++;
282 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
283
284 return -1; /* cannot identify the cluster */
285}
286
287u32 fsl_qoriq_core_to_type(unsigned int core)
288{
289 struct ccsr_gur __iomem *gur =
290 (void __iomem *)(CONFIG_SYS_FSL_GUTS_ADDR);
291 int i = 0, count = 0;
292 u32 cluster, type;
293
294 do {
295 int j;
296
297 cluster = gur_in32(&gur->tp_cluster[i].lower);
298 for (j = 0; j < TP_INIT_PER_CLUSTER; j++) {
299 type = initiator_type(cluster, j);
300 if (type) {
301 if (count == core)
302 return type;
303 count++;
304 }
305 }
306 i++;
307 } while ((cluster & TP_CLUSTER_EOC) == 0x0);
308
309 return -1; /* cannot identify the cluster */
310}
311
Priyanka Jain96b001f2016-11-17 12:29:51 +0530312#ifndef CONFIG_FSL_LSCH3
Sriram Dash9282d262016-06-13 09:58:32 +0530313uint get_svr(void)
314{
315 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
316
317 return gur_in32(&gur->svr);
318}
Priyanka Jain96b001f2016-11-17 12:29:51 +0530319#endif
Sriram Dash9282d262016-06-13 09:58:32 +0530320
Mingkai Hu0e58b512015-10-26 19:47:50 +0800321#ifdef CONFIG_DISPLAY_CPUINFO
322int print_cpuinfo(void)
323{
324 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
325 struct sys_info sysinfo;
326 char buf[32];
327 unsigned int i, core;
York Suncbe8e1c2016-04-04 11:41:26 -0700328 u32 type, rcw, svr = gur_in32(&gur->svr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800329
330 puts("SoC: ");
331
332 cpu_name(buf);
York Suncbe8e1c2016-04-04 11:41:26 -0700333 printf(" %s (0x%x)\n", buf, svr);
Mingkai Hu0e58b512015-10-26 19:47:50 +0800334 memset((u8 *)buf, 0x00, ARRAY_SIZE(buf));
335 get_sys_info(&sysinfo);
336 puts("Clock Configuration:");
337 for_each_cpu(i, core, cpu_numcores(), cpu_mask()) {
338 if (!(i % 3))
339 puts("\n ");
340 type = TP_ITYP_VER(fsl_qoriq_core_to_type(core));
341 printf("CPU%d(%s):%-4s MHz ", core,
342 type == TY_ITYP_VER_A7 ? "A7 " :
343 (type == TY_ITYP_VER_A53 ? "A53" :
Alison Wang79808392016-07-05 16:01:52 +0800344 (type == TY_ITYP_VER_A57 ? "A57" :
345 (type == TY_ITYP_VER_A72 ? "A72" : " "))),
Mingkai Hu0e58b512015-10-26 19:47:50 +0800346 strmhz(buf, sysinfo.freq_processor[core]));
347 }
Hou Zhiqiang3f91cda2017-01-10 16:44:15 +0800348 /* Display platform clock as Bus frequency. */
Mingkai Hu0e58b512015-10-26 19:47:50 +0800349 printf("\n Bus: %-4s MHz ",
Hou Zhiqiang3f91cda2017-01-10 16:44:15 +0800350 strmhz(buf, sysinfo.freq_systembus / CONFIG_SYS_FSL_PCLK_DIV));
Mingkai Hu0e58b512015-10-26 19:47:50 +0800351 printf("DDR: %-4s MT/s", strmhz(buf, sysinfo.freq_ddrbus));
Shaohui Xie04643262015-10-26 19:47:54 +0800352#ifdef CONFIG_SYS_DPAA_FMAN
353 printf(" FMAN: %-4s MHz", strmhz(buf, sysinfo.freq_fman[0]));
354#endif
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +0530355#ifdef CONFIG_SYS_FSL_HAS_DP_DDR
York Suncbe8e1c2016-04-04 11:41:26 -0700356 if (soc_has_dp_ddr()) {
357 printf(" DP-DDR: %-4s MT/s",
358 strmhz(buf, sysinfo.freq_ddrbus2));
359 }
Mingkai Hu0e58b512015-10-26 19:47:50 +0800360#endif
361 puts("\n");
362
363 /*
364 * Display the RCW, so that no one gets confused as to what RCW
365 * we're actually using for this boot.
366 */
367 puts("Reset Configuration Word (RCW):");
368 for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
369 rcw = gur_in32(&gur->rcwsr[i]);
370 if ((i % 4) == 0)
371 printf("\n %08x:", i * 4);
372 printf(" %08x", rcw);
373 }
374 puts("\n");
375
376 return 0;
377}
378#endif
379
380#ifdef CONFIG_FSL_ESDHC
381int cpu_mmc_init(bd_t *bis)
382{
383 return fsl_esdhc_mmc_init(bis);
384}
385#endif
386
387int cpu_eth_init(bd_t *bis)
388{
389 int error = 0;
390
391#ifdef CONFIG_FSL_MC_ENET
392 error = fsl_mc_ldpaa_init(bis);
393#endif
Shaohui Xie04643262015-10-26 19:47:54 +0800394#ifdef CONFIG_FMAN_ENET
395 fm_standard_init(bis);
396#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800397 return error;
398}
399
400int arch_early_init_r(void)
401{
402#ifdef CONFIG_MP
403 int rv = 1;
Hou Zhiqiang21c4d552016-06-28 20:18:15 +0800404 u32 psci_ver = 0xffffffff;
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +0530405#endif
406
407#ifdef CONFIG_SYS_FSL_ERRATUM_A009635
408 erratum_a009635();
409#endif
Shengzhou Liu15875a52016-11-21 11:36:48 +0800410#if defined(CONFIG_SYS_FSL_ERRATUM_A009942) && defined(CONFIG_SYS_FSL_DDR)
411 erratum_a009942_check_cpo();
412#endif
Prabhakar Kushwaha22cfe962015-11-05 12:00:14 +0530413#ifdef CONFIG_MP
macro.wave.z@gmail.comec2d7ed2016-12-08 11:58:21 +0800414#if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT) && \
Hou Zhiqiang6be115d2017-01-16 17:31:48 +0800415 defined(CONFIG_SEC_FIRMWARE_ARMV8_PSCI)
Hou Zhiqiang21c4d552016-06-28 20:18:15 +0800416 /* Check the psci version to determine if the psci is supported */
417 psci_ver = sec_firmware_support_psci_version();
418#endif
419 if (psci_ver == 0xffffffff) {
420 rv = fsl_layerscape_wake_seconday_cores();
421 if (rv)
422 printf("Did not wake secondary cores\n");
423 }
Mingkai Hu0e58b512015-10-26 19:47:50 +0800424#endif
425
426#ifdef CONFIG_SYS_HAS_SERDES
427 fsl_serdes_init();
428#endif
Shaohui Xie04643262015-10-26 19:47:54 +0800429#ifdef CONFIG_FMAN_ENET
430 fman_enet_init();
431#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800432 return 0;
433}
434
435int timer_init(void)
436{
437 u32 __iomem *cntcr = (u32 *)CONFIG_SYS_FSL_TIMER_ADDR;
438#ifdef CONFIG_FSL_LSCH3
439 u32 __iomem *cltbenr = (u32 *)CONFIG_SYS_FSL_PMU_CLTBENR;
440#endif
Yunhui Cui3dfb82a2016-06-08 10:31:42 +0800441#ifdef CONFIG_LS2080A
442 u32 __iomem *pctbenr = (u32 *)FSL_PMU_PCTBENR_OFFSET;
Priyanka Jain3d31ec72016-11-17 12:29:52 +0530443 u32 svr_dev_id;
Yunhui Cui3dfb82a2016-06-08 10:31:42 +0800444#endif
Mingkai Hu0e58b512015-10-26 19:47:50 +0800445#ifdef COUNTER_FREQUENCY_REAL
446 unsigned long cntfrq = COUNTER_FREQUENCY_REAL;
447
448 /* Update with accurate clock frequency */
449 asm volatile("msr cntfrq_el0, %0" : : "r" (cntfrq) : "memory");
450#endif
451
452#ifdef CONFIG_FSL_LSCH3
453 /* Enable timebase for all clusters.
454 * It is safe to do so even some clusters are not enabled.
455 */
456 out_le32(cltbenr, 0xf);
457#endif
458
Yunhui Cui3dfb82a2016-06-08 10:31:42 +0800459#ifdef CONFIG_LS2080A
460 /*
461 * In certain Layerscape SoCs, the clock for each core's
462 * has an enable bit in the PMU Physical Core Time Base Enable
463 * Register (PCTBENR), which allows the watchdog to operate.
464 */
465 setbits_le32(pctbenr, 0xff);
Priyanka Jain3d31ec72016-11-17 12:29:52 +0530466 /*
467 * For LS2080A SoC and its personalities, timer controller
468 * offset is different
469 */
470 svr_dev_id = get_svr() >> 16;
471 if (svr_dev_id == SVR_DEV_LS2080A)
472 cntcr = (u32 *)SYS_FSL_LS2080A_LS2085A_TIMER_ADDR;
473
Yunhui Cui3dfb82a2016-06-08 10:31:42 +0800474#endif
475
Mingkai Hu0e58b512015-10-26 19:47:50 +0800476 /* Enable clock for timer
477 * This is a global setting.
478 */
479 out_le32(cntcr, 0x1);
480
481 return 0;
482}
483
Alexander Graf12be31c2016-11-17 01:03:01 +0100484__efi_runtime_data u32 __iomem *rstcr = (u32 *)CONFIG_SYS_FSL_RST_ADDR;
485
486void __efi_runtime reset_cpu(ulong addr)
Mingkai Hu0e58b512015-10-26 19:47:50 +0800487{
Mingkai Hu0e58b512015-10-26 19:47:50 +0800488 u32 val;
489
490 /* Raise RESET_REQ_B */
491 val = scfg_in32(rstcr);
492 val |= 0x02;
493 scfg_out32(rstcr, val);
494}
York Sun928b6812015-12-07 11:08:58 -0800495
Alexander Graf12be31c2016-11-17 01:03:01 +0100496#ifdef CONFIG_EFI_LOADER
497
498void __efi_runtime EFIAPI efi_reset_system(
499 enum efi_reset_type reset_type,
500 efi_status_t reset_status,
501 unsigned long data_size, void *reset_data)
502{
503 switch (reset_type) {
504 case EFI_RESET_COLD:
505 case EFI_RESET_WARM:
506 reset_cpu(0);
507 break;
508 case EFI_RESET_SHUTDOWN:
509 /* Nothing we can do */
510 break;
511 }
512
513 while (1) { }
514}
515
516void efi_reset_system_init(void)
517{
518 efi_add_runtime_mmio(&rstcr, sizeof(*rstcr));
519}
520
521#endif
522
York Sun928b6812015-12-07 11:08:58 -0800523phys_size_t board_reserve_ram_top(phys_size_t ram_size)
524{
525 phys_size_t ram_top = ram_size;
526
York Sun928b6812015-12-07 11:08:58 -0800527#ifdef CONFIG_FSL_MC_ENET
York Sun4de24ef2017-03-06 09:02:28 -0800528 /* The start address of MC reserved memory needs to be aligned. */
York Sun928b6812015-12-07 11:08:58 -0800529 ram_top -= mc_get_dram_block_size();
530 ram_top &= ~(CONFIG_SYS_MC_RSV_MEM_ALIGN - 1);
531#endif
York Sun4de24ef2017-03-06 09:02:28 -0800532
533 return ram_size - ram_top;
534}
535
536phys_size_t get_effective_memsize(void)
537{
538 phys_size_t ea_size, rem = 0;
539
540 /*
541 * For ARMv8 SoCs, DDR memory is split into two or three regions. The
542 * first region is 2GB space at 0x8000_0000. If the memory extends to
543 * the second region (or the third region if applicable), the secure
544 * memory and Management Complex (MC) memory should be put into the
545 * highest region, i.e. the end of DDR memory. CONFIG_MAX_MEM_MAPPED
546 * is set to the size of first region so U-Boot doesn't relocate itself
547 * into higher address. Should DDR be configured to skip the first
548 * region, this function needs to be adjusted.
549 */
550 if (gd->ram_size > CONFIG_MAX_MEM_MAPPED) {
551 ea_size = CONFIG_MAX_MEM_MAPPED;
552 rem = gd->ram_size - ea_size;
553 } else {
554 ea_size = gd->ram_size;
555 }
556
557#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
558 /* Check if we have enough space for secure memory */
559 if (rem > CONFIG_SYS_MEM_RESERVE_SECURE) {
560 rem -= CONFIG_SYS_MEM_RESERVE_SECURE;
561 } else {
562 if (ea_size > CONFIG_SYS_MEM_RESERVE_SECURE) {
563 ea_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
564 rem = 0; /* Presume MC requires more memory */
565 } else {
566 printf("Error: No enough space for secure memory.\n");
567 }
568 }
569#endif
570 /* Check if we have enough memory for MC */
571 if (rem < board_reserve_ram_top(rem)) {
572 /* Not enough memory in high region to reserve */
573 if (ea_size > board_reserve_ram_top(rem))
574 ea_size -= board_reserve_ram_top(rem);
575 else
576 printf("Error: No enough space for reserved memory.\n");
577 }
578
579 return ea_size;
580}
581
582void dram_init_banksize(void)
583{
584#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
585 phys_size_t dp_ddr_size;
586#endif
587
588 /*
589 * gd->ram_size has the total size of DDR memory, less reserved secure
590 * memory. The DDR extends from low region to high region(s) presuming
591 * no hole is created with DDR configuration. gd->arch.secure_ram tracks
592 * the location of secure memory. gd->arch.resv_ram tracks the location
593 * of reserved memory for Management Complex (MC).
594 */
595 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
596 if (gd->ram_size > CONFIG_SYS_DDR_BLOCK1_SIZE) {
597 gd->bd->bi_dram[0].size = CONFIG_SYS_DDR_BLOCK1_SIZE;
598 gd->bd->bi_dram[1].start = CONFIG_SYS_DDR_BLOCK2_BASE;
599 gd->bd->bi_dram[1].size = gd->ram_size -
600 CONFIG_SYS_DDR_BLOCK1_SIZE;
601#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
602 if (gd->bi_dram[1].size > CONFIG_SYS_DDR_BLOCK2_SIZE) {
603 gd->bd->bi_dram[2].start = CONFIG_SYS_DDR_BLOCK3_BASE;
604 gd->bd->bi_dram[2].size = gd->bd->bi_dram[1].size -
605 CONFIG_SYS_DDR_BLOCK2_SIZE;
606 gd->bd->bi_dram[1].size = CONFIG_SYS_DDR_BLOCK2_SIZE;
607 }
608#endif
609 } else {
610 gd->bd->bi_dram[0].size = gd->ram_size;
611 }
612#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
613#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
614 if (gd->bd->bi_dram[2].size >= CONFIG_SYS_MEM_RESERVE_SECURE) {
615 gd->bd->bi_dram[2].size -= CONFIG_SYS_MEM_RESERVE_SECURE;
616 gd->arch.secure_ram = gd->bd->bi_dram[2].start +
617 gd->bd->bi_dram[2].size;
618 gd->arch.secure_ram |= MEM_RESERVE_SECURE_MAINTAINED;
619 gd->ram_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
620 } else
621#endif
622 {
623 if (gd->bd->bi_dram[1].size >= CONFIG_SYS_MEM_RESERVE_SECURE) {
624 gd->bd->bi_dram[1].size -=
625 CONFIG_SYS_MEM_RESERVE_SECURE;
626 gd->arch.secure_ram = gd->bd->bi_dram[1].start +
627 gd->bd->bi_dram[1].size;
628 gd->arch.secure_ram |= MEM_RESERVE_SECURE_MAINTAINED;
629 gd->ram_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
630 } else if (gd->bd->bi_dram[0].size >
631 CONFIG_SYS_MEM_RESERVE_SECURE) {
632 gd->bd->bi_dram[0].size -=
633 CONFIG_SYS_MEM_RESERVE_SECURE;
634 gd->arch.secure_ram = gd->bd->bi_dram[0].start +
635 gd->bd->bi_dram[0].size;
636 gd->arch.secure_ram |= MEM_RESERVE_SECURE_MAINTAINED;
637 gd->ram_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
638 }
639 }
640#endif /* CONFIG_SYS_MEM_RESERVE_SECURE */
641
642#ifdef CONFIG_FSL_MC_ENET
643 /* Assign memory for MC */
644#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
645 if (gd->bd->bi_dram[2].size >=
646 board_reserve_ram_top(gd->bd->bi_dram[2].size)) {
647 gd->arch.resv_ram = gd->bd->bi_dram[2].start +
648 gd->bd->bi_dram[2].size -
649 board_reserve_ram_top(gd->bd->bi_dram[2].size);
650 } else
651#endif
652 {
653 if (gd->bd->bi_dram[1].size >=
654 board_reserve_ram_top(gd->bd->bi_dram[1].size)) {
655 gd->arch.resv_ram = gd->bd->bi_dram[1].start +
656 gd->bd->bi_dram[1].size -
657 board_reserve_ram_top(gd->bd->bi_dram[1].size);
658 } else if (gd->bd->bi_dram[0].size >
659 board_reserve_ram_top(gd->bd->bi_dram[0].size)) {
660 gd->arch.resv_ram = gd->bd->bi_dram[0].start +
661 gd->bd->bi_dram[0].size -
662 board_reserve_ram_top(gd->bd->bi_dram[0].size);
663 }
664 }
665#endif /* CONFIG_FSL_MC_ENET */
666
667#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
668#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
669#error "This SoC shouldn't have DP DDR"
670#endif
671 if (soc_has_dp_ddr()) {
672 /* initialize DP-DDR here */
673 puts("DP-DDR: ");
674 /*
675 * DDR controller use 0 as the base address for binding.
676 * It is mapped to CONFIG_SYS_DP_DDR_BASE for core to access.
677 */
678 dp_ddr_size = fsl_other_ddr_sdram(CONFIG_SYS_DP_DDR_BASE_PHY,
679 CONFIG_DP_DDR_CTRL,
680 CONFIG_DP_DDR_NUM_CTRLS,
681 CONFIG_DP_DDR_DIMM_SLOTS_PER_CTLR,
682 NULL, NULL, NULL);
683 if (dp_ddr_size) {
684 gd->bd->bi_dram[2].start = CONFIG_SYS_DP_DDR_BASE;
685 gd->bd->bi_dram[2].size = dp_ddr_size;
686 } else {
687 puts("Not detected");
688 }
689 }
690#endif
691}
692
693#if defined(CONFIG_EFI_LOADER) && !defined(CONFIG_SPL_BUILD)
694void efi_add_known_memory(void)
695{
696 int i;
697 phys_addr_t ram_start, start;
698 phys_size_t ram_size;
699 u64 pages;
York Sun928b6812015-12-07 11:08:58 -0800700
York Sun4de24ef2017-03-06 09:02:28 -0800701 /* Add RAM */
702 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
703#ifdef CONFIG_SYS_DP_DDR_BASE_PHY
704#ifdef CONFIG_SYS_DDR_BLOCK3_BASE
705#error "This SoC shouldn't have DP DDR"
706#endif
707 if (i == 2)
708 continue; /* skip DP-DDR */
709#endif
710 ram_start = gd->bd->bi_dram[i].start;
711 ram_size = gd->bd->bi_dram[i].size;
712#ifdef CONFIG_RESV_RAM
713 if (gd->arch.resv_ram >= ram_start &&
714 gd->arch.resv_ram < ram_start + ram_size)
715 ram_size = gd->arch.resv_ram - ram_start;
716#endif
717 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
718 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
719
720 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
721 false);
722 }
York Sun928b6812015-12-07 11:08:58 -0800723}
York Sun4de24ef2017-03-06 09:02:28 -0800724#endif