Lines Matching +full:non +full:- +full:uniform
3 * Copyright (c) 2022-2024 Nordic Semiconductor ASA
5 * SPDX-License-Identifier: Apache-2.0
44 (block_size - (start & (block_size - 1)))
58 return ctx->erase_required; in flashdisk_with_erase()
69 ctx->erase_required = in flashdisk_probe_erase()
70 flash_params_get_erase_cap(flash_get_parameters(ctx->info.dev)) & in flashdisk_probe_erase()
79 LOG_DBG("status : %s", disk->dev ? "okay" : "no media"); in disk_flash_access_status()
80 if (!disk->dev) { in disk_flash_access_status()
97 rc = flash_get_page_info_by_offs(ctx->info.dev, ctx->offset, &page); in flashdisk_init_runtime()
103 ctx->page_size = page.size; in flashdisk_init_runtime()
105 ctx->page_size = DEFAULT_BLOCK_SIZE; in flashdisk_init_runtime()
108 LOG_INF("Initialize device %s", ctx->info.name); in flashdisk_init_runtime()
110 (long)ctx->offset, ctx->sector_size, ctx->page_size, ctx->size); in flashdisk_init_runtime()
112 if (ctx->cache_size == 0) { in flashdisk_init_runtime()
113 /* Read-only flashdisk, no flash partition constraints */ in flashdisk_init_runtime()
114 LOG_INF("%s is read-only", ctx->info.name); in flashdisk_init_runtime()
119 if (ctx->offset != page.start_offset) { in flashdisk_init_runtime()
121 ctx->info.name); in flashdisk_init_runtime()
122 return -EINVAL; in flashdisk_init_runtime()
125 offset = ctx->offset + page.size; in flashdisk_init_runtime()
126 while (offset < ctx->offset + ctx->size) { in flashdisk_init_runtime()
127 rc = flash_get_page_info_by_offs(ctx->info.dev, offset, &page); in flashdisk_init_runtime()
132 if (page.size != ctx->page_size) { in flashdisk_init_runtime()
133 LOG_ERR("Non-uniform page size is not supported"); in flashdisk_init_runtime()
139 if (offset != ctx->offset + ctx->size) { in flashdisk_init_runtime()
141 ctx->info.name); in flashdisk_init_runtime()
142 return -EINVAL; in flashdisk_init_runtime()
146 if (ctx->page_size > ctx->cache_size) { in flashdisk_init_runtime()
148 ctx->cache_size, ctx->page_size); in flashdisk_init_runtime()
149 return -ENOMEM; in flashdisk_init_runtime()
163 rc = flash_area_open(ctx->area_id, &fap); in disk_flash_access_init()
165 LOG_ERR("Flash area %u open error %d", ctx->area_id, rc); in disk_flash_access_init()
169 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_init()
171 disk->dev = flash_area_get_device(fap); in disk_flash_access_init()
177 k_mutex_unlock(&ctx->lock); in disk_flash_access_init()
187 start = ctx->offset + (start_sector * ctx->sector_size); in sectors_in_range()
188 end = start + (sector_count * ctx->sector_size); in sectors_in_range()
190 if ((end >= start) && (start >= ctx->offset) && (end <= ctx->offset + ctx->size)) { in sectors_in_range()
212 return -EINVAL; in disk_flash_access_read()
215 fl_addr = ctx->offset + start_sector * ctx->sector_size; in disk_flash_access_read()
216 remaining = (sector_count * ctx->sector_size); in disk_flash_access_read()
218 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_read()
221 offset = fl_addr & (ctx->page_size - 1); in disk_flash_access_read()
222 fl_addr = ROUND_DOWN(fl_addr, ctx->page_size); in disk_flash_access_read()
225 len = ctx->page_size - offset; in disk_flash_access_read()
231 if (ctx->cache_valid && ctx->cached_addr == fl_addr) { in disk_flash_access_read()
232 memcpy(buff, &ctx->cache[offset], len); in disk_flash_access_read()
233 } else if (flash_read(disk->dev, fl_addr + offset, buff, len) < 0) { in disk_flash_access_read()
234 rc = -EIO; in disk_flash_access_read()
238 fl_addr += ctx->page_size; in disk_flash_access_read()
239 remaining -= len; in disk_flash_access_read()
243 len = ctx->page_size; in disk_flash_access_read()
248 k_mutex_unlock(&ctx->lock); in disk_flash_access_read()
255 if (!ctx->cache_valid || !ctx->cache_dirty) { in flashdisk_cache_commit()
261 if (flash_erase(ctx->info.dev, ctx->cached_addr, ctx->page_size) < 0) { in flashdisk_cache_commit()
262 return -EIO; in flashdisk_cache_commit()
267 if (flash_write(ctx->info.dev, ctx->cached_addr, ctx->cache, ctx->page_size) < 0) { in flashdisk_cache_commit()
268 return -EIO; in flashdisk_cache_commit()
271 ctx->cache_dirty = false; in flashdisk_cache_commit()
279 __ASSERT_NO_MSG((fl_addr & (ctx->page_size - 1)) == 0); in flashdisk_cache_load()
281 if (ctx->cache_valid) { in flashdisk_cache_load()
282 if (ctx->cached_addr == fl_addr) { in flashdisk_cache_load()
295 ctx->cache_valid = false; in flashdisk_cache_load()
296 ctx->cache_dirty = false; in flashdisk_cache_load()
297 ctx->cached_addr = fl_addr; in flashdisk_cache_load()
298 rc = flash_read(ctx->info.dev, fl_addr, ctx->cache, ctx->page_size); in flashdisk_cache_load()
301 ctx->cache_valid = true; in flashdisk_cache_load()
305 return -EIO; in flashdisk_cache_load()
308 /* input size is either less or equal to a block size (ctx->page_size)
318 /* adjust offset if starting address is not erase-aligned address */ in flashdisk_cache_write()
319 offset = start_addr & (ctx->page_size - 1); in flashdisk_cache_write()
322 fl_addr = ROUND_DOWN(start_addr, ctx->page_size); in flashdisk_cache_write()
327 __ASSERT_NO_MSG(fl_addr + ctx->page_size >= start_addr + size); in flashdisk_cache_write()
337 if (ctx->cache_dirty || memcmp(&ctx->cache[offset], buff, size)) { in flashdisk_cache_write()
339 memcpy(&ctx->cache[offset], buff, size); in flashdisk_cache_write()
340 ctx->cache_dirty = true; in flashdisk_cache_write()
357 if (ctx->cache_size == 0) { in disk_flash_access_write()
358 return -ENOTSUP; in disk_flash_access_write()
362 return -EINVAL; in disk_flash_access_write()
365 fl_addr = ctx->offset + start_sector * ctx->sector_size; in disk_flash_access_write()
366 remaining = (sector_count * ctx->sector_size); in disk_flash_access_write()
368 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_write()
370 /* check if start address is erased-aligned address */ in disk_flash_access_write()
371 if (fl_addr & (ctx->page_size - 1)) { in disk_flash_access_write()
376 block_bnd = fl_addr + ctx->page_size; in disk_flash_access_write()
377 block_bnd = block_bnd & ~(ctx->page_size - 1); in disk_flash_access_write()
381 rc = -EIO; in disk_flash_access_write()
387 size = GET_SIZE_TO_BOUNDARY(fl_addr, ctx->page_size); in disk_flash_access_write()
391 rc = -EIO; in disk_flash_access_write()
396 remaining -= size; in disk_flash_access_write()
400 /* start is an erase-aligned address */ in disk_flash_access_write()
402 if (remaining < ctx->page_size) { in disk_flash_access_write()
406 if (flashdisk_cache_write(ctx, fl_addr, ctx->page_size, buff) < 0) { in disk_flash_access_write()
407 rc = -EIO; in disk_flash_access_write()
411 fl_addr += ctx->page_size; in disk_flash_access_write()
412 remaining -= ctx->page_size; in disk_flash_access_write()
413 buff += ctx->page_size; in disk_flash_access_write()
419 rc = -EIO; in disk_flash_access_write()
425 k_mutex_unlock(&ctx->lock); in disk_flash_access_write()
440 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_ioctl()
442 k_mutex_unlock(&ctx->lock); in disk_flash_access_ioctl()
445 *(uint32_t *)buff = ctx->size / ctx->sector_size; in disk_flash_access_ioctl()
448 *(uint32_t *)buff = ctx->sector_size; in disk_flash_access_ioctl()
451 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_ioctl()
452 *(uint32_t *)buff = ctx->page_size / ctx->sector_size; in disk_flash_access_ioctl()
453 k_mutex_unlock(&ctx->lock); in disk_flash_access_ioctl()
461 return -EINVAL; in disk_flash_access_ioctl()
475 /* Force cache size to 0 if partition is read-only */
502 (/* cache-size is not used for read-only disks */), \
505 " must have non-zero cache-size");))