mirror of
https://github.com/zephyrproject-rtos/zephyr
synced 2025-08-24 06:35:59 +00:00
These changes were obtained by running a script created by Ulf Magnusson <Ulf.Magnusson@nordicsemi.no> for the following specification: 1. Read the contents of all dts_fixup.h files in Zephyr 2. Check the left-hand side of the #define macros (i.e. the X in #define X Y) 3. Check if that name is also the name of a Kconfig option 3.a If it is, then do nothing 3.b If it is not, then replace CONFIG_ with DT_ or add DT_ if it has neither of these two prefixes 4. Replace the use of the changed #define in the code itself (.c, .h, .ld) Additionally, some tweaks had to be added to this script to catch some of the macros used in the code in a parameterized form, e.g.: - CONFIG_GPIO_STM32_GPIO##__SUFFIX##_BASE_ADDRESS - CONFIG_UART_##idx##_TX_PIN - I2C_SBCON_##_num##_BASE_ADDR and to prevent adding DT_ prefix to the following symbols: - FLASH_START - FLASH_SIZE - SRAM_START - SRAM_SIZE - _ROM_ADDR - _ROM_SIZE - _RAM_ADDR - _RAM_SIZE which are surprisingly also defined in some dts_fixup.h files. Finally, some manual corrections had to be done as well: - name##_IRQ -> DT_##name##_IRQ in uart_stm32.c Signed-off-by: Andrzej Głąbek <andrzej.glabek@nordicsemi.no>
158 lines
3.5 KiB
C
158 lines
3.5 KiB
C
/*
|
|
* Copyright (c) 2016 Linaro Limited
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <logging/sys_log.h>
|
|
#include <kernel.h>
|
|
#include <device.h>
|
|
#include <string.h>
|
|
#include <flash.h>
|
|
#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;
|
|
};
|
|
|
|
/*
|
|
* 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
|
|
*
|
|
*/
|
|
|
|
static int flash_mcux_erase(struct device *dev, off_t offset, size_t len)
|
|
{
|
|
struct flash_priv *priv = dev->driver_data;
|
|
u32_t addr;
|
|
status_t rc;
|
|
unsigned int key;
|
|
|
|
if (k_sem_take(&priv->write_lock, K_NO_WAIT)) {
|
|
return -EACCES;
|
|
}
|
|
|
|
addr = offset + priv->config.PFlashBlockBase;
|
|
|
|
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;
|
|
}
|
|
|
|
static int flash_mcux_read(struct device *dev, off_t offset,
|
|
void *data, size_t len)
|
|
{
|
|
struct flash_priv *priv = dev->driver_data;
|
|
u32_t addr;
|
|
|
|
/*
|
|
* 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->config.PFlashBlockBase;
|
|
|
|
memcpy(data, (void *) addr, len);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int flash_mcux_write(struct device *dev, off_t offset,
|
|
const void *data, size_t len)
|
|
{
|
|
struct flash_priv *priv = dev->driver_data;
|
|
u32_t addr;
|
|
status_t rc;
|
|
unsigned int key;
|
|
|
|
if (k_sem_take(&priv->write_lock, K_NO_WAIT)) {
|
|
return -EACCES;
|
|
}
|
|
|
|
addr = offset + priv->config.PFlashBlockBase;
|
|
|
|
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;
|
|
}
|
|
|
|
static int flash_mcux_write_protection(struct device *dev, bool enable)
|
|
{
|
|
struct flash_priv *priv = dev->driver_data;
|
|
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 = {
|
|
.pages_count = KB(CONFIG_FLASH_SIZE) / FLASH_ERASE_BLOCK_SIZE,
|
|
.pages_size = FLASH_ERASE_BLOCK_SIZE,
|
|
};
|
|
|
|
static void flash_mcux_pages_layout(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 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,
|
|
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
|
|
.page_layout = flash_mcux_pages_layout,
|
|
#endif
|
|
.write_block_size = FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE,
|
|
};
|
|
|
|
static int flash_mcux_init(struct device *dev)
|
|
{
|
|
struct flash_priv *priv = dev->driver_data;
|
|
status_t rc;
|
|
|
|
k_sem_init(&priv->write_lock, 0, 1);
|
|
|
|
rc = FLASH_Init(&priv->config);
|
|
|
|
return (rc == kStatus_Success) ? 0 : -EIO;
|
|
}
|
|
|
|
DEVICE_AND_API_INIT(flash_mcux, DT_FLASH_DEV_NAME,
|
|
flash_mcux_init, &flash_data, NULL, POST_KERNEL,
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &flash_mcux_api);
|
|
|