2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* drivers/mtd/sector512.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-03-25 00:45:45 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-03-25 00:45:45 +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.
|
2014-03-25 00:45:45 +01:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* MTD driver that contains another MTD driver and converts a larger sector
|
|
|
|
* size to a standard 512 byte sector size.
|
|
|
|
*/
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-12-03 00:20:36 +01:00
|
|
|
#include <inttypes.h>
|
2014-03-25 00:45:45 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2020-12-03 00:16:58 +01:00
|
|
|
|
2014-03-25 00:45:45 +01:00
|
|
|
/* Configuration */
|
|
|
|
|
|
|
|
#ifndef CONFIG_MTD_SECT512_ERASED_STATE
|
|
|
|
# define CONFIG_MTD_SECT512_ERASED_STATE 0xff
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 512-byte sector constants */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define SECTOR_512 512
|
|
|
|
#define SHIFT_512 9
|
|
|
|
#define MASK_512 511
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
/* Cache flags */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define SST25_CACHE_VALID (1 << 0) /* 1=Cache has valid data */
|
|
|
|
#define SST25_CACHE_DIRTY (1 << 1) /* 1=Cache is dirty */
|
|
|
|
#define SST25_CACHE_ERASED (1 << 2) /* 1=Backing FLASH is erased */
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define IS_VALID(p) ((((p)->flags) & SST25_CACHE_VALID) != 0)
|
|
|
|
#define IS_DIRTY(p) ((((p)->flags) & SST25_CACHE_DIRTY) != 0)
|
|
|
|
#define IS_ERASED(p) ((((p)->flags) & SST25_CACHE_DIRTY) != 0)
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define SET_VALID(p) do { (p)->flags |= SST25_CACHE_VALID; } while (0)
|
|
|
|
#define SET_DIRTY(p) do { (p)->flags |= SST25_CACHE_DIRTY; } while (0)
|
|
|
|
#define SET_ERASED(p) do { (p)->flags |= SST25_CACHE_DIRTY; } while (0)
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CLR_VALID(p) do { (p)->flags &= ~SST25_CACHE_VALID; } while (0)
|
|
|
|
#define CLR_DIRTY(p) do { (p)->flags &= ~SST25_CACHE_DIRTY; } while (0)
|
|
|
|
#define CLR_ERASED(p) do { (p)->flags &= ~SST25_CACHE_DIRTY; } while (0)
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01: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 s512_dev_s.
|
2014-03-25 00:45:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct s512_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct mtd_dev_s *dev; /* Saved lower level MTD interface instance */
|
|
|
|
uint32_t eblocksize; /* Size of one erase block */
|
|
|
|
size_t neblocks; /* Number of erase blocks */
|
|
|
|
size_t sectperblock; /* Number of read/write sectors per erase block */
|
|
|
|
uint16_t stdperblock; /* Number of 512 byte sectors in one erase block */
|
|
|
|
uint8_t flags; /* Buffered sector flags */
|
2015-10-10 18:41:00 +02:00
|
|
|
uint32_t eblockno; /* Erase sector number in the cache */
|
2014-03-25 00:45:45 +01:00
|
|
|
FAR uint8_t *eblock; /* Allocated erase block */
|
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static FAR uint8_t *s512_cacheread(struct s512_dev_s *priv, off_t sector);
|
|
|
|
#ifndef CONFIG_MTD_SECT512_READONLY
|
|
|
|
static void s512_cacheflush(struct s512_dev_s *priv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int s512_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t sector512,
|
|
|
|
size_t nsectors);
|
|
|
|
static ssize_t s512_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t sector512,
|
|
|
|
size_t nsectors,
|
|
|
|
FAR uint8_t *buf);
|
|
|
|
static ssize_t s512_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t sector512,
|
|
|
|
size_t nsectors,
|
|
|
|
FAR const uint8_t *buf);
|
|
|
|
static ssize_t s512_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR uint8_t *buffer);
|
|
|
|
static int s512_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Private Data
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Name: s512_cacheread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
static FAR uint8_t *s512_cacheread(struct s512_dev_s *priv, off_t sector512)
|
|
|
|
{
|
|
|
|
off_t eblockno;
|
|
|
|
off_t sector;
|
|
|
|
ssize_t result;
|
|
|
|
int index;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2014-03-25 00:45:45 +01:00
|
|
|
/* Get the erase block containing this sector */
|
|
|
|
|
|
|
|
eblockno = sector512 / priv->stdperblock;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector512: %lu eblockno: %lu\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
(unsigned long)sector512, (unsigned long)eblockno);
|
|
|
|
|
|
|
|
/* Check if the requested erase block is already in the cache */
|
|
|
|
|
|
|
|
if (!IS_VALID(priv) || eblockno != priv->eblockno)
|
|
|
|
{
|
|
|
|
/* No.. Flush any dirty erase block currently in the cache */
|
|
|
|
|
|
|
|
s512_cacheflush(priv);
|
|
|
|
|
|
|
|
/* Read the erase block into the cache */
|
|
|
|
|
|
|
|
sector = eblockno * priv->sectperblock;
|
|
|
|
result = priv->dev->bread(priv->dev, sector, priv->sectperblock,
|
|
|
|
priv->eblock);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: bread(%lu, %lu) returned %ld\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
(unsigned long)sector, (unsigned long)priv->eblocksize,
|
|
|
|
(long)result);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark the sector as cached */
|
|
|
|
|
|
|
|
priv->eblockno = eblockno;
|
|
|
|
|
|
|
|
SET_VALID(priv); /* The data in the cache is valid */
|
|
|
|
CLR_DIRTY(priv); /* It should match the FLASH contents */
|
|
|
|
CLR_ERASED(priv); /* The underlying FLASH has not been erased */
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Get the index to the 512 sector in the erase block that holds the
|
|
|
|
* argument
|
|
|
|
*/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
index = sector512 % priv->stdperblock;
|
|
|
|
|
|
|
|
/* Return the address in the cache that holds this sector */
|
|
|
|
|
|
|
|
return &priv->eblock[index << SHIFT_512];
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Name: s512_cacheflush
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
#if !defined(CONFIG_MTD_SECT512_READONLY)
|
|
|
|
static void s512_cacheflush(struct s512_dev_s *priv)
|
|
|
|
{
|
|
|
|
off_t sector;
|
|
|
|
ssize_t result;
|
|
|
|
|
2020-12-03 00:16:58 +01:00
|
|
|
/* If the cached is dirty (meaning that it no longer matches the old FLASH
|
|
|
|
* contents) or was erased (with the cache containing the correct FLASH
|
|
|
|
* contents), then write the cached erase block to FLASH.
|
2014-03-25 00:45:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (IS_DIRTY(priv) || IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
/* Write entire erase block to FLASH */
|
|
|
|
|
|
|
|
sector = priv->eblockno * priv->sectperblock;
|
2020-12-03 00:16:58 +01:00
|
|
|
result = priv->dev->bwrite(priv->dev, sector, priv->sectperblock,
|
|
|
|
priv->eblock);
|
2014-03-25 00:45:45 +01:00
|
|
|
if (result < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: bwrite(%lu, %lu) returned %ld\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
(unsigned long)sector, (unsigned long)priv->eblocksize,
|
|
|
|
(long)result);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The cache is no long dirty and the FLASH is no longer erased */
|
|
|
|
|
|
|
|
CLR_DIRTY(priv);
|
|
|
|
CLR_ERASED(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Name: s512_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int s512_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t sector512,
|
|
|
|
size_t nsectors)
|
2014-03-25 00:45:45 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_MTD_SECT512_READONLY
|
|
|
|
return -EACESS
|
|
|
|
#else
|
|
|
|
FAR struct s512_dev_s *priv = (FAR struct s512_dev_s *)dev;
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
size_t sectorsleft = nsectors;
|
|
|
|
size_t eblockno;
|
|
|
|
int ret;
|
|
|
|
|
2020-12-03 00:20:36 +01:00
|
|
|
finfo("sector512: %08jx nsectors: %zu\n", (intmax_t)sector512, nsectors);
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
while (sectorsleft-- > 0)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Erase each sector. First, make sure that the erase block containing
|
|
|
|
* the 512 byte sector is in the cache.
|
2014-03-25 00:45:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
dest = s512_cacheread(priv, sector512);
|
|
|
|
if (!dest)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
ferr("ERROR: s512_cacheread(%lu) failed\n",
|
|
|
|
(unsigned long)sector512);
|
2014-03-25 00:45:45 +01:00
|
|
|
DEBUGPANIC();
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Erase the block containing this sector if it is not already erased.
|
2021-01-27 16:48:40 +01:00
|
|
|
* The erased indicator will be cleared when the data from the erase
|
|
|
|
* sector is read into the cache and set here when we erase the block.
|
2015-10-10 18:41:00 +02:00
|
|
|
*/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
if (!IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
eblockno = sector512 / priv->stdperblock;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector512: %lu eblockno: %lu\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
(unsigned long)sector512, (unsigned long)eblockno);
|
|
|
|
|
|
|
|
ret = priv->dev->erase(priv->dev, eblockno, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to erase block %lu: %d\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
(unsigned long)eblockno, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SET_ERASED(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put the cached sector data into the erase state and mark the cache
|
|
|
|
* as dirty (but don't update the FLASH yet. The caller will do that
|
|
|
|
* at a more optimal time).
|
|
|
|
*/
|
|
|
|
|
|
|
|
memset(dest, CONFIG_MTD_SECT512_ERASED_STATE, SECTOR_512);
|
|
|
|
SET_DIRTY(priv);
|
|
|
|
sector512++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the last erase block left in the cache */
|
|
|
|
|
|
|
|
s512_cacheflush(priv);
|
|
|
|
|
|
|
|
return (int)nsectors;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Name: s512_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
static ssize_t s512_bread(FAR struct mtd_dev_s *dev, off_t sector512,
|
|
|
|
size_t nsectors, FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct s512_dev_s *priv = (FAR struct s512_dev_s *)dev;
|
|
|
|
FAR uint8_t *src;
|
|
|
|
ssize_t remaining;
|
|
|
|
ssize_t result = nsectors;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector512: %08lx nsectors: %d\n", (long)sector512, (int)nsectors);
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
/* Read each 512 byte sector from the block via the erase block cache */
|
|
|
|
|
|
|
|
for (remaining = nsectors; remaining; remaining--)
|
|
|
|
{
|
|
|
|
/* Make sure that the next sector is in the erase block cache */
|
|
|
|
|
|
|
|
src = s512_cacheread(priv, sector512);
|
|
|
|
if (!src)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
ferr("ERROR: s512_cacheread(%lu) failed\n",
|
|
|
|
(unsigned long)sector512);
|
2014-03-25 00:45:45 +01:00
|
|
|
DEBUGPANIC();
|
|
|
|
|
|
|
|
result = (ssize_t)nsectors - remaining;
|
|
|
|
if (result <= 0)
|
|
|
|
{
|
|
|
|
result = -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Copy the sector data from the erase block cache into the user
|
|
|
|
* buffer
|
|
|
|
*/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
memcpy(buffer, src, SECTOR_512);
|
|
|
|
|
|
|
|
buffer += SECTOR_512;
|
|
|
|
sector512++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Name: s512_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2020-12-03 00:16:58 +01:00
|
|
|
static ssize_t s512_bwrite(FAR struct mtd_dev_s *dev, off_t sector512,
|
|
|
|
size_t nsectors,
|
|
|
|
FAR const uint8_t *buffer)
|
2014-03-25 00:45:45 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_MTD_SECT512_READONLY
|
|
|
|
return -EACCESS;
|
|
|
|
#else
|
|
|
|
FAR struct s512_dev_s *priv = (FAR struct s512_dev_s *)dev;
|
|
|
|
ssize_t remaining;
|
|
|
|
ssize_t result;
|
|
|
|
off_t eblockno;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector512: %08lx nsectors: %d\n", (long)sector512, (int)nsectors);
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
|
|
|
|
for (remaining = nsectors; remaining > 0; remaining--)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* First, make sure that the erase block containing 512 byte sector is
|
|
|
|
* in memory.
|
2014-03-25 00:45:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
dest = s512_cacheread(priv, sector512);
|
|
|
|
if (!dest)
|
|
|
|
{
|
|
|
|
result = (ssize_t)nsectors - remaining;
|
|
|
|
if (result <= 0)
|
|
|
|
{
|
|
|
|
result = -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Erase the block containing this sector if it is not already erased.
|
2021-01-27 16:48:40 +01:00
|
|
|
* The erased indicated will be cleared when the data from the erase
|
|
|
|
* sector is read into the cache and set here when we erase the sector.
|
2014-03-25 00:45:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
eblockno = sector512 / priv->stdperblock;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector512: %lu eblockno: %lu\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
(unsigned long)sector512, (unsigned long)eblockno);
|
|
|
|
|
|
|
|
result = priv->dev->erase(priv->dev, eblockno, 1);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to erase block %lu: %ld\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
(unsigned long)eblockno, (long)result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SET_ERASED(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the new sector data into cached erase block */
|
|
|
|
|
|
|
|
memcpy(dest, buffer, SECTOR_512);
|
|
|
|
SET_DIRTY(priv);
|
|
|
|
|
|
|
|
/* Set up for the next 512 byte sector */
|
|
|
|
|
|
|
|
buffer += SECTOR_512;
|
|
|
|
sector512++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the last erase block left in the cache */
|
|
|
|
|
|
|
|
s512_cacheflush(priv);
|
|
|
|
return nsectors;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Name: s512_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t s512_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2014-03-25 00:45:45 +01:00
|
|
|
FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct s512_dev_s *priv = (FAR struct s512_dev_s *)dev;
|
|
|
|
FAR uint8_t *src;
|
|
|
|
ssize_t remaining;
|
|
|
|
ssize_t xfrsize;
|
|
|
|
off_t sectoffset;
|
|
|
|
off_t sector;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx nbytes: %lu\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
(unsigned long)offset, (unsigned long)nbytes);
|
|
|
|
|
|
|
|
/* Convert the offset into 512 byte sector address and a byte offset */
|
|
|
|
|
|
|
|
sectoffset = offset & MASK_512;
|
|
|
|
sector = offset >> SHIFT_512;
|
|
|
|
|
|
|
|
for (remaining = nbytes; remaining > 0; remaining -= xfrsize)
|
|
|
|
{
|
|
|
|
/* Read the erase block into the cache and get the address of the
|
|
|
|
* beginning of the 512 byte block in the cached erase block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
src = s512_cacheread(priv, sector);
|
|
|
|
if (!src)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
2020-12-03 00:20:36 +01:00
|
|
|
ferr("ERROR: s512_cacheread(%lu) failed\n", (unsigned long)sector);
|
2014-03-25 00:45:45 +01:00
|
|
|
DEBUGPANIC();
|
|
|
|
|
|
|
|
result = (ssize_t)nbytes - remaining;
|
|
|
|
if (result <= 0)
|
|
|
|
{
|
|
|
|
result = -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then copy the requested bytes from the cached erase block */
|
|
|
|
|
|
|
|
xfrsize = remaining;
|
|
|
|
if (sectoffset + xfrsize > SECTOR_512)
|
|
|
|
{
|
|
|
|
xfrsize = SECTOR_512 - sectoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, src + sectoffset, xfrsize);
|
|
|
|
buffer += xfrsize;
|
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return nbytes: %d\n", (int)nbytes);
|
2014-03-25 00:45:45 +01:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Name: s512_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
static int s512_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct s512_dev_s *priv = (FAR struct s512_dev_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("cmd: %d \n", cmd);
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2020-12-03 00:16:58 +01:00
|
|
|
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)
|
|
|
|
((uintptr_t)arg);
|
2014-03-25 00:45:45 +01:00
|
|
|
if (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-03-25 00:45:45 +01: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-03-25 00:45:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
geo->blocksize = SECTOR_512;
|
|
|
|
geo->erasesize = SECTOR_512;
|
|
|
|
geo->neraseblocks = priv->neblocks * priv->stdperblock;
|
|
|
|
ret = OK;
|
|
|
|
|
2020-12-03 00:20:36 +01:00
|
|
|
finfo("blocksize: %" PRId32 " erasesize: %" PRId32
|
|
|
|
" neraseblocks: %" PRId32 "\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
geo->blocksize, geo->erasesize, geo->neraseblocks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
ret = priv->dev->ioctl(priv->dev, MTDIOC_BULKERASE, 0);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
priv->flags = 0; /* Buffered sector flags */
|
2015-10-10 18:41:00 +02:00
|
|
|
priv->eblockno = 0; /* Erase sector number in the cache */
|
2014-03-25 00:45:45 +01:00
|
|
|
priv->eblock = NULL; /* Allocated erase block */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2021-07-16 19:16:41 +02:00
|
|
|
case MTDIOC_ERASESTATE:
|
|
|
|
{
|
|
|
|
FAR uint8_t *result = (FAR uint8_t *)arg;
|
|
|
|
*result = CONFIG_MTD_SECT512_ERASED_STATE;
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-03-25 00:45:45 +01:00
|
|
|
case MTDIOC_XIPBASE:
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return %d\n", ret);
|
2014-03-25 00:45:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2014-03-25 00:45:45 +01:00
|
|
|
* Name: s512_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-03-25 00:45:45 +01: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-03-25 00:45:45 +01:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
FAR struct mtd_dev_s *s512_initialize(FAR struct mtd_dev_s *mtd)
|
|
|
|
{
|
|
|
|
FAR struct s512_dev_s *priv;
|
|
|
|
FAR struct mtd_geometry_s geo;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mtd: %p\n", mtd);
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
/* Get the device geometry */
|
|
|
|
|
|
|
|
DEBUGASSERT(mtd && mtd->ioctl);
|
|
|
|
ret = mtd->ioctl(mtd, MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&geo));
|
|
|
|
|
|
|
|
/* We expect that the block size will be >512 and an even multiple of 512 */
|
|
|
|
|
|
|
|
if (ret < 0 || geo.erasesize <= SECTOR_512 ||
|
2015-10-10 18:41:00 +02:00
|
|
|
(geo.erasesize & ~MASK_512) != geo.erasesize)
|
2014-03-25 00:45:45 +01:00
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
ferr(
|
|
|
|
"ERROR: MTDIOC_GEOMETRY ioctl returned %d, eraseize=%" PRId32 "\n",
|
2014-03-25 00:45:45 +01:00
|
|
|
ret, geo.erasesize);
|
|
|
|
DEBUGPANIC();
|
|
|
|
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-03-25 00:45:45 +01:00
|
|
|
*/
|
|
|
|
|
2014-09-01 01:34:44 +02:00
|
|
|
priv = (FAR struct s512_dev_s *)kmm_zalloc(sizeof(struct s512_dev_s));
|
2014-03-25 00:45:45 +01:00
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated structure. (unsupported methods/fields
|
2014-09-01 01:34:44 +02:00
|
|
|
* were already nullified by kmm_zalloc).
|
2014-03-25 00:45:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
priv->mtd.erase = s512_erase;
|
|
|
|
priv->mtd.bread = s512_bread;
|
|
|
|
priv->mtd.bwrite = s512_bwrite;
|
|
|
|
priv->mtd.read = s512_read;
|
|
|
|
priv->mtd.ioctl = s512_ioctl;
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "sector512";
|
2014-03-25 00:45:45 +01:00
|
|
|
|
|
|
|
priv->dev = mtd;
|
|
|
|
priv->eblocksize = geo.erasesize;
|
|
|
|
priv->neblocks = geo.neraseblocks;
|
|
|
|
priv->sectperblock = geo.erasesize / geo.blocksize;
|
|
|
|
priv->stdperblock = geo.erasesize >> 9;
|
|
|
|
|
|
|
|
/* Allocate a buffer for the erase block cache */
|
|
|
|
|
2014-09-01 01:26:36 +02:00
|
|
|
priv->eblock = (FAR uint8_t *)kmm_malloc(priv->eblocksize);
|
2014-03-25 00:45:45 +01:00
|
|
|
if (!priv->eblock)
|
|
|
|
{
|
2020-12-03 00:16:58 +01:00
|
|
|
/* Allocation failed! Discard all of that work we just did and
|
|
|
|
* return NULL
|
|
|
|
*/
|
2014-03-25 00:45:45 +01:00
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Allocation failed\n");
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2014-03-25 00:45:45 +01:00
|
|
|
priv = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return %p\n", priv);
|
2014-03-25 00:45:45 +01:00
|
|
|
return &priv->mtd;
|
|
|
|
}
|