2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* drivers/spi/qspi_flash.c
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2016-07-19 18:39:43 +02:00
|
|
|
* Included Files
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-06-08 20:00:55 +02:00
|
|
|
#include <assert.h>
|
2016-07-19 18:39:43 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/spi/qspi_flash.h>
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2016-07-19 18:39:43 +02:00
|
|
|
* Pre-processor Definitions
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2020-02-10 15:49:00 +01:00
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/* Configuration ************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
/* Define the FLASH SIZE in bytes */
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifdef CONFIG_QSPI_FLASH_1M
|
|
|
|
# define CONFIG_QSPI_FLASH_SIZE (128 * 1024)
|
|
|
|
# define CONFIG_QSPI_FLASH_CAPACITY 0x11
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_SECTORSIZE
|
|
|
|
# define CONFIG_QSPI_FLASH_SECTORSIZE 2048
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifdef CONFIG_QSPI_FLASH_8M
|
|
|
|
# define CONFIG_QSPI_FLASH_SIZE (1024 * 1024)
|
|
|
|
# define CONFIG_QSPI_FLASH_CAPACITY 0x14
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifdef CONFIG_QSPI_FLASH_32M
|
|
|
|
# define CONFIG_QSPI_FLASH_SIZE (4 * 1024 * 1024)
|
|
|
|
# define CONFIG_QSPI_FLASH_CAPACITY 0x16
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifdef CONFIG_QSPI_FLASH_64M
|
|
|
|
# define CONFIG_QSPI_FLASH_SIZE (8 * 1024 * 1024)
|
|
|
|
# define CONFIG_QSPI_FLASH_CAPACITY 0x17
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifdef CONFIG_QSPI_FLASH_128M
|
|
|
|
# define CONFIG_QSPI_FLASH_SIZE (16 * 1024 * 1024)
|
|
|
|
# define CONFIG_QSPI_FLASH_CAPACITY 0x18
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_MANUFACTURER
|
|
|
|
# define CONFIG_QSPI_FLASH_MANUFACTURER 0x20
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_MEMORY_TYPE
|
|
|
|
# define CONFIG_QSPI_FLASH_MEMORY_TYPE 0xba
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_SECTORSIZE
|
|
|
|
# define CONFIG_QSPI_FLASH_SECTORSIZE 65536
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_SUBSECTORSIZE
|
|
|
|
# define CONFIG_QSPI_FLASH_SUBSECTORSIZE 4096
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_SECTORSIZE_MASK
|
|
|
|
# define CONFIG_QSPI_FLASH_SECTORSIZE_MASK (~(CONFIG_QSPI_FLASH_SECTORSIZE-1))
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_SUBSECTORSIZE_MASK
|
|
|
|
# define CONFIG_QSPI_FLASH_SUBSECTORSIZE_MASK (~(CONFIG_QSPI_FLASH_SUBSECTORSIZE-1))
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_PAGESIZE
|
|
|
|
# define CONFIG_QSPI_FLASH_PAGESIZE 256
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#ifndef CONFIG_QSPI_FLASH_PAGESIZE_MASK
|
|
|
|
# define CONFIG_QSPI_FLASH_PAGESIZE_MASK (CONFIG_QSPI_FLASH_PAGESIZE-1)
|
2016-07-19 18:39:43 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Define FLASH States */
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#define QSPI_FLASH_STATE_IDLE 0
|
|
|
|
#define QSPI_FLASH_STATE_RDID1 1
|
|
|
|
#define QSPI_FLASH_STATE_RDID2 2
|
|
|
|
#define QSPI_FLASH_STATE_RDID3 3
|
|
|
|
#define QSPI_FLASH_STATE_WREN 4
|
|
|
|
#define QSPI_FLASH_STATE_RDSR 5
|
|
|
|
#define QSPI_FLASH_STATE_SE1 6
|
|
|
|
#define QSPI_FLASH_STATE_SE2 7
|
|
|
|
#define QSPI_FLASH_STATE_SE3 8
|
|
|
|
#define QSPI_FLASH_STATE_PP1 9
|
|
|
|
#define QSPI_FLASH_STATE_PP2 10
|
|
|
|
#define QSPI_FLASH_STATE_PP3 11
|
|
|
|
#define QSPI_FLASH_STATE_PP4 12
|
|
|
|
#define QSPI_FLASH_STATE_READ1 13
|
|
|
|
#define QSPI_FLASH_STATE_READ2 14
|
|
|
|
#define QSPI_FLASH_STATE_READ3 15
|
|
|
|
#define QSPI_FLASH_STATE_READ4 16
|
|
|
|
#define QSPI_FLASH_STATE_FREAD_WAIT 17
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
/* Instructions */
|
2020-02-10 15:49:00 +01:00
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/* Command Value N Description Addr Dummy Data */
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#define QSPI_FLASH_WREN 0x06 /* 1 Write Enable 0 0 0 */
|
|
|
|
#define QSPI_FLASH_WRDI 0x04 /* 1 Write Disable 0 0 0 */
|
|
|
|
#define QSPI_FLASH_RDID 0x9f /* 1 Read Identification 0 0 1-3 */
|
|
|
|
#define QSPI_FLASH_RDSR 0x05 /* 1 Read Status Register 0 0 >=1 */
|
|
|
|
#define QSPI_FLASH_WRSR 0x01 /* 1 Write Status Register 0 0 1 */
|
|
|
|
#define QSPI_FLASH_READ 0x03 /* 1 Read Data Bytes 3 0 >=1 */
|
|
|
|
#define QSPI_FLASH_FAST_READ 0x0b /* 1 Higher speed read 3 1 >=1 */
|
|
|
|
#define QSPI_FLASH_PP 0x02 /* 1 Page Program 3 0 1-256 */
|
|
|
|
#define QSPI_FLASH_SE 0xd8 /* 1 Sector Erase 3 0 0 */
|
|
|
|
#define QSPI_FLASH_BE 0xc7 /* 1 Bulk Erase 0 0 0 */
|
|
|
|
#define QSPI_FLASH_DP 0xb9 /* 2 Deep power down 0 0 0 */
|
|
|
|
#define QSPI_FLASH_RES 0xab /* 2 Read Electronic
|
2021-04-10 09:23:45 +02:00
|
|
|
* Signature 0 3 >=1 */
|
2022-01-06 08:10:25 +01:00
|
|
|
#define QSPI_FLASH_SSE 0x20 /* 3 Sub-Sector Erase 0 0 0 */
|
2021-04-10 09:23:45 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#define QSPI_FLASH_ID 0x9f /* JEDEC ID */
|
|
|
|
#define QSPI_FLASH_READ_QUAD 0xeb
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#define QSPI_FLASH_DUMMY 0xa5
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
#define QSPI_FLASH_WREN_SET 0x02
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2016-07-19 18:39:43 +02:00
|
|
|
* Private Types
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
struct qspi_flashdev_s
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
|
|
|
struct qspi_dev_s spidev; /* Externally visible part of the SPI interface */
|
2022-01-06 08:10:25 +01:00
|
|
|
uint32_t selected; /* SPIn base address */
|
|
|
|
int wren;
|
|
|
|
int state;
|
|
|
|
uint16_t read_data;
|
|
|
|
uint8_t last_cmd;
|
|
|
|
unsigned long address;
|
|
|
|
unsigned char data[CONFIG_QSPI_FLASH_SIZE];
|
2016-07-19 18:39:43 +02:00
|
|
|
};
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2016-07-19 18:39:43 +02:00
|
|
|
* Private Function Prototypes
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
/* QSPI methods */
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static int qspi_flash_lock(FAR struct qspi_dev_s *dev, bool lock);
|
|
|
|
static uint32_t qspi_flash_setfrequency(FAR struct qspi_dev_s *dev,
|
|
|
|
uint32_t frequency);
|
|
|
|
static void qspi_flash_setmode(FAR struct qspi_dev_s *dev,
|
|
|
|
enum qspi_mode_e mode);
|
|
|
|
static void qspi_flash_setbits(FAR struct qspi_dev_s *dev, int nbits);
|
|
|
|
static int qspi_flash_command(FAR struct qspi_dev_s *dev,
|
|
|
|
FAR struct qspi_cmdinfo_s *cmd);
|
|
|
|
static int qspi_flash_memory(FAR struct qspi_dev_s *dev,
|
|
|
|
FAR struct qspi_meminfo_s *mem);
|
|
|
|
static FAR void *qspi_flash_alloc(FAR struct qspi_dev_s *dev,
|
|
|
|
size_t buflen);
|
|
|
|
static void qspi_flash_free(FAR struct qspi_dev_s *dev,
|
|
|
|
FAR void *buffer);
|
|
|
|
|
|
|
|
static void qspi_flash_writeword(FAR struct qspi_flashdev_s *priv,
|
|
|
|
uint16_t data,
|
|
|
|
FAR struct qspi_cmdinfo_s *cmdinfo);
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2016-07-19 18:39:43 +02:00
|
|
|
* Private Data
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
static const struct qspi_ops_s g_qspiops =
|
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
.lock = qspi_flash_lock,
|
|
|
|
.setfrequency = qspi_flash_setfrequency,
|
|
|
|
.setmode = qspi_flash_setmode,
|
|
|
|
.setbits = qspi_flash_setbits,
|
|
|
|
.command = qspi_flash_command,
|
|
|
|
.memory = qspi_flash_memory,
|
|
|
|
.alloc = qspi_flash_alloc,
|
|
|
|
.free = qspi_flash_free
|
2016-07-19 18:39:43 +02:00
|
|
|
};
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
struct qspi_flashdev_s g_qspidev =
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
2020-02-10 15:49:00 +01:00
|
|
|
.spidev =
|
|
|
|
{
|
|
|
|
&g_qspiops
|
|
|
|
}
|
2016-07-19 18:39:43 +02:00
|
|
|
};
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2016-07-19 18:39:43 +02:00
|
|
|
* Private Functions
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_lock
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* 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
|
|
|
|
* transfers. The bus should be locked before the chip is selected. After
|
|
|
|
* locking the SPI bus, the caller should then also call the setfrequency,
|
|
|
|
* setbits, and setmode methods to make sure that the SPI is properly
|
2020-02-23 09:50:23 +01:00
|
|
|
* configured for the device. If the SPI bus is being shared, then it
|
2016-07-19 18:39:43 +02:00
|
|
|
* may have been left in an incompatible state.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* lock - true: Lock spi bus, false: unlock SPI bus
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static int qspi_flash_lock(FAR struct qspi_dev_s *dev, bool lock)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_memory
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform QSPI Memory transaction operations
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Always returns zero
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
int qspi_flash_memory(FAR struct qspi_dev_s *dev,
|
|
|
|
FAR struct qspi_meminfo_s *mem)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
FAR struct qspi_flashdev_s *priv = (FAR struct qspi_flashdev_s *)dev;
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
switch (mem->cmd)
|
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_READ_QUAD:
|
2016-07-19 18:39:43 +02:00
|
|
|
priv->wren = 0;
|
|
|
|
memcpy(mem->buffer, &priv->data[mem->addr], mem->buflen);
|
|
|
|
priv->address += mem->addr + mem->buflen;
|
2022-01-06 08:10:25 +01:00
|
|
|
priv->state = QSPI_FLASH_STATE_IDLE;
|
2016-07-19 18:39:43 +02:00
|
|
|
break;
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_PP:
|
2016-07-19 18:39:43 +02:00
|
|
|
if (priv->wren)
|
|
|
|
{
|
|
|
|
memcpy(&priv->data[mem->addr], mem->buffer, mem->buflen);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_setfrequency
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the SPI frequency.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* frequency - The SPI frequency requested
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns the actual frequency selected
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static uint32_t qspi_flash_setfrequency(FAR struct qspi_dev_s *dev,
|
|
|
|
uint32_t frequency)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
|
|
|
return frequency;
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_setmode
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the SPI mode. see enum spi_mode_e for mode definitions
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* mode - The SPI mode requested
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns the actual frequency selected
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static void qspi_flash_setmode(FAR struct qspi_dev_s *dev,
|
|
|
|
enum qspi_mode_e mode)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_setbits
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the number of bits per word.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* nbits - The number of bits requested
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static void qspi_flash_setbits(FAR struct qspi_dev_s *dev, int nbits)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_alloc
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a buffer and associate it with the QSPI device
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* buflen - Length of buffer to allocate
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static FAR void *qspi_flash_alloc(FAR struct qspi_dev_s *dev, size_t buflen)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
|
|
|
return kmm_malloc(buflen);
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_free
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a buffer and associate it with the QSPI device
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* buflen - Length of buffer to allocate
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static void qspi_flash_free(FAR struct qspi_dev_s *dev, FAR void *buffer)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
|
|
|
kmm_free(buffer);
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_sectorerase
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Erase one sector
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - Device-specific state data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static void qspi_flash_sectorerase(FAR struct qspi_flashdev_s *priv)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
uint32_t address;
|
|
|
|
uint32_t len = 0;
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
/* Ensure the WREN bit is set before any erase operation */
|
|
|
|
|
|
|
|
if (priv->wren)
|
|
|
|
{
|
|
|
|
address = priv->address;
|
2022-01-06 08:10:25 +01:00
|
|
|
if (priv->last_cmd == QSPI_FLASH_SE)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
address &= CONFIG_QSPI_FLASH_SECTORSIZE_MASK;
|
|
|
|
len = CONFIG_QSPI_FLASH_SECTORSIZE;
|
2016-07-19 18:39:43 +02:00
|
|
|
}
|
2022-01-06 08:10:25 +01:00
|
|
|
else if (priv->last_cmd == QSPI_FLASH_SSE)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
address &= CONFIG_QSPI_FLASH_SUBSECTORSIZE_MASK;
|
|
|
|
len = CONFIG_QSPI_FLASH_SUBSECTORSIZE;
|
2016-07-19 18:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now perform the erase */
|
|
|
|
|
2020-02-10 15:49:00 +01:00
|
|
|
memset(&priv->data[address], 0xff, len);
|
2016-07-19 18:39:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_writeword
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a word (byte in our case) to the FLASH state machine.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* data - the data to send to the simulated FLASH
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static void qspi_flash_writeword(FAR struct qspi_flashdev_s *priv,
|
|
|
|
uint16_t data,
|
|
|
|
FAR struct qspi_cmdinfo_s *cmdinfo)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
|
|
|
switch (priv->state)
|
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_STATE_IDLE:
|
2016-07-19 18:39:43 +02:00
|
|
|
priv->last_cmd = data;
|
|
|
|
priv->read_data = 0xff;
|
|
|
|
switch (data)
|
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_WREN:
|
2016-07-19 18:39:43 +02:00
|
|
|
priv->wren = 1;
|
|
|
|
break;
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_WRDI:
|
2016-07-19 18:39:43 +02:00
|
|
|
priv->wren = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Sector / Subsector erase */
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_SE:
|
|
|
|
case QSPI_FLASH_SSE:
|
2016-07-19 18:39:43 +02:00
|
|
|
priv->address = cmdinfo->addr;
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/* Now perform the sector or sub-sector erase.
|
|
|
|
* Really this should be done during the deselect,
|
|
|
|
* but this is just a simulation .
|
2016-07-19 18:39:43 +02:00
|
|
|
*/
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
qspi_flash_sectorerase(priv);
|
2016-07-19 18:39:43 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Bulk Erase */
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_BE:
|
|
|
|
priv->state = QSPI_FLASH_STATE_IDLE;
|
2016-07-19 18:39:43 +02:00
|
|
|
if (priv->wren)
|
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
memset(priv->data, 0xff, CONFIG_QSPI_FLASH_SIZE);
|
2016-07-19 18:39:43 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-01-06 08:10:25 +01:00
|
|
|
priv->state = QSPI_FLASH_STATE_IDLE;
|
2020-02-10 15:49:00 +01:00
|
|
|
priv->read_data = 0xff;
|
2016-07-19 18:39:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_command
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform QSPI Command operations
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Always returns zero
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
static int qspi_flash_command(FAR struct qspi_dev_s *dev,
|
|
|
|
FAR struct qspi_cmdinfo_s *cmdinfo)
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
FAR uint8_t *p_buf;
|
|
|
|
FAR struct qspi_flashdev_s *priv = (FAR struct qspi_flashdev_s *)dev;
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(cmdinfo->cmd < 256);
|
|
|
|
|
|
|
|
/* Does data accompany the command? */
|
|
|
|
|
|
|
|
if (QSPICMD_ISDATA(cmdinfo->flags))
|
|
|
|
{
|
|
|
|
DEBUGASSERT(cmdinfo->buffer != NULL && cmdinfo->buflen > 0);
|
2022-01-06 08:10:25 +01:00
|
|
|
p_buf = (FAR uint8_t *)cmdinfo->buffer;
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
/* Read or write operation? */
|
|
|
|
|
|
|
|
if (QSPICMD_ISWRITE(cmdinfo->flags))
|
|
|
|
{
|
|
|
|
/* Write data operation */
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
qspi_flash_writeword(priv, cmdinfo->cmd, cmdinfo);
|
2016-07-19 18:39:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Read data operation */
|
|
|
|
|
|
|
|
switch (cmdinfo->cmd)
|
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_ID:
|
|
|
|
p_buf[0] = CONFIG_QSPI_FLASH_MANUFACTURER;
|
|
|
|
p_buf[1] = CONFIG_QSPI_FLASH_MEMORY_TYPE;
|
|
|
|
p_buf[2] = CONFIG_QSPI_FLASH_CAPACITY;
|
2016-07-19 18:39:43 +02:00
|
|
|
break;
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
case QSPI_FLASH_RDSR:
|
|
|
|
p_buf[0] = priv->wren == 1 ? QSPI_FLASH_WREN_SET : 0;
|
2016-07-19 18:39:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Write data operation */
|
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
qspi_flash_writeword(priv, cmdinfo->cmd, cmdinfo);
|
2016-07-19 18:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2016-07-19 18:39:43 +02:00
|
|
|
* Public Functions
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2021-04-10 09:23:45 +02:00
|
|
|
/****************************************************************************
|
2022-01-06 08:10:25 +01:00
|
|
|
* Name: qspi_flash_initialize
|
2016-07-19 18:39:43 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the selected SPI port
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Valid SPI device structure reference on success; a NULL on failure
|
|
|
|
*
|
2021-04-10 09:23:45 +02:00
|
|
|
****************************************************************************/
|
2016-07-19 18:39:43 +02:00
|
|
|
|
2022-01-06 08:10:25 +01:00
|
|
|
FAR struct qspi_dev_s *qspi_flash_initialize()
|
2016-07-19 18:39:43 +02:00
|
|
|
{
|
2022-01-06 08:10:25 +01:00
|
|
|
FAR struct qspi_flashdev_s *priv = NULL;
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
priv = &g_qspidev;
|
|
|
|
priv->selected = 0;
|
|
|
|
priv->wren = 0;
|
|
|
|
priv->address = 0;
|
2022-01-06 08:10:25 +01:00
|
|
|
priv->state = QSPI_FLASH_STATE_IDLE;
|
2020-02-10 15:49:00 +01:00
|
|
|
priv->read_data = 0xff;
|
|
|
|
priv->last_cmd = 0xff;
|
|
|
|
memset(&priv->data[0], 0xff, sizeof(priv->data));
|
2016-07-19 18:39:43 +02:00
|
|
|
|
|
|
|
return (FAR struct qspi_dev_s *)priv;
|
|
|
|
}
|