2019-05-22 23:07:50 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Linaro Limited
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-04-09 21:52:05 +08:00
|
|
|
#define DT_DRV_COMPAT openisa_rv32m1_ftfe
|
|
|
|
#define SOC_NV_FLASH_NODE DT_INST(0, soc_nv_flash)
|
2020-03-25 04:38:31 +08:00
|
|
|
|
2019-05-22 23:07:50 +08:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <string.h>
|
2019-06-26 03:53:50 +08:00
|
|
|
#include <drivers/flash.h>
|
2019-05-22 23:07:50 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <soc.h>
|
|
|
|
#include "flash_priv.h"
|
|
|
|
|
|
|
|
#include "fsl_common.h"
|
|
|
|
#include "fsl_flash.h"
|
|
|
|
|
|
|
|
struct flash_priv {
|
|
|
|
flash_config_t config;
|
|
|
|
/*
|
|
|
|
* HACK: flash write protection is managed in software.
|
|
|
|
*/
|
|
|
|
struct k_sem write_lock;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t pflash_block_base;
|
2019-05-22 23:07:50 +08:00
|
|
|
};
|
|
|
|
|
2020-06-04 02:30:32 +08:00
|
|
|
static const struct flash_parameters flash_mcux_parameters = {
|
2020-06-04 02:33:09 +08:00
|
|
|
.write_block_size = FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE,
|
2020-06-04 02:30:32 +08:00
|
|
|
.erase_value = 0xff,
|
|
|
|
};
|
|
|
|
|
2019-05-22 23:07:50 +08:00
|
|
|
/*
|
|
|
|
* Interrupt vectors could be executed from flash hence the need for locking.
|
|
|
|
* The underlying MCUX driver takes care of copying the functions to SRAM.
|
|
|
|
*
|
|
|
|
* For more information, see the application note below on Read-While-Write
|
|
|
|
* http://cache.freescale.com/files/32bit/doc/app_note/AN4695.pdf
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int flash_mcux_erase(const struct device *dev, off_t offset,
|
|
|
|
size_t len)
|
2019-05-22 23:07:50 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct flash_priv *priv = dev->data;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t addr;
|
2019-05-22 23:07:50 +08:00
|
|
|
status_t rc;
|
|
|
|
unsigned int key;
|
|
|
|
|
|
|
|
if (k_sem_take(&priv->write_lock, K_NO_WAIT)) {
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = offset + priv->pflash_block_base;
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
rc = FLASH_Erase(&priv->config, addr, len, kFLASH_ApiEraseKey);
|
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
k_sem_give(&priv->write_lock);
|
|
|
|
|
|
|
|
return (rc == kStatus_Success) ? 0 : -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int flash_mcux_read(const struct device *dev, off_t offset,
|
2019-05-22 23:07:50 +08:00
|
|
|
void *data, size_t len)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct flash_priv *priv = dev->data;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t addr;
|
2019-05-22 23:07:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The MCUX supports different flash chips whose valid ranges are
|
|
|
|
* hidden below the API: until the API export these ranges, we can not
|
|
|
|
* do any generic validation
|
|
|
|
*/
|
|
|
|
addr = offset + priv->pflash_block_base;
|
|
|
|
|
|
|
|
memcpy(data, (void *) addr, len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int flash_mcux_write(const struct device *dev, off_t offset,
|
2019-05-22 23:07:50 +08:00
|
|
|
const void *data, size_t len)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct flash_priv *priv = dev->data;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t addr;
|
2019-05-22 23:07:50 +08:00
|
|
|
status_t rc;
|
|
|
|
unsigned int key;
|
|
|
|
|
|
|
|
if (k_sem_take(&priv->write_lock, K_NO_WAIT)) {
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = offset + priv->pflash_block_base;
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
rc = FLASH_Program(&priv->config, addr, (uint32_t *) data, len);
|
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
k_sem_give(&priv->write_lock);
|
|
|
|
|
|
|
|
return (rc == kStatus_Success) ? 0 : -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int flash_mcux_write_protection(const struct device *dev, bool enable)
|
2019-05-22 23:07:50 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct flash_priv *priv = dev->data;
|
2019-05-22 23:07:50 +08:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
rc = k_sem_take(&priv->write_lock, K_FOREVER);
|
|
|
|
} else {
|
|
|
|
k_sem_give(&priv->write_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
|
|
|
|
static const struct flash_pages_layout dev_layout = {
|
2020-04-09 21:52:05 +08:00
|
|
|
.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),
|
2019-05-22 23:07:50 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static void flash_mcux_pages_layout(const struct device *dev,
|
|
|
|
const struct flash_pages_layout **layout,
|
|
|
|
size_t *layout_size)
|
2019-05-22 23:07:50 +08:00
|
|
|
{
|
|
|
|
*layout = &dev_layout;
|
|
|
|
*layout_size = 1;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
|
|
|
|
|
2020-06-04 02:30:32 +08:00
|
|
|
static const struct flash_parameters *
|
|
|
|
flash_mcux_get_parameters(const struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
return &flash_mcux_parameters;
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:07:50 +08:00
|
|
|
static struct flash_priv flash_data;
|
|
|
|
|
|
|
|
static const struct flash_driver_api flash_mcux_api = {
|
|
|
|
.write_protection = flash_mcux_write_protection,
|
|
|
|
.erase = flash_mcux_erase,
|
|
|
|
.write = flash_mcux_write,
|
|
|
|
.read = flash_mcux_read,
|
2020-06-04 02:30:32 +08:00
|
|
|
.get_parameters = flash_mcux_get_parameters,
|
2019-05-22 23:07:50 +08:00
|
|
|
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
|
|
|
|
.page_layout = flash_mcux_pages_layout,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int flash_mcux_init(const struct device *dev)
|
2019-05-22 23:07:50 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct flash_priv *priv = dev->data;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t pflash_block_base;
|
2019-05-22 23:07:50 +08:00
|
|
|
status_t rc;
|
|
|
|
|
|
|
|
CLOCK_EnableClock(kCLOCK_Mscm);
|
|
|
|
|
|
|
|
k_sem_init(&priv->write_lock, 0, 1);
|
|
|
|
|
|
|
|
rc = FLASH_Init(&priv->config);
|
|
|
|
|
|
|
|
FLASH_GetProperty(&priv->config, kFLASH_PropertyPflashBlockBaseAddr,
|
|
|
|
(uint32_t *)&pflash_block_base);
|
2020-05-28 00:26:57 +08:00
|
|
|
priv->pflash_block_base = (uint32_t) pflash_block_base;
|
2019-05-22 23:07:50 +08:00
|
|
|
|
|
|
|
return (rc == kStatus_Success) ? 0 : -EIO;
|
|
|
|
}
|
|
|
|
|
2020-04-08 22:39:36 +08:00
|
|
|
DEVICE_AND_API_INIT(flash_mcux, DT_INST_LABEL(0),
|
2019-05-22 23:07:50 +08:00
|
|
|
flash_mcux_init, &flash_data, NULL, POST_KERNEL,
|
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &flash_mcux_api);
|