xref: /rk3399_ARM-atf/drivers/intel/soc/stratix10/io/s10_memmap_qspi.c (revision f5ba408c342d16ef11dfd34b758eeb7d14eca6fc)
1 /*
2  * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include <platform_def.h>
11 
12 #include <common/debug.h>
13 #include <drivers/io/io_driver.h>
14 #include <drivers/io/io_memmap.h>
15 #include <drivers/io/io_storage.h>
16 #include <lib/utils.h>
17 
18 #include "drivers/qspi/cadence_qspi.h"
19 
20 /* As we need to be able to keep state for seek, only one file can be open
21  * at a time. Make this a structure and point to the entity->info. When we
22  * can malloc memory we can change this to support more open files.
23  */
24 typedef struct {
25 	/* Use the 'in_use' flag as any value for base and file_pos could be
26 	 * valid.
27 	 */
28 	int		in_use;
29 	uintptr_t	base;
30 	size_t		file_pos;
31 	size_t		size;
32 } file_state_t;
33 
34 static file_state_t current_file = {0};
35 
36 /* Identify the device type as memmap */
37 static io_type_t device_type_memmap(void)
38 {
39 	return IO_TYPE_MEMMAP;
40 }
41 
42 /* Memmap device functions */
43 static int memmap_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
44 static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
45 			     io_entity_t *entity);
46 static int memmap_block_seek(io_entity_t *entity, int mode,
47 			     ssize_t offset);
48 static int memmap_block_len(io_entity_t *entity, size_t *length);
49 static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
50 			     size_t length, size_t *length_read);
51 static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
52 			      size_t length, size_t *length_written);
53 static int memmap_block_close(io_entity_t *entity);
54 static int memmap_dev_close(io_dev_info_t *dev_info);
55 
56 
57 static const io_dev_connector_t memmap_dev_connector = {
58 	.dev_open = memmap_dev_open
59 };
60 
61 
62 static const io_dev_funcs_t memmap_dev_funcs = {
63 	.type = device_type_memmap,
64 	.open = memmap_block_open,
65 	.seek = memmap_block_seek,
66 	.size = memmap_block_len,
67 	.read = memmap_block_read,
68 	.write = memmap_block_write,
69 	.close = memmap_block_close,
70 	.dev_init = NULL,
71 	.dev_close = memmap_dev_close,
72 };
73 
74 
75 /* No state associated with this device so structure can be const */
76 static const io_dev_info_t memmap_dev_info = {
77 	.funcs = &memmap_dev_funcs,
78 	.info = (uintptr_t)NULL
79 };
80 
81 
82 /* Open a connection to the memmap device */
83 static int memmap_dev_open(const uintptr_t dev_spec __unused,
84 			   io_dev_info_t **dev_info)
85 {
86 	assert(dev_info != NULL);
87 	*dev_info = (io_dev_info_t *)&memmap_dev_info; /* cast away const */
88 
89 	return 0;
90 }
91 
92 
93 
94 /* Close a connection to the memmap device */
95 static int memmap_dev_close(io_dev_info_t *dev_info)
96 {
97 	/* NOP */
98 	/* TODO: Consider tracking open files and cleaning them up here */
99 	return 0;
100 }
101 
102 
103 /* Open a file on the memmap device */
104 static int memmap_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
105 			     io_entity_t *entity)
106 {
107 	int result = -ENOMEM;
108 	const io_block_spec_t *block_spec = (io_block_spec_t *)spec;
109 
110 	/* Since we need to track open state for seek() we only allow one open
111 	 * spec at a time. When we have dynamic memory we can malloc and set
112 	 * entity->info.
113 	 */
114 	if (current_file.in_use == 0) {
115 		assert(block_spec != NULL);
116 		assert(entity != NULL);
117 
118 		current_file.in_use = 1;
119 		current_file.base = block_spec->offset;
120 		/* File cursor offset for seek and incremental reads etc. */
121 		current_file.file_pos = 0;
122 		current_file.size = block_spec->length;
123 		entity->info = (uintptr_t)&current_file;
124 		result = 0;
125 	} else {
126 		WARN("A Memmap device is already active. Close first.\n");
127 	}
128 
129 	return result;
130 }
131 
132 
133 /* Seek to a particular file offset on the memmap device */
134 static int memmap_block_seek(io_entity_t *entity, int mode, ssize_t offset)
135 {
136 	int result = -ENOENT;
137 	file_state_t *fp;
138 
139 	/* We only support IO_SEEK_SET for the moment. */
140 	if (mode == IO_SEEK_SET) {
141 		assert(entity != NULL);
142 
143 		fp = (file_state_t *) entity->info;
144 
145 		/* Assert that new file position is valid */
146 		assert((offset >= 0) && (offset < fp->size));
147 
148 		/* Reset file position */
149 		fp->file_pos = offset;
150 		result = 0;
151 	}
152 
153 	return result;
154 }
155 
156 
157 /* Return the size of a file on the memmap device */
158 static int memmap_block_len(io_entity_t *entity, size_t *length)
159 {
160 	assert(entity != NULL);
161 	assert(length != NULL);
162 
163 	*length = ((file_state_t *)entity->info)->size;
164 
165 	return 0;
166 }
167 
168 
169 /* Read data from a file on the memmap device */
170 static int memmap_block_read(io_entity_t *entity, uintptr_t buffer,
171 			     size_t length, size_t *length_read)
172 {
173 	file_state_t *fp;
174 	size_t pos_after;
175 
176 	assert(entity != NULL);
177 	assert(length_read != NULL);
178 
179 	fp = (file_state_t *) entity->info;
180 
181 	/* Assert that file position is valid for this read operation */
182 	pos_after = fp->file_pos + length;
183 	assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
184 
185 	//memcpy((void *)buffer, (void *)(fp->base + fp->file_pos), length);
186 	cad_qspi_read((void *)buffer, fp->base + fp->file_pos, length);
187 	*length_read = length;
188 
189 	/* Set file position after read */
190 	fp->file_pos = pos_after;
191 
192 	return 0;
193 }
194 
195 
196 /* Write data to a file on the memmap device */
197 static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer,
198 			      size_t length, size_t *length_written)
199 {
200 	file_state_t *fp;
201 	size_t pos_after;
202 
203 	assert(entity != NULL);
204 	assert(length_written != NULL);
205 
206 	fp = (file_state_t *) entity->info;
207 
208 	/* Assert that file position is valid for this write operation */
209 	pos_after = fp->file_pos + length;
210 	assert((pos_after >= fp->file_pos) && (pos_after <= fp->size));
211 
212 	memcpy((void *)(fp->base + fp->file_pos), (void *)buffer, length);
213 
214 	*length_written = length;
215 
216 	/* Set file position after write */
217 	fp->file_pos = pos_after;
218 
219 	return 0;
220 }
221 
222 
223 /* Close a file on the memmap device */
224 static int memmap_block_close(io_entity_t *entity)
225 {
226 	assert(entity != NULL);
227 
228 	entity->info = 0;
229 
230 	/* This would be a mem free() if we had malloc.*/
231 	zeromem((void *)&current_file, sizeof(current_file));
232 
233 	return 0;
234 }
235 
236 
237 /* Exported functions */
238 
239 /* Register the memmap driver with the IO abstraction */
240 int register_io_dev_memmap(const io_dev_connector_t **dev_con)
241 {
242 	int result;
243 
244 	assert(dev_con != NULL);
245 
246 	result = io_register_device(&memmap_dev_info);
247 	if (result == 0)
248 		*dev_con = &memmap_dev_connector;
249 
250 	return result;
251 }
252