1 /*
2 * Copyright 2024-2025 NXP
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8
9 #include <drivers/io/io_block.h>
10 #include <drivers/io/io_driver.h>
11 #include <drivers/io/io_fip.h>
12 #include <drivers/io/io_memmap.h>
13 #include <drivers/mmc.h>
14 #include <drivers/partition/partition.h>
15 #include <lib/utils.h>
16 #include <lib/xlat_tables/xlat_tables_v2.h>
17 #include <plat/common/platform.h>
18 #include <tools_share/firmware_image_package.h>
19
20 #include <plat_io_storage.h>
21
22 struct plat_io_policy {
23 uintptr_t *dev_handle;
24 uintptr_t image_spec;
25 int (*check)(const uintptr_t spec);
26 };
27
28 static uintptr_t fip_dev_handle;
29
30 static io_block_spec_t fip_mmc_spec;
31
32 static uintptr_t mmc_dev_handle;
33
open_mmc(const uintptr_t spec)34 static int open_mmc(const uintptr_t spec)
35 {
36 uintptr_t temp_handle;
37 int result;
38
39 result = io_dev_init(mmc_dev_handle, (uintptr_t)0U);
40 if (result != 0) {
41 return result;
42 }
43
44 result = io_open(mmc_dev_handle, spec, &temp_handle);
45 if (result == 0) {
46 (void)io_close(temp_handle);
47 }
48
49 return result;
50 }
51
open_fip(const uintptr_t spec)52 static int open_fip(const uintptr_t spec)
53 {
54 uintptr_t temp_handle = 0U;
55 int result;
56
57 /* See if a Firmware Image Package is available */
58 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
59 if (result != 0) {
60 return result;
61 }
62
63 result = io_open(fip_dev_handle, spec, &temp_handle);
64 if (result == 0) {
65 (void)io_close(temp_handle);
66 }
67
68 return result;
69 }
70
plat_s32g2_io_setup(void)71 void plat_s32g2_io_setup(void)
72 {
73 static const io_block_dev_spec_t mmc_dev_spec = {
74 /* It's used as temp buffer in block driver. */
75 .buffer = {
76 .offset = IO_BUFFER_BASE,
77 .length = IO_BUFFER_SIZE,
78 },
79 .ops = {
80 .read = mmc_read_blocks,
81 .write = mmc_write_blocks,
82 },
83 .block_size = MMC_BLOCK_SIZE,
84 };
85 static const io_dev_connector_t *fip_dev_con;
86 static const io_dev_connector_t *mmc_dev_con;
87
88 partition_entry_t fip_part;
89 uintptr_t io_buf_base;
90 int result __unused;
91 size_t io_buf_size;
92 int ret;
93
94 io_buf_base = mmc_dev_spec.buffer.offset;
95 io_buf_size = mmc_dev_spec.buffer.length;
96
97 ret = mmap_add_dynamic_region(io_buf_base, io_buf_base,
98 io_buf_size,
99 MT_MEMORY | MT_RW | MT_SECURE);
100 if (ret != 0) {
101 ERROR("Failed to map the IO buffer\n");
102 panic();
103 }
104
105 result = register_io_dev_block(&mmc_dev_con);
106 assert(result == 0);
107
108 result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_dev_spec,
109 &mmc_dev_handle);
110 assert(result == 0);
111
112 result = register_io_dev_fip(&fip_dev_con);
113 assert(result == 0);
114
115 result = io_dev_open(fip_dev_con, (uintptr_t)0,
116 &fip_dev_handle);
117 assert(result == 0);
118
119 ret = gpt_partition_init();
120 if (ret != 0) {
121 ERROR("Could not load MBR partition table\n");
122 panic();
123 }
124
125 fip_part = get_partition_entry_list()->list[FIP_PART];
126 fip_mmc_spec.offset = fip_part.start;
127 fip_mmc_spec.length = fip_part.length;
128 }
129
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)130 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
131 uintptr_t *image_spec)
132 {
133 static const io_block_spec_t mbr_spec = {
134 .offset = 0,
135 .length = PLAT_PARTITION_BLOCK_SIZE,
136 };
137
138 static const io_uuid_spec_t bl31_uuid_spec = {
139 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
140 };
141
142 static const io_uuid_spec_t bl33_uuid_spec = {
143 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
144 };
145
146 static const struct plat_io_policy policies[GPT_IMAGE_ID + 1] = {
147 [FIP_IMAGE_ID] = {
148 .dev_handle = &mmc_dev_handle,
149 .image_spec = (uintptr_t)&fip_mmc_spec,
150 .check = open_mmc,
151 },
152 [BL31_IMAGE_ID] = {
153 .dev_handle = &fip_dev_handle,
154 .image_spec = (uintptr_t)&bl31_uuid_spec,
155 .check = open_fip,
156 },
157 [BL33_IMAGE_ID] = {
158 .dev_handle = &fip_dev_handle,
159 .image_spec = (uintptr_t)&bl33_uuid_spec,
160 .check = open_fip,
161 },
162 [GPT_IMAGE_ID] = {
163 .dev_handle = &mmc_dev_handle,
164 .image_spec = (uintptr_t)&mbr_spec,
165 .check = open_mmc,
166 },
167 };
168 const struct plat_io_policy *policy;
169 int result;
170
171 assert(image_id < ARRAY_SIZE(policies));
172
173 policy = &policies[image_id];
174 result = policy->check(policy->image_spec);
175 assert(result == 0);
176
177 *image_spec = policy->image_spec;
178 *dev_handle = *policy->dev_handle;
179
180 return result;
181 }
182