26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #include <linux/device.h>
31 #include <linux/resource.h>
42 static unsigned usedma = 1;
50 #define AU1550_SPI_DBDMA_DESCRIPTORS 1
51 #define AU1550_SPI_DMA_RXTMP_MINSIZE 2048U
98 .dev_physaddr = 0x00000000,
103 static int ddma_memid;
105 static void au1550_spi_bits_handlers_set(
struct au1550_spi *
hw,
int bpw);
117 static u32 au1550_spi_baudcfg(
struct au1550_spi *
hw,
unsigned speed_hz)
119 u32 mainclk_hz = hw->
pdata->mainclk_hz;
122 for (div = 0; div < 4; div++) {
123 brg = mainclk_hz / speed_hz / (4 <<
div);
140 static inline void au1550_spi_mask_ack_all(
struct au1550_spi *hw)
155 static void au1550_spi_reset_fifos(
struct au1550_spi *hw)
184 if (hw->
pdata->deactivate_cs)
229 if (hw->
pdata->activate_cs)
251 if (bpw < 4 || bpw > 24) {
252 dev_err(&spi->
dev,
"setupxfer: invalid bits_per_word=%d\n",
257 dev_err(&spi->
dev,
"setupxfer: clock rate=%d out of range\n",
269 if (hw->
usedma && bpw <= 8)
278 cfg |= au1550_spi_baudcfg(hw, hz);
287 }
while ((stat & PSC_SPISTAT_DR) == 0);
290 au1550_spi_reset_fifos(hw);
291 au1550_spi_mask_ack_all(hw);
295 static int au1550_spi_setup(
struct spi_device *spi)
300 dev_err(&spi->
dev,
"setup: invalid bits_per_word=%d\n",
326 static int au1550_spi_dma_rxtmp_alloc(
struct au1550_spi *hw,
unsigned size)
343 static void au1550_spi_dma_rxtmp_free(
struct au1550_spi *hw)
398 au1550_spi_dma_rxtmp_free(hw);
399 ret = au1550_spi_dma_rxtmp_alloc(hw,
max(t->
len,
425 dev_err(hw->
dev,
"tx dma put source error\n");
480 au1550_spi_mask_ack_all(hw);
490 au1550_spi_reset_fifos(hw);
494 "dma transfer: receive FIFO overflow!\n");
497 "dma transfer: unexpected SPI error "
498 "(event=0x%x stat=0x%x)!\n", evnt, stat);
506 au1550_spi_mask_ack_all(hw);
516 #define AU1550_SPI_RX_WORD(size, mask) \
517 static void au1550_spi_rx_word_##size(struct au1550_spi *hw) \
519 u32 fifoword = hw->regs->psc_spitxrx & (u32)(mask); \
522 *(u##size *)hw->rx = (u##size)fifoword; \
523 hw->rx += (size) / 8; \
525 hw->rx_count += (size) / 8; \
528 #define AU1550_SPI_TX_WORD(size, mask) \
529 static void au1550_spi_tx_word_##size(struct au1550_spi *hw) \
533 fifoword = *(u##size *)hw->tx & (u32)(mask); \
534 hw->tx += (size) / 8; \
536 hw->tx_count += (size) / 8; \
537 if (hw->tx_count >= hw->len) \
538 fifoword |= PSC_SPITXRX_LC; \
539 hw->regs->psc_spitxrx = fifoword; \
545 AU1550_SPI_RX_WORD(32,0xffffff)
547 AU1550_SPI_TX_WORD(16,0xffff)
548 AU1550_SPI_TX_WORD(32,0xffffff)
553 struct au1550_spi *hw = spi_master_get_devdata(spi->master);
601 if ((stat & PSC_SPISTAT_DI) == 0) {
614 au1550_spi_mask_ack_all(hw);
615 au1550_spi_reset_fifos(hw);
617 "pio transfer: unexpected SPI error "
618 "(event=0x%x stat=0x%x)!\n", evnt, stat);
677 au1550_spi_mask_ack_all(hw);
695 static void au1550_spi_bits_handlers_set(
struct au1550_spi *hw,
int bpw)
702 hw->
rx_word = &au1550_spi_rx_word_8;
703 hw->
tx_word = &au1550_spi_tx_word_8;
707 }
else if (bpw <= 16) {
708 hw->
rx_word = &au1550_spi_rx_word_16;
709 hw->
tx_word = &au1550_spi_tx_word_16;
713 hw->
rx_word = &au1550_spi_rx_word_32;
714 hw->
tx_word = &au1550_spi_tx_word_32;
748 #ifdef AU1550_SPI_DEBUG_LOOPBACK
755 au1550_spi_mask_ack_all(hw);
763 }
while ((stat & PSC_SPISTAT_DR) == 0);
765 au1550_spi_reset_fifos(hw);
777 if (master ==
NULL) {
778 dev_err(&pdev->
dev,
"No memory for spi_master\n");
786 hw = spi_master_get_devdata(master);
788 hw->
master = spi_master_get(master);
789 hw->
pdata = pdev->
dev.platform_data;
793 dev_err(&pdev->
dev,
"No platform data supplied\n");
813 if (usedma && ddma_memid) {
814 if (pdev->
dev.dma_mask ==
NULL)
832 dev_err(&pdev->
dev,
"Cannot reserve iomem region\n");
844 platform_set_drvdata(pdev, hw);
849 hw->
bitbang.setup_transfer = au1550_spi_setupxfer;
850 hw->
bitbang.chipselect = au1550_spi_chipsel;
851 hw->
bitbang.master->setup = au1550_spi_setup;
852 hw->
bitbang.txrx_bufs = au1550_spi_txrx_bufs;
859 "Cannot allocate tx dma channel\n");
867 "Cannot allocate tx dma descriptors\n");
869 goto err_no_txdma_descr;
874 ddma_memid,
NULL, (
void *)hw);
877 "Cannot allocate rx dma channel\n");
885 "Cannot allocate rx dma descriptors\n");
887 goto err_no_rxdma_descr;
890 err = au1550_spi_dma_rxtmp_alloc(hw,
894 "Cannot allocate initial rx dma tmp buffer\n");
895 goto err_dma_rxtmp_alloc;
899 au1550_spi_bits_handlers_set(hw, 8);
920 int min_div = (2 << 0) * (2 * (4 + 1));
921 int max_div = (2 << 3) * (2 * (63 + 1));
926 au1550_spi_setup_psc_as_spi(hw);
930 dev_err(&pdev->
dev,
"Failed to register SPI master\n");
935 "spi master registered: bus_num=%d num_chipselect=%d\n",
944 au1550_spi_dma_rxtmp_free(hw);
965 spi_master_put(hw->
master);
973 struct au1550_spi *hw = platform_get_drvdata(pdev);
975 dev_info(&pdev->
dev,
"spi master remove: bus_num=%d\n",
985 au1550_spi_dma_rxtmp_free(hw);
990 platform_set_drvdata(pdev,
NULL);
992 spi_master_put(hw->
master);
1000 .remove =
__exit_p(au1550_spi_remove),
1002 .name =
"au1550-spi",
1007 static int __init au1550_spi_init(
void)
1023 static void __exit au1550_spi_exit(
void)
1025 if (usedma && ddma_memid)