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;
40 #ifdef CONFIG_CHAIN_OF_TRUST
41 uintptr_t ppa_esbc_hdr = 0;
42 uintptr_t ppa_img_addr = 0;
43 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
44 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
49 /* Skip if running at lower exception level */
51 debug("Skipping PPA init, running at EL%d\n", el);
55 #ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP
56 ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR;
57 debug("%s: PPA image load from XIP\n", __func__);
58 #ifdef CONFIG_CHAIN_OF_TRUST
59 ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR;
61 #else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */
62 size_t fw_length, fdt_header_len = sizeof(struct fdt_header);
64 /* Copy PPA image from MMC/SD/NAND to allocated memory */
65 #ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC
67 int dev = CONFIG_SYS_MMC_ENV_DEV;
68 struct fdt_header *fitp;
72 debug("%s: PPA image load from eMMC/SD\n", __func__);
74 ret = mmc_initialize(gd->bd);
76 printf("%s: mmc_initialize() failed\n", __func__);
79 mmc = find_mmc_device(dev);
81 printf("PPA: MMC cannot find device for PPA firmware\n");
87 printf("%s: mmc_init() failed\n", __func__);
91 fitp = malloc(roundup(fdt_header_len, 512));
93 printf("PPA: malloc failed for FIT header(size 0x%zx)\n",
94 roundup(fdt_header_len, 512));
98 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
99 cnt = DIV_ROUND_UP(fdt_header_len, 512);
100 debug("%s: MMC read PPA FIT header: dev # %u, block # %u, count %u\n",
101 __func__, dev, blk, cnt);
102 ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, fitp);
105 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
106 CONFIG_SYS_LS_PPA_FW_ADDR);
110 ret = fdt_check_header(fitp);
113 printf("%s: fdt_check_header() failed\n", __func__);
117 #ifdef CONFIG_CHAIN_OF_TRUST
118 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
120 printf("PPA: malloc failed for PPA header\n");
124 blk = CONFIG_SYS_LS_PPA_ESBC_ADDR >> 9;
125 cnt = DIV_ROUND_UP(CONFIG_LS_PPA_ESBC_HDR_SIZE, 512);
126 ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, ppa_hdr_ddr);
129 printf("MMC/SD read of PPA header failed\n");
132 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
134 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
137 fw_length = fdt_totalsize(fitp);
140 fw_length = roundup(fw_length, 512);
141 ppa_fit_addr = malloc(fw_length);
143 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
148 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
149 cnt = DIV_ROUND_UP(fw_length, 512);
150 debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n",
151 __func__, dev, blk, cnt);
152 ret = mmc->block_dev.block_read(&mmc->block_dev,
153 blk, cnt, ppa_fit_addr);
156 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
157 CONFIG_SYS_LS_PPA_FW_ADDR);
161 #elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
162 struct fdt_header fit;
164 debug("%s: PPA image load from NAND\n", __func__);
167 ret = nand_read(get_nand_dev_by_index(0),
168 (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
169 &fdt_header_len, (u_char *)&fit);
170 if (ret == -EUCLEAN) {
171 printf("NAND read of PPA FIT header at offset 0x%x failed\n",
172 CONFIG_SYS_LS_PPA_FW_ADDR);
176 ret = fdt_check_header(&fit);
178 printf("%s: fdt_check_header() failed\n", __func__);
182 #ifdef CONFIG_CHAIN_OF_TRUST
183 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
185 printf("PPA: malloc failed for PPA header\n");
189 fw_length = CONFIG_LS_PPA_ESBC_HDR_SIZE;
191 ret = nand_read(get_nand_dev_by_index(0),
192 (loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR,
193 &fw_length, (u_char *)ppa_hdr_ddr);
194 if (ret == -EUCLEAN) {
196 printf("NAND read of PPA firmware at offset 0x%x failed\n",
197 CONFIG_SYS_LS_PPA_FW_ADDR);
200 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
202 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
205 fw_length = fdt_totalsize(&fit);
207 ppa_fit_addr = malloc(fw_length);
209 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
214 ret = nand_read(get_nand_dev_by_index(0),
215 (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
216 &fw_length, (u_char *)ppa_fit_addr);
217 if (ret == -EUCLEAN) {
219 printf("NAND read of PPA firmware at offset 0x%x failed\n",
220 CONFIG_SYS_LS_PPA_FW_ADDR);
224 #error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined"
229 #ifdef CONFIG_CHAIN_OF_TRUST
230 ppa_img_addr = (uintptr_t)ppa_fit_addr;
231 if (fsl_check_boot_mode_secure() != 0) {
233 * In case of failure in validation, fsl_secboot_validate
234 * would not return back in case of Production environment
235 * with ITS=1. In Development environment (ITS=0 and
236 * SB_EN=1), the function may return back in case of
237 * non-fatal failures.
239 ret = fsl_secboot_validate(ppa_esbc_hdr,
243 printf("PPA validation failed\n");
245 printf("PPA validation Successful\n");
247 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
248 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
253 #ifdef CONFIG_FSL_LSCH3
254 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
255 boot_loc_ptr_l = &gur->bootlocptrl;
256 boot_loc_ptr_h = &gur->bootlocptrh;
257 #elif defined(CONFIG_FSL_LSCH2)
258 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
259 boot_loc_ptr_l = &scfg->scratchrw[1];
260 boot_loc_ptr_h = &scfg->scratchrw[0];
263 debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n",
264 boot_loc_ptr_l, boot_loc_ptr_h);
265 ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h);
267 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
268 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)