2015-11-12 20:40:18 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/mtd/mtd_progmem.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
|
2015-11-12 20:40:18 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-12 20:40:18 +01: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.
|
2015-11-12 20:40:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
2015-11-13 18:32:34 +01:00
|
|
|
#include <stdbool.h>
|
2015-11-12 20:40:18 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/progmem.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
2021-03-01 16:02:18 +01:00
|
|
|
#ifdef CONFIG_MTD_PROGMEM
|
2015-11-12 20:40:18 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* 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 progmem_dev_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct progmem_dev_s
|
|
|
|
{
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Publicly visible representation of the interface */
|
2015-11-12 20:40:18 +01:00
|
|
|
|
|
|
|
struct mtd_dev_s mtd;
|
|
|
|
|
|
|
|
/* Fields unique to the progmem MTD driver */
|
|
|
|
|
|
|
|
bool initialized; /* True: Already initialized */
|
2018-05-01 14:23:52 +02:00
|
|
|
uint8_t blkshift; /* Log2 of the flash read/write block size */
|
|
|
|
uint8_t ersshift; /* Log2 of the flash erase block size */
|
2015-11-12 20:40:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Internal helper functions */
|
|
|
|
|
|
|
|
static int32_t progmem_log2(size_t blocksize);
|
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
|
|
|
static int progmem_erase(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t progmem_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buf);
|
|
|
|
static ssize_t progmem_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buf);
|
|
|
|
static ssize_t progmem_read(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR uint8_t *buffer);
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
static ssize_t progmem_write(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR const uint8_t *buffer);
|
|
|
|
#endif
|
|
|
|
static int progmem_ioctl(FAR struct mtd_dev_s *dev, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2017-11-13 16:08:39 +01:00
|
|
|
|
2015-11-12 20:40:18 +01:00
|
|
|
/* This structure holds the state of the MTD driver */
|
|
|
|
|
|
|
|
static struct progmem_dev_s g_progmem =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
progmem_erase,
|
|
|
|
progmem_bread,
|
|
|
|
progmem_bwrite,
|
|
|
|
progmem_read,
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
progmem_write,
|
|
|
|
#endif
|
2018-11-08 16:46:11 +01:00
|
|
|
progmem_ioctl,
|
|
|
|
"progmem",
|
2015-11-12 20:40:18 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-11-07 14:46:22 +01:00
|
|
|
* Name: progmem_log2
|
2015-11-12 20:40:18 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2019-11-07 14:46:22 +01:00
|
|
|
* Check block size is exact powers of two.
|
2015-11-12 20:40:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-19 13:39:07 +01:00
|
|
|
static int32_t progmem_log2(size_t blocksize)
|
2015-11-12 20:40:18 +01:00
|
|
|
{
|
|
|
|
uint32_t log2 = 0;
|
|
|
|
|
|
|
|
/* Search every bit in the blocksize from bit zero to bit 30 (omitting bit
|
|
|
|
* 31 which is the sign bit on return)
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (log2 = 0; log2 < 31; log2++, blocksize >>= 1)
|
|
|
|
{
|
|
|
|
/* Is bit zero set? */
|
|
|
|
|
|
|
|
if ((blocksize & 1) != 0)
|
|
|
|
{
|
|
|
|
/* Yes... the value should be exactly one. We do not support
|
|
|
|
* block sizes that are not exact powers of two.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return blocksize == 1 ? log2 : -ENOSYS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocksize == 0 ? -EINVAL : -E2BIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: progmem_erase
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Erase several blocks, each of the size previously reported.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int progmem_erase(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks)
|
|
|
|
{
|
|
|
|
ssize_t result;
|
|
|
|
|
|
|
|
/* Erase the specified blocks and return status (OK or a negated errno) */
|
|
|
|
|
|
|
|
while (nblocks > 0)
|
|
|
|
{
|
2018-09-21 05:18:38 +02:00
|
|
|
result = up_progmem_eraseblock(startblock);
|
2015-11-12 20:40:18 +01:00
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
return (int)result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup for the next pass through the loop */
|
|
|
|
|
|
|
|
startblock++;
|
|
|
|
nblocks--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: progmem_bread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the specified number of blocks into the user provided buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t progmem_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct progmem_dev_s *priv = (FAR struct progmem_dev_s *)dev;
|
2021-03-01 15:59:14 +01:00
|
|
|
#ifdef CONFIG_ARCH_HAVE_PROGMEM_READ
|
|
|
|
ssize_t result;
|
|
|
|
|
|
|
|
result = up_progmem_read(up_progmem_getaddress(startblock), buffer,
|
|
|
|
nblocks << priv->blkshift);
|
|
|
|
return result < 0 ? result : nblocks;
|
|
|
|
#else
|
2015-11-12 20:40:18 +01:00
|
|
|
FAR const uint8_t *src;
|
|
|
|
|
|
|
|
/* Read the specified blocks into the provided user buffer and return
|
|
|
|
* status (The positive, number of blocks actually read or a negated
|
|
|
|
* errno).
|
|
|
|
*/
|
|
|
|
|
2015-11-13 18:32:34 +01:00
|
|
|
src = (FAR const uint8_t *)up_progmem_getaddress(startblock);
|
2015-11-12 20:40:18 +01:00
|
|
|
memcpy(buffer, src, nblocks << priv->blkshift);
|
|
|
|
return nblocks;
|
2021-03-01 15:59:14 +01:00
|
|
|
#endif
|
2015-11-12 20:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: progmem_bwrite
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write the specified number of blocks from the user provided buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t progmem_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct progmem_dev_s *priv = (FAR struct progmem_dev_s *)dev;
|
|
|
|
ssize_t result;
|
|
|
|
|
2018-09-21 05:40:29 +02:00
|
|
|
/* Write the specified blocks from the provided user buffer and return
|
|
|
|
* status (The positive, number of blocks actually written or a negated
|
|
|
|
* errno)
|
2015-11-12 20:40:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
result = up_progmem_write(up_progmem_getaddress(startblock), buffer,
|
|
|
|
nblocks << priv->blkshift);
|
|
|
|
return result < 0 ? result : nblocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: progmem_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the specified number of bytes to the user provided buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t progmem_read(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR uint8_t *buffer)
|
|
|
|
{
|
2017-03-18 14:27:06 +01:00
|
|
|
FAR struct progmem_dev_s *priv = (FAR struct progmem_dev_s *)dev;
|
2021-03-01 15:59:14 +01:00
|
|
|
off_t startblock = offset >> priv->blkshift;
|
|
|
|
#ifdef CONFIG_ARCH_HAVE_PROGMEM_READ
|
|
|
|
ssize_t result;
|
|
|
|
|
|
|
|
result = up_progmem_read(up_progmem_getaddress(startblock) +
|
|
|
|
(offset & ((1 << priv->blkshift) - 1)), buffer, nbytes);
|
|
|
|
return result < 0 ? result : nbytes;
|
|
|
|
#else
|
2015-11-12 20:40:18 +01:00
|
|
|
FAR const uint8_t *src;
|
|
|
|
|
|
|
|
/* Read the specified bytes into the provided user buffer and return
|
|
|
|
* status (The positive, number of bytes actually read or a negated
|
|
|
|
* errno)
|
|
|
|
*/
|
|
|
|
|
2017-03-18 14:27:06 +01:00
|
|
|
src = (FAR const uint8_t *)up_progmem_getaddress(startblock) +
|
2018-09-21 05:40:29 +02:00
|
|
|
(offset & ((1 << priv->blkshift) - 1));
|
2015-11-12 20:40:18 +01:00
|
|
|
memcpy(buffer, src, nbytes);
|
|
|
|
return nbytes;
|
2021-03-01 15:59:14 +01:00
|
|
|
#endif
|
2015-11-12 20:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: progmem_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Some FLASH parts have the ability to write an arbitrary number of
|
|
|
|
* bytes to an arbitrary offset on the device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
static ssize_t progmem_write(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR const uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct progmem_dev_s *priv = (FAR struct progmem_dev_s *)dev;
|
2017-03-18 14:27:06 +01:00
|
|
|
off_t startblock;
|
2015-11-12 20:40:18 +01:00
|
|
|
ssize_t result;
|
|
|
|
|
2020-11-19 13:39:07 +01:00
|
|
|
/* Write the specified blocks from the provided user buffer and return
|
|
|
|
* status (The positive number of blocks actually written or a negated
|
|
|
|
* errno).
|
2015-11-12 20:40:18 +01:00
|
|
|
*/
|
|
|
|
|
2017-03-18 14:27:06 +01:00
|
|
|
startblock = offset >> priv->blkshift;
|
|
|
|
result = up_progmem_write(up_progmem_getaddress(startblock) +
|
2018-09-21 05:40:29 +02:00
|
|
|
(offset & ((1 << priv->blkshift) - 1)), buffer, nbytes);
|
2015-11-12 20:40:18 +01:00
|
|
|
return result < 0 ? result : nbytes;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: progmem_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-19 13:39:07 +01:00
|
|
|
static int progmem_ioctl(FAR struct mtd_dev_s *dev, int cmd,
|
|
|
|
unsigned long arg)
|
2015-11-12 20:40:18 +01:00
|
|
|
{
|
|
|
|
FAR struct progmem_dev_s *priv = (FAR struct progmem_dev_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
|
|
|
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)arg;
|
|
|
|
if (geo)
|
|
|
|
{
|
2020-11-19 13:39:07 +01:00
|
|
|
/* Populate the geometry structure with information needed to
|
|
|
|
* know the capacity and how to access the device.
|
2015-11-12 20:40:18 +01:00
|
|
|
*
|
2020-11-19 13:39:07 +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.
|
2015-11-12 20:40:18 +01:00
|
|
|
*/
|
|
|
|
|
2018-09-21 05:40:29 +02:00
|
|
|
geo->blocksize = (1 << priv->blkshift); /* Size of one read/write block */
|
|
|
|
geo->erasesize = (1 << priv->ersshift); /* Size of one erase block */
|
|
|
|
geo->neraseblocks = up_progmem_neraseblocks(); /* Number of erase blocks */
|
2015-11-12 20:40:18 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_XIPBASE:
|
|
|
|
{
|
|
|
|
FAR void **ppv = (FAR void**)arg;
|
|
|
|
|
|
|
|
if (ppv)
|
|
|
|
{
|
|
|
|
/* Return (void*) base address of FLASH memory. */
|
|
|
|
|
|
|
|
*ppv = (FAR void *)up_progmem_getaddress(0);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
2018-09-21 05:18:38 +02:00
|
|
|
size_t nblocks = up_progmem_neraseblocks();
|
2015-11-12 20:40:18 +01:00
|
|
|
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
ret = progmem_erase(dev, 0, nblocks);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: progmem_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2015-11-12 23:03:24 +01:00
|
|
|
* Create and initialize an MTD device instance that can be used to access
|
|
|
|
* on-chip program memory.
|
|
|
|
*
|
|
|
|
* 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).
|
2015-11-12 20:40:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct mtd_dev_s *progmem_initialize(void)
|
|
|
|
{
|
|
|
|
FAR struct progmem_dev_s *priv = (FAR struct progmem_dev_s *)&g_progmem;
|
|
|
|
int32_t blkshift;
|
2018-05-01 14:23:52 +02:00
|
|
|
int32_t ersshift;
|
2015-11-12 20:40:18 +01:00
|
|
|
|
|
|
|
/* Perform initialization if necessary */
|
|
|
|
|
|
|
|
if (!g_progmem.initialized)
|
|
|
|
{
|
2015-11-13 16:19:25 +01:00
|
|
|
/* Get the size of one block. Here we assume that the block size is
|
|
|
|
* uniform and that the size of block0 is the same as the size of any
|
|
|
|
* other block.
|
|
|
|
*/
|
2015-11-12 20:40:18 +01:00
|
|
|
|
2015-11-13 16:19:25 +01:00
|
|
|
size_t blocksize = up_progmem_pagesize(0);
|
|
|
|
|
2018-05-01 14:23:52 +02:00
|
|
|
/* Calculate Log2 of the flash read/write block size */
|
2015-11-13 16:19:25 +01:00
|
|
|
|
|
|
|
blkshift = progmem_log2(blocksize);
|
2015-11-12 20:40:18 +01:00
|
|
|
if (blkshift < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:23:52 +02:00
|
|
|
/* Calculate Log2 of the flash erase block size */
|
|
|
|
|
|
|
|
blocksize = up_progmem_erasesize(0);
|
|
|
|
|
|
|
|
ersshift = progmem_log2(blocksize);
|
|
|
|
if (ersshift < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-13 16:19:25 +01:00
|
|
|
/* Save the configuration data */
|
|
|
|
|
2015-11-12 20:40:18 +01:00
|
|
|
g_progmem.blkshift = blkshift;
|
2018-05-01 14:23:52 +02:00
|
|
|
g_progmem.ersshift = ersshift;
|
2015-11-12 20:40:18 +01:00
|
|
|
g_progmem.initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|
2017-11-13 16:08:39 +01:00
|
|
|
|
2021-03-01 16:02:18 +01:00
|
|
|
#endif /* CONFIG_MTD_PROGMEM */
|