xref: /rk3399_ARM-atf/plat/hisilicon/hikey/hikey_io_storage.c (revision 6311f63de02ee04d93016242977ade4727089de8)
1 /*
2  * Copyright (c) 2017, 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 #if 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 bl2_uuid_spec = {
69 	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
70 };
71 
72 static const io_uuid_spec_t bl31_uuid_spec = {
73 	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
74 };
75 
76 static const io_uuid_spec_t bl33_uuid_spec = {
77 	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
78 };
79 
80 static const io_uuid_spec_t scp_bl2_uuid_spec = {
81 	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
82 };
83 
84 static const struct plat_io_policy policies[] = {
85 	[FIP_IMAGE_ID] = {
86 		&emmc_dev_handle,
87 		(uintptr_t)&emmc_fip_spec,
88 		check_emmc
89 	},
90 	[BL2_IMAGE_ID] = {
91 		&fip_dev_handle,
92 		(uintptr_t)&bl2_uuid_spec,
93 		check_fip
94 	},
95 	[SCP_BL2_IMAGE_ID] = {
96 		&fip_dev_handle,
97 		(uintptr_t)&scp_bl2_uuid_spec,
98 		check_fip
99 	},
100 	[BL31_IMAGE_ID] = {
101 		&fip_dev_handle,
102 		(uintptr_t)&bl31_uuid_spec,
103 		check_fip
104 	},
105 	[BL33_IMAGE_ID] = {
106 		&fip_dev_handle,
107 		(uintptr_t)&bl33_uuid_spec,
108 		check_fip
109 	}
110 };
111 
112 static int check_emmc(const uintptr_t spec)
113 {
114 	int result;
115 	uintptr_t local_handle;
116 
117 	result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL);
118 	if (result == 0) {
119 		result = io_open(emmc_dev_handle, spec, &local_handle);
120 		if (result == 0)
121 			io_close(local_handle);
122 	}
123 	return result;
124 }
125 
126 static int check_fip(const uintptr_t spec)
127 {
128 	int result;
129 	uintptr_t local_image_handle;
130 
131 	/* See if a Firmware Image Package is available */
132 	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
133 	if (result == 0) {
134 		result = io_open(fip_dev_handle, spec, &local_image_handle);
135 		if (result == 0) {
136 			VERBOSE("Using FIP\n");
137 			io_close(local_image_handle);
138 		}
139 	}
140 	return result;
141 }
142 
143 void hikey_io_setup(void)
144 {
145 	int result;
146 
147 	result = register_io_dev_block(&emmc_dev_con);
148 	assert(result == 0);
149 
150 	result = register_io_dev_fip(&fip_dev_con);
151 	assert(result == 0);
152 
153 	result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec,
154 			     &emmc_dev_handle);
155 	assert(result == 0);
156 
157 	result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
158 	assert(result == 0);
159 
160 	/* Ignore improbable errors in release builds */
161 	(void)result;
162 }
163 
164 /* Return an IO device handle and specification which can be used to access
165  * an image. Use this to enforce platform load policy
166  */
167 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
168 			  uintptr_t *image_spec)
169 {
170 	int result;
171 	const struct plat_io_policy *policy;
172 
173 	assert(image_id < ARRAY_SIZE(policies));
174 
175 	policy = &policies[image_id];
176 	result = policy->check(policy->image_spec);
177 	assert(result == 0);
178 
179 	*image_spec = policy->image_spec;
180 	*dev_handle = *(policy->dev_handle);
181 
182 	return result;
183 }
184