1 /* 2 * Copyright 2016 NXP Semiconductor, Inc. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 #include <common.h> 7 #include <malloc.h> 8 #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 21 #ifdef CONFIG_CHAIN_OF_TRUST 22 #include <fsl_validate.h> 23 #endif 24 25 #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 31 DECLARE_GLOBAL_DATA_PTR; 32 33 int ppa_init(void) 34 { 35 void *ppa_fit_addr; 36 u32 *boot_loc_ptr_l, *boot_loc_ptr_h; 37 int ret; 38 39 #ifdef CONFIG_CHAIN_OF_TRUST 40 uintptr_t ppa_esbc_hdr = CONFIG_SYS_LS_PPA_ESBC_ADDR; 41 uintptr_t ppa_img_addr = 0; 42 #endif 43 44 #ifdef CONFIG_SYS_LS_PPA_FW_IN_XIP 45 ppa_fit_addr = (void *)CONFIG_SYS_LS_PPA_FW_ADDR; 46 debug("%s: PPA image load from XIP\n", __func__); 47 #else /* !CONFIG_SYS_LS_PPA_FW_IN_XIP */ 48 size_t fw_length, fdt_header_len = sizeof(struct fdt_header); 49 50 /* Copy PPA image from MMC/SD/NAND to allocated memory */ 51 #ifdef CONFIG_SYS_LS_PPA_FW_IN_MMC 52 struct mmc *mmc; 53 int dev = CONFIG_SYS_MMC_ENV_DEV; 54 struct fdt_header *fitp; 55 u32 cnt; 56 u32 blk = CONFIG_SYS_LS_PPA_FW_ADDR / 512; 57 58 debug("%s: PPA image load from eMMC/SD\n", __func__); 59 60 ret = mmc_initialize(gd->bd); 61 if (ret) { 62 printf("%s: mmc_initialize() failed\n", __func__); 63 return ret; 64 } 65 mmc = find_mmc_device(dev); 66 if (!mmc) { 67 printf("PPA: MMC cannot find device for PPA firmware\n"); 68 return -ENODEV; 69 } 70 71 ret = mmc_init(mmc); 72 if (ret) { 73 printf("%s: mmc_init() failed\n", __func__); 74 return ret; 75 } 76 77 fitp = malloc(roundup(fdt_header_len, 512)); 78 if (!fitp) { 79 printf("PPA: malloc failed for FIT header(size 0x%zx)\n", 80 roundup(fdt_header_len, 512)); 81 return -ENOMEM; 82 } 83 84 cnt = DIV_ROUND_UP(fdt_header_len, 512); 85 debug("%s: MMC read PPA FIT header: dev # %u, block # %u, count %u\n", 86 __func__, dev, blk, cnt); 87 ret = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, fitp); 88 if (ret != cnt) { 89 free(fitp); 90 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n", 91 CONFIG_SYS_LS_PPA_FW_ADDR); 92 return -EIO; 93 } 94 95 /* flush cache after read */ 96 flush_cache((ulong)fitp, cnt * 512); 97 98 ret = fdt_check_header(fitp); 99 if (ret) { 100 free(fitp); 101 printf("%s: fdt_check_header() failed\n", __func__); 102 return ret; 103 } 104 105 fw_length = fdt_totalsize(fitp); 106 free(fitp); 107 108 fw_length = roundup(fw_length, 512); 109 ppa_fit_addr = malloc(fw_length); 110 if (!ppa_fit_addr) { 111 printf("PPA: malloc failed for PPA image(size 0x%zx)\n", 112 fw_length); 113 return -ENOMEM; 114 } 115 116 cnt = DIV_ROUND_UP(fw_length, 512); 117 debug("%s: MMC read PPA FIT image: dev # %u, block # %u, count %u\n", 118 __func__, dev, blk, cnt); 119 ret = mmc->block_dev.block_read(&mmc->block_dev, 120 blk, cnt, ppa_fit_addr); 121 if (ret != cnt) { 122 free(ppa_fit_addr); 123 printf("MMC/SD read of PPA FIT header at offset 0x%x failed\n", 124 CONFIG_SYS_LS_PPA_FW_ADDR); 125 return -EIO; 126 } 127 128 /* flush cache after read */ 129 flush_cache((ulong)ppa_fit_addr, cnt * 512); 130 131 #elif defined(CONFIG_SYS_LS_PPA_FW_IN_NAND) 132 struct fdt_header fit; 133 134 debug("%s: PPA image load from NAND\n", __func__); 135 136 nand_init(); 137 ret = nand_read(nand_info[0], (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR, 138 &fdt_header_len, (u_char *)&fit); 139 if (ret == -EUCLEAN) { 140 printf("NAND read of PPA FIT header at offset 0x%x failed\n", 141 CONFIG_SYS_LS_PPA_FW_ADDR); 142 return -EIO; 143 } 144 145 ret = fdt_check_header(&fit); 146 if (ret) { 147 printf("%s: fdt_check_header() failed\n", __func__); 148 return ret; 149 } 150 151 fw_length = fdt_totalsize(&fit); 152 153 ppa_fit_addr = malloc(fw_length); 154 if (!ppa_fit_addr) { 155 printf("PPA: malloc failed for PPA image(size 0x%zx)\n", 156 fw_length); 157 return -ENOMEM; 158 } 159 160 ret = nand_read(nand_info[0], (loff_t)CONFIG_SYS_LS_PPA_FW_ADDR, 161 &fw_length, (u_char *)ppa_fit_addr); 162 if (ret == -EUCLEAN) { 163 free(ppa_fit_addr); 164 printf("NAND read of PPA firmware at offset 0x%x failed\n", 165 CONFIG_SYS_LS_PPA_FW_ADDR); 166 return -EIO; 167 } 168 169 /* flush cache after read */ 170 flush_cache((ulong)ppa_fit_addr, fw_length); 171 #else 172 #error "No CONFIG_SYS_LS_PPA_FW_IN_xxx defined" 173 #endif 174 175 #endif 176 177 #ifdef CONFIG_CHAIN_OF_TRUST 178 ppa_img_addr = (uintptr_t)ppa_fit_addr; 179 if (fsl_check_boot_mode_secure() != 0) { 180 ret = fsl_secboot_validate(ppa_esbc_hdr, 181 CONFIG_PPA_KEY_HASH, 182 &ppa_img_addr); 183 if (ret != 0) 184 printf("PPA validation failed\n"); 185 else 186 printf("PPA validation Successful\n"); 187 } 188 #endif 189 190 #ifdef CONFIG_FSL_LSCH3 191 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR); 192 boot_loc_ptr_l = &gur->bootlocptrl; 193 boot_loc_ptr_h = &gur->bootlocptrh; 194 #elif defined(CONFIG_FSL_LSCH2) 195 struct ccsr_scfg __iomem *scfg = (void *)(CONFIG_SYS_FSL_SCFG_ADDR); 196 boot_loc_ptr_l = &scfg->scratchrw[1]; 197 boot_loc_ptr_h = &scfg->scratchrw[0]; 198 #endif 199 200 debug("fsl-ppa: boot_loc_ptr_l = 0x%p, boot_loc_ptr_h =0x%p\n", 201 boot_loc_ptr_l, boot_loc_ptr_h); 202 ret = sec_firmware_init(ppa_fit_addr, boot_loc_ptr_l, boot_loc_ptr_h); 203 204 #if defined(CONFIG_SYS_LS_PPA_FW_IN_MMC) || \ 205 defined(CONFIG_SYS_LS_PPA_FW_IN_NAND) 206 free(ppa_fit_addr); 207 #endif 208 209 return ret; 210 } 211