2 * Copyright 2016 NXP Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/system.h>
11 #include <asm/types.h>
12 #include <asm/arch/soc.h>
13 #ifdef CONFIG_FSL_LSCH3
14 #include <asm/arch/immap_lsch3.h>
15 #elif defined(CONFIG_FSL_LSCH2)
16 #include <asm/arch/immap_lsch2.h>
18 #ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
19 #include <asm/armv8/sec_firmware.h>
21 #ifdef CONFIG_CHAIN_OF_TRUST
22 #include <fsl_validate.h>
25 #ifdef CONFIG_SYS_LS_PPA_FW_IN_NAND
27 #elif defined(CONFIG_SYS_LS_PPA_FW_IN_MMC)
31 DECLARE_GLOBAL_DATA_PTR
;
35 unsigned int el
= current_el();
37 u32
*boot_loc_ptr_l
, *boot_loc_ptr_h
;
38 u32
*loadable_l
, *loadable_h
;
41 #ifdef CONFIG_CHAIN_OF_TRUST
42 uintptr_t ppa_esbc_hdr
= 0;
43 uintptr_t ppa_img_addr
= 0;
44 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
45 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
50 /* Skip if running at lower exception level */
52 debug("Skipping PPA init, running at EL%d\n", el
);
56 #ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP
57 ppa_fit_addr
= (void *)CONFIG_SYS_LS_PPA_FW_ADDR
;
58 debug("%s: PPA image load from XIP\n", __func__
);
59 #ifdef CONFIG_CHAIN_OF_TRUST
60 ppa_esbc_hdr
= CONFIG_SYS_LS_PPA_ESBC_ADDR
;
62 #else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */
63 size_t fw_length
, fdt_header_len
= sizeof(struct fdt_header
);
65 /* Copy PPA image from MMC/SD/NAND to allocated memory */
66 #ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC
68 int dev
= CONFIG_SYS_MMC_ENV_DEV
;
69 struct fdt_header
*fitp
;
73 debug("%s: PPA image load from eMMC/SD\n", __func__
);
75 ret
= mmc_initialize(gd
->bd
);
77 printf("%s: mmc_initialize() failed\n", __func__
);
80 mmc
= find_mmc_device(dev
);
82 printf("PPA: MMC cannot find device for PPA firmware\n");
88 printf("%s: mmc_init() failed\n", __func__
);
92 fitp
= malloc(roundup(fdt_header_len
, 512));
94 printf("PPA: malloc failed for FIT header(size 0x%zx)\n",
95 roundup(fdt_header_len
, 512));
99 blk
= CONFIG_SYS_LS_PPA_FW_ADDR
/ 512;
100 cnt
= DIV_ROUND_UP(fdt_header_len
, 512);
101 debug("%s: MMC read PPA FIT header: dev # %u, block # %u, count %u\n",
102 __func__
, dev
, blk
, cnt
);
103 ret
= mmc
->block_dev
.block_read(&mmc
->block_dev
, blk
, cnt
, fitp
);
106 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
107 CONFIG_SYS_LS_PPA_FW_ADDR
);
111 ret
= fdt_check_header(fitp
);
114 printf("%s: fdt_check_header() failed\n", __func__
);
118 #ifdef CONFIG_CHAIN_OF_TRUST
119 ppa_hdr_ddr
= malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE
);
121 printf("PPA: malloc failed for PPA header\n");
125 blk
= CONFIG_SYS_LS_PPA_ESBC_ADDR
>> 9;
126 cnt
= DIV_ROUND_UP(CONFIG_LS_PPA_ESBC_HDR_SIZE
, 512);
127 ret
= mmc
->block_dev
.block_read(&mmc
->block_dev
, blk
, cnt
, ppa_hdr_ddr
);
130 printf("MMC/SD read of PPA header failed\n");
133 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr
);
135 ppa_esbc_hdr
= (uintptr_t)ppa_hdr_ddr
;
138 fw_length
= fdt_totalsize(fitp
);
141 fw_length
= roundup(fw_length
, 512);
142 ppa_fit_addr
= malloc(fw_length
);
144 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
149 blk
= CONFIG_SYS_LS_PPA_FW_ADDR
/ 512;
150 cnt
= DIV_ROUND_UP(fw_length
, 512);
151 debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n",
152 __func__
, dev
, blk
, cnt
);
153 ret
= mmc
->block_dev
.block_read(&mmc
->block_dev
,
154 blk
, cnt
, ppa_fit_addr
);
157 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
158 CONFIG_SYS_LS_PPA_FW_ADDR
);
162 #elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
163 struct fdt_header fit
;
165 debug("%s: PPA image load from NAND\n", __func__
);
168 ret
= nand_read(get_nand_dev_by_index(0),
169 (loff_t
)CONFIG_SYS_LS_PPA_FW_ADDR
,
170 &fdt_header_len
, (u_char
*)&fit
);
171 if (ret
== -EUCLEAN
) {
172 printf("NAND read of PPA FIT header at offset 0x%x failed\n",
173 CONFIG_SYS_LS_PPA_FW_ADDR
);
177 ret
= fdt_check_header(&fit
);
179 printf("%s: fdt_check_header() failed\n", __func__
);
183 #ifdef CONFIG_CHAIN_OF_TRUST
184 ppa_hdr_ddr
= malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE
);
186 printf("PPA: malloc failed for PPA header\n");
190 fw_length
= CONFIG_LS_PPA_ESBC_HDR_SIZE
;
192 ret
= nand_read(get_nand_dev_by_index(0),
193 (loff_t
)CONFIG_SYS_LS_PPA_ESBC_ADDR
,
194 &fw_length
, (u_char
*)ppa_hdr_ddr
);
195 if (ret
== -EUCLEAN
) {
197 printf("NAND read of PPA firmware at offset 0x%x failed\n",
198 CONFIG_SYS_LS_PPA_FW_ADDR
);
201 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr
);
203 ppa_esbc_hdr
= (uintptr_t)ppa_hdr_ddr
;
206 fw_length
= fdt_totalsize(&fit
);
208 ppa_fit_addr
= malloc(fw_length
);
210 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
215 ret
= nand_read(get_nand_dev_by_index(0),
216 (loff_t
)CONFIG_SYS_LS_PPA_FW_ADDR
,
217 &fw_length
, (u_char
*)ppa_fit_addr
);
218 if (ret
== -EUCLEAN
) {
220 printf("NAND read of PPA firmware at offset 0x%x failed\n",
221 CONFIG_SYS_LS_PPA_FW_ADDR
);
225 #error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined"
230 #ifdef CONFIG_CHAIN_OF_TRUST
231 ppa_img_addr
= (uintptr_t)ppa_fit_addr
;
232 if (fsl_check_boot_mode_secure() != 0) {
234 * In case of failure in validation, fsl_secboot_validate
235 * would not return back in case of Production environment
236 * with ITS=1. In Development environment (ITS=0 and
237 * SB_EN=1), the function may return back in case of
238 * non-fatal failures.
240 ret
= fsl_secboot_validate(ppa_esbc_hdr
,
244 printf("SEC firmware(s) validation failed\n");
246 printf("SEC firmware(s) validation Successful\n");
248 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
249 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
254 #ifdef CONFIG_FSL_LSCH3
255 struct ccsr_gur __iomem
*gur
= (void *)(CONFIG_SYS_FSL_GUTS_ADDR
);
256 boot_loc_ptr_l
= &gur
->bootlocptrl
;
257 boot_loc_ptr_h
= &gur
->bootlocptrh
;
259 /* Assign addresses to loadable ptrs */
260 loadable_l
= &gur
->scratchrw
[4];
261 loadable_h
= &gur
->scratchrw
[5];
262 #elif defined(CONFIG_FSL_LSCH2)
263 struct ccsr_scfg __iomem
*scfg
= (void *)(CONFIG_SYS_FSL_SCFG_ADDR
);
264 boot_loc_ptr_l
= &scfg
->scratchrw
[1];
265 boot_loc_ptr_h
= &scfg
->scratchrw
[0];
267 /* Assign addresses to loadable ptrs */
268 loadable_l
= &scfg
->scratchrw
[2];
269 loadable_h
= &scfg
->scratchrw
[3];
272 debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n",
273 boot_loc_ptr_l
, boot_loc_ptr_h
);
274 ret
= sec_firmware_init(ppa_fit_addr
, boot_loc_ptr_l
, boot_loc_ptr_h
,
275 loadable_l
, loadable_h
);
277 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
278 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)