2017-05-02 14:55:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Linaro Limited
|
|
|
|
* Copyright (c) 2017 BayLibre, SAS.
|
2019-07-04 11:38:06 +03:00
|
|
|
* Copyright (c) 2019 Centaur Analytics, Inc
|
2017-05-02 14:55:08 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <device.h>
|
2020-04-03 10:17:18 +02:00
|
|
|
|
2020-04-13 18:07:07 +02:00
|
|
|
#define DT_DRV_COMPAT st_stm32_flash_controller
|
2020-04-03 10:17:18 +02:00
|
|
|
|
2017-05-02 14:55:08 +00:00
|
|
|
#include <string.h>
|
2019-06-25 15:53:50 -04:00
|
|
|
#include <drivers/flash.h>
|
2017-05-02 14:55:08 +00:00
|
|
|
#include <init.h>
|
|
|
|
#include <soc.h>
|
2020-11-27 08:44:24 +01:00
|
|
|
#include <stm32_ll_bus.h>
|
|
|
|
#include <stm32_ll_rcc.h>
|
2020-03-23 13:57:50 +01:00
|
|
|
#include <logging/log.h>
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2017-10-27 16:11:54 +02:00
|
|
|
#include "flash_stm32.h"
|
2020-06-23 09:48:07 +02:00
|
|
|
#include "stm32_hsem.h"
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2020-03-23 13:57:50 +01:00
|
|
|
LOG_MODULE_REGISTER(flash_stm32, CONFIG_FLASH_LOG_LEVEL);
|
|
|
|
|
2018-09-02 21:05:52 +02:00
|
|
|
/* STM32F0: maximum erase time of 40ms for a 2K sector */
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 40
|
2019-02-14 15:38:52 +01:00
|
|
|
/* STM32F3: maximum erase time of 40ms for a 2K sector */
|
2019-07-30 16:21:30 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F1X)
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 40
|
2019-07-30 16:21:30 +08:00
|
|
|
/* STM32F3: maximum erase time of 40ms for a 2K sector */
|
2019-02-14 15:38:52 +01:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F3X)
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 40
|
2018-09-02 21:05:52 +02:00
|
|
|
/* STM32F4: maximum erase time of 4s for a 128K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F4X)
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 4000
|
2018-09-02 21:05:52 +02:00
|
|
|
/* STM32F7: maximum erase time of 4s for a 256K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F7X)
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 4000
|
2020-05-11 13:39:39 +01:00
|
|
|
/* STM32L0: maximum erase time of 3.2ms for a 128B page */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
|
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 4
|
2020-11-26 21:11:07 -03:00
|
|
|
/* STM32L1: maximum erase time of 3.94ms for a 128B half-page */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32L1X)
|
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 4
|
2018-09-02 21:05:52 +02:00
|
|
|
/* STM32L4: maximum erase time of 24.47ms for a 2K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 25
|
2019-03-27 16:52:37 +01:00
|
|
|
/* STM32WB: maximum erase time of 24.5ms for a 4K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 25
|
2019-07-05 15:02:21 +02:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32G0X)
|
|
|
|
/* STM32G0: maximum erase time of 40ms for a 2K sector */
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 40
|
2019-09-04 09:43:59 +01:00
|
|
|
/* STM32G4: maximum erase time of 24.47ms for a 2K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
2020-03-05 14:54:28 -08:00
|
|
|
#define STM32_FLASH_MAX_ERASE_TIME 25
|
2018-09-02 21:05:52 +02:00
|
|
|
#endif
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2019-07-04 11:38:06 +03:00
|
|
|
/* Let's wait for double the max erase time to be sure that the operation is
|
|
|
|
* completed.
|
|
|
|
*/
|
|
|
|
#define STM32_FLASH_TIMEOUT (2 * STM32_FLASH_MAX_ERASE_TIME)
|
|
|
|
|
2020-06-03 18:30:32 +00:00
|
|
|
static const struct flash_parameters flash_stm32_parameters = {
|
2020-05-11 13:39:39 +01:00
|
|
|
.write_block_size = FLASH_STM32_WRITE_BLOCK_SIZE,
|
|
|
|
/* Some SoCs (L0/L1) use an EEPROM under the hood. Distinguish
|
|
|
|
* between them based on the presence of the PECR register. */
|
|
|
|
#if defined(FLASH_PECR_ERASE)
|
|
|
|
.erase_value = 0,
|
2020-06-03 18:33:09 +00:00
|
|
|
#else
|
2020-06-03 18:30:32 +00:00
|
|
|
.erase_value = 0xff,
|
2020-05-11 13:39:39 +01:00
|
|
|
#endif
|
2020-06-03 18:30:32 +00:00
|
|
|
};
|
|
|
|
|
2021-03-12 14:35:50 +01:00
|
|
|
static int flash_stm32_write_protection(const struct device *dev, bool enable);
|
2020-12-09 11:10:04 +01:00
|
|
|
|
|
|
|
int __weak flash_stm32_check_configuration(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:03:34 +02:00
|
|
|
#if defined(CONFIG_MULTITHREADING)
|
2017-08-30 14:02:10 -04:00
|
|
|
/*
|
|
|
|
* This is named flash_stm32_sem_take instead of flash_stm32_lock (and
|
|
|
|
* similarly for flash_stm32_sem_give) to avoid confusion with locking
|
|
|
|
* actual flash pages.
|
|
|
|
*/
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline void _flash_stm32_sem_take(const struct device *dev)
|
2017-05-02 14:55:08 +00:00
|
|
|
{
|
2017-08-30 14:02:10 -04:00
|
|
|
k_sem_take(&FLASH_STM32_PRIV(dev)->sem, K_FOREVER);
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_lock(CFG_HW_FLASH_SEMID, HSEM_LOCK_WAIT_FOREVER);
|
2017-08-30 14:02:10 -04:00
|
|
|
}
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline void _flash_stm32_sem_give(const struct device *dev)
|
2017-08-30 14:02:10 -04:00
|
|
|
{
|
2020-06-23 09:48:07 +02:00
|
|
|
z_stm32_hsem_unlock(CFG_HW_FLASH_SEMID);
|
2017-08-30 14:02:10 -04:00
|
|
|
k_sem_give(&FLASH_STM32_PRIV(dev)->sem);
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:03:34 +02:00
|
|
|
#define flash_stm32_sem_init(dev) k_sem_init(&FLASH_STM32_PRIV(dev)->sem, 1, 1)
|
|
|
|
#define flash_stm32_sem_take(dev) _flash_stm32_sem_take(dev)
|
|
|
|
#define flash_stm32_sem_give(dev) _flash_stm32_sem_give(dev)
|
|
|
|
#else
|
|
|
|
#define flash_stm32_sem_init(dev)
|
|
|
|
#define flash_stm32_sem_take(dev)
|
|
|
|
#define flash_stm32_sem_give(dev)
|
|
|
|
#endif
|
|
|
|
|
2019-03-27 16:52:37 +01:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32WBX)
|
2020-04-30 20:33:38 +02:00
|
|
|
static int flash_stm32_check_status(const struct device *dev)
|
2017-08-30 14:02:10 -04:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t const error =
|
2017-12-04 14:09:59 +01:00
|
|
|
#if defined(FLASH_FLAG_PGAERR)
|
2017-05-02 14:55:08 +00:00
|
|
|
FLASH_FLAG_PGAERR |
|
2017-12-04 14:09:59 +01:00
|
|
|
#endif
|
2017-05-02 14:55:08 +00:00
|
|
|
#if defined(FLASH_FLAG_RDERR)
|
|
|
|
FLASH_FLAG_RDERR |
|
|
|
|
#endif
|
|
|
|
#if defined(FLASH_FLAG_PGPERR)
|
|
|
|
FLASH_FLAG_PGPERR |
|
|
|
|
#endif
|
2017-12-04 14:09:59 +01:00
|
|
|
#if defined(FLASH_FLAG_PGSERR)
|
2017-05-02 14:55:08 +00:00
|
|
|
FLASH_FLAG_PGSERR |
|
2017-12-04 14:09:59 +01:00
|
|
|
#endif
|
|
|
|
#if defined(FLASH_FLAG_OPERR)
|
|
|
|
FLASH_FLAG_OPERR |
|
|
|
|
#endif
|
|
|
|
#if defined(FLASH_FLAG_PGERR)
|
|
|
|
FLASH_FLAG_PGERR |
|
|
|
|
#endif
|
|
|
|
FLASH_FLAG_WRPERR;
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2019-11-16 14:34:13 -08:00
|
|
|
if (FLASH_STM32_REGS(dev)->SR & error) {
|
2020-03-23 13:57:50 +01:00
|
|
|
LOG_DBG("Status: 0x%08x", FLASH_STM32_REGS(dev)->SR & error);
|
2017-05-02 14:55:08 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-27 16:52:37 +01:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBX */
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int flash_stm32_wait_flash_idle(const struct device *dev)
|
2017-05-02 14:55:08 +00:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
int64_t timeout_time = k_uptime_get() + STM32_FLASH_TIMEOUT;
|
2017-05-02 14:55:08 +00:00
|
|
|
int rc;
|
|
|
|
|
2017-08-30 14:02:10 -04:00
|
|
|
rc = flash_stm32_check_status(dev);
|
2017-05-02 14:55:08 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
2019-11-16 14:34:13 -08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32G0X)
|
|
|
|
while ((FLASH_STM32_REGS(dev)->SR & FLASH_SR_BSY1)) {
|
|
|
|
#else
|
|
|
|
while ((FLASH_STM32_REGS(dev)->SR & FLASH_SR_BSY)) {
|
|
|
|
#endif
|
2018-09-02 21:05:52 +02:00
|
|
|
if (k_uptime_get() > timeout_time) {
|
2020-03-23 13:57:50 +01:00
|
|
|
LOG_ERR("Timeout! val: %d", STM32_FLASH_TIMEOUT);
|
2018-09-02 21:05:52 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
2017-05-02 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void flash_stm32_flush_caches(const struct device *dev,
|
2018-09-02 21:05:52 +02:00
|
|
|
off_t offset, size_t len)
|
2017-05-02 14:55:08 +00:00
|
|
|
{
|
2019-07-05 15:02:21 +02:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32F3X) || \
|
|
|
|
defined(CONFIG_SOC_SERIES_STM32G0X)
|
2018-09-02 21:05:52 +02:00
|
|
|
ARG_UNUSED(dev);
|
2018-09-02 21:05:52 +02:00
|
|
|
ARG_UNUSED(offset);
|
|
|
|
ARG_UNUSED(len);
|
2019-03-27 16:52:37 +01:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F4X) || \
|
|
|
|
defined(CONFIG_SOC_SERIES_STM32L4X) || \
|
2019-09-04 09:43:59 +01:00
|
|
|
defined(CONFIG_SOC_SERIES_STM32WBX) || \
|
|
|
|
defined(CONFIG_SOC_SERIES_STM32G4X)
|
2018-09-02 21:05:52 +02:00
|
|
|
ARG_UNUSED(offset);
|
|
|
|
ARG_UNUSED(len);
|
2019-11-16 14:34:13 -08:00
|
|
|
|
|
|
|
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
|
|
|
|
|
|
|
|
if (regs->ACR & FLASH_ACR_DCEN) {
|
|
|
|
regs->ACR &= ~FLASH_ACR_DCEN;
|
|
|
|
regs->ACR |= FLASH_ACR_DCRST;
|
|
|
|
regs->ACR &= ~FLASH_ACR_DCRST;
|
|
|
|
regs->ACR |= FLASH_ACR_DCEN;
|
2017-05-02 14:55:08 +00:00
|
|
|
}
|
2018-09-02 21:05:52 +02:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F7X)
|
|
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)(CONFIG_FLASH_BASE_ADDRESS
|
|
|
|
+ offset), len);
|
2017-12-04 14:09:59 +01:00
|
|
|
#endif
|
2018-09-02 21:05:52 +02:00
|
|
|
}
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int flash_stm32_read(const struct device *dev, off_t offset,
|
|
|
|
void *data,
|
2017-05-02 14:55:08 +00:00
|
|
|
size_t len)
|
|
|
|
{
|
2017-08-30 14:02:10 -04:00
|
|
|
if (!flash_stm32_valid_range(dev, offset, len, false)) {
|
2020-03-26 09:59:43 +01:00
|
|
|
LOG_ERR("Read range invalid. Offset: %ld, len: %zu",
|
|
|
|
(long int) offset, len);
|
2017-05-02 14:55:08 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-26 09:59:43 +01:00
|
|
|
LOG_DBG("Read offset: %ld, len: %zu", (long int) offset, len);
|
2020-03-23 13:57:50 +01:00
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
memcpy(data, (uint8_t *) CONFIG_FLASH_BASE_ADDRESS + offset, len);
|
2017-05-02 14:55:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int flash_stm32_erase(const struct device *dev, off_t offset,
|
|
|
|
size_t len)
|
2017-05-02 14:55:08 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2017-08-30 14:02:10 -04:00
|
|
|
if (!flash_stm32_valid_range(dev, offset, len, true)) {
|
2020-03-26 09:59:43 +01:00
|
|
|
LOG_ERR("Erase range invalid. Offset: %ld, len: %zu",
|
|
|
|
(long int) offset, len);
|
2017-05-02 14:55:08 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-30 14:02:10 -04:00
|
|
|
flash_stm32_sem_take(dev);
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2020-03-26 09:59:43 +01:00
|
|
|
LOG_DBG("Erase offset: %ld, len: %zu", (long int) offset, len);
|
2020-03-23 13:57:50 +01:00
|
|
|
|
2021-03-12 14:35:50 +01:00
|
|
|
rc = flash_stm32_write_protection(dev, false);
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = flash_stm32_block_erase_loop(dev, offset, len);
|
|
|
|
}
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2018-09-02 21:05:52 +02:00
|
|
|
flash_stm32_flush_caches(dev, offset, len);
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2021-03-12 14:35:50 +01:00
|
|
|
int rc2 = flash_stm32_write_protection(dev, true);
|
|
|
|
|
|
|
|
if (!rc) {
|
|
|
|
rc = rc2;
|
|
|
|
}
|
|
|
|
|
2017-08-30 14:02:10 -04:00
|
|
|
flash_stm32_sem_give(dev);
|
2017-05-02 14:55:08 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int flash_stm32_write(const struct device *dev, off_t offset,
|
2017-08-30 14:02:10 -04:00
|
|
|
const void *data, size_t len)
|
2017-05-02 14:55:08 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2017-08-30 14:02:10 -04:00
|
|
|
if (!flash_stm32_valid_range(dev, offset, len, true)) {
|
2020-03-26 09:59:43 +01:00
|
|
|
LOG_ERR("Write range invalid. Offset: %ld, len: %zu",
|
|
|
|
(long int) offset, len);
|
2017-05-02 14:55:08 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-30 14:02:10 -04:00
|
|
|
flash_stm32_sem_take(dev);
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2020-03-26 09:59:43 +01:00
|
|
|
LOG_DBG("Write offset: %ld, len: %zu", (long int) offset, len);
|
2020-03-23 13:57:50 +01:00
|
|
|
|
2021-03-12 14:35:50 +01:00
|
|
|
rc = flash_stm32_write_protection(dev, false);
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = flash_stm32_write_range(dev, offset, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc2 = flash_stm32_write_protection(dev, true);
|
|
|
|
|
|
|
|
if (!rc) {
|
|
|
|
rc = rc2;
|
|
|
|
}
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2017-08-30 14:02:10 -04:00
|
|
|
flash_stm32_sem_give(dev);
|
2017-05-02 14:55:08 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int flash_stm32_write_protection(const struct device *dev, bool enable)
|
2017-05-02 14:55:08 +00:00
|
|
|
{
|
2019-11-16 14:34:13 -08:00
|
|
|
FLASH_TypeDef *regs = FLASH_STM32_REGS(dev);
|
|
|
|
|
2017-05-02 14:55:08 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (enable) {
|
2017-08-30 14:02:10 -04:00
|
|
|
rc = flash_stm32_wait_flash_idle(dev);
|
2017-05-02 14:55:08 +00:00
|
|
|
if (rc) {
|
2017-08-30 14:02:10 -04:00
|
|
|
flash_stm32_sem_give(dev);
|
2017-05-02 14:55:08 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2020-05-11 13:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(FLASH_CR_LOCK)
|
|
|
|
if (enable) {
|
2019-11-16 14:34:13 -08:00
|
|
|
regs->CR |= FLASH_CR_LOCK;
|
2017-05-02 14:55:08 +00:00
|
|
|
} else {
|
2019-11-16 14:34:13 -08:00
|
|
|
if (regs->CR & FLASH_CR_LOCK) {
|
|
|
|
regs->KEYR = FLASH_KEY1;
|
|
|
|
regs->KEYR = FLASH_KEY2;
|
2017-05-02 14:55:08 +00:00
|
|
|
}
|
2020-05-11 13:39:39 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (enable) {
|
|
|
|
regs->PECR |= FLASH_PECR_PRGLOCK;
|
|
|
|
regs->PECR |= FLASH_PECR_PELOCK;
|
|
|
|
} else {
|
|
|
|
if (regs->PECR & FLASH_PECR_PRGLOCK) {
|
|
|
|
LOG_DBG("Disabling write protection");
|
|
|
|
regs->PEKEYR = FLASH_PEKEY1;
|
|
|
|
regs->PEKEYR = FLASH_PEKEY2;
|
|
|
|
regs->PRGKEYR = FLASH_PRGKEY1;
|
|
|
|
regs->PRGKEYR = FLASH_PRGKEY2;
|
|
|
|
}
|
|
|
|
if (FLASH->PECR & FLASH_PECR_PRGLOCK) {
|
|
|
|
LOG_ERR("Unlock failed");
|
|
|
|
rc = -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
LOG_DBG("Enable write protection");
|
|
|
|
} else {
|
2020-03-23 13:57:50 +01:00
|
|
|
LOG_DBG("Disable write protection");
|
2017-05-02 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:30:32 +00:00
|
|
|
static const struct flash_parameters *
|
|
|
|
flash_stm32_get_parameters(const struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
return &flash_stm32_parameters;
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:55:08 +00:00
|
|
|
static struct flash_stm32_priv flash_data = {
|
2020-03-24 14:28:48 -05:00
|
|
|
.regs = (FLASH_TypeDef *) DT_INST_REG_ADDR(0),
|
2021-04-16 16:41:29 +05:30
|
|
|
/* Getting clocks information from device tree description depending
|
|
|
|
* on the presence of 'clocks' property.
|
|
|
|
*/
|
2021-04-08 20:25:00 +05:30
|
|
|
#if DT_INST_NODE_HAS_PROP(0, clocks)
|
|
|
|
.pclken = {
|
|
|
|
.enr = DT_INST_CLOCKS_CELL(0, bits),
|
|
|
|
.bus = DT_INST_CLOCKS_CELL(0, bus),
|
|
|
|
}
|
2017-05-02 14:55:08 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct flash_driver_api flash_stm32_api = {
|
|
|
|
.erase = flash_stm32_erase,
|
|
|
|
.write = flash_stm32_write,
|
|
|
|
.read = flash_stm32_read,
|
2020-06-03 18:30:32 +00:00
|
|
|
.get_parameters = flash_stm32_get_parameters,
|
2017-08-30 16:39:26 -04:00
|
|
|
#ifdef CONFIG_FLASH_PAGE_LAYOUT
|
|
|
|
.page_layout = flash_stm32_page_layout,
|
|
|
|
#endif
|
2017-05-02 14:55:08 +00:00
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int stm32_flash_init(const struct device *dev)
|
2017-05-02 14:55:08 +00:00
|
|
|
{
|
2020-12-09 11:10:04 +01:00
|
|
|
int rc;
|
2021-04-16 16:41:29 +05:30
|
|
|
/* Below is applicable to F0, F1, F3, G0, G4, L1, L4 & WB55 series.
|
|
|
|
* For F2, F4, F7 & H7 series, this is not applicable.
|
|
|
|
*/
|
2021-04-08 20:25:00 +05:30
|
|
|
#if DT_INST_NODE_HAS_PROP(0, clocks)
|
2019-07-30 11:03:34 +02:00
|
|
|
struct flash_stm32_priv *p = FLASH_STM32_PRIV(dev);
|
2021-02-11 11:49:24 -06:00
|
|
|
const struct device *clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2017-12-04 14:09:59 +01:00
|
|
|
/*
|
2021-04-16 16:41:29 +05:30
|
|
|
* On STM32 F0, F1, F3 & L1 series, flash interface clock source is
|
|
|
|
* always HSI, so statically enable HSI here.
|
2017-12-04 14:09:59 +01:00
|
|
|
*/
|
2019-07-30 16:21:30 +08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F0X) || \
|
|
|
|
defined(CONFIG_SOC_SERIES_STM32F1X) || \
|
2021-04-16 16:41:29 +05:30
|
|
|
defined(CONFIG_SOC_SERIES_STM32F3X) || \
|
|
|
|
defined(CONFIG_SOC_SERIES_STM32L1X)
|
2017-12-04 14:09:59 +01:00
|
|
|
LL_RCC_HSI_Enable();
|
|
|
|
|
|
|
|
while (!LL_RCC_HSI_IsReady()) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-02 14:55:08 +00:00
|
|
|
/* enable clock */
|
2018-12-07 11:09:28 +01:00
|
|
|
if (clock_control_on(clk, (clock_control_subsys_t *)&p->pclken) != 0) {
|
2020-03-23 13:57:50 +01:00
|
|
|
LOG_ERR("Failed to enable clock");
|
2018-12-07 11:09:28 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
2017-05-02 14:55:08 +00:00
|
|
|
#endif
|
|
|
|
|
2019-03-04 09:56:17 +01:00
|
|
|
#ifdef CONFIG_SOC_SERIES_STM32WBX
|
|
|
|
LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_HSEM);
|
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBX */
|
|
|
|
|
2019-07-30 11:03:34 +02:00
|
|
|
flash_stm32_sem_init(dev);
|
2017-05-02 14:55:08 +00:00
|
|
|
|
2020-03-23 13:57:50 +01:00
|
|
|
LOG_DBG("Flash initialized. BS: %zu",
|
2020-06-03 18:33:09 +00:00
|
|
|
flash_stm32_parameters.write_block_size);
|
2020-03-23 13:57:50 +01:00
|
|
|
|
2020-12-09 11:10:04 +01:00
|
|
|
/* Check Flash configuration */
|
|
|
|
rc = flash_stm32_check_configuration();
|
|
|
|
if (rc < 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-03-23 13:57:50 +01:00
|
|
|
#if ((CONFIG_FLASH_LOG_LEVEL >= LOG_LEVEL_DBG) && CONFIG_FLASH_PAGE_LAYOUT)
|
|
|
|
const struct flash_pages_layout *layout;
|
|
|
|
size_t layout_size;
|
|
|
|
|
|
|
|
flash_stm32_page_layout(dev, &layout, &layout_size);
|
|
|
|
for (size_t i = 0; i < layout_size; i++) {
|
|
|
|
LOG_DBG("Block %zu: bs: %zu count: %zu", i,
|
|
|
|
layout[i].pages_size, layout[i].pages_count);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-02 14:55:08 +00:00
|
|
|
return flash_stm32_write_protection(dev, false);
|
|
|
|
}
|
|
|
|
|
2021-04-28 10:46:57 +02:00
|
|
|
DEVICE_DT_INST_DEFINE(0, stm32_flash_init, NULL,
|
2020-12-15 11:47:13 -06:00
|
|
|
&flash_data, NULL, POST_KERNEL,
|
2017-05-02 14:55:08 +00:00
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &flash_stm32_api);
|