/** * struct stm32_spi_reg - stm32 SPI register & bitfield desc * @reg: register offset * @mask: bitfield mask * @shift: left shift
*/ struct stm32_spi_reg { int reg; int mask; int shift;
};
/** * struct stm32_spi_regspec - stm32 registers definition, compatible dependent data * @en: enable register and SPI enable bit * @dma_rx_en: SPI DMA RX enable register end SPI DMA RX enable bit * @dma_tx_en: SPI DMA TX enable register end SPI DMA TX enable bit * @cpol: clock polarity register and polarity bit * @cpha: clock phase register and phase bit * @lsb_first: LSB transmitted first register and bit * @cs_high: chips select active value * @br: baud rate register and bitfields * @rx: SPI RX data register * @tx: SPI TX data register * @fullcfg: SPI full or limited feature set register * @rdy_en: SPI ready feature register
*/ struct stm32_spi_regspec { conststruct stm32_spi_reg en; conststruct stm32_spi_reg dma_rx_en; conststruct stm32_spi_reg dma_tx_en; conststruct stm32_spi_reg cpol; conststruct stm32_spi_reg cpha; conststruct stm32_spi_reg lsb_first; conststruct stm32_spi_reg cs_high; conststruct stm32_spi_reg br; conststruct stm32_spi_reg rx; conststruct stm32_spi_reg tx; conststruct stm32_spi_reg fullcfg; conststruct stm32_spi_reg rdy_en;
};
struct stm32_spi;
/** * struct stm32_spi_cfg - stm32 compatible configuration data * @regs: registers descriptions * @get_fifo_size: routine to get fifo size * @get_bpw_mask: routine to get bits per word mask * @disable: routine to disable controller * @config: routine to configure controller as SPI Host * @set_bpw: routine to configure registers to for bits per word * @set_mode: routine to configure registers to desired mode * @set_data_idleness: optional routine to configure registers to desired idle * time between frames (if driver has this functionality) * @set_number_of_data: optional routine to configure registers to desired * number of data (if driver has this functionality) * @write_tx: routine to write to transmit register/FIFO * @read_rx: routine to read from receive register/FIFO * @transfer_one_dma_start: routine to start transfer a single spi_transfer * using DMA * @dma_rx_cb: routine to call after DMA RX channel operation is complete * @dma_tx_cb: routine to call after DMA TX channel operation is complete * @transfer_one_irq: routine to configure interrupts for driver * @irq_handler_event: Interrupt handler for SPI controller events * @irq_handler_thread: thread of interrupt handler for SPI controller * @baud_rate_div_min: minimum baud rate divisor * @baud_rate_div_max: maximum baud rate divisor * @has_fifo: boolean to know if fifo is used for driver * @has_device_mode: is this compatible capable to switch on device mode * @flags: compatible specific SPI controller flags used at registration time * @prevent_dma_burst: boolean to indicate to prevent DMA burst
*/ struct stm32_spi_cfg { conststruct stm32_spi_regspec *regs; int (*get_fifo_size)(struct stm32_spi *spi); int (*get_bpw_mask)(struct stm32_spi *spi); void (*disable)(struct stm32_spi *spi); int (*config)(struct stm32_spi *spi); void (*set_bpw)(struct stm32_spi *spi); int (*set_mode)(struct stm32_spi *spi, unsignedint comm_type); void (*set_data_idleness)(struct stm32_spi *spi, struct spi_transfer *xfer); int (*set_number_of_data)(struct stm32_spi *spi, u32 length); void (*write_tx)(struct stm32_spi *spi); void (*read_rx)(struct stm32_spi *spi); void (*transfer_one_dma_start)(struct stm32_spi *spi); void (*dma_rx_cb)(void *data); void (*dma_tx_cb)(void *data); int (*transfer_one_irq)(struct stm32_spi *spi);
irqreturn_t (*irq_handler_event)(int irq, void *dev_id);
irqreturn_t (*irq_handler_thread)(int irq, void *dev_id); unsignedint baud_rate_div_min; unsignedint baud_rate_div_max; bool has_fifo; bool has_device_mode;
u16 flags; bool prevent_dma_burst;
};
/** * struct stm32_spi - private data of the SPI controller * @dev: driver model representation of the controller * @ctrl: controller interface * @cfg: compatible configuration data * @base: virtual memory area * @clk: hw kernel clock feeding the SPI clock generator * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator * @lock: prevent I/O concurrent access * @irq: SPI controller interrupt line * @fifo_size: size of the embedded fifo in bytes * @t_size_max: maximum number of data of one transfer * @feature_set: SPI full or limited feature set * @cur_midi: host inter-data idleness in ns * @cur_speed: speed configured in Hz * @cur_half_period: time of a half bit in us * @cur_bpw: number of bits in a single SPI data frame * @cur_fthlv: fifo threshold level (data frames in a single data packet) * @cur_comm: SPI communication mode * @cur_xferlen: current transfer length in bytes * @cur_usedma: boolean to know if dma is used in current transfer * @tx_buf: data to be written, or NULL * @rx_buf: data to be read, or NULL * @tx_len: number of data to be written in bytes * @rx_len: number of data to be read in bytes * @dma_tx: dma channel for TX transfer * @dma_rx: dma channel for RX transfer * @phys_addr: SPI registers physical base address * @device_mode: the controller is configured as SPI device * @sram_pool: SRAM pool for DMA transfers * @sram_rx_buf_size: size of SRAM buffer for RX transfer * @sram_rx_buf: SRAM buffer for RX transfer * @sram_dma_rx_buf: SRAM buffer physical address for RX transfer * @mdma_rx: MDMA channel for RX transfer
*/ struct stm32_spi { struct device *dev; struct spi_controller *ctrl; conststruct stm32_spi_cfg *cfg; void __iomem *base; struct clk *clk;
u32 clk_rate;
spinlock_t lock; /* prevent I/O concurrent access */ int irq; unsignedint fifo_size; unsignedint t_size_max; unsignedint feature_set; #define STM32_SPI_FEATURE_LIMITED STM32MP25_SPI_HWCFGR1_FULLCFG_LIMITED /* 0x0 */ #define STM32_SPI_FEATURE_FULL STM32MP25_SPI_HWCFGR1_FULLCFG_FULL /* 0x1 */
staticconststruct stm32_spi_regspec stm32h7_spi_regspec = { /* SPI data transfer is enabled but spi_ker_ck is idle. * CFG1 and CFG2 registers are write protected when SPE is enabled.
*/
.en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
staticconststruct stm32_spi_regspec stm32mp25_spi_regspec = { /* SPI data transfer is enabled but spi_ker_ck is idle. * CFG1 and CFG2 registers are write protected when SPE is enabled.
*/
.en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
return count;
}
/** * stm32f4_spi_get_bpw_mask - Return bits per word mask * @spi: pointer to the spi controller data structure
*/ staticint stm32f4_spi_get_bpw_mask(struct stm32_spi *spi)
{
dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n"); return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
}
/** * stm32f7_spi_get_bpw_mask - Return bits per word mask * @spi: pointer to the spi controller data structure
*/ staticint stm32f7_spi_get_bpw_mask(struct stm32_spi *spi)
{
dev_dbg(spi->dev, "16-bit maximum data frame\n"); return SPI_BPW_RANGE_MASK(4, 16);
}
/** * stm32h7_spi_get_bpw_mask - Return bits per word mask * @spi: pointer to the spi controller data structure
*/ staticint stm32h7_spi_get_bpw_mask(struct stm32_spi *spi)
{ unsignedlong flags;
u32 cfg1, max_bpw;
spin_lock_irqsave(&spi->lock, flags);
/* * The most significant bit at DSIZE bit field is reserved when the * maximum data size of periperal instances is limited to 16-bit
*/
stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE);
dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
return SPI_BPW_RANGE_MASK(4, max_bpw);
}
/** * stm32mp25_spi_get_bpw_mask - Return bits per word mask * @spi: pointer to the spi controller data structure
*/ staticint stm32mp25_spi_get_bpw_mask(struct stm32_spi *spi)
{
u32 dscfg, max_bpw;
if (spi->feature_set == STM32_SPI_FEATURE_LIMITED) {
dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n"); return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
}
dscfg = FIELD_GET(STM32MP25_SPI_HWCFGR1_DSCFG,
readl_relaxed(spi->base + STM32MP25_SPI_HWCFGR1));
max_bpw = 16; if (dscfg == STM32MP25_SPI_HWCFGR1_DSCFG_32_B)
max_bpw = 32;
dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw); return SPI_BPW_RANGE_MASK(4, max_bpw);
}
/** * stm32_spi_prepare_mbr - Determine baud rate divisor value * @spi: pointer to the spi controller data structure * @speed_hz: requested speed * @min_div: minimum baud rate divisor * @max_div: maximum baud rate divisor * * Return baud rate divisor value in case of success or -EINVAL
*/ staticint stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
u32 min_div, u32 max_div)
{
u32 div, mbrdiv;
/* Ensure spi->clk_rate is even */
div = DIV_ROUND_CLOSEST(spi->clk_rate & ~0x1, speed_hz);
/* * SPI framework set xfer->speed_hz to ctrl->max_speed_hz if * xfer->speed_hz is greater than ctrl->max_speed_hz, and it returns * an error when xfer->speed_hz is lower than ctrl->min_speed_hz, so * no need to check it there. * However, we need to ensure the following calculations.
*/ if ((div < min_div) || (div > max_div)) return -EINVAL;
/* Determine the first power of 2 greater than or equal to div */ if (div & (div - 1))
mbrdiv = fls(div); else
mbrdiv = fls(div) - 1;
if (spi->cur_usedma && spi->dma_tx)
dmaengine_terminate_async(spi->dma_tx); if (spi->cur_usedma && spi->dma_rx)
dmaengine_terminate_async(spi->dma_rx);
/* Sequence to clear OVR flag */
readl_relaxed(spi->base + STM32FX_SPI_DR);
readl_relaxed(spi->base + STM32FX_SPI_SR);
spin_unlock_irqrestore(&spi->lock, flags);
}
/** * stm32h7_spi_disable - Disable SPI controller * @spi: pointer to the spi controller data structure * * RX-Fifo is flushed when SPI controller is disabled.
*/ staticvoid stm32h7_spi_disable(struct stm32_spi *spi)
{ unsignedlong flags;
u32 cr1;
dev_dbg(spi->dev, "disable controller\n");
spin_lock_irqsave(&spi->lock, flags);
cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1);
if (!(cr1 & STM32H7_SPI_CR1_SPE)) {
spin_unlock_irqrestore(&spi->lock, flags); return;
}
/* Add a delay to make sure that transmission is ended. */ if (spi->cur_half_period)
udelay(spi->cur_half_period);
if (spi->cur_usedma && spi->dma_tx)
dmaengine_terminate_async(spi->dma_tx); if (spi->cur_usedma && spi->dma_rx) {
dmaengine_terminate_async(spi->dma_rx); if (spi->mdma_rx)
dmaengine_terminate_async(spi->mdma_rx);
}
/* Disable interrupts and clear status flags */
writel_relaxed(0, spi->base + STM32H7_SPI_IER);
writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR);
spin_unlock_irqrestore(&spi->lock, flags);
}
/** * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use * @ctrl: controller interface * @spi_dev: pointer to the spi device * @transfer: pointer to spi transfer * * If driver has fifo and the current transfer size is greater than fifo size, * use DMA. Otherwise use DMA for transfer longer than defined DMA min bytes.
*/ staticbool stm32_spi_can_dma(struct spi_controller *ctrl, struct spi_device *spi_dev, struct spi_transfer *transfer)
{ unsignedint dma_size; struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
if (spi->cfg->has_fifo)
dma_size = spi->fifo_size; else
dma_size = SPI_DMA_MIN_BYTES;
sr = readl_relaxed(spi->base + STM32FX_SPI_SR); /* * BSY flag is not handled in interrupt but it is normal behavior when * this flag is set.
*/
sr &= ~STM32FX_SPI_SR_BSY;
if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX ||
spi->cur_comm == SPI_3WIRE_TX)) { /* OVR flag shouldn't be handled for TX only mode */
sr &= ~(STM32FX_SPI_SR_OVR | STM32FX_SPI_SR_RXNE);
mask |= STM32FX_SPI_SR_TXE;
}
if (!spi->cur_usedma && (spi->cur_comm == SPI_FULL_DUPLEX ||
spi->cur_comm == SPI_SIMPLEX_RX ||
spi->cur_comm == SPI_3WIRE_RX)) { /* TXE flag is set and is handled when RXNE flag occurs */
sr &= ~STM32FX_SPI_SR_TXE;
mask |= STM32FX_SPI_SR_RXNE | STM32FX_SPI_SR_OVR;
}
if (!(sr & mask)) {
dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr);
spin_unlock(&spi->lock); return IRQ_NONE;
}
if (sr & STM32FX_SPI_SR_OVR) {
dev_warn(spi->dev, "Overrun: received value discarded\n");
/* Sequence to clear OVR flag */
readl_relaxed(spi->base + STM32FX_SPI_DR);
readl_relaxed(spi->base + STM32FX_SPI_SR);
/* * If overrun is detected, it means that something went wrong, * so stop the current transfer. Transfer can wait for next * RXNE but DR is already read and end never happens.
*/
end = true; goto end_irq;
}
if (sr & STM32FX_SPI_SR_TXE) { if (spi->tx_buf)
spi->cfg->write_tx(spi); if (spi->tx_len == 0)
end = true;
}
if (sr & STM32FX_SPI_SR_RXNE) {
spi->cfg->read_rx(spi); if (spi->rx_len == 0)
end = true; elseif (spi->tx_buf)/* Load data for discontinuous mode */
spi->cfg->write_tx(spi);
}
end_irq: if (end) { /* Immediately disable interrupts to do not generate new one */
stm32_spi_clr_bits(spi, STM32FX_SPI_CR2,
STM32FX_SPI_CR2_TXEIE |
STM32FX_SPI_CR2_RXNEIE |
STM32FX_SPI_CR2_ERRIE);
spin_unlock(&spi->lock); return IRQ_WAKE_THREAD;
}
sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
ier = readl_relaxed(spi->base + STM32H7_SPI_IER);
mask = ier; /* * EOTIE enables irq from EOT, SUSP and TXC events. We need to set * SUSP to acknowledge it later. TXC is automatically cleared
*/
mask |= STM32H7_SPI_SR_SUSP; /* * DXPIE is set in Full-Duplex, one IT will be raised if TXP and RXP * are set. So in case of Full-Duplex, need to poll TXP and RXP event.
*/ if ((spi->cur_comm == SPI_FULL_DUPLEX) && !spi->cur_usedma)
mask |= STM32H7_SPI_SR_TXP | STM32H7_SPI_SR_RXP;
if (!(sr & mask)) {
dev_vdbg(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
sr, ier);
spin_unlock_irqrestore(&spi->lock, flags); return IRQ_NONE;
}
if (sr & STM32H7_SPI_SR_SUSP) { static DEFINE_RATELIMIT_STATE(rs,
DEFAULT_RATELIMIT_INTERVAL * 10,
1);
ratelimit_set_flags(&rs, RATELIMIT_MSG_ON_RELEASE); if (__ratelimit(&rs))
dev_dbg_ratelimited(spi->dev, "Communication suspended\n"); if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
stm32h7_spi_read_rxfifo(spi); /* * If communication is suspended while using DMA, it means * that something went wrong, so stop the current transfer
*/ if (spi->cur_usedma)
end = true;
}
if (sr & STM32H7_SPI_SR_MODF) {
dev_warn(spi->dev, "Mode fault: transfer aborted\n");
end = true;
}
if (sr & STM32H7_SPI_SR_OVR) {
dev_err(spi->dev, "Overrun: RX data lost\n");
end = true;
}
if (sr & STM32H7_SPI_SR_EOT) {
dev_dbg(spi->dev, "End of transfer\n"); if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
stm32h7_spi_read_rxfifo(spi); if (!spi->cur_usedma ||
(spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) ||
(spi->mdma_rx && (spi->cur_comm == SPI_SIMPLEX_RX ||
spi->cur_comm == SPI_FULL_DUPLEX)))
end = true;
}
if (sr & STM32H7_SPI_SR_TXP) if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
stm32h7_spi_write_txfifo(spi);
if (sr & STM32H7_SPI_SR_RXP) if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
stm32h7_spi_read_rxfifo(spi);
/* On STM32H7, messages should not exceed a maximum size set * later via the set_number_of_data function. In order to * ensure that, split large messages into several messages
*/ if (spi->cfg->set_number_of_data) return spi_split_transfers_maxwords(ctrl, msg, spi->t_size_max);
return 0;
}
/** * stm32_spi_prepare_msg - set up the controller to transfer a single message * @ctrl: controller interface * @msg: pointer to spi message
*/ staticint stm32_spi_prepare_msg(struct spi_controller *ctrl, struct spi_message *msg)
{ struct stm32_spi *spi = spi_controller_get_devdata(ctrl); struct spi_device *spi_dev = msg->spi; struct device_node *np = spi_dev->dev.of_node; unsignedlong flags;
u32 clrb = 0, setb = 0;
/* SPI target device may need time between data frames */
spi->cur_midi = 0; if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
/* CPOL, CPHA, LSB FIRST, CS_HIGH and RDY_EN bits have common register */ if (clrb || setb)
writel_relaxed(
(readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) &
~clrb) | setb,
spi->base + spi->cfg->regs->cpol.reg);
spin_unlock_irqrestore(&spi->lock, flags);
return 0;
}
/** * stm32fx_spi_dma_tx_cb - dma callback * @data: pointer to the spi controller data structure * * DMA callback is called when the transfer is complete for DMA TX channel.
*/ staticvoid stm32fx_spi_dma_tx_cb(void *data)
{ struct stm32_spi *spi = data;
/** * stm32_spi_dma_rx_cb - dma callback * @data: pointer to the spi controller data structure * * DMA callback is called when the transfer is complete for DMA RX channel.
*/ staticvoid stm32_spi_dma_rx_cb(void *data)
{ struct stm32_spi *spi = data;
/** * stm32_spi_dma_config - configure dma slave channel depending on current * transfer bits_per_word. * @spi: pointer to the spi controller data structure * @dma_chan: pointer to the DMA channel * @dma_conf: pointer to the dma_slave_config structure * @dir: direction of the dma transfer
*/ staticvoid stm32_spi_dma_config(struct stm32_spi *spi, struct dma_chan *dma_chan, struct dma_slave_config *dma_conf, enum dma_transfer_direction dir)
{ enum dma_slave_buswidth buswidth; struct dma_slave_caps caps;
u32 maxburst = 1; int ret;
/* Valid for DMA Half or Full Fifo threshold */ if (!spi->cfg->prevent_dma_burst && spi->cfg->has_fifo && spi->cur_fthlv != 2)
maxburst = spi->cur_fthlv;
/* Get the DMA channel caps, and adjust maxburst if possible */
ret = dma_get_slave_caps(dma_chan, &caps); if (!ret)
maxburst = min(maxburst, caps.max_burst);
/** * stm32fx_spi_transfer_one_irq - transfer a single spi_transfer using * interrupts * @spi: pointer to the spi controller data structure * * It must returns 0 if the transfer is finished or 1 if the transfer is still * in progress.
*/ staticint stm32fx_spi_transfer_one_irq(struct stm32_spi *spi)
{ unsignedlong flags;
u32 cr2 = 0;
/* Enable the interrupts relative to the current communication mode */ if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
cr2 |= STM32FX_SPI_CR2_TXEIE;
} elseif (spi->cur_comm == SPI_FULL_DUPLEX ||
spi->cur_comm == SPI_SIMPLEX_RX ||
spi->cur_comm == SPI_3WIRE_RX) { /* In transmit-only mode, the OVR flag is set in the SR register * since the received data are never read. Therefore set OVR * interrupt only when rx buffer is available.
*/
cr2 |= STM32FX_SPI_CR2_RXNEIE | STM32FX_SPI_CR2_ERRIE;
} else { return -EINVAL;
}
spin_lock_irqsave(&spi->lock, flags);
stm32_spi_set_bits(spi, STM32FX_SPI_CR2, cr2);
stm32_spi_enable(spi);
/* starting data transfer when buffer is loaded */ if (spi->tx_buf)
spi->cfg->write_tx(spi);
spin_unlock_irqrestore(&spi->lock, flags);
return 1;
}
/** * stm32h7_spi_transfer_one_irq - transfer a single spi_transfer using * interrupts * @spi: pointer to the spi controller data structure * * It must returns 0 if the transfer is finished or 1 if the transfer is still * in progress.
*/ staticint stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
{ unsignedlong flags;
u32 ier = 0;
/* Enable the interrupts relative to the current communication mode */ if (spi->tx_buf && spi->rx_buf) /* Full Duplex */
ier |= STM32H7_SPI_IER_DXPIE; elseif (spi->tx_buf) /* Half-Duplex TX dir or Simplex TX */
ier |= STM32H7_SPI_IER_TXPIE; elseif (spi->rx_buf) /* Half-Duplex RX dir or Simplex RX */
ier |= STM32H7_SPI_IER_RXPIE;
/* Enable the interrupts relative to the end of transfer */
ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE |
STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
spin_lock_irqsave(&spi->lock, flags);
stm32_spi_enable(spi);
/* Be sure to have data in fifo before starting data transfer */ if (spi->tx_buf)
stm32h7_spi_write_txfifo(spi);
if (STM32_SPI_HOST_MODE(spi))
stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
writel_relaxed(ier, spi->base + STM32H7_SPI_IER);
spin_unlock_irqrestore(&spi->lock, flags);
return 1;
}
/** * stm32fx_spi_transfer_one_dma_start - Set SPI driver registers to start * transfer using DMA * @spi: pointer to the spi controller data structure
*/ staticvoid stm32fx_spi_transfer_one_dma_start(struct stm32_spi *spi)
{ /* In DMA mode end of transfer is handled by DMA TX or RX callback. */ if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX ||
spi->cur_comm == SPI_FULL_DUPLEX) { /* * In transmit-only mode, the OVR flag is set in the SR register * since the received data are never read. Therefore set OVR * interrupt only when rx buffer is available.
*/
stm32_spi_set_bits(spi, STM32FX_SPI_CR2, STM32FX_SPI_CR2_ERRIE);
}
stm32_spi_enable(spi);
}
/** * stm32f7_spi_transfer_one_dma_start - Set SPI driver registers to start * transfer using DMA * @spi: pointer to the spi controller data structure
*/ staticvoid stm32f7_spi_transfer_one_dma_start(struct stm32_spi *spi)
{ /* Configure DMA request trigger threshold according to DMA width */ if (spi->cur_bpw <= 8)
stm32_spi_set_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH); else
stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
stm32fx_spi_transfer_one_dma_start(spi);
}
/** * stm32h7_spi_transfer_one_dma_start - Set SPI driver registers to start * transfer using DMA * @spi: pointer to the spi controller data structure
*/ staticvoid stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
{
uint32_t ier = STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
if (STM32_SPI_HOST_MODE(spi))
stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
}
/** * stm32_spi_prepare_rx_dma_mdma_chaining - Prepare RX DMA and MDMA chaining * @spi: pointer to the spi controller data structure * @xfer: pointer to the spi transfer * @rx_dma_conf: pointer to the DMA configuration for RX channel * @rx_dma_desc: pointer to the RX DMA descriptor * @rx_mdma_desc: pointer to the RX MDMA descriptor * * It must return 0 if the chaining is possible or an error code if not.
*/ staticint stm32_spi_prepare_rx_dma_mdma_chaining(struct stm32_spi *spi, struct spi_transfer *xfer, struct dma_slave_config *rx_dma_conf, struct dma_async_tx_descriptor **rx_dma_desc, struct dma_async_tx_descriptor **rx_mdma_desc)
{ struct dma_async_tx_descriptor *_mdma_desc = *rx_mdma_desc; struct dma_async_tx_descriptor *_dma_desc = *rx_dma_desc; struct dma_slave_config rx_mdma_conf = {0};
u32 sram_period, nents = 0, spi_s_len; struct sg_table dma_sgt, mdma_sgt; struct scatterlist *spi_s, *s;
dma_addr_t dma_buf; int i, ret;
if (!spi_s_len && sg_next(spi_s)) {
spi_s = sg_next(spi_s);
spi_s_len = sg_dma_len(spi_s);
dma_buf = spi->sram_dma_rx_buf;
} else { /* DMA configured in DBM: it will swap between the SRAM periods */ if (i & 1)
dma_buf += sram_period; else
dma_buf = spi->sram_dma_rx_buf;
}
}
if (!_mdma_desc) {
_dma_desc = NULL; return -EINVAL;
}
return 0;
}
/** * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA * @spi: pointer to the spi controller data structure * @xfer: pointer to the spi_transfer structure * * It must returns 0 if the transfer is finished or 1 if the transfer is still * in progress.
*/ staticint stm32_spi_transfer_one_dma(struct stm32_spi *spi, struct spi_transfer *xfer)
{ struct dma_async_tx_descriptor *rx_mdma_desc = NULL, *rx_dma_desc = NULL; struct dma_async_tx_descriptor *tx_dma_desc = NULL; struct dma_slave_config tx_dma_conf, rx_dma_conf; unsignedlong flags; int ret = 0;
spin_lock_irqsave(&spi->lock, flags);
if (spi->rx_buf && spi->dma_rx) {
stm32_spi_dma_config(spi, spi->dma_rx, &rx_dma_conf, DMA_DEV_TO_MEM); if (spi->mdma_rx) {
rx_dma_conf.peripheral_size = 1;
dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
ret = stm32_spi_prepare_rx_dma_mdma_chaining(spi, xfer, &rx_dma_conf,
&rx_dma_desc, &rx_mdma_desc); if (ret) { /* RX DMA MDMA chaining not possible, fallback to DMA only */
rx_dma_conf.peripheral_config = 0;
rx_dma_desc = NULL;
}
} if (!rx_dma_desc) {
dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
rx_dma_desc = dmaengine_prep_slave_sg(spi->dma_rx, xfer->rx_sg.sgl,
xfer->rx_sg.nents,
rx_dma_conf.direction,
DMA_PREP_INTERRUPT);
}
}
/** * stm32_spi_communication_type - return transfer communication type * @spi_dev: pointer to the spi device * @transfer: pointer to spi transfer
*/ staticunsignedint stm32_spi_communication_type(struct spi_device *spi_dev, struct spi_transfer *transfer)
{ unsignedint type = SPI_FULL_DUPLEX;
if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */ /* * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL * is forbidden and unvalidated by SPI subsystem so depending * on the valid buffer, we can determine the direction of the * transfer.
*/ if (!transfer->tx_buf)
type = SPI_3WIRE_RX; else
type = SPI_3WIRE_TX;
} else { if (!transfer->tx_buf)
type = SPI_SIMPLEX_RX; elseif (!transfer->rx_buf)
type = SPI_SIMPLEX_TX;
}
return type;
}
/** * stm32fx_spi_set_mode - configure communication mode * @spi: pointer to the spi controller data structure * @comm_type: type of communication to configure
*/ staticint stm32fx_spi_set_mode(struct stm32_spi *spi, unsignedint comm_type)
{ if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
stm32_spi_set_bits(spi, STM32FX_SPI_CR1,
STM32FX_SPI_CR1_BIDIMODE |
STM32FX_SPI_CR1_BIDIOE);
} elseif (comm_type == SPI_FULL_DUPLEX ||
comm_type == SPI_SIMPLEX_RX) {
stm32_spi_clr_bits(spi, STM32FX_SPI_CR1,
STM32FX_SPI_CR1_BIDIMODE |
STM32FX_SPI_CR1_BIDIOE);
} elseif (comm_type == SPI_3WIRE_RX) {
stm32_spi_set_bits(spi, STM32FX_SPI_CR1,
STM32FX_SPI_CR1_BIDIMODE);
stm32_spi_clr_bits(spi, STM32FX_SPI_CR1,
STM32FX_SPI_CR1_BIDIOE);
} else { return -EINVAL;
}
return 0;
}
/** * stm32h7_spi_set_mode - configure communication mode * @spi: pointer to the spi controller data structure * @comm_type: type of communication to configure
*/ staticint stm32h7_spi_set_mode(struct stm32_spi *spi, unsignedint comm_type)
{
u32 mode;
u32 cfg2_clrb = 0, cfg2_setb = 0;
/** * stm32h7_spi_data_idleness - configure minimum time delay inserted between two * consecutive data frames in host mode * @spi: pointer to the spi controller data structure * @xfer: pointer to spi transfer
*/ staticvoid stm32h7_spi_data_idleness(struct stm32_spi *spi, struct spi_transfer *xfer)
{
u32 cfg2_clrb = 0, cfg2_setb = 0;
u32 len = xfer->len;
u32 spi_delay_ns;
/** * stm32h7_spi_number_of_data - configure number of data at current transfer * @spi: pointer to the spi controller data structure * @nb_words: transfer length (in words)
*/ staticint stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
{ if (nb_words <= spi->t_size_max) {
writel_relaxed(FIELD_PREP(STM32H7_SPI_CR2_TSIZE, nb_words),
spi->base + STM32H7_SPI_CR2);
} else { return -EMSGSIZE;
}
return 0;
}
/** * stm32_spi_transfer_one_setup - common setup to transfer a single * spi_transfer either using DMA or * interrupts. * @spi: pointer to the spi controller data structure * @spi_dev: pointer to the spi device * @transfer: pointer to spi transfer
*/ staticint stm32_spi_transfer_one_setup(struct stm32_spi *spi, struct spi_device *spi_dev, struct spi_transfer *transfer)
{ unsignedlong flags; unsignedint comm_type; int nb_words, ret = 0; int mbr;
if (spi->cfg->set_number_of_data) {
ret = spi->cfg->set_number_of_data(spi, nb_words); if (ret < 0) goto out;
}
dev_dbg(spi->dev, "transfer communication mode set to %d\n",
spi->cur_comm);
dev_dbg(spi->dev, "data frame of %d-bit, data packet of %d data frames\n",
spi->cur_bpw, spi->cur_fthlv); if (STM32_SPI_HOST_MODE(spi))
dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
spi->cur_xferlen, nb_words);
dev_dbg(spi->dev, "dma %s\n",
(spi->cur_usedma) ? "enabled" : "disabled");
out:
spin_unlock_irqrestore(&spi->lock, flags);
return ret;
}
/** * stm32_spi_transfer_one - transfer a single spi_transfer * @ctrl: controller interface * @spi_dev: pointer to the spi device * @transfer: pointer to spi transfer * * It must return 0 if the transfer is finished or 1 if the transfer is still * in progress.
*/ staticint stm32_spi_transfer_one(struct spi_controller *ctrl, struct spi_device *spi_dev, struct spi_transfer *transfer)
{ struct stm32_spi *spi = spi_controller_get_devdata(ctrl); int ret;
if (spi->sram_rx_buf)
memset(spi->sram_rx_buf, 0, spi->sram_rx_buf_size);
return 0;
}
/** * stm32fx_spi_config - Configure SPI controller as SPI host * @spi: pointer to the spi controller data structure
*/ staticint stm32fx_spi_config(struct stm32_spi *spi)
{ unsignedlong flags;
spin_lock_irqsave(&spi->lock, flags);
/* Ensure I2SMOD bit is kept cleared */
stm32_spi_clr_bits(spi, STM32FX_SPI_I2SCFGR,
STM32FX_SPI_I2SCFGR_I2SMOD);
/* * - SS input value high * - transmitter half duplex direction * - Set the host mode (default Motorola mode) * - Consider 1 host/n targets configuration and * SS input value is determined by the SSI bit
*/
stm32_spi_set_bits(spi, STM32FX_SPI_CR1, STM32FX_SPI_CR1_SSI |
STM32FX_SPI_CR1_BIDIOE |
STM32FX_SPI_CR1_MSTR |
STM32FX_SPI_CR1_SSM);
/* Ensure I2SMOD bit is kept cleared */
stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
STM32H7_SPI_I2SCFGR_I2SMOD);
if (STM32_SPI_DEVICE_MODE(spi)) { /* Use native device select */
cfg2 &= ~STM32H7_SPI_CFG2_SSM;
} else { /* * - Transmitter half duplex direction * - Automatic communication suspend when RX-Fifo is full * - SS input value high
*/
cr1 |= STM32H7_SPI_CR1_HDDIR | STM32H7_SPI_CR1_MASRX | STM32H7_SPI_CR1_SSI;
/* * - Set the host mode (default Motorola mode) * - Consider 1 host/n devices configuration and * SS input value is determined by the SSI bit * - keep control of all associated GPIOs
*/
cfg2 |= STM32H7_SPI_CFG2_MASTER | STM32H7_SPI_CFG2_SSM | STM32H7_SPI_CFG2_AFCNTR;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.