nes-proj/arch/dev/ext-flash/ext-flash.c

537 lines
16 KiB
C
Raw Normal View History

2015-02-25 12:11:09 +00:00
/*
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup ext-flash
2015-02-25 12:11:09 +00:00
* @{
*
* \file
* Implementation of a generic external SPI flash driver
2015-02-25 12:11:09 +00:00
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "ext-flash.h"
2018-03-02 20:37:28 +00:00
#include "dev/spi.h"
#include "gpio-hal.h"
#include "sys/log.h"
#include <stdint.h>
#include <stdbool.h>
2015-02-25 12:11:09 +00:00
/*---------------------------------------------------------------------------*/
#ifndef EXT_FLASH_SPI_CONTROLLER
#define EXT_FLASH_SPI_CONTROLLER 0xFF /* No controller */
#define EXT_FLASH_SPI_PIN_SCK GPIO_HAL_PIN_UNKNOWN
#define EXT_FLASH_SPI_PIN_MOSI GPIO_HAL_PIN_UNKNOWN
#define EXT_FLASH_SPI_PIN_MISO GPIO_HAL_PIN_UNKNOWN
#define EXT_FLASH_SPI_PIN_CS GPIO_HAL_PIN_UNKNOWN
#define EXT_FLASH_DEVICE_ID 0xFF
#define EXT_FLASH_MID 0xFF
#define EXT_FLASH_PROGRAM_PAGE_SIZE 256
#define EXT_FLASH_ERASE_SECTOR_SIZE 4096
#endif /* EXT_FLASH_SPI_CONTROLLER */
/*---------------------------------------------------------------------------*/
/* Log configuration */
#define LOG_MODULE "ext-flash"
#define LOG_LEVEL LOG_LEVEL_NONE
/*---------------------------------------------------------------------------*/
2015-02-25 12:11:09 +00:00
/* Instruction codes */
#define BLS_CODE_PROGRAM 0x02 /**< Page Program */
#define BLS_CODE_READ 0x03 /**< Read Data */
#define BLS_CODE_READ_STATUS 0x05 /**< Read Status Register */
#define BLS_CODE_WRITE_ENABLE 0x06 /**< Write Enable */
#define BLS_CODE_SECTOR_ERASE 0x20 /**< Sector Erase */
#define BLS_CODE_MDID 0x90 /**< Manufacturer Device ID */
#define BLS_CODE_PD 0xB9 /**< Power down */
#define BLS_CODE_RPD 0xAB /**< Release Power-Down */
2015-02-25 12:11:09 +00:00
/*---------------------------------------------------------------------------*/
/* Erase instructions */
#define BLS_CODE_ERASE_4K 0x20 /**< Sector Erase */
#define BLS_CODE_ERASE_32K 0x52
#define BLS_CODE_ERASE_64K 0xD8
#define BLS_CODE_ERASE_ALL 0xC7 /**< Mass Erase */
/*---------------------------------------------------------------------------*/
/* Bitmasks of the status register */
#define BLS_STATUS_SRWD_BM 0x80
#define BLS_STATUS_BP_BM 0x0C
#define BLS_STATUS_WEL_BM 0x02
#define BLS_STATUS_WIP_BM 0x01
#define BLS_STATUS_BIT_BUSY 0x01 /**< Busy bit of the status register */
/*---------------------------------------------------------------------------*/
#define VERIFY_PART_LOCKED -2
#define VERIFY_PART_ERROR -1
#define VERIFY_PART_POWERED_DOWN 0
#define VERIFY_PART_OK 1
/*---------------------------------------------------------------------------*/
static const spi_device_t flash_spi_configuration_default = {
.spi_controller = EXT_FLASH_SPI_CONTROLLER,
.pin_spi_sck = EXT_FLASH_SPI_PIN_SCK,
.pin_spi_miso = EXT_FLASH_SPI_PIN_MISO,
.pin_spi_mosi = EXT_FLASH_SPI_PIN_MOSI,
.pin_spi_cs = EXT_FLASH_SPI_PIN_CS,
.spi_bit_rate = 4000000,
.spi_pha = 0,
.spi_pol = 0
};
2018-04-04 10:08:18 +00:00
/*---------------------------------------------------------------------------*/
/**
* Get spi configuration, return default configuration if NULL
*/
static const spi_device_t *
get_spi_conf(const spi_device_t *conf)
2018-04-07 13:32:00 +00:00
{
2018-04-04 10:08:18 +00:00
if(conf == NULL) {
return &flash_spi_configuration_default;
}
return conf;
2018-04-07 13:32:00 +00:00
}
/*---------------------------------------------------------------------------*/
2015-02-25 12:11:09 +00:00
/**
* Clear external flash CSN line
*/
static bool
select_on_bus(const spi_device_t *flash_spi_configuration)
2015-02-25 12:11:09 +00:00
{
if(spi_select(flash_spi_configuration) == SPI_DEV_STATUS_OK) {
return true;
}
return false;
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
/**
* Set external flash CSN line
*/
static void
deselect(const spi_device_t *flash_spi_configuration)
2015-02-25 12:11:09 +00:00
{
spi_deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
/**
* \brief Wait till previous erase/program operation completes.
* \return True when successful.
2015-02-25 12:11:09 +00:00
*/
static bool
wait_ready(const spi_device_t *flash_spi_configuration)
2015-02-25 12:11:09 +00:00
{
bool ret;
2015-02-25 12:11:09 +00:00
const uint8_t wbuf[1] = { BLS_CODE_READ_STATUS };
2018-04-04 10:08:18 +00:00
if(select_on_bus(flash_spi_configuration) == false) {
return false;
}
2015-02-25 12:11:09 +00:00
ret = spi_write(flash_spi_configuration, wbuf, sizeof(wbuf));
2015-02-25 12:11:09 +00:00
if(ret != SPI_DEV_STATUS_OK) {
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
return false;
2015-02-25 12:11:09 +00:00
}
for(;;) {
uint8_t buf;
/* Note that this temporary implementation is not
* energy efficient.
* Thread could have yielded while waiting for flash
* erase/program to complete.
*/
ret = spi_read(flash_spi_configuration, &buf, sizeof(buf));
2015-02-25 12:11:09 +00:00
if(ret != SPI_DEV_STATUS_OK) {
2015-02-25 12:11:09 +00:00
/* Error */
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
return false;
2015-02-25 12:11:09 +00:00
}
2015-02-25 12:11:09 +00:00
if(!(buf & BLS_STATUS_BIT_BUSY)) {
/* Now ready */
break;
}
}
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
return true;
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
/**
* \brief Verify the flash part.
* \retval VERIFY_PART_OK The part was identified successfully
* \retval VERIFY_PART_ERROR There was an error communicating with the part
* \retval VERIFY_PART_POWERED_DOWN Communication was successful, but the part
* was powered down
*/
static uint8_t
verify_part(const spi_device_t *flash_spi_configuration)
{
const uint8_t wbuf[] = { BLS_CODE_MDID, 0xFF, 0xFF, 0x00 };
uint8_t rbuf[2] = { 0, 0 };
bool ret;
2018-04-04 10:08:18 +00:00
if(select_on_bus(flash_spi_configuration) == false) {
return VERIFY_PART_LOCKED;
}
if(spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) != SPI_DEV_STATUS_OK) {
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
return VERIFY_PART_ERROR;
}
ret = spi_read(flash_spi_configuration, rbuf, sizeof(rbuf));
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
if(ret != SPI_DEV_STATUS_OK) {
return VERIFY_PART_ERROR;
}
LOG_DBG("Verify: %02x %02x\n", rbuf[0], rbuf[1]);
if(rbuf[0] != EXT_FLASH_MID || rbuf[1] != EXT_FLASH_DEVICE_ID) {
return VERIFY_PART_POWERED_DOWN;
}
return VERIFY_PART_OK;
}
/*---------------------------------------------------------------------------*/
2015-02-25 12:11:09 +00:00
/**
* \brief Put the device in power save mode. No access to data; only
* the status register is accessible.
*/
static bool
power_down(const spi_device_t *flash_spi_configuration)
2015-02-25 12:11:09 +00:00
{
uint8_t cmd;
uint8_t i;
2015-02-25 12:11:09 +00:00
/* First, wait for the device to be ready */
2018-04-04 10:08:18 +00:00
if(wait_ready(flash_spi_configuration) == false) {
/* Entering here will leave the device in standby instead of powerdown */
return false;
}
cmd = BLS_CODE_PD;
2018-04-04 10:08:18 +00:00
if(select_on_bus(flash_spi_configuration) == false) {
return false;
}
if(spi_write_byte(flash_spi_configuration, cmd) != SPI_DEV_STATUS_OK) {
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
return false;
}
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
i = 0;
while(i < 10) {
2018-04-04 10:08:18 +00:00
if(verify_part(flash_spi_configuration) == VERIFY_PART_POWERED_DOWN) {
/* Device is powered down */
return true;
}
i++;
}
/* Should not be required */
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
return false;
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
/**
* \brief Take device out of power save mode and prepare it for normal operation
* \return True if the command was written successfully
*/
static bool
power_standby(const spi_device_t *flash_spi_configuration)
2015-02-25 12:11:09 +00:00
{
uint8_t cmd;
bool success;
cmd = BLS_CODE_RPD;
2018-04-04 10:08:18 +00:00
if(select_on_bus(flash_spi_configuration) == false) {
return false;
}
success = (spi_write(flash_spi_configuration, &cmd, sizeof(cmd)) == SPI_DEV_STATUS_OK);
2015-02-25 12:11:09 +00:00
if(success) {
2018-04-04 10:08:18 +00:00
success = wait_ready(flash_spi_configuration) == true ? true : false;
2015-02-25 12:11:09 +00:00
}
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
return success;
}
/*---------------------------------------------------------------------------*/
/**
* \brief Enable write.
* \return True when successful.
2015-02-25 12:11:09 +00:00
*/
static bool
write_enable(const spi_device_t *flash_spi_configuration)
2015-02-25 12:11:09 +00:00
{
bool ret;
2015-02-25 12:11:09 +00:00
const uint8_t wbuf[] = { BLS_CODE_WRITE_ENABLE };
2018-04-04 10:08:18 +00:00
if(select_on_bus(flash_spi_configuration) == false) {
return false;
}
ret = (spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) == SPI_DEV_STATUS_OK);
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
if(ret == false) {
return false;
2015-02-25 12:11:09 +00:00
}
return true;
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
bool
ext_flash_open(const spi_device_t *conf)
2015-02-25 12:11:09 +00:00
{
const spi_device_t *flash_spi_configuration;
2018-04-04 10:08:18 +00:00
flash_spi_configuration = get_spi_conf(conf);
/* Check if platform has ext-flash */
if(flash_spi_configuration->pin_spi_sck == GPIO_HAL_PIN_UNKNOWN) {
return false;
}
2015-02-25 12:11:09 +00:00
if(spi_acquire(flash_spi_configuration) != SPI_DEV_STATUS_OK) {
return false;
}
2015-02-25 12:11:09 +00:00
/* Default output to clear chip select */
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
/* Put the part is standby mode */
2018-04-04 10:08:18 +00:00
power_standby(flash_spi_configuration);
2018-04-07 13:32:00 +00:00
if(verify_part(flash_spi_configuration) == VERIFY_PART_OK) {
2018-04-04 10:08:18 +00:00
return true;
}
2015-02-25 12:11:09 +00:00
2018-04-04 10:08:18 +00:00
/* Failed to verify */
spi_release(flash_spi_configuration);
return false;
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
bool
ext_flash_close(const spi_device_t *conf)
2015-02-25 12:11:09 +00:00
{
2018-04-04 10:08:18 +00:00
bool ret;
const spi_device_t *flash_spi_configuration;
2015-02-25 12:11:09 +00:00
2018-04-04 10:08:18 +00:00
flash_spi_configuration = get_spi_conf(conf);
/* Put the part in low power mode */
ret = power_down(flash_spi_configuration);
2018-04-07 13:32:00 +00:00
2018-04-04 10:08:18 +00:00
/* SPI is released no matter if power_down() succeeds or fails */
if(spi_release(flash_spi_configuration) != SPI_DEV_STATUS_OK) {
return false;
}
2018-04-04 10:08:18 +00:00
return ret;
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
bool
ext_flash_read(const spi_device_t *conf, uint32_t offset, uint32_t length, uint8_t *buf)
2015-02-25 12:11:09 +00:00
{
uint8_t wbuf[4];
bool ret;
2015-02-25 12:11:09 +00:00
const spi_device_t *flash_spi_configuration;
2018-04-04 10:08:18 +00:00
flash_spi_configuration = get_spi_conf(conf);
2015-02-25 12:11:09 +00:00
/* Wait till previous erase/program operation completes */
2018-04-04 10:08:18 +00:00
if(wait_ready(flash_spi_configuration) == false) {
2015-02-25 12:11:09 +00:00
return false;
}
/*
* SPI is driven with very low frequency (1MHz < 33MHz fR spec)
* in this implementation, hence it is not necessary to use fast read.
*/
wbuf[0] = BLS_CODE_READ;
wbuf[1] = (offset >> 16) & 0xff;
wbuf[2] = (offset >> 8) & 0xff;
wbuf[3] = offset & 0xff;
2018-04-04 10:08:18 +00:00
if(select_on_bus(flash_spi_configuration) == false) {
return false;
}
2015-02-25 12:11:09 +00:00
if(spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) != SPI_DEV_STATUS_OK) {
2015-02-25 12:11:09 +00:00
/* failure */
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
return false;
}
ret = (spi_read(flash_spi_configuration, buf, length) == SPI_DEV_STATUS_OK);
2015-02-25 12:11:09 +00:00
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
return ret;
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
bool
ext_flash_write(const spi_device_t *conf, uint32_t offset, uint32_t length, const uint8_t *buf)
2015-02-25 12:11:09 +00:00
{
uint8_t wbuf[4];
uint32_t ilen; /* interim length per instruction */
2015-02-25 12:11:09 +00:00
const spi_device_t *flash_spi_configuration;
2018-04-04 10:08:18 +00:00
flash_spi_configuration = get_spi_conf(conf);
2015-02-25 12:11:09 +00:00
while(length > 0) {
/* Wait till previous erase/program operation completes */
2018-04-04 10:08:18 +00:00
if(wait_ready(flash_spi_configuration) == false) {
2015-02-25 12:11:09 +00:00
return false;
}
2018-04-04 10:08:18 +00:00
if(write_enable(flash_spi_configuration) == false) {
2015-02-25 12:11:09 +00:00
return false;
}
ilen = EXT_FLASH_PROGRAM_PAGE_SIZE - (offset % EXT_FLASH_PROGRAM_PAGE_SIZE);
2015-02-25 12:11:09 +00:00
if(length < ilen) {
ilen = length;
}
wbuf[0] = BLS_CODE_PROGRAM;
wbuf[1] = (offset >> 16) & 0xff;
wbuf[2] = (offset >> 8) & 0xff;
wbuf[3] = offset & 0xff;
offset += ilen;
length -= ilen;
/* Upto 100ns CS hold time (which is not clear
* whether it's application only inbetween reads)
* is not imposed here since above instructions
* should be enough to delay
* as much. */
2018-04-04 10:08:18 +00:00
if(select_on_bus(flash_spi_configuration) == false) {
return false;
}
2015-02-25 12:11:09 +00:00
if(spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) != SPI_DEV_STATUS_OK) {
2015-02-25 12:11:09 +00:00
/* failure */
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
return false;
}
if(spi_write(flash_spi_configuration, buf, ilen) != SPI_DEV_STATUS_OK) {
2015-02-25 12:11:09 +00:00
/* failure */
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
return false;
}
buf += ilen;
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
}
return true;
}
/*---------------------------------------------------------------------------*/
bool
ext_flash_erase(const spi_device_t *conf, uint32_t offset, uint32_t length)
2015-02-25 12:11:09 +00:00
{
/*
* Note that Block erase might be more efficient when the floor map
* is well planned for OTA, but to simplify this implementation,
* sector erase is used blindly.
*/
uint8_t wbuf[4];
uint32_t i, numsectors;
uint32_t endoffset = offset + length - 1;
2018-04-07 13:32:00 +00:00
const spi_device_t *flash_spi_configuration;
2018-04-04 10:08:18 +00:00
flash_spi_configuration = get_spi_conf(conf);
2015-02-25 12:11:09 +00:00
offset = (offset / EXT_FLASH_ERASE_SECTOR_SIZE) * EXT_FLASH_ERASE_SECTOR_SIZE;
numsectors = (endoffset - offset + EXT_FLASH_ERASE_SECTOR_SIZE - 1) / EXT_FLASH_ERASE_SECTOR_SIZE;
2015-02-25 12:11:09 +00:00
wbuf[0] = BLS_CODE_SECTOR_ERASE;
for(i = 0; i < numsectors; i++) {
/* Wait till previous erase/program operation completes */
2018-04-04 10:08:18 +00:00
if(wait_ready(flash_spi_configuration) == false) {
2015-02-25 12:11:09 +00:00
return false;
}
2018-04-04 10:08:18 +00:00
if(write_enable(flash_spi_configuration) == false) {
2015-02-25 12:11:09 +00:00
return false;
}
wbuf[1] = (offset >> 16) & 0xff;
wbuf[2] = (offset >> 8) & 0xff;
wbuf[3] = offset & 0xff;
2018-04-04 10:08:18 +00:00
if(select_on_bus(flash_spi_configuration) == false) {
return false;
}
2015-02-25 12:11:09 +00:00
if(spi_write(flash_spi_configuration, wbuf, sizeof(wbuf)) != SPI_DEV_STATUS_OK) {
2015-02-25 12:11:09 +00:00
/* failure */
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
return false;
}
2018-04-04 10:08:18 +00:00
deselect(flash_spi_configuration);
2015-02-25 12:11:09 +00:00
offset += EXT_FLASH_ERASE_SECTOR_SIZE;
2015-02-25 12:11:09 +00:00
}
return true;
}
/*---------------------------------------------------------------------------*/
bool
ext_flash_init(const spi_device_t *conf)
2015-02-25 12:11:09 +00:00
{
2018-04-04 10:08:18 +00:00
if(ext_flash_open(conf) == false) {
return false;
}
2015-02-25 12:11:09 +00:00
2018-04-04 10:08:18 +00:00
if(ext_flash_close(conf) == false) {
return false;
}
2015-02-25 12:11:09 +00:00
2018-04-04 10:08:18 +00:00
LOG_INFO("Flash init successful\n");
return true;
2015-02-25 12:11:09 +00:00
}
/*---------------------------------------------------------------------------*/
/** @} */