Skip to content

Commit

Permalink
ftmctrl-flash: use libcache
Browse files Browse the repository at this point in the history
JIRA: RTOS-883
  • Loading branch information
lukileczo committed Jan 21, 2025
1 parent 512777b commit 0a22767
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 20 deletions.
2 changes: 1 addition & 1 deletion storage/ftmctrl-flash/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ include $(static-lib.mk)
NAME := ftmctrl-flash
LOCAL_SRCS := flashsrv.c
DEP_LIBS := libflashdrv-ftmctrl
LIBS := libjffs2 libstorage libmtd libptable
LIBS := libjffs2 libstorage libmtd libptable libcache

include $(binary.mk)
4 changes: 2 additions & 2 deletions storage/ftmctrl-flash/cfi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include <sys/types.h>

/* Timeouts in us */
#define CFI_TIMEOUT_MAX_PROGRAM(typical, maximum) ((1u << typical) * (1u << maximum))
#define CFI_TIMEOUT_MAX_ERASE(typical, maximum) ((1u << typical) * (1u << maximum) * 1024u)
#define CFI_TIMEOUT_MAX_PROGRAM(typical, maximum) ((1u << typical) * (1u << maximum) * 2)
#define CFI_TIMEOUT_MAX_ERASE(typical, maximum) ((1u << typical) * (1u << maximum) * 1024u * 2)

#define CFI_SIZE(size) (1u << ((uint32_t)size))

Expand Down
126 changes: 109 additions & 17 deletions storage/ftmctrl-flash/flashdrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "ftmctrl.h"


#define LIBCACHE_LINECNT 1024
#define LIBCACHE_POLICY LIBCACHE_WRITE_THROUGH


/* Helper functions */


Expand All @@ -40,13 +44,8 @@ static int fdrv_isValidAddress(size_t memsz, off_t offs, size_t len)
/* MTD interface */


static int flashdrv_mtdRead(storage_t *strg, off_t offs, void *buff, size_t len, size_t *retlen)
static int _flashdrv_mtdRead(storage_t *strg, off_t offs, void *buff, size_t len, size_t *retlen)
{
if ((strg == NULL) || (strg->dev == NULL) || (strg->dev->ctx == NULL)) {
*retlen = 0;
return -EINVAL;
}

struct _storage_devCtx_t *ctx = strg->dev->ctx;
if (fdrv_isValidAddress(CFI_SIZE(ctx->cfi.chipSz), offs, len) == 0) {
*retlen = 0;
Expand All @@ -58,27 +57,51 @@ static int flashdrv_mtdRead(storage_t *strg, off_t offs, void *buff, size_t len,
return EOK;
}

mutexLock(ctx->lock);

ftmctrl_WrEn(ctx->ftmctrl);
flash_read(ctx, offs, buff, len);
ftmctrl_WrDis(ctx->ftmctrl);

mutexUnlock(ctx->lock);

*retlen = len;

return EOK;
}


static int flashdrv_mtdWrite(storage_t *strg, off_t offs, const void *buff, size_t len, size_t *retlen)
static ssize_t _flashdrv_mtdReadCb(uint64_t offs, void *buff, size_t len, cache_devCtx_t *ctx)
{
if ((strg == NULL) || (strg->dev == NULL) || (strg->dev->ctx == NULL)) {
storage_t *strg = ctx->strg;
size_t retlen;
ssize_t ret;

ret = _flashdrv_mtdRead(strg, offs, buff, len, &retlen);
if (ret < 0) {
return ret;
}

return (ssize_t)retlen;
}


static int flashdrv_mtdRead(storage_t *strg, off_t offs, void *buff, size_t len, size_t *retlen)
{
mutexLock(strg->dev->ctx->lock);
int ret = cache_read(strg->dev->ctx->cache, offs, buff, len);
mutexUnlock(strg->dev->ctx->lock);

if (ret < 0) {
*retlen = 0;
return -EINVAL;
}
else {
*retlen = len;
ret = 0;
}

return ret;
}


static int _flashdrv_mtdWrite(storage_t *strg, off_t offs, const void *buff, size_t len, size_t *retlen)
{
struct _storage_devCtx_t *ctx = strg->dev->ctx;
if (fdrv_isValidAddress(CFI_SIZE(ctx->cfi.chipSz), offs, len) == 0) {
*retlen = 0;
Expand All @@ -93,8 +116,6 @@ static int flashdrv_mtdWrite(storage_t *strg, off_t offs, const void *buff, size
const uint8_t *src = buff;
size_t doneBytes = 0;

mutexLock(ctx->lock);

int res = EOK;
const size_t writeBuffsz = strg->dev->mtd->writeBuffsz;

Expand All @@ -114,13 +135,49 @@ static int flashdrv_mtdWrite(storage_t *strg, off_t offs, const void *buff, size
offs += chunk;
}

mutexUnlock(ctx->lock);
*retlen = doneBytes;

return res;
}


static ssize_t _flashdrv_mtdWriteCb(uint64_t offs, const void *buff, size_t len, cache_devCtx_t *ctx)
{
storage_t *strg = ctx->strg;
size_t retlen;
ssize_t ret;

if ((offs % strg->dev->mtd->writesz) != 0 || (len % strg->dev->mtd->writesz) != 0) {
return -EINVAL;
}

ret = _flashdrv_mtdWrite(strg, offs, buff, len, &retlen);
if (ret < 0) {
return ret;
}

return (ssize_t)retlen;
}


static int flashdrv_mtdWrite(storage_t *strg, off_t offs, const void *buff, size_t len, size_t *retlen)
{
mutexLock(strg->dev->ctx->lock);
int ret = cache_write(strg->dev->ctx->cache, offs, buff, len, LIBCACHE_POLICY);
mutexUnlock(strg->dev->ctx->lock);

if (ret < 0) {
*retlen = 0;
}
else {
*retlen = len;
ret = 0;
}

return ret;
}


static int flashdrv_mtdErase(storage_t *strg, off_t offs, size_t len)
{
if ((strg == NULL) || (strg->dev == NULL) || (strg->dev->ctx == NULL)) {
Expand Down Expand Up @@ -163,12 +220,27 @@ static int flashdrv_mtdErase(storage_t *strg, off_t offs, size_t len)
}

ftmctrl_WrDis(ctx->ftmctrl);
res = cache_invalidate(ctx->cache, offs, end);
mutexUnlock(ctx->lock);

return res;
}


static void flashdrv_mtdSync(storage_t *strg)
{
if ((strg == NULL) || (strg->dev == NULL) || (strg->dev->ctx == NULL)) {
return;
}

struct _storage_devCtx_t *ctx = strg->dev->ctx;

mutexLock(ctx->lock);
cache_flush(ctx->cache, 0, CFI_SIZE(ctx->cfi.chipSz));
mutexUnlock(ctx->lock);
}


static const storage_mtdops_t mtdOps = {
.erase = flashdrv_mtdErase,
.unPoint = NULL,
Expand All @@ -179,7 +251,7 @@ static const storage_mtdops_t mtdOps = {
.meta_read = NULL,
.meta_write = NULL,

.sync = NULL,
.sync = flashdrv_mtdSync,
.lock = NULL,
.unLock = NULL,
.isLocked = NULL,
Expand Down Expand Up @@ -234,8 +306,28 @@ struct _storage_devCtx_t *flashdrv_contextInit(void)
}


int flashdrv_cacheInit(storage_t *strg)
{
cache_ops_t cacheOps = {
.readCb = _flashdrv_mtdReadCb,
.writeCb = _flashdrv_mtdWriteCb,
.ctx = &strg->dev->ctx->cacheCtx
};
strg->dev->ctx->cache = cache_init(strg->size, strg->dev->mtd->writeBuffsz, LIBCACHE_LINECNT, &cacheOps);
if (strg->dev->ctx->cache == NULL) {
return -ENOMEM;
}
strg->dev->ctx->cacheCtx.strg = strg;

return 0;
}


void flashdrv_contextDestroy(struct _storage_devCtx_t *ctx)
{
if (ctx->cache != NULL) {
cache_deinit(ctx->cache);
}
(void)resourceDestroy(ctx->lock);
(void)munmap(ctx->ftmctrl, _PAGE_SIZE);
free(ctx);
Expand Down
12 changes: 12 additions & 0 deletions storage/ftmctrl-flash/flashdrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "cfi.h"

#include <cache.h>
#include <stdio.h>
#include <sys/types.h>
#include <storage/storage.h>
Expand All @@ -27,13 +28,21 @@
/* clang-format on */


struct cache_devCtx_s {
struct _storage_t *strg;
};


struct _storage_devCtx_t {
cfi_info_t cfi;
const struct _flash_dev_t *dev;

handle_t lock;
void *ftmctrl;
size_t sectorsz;

cachectx_t *cache;
cache_devCtx_t cacheCtx;
};


Expand All @@ -43,6 +52,9 @@ const storage_mtdops_t *flashdrv_getMtdOps(void);
struct _storage_devCtx_t *flashdrv_contextInit(void);


int flashdrv_cacheInit(storage_t *strg);


void flashdrv_contextDestroy(struct _storage_devCtx_t *ctx);


Expand Down
8 changes: 8 additions & 0 deletions storage/ftmctrl-flash/flashsrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ static storage_t *flashsrv_init(void)
return NULL;
}

if (flashdrv_cacheInit(strg) < 0) {
free(strg->dev->mtd);
free(strg->dev);
flashdrv_contextDestroy(ctx);
free(strg);
return NULL;
}

oid_t oid;
res = storage_add(strg, &oid);
if (res < 0) {
Expand Down

0 comments on commit 0a22767

Please sign in to comment.