time_left = wait_for_completion_timeout(&mas->abort_done, HZ); if (!time_left) {
dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
/* * No need for a lock since SPI core has a lock and we never * access this from an interrupt.
*/
mas->abort_failed = true;
}
reset_if_dma: if (mas->cur_xfer_mode == GENI_SE_DMA) { if (xfer) { if (xfer->tx_buf) {
spin_lock_irq(&mas->lock);
reinit_completion(&mas->tx_reset_done);
writel(1, se->base + SE_DMA_TX_FSM_RST);
spin_unlock_irq(&mas->lock);
time_left = wait_for_completion_timeout(&mas->tx_reset_done, HZ); if (!time_left)
dev_err(mas->dev, "DMA TX RESET failed\n");
} if (xfer->rx_buf) {
spin_lock_irq(&mas->lock);
reinit_completion(&mas->rx_reset_done);
writel(1, se->base + SE_DMA_RX_FSM_RST);
spin_unlock_irq(&mas->lock);
time_left = wait_for_completion_timeout(&mas->rx_reset_done, HZ); if (!time_left)
dev_err(mas->dev, "DMA RX RESET failed\n");
}
} else { /* * This can happen if a timeout happened and we had to wait * for lock in this function because isr was holding the lock * and handling transfer completion at that time.
*/
dev_warn(mas->dev, "Cancel/Abort on completed SPI transfer\n");
}
}
}
switch (mas->cur_xfer_mode) { case GENI_SE_FIFO: case GENI_SE_DMA:
handle_se_timeout(spi, msg); break; case GENI_GPI_DMA:
handle_gpi_timeout(spi, msg); break; default:
dev_err(mas->dev, "Abort on Mode:%d not supported", mas->cur_xfer_mode);
}
}
/* * The only known case where a transfer times out and then a cancel * times out then an abort times out is if something is blocking our * interrupt handler from running. Avoid starting any new transfers * until that sorts itself out.
*/
spin_lock_irq(&mas->lock);
m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
m_irq_en = readl(se->base + SE_GENI_M_IRQ_EN);
spin_unlock_irq(&mas->lock);
if (m_irq & m_irq_en) {
dev_err(mas->dev, "Interrupts pending after abort: %#010x\n",
m_irq & m_irq_en); returntrue;
}
/* * If we're here the problem resolved itself so no need to check more * on future transfers.
*/
mas->abort_failed = false;
if (!(slv->mode & SPI_CS_HIGH))
set_flag = !set_flag;
if (set_flag == mas->cs_flag) return;
pm_runtime_get_sync(mas->dev);
if (spi_geni_is_abort_still_pending(mas)) {
dev_err(mas->dev, "Can't set chip select\n"); gotoexit;
}
spin_lock_irq(&mas->lock); if (mas->cur_xfer) {
dev_err(mas->dev, "Can't set CS when prev xfer running\n");
spin_unlock_irq(&mas->lock); gotoexit;
}
mas->cs_flag = set_flag; /* set xfer_mode to FIFO to complete cs_done in isr */
mas->cur_xfer_mode = GENI_SE_FIFO;
geni_se_select_mode(se, mas->cur_xfer_mode);
ret = get_spi_clk_cfg(clk_hz, mas, &idx, &div); if (ret) {
dev_err(mas->dev, "Err setting clk to %lu: %d\n", clk_hz, ret); return ret;
}
/* * SPI core clock gets configured with the requested frequency * or the frequency closer to the requested frequency. * For that reason requested frequency is stored in the * cur_speed_hz and referred in the consecutive transfer instead * of calling clk_get_rate() API.
*/
mas->cur_speed_hz = clk_hz;
/* Set BW quota for CPU as driver supports FIFO mode only. */
se->icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(mas->cur_speed_hz);
ret = geni_icc_set_bw(se); if (ret) return ret;
ret = get_spi_clk_cfg(mas->cur_speed_hz, mas,
&peripheral.clk_src, &peripheral.clk_div); if (ret) {
dev_err(mas->dev, "Err in get_spi_clk_cfg() :%d\n", ret); return ret;
}
if (!xfer->cs_change) { if (!list_is_last(&xfer->transfer_list, &spi->cur_msg->transfers))
peripheral.fragmentation = FRAGMENTATION;
}
if (peripheral.cmd & SPI_RX) {
dmaengine_slave_config(mas->rx, &config);
rx_desc = dmaengine_prep_slave_sg(mas->rx, xfer->rx_sg.sgl, xfer->rx_sg.nents,
DMA_DEV_TO_MEM, flags); if (!rx_desc) {
dev_err(mas->dev, "Err setting up rx desc\n"); return -EIO;
}
}
/* * Prepare the TX always, even for RX or tx_buf being null, we would * need TX to be prepared per GSI spec
*/
dmaengine_slave_config(mas->tx, &config);
tx_desc = dmaengine_prep_slave_sg(mas->tx, xfer->tx_sg.sgl, xfer->tx_sg.nents,
DMA_MEM_TO_DEV, flags); if (!tx_desc) {
dev_err(mas->dev, "Err setting up tx desc\n"); return -EIO;
}
switch (mas->cur_xfer_mode) { case GENI_SE_FIFO: case GENI_SE_DMA: if (spi_geni_is_abort_still_pending(mas)) return -EBUSY;
ret = setup_fifo_params(spi_msg->spi, spi); if (ret)
dev_err(mas->dev, "Couldn't select mode %d\n", ret); return ret;
case GENI_GPI_DMA: /* nothing to do for GPI DMA */ return 0;
}
dev_err(mas->dev, "Mode not supported %d", mas->cur_xfer_mode); return -EINVAL;
}
if (mas->rx) {
dma_release_channel(mas->rx);
mas->rx = NULL;
}
if (mas->tx) {
dma_release_channel(mas->tx);
mas->tx = NULL;
}
}
staticint spi_geni_grab_gpi_chan(struct spi_geni_master *mas)
{ int ret;
mas->tx = dma_request_chan(mas->dev, "tx"); if (IS_ERR(mas->tx)) {
ret = dev_err_probe(mas->dev, PTR_ERR(mas->tx), "Failed to get tx DMA ch\n"); goto err_tx;
}
mas->rx = dma_request_chan(mas->dev, "rx"); if (IS_ERR(mas->rx)) {
ret = dev_err_probe(mas->dev, PTR_ERR(mas->rx), "Failed to get rx DMA ch\n"); goto err_rx;
}
ret = devm_add_action_or_reset(mas->dev, spi_geni_release_dma_chan, mas); if (ret) {
dev_err(mas->dev, "Unable to add action.\n"); return ret;
}
staticint spi_geni_init(struct spi_geni_master *mas)
{ struct spi_controller *spi = dev_get_drvdata(mas->dev); struct geni_se *se = &mas->se; unsignedint proto, major, minor, ver;
u32 spi_tx_cfg, fifo_disable; int ret = -ENXIO;
pm_runtime_get_sync(mas->dev);
proto = geni_se_read_proto(se);
if (spi->target) { if (proto != GENI_SE_SPI_SLAVE) {
dev_err(mas->dev, "Invalid proto %d\n", proto); goto out_pm;
}
spi_slv_setup(mas);
} elseif (proto != GENI_SE_SPI) {
dev_err(mas->dev, "Invalid proto %d\n", proto); goto out_pm;
}
mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
/* Width of Tx and Rx FIFO is same */
mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
/* * Hardware programming guide suggests to configure * RX FIFO RFR level to fifo_depth-2.
*/
geni_se_init(se, mas->tx_fifo_depth - 3, mas->tx_fifo_depth - 2); /* Transmit an entire FIFO worth of data per IRQ */
mas->tx_wm = 1;
ver = geni_se_get_qup_hw_version(se);
major = GENI_SE_VERSION_MAJOR(ver);
minor = GENI_SE_VERSION_MINOR(ver);
if (major == 1 && minor == 0)
mas->oversampling = 2; else
mas->oversampling = 1;
fifo_disable = readl(se->base + GENI_IF_DISABLE_RO) & FIFO_IF_DISABLE; switch (fifo_disable) { case 1:
ret = spi_geni_grab_gpi_chan(mas); if (!ret) { /* success case */
mas->cur_xfer_mode = GENI_GPI_DMA;
geni_se_select_mode(se, GENI_GPI_DMA);
dev_dbg(mas->dev, "Using GPI DMA mode for SPI\n"); break;
} elseif (ret == -EPROBE_DEFER) { goto out_pm;
} /* * in case of failure to get gpi dma channel, we can still do the * FIFO mode, so fallthrough
*/
dev_warn(mas->dev, "FIFO mode disabled, but couldn't get DMA, fall back to FIFO mode\n");
fallthrough;
case 0:
mas->cur_xfer_mode = GENI_SE_FIFO;
geni_se_select_mode(se, GENI_SE_FIFO);
ret = 0; break;
}
/* We always control CS manually */ if (!spi->target) {
spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
spi_tx_cfg &= ~CS_TOGGLE;
writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
}
out_pm:
pm_runtime_put(mas->dev); return ret;
}
staticunsignedint geni_byte_per_fifo_word(struct spi_geni_master *mas)
{ /* * Calculate how many bytes we'll put in each FIFO word. If the * transfer words don't pack cleanly into a FIFO word we'll just put * one transfer word in each FIFO word. If they do pack we'll pack 'em.
*/ if (mas->fifo_width_bits % mas->cur_bits_per_word) return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
BITS_PER_BYTE));
/* Clear out the FIFO and bail if nowhere to put it */ if (!mas->cur_xfer) { for (i = 0; i < DIV_ROUND_UP(rx_bytes, bytes_per_fifo_word); i++)
readl(se->base + SE_GENI_RX_FIFOn); return;
}
if (mas->rx_rem_bytes < rx_bytes)
rx_bytes = mas->rx_rem_bytes;
/* * Ensure that our interrupt handler isn't still running from some * prior command before we start messing with the hardware behind * its back. We don't need to _keep_ the lock here since we're only * worried about racing with out interrupt handler. The SPI core * already handles making sure that we're not trying to do two * transfers at once or setting a chip select and doing a transfer * concurrently. * * NOTE: we actually _can't_ hold the lock here because possibly we * might call clk_set_rate() which needs to be able to sleep.
*/
spin_lock_irq(&mas->lock);
spin_unlock_irq(&mas->lock);
/* * Select DMA mode if sgt are present; and with only 1 entry * This is not a serious limitation because the xfer buffers are * expected to fit into in 1 entry almost always, and if any * doesn't for any reason we fall back to FIFO mode anyway
*/ if (!xfer->tx_sg.nents && !xfer->rx_sg.nents)
mas->cur_xfer_mode = GENI_SE_FIFO; elseif (xfer->tx_sg.nents > 1 || xfer->rx_sg.nents > 1) {
dev_warn_once(mas->dev, "Doing FIFO, cannot handle tx_nents-%d, rx_nents-%d\n",
xfer->tx_sg.nents, xfer->rx_sg.nents);
mas->cur_xfer_mode = GENI_SE_FIFO;
} else
mas->cur_xfer_mode = GENI_SE_DMA;
geni_se_select_mode(se, mas->cur_xfer_mode);
/* * Lock around right before we start the transfer since our * interrupt could come in at any time now.
*/
spin_lock_irq(&mas->lock);
geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
if (mas->cur_xfer_mode == GENI_SE_DMA) { if (m_cmd & SPI_RX_ONLY)
geni_se_rx_init_dma(se, sg_dma_address(xfer->rx_sg.sgl),
sg_dma_len(xfer->rx_sg.sgl)); if (m_cmd & SPI_TX_ONLY)
geni_se_tx_init_dma(se, sg_dma_address(xfer->tx_sg.sgl),
sg_dma_len(xfer->tx_sg.sgl));
} elseif (m_cmd & SPI_TX_ONLY) { if (geni_spi_handle_tx(mas))
writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
}
if (mas->cur_xfer_mode == GENI_SE_FIFO) { if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
geni_spi_handle_rx(mas);
if (m_irq & M_TX_FIFO_WATERMARK_EN)
geni_spi_handle_tx(mas);
if (m_irq & M_CMD_DONE_EN) { if (mas->cur_xfer) {
spi_finalize_current_transfer(spi);
mas->cur_xfer = NULL; /* * If this happens, then a CMD_DONE came before all the * Tx buffer bytes were sent out. This is unusual, log * this condition and disable the WM interrupt to * prevent the system from stalling due an interrupt * storm. * * If this happens when all Rx bytes haven't been * received, log the condition. The only known time * this can happen is if bits_per_word != 8 and some * registers that expect xfer lengths in num spi_words * weren't written correctly.
*/ if (mas->tx_rem_bytes) {
writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
mas->tx_rem_bytes, mas->cur_bits_per_word);
} if (mas->rx_rem_bytes)
dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
mas->rx_rem_bytes, mas->cur_bits_per_word);
} else {
complete(&mas->cs_done);
}
}
} elseif (mas->cur_xfer_mode == GENI_SE_DMA) { conststruct spi_transfer *xfer = mas->cur_xfer;
u32 dma_tx_status = readl_relaxed(se->base + SE_DMA_TX_IRQ_STAT);
u32 dma_rx_status = readl_relaxed(se->base + SE_DMA_RX_IRQ_STAT);
if (dma_tx_status)
writel(dma_tx_status, se->base + SE_DMA_TX_IRQ_CLR); if (dma_rx_status)
writel(dma_rx_status, se->base + SE_DMA_RX_IRQ_CLR); if (dma_tx_status & TX_DMA_DONE)
mas->tx_rem_bytes = 0; if (dma_rx_status & RX_DMA_DONE)
mas->rx_rem_bytes = 0; if (dma_tx_status & TX_RESET_DONE)
complete(&mas->tx_reset_done); if (dma_rx_status & RX_RESET_DONE)
complete(&mas->rx_reset_done); if (!mas->tx_rem_bytes && !mas->rx_rem_bytes && xfer) {
spi_finalize_current_transfer(spi);
mas->cur_xfer = NULL;
}
}
if (m_irq & M_CMD_CANCEL_EN)
complete(&mas->cancel_done); if (m_irq & M_CMD_ABORT_EN)
complete(&mas->abort_done);
/* * It's safe or a good idea to Ack all of our interrupts at the end * of the function. Specifically: * - M_CMD_DONE_EN / M_RX_FIFO_LAST_EN: Edge triggered interrupts and * clearing Acks. Clearing at the end relies on nobody else having * started a new transfer yet or else we could be clearing _their_ * done bit, but everyone grabs the spinlock before starting a new * transfer. * - M_RX_FIFO_WATERMARK_EN / M_TX_FIFO_WATERMARK_EN: These appear * to be "latched level" interrupts so it's important to clear them * _after_ you've handled the condition and always safe to do so * since they'll re-assert if they're still happening.
*/
writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
ret = devm_pm_opp_set_clkname(&pdev->dev, "se"); if (ret) return ret; /* OPP table is optional */
ret = devm_pm_opp_of_add_table(&pdev->dev); if (ret && ret != -ENODEV) {
dev_err(&pdev->dev, "invalid OPP table in device tree\n"); return ret;
}
ret = geni_icc_get(&mas->se, NULL); if (ret) return ret;
pm_runtime_use_autosuspend(&pdev->dev);
pm_runtime_set_autosuspend_delay(&pdev->dev, 250);
ret = devm_pm_runtime_enable(dev); if (ret) return ret;
if (device_property_read_bool(&pdev->dev, "spi-slave"))
spi->target = true;
/* Set the bus quota to a reasonable value for register access */
mas->se.icc_paths[GENI_TO_CORE].avg_bw = Bps_to_icc(CORE_2X_50_MHZ);
mas->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
ret = geni_icc_set_bw(&mas->se); if (ret) return ret;
ret = spi_geni_init(mas); if (ret) return ret;
/* * check the mode supported and set_cs for fifo mode only * for dma (gsi) mode, the gsi will set cs based on params passed in * TRE
*/ if (!spi->target && mas->cur_xfer_mode == GENI_SE_FIFO)
spi->set_cs = spi_geni_set_cs;
/* * TX is required per GSI spec, see setup_gsi_xfer().
*/ if (mas->cur_xfer_mode == GENI_GPI_DMA)
spi->flags = SPI_CONTROLLER_MUST_TX;
ret = devm_request_irq(dev, mas->irq, geni_spi_isr, 0, dev_name(dev), spi); if (ret) return ret;
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.