| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 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 "qspi/cadence_qspi.h" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
typedef struct { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
int in_use; |
| 29 |
uintptr_t base; |
| 30 |
unsigned long long file_pos; |
| 31 |
unsigned long long size; |
| 32 |
} file_state_t; |
| 33 |
|
| 34 |
static file_state_t current_file = {0}; |
| 35 |
|
| 36 |
|
| 37 |
static io_type_t device_type_memmap(void) |
| 38 |
{ |
| 39 |
return IO_TYPE_MEMMAP; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 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 |
signed long long 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 |
|
| 76 |
static const io_dev_info_t memmap_dev_info = { |
| 77 |
.funcs = &memmap_dev_funcs, |
| 78 |
.info = (uintptr_t)NULL |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 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; |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static int memmap_dev_close(io_dev_info_t *dev_info) |
| 96 |
{ |
| 97 |
|
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 121 |
current_file.file_pos = 0; |
| 122 |
current_file.size = block_spec->length; |
| 123 |
entity->info = (uintptr_t)¤t_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 |
|
| 134 |
static int memmap_block_seek(io_entity_t *entity, int mode, |
| 135 |
signed long long offset) |
| 136 |
{ |
| 137 |
int result = -ENOENT; |
| 138 |
file_state_t *fp; |
| 139 |
|
| 140 |
|
| 141 |
if (mode == IO_SEEK_SET) { |
| 142 |
assert(entity != NULL); |
| 143 |
|
| 144 |
fp = (file_state_t *) entity->info; |
| 145 |
|
| 146 |
|
| 147 |
assert((offset >= 0) && |
| 148 |
((unsigned long long)offset < fp->size)); |
| 149 |
|
| 150 |
|
| 151 |
fp->file_pos = offset; |
| 152 |
result = 0; |
| 153 |
} |
| 154 |
|
| 155 |
return result; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static int memmap_block_len(io_entity_t *entity, size_t *length) |
| 161 |
{ |
| 162 |
assert(entity != NULL); |
| 163 |
assert(length != NULL); |
| 164 |
|
| 165 |
*length = ((file_state_t *)entity->info)->size; |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
static int memmap_block_read(io_entity_t *entity, uintptr_t buffer, |
| 173 |
size_t length, size_t *length_read) |
| 174 |
{ |
| 175 |
file_state_t *fp; |
| 176 |
unsigned long long pos_after; |
| 177 |
|
| 178 |
assert(entity != NULL); |
| 179 |
assert(length_read != NULL); |
| 180 |
|
| 181 |
fp = (file_state_t *) entity->info; |
| 182 |
|
| 183 |
|
| 184 |
pos_after = fp->file_pos + length; |
| 185 |
assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); |
| 186 |
|
| 187 |
|
| 188 |
cad_qspi_read((void *)buffer, fp->base + fp->file_pos, length); |
| 189 |
*length_read = length; |
| 190 |
|
| 191 |
|
| 192 |
fp->file_pos = pos_after; |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
static int memmap_block_write(io_entity_t *entity, const uintptr_t buffer, |
| 200 |
size_t length, size_t *length_written) |
| 201 |
{ |
| 202 |
file_state_t *fp; |
| 203 |
unsigned long long pos_after; |
| 204 |
|
| 205 |
assert(entity != NULL); |
| 206 |
assert(length_written != NULL); |
| 207 |
|
| 208 |
fp = (file_state_t *) entity->info; |
| 209 |
|
| 210 |
|
| 211 |
pos_after = fp->file_pos + length; |
| 212 |
assert((pos_after >= fp->file_pos) && (pos_after <= fp->size)); |
| 213 |
|
| 214 |
memcpy((void *)(fp->base + fp->file_pos), (void *)buffer, length); |
| 215 |
|
| 216 |
*length_written = length; |
| 217 |
|
| 218 |
|
| 219 |
fp->file_pos = pos_after; |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static int memmap_block_close(io_entity_t *entity) |
| 227 |
{ |
| 228 |
assert(entity != NULL); |
| 229 |
|
| 230 |
entity->info = 0; |
| 231 |
|
| 232 |
|
| 233 |
zeromem((void *)¤t_file, sizeof(current_file)); |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
int register_io_dev_memmap(const io_dev_connector_t **dev_con) |
| 243 |
{ |
| 244 |
int result; |
| 245 |
|
| 246 |
assert(dev_con != NULL); |
| 247 |
|
| 248 |
result = io_register_device(&memmap_dev_info); |
| 249 |
if (result == 0) |
| 250 |
*dev_con = &memmap_dev_connector; |
| 251 |
|
| 252 |
return result; |
| 253 |
} |