9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
16 #include <linux/bitops.h>
24 #define DRIVER_NAME "sirfsoc_spi"
26 #define SIRFSOC_SPI_CTRL 0x0000
27 #define SIRFSOC_SPI_CMD 0x0004
28 #define SIRFSOC_SPI_TX_RX_EN 0x0008
29 #define SIRFSOC_SPI_INT_EN 0x000C
30 #define SIRFSOC_SPI_INT_STATUS 0x0010
31 #define SIRFSOC_SPI_TX_DMA_IO_CTRL 0x0100
32 #define SIRFSOC_SPI_TX_DMA_IO_LEN 0x0104
33 #define SIRFSOC_SPI_TXFIFO_CTRL 0x0108
34 #define SIRFSOC_SPI_TXFIFO_LEVEL_CHK 0x010C
35 #define SIRFSOC_SPI_TXFIFO_OP 0x0110
36 #define SIRFSOC_SPI_TXFIFO_STATUS 0x0114
37 #define SIRFSOC_SPI_TXFIFO_DATA 0x0118
38 #define SIRFSOC_SPI_RX_DMA_IO_CTRL 0x0120
39 #define SIRFSOC_SPI_RX_DMA_IO_LEN 0x0124
40 #define SIRFSOC_SPI_RXFIFO_CTRL 0x0128
41 #define SIRFSOC_SPI_RXFIFO_LEVEL_CHK 0x012C
42 #define SIRFSOC_SPI_RXFIFO_OP 0x0130
43 #define SIRFSOC_SPI_RXFIFO_STATUS 0x0134
44 #define SIRFSOC_SPI_RXFIFO_DATA 0x0138
45 #define SIRFSOC_SPI_DUMMY_DELAY_CTL 0x0144
48 #define SIRFSOC_SPI_SLV_MODE BIT(16)
49 #define SIRFSOC_SPI_CMD_MODE BIT(17)
50 #define SIRFSOC_SPI_CS_IO_OUT BIT(18)
51 #define SIRFSOC_SPI_CS_IO_MODE BIT(19)
52 #define SIRFSOC_SPI_CLK_IDLE_STAT BIT(20)
53 #define SIRFSOC_SPI_CS_IDLE_STAT BIT(21)
54 #define SIRFSOC_SPI_TRAN_MSB BIT(22)
55 #define SIRFSOC_SPI_DRV_POS_EDGE BIT(23)
56 #define SIRFSOC_SPI_CS_HOLD_TIME BIT(24)
57 #define SIRFSOC_SPI_CLK_SAMPLE_MODE BIT(25)
58 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_8 (0 << 26)
59 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_12 (1 << 26)
60 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_16 (2 << 26)
61 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_32 (3 << 26)
62 #define SIRFSOC_SPI_CMD_BYTE_NUM(x) ((x & 3) << 28)
63 #define SIRFSOC_SPI_ENA_AUTO_CLR BIT(30)
64 #define SIRFSOC_SPI_MUL_DAT_MODE BIT(31)
67 #define SIRFSOC_SPI_RX_DONE_INT_EN BIT(0)
68 #define SIRFSOC_SPI_TX_DONE_INT_EN BIT(1)
69 #define SIRFSOC_SPI_RX_OFLOW_INT_EN BIT(2)
70 #define SIRFSOC_SPI_TX_UFLOW_INT_EN BIT(3)
71 #define SIRFSOC_SPI_RX_IO_DMA_INT_EN BIT(4)
72 #define SIRFSOC_SPI_TX_IO_DMA_INT_EN BIT(5)
73 #define SIRFSOC_SPI_RXFIFO_FULL_INT_EN BIT(6)
74 #define SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN BIT(7)
75 #define SIRFSOC_SPI_RXFIFO_THD_INT_EN BIT(8)
76 #define SIRFSOC_SPI_TXFIFO_THD_INT_EN BIT(9)
77 #define SIRFSOC_SPI_FRM_END_INT_EN BIT(10)
79 #define SIRFSOC_SPI_INT_MASK_ALL 0x1FFF
82 #define SIRFSOC_SPI_RX_DONE BIT(0)
83 #define SIRFSOC_SPI_TX_DONE BIT(1)
84 #define SIRFSOC_SPI_RX_OFLOW BIT(2)
85 #define SIRFSOC_SPI_TX_UFLOW BIT(3)
86 #define SIRFSOC_SPI_RX_FIFO_FULL BIT(6)
87 #define SIRFSOC_SPI_TXFIFO_EMPTY BIT(7)
88 #define SIRFSOC_SPI_RXFIFO_THD_REACH BIT(8)
89 #define SIRFSOC_SPI_TXFIFO_THD_REACH BIT(9)
90 #define SIRFSOC_SPI_FRM_END BIT(10)
93 #define SIRFSOC_SPI_RX_EN BIT(0)
94 #define SIRFSOC_SPI_TX_EN BIT(1)
95 #define SIRFSOC_SPI_CMD_TX_EN BIT(2)
97 #define SIRFSOC_SPI_IO_MODE_SEL BIT(0)
98 #define SIRFSOC_SPI_RX_DMA_FLUSH BIT(2)
101 #define SIRFSOC_SPI_FIFO_RESET BIT(0)
102 #define SIRFSOC_SPI_FIFO_START BIT(1)
105 #define SIRFSOC_SPI_FIFO_WIDTH_BYTE (0 << 0)
106 #define SIRFSOC_SPI_FIFO_WIDTH_WORD (1 << 0)
107 #define SIRFSOC_SPI_FIFO_WIDTH_DWORD (2 << 0)
110 #define SIRFSOC_SPI_FIFO_LEVEL_MASK 0xFF
111 #define SIRFSOC_SPI_FIFO_FULL BIT(8)
112 #define SIRFSOC_SPI_FIFO_EMPTY BIT(9)
115 #define SIRFSOC_SPI_FIFO_SIZE 256
116 #define SIRFSOC_SPI_DAT_FRM_LEN_MAX (64 * 1024)
118 #define SIRFSOC_SPI_FIFO_SC(x) ((x) & 0x3F)
119 #define SIRFSOC_SPI_FIFO_LC(x) (((x) & 0x3F) << 10)
120 #define SIRFSOC_SPI_FIFO_HC(x) (((x) & 0x3F) << 20)
121 #define SIRFSOC_SPI_FIFO_THD(x) (((x) & 0xFF) << 2)
151 static void spi_sirfsoc_rx_word_u8(
struct sirfsoc_spi *sspi)
166 static void spi_sirfsoc_tx_word_u8(
struct sirfsoc_spi *sspi)
180 static void spi_sirfsoc_rx_word_u16(
struct sirfsoc_spi *sspi)
195 static void spi_sirfsoc_tx_word_u16(
struct sirfsoc_spi *sspi)
198 const u16 *tx = sspi->
tx;
209 static void spi_sirfsoc_rx_word_u32(
struct sirfsoc_spi *sspi)
225 static void spi_sirfsoc_tx_word_u32(
struct sirfsoc_spi *sspi)
228 const u32 *tx = sspi->
tx;
239 static void spi_sirfsoc_tasklet_tx(
unsigned long arg)
290 sspi = spi_master_get_devdata(spi->
master);
377 u8 bits_per_word = 0;
380 u32 txfifo_ctrl, rxfifo_ctrl;
383 sspi = spi_master_get_devdata(spi->
master);
392 regval = (sspi->
ctrl_freq / (2 * hz)) - 1;
394 if (regval > 0xFFFF || regval < 0) {
395 dev_err(&spi->
dev,
"Speed %d not supported\n", hz);
399 switch (bits_per_word) {
402 sspi->
rx_word = spi_sirfsoc_rx_word_u8;
403 sspi->
tx_word = spi_sirfsoc_tx_word_u8;
413 sspi->
rx_word = spi_sirfsoc_rx_word_u16;
414 sspi->
tx_word = spi_sirfsoc_tx_word_u16;
422 sspi->
rx_word = spi_sirfsoc_rx_word_u32;
423 sspi->
tx_word = spi_sirfsoc_tx_word_u32;
430 dev_err(&spi->
dev,
"Bits per word %d not supported\n",
467 static int spi_sirfsoc_setup(
struct spi_device *spi)
474 sspi = spi_master_get_devdata(spi->
master);
479 return spi_sirfsoc_setup_transfer(spi,
NULL);
487 int num_cs, cs_gpio, irq;
491 ret = of_property_read_u32(pdev->
dev.of_node,
492 "sirf,spi-num-chipselects", &num_cs);
494 dev_err(&pdev->
dev,
"Unable to get chip select number\n");
500 dev_err(&pdev->
dev,
"Unable to allocate SPI master\n");
503 platform_set_drvdata(pdev, master);
504 sspi = spi_master_get_devdata(master);
508 dev_err(&pdev->
dev,
"Unable to get IO resource\n");
515 cs_gpio = of_get_named_gpio(pdev->
dev.of_node,
"cs-gpios", i);
517 dev_err(&pdev->
dev,
"can't get cs gpio from DT\n");
533 dev_err(&pdev->
dev,
"fail to request cs gpios\n");
550 ret = devm_request_irq(&pdev->
dev, irq, spi_sirfsoc_irq, 0,
555 sspi->
bitbang.master = spi_master_get(master);
556 sspi->
bitbang.chipselect = spi_sirfsoc_chipselect;
557 sspi->
bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
558 sspi->
bitbang.txrx_bufs = spi_sirfsoc_transfer;
559 sspi->
bitbang.master->setup = spi_sirfsoc_setup;
561 sspi->
bitbang.master->dev.of_node = pdev->
dev.of_node;
563 sspi->
p = pinctrl_get_select_default(&pdev->
dev);
564 ret = IS_ERR(sspi->
p);
569 if (IS_ERR(sspi->
clk)) {
576 init_completion(&sspi->
done);
579 (
unsigned long)sspi);
602 spi_master_put(master);
613 master = platform_get_drvdata(pdev);
614 sspi = spi_master_get_devdata(master);
624 spi_master_put(master);
629 static int spi_sirfsoc_suspend(
struct device *
dev)
632 struct spi_master *master = platform_get_drvdata(pdev);
633 struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
639 static int spi_sirfsoc_resume(
struct device *
dev)
642 struct spi_master *master = platform_get_drvdata(pdev);
643 struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
654 static const struct dev_pm_ops spi_sirfsoc_pm_ops = {
655 .
suspend = spi_sirfsoc_suspend,
656 .resume = spi_sirfsoc_resume,
660 static const struct of_device_id spi_sirfsoc_of_match[] = {
661 { .compatible =
"sirf,prima2-spi", },
671 .pm = &spi_sirfsoc_pm_ops,
673 .of_match_table = spi_sirfsoc_of_match,
675 .probe = spi_sirfsoc_probe,