1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2016 NXP Semiconductor, Inc.
10 #include <asm/cache.h>
11 #include <asm/global_data.h>
12 #include <asm/system.h>
13 #include <asm/types.h>
14 #include <asm/arch/soc.h>
15 #ifdef CONFIG_FSL_LSCH3
16 #include <asm/arch/immap_lsch3.h>
17 #elif defined(CONFIG_FSL_LSCH2)
18 #include <asm/arch/immap_lsch2.h>
20 #if CONFIG_IS_ENABLED(ARMV8_SEC_FIRMWARE_SUPPORT)
21 #include <asm/armv8/sec_firmware.h>
23 #ifdef CONFIG_CHAIN_OF_TRUST
24 #include <fsl_validate.h>
27 #ifdef CONFIG_SYS_LS_PPA_FW_IN_NAND
29 #elif defined(CONFIG_SYS_LS_PPA_FW_IN_MMC)
33 DECLARE_GLOBAL_DATA_PTR;
37 unsigned int el = current_el();
39 u32 *boot_loc_ptr_l, *boot_loc_ptr_h;
40 u32 *loadable_l, *loadable_h;
43 #ifdef CONFIG_CHAIN_OF_TRUST
44 uintptr_t ppa_esbc_hdr = 0;
45 uintptr_t ppa_img_addr = 0;
46 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
47 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
52 /* Skip if running at lower exception level */
54 debug("Skipping PPA init, running at EL%d\n", el);
58 #ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP
59 ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR;
60 debug("%s: PPA image load from XIP\n", __func__);
61 #ifdef CONFIG_CHAIN_OF_TRUST
62 ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR;
64 #else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */
65 size_t fw_length, fdt_header_len = sizeof(struct fdt_header);
67 /* Copy PPA image from MMC/SD/NAND to allocated memory */
68 #ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC
70 int dev = CONFIG_SYS_MMC_ENV_DEV;
71 struct fdt_header *fitp;
75 debug("%s: PPA image load from eMMC/SD\n", __func__);
77 ret = mmc_initialize(gd->bd);
79 printf("%s: mmc_initialize() failed\n", __func__);
82 mmc = find_mmc_device(dev);
84 printf("PPA: MMC cannot find device for PPA firmware\n");
90 printf("%s: mmc_init() failed\n", __func__);
94 fitp = malloc(roundup(fdt_header_len, 512));
96 printf("PPA: malloc failed for FIT header(size 0x%zx)\n",
97 roundup(fdt_header_len, 512));
101 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
102 cnt = DIV_ROUND_UP(fdt_header_len, 512);
103 debug("%s: MMC read PPA FIT header: dev # %u, block # %u, count %u\n",
104 __func__, dev, blk, cnt);
105 ret = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, fitp);
108 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
109 CONFIG_SYS_LS_PPA_FW_ADDR);
113 ret = fdt_check_header(fitp);
116 printf("%s: fdt_check_header() failed\n", __func__);
120 #ifdef CONFIG_CHAIN_OF_TRUST
121 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
123 printf("PPA: malloc failed for PPA header\n");
127 blk = CONFIG_SYS_LS_PPA_ESBC_ADDR >> 9;
128 cnt = DIV_ROUND_UP(CONFIG_LS_PPA_ESBC_HDR_SIZE, 512);
129 ret = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, ppa_hdr_ddr);
132 printf("MMC/SD read of PPA header failed\n");
135 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
137 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
140 fw_length = fdt_totalsize(fitp);
143 fw_length = roundup(fw_length, 512);
144 ppa_fit_addr = malloc(fw_length);
146 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
151 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
152 cnt = DIV_ROUND_UP(fw_length, 512);
153 debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n",
154 __func__, dev, blk, cnt);
155 ret = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, ppa_fit_addr);
158 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
159 CONFIG_SYS_LS_PPA_FW_ADDR);
163 #elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
164 struct fdt_header fit;
166 debug("%s: PPA image load from NAND\n", __func__);
169 ret = nand_read(get_nand_dev_by_index(0),
170 (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
171 &fdt_header_len, (u_char *)&fit);
172 if (ret == -EUCLEAN) {
173 printf("NAND read of PPA FIT header at offset 0x%x failed\n",
174 CONFIG_SYS_LS_PPA_FW_ADDR);
178 ret = fdt_check_header(&fit);
180 printf("%s: fdt_check_header() failed\n", __func__);
184 #ifdef CONFIG_CHAIN_OF_TRUST
185 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
187 printf("PPA: malloc failed for PPA header\n");
191 fw_length = CONFIG_LS_PPA_ESBC_HDR_SIZE;
193 ret = nand_read(get_nand_dev_by_index(0),
194 (loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR,
195 &fw_length, (u_char *)ppa_hdr_ddr);
196 if (ret == -EUCLEAN) {
198 printf("NAND read of PPA firmware at offset 0x%x failed\n",
199 CONFIG_SYS_LS_PPA_FW_ADDR);
202 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
204 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
207 fw_length = fdt_totalsize(&fit);
209 ppa_fit_addr = malloc(fw_length);
211 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
216 ret = nand_read(get_nand_dev_by_index(0),
217 (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
218 &fw_length, (u_char *)ppa_fit_addr);
219 if (ret == -EUCLEAN) {
221 printf("NAND read of PPA firmware at offset 0x%x failed\n",
222 CONFIG_SYS_LS_PPA_FW_ADDR);
226 #error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined"
231 #ifdef CONFIG_CHAIN_OF_TRUST
232 ppa_img_addr = (uintptr_t)ppa_fit_addr;
233 if (fsl_check_boot_mode_secure() != 0) {
235 * In case of failure in validation, fsl_secboot_validate
236 * would not return back in case of Production environment
237 * with ITS=1. In Development environment (ITS=0 and
238 * SB_EN=1), the function may return back in case of
239 * non-fatal failures.
241 ret = fsl_secboot_validate(ppa_esbc_hdr,
245 printf("SEC firmware(s) validation failed\n");
247 printf("SEC firmware(s) validation Successful\n");
249 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
250 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
255 #ifdef CONFIG_FSL_LSCH3
256 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
257 boot_loc_ptr_l = &gur->bootlocptrl;
258 boot_loc_ptr_h = &gur->bootlocptrh;
260 /* Assign addresses to loadable ptrs */
261 loadable_l = &gur->scratchrw[4];
262 loadable_h = &gur->scratchrw[5];
263 #elif defined(CONFIG_FSL_LSCH2)
264 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
265 boot_loc_ptr_l = &scfg->scratchrw[1];
266 boot_loc_ptr_h = &scfg->scratchrw[0];
268 /* Assign addresses to loadable ptrs */
269 loadable_l = &scfg->scratchrw[2];
270 loadable_h = &scfg->scratchrw[3];
273 debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n",
274 boot_loc_ptr_l, boot_loc_ptr_h);
275 ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h,
276 loadable_l, loadable_h);
278 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
279 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)