2017-05-02 22:55:08 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Linaro Limited
|
|
|
|
* Copyright (c) 2017 BayLibre, SAS.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <string.h>
|
2019-06-26 03:53:50 +08:00
|
|
|
#include <drivers/flash.h>
|
2017-05-02 22:55:08 +08:00
|
|
|
#include <init.h>
|
|
|
|
#include <soc.h>
|
|
|
|
|
2017-10-27 22:11:54 +08:00
|
|
|
#include "flash_stm32.h"
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2018-09-03 03:05:52 +08:00
|
|
|
/* STM32F0: maximum erase time of 40ms for a 2K sector */
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
|
|
|
#define STM32_FLASH_TIMEOUT (K_MSEC(40))
|
2019-02-14 22:38:52 +08:00
|
|
|
/* STM32F3: maximum erase time of 40ms for a 2K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F3X)
|
|
|
|
#define STM32_FLASH_TIMEOUT (K_MSEC(40))
|
2018-09-03 03:05:52 +08:00
|
|
|
/* STM32F4: maximum erase time of 4s for a 128K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F4X)
|
|
|
|
#define STM32_FLASH_TIMEOUT (K_MSEC(4000))
|
2018-09-03 03:05:52 +08:00
|
|
|
/* STM32F7: maximum erase time of 4s for a 256K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F7X)
|
|
|
|
#define STM32_FLASH_TIMEOUT (K_MSEC(4000))
|
2018-09-03 03:05:52 +08:00
|
|
|
/* STM32L4: maximum erase time of 24.47ms for a 2K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
|
|
|
|
#define STM32_FLASH_TIMEOUT (K_MSEC(25))
|
2019-03-27 23:52:37 +08:00
|
|
|
/* STM32WB: maximum erase time of 24.5ms for a 4K sector */
|
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
|
|
|
|
#define STM32_FLASH_TIMEOUT (K_MSEC(25))
|
2018-09-03 03:05:52 +08:00
|
|
|
#endif
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2019-03-04 16:56:17 +08:00
|
|
|
#define CFG_HW_FLASH_SEMID 2
|
|
|
|
|
2017-08-31 02:02:10 +08: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.
|
|
|
|
*/
|
|
|
|
static inline void flash_stm32_sem_take(struct device *dev)
|
2017-05-02 22:55:08 +08:00
|
|
|
{
|
2019-03-04 16:56:17 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_SOC_SERIES_STM32WBX
|
2019-06-04 22:52:23 +08:00
|
|
|
while (LL_HSEM_1StepLock(HSEM, CFG_HW_FLASH_SEMID)) {
|
|
|
|
}
|
2019-03-04 16:56:17 +08:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBX */
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
k_sem_take(&FLASH_STM32_PRIV(dev)->sem, K_FOREVER);
|
|
|
|
}
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
static inline void flash_stm32_sem_give(struct device *dev)
|
|
|
|
{
|
2019-03-04 16:56:17 +08:00
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
k_sem_give(&FLASH_STM32_PRIV(dev)->sem);
|
2019-03-04 16:56:17 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_SOC_SERIES_STM32WBX
|
|
|
|
LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, 0);
|
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBX */
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:52:37 +08:00
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32WBX)
|
2017-08-31 02:02:10 +08:00
|
|
|
static int flash_stm32_check_status(struct device *dev)
|
|
|
|
{
|
2017-05-02 22:55:08 +08:00
|
|
|
u32_t const error =
|
2017-12-04 21:09:59 +08:00
|
|
|
#if defined(FLASH_FLAG_PGAERR)
|
2017-05-02 22:55:08 +08:00
|
|
|
FLASH_FLAG_PGAERR |
|
2017-12-04 21:09:59 +08:00
|
|
|
#endif
|
2017-05-02 22:55:08 +08:00
|
|
|
#if defined(FLASH_FLAG_RDERR)
|
|
|
|
FLASH_FLAG_RDERR |
|
|
|
|
#endif
|
|
|
|
#if defined(FLASH_FLAG_PGPERR)
|
|
|
|
FLASH_FLAG_PGPERR |
|
|
|
|
#endif
|
2017-12-04 21:09:59 +08:00
|
|
|
#if defined(FLASH_FLAG_PGSERR)
|
2017-05-02 22:55:08 +08:00
|
|
|
FLASH_FLAG_PGSERR |
|
2017-12-04 21:09:59 +08: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 22:55:08 +08:00
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
if (FLASH_STM32_REGS(dev)->sr & error) {
|
2017-05-02 22:55:08 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-27 23:52:37 +08:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBX */
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
int flash_stm32_wait_flash_idle(struct device *dev)
|
2017-05-02 22:55:08 +08:00
|
|
|
{
|
2018-09-03 03:05:52 +08:00
|
|
|
s64_t timeout_time = k_uptime_get() + STM32_FLASH_TIMEOUT;
|
2017-05-02 22:55:08 +08:00
|
|
|
int rc;
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
rc = flash_stm32_check_status(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
if (rc < 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2018-09-03 03:05:52 +08:00
|
|
|
while ((FLASH_STM32_REGS(dev)->sr & FLASH_SR_BSY)) {
|
|
|
|
if (k_uptime_get() > timeout_time) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
2017-05-02 22:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-03 03:05:52 +08:00
|
|
|
static void flash_stm32_flush_caches(struct device *dev,
|
|
|
|
off_t offset, size_t len)
|
2017-05-02 22:55:08 +08:00
|
|
|
{
|
2019-02-14 22:38:52 +08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32F3X)
|
2018-09-03 03:05:52 +08:00
|
|
|
ARG_UNUSED(dev);
|
2018-09-03 03:05:52 +08:00
|
|
|
ARG_UNUSED(offset);
|
|
|
|
ARG_UNUSED(len);
|
2019-03-27 23:52:37 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F4X) || \
|
|
|
|
defined(CONFIG_SOC_SERIES_STM32L4X) || \
|
|
|
|
defined(CONFIG_SOC_SERIES_STM32WBX)
|
2018-09-03 03:05:52 +08:00
|
|
|
ARG_UNUSED(offset);
|
|
|
|
ARG_UNUSED(len);
|
2017-05-02 22:55:08 +08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F4X)
|
2017-08-31 02:02:10 +08:00
|
|
|
struct stm32f4x_flash *regs = FLASH_STM32_REGS(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
|
2017-08-31 02:02:10 +08:00
|
|
|
struct stm32l4x_flash *regs = FLASH_STM32_REGS(dev);
|
2019-03-27 23:52:37 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
|
|
|
|
struct stm32wbx_flash *regs = FLASH_STM32_REGS(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
#endif
|
|
|
|
if (regs->acr.val & FLASH_ACR_DCEN) {
|
|
|
|
regs->acr.val &= ~FLASH_ACR_DCEN;
|
|
|
|
regs->acr.val |= FLASH_ACR_DCRST;
|
|
|
|
regs->acr.val &= ~FLASH_ACR_DCRST;
|
|
|
|
regs->acr.val |= FLASH_ACR_DCEN;
|
|
|
|
}
|
2018-09-03 03:05:52 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F7X)
|
|
|
|
SCB_InvalidateDCache_by_Addr((uint32_t *)(CONFIG_FLASH_BASE_ADDRESS
|
|
|
|
+ offset), len);
|
2017-12-04 21:09:59 +08:00
|
|
|
#endif
|
2018-09-03 03:05:52 +08:00
|
|
|
}
|
2017-05-02 22:55:08 +08:00
|
|
|
|
|
|
|
static int flash_stm32_read(struct device *dev, off_t offset, void *data,
|
|
|
|
size_t len)
|
|
|
|
{
|
2017-08-31 02:02:10 +08:00
|
|
|
if (!flash_stm32_valid_range(dev, offset, len, false)) {
|
2017-05-02 22:55:08 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-14 20:24:09 +08:00
|
|
|
memcpy(data, (u8_t *) CONFIG_FLASH_BASE_ADDRESS + offset, len);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
static int flash_stm32_erase(struct device *dev, off_t offset, size_t len)
|
2017-05-02 22:55:08 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
if (!flash_stm32_valid_range(dev, offset, len, true)) {
|
2017-05-02 22:55:08 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
flash_stm32_sem_take(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
rc = flash_stm32_block_erase_loop(dev, offset, len);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2018-09-03 03:05:52 +08:00
|
|
|
flash_stm32_flush_caches(dev, offset, len);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
flash_stm32_sem_give(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
static int flash_stm32_write(struct device *dev, off_t offset,
|
|
|
|
const void *data, size_t len)
|
2017-05-02 22:55:08 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
if (!flash_stm32_valid_range(dev, offset, len, true)) {
|
2017-05-02 22:55:08 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
flash_stm32_sem_take(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
rc = flash_stm32_write_range(dev, offset, data, len);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
flash_stm32_sem_give(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flash_stm32_write_protection(struct device *dev, bool enable)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F4X)
|
2017-08-31 02:02:10 +08:00
|
|
|
struct stm32f4x_flash *regs = FLASH_STM32_REGS(dev);
|
2018-09-03 03:05:52 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F7X)
|
|
|
|
struct stm32f7x_flash *regs = FLASH_STM32_REGS(dev);
|
2017-12-04 21:09:59 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F0X)
|
|
|
|
struct stm32f0x_flash *regs = FLASH_STM32_REGS(dev);
|
2019-02-14 22:38:52 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F3X)
|
|
|
|
struct stm32f3x_flash *regs = FLASH_STM32_REGS(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
|
2017-08-31 02:02:10 +08:00
|
|
|
struct stm32l4x_flash *regs = FLASH_STM32_REGS(dev);
|
2019-03-27 23:52:37 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
|
|
|
|
struct stm32wbx_flash *regs = FLASH_STM32_REGS(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
#endif
|
|
|
|
int rc = 0;
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
flash_stm32_sem_take(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
|
|
|
if (enable) {
|
2017-08-31 02:02:10 +08:00
|
|
|
rc = flash_stm32_wait_flash_idle(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
if (rc) {
|
2017-08-31 02:02:10 +08:00
|
|
|
flash_stm32_sem_give(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
regs->cr |= FLASH_CR_LOCK;
|
|
|
|
} else {
|
|
|
|
if (regs->cr & FLASH_CR_LOCK) {
|
|
|
|
regs->keyr = FLASH_KEY1;
|
|
|
|
regs->keyr = FLASH_KEY2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:02:10 +08:00
|
|
|
flash_stm32_sem_give(dev);
|
2017-05-02 22:55:08 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct flash_stm32_priv flash_data = {
|
2017-12-04 21:09:59 +08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F0X)
|
2018-11-13 22:15:23 +08:00
|
|
|
.regs = (struct stm32f0x_flash *) DT_FLASH_DEV_BASE_ADDRESS,
|
2017-12-04 21:09:59 +08:00
|
|
|
.pclken = { .bus = STM32_CLOCK_BUS_AHB1,
|
|
|
|
.enr = LL_AHB1_GRP1_PERIPH_FLASH },
|
2019-02-14 22:38:52 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F3X)
|
|
|
|
.regs = (struct stm32f3x_flash *) DT_FLASH_DEV_BASE_ADDRESS,
|
|
|
|
.pclken = { .bus = STM32_CLOCK_BUS_AHB1,
|
|
|
|
.enr = LL_AHB1_GRP1_PERIPH_FLASH },
|
2017-12-04 21:09:59 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F4X)
|
2018-11-13 22:15:23 +08:00
|
|
|
.regs = (struct stm32f4x_flash *) DT_FLASH_DEV_BASE_ADDRESS,
|
2018-09-03 03:05:52 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32F7X)
|
|
|
|
.regs = (struct stm32f7x_flash *) DT_FLASH_DEV_BASE_ADDRESS,
|
2017-05-02 22:55:08 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32L4X)
|
2018-11-13 22:15:23 +08:00
|
|
|
.regs = (struct stm32l4x_flash *) DT_FLASH_DEV_BASE_ADDRESS,
|
2017-05-02 22:55:08 +08:00
|
|
|
.pclken = { .bus = STM32_CLOCK_BUS_AHB1,
|
|
|
|
.enr = LL_AHB1_GRP1_PERIPH_FLASH },
|
2019-03-27 23:52:37 +08:00
|
|
|
#elif defined(CONFIG_SOC_SERIES_STM32WBX)
|
|
|
|
.regs = (struct stm32wbx_flash *) DT_FLASH_DEV_BASE_ADDRESS,
|
2017-05-02 22:55:08 +08:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct flash_driver_api flash_stm32_api = {
|
|
|
|
.write_protection = flash_stm32_write_protection,
|
|
|
|
.erase = flash_stm32_erase,
|
|
|
|
.write = flash_stm32_write,
|
|
|
|
.read = flash_stm32_read,
|
2017-08-31 04:39:26 +08:00
|
|
|
#ifdef CONFIG_FLASH_PAGE_LAYOUT
|
|
|
|
.page_layout = flash_stm32_page_layout,
|
|
|
|
#endif
|
2019-06-12 03:20:32 +08:00
|
|
|
#ifdef DT_INST_0_SOC_NV_FLASH_WRITE_BLOCK_SIZE
|
|
|
|
.write_block_size = DT_INST_0_SOC_NV_FLASH_WRITE_BLOCK_SIZE,
|
2018-01-12 18:52:13 +08:00
|
|
|
#else
|
|
|
|
#error Flash write block size not available
|
|
|
|
/* Flash Write block size is extracted from device tree */
|
|
|
|
/* as flash node property 'write-block-size' */
|
2017-09-29 19:03:40 +08:00
|
|
|
#endif
|
2017-05-02 22:55:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int stm32_flash_init(struct device *dev)
|
|
|
|
{
|
2017-08-31 02:02:10 +08:00
|
|
|
struct flash_stm32_priv *p = FLASH_STM32_PRIV(dev);
|
2017-12-04 21:09:59 +08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32L4X) || \
|
2019-02-14 22:38:52 +08:00
|
|
|
defined(CONFIG_SOC_SERIES_STM32F0X) || \
|
|
|
|
defined(CONFIG_SOC_SERIES_STM32F3X)
|
2017-05-02 22:55:08 +08:00
|
|
|
struct device *clk = device_get_binding(STM32_CLOCK_CONTROL_NAME);
|
|
|
|
|
2017-12-04 21:09:59 +08:00
|
|
|
/*
|
|
|
|
* On STM32F0, Flash interface clock source is always HSI,
|
|
|
|
* so statically enable HSI here.
|
|
|
|
*/
|
2019-02-14 22:38:52 +08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32F3X)
|
2017-12-04 21:09:59 +08:00
|
|
|
LL_RCC_HSI_Enable();
|
|
|
|
|
|
|
|
while (!LL_RCC_HSI_IsReady()) {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-02 22:55:08 +08:00
|
|
|
/* enable clock */
|
2018-12-07 18:09:28 +08:00
|
|
|
if (clock_control_on(clk, (clock_control_subsys_t *)&p->pclken) != 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
2017-05-02 22:55:08 +08:00
|
|
|
#endif
|
|
|
|
|
2019-03-04 16:56:17 +08:00
|
|
|
#ifdef CONFIG_SOC_SERIES_STM32WBX
|
|
|
|
LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_HSEM);
|
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBX */
|
|
|
|
|
2017-05-02 22:55:08 +08:00
|
|
|
k_sem_init(&p->sem, 1, 1);
|
|
|
|
|
|
|
|
return flash_stm32_write_protection(dev, false);
|
|
|
|
}
|
|
|
|
|
2018-11-13 22:15:23 +08:00
|
|
|
DEVICE_AND_API_INIT(stm32_flash, DT_FLASH_DEV_NAME,
|
2017-05-02 22:55:08 +08:00
|
|
|
stm32_flash_init, &flash_data, NULL, POST_KERNEL,
|
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &flash_stm32_api);
|