Microchip® Advanced Software Framework

nand_flash_raw_nfc.c File Reference

Raw flash operation.

This file contains definitions and functions for raw NAND Flash operation.

Copyright (c) 2012-2018 Microchip Technology Inc. and its subsidiaries.

#include <string.h>
#include "nand_flash_raw.h"
#include "conf_board.h"
#include "gpio.h"
#include "smc.h"
#include "pmc.h"
#include "dmac.h"
#include "delay.h"

Macros

#define CONF_NF_ADDRESS_CYCLE_CMD   NFCADDR_CMD_ACYCLE_FOUR
 
#define CONF_NF_DMA_CHANNEL   0
 
#define MODEL(raw)   ((struct nand_flash_model *)raw)
 
#define READ_DATA16(raw)   (*((volatile uint16_t *)raw->data_address))
 
#define READ_DATA8(raw)   (*((volatile uint8_t *)raw->data_address))
 

Functions

static uint32_t check_status (const struct nand_flash_raw *raw)
 Check if program or erase operation is completed in current operation. More...
 
static uint32_t copy_page (const struct nand_flash_raw *raw, uint16_t source_block, uint16_t source_page, uint16_t dest_block, uint16_t dest_page)
 Copy the data in a page of the NAND Flash device to an other page on that same chip. More...
 
static uint32_t erase_block (const struct nand_flash_raw *raw, uint16_t block)
 Erase the specified block of the NAND Flash chip. More...
 
uint32_t nand_flash_raw_copy_block (const struct nand_flash_raw *raw, uint16_t source_block, uint16_t dest_block)
 Copy the data of one whole block of a NAND Flash device to another block. More...
 
uint32_t nand_flash_raw_copy_page (const struct nand_flash_raw *raw, uint16_t source_block, uint16_t source_page, uint16_t dest_block, uint16_t dest_page)
 Copy the data in a page of the NAND Flash device to an other page on that same chip. More...
 
uint32_t nand_flash_raw_erase_block (const struct nand_flash_raw *raw, uint16_t block)
 Erase the specified block of the device, retrying several time if it fails. More...
 
uint32_t nand_flash_raw_initialize (struct nand_flash_raw *raw, const struct nand_flash_model *model, uint32_t command_address, uint32_t address_address, uint32_t data_address)
 Initialize a nand_flash_raw instance based on the given model and physical interface. More...
 
uint32_t nand_flash_raw_read_id (const struct nand_flash_raw *raw)
 Reads and returns the identifiers of a NAND Flash chip. More...
 
void nand_flash_raw_read_page (const struct nand_flash_raw *raw, uint16_t block, uint16_t page, uint8_t *data, uint8_t *spare)
 Read the data and/or the spare areas of a page of a NAND Flash into the provided buffers. More...
 
void nand_flash_raw_reset (const struct nand_flash_raw *raw)
 Reset a NAND Flash device. More...
 
uint32_t nand_flash_raw_write_page (const struct nand_flash_raw *raw, uint16_t block, uint16_t page, uint8_t *data, uint8_t *spare)
 Write the data and/or the spare areas of a page of a NAND Flash into the provided buffers. More...
 
static void nfc_copy_data_from_internal_sram (uint8_t *data, uint32_t sram_offset, uint16_t size)
 Copy data from the NFC sram to the provided buffer. More...
 
static void nfc_copy_data_to_internal_sram (uint8_t *data, uint32_t sram_offset, uint16_t size)
 Send data to the NFC sram from the provided buffer. More...
 
static void nfc_translate_address (const struct nand_flash_raw *raw, uint32_t column_address, uint32_t row_address, uint32_t *p_address_cycle0, uint32_t *p_address_cycle1234, uint32_t five_address)
 Translate the given column and row address into first and other (1-4) address cycles. More...
 
static uint32_t write_page (const struct nand_flash_raw *raw, uint16_t block, uint16_t page, void *data, void *spare)
 Write the data and/or the spare area of a page on a NAND Flash chip. More...
 

#define CONF_NF_ADDRESS_CYCLE_CMD   NFCADDR_CMD_ACYCLE_FOUR
#define READ_DATA16 (   raw)    (*((volatile uint16_t *)raw->data_address))
#define READ_DATA8 (   raw)    (*((volatile uint8_t *)raw->data_address))

static uint32_t check_status ( const struct nand_flash_raw raw)
static

Check if program or erase operation is completed in current operation.

Parameters
rawPointer to a nand_flash_raw instance.

References NAND_COMMAND_STATUS, NAND_STATUS_ERROR, NAND_STATUS_READY, NFCADDR_CMD_ACYCLE_NONE, NFCADDR_CMD_CSID, NFCADDR_CMD_NFC_READ, NFCADDR_CMD_NFCCMD, READ_DATA8, and smc_nfc_send_command().

Referenced by copy_page(), erase_block(), and write_page().

static uint32_t copy_page ( const struct nand_flash_raw raw,
uint16_t  source_block,
uint16_t  source_page,
uint16_t  dest_block,
uint16_t  dest_page 
)
static

Copy the data in a page of the NAND Flash device to an other page on that same chip.

Parameters
rawPointer to a nand_flash_raw instance.
source_blockSource block number.
source_pageSource page number inside the source block.
dest_blockDestination block number.
dest_pageDestination page number inside the destination block.
Returns
0 if successful; otherwise return the error code.

References check_status(), CONF_NF_ADDRESS_CYCLE_CMD, data, MODEL, NAND_COMMAND_COPYBACK_PROGRAM_1, NAND_COMMAND_COPYBACK_PROGRAM_2, NAND_COMMAND_COPYBACK_READ_1, NAND_COMMAND_COPYBACK_READ_2, NAND_COMMON_ERROR_CANNOTCOPY, NAND_COMMON_ERROR_CANNOTWRITE, NAND_COMMON_MAX_PAGE_SIZE, NAND_COMMON_MAX_PAGE_SPARE_SIZE, nand_flash_model_copy_back(), nand_flash_model_get_block_size_in_pages(), nand_flash_raw_read_page(), nand_flash_raw_write_page(), nfc_translate_address(), NFCADDR_CMD_CSID, NFCADDR_CMD_NFC_READ, NFCADDR_CMD_NFCCMD, NFCADDR_CMD_VCMD2, smc_nfc_get_status(), and smc_nfc_send_command().

Referenced by nand_flash_raw_copy_page().

static uint32_t erase_block ( const struct nand_flash_raw raw,
uint16_t  block 
)
static

Erase the specified block of the NAND Flash chip.

Parameters
rawPointer to a nand_flash_raw instance.
blockNumber of the physical block to erase.
Returns
0 if successful; otherwise return the error code.

References check_status(), delay_ms, MODEL, NAND_COMMAND_ERASE_1, NAND_COMMAND_ERASE_2, NAND_COMMON_ERROR_CANNOTERASE, nand_flash_model_get_block_size_in_pages(), nfc_translate_address(), NFCADDR_CMD_ACYCLE_THREE, NFCADDR_CMD_CSID, NFCADDR_CMD_NFC_READ, NFCADDR_CMD_NFCCMD, NFCADDR_CMD_VCMD2, smc_nfc_get_status(), and smc_nfc_send_command().

Referenced by nand_flash_raw_erase_block().

uint32_t nand_flash_raw_copy_block ( const struct nand_flash_raw raw,
uint16_t  source_block,
uint16_t  dest_block 
)

Copy the data of one whole block of a NAND Flash device to another block.

Parameters
rawPointer to a nand_flash_raw instance.
source_blockSource block number.
dest_blockDestination block number.
Returns
0 if successful; otherwise returns an NAND_COMMON_ERROR_BADBLOCK.
Note
Reference the Flash datasheet for the block address restriction.

References Assert, MODEL, NAND_COMMON_ERROR_BADBLOCK, nand_flash_model_get_block_size_in_pages(), and nand_flash_raw_copy_page().

uint32_t nand_flash_raw_copy_page ( const struct nand_flash_raw raw,
uint16_t  source_block,
uint16_t  source_page,
uint16_t  dest_block,
uint16_t  dest_page 
)

Copy the data in a page of the NAND Flash device to an other page on that same chip.

Parameters
rawPointer to a nand_flash_raw instance.
source_blockSource block number.
source_pageSource page number inside the source block.
dest_blockDestination block number.
dest_pageDestination page number inside the destination block.
Returns
0 if successful; otherwise returns an NAND_COMMON_ERROR_BADBLOCK.
Note
Reference the Flash datasheet for the block and page address restriction.

References copy_page(), and NAND_COMMON_ERROR_BADBLOCK.

Referenced by nand_flash_raw_copy_block().

uint32_t nand_flash_raw_erase_block ( const struct nand_flash_raw raw,
uint16_t  block 
)

Erase the specified block of the device, retrying several time if it fails.

Parameters
rawPointer to a nand_flash_raw instance.
blockNumber of the physical block to erase.
Returns
0 if successful; otherwise returns NAND_COMMON_ERROR_BADBLOCK.

References erase_block(), and NAND_COMMON_ERROR_BADBLOCK.

Referenced by main(), run_test_raw_read_write(), and run_test_software_ecc().

uint32_t nand_flash_raw_initialize ( struct nand_flash_raw raw,
const struct nand_flash_model model,
uint32_t  command_address,
uint32_t  address_address,
uint32_t  data_address 
)

Initialize a nand_flash_raw instance based on the given model and physical interface.

Parameters
rawPointer to a nand_flash_raw instance.
modelPointer to the underlying nand chip model. Can be 0.
command_addressAddress at which commands are sent.
address_addressAddress at which addresses are sent.
data_addressAddress at which data is sent.
Returns
0 if successful; otherwise returns NAND_COMMON_ERROR_UNKNOWNMODEL.

References nand_flash_raw::address_address, nand_flash_raw::command_address, CONF_NF_DMA_CHANNEL, nand_flash_raw::data_address, data_address, delay_ms, dmac_channel_set_configuration(), dmac_enable(), dmac_init(), DMAC_PRIORITY_ROUND_ROBIN, dmac_set_priority_mode(), nand_flash_raw::model, NAND_COMMON_ERROR_UNKNOWNMODEL, nand_flash_model_find(), nand_flash_model_list, NAND_FLASH_MODEL_LIST_SIZE, nand_flash_raw_read_id(), nand_flash_raw_reset(), nand_flash_model::page_size_in_bytes, pmc_enable_periph_clk(), smc_nfc_enable(), smc_nfc_init(), smc_set_cycle_timing(), smc_set_mode(), smc_set_nand_timing(), smc_set_pulse_timing(), and smc_set_setup_timing().

Referenced by main(), nand_flash_ecc_initialize(), and run_test_initialization().

uint32_t nand_flash_raw_read_id ( const struct nand_flash_raw raw)

Reads and returns the identifiers of a NAND Flash chip.

Parameters
rawPointer to a nand_flash_raw instance.
Returns
id1|(id2<<8)|(id3<<16)|(id4<<24)

References delay_us, NAND_COMMAND_READID, NFCADDR_CMD_ACYCLE_ONE, NFCADDR_CMD_CSID, NFCADDR_CMD_NFC_READ, NFCADDR_CMD_NFCCMD, READ_DATA8, and smc_nfc_send_command().

Referenced by nand_flash_raw_initialize(), and run_test_initialization().

void nand_flash_raw_read_page ( const struct nand_flash_raw raw,
uint16_t  block,
uint16_t  page,
uint8_t *  data,
uint8_t *  spare 
)

Read the data and/or the spare areas of a page of a NAND Flash into the provided buffers.

Parameters
rawPointer to a nand_flash_raw instance.
blockNumber of the physical block to read.
pageNumber of the page to read inside the given block.
dataBuffer where the data area will be read.
spareBuffer where the spare area will be read.
Note
If one of the buffer pointer is 0, the corresponding area is not read.

References CONF_NF_ADDRESS_CYCLE_CMD, MODEL, NAND_COMMAND_READ_1, NAND_COMMAND_READ_2, NAND_COMMAND_READ_A, nand_flash_model_get_block_size_in_pages(), nand_flash_model_get_page_data_size(), nand_flash_model_get_page_spare_size(), nand_flash_model_small_block(), nfc_copy_data_from_internal_sram(), nfc_translate_address(), NFCADDR_CMD_CSID, NFCADDR_CMD_NFC_READ, NFCADDR_CMD_NFCCMD, NFCADDR_CMD_NFCEN, NFCADDR_CMD_VCMD2, smc_nfc_disable_spare_read(), smc_nfc_enable_spare_read(), smc_nfc_get_status(), and smc_nfc_send_command().

Referenced by copy_page(), main(), nand_flash_ecc_read_page(), run_test_raw_read_write(), and run_test_software_ecc().

void nand_flash_raw_reset ( const struct nand_flash_raw raw)
uint32_t nand_flash_raw_write_page ( const struct nand_flash_raw raw,
uint16_t  block,
uint16_t  page,
uint8_t *  data,
uint8_t *  spare 
)

Write the data and/or the spare areas of a page of a NAND Flash into the provided buffers.

Parameters
rawPointer to a nand_flash_raw instance.
blockNumber of the physical block to write resides.
pageNumber of the page to write inside the given block.
dataBuffer where the data area will be stored.
spareBuffer where the spare area will be stored.
Returns
0 if successful; otherwise returns NAND_COMMON_ERROR_BADBLOCK.
Note
If one of the buffer pointer is 0, the corresponding area is not written.

References NAND_COMMON_ERROR_BADBLOCK, and write_page().

Referenced by copy_page(), main(), nand_flash_ecc_write_page(), and run_test_raw_read_write().

static void nfc_copy_data_from_internal_sram ( uint8_t *  data,
uint32_t  sram_offset,
uint16_t  size 
)
static

Copy data from the NFC sram to the provided buffer.

Parameters
dataBuffer where the data area will be stored.
sram_offsetNFC internal sram start offset.
sizeNumber of data bytes to receive.

References CONF_NF_DMA_CHANNEL, dmac_channel_enable(), dmac_channel_is_transfer_done(), dmac_channel_single_buf_transfer_init(), NULL, dma_transfer_descriptor_t::ul_ctrlA, dma_transfer_descriptor_t::ul_ctrlB, dma_transfer_descriptor_t::ul_descriptor_addr, dma_transfer_descriptor_t::ul_destination_addr, and dma_transfer_descriptor_t::ul_source_addr.

Referenced by nand_flash_raw_read_page().

static void nfc_copy_data_to_internal_sram ( uint8_t *  data,
uint32_t  sram_offset,
uint16_t  size 
)
static

Send data to the NFC sram from the provided buffer.

Parameters
dataPointer to where the data stored.
sram_offsetNFC internal sram start offset.
sizeNumber of data bytes to send.

References CONF_NF_DMA_CHANNEL, data, dmac_channel_enable(), dmac_channel_is_transfer_done(), dmac_channel_single_buf_transfer_init(), NULL, dma_transfer_descriptor_t::ul_ctrlA, dma_transfer_descriptor_t::ul_ctrlB, dma_transfer_descriptor_t::ul_descriptor_addr, dma_transfer_descriptor_t::ul_destination_addr, and dma_transfer_descriptor_t::ul_source_addr.

Referenced by write_page().

static void nfc_translate_address ( const struct nand_flash_raw raw,
uint32_t  column_address,
uint32_t  row_address,
uint32_t *  p_address_cycle0,
uint32_t *  p_address_cycle1234,
uint32_t  five_address 
)
static

Translate the given column and row address into first and other (1-4) address cycles.

The resulting values are stored in the provided variables if they are not null.

Parameters
rawPointer to a nand_flash_raw instance.
column_addressColumn address to translate.
row_addressRow address to translate.
p_address_cycle0First address cycle.
p_address_cycle1234four address cycles.
five_addressFlag for five address cycles.

References MODEL, nand_flash_model_get_data_bus_width(), nand_flash_model_get_device_size_in_pages(), and nand_flash_model_get_page_data_size().

Referenced by copy_page(), erase_block(), nand_flash_raw_read_page(), and write_page().

static uint32_t write_page ( const struct nand_flash_raw raw,
uint16_t  block,
uint16_t  page,
void *  data,
void *  spare 
)
static

Write the data and/or the spare area of a page on a NAND Flash chip.

Parameters
rawPointer to a nand_flash_raw instance.
blockNumber of the physical block to erase.
pageNumber of the page to write inside the given block.
dataBuffer containing the data area.
spareBuffer containing the spare area.
Returns
0 if successful; otherwise return the error code.
Note
If one of the buffer pointer is 0, the corresponding area is not written.

Note: special case when ECC parity generation. ECC results are available as soon as the counter reaches the end of the main area. But when reach page_size for an example, it could not generate last ECC_PR, The workaround enable SPARE_WRITE, whatever real spare area write or not.

References check_status(), CONF_NF_ADDRESS_CYCLE_CMD, MODEL, NAND_COMMAND_WRITE_1, NAND_COMMAND_WRITE_2, NAND_COMMON_ERROR_CANNOTWRITE, nand_flash_model_get_block_size_in_pages(), nand_flash_model_get_page_data_size(), nand_flash_model_get_page_spare_size(), nfc_copy_data_to_internal_sram(), nfc_translate_address(), NFCADDR_CMD_ACYCLE_NONE, NFCADDR_CMD_CSID, NFCADDR_CMD_NFC_READ, NFCADDR_CMD_NFC_WIRTE, NFCADDR_CMD_NFCCMD, NFCADDR_CMD_NFCEN, smc_nfc_disable_spare_write(), smc_nfc_enable_spare_write(), smc_nfc_get_status(), and smc_nfc_send_command().

Referenced by nand_flash_raw_write_page().