11 #include <linux/kernel.h>
14 #include <linux/module.h>
21 #include <linux/slab.h>
24 #include <mach/board.h>
31 #define SPI_RDR 0x0008
32 #define SPI_TDR 0x000c
34 #define SPI_IER 0x0014
35 #define SPI_IDR 0x0018
36 #define SPI_IMR 0x001c
37 #define SPI_CSR0 0x0030
38 #define SPI_CSR1 0x0034
39 #define SPI_CSR2 0x0038
40 #define SPI_CSR3 0x003c
41 #define SPI_RPR 0x0100
42 #define SPI_RCR 0x0104
43 #define SPI_TPR 0x0108
44 #define SPI_TCR 0x010c
45 #define SPI_RNPR 0x0110
46 #define SPI_RNCR 0x0114
47 #define SPI_TNPR 0x0118
48 #define SPI_TNCR 0x011c
49 #define SPI_PTCR 0x0120
50 #define SPI_PTSR 0x0124
53 #define SPI_SPIEN_OFFSET 0
54 #define SPI_SPIEN_SIZE 1
55 #define SPI_SPIDIS_OFFSET 1
56 #define SPI_SPIDIS_SIZE 1
57 #define SPI_SWRST_OFFSET 7
58 #define SPI_SWRST_SIZE 1
59 #define SPI_LASTXFER_OFFSET 24
60 #define SPI_LASTXFER_SIZE 1
63 #define SPI_MSTR_OFFSET 0
64 #define SPI_MSTR_SIZE 1
65 #define SPI_PS_OFFSET 1
67 #define SPI_PCSDEC_OFFSET 2
68 #define SPI_PCSDEC_SIZE 1
69 #define SPI_FDIV_OFFSET 3
70 #define SPI_FDIV_SIZE 1
71 #define SPI_MODFDIS_OFFSET 4
72 #define SPI_MODFDIS_SIZE 1
73 #define SPI_LLB_OFFSET 7
74 #define SPI_LLB_SIZE 1
75 #define SPI_PCS_OFFSET 16
76 #define SPI_PCS_SIZE 4
77 #define SPI_DLYBCS_OFFSET 24
78 #define SPI_DLYBCS_SIZE 8
81 #define SPI_RD_OFFSET 0
82 #define SPI_RD_SIZE 16
85 #define SPI_TD_OFFSET 0
86 #define SPI_TD_SIZE 16
89 #define SPI_RDRF_OFFSET 0
90 #define SPI_RDRF_SIZE 1
91 #define SPI_TDRE_OFFSET 1
92 #define SPI_TDRE_SIZE 1
93 #define SPI_MODF_OFFSET 2
94 #define SPI_MODF_SIZE 1
95 #define SPI_OVRES_OFFSET 3
96 #define SPI_OVRES_SIZE 1
97 #define SPI_ENDRX_OFFSET 4
98 #define SPI_ENDRX_SIZE 1
99 #define SPI_ENDTX_OFFSET 5
100 #define SPI_ENDTX_SIZE 1
101 #define SPI_RXBUFF_OFFSET 6
102 #define SPI_RXBUFF_SIZE 1
103 #define SPI_TXBUFE_OFFSET 7
104 #define SPI_TXBUFE_SIZE 1
105 #define SPI_NSSR_OFFSET 8
106 #define SPI_NSSR_SIZE 1
107 #define SPI_TXEMPTY_OFFSET 9
108 #define SPI_TXEMPTY_SIZE 1
109 #define SPI_SPIENS_OFFSET 16
110 #define SPI_SPIENS_SIZE 1
113 #define SPI_CPOL_OFFSET 0
114 #define SPI_CPOL_SIZE 1
115 #define SPI_NCPHA_OFFSET 1
116 #define SPI_NCPHA_SIZE 1
117 #define SPI_CSAAT_OFFSET 3
118 #define SPI_CSAAT_SIZE 1
119 #define SPI_BITS_OFFSET 4
120 #define SPI_BITS_SIZE 4
121 #define SPI_SCBR_OFFSET 8
122 #define SPI_SCBR_SIZE 8
123 #define SPI_DLYBS_OFFSET 16
124 #define SPI_DLYBS_SIZE 8
125 #define SPI_DLYBCT_OFFSET 24
126 #define SPI_DLYBCT_SIZE 8
129 #define SPI_RXCTR_OFFSET 0
130 #define SPI_RXCTR_SIZE 16
133 #define SPI_TXCTR_OFFSET 0
134 #define SPI_TXCTR_SIZE 16
137 #define SPI_RXNCR_OFFSET 0
138 #define SPI_RXNCR_SIZE 16
141 #define SPI_TXNCR_OFFSET 0
142 #define SPI_TXNCR_SIZE 16
145 #define SPI_RXTEN_OFFSET 0
146 #define SPI_RXTEN_SIZE 1
147 #define SPI_RXTDIS_OFFSET 1
148 #define SPI_RXTDIS_SIZE 1
149 #define SPI_TXTEN_OFFSET 8
150 #define SPI_TXTEN_SIZE 1
151 #define SPI_TXTDIS_OFFSET 9
152 #define SPI_TXTDIS_SIZE 1
155 #define SPI_BITS_8_BPT 0
156 #define SPI_BITS_9_BPT 1
157 #define SPI_BITS_10_BPT 2
158 #define SPI_BITS_11_BPT 3
159 #define SPI_BITS_12_BPT 4
160 #define SPI_BITS_13_BPT 5
161 #define SPI_BITS_14_BPT 6
162 #define SPI_BITS_15_BPT 7
163 #define SPI_BITS_16_BPT 8
166 #define SPI_BIT(name) \
167 (1 << SPI_##name##_OFFSET)
168 #define SPI_BF(name,value) \
169 (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
170 #define SPI_BFEXT(name,value) \
171 (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
172 #define SPI_BFINS(name,value,old) \
173 ( ((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
174 | SPI_BF(name,value))
177 #define spi_readl(port,reg) \
178 __raw_readl((port)->regs + SPI_##reg)
179 #define spi_writel(port,reg,value) \
180 __raw_writel((value), (port)->regs + SPI_##reg)
214 #define BUFFER_SIZE PAGE_SIZE
215 #define INVALID_DMA_ADDRESS 0xffffffff
229 static bool atmel_spi_is_v2(
void)
265 if (atmel_spi_is_v2()) {
282 for (i = 0; i < spi->
master->num_chipselect; i++) {
284 if ((csr ^ cpol) &
SPI_BIT(CPOL))
296 dev_dbg(&spi->
dev,
"activate %u%s, mr %08x\n",
297 asd->
npcs_pin, active ?
" (high)" :
"",
316 dev_dbg(&spi->
dev,
"DEactivate %u%s, mr %08x\n",
317 asd->
npcs_pin, active ?
" (low)" :
"",
330 static inline int atmel_spi_xfer_can_be_chained(
struct spi_transfer *xfer)
335 static void atmel_spi_next_xfer_data(
struct spi_master *master,
341 struct atmel_spi *as = spi_master_get_devdata(master);
370 static void atmel_spi_next_xfer(
struct spi_master *master,
373 struct atmel_spi *as = spi_master_get_devdata(master);
392 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
393 remaining = xfer->
len -
len;
398 if (msg->
spi->bits_per_word > 8)
404 " start xfer %p: len %u tx %p/%08x rx %p/%08x\n",
417 else if (!atmel_spi_xfer_is_last(msg, xfer)
418 && atmel_spi_xfer_can_be_chained(xfer)) {
431 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
437 if (msg->
spi->bits_per_word > 8)
443 " next xfer %p: len %u tx %p/%08x rx %p/%08x\n",
467 static void atmel_spi_next_message(
struct spi_master *master)
469 struct atmel_spi *as = spi_master_get_devdata(master);
478 dev_dbg(master->
dev.parent,
"start message %p for %s\n",
479 msg, dev_name(&spi->
dev));
483 if (as->
stay != spi) {
484 cs_deactivate(as, as->
stay);
485 cs_activate(as, spi);
489 cs_activate(as, spi);
491 atmel_spi_next_xfer(master, msg);
510 void *nonconst_tx = (
void *)xfer->
tx_buf;
513 nonconst_tx, xfer->
len,
533 static void atmel_spi_dma_unmap_xfer(
struct spi_master *master,
548 if (!stay || status < 0)
549 cs_deactivate(as, msg->
spi);
557 "xfer complete: %u bytes transferred\n",
560 spin_unlock(&as->
lock);
562 spin_lock(&as->
lock);
571 atmel_spi_next_message(master);
575 atmel_spi_interrupt(
int irq,
void *
dev_id)
578 struct atmel_spi *as = spi_master_get_devdata(master);
584 spin_lock(&as->
lock);
591 pending = status &
imr;
593 if (pending &
SPI_BIT(OVRES)) {
614 atmel_spi_dma_unmap_xfer(master, xfer);
620 dev_warn(master->
dev.parent,
"overrun (%u/%u remaining)\n",
631 for (timeout = 1000; timeout; timeout--)
636 "timeout waiting for TXEMPTY");
643 atmel_spi_msg_done(master, as, msg, -
EIO, 0);
653 atmel_spi_dma_unmap_xfer(master, xfer);
659 if (atmel_spi_xfer_is_last(msg, xfer)) {
661 atmel_spi_msg_done(master, as, msg, 0,
665 cs_deactivate(as, msg->
spi);
667 cs_activate(as, msg->
spi);
675 atmel_spi_next_xfer(master, msg);
682 atmel_spi_next_xfer(master, msg);
686 spin_unlock(&as->
lock);
691 static int atmel_spi_setup(
struct spi_device *spi)
697 unsigned long bus_hz;
701 as = spi_master_get_devdata(spi->
master);
708 "setup: invalid chipselect %u (%u defined)\n",
713 if (bits < 8 || bits > 16) {
715 "setup: invalid bits_per_word %u (8 to 16)\n",
721 if (!atmel_spi_is_v2()
724 dev_dbg(&spi->
dev,
"setup: can't be active-high\n");
730 if (!atmel_spi_is_v2())
746 "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
792 cs_deactivate(as, spi);
793 spin_unlock_irqrestore(&as->
lock, flags);
799 "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
802 if (!atmel_spi_is_v2())
817 as = spi_master_get_devdata(spi->
master);
819 dev_dbg(controller,
"new message %p submitted for %s\n",
820 msg, dev_name(&spi->
dev));
836 bits = (asd->
csr >> 4) & 0xf;
839 "bits_per_word in transfers\n");
846 dev_dbg(&spi->
dev,
"no protocol options yet\n");
859 if (atmel_spi_dma_map_xfer(as, xfer) < 0)
867 " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
880 atmel_spi_next_message(spi->
master);
881 spin_unlock_irqrestore(&as->
lock, flags);
886 static void atmel_spi_cleanup(
struct spi_device *spi)
897 if (as->
stay == spi) {
899 cs_deactivate(as, spi);
901 spin_unlock_irqrestore(&as->
lock, flags);
942 master->
setup = atmel_spi_setup;
943 master->
transfer = atmel_spi_transfer;
944 master->
cleanup = atmel_spi_cleanup;
945 platform_set_drvdata(pdev, master);
947 as = spi_master_get_devdata(master);
959 INIT_LIST_HEAD(&as->
queue);
963 goto out_free_buffer;
968 dev_name(&pdev->
dev), master);
981 dev_info(&pdev->
dev,
"Atmel SPI Controller at 0x%08lx (irq %d)\n",
982 (
unsigned long)regs->
start, irq);
1002 spi_master_put(master);
1008 struct spi_master *master = platform_get_drvdata(pdev);
1009 struct atmel_spi *as = spi_master_get_devdata(master);
1013 spin_lock_irq(&as->
lock);
1018 spin_unlock_irq(&as->
lock);
1046 struct spi_master *master = platform_get_drvdata(pdev);
1047 struct atmel_spi *as = spi_master_get_devdata(master);
1055 struct spi_master *master = platform_get_drvdata(pdev);
1056 struct atmel_spi *as = spi_master_get_devdata(master);
1063 #define atmel_spi_suspend NULL
1064 #define atmel_spi_resume NULL
1070 .name =
"atmel_spi",
1075 .probe = atmel_spi_probe,
1076 .remove =
__exit_p(atmel_spi_remove),