xref: /rk3399_ARM-atf/plat/hisilicon/hikey/hikey_io_storage.c (revision ff2743e544f0f82381ebb9dff8f14eacb837d2e0)
1 /*
2  * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch_helpers.h>
8 #include <assert.h>
9 #include <debug.h>
10 #include <emmc.h>
11 #include <errno.h>
12 #include <firmware_image_package.h>
13 #include <io_block.h>
14 #include <io_driver.h>
15 #include <io_fip.h>
16 #include <io_memmap.h>
17 #include <io_storage.h>
18 #include <mmio.h>
19 #include <platform_def.h>
20 #include <semihosting.h>	/* For FOPEN_MODE_... */
21 #include <string.h>
22 #include "hikey_private.h"
23 
24 #define EMMC_BLOCK_SHIFT			9
25 
26 /* Page 1024, since only a few pages before 2048 are used as partition table */
27 #define SERIALNO_EMMC_OFFSET			(1024 * 512)
28 
29 struct plat_io_policy {
30 	uintptr_t *dev_handle;
31 	uintptr_t image_spec;
32 	int (*check)(const uintptr_t spec);
33 };
34 
35 static const io_dev_connector_t *emmc_dev_con;
36 static uintptr_t emmc_dev_handle;
37 static const io_dev_connector_t *fip_dev_con;
38 static uintptr_t fip_dev_handle;
39 
40 static int check_emmc(const uintptr_t spec);
41 static int check_fip(const uintptr_t spec);
42 
43 static const io_block_spec_t emmc_fip_spec = {
44 	.offset		= HIKEY_FIP_BASE,
45 	.length		= HIKEY_FIP_MAX_SIZE,
46 };
47 
48 static const io_block_dev_spec_t emmc_dev_spec = {
49 	/* It's used as temp buffer in block driver. */
50 #ifdef IMAGE_BL1
51 	.buffer		= {
52 		.offset	= HIKEY_BL1_MMC_DATA_BASE,
53 		.length	= HIKEY_BL1_MMC_DATA_SIZE,
54 	},
55 #else
56 	.buffer		= {
57 		.offset	= HIKEY_MMC_DATA_BASE,
58 		.length	= HIKEY_MMC_DATA_SIZE,
59 	},
60 #endif
61 	.ops		= {
62 		.read	= emmc_read_blocks,
63 		.write	= emmc_write_blocks,
64 	},
65 	.block_size	= EMMC_BLOCK_SIZE,
66 };
67 
68 static const io_uuid_spec_t bl31_uuid_spec = {
69 	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
70 };
71 
72 static const io_uuid_spec_t bl32_uuid_spec = {
73 	.uuid = UUID_SECURE_PAYLOAD_BL32,
74 };
75 
76 static const io_uuid_spec_t bl32_extra1_uuid_spec = {
77 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
78 };
79 
80 static const io_uuid_spec_t bl32_extra2_uuid_spec = {
81 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
82 };
83 
84 static const io_uuid_spec_t bl33_uuid_spec = {
85 	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
86 };
87 
88 static const io_uuid_spec_t scp_bl2_uuid_spec = {
89 	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
90 };
91 
92 static const struct plat_io_policy policies[] = {
93 	[FIP_IMAGE_ID] = {
94 		&emmc_dev_handle,
95 		(uintptr_t)&emmc_fip_spec,
96 		check_emmc
97 	},
98 	[SCP_BL2_IMAGE_ID] = {
99 		&fip_dev_handle,
100 		(uintptr_t)&scp_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 	[BL32_IMAGE_ID] = {
109 		&fip_dev_handle,
110 		(uintptr_t)&bl32_uuid_spec,
111 		check_fip
112 	},
113 	[BL32_EXTRA1_IMAGE_ID] = {
114 		&fip_dev_handle,
115 		(uintptr_t)&bl32_extra1_uuid_spec,
116 		check_fip
117 	},
118 	[BL32_EXTRA2_IMAGE_ID] = {
119 		&fip_dev_handle,
120 		(uintptr_t)&bl32_extra2_uuid_spec,
121 		check_fip
122 	},
123 	[BL33_IMAGE_ID] = {
124 		&fip_dev_handle,
125 		(uintptr_t)&bl33_uuid_spec,
126 		check_fip
127 	}
128 };
129 
130 static int check_emmc(const uintptr_t spec)
131 {
132 	int result;
133 	uintptr_t local_handle;
134 
135 	result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL);
136 	if (result == 0) {
137 		result = io_open(emmc_dev_handle, spec, &local_handle);
138 		if (result == 0)
139 			io_close(local_handle);
140 	}
141 	return result;
142 }
143 
144 static int check_fip(const uintptr_t spec)
145 {
146 	int result;
147 	uintptr_t local_image_handle;
148 
149 	/* See if a Firmware Image Package is available */
150 	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
151 	if (result == 0) {
152 		result = io_open(fip_dev_handle, spec, &local_image_handle);
153 		if (result == 0) {
154 			VERBOSE("Using FIP\n");
155 			io_close(local_image_handle);
156 		}
157 	}
158 	return result;
159 }
160 
161 void hikey_io_setup(void)
162 {
163 	int result;
164 
165 	result = register_io_dev_block(&emmc_dev_con);
166 	assert(result == 0);
167 
168 	result = register_io_dev_fip(&fip_dev_con);
169 	assert(result == 0);
170 
171 	result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec,
172 			     &emmc_dev_handle);
173 	assert(result == 0);
174 
175 	result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
176 	assert(result == 0);
177 
178 	/* Ignore improbable errors in release builds */
179 	(void)result;
180 }
181 
182 /* Return an IO device handle and specification which can be used to access
183  * an image. Use this to enforce platform load policy
184  */
185 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
186 			  uintptr_t *image_spec)
187 {
188 	int result;
189 	const struct plat_io_policy *policy;
190 
191 	assert(image_id < ARRAY_SIZE(policies));
192 
193 	policy = &policies[image_id];
194 	result = policy->check(policy->image_spec);
195 	assert(result == 0);
196 
197 	*image_spec = policy->image_spec;
198 	*dev_handle = *(policy->dev_handle);
199 
200 	return result;
201 }
202