24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/errno.h>
28 #include <linux/list.h>
40 #define RSPI_SPCR 0x00
41 #define RSPI_SSLP 0x01
42 #define RSPI_SPPCR 0x02
43 #define RSPI_SPSR 0x03
44 #define RSPI_SPDR 0x04
45 #define RSPI_SPSCR 0x08
46 #define RSPI_SPSSR 0x09
47 #define RSPI_SPBR 0x0a
48 #define RSPI_SPDCR 0x0b
49 #define RSPI_SPCKD 0x0c
50 #define RSPI_SSLND 0x0d
51 #define RSPI_SPND 0x0e
52 #define RSPI_SPCR2 0x0f
53 #define RSPI_SPCMD0 0x10
54 #define RSPI_SPCMD1 0x12
55 #define RSPI_SPCMD2 0x14
56 #define RSPI_SPCMD3 0x16
57 #define RSPI_SPCMD4 0x18
58 #define RSPI_SPCMD5 0x1a
59 #define RSPI_SPCMD6 0x1c
60 #define RSPI_SPCMD7 0x1e
63 #define SPCR_SPRIE 0x80
65 #define SPCR_SPTIE 0x20
66 #define SPCR_SPEIE 0x10
67 #define SPCR_MSTR 0x08
68 #define SPCR_MODFEN 0x04
69 #define SPCR_TXMD 0x02
70 #define SPCR_SPMS 0x01
73 #define SSLP_SSL1P 0x02
74 #define SSLP_SSL0P 0x01
77 #define SPPCR_MOIFE 0x20
78 #define SPPCR_MOIFV 0x10
79 #define SPPCR_SPOM 0x04
80 #define SPPCR_SPLP2 0x02
81 #define SPPCR_SPLP 0x01
84 #define SPSR_SPRF 0x80
85 #define SPSR_SPTEF 0x20
86 #define SPSR_PERF 0x08
87 #define SPSR_MODF 0x04
88 #define SPSR_IDLNF 0x02
89 #define SPSR_OVRF 0x01
92 #define SPSCR_SPSLN_MASK 0x07
95 #define SPSSR_SPECM_MASK 0x70
96 #define SPSSR_SPCP_MASK 0x07
99 #define SPDCR_SPLW 0x20
100 #define SPDCR_SPRDTD 0x10
101 #define SPDCR_SLSEL1 0x08
102 #define SPDCR_SLSEL0 0x04
103 #define SPDCR_SLSEL_MASK 0x0c
104 #define SPDCR_SPFC1 0x02
105 #define SPDCR_SPFC0 0x01
108 #define SPCKD_SCKDL_MASK 0x07
111 #define SSLND_SLNDL_MASK 0x07
114 #define SPND_SPNDL_MASK 0x07
117 #define SPCR2_PTE 0x08
118 #define SPCR2_SPIE 0x04
119 #define SPCR2_SPOE 0x02
120 #define SPCR2_SPPE 0x01
123 #define SPCMD_SCKDEN 0x8000
124 #define SPCMD_SLNDEN 0x4000
125 #define SPCMD_SPNDEN 0x2000
126 #define SPCMD_LSBF 0x1000
127 #define SPCMD_SPB_MASK 0x0f00
128 #define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK)
129 #define SPCMD_SPB_20BIT 0x0000
130 #define SPCMD_SPB_24BIT 0x0100
131 #define SPCMD_SPB_32BIT 0x0200
132 #define SPCMD_SSLKP 0x0080
133 #define SPCMD_SSLA_MASK 0x0030
134 #define SPCMD_BRDV_MASK 0x000c
135 #define SPCMD_CPOL 0x0002
136 #define SPCMD_CPHA 0x0001
178 static unsigned char rspi_calc_spbr(
struct rspi_data *rspi)
184 spbr =
clamp(tmp, 0, 255);
199 static int rspi_wait_for_interrupt(
struct rspi_data *rspi,
u8 wait_mask,
205 rspi_enable_irq(rspi, enable_bit);
207 if (ret == 0 && !(rspi->
spsr & wait_mask))
213 static void rspi_assert_ssl(
struct rspi_data *rspi)
218 static void rspi_negate_ssl(
struct rspi_data *rspi)
229 rspi_write8(rspi, rspi_calc_spbr(rspi),
RSPI_SPBR);
265 "%s: tx empty timeout\n", __func__);
280 static void rspi_dma_complete(
void *
arg)
293 sg_set_buf(sg, buf, len);
304 static void rspi_memory_to_8bit(
void *
buf,
const void *data,
unsigned len)
310 *dst++ = (
u16)(*src++);
315 static void rspi_memory_from_8bit(
void *buf,
const void *data,
unsigned len)
346 rspi_memory_to_8bit(buf, t->
tx_buf, t->
len);
375 dmaengine_submit(desc);
376 dma_async_issue_pending(rspi->
chan_tx);
397 static void rspi_receive_init(
struct rspi_data *rspi)
405 rspi_write8(rspi, rspi_read8(rspi,
RSPI_SPSR) & ~SPSR_OVRF,
415 rspi_receive_init(rspi);
424 "%s: tx empty timeout\n", __func__);
430 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF,
SPCR_SPRIE) < 0) {
432 "%s: receive timeout\n", __func__);
476 if (!rspi_dma_map_sg(&sg_dummy, dummy, len, rspi->
chan_tx,
481 desc_dummy = dmaengine_prep_slave_sg(rspi->
chan_tx, &sg_dummy, 1,
485 goto end_dummy_mapped;
489 if (!rspi_dma_map_sg(&
sg, rx_buf, len, rspi->
chan_rx,
492 goto end_dummy_mapped;
502 rspi_receive_init(rspi);
516 dmaengine_submit(desc);
517 dma_async_issue_pending(rspi->
chan_rx);
520 dmaengine_submit(desc_dummy);
521 dma_async_issue_pending(rspi->
chan_tx);
540 rspi_memory_from_8bit(t->
rx_buf, rx_buf, t->
len);
568 while (!list_empty(&rspi->
queue)) {
570 list_del_init(&mesg->
queue);
571 spin_unlock_irqrestore(&rspi->
lock, flags);
573 rspi_assert_ssl(rspi);
577 if (rspi_is_dma(rspi, t))
578 ret = rspi_send_dma(rspi, t);
580 ret = rspi_send_pio(rspi, mesg, t);
585 if (rspi_is_dma(rspi, t))
586 ret = rspi_receive_dma(rspi, t);
588 ret = rspi_receive_pio(rspi, mesg, t);
594 rspi_negate_ssl(rspi);
617 rspi_set_config_register(rspi, 8);
633 spin_unlock_irqrestore(&rspi->
lock, flags);
638 static void rspi_cleanup(
struct spi_device *spi)
650 if (spsr & SPSR_SPRF)
657 rspi_disable_irq(rspi, disable_irq);
686 ret = dmaengine_slave_config(rspi->
chan_rx, &
cfg);
701 ret = dmaengine_slave_config(rspi->
chan_tx, &
cfg);
725 rspi_release_dma(rspi);
729 spi_master_put(rspi->
master);
751 dev_err(&pdev->
dev,
"platform_get_irq error\n");
756 if (master ==
NULL) {
757 dev_err(&pdev->
dev,
"spi_alloc_master error.\n");
761 rspi = spi_master_get_devdata(master);
772 snprintf(clk_name,
sizeof(clk_name),
"rspi%d", pdev->
id);
774 if (IS_ERR(rspi->
clk)) {
776 ret = PTR_ERR(rspi->
clk);
781 INIT_LIST_HEAD(&rspi->
queue);
788 master->
setup = rspi_setup;
790 master->
cleanup = rspi_cleanup;
799 ret = rspi_request_dma(rspi, pdev);
801 dev_err(&pdev->
dev,
"rspi_request_dma failed.\n");
807 dev_err(&pdev->
dev,
"spi_register_master error.\n");
816 rspi_release_dma(rspi);
823 spi_master_put(master);