#
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

menu "Partition Manager"

config PARTITION_MANAGER_ENABLED
	bool "Partition manager is enabled (read-only option)"
	help
	  Option which can be checked to see whether or not the
	  partition manager is enabled in the current build. Note that this
	  should ideally be a hidden option, but it can't due to limitations
	  in how parent images impose options on child images. Don't
	  change the value of this option.

# Override this option to use custom flash map implementation when partition
# manager is enabled.
config FLASH_MAP_CUSTOM
	bool
	default y
	depends on PARTITION_MANAGER_ENABLED

# These values are now set by partition manager, so hide the option.
config SRAM_SIZE
	int
	depends on !PARTITION_MANAGER_ENABLED

# These values are now set by partition manager, so hide the option.
config SRAM_BASE_ADDRESS
	hex
	depends on !PARTITION_MANAGER_ENABLED

menu "Zephyr subsystem configurations"

DT_CHOSEN_Z_IPC_SHM := zephyr,ipc_shm

config RPMSG_NRF53_SRAM_SIZE
	hex "RPMsg shared memory size"
	default "$(dt_chosen_reg_size_hex,$(DT_CHOSEN_Z_IPC_SHM))"
	depends on SOC_NRF5340_CPUAPP
	depends on PARTITION_MANAGER_ENABLED
	help
	  This option specifies size of the memory region to be used
	  for the RPMsg shared memory

if FILE_SYSTEM_LITTLEFS
partition=LITTLEFS
partition-size=0x6000
rsource "Kconfig.template.partition_config"
rsource "Kconfig.template.partition_region"
endif


if SETTINGS_FCB || SETTINGS_NVS
partition=SETTINGS_STORAGE
partition-size=0x2000
rsource "Kconfig.template.partition_config"
rsource "Kconfig.template.partition_region"

config PM_PARTITION_ALIGN_SETTINGS_STORAGE
	hex "Settings storage partition alignment"
	default NRF_SPU_FLASH_REGION_SIZE if BUILD_WITH_TFM
	default FPROTECT_BLOCK_SIZE
	help
	  Alignment of the settings storage partition.

endif

if NVS && !SETTINGS_NVS
partition=NVS_STORAGE
partition-size=0x6000
rsource "Kconfig.template.partition_config"
rsource "Kconfig.template.partition_region"
endif

if ZIGBEE && !SOC_NRF52833
partition=ZBOSS_NVRAM
partition-size=0x8000
rsource "Kconfig.template.partition_config"
endif

if ZIGBEE && SOC_NRF52833
partition=ZBOSS_NVRAM
partition-size=0x4000
rsource "Kconfig.template.partition_config"
endif

if ZIGBEE
partition=ZBOSS_PRODUCT_CONFIG
partition-size=0x1000
rsource "Kconfig.template.partition_config"
endif

if EMDS
partition=EMDS_STORAGE
partition-size=0x1000
rsource "Kconfig.template.partition_config"
endif

if NRF_CLOUD_PGPS_STORAGE_PARTITION
partition=PGPS
partition-size=NRF_CLOUD_PGPS_PARTITION_SIZE
rsource "Kconfig.template.partition_config"
rsource "Kconfig.template.partition_region"

config PM_PGPS_ALIGN
	hex
	default NRF_SPU_FLASH_REGION_SIZE if BUILD_WITH_TFM
	default NRF_CLOUD_PGPS_PARTITION_ALIGN
	depends on PM_PARTITION_REGION_PGPS_EXTERNAL && BUILD_WITH_TFM

endif

if DFU_TARGET_FULL_MODEM_USE_EXT_PARTITION
partition=FMFU_STORAGE
partition-size=DFU_TARGET_FULL_MODEM_EXT_FLASH_SIZE
rsource "Kconfig.template.partition_config"
rsource "Kconfig.template.partition_region"
endif

if SOC_NRF54L15_ENGA_CPUFLPR
config PM_PARTITION_SIZE_VPR_LAUNCHER
	hex
	default $(dt_node_reg_addr_hex,/soc/rram-controller@5004b000/rram@165000)
	help
	  Memory set aside for the vpr_launcher partition.
	  Must match the size of the cpuapp_rram partition which is deleted.
	  This to place the flpr app's code at the address found in the devicetree.
endif

endmenu # Zephyr subsystem configurations
menu "NCS subsystem configurations"

endmenu # NCS subsystem configurations

config PM_SINGLE_IMAGE
	bool "Use the Partition Manager for single image builds" if !BUILD_WITH_TFM
	default y if BUILD_WITH_TFM
	select PARTITION_MANAGER_ENABLED
	help
	  Use the Partition Manager feature to partition the device even if
	  only a single image is included in the build. This is typically set
	  when a subsystem that defines its own Partition Manager configuration
	  is included in the build.

	  The option is always defined in builds where a TF-M image for the
	  Secure Execution environment is generated.

DT_CHOSEN_EXT_FLASH:= nordic,pm-ext-flash
DT_CHOSEN_EXT_FLASH_PATH := $(dt_chosen_path,$(DT_CHOSEN_EXT_FLASH))

config PM_EXTERNAL_FLASH_HAS_DRIVER
	bool
	default y if NORDIC_QSPI_NOR || SPI_NOR
	default y if PM_OVERRIDE_EXTERNAL_DRIVER_CHECK
	help
	  This option needs to be enabled by flash drivers that provide
	  support for external flash devices with partition manager
	  controlled partitions on them.
	  Partitions on devices without drivers will not have partition map
	  entries generated and will not be available from Flash Map API.

config PM_EXTERNAL_FLASH_BASE
	hex "External flash base address"
	default 0

config PM_EXTERNAL_FLASH_ENABLED
	bool
	default $(dt_chosen_enabled,$(DT_CHOSEN_EXT_FLASH))
	help
	  Internal PM_EXTERNAL_FLASH kconfig for exporting devicetree to sysbuild.

config PM_EXTERNAL_FLASH_PATH
	string
	default "$(DT_CHOSEN_EXT_FLASH_PATH)"
	help
	  Internal PM_EXTERNAL_FLASH kconfig for exporting devicetree to sysbuild.

config PM_EXTERNAL_FLASH_SIZE_BITS
	int
	default $(dt_node_int_prop_int,$(DT_CHOSEN_EXT_FLASH_PATH),size) if PM_EXTERNAL_FLASH_ENABLED
	default 0
	help
	  Internal PM_EXTERNAL_FLASH kconfig for exporting devicetree to sysbuild.

config PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY
	bool "Place MCUboot secondary in external flash"
	depends on BOOTLOADER_MCUBOOT || MCUBOOT
	# Only depend on the chosen for the parent image. The value of this
	# option is propagated to the MCUboot child image automatically.
	depends on PM_EXTERNAL_FLASH_ENABLED || MCUBOOT
	default y if BOOTLOADER_MCUBOOT
	imply ADD_MCUBOOT_MEDIATE_SIM_FLASH_DTS
	help
	  Place the secondary partition of MCUboot in the external flash instead
	  of the internal flash. This option should only be enabled by the user
	  in the parent image.

config PM_IMAGE_NOT_BUILT_FROM_SOURCE
	bool
	help
	  Promptless helper config used to indicate that one or more
	  image is not built from source.

config PM_OVERRIDE_EXTERNAL_DRIVER_CHECK
	bool "Override external driver check"
	help
	  When enabled, will include all external flash stroage areas without
	  being gated on the NORDIC_QSPI_NOR Kconfig, can be enabled to use
	  non-QSPI external flash chips from partition manager.

# Helper configs needed since 'SRAM_SIZE/BASE' use the chosen node.
config PM_SRAM_BASE
	hex
	default $(dt_node_reg_addr_hex,/soc/memory@21000000) if SOC_NRF5340_CPUNET
	default 0x20000000 if SOC_NRF54L15_ENGA_CPUFLPR  # Because the cpuapp_sram DT node is deleted in nrf54l15_cpuflpr.dtsi
	default $(dt_node_reg_addr_hex,/soc/memory@20000000)

config PM_SRAM_SIZE
	hex
	default $(dt_node_reg_size_hex,/soc/memory@21000000) if SOC_NRF5340_CPUNET
	default 0x40000 if SOC_NRF54L15_ENGA_CPUFLPR  # Because the cpuapp_rram DT node is deleted in nrf54l15_cpuflpr.dtsi
	default $(dt_node_reg_size_hex,/soc/memory@20000000)

endmenu # Partition Manager
