Add SPI-NOR framework

SPI-NOR framework is based on SPI-MEM framework using
spi_mem_op execution interface.

It implements read functions and allows NOR configuration
up to quad mode.
Default management is 1 data line but it can be overridden
by platform.
It also includes specific quad mode configuration for
Spansion, Micron and Macronix memories.

Change-Id: If49502b899b4a75f6ebc3190f6bde1013651197f
Signed-off-by: Lionel Debieve <lionel.debieve@st.com>
Signed-off-by: Christophe Kerello <christophe.kerello@st.com>
This commit is contained in:
Lionel Debieve 2019-09-24 17:39:14 +02:00
parent c3e5773904
commit a13550d0ec
2 changed files with 445 additions and 0 deletions

387
drivers/mtd/nor/spi_nor.c Normal file
View File

@ -0,0 +1,387 @@
/*
* Copyright (c) 2019, STMicroelectronics - All Rights Reserved
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <drivers/spi_nor.h>
#include <lib/utils.h>
#define SR_WIP BIT(0) /* Write in progress */
#define CR_QUAD_EN_SPAN BIT(1) /* Spansion Quad I/O */
#define SR_QUAD_EN_MX BIT(6) /* Macronix Quad I/O */
#define FSR_READY BIT(7) /* Device status, 0 = Busy, 1 = Ready */
/* Defined IDs for supported memories */
#define SPANSION_ID 0x01U
#define MACRONIX_ID 0xC2U
#define MICRON_ID 0x2CU
#define BANK_SIZE 0x1000000U
#define SPI_READY_TIMEOUT_US 40000U
static struct nor_device nor_dev;
#pragma weak plat_get_nor_data
int plat_get_nor_data(struct nor_device *device)
{
return 0;
}
static int spi_nor_reg(uint8_t reg, uint8_t *buf, size_t len,
enum spi_mem_data_dir dir)
{
struct spi_mem_op op;
zeromem(&op, sizeof(struct spi_mem_op));
op.cmd.opcode = reg;
op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
op.data.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
op.data.dir = dir;
op.data.nbytes = len;
op.data.buf = buf;
return spi_mem_exec_op(&op);
}
static inline int spi_nor_read_id(uint8_t *id)
{
return spi_nor_reg(SPI_NOR_OP_READ_ID, id, 1U, SPI_MEM_DATA_IN);
}
static inline int spi_nor_read_cr(uint8_t *cr)
{
return spi_nor_reg(SPI_NOR_OP_READ_CR, cr, 1U, SPI_MEM_DATA_IN);
}
static inline int spi_nor_read_sr(uint8_t *sr)
{
return spi_nor_reg(SPI_NOR_OP_READ_SR, sr, 1U, SPI_MEM_DATA_IN);
}
static inline int spi_nor_read_fsr(uint8_t *fsr)
{
return spi_nor_reg(SPI_NOR_OP_READ_FSR, fsr, 1U, SPI_MEM_DATA_IN);
}
static inline int spi_nor_write_en(void)
{
return spi_nor_reg(SPI_NOR_OP_WREN, NULL, 0U, SPI_MEM_DATA_OUT);
}
/*
* Check if device is ready.
*
* Return 0 if ready, 1 if busy or a negative error code otherwise
*/
static int spi_nor_ready(void)
{
uint8_t sr;
int ret;
ret = spi_nor_read_sr(&sr);
if (ret != 0) {
return ret;
}
if ((nor_dev.flags & SPI_NOR_USE_FSR) != 0U) {
uint8_t fsr;
ret = spi_nor_read_fsr(&fsr);
if (ret != 0) {
return ret;
}
return (((fsr & FSR_READY) != 0U) && ((sr & SR_WIP) == 0U)) ?
0 : 1;
}
return (((sr & SR_WIP) != 0U) ? 1 : 0);
}
static int spi_nor_wait_ready(void)
{
int ret;
uint64_t timeout = timeout_init_us(SPI_READY_TIMEOUT_US);
while (!timeout_elapsed(timeout)) {
ret = spi_nor_ready();
if (ret <= 0) {
return ret;
}
}
return -ETIMEDOUT;
}
static int spi_nor_macronix_quad_enable(void)
{
uint8_t sr;
int ret;
ret = spi_nor_read_sr(&sr);
if (ret != 0) {
return ret;
}
if ((sr & SR_QUAD_EN_MX) == 0U) {
return 0;
}
ret = spi_nor_write_en();
if (ret != 0) {
return ret;
}
sr |= SR_QUAD_EN_MX;
ret = spi_nor_reg(SPI_NOR_OP_WRSR, &sr, 1, SPI_MEM_DATA_OUT);
if (ret != 0) {
return ret;
}
ret = spi_nor_wait_ready();
if (ret != 0) {
return ret;
}
ret = spi_nor_read_sr(&sr);
if ((ret != 0) || ((sr & SR_QUAD_EN_MX) == 0U)) {
return -EINVAL;
}
return 0;
}
static int spi_nor_write_sr_cr(uint8_t *sr_cr)
{
int ret;
ret = spi_nor_write_en();
if (ret != 0) {
return ret;
}
ret = spi_nor_reg(SPI_NOR_OP_WRSR, sr_cr, 2, SPI_MEM_DATA_OUT);
if (ret != 0) {
return -EINVAL;
}
ret = spi_nor_wait_ready();
if (ret != 0) {
return ret;
}
return 0;
}
static int spi_nor_quad_enable(void)
{
uint8_t sr_cr[2];
int ret;
ret = spi_nor_read_cr(&sr_cr[1]);
if (ret != 0) {
return ret;
}
if ((sr_cr[1] & CR_QUAD_EN_SPAN) != 0U) {
return 0;
}
sr_cr[1] |= CR_QUAD_EN_SPAN;
ret = spi_nor_read_sr(&sr_cr[0]);
if (ret != 0) {
return ret;
}
ret = spi_nor_write_sr_cr(sr_cr);
if (ret != 0) {
return ret;
}
ret = spi_nor_read_cr(&sr_cr[1]);
if ((ret != 0) || ((sr_cr[1] & CR_QUAD_EN_SPAN) == 0U)) {
return -EINVAL;
}
return 0;
}
static int spi_nor_clean_bar(void)
{
int ret;
if (nor_dev.selected_bank == 0U) {
return 0;
}
nor_dev.selected_bank = 0U;
ret = spi_nor_write_en();
if (ret != 0) {
return ret;
}
return spi_nor_reg(nor_dev.bank_write_cmd, &nor_dev.selected_bank,
1, SPI_MEM_DATA_OUT);
}
static int spi_nor_write_bar(uint32_t offset)
{
uint8_t selected_bank = offset / BANK_SIZE;
int ret;
if (selected_bank == nor_dev.selected_bank) {
return 0;
}
ret = spi_nor_write_en();
if (ret != 0) {
return ret;
}
ret = spi_nor_reg(nor_dev.bank_write_cmd, &selected_bank,
1, SPI_MEM_DATA_OUT);
if (ret != 0) {
return ret;
}
nor_dev.selected_bank = selected_bank;
return 0;
}
static int spi_nor_read_bar(void)
{
uint8_t selected_bank = 0;
int ret;
ret = spi_nor_reg(nor_dev.bank_read_cmd, &selected_bank,
1, SPI_MEM_DATA_IN);
if (ret != 0) {
return ret;
}
nor_dev.selected_bank = selected_bank;
return 0;
}
int spi_nor_read(unsigned int offset, uintptr_t buffer, size_t length,
size_t *length_read)
{
size_t remain_len;
int ret;
*length_read = 0;
nor_dev.read_op.addr.val = offset;
nor_dev.read_op.data.buf = (void *)buffer;
VERBOSE("%s offset %i length %zu\n", __func__, offset, length);
while (length != 0U) {
if ((nor_dev.flags & SPI_NOR_USE_BANK) != 0U) {
ret = spi_nor_write_bar(nor_dev.read_op.addr.val);
if (ret != 0) {
return ret;
}
remain_len = (BANK_SIZE * (nor_dev.selected_bank + 1)) -
nor_dev.read_op.addr.val;
nor_dev.read_op.data.nbytes = MIN(length, remain_len);
} else {
nor_dev.read_op.data.nbytes = length;
}
ret = spi_mem_exec_op(&nor_dev.read_op);
if (ret != 0) {
spi_nor_clean_bar();
return ret;
}
length -= nor_dev.read_op.data.nbytes;
nor_dev.read_op.addr.val += nor_dev.read_op.data.nbytes;
nor_dev.read_op.data.buf += nor_dev.read_op.data.nbytes;
*length_read += nor_dev.read_op.data.nbytes;
}
if ((nor_dev.flags & SPI_NOR_USE_BANK) != 0U) {
ret = spi_nor_clean_bar();
if (ret != 0) {
return ret;
}
}
return 0;
}
int spi_nor_init(unsigned long long *size, unsigned int *erase_size)
{
int ret = 0;
uint8_t id;
/* Default read command used */
nor_dev.read_op.cmd.opcode = SPI_NOR_OP_READ;
nor_dev.read_op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
nor_dev.read_op.addr.nbytes = 3U;
nor_dev.read_op.addr.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
nor_dev.read_op.data.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
nor_dev.read_op.data.dir = SPI_MEM_DATA_IN;
if (plat_get_nor_data(&nor_dev) != 0) {
return -EINVAL;
}
assert(nor_dev.size != 0);
if (nor_dev.size > BANK_SIZE) {
nor_dev.flags |= SPI_NOR_USE_BANK;
}
*size = nor_dev.size;
ret = spi_nor_read_id(&id);
if (ret != 0) {
return ret;
}
if ((nor_dev.flags & SPI_NOR_USE_BANK) != 0U) {
switch (id) {
case SPANSION_ID:
nor_dev.bank_read_cmd = SPINOR_OP_BRRD;
nor_dev.bank_write_cmd = SPINOR_OP_BRWR;
break;
default:
nor_dev.bank_read_cmd = SPINOR_OP_RDEAR;
nor_dev.bank_write_cmd = SPINOR_OP_WREAR;
break;
}
}
if (nor_dev.read_op.data.buswidth == 4U) {
switch (id) {
case MACRONIX_ID:
WARN("Enable Macronix quad support\n");
ret = spi_nor_macronix_quad_enable();
break;
case MICRON_ID:
break;
default:
ret = spi_nor_quad_enable();
break;
}
}
if ((ret == 0) && ((nor_dev.flags & SPI_NOR_USE_BANK) != 0U)) {
ret = spi_nor_read_bar();
}
return ret;
}

58
include/drivers/spi_nor.h Normal file
View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2019, STMicroelectronics - All Rights Reserved
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef DRIVERS_SPI_NOR_H
#define DRIVERS_SPI_NOR_H
#include <drivers/spi_mem.h>
/* OPCODE */
#define SPI_NOR_OP_WREN 0x06U /* Write enable */
#define SPI_NOR_OP_WRSR 0x01U /* Write status register 1 byte */
#define SPI_NOR_OP_READ_ID 0x9FU /* Read JEDEC ID */
#define SPI_NOR_OP_READ_CR 0x35U /* Read configuration register */
#define SPI_NOR_OP_READ_SR 0x05U /* Read status register */
#define SPI_NOR_OP_READ_FSR 0x70U /* Read flag status register */
#define SPINOR_OP_RDEAR 0xC8U /* Read Extended Address Register */
#define SPINOR_OP_WREAR 0xC5U /* Write Extended Address Register */
/* Used for Spansion flashes only. */
#define SPINOR_OP_BRWR 0x17U /* Bank register write */
#define SPINOR_OP_BRRD 0x16U /* Bank register read */
#define SPI_NOR_OP_READ 0x03U /* Read data bytes (low frequency) */
#define SPI_NOR_OP_READ_FAST 0x0BU /* Read data bytes (high frequency) */
#define SPI_NOR_OP_READ_1_1_2 0x3BU /* Read data bytes (Dual Output SPI) */
#define SPI_NOR_OP_READ_1_2_2 0xBBU /* Read data bytes (Dual I/O SPI) */
#define SPI_NOR_OP_READ_1_1_4 0x6BU /* Read data bytes (Quad Output SPI) */
#define SPI_NOR_OP_READ_1_4_4 0xEBU /* Read data bytes (Quad I/O SPI) */
/* Flags for NOR specific configuration */
#define SPI_NOR_USE_FSR BIT(0)
#define SPI_NOR_USE_BANK BIT(1)
struct nor_device {
struct spi_mem_op read_op;
uint32_t size;
uint32_t flags;
uint8_t selected_bank;
uint8_t bank_write_cmd;
uint8_t bank_read_cmd;
};
int spi_nor_read(unsigned int offset, uintptr_t buffer, size_t length,
size_t *length_read);
int spi_nor_init(unsigned long long *device_size, unsigned int *erase_size);
/*
* Platform can implement this to override default NOR instance configuration.
*
* @device: target NOR instance.
* Return 0 on success, negative value otherwise.
*/
int plat_get_nor_data(struct nor_device *device);
#endif /* DRIVERS_SPI_NOR_H */