/* use PIO for small transfers, avoiding DMA setup/teardown overhead and * cache operations; better heuristics consider wordsize and bitrate.
*/ #define DMA_MIN_BYTES 16
/* * The core SPI transfer engine just talks to a register bank to set up * DMA transfers; transfer queue progress is driven by IRQs. The clock * framework provides the base clock, subdivided for each spi_device.
*/ struct atmel_spi {
spinlock_t lock; unsignedlong flags;
#define SPI_MAX_DMA_XFER 65535 /* true for both PDC and DMA */ #define INVALID_DMA_ADDRESS 0xffffffff
/* * This frequency can be anything supported by the controller, but to avoid * unnecessary delay, the highest possible frequency is chosen. * * This frequency is the highest possible which is not interfering with other * chip select registers (see Note for Serial Clock Bit Rate configuration in * Atmel-11121F-ATARM-SAMA5D3-Series-Datasheet_02-Feb-16, page 1283)
*/ #define DUMMY_MSG_FREQUENCY 0x02 /* * 8 bits is the minimum data the controller is capable of sending. * * This message can be anything as it should not be treated by any SPI device.
*/ #define DUMMY_MSG 0xAA
/* * Version 2 of the SPI controller has * - CR.LASTXFER * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero) * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs) * - SPI_CSRx.CSAAT * - SPI_CSRx.SBCR allows faster clocking
*/ staticbool atmel_spi_is_v2(struct atmel_spi *as)
{ return as->caps.is_spi2;
}
/* * Send a dummy message. * * This is sometimes needed when using a CS GPIO to force clock transition when * switching between devices with different polarities.
*/ staticvoid atmel_spi_send_dummy(struct atmel_spi *as, struct spi_device *spi, int chip_select)
{
u32 status;
u32 csr;
/* * Set a clock frequency to allow sending message on SPI bus. * The frequency here can be anything, but is needed for * the controller to send the data.
*/
csr = spi_readl(as, CSR0 + 4 * chip_select);
csr = SPI_BFINS(SCBR, DUMMY_MSG_FREQUENCY, csr);
spi_writel(as, CSR0 + 4 * chip_select, csr);
/* * Read all data coming from SPI bus, needed to be able to send * the message.
*/
spi_readl(as, RDR); while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
spi_readl(as, RDR);
cpu_relax();
}
/* * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby * they assume that spi slave device state will not change on deselect, so * that automagic deselection is OK. ("NPCSx rises if no data is to be * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer * controllers have CSAAT and friends. * * Even controller newer than ar91rm9200, using GPIOs can make sens as * it lets us support active-high chipselects despite the controller's * belief that only active-low devices/systems exists. * * However, at91rm9200 has a second erratum whereby nCS0 doesn't work * right when driven with GPIO. ("Mode Fault does not allow more than one * Master on Chip Select 0.") No workaround exists for that ... so for * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH, * and (c) will trigger that first erratum in some cases. * * When changing the clock polarity, the SPI controller waits for the next * transmission to enforce the default clock state. This may be an issue when * using a GPIO as Chip Select: the clock level is applied only when the first * packet is sent, once the CS has already been asserted. The workaround is to * avoid this by sending a first (dummy) message before toggling the CS state.
*/ staticvoid cs_activate(struct atmel_spi *as, struct spi_device *spi)
{ struct atmel_spi_device *asd = spi->controller_state; bool new_polarity; int chip_select;
u32 mr;
if (atmel_spi_is_v2(as)) {
spi_writel(as, CSR0 + 4 * chip_select, asd->csr); /* For the low SPI version, there is a issue that PDC transfer * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
*/
spi_writel(as, CSR0, asd->csr); if (as->caps.has_wdrbt) {
spi_writel(as, MR,
SPI_BF(PCS, ~(0x01 << chip_select))
| SPI_BIT(WDRBT)
| SPI_BIT(MODFDIS)
| SPI_BIT(MSTR));
} else {
spi_writel(as, MR,
SPI_BF(PCS, ~(0x01 << chip_select))
| SPI_BIT(MODFDIS)
| SPI_BIT(MSTR));
}
mr = spi_readl(as, MR);
/* * Ensures the clock polarity is valid before we actually * assert the CS to avoid spurious clock edges to be * processed by the spi devices.
*/ if (spi_get_csgpiod(spi, 0)) {
new_polarity = (asd->csr & SPI_BIT(CPOL)) != 0; if (new_polarity != as->last_polarity) { /* * Need to disable the GPIO before sending the dummy * message because it is already set by the spi core.
*/
gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), 0);
atmel_spi_send_dummy(as, spi, chip_select);
as->last_polarity = new_polarity;
gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), 1);
}
}
} else {
u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; int i;
u32 csr;
/* Make sure clock polarity is correct */ for (i = 0; i < spi->controller->num_chipselect; i++) {
csr = spi_readl(as, CSR0 + 4 * i); if ((csr ^ cpol) & SPI_BIT(CPOL))
spi_writel(as, CSR0 + 4 * i,
csr ^ SPI_BIT(CPOL));
}
/* only deactivate *this* device; sometimes transfers to * another device may be active when this routine is called.
*/
mr = spi_readl(as, MR); if (~SPI_BFEXT(PCS, mr) & (1 << chip_select)) {
mr = SPI_BFINS(PCS, 0xf, mr);
spi_writel(as, MR, mr);
}
/* * This driver uses fixed peripheral select mode (PS bit set to '0' in * the Mode Register). * So according to the datasheet, when FIFOs are available (and * enabled), the Transmit FIFO operates in Multiple Data Mode. * In this mode, up to 2 data, not 4, can be written into the Transmit * Data Register in a single access. * However, the first data has to be written into the lowest 16 bits and * the second data into the highest 16 bits of the Transmit * Data Register. For 8bit data (the most frequent case), it would * require to rework tx_buf so each data would actually fit 16 bits. * So we'd rather write only one data at the time. Hence the transmit * path works the same whether FIFOs are available (and enabled) or not.
*/ if (dmaengine_slave_config(host->dma_tx, &slave_config)) {
dev_err(&as->pdev->dev, "failed to configure tx dma channel\n");
err = -EINVAL;
}
/* * This driver configures the spi controller for host mode (MSTR bit * set to '1' in the Mode Register). * So according to the datasheet, when FIFOs are available (and * enabled), the Receive FIFO operates in Single Data Mode. * So the receive path works the same whether FIFOs are available (and * enabled) or not.
*/ if (dmaengine_slave_config(host->dma_rx, &slave_config)) {
dev_err(&as->pdev->dev, "failed to configure rx dma channel\n");
err = -EINVAL;
}
host->dma_tx = dma_request_chan(dev, "tx"); if (IS_ERR(host->dma_tx)) {
err = PTR_ERR(host->dma_tx);
dev_dbg(dev, "No TX DMA channel, DMA is disabled\n"); goto error_clear;
}
host->dma_rx = dma_request_chan(dev, "rx"); if (IS_ERR(host->dma_rx)) {
err = PTR_ERR(host->dma_rx); /* * No reason to check EPROBE_DEFER here since we have already * requested tx channel.
*/
dev_dbg(dev, "No RX DMA channel, DMA is disabled\n"); goto error;
}
err = atmel_spi_dma_slave_config(as, 8); if (err) goto error;
dev_info(&as->pdev->dev, "Using %s (tx) and %s (rx) for DMA transfers\n",
dma_chan_name(host->dma_tx),
dma_chan_name(host->dma_rx));
return 0;
error: if (!IS_ERR(host->dma_rx))
dma_release_channel(host->dma_rx); if (!IS_ERR(host->dma_tx))
dma_release_channel(host->dma_tx);
error_clear:
host->dma_tx = host->dma_rx = NULL; return err;
}
staticvoid atmel_spi_stop_dma(struct spi_controller *host)
{ if (host->dma_rx)
dmaengine_terminate_all(host->dma_rx); if (host->dma_tx)
dmaengine_terminate_all(host->dma_tx);
}
/* This function is called by the DMA driver from tasklet context */ staticvoid dma_callback(void *data)
{ struct spi_controller *host = data; struct atmel_spi *as = spi_controller_get_devdata(host);
/* Compute the number of data to transfer in the current iteration */
current_remaining_data = ((xfer->bits_per_word > 8) ?
((u32)as->current_remaining_bytes >> 1) :
(u32)as->current_remaining_bytes);
num_data = min(current_remaining_data, as->fifo_size);
/* Flush RX and TX FIFOs */
spi_writel(as, CR, SPI_BIT(RXFCLR) | SPI_BIT(TXFCLR)); while (spi_readl(as, FLR))
cpu_relax();
/* Set RX FIFO Threshold to the number of data to transfer */
fifomr = spi_readl(as, FMR);
spi_writel(as, FMR, SPI_BFINS(RXFTHRES, num_data, fifomr));
/* Clear FIFO flags in the Status Register, especially RXFTHF */
(void)spi_readl(as, SR);
/* Put the callback on the RX transfer only, that should finish last */
rxdesc->callback = dma_callback;
rxdesc->callback_param = host;
/* Submit and fire RX and TX with TX last so we're ready to read! */
cookie = rxdesc->tx_submit(rxdesc); if (dma_submit_error(cookie)) goto err_dma;
cookie = txdesc->tx_submit(txdesc); if (dma_submit_error(cookie)) goto err_dma;
rxchan->device->device_issue_pending(rxchan);
txchan->device->device_issue_pending(txchan);
if (xfer->bits_per_word > 8)
len >>= 1;
spi_writel(as, RNCR, len);
spi_writel(as, TNCR, len);
dev_dbg(&host->dev, " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
xfer, xfer->len, xfer->tx_buf,
(unsignedlonglong)xfer->tx_dma, xfer->rx_buf,
(unsignedlonglong)xfer->rx_dma);
}
/* REVISIT: We're waiting for RXBUFF before we start the next * transfer because we need to handle some difficult timing * issues otherwise. If we wait for TXBUFE in one transfer and * then starts waiting for RXBUFF in the next, it's difficult * to tell the difference between the RXBUFF interrupt we're * actually waiting for and the RXBUFF interrupt of the * previous transfer. * * It should be doable, though. Just not now...
*/
spi_writel(as, IER, SPI_BIT(RXBUFF) | SPI_BIT(OVRES));
spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
}
/* * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma: * - The buffer is either valid for CPU access, else NULL * - If the buffer is valid, so is its DMA address
*/ staticint
atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
{ struct device *dev = &as->pdev->dev;
xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS; if (xfer->tx_buf) { /* tx_buf is a const void* where we need a void * for the dma
* mapping */ void *nonconst_tx = (void *)xfer->tx_buf;
xfer->tx_dma = dma_map_single(dev,
nonconst_tx, xfer->len,
DMA_TO_DEVICE); if (dma_mapping_error(dev, xfer->tx_dma)) return -ENOMEM;
} if (xfer->rx_buf) {
xfer->rx_dma = dma_map_single(dev,
xfer->rx_buf, xfer->len,
DMA_FROM_DEVICE); if (dma_mapping_error(dev, xfer->rx_dma)) { if (xfer->tx_buf)
dma_unmap_single(dev,
xfer->tx_dma, xfer->len,
DMA_TO_DEVICE); return -ENOMEM;
}
} return 0;
}
/* Handle odd number of bytes when data are more than 8bit width */ if (xfer->bits_per_word > 8)
as->current_remaining_bytes &= ~0x1;
/* Read data */ while (num_data) {
rd = spi_readl(as, RDR); if (xfer->bits_per_word > 8)
*words++ = rd; else
*bytes++ = rd;
num_data--;
}
}
/* Called from IRQ * * Must update "current_remaining_bytes" to keep track of data * to transfer.
*/ staticvoid
atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
{ if (as->fifo_size)
atmel_spi_pump_fifo_data(as, xfer); else
atmel_spi_pump_single_data(as, xfer);
}
imr = spi_readl(as, IMR);
status = spi_readl(as, SR);
pending = status & imr;
if (pending & SPI_BIT(OVRES)) {
ret = IRQ_HANDLED;
spi_writel(as, IDR, SPI_BIT(OVRES));
dev_warn(host->dev.parent, "overrun\n");
/* * When we get an overrun, we disregard the current * transfer. Data will not be copied back from any * bounce buffer and msg->actual_len will not be * updated with the last xfer. * * We will also not process any remaning transfers in * the message.
*/
as->done_status = -EIO;
smp_wmb();
/* Clear any overrun happening while cleaning up */
spi_readl(as, SR);
/* see notes above re chipselect */ if (!spi_get_csgpiod(spi, 0) && (spi->mode & SPI_CS_HIGH)) {
dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n"); return -EINVAL;
}
/* Setup() is called during spi_register_controller(aka * spi_register_master) but after all membmers of the cs_gpiod * array have been filled, so we can looked for which native * CS will be free for using with GPIO
*/
initialize_native_cs_for_gpio(as);
if (spi_get_csgpiod(spi, 0) && as->native_cs_free) {
dev_err(&spi->dev, "No native CS available to support this GPIO CS\n"); return -EBUSY;
}
csr = SPI_BF(BITS, bits - 8); if (spi->mode & SPI_CPOL)
csr |= SPI_BIT(CPOL); if (!(spi->mode & SPI_CPHA))
csr |= SPI_BIT(NCPHA);
if (!spi_get_csgpiod(spi, 0))
csr |= SPI_BIT(CSAAT);
csr |= SPI_BF(DLYBS, 0);
word_delay_csr = atmel_word_delay_csr(spi, as); if (word_delay_csr < 0) return word_delay_csr;
/* DLYBCT adds delays between words. This is useful for slow devices * that need a bit of time to setup the next transfer.
*/
csr |= SPI_BF(DLYBCT, word_delay_csr);
asd = spi->controller_state; if (!asd) {
asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL); if (!asd) return -ENOMEM;
if (!atmel_spi_is_v2(as))
spi_writel(as, CSR0 + 4 * chip_select, csr);
return 0;
}
staticvoid atmel_spi_set_cs(struct spi_device *spi, bool enable)
{ struct atmel_spi *as = spi_controller_get_devdata(spi->controller); /* the core doesn't really pass us enable/disable, but CS HIGH vs CS LOW * since we already have routines for activate/deactivate translate * high/low to active/inactive
*/
enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
if (as->use_pdc) {
atmel_spi_lock(as);
atmel_spi_pdc_next_xfer(host, xfer);
atmel_spi_unlock(as);
} elseif (atmel_spi_use_dma(as, xfer)) {
len = as->current_remaining_bytes;
ret = atmel_spi_next_xfer_dma_submit(host,
xfer, &len); if (ret) {
dev_err(&spi->dev, "unable to use DMA, fallback to PIO\n");
as->done_status = ret; break;
} else {
as->current_remaining_bytes -= len; if (as->current_remaining_bytes < 0)
as->current_remaining_bytes = 0;
}
} else {
atmel_spi_lock(as);
atmel_spi_next_xfer_pio(host, xfer);
atmel_spi_unlock(as);
}
dma_timeout = msecs_to_jiffies(spi_controller_xfer_timeout(host, xfer));
ret_timeout = wait_for_completion_timeout(&as->xfer_completion, dma_timeout); if (!ret_timeout) {
dev_err(&spi->dev, "spi transfer timeout\n");
as->done_status = -EIO;
}
if (as->done_status) break;
}
if (as->done_status) { if (as->use_pdc) {
dev_warn(host->dev.parent, "overrun (%u/%u remaining)\n",
spi_readl(as, TCR), spi_readl(as, RCR));
/* * Clean up DMA registers and make sure the data * registers are empty.
*/
spi_writel(as, RNCR, 0);
spi_writel(as, TNCR, 0);
spi_writel(as, RCR, 0);
spi_writel(as, TCR, 0); for (timeout = 1000; timeout; timeout--) if (spi_readl(as, SR) & SPI_BIT(TXEMPTY)) break; if (!timeout)
dev_warn(host->dev.parent, "timeout waiting for TXEMPTY"); while (spi_readl(as, SR) & SPI_BIT(RDRF))
spi_readl(as, RDR);
/* Clear any overrun happening while cleaning up */
spi_readl(as, SR);
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.