drivers: flash: add flash_simulator driver

This commit adds a flash driver implementation that writes to RAM and
exports statistics through stats.h. It can be used to simulate flash
memory for testing purposes.

Signed-off-by: Emanuele Di Santo <emdi@nordicsemi.no>
Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
Signed-off-by: Kamil Piszczek <Kamil.Piszczek@nordicsemi.no>
This commit is contained in:
Kamil Piszczek 2019-04-18 14:55:30 +02:00 committed by Carles Cufí
commit c837d85c2b
9 changed files with 407 additions and 0 deletions

View file

@ -159,6 +159,7 @@
/dts/bindings/*/st,stm32* @erwango /dts/bindings/*/st,stm32* @erwango
/dts/bindings/sensor/ams,ens210.yaml @alexanderwachter /dts/bindings/sensor/ams,ens210.yaml @alexanderwachter
/dts/bindings/sensor/ams,iaqcore.yaml @alexanderwachter /dts/bindings/sensor/ams,iaqcore.yaml @alexanderwachter
/dts/common/sim-flash.dtsi @nvlsianpu
/ext/fs/ @nashif @wentongwu /ext/fs/ @nashif @wentongwu
/ext/hal/cmsis/ @MaureenHelm @galak /ext/hal/cmsis/ @MaureenHelm @galak
/ext/hal/libmetal/ @galak /ext/hal/libmetal/ @galak

View file

@ -7,4 +7,6 @@
#define DT_ETH_E1000_IRQ_PRIORITY DT_INTEL_E1000_FEBC0000_IRQ_0_PRIORITY #define DT_ETH_E1000_IRQ_PRIORITY DT_INTEL_E1000_FEBC0000_IRQ_0_PRIORITY
#define DT_ETH_E1000_IRQ_FLAGS DT_INTEL_E1000_FEBC0000_IRQ_0_SENSE #define DT_ETH_E1000_IRQ_FLAGS DT_INTEL_E1000_FEBC0000_IRQ_0_SENSE
#define DT_SIM_FLASH_DEV_NAME DT_SIM_FLASH_SIM_FLASH_LABEL
/* End of Board Level DTS fixup file */ /* End of Board Level DTS fixup file */

View file

@ -13,6 +13,7 @@
#endif #endif
#include <ia32.dtsi> #include <ia32.dtsi>
#include "../../../dts/common/sim-flash.dtsi"
/ { / {
model = "QEMU X86"; model = "QEMU X86";

View file

@ -3,6 +3,7 @@
zephyr_library() zephyr_library()
zephyr_library_sources_ifdef(CONFIG_SPI_NOR spi_nor.c) zephyr_library_sources_ifdef(CONFIG_SPI_NOR spi_nor.c)
zephyr_library_sources_ifdef(CONFIG_FLASH_SIMULATOR flash_simulator.c)
zephyr_library_sources_ifdef(CONFIG_SPI_FLASH_W25QXXDV spi_flash_w25qxxdv.c) zephyr_library_sources_ifdef(CONFIG_SPI_FLASH_W25QXXDV spi_flash_w25qxxdv.c)
zephyr_library_sources_ifdef(CONFIG_SOC_FLASH_QMSI soc_flash_qmsi.c) zephyr_library_sources_ifdef(CONFIG_SOC_FLASH_QMSI soc_flash_qmsi.c)
zephyr_library_sources_ifdef(CONFIG_SOC_FLASH_NRF soc_flash_nrf.c) zephyr_library_sources_ifdef(CONFIG_SOC_FLASH_NRF soc_flash_nrf.c)

View file

@ -65,4 +65,6 @@ source "drivers/flash/Kconfig.sam"
source "drivers/flash/Kconfig.w25qxxdv" source "drivers/flash/Kconfig.w25qxxdv"
source "drivers/flash/Kconfig.simulator"
endif # FLASH endif # FLASH

View file

@ -0,0 +1,94 @@
# Kconfig - Flash simulator config
#
# Copyright (c) 2018 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
menuconfig FLASH_SIMULATOR
bool
prompt "Flash simulator"
default n
depends on FLASH
select STATS
select STATS_NAMES
select FLASH_HAS_PAGE_LAYOUT
select FLASH_HAS_DRIVER_ENABLED
help
Enable the flash simulator.
if FLASH_SIMULATOR
config FLASH_SIMULATOR_BASE_OFFSET
int
prompt "Base offset of the flash"
default 0
help
The base offset of the flash.
The address space of the simulated flash ranges from this value
to this value plus FLASH_SIMULATOR_ERASE_UNIT * FLASH_SIMULATOR_FLASH_SIZE.
config FLASH_SIMULATOR_ERASE_UNIT
int
prompt "Erase unit size in bytes"
default 4096
range 1 65536
help
The smallest area of flash memory that can be indipendently erased.
config FLASH_SIMULATOR_PROG_UNIT
int
prompt "Program unit size in bytes"
default 4
range 1 4096
help
The smallest area of flash memory that can be indipendently programmed.
config FLASH_SIMULATOR_FLASH_SIZE
int
prompt "Flash size in erase units"
default 4
range 1 65536
help
The size of the simulated flash area expressed in erase units.
config FLASH_SIMULATOR_DOUBLE_WRITES
bool
prompt "Allow program units to be programmed more than once"
default n
help
If selected, writing to a non-erased program unit will succeed, otherwise, it will return an error.
Keep in mind that write operations can only pull bits to zero, regardless.
config FLASH_SIMULATOR_ERASE_PROTECT
bool
prompt "Enable erase protection on write protection"
default y
help
If selected, turning on write protection will also prevent erasing.
config FLASH_SIMULATOR_SIMULATE_TIMING
bool
prompt "Enable hardware timing simulation"
if FLASH_SIMULATOR_SIMULATE_TIMING
config FLASH_SIMULATOR_MIN_READ_TIME_US
int
prompt "Minimum read time (µS)"
default 2
range 1 1000000
config FLASH_SIMULATOR_MIN_WRITE_TIME_US
int
prompt "Minimum write time (µS)"
default 100
range 1 1000000
config FLASH_SIMULATOR_MIN_ERASE_TIME_US
int
prompt "Minimum erase time (µS)"
default 2000
range 1 1000000
endif
endif # FLASH_SIMULATOR

View file

@ -0,0 +1,267 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <flash.h>
#include <init.h>
#include <kernel.h>
#include <misc/util.h>
#include <random/rand32.h>
#include <stats.h>
#include <string.h>
#if (CONFIG_FLASH_SIMULATOR_ERASE_UNIT % CONFIG_FLASH_SIMULATOR_PROG_UNIT)
#error "Erase unit must be a multiple of program unit"
#endif
#define FLASH(addr) (mock_flash + (addr) - CONFIG_FLASH_SIMULATOR_BASE_OFFSET)
#define FLASH_SIZE \
(CONFIG_FLASH_SIMULATOR_FLASH_SIZE * CONFIG_FLASH_SIMULATOR_ERASE_UNIT)
#define STATS_SECT_EC(N, _) STATS_SECT_ENTRY32(erase_cycles_unit##N)
#define STATS_NAME_EC(N, _) STATS_NAME(flash_sim_stats, erase_cycles_unit##N)
#define STATS_SECT_DIRTYR(N, _) STATS_SECT_ENTRY32(dirty_read_unit##N)
#define STATS_NAME_DIRTYR(N, _) STATS_NAME(flash_sim_stats, dirty_read_unit##N)
/* increment a unit erase cycles counter */
#define ERASE_CYCLES_INC(U) (*(&flash_sim_stats.erase_cycles_unit0 + (U)) += 1)
/* simulator statistcs */
STATS_SECT_START(flash_sim_stats)
STATS_SECT_ENTRY32(bytes_read) /* total bytes read */
STATS_SECT_ENTRY32(bytes_written) /* total bytes written */
STATS_SECT_ENTRY32(double_writes) /* num. of writes to non-erased units */
STATS_SECT_ENTRY32(flash_read_calls) /* calls to flash_read() */
STATS_SECT_ENTRY32(flash_read_time_us) /* time spent in flash_read() */
STATS_SECT_ENTRY32(flash_write_calls) /* calls to flash_write() */
STATS_SECT_ENTRY32(flash_write_time_us) /* time spent in flash_write() */
STATS_SECT_ENTRY32(flash_erase_calls) /* calls to flash_erase() */
STATS_SECT_ENTRY32(flash_erase_time_us) /* time spent in flash_erase() */
/* -- per-unit statistics -- */
/* erase cycle count for unit */
UTIL_EVAL(UTIL_REPEAT(CONFIG_FLASH_SIMULATOR_FLASH_SIZE, STATS_SECT_EC))
/* number of read operations on worn out erase units */
UTIL_EVAL(UTIL_REPEAT(CONFIG_FLASH_SIMULATOR_FLASH_SIZE, STATS_SECT_DIRTYR))
STATS_SECT_END;
STATS_SECT_DECL(flash_sim_stats) flash_sim_stats;
STATS_NAME_START(flash_sim_stats)
STATS_NAME(flash_sim_stats, bytes_read)
STATS_NAME(flash_sim_stats, bytes_written)
STATS_NAME(flash_sim_stats, double_writes)
STATS_NAME(flash_sim_stats, flash_read_calls)
STATS_NAME(flash_sim_stats, flash_read_time_us)
STATS_NAME(flash_sim_stats, flash_write_calls)
STATS_NAME(flash_sim_stats, flash_write_time_us)
STATS_NAME(flash_sim_stats, flash_erase_calls)
STATS_NAME(flash_sim_stats, flash_erase_time_us)
UTIL_EVAL(UTIL_REPEAT(CONFIG_FLASH_SIMULATOR_FLASH_SIZE, STATS_NAME_EC))
UTIL_EVAL(UTIL_REPEAT(CONFIG_FLASH_SIMULATOR_FLASH_SIZE, STATS_NAME_DIRTYR))
STATS_NAME_END(flash_sim_stats);
static u8_t mock_flash[FLASH_SIZE];
static bool write_protection;
static const struct flash_driver_api flash_sim_api;
static int flash_range_is_valid(struct device *dev, off_t offset, size_t len)
{
ARG_UNUSED(dev);
if ((offset + len > FLASH_SIZE + CONFIG_FLASH_SIMULATOR_BASE_OFFSET) ||
(offset < CONFIG_FLASH_SIMULATOR_BASE_OFFSET)) {
return 0;
}
return 1;
}
static int flash_wp_set(struct device *dev, bool enable)
{
ARG_UNUSED(dev);
write_protection = enable;
return 0;
}
static bool flash_wp_is_set(void)
{
return write_protection;
}
static int flash_sim_read(struct device *dev, const off_t offset, void *data,
const size_t len)
{
ARG_UNUSED(dev);
if (!flash_range_is_valid(dev, offset, len)) {
return -EINVAL;
}
if ((offset % CONFIG_FLASH_SIMULATOR_PROG_UNIT) ||
(len % CONFIG_FLASH_SIMULATOR_PROG_UNIT)) {
return -EINVAL;
}
STATS_INC(flash_sim_stats, flash_read_calls);
memcpy(data, FLASH(offset), len);
STATS_INCN(flash_sim_stats, bytes_read, len);
#ifdef CONFIG_FLASH_SIMULATOR_SIMULATE_TIMING
k_busy_wait(CONFIG_FLASH_SIMULATOR_MIN_READ_TIME_US);
STATS_INCN(flash_sim_stats, flash_read_time_us,
CONFIG_FLASH_SIMULATOR_MIN_READ_TIME_US);
#endif
return 0;
}
static int flash_sim_write(struct device *dev, const off_t offset,
const void *data, const size_t len)
{
ARG_UNUSED(dev);
if (!flash_range_is_valid(dev, offset, len)) {
return -EINVAL;
}
if ((offset % CONFIG_FLASH_SIMULATOR_PROG_UNIT) ||
(len % CONFIG_FLASH_SIMULATOR_PROG_UNIT)) {
return -EINVAL;
}
if (flash_wp_is_set()) {
return -EACCES;
}
STATS_INC(flash_sim_stats, flash_write_calls);
/* check if any unit has been already programmed */
for (u32_t i = 0; i < len; i += CONFIG_FLASH_SIMULATOR_PROG_UNIT) {
u8_t buf[CONFIG_FLASH_SIMULATOR_PROG_UNIT];
memset(buf, 0xFF, sizeof(buf));
if (memcmp(buf, FLASH(offset + i), sizeof(buf))) {
STATS_INC(flash_sim_stats, double_writes);
#if !CONFIG_FLASH_SIMULATOR_DOUBLE_WRITES
return -EIO;
#endif
}
}
for (u32_t i = 0; i < len; i++) {
/* only pull bits to zero */
*(FLASH(offset + i)) &= *((u8_t *)data + i);
}
STATS_INCN(flash_sim_stats, bytes_written, len);
#ifdef CONFIG_FLASH_SIMULATOR_SIMULATE_TIMING
/* wait before returning */
k_busy_wait(CONFIG_FLASH_SIMULATOR_MIN_WRITE_TIME_US);
STATS_INCN(flash_sim_stats, flash_write_time_us,
CONFIG_FLASH_SIMULATOR_MIN_WRITE_TIME_US);
#endif
return 0;
}
static void unit_erase(const u32_t unit)
{
const off_t unit_addr = CONFIG_FLASH_SIMULATOR_BASE_OFFSET +
(unit * CONFIG_FLASH_SIMULATOR_ERASE_UNIT);
/* byte pattern to fill the flash with */
u8_t byte_pattern = 0xFF;
/* erase the memory unit by pulling all bits to one */
memset(FLASH(unit_addr), byte_pattern,
CONFIG_FLASH_SIMULATOR_ERASE_UNIT);
}
static int flash_sim_erase(struct device *dev, const off_t offset,
const size_t len)
{
ARG_UNUSED(dev);
if (!flash_range_is_valid(dev, offset, len)) {
return -EINVAL;
}
#ifdef CONFIG_FLASH_SIMULATOR_ERASE_PROTECT
if (flash_wp_is_set()) {
return -EACCES;
}
#endif
/* erase operation must be aligned to the erase unit boundary */
if ((offset % CONFIG_FLASH_SIMULATOR_ERASE_UNIT) ||
(len % CONFIG_FLASH_SIMULATOR_ERASE_UNIT)) {
return -EINVAL;
}
STATS_INC(flash_sim_stats, flash_erase_calls);
/* the first unit to be erased */
u32_t unit_start = (offset - CONFIG_FLASH_SIMULATOR_BASE_OFFSET) /
CONFIG_FLASH_SIMULATOR_ERASE_UNIT;
/* erase as many units as necessary and increase their erase counter */
for (u32_t i = 0; i < len / CONFIG_FLASH_SIMULATOR_ERASE_UNIT; i++) {
ERASE_CYCLES_INC(unit_start + i);
unit_erase(unit_start + i);
}
#ifdef CONFIG_FLASH_SIMULATOR_SIMULATE_TIMING
/* wait before returning */
k_busy_wait(CONFIG_FLASH_SIMULATOR_MIN_ERASE_TIME_US);
STATS_INCN(flash_sim_stats, flash_erase_time_us,
CONFIG_FLASH_SIMULATOR_MIN_ERASE_TIME_US);
#endif
return 0;
}
#ifdef CONFIG_FLASH_PAGE_LAYOUT
static const struct flash_pages_layout flash_sim_pages_layout = {
.pages_count = CONFIG_FLASH_SIMULATOR_FLASH_SIZE,
.pages_size = CONFIG_FLASH_SIMULATOR_ERASE_UNIT,
};
static void flash_sim_page_layout(struct device *dev,
const struct flash_pages_layout **layout,
size_t *layout_size)
{
*layout = &flash_sim_pages_layout;
*layout_size = 1;
}
#endif
static const struct flash_driver_api flash_sim_api = {
.read = flash_sim_read,
.write = flash_sim_write,
.erase = flash_sim_erase,
.write_protection = flash_wp_set,
.write_block_size = CONFIG_FLASH_SIMULATOR_PROG_UNIT,
#ifdef CONFIG_FLASH_PAGE_LAYOUT
.page_layout = flash_sim_page_layout,
#endif
};
static int flash_init(struct device *dev)
{
STATS_INIT_AND_REG(flash_sim_stats, STATS_SIZE_32, "flash_sim_stats");
memset(mock_flash, 0xFF, ARRAY_SIZE(mock_flash));
return 0;
}
DEVICE_AND_API_INIT(flash_simulator, "FLASH_SIMULATOR", flash_init, NULL, NULL,
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
&flash_sim_api);

View file

@ -0,0 +1,22 @@
# SPDX-License-Identifier: Apache-2.0
---
title: simulated flash
version: 0.1
description: >
This binding gives a base representation of a simulated flash memory
properties:
compatible:
type: string
category: required
description: compatible strings
constraint: "sim-flash"
label:
type: string
category: required
description: Human readable string describing the device (used by Zephyr for API name)
generation: define
...

17
dts/common/sim-flash.dtsi Normal file
View file

@ -0,0 +1,17 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA.
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Basic Device Tree file for simulated flash entry declaration.
*/
/ {
sim_flash: sim-flash {
compatible = "sim-flash";
label = "FLASH_SIMULATOR";
};
};