* drivers/mtd/sst25xx.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/signal.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/spi/spi.h>
#include <nuttx/mtd/mtd.h>
* Pre-processor Definitions
****************************************************************************/
* (CPOL=0 and CPHA=0) or mode 3 (CPOL=1 and CPHA=1). So you may need to
* specify CONFIG_SST25XX_SPIMODE to select the best mode for your device.
* If CONFIG_SST25XX_SPIMODE is not defined, mode 0 will be used.
*/
#ifndef CONFIG_SST25XX_SPIMODE
# define CONFIG_SST25XX_SPIMODE SPIDEV_MODE0
#endif
#ifndef CONFIG_SST25XX_SPIFREQUENCY
# define CONFIG_SST25XX_SPIFREQUENCY 20000000
#endif
* manufacturer ID for the SST serial FLASH.
*/
#ifndef CONFIG_SST25XX_MANUFACTURER
# define CONFIG_SST25XX_MANUFACTURER 0xBF
#endif
#ifndef CONFIG_SST25XX_MEMORY_TYPE
# define CONFIG_SST25XX_MEMORY_TYPE 0x25
#endif
#define SST25_MANUFACTURER CONFIG_SST25XX_MANUFACTURER
#define SST25_MEMORY_TYPE CONFIG_SST25XX_MEMORY_TYPE
#define SST25_SST25064_CAPACITY 0x4b
* (2,0548 sectors) * (4,096 bytes per sector)
* (32,768 pages) * (256 bytes per page)
*/
#define SST25_SST25064_SECTOR_SHIFT 12
#define SST25_SST25064_NSECTORS 2048
#define SST25_SST25064_PAGE_SHIFT 8
#define SST25_SST25064_NPAGES 32768
*/
#define SST25_WREN 0x06
#define SST25_WRDI 0x04
#define SST25_RDID 0x9f
#define SST25_RDSR 0x05
#define SST25_EWSR 0x50
#define SST25_WRSR 0x01
#define SST25_READ 0x03
#define SST25_FAST_READ 0x0b
#define SST25_PP 0x02
#define SST25_SE 0x20
#define SST25_BE32 0x52
#define SST25_BE64 0xD8
#define SST25_BE 0xc7
#define SST25_RES 0xab
* NOTE 2: In SST25064 terminology, 0x52 and 0xd8 are block erase and 0x20
* is a sector erase. Block erase provides a faster way to erase
* multiple 4K sectors at once.
*/
#define SST25_SR_WIP (1 << 0)
#define SST25_SR_WEL (1 << 1)
#define SST25_SR_BP_SHIFT (2)
#define SST25_SR_BP_MASK (15 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_NONE (0 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_UPPER128th (1 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_UPPER64th (2 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_UPPER32nd (3 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_UPPER16th (4 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_UPPER8th (5 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_UPPERQTR (6 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_UPPERHALF (7 << SST25_SR_BP_SHIFT)
# define SST25_SR_BP_ALL (8 << SST25_SR_BP_SHIFT)
#define SST_SR_SEC (1 << 6)
#define SST25_SR_SRWD (1 << 7)
#define SST25_DUMMY 0xa5
* Private Types
****************************************************************************/
* must appear at the beginning of the definition so that you can freely
* cast between pointers to struct mtd_dev_s and struct sst25xx_dev_s.
*/
struct sst25xx_dev_s
{
struct mtd_dev_s mtd;
FAR struct spi_dev_s *dev;
uint8_t sectorshift;
uint8_t pageshift;
uint16_t nsectors;
uint32_t npages;
uint8_t lastwaswrite;
};
* Private Function Prototypes
****************************************************************************/
static void sst25xx_lock(FAR struct spi_dev_s *dev);
static inline void sst25xx_unlock(FAR struct spi_dev_s *dev);
static inline int sst25xx_readid(struct sst25xx_dev_s *priv);
static void sst25xx_waitwritecomplete(struct sst25xx_dev_s *priv);
static void sst25xx_writeenable(struct sst25xx_dev_s *priv);
static inline void sst25xx_sectorerase(struct sst25xx_dev_s *priv,
off_t offset, uint8_t type);
static inline int sst25xx_bulkerase(struct sst25xx_dev_s *priv);
static inline void sst25xx_pagewrite(struct sst25xx_dev_s *priv,
FAR const uint8_t *buffer,
off_t offset);
static int sst25xx_erase(FAR struct mtd_dev_s *dev, off_t startblock,
size_t nblocks);
static ssize_t sst25xx_bread(FAR struct mtd_dev_s *dev, off_t startblock,
size_t nblocks, FAR uint8_t *buf);
static ssize_t sst25xx_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
size_t nblocks, FAR const uint8_t *buf);
static ssize_t sst25xx_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 sst25xx_write(FAR struct mtd_dev_s *dev, off_t offset,
size_t nbytes,
FAR const uint8_t *buffer);
#endif
static int sst25xx_ioctl(FAR struct mtd_dev_s *dev, int cmd,
unsigned long arg);
* Private Data
****************************************************************************/
* Private Functions
****************************************************************************/
* Name: sst25xx_lock
****************************************************************************/
static void sst25xx_lock(FAR struct spi_dev_s *dev)
{
* lock SPI to have exclusive access to the buses for a sequence of
* transfers. The bus should be locked before the chip is selected.
*
* This is a blocking call and will not return until we have exclusive
* access to the SPI bus. We will retain that exclusive access until
* the bus is unlocked.
*/
SPI_LOCK(dev, true);
* setbits, and setmode methods to make sure that the SPI is properly
* configured for the device.
* If the SPI bus is being shared, then it may have been left in an
* incompatible state.
*/
SPI_SETMODE(dev, CONFIG_SST25XX_SPIMODE);
SPI_SETBITS(dev, 8);
SPI_HWFEATURES(dev, 0);
SPI_SETFREQUENCY(dev, CONFIG_SST25XX_SPIFREQUENCY);
}
* Name: sst25xx_unlock
****************************************************************************/
static inline void sst25xx_unlock(FAR struct spi_dev_s *dev)
{
SPI_LOCK(dev, false);
}
* Name: sst25xx_readid
****************************************************************************/
static inline int sst25xx_readid(struct sst25xx_dev_s *priv)
{
uint16_t manufacturer;
uint16_t memory;
uint16_t capacity;
finfo("priv: %p\n", priv);
sst25xx_lock(priv->dev);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_RDID);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
manufacturer = SPI_SEND(priv->dev, SST25_DUMMY);
memory = SPI_SEND(priv->dev, SST25_DUMMY);
capacity = SPI_SEND(priv->dev, SST25_DUMMY);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
sst25xx_unlock(priv->dev);
finfo("manufacturer: %02x memory: %02x capacity: %02x\n",
manufacturer, memory, capacity);
if (manufacturer == SST25_MANUFACTURER && memory == SST25_MEMORY_TYPE)
{
if (capacity == SST25_SST25064_CAPACITY)
{
priv->sectorshift = SST25_SST25064_SECTOR_SHIFT;
priv->nsectors = SST25_SST25064_NSECTORS;
priv->pageshift = SST25_SST25064_PAGE_SHIFT;
priv->npages = SST25_SST25064_NPAGES;
return OK;
}
}
return -ENODEV;
}
* Name: sst25xx_waitwritecomplete
****************************************************************************/
static void sst25xx_waitwritecomplete(struct sst25xx_dev_s *priv)
{
uint8_t status;
#if 0
if (!priv->lastwaswrite)
{
return;
}
#endif
do
{
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_RDSR);
* status
*/
status = SPI_SEND(priv->dev, SST25_DUMMY);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
* erasing could take more. The following short delay in the "busy"
* case will allow other peripherals to access the SPI bus.
*/
if ((status & SST25_SR_WIP) != 0)
{
sst25xx_unlock(priv->dev);
nxsig_usleep(1000);
sst25xx_lock(priv->dev);
}
}
while ((status & SST25_SR_WIP) != 0);
priv->lastwaswrite = false;
finfo("Complete\n");
}
* Name: sst25xx_writeenable
****************************************************************************/
static void sst25xx_writeenable(struct sst25xx_dev_s *priv)
{
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_WREN);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
finfo("Enabled\n");
}
* Name: sst25xx_unprotect
****************************************************************************/
static void sst25xx_unprotect(struct sst25xx_dev_s *priv)
{
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_EWSR);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_WRSR);
SPI_SEND(priv->dev, 0);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
}
* Name: sst25xx_sectorerase
****************************************************************************/
static void sst25xx_sectorerase(struct sst25xx_dev_s *priv, off_t sector,
uint8_t type)
{
off_t offset;
offset = sector << priv->sectorshift;
finfo("sector: %08lx\n", (long)sector);
* perform this wait at the end of each write operation (rather than at
* the beginning of ALL operations), but have the wait first will slightly
* improve performance.
*/
sst25xx_waitwritecomplete(priv);
sst25xx_writeenable(priv);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
* that was passed in as the erase type.
*/
SPI_SEND(priv->dev, type);
* parts, the sector number is completely contained in the first byte
* and the values used in the following two bytes don't really matter.
*/
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
SPI_SEND(priv->dev, offset & 0xff);
priv->lastwaswrite = true;
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
finfo("Erased\n");
}
* Name: sst25xx_bulkerase
****************************************************************************/
static inline int sst25xx_bulkerase(struct sst25xx_dev_s *priv)
{
finfo("priv: %p\n", priv);
* perform this wait at the end of each write operation (rather than at
* the beginning of ALL operations), but have the wait first will slightly
* improve performance.
*/
sst25xx_waitwritecomplete(priv);
sst25xx_writeenable(priv);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_BE);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
sst25xx_waitwritecomplete(priv);
finfo("Return: OK\n");
return OK;
}
* Name: sst25xx_pagewrite
****************************************************************************/
static inline void sst25xx_pagewrite(struct sst25xx_dev_s *priv,
FAR const uint8_t *buffer,
off_t page)
{
off_t offset = page << priv->pageshift;
finfo("page: %08lx offset: %08lx\n", (long)page, (long)offset);
* perform this wait at the end of each write operation (rather than at
* the beginning of ALL operations), but have the wait first will slightly
* improve performance.
*/
sst25xx_waitwritecomplete(priv);
sst25xx_writeenable(priv);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_PP);
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
SPI_SEND(priv->dev, offset & 0xff);
SPI_SNDBLOCK(priv->dev, buffer, 1 << priv->pageshift);
priv->lastwaswrite = true;
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
finfo("Written\n");
}
* Name: sst25xx_bytewrite
****************************************************************************/
#ifdef CONFIG_MTD_BYTE_WRITE
static inline void sst25xx_bytewrite(struct sst25xx_dev_s *priv,
FAR const uint8_t *buffer, off_t offset,
uint16_t count)
{
finfo("offset: %08lx count:%d\n", (long)offset, count);
* perform this wait at the end of each write operation (rather than at
* the beginning of ALL operations), but have the wait first will slightly
* improve performance.
*/
sst25xx_waitwritecomplete(priv);
sst25xx_writeenable(priv);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_PP);
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
SPI_SEND(priv->dev, offset & 0xff);
SPI_SNDBLOCK(priv->dev, buffer, count);
priv->lastwaswrite = true;
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
finfo("Written\n");
}
#endif
* Name: sst25xx_erase
****************************************************************************/
static int sst25xx_erase(FAR struct mtd_dev_s *dev, off_t startblock,
size_t nblocks)
{
FAR struct sst25xx_dev_s *priv = (FAR struct sst25xx_dev_s *)dev;
size_t blocksleft = nblocks;
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
sst25xx_lock(priv->dev);
while (blocksleft > 0)
{
size_t sectorboundry;
size_t blkper;
* on the largest unit we can use given the startblock and nblocks.
* This will reduce erase time (in the event we have partitions
* enabled and are doing a bulk erase which is translated into
* a block erase operation).
*/
blkper = 64 / 4;
sectorboundry = (startblock + blkper - 1) / blkper;
sectorboundry *= blkper;
* of blocks left to erase, then we can do a full sector erase.
*/
if (startblock == sectorboundry && blocksleft >= blkper)
{
sst25xx_sectorerase(priv, startblock, SST25_BE64);
startblock += blkper;
blocksleft -= blkper;
continue;
}
blkper = 32 / 4;
sectorboundry = (startblock + blkper - 1) / blkper;
sectorboundry *= blkper;
if (startblock == sectorboundry && blocksleft >= blkper)
{
sst25xx_sectorerase(priv, startblock, SST25_BE32);
startblock += blkper;
blocksleft -= blkper;
continue;
}
else
{
sst25xx_sectorerase(priv, startblock, SST25_SE);
startblock++;
blocksleft--;
continue;
}
}
sst25xx_unlock(priv->dev);
return (int)nblocks;
}
* Name: sst25xx_bread
****************************************************************************/
static ssize_t sst25xx_bread(FAR struct mtd_dev_s *dev, off_t startblock,
size_t nblocks,
FAR uint8_t *buffer)
{
FAR struct sst25xx_dev_s *priv = (FAR struct sst25xx_dev_s *)dev;
ssize_t nbytes;
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
* byte-oriented read
*/
nbytes = sst25xx_read(dev, startblock << priv->pageshift,
nblocks << priv->pageshift, buffer);
if (nbytes > 0)
{
return nbytes >> priv->pageshift;
}
return (int)nbytes;
}
* Name: sst25xx_bwrite
****************************************************************************/
static ssize_t sst25xx_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
size_t nblocks,
FAR const uint8_t *buffer)
{
FAR struct sst25xx_dev_s *priv = (FAR struct sst25xx_dev_s *)dev;
size_t blocksleft = nblocks;
size_t pagesize = 1 << priv->pageshift;
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
sst25xx_lock(priv->dev);
while (blocksleft-- > 0)
{
sst25xx_pagewrite(priv, buffer, startblock);
buffer += pagesize;
startblock++;
}
sst25xx_unlock(priv->dev);
return nblocks;
}
* Name: sst25xx_read
****************************************************************************/
static ssize_t sst25xx_read(FAR struct mtd_dev_s *dev, off_t offset,
size_t nbytes,
FAR uint8_t *buffer)
{
FAR struct sst25xx_dev_s *priv = (FAR struct sst25xx_dev_s *)dev;
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
* sst25xx_waitwritecomplete must be executed with the bus locked.
*/
sst25xx_lock(priv->dev);
* perform this wait at the end of each write operation (rather than at
* the beginning of ALL operations), but have the wait first will slightly
* improve performance.
*/
if (priv->lastwaswrite)
{
sst25xx_waitwritecomplete(priv);
}
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
SPI_SEND(priv->dev, SST25_READ);
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
SPI_SEND(priv->dev, offset & 0xff);
SPI_RECVBLOCK(priv->dev, buffer, nbytes);
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
sst25xx_unlock(priv->dev);
finfo("return nbytes: %d\n", (int)nbytes);
return nbytes;
}
* Name: sst25xx_write
****************************************************************************/
#ifdef CONFIG_MTD_BYTE_WRITE
static ssize_t sst25xx_write(FAR struct mtd_dev_s *dev, off_t offset,
size_t nbytes,
FAR const uint8_t *buffer)
{
FAR struct sst25xx_dev_s *priv = (FAR struct sst25xx_dev_s *)dev;
int startpage;
int endpage;
int count;
int index;
int pagesize;
int bytestowrite;
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
* and perform individual writes. The devices can only write in
* page increments.
*/
startpage = offset / (1 << priv->pageshift);
endpage = (offset + nbytes) / (1 << priv->pageshift);
sst25xx_lock(priv->dev);
if (startpage == endpage)
{
sst25xx_bytewrite(priv, buffer, offset, nbytes);
}
else
{
count = nbytes;
pagesize = (1 << priv->pageshift);
bytestowrite = pagesize - (offset & (pagesize - 1));
sst25xx_bytewrite(priv, buffer, offset, bytestowrite);
offset += bytestowrite;
count -= bytestowrite;
index = bytestowrite;
while (count >= pagesize)
{
sst25xx_bytewrite(priv, &buffer[index], offset, pagesize);
offset += pagesize;
count -= pagesize;
index += pagesize;
}
if (count > 0)
{
sst25xx_bytewrite(priv, &buffer[index], offset, count);
}
priv->lastwaswrite = true;
}
sst25xx_unlock(priv->dev);
return nbytes;
}
#endif
* Name: sst25xx_ioctl
****************************************************************************/
static int sst25xx_ioctl(FAR struct mtd_dev_s *dev, int cmd,
unsigned long arg)
{
FAR struct sst25xx_dev_s *priv = (FAR struct sst25xx_dev_s *)dev;
int ret = -EINVAL;
finfo("cmd: %d\n", cmd);
switch (cmd)
{
case MTDIOC_GEOMETRY:
{
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)
((uintptr_t)arg);
if (geo)
{
memset(geo, 0, sizeof(*geo));
* know the capacity and how to access the device.
*
* 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.
*/
geo->blocksize = (1 << priv->pageshift);
geo->erasesize = (1 << priv->sectorshift);
geo->neraseblocks = priv->nsectors;
ret = OK;
finfo("blocksize: %" PRId32 " erasesize: %" PRId32
" neraseblocks: %" PRId32 "\n",
geo->blocksize, geo->erasesize, geo->neraseblocks);
}
}
break;
case BIOC_PARTINFO:
{
FAR struct partition_info_s *info =
(FAR struct partition_info_s *)arg;
if (info != NULL)
{
info->numsectors = priv->nsectors *
(priv->sectorshift - priv->pageshift);
info->sectorsize = 1 << priv->pageshift;
info->startsector = 0;
info->parent[0] = '\0';
ret = OK;
}
}
break;
case MTDIOC_BULKERASE:
{
sst25xx_lock(priv->dev);
ret = sst25xx_bulkerase(priv);
sst25xx_unlock(priv->dev);
}
break;
default:
ret = -ENOTTY;
break;
}
finfo("return %d\n", ret);
return ret;
}
* Public Functions
****************************************************************************/
* Name: sst25xx_initialize
*
* Description:
* Create an initialize MTD device instance. 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).
*
****************************************************************************/
FAR struct mtd_dev_s *sst25xx_initialize(FAR struct spi_dev_s *dev)
{
FAR struct sst25xx_dev_s *priv;
int ret;
finfo("dev: %p\n", dev);
* a fixed, static allocation so that we can handle multiple FLASH devices.
* The current implementation would handle only one FLASH part per SPI
* 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.
*/
priv = (FAR struct sst25xx_dev_s *)
kmm_zalloc(sizeof(struct sst25xx_dev_s));
if (priv)
{
* nullified by kmm_zalloc).
*/
priv->mtd.erase = sst25xx_erase;
priv->mtd.bread = sst25xx_bread;
priv->mtd.bwrite = sst25xx_bwrite;
priv->mtd.read = sst25xx_read;
#ifdef CONFIG_MTD_BYTE_WRITE
priv->mtd.write = sst25xx_write;
#endif
priv->mtd.ioctl = sst25xx_ioctl;
priv->mtd.name = "sst25xx";
priv->dev = dev;
priv->lastwaswrite = false;
SPI_SELECT(dev, SPIDEV_FLASH(0), false);
ret = sst25xx_readid(priv);
if (ret != OK)
{
* NULL
*/
ferr("ERROR: Unrecognized\n");
kmm_free(priv);
return NULL;
}
else
{
* into it
*/
sst25xx_unprotect(priv);
}
}
finfo("Return %p\n", priv);
return (FAR struct mtd_dev_s *)priv;
}