uniphier_io_storage.c 9.51 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
8
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>
9
#include <stdint.h>
10
11
12
13
14
15
16
17
18
19

#include <platform_def.h>

#include <drivers/io/io_block.h>
#include <drivers/io/io_driver.h>
#include <drivers/io/io_fip.h>
#include <drivers/io/io_memmap.h>
#include <lib/utils_def.h>
#include <lib/xlat_tables/xlat_tables_v2.h>
#include <tools_share/firmware_image_package.h>
20
21
22

#include "uniphier.h"

23
#define UNIPHIER_ROM_REGION_BASE	0x00000000ULL
24
#define UNIPHIER_ROM_REGION_SIZE	0x04000000ULL
25

26
#define UNIPHIER_OCM_REGION_SIZE	0x00040000ULL
27

28
29
#define UNIPHIER_BLOCK_BUF_OFFSET	0x03000000UL
#define UNIPHIER_BLOCK_BUF_SIZE		0x00800000UL
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
static const io_dev_connector_t *uniphier_fip_dev_con;
static uintptr_t uniphier_fip_dev_handle;

static const io_dev_connector_t *uniphier_backend_dev_con;
static uintptr_t uniphier_backend_dev_handle;

static io_block_spec_t uniphier_fip_spec = {
	/* .offset will be set by the io_setup func */
	.length = 0x00200000,
};

static const io_uuid_spec_t uniphier_bl2_spec = {
	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
};

static const io_uuid_spec_t uniphier_scp_spec = {
	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
};

static const io_uuid_spec_t uniphier_bl31_spec = {
	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
};

static const io_uuid_spec_t uniphier_bl32_spec = {
	.uuid = UUID_SECURE_PAYLOAD_BL32,
};

static const io_uuid_spec_t uniphier_bl33_spec = {
	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
};

#if TRUSTED_BOARD_BOOT
static const io_uuid_spec_t uniphier_tb_fw_cert_spec = {
	.uuid = UUID_TRUSTED_BOOT_FW_CERT,
};

static const io_uuid_spec_t uniphier_trusted_key_cert_spec = {
	.uuid = UUID_TRUSTED_KEY_CERT,
};

static const io_uuid_spec_t uniphier_scp_fw_key_cert_spec = {
	.uuid = UUID_SCP_FW_KEY_CERT,
};

static const io_uuid_spec_t uniphier_soc_fw_key_cert_spec = {
	.uuid = UUID_SOC_FW_KEY_CERT,
};

static const io_uuid_spec_t uniphier_tos_fw_key_cert_spec = {
	.uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
};

static const io_uuid_spec_t uniphier_nt_fw_key_cert_spec = {
	.uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
};

static const io_uuid_spec_t uniphier_scp_fw_cert_spec = {
	.uuid = UUID_SCP_FW_CONTENT_CERT,
};

static const io_uuid_spec_t uniphier_soc_fw_cert_spec = {
	.uuid = UUID_SOC_FW_CONTENT_CERT,
};

static const io_uuid_spec_t uniphier_tos_fw_cert_spec = {
	.uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
};

static const io_uuid_spec_t uniphier_nt_fw_cert_spec = {
	.uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
};
#endif /* TRUSTED_BOARD_BOOT */

struct uniphier_io_policy {
	uintptr_t *dev_handle;
	uintptr_t image_spec;
	uintptr_t init_params;
};

static const struct uniphier_io_policy uniphier_io_policies[] = {
	[FIP_IMAGE_ID] = {
		.dev_handle = &uniphier_backend_dev_handle,
		.image_spec = (uintptr_t)&uniphier_fip_spec,
	},
	[BL2_IMAGE_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_bl2_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[SCP_BL2_IMAGE_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_scp_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[BL31_IMAGE_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_bl31_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[BL32_IMAGE_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_bl32_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[BL33_IMAGE_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_bl33_spec,
		.init_params = FIP_IMAGE_ID,
	},
#if TRUSTED_BOARD_BOOT
	[TRUSTED_BOOT_FW_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_tb_fw_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[TRUSTED_KEY_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_trusted_key_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[SCP_FW_KEY_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_scp_fw_key_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[SOC_FW_KEY_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_soc_fw_key_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[TRUSTED_OS_FW_KEY_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_tos_fw_key_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[NON_TRUSTED_FW_KEY_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_nt_fw_key_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[SCP_FW_CONTENT_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_scp_fw_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[SOC_FW_CONTENT_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_soc_fw_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_tos_fw_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
		.dev_handle = &uniphier_fip_dev_handle,
		.image_spec = (uintptr_t)&uniphier_nt_fw_cert_spec,
		.init_params = FIP_IMAGE_ID,
	},
#endif
};

194
195
196
static int uniphier_io_block_setup(size_t fip_offset,
				   struct io_block_dev_spec *block_dev_spec,
				   size_t buffer_offset)
197
198
199
200
201
{
	int ret;

	uniphier_fip_spec.offset = fip_offset;

202
203
204
205
206
207
	block_dev_spec->buffer.offset = buffer_offset;
	block_dev_spec->buffer.length = UNIPHIER_BLOCK_BUF_SIZE;

	ret = mmap_add_dynamic_region(block_dev_spec->buffer.offset,
				      block_dev_spec->buffer.offset,
				      block_dev_spec->buffer.length,
208
209
210
211
				      MT_MEMORY | MT_RW | MT_NS);
	if (ret)
		return ret;

212
213
214
215
	ret = register_io_dev_block(&uniphier_backend_dev_con);
	if (ret)
		return ret;

216
	return io_dev_open(uniphier_backend_dev_con, (uintptr_t)block_dev_spec,
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
			   &uniphier_backend_dev_handle);
}

static int uniphier_io_memmap_setup(size_t fip_offset)
{
	int ret;

	uniphier_fip_spec.offset = fip_offset;

	ret = mmap_add_dynamic_region(fip_offset, fip_offset,
				      uniphier_fip_spec.length,
				      MT_RO_DATA | MT_SECURE);
	if (ret)
		return ret;

	ret = register_io_dev_memmap(&uniphier_backend_dev_con);
	if (ret)
		return ret;

	return io_dev_open(uniphier_backend_dev_con, 0,
			   &uniphier_backend_dev_handle);
}

static int uniphier_io_fip_setup(void)
{
	int ret;

	ret = register_io_dev_fip(&uniphier_fip_dev_con);
	if (ret)
		return ret;

	return io_dev_open(uniphier_fip_dev_con, 0, &uniphier_fip_dev_handle);
}

251
static int uniphier_io_emmc_setup(unsigned int soc, size_t buffer_offset)
252
{
253
	struct io_block_dev_spec *block_dev_spec;
254
255
	int ret;

256
	ret = uniphier_emmc_init(soc, &block_dev_spec);
257
258
259
	if (ret)
		return ret;

260
	return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
261
262
}

263
static int uniphier_io_nand_setup(unsigned int soc, size_t buffer_offset)
264
{
265
	struct io_block_dev_spec *block_dev_spec;
266
267
	int ret;

268
	ret = uniphier_nand_init(soc, &block_dev_spec);
269
270
271
	if (ret)
		return ret;

272
	return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
273
274
}

275
static int uniphier_io_nor_setup(unsigned int soc_id, size_t buffer_offset)
276
277
278
279
{
	return uniphier_io_memmap_setup(0x70000);
}

280
281
282
283
284
285
286
static const uintptr_t uniphier_ocm_base[] = {
	[UNIPHIER_SOC_LD11] = 0x30000000,
	[UNIPHIER_SOC_LD20] = 0x30000000,
	[UNIPHIER_SOC_PXS3] = 0x30000000,
};

static int uniphier_io_rom_api_setup(unsigned int soc)
287
{
288
	uintptr_t ocm_base;
289
290
	int ret;

291
292
293
	assert(soc < ARRAY_SIZE(uniphier_ocm_base));
	ocm_base = uniphier_ocm_base[soc];

294
295
296
297
298
299
300
	ret = mmap_add_dynamic_region(UNIPHIER_ROM_REGION_BASE,
				      UNIPHIER_ROM_REGION_BASE,
				      UNIPHIER_ROM_REGION_SIZE,
				      MT_CODE | MT_SECURE);
	if (ret)
		return ret;

301
302
303
304
305
	/*
	 * on-chip SRAM region: should be DEVICE attribute because the USB
	 * load functions provided by the ROM use this memory region as a work
	 * area, but do not cater to cache coherency.
	 */
306
	ret = mmap_add_dynamic_region(ocm_base, ocm_base,
307
308
309
310
311
				      UNIPHIER_OCM_REGION_SIZE,
				      MT_DEVICE | MT_RW | MT_SECURE);
	if (ret)
		return ret;

312
313
314
315
316
317
318
319
320
321
322
323
324
325
	return 0;
}

static int uniphier_io_usb_setup(unsigned int soc, size_t buffer_offset)
{
	struct io_block_dev_spec *block_dev_spec;
	int ret;

	/* use ROM API for loading images from USB storage */
	ret = uniphier_io_rom_api_setup(soc);
	if (ret)
		return ret;

	ret = uniphier_usb_init(soc, &block_dev_spec);
326
327
328
	if (ret)
		return ret;

329
	return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
330
331
}

332
static int (* const uniphier_io_setup_table[])(unsigned int, size_t) = {
333
334
335
336
337
338
	[UNIPHIER_BOOT_DEVICE_EMMC] = uniphier_io_emmc_setup,
	[UNIPHIER_BOOT_DEVICE_NAND] = uniphier_io_nand_setup,
	[UNIPHIER_BOOT_DEVICE_NOR] = uniphier_io_nor_setup,
	[UNIPHIER_BOOT_DEVICE_USB] = uniphier_io_usb_setup,
};

339
int uniphier_io_setup(unsigned int soc_id, uintptr_t mem_base)
340
{
341
	int (*io_setup)(unsigned int soc_id, size_t buffer_offset);
342
343
344
345
346
347
348
349
	unsigned int boot_dev;
	int ret;

	boot_dev = uniphier_get_boot_device(soc_id);
	if (boot_dev == UNIPHIER_BOOT_DEVICE_RSV)
		return -EINVAL;

	io_setup = uniphier_io_setup_table[boot_dev];
350
	ret = io_setup(soc_id, mem_base + UNIPHIER_BLOCK_BUF_OFFSET);
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
	if (ret)
		return ret;

	ret = uniphier_io_fip_setup();
	if (ret)
		return ret;

	return 0;
}

int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
			  uintptr_t *image_spec)
{
	uintptr_t init_params;

	assert(image_id < ARRAY_SIZE(uniphier_io_policies));

368
	*dev_handle = *uniphier_io_policies[image_id].dev_handle;
369
370
371
372
373
	*image_spec = uniphier_io_policies[image_id].image_spec;
	init_params = uniphier_io_policies[image_id].init_params;

	return io_dev_init(*dev_handle, init_params);
}