2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* drivers/mtd/sst25.c
|
2012-06-25 23:21:08 +02:00
|
|
|
*
|
2021-03-20 19:59:00 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-06-25 23:21:08 +02:00
|
|
|
*
|
2021-03-20 19:59:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-10-16 16:28:58 +02:00
|
|
|
*
|
2021-03-20 19:59:00 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-06-25 23:21:08 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-12-05 05:56:21 +01:00
|
|
|
#include <inttypes.h>
|
2012-06-25 23:21:08 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-06-26 01:40:39 +02:00
|
|
|
#include <string.h>
|
2012-06-25 23:21:08 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2012-06-25 23:21:08 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-07-01 16:11:54 +02:00
|
|
|
#include <nuttx/spi/spi.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Configuration ************************************************************/
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Per the data sheet, the SST25 parts can be driven with either SPI mode 0
|
|
|
|
* (CPOL=0 and CPHA=0) or mode 3 (CPOL=1 and CPHA=1).
|
|
|
|
* But I have heard that other devices can operate in mode 0 or 1.
|
|
|
|
* So you may need to specify CONFIG_SST25_SPIMODE to select the best mode
|
|
|
|
* for your device. If CONFIG_SST25_SPIMODE is not defined, mode 0 will
|
|
|
|
* be used.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_SST25_SPIMODE
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CONFIG_SST25_SPIMODE SPIDEV_MODE0
|
2012-06-25 23:21:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SPI Frequency. May be up to 25MHz. */
|
|
|
|
|
|
|
|
#ifndef CONFIG_SST25_SPIFREQUENCY
|
2021-01-27 16:48:40 +01:00
|
|
|
#define CONFIG_SST25_SPIFREQUENCY 20000000
|
2012-06-25 23:21:08 +02:00
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* SST25 Instructions *******************************************************/
|
|
|
|
|
|
|
|
/* Command Value Description Addr Data */
|
|
|
|
|
|
|
|
/* Dummy */
|
|
|
|
|
|
|
|
#define SST25_READ 0x03 /* Read data bytes 3 0 >=1 */
|
|
|
|
#define SST25_FAST_READ 0x0b /* Higher speed read 3 1 >=1 */
|
|
|
|
#define SST25_SE 0x20 /* 4Kb Sector erase 3 0 0 */
|
|
|
|
#define SST25_BE32 0x52 /* 32Kbit block Erase 3 0 0 */
|
|
|
|
#define SST25_BE64 0xd8 /* 64Kbit block Erase 3 0 0 */
|
|
|
|
#define SST25_CE 0xc7 /* Chip erase 0 0 0 */
|
|
|
|
#define SST25_CE_ALT 0x60 /* Chip erase (alternate) 0 0 0 */
|
|
|
|
#define SST25_BP 0x02 /* Byte program 3 0 1 */
|
|
|
|
#define SST25_AAI 0xad /* Auto address increment 3 0 >=2 */
|
|
|
|
#define SST25_RDSR 0x05 /* Read status register 0 0 >=1 */
|
|
|
|
#define SST25_EWSR 0x50 /* Write enable status 0 0 0 */
|
|
|
|
#define SST25_WRSR 0x01 /* Write Status Register 0 0 1 */
|
|
|
|
#define SST25_WREN 0x06 /* Write Enable 0 0 0 */
|
|
|
|
#define SST25_WRDI 0x04 /* Write Disable 0 0 0 */
|
|
|
|
#define SST25_RDID 0xab /* Read Identification 0 0 >=1 */
|
|
|
|
#define SST25_RDID_ALT 0x90 /* Read Identification (alt) 0 0 >=1 */
|
|
|
|
#define SST25_JEDEC_ID 0x9f /* JEDEC ID read 0 0 >=3 */
|
|
|
|
#define SST25_EBSY 0x70 /* Enable SO RY/BY# status 0 0 0 */
|
|
|
|
#define SST25_DBSY 0x80 /* Disable SO RY/BY# status 0 0 0 */
|
|
|
|
|
|
|
|
/* SST25 Registers **********************************************************/
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
/* Read ID (RDID) register values */
|
|
|
|
|
|
|
|
#define SST25_MANUFACTURER 0xbf /* SST manufacturer ID */
|
2013-10-16 16:28:58 +02:00
|
|
|
#define SST25_VF016_DEVID 0x25 /* SSTVF016B device ID */
|
2012-06-25 23:21:08 +02:00
|
|
|
#define SST25_VF032_DEVID 0x20 /* SSTVF032B device ID */
|
|
|
|
|
|
|
|
/* JEDEC Read ID register values */
|
|
|
|
|
|
|
|
#define SST25_JEDEC_MANUFACTURER 0xbf /* SST manufacturer ID */
|
|
|
|
#define SST25_JEDEC_MEMORY_TYPE 0x25 /* SST25 memory type */
|
2013-10-16 16:28:58 +02:00
|
|
|
#define SST25_JEDEC_VF032_CAPACITY 0x4a /* SST25VF032B memory capacity */
|
|
|
|
#define SST25_JEDEC_VF016_CAPACITY 0x41 /* SST25VF016B memory capacity */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Status register bit definitions */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define SST25_SR_BUSY (1 << 0) /* Bit 0: Write in progress */
|
|
|
|
#define SST25_SR_WEL (1 << 1) /* Bit 1: Write enable latch bit */
|
|
|
|
#define SST25_SR_BP_SHIFT (2) /* Bits 2-5: Block protect bits */
|
|
|
|
#define SST25_SR_BP_MASK (15 << SST25_SR_BP_SHIFT)
|
|
|
|
#define SST25_SR_BP_NONE (0 << SST25_SR_BP_SHIFT) /* Unprotected */
|
|
|
|
#define SST25_SR_BP_UPPER64th (1 << SST25_SR_BP_SHIFT) /* Upper 64th */
|
|
|
|
#define SST25_SR_BP_UPPER32nd (2 << SST25_SR_BP_SHIFT) /* Upper 32nd */
|
|
|
|
#define SST25_SR_BP_UPPER16th (3 << SST25_SR_BP_SHIFT) /* Upper 16th */
|
|
|
|
#define SST25_SR_BP_UPPER8th (4 << SST25_SR_BP_SHIFT) /* Upper 8th */
|
|
|
|
#define SST25_SR_BP_UPPERQTR (5 << SST25_SR_BP_SHIFT) /* Upper quarter */
|
|
|
|
#define SST25_SR_BP_UPPERHALF (6 << SST25_SR_BP_SHIFT) /* Upper half */
|
|
|
|
#define SST25_SR_BP_ALL (7 << SST25_SR_BP_SHIFT) /* All sectors */
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define SST25_SR_AAI (1 << 6) /* Bit 6: Auto Address increment programming */
|
|
|
|
#define SST25_SR_SRWD (1 << 7) /* Bit 7: Status register write protect */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
#define SST25_DUMMY 0xa5
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Chip Geometries **********************************************************/
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
/* SST25VF512 capacity is 512Kbit (64Kbit x 8) = 64Kb (8Kb x 8) */
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
/* SST25VF010 capacity is 1Mbit (128Kbit x 8) = 128Kb (16Kb x 8 */
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
/* SST25VF520 capacity is 2Mbit (256Kbit x 8) = 256Kb (32Kb x 8) */
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
/* SST25VF540 capacity is 4Mbit (512Kbit x 8) = 512Kb (64Kb x 8) */
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
/* SST25VF080 capacity is 8Mbit (1024Kbit x 8) = 1Mb (128Kb x 8) */
|
2020-12-05 05:55:23 +01:00
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
/* Not yet supported */
|
|
|
|
|
2013-10-16 16:28:58 +02:00
|
|
|
/* SST25VF016 capacity is 16Mbit (2048Kbit x 8) = 2Mb (256Kb x 8) */
|
|
|
|
|
2020-12-05 05:55:23 +01:00
|
|
|
#define SST25_VF016_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4Kb */
|
2013-10-16 16:28:58 +02:00
|
|
|
#define SST25_VF016_NSECTORS 512 /* 512 sectors x 4096 bytes/sector = 2Mb */
|
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
/* SST25VF032 capacity is 32Mbit (4096Kbit x 8) = 4Mb (512kb x 8) */
|
|
|
|
|
|
|
|
#define SST25_VF032_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define SST25_VF032_NSECTORS 1024 /* 1024 sectors x 4096 bytes/sector = 4Mb */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SST25_SECTOR512 /* Simulate a 512 byte sector */
|
2021-01-27 16:48:40 +01:00
|
|
|
#define SST25_SECTOR_SHIFT 9 /* Sector size 1 << 9 = 512 bytes */
|
|
|
|
#define SST25_SECTOR_SIZE 512 /* Sector size = 512 bytes */
|
2012-06-25 23:21:08 +02:00
|
|
|
#endif
|
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
#define SST25_ERASED_STATE 0xff /* State of FLASH when erased */
|
2012-06-26 01:40:39 +02:00
|
|
|
|
|
|
|
/* Cache flags */
|
|
|
|
|
|
|
|
#define SST25_CACHE_VALID (1 << 0) /* 1=Cache has valid data */
|
|
|
|
#define SST25_CACHE_DIRTY (1 << 1) /* 1=Cache is dirty */
|
|
|
|
#define SST25_CACHE_ERASED (1 << 2) /* 1=Backing FLASH is erased */
|
|
|
|
|
|
|
|
#define IS_VALID(p) ((((p)->flags) & SST25_CACHE_VALID) != 0)
|
|
|
|
#define IS_DIRTY(p) ((((p)->flags) & SST25_CACHE_DIRTY) != 0)
|
2016-09-02 15:27:57 +02:00
|
|
|
#define IS_ERASED(p) ((((p)->flags) & SST25_CACHE_ERASED) != 0)
|
2012-06-26 01:40:39 +02:00
|
|
|
|
|
|
|
#define SET_VALID(p) do { (p)->flags |= SST25_CACHE_VALID; } while (0)
|
|
|
|
#define SET_DIRTY(p) do { (p)->flags |= SST25_CACHE_DIRTY; } while (0)
|
2016-09-02 15:27:57 +02:00
|
|
|
#define SET_ERASED(p) do { (p)->flags |= SST25_CACHE_ERASED; } while (0)
|
2012-06-26 01:40:39 +02:00
|
|
|
|
|
|
|
#define CLR_VALID(p) do { (p)->flags &= ~SST25_CACHE_VALID; } while (0)
|
|
|
|
#define CLR_DIRTY(p) do { (p)->flags &= ~SST25_CACHE_DIRTY; } while (0)
|
2016-09-02 15:27:57 +02:00
|
|
|
#define CLR_ERASED(p) do { (p)->flags &= ~SST25_CACHE_ERASED; } while (0)
|
2012-06-26 01:40:39 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* This type represents the state of the MTD device.
|
|
|
|
* The struct mtd_dev_s must appear at the beginning of the definition so
|
|
|
|
* that you can freely cast between pointers to struct mtd_dev_s and struct
|
|
|
|
* sst25_dev_s.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct sst25_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct spi_dev_s *dev; /* Saved SPI interface instance */
|
|
|
|
uint16_t nsectors; /* Number of erase sectors */
|
|
|
|
uint8_t sectorshift; /* Log2 of erase sector size */
|
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#if defined(CONFIG_SST25_SECTOR512) && !defined(CONFIG_SST25_READONLY)
|
2012-06-26 01:40:39 +02:00
|
|
|
uint8_t flags; /* Buffered sector flags */
|
2015-10-10 18:41:00 +02:00
|
|
|
uint16_t esectno; /* Erase sector number in the cache */
|
2012-06-25 23:21:08 +02:00
|
|
|
FAR uint8_t *sector; /* Allocated sector data */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static void sst25_lock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline void sst25_unlock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline int sst25_readid(FAR struct sst25_dev_s *priv);
|
2012-06-27 01:23:08 +02:00
|
|
|
#ifndef CONFIG_SST25_READONLY
|
2013-11-02 15:27:13 +01:00
|
|
|
static void sst25_unprotect(FAR struct sst25_dev_s *priv);
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-27 17:35:35 +02:00
|
|
|
static uint8_t sst25_waitwritecomplete(FAR struct sst25_dev_s *priv);
|
2013-11-02 15:27:13 +01:00
|
|
|
static inline void sst25_cmd(struct sst25_dev_s *priv, uint8_t cmd);
|
2012-06-25 23:21:08 +02:00
|
|
|
static inline void sst25_wren(FAR struct sst25_dev_s *priv);
|
2013-10-16 16:28:58 +02:00
|
|
|
#if !defined(CONFIG_SST25_SLOWWRITE) && !defined(CONFIG_SST25_READONLY)
|
2012-06-25 23:21:08 +02:00
|
|
|
static inline void sst25_wrdi(FAR struct sst25_dev_s *priv);
|
2013-10-16 16:28:58 +02:00
|
|
|
#endif
|
2012-06-26 01:40:39 +02:00
|
|
|
static void sst25_sectorerase(FAR struct sst25_dev_s *priv, off_t offset);
|
2012-06-25 23:21:08 +02:00
|
|
|
static inline int sst25_chiperase(FAR struct sst25_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static void sst25_byteread(FAR struct sst25_dev_s *priv,
|
|
|
|
FAR uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes);
|
2012-06-27 01:23:08 +02:00
|
|
|
#ifndef CONFIG_SST25_READONLY
|
|
|
|
#ifdef CONFIG_SST25_SLOWWRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
static void sst25_bytewrite(FAR struct sst25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes);
|
2012-06-27 01:23:08 +02:00
|
|
|
#else
|
2021-01-27 16:48:40 +01:00
|
|
|
static void sst25_wordwrite(FAR struct sst25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes);
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-26 01:40:39 +02:00
|
|
|
#ifdef CONFIG_SST25_SECTOR512
|
|
|
|
static void sst25_cacheflush(struct sst25_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static FAR uint8_t *sst25_cacheread(struct sst25_dev_s *priv,
|
|
|
|
off_t sector);
|
|
|
|
static void sst25_cacheerase(struct sst25_dev_s *priv,
|
|
|
|
off_t sector);
|
|
|
|
static void sst25_cachewrite(FAR struct sst25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t sector,
|
|
|
|
size_t nsectors);
|
2012-06-25 23:21:08 +02:00
|
|
|
#endif
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int sst25_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t sst25_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR uint8_t *buf);
|
|
|
|
static ssize_t sst25_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR const uint8_t *buf);
|
|
|
|
static ssize_t sst25_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2012-06-25 23:21:08 +02:00
|
|
|
FAR uint8_t *buffer);
|
2021-01-27 16:48:40 +01:00
|
|
|
static int sst25_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Private Data
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_lock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
static void sst25_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
|
2012-06-25 23:21:08 +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.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, true);
|
2012-06-25 23:21:08 +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.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SETMODE(dev, CONFIG_SST25_SPIMODE);
|
|
|
|
SPI_SETBITS(dev, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(dev, 0);
|
|
|
|
SPI_SETFREQUENCY(dev, CONFIG_SST25_SPIFREQUENCY);
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_unlock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
static inline void sst25_unlock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, false);
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_readid
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
static inline int sst25_readid(struct sst25_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);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus, configure the bus, and select this FLASH part. */
|
|
|
|
|
|
|
|
sst25_lock(priv->dev);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2012-06-25 23:21:08 +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, SST25_JEDEC_ID);
|
2012-06-25 23:21:08 +02:00
|
|
|
manufacturer = SPI_SEND(priv->dev, SST25_DUMMY);
|
|
|
|
memory = SPI_SEND(priv->dev, SST25_DUMMY);
|
|
|
|
capacity = SPI_SEND(priv->dev, SST25_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and unlock the bus */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-25 23:21:08 +02:00
|
|
|
sst25_unlock(priv->dev);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("manufacturer: %02x memory: %02x capacity: %02x\n",
|
2012-06-25 23:21:08 +02:00
|
|
|
manufacturer, memory, capacity);
|
|
|
|
|
|
|
|
/* Check for a valid manufacturer and memory type */
|
|
|
|
|
2013-10-16 16:28:58 +02:00
|
|
|
if (manufacturer == SST25_JEDEC_MANUFACTURER &&
|
|
|
|
memory == SST25_JEDEC_MEMORY_TYPE)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
2013-10-16 16:28:58 +02:00
|
|
|
/* Okay.. is it a FLASH capacity that we understand? This should be
|
|
|
|
* extended support other members of the SST25 family. If so, save
|
|
|
|
* the FLASH geometry.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
2013-10-16 16:28:58 +02:00
|
|
|
switch (capacity)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
2013-10-16 16:28:58 +02:00
|
|
|
case SST25_JEDEC_VF032_CAPACITY:
|
|
|
|
priv->sectorshift = SST25_VF032_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = SST25_VF032_NSECTORS;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
case SST25_JEDEC_VF016_CAPACITY:
|
|
|
|
priv->sectorshift = SST25_VF016_SECTOR_SHIFT;
|
|
|
|
priv->nsectors = SST25_VF016_NSECTORS;
|
|
|
|
return OK;
|
|
|
|
|
2013-10-16 19:59:26 +02:00
|
|
|
/* Support for this part is not implemented yet */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2013-10-16 16:28:58 +02:00
|
|
|
default:
|
|
|
|
break;
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-27 01:23:08 +02:00
|
|
|
* Name: sst25_unprotect
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-27 01:23:08 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_SST25_READONLY
|
2013-11-02 15:27:13 +01:00
|
|
|
static void sst25_unprotect(struct sst25_dev_s *priv)
|
2012-06-27 01:23:08 +02:00
|
|
|
{
|
|
|
|
/* Send "Write enable status (EWSR)" */
|
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
sst25_cmd(priv, SST25_EWSR);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
/* Send "Write enable status (WRSR)" */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_WRSR);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
/* Followed by the new status value */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
SPI_SEND(priv->dev, 0);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-27 01:23:08 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_waitwritecomplete
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
static uint8_t sst25_waitwritecomplete(struct sst25_dev_s *priv)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
/* 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);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Send "Read Status Register (RDSR)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_RDSR);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Send a dummy byte to generate the clock needed to shift out the
|
|
|
|
* status
|
|
|
|
*/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
status = SPI_SEND(priv->dev, SST25_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-25 23:21:08 +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.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
#if 0 /* Makes writes too slow */
|
2012-06-25 23:21:08 +02:00
|
|
|
if ((status & SST25_SR_BUSY) != 0)
|
|
|
|
{
|
|
|
|
sst25_unlock(priv->dev);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2012-06-25 23:21:08 +02:00
|
|
|
sst25_lock(priv->dev);
|
|
|
|
}
|
2012-06-27 17:35:35 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
while ((status & SST25_SR_BUSY) != 0);
|
2012-06-27 17:35:35 +02:00
|
|
|
|
|
|
|
return status;
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2013-11-02 15:27:13 +01:00
|
|
|
* Name: sst25_cmd
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
static inline void sst25_cmd(struct sst25_dev_s *priv, uint8_t cmd)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
/* Send command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, cmd);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2013-11-02 15:27:13 +01:00
|
|
|
* Name: sst25_wren
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2013-11-02 15:27:13 +01:00
|
|
|
|
|
|
|
static inline void sst25_wren(struct sst25_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Send "Write Enable (WREN)" command */
|
|
|
|
|
|
|
|
sst25_cmd(priv, SST25_WREN);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_wrdi
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2013-11-02 15:27:13 +01:00
|
|
|
|
2013-10-16 16:28:58 +02:00
|
|
|
#if !defined(CONFIG_SST25_SLOWWRITE) && !defined(CONFIG_SST25_READONLY)
|
2012-06-25 23:21:08 +02:00
|
|
|
static inline void sst25_wrdi(struct sst25_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Send "Write Disable (WRDI)" command */
|
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
sst25_cmd(priv, SST25_WRDI);
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
2013-10-16 16:28:58 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_sectorerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-26 01:40:39 +02:00
|
|
|
static void sst25_sectorerase(struct sst25_dev_s *priv, off_t sector)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
|
|
|
off_t address = sector << priv->sectorshift;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %08lx\n", (long)sector);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
sst25_waitwritecomplete(priv);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
sst25_wren(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Send the "Sector Erase (SE)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_SE);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Send the sector address high byte first. Only the most significant bits
|
|
|
|
* (those corresponding to the sector) have any meaning.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, address & 0xff);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_chiperase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
static inline int sst25_chiperase(struct sst25_dev_s *priv)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("priv: %p\n", priv);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
sst25_waitwritecomplete(priv);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
sst25_wren(priv);
|
|
|
|
|
|
|
|
/* Send the "Chip Erase (CE)" instruction */
|
|
|
|
|
2013-11-03 14:56:29 +01:00
|
|
|
sst25_cmd(priv, SST25_CE);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return: OK\n");
|
2012-06-25 23:21:08 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_byteread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static void sst25_byteread(FAR struct sst25_dev_s *priv,
|
|
|
|
FAR uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
2012-06-27 17:35:35 +02:00
|
|
|
uint8_t status;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("address: %08lx nbytes: %d\n", (long)address, (int)nbytes);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
status = sst25_waitwritecomplete(priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
DEBUGASSERT((status & (SST25_SR_WEL |
|
|
|
|
SST25_SR_BP_MASK |
|
|
|
|
SST25_SR_AAI)) == 0);
|
2015-07-03 22:38:33 +02:00
|
|
|
UNUSED(status);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Send "Read from Memory " instruction */
|
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#ifdef CONFIG_SST25_SLOWREAD
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_READ);
|
2012-06-27 01:23:08 +02:00
|
|
|
#else
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_FAST_READ);
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, address & 0xff);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Send a dummy byte */
|
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#ifndef CONFIG_SST25_SLOWREAD
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_DUMMY);
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Then read all of the requested bytes */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->dev, buffer, nbytes);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-27 17:35:35 +02:00
|
|
|
* Name: sst25_bytewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-27 01:23:08 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_SST25_SLOWWRITE) && !defined(CONFIG_SST25_READONLY)
|
2021-01-27 16:48:40 +01:00
|
|
|
static void sst25_bytewrite(struct sst25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes)
|
2012-06-27 01:23:08 +02:00
|
|
|
{
|
2012-06-27 17:35:35 +02:00
|
|
|
uint8_t status;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("address: %08lx nwords: %d\n", (long)address, (int)nbytes);
|
2012-06-27 01:23:08 +02:00
|
|
|
DEBUGASSERT(priv && buffer);
|
|
|
|
|
|
|
|
/* Write each byte individually */
|
|
|
|
|
|
|
|
for (; nbytes > 0; nbytes--)
|
|
|
|
{
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Skip over bytes that are begin written to the erased state */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
if (*buffer != SST25_ERASED_STATE)
|
|
|
|
{
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
status = sst25_waitwritecomplete(priv);
|
2020-12-05 05:55:23 +01:00
|
|
|
DEBUGASSERT((status & (SST25_SR_WEL | SST25_SR_BP_MASK |
|
|
|
|
SST25_SR_AAI)) == 0);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Enable write access to the FLASH */
|
|
|
|
|
|
|
|
sst25_wren(priv);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Select this FLASH part */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Send "Byte Program (BP)" command */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_BP);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Send the byte address high byte first. */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, address & 0xff);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Then write the single byte */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, *buffer);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Deselect the FLASH and setup for the next pass through the
|
|
|
|
* loop
|
|
|
|
*/
|
2012-06-27 17:35:35 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-27 17:35:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance to the next byte */
|
2012-06-27 01:23:08 +02:00
|
|
|
|
|
|
|
buffer++;
|
|
|
|
address++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-27 17:35:35 +02:00
|
|
|
* Name: sst25_wordwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#if !defined(CONFIG_SST25_SLOWWRITE) && !defined(CONFIG_SST25_READONLY)
|
2021-01-27 16:48:40 +01:00
|
|
|
static void sst25_wordwrite(struct sst25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
2012-06-27 01:23:08 +02:00
|
|
|
size_t nwords = (nbytes + 1) >> 1;
|
2012-06-27 17:35:35 +02:00
|
|
|
uint8_t status;
|
2012-06-27 01:23:08 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("address: %08lx nwords: %d\n", (long)address, (int)nwords);
|
2012-06-25 23:21:08 +02:00
|
|
|
DEBUGASSERT(priv && buffer);
|
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Loop until all of the bytes have been written */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
while (nwords > 0)
|
|
|
|
{
|
|
|
|
/* Skip over any data that is being written to the erased state */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
while (nwords > 0 &&
|
|
|
|
buffer[0] == SST25_ERASED_STATE &&
|
|
|
|
buffer[1] == SST25_ERASED_STATE)
|
|
|
|
{
|
|
|
|
/* Decrement the word count and advance the write position */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
nwords--;
|
|
|
|
buffer += 2;
|
|
|
|
address += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there are no further non-erased bytes in the user buffer, then
|
|
|
|
* we are finished.
|
|
|
|
*/
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2014-11-25 20:15:09 +01:00
|
|
|
if (nwords < 1)
|
2012-06-27 17:35:35 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
|
|
|
status = sst25_waitwritecomplete(priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
DEBUGASSERT((status & (SST25_SR_WEL |
|
|
|
|
SST25_SR_BP_MASK |
|
|
|
|
SST25_SR_AAI)) == 0);
|
2015-07-03 22:38:33 +02:00
|
|
|
UNUSED(status);
|
2012-06-27 17:35:35 +02:00
|
|
|
|
|
|
|
/* Enable write access to the FLASH */
|
|
|
|
|
|
|
|
sst25_wren(priv);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Select this FLASH part */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Send "Auto Address Increment (AAI)" command */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_AAI);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Send the word address high byte first. */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, address & 0xff);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Then write one 16-bit word */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, 2);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Deselect the FLASH: Chip Select high */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
/* Wait for the preceding write to complete. */
|
|
|
|
|
|
|
|
status = sst25_waitwritecomplete(priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
DEBUGASSERT((status & (SST25_SR_WEL |
|
|
|
|
SST25_SR_BP_MASK |
|
|
|
|
SST25_SR_AAI)) ==
|
|
|
|
(SST25_SR_WEL |
|
|
|
|
SST25_SR_AAI));
|
2015-07-03 22:38:33 +02:00
|
|
|
UNUSED(status);
|
2013-11-02 15:27:13 +01:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Decrement the word count and advance the write position */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
nwords--;
|
|
|
|
buffer += 2;
|
|
|
|
address += 2;
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Now loop, writing 16-bits of data on each pass through the loop
|
|
|
|
* until all of the words have been transferred or until we encounter
|
|
|
|
* data to be written to the erased state.
|
|
|
|
*/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
while (nwords > 0 &&
|
|
|
|
(buffer[0] != SST25_ERASED_STATE ||
|
|
|
|
buffer[1] != SST25_ERASED_STATE))
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Send "Auto Address Increment (AAI)" command with no address */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, SST25_AAI);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Then write one 16-bit word */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, 2);
|
|
|
|
|
|
|
|
/* Deselect the FLASH: Chip Select high */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2013-11-02 15:27:13 +01:00
|
|
|
/* Wait for the preceding write to complete. */
|
|
|
|
|
|
|
|
status = sst25_waitwritecomplete(priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
DEBUGASSERT((status & (SST25_SR_WEL |
|
|
|
|
SST25_SR_BP_MASK |
|
|
|
|
SST25_SR_AAI)) ==
|
|
|
|
(SST25_SR_WEL |
|
|
|
|
SST25_SR_AAI));
|
2015-07-03 22:38:33 +02:00
|
|
|
UNUSED(status);
|
2013-11-02 15:27:13 +01:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Decrement the word count and advance the write position */
|
|
|
|
|
|
|
|
nwords--;
|
|
|
|
buffer += 2;
|
|
|
|
address += 2;
|
|
|
|
}
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
/* Disable writing */
|
|
|
|
|
|
|
|
sst25_wrdi(priv);
|
|
|
|
}
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-26 01:40:39 +02:00
|
|
|
* Name: sst25_cacheflush
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-26 01:40:39 +02:00
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#if defined(CONFIG_SST25_SECTOR512) && !defined(CONFIG_SST25_READONLY)
|
2012-06-26 01:40:39 +02:00
|
|
|
static void sst25_cacheflush(struct sst25_dev_s *priv)
|
|
|
|
{
|
2020-12-05 05:55:23 +01:00
|
|
|
/* If the cached is dirty (meaning that it no longer matches the old FLASH
|
|
|
|
* contents) or was erased (with the cache containing the correct FLASH
|
|
|
|
* contents), then write the cached erase block to FLASH.
|
2012-06-26 01:40:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (IS_DIRTY(priv) || IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
/* Write entire erase block to FLASH */
|
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#ifdef CONFIG_SST25_SLOWWRITE
|
2021-01-27 16:48:40 +01:00
|
|
|
sst25_bytewrite(priv, priv->sector,
|
|
|
|
(off_t)priv->esectno << priv->sectorshift,
|
|
|
|
(1 << priv->sectorshift));
|
2012-06-27 01:23:08 +02:00
|
|
|
#else
|
2021-01-27 16:48:40 +01:00
|
|
|
sst25_wordwrite(priv, priv->sector,
|
|
|
|
(off_t)priv->esectno << priv->sectorshift,
|
|
|
|
(1 << priv->sectorshift));
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-26 01:40:39 +02:00
|
|
|
|
|
|
|
/* The case is no long dirty and the FLASH is no longer erased */
|
|
|
|
|
|
|
|
CLR_DIRTY(priv);
|
|
|
|
CLR_ERASED(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-26 01:40:39 +02:00
|
|
|
* Name: sst25_cacheread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#if defined(CONFIG_SST25_SECTOR512) && !defined(CONFIG_SST25_READONLY)
|
2012-06-26 01:40:39 +02:00
|
|
|
static FAR uint8_t *sst25_cacheread(struct sst25_dev_s *priv, off_t sector)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
|
|
|
off_t esectno;
|
|
|
|
int shift;
|
|
|
|
int index;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Convert from the 512 byte sector to the erase sector size of the device.
|
|
|
|
* For exmample, if the actual erase sector size if 4Kb (1 << 12), then we
|
|
|
|
* first shift to the right by 3 to get the sector number in 4096
|
|
|
|
* increments.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
shift = priv->sectorshift - SST25_SECTOR_SHIFT;
|
|
|
|
esectno = sector >> shift;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %ld esectno: %d shift=%d\n", sector, esectno, shift);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-26 01:40:39 +02:00
|
|
|
/* Check if the requested erase block is already in the cache */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2012-06-26 01:40:39 +02:00
|
|
|
if (!IS_VALID(priv) || esectno != priv->esectno)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
2012-06-26 01:40:39 +02:00
|
|
|
/* No.. Flush any dirty erase block currently in the cache */
|
|
|
|
|
|
|
|
sst25_cacheflush(priv);
|
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
/* Read the erase block into the cache */
|
|
|
|
|
2020-12-05 05:55:23 +01:00
|
|
|
sst25_byteread(priv, priv->sector, (esectno << priv->sectorshift),
|
|
|
|
1 << priv->sectorshift);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Mark the sector as cached */
|
|
|
|
|
|
|
|
priv->esectno = esectno;
|
2012-06-26 01:40:39 +02:00
|
|
|
|
|
|
|
SET_VALID(priv); /* The data in the cache is valid */
|
|
|
|
CLR_DIRTY(priv); /* It should match the FLASH contents */
|
|
|
|
CLR_ERASED(priv); /* The underlying FLASH has not been erased */
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Get the index to the 512 sector in the erase block that holds the
|
|
|
|
* argument
|
|
|
|
*/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
index = sector & ((1 << shift) - 1);
|
|
|
|
|
|
|
|
/* Return the address in the cache that holds this sector */
|
|
|
|
|
|
|
|
return &priv->sector[index << SST25_SECTOR_SHIFT];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-26 01:40:39 +02:00
|
|
|
* Name: sst25_cacheerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-26 01:40:39 +02:00
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#if defined(CONFIG_SST25_SECTOR512) && !defined(CONFIG_SST25_READONLY)
|
2012-06-26 01:40:39 +02:00
|
|
|
static void sst25_cacheerase(struct sst25_dev_s *priv, off_t sector)
|
|
|
|
{
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* First, make sure that the erase block containing the 512 byte sector is
|
|
|
|
* in the cache.
|
2012-06-26 01:40:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
dest = sst25_cacheread(priv, sector);
|
|
|
|
|
|
|
|
/* Erase the block containing this sector if it is not already erased.
|
|
|
|
* The erased indicated will be cleared when the data from the erase sector
|
|
|
|
* is read into the cache and set here when we erase the block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
off_t esectno = sector >> (priv->sectorshift - SST25_SECTOR_SHIFT);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %ld esectno: %d\n", sector, esectno);
|
2012-06-26 01:40:39 +02:00
|
|
|
|
|
|
|
sst25_sectorerase(priv, esectno);
|
|
|
|
SET_ERASED(priv);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Put the cached sector data into the erase state and mart the cache as
|
|
|
|
* dirty (but don't update the FLASH yet. The caller will do that at a
|
|
|
|
* more optimal time).
|
2012-06-26 01:40:39 +02:00
|
|
|
*/
|
|
|
|
|
2012-06-27 17:35:35 +02:00
|
|
|
memset(dest, SST25_ERASED_STATE, SST25_SECTOR_SIZE);
|
2012-06-26 01:40:39 +02:00
|
|
|
SET_DIRTY(priv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-26 01:40:39 +02:00
|
|
|
* Name: sst25_cachewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-26 01:40:39 +02:00
|
|
|
|
2012-06-27 01:23:08 +02:00
|
|
|
#if defined(CONFIG_SST25_SECTOR512) && !defined(CONFIG_SST25_READONLY)
|
2021-01-27 16:48:40 +01:00
|
|
|
static void sst25_cachewrite(FAR struct sst25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t sector,
|
|
|
|
size_t nsectors)
|
2012-06-26 01:40:39 +02:00
|
|
|
{
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
|
|
|
|
for (; nsectors > 0; nsectors--)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
/* First, make sure that the erase block containing 512 byte sector is
|
|
|
|
* in memory.
|
2012-06-26 01:40:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
dest = sst25_cacheread(priv, sector);
|
|
|
|
|
|
|
|
/* Erase the block containing this sector if it is not already erased.
|
2021-01-27 16:48:40 +01:00
|
|
|
* The erased indicated will be cleared when the data from the erase
|
|
|
|
* sector is read into the cache and set here when we erase the sector.
|
2012-06-26 01:40:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!IS_ERASED(priv))
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
off_t esectno = sector >>
|
|
|
|
(priv->sectorshift - SST25_SECTOR_SHIFT);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %ld esectno: %d\n", sector, esectno);
|
2012-06-26 01:40:39 +02:00
|
|
|
|
|
|
|
sst25_sectorerase(priv, esectno);
|
|
|
|
SET_ERASED(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the new sector data into cached erase block */
|
|
|
|
|
|
|
|
memcpy(dest, buffer, SST25_SECTOR_SIZE);
|
|
|
|
SET_DIRTY(priv);
|
|
|
|
|
|
|
|
/* Set up for the next 512 byte sector */
|
|
|
|
|
|
|
|
buffer += SST25_SECTOR_SIZE;
|
|
|
|
sector++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the last erase block left in the cache */
|
|
|
|
|
|
|
|
sst25_cacheflush(priv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int sst25_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
2012-06-27 01:23:08 +02:00
|
|
|
#ifdef CONFIG_SST25_READONLY
|
2023-05-12 00:55:11 +02:00
|
|
|
return -EACCES;
|
2012-06-27 01:23:08 +02:00
|
|
|
#else
|
2012-06-25 23:21:08 +02:00
|
|
|
FAR struct sst25_dev_s *priv = (FAR struct sst25_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);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Lock access to the SPI bus until we complete the erase */
|
|
|
|
|
|
|
|
sst25_lock(priv->dev);
|
2012-06-26 01:40:39 +02:00
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
while (blocksleft-- > 0)
|
|
|
|
{
|
|
|
|
/* Erase each sector */
|
|
|
|
|
2012-06-26 01:40:39 +02:00
|
|
|
#ifdef CONFIG_SST25_SECTOR512
|
|
|
|
sst25_cacheerase(priv, startblock);
|
|
|
|
#else
|
2012-06-25 23:21:08 +02:00
|
|
|
sst25_sectorerase(priv, startblock);
|
2012-06-26 01:40:39 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
startblock++;
|
|
|
|
}
|
|
|
|
|
2012-06-26 01:40:39 +02:00
|
|
|
#ifdef CONFIG_SST25_SECTOR512
|
|
|
|
/* Flush the last erase block left in the cache */
|
|
|
|
|
|
|
|
sst25_cacheflush(priv);
|
|
|
|
#endif
|
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
sst25_unlock(priv->dev);
|
|
|
|
return (int)nblocks;
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2020-12-05 05:55:23 +01:00
|
|
|
static ssize_t sst25_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks,
|
2012-06-27 17:35:35 +02:00
|
|
|
FAR uint8_t *buffer)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
2012-06-26 01:40:39 +02:00
|
|
|
#ifdef CONFIG_SST25_SECTOR512
|
2012-06-25 23:21:08 +02:00
|
|
|
ssize_t nbytes;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2012-06-25 23:21:08 +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
|
|
|
|
*/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2020-12-05 05:55:23 +01:00
|
|
|
nbytes = sst25_read(dev, startblock << SST25_SECTOR_SHIFT,
|
|
|
|
nblocks << SST25_SECTOR_SHIFT, buffer);
|
2012-06-25 23:21:08 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
return nbytes >> SST25_SECTOR_SHIFT;
|
|
|
|
}
|
2012-06-26 01:40:39 +02:00
|
|
|
|
|
|
|
return (int)nbytes;
|
2012-06-25 23:21:08 +02:00
|
|
|
#else
|
2012-06-26 01:40:39 +02:00
|
|
|
FAR struct sst25_dev_s *priv = (FAR struct sst25_dev_s *)dev;
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2012-06-26 01:40:39 +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
|
|
|
|
*/
|
2012-06-26 01:40:39 +02:00
|
|
|
|
2020-12-05 05:55:23 +01:00
|
|
|
nbytes = sst25_read(dev, startblock << priv->sectorshift,
|
|
|
|
nblocks << priv->sectorshift, buffer);
|
2012-06-25 23:21:08 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
return nbytes >> priv->sectorshift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)nbytes;
|
2012-06-26 01:40:39 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2020-12-05 05:55:23 +01:00
|
|
|
static ssize_t sst25_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks,
|
2012-06-27 17:35:35 +02:00
|
|
|
FAR const uint8_t *buffer)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
2012-06-27 01:23:08 +02:00
|
|
|
#ifdef CONFIG_SST25_READONLY
|
|
|
|
return -EACCESS;
|
|
|
|
#else
|
2012-06-25 23:21:08 +02:00
|
|
|
FAR struct sst25_dev_s *priv = (FAR struct sst25_dev_s *)dev;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus and write all of the pages to FLASH */
|
|
|
|
|
|
|
|
sst25_lock(priv->dev);
|
2012-06-27 01:23:08 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_SST25_SECTOR512)
|
2012-06-27 17:35:35 +02:00
|
|
|
sst25_cachewrite(priv, buffer, startblock, nblocks);
|
2012-06-27 01:23:08 +02:00
|
|
|
#elif defined(CONFIG_SST25_SLOWWRITE)
|
2012-06-27 17:35:35 +02:00
|
|
|
sst25_bytewrite(priv, buffer, startblock << priv->sectorshift,
|
2012-06-27 01:23:08 +02:00
|
|
|
nblocks << priv->sectorshift);
|
2012-06-25 23:21:08 +02:00
|
|
|
#else
|
2012-06-27 17:35:35 +02:00
|
|
|
sst25_wordwrite(priv, buffer, startblock << priv->sectorshift,
|
2012-06-27 01:23:08 +02:00
|
|
|
nblocks << priv->sectorshift);
|
2012-06-25 23:21:08 +02:00
|
|
|
#endif
|
|
|
|
sst25_unlock(priv->dev);
|
|
|
|
|
|
|
|
return nblocks;
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t sst25_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2012-06-27 17:35:35 +02:00
|
|
|
FAR uint8_t *buffer)
|
2012-06-25 23:21:08 +02:00
|
|
|
{
|
|
|
|
FAR struct sst25_dev_s *priv = (FAR struct sst25_dev_s *)dev;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus and select this FLASH part */
|
|
|
|
|
|
|
|
sst25_lock(priv->dev);
|
|
|
|
sst25_byteread(priv, buffer, offset, nbytes);
|
|
|
|
sst25_unlock(priv->dev);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return nbytes: %d\n", (int)nbytes);
|
2012-06-25 23:21:08 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
static int sst25_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct sst25_dev_s *priv = (FAR struct sst25_dev_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
finfo("cmd: %d\n", cmd);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2020-12-05 05:55:23 +01:00
|
|
|
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)
|
|
|
|
((uintptr_t)arg);
|
2012-06-25 23:21:08 +02:00
|
|
|
if (geo)
|
|
|
|
{
|
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.
|
2012-06-25 23:21:08 +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.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SST25_SECTOR512
|
|
|
|
geo->blocksize = (1 << SST25_SECTOR_SHIFT);
|
2012-06-26 20:58:52 +02:00
|
|
|
geo->erasesize = (1 << SST25_SECTOR_SHIFT);
|
2013-10-28 01:53:09 +01:00
|
|
|
geo->neraseblocks = priv->nsectors << (priv->sectorshift - 9);
|
2012-06-25 23:21:08 +02:00
|
|
|
#else
|
|
|
|
geo->blocksize = (1 << priv->sectorshift);
|
|
|
|
geo->erasesize = (1 << priv->sectorshift);
|
|
|
|
geo->neraseblocks = priv->nsectors;
|
2012-06-26 20:58:52 +02:00
|
|
|
#endif
|
2012-06-25 23:21:08 +02:00
|
|
|
ret = OK;
|
|
|
|
|
2020-12-05 05:56:21 +01:00
|
|
|
finfo("blocksize: %" PRId32 " erasesize: %" PRId32
|
|
|
|
" neraseblocks: %" PRId32 "\n",
|
2012-06-25 23:21:08 +02:00
|
|
|
geo->blocksize, geo->erasesize, geo->neraseblocks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SST25_SECTOR512
|
|
|
|
info->numsectors = priv->nsectors <<
|
|
|
|
(priv->sectorshift - SST25_SECTOR_SHIFT);
|
|
|
|
info->sectorsize = 1 << SST25_SECTOR_SHIFT;
|
|
|
|
#else
|
|
|
|
info->numsectors = priv->nsectors;
|
|
|
|
info->sectorsize = 1 << priv->sectorshift;
|
|
|
|
#endif
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
sst25_lock(priv->dev);
|
|
|
|
ret = sst25_chiperase(priv);
|
|
|
|
sst25_unlock(priv->dev);
|
|
|
|
}
|
|
|
|
break;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2021-07-16 19:16:41 +02:00
|
|
|
case MTDIOC_ERASESTATE:
|
|
|
|
{
|
|
|
|
FAR uint8_t *result = (FAR uint8_t *)arg;
|
|
|
|
*result = SST25_ERASED_STATE;
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-06-25 23:21:08 +02:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return %d\n", ret);
|
2012-06-25 23:21:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-06-25 23:21:08 +02:00
|
|
|
* Name: sst25_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* 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).
|
2012-06-25 23:21:08 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
FAR struct mtd_dev_s *sst25_initialize(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct sst25_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("dev: %p\n", dev);
|
2012-06-25 23:21:08 +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.
|
2012-06-25 23:21:08 +02:00
|
|
|
*/
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct sst25_dev_s));
|
2012-06-25 23:21:08 +02:00
|
|
|
if (priv)
|
|
|
|
{
|
2013-05-01 18:59:57 +02:00
|
|
|
/* Initialize the allocated structure. (unsupported methods were
|
2014-09-01 01:34:44 +02:00
|
|
|
* nullified by kmm_zalloc).
|
2013-05-01 18:59:57 +02:00
|
|
|
*/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
priv->mtd.erase = sst25_erase;
|
|
|
|
priv->mtd.bread = sst25_bread;
|
|
|
|
priv->mtd.bwrite = sst25_bwrite;
|
|
|
|
priv->mtd.read = sst25_read;
|
|
|
|
priv->mtd.ioctl = sst25_ioctl;
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "sst25";
|
2012-06-25 23:21:08 +02:00
|
|
|
priv->dev = dev;
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(dev, SPIDEV_FLASH(0), false);
|
2012-06-25 23:21:08 +02:00
|
|
|
|
|
|
|
/* Identify the FLASH chip and get its capacity */
|
|
|
|
|
|
|
|
ret = sst25_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
|
|
|
|
*/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Unrecognized\n");
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2016-07-01 01:49:53 +02:00
|
|
|
return NULL;
|
2012-06-25 23:21:08 +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
|
|
|
|
*/
|
2012-06-27 01:23:08 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_SST25_READONLY
|
2013-11-02 15:27:13 +01:00
|
|
|
sst25_unprotect(priv);
|
2012-06-27 01:23:08 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SST25_SECTOR512 /* Simulate a 512 byte sector */
|
2012-06-26 01:40:39 +02:00
|
|
|
/* Allocate a buffer for the erase block cache */
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv->sector = kmm_malloc(1 << priv->sectorshift);
|
2012-06-25 23:21:08 +02:00
|
|
|
if (!priv->sector)
|
|
|
|
{
|
2020-12-05 05:55:23 +01:00
|
|
|
/* Allocation failed! Discard all of that work we just did and
|
|
|
|
* return NULL
|
|
|
|
*/
|
2012-06-25 23:21:08 +02:00
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Allocation failed\n");
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2016-09-02 15:27:57 +02:00
|
|
|
return NULL;
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
#endif
|
2012-06-27 01:23:08 +02:00
|
|
|
}
|
2012-06-25 23:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return %p\n", priv);
|
2012-06-25 23:21:08 +02:00
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|