2018-07-24 20:04:02 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
//
|
|
|
|
// Copyright 2013 Freescale Semiconductor, Inc.
|
|
|
|
//
|
|
|
|
// Freescale DSPI driver
|
|
|
|
// This file contains a driver for the Freescale DSPI
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2014-09-29 09:57:06 +07:00
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/delay.h>
|
2016-11-10 19:19:15 +07:00
|
|
|
#include <linux/dmaengine.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
2014-09-29 09:57:06 +07:00
|
|
|
#include <linux/interrupt.h>
|
2013-08-16 10:08:55 +07:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2014-09-29 09:57:06 +07:00
|
|
|
#include <linux/of_device.h>
|
2015-06-12 23:55:22 +07:00
|
|
|
#include <linux/pinctrl/consumer.h>
|
2014-02-12 14:29:05 +07:00
|
|
|
#include <linux/regmap.h>
|
2013-08-16 10:08:55 +07:00
|
|
|
#include <linux/spi/spi.h>
|
2017-10-28 05:23:01 +07:00
|
|
|
#include <linux/spi/spi-fsl-dspi.h>
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-08-19 01:01:02 +07:00
|
|
|
#define DRIVER_NAME "fsl-dspi"
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2018-08-18 06:51:58 +07:00
|
|
|
#ifdef CONFIG_M5441x
|
|
|
|
#define DSPI_FIFO_SIZE 16
|
|
|
|
#else
|
2013-08-16 10:08:55 +07:00
|
|
|
#define DSPI_FIFO_SIZE 4
|
2018-08-18 06:51:58 +07:00
|
|
|
#endif
|
2016-11-10 19:19:15 +07:00
|
|
|
#define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-08-19 01:01:02 +07:00
|
|
|
#define SPI_MCR 0x00
|
2019-08-19 01:01:04 +07:00
|
|
|
#define SPI_MCR_MASTER BIT(31)
|
2019-08-19 01:01:02 +07:00
|
|
|
#define SPI_MCR_PCSIS (0x3F << 16)
|
2019-08-19 01:01:04 +07:00
|
|
|
#define SPI_MCR_CLR_TXF BIT(11)
|
|
|
|
#define SPI_MCR_CLR_RXF BIT(10)
|
|
|
|
#define SPI_MCR_XSPI BIT(3)
|
2019-08-19 01:01:02 +07:00
|
|
|
|
|
|
|
#define SPI_TCR 0x08
|
2019-08-19 01:01:04 +07:00
|
|
|
#define SPI_TCR_GET_TCNT(x) (((x) & GENMASK(31, 16)) >> 16)
|
|
|
|
|
|
|
|
#define SPI_CTAR(x) (0x0c + (((x) & GENMASK(1, 0)) * 4))
|
|
|
|
#define SPI_CTAR_FMSZ(x) (((x) << 27) & GENMASK(30, 27))
|
2019-08-19 01:01:06 +07:00
|
|
|
#define SPI_CTAR_CPOL BIT(26)
|
|
|
|
#define SPI_CTAR_CPHA BIT(25)
|
|
|
|
#define SPI_CTAR_LSBFE BIT(24)
|
2019-08-19 01:01:04 +07:00
|
|
|
#define SPI_CTAR_PCSSCK(x) (((x) << 22) & GENMASK(23, 22))
|
|
|
|
#define SPI_CTAR_PASC(x) (((x) << 20) & GENMASK(21, 20))
|
|
|
|
#define SPI_CTAR_PDT(x) (((x) << 18) & GENMASK(19, 18))
|
|
|
|
#define SPI_CTAR_PBR(x) (((x) << 16) & GENMASK(17, 16))
|
|
|
|
#define SPI_CTAR_CSSCK(x) (((x) << 12) & GENMASK(15, 12))
|
|
|
|
#define SPI_CTAR_ASC(x) (((x) << 8) & GENMASK(11, 8))
|
|
|
|
#define SPI_CTAR_DT(x) (((x) << 4) & GENMASK(7, 4))
|
|
|
|
#define SPI_CTAR_BR(x) ((x) & GENMASK(3, 0))
|
2019-08-19 01:01:02 +07:00
|
|
|
#define SPI_CTAR_SCALE_BITS 0xf
|
|
|
|
|
|
|
|
#define SPI_CTAR0_SLAVE 0x0c
|
|
|
|
|
|
|
|
#define SPI_SR 0x2c
|
2019-08-19 01:01:04 +07:00
|
|
|
#define SPI_SR_TCFQF BIT(31)
|
|
|
|
#define SPI_SR_EOQF BIT(28)
|
2019-08-19 01:01:05 +07:00
|
|
|
#define SPI_SR_TFUF BIT(27)
|
|
|
|
#define SPI_SR_TFFF BIT(25)
|
|
|
|
#define SPI_SR_CMDTCF BIT(23)
|
|
|
|
#define SPI_SR_SPEF BIT(21)
|
|
|
|
#define SPI_SR_RFOF BIT(19)
|
|
|
|
#define SPI_SR_TFIWF BIT(18)
|
|
|
|
#define SPI_SR_RFDF BIT(17)
|
|
|
|
#define SPI_SR_CMDFFF BIT(16)
|
|
|
|
#define SPI_SR_CLEAR (SPI_SR_TCFQF | SPI_SR_EOQF | \
|
|
|
|
SPI_SR_TFUF | SPI_SR_TFFF | \
|
|
|
|
SPI_SR_CMDTCF | SPI_SR_SPEF | \
|
|
|
|
SPI_SR_RFOF | SPI_SR_TFIWF | \
|
|
|
|
SPI_SR_RFDF | SPI_SR_CMDFFF)
|
2019-08-19 01:01:02 +07:00
|
|
|
|
|
|
|
#define SPI_RSER_TFFFE BIT(25)
|
|
|
|
#define SPI_RSER_TFFFD BIT(24)
|
|
|
|
#define SPI_RSER_RFDFE BIT(17)
|
|
|
|
#define SPI_RSER_RFDFD BIT(16)
|
|
|
|
|
|
|
|
#define SPI_RSER 0x30
|
2019-08-19 01:01:04 +07:00
|
|
|
#define SPI_RSER_TCFQE BIT(31)
|
|
|
|
#define SPI_RSER_EOQFE BIT(28)
|
2019-08-19 01:01:02 +07:00
|
|
|
|
|
|
|
#define SPI_PUSHR 0x34
|
2019-08-19 01:01:04 +07:00
|
|
|
#define SPI_PUSHR_CMD_CONT BIT(15)
|
|
|
|
#define SPI_PUSHR_CMD_CTAS(x) (((x) << 12 & GENMASK(14, 12)))
|
|
|
|
#define SPI_PUSHR_CMD_EOQ BIT(11)
|
|
|
|
#define SPI_PUSHR_CMD_CTCNT BIT(10)
|
|
|
|
#define SPI_PUSHR_CMD_PCS(x) (BIT(x) & GENMASK(5, 0))
|
2019-08-19 01:01:02 +07:00
|
|
|
|
|
|
|
#define SPI_PUSHR_SLAVE 0x34
|
|
|
|
|
|
|
|
#define SPI_POPR 0x38
|
|
|
|
|
|
|
|
#define SPI_TXFR0 0x3c
|
|
|
|
#define SPI_TXFR1 0x40
|
|
|
|
#define SPI_TXFR2 0x44
|
|
|
|
#define SPI_TXFR3 0x48
|
|
|
|
#define SPI_RXFR0 0x7c
|
|
|
|
#define SPI_RXFR1 0x80
|
|
|
|
#define SPI_RXFR2 0x84
|
|
|
|
#define SPI_RXFR3 0x88
|
|
|
|
|
2019-08-19 01:01:04 +07:00
|
|
|
#define SPI_CTARE(x) (0x11c + (((x) & GENMASK(1, 0)) * 4))
|
2019-08-19 01:01:02 +07:00
|
|
|
#define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16)
|
|
|
|
#define SPI_CTARE_DTCP(x) ((x) & 0x7ff)
|
|
|
|
|
|
|
|
#define SPI_SREX 0x13c
|
|
|
|
|
|
|
|
#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
|
|
|
|
#define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4)
|
2018-06-20 14:34:39 +07:00
|
|
|
|
2018-06-20 14:34:38 +07:00
|
|
|
/* Register offsets for regmap_pushr */
|
2019-08-19 01:01:02 +07:00
|
|
|
#define PUSHR_CMD 0x0
|
|
|
|
#define PUSHR_TX 0x2
|
2018-06-20 14:34:38 +07:00
|
|
|
|
2019-08-19 01:01:02 +07:00
|
|
|
#define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
|
2016-11-10 19:19:15 +07:00
|
|
|
|
2013-08-16 10:08:55 +07:00
|
|
|
struct chip_data {
|
2019-08-19 01:01:02 +07:00
|
|
|
u32 ctar_val;
|
|
|
|
u16 void_write_data;
|
2013-08-16 10:08:55 +07:00
|
|
|
};
|
|
|
|
|
2015-06-09 18:45:27 +07:00
|
|
|
enum dspi_trans_mode {
|
|
|
|
DSPI_EOQ_MODE = 0,
|
|
|
|
DSPI_TCFQ_MODE,
|
2016-11-10 19:19:15 +07:00
|
|
|
DSPI_DMA_MODE,
|
2015-06-09 18:45:27 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct fsl_dspi_devtype_data {
|
2019-08-19 01:01:02 +07:00
|
|
|
enum dspi_trans_mode trans_mode;
|
|
|
|
u8 max_clock_factor;
|
2019-09-05 08:01:13 +07:00
|
|
|
bool ptp_sts_supported;
|
2019-08-19 01:01:02 +07:00
|
|
|
bool xspi_mode;
|
2015-06-09 18:45:27 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fsl_dspi_devtype_data vf610_data = {
|
2019-08-19 01:01:02 +07:00
|
|
|
.trans_mode = DSPI_DMA_MODE,
|
|
|
|
.max_clock_factor = 2,
|
2015-06-09 18:45:27 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
|
2019-08-19 01:01:02 +07:00
|
|
|
.trans_mode = DSPI_TCFQ_MODE,
|
|
|
|
.max_clock_factor = 8,
|
2019-09-05 08:01:13 +07:00
|
|
|
.ptp_sts_supported = true,
|
2019-08-19 01:01:02 +07:00
|
|
|
.xspi_mode = true,
|
2015-06-09 18:45:27 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fsl_dspi_devtype_data ls2085a_data = {
|
2019-08-19 01:01:02 +07:00
|
|
|
.trans_mode = DSPI_TCFQ_MODE,
|
|
|
|
.max_clock_factor = 8,
|
2019-09-05 08:01:13 +07:00
|
|
|
.ptp_sts_supported = true,
|
2015-06-09 18:45:27 +07:00
|
|
|
};
|
|
|
|
|
2017-10-28 05:23:01 +07:00
|
|
|
static const struct fsl_dspi_devtype_data coldfire_data = {
|
2019-08-19 01:01:02 +07:00
|
|
|
.trans_mode = DSPI_EOQ_MODE,
|
|
|
|
.max_clock_factor = 8,
|
2017-10-28 05:23:01 +07:00
|
|
|
};
|
|
|
|
|
2016-11-10 19:19:15 +07:00
|
|
|
struct fsl_dspi_dma {
|
2016-11-22 14:01:30 +07:00
|
|
|
/* Length of transfer in words of DSPI_FIFO_SIZE */
|
2019-08-19 01:01:02 +07:00
|
|
|
u32 curr_xfer_len;
|
|
|
|
|
|
|
|
u32 *tx_dma_buf;
|
|
|
|
struct dma_chan *chan_tx;
|
|
|
|
dma_addr_t tx_dma_phys;
|
|
|
|
struct completion cmd_tx_complete;
|
|
|
|
struct dma_async_tx_descriptor *tx_desc;
|
|
|
|
|
|
|
|
u32 *rx_dma_buf;
|
|
|
|
struct dma_chan *chan_rx;
|
|
|
|
dma_addr_t rx_dma_phys;
|
|
|
|
struct completion cmd_rx_complete;
|
|
|
|
struct dma_async_tx_descriptor *rx_desc;
|
2016-11-10 19:19:15 +07:00
|
|
|
};
|
|
|
|
|
2013-08-16 10:08:55 +07:00
|
|
|
struct fsl_dspi {
|
2019-08-19 01:01:10 +07:00
|
|
|
struct spi_controller *ctlr;
|
2019-08-19 01:01:02 +07:00
|
|
|
struct platform_device *pdev;
|
|
|
|
|
|
|
|
struct regmap *regmap;
|
|
|
|
struct regmap *regmap_pushr;
|
|
|
|
int irq;
|
|
|
|
struct clk *clk;
|
|
|
|
|
|
|
|
struct spi_transfer *cur_transfer;
|
|
|
|
struct spi_message *cur_msg;
|
|
|
|
struct chip_data *cur_chip;
|
2019-12-27 08:24:17 +07:00
|
|
|
size_t progress;
|
2019-08-19 01:01:02 +07:00
|
|
|
size_t len;
|
|
|
|
const void *tx;
|
|
|
|
void *rx;
|
|
|
|
void *rx_end;
|
|
|
|
u16 void_write_data;
|
|
|
|
u16 tx_cmd;
|
|
|
|
u8 bits_per_word;
|
|
|
|
u8 bytes_per_word;
|
|
|
|
const struct fsl_dspi_devtype_data *devtype_data;
|
|
|
|
|
|
|
|
wait_queue_head_t waitq;
|
|
|
|
u32 waitflags;
|
|
|
|
|
|
|
|
struct fsl_dspi_dma *dma;
|
2013-08-16 10:08:55 +07:00
|
|
|
};
|
|
|
|
|
2018-06-20 14:34:40 +07:00
|
|
|
static u32 dspi_pop_tx(struct fsl_dspi *dspi)
|
2018-06-20 14:34:35 +07:00
|
|
|
{
|
2018-06-20 14:34:40 +07:00
|
|
|
u32 txdata = 0;
|
2018-06-20 14:34:35 +07:00
|
|
|
|
|
|
|
if (dspi->tx) {
|
|
|
|
if (dspi->bytes_per_word == 1)
|
|
|
|
txdata = *(u8 *)dspi->tx;
|
2018-06-20 14:34:40 +07:00
|
|
|
else if (dspi->bytes_per_word == 2)
|
2018-06-20 14:34:35 +07:00
|
|
|
txdata = *(u16 *)dspi->tx;
|
2018-06-20 14:34:40 +07:00
|
|
|
else /* dspi->bytes_per_word == 4 */
|
|
|
|
txdata = *(u32 *)dspi->tx;
|
2018-06-20 14:34:35 +07:00
|
|
|
dspi->tx += dspi->bytes_per_word;
|
|
|
|
}
|
|
|
|
dspi->len -= dspi->bytes_per_word;
|
|
|
|
return txdata;
|
|
|
|
}
|
2016-11-22 14:01:31 +07:00
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi)
|
2013-08-16 10:08:55 +07:00
|
|
|
{
|
2018-06-20 14:34:35 +07:00
|
|
|
u16 cmd = dspi->tx_cmd, data = dspi_pop_tx(dspi);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
if (spi_controller_is_slave(dspi->ctlr))
|
2019-02-06 05:13:49 +07:00
|
|
|
return data;
|
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
if (dspi->len > 0)
|
|
|
|
cmd |= SPI_PUSHR_CMD_CONT;
|
|
|
|
return cmd << 16 | data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dspi_push_rx(struct fsl_dspi *dspi, u32 rxdata)
|
|
|
|
{
|
|
|
|
if (!dspi->rx)
|
|
|
|
return;
|
|
|
|
|
2019-08-19 01:01:12 +07:00
|
|
|
/* Mask off undefined bits */
|
2018-06-20 14:34:35 +07:00
|
|
|
rxdata &= (1 << dspi->bits_per_word) - 1;
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
if (dspi->bytes_per_word == 1)
|
|
|
|
*(u8 *)dspi->rx = rxdata;
|
2018-06-20 14:34:40 +07:00
|
|
|
else if (dspi->bytes_per_word == 2)
|
2018-06-20 14:34:35 +07:00
|
|
|
*(u16 *)dspi->rx = rxdata;
|
2018-06-20 14:34:40 +07:00
|
|
|
else /* dspi->bytes_per_word == 4 */
|
|
|
|
*(u32 *)dspi->rx = rxdata;
|
2018-06-20 14:34:35 +07:00
|
|
|
dspi->rx += dspi->bytes_per_word;
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:19:15 +07:00
|
|
|
static void dspi_tx_dma_callback(void *arg)
|
|
|
|
{
|
|
|
|
struct fsl_dspi *dspi = arg;
|
|
|
|
struct fsl_dspi_dma *dma = dspi->dma;
|
|
|
|
|
|
|
|
complete(&dma->cmd_tx_complete);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dspi_rx_dma_callback(void *arg)
|
|
|
|
{
|
|
|
|
struct fsl_dspi *dspi = arg;
|
|
|
|
struct fsl_dspi_dma *dma = dspi->dma;
|
2016-11-22 14:01:30 +07:00
|
|
|
int i;
|
2016-11-10 19:19:15 +07:00
|
|
|
|
2018-06-20 14:34:32 +07:00
|
|
|
if (dspi->rx) {
|
2018-06-20 14:34:35 +07:00
|
|
|
for (i = 0; i < dma->curr_xfer_len; i++)
|
|
|
|
dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
|
2016-11-10 19:19:15 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
complete(&dma->cmd_rx_complete);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
|
|
|
|
{
|
|
|
|
struct device *dev = &dspi->pdev->dev;
|
2019-08-19 01:01:11 +07:00
|
|
|
struct fsl_dspi_dma *dma = dspi->dma;
|
2016-11-10 19:19:15 +07:00
|
|
|
int time_left;
|
2016-11-22 14:01:30 +07:00
|
|
|
int i;
|
2016-11-10 19:19:15 +07:00
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
for (i = 0; i < dma->curr_xfer_len; i++)
|
|
|
|
dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);
|
2016-11-10 19:19:15 +07:00
|
|
|
|
|
|
|
dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
|
|
|
|
dma->tx_dma_phys,
|
2016-11-22 14:01:30 +07:00
|
|
|
dma->curr_xfer_len *
|
|
|
|
DMA_SLAVE_BUSWIDTH_4_BYTES,
|
|
|
|
DMA_MEM_TO_DEV,
|
2016-11-10 19:19:15 +07:00
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!dma->tx_desc) {
|
|
|
|
dev_err(dev, "Not able to get desc for DMA xfer\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma->tx_desc->callback = dspi_tx_dma_callback;
|
|
|
|
dma->tx_desc->callback_param = dspi;
|
|
|
|
if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
|
|
|
|
dev_err(dev, "DMA submit failed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
|
|
|
|
dma->rx_dma_phys,
|
2016-11-22 14:01:30 +07:00
|
|
|
dma->curr_xfer_len *
|
|
|
|
DMA_SLAVE_BUSWIDTH_4_BYTES,
|
|
|
|
DMA_DEV_TO_MEM,
|
2016-11-10 19:19:15 +07:00
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!dma->rx_desc) {
|
|
|
|
dev_err(dev, "Not able to get desc for DMA xfer\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma->rx_desc->callback = dspi_rx_dma_callback;
|
|
|
|
dma->rx_desc->callback_param = dspi;
|
|
|
|
if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
|
|
|
|
dev_err(dev, "DMA submit failed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
reinit_completion(&dspi->dma->cmd_rx_complete);
|
|
|
|
reinit_completion(&dspi->dma->cmd_tx_complete);
|
|
|
|
|
|
|
|
dma_async_issue_pending(dma->chan_rx);
|
|
|
|
dma_async_issue_pending(dma->chan_tx);
|
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
if (spi_controller_is_slave(dspi->ctlr)) {
|
2019-02-06 05:13:49 +07:00
|
|
|
wait_for_completion_interruptible(&dspi->dma->cmd_rx_complete);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-10 19:19:15 +07:00
|
|
|
time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
|
2019-08-19 01:01:02 +07:00
|
|
|
DMA_COMPLETION_TIMEOUT);
|
2016-11-10 19:19:15 +07:00
|
|
|
if (time_left == 0) {
|
|
|
|
dev_err(dev, "DMA tx timeout\n");
|
|
|
|
dmaengine_terminate_all(dma->chan_tx);
|
|
|
|
dmaengine_terminate_all(dma->chan_rx);
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
|
2019-08-19 01:01:02 +07:00
|
|
|
DMA_COMPLETION_TIMEOUT);
|
2016-11-10 19:19:15 +07:00
|
|
|
if (time_left == 0) {
|
|
|
|
dev_err(dev, "DMA rx timeout\n");
|
|
|
|
dmaengine_terminate_all(dma->chan_tx);
|
|
|
|
dmaengine_terminate_all(dma->chan_rx);
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dspi_dma_xfer(struct fsl_dspi *dspi)
|
|
|
|
{
|
2018-07-17 11:33:29 +07:00
|
|
|
struct spi_message *message = dspi->cur_msg;
|
2019-08-19 01:01:11 +07:00
|
|
|
struct device *dev = &dspi->pdev->dev;
|
|
|
|
struct fsl_dspi_dma *dma = dspi->dma;
|
2016-11-10 19:19:15 +07:00
|
|
|
int curr_remaining_bytes;
|
|
|
|
int bytes_per_buffer;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
curr_remaining_bytes = dspi->len;
|
2016-11-22 14:01:30 +07:00
|
|
|
bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
|
2016-11-10 19:19:15 +07:00
|
|
|
while (curr_remaining_bytes) {
|
|
|
|
/* Check if current transfer fits the DMA buffer */
|
2018-06-20 14:34:35 +07:00
|
|
|
dma->curr_xfer_len = curr_remaining_bytes
|
|
|
|
/ dspi->bytes_per_word;
|
2016-11-22 14:01:30 +07:00
|
|
|
if (dma->curr_xfer_len > bytes_per_buffer)
|
2016-11-10 19:19:15 +07:00
|
|
|
dma->curr_xfer_len = bytes_per_buffer;
|
|
|
|
|
|
|
|
ret = dspi_next_xfer_dma_submit(dspi);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "DMA transfer failed\n");
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
} else {
|
2018-07-17 11:33:29 +07:00
|
|
|
const int len =
|
|
|
|
dma->curr_xfer_len * dspi->bytes_per_word;
|
|
|
|
curr_remaining_bytes -= len;
|
|
|
|
message->actual_length += len;
|
2016-11-10 19:19:15 +07:00
|
|
|
if (curr_remaining_bytes < 0)
|
|
|
|
curr_remaining_bytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
|
|
|
|
{
|
|
|
|
struct device *dev = &dspi->pdev->dev;
|
2019-08-19 01:01:11 +07:00
|
|
|
struct dma_slave_config cfg;
|
|
|
|
struct fsl_dspi_dma *dma;
|
2016-11-10 19:19:15 +07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
|
|
|
|
if (!dma)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-12-12 20:55:48 +07:00
|
|
|
dma->chan_rx = dma_request_chan(dev, "rx");
|
|
|
|
if (IS_ERR(dma->chan_rx)) {
|
2016-11-10 19:19:15 +07:00
|
|
|
dev_err(dev, "rx dma channel not available\n");
|
2019-12-12 20:55:48 +07:00
|
|
|
ret = PTR_ERR(dma->chan_rx);
|
2016-11-10 19:19:15 +07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-12 20:55:48 +07:00
|
|
|
dma->chan_tx = dma_request_chan(dev, "tx");
|
|
|
|
if (IS_ERR(dma->chan_tx)) {
|
2016-11-10 19:19:15 +07:00
|
|
|
dev_err(dev, "tx dma channel not available\n");
|
2019-12-12 20:55:48 +07:00
|
|
|
ret = PTR_ERR(dma->chan_tx);
|
2016-11-10 19:19:15 +07:00
|
|
|
goto err_tx_channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma->tx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
|
2019-08-19 01:01:02 +07:00
|
|
|
&dma->tx_dma_phys, GFP_KERNEL);
|
2016-11-10 19:19:15 +07:00
|
|
|
if (!dma->tx_dma_buf) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_tx_dma_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma->rx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
|
2019-08-19 01:01:02 +07:00
|
|
|
&dma->rx_dma_phys, GFP_KERNEL);
|
2016-11-10 19:19:15 +07:00
|
|
|
if (!dma->rx_dma_buf) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_rx_dma_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.src_addr = phy_addr + SPI_POPR;
|
|
|
|
cfg.dst_addr = phy_addr + SPI_PUSHR;
|
|
|
|
cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
|
|
|
|
cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
|
|
|
|
cfg.src_maxburst = 1;
|
|
|
|
cfg.dst_maxburst = 1;
|
|
|
|
|
|
|
|
cfg.direction = DMA_DEV_TO_MEM;
|
|
|
|
ret = dmaengine_slave_config(dma->chan_rx, &cfg);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "can't configure rx dma channel\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_slave_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.direction = DMA_MEM_TO_DEV;
|
|
|
|
ret = dmaengine_slave_config(dma->chan_tx, &cfg);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "can't configure tx dma channel\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_slave_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
dspi->dma = dma;
|
|
|
|
init_completion(&dma->cmd_tx_complete);
|
|
|
|
init_completion(&dma->cmd_rx_complete);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_slave_config:
|
2016-11-22 14:01:32 +07:00
|
|
|
dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
|
|
|
|
dma->rx_dma_buf, dma->rx_dma_phys);
|
2016-11-10 19:19:15 +07:00
|
|
|
err_rx_dma_buf:
|
2016-11-22 14:01:32 +07:00
|
|
|
dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
|
|
|
|
dma->tx_dma_buf, dma->tx_dma_phys);
|
2016-11-10 19:19:15 +07:00
|
|
|
err_tx_dma_buf:
|
|
|
|
dma_release_channel(dma->chan_tx);
|
|
|
|
err_tx_channel:
|
|
|
|
dma_release_channel(dma->chan_rx);
|
|
|
|
|
|
|
|
devm_kfree(dev, dma);
|
|
|
|
dspi->dma = NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dspi_release_dma(struct fsl_dspi *dspi)
|
|
|
|
{
|
|
|
|
struct fsl_dspi_dma *dma = dspi->dma;
|
|
|
|
struct device *dev = &dspi->pdev->dev;
|
|
|
|
|
2019-08-19 01:01:07 +07:00
|
|
|
if (!dma)
|
|
|
|
return;
|
2016-11-10 19:19:15 +07:00
|
|
|
|
2019-08-19 01:01:07 +07:00
|
|
|
if (dma->chan_tx) {
|
|
|
|
dma_unmap_single(dev, dma->tx_dma_phys,
|
|
|
|
DSPI_DMA_BUFSIZE, DMA_TO_DEVICE);
|
|
|
|
dma_release_channel(dma->chan_tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dma->chan_rx) {
|
|
|
|
dma_unmap_single(dev, dma->rx_dma_phys,
|
|
|
|
DSPI_DMA_BUFSIZE, DMA_FROM_DEVICE);
|
|
|
|
dma_release_channel(dma->chan_rx);
|
2016-11-10 19:19:15 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-16 10:08:55 +07:00
|
|
|
static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
|
2019-08-19 01:01:02 +07:00
|
|
|
unsigned long clkrate)
|
2013-08-16 10:08:55 +07:00
|
|
|
{
|
|
|
|
/* Valid baud rate pre-scaler values */
|
|
|
|
int pbr_tbl[4] = {2, 3, 5, 7};
|
|
|
|
int brs[16] = { 2, 4, 6, 8,
|
2019-08-19 01:01:02 +07:00
|
|
|
16, 32, 64, 128,
|
|
|
|
256, 512, 1024, 2048,
|
|
|
|
4096, 8192, 16384, 32768 };
|
spi: fsl-dspi: Fix clock rate scale values
Previous algorithm had an outer loop with the values {2,3,5,7} and an
inner loop with {2,4,6,8,16,32,...,32768}, and would pick the first
value over the required scaling value (where the total scale was the two
numbers multiplied).
Since the inner loop went up to 32768 it would always pick a value of 2
for PBR and a much higher than necessary value for BR. The desired
scale factor was being divided by two I believe to compensate for the
much higher scale factors (the divide by two not specified in the
reference manual).
Updated to check all values and find the smallest scale factor possible
without going over the desired clock rate.
Signed-off-by: Aaron Brice <aaron.brice@datasoft.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
2015-03-31 00:49:15 +07:00
|
|
|
int scale_needed, scale, minscale = INT_MAX;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
scale_needed = clkrate / speed_hz;
|
spi: fsl-dspi: Fix clock rate scale values
Previous algorithm had an outer loop with the values {2,3,5,7} and an
inner loop with {2,4,6,8,16,32,...,32768}, and would pick the first
value over the required scaling value (where the total scale was the two
numbers multiplied).
Since the inner loop went up to 32768 it would always pick a value of 2
for PBR and a much higher than necessary value for BR. The desired
scale factor was being divided by two I believe to compensate for the
much higher scale factors (the divide by two not specified in the
reference manual).
Updated to check all values and find the smallest scale factor possible
without going over the desired clock rate.
Signed-off-by: Aaron Brice <aaron.brice@datasoft.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
2015-04-04 03:39:29 +07:00
|
|
|
if (clkrate % speed_hz)
|
|
|
|
scale_needed++;
|
spi: fsl-dspi: Fix clock rate scale values
Previous algorithm had an outer loop with the values {2,3,5,7} and an
inner loop with {2,4,6,8,16,32,...,32768}, and would pick the first
value over the required scaling value (where the total scale was the two
numbers multiplied).
Since the inner loop went up to 32768 it would always pick a value of 2
for PBR and a much higher than necessary value for BR. The desired
scale factor was being divided by two I believe to compensate for the
much higher scale factors (the divide by two not specified in the
reference manual).
Updated to check all values and find the smallest scale factor possible
without going over the desired clock rate.
Signed-off-by: Aaron Brice <aaron.brice@datasoft.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
2015-03-31 00:49:15 +07:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(brs); i++)
|
|
|
|
for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
|
|
|
|
scale = brs[i] * pbr_tbl[j];
|
|
|
|
if (scale >= scale_needed) {
|
|
|
|
if (scale < minscale) {
|
|
|
|
minscale = scale;
|
|
|
|
*br = i;
|
|
|
|
*pbr = j;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
spi: fsl-dspi: Fix clock rate scale values
Previous algorithm had an outer loop with the values {2,3,5,7} and an
inner loop with {2,4,6,8,16,32,...,32768}, and would pick the first
value over the required scaling value (where the total scale was the two
numbers multiplied).
Since the inner loop went up to 32768 it would always pick a value of 2
for PBR and a much higher than necessary value for BR. The desired
scale factor was being divided by two I believe to compensate for the
much higher scale factors (the divide by two not specified in the
reference manual).
Updated to check all values and find the smallest scale factor possible
without going over the desired clock rate.
Signed-off-by: Aaron Brice <aaron.brice@datasoft.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
2015-03-31 00:49:15 +07:00
|
|
|
if (minscale == INT_MAX) {
|
|
|
|
pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
|
|
|
|
speed_hz, clkrate);
|
|
|
|
*pbr = ARRAY_SIZE(pbr_tbl) - 1;
|
|
|
|
*br = ARRAY_SIZE(brs) - 1;
|
|
|
|
}
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2015-04-04 03:39:31 +07:00
|
|
|
static void ns_delay_scale(char *psc, char *sc, int delay_ns,
|
2019-08-19 01:01:02 +07:00
|
|
|
unsigned long clkrate)
|
2015-04-04 03:39:31 +07:00
|
|
|
{
|
|
|
|
int scale_needed, scale, minscale = INT_MAX;
|
2019-08-19 01:01:11 +07:00
|
|
|
int pscale_tbl[4] = {1, 3, 5, 7};
|
2015-04-04 03:39:31 +07:00
|
|
|
u32 remainder;
|
2019-08-19 01:01:11 +07:00
|
|
|
int i, j;
|
2015-04-04 03:39:31 +07:00
|
|
|
|
|
|
|
scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
|
2019-08-19 01:01:02 +07:00
|
|
|
&remainder);
|
2015-04-04 03:39:31 +07:00
|
|
|
if (remainder)
|
|
|
|
scale_needed++;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
|
|
|
|
for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
|
|
|
|
scale = pscale_tbl[i] * (2 << j);
|
|
|
|
if (scale >= scale_needed) {
|
|
|
|
if (scale < minscale) {
|
|
|
|
minscale = scale;
|
|
|
|
*psc = i;
|
|
|
|
*sc = j;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 03:39:31 +07:00
|
|
|
if (minscale == INT_MAX) {
|
|
|
|
pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
|
|
|
|
delay_ns, clkrate);
|
|
|
|
*psc = ARRAY_SIZE(pscale_tbl) - 1;
|
|
|
|
*sc = SPI_CTAR_SCALE_BITS;
|
|
|
|
}
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
static void fifo_write(struct fsl_dspi *dspi)
|
2013-08-16 10:08:55 +07:00
|
|
|
{
|
2018-06-20 14:34:35 +07:00
|
|
|
regmap_write(dspi->regmap, SPI_PUSHR, dspi_pop_tx_pushr(dspi));
|
2015-06-09 18:45:27 +07:00
|
|
|
}
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2018-06-20 14:34:40 +07:00
|
|
|
static void cmd_fifo_write(struct fsl_dspi *dspi)
|
|
|
|
{
|
|
|
|
u16 cmd = dspi->tx_cmd;
|
|
|
|
|
|
|
|
if (dspi->len > 0)
|
|
|
|
cmd |= SPI_PUSHR_CMD_CONT;
|
|
|
|
regmap_write(dspi->regmap_pushr, PUSHR_CMD, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tx_fifo_write(struct fsl_dspi *dspi, u16 txdata)
|
|
|
|
{
|
|
|
|
regmap_write(dspi->regmap_pushr, PUSHR_TX, txdata);
|
|
|
|
}
|
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
static void dspi_tcfq_write(struct fsl_dspi *dspi)
|
2015-06-09 18:45:27 +07:00
|
|
|
{
|
2018-06-20 14:34:35 +07:00
|
|
|
/* Clear transfer count */
|
|
|
|
dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
|
2018-06-20 14:34:40 +07:00
|
|
|
|
|
|
|
if (dspi->devtype_data->xspi_mode && dspi->bits_per_word > 16) {
|
2019-12-28 20:55:36 +07:00
|
|
|
/* Write the CMD FIFO entry first, and then the two
|
|
|
|
* corresponding TX FIFO entries.
|
2018-06-20 14:34:40 +07:00
|
|
|
*/
|
|
|
|
u32 data = dspi_pop_tx(dspi);
|
|
|
|
|
|
|
|
cmd_fifo_write(dspi);
|
2019-12-28 20:55:36 +07:00
|
|
|
tx_fifo_write(dspi, data & 0xFFFF);
|
|
|
|
tx_fifo_write(dspi, data >> 16);
|
2018-06-20 14:34:40 +07:00
|
|
|
} else {
|
|
|
|
/* Write one entry to both TX FIFO and CMD FIFO
|
|
|
|
* simultaneously.
|
|
|
|
*/
|
|
|
|
fifo_write(dspi);
|
|
|
|
}
|
2015-06-09 18:45:27 +07:00
|
|
|
}
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
static u32 fifo_read(struct fsl_dspi *dspi)
|
2015-06-09 18:45:27 +07:00
|
|
|
{
|
2018-06-20 14:34:35 +07:00
|
|
|
u32 rxdata = 0;
|
2015-06-09 18:45:27 +07:00
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
regmap_read(dspi->regmap, SPI_POPR, &rxdata);
|
|
|
|
return rxdata;
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
static void dspi_tcfq_read(struct fsl_dspi *dspi)
|
2013-08-16 10:08:55 +07:00
|
|
|
{
|
2018-06-20 14:34:35 +07:00
|
|
|
dspi_push_rx(dspi, fifo_read(dspi));
|
2015-06-09 18:45:27 +07:00
|
|
|
}
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
static void dspi_eoq_write(struct fsl_dspi *dspi)
|
2015-06-09 18:45:27 +07:00
|
|
|
{
|
2018-06-20 14:34:35 +07:00
|
|
|
int fifo_size = DSPI_FIFO_SIZE;
|
2018-08-18 06:51:58 +07:00
|
|
|
u16 xfer_cmd = dspi->tx_cmd;
|
2018-06-20 14:34:35 +07:00
|
|
|
|
|
|
|
/* Fill TX FIFO with as many transfers as possible */
|
|
|
|
while (dspi->len && fifo_size--) {
|
2018-08-18 06:51:58 +07:00
|
|
|
dspi->tx_cmd = xfer_cmd;
|
2018-06-20 14:34:35 +07:00
|
|
|
/* Request EOQF for last transfer in FIFO */
|
|
|
|
if (dspi->len == dspi->bytes_per_word || fifo_size == 0)
|
|
|
|
dspi->tx_cmd |= SPI_PUSHR_CMD_EOQ;
|
|
|
|
/* Clear transfer count for first transfer in FIFO */
|
|
|
|
if (fifo_size == (DSPI_FIFO_SIZE - 1))
|
|
|
|
dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
|
|
|
|
/* Write combined TX FIFO and CMD FIFO entry */
|
|
|
|
fifo_write(dspi);
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
2015-06-09 18:45:27 +07:00
|
|
|
}
|
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
static void dspi_eoq_read(struct fsl_dspi *dspi)
|
2015-06-09 18:45:27 +07:00
|
|
|
{
|
2018-06-20 14:34:35 +07:00
|
|
|
int fifo_size = DSPI_FIFO_SIZE;
|
2015-06-09 18:45:27 +07:00
|
|
|
|
2019-08-19 01:01:12 +07:00
|
|
|
/* Read one FIFO entry and push to rx buffer */
|
2018-06-20 14:34:35 +07:00
|
|
|
while ((dspi->rx < dspi->rx_end) && fifo_size--)
|
|
|
|
dspi_push_rx(dspi, fifo_read(dspi));
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2019-08-23 04:15:13 +07:00
|
|
|
static int dspi_rxtx(struct fsl_dspi *dspi)
|
2019-08-19 01:01:13 +07:00
|
|
|
{
|
|
|
|
struct spi_message *msg = dspi->cur_msg;
|
|
|
|
enum dspi_trans_mode trans_mode;
|
|
|
|
u16 spi_tcnt;
|
2019-08-23 04:15:13 +07:00
|
|
|
u32 spi_tcr;
|
2019-08-23 04:15:10 +07:00
|
|
|
|
2019-09-05 08:01:13 +07:00
|
|
|
spi_take_timestamp_post(dspi->ctlr, dspi->cur_transfer,
|
2019-12-27 08:24:17 +07:00
|
|
|
dspi->progress, !dspi->irq);
|
2019-09-05 08:01:13 +07:00
|
|
|
|
2019-08-23 04:15:10 +07:00
|
|
|
/* Get transfer counter (in number of SPI transfers). It was
|
|
|
|
* reset to 0 when transfer(s) were started.
|
|
|
|
*/
|
|
|
|
regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
|
|
|
|
spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
|
|
|
|
/* Update total number of bytes that were transferred */
|
|
|
|
msg->actual_length += spi_tcnt * dspi->bytes_per_word;
|
2019-12-27 08:24:17 +07:00
|
|
|
dspi->progress += spi_tcnt;
|
2019-08-23 04:15:10 +07:00
|
|
|
|
|
|
|
trans_mode = dspi->devtype_data->trans_mode;
|
2019-08-23 04:15:12 +07:00
|
|
|
if (trans_mode == DSPI_EOQ_MODE)
|
2019-08-23 04:15:10 +07:00
|
|
|
dspi_eoq_read(dspi);
|
2019-08-23 04:15:12 +07:00
|
|
|
else if (trans_mode == DSPI_TCFQ_MODE)
|
2019-08-23 04:15:10 +07:00
|
|
|
dspi_tcfq_read(dspi);
|
2019-08-19 01:01:13 +07:00
|
|
|
|
2019-08-23 04:15:13 +07:00
|
|
|
if (!dspi->len)
|
|
|
|
/* Success! */
|
|
|
|
return 0;
|
2019-08-19 01:01:13 +07:00
|
|
|
|
2019-09-05 08:01:13 +07:00
|
|
|
spi_take_timestamp_pre(dspi->ctlr, dspi->cur_transfer,
|
2019-12-27 08:24:17 +07:00
|
|
|
dspi->progress, !dspi->irq);
|
2019-09-05 08:01:13 +07:00
|
|
|
|
2019-08-23 04:15:12 +07:00
|
|
|
if (trans_mode == DSPI_EOQ_MODE)
|
2019-08-23 04:15:10 +07:00
|
|
|
dspi_eoq_write(dspi);
|
2019-08-23 04:15:12 +07:00
|
|
|
else if (trans_mode == DSPI_TCFQ_MODE)
|
2019-08-23 04:15:10 +07:00
|
|
|
dspi_tcfq_write(dspi);
|
2019-08-19 01:01:13 +07:00
|
|
|
|
2019-08-23 04:15:13 +07:00
|
|
|
return -EINPROGRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dspi_poll(struct fsl_dspi *dspi)
|
|
|
|
{
|
|
|
|
int tries = 1000;
|
|
|
|
u32 spi_sr;
|
|
|
|
|
|
|
|
do {
|
|
|
|
regmap_read(dspi->regmap, SPI_SR, &spi_sr);
|
|
|
|
regmap_write(dspi->regmap, SPI_SR, spi_sr);
|
|
|
|
|
|
|
|
if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF))
|
|
|
|
break;
|
|
|
|
} while (--tries);
|
|
|
|
|
|
|
|
if (!tries)
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
|
|
|
return dspi_rxtx(dspi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t dspi_interrupt(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
|
|
|
|
u32 spi_sr;
|
|
|
|
|
|
|
|
regmap_read(dspi->regmap, SPI_SR, &spi_sr);
|
|
|
|
regmap_write(dspi->regmap, SPI_SR, spi_sr);
|
|
|
|
|
spi: spi-fsl-dspi: Always use the TCFQ devices in poll mode
With this patch, the "interrupts" property from the device tree bindings
is ignored, even if present, if the driver runs in TCFQ mode.
Switching to using the DSPI in poll mode has several distinct
benefits:
- With interrupts, the DSPI driver in TCFQ mode raises an IRQ after each
transmitted word. There is more time wasted for the "waitq" event than
for actual I/O. And the DSPI IRQ count can easily get the largest in
/proc/interrupts on Freescale boards with attached SPI devices.
- The SPI I/O time is both lower, and more consistently so. Attached to
some Freescale devices are either PTP switches, or SPI RTCs. For
reading time off of a SPI slave device, it is important that all SPI
transfers take a deterministic time to complete.
- In poll mode there is much less time spent by the CPU in hardirq
context, which helps with the response latency of the system, and at
the same time there is more control over when interrupts must be
disabled (to get a precise timestamp measurement, which will come in a
future patch): win-win.
On the LS1021A-TSN board, where the SPI device is a SJA1105 PTP switch
(with a bits_per_word=8 driver), I created a "benchmark" where I
periodically transferred a 12-byte message once per second, for 120
seconds. I then recorded the time before putting the first byte in the
TX FIFO, and the time after reading the last byte from the RX FIFO. That
is the transfer delay in nanoseconds.
Interrupt mode:
delay: min 125120 max 168320 mean 150286 std dev 17675.3
Poll mode:
delay: min 69440 max 119040 mean 70312.9 std dev 8065.34
Both the mean latency and the standard deviation are more than 50% lower
in poll mode than in interrupt mode, and the 'max' in poll mode is lower
than the 'min' in interrupt mode. This is with an 'ondemand' governor on
an otherwise idle system - therefore running mostly at 600 MHz out of a
max of 1200 MHz.
Signed-off-by: Vladimir Oltean <olteanv@gmail.com>
Link: https://lore.kernel.org/r/20191001205216.32115-1-olteanv@gmail.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2019-10-02 03:52:16 +07:00
|
|
|
if (!(spi_sr & SPI_SR_EOQF))
|
2019-08-23 04:15:13 +07:00
|
|
|
return IRQ_NONE;
|
|
|
|
|
2019-09-03 17:57:08 +07:00
|
|
|
if (dspi_rxtx(dspi) == 0) {
|
2019-08-23 04:15:13 +07:00
|
|
|
dspi->waitflags = 1;
|
|
|
|
wake_up_interruptible(&dspi->waitq);
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:01:13 +07:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
static int dspi_transfer_one_message(struct spi_controller *ctlr,
|
2019-08-19 01:01:02 +07:00
|
|
|
struct spi_message *message)
|
2013-08-16 10:08:55 +07:00
|
|
|
{
|
2019-08-19 01:01:10 +07:00
|
|
|
struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
|
2015-01-27 17:57:22 +07:00
|
|
|
struct spi_device *spi = message->spi;
|
2019-08-19 01:01:11 +07:00
|
|
|
enum dspi_trans_mode trans_mode;
|
2015-01-27 17:57:22 +07:00
|
|
|
struct spi_transfer *transfer;
|
|
|
|
int status = 0;
|
2015-06-09 18:45:27 +07:00
|
|
|
|
2015-01-27 17:57:22 +07:00
|
|
|
message->actual_length = 0;
|
|
|
|
|
|
|
|
list_for_each_entry(transfer, &message->transfers, transfer_list) {
|
|
|
|
dspi->cur_transfer = transfer;
|
|
|
|
dspi->cur_msg = message;
|
|
|
|
dspi->cur_chip = spi_get_ctldata(spi);
|
2018-06-20 14:34:33 +07:00
|
|
|
/* Prepare command word for CMD FIFO */
|
|
|
|
dspi->tx_cmd = SPI_PUSHR_CMD_CTAS(0) |
|
2019-08-19 01:01:02 +07:00
|
|
|
SPI_PUSHR_CMD_PCS(spi->chip_select);
|
2016-04-05 19:33:14 +07:00
|
|
|
if (list_is_last(&dspi->cur_transfer->transfer_list,
|
2018-06-20 14:34:33 +07:00
|
|
|
&dspi->cur_msg->transfers)) {
|
|
|
|
/* Leave PCS activated after last transfer when
|
|
|
|
* cs_change is set.
|
|
|
|
*/
|
|
|
|
if (transfer->cs_change)
|
|
|
|
dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
|
|
|
|
} else {
|
|
|
|
/* Keep PCS active between transfers in same message
|
|
|
|
* when cs_change is not set, and de-activate PCS
|
|
|
|
* between transfers in the same message when
|
|
|
|
* cs_change is set.
|
|
|
|
*/
|
|
|
|
if (!transfer->cs_change)
|
|
|
|
dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
|
|
|
|
}
|
|
|
|
|
2015-01-27 17:57:22 +07:00
|
|
|
dspi->void_write_data = dspi->cur_chip->void_write_data;
|
|
|
|
|
2018-06-20 14:34:35 +07:00
|
|
|
dspi->tx = transfer->tx_buf;
|
2015-01-27 17:57:22 +07:00
|
|
|
dspi->rx = transfer->rx_buf;
|
|
|
|
dspi->rx_end = dspi->rx + transfer->len;
|
|
|
|
dspi->len = transfer->len;
|
2019-12-27 08:24:17 +07:00
|
|
|
dspi->progress = 0;
|
2018-06-20 14:34:35 +07:00
|
|
|
/* Validated transfer specific frame size (defaults applied) */
|
|
|
|
dspi->bits_per_word = transfer->bits_per_word;
|
|
|
|
if (transfer->bits_per_word <= 8)
|
|
|
|
dspi->bytes_per_word = 1;
|
2018-06-20 14:34:40 +07:00
|
|
|
else if (transfer->bits_per_word <= 16)
|
2018-06-20 14:34:35 +07:00
|
|
|
dspi->bytes_per_word = 2;
|
2018-06-20 14:34:40 +07:00
|
|
|
else
|
|
|
|
dspi->bytes_per_word = 4;
|
2015-01-27 17:57:22 +07:00
|
|
|
|
|
|
|
regmap_update_bits(dspi->regmap, SPI_MCR,
|
2018-06-20 14:34:37 +07:00
|
|
|
SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
|
|
|
|
SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
|
2015-12-10 12:55:30 +07:00
|
|
|
regmap_write(dspi->regmap, SPI_CTAR(0),
|
2018-06-20 14:34:35 +07:00
|
|
|
dspi->cur_chip->ctar_val |
|
|
|
|
SPI_FRAME_BITS(transfer->bits_per_word));
|
2018-06-20 14:34:39 +07:00
|
|
|
if (dspi->devtype_data->xspi_mode)
|
|
|
|
regmap_write(dspi->regmap, SPI_CTARE(0),
|
2019-08-19 01:01:02 +07:00
|
|
|
SPI_FRAME_EBITS(transfer->bits_per_word) |
|
|
|
|
SPI_CTARE_DTCP(1));
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-09-05 08:01:13 +07:00
|
|
|
spi_take_timestamp_pre(dspi->ctlr, dspi->cur_transfer,
|
2019-12-27 08:24:17 +07:00
|
|
|
dspi->progress, !dspi->irq);
|
2019-09-05 08:01:13 +07:00
|
|
|
|
2015-06-09 18:45:27 +07:00
|
|
|
trans_mode = dspi->devtype_data->trans_mode;
|
|
|
|
switch (trans_mode) {
|
|
|
|
case DSPI_EOQ_MODE:
|
|
|
|
regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
|
2015-06-09 18:45:37 +07:00
|
|
|
dspi_eoq_write(dspi);
|
2015-06-09 18:45:27 +07:00
|
|
|
break;
|
|
|
|
case DSPI_TCFQ_MODE:
|
|
|
|
regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
|
2015-06-09 18:45:37 +07:00
|
|
|
dspi_tcfq_write(dspi);
|
2015-06-09 18:45:27 +07:00
|
|
|
break;
|
2016-11-10 19:19:15 +07:00
|
|
|
case DSPI_DMA_MODE:
|
|
|
|
regmap_write(dspi->regmap, SPI_RSER,
|
2019-08-19 01:01:02 +07:00
|
|
|
SPI_RSER_TFFFE | SPI_RSER_TFFFD |
|
|
|
|
SPI_RSER_RFDFE | SPI_RSER_RFDFD);
|
2016-11-10 19:19:15 +07:00
|
|
|
status = dspi_dma_xfer(dspi);
|
2016-11-17 19:16:48 +07:00
|
|
|
break;
|
2015-06-09 18:45:27 +07:00
|
|
|
default:
|
|
|
|
dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
|
|
|
|
trans_mode);
|
|
|
|
status = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-02-12 14:29:05 +07:00
|
|
|
|
2019-08-23 04:15:13 +07:00
|
|
|
if (!dspi->irq) {
|
|
|
|
do {
|
|
|
|
status = dspi_poll(dspi);
|
|
|
|
} while (status == -EINPROGRESS);
|
|
|
|
} else if (trans_mode != DSPI_DMA_MODE) {
|
|
|
|
status = wait_event_interruptible(dspi->waitq,
|
|
|
|
dspi->waitflags);
|
2016-11-17 19:16:48 +07:00
|
|
|
dspi->waitflags = 0;
|
|
|
|
}
|
2019-08-23 04:15:13 +07:00
|
|
|
if (status)
|
|
|
|
dev_err(&dspi->pdev->dev,
|
|
|
|
"Waiting for transfer to complete failed!\n");
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-09-26 17:51:37 +07:00
|
|
|
spi_transfer_delay_exec(transfer);
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:45:27 +07:00
|
|
|
out:
|
2015-01-27 17:57:22 +07:00
|
|
|
message->status = status;
|
2019-08-19 01:01:10 +07:00
|
|
|
spi_finalize_current_message(ctlr);
|
2015-01-27 17:57:22 +07:00
|
|
|
|
|
|
|
return status;
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2015-01-27 17:57:22 +07:00
|
|
|
static int dspi_setup(struct spi_device *spi)
|
2013-08-16 10:08:55 +07:00
|
|
|
{
|
2019-08-19 01:01:10 +07:00
|
|
|
struct fsl_dspi *dspi = spi_controller_get_devdata(spi->controller);
|
2015-04-04 03:39:31 +07:00
|
|
|
unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
|
2019-08-19 01:01:11 +07:00
|
|
|
u32 cs_sck_delay = 0, sck_cs_delay = 0;
|
|
|
|
struct fsl_dspi_platform_data *pdata;
|
2018-06-20 14:34:35 +07:00
|
|
|
unsigned char pasc = 0, asc = 0;
|
2019-08-19 01:01:11 +07:00
|
|
|
struct chip_data *chip;
|
2015-04-04 03:39:31 +07:00
|
|
|
unsigned long clkrate;
|
2013-08-16 10:08:55 +07:00
|
|
|
|
|
|
|
/* Only alloc on first setup */
|
|
|
|
chip = spi_get_ctldata(spi);
|
|
|
|
if (chip == NULL) {
|
2015-01-27 17:57:20 +07:00
|
|
|
chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
|
2013-08-16 10:08:55 +07:00
|
|
|
if (!chip)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-10-28 05:23:01 +07:00
|
|
|
pdata = dev_get_platdata(&dspi->pdev->dev);
|
2015-04-04 03:39:31 +07:00
|
|
|
|
2017-10-28 05:23:01 +07:00
|
|
|
if (!pdata) {
|
|
|
|
of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
|
2019-08-19 01:01:02 +07:00
|
|
|
&cs_sck_delay);
|
2017-10-28 05:23:01 +07:00
|
|
|
|
|
|
|
of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
|
2019-08-19 01:01:02 +07:00
|
|
|
&sck_cs_delay);
|
2017-10-28 05:23:01 +07:00
|
|
|
} else {
|
|
|
|
cs_sck_delay = pdata->cs_sck_delay;
|
|
|
|
sck_cs_delay = pdata->sck_cs_delay;
|
|
|
|
}
|
2015-04-04 03:39:31 +07:00
|
|
|
|
2013-08-16 10:08:55 +07:00
|
|
|
chip->void_write_data = 0;
|
|
|
|
|
2015-04-04 03:39:31 +07:00
|
|
|
clkrate = clk_get_rate(dspi->clk);
|
|
|
|
hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
|
|
|
|
|
|
|
|
/* Set PCS to SCK delay scale values */
|
|
|
|
ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
|
|
|
|
|
|
|
|
/* Set After SCK delay scale values */
|
|
|
|
ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-08-19 01:01:06 +07:00
|
|
|
chip->ctar_val = 0;
|
|
|
|
if (spi->mode & SPI_CPOL)
|
|
|
|
chip->ctar_val |= SPI_CTAR_CPOL;
|
|
|
|
if (spi->mode & SPI_CPHA)
|
|
|
|
chip->ctar_val |= SPI_CTAR_CPHA;
|
2019-02-06 05:13:49 +07:00
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
if (!spi_controller_is_slave(dspi->ctlr)) {
|
2019-08-19 01:01:06 +07:00
|
|
|
chip->ctar_val |= SPI_CTAR_PCSSCK(pcssck) |
|
|
|
|
SPI_CTAR_CSSCK(cssck) |
|
|
|
|
SPI_CTAR_PASC(pasc) |
|
|
|
|
SPI_CTAR_ASC(asc) |
|
|
|
|
SPI_CTAR_PBR(pbr) |
|
|
|
|
SPI_CTAR_BR(br);
|
|
|
|
|
|
|
|
if (spi->mode & SPI_LSB_FIRST)
|
|
|
|
chip->ctar_val |= SPI_CTAR_LSBFE;
|
2019-02-06 05:13:49 +07:00
|
|
|
}
|
2013-08-16 10:08:55 +07:00
|
|
|
|
|
|
|
spi_set_ctldata(spi, chip);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-27 17:57:20 +07:00
|
|
|
static void dspi_cleanup(struct spi_device *spi)
|
|
|
|
{
|
|
|
|
struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
|
|
|
|
|
|
|
|
dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
|
2019-08-19 01:01:10 +07:00
|
|
|
spi->controller->bus_num, spi->chip_select);
|
2015-01-27 17:57:20 +07:00
|
|
|
|
|
|
|
kfree(chip);
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:45:41 +07:00
|
|
|
static const struct of_device_id fsl_dspi_dt_ids[] = {
|
2018-01-02 20:28:06 +07:00
|
|
|
{ .compatible = "fsl,vf610-dspi", .data = &vf610_data, },
|
|
|
|
{ .compatible = "fsl,ls1021a-v1.0-dspi", .data = &ls1021a_v1_data, },
|
|
|
|
{ .compatible = "fsl,ls2085a-dspi", .data = &ls2085a_data, },
|
2013-08-16 10:08:55 +07:00
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
static int dspi_suspend(struct device *dev)
|
|
|
|
{
|
2019-08-19 01:01:10 +07:00
|
|
|
struct spi_controller *ctlr = dev_get_drvdata(dev);
|
|
|
|
struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
spi_controller_suspend(ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
clk_disable_unprepare(dspi->clk);
|
|
|
|
|
2015-06-12 23:55:22 +07:00
|
|
|
pinctrl_pm_select_sleep_state(dev);
|
|
|
|
|
2013-08-16 10:08:55 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dspi_resume(struct device *dev)
|
|
|
|
{
|
2019-08-19 01:01:10 +07:00
|
|
|
struct spi_controller *ctlr = dev_get_drvdata(dev);
|
|
|
|
struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
|
2016-08-22 09:05:30 +07:00
|
|
|
int ret;
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2015-06-12 23:55:22 +07:00
|
|
|
pinctrl_pm_select_default_state(dev);
|
|
|
|
|
2016-08-22 09:05:30 +07:00
|
|
|
ret = clk_prepare_enable(dspi->clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-08-19 01:01:10 +07:00
|
|
|
spi_controller_resume(ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_PM_SLEEP */
|
|
|
|
|
2014-02-26 08:30:14 +07:00
|
|
|
static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2018-06-20 14:34:36 +07:00
|
|
|
static const struct regmap_range dspi_volatile_ranges[] = {
|
|
|
|
regmap_reg_range(SPI_MCR, SPI_TCR),
|
|
|
|
regmap_reg_range(SPI_SR, SPI_SR),
|
|
|
|
regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct regmap_access_table dspi_volatile_table = {
|
2019-08-19 01:01:02 +07:00
|
|
|
.yes_ranges = dspi_volatile_ranges,
|
|
|
|
.n_yes_ranges = ARRAY_SIZE(dspi_volatile_ranges),
|
2018-06-20 14:34:36 +07:00
|
|
|
};
|
|
|
|
|
2014-10-09 10:27:45 +07:00
|
|
|
static const struct regmap_config dspi_regmap_config = {
|
2019-08-19 01:01:02 +07:00
|
|
|
.reg_bits = 32,
|
|
|
|
.val_bits = 32,
|
|
|
|
.reg_stride = 4,
|
|
|
|
.max_register = 0x88,
|
|
|
|
.volatile_table = &dspi_volatile_table,
|
2013-08-16 10:08:55 +07:00
|
|
|
};
|
|
|
|
|
2018-06-20 14:34:38 +07:00
|
|
|
static const struct regmap_range dspi_xspi_volatile_ranges[] = {
|
|
|
|
regmap_reg_range(SPI_MCR, SPI_TCR),
|
|
|
|
regmap_reg_range(SPI_SR, SPI_SR),
|
|
|
|
regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
|
|
|
|
regmap_reg_range(SPI_SREX, SPI_SREX),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct regmap_access_table dspi_xspi_volatile_table = {
|
2019-08-19 01:01:02 +07:00
|
|
|
.yes_ranges = dspi_xspi_volatile_ranges,
|
|
|
|
.n_yes_ranges = ARRAY_SIZE(dspi_xspi_volatile_ranges),
|
2018-06-20 14:34:38 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct regmap_config dspi_xspi_regmap_config[] = {
|
|
|
|
{
|
2019-08-19 01:01:02 +07:00
|
|
|
.reg_bits = 32,
|
|
|
|
.val_bits = 32,
|
|
|
|
.reg_stride = 4,
|
|
|
|
.max_register = 0x13c,
|
|
|
|
.volatile_table = &dspi_xspi_volatile_table,
|
2018-06-20 14:34:38 +07:00
|
|
|
},
|
|
|
|
{
|
2019-08-19 01:01:02 +07:00
|
|
|
.name = "pushr",
|
|
|
|
.reg_bits = 16,
|
|
|
|
.val_bits = 16,
|
|
|
|
.reg_stride = 2,
|
|
|
|
.max_register = 0x2,
|
2018-06-20 14:34:38 +07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-10-17 17:02:34 +07:00
|
|
|
static void dspi_init(struct fsl_dspi *dspi)
|
|
|
|
{
|
2019-08-19 01:01:06 +07:00
|
|
|
unsigned int mcr = SPI_MCR_PCSIS;
|
2019-02-06 05:13:49 +07:00
|
|
|
|
2019-08-19 01:01:06 +07:00
|
|
|
if (dspi->devtype_data->xspi_mode)
|
|
|
|
mcr |= SPI_MCR_XSPI;
|
2019-08-19 01:01:10 +07:00
|
|
|
if (!spi_controller_is_slave(dspi->ctlr))
|
2019-02-06 05:13:49 +07:00
|
|
|
mcr |= SPI_MCR_MASTER;
|
|
|
|
|
|
|
|
regmap_write(dspi->regmap, SPI_MCR, mcr);
|
2016-10-17 17:02:34 +07:00
|
|
|
regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
|
2018-06-20 14:34:39 +07:00
|
|
|
if (dspi->devtype_data->xspi_mode)
|
|
|
|
regmap_write(dspi->regmap, SPI_CTARE(0),
|
|
|
|
SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1));
|
2016-10-17 17:02:34 +07:00
|
|
|
}
|
|
|
|
|
2019-09-24 18:05:47 +07:00
|
|
|
static int dspi_slave_abort(struct spi_master *master)
|
|
|
|
{
|
|
|
|
struct fsl_dspi *dspi = spi_master_get_devdata(master);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Terminate all pending DMA transactions for the SPI working
|
|
|
|
* in SLAVE mode.
|
|
|
|
*/
|
|
|
|
dmaengine_terminate_sync(dspi->dma->chan_rx);
|
|
|
|
dmaengine_terminate_sync(dspi->dma->chan_tx);
|
|
|
|
|
|
|
|
/* Clear the internal DSPI RX and TX FIFO buffers */
|
|
|
|
regmap_update_bits(dspi->regmap, SPI_MCR,
|
|
|
|
SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
|
|
|
|
SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-16 10:08:55 +07:00
|
|
|
static int dspi_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct device_node *np = pdev->dev.of_node;
|
2019-08-19 01:01:11 +07:00
|
|
|
const struct regmap_config *regmap_config;
|
|
|
|
struct fsl_dspi_platform_data *pdata;
|
2019-08-19 01:01:10 +07:00
|
|
|
struct spi_controller *ctlr;
|
2019-08-19 01:01:11 +07:00
|
|
|
int ret, cs_num, bus_num;
|
2013-08-16 10:08:55 +07:00
|
|
|
struct fsl_dspi *dspi;
|
|
|
|
struct resource *res;
|
2014-02-12 14:29:05 +07:00
|
|
|
void __iomem *base;
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
|
|
|
|
if (!ctlr)
|
2013-08-16 10:08:55 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
dspi = spi_controller_get_devdata(ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
dspi->pdev = pdev;
|
2019-08-19 01:01:10 +07:00
|
|
|
dspi->ctlr = ctlr;
|
2015-01-27 17:57:22 +07:00
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->setup = dspi_setup;
|
|
|
|
ctlr->transfer_one_message = dspi_transfer_one_message;
|
|
|
|
ctlr->dev.of_node = pdev->dev.of_node;
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->cleanup = dspi_cleanup;
|
2019-09-24 18:05:47 +07:00
|
|
|
ctlr->slave_abort = dspi_slave_abort;
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2017-10-28 05:23:01 +07:00
|
|
|
pdata = dev_get_platdata(&pdev->dev);
|
|
|
|
if (pdata) {
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->num_chipselect = pdata->cs_num;
|
|
|
|
ctlr->bus_num = pdata->bus_num;
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2017-10-28 05:23:01 +07:00
|
|
|
dspi->devtype_data = &coldfire_data;
|
|
|
|
} else {
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2017-10-28 05:23:01 +07:00
|
|
|
ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
|
2019-08-19 01:01:10 +07:00
|
|
|
goto out_ctlr_put;
|
2017-10-28 05:23:01 +07:00
|
|
|
}
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->num_chipselect = cs_num;
|
2017-10-28 05:23:01 +07:00
|
|
|
|
|
|
|
ret = of_property_read_u32(np, "bus-num", &bus_num);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "can't get bus-num\n");
|
2019-08-19 01:01:10 +07:00
|
|
|
goto out_ctlr_put;
|
2017-10-28 05:23:01 +07:00
|
|
|
}
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->bus_num = bus_num;
|
2017-10-28 05:23:01 +07:00
|
|
|
|
2019-02-06 05:13:49 +07:00
|
|
|
if (of_property_read_bool(np, "spi-slave"))
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->slave = true;
|
2019-02-06 05:13:49 +07:00
|
|
|
|
2017-10-28 05:23:01 +07:00
|
|
|
dspi->devtype_data = of_device_get_match_data(&pdev->dev);
|
|
|
|
if (!dspi->devtype_data) {
|
|
|
|
dev_err(&pdev->dev, "can't get devtype_data\n");
|
|
|
|
ret = -EFAULT;
|
2019-08-19 01:01:10 +07:00
|
|
|
goto out_ctlr_put;
|
2017-10-28 05:23:01 +07:00
|
|
|
}
|
2015-06-09 18:45:27 +07:00
|
|
|
}
|
|
|
|
|
2018-06-20 14:34:41 +07:00
|
|
|
if (dspi->devtype_data->xspi_mode)
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
|
2018-06-20 14:34:41 +07:00
|
|
|
else
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
|
2018-06-20 14:34:41 +07:00
|
|
|
|
2013-08-16 10:08:55 +07:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2014-02-12 14:29:05 +07:00
|
|
|
base = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(base)) {
|
|
|
|
ret = PTR_ERR(base);
|
2019-08-19 01:01:10 +07:00
|
|
|
goto out_ctlr_put;
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2018-06-20 14:34:38 +07:00
|
|
|
if (dspi->devtype_data->xspi_mode)
|
|
|
|
regmap_config = &dspi_xspi_regmap_config[0];
|
|
|
|
else
|
|
|
|
regmap_config = &dspi_regmap_config;
|
|
|
|
dspi->regmap = devm_regmap_init_mmio(&pdev->dev, base, regmap_config);
|
2014-02-12 14:29:05 +07:00
|
|
|
if (IS_ERR(dspi->regmap)) {
|
|
|
|
dev_err(&pdev->dev, "failed to init regmap: %ld\n",
|
|
|
|
PTR_ERR(dspi->regmap));
|
2017-02-19 20:19:02 +07:00
|
|
|
ret = PTR_ERR(dspi->regmap);
|
2019-08-19 01:01:10 +07:00
|
|
|
goto out_ctlr_put;
|
2014-02-12 14:29:05 +07:00
|
|
|
}
|
|
|
|
|
2018-06-20 14:34:38 +07:00
|
|
|
if (dspi->devtype_data->xspi_mode) {
|
|
|
|
dspi->regmap_pushr = devm_regmap_init_mmio(
|
|
|
|
&pdev->dev, base + SPI_PUSHR,
|
|
|
|
&dspi_xspi_regmap_config[1]);
|
|
|
|
if (IS_ERR(dspi->regmap_pushr)) {
|
|
|
|
dev_err(&pdev->dev,
|
|
|
|
"failed to init pushr regmap: %ld\n",
|
|
|
|
PTR_ERR(dspi->regmap_pushr));
|
2018-06-21 20:22:09 +07:00
|
|
|
ret = PTR_ERR(dspi->regmap_pushr);
|
2019-08-19 01:01:10 +07:00
|
|
|
goto out_ctlr_put;
|
2018-06-20 14:34:38 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 18:33:09 +07:00
|
|
|
dspi->clk = devm_clk_get(&pdev->dev, "dspi");
|
|
|
|
if (IS_ERR(dspi->clk)) {
|
|
|
|
ret = PTR_ERR(dspi->clk);
|
|
|
|
dev_err(&pdev->dev, "unable to get clock\n");
|
2019-08-19 01:01:10 +07:00
|
|
|
goto out_ctlr_put;
|
2018-06-29 18:33:09 +07:00
|
|
|
}
|
|
|
|
ret = clk_prepare_enable(dspi->clk);
|
|
|
|
if (ret)
|
2019-08-19 01:01:10 +07:00
|
|
|
goto out_ctlr_put;
|
2018-06-29 18:33:09 +07:00
|
|
|
|
2016-10-17 17:02:34 +07:00
|
|
|
dspi_init(dspi);
|
2019-08-23 04:15:13 +07:00
|
|
|
|
spi: spi-fsl-dspi: Always use the TCFQ devices in poll mode
With this patch, the "interrupts" property from the device tree bindings
is ignored, even if present, if the driver runs in TCFQ mode.
Switching to using the DSPI in poll mode has several distinct
benefits:
- With interrupts, the DSPI driver in TCFQ mode raises an IRQ after each
transmitted word. There is more time wasted for the "waitq" event than
for actual I/O. And the DSPI IRQ count can easily get the largest in
/proc/interrupts on Freescale boards with attached SPI devices.
- The SPI I/O time is both lower, and more consistently so. Attached to
some Freescale devices are either PTP switches, or SPI RTCs. For
reading time off of a SPI slave device, it is important that all SPI
transfers take a deterministic time to complete.
- In poll mode there is much less time spent by the CPU in hardirq
context, which helps with the response latency of the system, and at
the same time there is more control over when interrupts must be
disabled (to get a precise timestamp measurement, which will come in a
future patch): win-win.
On the LS1021A-TSN board, where the SPI device is a SJA1105 PTP switch
(with a bits_per_word=8 driver), I created a "benchmark" where I
periodically transferred a 12-byte message once per second, for 120
seconds. I then recorded the time before putting the first byte in the
TX FIFO, and the time after reading the last byte from the RX FIFO. That
is the transfer delay in nanoseconds.
Interrupt mode:
delay: min 125120 max 168320 mean 150286 std dev 17675.3
Poll mode:
delay: min 69440 max 119040 mean 70312.9 std dev 8065.34
Both the mean latency and the standard deviation are more than 50% lower
in poll mode than in interrupt mode, and the 'max' in poll mode is lower
than the 'min' in interrupt mode. This is with an 'ondemand' governor on
an otherwise idle system - therefore running mostly at 600 MHz out of a
max of 1200 MHz.
Signed-off-by: Vladimir Oltean <olteanv@gmail.com>
Link: https://lore.kernel.org/r/20191001205216.32115-1-olteanv@gmail.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2019-10-02 03:52:16 +07:00
|
|
|
if (dspi->devtype_data->trans_mode == DSPI_TCFQ_MODE)
|
|
|
|
goto poll_mode;
|
|
|
|
|
2013-08-16 10:08:55 +07:00
|
|
|
dspi->irq = platform_get_irq(pdev, 0);
|
2019-08-23 04:15:13 +07:00
|
|
|
if (dspi->irq <= 0) {
|
|
|
|
dev_info(&pdev->dev,
|
|
|
|
"can't get platform irq, using poll mode\n");
|
|
|
|
dspi->irq = 0;
|
|
|
|
goto poll_mode;
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2018-10-29 10:11:16 +07:00
|
|
|
ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt,
|
|
|
|
IRQF_SHARED, pdev->name, dspi);
|
2013-08-16 10:08:55 +07:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
|
2018-06-29 18:33:09 +07:00
|
|
|
goto out_clk_put;
|
2013-08-16 10:08:55 +07:00
|
|
|
}
|
|
|
|
|
2019-08-23 04:15:13 +07:00
|
|
|
init_waitqueue_head(&dspi->waitq);
|
|
|
|
|
|
|
|
poll_mode:
|
2019-09-05 08:01:13 +07:00
|
|
|
|
2016-11-10 19:19:15 +07:00
|
|
|
if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
|
2017-05-22 20:19:20 +07:00
|
|
|
ret = dspi_request_dma(dspi, res->start);
|
|
|
|
if (ret < 0) {
|
2016-11-10 19:19:15 +07:00
|
|
|
dev_err(&pdev->dev, "can't get dma channels\n");
|
|
|
|
goto out_clk_put;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
ctlr->max_speed_hz =
|
2016-03-22 03:11:52 +07:00
|
|
|
clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
|
|
|
|
|
2019-09-05 08:01:13 +07:00
|
|
|
ctlr->ptp_sts_supported = dspi->devtype_data->ptp_sts_supported;
|
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
platform_set_drvdata(pdev, ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
2019-08-19 01:01:10 +07:00
|
|
|
ret = spi_register_controller(ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
if (ret != 0) {
|
2019-08-19 01:01:10 +07:00
|
|
|
dev_err(&pdev->dev, "Problem registering DSPI ctlr\n");
|
2013-08-16 10:08:55 +07:00
|
|
|
goto out_clk_put;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
out_clk_put:
|
|
|
|
clk_disable_unprepare(dspi->clk);
|
2019-08-19 01:01:10 +07:00
|
|
|
out_ctlr_put:
|
|
|
|
spi_controller_put(ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dspi_remove(struct platform_device *pdev)
|
|
|
|
{
|
2019-08-19 01:01:10 +07:00
|
|
|
struct spi_controller *ctlr = platform_get_drvdata(pdev);
|
|
|
|
struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
|
|
|
/* Disconnect from the SPI framework */
|
2016-11-10 19:19:15 +07:00
|
|
|
dspi_release_dma(dspi);
|
2013-10-12 14:15:31 +07:00
|
|
|
clk_disable_unprepare(dspi->clk);
|
2019-08-19 01:01:10 +07:00
|
|
|
spi_unregister_controller(dspi->ctlr);
|
2013-08-16 10:08:55 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver fsl_dspi_driver = {
|
2019-08-19 01:01:02 +07:00
|
|
|
.driver.name = DRIVER_NAME,
|
|
|
|
.driver.of_match_table = fsl_dspi_dt_ids,
|
|
|
|
.driver.owner = THIS_MODULE,
|
|
|
|
.driver.pm = &dspi_pm,
|
|
|
|
.probe = dspi_probe,
|
|
|
|
.remove = dspi_remove,
|
2013-08-16 10:08:55 +07:00
|
|
|
};
|
|
|
|
module_platform_driver(fsl_dspi_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
|
2013-09-10 15:46:33 +07:00
|
|
|
MODULE_LICENSE("GPL");
|
2013-08-16 10:08:55 +07:00
|
|
|
MODULE_ALIAS("platform:" DRIVER_NAME);
|