23 #include <linux/module.h>
33 #include <linux/slab.h>
37 #define SPI_NO_RESOURCE ((resource_size_t)-1)
39 #define SPI_MAX_CHIPSELECT 2
41 #define CS_DEFAULT 0xFF
43 #define SPIFMT_PHASE_MASK BIT(16)
44 #define SPIFMT_POLARITY_MASK BIT(17)
45 #define SPIFMT_DISTIMER_MASK BIT(18)
46 #define SPIFMT_SHIFTDIR_MASK BIT(20)
47 #define SPIFMT_WAITENA_MASK BIT(21)
48 #define SPIFMT_PARITYENA_MASK BIT(22)
49 #define SPIFMT_ODD_PARITY_MASK BIT(23)
50 #define SPIFMT_WDELAY_MASK 0x3f000000u
51 #define SPIFMT_WDELAY_SHIFT 24
52 #define SPIFMT_PRESCALE_SHIFT 8
55 #define SPIPC0_DIFUN_MASK BIT(11)
56 #define SPIPC0_DOFUN_MASK BIT(10)
57 #define SPIPC0_CLKFUN_MASK BIT(9)
58 #define SPIPC0_SPIENA_MASK BIT(8)
60 #define SPIINT_MASKALL 0x0101035F
61 #define SPIINT_MASKINT 0x0000015F
62 #define SPI_INTLVL_1 0x000001FF
63 #define SPI_INTLVL_0 0x00000000
66 #define SPIDAT1_CSHOLD_MASK BIT(12)
69 #define SPIGCR1_CLKMOD_MASK BIT(1)
70 #define SPIGCR1_MASTER_MASK BIT(0)
71 #define SPIGCR1_POWERDOWN_MASK BIT(8)
72 #define SPIGCR1_LOOPBACK_MASK BIT(16)
73 #define SPIGCR1_SPIENA_MASK BIT(24)
76 #define SPIBUF_TXFULL_MASK BIT(29)
77 #define SPIBUF_RXEMPTY_MASK BIT(31)
80 #define SPIDELAY_C2TDELAY_SHIFT 24
81 #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
82 #define SPIDELAY_T2CDELAY_SHIFT 16
83 #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
84 #define SPIDELAY_T2EDELAY_SHIFT 8
85 #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
86 #define SPIDELAY_C2EDELAY_SHIFT 0
87 #define SPIDELAY_C2EDELAY_MASK 0xFF
90 #define SPIFLG_DLEN_ERR_MASK BIT(0)
91 #define SPIFLG_TIMEOUT_MASK BIT(1)
92 #define SPIFLG_PARERR_MASK BIT(2)
93 #define SPIFLG_DESYNC_MASK BIT(3)
94 #define SPIFLG_BITERR_MASK BIT(4)
95 #define SPIFLG_OVRRUN_MASK BIT(6)
96 #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
97 #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
98 | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
99 | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
100 | SPIFLG_OVRRUN_MASK)
102 #define SPIINT_DMA_REQ_EN BIT(16)
113 #define SPIDELAY 0x48
157 static void davinci_spi_rx_buf_u16(
u32 data,
struct davinci_spi *dspi)
213 bool gpio_chipsel =
false;
215 dspi = spi_master_get_devdata(spi->
master);
251 static inline int davinci_spi_get_prescale(
struct davinci_spi *dspi,
258 if (ret < 3 || ret > 256)
273 static int davinci_spi_setup_transfer(
struct spi_device *spi,
279 u8 bits_per_word = 0;
282 dspi = spi_master_get_devdata(spi->
master);
285 spicfg = &davinci_spi_default_cfg;
300 if (bits_per_word <= 8 && bits_per_word >= 2) {
301 dspi->
get_rx = davinci_spi_rx_buf_u8;
302 dspi->
get_tx = davinci_spi_tx_buf_u8;
304 }
else if (bits_per_word <= 16 && bits_per_word >= 2) {
305 dspi->
get_rx = davinci_spi_rx_buf_u16;
306 dspi->
get_tx = davinci_spi_tx_buf_u16;
316 prescale = davinci_spi_get_prescale(dspi, hz);
388 static int davinci_spi_setup(
struct spi_device *spi)
394 dspi = spi_master_get_devdata(spi->
master);
424 dev_dbg(sdev,
"SPI Time-out Error\n");
428 dev_dbg(sdev,
"SPI Desynchronization Error\n");
432 dev_dbg(sdev,
"SPI Bit error\n");
438 dev_dbg(sdev,
"SPI Data Length Error\n");
442 dev_dbg(sdev,
"SPI Parity Error\n");
446 dev_dbg(sdev,
"SPI Data Overrun error\n");
450 dev_dbg(sdev,
"SPI Buffer Init Active\n");
465 static int davinci_spi_process_events(
struct davinci_spi *dspi)
472 dspi->
get_rx(buf & 0xFFFF, dspi);
487 spidat1 |= 0xFFFF & dspi->
get_tx(dspi);
495 static void davinci_spi_dma_rx_callback(
void *data)
505 static void davinci_spi_dma_tx_callback(
void *data)
533 void *dummy_buf =
NULL;
536 dspi = spi_master_get_devdata(spi->
master);
540 spicfg = &davinci_spi_default_cfg;
543 data_type = dspi->bytes_per_word[spi->
chip_select];
548 dspi->rcount = dspi->wcount;
563 tx_data = dspi->get_tx(dspi);
564 spidat1 &= 0xFFFF0000;
565 spidat1 |= tx_data & 0xFFFF;
570 .src_addr = (
unsigned long)dspi->pbase +
SPIBUF,
571 .src_addr_width = data_type,
577 .dst_addr_width = data_type,
586 goto err_alloc_dummy_buf;
588 dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
589 dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);
619 rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
625 txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
631 rxdesc->
callback = davinci_spi_dma_rx_callback;
633 txdesc->
callback = davinci_spi_dma_tx_callback;
639 dmaengine_submit(rxdesc);
640 dmaengine_submit(txdesc);
642 dma_async_issue_pending(dspi->dma_rx);
643 dma_async_issue_pending(dspi->dma_tx);
652 while (dspi->rcount > 0 || dspi->wcount > 0) {
653 errors = davinci_spi_process_events(dspi);
679 ret = davinci_spi_check_error(dspi, errors);
680 WARN(!ret,
"%s: error reported but no error found!\n",
681 dev_name(&spi->
dev));
685 if (dspi->rcount != 0 || dspi->wcount != 0) {
686 dev_err(&spi->
dev,
"SPI data transfer error\n");
718 status = davinci_spi_process_events(dspi);
728 static int davinci_spi_request_dma(
struct davinci_spi *dspi)
740 dev_err(sdev,
"request RX DMA channel failed\n");
748 dev_err(sdev,
"request TX DMA channel failed\n");
783 pdata = pdev->
dev.platform_data;
790 if (master ==
NULL) {
797 dspi = spi_master_get_devdata(master);
825 if (dspi->
irq <= 0) {
835 dspi->
bitbang.master = spi_master_get(master);
842 if (IS_ERR(dspi->
clk)) {
850 master->
setup = davinci_spi_setup;
852 dspi->
bitbang.chipselect = davinci_spi_chipselect;
853 dspi->
bitbang.setup_transfer = davinci_spi_setup_transfer;
863 dma_rx_chan = r->
start;
866 dma_tx_chan = r->
start;
868 dspi->
bitbang.txrx_bufs = davinci_spi_bufs;
874 ret = davinci_spi_request_dma(dspi);
879 dev_info(&pdev->
dev,
"DMA: RX channel: %d, TX channel: %d, "
880 "event queue: %d\n", dma_rx_chan, dma_tx_chan,
884 dspi->
get_rx = davinci_spi_rx_buf_u8;
885 dspi->
get_tx = davinci_spi_tx_buf_u8;
887 init_completion(&dspi->
done);
933 spi_master_put(master);
962 dspi = spi_master_get_devdata(master);
968 spi_master_put(master);
979 .name =
"spi_davinci",
982 .probe = davinci_spi_probe,