2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* drivers/mtd/w25.c
|
|
|
|
*
|
2020-03-06 20:01:57 +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-09-17 20:35:37 +02:00
|
|
|
*
|
2020-03-06 20:01:57 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-09-17 20:35:37 +02:00
|
|
|
*
|
2020-03-06 20:01:57 +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-09-17 20:35:37 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* Driver for SPI-based W25x16, x32, and x64 and W25q16, q32, q64, and q128
|
|
|
|
* FLASH from Winbond (and work-alike parts from AMIC)
|
|
|
|
*/
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-12-13 14:45:43 +01:00
|
|
|
#include <inttypes.h>
|
2012-09-17 20:35:37 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-12-07 15:25:16 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Configuration ************************************************************/
|
2019-12-07 15:25:16 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Per the data sheet, the W25 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_W25_SPIMODE to select the best mode
|
|
|
|
* for your device. If CONFIG_W25_SPIMODE is not defined, mode 0 will be
|
|
|
|
* used.
|
2012-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_W25_SPIMODE
|
|
|
|
# define CONFIG_W25_SPIMODE SPIDEV_MODE0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SPI Frequency. May be up to 25MHz. */
|
|
|
|
|
|
|
|
#ifndef CONFIG_W25_SPIFREQUENCY
|
|
|
|
# define CONFIG_W25_SPIFREQUENCY 20000000
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* W25 Instructions *********************************************************/
|
|
|
|
|
|
|
|
#define W25_WREN 0x06 /* Write enable */
|
|
|
|
#define W25_WRDI 0x04 /* Write Disable */
|
|
|
|
#define W25_RDSR 0x05 /* Read status register */
|
|
|
|
#define W25_WRSR 0x01 /* Write Status Register */
|
|
|
|
#define W25_RDDATA 0x03 /* Read data bytes */
|
|
|
|
#define W25_FRD 0x0b /* Higher speed read */
|
|
|
|
#define W25_FRDD 0x3b /* Fast read, dual output */
|
|
|
|
#define W25_PP 0x02 /* Program page */
|
|
|
|
#define W25_BE 0xd8 /* Block Erase (64KB) */
|
|
|
|
#define W25_SE 0x20 /* Sector erase (4KB) */
|
|
|
|
#define W25_CE 0xc7 /* Chip erase */
|
|
|
|
#define W25_PD 0xb9 /* Power down */
|
|
|
|
#define W25_PURDID 0xab /* Release PD, Device ID */
|
|
|
|
#define W25_RDMFID 0x90 /* Read Manufacturer / Device */
|
|
|
|
#define W25_JEDEC_ID 0x9f /* JEDEC ID read */
|
|
|
|
|
|
|
|
/* W25 Registers ************************************************************/
|
2019-12-07 15:25:16 +01:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
/* Read ID (RDID) register values */
|
|
|
|
|
|
|
|
#define W25_MANUFACTURER 0xef /* Winbond Serial Flash */
|
|
|
|
#define W25X16_DEVID 0x14 /* W25X16 device ID (0xab, 0x90) */
|
|
|
|
#define W25X32_DEVID 0x15 /* W25X16 device ID (0xab, 0x90) */
|
|
|
|
#define W25X64_DEVID 0x16 /* W25X16 device ID (0xab, 0x90) */
|
|
|
|
|
|
|
|
/* JEDEC Read ID register values */
|
|
|
|
|
2020-03-07 18:35:55 +01:00
|
|
|
#define W25_JEDEC_WINBOND 0xef /* Winbond manufacturer ID */
|
|
|
|
#define W25_JEDEC_AMIC 0x37 /* AMIC manufacturer ID */
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
#define W25X_JEDEC_MEMORY_TYPE 0x30 /* W25X memory type */
|
|
|
|
#define W25Q_JEDEC_MEMORY_TYPE_A 0x40 /* W25Q memory type */
|
|
|
|
#define W25Q_JEDEC_MEMORY_TYPE_B 0x60 /* W25Q memory type */
|
2014-04-30 21:31:42 +02:00
|
|
|
#define W25Q_JEDEC_MEMORY_TYPE_C 0x50 /* W25Q memory type */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2014-04-30 21:31:42 +02:00
|
|
|
#define W25_JEDEC_CAPACITY_8MBIT 0x14 /* 256x4096 = 8Mbit memory capacity */
|
2012-09-17 20:35:37 +02:00
|
|
|
#define W25_JEDEC_CAPACITY_16MBIT 0x15 /* 512x4096 = 16Mbit memory capacity */
|
|
|
|
#define W25_JEDEC_CAPACITY_32MBIT 0x16 /* 1024x4096 = 32Mbit memory capacity */
|
|
|
|
#define W25_JEDEC_CAPACITY_64MBIT 0x17 /* 2048x4096 = 64Mbit memory capacity */
|
|
|
|
#define W25_JEDEC_CAPACITY_128MBIT 0x18 /* 4096x4096 = 128Mbit memory capacity */
|
|
|
|
|
2014-04-30 21:31:42 +02:00
|
|
|
#define NSECTORS_8MBIT 256 /* 256 sectors x 4096 bytes/sector = 1Mb */
|
2012-09-17 20:35:37 +02:00
|
|
|
#define NSECTORS_16MBIT 512 /* 512 sectors x 4096 bytes/sector = 2Mb */
|
|
|
|
#define NSECTORS_32MBIT 1024 /* 1024 sectors x 4096 bytes/sector = 4Mb */
|
|
|
|
#define NSECTORS_64MBIT 2048 /* 2048 sectors x 4096 bytes/sector = 8Mb */
|
|
|
|
#define NSECTORS_128MBIT 4096 /* 4096 sectors x 4096 bytes/sector = 16Mb */
|
|
|
|
|
|
|
|
/* Status register bit definitions */
|
|
|
|
|
|
|
|
#define W25_SR_BUSY (1 << 0) /* Bit 0: Write in progress */
|
|
|
|
#define W25_SR_WEL (1 << 1) /* Bit 1: Write enable latch bit */
|
|
|
|
#define W25_SR_BP_SHIFT (2) /* Bits 2-5: Block protect bits */
|
|
|
|
#define W25_SR_BP_MASK (15 << W25_SR_BP_SHIFT)
|
|
|
|
# define W25X16_SR_BP_NONE (0 << W25_SR_BP_SHIFT) /* Unprotected */
|
|
|
|
# define W25X16_SR_BP_UPPER32nd (1 << W25_SR_BP_SHIFT) /* Upper 32nd */
|
|
|
|
# define W25X16_SR_BP_UPPER16th (2 << W25_SR_BP_SHIFT) /* Upper 16th */
|
|
|
|
# define W25X16_SR_BP_UPPER8th (3 << W25_SR_BP_SHIFT) /* Upper 8th */
|
|
|
|
# define W25X16_SR_BP_UPPERQTR (4 << W25_SR_BP_SHIFT) /* Upper quarter */
|
|
|
|
# define W25X16_SR_BP_UPPERHALF (5 << W25_SR_BP_SHIFT) /* Upper half */
|
|
|
|
# define W25X16_SR_BP_ALL (6 << W25_SR_BP_SHIFT) /* All sectors */
|
|
|
|
# define W25X16_SR_BP_LOWER32nd (9 << W25_SR_BP_SHIFT) /* Lower 32nd */
|
|
|
|
# define W25X16_SR_BP_LOWER16th (10 << W25_SR_BP_SHIFT) /* Lower 16th */
|
|
|
|
# define W25X16_SR_BP_LOWER8th (11 << W25_SR_BP_SHIFT) /* Lower 8th */
|
|
|
|
# define W25X16_SR_BP_LOWERQTR (12 << W25_SR_BP_SHIFT) /* Lower quarter */
|
|
|
|
# define W25X16_SR_BP_LOWERHALF (13 << W25_SR_BP_SHIFT) /* Lower half */
|
|
|
|
|
|
|
|
# define W25X32_SR_BP_NONE (0 << W25_SR_BP_SHIFT) /* Unprotected */
|
|
|
|
# define W25X32_SR_BP_UPPER64th (1 << W25_SR_BP_SHIFT) /* Upper 64th */
|
|
|
|
# define W25X32_SR_BP_UPPER32nd (2 << W25_SR_BP_SHIFT) /* Upper 32nd */
|
|
|
|
# define W25X32_SR_BP_UPPER16th (3 << W25_SR_BP_SHIFT) /* Upper 16th */
|
|
|
|
# define W25X32_SR_BP_UPPER8th (4 << W25_SR_BP_SHIFT) /* Upper 8th */
|
|
|
|
# define W25X32_SR_BP_UPPERQTR (5 << W25_SR_BP_SHIFT) /* Upper quarter */
|
|
|
|
# define W25X32_SR_BP_UPPERHALF (6 << W25_SR_BP_SHIFT) /* Upper half */
|
|
|
|
# define W25X32_SR_BP_ALL (7 << W25_SR_BP_SHIFT) /* All sectors */
|
|
|
|
# define W25X32_SR_BP_LOWER64th (9 << W25_SR_BP_SHIFT) /* Lower 64th */
|
|
|
|
# define W25X32_SR_BP_LOWER32nd (10 << W25_SR_BP_SHIFT) /* Lower 32nd */
|
|
|
|
# define W25X32_SR_BP_LOWER16th (11 << W25_SR_BP_SHIFT) /* Lower 16th */
|
|
|
|
# define W25X32_SR_BP_LOWER8th (12 << W25_SR_BP_SHIFT) /* Lower 8th */
|
|
|
|
# define W25X32_SR_BP_LOWERQTR (13 << W25_SR_BP_SHIFT) /* Lower quarter */
|
|
|
|
# define W25X32_SR_BP_LOWERHALF (14 << W25_SR_BP_SHIFT) /* Lower half */
|
|
|
|
|
|
|
|
# define W25X64_SR_BP_NONE (0 << W25_SR_BP_SHIFT) /* Unprotected */
|
|
|
|
# define W25X64_SR_BP_UPPER64th (1 << W25_SR_BP_SHIFT) /* Upper 64th */
|
|
|
|
# define W25X64_SR_BP_UPPER32nd (2 << W25_SR_BP_SHIFT) /* Upper 32nd */
|
|
|
|
# define W25X64_SR_BP_UPPER16th (3 << W25_SR_BP_SHIFT) /* Upper 16th */
|
|
|
|
# define W25X64_SR_BP_UPPER8th (4 << W25_SR_BP_SHIFT) /* Upper 8th */
|
|
|
|
# define W25X64_SR_BP_UPPERQTR (5 << W25_SR_BP_SHIFT) /* Upper quarter */
|
|
|
|
# define W25X64_SR_BP_UPPERHALF (6 << W25_SR_BP_SHIFT) /* Upper half */
|
|
|
|
# define W25X46_SR_BP_ALL (7 << W25_SR_BP_SHIFT) /* All sectors */
|
|
|
|
# define W25X64_SR_BP_LOWER64th (9 << W25_SR_BP_SHIFT) /* Lower 64th */
|
|
|
|
# define W25X64_SR_BP_LOWER32nd (10 << W25_SR_BP_SHIFT) /* Lower 32nd */
|
|
|
|
# define W25X64_SR_BP_LOWER16th (11 << W25_SR_BP_SHIFT) /* Lower 16th */
|
|
|
|
# define W25X64_SR_BP_LOWER8th (12 << W25_SR_BP_SHIFT) /* Lower 8th */
|
|
|
|
# define W25X64_SR_BP_LOWERQTR (13 << W25_SR_BP_SHIFT) /* Lower quarter */
|
|
|
|
# define W25X64_SR_BP_LOWERHALF (14 << W25_SR_BP_SHIFT) /* Lower half */
|
2020-12-13 14:45:43 +01:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
/* Bit 6: Reserved */
|
|
|
|
#define W25_SR_SRP (1 << 7) /* Bit 7: Status register write protect */
|
|
|
|
|
|
|
|
#define W25_DUMMY 0xa5
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Chip Geometries **********************************************************/
|
2019-12-07 15:25:16 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* All members of the family support uniform 4K-byte sectors and 256 byte
|
|
|
|
* pages
|
|
|
|
*/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#define W25_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define W25_SECTOR_SIZE (1 << 12) /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define W25_PAGE_SHIFT 8 /* Sector size 1 << 8 = 256b */
|
|
|
|
#define W25_PAGE_SIZE (1 << 8) /* Sector size 1 << 8 = 256b */
|
|
|
|
|
|
|
|
#ifdef CONFIG_W25_SECTOR512 /* Simulate a 512 byte sector */
|
|
|
|
# define W25_SECTOR512_SHIFT 9 /* Sector size 1 << 9 = 512 bytes */
|
|
|
|
# define W25_SECTOR512_SIZE (1 << 9) /* Sector size 1 << 9 = 512 bytes */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define W25_ERASED_STATE 0xff /* State of FLASH when erased */
|
|
|
|
|
|
|
|
/* Cache flags */
|
|
|
|
|
|
|
|
#define W25_CACHE_VALID (1 << 0) /* 1=Cache has valid data */
|
|
|
|
#define W25_CACHE_DIRTY (1 << 1) /* 1=Cache is dirty */
|
|
|
|
#define W25_CACHE_ERASED (1 << 2) /* 1=Backing FLASH is erased */
|
|
|
|
|
|
|
|
#define IS_VALID(p) ((((p)->flags) & W25_CACHE_VALID) != 0)
|
|
|
|
#define IS_DIRTY(p) ((((p)->flags) & W25_CACHE_DIRTY) != 0)
|
2016-09-02 15:27:57 +02:00
|
|
|
#define IS_ERASED(p) ((((p)->flags) & W25_CACHE_ERASED) != 0)
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#define SET_VALID(p) do { (p)->flags |= W25_CACHE_VALID; } while (0)
|
|
|
|
#define SET_DIRTY(p) do { (p)->flags |= W25_CACHE_DIRTY; } while (0)
|
2016-09-02 15:27:57 +02:00
|
|
|
#define SET_ERASED(p) do { (p)->flags |= W25_CACHE_ERASED; } while (0)
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#define CLR_VALID(p) do { (p)->flags &= ~W25_CACHE_VALID; } while (0)
|
|
|
|
#define CLR_DIRTY(p) do { (p)->flags &= ~W25_CACHE_DIRTY; } while (0)
|
2016-09-02 15:27:57 +02:00
|
|
|
#define CLR_ERASED(p) do { (p)->flags &= ~W25_CACHE_ERASED; } while (0)
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +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
|
|
|
|
* w25_dev_s.
|
2012-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct w25_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct spi_dev_s *spi; /* Saved SPI interface instance */
|
|
|
|
uint16_t nsectors; /* Number of erase sectors */
|
2017-05-31 17:17:58 +02:00
|
|
|
uint8_t prev_instr; /* Previous instruction given to W25 device */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_W25_SECTOR512) && !defined(CONFIG_W25_READONLY)
|
|
|
|
uint8_t flags; /* Buffered sector flags */
|
2020-12-13 14:45:43 +01:00
|
|
|
uint16_t esectno; /* Erase sector number in the cache */
|
2012-09-17 20:35:37 +02:00
|
|
|
FAR uint8_t *sector; /* Allocated sector data */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static void w25_lock(FAR struct spi_dev_s *spi);
|
|
|
|
static inline void w25_unlock(FAR struct spi_dev_s *spi);
|
|
|
|
static inline int w25_readid(FAR struct w25_dev_s *priv);
|
|
|
|
#ifndef CONFIG_W25_READONLY
|
|
|
|
static void w25_unprotect(FAR struct w25_dev_s *priv);
|
|
|
|
#endif
|
|
|
|
static uint8_t w25_waitwritecomplete(FAR struct w25_dev_s *priv);
|
|
|
|
static inline void w25_wren(FAR struct w25_dev_s *priv);
|
|
|
|
static inline void w25_wrdi(FAR struct w25_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static bool w25_is_erased(struct w25_dev_s *priv,
|
|
|
|
off_t address,
|
|
|
|
off_t size);
|
|
|
|
static void w25_sectorerase(FAR struct w25_dev_s *priv,
|
|
|
|
off_t offset);
|
2012-09-17 20:35:37 +02:00
|
|
|
static inline int w25_chiperase(FAR struct w25_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static void w25_byteread(FAR struct w25_dev_s *priv,
|
|
|
|
FAR uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes);
|
2012-09-17 20:35:37 +02:00
|
|
|
#ifndef CONFIG_W25_READONLY
|
2021-01-27 16:48:40 +01:00
|
|
|
static void w25_pagewrite(FAR struct w25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t address,
|
|
|
|
size_t nbytes);
|
2012-09-17 20:35:37 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_W25_SECTOR512
|
|
|
|
static void w25_cacheflush(struct w25_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static FAR uint8_t *w25_cacheread(struct w25_dev_s *priv,
|
|
|
|
off_t sector);
|
|
|
|
static void w25_cacheerase(struct w25_dev_s *priv,
|
|
|
|
off_t sector);
|
|
|
|
static void w25_cachewrite(FAR struct w25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t sector, size_t nsectors);
|
2012-09-17 20:35:37 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int w25_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t w25_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR uint8_t *buf);
|
|
|
|
static ssize_t w25_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR const uint8_t *buf);
|
|
|
|
static ssize_t w25_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR uint8_t *buffer);
|
|
|
|
static int w25_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2015-11-20 14:34:07 +01:00
|
|
|
#if defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_W25_READONLY)
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t w25_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2015-11-20 14:34:07 +01:00
|
|
|
FAR const uint8_t *buffer);
|
|
|
|
#endif
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Private Data
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_lock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static void w25_lock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
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-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, true);
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SETMODE(spi, CONFIG_W25_SPIMODE);
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(spi, 0);
|
|
|
|
SPI_SETFREQUENCY(spi, CONFIG_W25_SPIFREQUENCY);
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_unlock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static inline void w25_unlock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, false);
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_readid
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static inline int w25_readid(struct w25_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-09-17 20:35:37 +02:00
|
|
|
|
2017-06-13 15:35:49 +02:00
|
|
|
/* Lock and configure the SPI bus */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
w25_lock(priv->spi);
|
2017-06-13 15:35:49 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
w25_waitwritecomplete(priv);
|
2017-06-13 15:35:49 +02:00
|
|
|
|
|
|
|
/* Select this FLASH part. */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +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->spi, W25_JEDEC_ID);
|
2012-09-17 20:35:37 +02:00
|
|
|
manufacturer = SPI_SEND(priv->spi, W25_DUMMY);
|
|
|
|
memory = SPI_SEND(priv->spi, W25_DUMMY);
|
|
|
|
capacity = SPI_SEND(priv->spi, W25_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and unlock the bus */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2012-09-17 20:35:37 +02:00
|
|
|
w25_unlock(priv->spi);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("manufacturer: %02x memory: %02x capacity: %02x\n",
|
2012-09-17 20:35:37 +02:00
|
|
|
manufacturer, memory, capacity);
|
|
|
|
|
|
|
|
/* Check for a valid manufacturer and memory type */
|
|
|
|
|
2020-03-07 18:35:55 +01:00
|
|
|
if ((manufacturer == W25_JEDEC_WINBOND ||
|
|
|
|
manufacturer == W25_JEDEC_AMIC) &&
|
2014-04-30 21:31:42 +02:00
|
|
|
(memory == W25X_JEDEC_MEMORY_TYPE ||
|
2012-09-17 20:35:37 +02:00
|
|
|
memory == W25Q_JEDEC_MEMORY_TYPE_A ||
|
2014-04-30 21:31:42 +02:00
|
|
|
memory == W25Q_JEDEC_MEMORY_TYPE_B ||
|
|
|
|
memory == W25Q_JEDEC_MEMORY_TYPE_C))
|
2012-09-17 20:35:37 +02:00
|
|
|
{
|
|
|
|
/* Okay.. is it a FLASH capacity that we understand? If so, save
|
|
|
|
* the FLASH capacity.
|
|
|
|
*/
|
|
|
|
|
2014-04-30 21:31:42 +02:00
|
|
|
/* 8M-bit / 1M-byte
|
|
|
|
*
|
|
|
|
* W25Q80BV
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (capacity == W25_JEDEC_CAPACITY_8MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = NSECTORS_8MBIT;
|
|
|
|
}
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
/* 16M-bit / 2M-byte (2,097,152)
|
|
|
|
*
|
|
|
|
* W24X16, W25Q16BV, W25Q16CL, W25Q16CV, W25Q16DW
|
|
|
|
*/
|
|
|
|
|
2014-04-30 21:31:42 +02:00
|
|
|
else if (capacity == W25_JEDEC_CAPACITY_16MBIT)
|
2012-09-17 20:35:37 +02:00
|
|
|
{
|
|
|
|
priv->nsectors = NSECTORS_16MBIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 32M-bit / M-byte (4,194,304)
|
|
|
|
*
|
|
|
|
* W25X32, W25Q32BV, W25Q32DW
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (capacity == W25_JEDEC_CAPACITY_32MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = NSECTORS_32MBIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 64M-bit / 8M-byte (8,388,608)
|
|
|
|
*
|
|
|
|
* W25X64, W25Q64BV, W25Q64CV, W25Q64DW
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (capacity == W25_JEDEC_CAPACITY_64MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = NSECTORS_64MBIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 128M-bit / 16M-byte (16,777,216)
|
|
|
|
*
|
|
|
|
* W25Q128BV
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (capacity == W25_JEDEC_CAPACITY_128MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = NSECTORS_128MBIT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Nope.. we don't understand this capacity. */
|
|
|
|
|
2020-03-06 20:01:57 +01:00
|
|
|
ferr("ERROR: Unsupported capacity: %02x\n", capacity);
|
2012-09-17 20:35:37 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't understand the manufacturer or the memory type */
|
|
|
|
|
2020-03-06 20:01:57 +01:00
|
|
|
ferr("ERROR: Unrecognized manufacturer/memory type: %02x/%02x\n",
|
|
|
|
manufacturer, memory);
|
2012-09-17 20:35:37 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_unprotect
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_W25_READONLY
|
|
|
|
static void w25_unprotect(FAR struct w25_dev_s *priv)
|
|
|
|
{
|
2017-06-13 15:33:34 +02:00
|
|
|
/* Lock and configure the SPI bus */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2017-06-13 15:33:34 +02:00
|
|
|
w25_lock(priv->spi);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2017-06-13 15:35:49 +02:00
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
w25_waitwritecomplete(priv);
|
2017-06-13 15:35:49 +02:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
/* Send "Write enable (WREN)" */
|
|
|
|
|
|
|
|
w25_wren(priv);
|
|
|
|
|
2017-06-13 15:33:34 +02:00
|
|
|
/* Select this FLASH part */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send "Write enable status (EWSR)" */
|
|
|
|
|
|
|
|
SPI_SEND(priv->spi, W25_WRSR);
|
|
|
|
|
|
|
|
/* Following by the new status value */
|
|
|
|
|
|
|
|
SPI_SEND(priv->spi, 0);
|
|
|
|
SPI_SEND(priv->spi, 0);
|
2017-06-13 15:33:34 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH and unlock the bus */
|
|
|
|
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
|
|
|
w25_unlock(priv->spi);
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_waitwritecomplete
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static uint8_t w25_waitwritecomplete(struct w25_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
2017-06-13 15:35:49 +02:00
|
|
|
/* Loop as long as the memory is busy with a write cycle. Device sets BUSY
|
2021-01-27 16:48:40 +01:00
|
|
|
* flag to a 1 state whhen previous write or erase command is still
|
|
|
|
* executing and during this time, device will ignore further instructions
|
|
|
|
* except for "Read Status Register" and "Erase/Program Suspend"
|
|
|
|
* instructions.
|
2017-06-13 15:35:49 +02:00
|
|
|
*/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send "Read Status Register (RDSR)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_RDSR);
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
status = SPI_SEND(priv->spi, W25_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2012-09-17 20:35:37 +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.
|
|
|
|
* Delay would slow down writing too much, so go to sleep only if
|
|
|
|
* previous operation was not a page program operation.
|
2012-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
2017-05-31 17:17:58 +02:00
|
|
|
if (priv->prev_instr != W25_PP && (status & W25_SR_BUSY) != 0)
|
2012-09-17 20:35:37 +02:00
|
|
|
{
|
|
|
|
w25_unlock(priv->spi);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2012-09-17 20:35:37 +02:00
|
|
|
w25_lock(priv->spi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((status & W25_SR_BUSY) != 0);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_wren
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static inline void w25_wren(struct w25_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send "Write Enable (WREN)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_WREN);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_wrdi
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static inline void w25_wrdi(struct w25_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send "Write Disable (WRDI)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_WRDI);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2017-05-31 17:09:24 +02:00
|
|
|
* Name: w25_is_erased
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2017-05-31 17:09:24 +02:00
|
|
|
|
|
|
|
static bool w25_is_erased(struct w25_dev_s *priv, off_t address, off_t size)
|
|
|
|
{
|
|
|
|
size_t npages = size >> W25_PAGE_SHIFT;
|
|
|
|
uint32_t erased_32;
|
|
|
|
unsigned int i;
|
|
|
|
uint32_t *buf;
|
|
|
|
|
|
|
|
DEBUGASSERT((address % W25_PAGE_SIZE) == 0);
|
|
|
|
DEBUGASSERT((size % W25_PAGE_SIZE) == 0);
|
|
|
|
|
|
|
|
buf = kmm_malloc(W25_PAGE_SIZE);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&erased_32, W25_ERASED_STATE, sizeof(erased_32));
|
|
|
|
|
|
|
|
/* Walk all pages in given area. */
|
|
|
|
|
|
|
|
while (npages)
|
|
|
|
{
|
|
|
|
/* Check if all bytes of page is in erased state. */
|
|
|
|
|
|
|
|
w25_byteread(priv, (unsigned char *)buf, address, W25_PAGE_SIZE);
|
|
|
|
|
|
|
|
for (i = 0; i < W25_PAGE_SIZE / sizeof(uint32_t); i++)
|
|
|
|
{
|
|
|
|
if (buf[i] != erased_32)
|
|
|
|
{
|
|
|
|
/* Page not in erased state! */
|
|
|
|
|
|
|
|
kmm_free(buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
address += W25_PAGE_SIZE;
|
|
|
|
npages--;
|
|
|
|
}
|
|
|
|
|
|
|
|
kmm_free(buf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_sectorerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static void w25_sectorerase(struct w25_dev_s *priv, off_t sector)
|
|
|
|
{
|
|
|
|
off_t address = sector << W25_SECTOR_SHIFT;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %08lx\n", (long)sector);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2017-05-31 17:09:24 +02:00
|
|
|
/* Check if sector is already erased. */
|
|
|
|
|
|
|
|
if (w25_is_erased(priv, address, W25_SECTOR_SIZE))
|
|
|
|
{
|
|
|
|
/* Sector already in erased state, so skip erase. */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
w25_waitwritecomplete(priv);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
w25_wren(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send the "Sector Erase (SE)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_SE);
|
2017-05-31 17:17:58 +02:00
|
|
|
priv->prev_instr = W25_SE;
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->spi, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->spi, address & 0xff);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_chiperase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static inline int w25_chiperase(struct w25_dev_s *priv)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("priv: %p\n", priv);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
w25_waitwritecomplete(priv);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
w25_wren(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send the "Chip Erase (CE)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_CE);
|
2017-05-31 17:17:58 +02:00
|
|
|
priv->prev_instr = W25_CE;
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return: OK\n");
|
2012-09-17 20:35:37 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_byteread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static void w25_byteread(FAR struct w25_dev_s *priv, FAR uint8_t *buffer,
|
|
|
|
off_t address, size_t nbytes)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("address: %08lx nbytes: %d\n", (long)address, (int)nbytes);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
|
|
|
status = w25_waitwritecomplete(priv);
|
2015-10-10 18:41:00 +02:00
|
|
|
DEBUGASSERT((status & (W25_SR_WEL | W25_SR_BP_MASK)) == 0);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Make sure that writing is disabled */
|
|
|
|
|
|
|
|
w25_wrdi(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send "Read from Memory " instruction */
|
|
|
|
|
|
|
|
#ifdef CONFIG_W25_SLOWREAD
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_RDDATA);
|
2017-05-31 17:17:58 +02:00
|
|
|
priv->prev_instr = W25_RDDATA;
|
2012-09-17 20:35:37 +02:00
|
|
|
#else
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_FRD);
|
2017-05-31 17:17:58 +02:00
|
|
|
priv->prev_instr = W25_FRD;
|
2012-09-17 20:35:37 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->spi, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->spi, address & 0xff);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send a dummy byte */
|
|
|
|
|
|
|
|
#ifndef CONFIG_W25_SLOWREAD
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_DUMMY);
|
2012-09-17 20:35:37 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then read all of the requested bytes */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->spi, buffer, nbytes);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_pagewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_W25_READONLY
|
|
|
|
static void w25_pagewrite(struct w25_dev_s *priv, FAR const uint8_t *buffer,
|
|
|
|
off_t address, size_t nbytes)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("address: %08lx nwords: %d\n", (long)address, (int)nbytes);
|
2012-10-04 20:42:28 +02:00
|
|
|
DEBUGASSERT(priv && buffer && (address & 0xff) == 0 &&
|
2012-09-17 20:35:37 +02:00
|
|
|
(nbytes & 0xff) == 0);
|
|
|
|
|
|
|
|
for (; nbytes > 0; nbytes -= W25_PAGE_SIZE)
|
|
|
|
{
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
|
|
|
status = w25_waitwritecomplete(priv);
|
2015-10-10 18:41:00 +02:00
|
|
|
DEBUGASSERT((status & (W25_SR_WEL | W25_SR_BP_MASK)) == 0);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Enable write access to the FLASH */
|
|
|
|
|
|
|
|
w25_wren(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send the "Page Program (W25_PP)" Command */
|
|
|
|
|
|
|
|
SPI_SEND(priv->spi, W25_PP);
|
2017-05-31 17:17:58 +02:00
|
|
|
priv->prev_instr = W25_PP;
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, (address >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->spi, (address >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->spi, address & 0xff);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Then send the page of data */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->spi, buffer, W25_PAGE_SIZE);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and setup for the next pass through the loop */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Update addresses */
|
|
|
|
|
|
|
|
address += W25_PAGE_SIZE;
|
|
|
|
buffer += W25_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable writing */
|
|
|
|
|
|
|
|
w25_wrdi(priv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2015-11-20 14:34:07 +01:00
|
|
|
* Name: w25_bytewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2015-11-20 14:34:07 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_W25_READONLY)
|
2021-01-27 16:48:40 +01:00
|
|
|
static inline void w25_bytewrite(struct w25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t offset,
|
|
|
|
uint16_t count)
|
2015-11-20 14:34:07 +01:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx count:%d\n", (long)offset, count);
|
2015-11-20 14:34:07 +01:00
|
|
|
|
|
|
|
/* Wait for any preceding write to complete. We could simplify things by
|
|
|
|
* perform this wait at the end of each write operation (rather than at
|
|
|
|
* the beginning of ALL operations), but have the wait first will slightly
|
|
|
|
* improve performance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
w25_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Enable the write access to the FLASH */
|
|
|
|
|
|
|
|
w25_wren(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), true);
|
2015-11-20 14:34:07 +01:00
|
|
|
|
|
|
|
/* Send "Page Program (PP)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, W25_PP);
|
2017-05-31 17:17:58 +02:00
|
|
|
priv->prev_instr = W25_PP;
|
2015-11-20 14:34:07 +01:00
|
|
|
|
|
|
|
/* Send the page offset high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, (offset >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->spi, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->spi, offset & 0xff);
|
2015-11-20 14:34:07 +01:00
|
|
|
|
|
|
|
/* Then write the specified number of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->spi, buffer, count);
|
|
|
|
|
|
|
|
/* Deselect the FLASH: Chip Select high */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(0), false);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Written\n");
|
2015-11-20 14:34:07 +01:00
|
|
|
}
|
|
|
|
#endif /* defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_W25_READONLY) */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_cacheflush
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_W25_SECTOR512) && !defined(CONFIG_W25_READONLY)
|
|
|
|
static void w25_cacheflush(struct w25_dev_s *priv)
|
|
|
|
{
|
2020-03-07 18:35:55 +01:00
|
|
|
/* If the cached is dirty (meaning that it no longer matches the old FLASH
|
2021-01-27 16:48:40 +01:00
|
|
|
* contents) or was erased (with the cache containing the correct FLASH
|
|
|
|
* contents), then write the cached erase block to FLASH.
|
2012-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (IS_DIRTY(priv) || IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
/* Write entire erase block to FLASH */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
w25_pagewrite(priv, priv->sector,
|
|
|
|
(off_t)priv->esectno << W25_SECTOR_SHIFT,
|
2018-06-23 20:53:27 +02:00
|
|
|
W25_SECTOR_SIZE);
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
* Name: w25_cacheread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_W25_SECTOR512) && !defined(CONFIG_W25_READONLY)
|
|
|
|
static FAR uint8_t *w25_cacheread(struct w25_dev_s *priv, off_t sector)
|
|
|
|
{
|
|
|
|
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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
2018-06-23 20:53:27 +02:00
|
|
|
shift = W25_SECTOR_SHIFT - W25_SECTOR512_SHIFT;
|
|
|
|
esectno = sector >> shift;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %ld esectno: %d shift=%d\n", sector, esectno, shift);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Check if the requested erase block is already in the cache */
|
|
|
|
|
|
|
|
if (!IS_VALID(priv) || esectno != priv->esectno)
|
|
|
|
{
|
|
|
|
/* No.. Flush any dirty erase block currently in the cache */
|
|
|
|
|
|
|
|
w25_cacheflush(priv);
|
|
|
|
|
|
|
|
/* Read the erase block into the cache */
|
|
|
|
|
2019-12-07 15:25:16 +01:00
|
|
|
w25_byteread(priv, priv->sector, (esectno << W25_SECTOR_SHIFT),
|
|
|
|
W25_SECTOR_SIZE);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Mark the sector as cached */
|
|
|
|
|
|
|
|
priv->esectno = esectno;
|
|
|
|
|
|
|
|
SET_VALID(priv); /* The data in the cache is valid */
|
|
|
|
CLR_DIRTY(priv); /* It should match the FLASH contents */
|
|
|
|
CLR_ERASED(priv); /* The underlying FLASH has not been erased */
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Get the index to the 512 sector in the erase block that holds the
|
|
|
|
* argument
|
|
|
|
*/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
index = sector & ((1 << shift) - 1);
|
|
|
|
|
|
|
|
/* Return the address in the cache that holds this sector */
|
|
|
|
|
|
|
|
return &priv->sector[index << W25_SECTOR512_SHIFT];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_cacheerase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_W25_SECTOR512) && !defined(CONFIG_W25_READONLY)
|
|
|
|
static void w25_cacheerase(struct w25_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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
dest = w25_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 block.
|
2012-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!IS_ERASED(priv))
|
|
|
|
{
|
|
|
|
off_t esectno = sector >> (W25_SECTOR_SHIFT - W25_SECTOR512_SHIFT);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %ld esectno: %d\n", sector, esectno);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
w25_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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
memset(dest, W25_ERASED_STATE, W25_SECTOR512_SIZE);
|
|
|
|
SET_DIRTY(priv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_cachewrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_W25_SECTOR512) && !defined(CONFIG_W25_READONLY)
|
2021-01-27 16:48:40 +01:00
|
|
|
static void w25_cachewrite(FAR struct w25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer,
|
|
|
|
off_t sector,
|
|
|
|
size_t nsectors)
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
dest = w25_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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!IS_ERASED(priv))
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
off_t esectno = sector >>
|
|
|
|
(W25_SECTOR_SHIFT - W25_SECTOR512_SHIFT);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %ld esectno: %d\n", sector, esectno);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
w25_sectorerase(priv, esectno);
|
|
|
|
SET_ERASED(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the new sector data into cached erase block */
|
|
|
|
|
|
|
|
memcpy(dest, buffer, W25_SECTOR512_SIZE);
|
|
|
|
SET_DIRTY(priv);
|
|
|
|
|
|
|
|
/* Set up for the next 512 byte sector */
|
|
|
|
|
|
|
|
buffer += W25_SECTOR512_SIZE;
|
|
|
|
sector++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the last erase block left in the cache */
|
|
|
|
|
|
|
|
w25_cacheflush(priv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int w25_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks)
|
2012-09-17 20:35:37 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_W25_READONLY
|
2023-05-12 00:55:11 +02:00
|
|
|
return -EACCES;
|
2012-09-17 20:35:37 +02:00
|
|
|
#else
|
|
|
|
FAR struct w25_dev_s *priv = (FAR struct w25_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-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Lock access to the SPI bus until we complete the erase */
|
|
|
|
|
|
|
|
w25_lock(priv->spi);
|
|
|
|
|
|
|
|
while (blocksleft-- > 0)
|
|
|
|
{
|
|
|
|
/* Erase each sector */
|
|
|
|
|
|
|
|
#ifdef CONFIG_W25_SECTOR512
|
|
|
|
w25_cacheerase(priv, startblock);
|
|
|
|
#else
|
|
|
|
w25_sectorerase(priv, startblock);
|
|
|
|
#endif
|
|
|
|
startblock++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_W25_SECTOR512
|
|
|
|
/* Flush the last erase block left in the cache */
|
|
|
|
|
|
|
|
w25_cacheflush(priv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
w25_unlock(priv->spi);
|
|
|
|
return (int)nblocks;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t w25_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR uint8_t *buffer)
|
2012-09-17 20:35:37 +02:00
|
|
|
{
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n",
|
|
|
|
(long)startblock, (int)nblocks);
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
|
2012-10-04 20:42:28 +02:00
|
|
|
#ifdef CONFIG_W25_SECTOR512
|
2019-12-07 15:25:16 +01:00
|
|
|
nbytes = w25_read(dev, startblock << W25_SECTOR512_SHIFT,
|
|
|
|
nblocks << W25_SECTOR512_SHIFT, buffer);
|
2012-09-17 20:35:37 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
2012-10-04 20:42:28 +02:00
|
|
|
nbytes >>= W25_SECTOR512_SHIFT;
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
2017-06-12 17:51:42 +02:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
#else
|
2019-12-07 15:25:16 +01:00
|
|
|
nbytes = w25_read(dev, startblock << W25_PAGE_SHIFT,
|
|
|
|
nblocks << W25_PAGE_SHIFT, buffer);
|
2012-09-17 20:35:37 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
2015-11-18 13:56:53 +01:00
|
|
|
nbytes >>= W25_PAGE_SHIFT;
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
#endif
|
2012-10-04 20:42:28 +02:00
|
|
|
|
|
|
|
return nbytes;
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2020-03-07 18:35:55 +01:00
|
|
|
static ssize_t w25_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buffer)
|
2012-09-17 20:35:37 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_W25_READONLY
|
|
|
|
return -EACCESS;
|
|
|
|
#else
|
|
|
|
FAR struct w25_dev_s *priv = (FAR struct w25_dev_s *)dev;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus and write all of the pages to FLASH */
|
|
|
|
|
|
|
|
w25_lock(priv->spi);
|
|
|
|
|
|
|
|
#if defined(CONFIG_W25_SECTOR512)
|
|
|
|
w25_cachewrite(priv, buffer, startblock, nblocks);
|
|
|
|
#else
|
2015-11-18 13:56:53 +01:00
|
|
|
w25_pagewrite(priv, buffer, startblock << W25_PAGE_SHIFT,
|
|
|
|
nblocks << W25_PAGE_SHIFT);
|
2012-09-17 20:35:37 +02:00
|
|
|
#endif
|
|
|
|
w25_unlock(priv->spi);
|
|
|
|
|
|
|
|
return nblocks;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t w25_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR uint8_t *buffer)
|
2012-09-17 20:35:37 +02:00
|
|
|
{
|
|
|
|
FAR struct w25_dev_s *priv = (FAR struct w25_dev_s *)dev;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus and select this FLASH part */
|
|
|
|
|
|
|
|
w25_lock(priv->spi);
|
|
|
|
w25_byteread(priv, buffer, offset, nbytes);
|
|
|
|
w25_unlock(priv->spi);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return nbytes: %d\n", (int)nbytes);
|
2012-09-17 20:35:37 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2015-11-20 14:34:07 +01:00
|
|
|
* Name: w25_write
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2015-11-20 14:34:07 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_W25_READONLY)
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t w25_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2015-11-20 14:34:07 +01:00
|
|
|
FAR const uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct w25_dev_s *priv = (FAR struct w25_dev_s *)dev;
|
|
|
|
int startpage;
|
|
|
|
int endpage;
|
|
|
|
int count;
|
|
|
|
int index;
|
|
|
|
int bytestowrite;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2015-11-20 14:34:07 +01: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 / W25_PAGE_SIZE;
|
|
|
|
endpage = (offset + nbytes) / W25_PAGE_SIZE;
|
|
|
|
|
2017-06-07 00:10:41 +02:00
|
|
|
w25_lock(priv->spi);
|
2015-11-20 14:34:07 +01:00
|
|
|
if (startpage == endpage)
|
|
|
|
{
|
|
|
|
/* All bytes within one programmable page. Just do the write. */
|
|
|
|
|
|
|
|
w25_bytewrite(priv, buffer, offset, nbytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Write the 1st partial-page */
|
|
|
|
|
|
|
|
count = nbytes;
|
2019-12-07 15:25:16 +01:00
|
|
|
bytestowrite = W25_PAGE_SIZE - (offset & (W25_PAGE_SIZE - 1));
|
2015-11-20 14:34:07 +01:00
|
|
|
w25_bytewrite(priv, buffer, offset, bytestowrite);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += bytestowrite;
|
|
|
|
count -= bytestowrite;
|
|
|
|
index = bytestowrite;
|
|
|
|
|
|
|
|
/* Write full pages */
|
|
|
|
|
|
|
|
while (count >= W25_PAGE_SIZE)
|
|
|
|
{
|
|
|
|
w25_bytewrite(priv, &buffer[index], offset, W25_PAGE_SIZE);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += W25_PAGE_SIZE;
|
|
|
|
count -= W25_PAGE_SIZE;
|
|
|
|
index += W25_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now write any partial page at the end */
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
w25_bytewrite(priv, &buffer[index], offset, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:10:41 +02:00
|
|
|
w25_unlock(priv->spi);
|
2015-11-20 14:34:07 +01:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
#endif /* defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_W25_READONLY) */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
static int w25_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct w25_dev_s *priv = (FAR struct w25_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-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2019-12-07 15:25:16 +01:00
|
|
|
FAR struct mtd_geometry_s *geo =
|
|
|
|
(FAR struct mtd_geometry_s *)((uintptr_t)arg);
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_W25_SECTOR512
|
|
|
|
geo->blocksize = (1 << W25_SECTOR512_SHIFT);
|
|
|
|
geo->erasesize = (1 << W25_SECTOR512_SHIFT);
|
2019-12-07 15:25:16 +01:00
|
|
|
geo->neraseblocks = priv->nsectors <<
|
|
|
|
(W25_SECTOR_SHIFT - W25_SECTOR512_SHIFT);
|
2012-09-17 20:35:37 +02:00
|
|
|
#else
|
2015-11-18 13:56:53 +01:00
|
|
|
geo->blocksize = W25_PAGE_SIZE;
|
2012-09-17 20:35:37 +02:00
|
|
|
geo->erasesize = W25_SECTOR_SIZE;
|
|
|
|
geo->neraseblocks = priv->nsectors;
|
|
|
|
#endif
|
|
|
|
ret = OK;
|
|
|
|
|
2020-12-13 14:45:43 +01:00
|
|
|
finfo("blocksize: %" PRIu32 " erasesize: %" PRIu32
|
|
|
|
" neraseblocks: %" PRIu32 "\n",
|
2012-09-17 20:35:37 +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_W25_SECTOR512
|
|
|
|
info->numsectors = priv->nsectors <<
|
|
|
|
(W25_SECTOR_SHIFT - W25_SECTOR512_SHIFT);
|
|
|
|
info->sectorsize = 1 << W25_SECTOR512_SHIFT;
|
|
|
|
#else
|
|
|
|
info->numsectors = priv->nsectors *
|
|
|
|
W25_SECTOR_SIZE / W25_PAGE_SIZE;
|
|
|
|
info->sectorsize = W25_PAGE_SIZE;
|
|
|
|
#endif
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
w25_lock(priv->spi);
|
|
|
|
ret = w25_chiperase(priv);
|
|
|
|
w25_unlock(priv->spi);
|
|
|
|
}
|
2021-07-16 19:16:41 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_ERASESTATE:
|
|
|
|
{
|
|
|
|
FAR uint8_t *result = (FAR uint8_t *)arg;
|
|
|
|
*result = W25_ERASED_STATE;
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
2012-09-17 20:35:37 +02:00
|
|
|
break;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("return %d\n", ret);
|
2012-09-17 20:35:37 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2012-09-17 20:35:37 +02:00
|
|
|
* Name: w25_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Create an initialize MTD device instance. MTD devices are not registered
|
2012-09-17 20:35:37 +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
|
|
|
****************************************************************************/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
FAR struct mtd_dev_s *w25_initialize(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
|
|
|
FAR struct w25_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("spi: %p\n", spi);
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
*/
|
|
|
|
|
2014-09-01 01:34:44 +02:00
|
|
|
priv = (FAR struct w25_dev_s *)kmm_zalloc(sizeof(struct w25_dev_s));
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
priv->mtd.erase = w25_erase;
|
|
|
|
priv->mtd.bread = w25_bread;
|
|
|
|
priv->mtd.bwrite = w25_bwrite;
|
|
|
|
priv->mtd.read = w25_read;
|
|
|
|
priv->mtd.ioctl = w25_ioctl;
|
2015-11-20 14:34:07 +01:00
|
|
|
#if defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_W25_READONLY)
|
|
|
|
priv->mtd.write = w25_write;
|
|
|
|
#endif
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "w25";
|
2012-09-17 20:35:37 +02:00
|
|
|
priv->spi = spi;
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_FLASH(0), false);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Identify the FLASH chip and get its capacity */
|
|
|
|
|
|
|
|
ret = w25_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-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-13 14:45:43 +01:00
|
|
|
/* Make sure that the FLASH is unprotected so that we can write
|
|
|
|
* into it.
|
|
|
|
*/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_W25_READONLY
|
|
|
|
w25_unprotect(priv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_W25_SECTOR512 /* Simulate a 512 byte sector */
|
|
|
|
/* Allocate a buffer for the erase block cache */
|
|
|
|
|
2014-09-01 01:26:36 +02:00
|
|
|
priv->sector = (FAR uint8_t *)kmm_malloc(W25_SECTOR_SIZE);
|
2012-09-17 20:35:37 +02:00
|
|
|
if (!priv->sector)
|
|
|
|
{
|
2020-12-13 14:45:43 +01:00
|
|
|
/* Discard all of that work we just did and return NULL */
|
2012-09-17 20:35:37 +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-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return %p\n", priv);
|
2012-09-17 20:35:37 +02:00
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|