drivers/mtd: add rpmsg mtd support

Signed-off-by: wangbowen6 <wangbowen6@xiaomi.com>
This commit is contained in:
wangbowen6 2022-09-05 09:46:44 +08:00 committed by Xiang Xiao
parent 25c2f3e042
commit c754df6411
10 changed files with 1736 additions and 22 deletions

View File

@ -16,9 +16,12 @@ CONFIG_BUILTIN=y
CONFIG_DEBUG_ASSERTIONS=y
CONFIG_DEBUG_ERROR=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_FS_ERROR=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DEV_RPMSG=y
CONFIG_DEV_SIMPLE_ADDRENV=y
CONFIG_EXAMPLES_HELLO=y
CONFIG_EXAMPLES_RPMSGSOCKET=y
CONFIG_FS_PROCFS=y
CONFIG_FS_RPMSGFS=y
@ -26,6 +29,8 @@ CONFIG_IDLETHREAD_STACKSIZE=4096
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INPUT=y
CONFIG_LIBC_HOSTNAME="proxy"
CONFIG_MTD=y
CONFIG_MTD_BYTE_WRITE=y
CONFIG_NDEBUG=y
CONFIG_NET=y
CONFIG_NETDB_DNSCLIENT=y
@ -48,6 +53,7 @@ CONFIG_NSH_READLINE=y
CONFIG_OPENAMP=y
CONFIG_READLINE_CMD_HISTORY=y
CONFIG_READLINE_TABCOMPLETION=y
CONFIG_RPMSGMTD=y
CONFIG_RPMSG_UART=y
CONFIG_RPTUN=y
CONFIG_RTC=y

View File

@ -17,6 +17,8 @@ CONFIG_CLK_RPMSG=y
CONFIG_DEBUG_ASSERTIONS=y
CONFIG_DEBUG_ERROR=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_FS_ERROR=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DEV_RPMSG_SERVER=y
CONFIG_DEV_SIMPLE_ADDRENV=y
@ -30,6 +32,8 @@ CONFIG_INPUT=y
CONFIG_IOEXPANDER=y
CONFIG_IOEXPANDER_RPMSG=y
CONFIG_LIBC_HOSTNAME="server"
CONFIG_MTD=y
CONFIG_MTD_BYTE_WRITE=y
CONFIG_NDEBUG=y
CONFIG_NET=y
CONFIG_NETDB_DNSCLIENT=y
@ -60,10 +64,12 @@ CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_PROMPT_STRING="server> "
CONFIG_NSH_READLINE=y
CONFIG_OPENAMP=y
CONFIG_RAMMTD=y
CONFIG_READLINE_CMD_HISTORY=y
CONFIG_READLINE_TABCOMPLETION=y
CONFIG_REGULATOR=y
CONFIG_REGULATOR_RPMSG=y
CONFIG_RPMSGMTD_SERVER=y
CONFIG_RPMSG_UART=y
CONFIG_RPTUN=y
CONFIG_RTC=y

View File

@ -176,14 +176,6 @@ int sim_bringup(void)
syslog(LOG_ERR, "ERROR: IOCTL MTDIOC_BULKERASE failed\n");
}
#if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS)
/* Initialize a SMART Flash block device and bind it to the MTD
* device.
*/
smart_initialize(0, mtd, NULL);
#elif defined(CONFIG_FS_SPIFFS)
/* Register the MTD driver so that it can be accessed from the
* VFS.
*/
@ -195,6 +187,14 @@ int sim_bringup(void)
ret);
}
#if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS)
/* Initialize a SMART Flash block device and bind it to the MTD
* device.
*/
smart_initialize(0, mtd, NULL);
#elif defined(CONFIG_FS_SPIFFS)
/* Mount the SPIFFS file system */
ret = nx_mount("/dev/rammtd", "/mnt/spiffs", "spiffs", 0, NULL);
@ -206,17 +206,6 @@ int sim_bringup(void)
}
#elif defined(CONFIG_FS_LITTLEFS)
/* Register the MTD driver so that it can be accessed from the
* VFS.
*/
ret = register_mtddriver("/dev/rammtd", mtd, 0755, NULL);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: Failed to register MTD driver: %d\n",
ret);
}
/* Mount the LittleFS file system */
ret = nx_mount("/dev/rammtd", "/mnt/lfs", "littlefs", 0,
@ -452,6 +441,10 @@ int sim_bringup(void)
rpmsgdev_register("server", "/dev/console", "/dev/server-console");
rpmsgdev_register("server", "/dev/null", "/dev/server-null");
#endif
#ifdef CONFIG_RPMSGMTD
rpmsgmtd_register("server", "/dev/rammtd", NULL);
#endif
#endif
#ifdef CONFIG_SIM_WTGAHRS2_UARTN

View File

@ -28,6 +28,7 @@
#include <nuttx/drivers/rpmsgdev.h>
#include <nuttx/fs/loop.h>
#include <nuttx/input/uinput.h>
#include <nuttx/mtd/mtd.h>
#include <nuttx/net/loopback.h>
#include <nuttx/net/tun.h>
#include <nuttx/net/telnet.h>
@ -167,4 +168,8 @@ void drivers_initialize(void)
#ifdef CONFIG_DEV_RPMSG_SERVER
rpmsgdev_server_init();
#endif
#ifdef CONFIG_RPMSGMTD_SERVER
rpmsgmtd_server_init();
#endif
}

View File

@ -137,7 +137,7 @@ config MTD_PROGMEM
interfaces must be exported by chip-specific logic.
if MTD_PROGMEM
endif #MTD_PROGMEM
config MTD_CONFIG
@ -271,6 +271,16 @@ config MTD_NAND_EMBEDDEDECC
endif # MTD_NAND
config RPMSGMTD
bool "MTD RPMSG Client Enable"
default n
depends on RPTUN
config RPMSGMTD_SERVER
bool "MTD RPMSG Server Enable"
default n
depends on RPTUN
config RAMMTD
bool "RAM-based MTD driver"
default n
@ -719,11 +729,11 @@ config MX25RXX_SECTOR512
config MX25RXX_PAGE128
bool "128 byte size pages"
default n
config MX25RXX_LXX
bool "Run MX25RXX driver in MX25LXX mode"
default n
endif # MTD_MX25RXX
config MTD_SMART

View File

@ -68,6 +68,14 @@ ifeq ($(CONFIG_NULLMTD),y)
CSRCS += nullmtd.c
endif
ifeq ($(CONFIG_RPMSGMTD),y)
CSRCS += rpmsgmtd.c
endif
ifeq ($(CONFIG_RPMSGMTD_SERVER),y)
CSRCS += rpmsgmtd_server.c
endif
ifeq ($(CONFIG_MTD_AT24XX),y)
CSRCS += at24xx.c
endif

1118
drivers/mtd/rpmsgmtd.c Normal file

File diff suppressed because it is too large Load Diff

105
drivers/mtd/rpmsgmtd.h Normal file
View File

@ -0,0 +1,105 @@
/****************************************************************************
* drivers/mtd/rpmsgmtd.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __DRIVERS_MTD_RPMSGMTD_H
#define __DRIVERS_MTD_RPMSGMTD_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
#include <sys/types.h>
/****************************************************************************
* Pre-processor definitions
****************************************************************************/
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
#define RPMSGMTD_NAME_PREFIX "rpmsgmtd-"
#define RPMSGMTD_NAME_PREFIX_LEN 9
#define RPMSGMTD_ERASE 1
#define RPMSGMTD_BREAD 2
#define RPMSGMTD_BWRITE 3
#define RPMSGMTD_READ 4
#define RPMSGMTD_WRITE 5
#define RPMSGMTD_IOCTL 6
/****************************************************************************
* Public Types
****************************************************************************/
begin_packed_struct struct rpmsgmtd_header_s
{
uint32_t command;
int32_t result;
uint64_t cookie;
} end_packed_struct;
begin_packed_struct struct rpmsgmtd_erase_s
{
struct rpmsgmtd_header_s header;
int64_t startblock;
uint64_t nblocks;
} end_packed_struct;
begin_packed_struct struct rpmsgmtd_bread_s
{
struct rpmsgmtd_header_s header;
int64_t startblock;
uint64_t nblocks;
uint32_t blocksize;
uint8_t buf[1];
} end_packed_struct;
#define rpmsgmtd_bwrite_s rpmsgmtd_bread_s
begin_packed_struct struct rpmsgmtd_read_s
{
struct rpmsgmtd_header_s header;
int64_t offset;
uint64_t nbytes;
uint8_t buf[1];
} end_packed_struct;
#define rpmsgmtd_write_s rpmsgmtd_read_s
begin_packed_struct struct rpmsgmtd_ioctl_s
{
struct rpmsgmtd_header_s header;
int32_t request;
uint64_t arg;
uint32_t arglen;
uint8_t buf[1];
} end_packed_struct;
/****************************************************************************
* Internal function prototypes
****************************************************************************/
/****************************************************************************
* Internal data
****************************************************************************/
#endif /* __DRIVERS_MTD_RPMSGMTD_H */

View File

@ -0,0 +1,420 @@
/****************************************************************************
* drivers/mtd/rpmsgmtd_server.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/mtd/mtd.h>
#include <nuttx/rptun/openamp.h>
#include "rpmsgmtd.h"
/****************************************************************************
* Pre-processor definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
struct rpmsgmtd_server_s
{
struct rpmsg_endpoint ept;
FAR struct mtd_dev_s *dev;
FAR struct inode *mtdnode;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Functions handle the messages from the client cpu */
static int rpmsgmtd_erase_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgmtd_bread_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgmtd_bwrite_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgmtd_read_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgmtd_write_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgmtd_ioctl_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
/* Functions for creating communication with client cpu */
static bool rpmsgmtd_ns_match(FAR struct rpmsg_device *rdev,
FAR void *priv, FAR const char *name,
uint32_t dest);
static void rpmsgmtd_ns_bind(FAR struct rpmsg_device *rdev,
FAR void *priv, FAR const char *name,
uint32_t dest);
static void rpmsgmtd_ns_unbind(FAR struct rpmsg_endpoint *ept);
static int rpmsgmtd_ept_cb(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len, uint32_t src,
FAR void *priv);
/****************************************************************************
* Private Data
****************************************************************************/
static const rpmsg_ept_cb g_rpmsgmtd_handler[] =
{
[RPMSGMTD_ERASE] = rpmsgmtd_erase_handler,
[RPMSGMTD_BREAD] = rpmsgmtd_bread_handler,
[RPMSGMTD_BWRITE] = rpmsgmtd_bwrite_handler,
[RPMSGMTD_READ] = rpmsgmtd_read_handler,
[RPMSGMTD_WRITE] = rpmsgmtd_write_handler,
[RPMSGMTD_IOCTL] = rpmsgmtd_ioctl_handler,
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: rpmsgmtd_erase_handler
****************************************************************************/
static int rpmsgmtd_erase_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgmtd_server_s *server = ept->priv;
FAR struct rpmsgmtd_erase_s *msg = data;
msg->header.result = MTD_ERASE(server->dev, msg->startblock, msg->nblocks);
return rpmsg_send(ept, msg, sizeof(*msg));
}
/****************************************************************************
* Name: rpmsgmtd_bread_handler
****************************************************************************/
static int rpmsgmtd_bread_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgmtd_server_s *server = ept->priv;
FAR struct rpmsgmtd_bread_s *msg = data;
FAR struct rpmsgmtd_bread_s *rsp;
int ret = -ENOENT;
size_t read = 0;
size_t nblocks;
uint32_t space;
while (read < msg->nblocks)
{
rsp = rpmsg_get_tx_payload_buffer(ept, &space, true);
if (rsp == NULL)
{
ferr("get tx payload failed or no enough space\n");
return -ENOMEM;
}
DEBUGASSERT(space >= sizeof(*msg) - 1 + msg->blocksize);
*rsp = *msg;
nblocks = (space - sizeof(*msg) + 1) / msg->blocksize;
if (nblocks > msg->nblocks - read)
{
nblocks = msg->nblocks - read;
}
ret = MTD_BREAD(server->dev, msg->startblock + read, nblocks,
rsp->buf);
rsp->header.result = ret;
rpmsg_send_nocopy(ept, rsp, (ret < 0 ? 0 : ret * msg->blocksize) +
sizeof(*rsp) - 1);
if (ret <= 0)
{
ferr("mtd block read failed\n");
break;
}
read += ret;
}
return 0;
}
/****************************************************************************
* Name: rpmsgmtd_bwrite_handler
****************************************************************************/
static int rpmsgmtd_bwrite_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgmtd_server_s *server = ept->priv;
FAR struct rpmsgmtd_bwrite_s *msg = data;
int ret;
ret = MTD_BWRITE(server->dev, msg->startblock, msg->nblocks, msg->buf);
if (ret <= 0)
{
ferr("mtd block write failed\n");
}
/* cookie != 0 indicate the data has been sent complete, so send back
* the total written blocks.
*/
if (msg->header.cookie != 0)
{
msg->header.result = ret;
rpmsg_send(ept, msg, sizeof(*msg) - 1);
}
return 0;
}
/****************************************************************************
* Name: rpmsgmtd_read_handler
****************************************************************************/
static int rpmsgmtd_read_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgmtd_server_s *server = ept->priv;
FAR struct rpmsgmtd_read_s *msg = data;
FAR struct rpmsgmtd_read_s *rsp;
int ret = -ENOENT;
size_t read = 0;
uint32_t space;
while (read < msg->nbytes)
{
rsp = rpmsg_get_tx_payload_buffer(ept, &space, true);
if (rsp == NULL)
{
ferr("get tx payload failed\n");
return -ENOMEM;
}
*rsp = *msg;
space -= sizeof(*msg) - 1;
if (space > msg->nbytes - read)
{
space = msg->nbytes - read;
}
ret = MTD_READ(server->dev, msg->offset + read, space,
(FAR uint8_t *)rsp->buf);
rsp->header.result = ret;
rpmsg_send_nocopy(ept, rsp, (ret < 0 ? 0 : ret) + sizeof(*rsp) - 1);
if (ret <= 0)
{
break;
}
read += ret;
}
return 0;
}
/****************************************************************************
* Name: rpmsgmtd_write_handler
****************************************************************************/
static int rpmsgmtd_write_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgmtd_server_s *server = ept->priv;
FAR struct rpmsgmtd_write_s *msg = data;
int ret;
ret = MTD_WRITE(server->dev, msg->offset, msg->nbytes, msg->buf);
if (ret <= 0)
{
ferr("mtd write failed\n");
}
/* cookie != 0 indicate the data has been sent complete, so send back
* the total written bytes.
*/
if (msg->header.cookie != 0)
{
msg->header.result = ret;
rpmsg_send(ept, msg, sizeof(*msg) - 1);
}
return 0;
}
/****************************************************************************
* Name: rpmsgmtd_ioctl_handler
****************************************************************************/
static int rpmsgmtd_ioctl_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgmtd_server_s *server = ept->priv;
FAR struct rpmsgmtd_ioctl_s *msg = data;
msg->header.result = MTD_IOCTL(server->dev, msg->request,
msg->arglen > 0 ? (unsigned long)msg->buf :
msg->arg);
return rpmsg_send(ept, msg, len);
}
/****************************************************************************
* Name: rpmsgmtd_ns_match
****************************************************************************/
static bool rpmsgmtd_ns_match(FAR struct rpmsg_device *rdev,
FAR void *priv, FAR const char *name,
uint32_t dest)
{
return !strncmp(name, RPMSGMTD_NAME_PREFIX, RPMSGMTD_NAME_PREFIX_LEN);
}
/****************************************************************************
* Name: rpmsgmtd_ns_bind
****************************************************************************/
static void rpmsgmtd_ns_bind(FAR struct rpmsg_device *rdev,
FAR void *priv, FAR const char *name,
uint32_t dest)
{
FAR struct rpmsgmtd_server_s *server;
FAR struct inode *mtdnode;
int ret;
server = kmm_zalloc(sizeof(*server));
if (server == NULL)
{
ferr("mtd server malloced failed\n");
return;
}
ret = find_mtddriver(&name[RPMSGMTD_NAME_PREFIX_LEN], &mtdnode);
if (ret < 0)
{
ferr("mtd device find failed, ret=%d\n", ret);
goto errout;
}
server->ept.priv = server;
server->mtdnode = mtdnode;
server->dev = mtdnode->u.i_mtd;
ret = rpmsg_create_ept(&server->ept, rdev, name,
RPMSG_ADDR_ANY, dest,
rpmsgmtd_ept_cb, rpmsgmtd_ns_unbind);
if (ret < 0)
{
ferr("endpoint create failed, ret=%d\n", ret);
close_mtddriver(mtdnode);
goto errout;
}
return;
errout:
kmm_free(server);
}
/****************************************************************************
* Name: rpmsgmtd_ns_unbind
****************************************************************************/
static void rpmsgmtd_ns_unbind(FAR struct rpmsg_endpoint *ept)
{
FAR struct rpmsgmtd_server_s *server = ept->priv;
rpmsg_destroy_ept(&server->ept);
close_mtddriver(server->mtdnode);
kmm_free(server);
}
/****************************************************************************
* Name: rpmsgmtd_ept_cb
****************************************************************************/
static int rpmsgmtd_ept_cb(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len, uint32_t src,
FAR void *priv)
{
FAR struct rpmsgmtd_header_s *header = data;
uint32_t command = header->command;
if (command < ARRAY_SIZE(g_rpmsgmtd_handler))
{
return g_rpmsgmtd_handler[command](ept, data, len, src, priv);
}
return -EINVAL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: rpmsgmtd_server_init
*
* Description:
* Rpmsg-mtd server initialize function, the server cpu should call
* this function.
*
* Parameters:
* None
*
* Returned Values:
* OK on success; A negated errno value is returned on any failure.
*
****************************************************************************/
int rpmsgmtd_server_init(void)
{
return rpmsg_register_callback(NULL,
NULL,
NULL,
rpmsgmtd_ns_match,
rpmsgmtd_ns_bind);
}

View File

@ -687,6 +687,49 @@ bool filemtd_isfilemtd(FAR struct mtd_dev_s *mtd);
FAR struct mtd_dev_s *nullmtd_initialize(size_t mtdlen, int16_t sectsize,
int32_t erasesize);
/****************************************************************************
* Name: rpmsgmtd_register
*
* Description:
* Rpmsg-mtd client register function, the client cpu should call
* this function in the board initialize process.
*
* Parameters:
* remotecpu - the server cpu name
* remotepath - the device you want to access in the remote cpu
* localpath - the device path in local cpu, if NULL, the localpath is
* same as the remotepath, provide this argument to supoort
* custom device path
*
* Returned Values:
* OK on success; A negated errno value is returned on any failure.
*
****************************************************************************/
#ifdef CONFIG_RPMSGMTD
int rpmsgmtd_register(FAR const char *remotecpu, FAR const char *remotepath,
FAR const char *localpath);
#endif
/****************************************************************************
* Name: rpmsgmtd_server_init
*
* Description:
* Rpmsg-mtd server initialize function, the server cpu should call
* this function.
*
* Parameters:
* None
*
* Returned Values:
* OK on success; A negated errno value is returned on any failure.
*
****************************************************************************/
#ifdef CONFIG_RPMSGMTD_SERVER
int rpmsgmtd_server_init(void);
#endif
#undef EXTERN
#ifdef __cplusplus
}