![]() |
SyterKit 0.4.0.x
SyterKit is a bare-metal framework
|
Allwinner Platform SPI Controller Driver. More...
#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-spi.h>
Functions | |
| static | __attribute__ ((section(".data"))) |
| DMA configuration structure for SPI RX (Receive) | |
| static void | sunxi_spi_enable_bus (sunxi_spi_t *spi) |
| Enable the SPI bus. | |
| static void | sunxi_spi_disable_bus (sunxi_spi_t *spi) |
| Disable the SPI bus. | |
| static void | sunxi_spi_set_cs (sunxi_spi_t *spi, uint8_t cs) |
| Set the chip select (CS) for the SPI transfer. | |
| static void | sunxi_spi_set_master (sunxi_spi_t *spi) |
| Set the SPI controller to master mode. | |
| static void | sunxi_spi_start_xfer (sunxi_spi_t *spi) |
| Start the SPI data transfer. | |
| static void | sunxi_spi_enable_transmit_pause (sunxi_spi_t *spi) |
| Enable the SPI transmit pause feature. | |
| static void | sunxi_spi_set_ss_owner (sunxi_spi_t *spi, uint32_t on_off) |
| Set the SPI chip select (SS) ownership. | |
| static uint32_t | sunxi_spi_query_txfifo (sunxi_spi_t *spi) |
| Query the number of bytes in the SPI transmit FIFO. | |
| static uint32_t | sunxi_spi_query_rxfifo (sunxi_spi_t *spi) |
| Query the number of bytes in the SPI receive FIFO. | |
| static void | sunxi_spi_disable_irq (sunxi_spi_t *spi, uint32_t bitmap) |
| Disable SPI interrupts. | |
| static void | sunxi_spi_clr_irq_pending (sunxi_spi_t *spi, uint32_t pending_bit) |
| Clear SPI interrupt pending flags. | |
| static uint32_t | sunxi_spi_query_irq_pending (sunxi_spi_t *spi) |
| Query the status of pending SPI interrupts. | |
| static void | sunxi_spi_set_ss_level (sunxi_spi_t *spi, uint32_t high_low) |
| Set the SPI chip select (SS) level. | |
| static void | sunxi_spi_dma_disable (sunxi_spi_t *spi) |
| Disable DMA for SPI data transmission and reception. | |
| static void | sunxi_spi_reset_fifo (sunxi_spi_t *spi) |
| Reset the SPI FIFO buffers. | |
| static uint32_t | sunxi_spi_read_rx_fifo (sunxi_spi_t *spi, uint8_t *buf, uint32_t len) |
| Read data from the SPI receive FIFO. | |
| static void | sunxi_spi_write_tx_fifo (sunxi_spi_t *spi, uint8_t *buf, uint32_t len) |
| Write data to the SPI transmit FIFO. | |
| static void | sunxi_spi_read_by_dma (sunxi_spi_t *spi, uint8_t *buf, uint32_t len) |
| Perform SPI data reception using DMA. | |
| static uint32_t | sunxi_spi_set_clk (sunxi_spi_t *spi, uint32_t spi_clk, uint32_t mclk, uint32_t cdr_mode) |
| Set the SPI clock frequency. | |
| static int | sunxi_spi_dma_init (sunxi_spi_t *spi) |
| Initialize the SPI DMA for data transfer. | |
| static int | sunxi_spi_dma_deinit (sunxi_spi_t *spi) |
| Deinitialize the SPI DMA. | |
| static int | sunxi_spi_get_clk (sunxi_spi_t *spi) |
| Get the current SPI clock frequency. | |
| void | __attribute__ ((weak)) |
| Initializes the SPI clock. | |
| static void | sunxi_spi_clk_deinit (sunxi_spi_t *spi) |
| Deinitializes the SPI clock. | |
| static void | sunxi_spi_config_transer_control (sunxi_spi_t *spi) |
| Configures the SPI transfer control settings. | |
| static void | sunxi_spi_set_io_mode (sunxi_spi_t *spi, spi_io_mode_t mode) |
| Sets the SPI I/O mode. | |
| static void | sunxi_spi_set_counters (sunxi_spi_t *spi, int txlen, int rxlen, int stxlen, int dummylen) |
| Sets the counters for SPI transmission. | |
| static void | sunxi_spi_bus_init (sunxi_spi_t *spi) |
| Initializes the SPI bus. | |
| static void | sunxi_spi_gpio_init (sunxi_spi_t *spi) |
| Configures the GPIO pins for SPI communication. | |
| int | sunxi_spi_init (sunxi_spi_t *spi) |
| Initializes the SPI interface. | |
| void | sunxi_spi_disable (sunxi_spi_t *spi) |
| Disables the SPI interface. | |
| int | sunxi_spi_update_clk (sunxi_spi_t *spi, uint32_t clk) |
| Updates the SPI clock rate. | |
| int | sunxi_spi_transfer (sunxi_spi_t *spi, spi_io_mode_t mode, void *txbuf, uint32_t txlen, void *rxbuf, uint32_t rxlen) |
| Performs SPI data transfer. | |
Allwinner Platform SPI Controller Driver.
This file implements the SPI (Serial Peripheral Interface) controller driver for Allwinner platforms. The SPI driver provides functionality for initializing and controlling the SPI interface, including clock configuration, data transfer, GPIO setup, and DMA operations. It supports various SPI modes, data widths, and transfer methods (polling and DMA).
The driver supports different I/O modes including single, dual, and quad SPI, making it suitable for interfacing with various SPI devices such as flash memory, sensors, and other peripheral devices.
SPDX-License-Identifier: GPL-2.0-or-later
Copyright (c) 2023, Allwinner Technology Co., Ltd.
|
static |
DMA configuration structure for SPI RX (Receive)
This structure is used for configuring the Direct Memory Access (DMA) controller for SPI data reception. It is placed in the section ".data" of the memory.
Perform a software reset on the SPI controller
This function triggers a software reset on the SPI controller by setting the appropriate bit in the control register.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| void __attribute__ | ( | (weak) | ) |
Initializes the SPI clock.
This function configures the SPI clock by setting the appropriate divisor and clock source, enabling the SPI clock, performing a reset on the SPI, and setting up the clock gating.
| spi | Pointer to the SPI structure containing configuration and register information. |
|
static |
Initializes the SPI bus.
This function performs the necessary initialization steps for the SPI bus, including performing a soft reset, enabling the bus, configuring the chip select (CS), setting the SPI mode to master, configuring the clock, transfer control, and setting other SPI bus settings like slave select level and transmit pause.
| spi | Pointer to the SPI structure containing configuration and register information. |
|
static |
Deinitializes the SPI clock.
This function disables the SPI clock, clears the clock gating, and asserts the SPI reset.
| spi | Pointer to the SPI structure containing configuration and register information. |
|
inlinestatic |
Clear SPI interrupt pending flags.
This function clears the interrupt pending flags for the SPI controller by writing the relevant bits to the interrupt status register.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [in] | pending_bit | A mask representing the pending interrupt to be cleared. |
|
static |
Configures the SPI transfer control settings.
This function configures the transfer control register (tc) based on the SPI clock frequency. The transfer control settings such as data width, polarity, and phase are set accordingly.
| spi | Pointer to the SPI structure containing configuration and register information. |
| void sunxi_spi_disable | ( | sunxi_spi_t * | spi | ) |
Disables the SPI interface.
This function disables the SPI bus, deinitializes DMA (if used), and deinitializes the SPI clock. It is called when the SPI interface is no longer needed or when performing cleanup operations.
| spi | Pointer to the SPI structure containing configuration and register information. |
|
inlinestatic |
Disable the SPI bus.
This function disables the SPI bus by clearing the enable bit in the control register.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
inlinestatic |
Disable SPI interrupts.
This function disables specific interrupts for the SPI controller based on the provided interrupt bitmap. The interrupt mask is applied to the interrupt control register.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [in] | bitmap | A mask representing the interrupts to be disabled. Each bit corresponds to a specific interrupt. |
|
static |
Deinitialize the SPI DMA.
This function disables the DMA interrupt for the SPI DMA channel, effectively deinitializing the DMA setup and preparing for cleanup.
| [in] | spi | A pointer to the SPI structure. |
|
inlinestatic |
Disable DMA for SPI data transmission and reception.
This function disables the Direct Memory Access (DMA) for both transmitting and receiving data through the SPI interface. Disabling DMA may be useful when switching to interrupt or polling modes of data transfer.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
static |
Initialize the SPI DMA for data transfer.
This function initializes the DMA controller for SPI data reception. It requests a DMA channel, configures the DMA settings for the SPI receive path, and installs the DMA interrupt handler. The DMA is then enabled and the necessary settings are applied to start data transfer via DMA.
| [in] | spi | A pointer to the SPI structure, containing the necessary information about the SPI controller and DMA settings. |
printk_error.
|
inlinestatic |
Enable the SPI bus.
This function enables the SPI bus by setting the enable bit in the control register.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
inlinestatic |
Enable the SPI transmit pause feature.
This function enables the transmit pause feature for the SPI controller. When enabled, it allows the SPI transmission to be paused, providing control over when to send data.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
static |
Get the current SPI clock frequency.
This function calculates and returns the current SPI clock frequency based on the SPI clock configuration registers. It reads the configuration, extracts the relevant clock parameters, and computes the actual SPI clock frequency in Hz.
| [in] | spi | A pointer to the SPI structure. |
|
static |
Configures the GPIO pins for SPI communication.
This function configures the SPI-related GPIO pins, including chip select (CS), clock (SCK), master-out slave-in (MOSI), master-in slave-out (MISO), write protect (WP), and hold pins. It sets the appropriate multiplexing functions and default pull-up resistors for the WP and hold pins.
| spi | Pointer to the SPI structure containing the GPIO configuration. |
| int sunxi_spi_init | ( | sunxi_spi_t * | spi | ) |
Initializes the SPI interface.
This function initializes the SPI interface by configuring the GPIO pins, clock, bus, and counters. If a DMA handle is set, DMA mode is used for data transfers. The function calls several other SPI initialization functions to set up the SPI hardware.
| spi | Pointer to the SPI structure containing configuration and register information. |
|
inlinestatic |
Query the status of pending SPI interrupts.
This function queries the interrupt status register to determine which SPI interrupts are pending. The function returns a mask of the pending interrupts.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
inlinestatic |
Query the number of bytes in the SPI receive FIFO.
This function queries the SPI receive FIFO to determine how many bytes are currently available to be read.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
inlinestatic |
Query the number of bytes in the SPI transmit FIFO.
This function queries the SPI transmit FIFO to determine how many bytes are currently waiting to be transmitted.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
static |
Perform SPI data reception using DMA.
This function initiates a DMA transfer to read data from the SPI receive FIFO into a provided buffer. It enables the DMA receive request and starts the DMA transfer. The function waits until the DMA transfer is complete before returning.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [out] | buf | A pointer to the buffer where received data will be stored. |
| [in] | len | The number of bytes to read from the SPI receive FIFO. |
spi_dma_handler must be initialized properly before calling this function.printk_warning.
|
static |
Read data from the SPI receive FIFO.
This function reads data from the SPI receive FIFO into a buffer. It waits until there is sufficient data available in the FIFO and transfers it into the provided buffer.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [out] | buf | A pointer to the buffer where the received data will be stored. |
| [in] | len | The number of bytes to read from the receive FIFO. |
|
static |
Reset the SPI FIFO buffers.
This function resets the SPI transmit and receive FIFO buffers. The FIFO reset is done by writing specific bits to the FIFO control register. It also configures the trigger levels for both receive and transmit FIFOs.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
static |
Set the SPI clock frequency.
This function calculates and sets the SPI clock frequency based on the desired SPI clock (spi_clk), the system's master clock (mclk), and the clock divider mode (cdr2). It configures the clock control register to achieve the requested clock frequency for the SPI interface.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [in] | spi_clk | The desired SPI clock frequency in Hz. |
| [in] | mclk | The master clock frequency in Hz. |
| [in] | cdr2 | If set to 1, the clock divider mode will be set to CDR2 (double the frequency). |
printk_debug.
|
static |
Sets the counters for SPI transmission.
This function sets the burst, transmit, and dummy counters for SPI data transmission. The counters are configured based on the transmission length, reception length, start transmission length, and dummy data length.
| spi | Pointer to the SPI structure containing configuration and register information. |
| txlen | The length of the transmission data in bytes. |
| rxlen | The length of the reception data in bytes. |
| stxlen | The length of the start transmission data in bytes. |
| dummylen | The length of the dummy data in bytes. |
|
inlinestatic |
Set the chip select (CS) for the SPI transfer.
This function sets the chip select (CS) line by writing the value of the cs parameter to the relevant bits in the SPI control register. The CS line is used to select which peripheral is active for communication.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [in] | cs | The chip select value to be set. This is typically a 0 or 1, corresponding to a specific peripheral on the SPI bus. |
|
static |
Sets the SPI I/O mode.
This function configures the I/O mode for SPI data transfer, including single, dual, or quad mode.
| spi | Pointer to the SPI structure containing configuration and register information. |
| mode | The desired SPI I/O mode (e.g., SPI_IO_SINGLE, SPI_IO_DUAL_RX, SPI_IO_QUAD_RX, etc.). |
|
inlinestatic |
Set the SPI controller to master mode.
This function sets the SPI controller to master mode by modifying the relevant control bit. In master mode, the SPI controller generates clock signals for communication with slaves.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
|
inlinestatic |
Set the SPI chip select (SS) level.
This function controls the logic level of the chip select (SS) line. It sets the SS line to either high or low level, depending on the high_low parameter.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [in] | high_low | A value indicating the level of the SS line:
|
|
inlinestatic |
Set the SPI chip select (SS) ownership.
This function controls the ownership of the chip select (SS) line. When the SS ownership is set to "on", the SPI controller has control over the SS line; when set to "off", the external controller or logic can take control.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [in] | on_off | A value indicating whether to enable or disable SS ownership:
|
|
inlinestatic |
Start the SPI data transfer.
This function triggers the start of a data transfer by setting the relevant bit in the transfer control register. It begins the exchange of data on the SPI bus.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| int sunxi_spi_transfer | ( | sunxi_spi_t * | spi, |
| spi_io_mode_t | mode, | ||
| void * | txbuf, | ||
| uint32_t | txlen, | ||
| void * | rxbuf, | ||
| uint32_t | rxlen | ||
| ) |
Performs SPI data transfer.
This function initiates a data transfer on the SPI bus. The transfer can be either full-duplex (both transmission and reception) or half-duplex (only transmission or reception). The transfer is done based on the specified SPI I/O mode. The function handles both transmit and receive operations, including the use of DMA if required for large transfers.
| spi | Pointer to the SPI structure containing configuration and register information. |
| mode | The I/O mode to use for the transfer (e.g., single, dual, quad). |
| txbuf | Pointer to the transmission buffer. |
| txlen | Length of the transmission data in bytes. |
| rxbuf | Pointer to the reception buffer. |
| rxlen | Length of the reception data in bytes. |
| int sunxi_spi_update_clk | ( | sunxi_spi_t * | spi, |
| uint32_t | clk | ||
| ) |
Updates the SPI clock rate.
This function updates the SPI clock rate and reinitializes the clock, bus, and transfer control settings to apply the new clock rate.
| spi | Pointer to the SPI structure containing configuration and register information. |
| clk | The new clock rate to be set. |
|
static |
Write data to the SPI transmit FIFO.
This function writes data from a buffer to the SPI transmit FIFO. It waits until there is space available in the FIFO before writing more data.
| [in] | spi | A pointer to the SPI structure, which contains the base address of the SPI controller's registers. |
| [in] | buf | A pointer to the buffer containing the data to be transmitted. |
| [in] | len | The number of bytes to write to the transmit FIFO. |