2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* drivers/mtd/gd25.c
|
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-08-28 14:31:28 +02:00
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-08-28 14:31:28 +02:00
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-08-28 14:31:28 +02:00
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* Included Files
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-12-13 14:44:21 +01:00
|
|
|
#include <inttypes.h>
|
2018-08-28 14:31:28 +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>
|
|
|
|
#include <nuttx/signal.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Configuration
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_GD25_SPIMODE
|
|
|
|
# define CONFIG_GD25_SPIMODE SPIDEV_MODE0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_GD25_SPIFREQUENCY
|
|
|
|
# define CONFIG_GD25_SPIFREQUENCY 20000000
|
|
|
|
#endif
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* GD25 Instructions
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Command Value Description */
|
2019-10-29 14:47:03 +01:00
|
|
|
|
2018-08-28 14:31:28 +02:00
|
|
|
#define GD25_WREN 0x06 /* Write enable */
|
|
|
|
#define GD25_WRDI 0x04 /* Write Disable */
|
|
|
|
#define GD25_RDSR 0x05 /* Read status register */
|
2019-10-25 15:31:00 +02:00
|
|
|
#define GD25_RDSR1 0x35 /* Read status register-1 */
|
2018-08-28 14:31:28 +02:00
|
|
|
#define GD25_WRSR 0x01 /* Write Status Register */
|
|
|
|
#define GD25_RDDATA 0x03 /* Read data bytes */
|
|
|
|
#define GD25_FRD 0x0b /* Higher speed read */
|
|
|
|
#define GD25_FRDD 0x3b /* Fast read, dual output */
|
|
|
|
#define GD25_PP 0x02 /* Program page */
|
|
|
|
#define GD25_SE 0x20 /* Sector erase (4KB) */
|
|
|
|
#define GD25_BE 0xd8 /* Block Erase (64KB) */
|
|
|
|
#define GD25_CE 0xc7 /* Chip erase */
|
|
|
|
#define GD25_PD 0xb9 /* Power down */
|
|
|
|
#define GD25_PURDID 0xab /* Release PD, Device ID */
|
|
|
|
#define GD25_RDMFID 0x90 /* Read Manufacturer / Device */
|
|
|
|
#define GD25_JEDEC_ID 0x9f /* JEDEC ID read */
|
2019-10-25 15:31:00 +02:00
|
|
|
#define GD25_4BEN 0xb7 /* Enable 4-byte Mode */
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* GD25 Registers
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* JEDEC Read ID register values */
|
|
|
|
|
2019-09-15 16:17:19 +02:00
|
|
|
#define P25_JEDEC_MANUFACTURER 0x85
|
2018-08-28 14:31:28 +02:00
|
|
|
#define GD25_JEDEC_MANUFACTURER 0xc8 /* GigaDevice manufacturer ID */
|
|
|
|
#define GD25L_JEDEC_MEMORY_TYPE 0x60 /* GD25L memory type, 1.8V */
|
|
|
|
#define GD25Q_JEDEC_MEMORY_TYPE 0x40 /* GD25Q memory type, 3V */
|
|
|
|
|
|
|
|
#define GD25_JEDEC_CAPACITY_8MBIT 0x14 /* 256x4096 = 8Mbit memory capacity */
|
|
|
|
#define GD25_JEDEC_CAPACITY_16MBIT 0x15 /* 512x4096 = 16Mbit memory capacity */
|
|
|
|
#define GD25_JEDEC_CAPACITY_32MBIT 0x16 /* 1024x4096 = 32Mbit memory capacity */
|
|
|
|
#define GD25_JEDEC_CAPACITY_64MBIT 0x17 /* 2048x4096 = 64Mbit memory capacity */
|
|
|
|
#define GD25_JEDEC_CAPACITY_128MBIT 0x18 /* 4096x4096 = 128Mbit memory capacity */
|
2019-09-15 16:19:06 +02:00
|
|
|
#define GD25_JEDEC_CAPACITY_256MBIT 0x19 /* 8192x4096 = 256Mbit memory capacity */
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
#define GD25_NSECTORS_8MBIT 256 /* 256 sectors x 4096 bytes/sector = 1Mb */
|
|
|
|
#define GD25_NSECTORS_16MBIT 512 /* 512 sectors x 4096 bytes/sector = 2Mb */
|
|
|
|
#define GD25_NSECTORS_32MBIT 1024 /* 1024 sectors x 4096 bytes/sector = 4Mb */
|
|
|
|
#define GD25_NSECTORS_64MBIT 2048 /* 2048 sectors x 4096 bytes/sector = 8Mb */
|
|
|
|
#define GD25_NSECTORS_128MBIT 4096 /* 4096 sectors x 4096 bytes/sector = 16Mb */
|
2019-09-15 16:19:06 +02:00
|
|
|
#define GD25_NSECTORS_256MBIT 8192 /* 8192 sectors x 4096 bytes/sector = 32Mb */
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Status register bit definitions */
|
|
|
|
|
|
|
|
#define GD25_SR_WIP (1 << 0) /* Bit 0: Write in Progress */
|
|
|
|
#define GD25_SR_WEL (1 << 1) /* Bit 1: Write Enable Latch */
|
2019-10-25 15:31:00 +02:00
|
|
|
#define GD25_SR1_EN4B (1 << 3) /* Bit 3: Enable 4byte address */
|
2022-11-24 19:24:16 +01:00
|
|
|
#define GD25Q_SR1_EN4B (1 << 0) /* Bit 0: Enable 4byte address GD25Q memories */
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
#define GD25_DUMMY 0x00
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Chip Geometries
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/* All members of the family support uniform 4KB sectors and 256B pages */
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
#define GD25_SECTOR_SHIFT 12 /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define GD25_SECTOR_SIZE (1 << 12) /* Sector size 1 << 12 = 4Kb */
|
|
|
|
#define GD25_PAGE_SHIFT 8 /* Sector size 1 << 8 = 256b */
|
|
|
|
#define GD25_PAGE_SIZE (1 << 8) /* Sector size 1 << 8 = 256b */
|
|
|
|
|
|
|
|
#define GD25_ERASED_STATE 0xff /* State of FLASH when erased */
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Private Types
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/* This type represents the state of the MTD device. The struct mtd_dev_s
|
|
|
|
* must appear at the beginning of the definition so that you can freely
|
|
|
|
* cast between pointers to struct mtd_dev_s and struct gd25_dev_s.
|
2018-08-28 14:31:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct gd25_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct spi_dev_s *spi; /* Saved SPI interface instance */
|
2019-10-31 14:43:21 +01:00
|
|
|
uint32_t spi_devid; /* Chip select inputs */
|
2018-08-28 14:31:28 +02:00
|
|
|
uint16_t nsectors; /* Number of erase sectors */
|
|
|
|
uint8_t prev_instr; /* Previous instruction given to GD25 device */
|
2019-10-25 15:31:00 +02:00
|
|
|
bool addr_4byte; /* True: Use Four-byte address */
|
2022-11-24 19:24:16 +01:00
|
|
|
uint8_t memory; /* memory type read from device */
|
2018-08-28 14:31:28 +02:00
|
|
|
};
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Private Function Prototypes
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
2019-11-12 16:02:19 +01:00
|
|
|
static inline void gd25_purdid(FAR struct gd25_dev_s *priv);
|
|
|
|
static inline void gd25_pd(FAR struct gd25_dev_s *priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
static void gd25_lock(FAR struct spi_dev_s *spi);
|
|
|
|
static inline void gd25_unlock(FAR struct spi_dev_s *spi);
|
|
|
|
static inline int gd25_readid(FAR struct gd25_dev_s *priv);
|
|
|
|
#ifndef CONFIG_GD25_READONLY
|
|
|
|
static void gd25_unprotect(FAR struct gd25_dev_s *priv);
|
|
|
|
#endif
|
|
|
|
static uint8_t gd25_waitwritecomplete(FAR struct gd25_dev_s *priv);
|
|
|
|
static inline void gd25_wren(FAR struct gd25_dev_s *priv);
|
|
|
|
static inline void gd25_wrdi(FAR struct gd25_dev_s *priv);
|
2019-10-25 15:31:00 +02:00
|
|
|
static bool gd25_is_erased(FAR struct gd25_dev_s *priv, off_t address,
|
|
|
|
off_t size);
|
2018-08-28 14:31:28 +02:00
|
|
|
static void gd25_sectorerase(FAR struct gd25_dev_s *priv, off_t offset);
|
|
|
|
static inline int gd25_chiperase(FAR struct gd25_dev_s *priv);
|
|
|
|
static void gd25_byteread(FAR struct gd25_dev_s *priv, FAR uint8_t *buffer,
|
|
|
|
off_t address, size_t nbytes);
|
|
|
|
#ifndef CONFIG_GD25_READONLY
|
|
|
|
static void gd25_pagewrite(FAR struct gd25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, off_t address, size_t nbytes);
|
|
|
|
#endif
|
2019-10-25 15:31:00 +02:00
|
|
|
static inline uint8_t gd25_rdsr(FAR struct gd25_dev_s *priv, uint32_t id);
|
2022-11-24 19:24:16 +01:00
|
|
|
static inline bool gd25_4ben(FAR struct gd25_dev_s *priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
|
|
|
static int gd25_erase(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t gd25_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buf);
|
|
|
|
static ssize_t gd25_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buf);
|
|
|
|
static ssize_t gd25_read(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR uint8_t *buffer);
|
2019-10-25 15:31:00 +02:00
|
|
|
static int gd25_ioctl(FAR struct mtd_dev_s *dev, int cmd,
|
|
|
|
unsigned long arg);
|
2018-08-28 14:31:28 +02:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
static ssize_t gd25_write(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR const uint8_t *buffer);
|
|
|
|
#endif
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Private Functions
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2019-11-12 16:02:19 +01:00
|
|
|
* Name: gd25_purdid
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2019-11-12 16:02:19 +01:00
|
|
|
|
|
|
|
static inline void gd25_purdid(FAR struct gd25_dev_s *priv)
|
|
|
|
{
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_PURDID);
|
2019-11-12 16:02:19 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
|
|
|
up_udelay(20);
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2019-11-12 16:02:19 +01:00
|
|
|
* Name: gd25_pd
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2019-11-12 16:02:19 +01:00
|
|
|
|
|
|
|
static inline void gd25_pd(FAR struct gd25_dev_s *priv)
|
|
|
|
{
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_PD);
|
2019-11-12 16:02:19 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_lock
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
static void gd25_lock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, true);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
SPI_SETMODE(spi, CONFIG_GD25_SPIMODE);
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(spi, 0);
|
|
|
|
SPI_SETFREQUENCY(spi, CONFIG_GD25_SPIFREQUENCY);
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_unlock
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
static inline void gd25_unlock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, false);
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_readid
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
static inline int gd25_readid(FAR struct gd25_dev_s *priv)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
uint16_t manufacturer;
|
|
|
|
uint16_t memory;
|
|
|
|
uint16_t capacity;
|
2019-11-12 16:01:10 +01:00
|
|
|
int ret = -ENODEV;
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Lock and configure the SPI bus */
|
|
|
|
|
|
|
|
gd25_lock(priv->spi);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_purdid(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Select this FLASH part. */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2018-08-28 14:31:28 +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, GD25_JEDEC_ID);
|
2018-08-28 14:31:28 +02:00
|
|
|
manufacturer = SPI_SEND(priv->spi, GD25_DUMMY);
|
|
|
|
memory = SPI_SEND(priv->spi, GD25_DUMMY);
|
|
|
|
capacity = SPI_SEND(priv->spi, GD25_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH and unlock the bus */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
finfo("manufacturer: %02x memory: %02x capacity: %02x\n",
|
|
|
|
manufacturer, memory, capacity);
|
|
|
|
|
|
|
|
/* Check for a valid manufacturer and memory type */
|
|
|
|
|
2019-09-15 16:17:19 +02:00
|
|
|
if ((manufacturer == GD25_JEDEC_MANUFACTURER ||
|
|
|
|
manufacturer == P25_JEDEC_MANUFACTURER) &&
|
2018-08-28 14:31:28 +02:00
|
|
|
(memory == GD25L_JEDEC_MEMORY_TYPE ||
|
|
|
|
memory == GD25Q_JEDEC_MEMORY_TYPE))
|
|
|
|
{
|
|
|
|
if (capacity == GD25_JEDEC_CAPACITY_8MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD25_NSECTORS_8MBIT;
|
|
|
|
}
|
|
|
|
else if (capacity == GD25_JEDEC_CAPACITY_16MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD25_NSECTORS_16MBIT;
|
|
|
|
}
|
|
|
|
else if (capacity == GD25_JEDEC_CAPACITY_32MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD25_NSECTORS_32MBIT;
|
|
|
|
}
|
|
|
|
else if (capacity == GD25_JEDEC_CAPACITY_64MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD25_NSECTORS_64MBIT;
|
|
|
|
}
|
|
|
|
else if (capacity == GD25_JEDEC_CAPACITY_128MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD25_NSECTORS_128MBIT;
|
|
|
|
}
|
2019-09-15 16:19:06 +02:00
|
|
|
else if (capacity == GD25_JEDEC_CAPACITY_256MBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD25_NSECTORS_256MBIT;
|
|
|
|
}
|
2018-08-28 14:31:28 +02:00
|
|
|
else
|
|
|
|
{
|
2019-11-12 16:01:10 +01:00
|
|
|
goto out;
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2022-11-24 19:24:16 +01:00
|
|
|
priv->memory = memory;
|
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
/* Capacity greater than 16MB, Enable four-byte address */
|
|
|
|
|
|
|
|
if (priv->nsectors > GD25_NSECTORS_128MBIT)
|
|
|
|
{
|
2022-11-24 19:24:16 +01:00
|
|
|
if (!gd25_4ben(priv))
|
2019-10-25 15:31:00 +02:00
|
|
|
{
|
|
|
|
ferr("ERROR: capacity %02x: Can't enable 4-byte mode!\n",
|
|
|
|
capacity);
|
2019-11-12 16:01:10 +01:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
2019-10-25 15:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->addr_4byte = true;
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:01:10 +01:00
|
|
|
ret = OK;
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2019-11-12 16:01:10 +01:00
|
|
|
out:
|
|
|
|
/* We don't understand the manufacturer or the memory type.
|
|
|
|
* Or enable four-byte address failed.
|
|
|
|
* Or success.
|
|
|
|
*/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_pd(priv);
|
2019-11-12 16:01:10 +01:00
|
|
|
gd25_unlock(priv->spi);
|
|
|
|
return ret;
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_unprotect
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_GD25_READONLY
|
|
|
|
static void gd25_unprotect(FAR struct gd25_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Lock and configure the SPI bus */
|
|
|
|
|
|
|
|
gd25_lock(priv->spi);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_purdid(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
gd25_waitwritecomplete(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send "Write enable (WREN)" */
|
|
|
|
|
|
|
|
gd25_wren(priv);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send "Write enable status (EWSR)" */
|
|
|
|
|
|
|
|
SPI_SEND(priv->spi, GD25_WRSR);
|
|
|
|
|
|
|
|
/* Following by the new status value */
|
|
|
|
|
|
|
|
SPI_SEND(priv->spi, 0);
|
|
|
|
SPI_SEND(priv->spi, 0);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Unlock the SPI bus */
|
|
|
|
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_pd(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_unlock(priv->spi);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_waitwritecomplete
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
static uint8_t gd25_waitwritecomplete(FAR struct gd25_dev_s *priv)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-10-25 15:31:00 +02:00
|
|
|
status = gd25_rdsr(priv, 0);
|
2018-08-28 14:31:28 +02:00
|
|
|
if (priv->prev_instr != GD25_PP && (status & GD25_SR_WIP) != 0)
|
|
|
|
{
|
|
|
|
gd25_unlock(priv->spi);
|
2019-11-30 00:37:39 +01:00
|
|
|
nxsig_usleep(1000);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_lock(priv->spi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((status & GD25_SR_WIP) != 0);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2019-10-25 15:31:00 +02:00
|
|
|
* Name: gd25_rdsr
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2019-10-25 15:31:00 +02:00
|
|
|
|
|
|
|
static inline uint8_t gd25_rdsr(FAR struct gd25_dev_s *priv, uint32_t id)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t rdsr[2] =
|
|
|
|
{
|
|
|
|
GD25_RDSR, GD25_RDSR1
|
|
|
|
};
|
|
|
|
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, rdsr[id]);
|
2019-10-25 15:31:00 +02:00
|
|
|
status = SPI_SEND(priv->spi, GD25_DUMMY);
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2019-10-25 15:31:00 +02:00
|
|
|
* Name: gd25_4ben
|
2022-11-24 19:24:16 +01:00
|
|
|
*
|
|
|
|
* Enable 4 byte memory addressing mode
|
|
|
|
* Return success or not
|
|
|
|
*
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2019-10-25 15:31:00 +02:00
|
|
|
|
2022-11-24 19:24:16 +01:00
|
|
|
static inline bool gd25_4ben(FAR struct gd25_dev_s *priv)
|
2019-10-25 15:31:00 +02:00
|
|
|
{
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_4BEN);
|
2019-10-25 15:31:00 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2022-11-24 19:24:16 +01:00
|
|
|
if (priv->memory == GD25Q_JEDEC_MEMORY_TYPE)
|
|
|
|
{
|
|
|
|
return ((gd25_rdsr(priv, 1) & GD25Q_SR1_EN4B) == GD25Q_SR1_EN4B);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ((gd25_rdsr(priv, 1) & GD25_SR1_EN4B) == GD25_SR1_EN4B);
|
|
|
|
}
|
2019-10-25 15:31:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_wren
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
static inline void gd25_wren(FAR struct gd25_dev_s *priv)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_WREN);
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_wrdi
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
static inline void gd25_wrdi(FAR struct gd25_dev_s *priv)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_WRDI);
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_is_erased
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
static bool gd25_is_erased(FAR struct gd25_dev_s *priv, off_t address,
|
|
|
|
off_t size)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
size_t npages = size >> GD25_PAGE_SHIFT;
|
|
|
|
uint32_t erased_32;
|
|
|
|
unsigned int i;
|
|
|
|
uint32_t buf[GD25_PAGE_SIZE / sizeof(uint32_t)];
|
|
|
|
|
|
|
|
DEBUGASSERT((address % GD25_PAGE_SIZE) == 0);
|
|
|
|
DEBUGASSERT((size % GD25_PAGE_SIZE) == 0);
|
|
|
|
|
|
|
|
memset(&erased_32, GD25_ERASED_STATE, sizeof(erased_32));
|
|
|
|
|
|
|
|
/* Walk all pages in given area. */
|
|
|
|
|
|
|
|
while (npages)
|
|
|
|
{
|
|
|
|
/* Check if all bytes of page is in erased state. */
|
|
|
|
|
|
|
|
gd25_byteread(priv, (uint8_t *)buf, address, GD25_PAGE_SIZE);
|
|
|
|
|
|
|
|
for (i = 0; i < GD25_PAGE_SIZE / sizeof(uint32_t); i++)
|
|
|
|
{
|
|
|
|
if (buf[i] != erased_32)
|
|
|
|
{
|
|
|
|
/* Page not in erased state! */
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
address += GD25_PAGE_SIZE;
|
|
|
|
npages--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_sectorerase
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
static void gd25_sectorerase(FAR struct gd25_dev_s *priv, off_t sector)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
off_t address = sector << GD25_SECTOR_SHIFT;
|
|
|
|
|
|
|
|
finfo("sector: %08lx\n", (long)sector);
|
|
|
|
|
|
|
|
/* Check if sector is already erased. */
|
|
|
|
|
|
|
|
if (gd25_is_erased(priv, address, GD25_SECTOR_SIZE))
|
|
|
|
{
|
|
|
|
/* Sector already in erased state, so skip erase. */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
gd25_waitwritecomplete(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
gd25_wren(priv);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send the "Sector Erase (SE)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_SE);
|
2018-08-28 14:31:28 +02:00
|
|
|
priv->prev_instr = GD25_SE;
|
|
|
|
|
|
|
|
/* Send the sector address high byte first. Only the most significant
|
|
|
|
* bits (those corresponding to the sector) have any meaning.
|
|
|
|
*/
|
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
if (priv->addr_4byte)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, (address >> 24) & 0xff);
|
2019-10-25 15:31:00 +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);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_chiperase
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
static inline int gd25_chiperase(FAR struct gd25_dev_s *priv)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
gd25_waitwritecomplete(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
gd25_wren(priv);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send the "Chip Erase (CE)" instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_CE);
|
2018-08-28 14:31:28 +02:00
|
|
|
priv->prev_instr = GD25_CE;
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_byteread
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
static void gd25_byteread(FAR struct gd25_dev_s *priv, FAR uint8_t *buffer,
|
2019-10-25 15:31:00 +02:00
|
|
|
off_t address, size_t nbytes)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
finfo("address: %08lx nbytes: %d\n", (long)address, (int)nbytes);
|
|
|
|
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
|
|
|
gd25_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Make sure that writing is disabled */
|
|
|
|
|
|
|
|
gd25_wrdi(priv);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send "Read from Memory " instruction */
|
|
|
|
|
|
|
|
#ifdef CONFIG_GD25_SLOWREAD
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_RDDATA);
|
2018-08-28 14:31:28 +02:00
|
|
|
priv->prev_instr = GD25_RDDATA;
|
|
|
|
#else
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_FRD);
|
2018-08-28 14:31:28 +02:00
|
|
|
priv->prev_instr = GD25_FRD;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
if (priv->addr_4byte)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, (address >> 24) & 0xff);
|
2019-10-25 15:31:00 +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);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send a dummy byte */
|
|
|
|
|
|
|
|
#ifndef CONFIG_GD25_SLOWREAD
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_DUMMY);
|
2018-08-28 14:31:28 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then read all of the requested bytes */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->spi, buffer, nbytes);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_pagewrite
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_GD25_READONLY
|
2019-10-25 15:31:00 +02:00
|
|
|
static void gd25_pagewrite(FAR struct gd25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, off_t address,
|
|
|
|
size_t nbytes)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
finfo("address: %08lx nwords: %d\n", (long)address, (int)nbytes);
|
2019-10-25 15:31:00 +02:00
|
|
|
DEBUGASSERT(priv && buffer && (address & 0xff) == 0 &&
|
|
|
|
(nbytes & 0xff) == 0);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
for (; nbytes > 0; nbytes -= GD25_PAGE_SIZE)
|
|
|
|
{
|
|
|
|
/* Wait for any preceding write or erase operation to complete. */
|
|
|
|
|
|
|
|
gd25_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Enable write access to the FLASH */
|
|
|
|
|
|
|
|
gd25_wren(priv);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send the "Page Program (GD25_PP)" Command */
|
|
|
|
|
|
|
|
SPI_SEND(priv->spi, GD25_PP);
|
|
|
|
priv->prev_instr = GD25_PP;
|
|
|
|
|
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
if (priv->addr_4byte)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, (address >> 24) & 0xff);
|
2019-10-25 15:31:00 +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);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Then send the page of data */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->spi, buffer, GD25_PAGE_SIZE);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Update addresses */
|
|
|
|
|
|
|
|
address += GD25_PAGE_SIZE;
|
|
|
|
buffer += GD25_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_bytewrite
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_GD25_READONLY)
|
2019-10-25 15:31:00 +02:00
|
|
|
static inline void gd25_bytewrite(FAR struct gd25_dev_s *priv,
|
|
|
|
FAR const uint8_t *buffer, off_t offset,
|
|
|
|
uint16_t count)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
finfo("offset: %08lx count:%d\n", (long)offset, count);
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
gd25_waitwritecomplete(priv);
|
|
|
|
|
|
|
|
/* Enable the write access to the FLASH */
|
|
|
|
|
|
|
|
gd25_wren(priv);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), true);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Send "Page Program (PP)" command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, GD25_PP);
|
2018-08-28 14:31:28 +02:00
|
|
|
priv->prev_instr = GD25_PP;
|
|
|
|
|
|
|
|
/* Send the page offset high byte first. */
|
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
if (priv->addr_4byte)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, (offset >> 24) & 0xff);
|
2019-10-25 15:31:00 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Then write the specified number of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->spi, buffer, count);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
finfo("Written\n");
|
|
|
|
}
|
|
|
|
#endif /* defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_GD25_READONLY) */
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_erase
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
static int gd25_erase(FAR struct mtd_dev_s *dev, off_t startblock,
|
2019-10-25 15:31:00 +02:00
|
|
|
size_t nblocks)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_GD25_READONLY
|
|
|
|
return -EACESS
|
|
|
|
#else
|
|
|
|
FAR struct gd25_dev_s *priv = (FAR struct gd25_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
|
|
|
|
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
|
|
|
|
|
|
|
/* Lock access to the SPI bus until we complete the erase */
|
|
|
|
|
|
|
|
gd25_lock(priv->spi);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_purdid(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
while (blocksleft-- > 0)
|
|
|
|
{
|
|
|
|
/* Erase each sector */
|
|
|
|
|
|
|
|
gd25_sectorerase(priv, startblock);
|
|
|
|
startblock++;
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_pd(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_unlock(priv->spi);
|
|
|
|
return (int)nblocks;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_bread
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
static ssize_t gd25_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
2019-10-25 15:31:00 +02:00
|
|
|
size_t nblocks, FAR uint8_t *buffer)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
nbytes = gd25_read(dev, startblock << GD25_PAGE_SHIFT,
|
|
|
|
nblocks << GD25_PAGE_SHIFT, buffer);
|
2018-08-28 14:31:28 +02:00
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
nbytes >>= GD25_PAGE_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_bwrite
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
static ssize_t gd25_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
2019-10-25 15:31:00 +02:00
|
|
|
size_t nblocks, FAR const uint8_t *buffer)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_GD25_READONLY
|
|
|
|
return -EACCESS;
|
|
|
|
#else
|
|
|
|
FAR struct gd25_dev_s *priv = (FAR struct gd25_dev_s *)dev;
|
|
|
|
|
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
|
|
|
|
|
|
|
/* Lock the SPI bus and write all of the pages to FLASH */
|
|
|
|
|
|
|
|
gd25_lock(priv->spi);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_purdid(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_pagewrite(priv, buffer, startblock << GD25_PAGE_SHIFT,
|
|
|
|
nblocks << GD25_PAGE_SHIFT);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_pd(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_unlock(priv->spi);
|
|
|
|
|
|
|
|
return nblocks;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_read
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-25 15:31:00 +02:00
|
|
|
static ssize_t gd25_read(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR uint8_t *buffer)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
FAR struct gd25_dev_s *priv = (FAR struct gd25_dev_s *)dev;
|
|
|
|
|
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
|
|
|
|
|
|
|
/* Lock the SPI bus and select this FLASH part */
|
|
|
|
|
|
|
|
gd25_lock(priv->spi);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_purdid(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_byteread(priv, buffer, offset, nbytes);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_pd(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_unlock(priv->spi);
|
|
|
|
|
|
|
|
finfo("return nbytes: %d,%x,%x\n", (int)nbytes, buffer[0], buffer[1]);
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_write
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
static ssize_t gd25_write(FAR struct mtd_dev_s *dev, off_t offset,
|
2019-10-25 15:31:00 +02:00
|
|
|
size_t nbytes, FAR const uint8_t *buffer)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_GD25_READONLY
|
|
|
|
return -EACCESS;
|
|
|
|
#else
|
|
|
|
FAR struct gd25_dev_s *priv = (FAR struct gd25_dev_s *)dev;
|
|
|
|
int startpage;
|
|
|
|
int endpage;
|
|
|
|
int count;
|
|
|
|
int index;
|
|
|
|
int bytestowrite;
|
|
|
|
|
|
|
|
finfo("offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
|
|
|
|
|
|
|
/* 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 / GD25_PAGE_SIZE;
|
|
|
|
endpage = (offset + nbytes) / GD25_PAGE_SIZE;
|
|
|
|
|
|
|
|
gd25_lock(priv->spi);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_purdid(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
if (startpage == endpage)
|
|
|
|
{
|
|
|
|
/* All bytes within one programmable page. Just do the write. */
|
|
|
|
|
|
|
|
gd25_bytewrite(priv, buffer, offset, nbytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Write the 1st partial-page */
|
|
|
|
|
|
|
|
count = nbytes;
|
2019-10-25 15:31:00 +02:00
|
|
|
bytestowrite = GD25_PAGE_SIZE - (offset & (GD25_PAGE_SIZE - 1));
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_bytewrite(priv, buffer, offset, bytestowrite);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += bytestowrite;
|
|
|
|
count -= bytestowrite;
|
|
|
|
index = bytestowrite;
|
|
|
|
|
|
|
|
/* Write full pages */
|
|
|
|
|
|
|
|
while (count >= GD25_PAGE_SIZE)
|
|
|
|
{
|
|
|
|
gd25_bytewrite(priv, &buffer[index], offset, GD25_PAGE_SIZE);
|
|
|
|
|
|
|
|
/* Update offset and count */
|
|
|
|
|
|
|
|
offset += GD25_PAGE_SIZE;
|
|
|
|
count -= GD25_PAGE_SIZE;
|
|
|
|
index += GD25_PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now write any partial page at the end */
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
gd25_bytewrite(priv, &buffer[index], offset, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_pd(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_unlock(priv->spi);
|
|
|
|
return nbytes;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_ioctl
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
static int gd25_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct gd25_dev_s *priv = (FAR struct gd25_dev_s *)dev;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
finfo("cmd: %d\n", cmd);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
|
|
|
FAR struct mtd_geometry_s *geo =
|
|
|
|
(FAR struct mtd_geometry_s *)((uintptr_t)arg);
|
|
|
|
if (geo)
|
|
|
|
{
|
|
|
|
geo->blocksize = GD25_PAGE_SIZE;
|
|
|
|
geo->erasesize = GD25_SECTOR_SIZE;
|
|
|
|
geo->neraseblocks = priv->nsectors;
|
|
|
|
ret = OK;
|
|
|
|
|
2020-12-13 14:44:21 +01:00
|
|
|
finfo("blocksize: %" PRIu32 " erasesize: %" PRIu32
|
|
|
|
" neraseblocks: %" PRIu32 "\n",
|
2018-08-28 14:31:28 +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)
|
|
|
|
{
|
|
|
|
info->numsectors = priv->nsectors *
|
|
|
|
GD25_SECTOR_SIZE / GD25_PAGE_SIZE;
|
|
|
|
info->sectorsize = GD25_PAGE_SIZE;
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-08-28 14:31:28 +02:00
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
gd25_lock(priv->spi);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_purdid(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
ret = gd25_chiperase(priv);
|
2019-11-12 16:02:19 +01:00
|
|
|
gd25_pd(priv);
|
2018-08-28 14:31:28 +02:00
|
|
|
gd25_unlock(priv->spi);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-07-16 19:16:41 +02:00
|
|
|
case MTDIOC_ERASESTATE:
|
|
|
|
{
|
|
|
|
FAR uint8_t *result = (FAR uint8_t *)arg;
|
|
|
|
*result = GD25_ERASED_STATE;
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-08-28 14:31:28 +02:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
finfo("return %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Public Functions
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2020-08-22 19:15:21 +02:00
|
|
|
/***************************************************************************
|
2018-08-28 14:31:28 +02:00
|
|
|
* Name: gd25_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2020-08-22 19:15:21 +02:00
|
|
|
* Create an initialize MTD device instance. MTD devices aren't registered
|
2018-08-28 14:31:28 +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).
|
|
|
|
*
|
2020-08-22 19:15:21 +02:00
|
|
|
***************************************************************************/
|
2018-08-28 14:31:28 +02:00
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
FAR struct mtd_dev_s *gd25_initialize(FAR struct spi_dev_s *spi,
|
|
|
|
uint32_t spi_devid)
|
2018-08-28 14:31:28 +02:00
|
|
|
{
|
|
|
|
FAR struct gd25_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv = (FAR struct gd25_dev_s *)kmm_zalloc(sizeof(struct gd25_dev_s));
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated structure (unsupported methods were
|
|
|
|
* nullified by kmm_zalloc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->mtd.erase = gd25_erase;
|
|
|
|
priv->mtd.bread = gd25_bread;
|
|
|
|
priv->mtd.bwrite = gd25_bwrite;
|
|
|
|
priv->mtd.read = gd25_read;
|
|
|
|
priv->mtd.ioctl = gd25_ioctl;
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
priv->mtd.write = gd25_write;
|
|
|
|
#endif
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "gd25";
|
2018-08-28 14:31:28 +02:00
|
|
|
priv->spi = spi;
|
2019-10-31 14:43:21 +01:00
|
|
|
priv->spi_devid = spi_devid;
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_FLASH(priv->spi_devid), false);
|
2018-08-28 14:31:28 +02:00
|
|
|
|
|
|
|
/* Identify the FLASH chip and get its capacity */
|
|
|
|
|
|
|
|
ret = gd25_readid(priv);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Unrecognized\n");
|
|
|
|
kmm_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Make sure that the FLASH is unprotected
|
|
|
|
* so that we can write into it
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_GD25_READONLY
|
|
|
|
gd25_unprotect(priv);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|