2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* drivers/mtd/is25xp.c
|
|
|
|
* Driver for SPI-based IS25LPxx parts 32MBit and larger.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Marten Svanfeldt. All rights reserved.
|
|
|
|
*
|
2017-06-07 00:10:41 +02:00
|
|
|
* Copyright (C) 2009-2011, 2013, 2017 Gregory Nutt. All rights reserved.
|
2016-06-09 05:13:06 +02:00
|
|
|
* Author: Ken Pettit <pettitkd@gmail.com>
|
|
|
|
*
|
|
|
|
* Copied from / based on m25px.c and sst25.c drivers written by
|
|
|
|
* Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-06-09 05:13:06 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 17:36:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
/* Per the data sheet, IS25xP parts can be driven with either SPI mode 0
|
|
|
|
* (CPOL=0 and CPHA=0) or mode 3 (CPOL=1 and CPHA=1). So you may need to
|
|
|
|
* specify CONFIG_IS25XP_SPIMODE to select the best mode for your device.
|
|
|
|
* If CONFIG_IS25XP_SPIMODE is not defined, mode 0 will be used.
|
2016-06-09 05:13:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_IS25XP_SPIMODE
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CONFIG_IS25XP_SPIMODE SPIDEV_MODE0
|
2016-06-09 05:13:06 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SPI Frequency. May be up to 50MHz. */
|
|
|
|
|
|
|
|
#ifndef CONFIG_IS25XP_SPIFREQUENCY
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CONFIG_IS25XP_SPIFREQUENCY 20000000
|
2016-06-09 05:13:06 +02:00
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* IS25 Registers ***********************************************************/
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Identification register values */
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
#define IS25_MANUFACTURER 0x9d
|
|
|
|
#define IS25_MEMORY_TYPE 0x60
|
|
|
|
|
|
|
|
/* IS25LP064 capacity is 8,388,608 bytes:
|
|
|
|
* (2,048 sectors) * (4,096 bytes per sector)
|
|
|
|
* (32,768 pages) * (256 bytes per page)
|
|
|
|
*/
|
2016-06-09 17:36:06 +02:00
|
|
|
|
2016-06-09 05:13:06 +02:00
|
|
|
#define IS25_IS25LP064_CAPACITY 0x17
|
|
|
|
#define IS25_IS25LP064_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4,096 */
|
|
|
|
#define IS25_IS25LP064_NSECTORS 2048
|
|
|
|
#define IS25_IS25LP064_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define IS25_IS25LP064_NPAGES 32768
|
|
|
|
|
|
|
|
/* IS25LP128 capacity is 16,777,216 bytes:
|
|
|
|
* (4,096 sectors) * (4,096 bytes per sector)
|
|
|
|
* (65,536 pages) * (256 bytes per page)
|
|
|
|
*/
|
2016-06-09 17:36:06 +02:00
|
|
|
|
2016-06-09 05:13:06 +02:00
|
|
|
#define IS25_IS25LP128_CAPACITY 0x18
|
|
|
|
#define IS25_IS25LP128_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4,096 */
|
|
|
|
#define IS25_IS25LP128_NSECTORS 4096
|
|
|
|
#define IS25_IS25LP128_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define IS25_IS25LP128_NPAGES 65536
|
|
|
|
|
|
|
|
/* Instructions */
|
2021-01-27 16:48:40 +01:00
|
|
|
|
|
|
|
/* Command Value N Description Addr Dummy Data */
|
|
|
|
|
|
|
|
#define IS25_WREN 0x06 /* 1 Write Enable 0 0 0 */
|
|
|
|
#define IS25_WRDI 0x04 /* 1 Write Disable 0 0 0 */
|
|
|
|
#define IS25_RDID 0x9f /* 1 Read Identification 0 0 1-3 */
|
|
|
|
#define IS25_RDSR 0x05 /* 1 Read Status Register 0 0 >=1 */
|
|
|
|
|
|
|
|
/* #define IS25_EWSR 0x50 1 Write enable status 0 0 0 */
|
|
|
|
#define IS25_WRSR 0x01 /* 1 Write Status Register 0 0 1 */
|
|
|
|
#define IS25_READ 0x03 /* 1 Read Data Bytes 3 0 >=1 */
|
|
|
|
#define IS25_FAST_READ 0x0b /* 1 Higher speed read 3 1 >=1 */
|
|
|
|
#define IS25_PP 0x02 /* 1 Page Program 3 0 1-256 */
|
|
|
|
#define IS25_SE 0x20 /* 1 Sector Erase 3 0 0 */
|
|
|
|
#define IS25_BE32 0x52 /* 2 32K Block Erase 3 0 0 */
|
|
|
|
#define IS25_BE64 0xD8 /* 2 64K Block Erase 3 0 0 */
|
|
|
|
#define IS25_CER 0xC7 /* 1 Chip Erase 0 0 0 */
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* NOTE 1: All parts.
|
|
|
|
* NOTE 2: In IS25XP 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Status register bit definitions */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define IS25_SR_WIP (1 << 0) /* Bit 0: Write in progress bit */
|
|
|
|
#define IS25_SR_WEL (1 << 1) /* Bit 1: Write enable latch bit */
|
|
|
|
#define IS25_SR_BP_SHIFT (2) /* Bits 2-5: Block protect bits */
|
|
|
|
#define IS25_SR_BP_MASK (15 << IS25_SR_BP_SHIFT)
|
|
|
|
#define IS25_SR_BP_NONE (0 << IS25_SR_BP_SHIFT) /* Unprotected */
|
|
|
|
#define IS25_SR_BP_UPPER128th (1 << IS25_SR_BP_SHIFT) /* Upper 128th */
|
|
|
|
#define IS25_SR_BP_UPPER64th (2 << IS25_SR_BP_SHIFT) /* Upper 64th */
|
|
|
|
#define IS25_SR_BP_UPPER32nd (3 << IS25_SR_BP_SHIFT) /* Upper 32nd */
|
|
|
|
#define IS25_SR_BP_UPPER16th (4 << IS25_SR_BP_SHIFT) /* Upper 16th */
|
|
|
|
#define IS25_SR_BP_UPPER8th (5 << IS25_SR_BP_SHIFT) /* Upper 8th */
|
|
|
|
#define IS25_SR_BP_UPPERQTR (6 << IS25_SR_BP_SHIFT) /* Upper quarter */
|
|
|
|
#define IS25_SR_BP_UPPERHALF (7 << IS25_SR_BP_SHIFT) /* Upper half */
|
|
|
|
#define IS25_SR_BP_ALL (8 << IS25_SR_BP_SHIFT) /* All sectors */
|
|
|
|
#define IS25_SR_QE (1 << 6) /* Bit 6: Quad (QSPI) enable bit */
|
|
|
|
#define IS25_SR_SRWD (1 << 7) /* Bit 7: Status register write protect */
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
#define IS25_DUMMY 0xa5
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02: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 is25xp_dev_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct is25xp_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct spi_dev_s *dev; /* Saved SPI interface instance */
|
|
|
|
uint8_t sectorshift; /* 12 */
|
|
|
|
uint8_t pageshift; /* 8 */
|
|
|
|
uint16_t nsectors; /* 2,048 or 4,096 */
|
|
|
|
uint32_t npages; /* 32,768 or 65,536 */
|
|
|
|
uint8_t lastwaswrite; /* Indicates if last operation was write */
|
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static void is25xp_lock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline void is25xp_unlock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline int is25xp_readid(struct is25xp_dev_s *priv);
|
|
|
|
static void is25xp_waitwritecomplete(struct is25xp_dev_s *priv);
|
|
|
|
static void is25xp_writeenable(struct is25xp_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void is25xp_sectorerase(struct is25xp_dev_s *priv,
|
|
|
|
off_t offset,
|
|
|
|
uint8_t type);
|
2016-06-09 05:13:06 +02:00
|
|
|
static inline int is25xp_bulkerase(struct is25xp_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void is25xp_pagewrite(struct is25xp_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t offset);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int is25xp_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t is25xp_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buf);
|
|
|
|
static ssize_t is25xp_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buf);
|
|
|
|
static ssize_t is25xp_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR uint8_t *buffer);
|
2016-06-09 05:13:06 +02:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t is25xp_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR const uint8_t *buffer);
|
2016-06-09 05:13:06 +02:00
|
|
|
#endif
|
2021-01-27 16:48:40 +01:00
|
|
|
static int is25xp_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_lock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
static void is25xp_lock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* On SPI buses where there are multiple devices, it will be necessary to
|
|
|
|
* lock SPI to have exclusive access to the buses for a sequence of
|
2016-06-09 05:13:06 +02:00
|
|
|
* transfers. The bus should be locked before the chip is selected.
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
* 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.
|
2016-06-09 05:13:06 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* After locking the SPI bus, the we also need call the setfrequency,
|
|
|
|
* 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.
|
2016-06-09 05:13:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SETMODE(dev, CONFIG_IS25XP_SPIMODE);
|
|
|
|
SPI_SETBITS(dev, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(dev, 0);
|
|
|
|
SPI_SETFREQUENCY(dev, CONFIG_IS25XP_SPIFREQUENCY);
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_unlock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
static inline void is25xp_unlock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, false);
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_readid
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
static inline int is25xp_readid(struct is25xp_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint16_t manufacturer;
|
|
|
|
uint16_t memory;
|
|
|
|
uint16_t capacity;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("priv: %p\n", priv);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus, configure the bus, and select this FLASH part. */
|
|
|
|
|
|
|
|
is25xp_lock(priv->dev);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send the "Read ID (RDID)" command and read the first three ID bytes */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, IS25_RDID);
|
2016-06-09 05:13:06 +02:00
|
|
|
manufacturer = SPI_SEND(priv->dev, IS25_DUMMY);
|
|
|
|
memory = SPI_SEND(priv->dev, IS25_DUMMY);
|
|
|
|
capacity = SPI_SEND(priv->dev, IS25_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and unlock the bus */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-09 05:13:06 +02:00
|
|
|
is25xp_unlock(priv->dev);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("manufacturer: %02x memory: %02x capacity: %02x\n",
|
2016-06-09 05:13:06 +02:00
|
|
|
manufacturer, memory, capacity);
|
|
|
|
|
|
|
|
/* Check for a valid manufacturer and memory type */
|
|
|
|
|
|
|
|
if (manufacturer == IS25_MANUFACTURER && memory == IS25_MEMORY_TYPE)
|
|
|
|
{
|
|
|
|
/* Okay.. is it a FLASH capacity that we understand? */
|
|
|
|
|
|
|
|
if (capacity == IS25_IS25LP064_CAPACITY)
|
|
|
|
{
|
|
|
|
/* Save the FLASH geometry */
|
|
|
|
|
|
|
|
priv->sectorshift = IS25_IS25LP064_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = IS25_IS25LP064_NSECTORS;
|
|
|
|
priv->pageshift = IS25_IS25LP064_PAGE_SHIFT;
|
|
|
|
priv->npages = IS25_IS25LP064_NPAGES;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (capacity == IS25_IS25LP128_CAPACITY)
|
|
|
|
{
|
|
|
|
/* Save the FLASH geometry */
|
|
|
|
|
|
|
|
priv->sectorshift = IS25_IS25LP128_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = IS25_IS25LP128_NSECTORS;
|
|
|
|
priv->pageshift = IS25_IS25LP128_PAGE_SHIFT;
|
|
|
|
priv->npages = IS25_IS25LP128_NPAGES;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_waitwritecomplete
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
static void is25xp_waitwritecomplete(struct is25xp_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
/* No need to check if no write / erase was done */
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (!priv->lastwaswrite)
|
2016-06-09 17:36:06 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-06-09 05:13:06 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Are we the only device on the bus? */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SPI_OWNBUS
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send "Read Status Register (RDSR)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, IS25_RDSR);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Loop as long as the memory is busy with a write cycle */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Send a dummy byte to generate the clock needed to shift out
|
|
|
|
* the status
|
|
|
|
*/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
status = SPI_SEND(priv->dev, IS25_DUMMY);
|
|
|
|
}
|
|
|
|
while ((status & IS25_SR_WIP) != 0);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* Loop as long as the memory is busy with a write cycle */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send "Read Status Register (RDSR)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, IS25_RDSR);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Send a dummy byte to generate the clock needed to shift out
|
|
|
|
* the status
|
|
|
|
*/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
status = SPI_SEND(priv->dev, IS25_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Given that writing could take up to few tens of milliseconds,
|
|
|
|
* and erasing could take more. The following short delay in the
|
|
|
|
* "busy" case will allow other peripherals to access the SPI bus.
|
2016-06-09 05:13:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ((status & IS25_SR_WIP) != 0)
|
|
|
|
{
|
|
|
|
is25xp_unlock(priv->dev);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2016-06-09 05:13:06 +02:00
|
|
|
is25xp_lock(priv->dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((status & IS25_SR_WIP) != 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
priv->lastwaswrite = false;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Complete\n");
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_writeenable
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
static void is25xp_writeenable(struct is25xp_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send "Write Enable (WREN)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, IS25_WREN);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Enabled\n");
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_unprotect
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
static void is25xp_unprotect(struct is25xp_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Make writeable */
|
|
|
|
|
|
|
|
is25xp_writeenable(priv);
|
|
|
|
|
|
|
|
/* Send "Write status (WRSR)" */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
SPI_SEND(priv->dev, IS25_WRSR);
|
|
|
|
|
|
|
|
/* Followed by the new status value */
|
|
|
|
|
|
|
|
SPI_SEND(priv->dev, 0);
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_sectorerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static void is25xp_sectorerase(struct is25xp_dev_s *priv,
|
|
|
|
off_t sector,
|
|
|
|
uint8_t type)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
offset = sector << priv->sectorshift;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %08lx\n", (long)sector);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
is25xp_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
is25xp_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send the "Sector Erase (SE)" or Sub-Sector Erase (SSE) instruction
|
|
|
|
* that was passed in as the erase type.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, type);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send the sector offset high byte first. For all of the supported
|
|
|
|
* parts, the sector number is completely contained in the first byte
|
|
|
|
* and the values used in the following two bytes don't really matter.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, offset & 0xff);
|
2016-06-09 05:13:06 +02:00
|
|
|
priv->lastwaswrite = true;
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Erased\n");
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_bulkerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
static inline int is25xp_bulkerase(struct is25xp_dev_s *priv)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("priv: %p\n", priv);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
is25xp_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
is25xp_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send the "Chip Erase (CER)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, IS25_CER);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-09 05:13:06 +02:00
|
|
|
is25xp_waitwritecomplete(priv);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return: OK\n");
|
2016-06-09 05:13:06 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_pagewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void is25xp_pagewrite(struct is25xp_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t page)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
|
|
|
off_t offset = page << priv->pageshift;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("page: %08lx offset: %08lx\n", (long)page, (long)offset);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
is25xp_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Enable the write access to the FLASH */
|
|
|
|
|
|
|
|
is25xp_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send "Page Program (PP)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, IS25_PP);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send the page offset high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, offset & 0xff);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Then write the specified number of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, 1 << priv->pageshift);
|
|
|
|
priv->lastwaswrite = true;
|
|
|
|
|
|
|
|
/* Deselect the FLASH: Chip Select high */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Written\n");
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_bytewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2017-06-07 00:10:41 +02:00
|
|
|
static inline void is25xp_bytewrite(struct is25xp_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, off_t offset,
|
|
|
|
uint16_t count)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx count:%d\n", (long)offset, count);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
is25xp_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Enable the write access to the FLASH */
|
|
|
|
|
|
|
|
is25xp_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send "Page Program (PP)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, IS25_PP);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send the page offset high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, offset & 0xff);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Then write the specified number of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, count);
|
|
|
|
priv->lastwaswrite = true;
|
|
|
|
|
|
|
|
/* Deselect the FLASH: Chip Select high */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Written\n");
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int is25xp_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
|
|
|
FAR struct is25xp_dev_s *priv = (FAR struct is25xp_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Lock access to the SPI bus until we complete the erase */
|
|
|
|
|
|
|
|
is25xp_lock(priv->dev);
|
|
|
|
while (blocksleft > 0)
|
|
|
|
{
|
|
|
|
size_t sectorboundry;
|
|
|
|
size_t blkper;
|
|
|
|
|
|
|
|
/* We will erase in either 4K sectors or 32K or 64K blocks depending
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Test for 64K alignment */
|
|
|
|
|
|
|
|
blkper = 64 / 4;
|
|
|
|
sectorboundry = (startblock + blkper - 1) / blkper;
|
|
|
|
sectorboundry *= blkper;
|
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* If we are on a sector boundary and have at least a full sector
|
2016-06-09 05:13:06 +02:00
|
|
|
* of blocks left to erase, then we can do a full sector erase.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (startblock == sectorboundry && blocksleft >= blkper)
|
|
|
|
{
|
|
|
|
/* Do a 64k block erase */
|
|
|
|
|
|
|
|
is25xp_sectorerase(priv, startblock, IS25_BE64);
|
|
|
|
startblock += blkper;
|
|
|
|
blocksleft -= blkper;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for 32K block alignment */
|
|
|
|
|
|
|
|
blkper = 32 / 4;
|
|
|
|
sectorboundry = (startblock + blkper - 1) / blkper;
|
|
|
|
sectorboundry *= blkper;
|
|
|
|
|
|
|
|
if (startblock == sectorboundry && blocksleft >= blkper)
|
|
|
|
{
|
|
|
|
/* Do a 32k block erase */
|
|
|
|
|
|
|
|
is25xp_sectorerase(priv, startblock, IS25_BE32);
|
|
|
|
startblock += blkper;
|
|
|
|
blocksleft -= blkper;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just do a sector erase */
|
|
|
|
|
|
|
|
is25xp_sectorerase(priv, startblock, IS25_SE);
|
|
|
|
startblock++;
|
|
|
|
blocksleft--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
is25xp_unlock(priv->dev);
|
|
|
|
return (int)nblocks;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t is25xp_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR uint8_t *buffer)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
|
|
|
FAR struct is25xp_dev_s *priv = (FAR struct is25xp_dev_s *)dev;
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* On this device, we can handle the block read just like the byte-oriented
|
|
|
|
* read
|
|
|
|
*/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
nbytes = is25xp_read(dev,
|
|
|
|
startblock << priv->pageshift,
|
|
|
|
nblocks << priv->pageshift,
|
|
|
|
buffer);
|
2016-06-09 05:13:06 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
return nbytes >> priv->pageshift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t is25xp_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR const uint8_t *buffer)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
|
|
|
FAR struct is25xp_dev_s *priv = (FAR struct is25xp_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
|
|
|
size_t pagesize = 1 << priv->pageshift;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus and write each page to FLASH */
|
|
|
|
|
|
|
|
is25xp_lock(priv->dev);
|
|
|
|
while (blocksleft-- > 0)
|
|
|
|
{
|
|
|
|
is25xp_pagewrite(priv, buffer, startblock);
|
|
|
|
buffer += pagesize;
|
|
|
|
startblock++;
|
2021-01-27 16:48:40 +01:00
|
|
|
}
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
is25xp_unlock(priv->dev);
|
|
|
|
return nblocks;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t is25xp_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR uint8_t *buffer)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
|
|
|
FAR struct is25xp_dev_s *priv = (FAR struct is25xp_dev_s *)dev;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2017-06-12 17:51:42 +02:00
|
|
|
/* Lock the SPI bus NOW because the following call must be executed with
|
|
|
|
* the bus locked.
|
|
|
|
*/
|
|
|
|
|
|
|
|
is25xp_lock(priv->dev);
|
|
|
|
|
2016-06-09 05:13:06 +02:00
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
is25xp_waitwritecomplete(priv);
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:51:42 +02:00
|
|
|
/* Select this FLASH part */
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send "Read from Memory " instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, IS25_READ);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Send the page offset high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (offset >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, offset & 0xff);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Then read all of the requested bytes */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->dev, buffer, nbytes);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and unlock the SPI bus */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2016-06-09 05:13:06 +02:00
|
|
|
is25xp_unlock(priv->dev);
|
2017-06-12 17:51:42 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return nbytes: %d\n", (int)nbytes);
|
2016-06-09 05:13:06 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_write
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t is25xp_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR const uint8_t *buffer)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
|
|
|
FAR struct is25xp_dev_s *priv = (FAR struct is25xp_dev_s *)dev;
|
|
|
|
int startpage;
|
|
|
|
int endpage;
|
|
|
|
int count;
|
|
|
|
int index;
|
|
|
|
int pagesize;
|
|
|
|
int bytestowrite;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* We must test if the offset + count crosses one or more pages
|
|
|
|
* and perform individual writes. The devices can only write in
|
|
|
|
* page increments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
startpage = offset / (1 << priv->pageshift);
|
|
|
|
endpage = (offset + nbytes) / (1 << priv->pageshift);
|
|
|
|
|
2017-06-07 00:10:41 +02:00
|
|
|
is25xp_lock(priv->dev);
|
2016-06-09 05:13:06 +02:00
|
|
|
if (startpage == endpage)
|
|
|
|
{
|
|
|
|
/* All bytes within one programmable page. Just do the write. */
|
|
|
|
|
|
|
|
is25xp_bytewrite(priv, buffer, offset, nbytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Write the 1st partial-page */
|
|
|
|
|
|
|
|
count = nbytes;
|
|
|
|
pagesize = (1 << priv->pageshift);
|
2021-01-27 16:48:40 +01:00
|
|
|
bytestowrite = pagesize - (offset & (pagesize - 1));
|
2016-06-09 05:13:06 +02:00
|
|
|
is25xp_bytewrite(priv, buffer, offset, bytestowrite);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += bytestowrite;
|
|
|
|
count -= bytestowrite;
|
|
|
|
index = bytestowrite;
|
|
|
|
|
|
|
|
/* Write full pages */
|
|
|
|
|
|
|
|
while (count >= pagesize)
|
|
|
|
{
|
|
|
|
is25xp_bytewrite(priv, &buffer[index], offset, pagesize);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += pagesize;
|
|
|
|
count -= pagesize;
|
|
|
|
index += pagesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now write any partial page at the end */
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
is25xp_bytewrite(priv, &buffer[index], offset, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->lastwaswrite = true;
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:10:41 +02:00
|
|
|
is25xp_unlock(priv->dev);
|
2016-06-09 05:13:06 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MTD_BYTE_WRITE */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int is25xp_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg)
|
2016-06-09 05:13:06 +02:00
|
|
|
{
|
|
|
|
FAR struct is25xp_dev_s *priv = (FAR struct is25xp_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);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
FAR struct mtd_geometry_s *geo =
|
|
|
|
(FAR struct mtd_geometry_s *)((uintptr_t)arg);
|
2016-06-09 05:13:06 +02: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.
|
2016-06-09 05:13:06 +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.
|
2016-06-09 05:13:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
geo->blocksize = (1 << priv->pageshift);
|
|
|
|
geo->erasesize = (1 << priv->sectorshift);
|
|
|
|
geo->neraseblocks = priv->nsectors;
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("blocksize: %d erasesize: %d neraseblocks: %d\n",
|
2016-06-09 05:13:06 +02:00
|
|
|
geo->blocksize, geo->erasesize, geo->neraseblocks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
is25xp_lock(priv->dev);
|
|
|
|
ret = is25xp_bulkerase(priv);
|
|
|
|
is25xp_unlock(priv->dev);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return %d\n", ret);
|
2016-06-09 05:13:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-06-09 05:13:06 +02:00
|
|
|
* Name: is25xp_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Create an initialize MTD device instance. MTD devices are not registered
|
2016-06-09 05:13:06 +02:00
|
|
|
* 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).
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
FAR struct mtd_dev_s *is25xp_initialize(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct is25xp_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("dev: %p\n", dev);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* 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.
|
2016-06-09 05:13:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
priv = (FAR struct is25xp_dev_s *)kmm_zalloc(sizeof(struct is25xp_dev_s));
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated structure. (unsupported methods were
|
|
|
|
* nullified by kmm_zalloc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->mtd.erase = is25xp_erase;
|
|
|
|
priv->mtd.bread = is25xp_bread;
|
|
|
|
priv->mtd.bwrite = is25xp_bwrite;
|
|
|
|
priv->mtd.read = is25xp_read;
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
priv->mtd.write = is25xp_write;
|
|
|
|
#endif
|
|
|
|
priv->mtd.ioctl = is25xp_ioctl;
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "is25xp";
|
2016-06-09 05:13:06 +02:00
|
|
|
priv->dev = dev;
|
|
|
|
priv->lastwaswrite = false;
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(dev, SPIDEV_FLASH(0), false);
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
/* Identify the FLASH chip and get its capacity */
|
|
|
|
|
|
|
|
ret = is25xp_readid(priv);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Unrecognized! Discard all of that work we just did and
|
|
|
|
* return NULL
|
|
|
|
*/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Unrecognized\n");
|
2016-06-09 05:13:06 +02:00
|
|
|
kmm_free(priv);
|
2016-07-01 01:49:53 +02:00
|
|
|
return NULL;
|
2016-06-09 05:13:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Make sure that the FLASH is unprotected so that we can
|
|
|
|
* write into it
|
|
|
|
*/
|
2016-06-09 05:13:06 +02:00
|
|
|
|
|
|
|
is25xp_unprotect(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return %p\n", priv);
|
2016-06-09 05:13:06 +02:00
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|