xref: /rk3399_ARM-atf/plat/st/common/bl2_io_storage.c (revision 73308618fee8afc4518c592956b31864e57e48e7)
1 /*
2  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include <platform_def.h>
11 
12 #include <arch_helpers.h>
13 #include <common/debug.h>
14 #include <drivers/io/io_block.h>
15 #include <drivers/io/io_driver.h>
16 #include <drivers/io/io_dummy.h>
17 #include <drivers/io/io_storage.h>
18 #include <drivers/mmc.h>
19 #include <drivers/partition/partition.h>
20 #include <drivers/st/io_mmc.h>
21 #include <drivers/st/io_stm32image.h>
22 #include <drivers/st/stm32_sdmmc2.h>
23 #include <lib/mmio.h>
24 #include <lib/utils.h>
25 #include <plat/common/platform.h>
26 
27 /* IO devices */
28 static const io_dev_connector_t *dummy_dev_con;
29 static uintptr_t dummy_dev_handle;
30 static uintptr_t dummy_dev_spec;
31 
32 static uintptr_t image_dev_handle;
33 
34 static io_block_spec_t gpt_block_spec = {
35 	.offset = 0,
36 	.length = 34 * MMC_BLOCK_SIZE, /* Size of GPT table */
37 };
38 
39 static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
40 
41 static const io_block_dev_spec_t mmc_block_dev_spec = {
42 	/* It's used as temp buffer in block driver */
43 	.buffer = {
44 		.offset = (size_t)&block_buffer,
45 		.length = MMC_BLOCK_SIZE,
46 	},
47 	.ops = {
48 		.read = mmc_read_blocks,
49 		.write = NULL,
50 	},
51 	.block_size = MMC_BLOCK_SIZE,
52 };
53 
54 static uintptr_t storage_dev_handle;
55 static const io_dev_connector_t *mmc_dev_con;
56 
57 static const io_block_spec_t bl32_block_spec = {
58 	.offset = BL32_BASE,
59 	.length = STM32MP_BL32_SIZE
60 };
61 
62 static const io_block_spec_t bl2_block_spec = {
63 	.offset = BL2_BASE,
64 	.length = STM32MP_BL2_SIZE,
65 };
66 
67 static const struct stm32image_part_info bl33_partition_spec = {
68 	.name = BL33_IMAGE_NAME,
69 	.binary_type = BL33_BINARY_TYPE,
70 };
71 
72 enum {
73 	IMG_IDX_BL33,
74 	IMG_IDX_NUM
75 };
76 
77 static struct stm32image_device_info stm32image_dev_info_spec = {
78 	.lba_size = MMC_BLOCK_SIZE,
79 	.part_info[IMG_IDX_BL33] = {
80 		.name = BL33_IMAGE_NAME,
81 		.binary_type = BL33_BINARY_TYPE,
82 	},
83 };
84 
85 static io_block_spec_t stm32image_block_spec = {
86 	.offset = 0,
87 	.length = 0,
88 };
89 
90 static const io_dev_connector_t *stm32image_dev_con;
91 
92 static int open_dummy(const uintptr_t spec);
93 static int open_image(const uintptr_t spec);
94 static int open_storage(const uintptr_t spec);
95 
96 struct plat_io_policy {
97 	uintptr_t *dev_handle;
98 	uintptr_t image_spec;
99 	int (*check)(const uintptr_t spec);
100 };
101 
102 static const struct plat_io_policy policies[] = {
103 	[BL2_IMAGE_ID] = {
104 		.dev_handle = &dummy_dev_handle,
105 		.image_spec = (uintptr_t)&bl2_block_spec,
106 		.check = open_dummy
107 	},
108 	[BL32_IMAGE_ID] = {
109 		.dev_handle = &dummy_dev_handle,
110 		.image_spec = (uintptr_t)&bl32_block_spec,
111 		.check = open_dummy
112 	},
113 	[BL33_IMAGE_ID] = {
114 		.dev_handle = &image_dev_handle,
115 		.image_spec = (uintptr_t)&bl33_partition_spec,
116 		.check = open_image
117 	},
118 	[GPT_IMAGE_ID] = {
119 		.dev_handle = &storage_dev_handle,
120 		.image_spec = (uintptr_t)&gpt_block_spec,
121 		.check = open_storage
122 	},
123 	[STM32_IMAGE_ID] = {
124 		.dev_handle = &storage_dev_handle,
125 		.image_spec = (uintptr_t)&stm32image_block_spec,
126 		.check = open_storage
127 	}
128 };
129 
130 static int open_dummy(const uintptr_t spec)
131 {
132 	return io_dev_init(dummy_dev_handle, 0);
133 }
134 
135 static int open_image(const uintptr_t spec)
136 {
137 	return io_dev_init(image_dev_handle, 0);
138 }
139 
140 static int open_storage(const uintptr_t spec)
141 {
142 	return io_dev_init(storage_dev_handle, 0);
143 }
144 
145 static void print_boot_device(boot_api_context_t *boot_context)
146 {
147 	switch (boot_context->boot_interface_selected) {
148 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
149 		INFO("Using SDMMC\n");
150 		break;
151 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
152 		INFO("Using EMMC\n");
153 		break;
154 	default:
155 		ERROR("Boot interface not found\n");
156 		panic();
157 		break;
158 	}
159 
160 	if (boot_context->boot_interface_instance != 0U) {
161 		INFO("  Instance %d\n", boot_context->boot_interface_instance);
162 	}
163 }
164 
165 void stm32mp_io_setup(void)
166 {
167 	int io_result __unused;
168 	uint8_t idx;
169 	struct stm32image_part_info *part;
170 	struct stm32_sdmmc2_params params;
171 	struct mmc_device_info device_info;
172 	uintptr_t mmc_default_instance;
173 	const partition_entry_t *entry;
174 	boot_api_context_t *boot_context =
175 		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
176 
177 	print_boot_device(boot_context);
178 
179 	if ((boot_context->boot_partition_used_toboot == 1U) ||
180 	    (boot_context->boot_partition_used_toboot == 2U)) {
181 		INFO("Boot used partition fsbl%d\n",
182 		     boot_context->boot_partition_used_toboot);
183 	}
184 
185 	io_result = register_io_dev_dummy(&dummy_dev_con);
186 	assert(io_result == 0);
187 
188 	io_result = io_dev_open(dummy_dev_con, dummy_dev_spec,
189 				&dummy_dev_handle);
190 	assert(io_result == 0);
191 
192 	switch (boot_context->boot_interface_selected) {
193 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
194 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
195 		dmbsy();
196 
197 		memset(&params, 0, sizeof(struct stm32_sdmmc2_params));
198 
199 		if (boot_context->boot_interface_selected ==
200 		    BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC) {
201 			device_info.mmc_dev_type = MMC_IS_EMMC;
202 			mmc_default_instance = STM32MP_SDMMC2_BASE;
203 		} else {
204 			device_info.mmc_dev_type = MMC_IS_SD;
205 			mmc_default_instance = STM32MP_SDMMC1_BASE;
206 		}
207 
208 		switch (boot_context->boot_interface_instance) {
209 		case 1:
210 			params.reg_base = STM32MP_SDMMC1_BASE;
211 			break;
212 		case 2:
213 			params.reg_base = STM32MP_SDMMC2_BASE;
214 			break;
215 		case 3:
216 			params.reg_base = STM32MP_SDMMC3_BASE;
217 			break;
218 		default:
219 			WARN("SDMMC instance not found, using default\n");
220 			params.reg_base = mmc_default_instance;
221 			break;
222 		}
223 
224 		params.device_info = &device_info;
225 		if (stm32_sdmmc2_mmc_init(&params) != 0) {
226 			ERROR("SDMMC%u init failed\n",
227 			      boot_context->boot_interface_instance);
228 			panic();
229 		}
230 
231 		/* Open MMC as a block device to read GPT table */
232 		io_result = register_io_dev_block(&mmc_dev_con);
233 		if (io_result != 0) {
234 			panic();
235 		}
236 
237 		io_result = io_dev_open(mmc_dev_con,
238 					(uintptr_t)&mmc_block_dev_spec,
239 					&storage_dev_handle);
240 		assert(io_result == 0);
241 
242 		partition_init(GPT_IMAGE_ID);
243 
244 		io_result = io_dev_close(storage_dev_handle);
245 		assert(io_result == 0);
246 
247 		stm32image_dev_info_spec.device_size =
248 			stm32_sdmmc2_mmc_get_device_size();
249 
250 		for (idx = 0U; idx < IMG_IDX_NUM; idx++) {
251 			part = &stm32image_dev_info_spec.part_info[idx];
252 			entry = get_partition_entry(part->name);
253 			if (entry == NULL) {
254 				ERROR("Partition %s not found\n",
255 				      part->name);
256 				panic();
257 			}
258 
259 			part->part_offset = entry->start;
260 			part->bkp_offset = 0U;
261 		}
262 
263 		/*
264 		 * Re-open MMC with io_mmc, for better perfs compared to
265 		 * io_block.
266 		 */
267 		io_result = register_io_dev_mmc(&mmc_dev_con);
268 		assert(io_result == 0);
269 
270 		io_result = io_dev_open(mmc_dev_con, 0, &storage_dev_handle);
271 		assert(io_result == 0);
272 
273 		io_result = register_io_dev_stm32image(&stm32image_dev_con);
274 		assert(io_result == 0);
275 
276 		io_result = io_dev_open(stm32image_dev_con,
277 					(uintptr_t)&stm32image_dev_info_spec,
278 					&image_dev_handle);
279 		assert(io_result == 0);
280 		break;
281 
282 	default:
283 		ERROR("Boot interface %d not supported\n",
284 		      boot_context->boot_interface_selected);
285 		break;
286 	}
287 }
288 
289 /*
290  * Return an IO device handle and specification which can be used to access
291  * an image. Use this to enforce platform load policy.
292  */
293 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
294 			  uintptr_t *image_spec)
295 {
296 	int rc;
297 	const struct plat_io_policy *policy;
298 
299 	assert(image_id < ARRAY_SIZE(policies));
300 
301 	policy = &policies[image_id];
302 	rc = policy->check(policy->image_spec);
303 	if (rc == 0) {
304 		*image_spec = policy->image_spec;
305 		*dev_handle = *(policy->dev_handle);
306 	}
307 
308 	return rc;
309 }
310