xref: /rk3399_ARM-atf/plat/nxp/s32/s32g274ardb2/plat_io_storage.c (revision 06f3c7058c42a9f1a9f7df75ea2de71a000855e8)
1 /*
2  * Copyright 2024 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <drivers/io/io_driver.h>
10 #include <drivers/io/io_fip.h>
11 #include <drivers/io/io_memmap.h>
12 #include <plat/common/platform.h>
13 #include <tools_share/firmware_image_package.h>
14 
15 #include <plat_io_storage.h>
16 
17 struct plat_io_policy {
18 	uintptr_t *dev_handle;
19 	uintptr_t image_spec;
20 	int (*check)(const uintptr_t spec);
21 };
22 
23 static int open_memmap(const uintptr_t spec);
24 static int open_fip(const uintptr_t spec);
25 
26 static uintptr_t fip_dev_handle;
27 
28 static uintptr_t memmap_dev_handle;
29 
30 static int open_memmap(const uintptr_t spec)
31 {
32 	uintptr_t temp_handle = 0U;
33 	int result;
34 
35 	result = io_dev_init(memmap_dev_handle, (uintptr_t)0);
36 	if (result != 0) {
37 		return result;
38 	}
39 
40 	result = io_open(memmap_dev_handle, spec, &temp_handle);
41 	if (result == 0) {
42 		(void)io_close(temp_handle);
43 	}
44 
45 	return result;
46 }
47 
48 static int open_fip(const uintptr_t spec)
49 {
50 	uintptr_t temp_handle = 0U;
51 	int result;
52 
53 	/* See if a Firmware Image Package is available */
54 	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
55 	if (result != 0) {
56 		return result;
57 	}
58 
59 	result = io_open(fip_dev_handle, spec, &temp_handle);
60 	if (result == 0) {
61 		(void)io_close(temp_handle);
62 	}
63 
64 	return result;
65 }
66 
67 void plat_s32g2_io_setup(void)
68 {
69 	static const io_dev_connector_t *memmap_dev_con;
70 	static const io_dev_connector_t *fip_dev_con;
71 
72 	int result __unused;
73 
74 	result = register_io_dev_memmap(&memmap_dev_con);
75 	assert(result == 0);
76 
77 	result = io_dev_open(memmap_dev_con, (uintptr_t)0,
78 			     &memmap_dev_handle);
79 	assert(result == 0);
80 
81 	result = register_io_dev_fip(&fip_dev_con);
82 	assert(result == 0);
83 
84 	result = io_dev_open(fip_dev_con, (uintptr_t)0,
85 			     &fip_dev_handle);
86 	assert(result == 0);
87 }
88 
89 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
90 			  uintptr_t *image_spec)
91 {
92 	static const io_block_spec_t fip_block_spec = {
93 		.offset = S32G_FIP_BASE,
94 		.length = S32G_FIP_SIZE,
95 	};
96 
97 	static const io_uuid_spec_t bl31_uuid_spec = {
98 		.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
99 	};
100 
101 	static const io_uuid_spec_t bl33_uuid_spec = {
102 		.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
103 	};
104 
105 	static const struct plat_io_policy policies[BL33_IMAGE_ID + 1] = {
106 		[FIP_IMAGE_ID] = {
107 			.dev_handle = &memmap_dev_handle,
108 			.image_spec = (uintptr_t)&fip_block_spec,
109 			.check = open_memmap,
110 		},
111 		[BL31_IMAGE_ID] = {
112 			.dev_handle = &fip_dev_handle,
113 			.image_spec = (uintptr_t)&bl31_uuid_spec,
114 			.check = open_fip,
115 		},
116 		[BL33_IMAGE_ID] = {
117 			.dev_handle = &fip_dev_handle,
118 			.image_spec = (uintptr_t)&bl33_uuid_spec,
119 			.check = open_fip,
120 		},
121 	};
122 	const struct plat_io_policy *policy;
123 	int result;
124 
125 	assert(image_id < ARRAY_SIZE(policies));
126 
127 	policy = &policies[image_id];
128 	result = policy->check(policy->image_spec);
129 	assert(result == 0);
130 
131 	*image_spec = policy->image_spec;
132 	*dev_handle = *policy->dev_handle;
133 
134 	return result;
135 }
136