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 "nand_flash_raw.h"
#include "conf_board.h"
#include "conf_nf.h"
#include "gpio.h"
#include "smc.h"
#include "sysclk.h"
#include "conf_ebi.h"
#include "delay.h"
Macros | |
#define | BOARD_NAND_CS 0 |
#define | CONF_NF_BUSWIDTH 0 |
#define | CONF_NF_CYCLE_TIMING 0 |
#define | CONF_NF_PULSE_TIMING 0 |
#define | CONF_NF_SETUP_TIMING 0 |
#define | DISABLE_CE(raw) gpio_set_pin_high(PIN_NF_CE_IDX) |
#define | ENABLE_CE(raw) gpio_set_pin_low(PIN_NF_CE_IDX) |
#define | MODEL(raw) ((struct nand_flash_model *)raw) |
#define | NF_WP_PIN 0 |
#define | PIN_NF_CE_IDX 0 |
#define | PIN_NF_RB_IDX 0 |
#define | READ_DATA16(raw) (*((volatile uint16_t *)raw->data_address)) |
#define | READ_DATA8(raw) (*((volatile uint8_t *)raw->data_address)) |
#define | WRITE_ADDRESS(raw, address) {*((volatile uint8_t *)raw->address_address) = (uint8_t)address;} |
#define | WRITE_ADDRESS16(raw, address) {*((volatile uint16_t *)raw->address_address) = (uint16_t)address;} |
#define | WRITE_COMMAND(raw, command) {*((volatile uint8_t *)raw->command_address) = (uint8_t)command;} |
#define | WRITE_COMMAND16(raw, command) {*((volatile uint16_t *)raw->command_address) = (uint16_t)command;} |
#define | WRITE_DATA16(raw, data) {*((volatile uint16_t *)raw->data_address) = (uint16_t)data;} |
#define | WRITE_DATA8(raw, data) {*((volatile uint8_t *)raw->data_address) = (uint8_t)data;} |
Functions | |
static uint32_t | check_status (const struct nand_flash_raw *raw) |
Check for program or erase operation is completed. 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 device. More... | |
uint32_t | nand_flash_raw_copy_block (const struct nand_flash_raw *raw, uint16_t source_block, uint16_t dest_block) |
Copies 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) |
Initializes 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) |
Read and return 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 into a NAND Flash. More... | |
static void | read_data (const struct nand_flash_raw *raw, uint8_t *buffer, uint32_t size) |
Read data from the NAND Flash chip into the provided buffer. More... | |
static void | wait_ready (void) |
Wait for the completion of a page program, erase and random read completion. More... | |
static void | write_column_address (const struct nand_flash_raw *raw, uint16_t column_address) |
Send the column address to the NAND Flash chip. More... | |
static void | write_data (const struct nand_flash_raw *raw, uint8_t *buffer, uint32_t size) |
Send data to the NAND Flash chip from the provided buffer. 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... | |
static void | write_row_address (const struct nand_flash_raw *raw, uint32_t row_address) |
Send the row address to the NAND Flash chip. More... | |
#define BOARD_NAND_CS 0 |
Referenced by nand_flash_raw_initialize().
#define CONF_NF_BUSWIDTH 0 |
Referenced by nand_flash_raw_initialize().
#define CONF_NF_CYCLE_TIMING 0 |
Referenced by nand_flash_raw_initialize().
#define CONF_NF_PULSE_TIMING 0 |
Referenced by nand_flash_raw_initialize().
#define CONF_NF_SETUP_TIMING 0 |
Referenced by nand_flash_raw_initialize().
#define DISABLE_CE | ( | raw | ) | gpio_set_pin_high(PIN_NF_CE_IDX) |
Referenced by copy_page(), erase_block(), nand_flash_raw_read_id(), nand_flash_raw_read_page(), nand_flash_raw_reset(), and write_page().
#define ENABLE_CE | ( | raw | ) | gpio_set_pin_low(PIN_NF_CE_IDX) |
Referenced by copy_page(), erase_block(), nand_flash_raw_read_id(), nand_flash_raw_read_page(), nand_flash_raw_reset(), and write_page().
#define MODEL | ( | raw | ) | ((struct nand_flash_model *)raw) |
#define NF_WP_PIN 0 |
Referenced by nand_flash_raw_initialize().
#define PIN_NF_CE_IDX 0 |
Referenced by nand_flash_raw_initialize().
#define PIN_NF_RB_IDX 0 |
Referenced by nand_flash_raw_initialize(), and wait_ready().
#define READ_DATA16 | ( | raw | ) | (*((volatile uint16_t *)raw->data_address)) |
Referenced by read_data().
#define READ_DATA8 | ( | raw | ) | (*((volatile uint8_t *)raw->data_address)) |
Referenced by check_status(), nand_flash_raw_read_id(), and read_data().
#define WRITE_ADDRESS | ( | raw, | |
address | |||
) | {*((volatile uint8_t *)raw->address_address) = (uint8_t)address;} |
Referenced by nand_flash_raw_read_id(), write_column_address(), and write_row_address().
#define WRITE_ADDRESS16 | ( | raw, | |
address | |||
) | {*((volatile uint16_t *)raw->address_address) = (uint16_t)address;} |
Referenced by write_column_address(), and write_row_address().
#define WRITE_COMMAND | ( | raw, | |
command | |||
) | {*((volatile uint8_t *)raw->command_address) = (uint8_t)command;} |
Referenced by check_status(), copy_page(), erase_block(), nand_flash_raw_read_id(), nand_flash_raw_read_page(), nand_flash_raw_reset(), and write_page().
#define WRITE_COMMAND16 | ( | raw, | |
command | |||
) | {*((volatile uint16_t *)raw->command_address) = (uint16_t)command;} |
#define WRITE_DATA16 | ( | raw, | |
data | |||
) | {*((volatile uint16_t *)raw->data_address) = (uint16_t)data;} |
Referenced by write_data().
#define WRITE_DATA8 | ( | raw, | |
data | |||
) | {*((volatile uint8_t *)raw->data_address) = (uint8_t)data;} |
Referenced by write_data().
|
static |
Check for program or erase operation is completed.
raw | Pointer to a nand_flash_raw instance. |
References NAND_COMMAND_STATUS, NAND_STATUS_ERROR, NAND_STATUS_READY, READ_DATA8, status, and WRITE_COMMAND.
Referenced by copy_page(), erase_block(), and write_page().
|
static |
Copy the data in a page of the NAND Flash device to an other page on that same chip.
raw | Pointer to a nand_flash_raw instance. |
source_block | Source block number. |
source_page | Source page number inside the source block. |
dest_block | Destination block number. |
dest_page | Destination page number inside the destination block. |
References check_status(), data, DISABLE_CE, ENABLE_CE, 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(), wait_ready(), write_column_address(), WRITE_COMMAND, and write_row_address().
Referenced by nand_flash_raw_copy_page().
|
static |
Erase the specified block of the device.
raw | Pointer to a nand_flash_raw instance. |
block | Number of the physical block to erase. |
References check_status(), delay_ms, DISABLE_CE, ENABLE_CE, MODEL, NAND_COMMAND_ERASE_1, NAND_COMMAND_ERASE_2, NAND_COMMON_ERROR_CANNOTERASE, nand_flash_model_get_block_size_in_pages(), wait_ready(), WRITE_COMMAND, and write_row_address().
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 | ||
) |
Copies the data of one whole block of a NAND Flash device to another block.
raw | Pointer to a nand_flash_raw instance. |
source_block | Source block number. |
dest_block | Destination block number. |
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.
raw | Pointer to a nand_flash_raw instance. |
source_block | Source block number. |
source_page | Source page number inside the source block. |
dest_block | Destination block number. |
dest_page | Destination page number inside the destination block. |
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.
raw | Pointer to a nand_flash_raw instance. |
block | Number of the physical block to erase. |
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 | ||
) |
Initializes a nand_flash_raw instance based on the given model and physical interface.
raw | Pointer to a nand_flash_raw instance. |
model | Pointer to the underlying NAND chip model. Can be 0. |
command_address | Address at which commands are sent. |
address_address | Address at which addresses are sent. |
data_address | Address at which data is sent. |
References nand_flash_raw::address_address, ATPASTE2, BOARD_NAND_CS, nand_flash_raw::command_address, CONF_NF_BUSWIDTH, CONF_NF_CYCLE_TIMING, CONF_NF_PULSE_TIMING, CONF_NF_SETUP_TIMING, nand_flash_raw::data_address, data_address, delay_ms, gpio_configure_pin, IOPORT_DIR_INPUT, IOPORT_MODE_PULLUP, ioport_set_pin_dir(), ioport_set_pin_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(), NF_WP_PIN, PIN_NF_CE_IDX, PIN_NF_RB_IDX, pmc_enable_periph_clk(), smc_set_cycle_timing(), smc_set_mode(), smc_set_pulse_timing(), smc_set_setup_timing(), and sysclk_get_cpu_hz().
Referenced by main(), nand_flash_ecc_initialize(), and run_test_initialization().
uint32_t nand_flash_raw_read_id | ( | const struct nand_flash_raw * | raw | ) |
Read and return the identifiers of a NAND Flash chip.
raw | Pointer to a nand_flash_raw instance. |
References delay_us, DISABLE_CE, ENABLE_CE, NAND_COMMAND_READID, READ_DATA8, WRITE_ADDRESS, and WRITE_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.
raw | Pointer to a nand_flash_raw instance. |
block | Number of the physical block to read. |
page | Number of the page to read inside the given block. |
data | Buffer where the data area will be read. |
spare | Buffer where the spare area will be read. |
References delay_us, DISABLE_CE, ENABLE_CE, MODEL, NAND_COMMAND_READ_1, NAND_COMMAND_READ_2, NAND_COMMAND_READ_A, NAND_COMMAND_READ_C, 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(), read_data(), wait_ready(), write_column_address(), WRITE_COMMAND, and write_row_address().
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 | ) |
Reset a NAND Flash device.
raw | Pointer to a nand_flash_raw instance. |
References DISABLE_CE, ENABLE_CE, NAND_COMMAND_RESET, wait_ready(), and WRITE_COMMAND.
Referenced by nand_flash_raw_initialize().
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 into a NAND Flash.
raw | Pointer to a nand_flash_raw instance. |
block | Number of the physical block to write resides. |
page | Number of the page to write inside the given block. |
data | Buffer where the data area will be stored. |
spare | Buffer where the spare area will be stored. |
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 |
Read data from the NAND Flash chip into the provided buffer.
raw | Pointer to a nand_flash_raw instance. |
buffer | Pointer to data buffer. |
size | Number of bytes that will be written. |
References MODEL, nand_flash_model_get_data_bus_width(), READ_DATA16, and READ_DATA8.
Referenced by nand_flash_raw_read_page(), and write_page().
|
static |
Wait for the completion of a page program, erase and random read completion.
References gpio_pin_is_low, and PIN_NF_RB_IDX.
Referenced by copy_page(), erase_block(), nand_flash_raw_read_page(), nand_flash_raw_reset(), and write_page().
|
static |
Send the column address to the NAND Flash chip.
raw | Pointer to a nand_flash_raw instance. |
column_address | Column address to send. |
References MODEL, nand_flash_model_get_data_bus_width(), nand_flash_model_get_page_data_size(), WRITE_ADDRESS, and WRITE_ADDRESS16.
Referenced by copy_page(), nand_flash_raw_read_page(), and write_page().
|
static |
Send data to the NAND Flash chip from the provided buffer.
raw | Pointer to a nand_flash_raw instance. |
buffer | Pointer to data buffer. |
size | Number of bytes that will be written. |
References MODEL, nand_flash_model_get_data_bus_width(), WRITE_DATA16, and WRITE_DATA8.
Referenced by write_page().
|
static |
Write the data and/or the spare area of a page on a NAND Flash chip.
raw | Pointer to a nand_flash_raw instance. |
block | Number of the physical block to erase. |
page | Number of the page to write inside the given block. |
data | Buffer containing the data area. |
spare | Buffer containing the spare area. |
References check_status(), DISABLE_CE, ENABLE_CE, 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(), read_data(), wait_ready(), write_column_address(), WRITE_COMMAND, write_data(), and write_row_address().
Referenced by nand_flash_raw_write_page().
|
static |
Send the row address to the NAND Flash chip.
raw | Pointer to a nand_flash_raw instance. |
row_address | Row address to send. |
References MODEL, nand_flash_model_get_data_bus_width(), nand_flash_model_get_device_size_in_pages(), WRITE_ADDRESS, and WRITE_ADDRESS16.
Referenced by copy_page(), erase_block(), nand_flash_raw_read_page(), and write_page().