@@ -420,6 +420,8 @@ static const struct disk_operations flash_disk_ops = {
420420 .ioctl = disk_flash_access_ioctl ,
421421};
422422
423+ #ifndef USE_PARTITION_MANAGER
424+ /* The non-Partition manager, DTS based generators below */
423425#define DT_DRV_COMPAT zephyr_flash_disk
424426
425427#define PARTITION_PHANDLE (n ) DT_PHANDLE_BY_IDX(DT_DRV_INST(n), partition, 0)
@@ -461,6 +463,82 @@ DT_INST_FOREACH_STATUS_OKAY(VERIFY_CACHE_SIZE_IS_NOT_ZERO_IF_NOT_READ_ONLY)
461463 "Devicetree node " DT_NODE_PATH(DT_DRV_INST(n)) \
462464 " has cache size which is not a multiple of its sector size");
463465DT_INST_FOREACH_STATUS_OKAY (VERIFY_CACHE_SIZE_IS_MULTIPLY_OF_SECTOR_SIZE )
466+ #else /* ifndef USE_PARTITION_MANAGER */
467+ /* Partition Manager based generators below */
468+
469+ /* Gets the PM_..._EXTRA_PARAM_##param value */
470+ #define PM_FLASH_DISK_ENTRY_EXTRA_PARAM (name , param ) PM_##name##_EXTRA_PARAM_disk_##param
471+
472+ /* Gets the PM_..._NAME value which is originally cased, as in yaml, partition name */
473+ #define PM_FLASH_DISK_ENTRY_PARTITION_NAME (name ) PM_##name##_NAME
474+
475+ /* Generates flashdiskN_cache variable name, where N is partition ID */
476+ #define PM_FLASH_DISK_CACHE_VARIABLE (n ) UTIL_CAT(flashdisk, UTIL_CAT(FIXED_PARTITION_ID(n), _cache))
477+
478+ /* Generate cache buffers */
479+ #define CACHE_SIZE (n ) (COND_CODE_1(PM_FLASH_DISK_ENTRY_EXTRA_PARAM(n, read_only), (0), (1)) * \
480+ PM_FLASH_DISK_ENTRY_EXTRA_PARAM(n, cache_size))
481+ #define DEFINE_FLASHDISKS_CACHE (n ) \
482+ static uint8_t __aligned(4) PM_FLASH_DISK_CACHE_VARIABLE(n)[CACHE_SIZE(n)];
483+
484+ PM_FOREACH_AFFILIATED_TO_disk (DEFINE_FLASHDISKS_CACHE )
485+
486+ /* Generated single Flash Disk device data from Partition Manager partition.
487+ * Partition is required to have type set to disk in partition definitions:
488+ * type: disk
489+ * and following extra params can be provided:
490+ * extra_params: {
491+ * name = "<name>",
492+ * cache_size = <size>,
493+ * sector_size = <ssize>,
494+ * read_only = <ro>
495+ * }
496+ * where:
497+ * <name> is mandatory device name that will be used by Disk Access and FAT FS to mount device;
498+ * <cache_size> is cache r/w cache size, which is mandatory if read_only = 0 or not present,
499+ * and should be multiple of <ssize>;
500+ * <ssize> is mandatory device sector size information, usually should be erase page size,
501+ * for flash devices, for example 4096 bytes;
502+ * read_only is optional, if not present then assumed false; <ro> can be 0(false) or 1(true).
503+ */
504+ #define DEFINE_FLASHDISKS_DEVICE (n ) \
505+ { \
506+ .info = { \
507+ .ops = &flash_disk_ops, \
508+ .name = STRINGIFY(PM_FLASH_DISK_ENTRY_EXTRA_PARAM(n, name)), \
509+ }, \
510+ .area_id = FIXED_PARTITION_ID(n), \
511+ .offset = FIXED_PARTITION_OFFSET(n), \
512+ .cache = PM_FLASH_DISK_CACHE_VARIABLE(n), \
513+ .cache_size = sizeof(PM_FLASH_DISK_CACHE_VARIABLE(n)), \
514+ .size = FIXED_PARTITION_SIZE(n), \
515+ .sector_size = PM_FLASH_DISK_ENTRY_EXTRA_PARAM(n, sector_size), \
516+ },
517+
518+ /* The bellow used PM_FOREACH_TYPE_disk is generated by Partition Manager foreach
519+ * loop macro. The lower case _disk is type name for which the macro has been generated;
520+ * partition entry can have multiple types set and foreach macro will be generated
521+ * for every type found across partition definitions.
522+ */
523+ static struct flashdisk_data flash_disks [] = {
524+ PM_FOREACH_AFFILIATED_TO_disk (DEFINE_FLASHDISKS_DEVICE )
525+ };
526+
527+ #define VERIFY_CACHE_SIZE_IS_NOT_ZERO_IF_NOT_READ_ONLY (n ) \
528+ COND_CODE_1(PM_FLASH_DISK_ENTRY_EXTRA_PARAM(n, read_only), \
529+ (/* cache-size is not used for read-only disks */ ), \
530+ (BUILD_ASSERT (PM_FLASH_DISK_ENTRY_EXTRA_PARAM (n , cache_size ) != 0 , \
531+ "Flash disk partition " STRINGIFY (PM_FLASH_DISK_ENTRY_PARTITION_NAME (n ))\
532+ " must have non-zero cache-size" );))
533+ PM_FOREACH_AFFILIATED_TO_disk (VERIFY_CACHE_SIZE_IS_NOT_ZERO_IF_NOT_READ_ONLY )
534+
535+ #define VERIFY_CACHE_SIZE_IS_MULTIPLY_OF_SECTOR_SIZE (n ) \
536+ BUILD_ASSERT(PM_FLASH_DISK_ENTRY_EXTRA_PARAM(n, cache_size) % \
537+ PM_FLASH_DISK_ENTRY_EXTRA_PARAM(n, sector_size) == 0, \
538+ "Devicetree node " STRINGIFY(PM_FLASH_DISK_ENTRY_PARTITION_NAME(n)) \
539+ " has cache size which is not a multiple of its sector size");
540+ PM_FOREACH_AFFILIATED_TO_disk (VERIFY_CACHE_SIZE_IS_MULTIPLY_OF_SECTOR_SIZE )
541+ #endif /* USE_PARTITION_MANAGER */
464542
465543static int disk_flash_init (void )
466544{
0 commit comments