2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* drivers/mtd/gd5f.c
|
|
|
|
*
|
2022-04-19 14:43:15 +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
|
2019-09-15 16:33:22 +02:00
|
|
|
*
|
2022-04-19 14:43:15 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-09-15 16:33:22 +02:00
|
|
|
*
|
2022-04-19 14:43:15 +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.
|
2019-09-15 16:33:22 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2019-11-30 00:37:39 +01:00
|
|
|
#include <nuttx/signal.h>
|
2019-09-15 16:33:22 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
#include <nuttx/mtd/mtd.h>
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Configuration ************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_GD5F_SPIMODE
|
|
|
|
# define CONFIG_GD5F_SPIMODE SPIDEV_MODE0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_GD5F_SPIFREQUENCY
|
|
|
|
# define CONFIG_GD5F_SPIFREQUENCY 20000000
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* GD5F Instructions ********************************************************/
|
|
|
|
|
|
|
|
/* Command Value Description Addr Data */
|
|
|
|
|
|
|
|
/* Dummy */
|
|
|
|
|
|
|
|
#define GD5F_GET_FEATURE 0x0f /* Get features 1 0 1 */
|
|
|
|
#define GD5F_SET_FEATURE 0x1f /* Set features 1 0 1 */
|
|
|
|
#define GD5F_PAGE_READ 0x13 /* Array read 3 0 0 */
|
|
|
|
#define GD5F_READ_FROM_CACHE 0x03 /* Output cache data
|
|
|
|
* on SO 2 1 1-2112 */
|
|
|
|
#define GD5F_READ_ID 0x9f /* Read device ID 0 1 2 */
|
|
|
|
#define GD5F_ECC_STATUS_READ 0x7c /* Internal ECC status
|
|
|
|
* output 0 1 1 */
|
|
|
|
#define GD5F_BLOCK_ERASE 0xd8 /* Block erase 3 0 0 */
|
|
|
|
#define GD5F_PROGRAM_EXECUTE 0x10 /* Enter block/page
|
|
|
|
* address, execute 3 0 0 */
|
|
|
|
#define GD5F_PROGRAM_LOAD 0x02 /* Load program data with
|
|
|
|
* cache reset first 2 0 1-2112 */
|
|
|
|
#define GD5F_PROGRAM_LOAD_RANDOM 0x84 /* Load program data
|
|
|
|
* without cache reset 2 0 1-2112 */
|
|
|
|
#define GD5F_WRITE_ENABLE 0x06 /* 0 0 0 */
|
|
|
|
#define GD5F_WRITE_DISABLE 0x04 /* 0 0 0 */
|
|
|
|
#define GD5F_RESET 0xff /* Reset the device 0 0 0 */
|
|
|
|
#define GD5F_DUMMY 0x00 /* No Operation 0 0 0 */
|
|
|
|
|
|
|
|
/* Feature register *********************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* JEDEC Read ID register values */
|
|
|
|
|
|
|
|
#define GD5F_MANUFACTURER 0xc8
|
|
|
|
#define GD5F_GD5F_CAPACITY_MASK 0x0f
|
|
|
|
#define GD5F_CAPACITY_1GBIT 0x01 /* 1 Gb */
|
|
|
|
#define GD5F_CAPACITY_2GBIT 0x02 /* 2 Gb */
|
|
|
|
#define GD5F_CAPACITY_4GBIT 0x04 /* 4 Gb */
|
|
|
|
|
|
|
|
#define GD5F_NSECTORS_1GBIT 1024 /* 1024x131072 = 1Gbit memory capacity */
|
|
|
|
#define GD5F_NSECTORS_2GBIT 2048 /* 2048x131072 = 2Gbit memory capacity */
|
|
|
|
#define GD5F_NSECTORS_4GBIT 4096 /* 4096x131072 = 4Gbit memory capacity */
|
|
|
|
|
|
|
|
#define GD5F_SECTOR_SHIFT 17 /* 131072 byte */
|
|
|
|
#define GD5F_PAGE_SHIFT 11 /* 2048 */
|
|
|
|
|
|
|
|
/* Register address */
|
|
|
|
|
|
|
|
#define GD5F_SECURE_OTP 0xb0
|
|
|
|
#define GD5F_STATUS 0xc0
|
|
|
|
#define GD5F_BLOCK_PROTECTION 0xa0
|
|
|
|
|
|
|
|
/* Bit definitions */
|
|
|
|
|
|
|
|
/* Secure OTP (On-Time-Programmable) register */
|
|
|
|
|
|
|
|
#define GD5F_SOTP_QE (1 << 0) /* Bit 0: Quad Enable */
|
|
|
|
#define GD5F_SOTP_ECC (1 << 4) /* Bit 4: ECC enabled */
|
|
|
|
#define GD5F_SOTP_SOTP_EN (1 << 6) /* Bit 6: Secure OTP Enable */
|
|
|
|
#define GD5F_SOTP_SOTP_PROT (1 << 7) /* Bit 7: Secure OTP Protect */
|
|
|
|
|
|
|
|
/* Status register */
|
|
|
|
|
|
|
|
#define GD5F_SR_OIP (1 << 0) /* Bit 0: Operation in progress */
|
|
|
|
#define GD5F_SR_WEL (1 << 1) /* Bit 1: Write enable latch */
|
|
|
|
#define GD5F_SR_E_FAIL (1 << 2) /* Bit 2: Erase fail */
|
|
|
|
#define GD5F_SR_P_FAIL (1 << 3) /* Bit 3: Program Fail */
|
|
|
|
#define GD5F_SR_ECC_S0 (1 << 4) /* Bit 4-5: ECC Status */
|
|
|
|
#define GD5F_SR_ECC_S1 (1 << 5)
|
|
|
|
|
|
|
|
/* Block Protection register */
|
|
|
|
|
|
|
|
#define GD5F_BP_SP (1 << 0) /* Bit 0: Solid-protection (1Gb only) */
|
|
|
|
#define GD5F_BP_COMPL (1 << 1) /* Bit 1: Complementary (1Gb only) */
|
|
|
|
#define GD5F_BP_INV (1 << 2) /* Bit 2: Invert (1Gb only) */
|
|
|
|
#define GD5F_BP_BP0 (1 << 3) /* Bit 3: Block Protection 0 */
|
|
|
|
#define GD5F_BP_BP1 (1 << 4) /* Bit 4: Block Protection 1 */
|
|
|
|
#define GD5F_BP_BP2 (1 << 5) /* Bit 5: Block Protection 2 */
|
|
|
|
#define GD5F_BP_BPRWD (1 << 7) /* Bit 7: Block Protection Register
|
|
|
|
* Write Disable */
|
|
|
|
|
|
|
|
/* ECC Status register */
|
|
|
|
|
|
|
|
#define GD5F_FEATURE_ECC_MASK (0x03 << 4)
|
|
|
|
#define GD5F_FEATURE_ECC_ERROR (0x02 << 4)
|
|
|
|
#define GD5F_FEATURE_ECC_OFFSET 4
|
|
|
|
#define GD5F_ECC_STATUS_MASK 0x0f
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +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 gd5f_dev_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct gd5f_dev_s
|
|
|
|
{
|
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct spi_dev_s *dev; /* Saved SPI interface instance */
|
2019-10-31 14:43:21 +01:00
|
|
|
uint32_t spi_devid; /* Chip select inputs */
|
2019-09-15 16:33:22 +02:00
|
|
|
uint16_t nsectors; /* 1024 or 2048 */
|
2019-10-31 14:43:21 +01:00
|
|
|
uint8_t sectorshift; /* 17 */
|
2019-09-15 16:33:22 +02:00
|
|
|
uint8_t pageshift; /* 11 */
|
|
|
|
uint8_t eccstatus; /* Internal ECC status */
|
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static inline void gd5f_lock(FAR struct spi_dev_s *dev);
|
|
|
|
static inline void gd5f_unlock(FAR struct spi_dev_s *dev);
|
|
|
|
|
|
|
|
static int gd5f_readid(FAR struct gd5f_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static bool gd5f_waitstatus(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint8_t mask,
|
2019-09-15 16:33:22 +02:00
|
|
|
bool successif);
|
|
|
|
static inline void gd5f_writeenable(FAR struct gd5f_dev_s *priv);
|
|
|
|
static inline void gd5f_writedisable(FAR struct gd5f_dev_s *priv);
|
2021-01-27 16:48:40 +01:00
|
|
|
static bool gd5f_sectorerase(FAR struct gd5f_dev_s *priv,
|
|
|
|
off_t startsector);
|
|
|
|
static void gd5f_readbuffer(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint32_t address,
|
|
|
|
uint8_t *buffer,
|
|
|
|
size_t length);
|
|
|
|
static bool gd5f_read_page(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint32_t position);
|
|
|
|
|
|
|
|
static void gd5f_write_to_cache(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint32_t address,
|
|
|
|
const uint8_t *buffer,
|
|
|
|
size_t length);
|
|
|
|
static bool gd5f_execute_write(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint32_t position);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static inline void gd5f_eccstatusread(FAR struct gd5f_dev_s *priv);
|
|
|
|
static inline void gd5f_enable_ecc(FAR struct gd5f_dev_s *priv);
|
|
|
|
static inline void gd5f_unlockblocks(FAR struct gd5f_dev_s *priv);
|
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t gd5f_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR uint8_t *buffer);
|
|
|
|
static ssize_t gd5f_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2019-09-15 16:33:22 +02:00
|
|
|
FAR uint8_t *buffer);
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t gd5f_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR const uint8_t *buffer);
|
|
|
|
static ssize_t gd5f_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR const uint8_t *buffer);
|
|
|
|
static int gd5f_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
static int gd5f_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_lock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static inline void gd5f_lock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
SPI_SETMODE(dev, CONFIG_GD5F_SPIMODE);
|
|
|
|
SPI_SETBITS(dev, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(dev, 0);
|
|
|
|
SPI_SETFREQUENCY(dev, CONFIG_GD5F_SPIFREQUENCY);
|
2019-09-15 16:33:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_unlock
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static inline void gd5f_unlock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, false);
|
2019-09-15 16:33:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_readid
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static int gd5f_readid(FAR struct gd5f_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint16_t manufacturer;
|
|
|
|
uint16_t deviceid;
|
|
|
|
uint16_t capacity;
|
|
|
|
|
|
|
|
finfo("priv: %p\n", priv);
|
|
|
|
|
|
|
|
/* Lock the SPI bus, configure the bus, and select this FLASH part. */
|
|
|
|
|
|
|
|
gd5f_lock(priv->dev);
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send the "Read ID" command and read two ID bytes */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_READ_ID);
|
|
|
|
SPI_SEND(priv->dev, GD5F_DUMMY);
|
2019-09-15 16:33:22 +02:00
|
|
|
manufacturer = SPI_SEND(priv->dev, GD5F_DUMMY);
|
|
|
|
deviceid = SPI_SEND(priv->dev, GD5F_DUMMY);
|
|
|
|
|
|
|
|
/* De-select the FLASH and unlock the bus */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
gd5f_unlock(priv->dev);
|
|
|
|
|
|
|
|
finfo("manufacturer: %02x deviceid: %02x\n",
|
|
|
|
manufacturer, deviceid);
|
|
|
|
|
|
|
|
/* Check for a valid manufacturer */
|
|
|
|
|
|
|
|
if (manufacturer == GD5F_MANUFACTURER)
|
|
|
|
{
|
|
|
|
capacity = deviceid & GD5F_GD5F_CAPACITY_MASK;
|
|
|
|
|
|
|
|
if (capacity == GD5F_CAPACITY_1GBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD5F_NSECTORS_1GBIT;
|
|
|
|
}
|
|
|
|
else if (capacity == GD5F_CAPACITY_2GBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD5F_NSECTORS_2GBIT;
|
|
|
|
}
|
|
|
|
else if (capacity == GD5F_CAPACITY_4GBIT)
|
|
|
|
{
|
|
|
|
priv->nsectors = GD5F_NSECTORS_4GBIT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->sectorshift = GD5F_SECTOR_SHIFT;
|
|
|
|
priv->pageshift = GD5F_PAGE_SHIFT;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_waitstatus
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static bool gd5f_waitstatus(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint8_t mask,
|
|
|
|
bool successif)
|
2019-09-15 16:33:22 +02:00
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
/* Loop as long as the memory is busy with a write cycle */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Get feature command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_GET_FEATURE);
|
|
|
|
SPI_SEND(priv->dev, GD5F_STATUS);
|
2019-09-15 16:33:22 +02:00
|
|
|
status = SPI_SEND(priv->dev, GD5F_DUMMY);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-11-30 00:37:39 +01:00
|
|
|
nxsig_usleep(1000);
|
2019-09-15 16:33:22 +02:00
|
|
|
}
|
|
|
|
while ((status & GD5F_SR_OIP) != 0);
|
|
|
|
|
|
|
|
finfo("Complete %02x\n", status);
|
|
|
|
|
|
|
|
return successif ? ((status & mask) != 0) : ((status & mask) == 0);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_writeenable
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static inline void gd5f_writeenable(FAR struct gd5f_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send Write Enable command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_WRITE_ENABLE);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_writedisable
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static inline void gd5f_writedisable(FAR struct gd5f_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send Write Enable command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_WRITE_DISABLE);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_sectorerase (128K)
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static bool gd5f_sectorerase(FAR struct gd5f_dev_s *priv,
|
|
|
|
off_t startsector)
|
2019-09-15 16:33:22 +02:00
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
const uint32_t block = startsector << (priv->sectorshift -
|
|
|
|
priv->pageshift);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
finfo("block sector: %08lx\n", (long)block);
|
|
|
|
|
|
|
|
/* Send write enable instruction */
|
|
|
|
|
|
|
|
gd5f_writeenable(priv);
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send the Block Erase instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_BLOCK_ERASE);
|
|
|
|
SPI_SEND(priv->dev, (block >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (block >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, block & 0xff);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* De-select the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
finfo("Erased\n");
|
|
|
|
return gd5f_waitstatus(priv, GD5F_SR_E_FAIL, false);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int gd5f_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks)
|
2019-09-15 16:33:22 +02:00
|
|
|
{
|
|
|
|
FAR struct gd5f_dev_s *priv = (FAR struct gd5f_dev_s *)dev;
|
|
|
|
size_t blocksleft = nblocks;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
finfo("Erase: startblock: %08lx nblocks: %d\n",
|
|
|
|
(long)startblock,
|
|
|
|
(int)nblocks);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Lock access to the SPI bus until we complete the erase */
|
|
|
|
|
|
|
|
gd5f_lock(priv->dev);
|
|
|
|
|
|
|
|
/* Wait all operations complete */
|
|
|
|
|
|
|
|
gd5f_waitstatus(priv, GD5F_SR_OIP, false);
|
|
|
|
|
|
|
|
while (blocksleft > 0)
|
|
|
|
{
|
|
|
|
if (!gd5f_sectorerase(priv, startblock))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
startblock++;
|
|
|
|
blocksleft--;
|
|
|
|
}
|
|
|
|
|
|
|
|
gd5f_unlock(priv->dev);
|
|
|
|
return nblocks - blocksleft;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_readbuffer
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static void gd5f_readbuffer(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint32_t address,
|
|
|
|
uint8_t *buffer,
|
|
|
|
size_t length)
|
2019-09-15 16:33:22 +02:00
|
|
|
{
|
|
|
|
const uint16_t offset = address & ((1 << priv->pageshift) - 1);
|
|
|
|
|
|
|
|
/* Select the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_READ_FROM_CACHE);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset) & 0xff);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send a dummy byte */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_DUMMY);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Then read all of the requested bytes */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->dev, buffer, length);
|
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_read_page
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static bool gd5f_read_page(FAR struct gd5f_dev_s *priv, uint32_t pageaddress)
|
|
|
|
{
|
|
|
|
const uint32_t row = pageaddress >> priv->pageshift;
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send the Read Page instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_PAGE_READ);
|
|
|
|
SPI_SEND(priv->dev, (row >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (row >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, row & 0xff);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Deselect the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Wait Page Read Complete */
|
|
|
|
|
|
|
|
gd5f_waitstatus(priv, GD5F_SR_OIP, false);
|
|
|
|
|
|
|
|
/* Check HardWare ECC result */
|
|
|
|
|
|
|
|
gd5f_eccstatusread(priv);
|
|
|
|
if ((priv->eccstatus & GD5F_FEATURE_ECC_MASK) == GD5F_FEATURE_ECC_ERROR)
|
|
|
|
{
|
|
|
|
/* ECC report uncorrectable, discard data */
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t gd5f_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2019-09-15 16:33:22 +02:00
|
|
|
FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct gd5f_dev_s *priv = (FAR struct gd5f_dev_s *)dev;
|
|
|
|
size_t bytesleft = nbytes;
|
|
|
|
uint32_t position = offset;
|
|
|
|
|
|
|
|
finfo("Read: offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
|
|
|
|
|
|
|
/* Lock the SPI bus and select this FLASH part */
|
|
|
|
|
|
|
|
gd5f_lock(priv->dev);
|
|
|
|
|
|
|
|
/* Wait all operations complete */
|
|
|
|
|
|
|
|
gd5f_waitstatus(priv, GD5F_SR_OIP, false);
|
|
|
|
|
|
|
|
while (bytesleft)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
const uint32_t pageaddress =
|
|
|
|
(position >> priv->pageshift) << priv->pageshift;
|
|
|
|
const uint32_t spaceleft =
|
|
|
|
pageaddress + (1 << priv->pageshift) - position;
|
|
|
|
const size_t chunklength =
|
|
|
|
bytesleft < spaceleft ? bytesleft : spaceleft;
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
if (!gd5f_read_page(priv, pageaddress))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gd5f_readbuffer(priv, position, buffer, chunklength);
|
|
|
|
|
|
|
|
position += chunklength;
|
|
|
|
buffer += chunklength;
|
|
|
|
bytesleft -= chunklength;
|
|
|
|
}
|
|
|
|
|
|
|
|
gd5f_unlock(priv->dev);
|
|
|
|
|
|
|
|
finfo("return nbytes: %d\n", (int)(nbytes - bytesleft));
|
|
|
|
return nbytes - bytesleft;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static ssize_t gd5f_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
ssize_t nbytes;
|
|
|
|
FAR struct gd5f_dev_s *priv = (FAR struct gd5f_dev_s *)dev;
|
|
|
|
|
|
|
|
finfo("Bread: startblock: %08lx nblocks: %d\n",
|
|
|
|
(long)startblock, (int)nblocks);
|
|
|
|
|
|
|
|
nbytes = gd5f_read(dev, startblock << priv->pageshift,
|
|
|
|
nblocks << priv->pageshift, buffer);
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
nbytes >>= priv->pageshift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_write_to_cache
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static void gd5f_write_to_cache(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint32_t address,
|
|
|
|
const uint8_t *buffer,
|
|
|
|
size_t length)
|
2019-09-15 16:33:22 +02:00
|
|
|
{
|
|
|
|
const uint16_t offset = address & ((1 << priv->pageshift) - 1);
|
|
|
|
|
|
|
|
/* Select the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send the Program Load command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_PROGRAM_LOAD);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send the address high byte first. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, (offset >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (offset) & 0xff);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send block of bytes */
|
|
|
|
|
|
|
|
SPI_SNDBLOCK(priv->dev, buffer, length);
|
|
|
|
|
|
|
|
/* De-select the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_execute_write
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static bool gd5f_execute_write(FAR struct gd5f_dev_s *priv,
|
|
|
|
uint32_t pageaddress)
|
2019-09-15 16:33:22 +02:00
|
|
|
{
|
|
|
|
const uint32_t row = pageaddress >> priv->pageshift;
|
|
|
|
|
|
|
|
/* Select this FLASH part */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Send the Program Execute instruction */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_PROGRAM_EXECUTE);
|
|
|
|
SPI_SEND(priv->dev, (row >> 16) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, (row >> 8) & 0xff);
|
|
|
|
SPI_SEND(priv->dev, row & 0xff);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* De-select the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
return gd5f_waitstatus(priv, GD5F_SR_P_FAIL, false);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_write
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t gd5f_write(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2019-09-15 16:33:22 +02:00
|
|
|
FAR const uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct gd5f_dev_s *priv = (FAR struct gd5f_dev_s *)dev;
|
|
|
|
size_t bytesleft = nbytes;
|
|
|
|
uint32_t position = offset;
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
finfo("Write: offset: %08lx nbytes: %d\n", (long)offset, (int)nbytes);
|
2019-09-15 16:33:22 +02:00
|
|
|
gd5f_lock(priv->dev);
|
|
|
|
|
|
|
|
/* Wait all operations complete */
|
|
|
|
|
|
|
|
gd5f_waitstatus(priv, GD5F_SR_OIP, false);
|
|
|
|
|
|
|
|
while (bytesleft)
|
|
|
|
{
|
2021-01-27 16:48:40 +01:00
|
|
|
const uint32_t pageaddress =
|
|
|
|
(position >> priv->pageshift) << priv->pageshift;
|
|
|
|
const uint32_t spaceleft =
|
|
|
|
pageaddress + (1 << priv->pageshift) - position;
|
|
|
|
const size_t chunklength =
|
|
|
|
bytesleft < spaceleft ? bytesleft : spaceleft;
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
gd5f_write_to_cache(priv, position, buffer, chunklength);
|
|
|
|
gd5f_writeenable(priv);
|
|
|
|
if (!gd5f_execute_write(priv, pageaddress))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
position += chunklength;
|
|
|
|
buffer += chunklength;
|
|
|
|
bytesleft -= chunklength;
|
|
|
|
}
|
|
|
|
|
|
|
|
gd5f_unlock(priv->dev);
|
|
|
|
|
|
|
|
return nbytes - bytesleft;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_bwrite
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static ssize_t gd5f_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buffer)
|
|
|
|
{
|
|
|
|
ssize_t nbytes;
|
|
|
|
|
|
|
|
FAR struct gd5f_dev_s *priv = (FAR struct gd5f_dev_s *)dev;
|
|
|
|
|
|
|
|
finfo("Bwrite: startblock: %08lx nblocks: %d\n",
|
|
|
|
(long)startblock, (int)nblocks);
|
|
|
|
|
|
|
|
/* Lock the SPI bus and write all of the pages to FLASH */
|
|
|
|
|
|
|
|
nbytes = gd5f_write(dev, startblock << priv->pageshift,
|
|
|
|
nblocks << priv->pageshift, buffer);
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
|
|
|
nbytes >>= priv->pageshift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: mx25l_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static int gd5f_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct gd5f_dev_s *priv = (FAR struct gd5f_dev_s *)dev;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
finfo("cmd: %d\n", cmd);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
|
|
|
FAR struct mtd_geometry_s *geo =
|
|
|
|
(FAR struct mtd_geometry_s *)((uintptr_t)arg);
|
|
|
|
if (geo)
|
|
|
|
{
|
2021-08-11 06:21:39 +02:00
|
|
|
memset(geo, 0, sizeof(*geo));
|
|
|
|
|
2019-09-15 16:33:22 +02:00
|
|
|
geo->blocksize = (1 << priv->pageshift);
|
|
|
|
geo->erasesize = (1 << priv->sectorshift);
|
|
|
|
geo->neraseblocks = priv->nsectors;
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
finfo("blocksize: %d erasesize: %d neraseblocks: %d\n",
|
|
|
|
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 <<
|
|
|
|
(priv->sectorshift - priv->pageshift);
|
|
|
|
info->sectorsize = 1 << priv->pageshift;
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-09-15 16:33:22 +02:00
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire device */
|
|
|
|
|
|
|
|
ret = gd5f_erase(dev, 0, priv->nsectors);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_ECCSTATUS:
|
|
|
|
{
|
2024-08-25 22:37:15 +02:00
|
|
|
FAR uint8_t *result = (FAR uint8_t *)arg;
|
2019-09-15 16:33:22 +02:00
|
|
|
*result =
|
2021-01-27 16:48:40 +01:00
|
|
|
(priv->eccstatus & GD5F_FEATURE_ECC_MASK)
|
|
|
|
>> GD5F_FEATURE_ECC_OFFSET;
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
finfo("return %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_eccstatusread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static inline void gd5f_eccstatusread(FAR struct gd5f_dev_s *priv)
|
|
|
|
{
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_GET_FEATURE);
|
|
|
|
SPI_SEND(priv->dev, GD5F_STATUS);
|
2019-09-15 16:33:22 +02:00
|
|
|
priv->eccstatus = SPI_SEND(priv->dev, GD5F_DUMMY);
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_enable_ecc
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static inline void gd5f_enable_ecc(FAR struct gd5f_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t secure_otp = GD5F_SOTP_ECC;
|
|
|
|
|
|
|
|
gd5f_lock(priv->dev);
|
|
|
|
gd5f_writeenable(priv);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_SET_FEATURE);
|
|
|
|
SPI_SEND(priv->dev, GD5F_SECURE_OTP);
|
|
|
|
SPI_SEND(priv->dev, secure_otp);
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
gd5f_writedisable(priv);
|
|
|
|
gd5f_unlock(priv->dev);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_unlockblocks
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
static inline void gd5f_unlockblocks(FAR struct gd5f_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t blockprotection = 0x00;
|
|
|
|
|
|
|
|
gd5f_lock(priv->dev);
|
|
|
|
gd5f_writeenable(priv);
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_SET_FEATURE);
|
|
|
|
SPI_SEND(priv->dev, GD5F_BLOCK_PROTECTION);
|
|
|
|
SPI_SEND(priv->dev, blockprotection);
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
gd5f_writedisable(priv);
|
|
|
|
gd5f_unlock(priv->dev);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2019-09-15 16:33:22 +02:00
|
|
|
* Name: gd5f_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Create an initialize MTD device instance.
|
|
|
|
* MTD devices are not registered in the file system, but are created
|
|
|
|
* as instances that can be bound to other functions(such as a block
|
|
|
|
* or character driver front end).
|
2019-09-15 16:33:22 +02:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
FAR struct mtd_dev_s *gd5f_initialize(FAR struct spi_dev_s *dev,
|
|
|
|
uint32_t spi_devid)
|
2019-09-15 16:33:22 +02:00
|
|
|
{
|
|
|
|
FAR struct gd5f_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
finfo("dev: %p\n", dev);
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct gd5f_dev_s));
|
2019-09-15 16:33:22 +02:00
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated structure. (unsupported methods were
|
|
|
|
* nullified by kmm_zalloc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->mtd.erase = gd5f_erase;
|
|
|
|
priv->mtd.bread = gd5f_bread;
|
|
|
|
priv->mtd.bwrite = gd5f_bwrite;
|
|
|
|
priv->mtd.ioctl = gd5f_ioctl;
|
|
|
|
priv->mtd.name = "gd5f";
|
|
|
|
priv->dev = dev;
|
2019-10-29 14:47:03 +01:00
|
|
|
priv->spi_devid = spi_devid;
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* De-select the FLASH */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Reset the flash */
|
|
|
|
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), true);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->dev, GD5F_RESET);
|
2019-10-29 14:47:03 +01:00
|
|
|
SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->spi_devid), false);
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
/* Wait reset complete */
|
|
|
|
|
|
|
|
gd5f_waitstatus(priv, GD5F_SR_OIP, false);
|
|
|
|
|
|
|
|
/* Identify the FLASH chip and get its capacity */
|
|
|
|
|
|
|
|
ret = gd5f_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
|
|
|
|
*/
|
2019-09-15 16:33:22 +02:00
|
|
|
|
|
|
|
ferr("ERROR: Unrecognized\n");
|
|
|
|
kmm_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gd5f_enable_ecc(priv);
|
|
|
|
gd5f_waitstatus(priv, GD5F_SR_OIP, false);
|
|
|
|
gd5f_unlockblocks(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
|
|
|
finfo("Return %p\n", priv);
|
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|