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 <errno.h> 9 #include <string.h> 10 11 #include <platform_def.h> 12 13 #include <common/debug.h> 14 #include <drivers/io/io_driver.h> 15 #include <drivers/io/io_mtd.h> 16 #include <lib/utils.h> 17 18 typedef struct { 19 io_mtd_dev_spec_t *dev_spec; 20 uintptr_t base; 21 unsigned long long offset; /* Offset in bytes */ 22 unsigned long long size; /* Size of device in bytes */ 23 } mtd_dev_state_t; 24 25 io_type_t device_type_mtd(void); 26 27 static int mtd_open(io_dev_info_t *dev_info, const uintptr_t spec, 28 io_entity_t *entity); 29 static int mtd_seek(io_entity_t *entity, int mode, signed long long offset); 30 static int mtd_read(io_entity_t *entity, uintptr_t buffer, size_t length, 31 size_t *length_read); 32 static int mtd_close(io_entity_t *entity); 33 static int mtd_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); 34 static int mtd_dev_close(io_dev_info_t *dev_info); 35 36 static const io_dev_connector_t mtd_dev_connector = { 37 .dev_open = mtd_dev_open 38 }; 39 40 static const io_dev_funcs_t mtd_dev_funcs = { 41 .type = device_type_mtd, 42 .open = mtd_open, 43 .seek = mtd_seek, 44 .read = mtd_read, 45 .close = mtd_close, 46 .dev_close = mtd_dev_close, 47 }; 48 49 static mtd_dev_state_t state_pool[MAX_IO_MTD_DEVICES]; 50 static io_dev_info_t dev_info_pool[MAX_IO_MTD_DEVICES]; 51 52 io_type_t device_type_mtd(void) 53 { 54 return IO_TYPE_MTD; 55 } 56 57 /* Locate a MTD state in the pool, specified by address */ 58 static int find_first_mtd_state(const io_mtd_dev_spec_t *dev_spec, 59 unsigned int *index_out) 60 { 61 unsigned int index; 62 int result = -ENOENT; 63 64 for (index = 0U; index < MAX_IO_MTD_DEVICES; index++) { 65 /* dev_spec is used as identifier since it's unique */ 66 if (state_pool[index].dev_spec == dev_spec) { 67 result = 0; 68 *index_out = index; 69 break; 70 } 71 } 72 73 return result; 74 } 75 76 /* Allocate a device info from the pool */ 77 static int allocate_dev_info(io_dev_info_t **dev_info) 78 { 79 unsigned int index = 0U; 80 int result; 81 82 result = find_first_mtd_state(NULL, &index); 83 if (result != 0) { 84 return -ENOMEM; 85 } 86 87 dev_info_pool[index].funcs = &mtd_dev_funcs; 88 dev_info_pool[index].info = (uintptr_t)&state_pool[index]; 89 *dev_info = &dev_info_pool[index]; 90 91 return 0; 92 } 93 94 /* Release a device info from the pool */ 95 static int free_dev_info(io_dev_info_t *dev_info) 96 { 97 int result; 98 unsigned int index = 0U; 99 mtd_dev_state_t *state; 100 101 state = (mtd_dev_state_t *)dev_info->info; 102 result = find_first_mtd_state(state->dev_spec, &index); 103 if (result != 0) { 104 return result; 105 } 106 107 zeromem(state, sizeof(mtd_dev_state_t)); 108 zeromem(dev_info, sizeof(io_dev_info_t)); 109 110 return 0; 111 } 112 113 static int mtd_open(io_dev_info_t *dev_info, const uintptr_t spec, 114 io_entity_t *entity) 115 { 116 mtd_dev_state_t *cur; 117 118 assert((dev_info->info != 0UL) && (entity->info == 0UL)); 119 120 cur = (mtd_dev_state_t *)dev_info->info; 121 entity->info = (uintptr_t)cur; 122 cur->offset = 0U; 123 124 return 0; 125 } 126 127 /* Seek to a specific position using offset */ 128 static int mtd_seek(io_entity_t *entity, int mode, signed long long offset) 129 { 130 mtd_dev_state_t *cur; 131 132 assert((entity->info != (uintptr_t)NULL) && (offset >= 0)); 133 134 cur = (mtd_dev_state_t *)entity->info; 135 136 switch (mode) { 137 case IO_SEEK_SET: 138 if ((offset >= 0) && 139 ((unsigned long long)offset >= cur->size)) { 140 return -EINVAL; 141 } 142 143 cur->offset = offset; 144 break; 145 case IO_SEEK_CUR: 146 if (((cur->offset + (unsigned long long)offset) >= 147 cur->size) || 148 ((cur->offset + (unsigned long long)offset) < 149 cur->offset)) { 150 return -EINVAL; 151 } 152 153 cur->offset += (unsigned long long)offset; 154 break; 155 default: 156 return -EINVAL; 157 } 158 159 return 0; 160 } 161 162 static int mtd_read(io_entity_t *entity, uintptr_t buffer, size_t length, 163 size_t *out_length) 164 { 165 mtd_dev_state_t *cur; 166 io_mtd_ops_t *ops; 167 int ret; 168 169 assert(entity->info != (uintptr_t)NULL); 170 assert((length > 0U) && (buffer != (uintptr_t)NULL)); 171 172 cur = (mtd_dev_state_t *)entity->info; 173 ops = &cur->dev_spec->ops; 174 assert(ops->read != NULL); 175 176 VERBOSE("Read at %llx into %lx, length %zi\n", 177 cur->offset, buffer, length); 178 if ((cur->offset + length) > cur->dev_spec->device_size) { 179 return -EINVAL; 180 } 181 182 ret = ops->read(cur->offset, buffer, length, out_length); 183 if (ret < 0) { 184 return ret; 185 } 186 187 assert(*out_length == length); 188 cur->offset += *out_length; 189 190 return 0; 191 } 192 193 static int mtd_close(io_entity_t *entity) 194 { 195 entity->info = (uintptr_t)NULL; 196 197 return 0; 198 } 199 200 static int mtd_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info) 201 { 202 mtd_dev_state_t *cur; 203 io_dev_info_t *info; 204 io_mtd_ops_t *ops; 205 int result; 206 207 result = allocate_dev_info(&info); 208 if (result != 0) { 209 return -ENOENT; 210 } 211 212 cur = (mtd_dev_state_t *)info->info; 213 cur->dev_spec = (io_mtd_dev_spec_t *)dev_spec; 214 *dev_info = info; 215 ops = &(cur->dev_spec->ops); 216 if (ops->init != NULL) { 217 result = ops->init(&cur->dev_spec->device_size, 218 &cur->dev_spec->erase_size); 219 } 220 221 if (result == 0) { 222 cur->size = cur->dev_spec->device_size; 223 } else { 224 cur->size = 0ULL; 225 } 226 227 return result; 228 } 229 230 static int mtd_dev_close(io_dev_info_t *dev_info) 231 { 232 return free_dev_info(dev_info); 233 } 234 235 /* Exported functions */ 236 237 /* Register the MTD driver in the IO abstraction */ 238 int register_io_dev_mtd(const io_dev_connector_t **dev_con) 239 { 240 int result; 241 242 result = io_register_device(&dev_info_pool[0]); 243 if (result == 0) { 244 *dev_con = &mtd_dev_connector; 245 } 246 247 return result; 248 } 249