Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | /*
* Copyright (c) 2017 Nordic Semiconductor ASA
* Copyright (c) 2015 Runtime Inc
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/types.h>
#include <stddef.h>
#include <sys/types.h>
#include <device.h>
#include <flash_map.h>
#include <flash.h>
#include <soc.h>
#include <init.h>
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
struct layout_data {
u32_t area_idx;
u32_t area_off;
u32_t area_len;
void *ret; /* struct flash_area* or struct flash_sector* */
u32_t ret_idx;
u32_t ret_len;
int status;
};
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
struct driver_map_entry {
u8_t id;
const char * const name;
};
static const struct driver_map_entry flash_drivers_map[] = {
#ifdef FLASH_DEV_NAME /* SoC embedded flash driver */
{SOC_FLASH_0_ID, FLASH_DEV_NAME},
#endif
#ifdef CONFIG_SPI_FLASH_W25QXXDV
{SPI_FLASH_0_ID, CONFIG_SPI_FLASH_W25QXXDV_DRV_NAME},
#endif
};
const struct flash_area *flash_map;
extern const int flash_map_entries;
static struct device *flash_dev[ARRAY_SIZE(flash_drivers_map)];
static struct flash_area const *get_flash_area_from_id(int idx)
{
for (int i = 0; i < flash_map_entries; i++) {
if (flash_map[i].fa_id == idx) {
return &flash_map[i];
}
}
return NULL;
}
int flash_area_open(u8_t id, const struct flash_area **fap)
{
const struct flash_area *area;
if (flash_map == NULL) {
return -EACCES;
}
area = get_flash_area_from_id(id);
if (area == NULL) {
return -ENOENT;
}
*fap = area;
return 0;
}
void flash_area_close(const struct flash_area *fa)
{
/* nothing to do for now */
}
static struct device *get_flash_dev_from_id(u8_t id)
{
for (unsigned int i = 0; i < ARRAY_SIZE(flash_drivers_map); i++) {
if (flash_drivers_map[i].id == id) {
return flash_dev[i];
}
}
k_panic();
}
static inline bool is_in_flash_area_bounds(const struct flash_area *fa,
off_t off, size_t len)
{
return (off <= fa->fa_size && off + len <= fa->fa_size);
}
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
/*
* Check if a flash_page_foreach() callback should exit early, due to
* one of the following conditions:
*
* - The flash page described by "info" is before the area of interest
* described in "data"
* - The flash page is after the end of the area
* - There are too many flash pages on the device to fit in the array
* held in data->ret. In this case, data->status is set to -ENOMEM.
*
* The value to return to flash_page_foreach() is stored in
* "bail_value" if the callback should exit early.
*/
static bool should_bail(const struct flash_pages_info *info,
struct layout_data *data,
bool *bail_value)
{
if (info->start_offset < data->area_off) {
*bail_value = true;
return true;
} else if (info->start_offset >= data->area_off + data->area_len) {
*bail_value = false;
return true;
} else if (data->ret_idx >= data->ret_len) {
data->status = -ENOMEM;
*bail_value = false;
return true;
}
return false;
}
/*
* Generic page layout discovery routine. This is kept separate to
* support both the deprecated flash_area_to_sectors() and the current
* flash_area_get_sectors(). A lot of this can be inlined once
* flash_area_to_sectors() is removed.
*/
static int flash_area_layout(int idx, u32_t *cnt, void *ret,
flash_page_cb cb, struct layout_data *cb_data)
{
struct device *flash_dev;
cb_data->area_idx = idx;
const struct flash_area *fa;
fa = get_flash_area_from_id(idx);
if (fa == NULL) {
return -EINVAL;
}
cb_data->area_idx = idx;
cb_data->area_off = fa->fa_off;
cb_data->area_len = fa->fa_size;
cb_data->ret = ret;
cb_data->ret_idx = 0;
cb_data->ret_len = *cnt;
cb_data->status = 0;
flash_dev = get_flash_dev_from_id(fa->fa_device_id);
flash_page_foreach(flash_dev, cb, cb_data);
if (cb_data->status == 0) {
*cnt = cb_data->ret_idx;
}
return cb_data->status;
}
static bool get_sectors_cb(const struct flash_pages_info *info, void *datav)
{
struct layout_data *data = datav;
struct flash_sector *ret = data->ret;
bool bail;
if (should_bail(info, data, &bail)) {
return bail;
}
ret[data->ret_idx].fs_off = info->start_offset - data->area_off;
ret[data->ret_idx].fs_size = info->size;
data->ret_idx++;
return true;
}
int flash_area_get_sectors(int idx, u32_t *cnt, struct flash_sector *ret)
{
struct layout_data data;
return flash_area_layout(idx, cnt, ret, get_sectors_cb, &data);
}
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
int flash_area_read(const struct flash_area *fa, off_t off, void *dst,
size_t len)
{
struct device *dev;
if (!is_in_flash_area_bounds(fa, off, len)) {
return -1;
}
dev = get_flash_dev_from_id(fa->fa_device_id);
return flash_read(dev, fa->fa_off + off, dst, len);
}
int flash_area_write(const struct flash_area *fa, off_t off, const void *src,
size_t len)
{
struct device *flash_dev;
int rc;
if (!is_in_flash_area_bounds(fa, off, len)) {
return -1;
}
flash_dev = get_flash_dev_from_id(fa->fa_device_id);
rc = flash_write_protection_set(flash_dev, false);
if (rc) {
return rc;
}
rc = flash_write(flash_dev, fa->fa_off + off, (void *)src, len);
/* Ignore errors here - this does not affect write operation */
(void) flash_write_protection_set(flash_dev, true);
return rc;
}
int flash_area_erase(const struct flash_area *fa, off_t off, size_t len)
{
struct device *flash_dev;
int rc;
if (!is_in_flash_area_bounds(fa, off, len)) {
return -1;
}
flash_dev = get_flash_dev_from_id(fa->fa_device_id);
rc = flash_write_protection_set(flash_dev, false);
if (rc) {
return rc;
}
rc = flash_erase(flash_dev, fa->fa_off + off, len);
/* Ignore errors here - this does not affect write operation */
(void) flash_write_protection_set(flash_dev, true);
return rc;
}
u8_t flash_area_align(const struct flash_area *fa)
{
struct device *dev;
dev = get_flash_dev_from_id(fa->fa_device_id);
return flash_get_write_block_size(dev);
}
static int flash_map_init(struct device *dev)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(flash_dev); i++) {
flash_dev[i] = device_get_binding(flash_drivers_map[i].name);
}
return 0;
}
SYS_INIT(flash_map_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
|