blob: 7679f5940536ee6347583271b27deb43cab37ddf [file] [log] [blame]
Hadi Asyrafi616da772019-06-27 11:34:03 +08001/*
2 * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
Jit Loon Lim86f6fb32023-05-17 12:26:11 +08003 * Copyright (c) 2019-2023, Intel Corporation. All rights reserved.
Sieu Mun Tang7c14cea2024-02-02 23:23:12 +08004 * Copyright (c) 2024, Altera Corporation. All rights reserved.
Hadi Asyrafi616da772019-06-27 11:34:03 +08005 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#include <arch_helpers.h>
10#include <assert.h>
11#include <common/debug.h>
12#include <common/tbbr/tbbr_img_def.h>
Jit Loon Limb24dddf2023-05-17 12:26:11 +080013#include <drivers/cadence/cdns_nand.h>
14#include <drivers/cadence/cdns_sdmmc.h>
Hadi Asyrafi616da772019-06-27 11:34:03 +080015#include <drivers/io/io_block.h>
16#include <drivers/io/io_driver.h>
17#include <drivers/io/io_fip.h>
18#include <drivers/io/io_memmap.h>
Jit Loon Limb24dddf2023-05-17 12:26:11 +080019#include <drivers/io/io_mtd.h>
Hadi Asyrafi616da772019-06-27 11:34:03 +080020#include <drivers/io/io_storage.h>
21#include <drivers/mmc.h>
22#include <drivers/partition/partition.h>
23#include <lib/mmio.h>
24#include <tools_share/firmware_image_package.h>
Jit Loon Lima7f54942023-05-17 12:26:11 +080025#include "drivers/sdmmc/sdmmc.h"
Hadi Asyrafif0fa8072019-10-23 17:02:55 +080026#include "socfpga_private.h"
Mahesh Raoc2715992023-08-22 17:26:23 +080027#include "socfpga_ros.h"
Hadi Asyrafi616da772019-06-27 11:34:03 +080028
Jit Loon Limb24dddf2023-05-17 12:26:11 +080029
Hadi Asyrafi616da772019-06-27 11:34:03 +080030#define PLAT_FIP_BASE (0)
Sieu Mun Tang7b347f42024-10-22 00:31:02 +080031# if ARM_LINUX_KERNEL_AS_BL33
Sieu Mun Tang7c14cea2024-02-02 23:23:12 +080032#define PLAT_FIP_MAX_SIZE (0x8000000)
33#define PLAT_MMC_DATA_BASE (0x10000000)
34#define PLAT_MMC_DATA_SIZE (0x100000)
Sieu Mun Tang7b347f42024-10-22 00:31:02 +080035# else
36#define PLAT_FIP_MAX_SIZE (0x1000000)
37#define PLAT_MMC_DATA_BASE (0xffe3c000)
38#define PLAT_MMC_DATA_SIZE (0x2000)
39# endif
Hadi Asyrafi616da772019-06-27 11:34:03 +080040
41static const io_dev_connector_t *fip_dev_con;
42static const io_dev_connector_t *boot_dev_con;
43
Jit Loon Limb24dddf2023-05-17 12:26:11 +080044static io_mtd_dev_spec_t nand_dev_spec;
45
Hadi Asyrafi616da772019-06-27 11:34:03 +080046static uintptr_t fip_dev_handle;
47static uintptr_t boot_dev_handle;
48
49static const io_uuid_spec_t bl2_uuid_spec = {
50 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
51};
52
53static const io_uuid_spec_t bl31_uuid_spec = {
54 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
55};
56
57static const io_uuid_spec_t bl33_uuid_spec = {
58 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
59};
60
Jit Loon Limc5a3e3a2023-10-16 00:19:34 +080061# if ARM_LINUX_KERNEL_AS_BL33 != 0
62static const io_uuid_spec_t nt_fw_config_uuid_spec = {
63 .uuid = UUID_NT_FW_CONFIG,
64};
65# endif
66
Hadi Asyrafi616da772019-06-27 11:34:03 +080067uintptr_t a2_lba_offset;
68const char a2[] = {0xa2, 0x0};
69
70static const io_block_spec_t gpt_block_spec = {
71 .offset = 0,
72 .length = MMC_BLOCK_SIZE
73};
74
75static int check_fip(const uintptr_t spec);
76static int check_dev(const uintptr_t spec);
77
78static io_block_dev_spec_t boot_dev_spec;
79static int (*register_io_dev)(const io_dev_connector_t **);
80
81static io_block_spec_t fip_spec = {
82 .offset = PLAT_FIP_BASE,
83 .length = PLAT_FIP_MAX_SIZE,
84};
85
86struct plat_io_policy {
87 uintptr_t *dev_handle;
88 uintptr_t image_spec;
89 int (*check)(const uintptr_t spec);
90};
91
92static const struct plat_io_policy policies[] = {
93 [FIP_IMAGE_ID] = {
94 &boot_dev_handle,
95 (uintptr_t)&fip_spec,
96 check_dev
97 },
98 [BL2_IMAGE_ID] = {
99 &fip_dev_handle,
100 (uintptr_t)&bl2_uuid_spec,
101 check_fip
102 },
103 [BL31_IMAGE_ID] = {
104 &fip_dev_handle,
105 (uintptr_t)&bl31_uuid_spec,
106 check_fip
107 },
108 [BL33_IMAGE_ID] = {
109 &fip_dev_handle,
110 (uintptr_t) &bl33_uuid_spec,
111 check_fip
112 },
Jit Loon Limc5a3e3a2023-10-16 00:19:34 +0800113# if ARM_LINUX_KERNEL_AS_BL33 != 0
114 [NT_FW_CONFIG_ID] = {
115 &fip_dev_handle,
116 (uintptr_t)&nt_fw_config_uuid_spec,
117 check_fip
118 },
119# endif
Hadi Asyrafi616da772019-06-27 11:34:03 +0800120 [GPT_IMAGE_ID] = {
121 &boot_dev_handle,
122 (uintptr_t) &gpt_block_spec,
123 check_dev
124 },
125};
126
127static int check_dev(const uintptr_t spec)
128{
129 int result;
130 uintptr_t local_handle;
131
132 result = io_dev_init(boot_dev_handle, (uintptr_t)NULL);
133 if (result == 0) {
134 result = io_open(boot_dev_handle, spec, &local_handle);
135 if (result == 0)
136 io_close(local_handle);
137 }
138 return result;
139}
140
141static int check_fip(const uintptr_t spec)
142{
143 int result;
144 uintptr_t local_image_handle;
145
146 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
147 if (result == 0) {
148 result = io_open(fip_dev_handle, spec, &local_image_handle);
149 if (result == 0)
150 io_close(local_image_handle);
151 }
152 return result;
153}
154
Mahesh Raoc2715992023-08-22 17:26:23 +0800155void socfpga_io_setup(int boot_source, unsigned long offset)
Hadi Asyrafi616da772019-06-27 11:34:03 +0800156{
157 int result;
Mahesh Raoc2715992023-08-22 17:26:23 +0800158 fip_spec.offset = offset;
Hadi Asyrafi616da772019-06-27 11:34:03 +0800159
160 switch (boot_source) {
161 case BOOT_SOURCE_SDMMC:
162 register_io_dev = &register_io_dev_block;
163 boot_dev_spec.buffer.offset = PLAT_MMC_DATA_BASE;
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800164 boot_dev_spec.buffer.length = SOCFPGA_MMC_BLOCK_SIZE;
Jit Loon Lima7f54942023-05-17 12:26:11 +0800165 boot_dev_spec.ops.read = SDMMC_READ_BLOCKS;
166 boot_dev_spec.ops.write = SDMMC_WRITE_BLOCKS;
Hadi Asyrafi616da772019-06-27 11:34:03 +0800167 boot_dev_spec.block_size = MMC_BLOCK_SIZE;
168 break;
169
170 case BOOT_SOURCE_QSPI:
171 register_io_dev = &register_io_dev_memmap;
Hadi Asyrafi616da772019-06-27 11:34:03 +0800172 break;
173
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800174#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
175 case BOOT_SOURCE_NAND:
176 register_io_dev = &register_io_dev_mtd;
177 nand_dev_spec.ops.init = cdns_nand_init_mtd;
178 nand_dev_spec.ops.read = cdns_nand_read;
179 nand_dev_spec.ops.write = NULL;
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800180 break;
181#endif
182
Hadi Asyrafi616da772019-06-27 11:34:03 +0800183 default:
184 ERROR("Unsupported boot source\n");
185 panic();
186 break;
187 }
188
189 result = (*register_io_dev)(&boot_dev_con);
190 assert(result == 0);
191
192 result = register_io_dev_fip(&fip_dev_con);
193 assert(result == 0);
194
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800195 if (boot_source == BOOT_SOURCE_NAND) {
196 result = io_dev_open(boot_dev_con, (uintptr_t)&nand_dev_spec,
197 &boot_dev_handle);
198 } else {
199 result = io_dev_open(boot_dev_con, (uintptr_t)&boot_dev_spec,
200 &boot_dev_handle);
201 }
Hadi Asyrafi616da772019-06-27 11:34:03 +0800202 assert(result == 0);
203
204 result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
205 assert(result == 0);
206
207 if (boot_source == BOOT_SOURCE_SDMMC) {
208 partition_init(GPT_IMAGE_ID);
209 fip_spec.offset = get_partition_entry(a2)->start;
210 }
211
212 (void)result;
213}
214
215int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
216 uintptr_t *image_spec)
217{
218 int result;
219 const struct plat_io_policy *policy;
220
221 assert(image_id < ARRAY_SIZE(policies));
222
223 policy = &policies[image_id];
224 result = policy->check(policy->image_spec);
225 assert(result == 0);
226
227 *image_spec = policy->image_spec;
228 *dev_handle = *(policy->dev_handle);
229
230 return result;
231}