SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
Functions | Variables
sys-spi-nor.c File Reference
#include <io.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <types.h>
#include <timer.h>
#include <log.h>
#include <sys-clk.h>
#include <sys-dma.h>
#include <sys-gpio.h>
#include <sys-spi-nor.h>
#include <sys-spi.h>
Include dependency graph for sys-spi-nor.c:

Functions

 __attribute__ ((unused))
 Dump the contents of the SFDP (Serial Flash Discoverable Parameters) data structure.
 
static int spi_nor_read_sfdp (sunxi_spi_t *spi, sfdp_t *sfdp)
 Read the SFDP (Serial Flash Discoverable Parameters) data from a SPI NOR Flash chip.
 
static int spinor_read_id (sunxi_spi_t *spi, uint32_t *id)
 Read the identification (ID) of the SPI NOR Flash chip.
 
static uint8_t spi_nor_read_status_register (sunxi_spi_t *spi)
 Read the status register of the SPI NOR Flash chip.
 
static void spi_nor_write_status_register (sunxi_spi_t *spi, uint8_t sr)
 Write to the status register of the SPI NOR Flash chip.
 
static void spi_nor_wait_for_busy (sunxi_spi_t *spi)
 Wait for SPI NOR Flash to finish operation by checking its "busy" status.
 
static void spi_nor_chip_reset (sunxi_spi_t *spi)
 Reset the SPI NOR Flash chip.
 
static void spi_nor_set_write_enable (sunxi_spi_t *spi)
 Enable write operations on the SPI NOR Flash chip.
 
static int spi_nor_get_info (sunxi_spi_t *spi)
 Retrieves the information of the SPI NOR flash.
 
static void spi_nor_read_bytes (sunxi_spi_t *spi, uint32_t addr, uint8_t *buf, uint32_t count)
 Reads a specified number of bytes from the SPI NOR flash memory.
 
int spi_nor_detect (sunxi_spi_t *spi)
 Detects the presence of an SPI NOR flash chip.
 
uint32_t spi_nor_read_block (sunxi_spi_t *spi, uint8_t *buf, uint32_t blk_no, uint32_t blk_cnt)
 Reads a block or multiple blocks of data from the SPI NAND flash memory.
 
uint32_t spi_nor_read (sunxi_spi_t *spi, uint8_t *buf, uint32_t addr, uint32_t rxlen)
 Reads data from the SPI NOR flash memory.
 

Variables

static spi_nor_info_t info
 
static const spi_nor_info_t spi_nor_info_table []
 

Function Documentation

◆ __attribute__()

__attribute__ ( (unused )

Dump the contents of the SFDP (Serial Flash Discoverable Parameters) data structure.

This function prints the SFDP header, parameter headers, and the basic table information to the log. It will provide detailed output about the SFDP format, including version information, the number of parameter headers, and the contents of the basic table in the SFDP structure.

Parameters
sfdpPointer to an sfdp_t structure containing the SFDP data to be dumped.
Note
This function is marked as __attribute__((unused)) to avoid unused function warnings if not used.
Warning
If the provided sfdp pointer is NULL, the function will print a trace log indicating the issue.

◆ spi_nor_chip_reset()

static void spi_nor_chip_reset ( sunxi_spi_t spi)
inlinestatic

Reset the SPI NOR Flash chip.

This function sends a specific command sequence (0x66 and 0x99) to reset the NOR Flash chip. These two bytes are typically used to return the NOR Flash to its initial state. It is often used during chip initialization or recovery processes.

Parameters
spiPointer to a sunxi_spi_t structure representing the SPI device.

◆ spi_nor_detect()

int spi_nor_detect ( sunxi_spi_t spi)

Detects the presence of an SPI NOR flash chip.

This function attempts to identify and initialize the SPI NOR flash chip connected to the specified SPI interface. It resets the chip, waits for it to be ready, and retrieves its information. If successful, it logs the chip ID and its capacity.

Parameters
[in]spiPointer to the SPI interface structure. This should be initialized and configured before calling this function.
Returns
  • 0 on successful detection and initialization of the SPI NOR chip.
  • -1 if no supported SPI NOR chip is found.

    The function performs the following steps:

    1. Resets the SPI NOR chip.
    2. Waits until the chip is not busy.
    3. Checks the chip information. If no supported chip is found, a warning is logged and the function returns -1.
    4. If a chip is detected, its ID and capacity are logged to inform the user.

◆ spi_nor_get_info()

static int spi_nor_get_info ( sunxi_spi_t spi)
inlinestatic

Retrieves the information of the SPI NOR flash.

This function reads the identification (ID) of the SPI NOR flash and attempts to fetch its detailed parameters using the Serial Flash Discoverable Parameters (SFDP)

table. Based on the flash type and capacity, it configures the address length, erase opcodes, write granularity, and other flash-specific parameters. If the SFDP table is unavailable, the function will fall back on a predefined lookup table using the flash's ID to find a match. It then populates the info structure with the gathered information.

Parameters
spiThe SPI interface structure representing the SPI controller to interact with the NOR flash.
Returns
  • 1 if the flash information was successfully retrieved.
  • 0 if the flash ID is not recognized or the SPI NOR flash could not be detected.
Note
If the SPI NOR flash is recognized using its ID, this function will populate the info structure with its capabilities such as capacity, address length, erase block size, and supported opcodes. If the SFDP is valid, it also provides more granular details such as the opcode for 4K, 32K, 64K, and 256K erases, as well as write granularity.
See also
spinor_read_id(), spi_nor_read_sfdp(), spi_nor_dump_sfdp(), NOR_OPCODE_WREN, NOR_OPCODE_READ, NOR_OPCODE_PROG

◆ spi_nor_read()

uint32_t spi_nor_read ( sunxi_spi_t spi,
uint8_t buf,
uint32_t  addr,
uint32_t  rxlen 
)

Reads data from the SPI NOR flash memory.

This function reads a specified length of data from a given address in the SPI NOR flash memory into a provided buffer. The reading is performed in blocks, and it handles cases where the read address is not aligned to the block size.

Parameters
[in]spiPointer to the SPI interface structure.
[out]bufPointer to the buffer where the read data will be stored.
[in]addrThe starting address from which to read data in the SPI NOR.
[in]rxlenThe number of bytes to read from the SPI NOR.
Returns
The number of bytes successfully read from the SPI NOR. This can be less than rxlen if an error occurs or if the end of the memory space is reached.
Note
This function assumes that the buffer provided by the caller is large enough to accommodate the data being read.

The function first checks if the read address is misaligned with the block size. If so, it reads a partial block. Then, it reads as many complete blocks as possible before potentially reading another partial block at the end.

◆ spi_nor_read_block()

uint32_t spi_nor_read_block ( sunxi_spi_t spi,
uint8_t buf,
uint32_t  blk_no,
uint32_t  blk_cnt 
)

Reads a block or multiple blocks of data from the SPI NAND flash memory.

This function reads one or more contiguous blocks from the SPI NAND flash memory into a provided buffer. It handles reading the data in chunks defined by the read granularity and performs the necessary address calculations to handle multiple blocks.

Parameters
[in]spiPointer to the SPI interface structure.
[out]bufPointer to the buffer where the read data will be stored.
[in]blk_noThe starting block number from which to read data.
[in]blk_cntThe number of blocks to read from the SPI NAND.
Returns
The number of blocks successfully read. In case of an error, it will return the requested block count, indicating the operation was completed.
Note
This function assumes that the buffer provided by the caller is large enough to accommodate the data being read. If the block count is large, ensure the buffer has sufficient space for all the blocks.

The function reads data from the SPI NAND flash memory in chunks based on the configured read granularity (info.read_granularity). It waits for the SPI bus to be ready before each read operation. The function will continue reading until the requested number of blocks has been fetched. If the data to be read exceeds the maximum read length (0x7FFFFFFF bytes), it adjusts the size of each read operation accordingly.

◆ spi_nor_read_bytes()

static void spi_nor_read_bytes ( sunxi_spi_t spi,
uint32_t  addr,
uint8_t buf,
uint32_t  count 
)
static

Reads a specified number of bytes from the SPI NOR flash memory.

This function sends the read command and address to the SPI NOR flash memory and retrieves the requested data. It supports different address lengths (3 or 4 bytes) based on the configuration in the info structure.

Parameters
[in]spiPointer to the SPI interface structure.
[in]addrThe starting address to read data from in the SPI NOR.
[out]bufPointer to the buffer where the read data will be stored.
[in]countThe number of bytes to read from the SPI NOR.
Returns
None
Note
This function uses the sunxi_spi_transfer function to perform the SPI data transfer. The number of bytes transferred depends on the address length configuration, which is either 3 or 4 bytes.

This function first checks the address_length configuration (from info) to determine if the address is 3 bytes or 4 bytes long. Based on this configuration, it sends the appropriate number of address bytes and the read opcode to the SPI NOR. The data is then transferred to the provided buffer. The function supports 3-byte or 4-byte address modes, but any other address length is not supported.

◆ spi_nor_read_sfdp()

static int spi_nor_read_sfdp ( sunxi_spi_t spi,
sfdp_t sfdp 
)
inlinestatic

Read the SFDP (Serial Flash Discoverable Parameters) data from a SPI NOR Flash chip.

This function sends the appropriate commands to the SPI NOR Flash to retrieve the SFDP information. The SFDP is a standardized way for flash devices to expose their capabilities and characteristics. It reads the SFDP header and parameter headers, then fetches the basic table if the parameter header conditions are met.

Parameters
spiPointer to a sunxi_spi_t structure representing the SPI device.
sfdpPointer to a sfdp_t structure where the SFDP data will be stored.
Returns
1 if the SFDP data was successfully read, 0 if there was an error or the data was invalid.

◆ spi_nor_read_status_register()

static uint8_t spi_nor_read_status_register ( sunxi_spi_t spi)
inlinestatic

Read the status register of the SPI NOR Flash chip.

This function sends the "Read Status Register" command (RDSR) to the SPI NOR Flash chip and reads back a single byte representing the current status register value.

Parameters
spiPointer to a sunxi_spi_t structure representing the SPI device.
Returns
The 1-byte status register value returned by the NOR Flash chip.

◆ spi_nor_set_write_enable()

static void spi_nor_set_write_enable ( sunxi_spi_t spi)
inlinestatic

Enable write operations on the SPI NOR Flash chip.

This function sends the "Write Enable" command (usually 0x06) to enable write operations on the NOR Flash chip. The Write Enable command must be issued before any write operation can take place.

Parameters
spiPointer to a sunxi_spi_t structure representing the SPI device.

◆ spi_nor_wait_for_busy()

static void spi_nor_wait_for_busy ( sunxi_spi_t spi)
inlinestatic

Wait for SPI NOR Flash to finish operation by checking its "busy" status.

This function continuously checks the NOR Flash status register until the "busy" bit is cleared (i.e., operation is complete). It reads the status register to determine whether the NOR Flash is still in a busy state.

Parameters
spiPointer to a sunxi_spi_t structure representing the SPI device.

◆ spi_nor_write_status_register()

static void spi_nor_write_status_register ( sunxi_spi_t spi,
uint8_t  sr 
)
inlinestatic

Write to the status register of the SPI NOR Flash chip.

This function sends the "Write Status Register" command (WRSR) to the SPI NOR Flash chip, followed by a byte containing the new status register value to be written.

Parameters
spiPointer to a sunxi_spi_t structure representing the SPI device.
srThe new status register value to write to the NOR Flash chip.

◆ spinor_read_id()

static int spinor_read_id ( sunxi_spi_t spi,
uint32_t id 
)
inlinestatic

Read the identification (ID) of the SPI NOR Flash chip.

This function sends the "Read ID" command (RDID) to the SPI NOR Flash chip and reads back its 3-byte identification. The identification is stored in the provided id variable as a 24-bit value, combining the 3 received bytes.

Parameters
spiPointer to a sunxi_spi_t structure representing the SPI device.
idPointer to a uint32_t variable where the 24-bit chip ID will be stored.
Returns
1 if the ID was successfully read, 0 if the transfer failed.

Variable Documentation

◆ info

spi_nor_info_t info
static

◆ spi_nor_info_table

const spi_nor_info_t spi_nor_info_table[]
static
Initial value:
= {
{"W25X40", 0xef3013, 512 * 1024, 4096, 1, 256, 3, NOR_OPCODE_READ, NOR_OPCODE_PROG, NOR_OPCODE_WREN, NOR_OPCODE_E4K, 0, NOR_OPCODE_E64K, 0},
{"W25Q128JVEIQ", 0xefc018, 16 * 1024 * 1024, 4096, 1, 256, 3, NOR_OPCODE_READ, NOR_OPCODE_PROG, NOR_OPCODE_WREN, NOR_OPCODE_E4K, NOR_OPCODE_E32K, NOR_OPCODE_E64K, 0},
{"GD25D10B", 0xc84011, 128 * 1024, 4096, 1, 256, 3, NOR_OPCODE_READ, NOR_OPCODE_PROG, NOR_OPCODE_WREN, NOR_OPCODE_E4K, NOR_OPCODE_E32K, NOR_OPCODE_E64K, 0},
}
@ NOR_OPCODE_WREN
Write Enable Command: Enable write operations on the memory.
Definition sys-spi-nor.h:121
@ NOR_OPCODE_E32K
32K Block Erase Command: Erase a 32K block of memory
Definition sys-spi-nor.h:125
@ NOR_OPCODE_READ
Read Data Command: Read data from the memory.
Definition sys-spi-nor.h:122
@ NOR_OPCODE_E4K
4K Block Erase Command: Erase a 4K block of memory
Definition sys-spi-nor.h:124
@ NOR_OPCODE_E64K
64K Block Erase Command: Erase a 64K block of memory
Definition sys-spi-nor.h:126
@ NOR_OPCODE_PROG
Page Program Command: Program data into a memory page.
Definition sys-spi-nor.h:123