Skip to content

Commit 8981015

Browse files
tmon-nordiccarlescufi
authored andcommitted
drivers: flashdisk: support read-only flashdisks
Force cache-size to 0 and treat flashdisk as read-only when backing partition has read-only flag set. This allows users to save RAM when the application does not write to the flashdisk, e.g. when a predefined FAT filesystem is used. Signed-off-by: Tomasz Moń <[email protected]>
1 parent fc86517 commit 8981015

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

drivers/disk/flashdisk.c

+19-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ static int disk_flash_access_init(struct disk_info *disk)
7777
LOG_INF("offset %lx, sector size %zu, page size %zu, volume size %zu",
7878
(long)ctx->offset, ctx->sector_size, ctx->page_size, ctx->size);
7979

80-
if (ctx->page_size > ctx->cache_size) {
80+
if (ctx->cache_size == 0) {
81+
LOG_INF("%s is read-only", disk->name);
82+
} else if (ctx->page_size > ctx->cache_size) {
8183
LOG_ERR("Cache too small (%zu needs %zu)",
8284
ctx->cache_size, ctx->page_size);
8385
rc = -ENOMEM;
@@ -272,6 +274,10 @@ static int disk_flash_access_write(struct disk_info *disk, const uint8_t *buff,
272274

273275
ctx = CONTAINER_OF(disk, struct flashdisk_data, info);
274276

277+
if (ctx->cache_size == 0) {
278+
return -ENOTSUP;
279+
}
280+
275281
if (!sectors_in_range(ctx, start_sector, sector_count)) {
276282
return -EINVAL;
277283
}
@@ -383,9 +389,11 @@ static const struct disk_operations flash_disk_ops = {
383389
#define DT_DRV_COMPAT zephyr_flash_disk
384390

385391
#define PARTITION_PHANDLE(n) DT_PHANDLE_BY_IDX(DT_DRV_INST(n), partition, 0)
392+
/* Force cache size to 0 if partition is read-only */
393+
#define CACHE_SIZE(n) (DT_INST_PROP(n, cache_size) * !DT_PROP(PARTITION_PHANDLE(n), read_only))
386394

387395
#define DEFINE_FLASHDISKS_CACHE(n) \
388-
static uint8_t __aligned(4) flashdisk##n##_cache[DT_INST_PROP(n, cache_size)];
396+
static uint8_t __aligned(4) flashdisk##n##_cache[CACHE_SIZE(n)];
389397
DT_INST_FOREACH_STATUS_OKAY(DEFINE_FLASHDISKS_CACHE)
390398

391399
#define DEFINE_FLASHDISKS_DEVICE(n) \
@@ -397,7 +405,7 @@ DT_INST_FOREACH_STATUS_OKAY(DEFINE_FLASHDISKS_CACHE)
397405
.area_id = DT_FIXED_PARTITION_ID(PARTITION_PHANDLE(n)), \
398406
.offset = DT_REG_ADDR(PARTITION_PHANDLE(n)), \
399407
.cache = flashdisk##n##_cache, \
400-
.cache_size = DT_INST_PROP(n, cache_size), \
408+
.cache_size = sizeof(flashdisk##n##_cache), \
401409
.size = DT_REG_SIZE(PARTITION_PHANDLE(n)), \
402410
.sector_size = DT_INST_PROP(n, sector_size), \
403411
},
@@ -406,6 +414,14 @@ static struct flashdisk_data flash_disks[] = {
406414
DT_INST_FOREACH_STATUS_OKAY(DEFINE_FLASHDISKS_DEVICE)
407415
};
408416

417+
#define VERIFY_CACHE_SIZE_IS_NOT_ZERO_IF_NOT_READ_ONLY(n) \
418+
COND_CODE_1(DT_PROP(PARTITION_PHANDLE(n), read_only), \
419+
(/* cache-size is not used for read-only disks */), \
420+
(BUILD_ASSERT(DT_INST_PROP(n, cache_size) != 0, \
421+
"Devicetree node " DT_NODE_PATH(DT_DRV_INST(n)) \
422+
" must have non-zero cache-size");))
423+
DT_INST_FOREACH_STATUS_OKAY(VERIFY_CACHE_SIZE_IS_NOT_ZERO_IF_NOT_READ_ONLY)
424+
409425
#define VERIFY_CACHE_SIZE_IS_MULTIPLY_OF_SECTOR_SIZE(n) \
410426
BUILD_ASSERT(DT_INST_PROP(n, cache_size) % DT_INST_PROP(n, sector_size) == 0, \
411427
"Devicetree node " DT_NODE_PATH(DT_DRV_INST(n)) \

dts/bindings/misc/zephyr,flash-disk.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ properties:
3535
adequately chosen. On storage backends with uniform erase-blocks it
3636
should be at least the erase-block-size, on storage backends with
3737
non-uniform erase-blocks it should be at least the largest
38-
erase-block-size.
38+
erase-block-size. The cache-size property is ignored if the partition
39+
is read-only.

0 commit comments

Comments
 (0)