2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* drivers/mtd/mtd_rwbuffer.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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
|
2014-07-11 19:20:11 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-11 19:20:11 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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.
|
2014-07-11 19:20:11 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* MTD driver that contains another MTD driver and provides read-ahead
|
|
|
|
* and/or write buffering.
|
|
|
|
*/
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-11-23 05:22:51 +01:00
|
|
|
#include <inttypes.h>
|
2014-07-11 19:20:11 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2016-07-20 21:54:38 +02:00
|
|
|
#include <nuttx/drivers/rwbuffer.h>
|
2014-07-11 19:20:11 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_DRVR_WRITEBUFFER) || defined(CONFIG_DRVR_READAHEAD)
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2014-07-12 00:19:17 +02:00
|
|
|
#ifndef CONFIG_DRVR_INVALIDATE
|
|
|
|
# error This driver requires CONFIG_DRVR_INVALIDATE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_DRVR_READBYTES
|
|
|
|
# error This driver requires CONFIG_DRVR_READBYTES
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 19:20:11 +02:00
|
|
|
#ifndef CONFIG_MTD_NWRBLOCKS
|
|
|
|
# define CONFIG_MTD_NWRBLOCKS 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_MTD_NRDBLOCKS
|
|
|
|
# define CONFIG_MTD_NRDBLOCKS 4
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* This type represents the state of the MTD device.
|
|
|
|
* The struct mtd_dev_s must appear at the beginning of the definition so
|
|
|
|
* that you can freely cast between pointers to struct mtd_dev_s and struct
|
|
|
|
* mtd_rwbuffer_s.
|
2014-07-11 19:20:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct mtd_rwbuffer_s
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
struct mtd_dev_s mtd; /* Our exported MTD interface */
|
|
|
|
FAR struct mtd_dev_s *dev; /* Saved lower level MTD interface instance */
|
|
|
|
struct rwbuffer_s rwb; /* The rwbuffer state structure */
|
|
|
|
uint16_t spb; /* Number of sectors per block */
|
2014-07-11 19:20:11 +02:00
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
/* rwbuffer callouts */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t mtd_reload(FAR void *dev,
|
|
|
|
FAR uint8_t *buffer,
|
|
|
|
off_t startblock,
|
2014-07-11 19:20:11 +02:00
|
|
|
size_t nblocks);
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t mtd_flush(FAR void *dev,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t startblock,
|
2014-07-11 19:20:11 +02:00
|
|
|
size_t nblocks);
|
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int mtd_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t block,
|
|
|
|
size_t nsectors);
|
|
|
|
static ssize_t mtd_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t block,
|
|
|
|
size_t nsectors,
|
|
|
|
FAR uint8_t *buf);
|
|
|
|
static ssize_t mtd_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t block,
|
|
|
|
size_t nsectors,
|
|
|
|
FAR const uint8_t *buf);
|
|
|
|
static ssize_t mtd_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2014-07-11 19:20:11 +02:00
|
|
|
FAR uint8_t *buffer);
|
2021-01-27 16:48:40 +01:00
|
|
|
static int mtd_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Private Data
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Name: mtd_reload
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Reload the read-ahead buffer
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t mtd_reload(FAR void *dev,
|
|
|
|
FAR uint8_t *buffer,
|
|
|
|
off_t startblock,
|
2014-07-11 19:20:11 +02:00
|
|
|
size_t nblocks)
|
|
|
|
{
|
|
|
|
FAR struct mtd_rwbuffer_s *priv = (FAR struct mtd_rwbuffer_s *)dev;
|
|
|
|
DEBUGASSERT(priv && priv->dev);
|
|
|
|
|
|
|
|
/* This is just a pass-through to the contained MTD */
|
|
|
|
|
|
|
|
return priv->dev->bread(priv->dev, startblock, nblocks, buffer);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Name: mtd_flush
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Flush the write buffer to hardware
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t mtd_flush(FAR void *dev,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t startblock,
|
2014-07-11 19:20:11 +02:00
|
|
|
size_t nblocks)
|
|
|
|
{
|
|
|
|
FAR struct mtd_rwbuffer_s *priv = (FAR struct mtd_rwbuffer_s *)dev;
|
|
|
|
DEBUGASSERT(priv && priv->dev);
|
|
|
|
|
|
|
|
/* This is just a pass-through to the contained MTD */
|
|
|
|
|
|
|
|
return priv->dev->bwrite(priv->dev, startblock, nblocks, buffer);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Name: mtd_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int mtd_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t block,
|
|
|
|
size_t nblocks)
|
2014-07-11 19:20:11 +02:00
|
|
|
{
|
|
|
|
FAR struct mtd_rwbuffer_s *priv = (FAR struct mtd_rwbuffer_s *)dev;
|
|
|
|
off_t sector;
|
|
|
|
size_t nsectors;
|
|
|
|
int ret;
|
|
|
|
|
2020-11-23 05:22:51 +01:00
|
|
|
finfo("block: %08zx nsectors: %zu\n",
|
|
|
|
(intmax_t)block, nsectors);
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
/* Convert to logical sectors and sector numbers */
|
|
|
|
|
|
|
|
sector = block * priv->spb;
|
|
|
|
nsectors = nblocks * priv->spb;
|
|
|
|
|
|
|
|
/* Then invalidate in cached data */
|
|
|
|
|
|
|
|
ret = rwb_invalidate(&priv->rwb, sector, nsectors);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: rwb_invalidate failed: %d\n", ret);
|
2014-07-11 19:20:11 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then let the lower level MTD driver do the real erase */
|
|
|
|
|
|
|
|
return priv->dev->erase(priv->dev, block, nblocks);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Name: mtd_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
static ssize_t mtd_bread(FAR struct mtd_dev_s *dev, off_t sector,
|
|
|
|
size_t nsectors, FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct mtd_rwbuffer_s *priv = (FAR struct mtd_rwbuffer_s *)dev;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Let the rwbuffer logic do it real work.
|
|
|
|
* It will call out to mtd_reload if is needs to read any data.
|
2014-07-11 19:20:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
return rwb_read(&priv->rwb, sector, nsectors, buffer);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Name: mtd_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t mtd_bwrite(FAR struct mtd_dev_s *dev, off_t block,
|
|
|
|
size_t nsectors,
|
|
|
|
FAR const uint8_t *buffer)
|
2014-07-11 19:20:11 +02:00
|
|
|
{
|
|
|
|
FAR struct mtd_rwbuffer_s *priv = (FAR struct mtd_rwbuffer_s *)dev;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Let the rwbuffer logic do it real work.
|
|
|
|
* It will call out to wrb_reload it is needs to read any data.
|
2014-07-11 19:20:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
return rwb_write(&priv->rwb, block, nsectors, buffer);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Name: mtd_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t mtd_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR uint8_t *buffer)
|
2014-07-11 19:20:11 +02:00
|
|
|
{
|
|
|
|
FAR struct mtd_rwbuffer_s *priv = (FAR struct mtd_rwbuffer_s *)dev;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Let the rwbuffer logic do it real work.
|
|
|
|
* It will call out to mtd_reload it is needs to read any data.
|
2014-07-11 19:20:11 +02:00
|
|
|
*/
|
|
|
|
|
2014-07-12 00:19:17 +02:00
|
|
|
return rwb_readbytes(&priv->rwb, offset, nbytes, buffer);
|
2014-07-11 19:20:11 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Name: mtd_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
static int mtd_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct mtd_rwbuffer_s *priv = (FAR struct mtd_rwbuffer_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
finfo("cmd: %d\n", cmd);
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2020-11-23 05:17:04 +01:00
|
|
|
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)
|
|
|
|
((uintptr_t)arg);
|
2014-07-11 19:20:11 +02:00
|
|
|
if (geo)
|
|
|
|
{
|
2021-08-11 06:21:39 +02:00
|
|
|
memset(geo, 0, sizeof(*geo));
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Populate the geometry structure with information need to
|
|
|
|
* know the capacity and how to access the device.
|
2014-07-11 19:20:11 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
* NOTE:
|
|
|
|
* that the device is treated as though it where just an array
|
|
|
|
* of fixed size blocks. That is most likely not true, but the
|
|
|
|
* client will expect the device logic to do whatever is
|
|
|
|
* necessary to make it appear so.
|
2014-07-11 19:20:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
geo->blocksize = priv->rwb.blocksize;
|
2014-09-25 21:35:10 +02:00
|
|
|
geo->erasesize = priv->rwb.blocksize * priv->spb;
|
|
|
|
geo->neraseblocks = priv->rwb.nblocks / priv->spb;
|
2014-07-11 19:20:11 +02:00
|
|
|
ret = OK;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("blocksize: %d erasesize: %d neraseblocks: %d\n",
|
2014-07-11 19:20:11 +02:00
|
|
|
geo->blocksize, geo->erasesize, geo->neraseblocks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-08-15 20:10:23 +02:00
|
|
|
case BIOC_PARTINFO:
|
|
|
|
{
|
|
|
|
FAR struct partition_info_s *info =
|
|
|
|
(FAR struct partition_info_s *)arg;
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
info->numsectors = priv->rwb.nblocks;
|
|
|
|
info->sectorsize = priv->rwb.blocksize;
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-07-11 19:20:11 +02:00
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
ret = priv->dev->ioctl(priv->dev, MTDIOC_BULKERASE, 0);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Device ioctl failed: %d\n", ret);
|
2014-07-11 19:20:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then invalidate in cached data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
ret = rwb_invalidate(&priv->rwb, 0, priv->rwb.nblocks);
|
2014-07-11 19:20:11 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: rwb_invalidate failed: %d\n", ret);
|
2014-07-11 19:20:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return %d\n", ret);
|
2014-07-11 19:20:11 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Name: mtd_rwb_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Create an initialized MTD device instance.
|
|
|
|
* This MTD driver contains another MTD driver and converts a larger
|
|
|
|
* sector size to a standard 512 byte sector size.
|
2014-07-11 19:20:11 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
* MTD devices are not registered in the file system, but are created as
|
|
|
|
* instances that can be bound to other functions (such as a block or
|
|
|
|
* character driver front end).
|
2014-07-11 19:20:11 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
FAR struct mtd_dev_s *mtd_rwb_initialize(FAR struct mtd_dev_s *mtd)
|
|
|
|
{
|
|
|
|
FAR struct mtd_rwbuffer_s *priv;
|
|
|
|
struct mtd_geometry_s geo;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mtd: %p\n", mtd);
|
2014-07-11 19:20:11 +02:00
|
|
|
DEBUGASSERT(mtd && mtd->ioctl);
|
|
|
|
|
|
|
|
/* Get the device geometry */
|
|
|
|
|
|
|
|
ret = mtd->ioctl(mtd, MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&geo));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: MTDIOC_GEOMETRY ioctl failed: %d\n", ret);
|
2014-07-11 19:20:11 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a state structure (we allocate the structure instead of using
|
|
|
|
* a fixed, static allocation so that we can handle multiple FLASH devices.
|
|
|
|
* The current implementation would handle only one FLASH part per SPI
|
2021-01-27 16:48:40 +01:00
|
|
|
* device (only because of the SPIDEV_FLASH(0) definition) and so would
|
|
|
|
* have to be extended to handle multiple FLASH parts on the same SPI bus.
|
2014-07-11 19:20:11 +02:00
|
|
|
*/
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
priv = (FAR struct mtd_rwbuffer_s *)
|
|
|
|
kmm_zalloc(sizeof(struct mtd_rwbuffer_s));
|
2017-05-11 15:22:21 +02:00
|
|
|
if (!priv)
|
2014-07-11 19:20:11 +02:00
|
|
|
{
|
2017-05-11 15:22:21 +02:00
|
|
|
ferr("ERROR: Failed to allocate mtd_rwbuffer\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
/* Initialize the allocated structure. (unsupported methods/fields
|
|
|
|
* were already nullified by kmm_zalloc).
|
|
|
|
*/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
priv->mtd.erase = mtd_erase; /* Our MTD erase method */
|
|
|
|
priv->mtd.bread = mtd_bread; /* Our MTD bread method */
|
|
|
|
priv->mtd.bwrite = mtd_bwrite; /* Our MTD bwrite method */
|
|
|
|
priv->mtd.read = mtd_read; /* Our MTD read method */
|
|
|
|
priv->mtd.ioctl = mtd_ioctl; /* Our MTD ioctl method */
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "rwbuffer";
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
priv->dev = mtd; /* The contained MTD instance */
|
|
|
|
|
|
|
|
/* Sectors per block. The erase block size must be an even multiple
|
|
|
|
* of the sector size.
|
|
|
|
*/
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
priv->spb = geo.erasesize / geo.blocksize;
|
|
|
|
DEBUGASSERT((size_t)priv->spb * geo.blocksize == geo.erasesize);
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
/* Values must be provided to rwb_initialize() */
|
2020-11-23 05:17:04 +01:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
/* Supported geometry */
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
priv->rwb.blocksize = geo.blocksize;
|
|
|
|
priv->rwb.nblocks = geo.neraseblocks * priv->spb;
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
/* Buffer setup */
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_DRVR_WRITEBUFFER
|
2017-05-11 15:22:21 +02:00
|
|
|
priv->rwb.wrmaxblocks = CONFIG_MTD_NWRBLOCKS;
|
2014-07-11 19:20:11 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_DRVR_READAHEAD
|
2017-05-11 15:22:21 +02:00
|
|
|
priv->rwb.rhmaxblocks = CONFIG_MTD_NRDBLOCKS;
|
2014-07-11 19:20:11 +02:00
|
|
|
#endif
|
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
/* Callouts */
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
priv->rwb.dev = priv; /* Device state passed to callouts */
|
|
|
|
priv->rwb.wrflush = mtd_flush; /* Callout to flush buffer */
|
|
|
|
priv->rwb.rhreload = mtd_reload; /* Callout to reload buffer */
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
/* Initialize read-ahead/write buffering */
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2017-05-11 15:22:21 +02:00
|
|
|
ret = rwb_initialize(&priv->rwb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: rwb_initialize failed: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
return NULL;
|
2014-07-11 19:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
|
|
|
return &priv->mtd;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DRVR_WRITEBUFFER || CONFIG_DRVR_READAHEAD */
|