blob: d250d9e30c1e9f34cebf8be160ebc0d7ea8ed9d3 [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.
Hadi Asyrafi616da772019-06-27 11:34:03 +08004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#include <arch_helpers.h>
9#include <assert.h>
10#include <common/debug.h>
11#include <common/tbbr/tbbr_img_def.h>
Jit Loon Limb24dddf2023-05-17 12:26:11 +080012#include <drivers/cadence/cdns_nand.h>
13#include <drivers/cadence/cdns_sdmmc.h>
Hadi Asyrafi616da772019-06-27 11:34:03 +080014#include <drivers/io/io_block.h>
15#include <drivers/io/io_driver.h>
16#include <drivers/io/io_fip.h>
17#include <drivers/io/io_memmap.h>
Jit Loon Limb24dddf2023-05-17 12:26:11 +080018#include <drivers/io/io_mtd.h>
Hadi Asyrafi616da772019-06-27 11:34:03 +080019#include <drivers/io/io_storage.h>
20#include <drivers/mmc.h>
21#include <drivers/partition/partition.h>
22#include <lib/mmio.h>
23#include <tools_share/firmware_image_package.h>
24
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)
31#define PLAT_FIP_MAX_SIZE (0x1000000)
32#define PLAT_MMC_DATA_BASE (0xffe3c000)
33#define PLAT_MMC_DATA_SIZE (0x2000)
Hadi Asyrafi616da772019-06-27 11:34:03 +080034
35static const io_dev_connector_t *fip_dev_con;
36static const io_dev_connector_t *boot_dev_con;
37
Jit Loon Limb24dddf2023-05-17 12:26:11 +080038static io_mtd_dev_spec_t nand_dev_spec;
39
Hadi Asyrafi616da772019-06-27 11:34:03 +080040static uintptr_t fip_dev_handle;
41static uintptr_t boot_dev_handle;
42
43static const io_uuid_spec_t bl2_uuid_spec = {
44 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
45};
46
47static const io_uuid_spec_t bl31_uuid_spec = {
48 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
49};
50
51static const io_uuid_spec_t bl33_uuid_spec = {
52 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
53};
54
Jit Loon Limc5a3e3a2023-10-16 00:19:34 +080055# if ARM_LINUX_KERNEL_AS_BL33 != 0
56static const io_uuid_spec_t nt_fw_config_uuid_spec = {
57 .uuid = UUID_NT_FW_CONFIG,
58};
59# endif
60
Hadi Asyrafi616da772019-06-27 11:34:03 +080061uintptr_t a2_lba_offset;
62const char a2[] = {0xa2, 0x0};
63
64static const io_block_spec_t gpt_block_spec = {
65 .offset = 0,
66 .length = MMC_BLOCK_SIZE
67};
68
69static int check_fip(const uintptr_t spec);
70static int check_dev(const uintptr_t spec);
71
72static io_block_dev_spec_t boot_dev_spec;
73static int (*register_io_dev)(const io_dev_connector_t **);
74
75static io_block_spec_t fip_spec = {
76 .offset = PLAT_FIP_BASE,
77 .length = PLAT_FIP_MAX_SIZE,
78};
79
80struct plat_io_policy {
81 uintptr_t *dev_handle;
82 uintptr_t image_spec;
83 int (*check)(const uintptr_t spec);
84};
85
86static const struct plat_io_policy policies[] = {
87 [FIP_IMAGE_ID] = {
88 &boot_dev_handle,
89 (uintptr_t)&fip_spec,
90 check_dev
91 },
92 [BL2_IMAGE_ID] = {
93 &fip_dev_handle,
94 (uintptr_t)&bl2_uuid_spec,
95 check_fip
96 },
97 [BL31_IMAGE_ID] = {
98 &fip_dev_handle,
99 (uintptr_t)&bl31_uuid_spec,
100 check_fip
101 },
102 [BL33_IMAGE_ID] = {
103 &fip_dev_handle,
104 (uintptr_t) &bl33_uuid_spec,
105 check_fip
106 },
Jit Loon Limc5a3e3a2023-10-16 00:19:34 +0800107# if ARM_LINUX_KERNEL_AS_BL33 != 0
108 [NT_FW_CONFIG_ID] = {
109 &fip_dev_handle,
110 (uintptr_t)&nt_fw_config_uuid_spec,
111 check_fip
112 },
113# endif
Hadi Asyrafi616da772019-06-27 11:34:03 +0800114 [GPT_IMAGE_ID] = {
115 &boot_dev_handle,
116 (uintptr_t) &gpt_block_spec,
117 check_dev
118 },
119};
120
121static int check_dev(const uintptr_t spec)
122{
123 int result;
124 uintptr_t local_handle;
125
126 result = io_dev_init(boot_dev_handle, (uintptr_t)NULL);
127 if (result == 0) {
128 result = io_open(boot_dev_handle, spec, &local_handle);
129 if (result == 0)
130 io_close(local_handle);
131 }
132 return result;
133}
134
135static int check_fip(const uintptr_t spec)
136{
137 int result;
138 uintptr_t local_image_handle;
139
140 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
141 if (result == 0) {
142 result = io_open(fip_dev_handle, spec, &local_image_handle);
143 if (result == 0)
144 io_close(local_image_handle);
145 }
146 return result;
147}
148
Mahesh Raoc2715992023-08-22 17:26:23 +0800149void socfpga_io_setup(int boot_source, unsigned long offset)
Hadi Asyrafi616da772019-06-27 11:34:03 +0800150{
151 int result;
Mahesh Raoc2715992023-08-22 17:26:23 +0800152 fip_spec.offset = offset;
Hadi Asyrafi616da772019-06-27 11:34:03 +0800153
154 switch (boot_source) {
155 case BOOT_SOURCE_SDMMC:
156 register_io_dev = &register_io_dev_block;
157 boot_dev_spec.buffer.offset = PLAT_MMC_DATA_BASE;
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800158 boot_dev_spec.buffer.length = SOCFPGA_MMC_BLOCK_SIZE;
Jit Loon Lima7f54942023-05-17 12:26:11 +0800159 boot_dev_spec.ops.read = SDMMC_READ_BLOCKS;
160 boot_dev_spec.ops.write = SDMMC_WRITE_BLOCKS;
Hadi Asyrafi616da772019-06-27 11:34:03 +0800161 boot_dev_spec.block_size = MMC_BLOCK_SIZE;
162 break;
163
164 case BOOT_SOURCE_QSPI:
165 register_io_dev = &register_io_dev_memmap;
Hadi Asyrafi616da772019-06-27 11:34:03 +0800166 break;
167
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800168#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
169 case BOOT_SOURCE_NAND:
170 register_io_dev = &register_io_dev_mtd;
171 nand_dev_spec.ops.init = cdns_nand_init_mtd;
172 nand_dev_spec.ops.read = cdns_nand_read;
173 nand_dev_spec.ops.write = NULL;
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800174 break;
175#endif
176
Hadi Asyrafi616da772019-06-27 11:34:03 +0800177 default:
178 ERROR("Unsupported boot source\n");
179 panic();
180 break;
181 }
182
183 result = (*register_io_dev)(&boot_dev_con);
184 assert(result == 0);
185
186 result = register_io_dev_fip(&fip_dev_con);
187 assert(result == 0);
188
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800189 if (boot_source == BOOT_SOURCE_NAND) {
190 result = io_dev_open(boot_dev_con, (uintptr_t)&nand_dev_spec,
191 &boot_dev_handle);
192 } else {
193 result = io_dev_open(boot_dev_con, (uintptr_t)&boot_dev_spec,
194 &boot_dev_handle);
195 }
Hadi Asyrafi616da772019-06-27 11:34:03 +0800196 assert(result == 0);
197
198 result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
199 assert(result == 0);
200
201 if (boot_source == BOOT_SOURCE_SDMMC) {
202 partition_init(GPT_IMAGE_ID);
203 fip_spec.offset = get_partition_entry(a2)->start;
204 }
205
206 (void)result;
207}
208
209int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
210 uintptr_t *image_spec)
211{
212 int result;
213 const struct plat_io_policy *policy;
214
215 assert(image_id < ARRAY_SIZE(policies));
216
217 policy = &policies[image_id];
218 result = policy->check(policy->image_spec);
219 assert(result == 0);
220
221 *image_spec = policy->image_spec;
222 *dev_handle = *(policy->dev_handle);
223
224 return result;
225}