drivers: flash: support for Nuvoton numaker series RMC

Add Nuvoton numaker series flash memory controller(RMC) with erase,
 read & write features of soc-flash.

Signed-off-by: cyliang tw <cyliang@nuvoton.com>
This commit is contained in:
cyliang tw 2024-03-11 16:33:47 +08:00 committed by Alberto Escolar
parent e22958ceaf
commit 47ed29bf49
7 changed files with 308 additions and 1 deletions

View File

@ -130,3 +130,4 @@ zephyr_library_sources_ifdef(CONFIG_FLASH_INFINEON_CAT1 flash_ifx_cat1.c)
zephyr_library_sources_ifdef(CONFIG_SOC_FLASH_NUMAKER soc_flash_numaker.c)
zephyr_library_sources_ifdef(CONFIG_SOC_FLASH_NRF_RRAM soc_flash_nrf_rram.c)
zephyr_library_sources_ifdef(CONFIG_SOC_FLASH_NRF_MRAM soc_flash_nrf_mram.c)
zephyr_library_sources_ifdef(CONFIG_SOC_FLASH_NUMAKER_RMC soc_flash_numaker_rmc.c)

View File

@ -165,4 +165,6 @@ source "drivers/flash/Kconfig.nrf_rram"
source "drivers/flash/Kconfig.nrf_mram"
source "drivers/flash/Kconfig.numaker_rmc"
endif # FLASH

View File

@ -0,0 +1,16 @@
# NUMAKER GPIO driver configuration options
# Copyright (c) 2024 Nuvoton Technology Corporation.
# SPDX-License-Identifier: Apache-2.0
config SOC_FLASH_NUMAKER_RMC
bool "Nuvoton NuMaker MCU embedded RRAM memory controller"
default y
select FLASH_HAS_PAGE_LAYOUT
select FLASH_HAS_DRIVER_ENABLED
select HAS_NUMAKER_RMC
depends on DT_HAS_NUVOTON_NUMAKER_RMC_ENABLED
help
This option enables the RMC driver for Nuvoton NuMaker family of
processors.
Say y if you wish to enable NuMaker RMC.

View File

@ -0,0 +1,275 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright (c) 2024 Nuvoton Technology Corporation.
*/
#define DT_DRV_COMPAT nuvoton_numaker_rmc
#include <string.h>
#include <errno.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/logging/log.h>
#include "flash_priv.h"
#include <NuMicro.h>
LOG_MODULE_REGISTER(flash_numaker_rmc, CONFIG_FLASH_LOG_LEVEL);
#define SOC_NV_FLASH_NODE DT_INST(0, soc_nv_flash)
#define SOC_NV_FLASH_WRITE_BLOCK_SIZE DT_PROP_OR(SOC_NV_FLASH_NODE, write_block_size, 0x04)
struct flash_numaker_data {
RMC_T *rmc;
struct k_sem write_lock;
uint32_t flash_block_base;
};
static const struct flash_parameters flash_numaker_parameters = {
.write_block_size = SOC_NV_FLASH_WRITE_BLOCK_SIZE,
.erase_value = 0xff,
};
/* Validate offset and length */
static bool flash_numaker_is_range_valid(off_t offset, size_t len)
{
uint32_t aprom_size = RMC_APROM_END - RMC_APROM_BASE;
/* check for min value */
if ((offset < 0) || (len == 0)) {
return false;
}
/* check for max value */
if (offset >= aprom_size || len > aprom_size || (aprom_size - offset) < len) {
return false;
}
return true;
}
/*
* Erase a flash memory area.
*
* param dev Device struct
* param offset The address's offset
* param len The size of the buffer
* return 0 on success
* return -EINVAL erroneous code
*/
static int flash_numaker_erase(const struct device *dev, off_t offset, size_t len)
{
struct flash_numaker_data *dev_data = dev->data;
uint32_t rc = 0;
unsigned int key;
int page_nums = len / RMC_FLASH_PAGE_SIZE;
uint32_t addr = dev_data->flash_block_base + offset;
/* return SUCCESS for len == 0 (required by tests/drivers/flash) */
if (len == 0) {
return 0;
}
/* Validate range */
if (!flash_numaker_is_range_valid(offset, len)) {
return -EINVAL;
}
/* check alignment and erase only by pages */
if (((addr % RMC_FLASH_PAGE_SIZE) != 0) || ((len % RMC_FLASH_PAGE_SIZE) != 0)) {
return -EINVAL;
}
/* take semaphore */
if (k_sem_take(&dev_data->write_lock, K_NO_WAIT)) {
return -EACCES;
}
SYS_UnlockReg();
key = irq_lock();
while (page_nums) {
/* erase page */
if (RMC_Erase(addr)) {
LOG_ERR("Erase flash page failed or erase time-out");
rc = -EIO;
goto done;
}
page_nums--;
addr += RMC_FLASH_PAGE_SIZE;
}
done:
SYS_LockReg();
irq_unlock(key);
/* release semaphore */
k_sem_give(&dev_data->write_lock);
return rc;
}
/*
* Read a flash memory area.
*
* param dev Device struct
* param offset The address's offset
* param data The buffer to store or read the value
* param length The size of the buffer
* return 0 on success,
* return -EIO erroneous code
*/
static int flash_numaker_read(const struct device *dev, off_t offset, void *data, size_t len)
{
struct flash_numaker_data *dev_data = dev->data;
uint32_t addr = dev_data->flash_block_base + offset;
/* return SUCCESS for len == 0 (required by tests/drivers/flash) */
if (len == 0) {
return 0;
}
/* Validate range */
if (!flash_numaker_is_range_valid(offset, len)) {
return -EINVAL;
}
/* read flash */
memcpy(data, (void *)addr, len);
return 0;
}
static int32_t flash_numaker_block_write(uint32_t u32_addr, const uint8_t *pu8_data, int block_size)
{
int32_t retval;
const uint32_t *pu32_data = (const uint32_t *)pu8_data;
SYS_UnlockReg();
if (block_size == 4) {
retval = RMC_Write(u32_addr, *pu32_data);
} else if (block_size == 8) {
retval = RMC_Write(u32_addr, *pu32_data) |
RMC_Write(u32_addr + 4, *(pu32_data + 1));
} else {
retval = -1;
}
SYS_LockReg();
return retval;
}
static int flash_numaker_write(const struct device *dev, off_t offset, const void *data, size_t len)
{
struct flash_numaker_data *dev_data = dev->data;
uint32_t rc = 0;
unsigned int key;
uint32_t addr = dev_data->flash_block_base + offset;
int block_size = flash_numaker_parameters.write_block_size;
int blocks = len / flash_numaker_parameters.write_block_size;
const uint8_t *pu8_data = (const uint8_t *)data;
/* return SUCCESS for len == 0 (required by tests/drivers/flash) */
if (len == 0) {
return 0;
}
/* Validate range */
if (!flash_numaker_is_range_valid(offset, len)) {
return -EINVAL;
}
/* Validate address alignment */
if ((addr % flash_numaker_parameters.write_block_size) != 0) {
return -EINVAL;
}
/* Validate write size be multiples of the write block size */
if ((len % block_size) != 0) {
return -EINVAL;
}
/* Validate offset be multiples of the write block size */
if ((offset % block_size) != 0) {
return -EINVAL;
}
if (k_sem_take(&dev_data->write_lock, K_FOREVER)) {
return -EACCES;
}
key = irq_lock();
while (blocks) {
if (flash_numaker_block_write(addr, pu8_data, block_size)) {
rc = -EIO;
goto done;
}
pu8_data += block_size;
addr += block_size;
blocks--;
}
done:
irq_unlock(key);
k_sem_give(&dev_data->write_lock);
return rc;
}
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
static const struct flash_pages_layout dev_layout = {
.pages_count = DT_REG_SIZE(SOC_NV_FLASH_NODE) /
DT_PROP(SOC_NV_FLASH_NODE, erase_block_size),
.pages_size = DT_PROP(SOC_NV_FLASH_NODE, erase_block_size),
};
static void flash_numaker_pages_layout(const struct device *dev,
const struct flash_pages_layout **layout,
size_t *layout_size)
{
*layout = &dev_layout;
*layout_size = 1;
}
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
static const struct flash_parameters *flash_numaker_get_parameters(const struct device *dev)
{
ARG_UNUSED(dev);
return &flash_numaker_parameters;
}
static struct flash_numaker_data flash_data;
static const struct flash_driver_api flash_numaker_api = {
.erase = flash_numaker_erase,
.write = flash_numaker_write,
.read = flash_numaker_read,
.get_parameters = flash_numaker_get_parameters,
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
.page_layout = flash_numaker_pages_layout,
#endif
};
static int flash_numaker_init(const struct device *dev)
{
struct flash_numaker_data *dev_data = dev->data;
k_sem_init(&dev_data->write_lock, 1, 1);
/* Enable RMC ISP function */
SYS_UnlockReg();
RMC_Open();
/* Enable APROM update. */
RMC_ENABLE_AP_UPDATE();
SYS_LockReg();
dev_data->flash_block_base = (uint32_t)RMC_APROM_BASE;
dev_data->rmc = (RMC_T *)DT_REG_ADDR(DT_NODELABEL(rmc));
return 0;
}
DEVICE_DT_INST_DEFINE(0, flash_numaker_init, NULL, &flash_data, NULL, POST_KERNEL,
CONFIG_FLASH_INIT_PRIORITY, &flash_numaker_api);

View File

@ -0,0 +1,9 @@
description: Nuvoton NuMaker RRAM Memory Controller
compatible: "nuvoton,numaker-rmc"
include: flash-controller.yaml
properties:
reg:
required: true

View File

@ -71,4 +71,8 @@ menu "Nuvoton NuMaker drivers"
bool "NuMaker ADC"
help
Enable Nuvoton ADC HAL module driver
config HAS_NUMAKER_RMC
bool "NuMaker RMC"
help
Enable Nuvoton RMC HAL module driver
endmenu

View File

@ -188,7 +188,7 @@ manifest:
groups:
- hal
- name: hal_nuvoton
revision: 68a91bb343ff47e40dbd9189a7d6e3ee801a7135
revision: cf24f9ebd893b349cec439ef7eaac6554a6b6d6f
path: modules/hal/nuvoton
groups:
- hal