diff --git a/drivers/flash/Kconfig b/drivers/flash/Kconfig index 9d2192571f95fb9..287970dc2ef958e 100644 --- a/drivers/flash/Kconfig +++ b/drivers/flash/Kconfig @@ -84,6 +84,12 @@ config FLASH_PAGE_LAYOUT help Enables API for retrieving the layout of flash memory pages. +config FLASH_RESET + bool "API for resetting the flash registers" + default n + help + Enables API for for resetting the flash registers to default power on state. + config FLASH_EX_OP_ENABLED bool "API for extended flash operations" depends on FLASH_HAS_EX_OP diff --git a/drivers/flash/spi_nor.c b/drivers/flash/spi_nor.c index ddfdbd6c81d0d7e..cac798a66f51e0c 100644 --- a/drivers/flash/spi_nor.c +++ b/drivers/flash/spi_nor.c @@ -679,6 +679,18 @@ static int spi_nor_read(const struct device *dev, off_t addr, void *dest, return ret; } +#if defined(CONFIG_FLASH_RESET) +static void spi_nor_send_flash_reset(const struct device *dev) +{ + acquire_device(dev); + + spi_nor_cmd_write(dev, SPI_NOR_CMD_RESET_EN); + spi_nor_cmd_write(dev, SPI_NOR_CMD_RESET_MEM); + + release_device(dev); +} +#endif + static int spi_nor_write(const struct device *dev, off_t addr, const void *src, size_t size) @@ -1351,6 +1363,9 @@ static const struct flash_driver_api spi_nor_api = { .sfdp_read = spi_nor_sfdp_read, .read_jedec_id = spi_nor_read_jedec_id, #endif +#if defined(CONFIG_FLASH_RESET) + .send_flash_reset = spi_nor_send_flash_reset, +#endif }; #ifndef CONFIG_SPI_NOR_SFDP_RUNTIME diff --git a/include/zephyr/drivers/flash.h b/include/zephyr/drivers/flash.h index de5e7c2116e0379..0171e264f21d86f 100644 --- a/include/zephyr/drivers/flash.h +++ b/include/zephyr/drivers/flash.h @@ -128,6 +128,10 @@ typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id); typedef int (*flash_api_ex_op)(const struct device *dev, uint16_t code, const uintptr_t in, void *out); +#if defined(CONFIG_FLASH_RESET) +typedef void (*flash_api_send_flash_reset)(const struct device *dev); +#endif + __subsystem struct flash_driver_api { flash_api_read read; flash_api_write write; @@ -143,6 +147,9 @@ __subsystem struct flash_driver_api { #if defined(CONFIG_FLASH_EX_OP_ENABLED) flash_api_ex_op ex_op; #endif /* CONFIG_FLASH_EX_OP_ENABLED */ +#if defined(CONFIG_FLASH_RESET) + flash_api_send_flash_reset send_flash_reset; +#endif }; /** @@ -249,6 +256,27 @@ static inline int z_impl_flash_erase(const struct device *dev, off_t offset, return rc; } +#if defined(CONFIG_FLASH_RESET) +/* + * @brief Send SPI reset command. + * + * This api can be used to reset the SPI flash memory i.e volatile + * register configurations before SPI access. + * Command also aborts any ongoing internal SPI operations. + * + * @param dev : flash dev + */ +__syscall void flash_reset(const struct device *dev); + +static inline void z_impl_flash_reset(const struct device *dev) +{ + const struct flash_driver_api *api = + (const struct flash_driver_api *)dev->api; + + api->send_flash_reset(dev); +} +#endif + struct flash_pages_info { off_t start_offset; /* offset from the base of flash address */ size_t size;