2019-11-08 18:27:32 +08:00
|
|
|
/*
|
2019-11-10 19:09:44 +08:00
|
|
|
* Copyright (c) 2019 Laczen
|
2019-11-08 18:27:32 +08:00
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-26 01:09:22 +08:00
|
|
|
#define DT_DRV_COMPAT zephyr_sim_eeprom
|
|
|
|
|
2023-12-28 18:45:30 +08:00
|
|
|
#ifdef CONFIG_ARCH_POSIX
|
2024-09-27 16:13:30 +08:00
|
|
|
#include "eeprom_simulator_native.h"
|
2023-12-28 18:45:30 +08:00
|
|
|
#include "cmdline.h"
|
|
|
|
#include "soc.h"
|
2024-09-27 16:13:30 +08:00
|
|
|
#endif /* CONFIG_ARCH_POSIX */
|
2023-12-28 18:45:30 +08:00
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/drivers/eeprom.h>
|
2019-11-10 19:09:44 +08:00
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/stats/stats.h>
|
2019-11-08 18:27:32 +08:00
|
|
|
#include <string.h>
|
2019-11-10 19:09:44 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2019-11-08 18:27:32 +08:00
|
|
|
#define LOG_LEVEL CONFIG_EEPROM_LOG_LEVEL
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2019-11-08 18:27:32 +08:00
|
|
|
LOG_MODULE_REGISTER(eeprom_simulator);
|
|
|
|
|
|
|
|
struct eeprom_sim_config {
|
|
|
|
size_t size;
|
|
|
|
bool readonly;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define EEPROM(addr) (mock_eeprom + (addr))
|
|
|
|
|
|
|
|
#if defined(CONFIG_MULTITHREADING)
|
|
|
|
/* semaphore for locking flash resources (tickers) */
|
|
|
|
static struct k_sem sem_lock;
|
|
|
|
#define SYNC_INIT() k_sem_init(&sem_lock, 1, 1)
|
|
|
|
#define SYNC_LOCK() k_sem_take(&sem_lock, K_FOREVER)
|
|
|
|
#define SYNC_UNLOCK() k_sem_give(&sem_lock)
|
|
|
|
#else
|
|
|
|
#define SYNC_INIT()
|
|
|
|
#define SYNC_LOCK()
|
|
|
|
#define SYNC_UNLOCK()
|
|
|
|
#endif
|
|
|
|
|
2022-02-24 20:00:55 +08:00
|
|
|
/* simulator statistics */
|
2019-11-08 18:27:32 +08:00
|
|
|
STATS_SECT_START(eeprom_sim_stats)
|
|
|
|
STATS_SECT_ENTRY32(bytes_read) /* total bytes read */
|
|
|
|
STATS_SECT_ENTRY32(bytes_written) /* total bytes written */
|
|
|
|
STATS_SECT_ENTRY32(eeprom_read_calls) /* calls to eeprom_read() */
|
|
|
|
STATS_SECT_ENTRY32(eeprom_read_time_us) /* time spent in eeprom_read() */
|
|
|
|
STATS_SECT_ENTRY32(eeprom_write_calls) /* calls to eeprom_write() */
|
|
|
|
STATS_SECT_ENTRY32(eeprom_write_time_us)/* time spent in eeprom_write() */
|
|
|
|
STATS_SECT_END;
|
|
|
|
|
|
|
|
STATS_SECT_DECL(eeprom_sim_stats) eeprom_sim_stats;
|
|
|
|
STATS_NAME_START(eeprom_sim_stats)
|
|
|
|
STATS_NAME(eeprom_sim_stats, bytes_read)
|
|
|
|
STATS_NAME(eeprom_sim_stats, bytes_written)
|
|
|
|
STATS_NAME(eeprom_sim_stats, eeprom_read_calls)
|
|
|
|
STATS_NAME(eeprom_sim_stats, eeprom_read_time_us)
|
|
|
|
STATS_NAME(eeprom_sim_stats, eeprom_write_calls)
|
|
|
|
STATS_NAME(eeprom_sim_stats, eeprom_write_time_us)
|
|
|
|
STATS_NAME_END(eeprom_sim_stats);
|
|
|
|
|
|
|
|
/* simulator dynamic thresholds */
|
|
|
|
STATS_SECT_START(eeprom_sim_thresholds)
|
|
|
|
STATS_SECT_ENTRY32(max_write_calls)
|
|
|
|
STATS_SECT_ENTRY32(max_len)
|
|
|
|
STATS_SECT_END;
|
|
|
|
|
|
|
|
STATS_SECT_DECL(eeprom_sim_thresholds) eeprom_sim_thresholds;
|
|
|
|
STATS_NAME_START(eeprom_sim_thresholds)
|
|
|
|
STATS_NAME(eeprom_sim_thresholds, max_write_calls)
|
|
|
|
STATS_NAME(eeprom_sim_thresholds, max_len)
|
|
|
|
STATS_NAME_END(eeprom_sim_thresholds);
|
|
|
|
|
2019-11-10 19:09:44 +08:00
|
|
|
#ifdef CONFIG_ARCH_POSIX
|
2024-09-27 16:13:30 +08:00
|
|
|
static char *mock_eeprom;
|
2019-11-10 19:09:44 +08:00
|
|
|
static int eeprom_fd = -1;
|
|
|
|
static const char *eeprom_file_path;
|
2024-09-27 16:13:30 +08:00
|
|
|
#define DEFAULT_EEPROM_FILE_PATH "eeprom.bin"
|
|
|
|
static bool eeprom_erase_at_start;
|
|
|
|
static bool eeprom_rm_at_exit;
|
|
|
|
static bool eeprom_in_ram;
|
2019-11-10 19:09:44 +08:00
|
|
|
#else
|
2020-05-28 00:26:57 +08:00
|
|
|
static uint8_t mock_eeprom[DT_INST_PROP(0, size)];
|
2019-11-10 19:09:44 +08:00
|
|
|
#endif /* CONFIG_ARCH_POSIX */
|
2019-11-08 18:27:32 +08:00
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int eeprom_range_is_valid(const struct device *dev, off_t offset,
|
|
|
|
size_t len)
|
2019-11-08 18:27:32 +08:00
|
|
|
{
|
2022-01-18 22:34:34 +08:00
|
|
|
const struct eeprom_sim_config *config = dev->config;
|
2019-11-08 18:27:32 +08:00
|
|
|
|
2021-02-02 16:30:18 +08:00
|
|
|
if ((offset + len) <= config->size) {
|
2019-11-08 18:27:32 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int eeprom_sim_read(const struct device *dev, off_t offset, void *data,
|
2019-11-10 19:09:44 +08:00
|
|
|
size_t len)
|
2019-11-08 18:27:32 +08:00
|
|
|
{
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eeprom_range_is_valid(dev, offset, len)) {
|
|
|
|
LOG_WRN("attempt to read past device boundary");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYNC_LOCK();
|
|
|
|
|
|
|
|
STATS_INC(eeprom_sim_stats, eeprom_read_calls);
|
|
|
|
memcpy(data, EEPROM(offset), len);
|
|
|
|
STATS_INCN(eeprom_sim_stats, bytes_read, len);
|
|
|
|
|
|
|
|
SYNC_UNLOCK();
|
|
|
|
|
|
|
|
#ifdef CONFIG_EEPROM_SIMULATOR_SIMULATE_TIMING
|
|
|
|
k_busy_wait(CONFIG_EEPROM_SIMULATOR_MIN_READ_TIME_US);
|
|
|
|
STATS_INCN(eeprom_sim_stats, eeprom_read_time_us,
|
|
|
|
CONFIG_EEPROM_SIMULATOR_MIN_READ_TIME_US);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int eeprom_sim_write(const struct device *dev, off_t offset,
|
|
|
|
const void *data,
|
2019-11-10 19:09:44 +08:00
|
|
|
size_t len)
|
2019-11-08 18:27:32 +08:00
|
|
|
{
|
2022-01-18 22:34:34 +08:00
|
|
|
const struct eeprom_sim_config *config = dev->config;
|
2019-11-08 18:27:32 +08:00
|
|
|
|
|
|
|
if (config->readonly) {
|
|
|
|
LOG_WRN("attempt to write to read-only device");
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eeprom_range_is_valid(dev, offset, len)) {
|
|
|
|
LOG_WRN("attempt to write past device boundary");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYNC_LOCK();
|
|
|
|
|
|
|
|
STATS_INC(eeprom_sim_stats, eeprom_write_calls);
|
|
|
|
|
|
|
|
bool data_part_ignored = false;
|
|
|
|
|
|
|
|
if (eeprom_sim_thresholds.max_write_calls != 0) {
|
|
|
|
if (eeprom_sim_stats.eeprom_write_calls >
|
|
|
|
eeprom_sim_thresholds.max_write_calls) {
|
2019-11-10 19:09:44 +08:00
|
|
|
goto end;
|
2019-11-08 18:27:32 +08:00
|
|
|
} else if (eeprom_sim_stats.eeprom_write_calls ==
|
|
|
|
eeprom_sim_thresholds.max_write_calls) {
|
|
|
|
if (eeprom_sim_thresholds.max_len == 0) {
|
2019-11-10 19:09:44 +08:00
|
|
|
goto end;
|
2019-11-08 18:27:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
data_part_ignored = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 19:09:44 +08:00
|
|
|
if ((data_part_ignored) && (len > eeprom_sim_thresholds.max_len)) {
|
|
|
|
len = eeprom_sim_thresholds.max_len;
|
2019-11-08 18:27:32 +08:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:09:44 +08:00
|
|
|
memcpy(EEPROM(offset), data, len);
|
|
|
|
|
2019-11-08 18:27:32 +08:00
|
|
|
STATS_INCN(eeprom_sim_stats, bytes_written, len);
|
|
|
|
|
|
|
|
#ifdef CONFIG_EEPROM_SIMULATOR_SIMULATE_TIMING
|
|
|
|
/* wait before returning */
|
|
|
|
k_busy_wait(CONFIG_EEPROM_SIMULATOR_MIN_WRITE_TIME_US);
|
|
|
|
STATS_INCN(eeprom_sim_stats, eeprom_write_time_us,
|
|
|
|
CONFIG_EEPROM_SIMULATOR_MIN_WRITE_TIME_US);
|
|
|
|
#endif
|
|
|
|
|
2019-11-10 19:09:44 +08:00
|
|
|
end:
|
2019-11-08 18:27:32 +08:00
|
|
|
SYNC_UNLOCK();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static size_t eeprom_sim_size(const struct device *dev)
|
2019-11-08 18:27:32 +08:00
|
|
|
{
|
2022-01-18 22:34:34 +08:00
|
|
|
const struct eeprom_sim_config *config = dev->config;
|
2019-11-08 18:27:32 +08:00
|
|
|
|
|
|
|
return config->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct eeprom_driver_api eeprom_sim_api = {
|
|
|
|
.read = eeprom_sim_read,
|
|
|
|
.write = eeprom_sim_write,
|
|
|
|
.size = eeprom_sim_size,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct eeprom_sim_config eeprom_sim_config_0 = {
|
2020-03-26 01:09:22 +08:00
|
|
|
.size = DT_INST_PROP(0, size),
|
|
|
|
.readonly = DT_INST_PROP(0, read_only),
|
2019-11-08 18:27:32 +08:00
|
|
|
};
|
|
|
|
|
2019-11-10 19:09:44 +08:00
|
|
|
#ifdef CONFIG_ARCH_POSIX
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int eeprom_mock_init(const struct device *dev)
|
2019-11-10 19:09:44 +08:00
|
|
|
{
|
2024-09-27 16:13:30 +08:00
|
|
|
int rc;
|
2019-11-10 19:09:44 +08:00
|
|
|
|
2024-09-27 16:13:30 +08:00
|
|
|
ARG_UNUSED(dev);
|
2019-11-10 19:09:44 +08:00
|
|
|
|
2024-09-27 16:13:30 +08:00
|
|
|
if (eeprom_in_ram == false && eeprom_file_path == NULL) {
|
|
|
|
eeprom_file_path = DEFAULT_EEPROM_FILE_PATH;
|
2019-11-10 19:09:44 +08:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:13:30 +08:00
|
|
|
rc = eeprom_mock_init_native(eeprom_in_ram, &mock_eeprom, DT_INST_PROP(0, size), &eeprom_fd,
|
|
|
|
eeprom_file_path, 0xFF, eeprom_erase_at_start);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
2019-11-10 19:09:44 +08:00
|
|
|
return -EIO;
|
2024-09-27 16:13:30 +08:00
|
|
|
} else {
|
|
|
|
return 0;
|
2019-11-10 19:09:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int eeprom_mock_init(const struct device *dev)
|
2019-11-10 19:09:44 +08:00
|
|
|
{
|
|
|
|
memset(mock_eeprom, 0xFF, ARRAY_SIZE(mock_eeprom));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_ARCH_POSIX */
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int eeprom_sim_init(const struct device *dev)
|
2019-11-08 18:27:32 +08:00
|
|
|
{
|
|
|
|
SYNC_INIT();
|
|
|
|
STATS_INIT_AND_REG(eeprom_sim_stats, STATS_SIZE_32, "eeprom_sim_stats");
|
|
|
|
STATS_INIT_AND_REG(eeprom_sim_thresholds, STATS_SIZE_32,
|
|
|
|
"eeprom_sim_thresholds");
|
|
|
|
|
2019-11-10 19:09:44 +08:00
|
|
|
return eeprom_mock_init(dev);
|
2019-11-08 18:27:32 +08:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:13:30 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(0, &eeprom_sim_init, NULL, NULL, &eeprom_sim_config_0, POST_KERNEL,
|
|
|
|
CONFIG_EEPROM_INIT_PRIORITY, &eeprom_sim_api);
|
2019-11-10 19:09:44 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_POSIX
|
|
|
|
|
2024-08-13 23:28:56 +08:00
|
|
|
static void eeprom_native_cleanup(void)
|
2019-11-10 19:09:44 +08:00
|
|
|
{
|
2024-09-27 16:13:30 +08:00
|
|
|
eeprom_mock_cleanup_native(eeprom_in_ram, eeprom_fd, mock_eeprom, DT_INST_PROP(0, size),
|
|
|
|
eeprom_file_path, eeprom_rm_at_exit);
|
2019-11-10 19:09:44 +08:00
|
|
|
}
|
|
|
|
|
2024-08-13 23:28:56 +08:00
|
|
|
static void eeprom_native_options(void)
|
2019-11-10 19:09:44 +08:00
|
|
|
{
|
|
|
|
static struct args_struct_t eeprom_options[] = {
|
2024-09-27 16:13:30 +08:00
|
|
|
{.option = "eeprom",
|
|
|
|
.name = "path",
|
|
|
|
.type = 's',
|
|
|
|
.dest = (void *)&eeprom_file_path,
|
|
|
|
.descript = "Path to binary file to be used as EEPROM, by default "
|
|
|
|
"\"" DEFAULT_EEPROM_FILE_PATH "\""},
|
|
|
|
{.is_switch = true,
|
|
|
|
.option = "eeprom_erase",
|
|
|
|
.type = 'b',
|
|
|
|
.dest = (void *)&eeprom_erase_at_start,
|
|
|
|
.descript = "Erase the EEPROM content at startup"},
|
|
|
|
{.is_switch = true,
|
|
|
|
.option = "eeprom_rm",
|
|
|
|
.type = 'b',
|
|
|
|
.dest = (void *)&eeprom_rm_at_exit,
|
|
|
|
.descript = "Remove the EEPROM file when terminating the execution"},
|
|
|
|
{.is_switch = true,
|
|
|
|
.option = "eeprom_in_ram",
|
|
|
|
.type = 'b',
|
|
|
|
.dest = (void *)&eeprom_in_ram,
|
|
|
|
.descript = "Instead of a file, keep the file content just in RAM. If this is "
|
|
|
|
"set, eeprom, eeprom_erase & eeprom_rm are ignored, and the EEPROM "
|
|
|
|
"content is always erased at startup"},
|
2019-11-10 19:09:44 +08:00
|
|
|
ARG_TABLE_ENDMARKER
|
|
|
|
};
|
|
|
|
|
|
|
|
native_add_command_line_opts(eeprom_options);
|
|
|
|
}
|
|
|
|
|
2024-08-13 23:28:56 +08:00
|
|
|
NATIVE_TASK(eeprom_native_options, PRE_BOOT_1, 1);
|
|
|
|
NATIVE_TASK(eeprom_native_cleanup, ON_EXIT, 1);
|
2019-11-10 19:09:44 +08:00
|
|
|
|
|
|
|
#endif /* CONFIG_ARCH_POSIX */
|