blob: da4f2ca42a5ba97b71a1591d9afa58981369e9fa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Calvin Johnson781b8382018-03-08 15:30:25 +05302/*
3 * Copyright 2015-2016 Freescale Semiconductor, Inc.
Chaitanya Sakinam811cbcf2021-05-07 12:22:05 +08004 * Copyright 2017,2021 NXP
Calvin Johnson781b8382018-03-08 15:30:25 +05305 */
6
7/*
8 * @file
9 * Contains all the functions to handle parsing and loading of PE firmware
10 * files.
11 */
12
Biwen Lib9eeb082020-11-05 19:28:12 +080013#include <dm.h>
14#include <dm/device-internal.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060015#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Calvin Johnson781b8382018-03-08 15:30:25 +053019#include <net/pfe_eth/pfe_eth.h>
20#include <net/pfe_eth/pfe_firmware.h>
Kuldeep Singh14e956f2020-05-28 11:42:53 +053021#include <spi_flash.h>
Vinitha V Pillaiad698c32018-05-23 11:03:31 +053022#ifdef CONFIG_CHAIN_OF_TRUST
23#include <fsl_validate.h>
24#endif
Calvin Johnson781b8382018-03-08 15:30:25 +053025
Thomas Hebb1dbd3d12019-11-10 08:23:15 -080026#define PFE_FIRMWARE_FIT_CNF_NAME "config@1"
Calvin Johnson781b8382018-03-08 15:30:25 +053027
Kuldeep Singh14e956f2020-05-28 11:42:53 +053028static const void *pfe_fit_addr;
Biwen Lib9eeb082020-11-05 19:28:12 +080029#ifdef CONFIG_CHAIN_OF_TRUST
30static const void *pfe_esbc_hdr_addr;
31#endif
Calvin Johnson781b8382018-03-08 15:30:25 +053032
33/*
34 * PFE elf firmware loader.
35 * Loads an elf firmware image into a list of PE's (specified using a bitmask)
36 *
37 * @param pe_mask Mask of PE id's to load firmware to
38 * @param pfe_firmware Pointer to the firmware image
39 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010040 * Return: 0 on success, a negative value on error
Calvin Johnson781b8382018-03-08 15:30:25 +053041 */
42static int pfe_load_elf(int pe_mask, uint8_t *pfe_firmware)
43{
44 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *)pfe_firmware;
45 Elf32_Half sections = be16_to_cpu(elf_hdr->e_shnum);
46 Elf32_Shdr *shdr = (Elf32_Shdr *)(pfe_firmware +
47 be32_to_cpu(elf_hdr->e_shoff));
48 int id, section;
49 int ret;
50
51 debug("%s: no of sections: %d\n", __func__, sections);
52
53 /* Some sanity checks */
54 if (strncmp((char *)&elf_hdr->e_ident[EI_MAG0], ELFMAG, SELFMAG)) {
55 printf("%s: incorrect elf magic number\n", __func__);
56 return -1;
57 }
58
59 if (elf_hdr->e_ident[EI_CLASS] != ELFCLASS32) {
60 printf("%s: incorrect elf class(%x)\n", __func__,
61 elf_hdr->e_ident[EI_CLASS]);
62 return -1;
63 }
64
65 if (elf_hdr->e_ident[EI_DATA] != ELFDATA2MSB) {
66 printf("%s: incorrect elf data(%x)\n", __func__,
67 elf_hdr->e_ident[EI_DATA]);
68 return -1;
69 }
70
71 if (be16_to_cpu(elf_hdr->e_type) != ET_EXEC) {
72 printf("%s: incorrect elf file type(%x)\n", __func__,
73 be16_to_cpu(elf_hdr->e_type));
74 return -1;
75 }
76
77 for (section = 0; section < sections; section++, shdr++) {
78 if (!(be32_to_cpu(shdr->sh_flags) & (SHF_WRITE | SHF_ALLOC |
79 SHF_EXECINSTR)))
80 continue;
81 for (id = 0; id < MAX_PE; id++)
82 if (pe_mask & BIT(id)) {
83 ret = pe_load_elf_section(id,
84 pfe_firmware, shdr);
85 if (ret < 0)
86 goto err;
87 }
88 }
89 return 0;
90
91err:
92 return ret;
93}
94
95/*
96 * Get PFE firmware from FIT image
97 *
98 * @param data pointer to PFE firmware
99 * @param size pointer to size of the firmware
100 * @param fw_name pfe firmware name, either class or tmu
101 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100102 * Return: 0 on success, a negative value on error
Calvin Johnson781b8382018-03-08 15:30:25 +0530103 */
104static int pfe_get_fw(const void **data,
105 size_t *size, char *fw_name)
106{
Sean Anderson4b2c94f2022-08-16 11:16:06 -0400107 return fit_get_data_conf_prop(pfe_fit_addr, fw_name, data, size);
Calvin Johnson781b8382018-03-08 15:30:25 +0530108}
109
110/*
111 * Check PFE FIT image
112 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100113 * Return: 0 on success, a negative value on error
Calvin Johnson781b8382018-03-08 15:30:25 +0530114 */
115static int pfe_fit_check(void)
116{
117 int ret = 0;
118
119 ret = fdt_check_header(pfe_fit_addr);
120 if (ret) {
121 printf("PFE Firmware: Bad firmware image (not a FIT image)\n");
122 return ret;
123 }
124
Simon Glassd563c252021-02-15 17:08:09 -0700125 if (fit_check_format(pfe_fit_addr, IMAGE_SIZE_INVAL)) {
Calvin Johnson781b8382018-03-08 15:30:25 +0530126 printf("PFE Firmware: Bad firmware image (bad FIT header)\n");
127 ret = -1;
128 return ret;
129 }
130
131 return ret;
132}
133
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530134int pfe_spi_flash_init(void)
135{
136 struct spi_flash *pfe_flash;
137 int ret = 0;
Biwen Lib9eeb082020-11-05 19:28:12 +0800138 void *addr = malloc(CONFIG_SYS_LS_PFE_FW_LENGTH);
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530139
Kuldeep Singhb363eca2020-09-11 16:36:48 +0530140 if (!addr)
141 return -ENOMEM;
142
Patrice Chotard05b1aa62022-03-30 09:33:14 +0200143 pfe_flash = spi_flash_probe(CONFIG_SYS_FSL_PFE_SPI_BUS,
144 CONFIG_SYS_FSL_PFE_SPI_CS,
145 CONFIG_SYS_FSL_PFE_SPI_MAX_HZ,
146 CONFIG_SYS_FSL_PFE_SPI_MODE);
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530147
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530148 if (!pfe_flash) {
149 printf("SF: probe for pfe failed\n");
Kuldeep Singhb363eca2020-09-11 16:36:48 +0530150 free(addr);
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530151 return -ENODEV;
152 }
153
154 ret = spi_flash_read(pfe_flash,
155 CONFIG_SYS_LS_PFE_FW_ADDR,
Biwen Lib9eeb082020-11-05 19:28:12 +0800156 CONFIG_SYS_LS_PFE_FW_LENGTH,
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530157 addr);
Biwen Lib9eeb082020-11-05 19:28:12 +0800158 if (ret) {
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530159 printf("SF: read for pfe failed\n");
Biwen Lib9eeb082020-11-05 19:28:12 +0800160 free(addr);
161 spi_flash_free(pfe_flash);
162 return ret;
163 }
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530164
Biwen Lib9eeb082020-11-05 19:28:12 +0800165#ifdef CONFIG_CHAIN_OF_TRUST
166 void *hdr_addr = malloc(CONFIG_SYS_LS_PFE_ESBC_LENGTH);
167
168 if (!hdr_addr) {
169 free(addr);
170 spi_flash_free(pfe_flash);
171 return -ENOMEM;
172 }
173
174 ret = spi_flash_read(pfe_flash,
175 CONFIG_SYS_LS_PFE_ESBC_ADDR,
176 CONFIG_SYS_LS_PFE_ESBC_LENGTH,
177 hdr_addr);
178 if (ret) {
179 printf("SF: failed to read pfe esbc header\n");
180 free(addr);
181 free(hdr_addr);
182 spi_flash_free(pfe_flash);
183 return ret;
184 }
185
186 pfe_esbc_hdr_addr = hdr_addr;
187#endif
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530188 pfe_fit_addr = addr;
189 spi_flash_free(pfe_flash);
190
191 return ret;
192}
193
Calvin Johnson781b8382018-03-08 15:30:25 +0530194/*
195 * PFE firmware initialization.
196 * Loads different firmware files from FIT image.
197 * Initializes PE IMEM/DMEM and UTIL-PE DDR
198 * Initializes control path symbol addresses (by looking them up in the elf
199 * firmware files
200 * Takes PE's out of reset
201 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100202 * Return: 0 on success, a negative value on error
Calvin Johnson781b8382018-03-08 15:30:25 +0530203 */
204int pfe_firmware_init(void)
205{
Vinitha V Pillaiad698c32018-05-23 11:03:31 +0530206#define PFE_KEY_HASH NULL
Calvin Johnson781b8382018-03-08 15:30:25 +0530207 char *pfe_firmware_name;
208 const void *raw_image_addr;
209 size_t raw_image_size = 0;
210 u8 *pfe_firmware;
Vinitha V Pillaiad698c32018-05-23 11:03:31 +0530211#ifdef CONFIG_CHAIN_OF_TRUST
212 uintptr_t pfe_esbc_hdr = 0;
213 uintptr_t pfe_img_addr = 0;
214#endif
Calvin Johnson781b8382018-03-08 15:30:25 +0530215 int ret = 0;
Chaitanya Sakinam811cbcf2021-05-07 12:22:05 +0800216 int fw_count, max_fw_count;
217 const char *p;
Calvin Johnson781b8382018-03-08 15:30:25 +0530218
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530219 ret = pfe_spi_flash_init();
220 if (ret)
221 goto err;
222
Calvin Johnson781b8382018-03-08 15:30:25 +0530223 ret = pfe_fit_check();
224 if (ret)
225 goto err;
226
Vinitha V Pillaiad698c32018-05-23 11:03:31 +0530227#ifdef CONFIG_CHAIN_OF_TRUST
Biwen Lib9eeb082020-11-05 19:28:12 +0800228 pfe_esbc_hdr = (uintptr_t)pfe_esbc_hdr_addr;
Vinitha V Pillaiad698c32018-05-23 11:03:31 +0530229 pfe_img_addr = (uintptr_t)pfe_fit_addr;
230 if (fsl_check_boot_mode_secure() != 0) {
231 /*
232 * In case of failure in validation, fsl_secboot_validate
233 * would not return back in case of Production environment
234 * with ITS=1. In Development environment (ITS=0 and
235 * SB_EN=1), the function may return back in case of
236 * non-fatal failures.
237 */
238 ret = fsl_secboot_validate(pfe_esbc_hdr,
239 PFE_KEY_HASH,
240 &pfe_img_addr);
241 if (ret != 0)
242 printf("PFE firmware(s) validation failed\n");
243 else
244 printf("PFE firmware(s) validation Successful\n");
245 }
246#endif
247
Chaitanya Sakinam811cbcf2021-05-07 12:22:05 +0800248 p = env_get("load_util");
249 if (!p) {
250 max_fw_count = 2;
251 } else {
Simon Glassff9b9032021-07-24 09:03:30 -0600252 max_fw_count = dectoul(p, NULL);
Chaitanya Sakinam811cbcf2021-05-07 12:22:05 +0800253 if (max_fw_count)
254 max_fw_count = 3;
255 else
256 max_fw_count = 2;
257 }
258
259 for (fw_count = 0; fw_count < max_fw_count; fw_count++) {
260 switch (fw_count) {
261 case 0:
262 pfe_firmware_name = "class_slowpath";
263 break;
264 case 1:
265 pfe_firmware_name = "tmu_slowpath";
266 break;
267 case 2:
268 pfe_firmware_name = "util_slowpath";
269 break;
270 }
271
272 if (pfe_get_fw(&raw_image_addr, &raw_image_size,
273 pfe_firmware_name)) {
274 printf("%s firmware couldn't be found in FIT image\n",
275 pfe_firmware_name);
276 break;
277 }
278 pfe_firmware = malloc(raw_image_size);
279 if (!pfe_firmware)
280 return -ENOMEM;
281 memcpy((void *)pfe_firmware, (void *)raw_image_addr,
282 raw_image_size);
283
284 switch (fw_count) {
285 case 0:
286 env_set_addr("class_elf_firmware", pfe_firmware);
287 env_set_addr("class_elf_size", (void *)raw_image_size);
288 break;
289 case 1:
290 env_set_addr("tmu_elf_firmware", pfe_firmware);
291 env_set_addr("tmu_elf_size", (void *)raw_image_size);
292 break;
293 case 2:
294 env_set_addr("util_elf_firmware", pfe_firmware);
295 env_set_addr("util_elf_size", (void *)raw_image_size);
296 break;
297 }
298 }
299
300 raw_image_addr = NULL;
301 pfe_firmware = NULL;
302 raw_image_size = 0;
Calvin Johnson781b8382018-03-08 15:30:25 +0530303 for (fw_count = 0; fw_count < 2; fw_count++) {
304 if (fw_count == 0)
305 pfe_firmware_name = "class";
306 else if (fw_count == 1)
307 pfe_firmware_name = "tmu";
308
309 pfe_get_fw(&raw_image_addr, &raw_image_size, pfe_firmware_name);
310 pfe_firmware = malloc(raw_image_size);
311 if (!pfe_firmware)
312 return -ENOMEM;
313 memcpy((void *)pfe_firmware, (void *)raw_image_addr,
314 raw_image_size);
315
316 if (fw_count == 0)
317 ret = pfe_load_elf(CLASS_MASK, pfe_firmware);
318 else if (fw_count == 1)
319 ret = pfe_load_elf(TMU_MASK, pfe_firmware);
320
321 if (ret < 0) {
322 printf("%s: %s firmware load failed\n", __func__,
323 pfe_firmware_name);
324 goto err;
325 }
326 debug("%s: %s firmware loaded\n", __func__, pfe_firmware_name);
327 free(pfe_firmware);
328 }
329
330 tmu_enable(0xb);
331 class_enable();
332 gpi_enable(HGPI_BASE_ADDR);
333
334err:
335 return ret;
336}
337
338/*
339 * PFE firmware cleanup
340 * Puts PE's in reset
341 */
342void pfe_firmware_exit(void)
343{
344 debug("%s\n", __func__);
345
346 class_disable();
347 tmu_disable(0xf);
348 hif_tx_disable();
349 hif_rx_disable();
350}