blob: 82a4aa89a4d52be3ba26ea644f86d0bd02a5179f [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{
107 int conf_node_off, fw_node_off;
108 char *conf_node_name = NULL;
109 char *desc;
110 int ret = 0;
111
Thomas Hebb1dbd3d12019-11-10 08:23:15 -0800112 conf_node_name = PFE_FIRMWARE_FIT_CNF_NAME;
Calvin Johnson781b8382018-03-08 15:30:25 +0530113
114 conf_node_off = fit_conf_get_node(pfe_fit_addr, conf_node_name);
115 if (conf_node_off < 0) {
116 printf("PFE Firmware: %s: no such config\n", conf_node_name);
117 return -ENOENT;
118 }
119
120 fw_node_off = fit_conf_get_prop_node(pfe_fit_addr, conf_node_off,
121 fw_name);
122 if (fw_node_off < 0) {
123 printf("PFE Firmware: No '%s' in config\n",
124 fw_name);
125 return -ENOLINK;
126 }
127
128 if (!(fit_image_verify(pfe_fit_addr, fw_node_off))) {
129 printf("PFE Firmware: Bad firmware image (bad CRC)\n");
130 return -EINVAL;
131 }
132
133 if (fit_image_get_data(pfe_fit_addr, fw_node_off, data, size)) {
134 printf("PFE Firmware: Can't get %s subimage data/size",
135 fw_name);
136 return -ENOENT;
137 }
138
139 ret = fit_get_desc(pfe_fit_addr, fw_node_off, &desc);
140 if (ret)
141 printf("PFE Firmware: Can't get description\n");
142 else
143 printf("%s\n", desc);
144
145 return ret;
146}
147
148/*
149 * Check PFE FIT image
150 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100151 * Return: 0 on success, a negative value on error
Calvin Johnson781b8382018-03-08 15:30:25 +0530152 */
153static int pfe_fit_check(void)
154{
155 int ret = 0;
156
157 ret = fdt_check_header(pfe_fit_addr);
158 if (ret) {
159 printf("PFE Firmware: Bad firmware image (not a FIT image)\n");
160 return ret;
161 }
162
Simon Glassd563c252021-02-15 17:08:09 -0700163 if (fit_check_format(pfe_fit_addr, IMAGE_SIZE_INVAL)) {
Calvin Johnson781b8382018-03-08 15:30:25 +0530164 printf("PFE Firmware: Bad firmware image (bad FIT header)\n");
165 ret = -1;
166 return ret;
167 }
168
169 return ret;
170}
171
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530172int pfe_spi_flash_init(void)
173{
174 struct spi_flash *pfe_flash;
175 int ret = 0;
Biwen Lib9eeb082020-11-05 19:28:12 +0800176 void *addr = malloc(CONFIG_SYS_LS_PFE_FW_LENGTH);
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530177
Kuldeep Singhb363eca2020-09-11 16:36:48 +0530178 if (!addr)
179 return -ENOMEM;
180
Patrice Chotard05b1aa62022-03-30 09:33:14 +0200181 pfe_flash = spi_flash_probe(CONFIG_SYS_FSL_PFE_SPI_BUS,
182 CONFIG_SYS_FSL_PFE_SPI_CS,
183 CONFIG_SYS_FSL_PFE_SPI_MAX_HZ,
184 CONFIG_SYS_FSL_PFE_SPI_MODE);
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530185
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530186 if (!pfe_flash) {
187 printf("SF: probe for pfe failed\n");
Kuldeep Singhb363eca2020-09-11 16:36:48 +0530188 free(addr);
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530189 return -ENODEV;
190 }
191
192 ret = spi_flash_read(pfe_flash,
193 CONFIG_SYS_LS_PFE_FW_ADDR,
Biwen Lib9eeb082020-11-05 19:28:12 +0800194 CONFIG_SYS_LS_PFE_FW_LENGTH,
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530195 addr);
Biwen Lib9eeb082020-11-05 19:28:12 +0800196 if (ret) {
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530197 printf("SF: read for pfe failed\n");
Biwen Lib9eeb082020-11-05 19:28:12 +0800198 free(addr);
199 spi_flash_free(pfe_flash);
200 return ret;
201 }
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530202
Biwen Lib9eeb082020-11-05 19:28:12 +0800203#ifdef CONFIG_CHAIN_OF_TRUST
204 void *hdr_addr = malloc(CONFIG_SYS_LS_PFE_ESBC_LENGTH);
205
206 if (!hdr_addr) {
207 free(addr);
208 spi_flash_free(pfe_flash);
209 return -ENOMEM;
210 }
211
212 ret = spi_flash_read(pfe_flash,
213 CONFIG_SYS_LS_PFE_ESBC_ADDR,
214 CONFIG_SYS_LS_PFE_ESBC_LENGTH,
215 hdr_addr);
216 if (ret) {
217 printf("SF: failed to read pfe esbc header\n");
218 free(addr);
219 free(hdr_addr);
220 spi_flash_free(pfe_flash);
221 return ret;
222 }
223
224 pfe_esbc_hdr_addr = hdr_addr;
225#endif
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530226 pfe_fit_addr = addr;
227 spi_flash_free(pfe_flash);
228
229 return ret;
230}
231
Calvin Johnson781b8382018-03-08 15:30:25 +0530232/*
233 * PFE firmware initialization.
234 * Loads different firmware files from FIT image.
235 * Initializes PE IMEM/DMEM and UTIL-PE DDR
236 * Initializes control path symbol addresses (by looking them up in the elf
237 * firmware files
238 * Takes PE's out of reset
239 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100240 * Return: 0 on success, a negative value on error
Calvin Johnson781b8382018-03-08 15:30:25 +0530241 */
242int pfe_firmware_init(void)
243{
Vinitha V Pillaiad698c32018-05-23 11:03:31 +0530244#define PFE_KEY_HASH NULL
Calvin Johnson781b8382018-03-08 15:30:25 +0530245 char *pfe_firmware_name;
246 const void *raw_image_addr;
247 size_t raw_image_size = 0;
248 u8 *pfe_firmware;
Vinitha V Pillaiad698c32018-05-23 11:03:31 +0530249#ifdef CONFIG_CHAIN_OF_TRUST
250 uintptr_t pfe_esbc_hdr = 0;
251 uintptr_t pfe_img_addr = 0;
252#endif
Calvin Johnson781b8382018-03-08 15:30:25 +0530253 int ret = 0;
Chaitanya Sakinam811cbcf2021-05-07 12:22:05 +0800254 int fw_count, max_fw_count;
255 const char *p;
Calvin Johnson781b8382018-03-08 15:30:25 +0530256
Kuldeep Singh14e956f2020-05-28 11:42:53 +0530257 ret = pfe_spi_flash_init();
258 if (ret)
259 goto err;
260
Calvin Johnson781b8382018-03-08 15:30:25 +0530261 ret = pfe_fit_check();
262 if (ret)
263 goto err;
264
Vinitha V Pillaiad698c32018-05-23 11:03:31 +0530265#ifdef CONFIG_CHAIN_OF_TRUST
Biwen Lib9eeb082020-11-05 19:28:12 +0800266 pfe_esbc_hdr = (uintptr_t)pfe_esbc_hdr_addr;
Vinitha V Pillaiad698c32018-05-23 11:03:31 +0530267 pfe_img_addr = (uintptr_t)pfe_fit_addr;
268 if (fsl_check_boot_mode_secure() != 0) {
269 /*
270 * In case of failure in validation, fsl_secboot_validate
271 * would not return back in case of Production environment
272 * with ITS=1. In Development environment (ITS=0 and
273 * SB_EN=1), the function may return back in case of
274 * non-fatal failures.
275 */
276 ret = fsl_secboot_validate(pfe_esbc_hdr,
277 PFE_KEY_HASH,
278 &pfe_img_addr);
279 if (ret != 0)
280 printf("PFE firmware(s) validation failed\n");
281 else
282 printf("PFE firmware(s) validation Successful\n");
283 }
284#endif
285
Chaitanya Sakinam811cbcf2021-05-07 12:22:05 +0800286 p = env_get("load_util");
287 if (!p) {
288 max_fw_count = 2;
289 } else {
Simon Glassff9b9032021-07-24 09:03:30 -0600290 max_fw_count = dectoul(p, NULL);
Chaitanya Sakinam811cbcf2021-05-07 12:22:05 +0800291 if (max_fw_count)
292 max_fw_count = 3;
293 else
294 max_fw_count = 2;
295 }
296
297 for (fw_count = 0; fw_count < max_fw_count; fw_count++) {
298 switch (fw_count) {
299 case 0:
300 pfe_firmware_name = "class_slowpath";
301 break;
302 case 1:
303 pfe_firmware_name = "tmu_slowpath";
304 break;
305 case 2:
306 pfe_firmware_name = "util_slowpath";
307 break;
308 }
309
310 if (pfe_get_fw(&raw_image_addr, &raw_image_size,
311 pfe_firmware_name)) {
312 printf("%s firmware couldn't be found in FIT image\n",
313 pfe_firmware_name);
314 break;
315 }
316 pfe_firmware = malloc(raw_image_size);
317 if (!pfe_firmware)
318 return -ENOMEM;
319 memcpy((void *)pfe_firmware, (void *)raw_image_addr,
320 raw_image_size);
321
322 switch (fw_count) {
323 case 0:
324 env_set_addr("class_elf_firmware", pfe_firmware);
325 env_set_addr("class_elf_size", (void *)raw_image_size);
326 break;
327 case 1:
328 env_set_addr("tmu_elf_firmware", pfe_firmware);
329 env_set_addr("tmu_elf_size", (void *)raw_image_size);
330 break;
331 case 2:
332 env_set_addr("util_elf_firmware", pfe_firmware);
333 env_set_addr("util_elf_size", (void *)raw_image_size);
334 break;
335 }
336 }
337
338 raw_image_addr = NULL;
339 pfe_firmware = NULL;
340 raw_image_size = 0;
Calvin Johnson781b8382018-03-08 15:30:25 +0530341 for (fw_count = 0; fw_count < 2; fw_count++) {
342 if (fw_count == 0)
343 pfe_firmware_name = "class";
344 else if (fw_count == 1)
345 pfe_firmware_name = "tmu";
346
347 pfe_get_fw(&raw_image_addr, &raw_image_size, pfe_firmware_name);
348 pfe_firmware = malloc(raw_image_size);
349 if (!pfe_firmware)
350 return -ENOMEM;
351 memcpy((void *)pfe_firmware, (void *)raw_image_addr,
352 raw_image_size);
353
354 if (fw_count == 0)
355 ret = pfe_load_elf(CLASS_MASK, pfe_firmware);
356 else if (fw_count == 1)
357 ret = pfe_load_elf(TMU_MASK, pfe_firmware);
358
359 if (ret < 0) {
360 printf("%s: %s firmware load failed\n", __func__,
361 pfe_firmware_name);
362 goto err;
363 }
364 debug("%s: %s firmware loaded\n", __func__, pfe_firmware_name);
365 free(pfe_firmware);
366 }
367
368 tmu_enable(0xb);
369 class_enable();
370 gpi_enable(HGPI_BASE_ADDR);
371
372err:
373 return ret;
374}
375
376/*
377 * PFE firmware cleanup
378 * Puts PE's in reset
379 */
380void pfe_firmware_exit(void)
381{
382 debug("%s\n", __func__);
383
384 class_disable();
385 tmu_disable(0xf);
386 hif_tx_disable();
387 hif_rx_disable();
388}