2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* drivers/mtd/sst26.c
|
|
|
|
*
|
2021-04-10 08:50:57 +02: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
|
2016-05-10 19:09:15 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-10 19:09:15 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02: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.
|
2016-05-10 19:09:15 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Configuration ************************************************************/
|
2019-07-22 00:07:44 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Per the data sheet, SST26 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_SST26_SPIMODE to select the best mode
|
|
|
|
* for your device. If CONFIG_SST26_SPIMODE is not defined, mode 0 will be
|
|
|
|
* used.
|
2016-05-10 19:09:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_SST26_SPIMODE
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CONFIG_SST26_SPIMODE SPIDEV_MODE0
|
2016-05-10 19:09:15 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SPI Frequency. May be up to 104 MHz. */
|
|
|
|
|
|
|
|
#ifndef CONFIG_SST26_SPIFREQUENCY
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CONFIG_SST26_SPIFREQUENCY 20000000
|
2016-05-10 19:09:15 +02:00
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Various manufacturers may have produced the parts.
|
|
|
|
* 0xBF is the manufacturer ID for the SST serial FLASH.
|
2016-05-10 19:09:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_SST26_MANUFACTURER
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CONFIG_SST26_MANUFACTURER 0xBF
|
2016-05-10 19:09:15 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_SST26_MEMORY_TYPE
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CONFIG_SST26_MEMORY_TYPE 0x26
|
2016-05-10 19:09:15 +02:00
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* SST26 Registers **********************************************************/
|
2019-07-22 00:07:44 +02:00
|
|
|
|
|
|
|
/* Identification register values */
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
#define SST26_MANUFACTURER CONFIG_SST26_MANUFACTURER
|
|
|
|
#define SST26_MEMORY_TYPE CONFIG_SST26_MEMORY_TYPE
|
|
|
|
|
|
|
|
#define SST26_SST26VF016_CAPACITY 0x41 /* 16 M-bit */
|
|
|
|
#define SST26_SST26VF032_CAPACITY 0x42 /* 32 M-bit */
|
|
|
|
#define SST26_SST26VF064_CAPACITY 0x43 /* 64 M-bit */
|
|
|
|
|
|
|
|
/* SST26VF016 capacity is 2,097,152 bytes:
|
|
|
|
* (512 sectors) * (4,096 bytes per sector)
|
|
|
|
* (8192 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SST26_SST26VF016_SECTOR_SHIFT 12 /* Sector size 1 << 15 = 65,536 */
|
|
|
|
#define SST26_SST26VF016_NSECTORS 512
|
|
|
|
#define SST26_SST26VF016_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define SST26_SST26VF016_NPAGES 8192
|
|
|
|
|
|
|
|
/* SST26VF032 capacity is 4,194,304 bytes:
|
|
|
|
* (1,024 sectors) * (4,096 bytes per sector)
|
|
|
|
* (16,384 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SST26_SST26VF032_SECTOR_SHIFT 12 /* Sector size 1 << 15 = 65,536 */
|
|
|
|
#define SST26_SST26VF032_NSECTORS 1024
|
|
|
|
#define SST26_SST26VF032_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define SST26_SST26VF032_NPAGES 16384
|
|
|
|
|
|
|
|
/* SST26VF064 capacity is 8,388,608 bytes:
|
|
|
|
* (2,048 sectors) * (4,096 bytes per sector)
|
|
|
|
* (32,768 pages) * (256 bytes per page)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SST26_SST26VF064_SECTOR_SHIFT 12 /* Sector size 1 << 15 = 65,536 */
|
|
|
|
#define SST26_SST26VF064_NSECTORS 2048
|
|
|
|
#define SST26_SST26VF064_PAGE_SHIFT 8 /* Page size 1 << 8 = 256 */
|
|
|
|
#define SST26_SST26VF064_NPAGES 32768
|
|
|
|
|
|
|
|
/* Instructions */
|
2021-01-27 16:48:40 +01:00
|
|
|
|
|
|
|
/* Command Value NN Description Addr Dummy Data */
|
|
|
|
|
|
|
|
#define SST26_NOP 0x00 /* 14 No Operation 0 0 0 */
|
|
|
|
#define SST26_RSTEN 0x66 /* 14 Reset Enable 0 0 0 */
|
|
|
|
#define SST26_RST 0x99 /* 14 Reset Memory 0 0 0 */
|
|
|
|
#define SST26_EQIO 0x38 /* 1 Enable Quad I/O 0 0 0 */
|
|
|
|
#define SST26_RSTQIO 0xFF /* 4 Reset Quad I/O 0 0 0 */
|
|
|
|
#define SST26_RDSR 0x05 /* 1 Read Status Register 0 0 >=1 */
|
|
|
|
/* 4 Read Status Register 0 1 >=1 */
|
|
|
|
#define SST26_WRSR 0x01 /* 14 Write Status Register 0 0 2 */
|
|
|
|
#define SST26_RDCR 0x35 /* 1 Read Config Register 0 0 >=1 */
|
|
|
|
/* 4 Read Config Register 0 1 >=1 */
|
|
|
|
#define SST26_READ 0x03 /* 1 Read Data Bytes 3 0 >=1 */
|
|
|
|
#define SST26_FAST_READ 0x0b /* 1 Higher speed read 3 1 >=1 */
|
|
|
|
/* 4 Higher speed read 3 3 >=1 */
|
|
|
|
#define SST26_SQOR 0x6b /* 1 SQI Output Read 3 1 >=1 */
|
|
|
|
#define SST26_SQIOR 0xeb /* 1 SQI I/O Read 3 3 >=1 */
|
|
|
|
#define SST26_SDOR 0x3b /* 1 SDI Output Read 3 1 >=1 */
|
|
|
|
#define SST26_SDIOR 0xbb /* 1 SDI I/O Read 3 1 >=1 */
|
|
|
|
#define SST26_SB 0xc0 /* 14 Set Burst Length 0 0 1 */
|
|
|
|
#define SST26_RBSQI 0x0c /* 4 SQI Read Burst w/ Wrap 3 3 >=1 */
|
|
|
|
#define SST26_RBSPI 0xec /* 1 SPI Read Burst w/ Wrap 3 3 >=1 */
|
|
|
|
#define SST26_RDID 0x9f /* 1 Read Identification 0 0 >=3 */
|
|
|
|
#define SST26_QRDID 0xaf /* 4 Quad Read Identification 0 1 >=3 */
|
|
|
|
#define SST26_SFDP 0x5a /* 1 Serial Flash Discov. Par.3 1 >=1 */
|
|
|
|
#define SST26_WREN 0x06 /* 14 Write Enable 0 0 0 */
|
|
|
|
#define SST26_WRDI 0x04 /* 14 Write Disable 0 0 0 */
|
|
|
|
#define SST26_SE 0x20 /* 14 Sector Erase 3 0 0 */
|
|
|
|
#define SST26_BE 0xd8 /* 14 8/32/64K Block Erase 3 0 0 */
|
|
|
|
#define SST26_CE 0xc7 /* 14 Chip Erase 0 0 0 */
|
|
|
|
#define SST26_PP 0x02 /* 1 Page Program 3 0 1-256 */
|
|
|
|
#define SST26_QPP 0x32 /* 1 Quad Page Program 3 0 1-256 */
|
|
|
|
#define SST26_WRSU 0xb0 /* 14 Suspend Program/Erase 0 0 0 */
|
|
|
|
#define SST26_WRRE 0x30 /* 14 Resume Program/Erase 0 0 0 */
|
|
|
|
#define SST26_RBPR 0x72 /* 1 Read Block-Protection reg 0 0 1-18 */
|
|
|
|
/* 4 Read Block-Protection reg 0 1 1-18 */
|
|
|
|
#define SST26_WBPR 0x42 /* 14 Write Block-Protection reg 0 0 1-18 */
|
|
|
|
#define SST26_LBPR 0x8d /* 14 Lock down Block-Prot. reg 0 0 0 */
|
|
|
|
#define SST26_NVWLDR 0xe8 /* 14 non-Volatile Write L-D reg 0 0 1-18 */
|
|
|
|
#define SST26_ULBPR 0x98 /* 14 Global Block Protection unlock 0 0 0 */
|
|
|
|
#define SST26_RSID 0x88 /* 14 Read Security ID 2 1 1-2048 */
|
|
|
|
/* 4 Read Security ID 2 3 1-2048 */
|
|
|
|
#define SST26_PSID 0xa5 /* 14 Program User Security ID area 2 0 1-256 */
|
|
|
|
#define SST26_LSID 0x85 /* 14 Lockout Security ID programming 0 0 0 */
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* NOTE 1: All parts.
|
|
|
|
* NOTE 2: In SST26VF064 terminology, 0xd8 is 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 SST26_SR_WIP (1 << 0) /* Bit 0: Write in progress */
|
|
|
|
#define SST26_SR_WEL (1 << 1) /* Bit 1: Write enable latch */
|
|
|
|
#define SST26_SR_WSE (1 << 2) /* Bit 2: Write Suspend-Erase Status */
|
|
|
|
#define SST26_SR_WSP (1 << 3) /* Bit 3: Write Suspend-Program Status */
|
|
|
|
#define SST26_SR_WPLD (1 << 4) /* Bit 4: Write Protection Lock-Down Status */
|
|
|
|
#define SST26_SR_SEC (1 << 5) /* Bit 5: Security ID status */
|
|
|
|
#define SST26_SR_RES (1 << 6) /* Bit 6: RFU */
|
|
|
|
#define SST26_SR_WIP2 (1 << 7) /* Bit 7: Write in progress */
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
#define SST26_DUMMY 0xa5
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Debug ********************************************************************/
|
2016-05-25 21:43:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SST26_DEBUG
|
2023-05-20 00:32:34 +02:00
|
|
|
# define ssterr(format, ...) _err(format, ##__VA_ARGS__)
|
|
|
|
# define sstinfo(format, ...) _info(format, ##__VA_ARGS__)
|
2016-05-25 21:43:44 +02:00
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define ssterr(x...)
|
|
|
|
# define sstinfo(x...)
|
2016-05-25 21:43:44 +02:00
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +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 sst26_dev_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct sst26_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct spi_dev_s *dev; /* Saved SPI interface instance */
|
2021-11-30 17:26:24 +01:00
|
|
|
uint32_t npages;
|
|
|
|
uint16_t nsectors;
|
|
|
|
uint16_t devid; /* SPI device ID to manage CS lines in board */
|
2016-05-10 19:09:15 +02:00
|
|
|
uint8_t sectorshift;
|
|
|
|
uint8_t pageshift;
|
2021-11-30 17:26:24 +01:00
|
|
|
bool lastwaswrite;
|
2016-05-10 19:09:15 +02:00
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static void sst26_lock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline void sst26_unlock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline int sst26_readid(struct sst26_dev_s *priv);
|
|
|
|
static void sst26_waitwritecomplete(struct sst26_dev_s *priv);
|
|
|
|
static void sst26_writeenable(struct sst26_dev_s *priv);
|
2016-07-12 18:34:03 +02:00
|
|
|
static void sst26_writedisable(struct sst26_dev_s *priv);
|
2016-05-10 19:09:15 +02:00
|
|
|
static void sst26_globalunlock(struct sst26_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void sst26_sectorerase(struct sst26_dev_s *priv,
|
|
|
|
off_t offset,
|
2019-07-22 00:07:44 +02:00
|
|
|
uint8_t type);
|
2016-05-10 19:09:15 +02:00
|
|
|
static inline int sst26_chiperase(struct sst26_dev_s *priv);
|
2019-07-22 00:07:44 +02:00
|
|
|
static inline void sst26_pagewrite(struct sst26_dev_s *priv,
|
2021-01-27 16:48:40 +01:00
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t offset);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int sst26_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t sst26_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR uint8_t *buf);
|
|
|
|
static ssize_t sst26_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR const uint8_t *buf);
|
|
|
|
static ssize_t sst26_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2016-05-10 19:09:15 +02:00
|
|
|
FAR uint8_t *buffer);
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t sst26_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2016-05-10 19:09:15 +02:00
|
|
|
FAR const uint8_t *buffer);
|
|
|
|
#endif
|
2021-01-27 16:48:40 +01:00
|
|
|
static int sst26_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_lock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static void sst26_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-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* After locking the SPI bus, then we also need to 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-05-10 19:09:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SETMODE(dev, CONFIG_SST26_SPIMODE);
|
|
|
|
SPI_SETBITS(dev, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(dev, 0);
|
|
|
|
SPI_SETFREQUENCY(dev, CONFIG_SST26_SPIFREQUENCY);
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_unlock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static inline void sst26_unlock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, false);
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_readid
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static inline int sst26_readid(struct sst26_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint16_t manufacturer;
|
|
|
|
uint16_t memory;
|
|
|
|
uint16_t capacity;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("priv: %p\n", priv);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus, configure the bus, and select this FLASH part. */
|
|
|
|
|
|
|
|
sst26_lock(priv->dev);
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +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, SST26_RDID);
|
2016-05-10 19:09:15 +02:00
|
|
|
manufacturer = SPI_SEND(priv->dev, SST26_DUMMY);
|
|
|
|
memory = SPI_SEND(priv->dev, SST26_DUMMY);
|
|
|
|
capacity = SPI_SEND(priv->dev, SST26_DUMMY);
|
|
|
|
|
2019-07-22 00:07:44 +02:00
|
|
|
/* De-select the FLASH and unlock the bus */
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
sst26_unlock(priv->dev);
|
|
|
|
|
2017-01-15 20:00:50 +01:00
|
|
|
sstinfo("manufacturer: %02x memory: %02x capacity: %02x\n",
|
|
|
|
manufacturer, memory, capacity);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Check for a valid manufacturer and memory type */
|
|
|
|
|
|
|
|
if (manufacturer == SST26_MANUFACTURER && memory == SST26_MEMORY_TYPE)
|
|
|
|
{
|
|
|
|
/* Okay.. is it a FLASH capacity that we understand? */
|
|
|
|
|
|
|
|
if (capacity == SST26_SST26VF064_CAPACITY)
|
|
|
|
{
|
2019-07-22 00:07:44 +02:00
|
|
|
/* Save the FLASH geometry */
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2019-07-22 00:07:44 +02:00
|
|
|
priv->sectorshift = SST26_SST26VF064_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = SST26_SST26VF064_NSECTORS;
|
|
|
|
priv->pageshift = SST26_SST26VF064_PAGE_SHIFT;
|
|
|
|
priv->npages = SST26_SST26VF064_NPAGES;
|
|
|
|
return OK;
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
else if (capacity == SST26_SST26VF032_CAPACITY)
|
|
|
|
{
|
2019-07-22 00:07:44 +02:00
|
|
|
/* Save the FLASH geometry */
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2019-07-22 00:07:44 +02:00
|
|
|
priv->sectorshift = SST26_SST26VF032_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = SST26_SST26VF032_NSECTORS;
|
|
|
|
priv->pageshift = SST26_SST26VF032_PAGE_SHIFT;
|
|
|
|
priv->npages = SST26_SST26VF032_NPAGES;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (capacity == SST26_SST26VF016_CAPACITY)
|
|
|
|
{
|
|
|
|
/* Save the FLASH geometry */
|
|
|
|
|
|
|
|
priv->sectorshift = SST26_SST26VF016_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = SST26_SST26VF016_NSECTORS;
|
|
|
|
priv->pageshift = SST26_SST26VF016_PAGE_SHIFT;
|
|
|
|
priv->npages = SST26_SST26VF016_NPAGES;
|
|
|
|
return OK;
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_waitwritecomplete
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static void sst26_waitwritecomplete(struct sst26_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
/* Loop as long as the memory is busy with a write cycle */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send "Read Status Register (RDSR)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_RDSR);
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
status = SPI_SEND(priv->dev, SST26_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ((status & SST26_SR_WIP) != 0)
|
|
|
|
{
|
|
|
|
sst26_unlock(priv->dev);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2016-05-10 19:09:15 +02:00
|
|
|
sst26_lock(priv->dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((status & SST26_SR_WIP) != 0);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("Complete\n");
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_globalunlock
|
2021-01-27 16:48:40 +01:00
|
|
|
* Description: SST26 flashes are globally locked after startup.
|
|
|
|
* To allow writing, this command must be sent once.
|
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static void sst26_globalunlock(struct sst26_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2016-07-12 18:34:03 +02:00
|
|
|
/* Send "Global Unlock (ULBPR)" command */
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_ULBPR);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("Device unlocked.\n");
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_writeenable
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static void sst26_writeenable(struct sst26_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send "Write Enable (WREN)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_WREN);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("Enabled\n");
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-07-12 18:34:03 +02:00
|
|
|
* Name: sst26_writedisable
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-07-12 18:34:03 +02:00
|
|
|
|
|
|
|
static void sst26_writedisable(struct sst26_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-07-12 18:34:03 +02:00
|
|
|
|
|
|
|
/* Send "Write Disable (WRDI)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_WRDI);
|
2016-07-12 18:34:03 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-07-12 18:34:03 +02:00
|
|
|
|
|
|
|
sstinfo("Disabled\n");
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_sectorerase (4k)
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static void sst26_sectorerase(struct sst26_dev_s *priv,
|
|
|
|
off_t sector,
|
|
|
|
uint8_t type)
|
2016-05-10 19:09:15 +02:00
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
offset = sector << priv->sectorshift;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("sector: %08lx\n", (long)sector);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
sst26_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send the "Sector Erase (SE)" or "Block Erase (BE)" instruction
|
|
|
|
* that was passed in as the erase type.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, type);
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
sst26_waitwritecomplete(priv);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("Erased\n");
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_chiperase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static inline int sst26_chiperase(struct sst26_dev_s *priv)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("priv: %p\n", priv);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
sst26_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send the "Chip Erase (CE)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_CE);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
sst26_waitwritecomplete(priv);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("Return: OK\n");
|
2016-05-10 19:09:15 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_pagewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static inline void sst26_pagewrite(struct sst26_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, off_t page)
|
|
|
|
{
|
|
|
|
off_t offset = page << priv->pageshift;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("page: %08lx offset: %08lx\n", (long)page, (long)offset);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Enable the write access to the FLASH */
|
|
|
|
|
|
|
|
sst26_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send "Page Program (PP)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_PP);
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Then write the specified number of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, 1 << priv->pageshift);
|
|
|
|
|
|
|
|
/* Deselect the FLASH: Chip Select high */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
sst26_waitwritecomplete(priv);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("Written\n");
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_bytewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
static inline void sst26_bytewrite(struct sst26_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, off_t offset,
|
|
|
|
uint16_t count)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("offset: %08lx count:%d\n", (long)offset, count);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Enable the write access to the FLASH */
|
|
|
|
|
|
|
|
sst26_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send "Page Program (PP)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_PP);
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Then write the specified number of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, count);
|
|
|
|
priv->lastwaswrite = true;
|
|
|
|
|
|
|
|
/* Deselect the FLASH: Chip Select high */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
sst26_waitwritecomplete(priv);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("Written\n");
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Driver routines */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int sst26_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks)
|
2016-05-10 19:09:15 +02:00
|
|
|
{
|
|
|
|
FAR struct sst26_dev_s *priv = (FAR struct sst26_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Lock access to the SPI bus until we complete the erase */
|
|
|
|
|
|
|
|
sst26_lock(priv->dev);
|
|
|
|
while (blocksleft > 0)
|
|
|
|
{
|
|
|
|
/* SST26VF parts have complex block overlay structure for the moment
|
|
|
|
* we just erase in 4k blocks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sst26_sectorerase(priv, startblock, SST26_SE);
|
|
|
|
startblock++;
|
|
|
|
blocksleft--;
|
|
|
|
}
|
|
|
|
|
|
|
|
sst26_unlock(priv->dev);
|
|
|
|
return (int)nblocks;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static ssize_t sst26_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct sst26_dev_s *priv = (FAR struct sst26_dev_s *)dev;
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
nbytes = sst26_read(dev,
|
|
|
|
startblock << priv->pageshift,
|
|
|
|
nblocks << priv->pageshift,
|
2016-05-10 19:09:15 +02:00
|
|
|
buffer);
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
return nbytes >> priv->pageshift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2019-07-22 00:07:44 +02:00
|
|
|
static ssize_t sst26_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buffer)
|
2016-05-10 19:09:15 +02:00
|
|
|
{
|
|
|
|
FAR struct sst26_dev_s *priv = (FAR struct sst26_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
|
|
|
size_t pagesize = 1 << priv->pageshift;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus and write each page to FLASH */
|
|
|
|
|
|
|
|
sst26_lock(priv->dev);
|
|
|
|
while (blocksleft-- > 0)
|
|
|
|
{
|
|
|
|
sst26_pagewrite(priv, buffer, startblock);
|
|
|
|
buffer += pagesize;
|
|
|
|
startblock++;
|
2019-07-22 00:07:44 +02:00
|
|
|
}
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
sst26_unlock(priv->dev);
|
|
|
|
return nblocks;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t sst26_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR uint8_t *buffer)
|
2016-05-10 19:09:15 +02:00
|
|
|
{
|
|
|
|
FAR struct sst26_dev_s *priv = (FAR struct sst26_dev_s *)dev;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus and select this FLASH part */
|
|
|
|
|
|
|
|
sst26_lock(priv->dev);
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Send "Read from Memory " instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_FAST_READ);
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
|
2016-05-25 21:43:44 +02:00
|
|
|
/* Dummy read */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST26_DUMMY);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Then read all of the requested bytes */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->dev, buffer, nbytes);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and unlock the SPI bus */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
sst26_unlock(priv->dev);
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("return nbytes: %d\n", (int)nbytes);
|
2016-05-10 19:09:15 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_write
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t sst26_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR const uint8_t *buffer)
|
2016-05-10 19:09:15 +02:00
|
|
|
{
|
|
|
|
FAR struct sst26_dev_s *priv = (FAR struct sst26_dev_s *)dev;
|
|
|
|
int startpage;
|
|
|
|
int endpage;
|
|
|
|
int count;
|
|
|
|
int index;
|
|
|
|
int pagesize;
|
|
|
|
int bytestowrite;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2016-05-10 19:09:15 +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
|
|
|
sst26_lock(priv->dev);
|
2016-05-10 19:09:15 +02:00
|
|
|
if (startpage == endpage)
|
|
|
|
{
|
|
|
|
/* All bytes within one programmable page. Just do the write. */
|
|
|
|
|
|
|
|
sst26_bytewrite(priv, buffer, offset, nbytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Write the 1st partial-page */
|
|
|
|
|
|
|
|
count = nbytes;
|
|
|
|
pagesize = (1 << priv->pageshift);
|
2019-07-22 00:07:44 +02:00
|
|
|
bytestowrite = pagesize - (offset & (pagesize - 1));
|
2016-05-10 19:09:15 +02:00
|
|
|
sst26_bytewrite(priv, buffer, offset, bytestowrite);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += bytestowrite;
|
|
|
|
count -= bytestowrite;
|
|
|
|
index = bytestowrite;
|
|
|
|
|
|
|
|
/* Write full pages */
|
|
|
|
|
|
|
|
while (count >= pagesize)
|
|
|
|
{
|
|
|
|
sst26_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)
|
|
|
|
{
|
|
|
|
sst26_bytewrite(priv, &buffer[index], offset, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->lastwaswrite = true;
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:10:41 +02:00
|
|
|
sst26_unlock(priv->dev);
|
2016-05-10 19:09:15 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MTD_BYTE_WRITE */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
static int sst26_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct sst26_dev_s *priv = (FAR struct sst26_dev_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
sstinfo("cmd: %d\n", cmd);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2019-07-22 00:07:44 +02:00
|
|
|
FAR struct mtd_geometry_s *geo =
|
|
|
|
(FAR struct mtd_geometry_s *)((uintptr_t)arg);
|
|
|
|
if (geo != NULL)
|
2016-05-10 19:09:15 +02:00
|
|
|
{
|
2021-08-11 06:21:39 +02:00
|
|
|
memset(geo, 0, sizeof(*geo));
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Populate the geometry structure with information need to
|
|
|
|
* know the capacity and how to access the device.
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +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
|
|
|
sstinfo("blocksize: %d erasesize: %d neraseblocks: %d\n",
|
2016-05-25 21:43:44 +02:00
|
|
|
geo->blocksize, geo->erasesize, geo->neraseblocks);
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-08-15 20:10:23 +02:00
|
|
|
case BIOC_PARTINFO:
|
|
|
|
{
|
|
|
|
FAR struct partition_info_s *info =
|
|
|
|
(FAR struct partition_info_s *)arg;
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
info->numsectors = priv->nsectors *
|
|
|
|
(priv->sectorshift - priv->pageshift);
|
|
|
|
info->sectorsize = 1 << priv->pageshift;
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-05-10 19:09:15 +02:00
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
sst26_lock(priv->dev);
|
|
|
|
ret = sst26_chiperase(priv);
|
|
|
|
sst26_unlock(priv->dev);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("return %d\n", ret);
|
2016-05-10 19:09:15 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-05-10 19:09:15 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-05-10 19:09:15 +02:00
|
|
|
* Name: sst26_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Create an initialize MTD device instance. MTD devices are not registered
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
FAR struct mtd_dev_s *sst26_initialize_spi(FAR struct spi_dev_s *dev,
|
|
|
|
uint16_t spi_devid)
|
2016-05-10 19:09:15 +02:00
|
|
|
{
|
|
|
|
FAR struct sst26_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("dev: %p\n", dev);
|
2016-05-10 19:09:15 +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-05-10 19:09:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
priv = (FAR struct sst26_dev_s *)kmm_zalloc(sizeof(struct sst26_dev_s));
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated structure. (unsupported methods were
|
|
|
|
* nullified by kmm_zalloc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->mtd.erase = sst26_erase;
|
|
|
|
priv->mtd.bread = sst26_bread;
|
|
|
|
priv->mtd.bwrite = sst26_bwrite;
|
|
|
|
priv->mtd.read = sst26_read;
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
priv->mtd.write = sst26_write;
|
|
|
|
#endif
|
|
|
|
priv->mtd.ioctl = sst26_ioctl;
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "sst26";
|
2016-05-10 19:09:15 +02:00
|
|
|
priv->dev = dev;
|
2021-11-30 17:26:24 +01:00
|
|
|
priv->devid = spi_devid;
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2021-11-30 17:26:24 +01:00
|
|
|
SPI_SELECT(dev, SPIDEV_FLASH(priv->devid), false);
|
2016-05-10 19:09:15 +02:00
|
|
|
|
|
|
|
/* Identify the FLASH chip and get its capacity */
|
|
|
|
|
|
|
|
ret = sst26_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-05-10 19:09:15 +02:00
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ssterr("ERROR: Unrecognized\n");
|
2016-05-10 19:09:15 +02:00
|
|
|
kmm_free(priv);
|
2016-07-01 01:49:53 +02:00
|
|
|
return NULL;
|
2016-05-10 19:09:15 +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
|
|
|
|
*/
|
2019-07-22 00:07:44 +02:00
|
|
|
|
2016-07-12 18:34:03 +02:00
|
|
|
sst26_writeenable(priv);
|
2016-05-10 19:09:15 +02:00
|
|
|
sst26_globalunlock(priv);
|
2016-07-12 18:34:03 +02:00
|
|
|
sst26_writedisable(priv);
|
2016-05-10 19:09:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sstinfo("Return %p\n", priv);
|
2016-05-10 19:09:15 +02:00
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|