1 /*
2  * Copyright (c) 2017-2024 Nordic Semiconductor ASA
3  * Copyright (c) 2015 Runtime Inc
4  * Copyright (c) 2023 Sensorfy B.V.
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define DT_DRV_COMPAT fixed_partitions
10 
11 #include <zephyr/device.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/storage/flash_map.h>
14 
15 #if CONFIG_FLASH_MAP_LABELS
16 #define FLASH_AREA_FOO(part)							\
17 	{.fa_id = DT_FIXED_PARTITION_ID(part),					\
18 	 .fa_off = DT_REG_ADDR(part),						\
19 	 .fa_dev = DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(part)),	        \
20 	 .fa_size = DT_REG_SIZE(part),						\
21 	 .fa_label = DT_PROP_OR(part, label, NULL),	},
22 #else
23 #define FLASH_AREA_FOO(part)							\
24 	{.fa_id = DT_FIXED_PARTITION_ID(part),					\
25 	 .fa_off = DT_REG_ADDR(part),						\
26 	 .fa_dev = DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(part)),	        \
27 	 .fa_size = DT_REG_SIZE(part), },
28 #endif
29 
30 #define FLASH_AREA_FOOO(part)	\
31 	COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(DT_MTD_FROM_FIXED_PARTITION(part)), \
32 		(FLASH_AREA_FOO(part)), ())
33 
34 #define FOREACH_PARTITION(n) DT_FOREACH_CHILD(DT_DRV_INST(n), FLASH_AREA_FOOO)
35 
36 /* We iterate over all compatible 'fixed-partitions' nodes and
37  * use DT_FOREACH_CHILD to iterate over all the partitions for that
38  * 'fixed-partitions' node.  This way we build a global partition map
39  */
40 const struct flash_area default_flash_map[] = {
41 	DT_INST_FOREACH_STATUS_OKAY(FOREACH_PARTITION)
42 };
43 
44 const int flash_map_entries = ARRAY_SIZE(default_flash_map);
45 const struct flash_area *flash_map = default_flash_map;
46 
47 /* Generate objects representing each partition in system. In the end only
48  * objects referenced by code will be included into build.
49  */
50 #define DEFINE_PARTITION(part) DEFINE_PARTITION_1(part, DT_DEP_ORD(part))
51 #define DEFINE_PARTITION_1(part, ord)								\
52 	COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(DT_MTD_FROM_FIXED_PARTITION(part)),			\
53 		(DEFINE_PARTITION_0(part, ord)), ())
54 #define DEFINE_PARTITION_0(part, ord)								\
55 	const struct flash_area DT_CAT(global_fixed_partition_ORD_, ord) = {			\
56 		.fa_id = DT_FIXED_PARTITION_ID(part),						\
57 		.fa_off = DT_REG_ADDR(part),							\
58 		.fa_dev = DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(part)),			\
59 		.fa_size = DT_REG_SIZE(part),							\
60 	};
61 
62 #define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DEFINE_PARTITION)
63 DT_FOREACH_STATUS_OKAY(fixed_partitions, FOR_EACH_PARTITION_TABLE)
64