blob: e80f0747d2d9400e85939f610a731792660d3dce [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"
Hadi Asyrafi616da772019-06-27 11:34:03 +080027
Jit Loon Limb24dddf2023-05-17 12:26:11 +080028
Hadi Asyrafi616da772019-06-27 11:34:03 +080029#define PLAT_FIP_BASE (0)
30#define PLAT_FIP_MAX_SIZE (0x1000000)
31#define PLAT_MMC_DATA_BASE (0xffe3c000)
32#define PLAT_MMC_DATA_SIZE (0x2000)
33#define PLAT_QSPI_DATA_BASE (0x3C00000)
34#define PLAT_QSPI_DATA_SIZE (0x1000000)
Jit Loon Limb24dddf2023-05-17 12:26:11 +080035#define PLAT_NAND_DATA_BASE (0x0200000)
36#define PLAT_NAND_DATA_SIZE (0x1000000)
Hadi Asyrafi616da772019-06-27 11:34:03 +080037
38static const io_dev_connector_t *fip_dev_con;
39static const io_dev_connector_t *boot_dev_con;
40
Jit Loon Limb24dddf2023-05-17 12:26:11 +080041static io_mtd_dev_spec_t nand_dev_spec;
42
Hadi Asyrafi616da772019-06-27 11:34:03 +080043static uintptr_t fip_dev_handle;
44static uintptr_t boot_dev_handle;
45
46static const io_uuid_spec_t bl2_uuid_spec = {
47 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
48};
49
50static const io_uuid_spec_t bl31_uuid_spec = {
51 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
52};
53
54static const io_uuid_spec_t bl33_uuid_spec = {
55 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
56};
57
58uintptr_t a2_lba_offset;
59const char a2[] = {0xa2, 0x0};
60
61static const io_block_spec_t gpt_block_spec = {
62 .offset = 0,
63 .length = MMC_BLOCK_SIZE
64};
65
66static int check_fip(const uintptr_t spec);
67static int check_dev(const uintptr_t spec);
68
69static io_block_dev_spec_t boot_dev_spec;
70static int (*register_io_dev)(const io_dev_connector_t **);
71
72static io_block_spec_t fip_spec = {
73 .offset = PLAT_FIP_BASE,
74 .length = PLAT_FIP_MAX_SIZE,
75};
76
77struct plat_io_policy {
78 uintptr_t *dev_handle;
79 uintptr_t image_spec;
80 int (*check)(const uintptr_t spec);
81};
82
83static const struct plat_io_policy policies[] = {
84 [FIP_IMAGE_ID] = {
85 &boot_dev_handle,
86 (uintptr_t)&fip_spec,
87 check_dev
88 },
89 [BL2_IMAGE_ID] = {
90 &fip_dev_handle,
91 (uintptr_t)&bl2_uuid_spec,
92 check_fip
93 },
94 [BL31_IMAGE_ID] = {
95 &fip_dev_handle,
96 (uintptr_t)&bl31_uuid_spec,
97 check_fip
98 },
99 [BL33_IMAGE_ID] = {
100 &fip_dev_handle,
101 (uintptr_t) &bl33_uuid_spec,
102 check_fip
103 },
104 [GPT_IMAGE_ID] = {
105 &boot_dev_handle,
106 (uintptr_t) &gpt_block_spec,
107 check_dev
108 },
109};
110
111static int check_dev(const uintptr_t spec)
112{
113 int result;
114 uintptr_t local_handle;
115
116 result = io_dev_init(boot_dev_handle, (uintptr_t)NULL);
117 if (result == 0) {
118 result = io_open(boot_dev_handle, spec, &local_handle);
119 if (result == 0)
120 io_close(local_handle);
121 }
122 return result;
123}
124
125static int check_fip(const uintptr_t spec)
126{
127 int result;
128 uintptr_t local_image_handle;
129
130 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
131 if (result == 0) {
132 result = io_open(fip_dev_handle, spec, &local_image_handle);
133 if (result == 0)
134 io_close(local_image_handle);
135 }
136 return result;
137}
138
139void socfpga_io_setup(int boot_source)
140{
141 int result;
142
143 switch (boot_source) {
144 case BOOT_SOURCE_SDMMC:
145 register_io_dev = &register_io_dev_block;
146 boot_dev_spec.buffer.offset = PLAT_MMC_DATA_BASE;
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800147 boot_dev_spec.buffer.length = SOCFPGA_MMC_BLOCK_SIZE;
Jit Loon Lima7f54942023-05-17 12:26:11 +0800148 boot_dev_spec.ops.read = SDMMC_READ_BLOCKS;
149 boot_dev_spec.ops.write = SDMMC_WRITE_BLOCKS;
Hadi Asyrafi616da772019-06-27 11:34:03 +0800150 boot_dev_spec.block_size = MMC_BLOCK_SIZE;
151 break;
152
153 case BOOT_SOURCE_QSPI:
154 register_io_dev = &register_io_dev_memmap;
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800155 fip_spec.offset = PLAT_QSPI_DATA_BASE;
Hadi Asyrafi616da772019-06-27 11:34:03 +0800156 break;
157
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800158#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
159 case BOOT_SOURCE_NAND:
160 register_io_dev = &register_io_dev_mtd;
161 nand_dev_spec.ops.init = cdns_nand_init_mtd;
162 nand_dev_spec.ops.read = cdns_nand_read;
163 nand_dev_spec.ops.write = NULL;
164 fip_spec.offset = PLAT_NAND_DATA_BASE;
165 break;
166#endif
167
Hadi Asyrafi616da772019-06-27 11:34:03 +0800168 default:
169 ERROR("Unsupported boot source\n");
170 panic();
171 break;
172 }
173
174 result = (*register_io_dev)(&boot_dev_con);
175 assert(result == 0);
176
177 result = register_io_dev_fip(&fip_dev_con);
178 assert(result == 0);
179
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800180 if (boot_source == BOOT_SOURCE_NAND) {
181 result = io_dev_open(boot_dev_con, (uintptr_t)&nand_dev_spec,
182 &boot_dev_handle);
183 } else {
184 result = io_dev_open(boot_dev_con, (uintptr_t)&boot_dev_spec,
185 &boot_dev_handle);
186 }
Hadi Asyrafi616da772019-06-27 11:34:03 +0800187 assert(result == 0);
188
189 result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
190 assert(result == 0);
191
192 if (boot_source == BOOT_SOURCE_SDMMC) {
193 partition_init(GPT_IMAGE_ID);
194 fip_spec.offset = get_partition_entry(a2)->start;
195 }
196
197 (void)result;
198}
199
200int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
201 uintptr_t *image_spec)
202{
203 int result;
204 const struct plat_io_policy *policy;
205
206 assert(image_id < ARRAY_SIZE(policies));
207
208 policy = &policies[image_id];
209 result = policy->check(policy->image_spec);
210 assert(result == 0);
211
212 *image_spec = policy->image_spec;
213 *dev_handle = *(policy->dev_handle);
214
215 return result;
216}