blob: 35c612df05181ea7301a578cb7db30d7de202100 [file] [log] [blame]
Hou Zhiqiang00787862016-06-28 20:18:14 +08001/*
2 * Copyright 2016 NXP Semiconductor, Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6#include <common.h>
Hou Zhiqiangc697bd12017-03-17 16:12:32 +08007#include <malloc.h>
Hou Zhiqiang00787862016-06-28 20:18:14 +08008#include <config.h>
9#include <errno.h>
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>
17#endif
18#ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
19#include <asm/armv8/sec_firmware.h>
20#endif
Sumit Garge0f9e9b2016-09-01 12:56:44 -040021#ifdef CONFIG_CHAIN_OF_TRUST
22#include <fsl_validate.h>
23#endif
Hou Zhiqiang00787862016-06-28 20:18:14 +080024
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080025#ifdef CONFIG_SYS_LS_PPA_FW_IN_NAND
26#include <nand.h>
27#elif defined(CONFIG_SYS_LS_PPA_FW_IN_MMC)
28#include <mmc.h>
29#endif
30
31DECLARE_GLOBAL_DATA_PTR;
32
Hou Zhiqiang00787862016-06-28 20:18:14 +080033int ppa_init(void)
34{
York Sune6b871e2017-05-15 08:51:59 -070035 unsigned int el = current_el();
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080036 void *ppa_fit_addr;
Hou Zhiqiang00787862016-06-28 20:18:14 +080037 u32 *boot_loc_ptr_l, *boot_loc_ptr_h;
38 int ret;
39
Sumit Garge0f9e9b2016-09-01 12:56:44 -040040#ifdef CONFIG_CHAIN_OF_TRUST
Sumit Garg22a66f82017-04-20 05:09:12 +053041 uintptr_t ppa_esbc_hdr = 0;
Sumit Garge0f9e9b2016-09-01 12:56:44 -040042 uintptr_t ppa_img_addr = 0;
Sumit Garg22a66f82017-04-20 05:09:12 +053043#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
44 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
45 void *ppa_hdr_ddr;
46#endif
Sumit Garge0f9e9b2016-09-01 12:56:44 -040047#endif
48
York Sune6b871e2017-05-15 08:51:59 -070049 /* Skip if running at lower exception level */
50 if (el < 3) {
51 debug("Skipping PPA init, running at EL%d\n", el);
52 return 0;
53 }
54
Hou Zhiqiang7f83a5f2016-07-29 19:26:34 +080055#ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP
Hou Zhiqiang00787862016-06-28 20:18:14 +080056 ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080057 debug("%s: PPA image load from XIP\n", __func__);
Sumit Garg22a66f82017-04-20 05:09:12 +053058#ifdef CONFIG_CHAIN_OF_TRUST
59 ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR;
60#endif
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080061#else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */
62 size_t fw_length, fdt_header_len = sizeof(struct fdt_header);
63
64 /* Copy PPA image from MMC/SD/NAND to allocated memory */
65#ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC
66 struct mmc *mmc;
67 int dev = CONFIG_SYS_MMC_ENV_DEV;
68 struct fdt_header *fitp;
69 u32 cnt;
Sumit Garg22a66f82017-04-20 05:09:12 +053070 u32 blk;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080071
72 debug("%s: PPA image load from eMMC/SD\n", __func__);
73
74 ret = mmc_initialize(gd->bd);
75 if (ret) {
76 printf("%s: mmc_initialize() failed\n", __func__);
77 return ret;
78 }
79 mmc = find_mmc_device(dev);
80 if (!mmc) {
81 printf("PPA: MMC cannot find device for PPA firmware\n");
82 return -ENODEV;
83 }
84
85 ret = mmc_init(mmc);
86 if (ret) {
87 printf("%s: mmc_init() failed\n", __func__);
88 return ret;
89 }
90
91 fitp = malloc(roundup(fdt_header_len, 512));
92 if (!fitp) {
93 printf("PPA: malloc failed for FIT header(size 0x%zx)\n",
94 roundup(fdt_header_len, 512));
95 return -ENOMEM;
96 }
97
Sumit Garg22a66f82017-04-20 05:09:12 +053098 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +080099 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);
103 if (ret != cnt) {
104 free(fitp);
105 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
106 CONFIG_SYS_LS_PPA_FW_ADDR);
107 return -EIO;
108 }
109
110 /* flush cache after read */
111 flush_cache((ulong)fitp, cnt * 512);
112
113 ret = fdt_check_header(fitp);
114 if (ret) {
115 free(fitp);
116 printf("%s: fdt_check_header() failed\n", __func__);
117 return ret;
118 }
Sumit Garg22a66f82017-04-20 05:09:12 +0530119
120#ifdef CONFIG_CHAIN_OF_TRUST
121 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
122 if (!ppa_hdr_ddr) {
123 printf("PPA: malloc failed for PPA header\n");
124 return -ENOMEM;
125 }
126
127 blk = CONFIG_SYS_LS_PPA_ESBC_ADDR >> 9;
128 cnt = DIV_ROUND_UP(CONFIG_LS_PPA_ESBC_HDR_SIZE, 512);
129 ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, ppa_hdr_ddr);
130 if (ret != cnt) {
131 free(ppa_hdr_ddr);
132 printf("MMC/SD read of PPA header failed\n");
133 return -EIO;
134 }
135 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
136
137 /* flush cache after read */
138 flush_cache((ulong)ppa_hdr_ddr, cnt * 512);
139
140 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
141#endif
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800142
143 fw_length = fdt_totalsize(fitp);
144 free(fitp);
145
146 fw_length = roundup(fw_length, 512);
147 ppa_fit_addr = malloc(fw_length);
148 if (!ppa_fit_addr) {
149 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
150 fw_length);
151 return -ENOMEM;
152 }
153
Sumit Garg22a66f82017-04-20 05:09:12 +0530154 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512;
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800155 cnt = DIV_ROUND_UP(fw_length, 512);
156 debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n",
157 __func__, dev, blk, cnt);
158 ret = mmc->block_dev.block_read(&mmc->block_dev,
159 blk, cnt, ppa_fit_addr);
160 if (ret != cnt) {
161 free(ppa_fit_addr);
162 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n",
163 CONFIG_SYS_LS_PPA_FW_ADDR);
164 return -EIO;
165 }
166
167 /* flush cache after read */
168 flush_cache((ulong)ppa_fit_addr, cnt * 512);
169
170#elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
171 struct fdt_header fit;
172
173 debug("%s: PPA image load from NAND\n", __func__);
174
175 nand_init();
176 ret = nand_read(nand_info[0], (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
177 &fdt_header_len, (u_char *)&fit);
178 if (ret == -EUCLEAN) {
179 printf("NAND read of PPA FIT header at offset 0x%x failed\n",
180 CONFIG_SYS_LS_PPA_FW_ADDR);
181 return -EIO;
182 }
183
184 ret = fdt_check_header(&fit);
185 if (ret) {
186 printf("%s: fdt_check_header() failed\n", __func__);
187 return ret;
188 }
189
Sumit Garg22a66f82017-04-20 05:09:12 +0530190#ifdef CONFIG_CHAIN_OF_TRUST
191 ppa_hdr_ddr = malloc(CONFIG_LS_PPA_ESBC_HDR_SIZE);
192 if (!ppa_hdr_ddr) {
193 printf("PPA: malloc failed for PPA header\n");
194 return -ENOMEM;
195 }
196
197 fw_length = CONFIG_LS_PPA_ESBC_HDR_SIZE;
198
199 ret = nand_read(nand_info[0], (loff_t)CONFIG_SYS_LS_PPA_ESBC_ADDR,
200 &fw_length, (u_char *)ppa_hdr_ddr);
201 if (ret == -EUCLEAN) {
202 free(ppa_hdr_ddr);
203 printf("NAND read of PPA firmware at offset 0x%x failed\n",
204 CONFIG_SYS_LS_PPA_FW_ADDR);
205 return -EIO;
206 }
207 debug("Read PPA header to 0x%p\n", ppa_hdr_ddr);
208
209 /* flush cache after read */
210 flush_cache((ulong)ppa_hdr_ddr, fw_length);
211
212 ppa_esbc_hdr = (uintptr_t)ppa_hdr_ddr;
213#endif
214
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800215 fw_length = fdt_totalsize(&fit);
216
217 ppa_fit_addr = malloc(fw_length);
218 if (!ppa_fit_addr) {
219 printf("PPA: malloc failed for PPA image(size 0x%zx)\n",
220 fw_length);
221 return -ENOMEM;
222 }
223
224 ret = nand_read(nand_info[0], (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR,
225 &fw_length, (u_char *)ppa_fit_addr);
226 if (ret == -EUCLEAN) {
227 free(ppa_fit_addr);
228 printf("NAND read of PPA firmware at offset 0x%x failed\n",
229 CONFIG_SYS_LS_PPA_FW_ADDR);
230 return -EIO;
231 }
232
233 /* flush cache after read */
234 flush_cache((ulong)ppa_fit_addr, fw_length);
Hou Zhiqiang00787862016-06-28 20:18:14 +0800235#else
236#error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined"
237#endif
238
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800239#endif
240
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400241#ifdef CONFIG_CHAIN_OF_TRUST
242 ppa_img_addr = (uintptr_t)ppa_fit_addr;
243 if (fsl_check_boot_mode_secure() != 0) {
Sumit Garg22a66f82017-04-20 05:09:12 +0530244 /*
245 * In case of failure in validation, fsl_secboot_validate
246 * would not return back in case of Production environment
247 * with ITS=1. In Development environment (ITS=0 and
248 * SB_EN=1), the function may return back in case of
249 * non-fatal failures.
250 */
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400251 ret = fsl_secboot_validate(ppa_esbc_hdr,
Vinitha Pillai-B57223a4b3ded2017-03-23 13:48:14 +0530252 PPA_KEY_HASH,
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400253 &ppa_img_addr);
254 if (ret != 0)
255 printf("PPA validation failed\n");
256 else
257 printf("PPA validation Successful\n");
258 }
Sumit Garg22a66f82017-04-20 05:09:12 +0530259#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
260 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
261 free(ppa_hdr_ddr);
262#endif
Sumit Garge0f9e9b2016-09-01 12:56:44 -0400263#endif
264
Hou Zhiqiang00787862016-06-28 20:18:14 +0800265#ifdef CONFIG_FSL_LSCH3
266 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
267 boot_loc_ptr_l = &gur->bootlocptrl;
268 boot_loc_ptr_h = &gur->bootlocptrh;
269#elif defined(CONFIG_FSL_LSCH2)
270 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR);
271 boot_loc_ptr_l = &scfg->scratchrw[1];
272 boot_loc_ptr_h = &scfg->scratchrw[0];
273#endif
274
275 debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n",
276 boot_loc_ptr_l, boot_loc_ptr_h);
277 ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h);
278
Hou Zhiqiangc697bd12017-03-17 16:12:32 +0800279#if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \
280 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND)
281 free(ppa_fit_addr);
282#endif
283
Hou Zhiqiang00787862016-06-28 20:18:14 +0800284 return ret;
285}