4 * Xilinx SPI controller driver (master mode only)
6 * Author: MontaVista Software, Inc.
9 * 2002-2007 (c) MontaVista Software, Inc. This file is licensed under the
10 * terms of the GNU General Public License version 2. This program is licensed
11 * "as is" without any warranty of any kind, whether express or implied.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/spi/spi.h>
19 #include <linux/spi/spi_bitbang.h>
22 #include "xilinx_spi.h"
23 #include <linux/spi/xilinx_spi.h>
25 #define XILINX_SPI_NAME "xilinx_spi"
27 /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
28 * Product Specification", DS464
30 #define XSPI_CR_OFFSET 0x60 /* Control Register */
32 #define XSPI_CR_ENABLE 0x02
33 #define XSPI_CR_MASTER_MODE 0x04
34 #define XSPI_CR_CPOL 0x08
35 #define XSPI_CR_CPHA 0x10
36 #define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL)
37 #define XSPI_CR_TXFIFO_RESET 0x20
38 #define XSPI_CR_RXFIFO_RESET 0x40
39 #define XSPI_CR_MANUAL_SSELECT 0x80
40 #define XSPI_CR_TRANS_INHIBIT 0x100
41 #define XSPI_CR_LSB_FIRST 0x200
43 #define XSPI_SR_OFFSET 0x64 /* Status Register */
45 #define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
46 #define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
47 #define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
48 #define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
49 #define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
51 #define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */
52 #define XSPI_RXD_OFFSET 0x6c /* Data Receive Register */
54 #define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
56 /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
57 * IPIF registers are 32 bit
59 #define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
60 #define XIPIF_V123B_GINTR_ENABLE 0x80000000
62 #define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
63 #define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
65 #define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
66 #define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
68 #define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
69 #define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
70 #define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
71 #define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
72 #define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */
74 #define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
75 #define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
78 /* bitbang has to be first */
79 struct spi_bitbang bitbang
;
80 struct completion done
;
81 struct resource mem
; /* phys mem */
82 void __iomem
*regs
; /* virt. address of the control registers */
86 u8
*rx_ptr
; /* pointer in the Tx buffer */
87 const u8
*tx_ptr
; /* pointer in the Rx buffer */
88 int remaining_bytes
; /* the number of bytes left to transfer */
90 unsigned int (*read_fn
) (void __iomem
*);
91 void (*write_fn
) (u32
, void __iomem
*);
92 void (*tx_fn
) (struct xilinx_spi
*);
93 void (*rx_fn
) (struct xilinx_spi
*);
96 static void xspi_tx8(struct xilinx_spi
*xspi
)
98 xspi
->write_fn(*xspi
->tx_ptr
, xspi
->regs
+ XSPI_TXD_OFFSET
);
102 static void xspi_tx16(struct xilinx_spi
*xspi
)
104 xspi
->write_fn(*(u16
*)(xspi
->tx_ptr
), xspi
->regs
+ XSPI_TXD_OFFSET
);
108 static void xspi_tx32(struct xilinx_spi
*xspi
)
110 xspi
->write_fn(*(u32
*)(xspi
->tx_ptr
), xspi
->regs
+ XSPI_TXD_OFFSET
);
114 static void xspi_rx8(struct xilinx_spi
*xspi
)
116 u32 data
= xspi
->read_fn(xspi
->regs
+ XSPI_RXD_OFFSET
);
118 *xspi
->rx_ptr
= data
& 0xff;
123 static void xspi_rx16(struct xilinx_spi
*xspi
)
125 u32 data
= xspi
->read_fn(xspi
->regs
+ XSPI_RXD_OFFSET
);
127 *(u16
*)(xspi
->rx_ptr
) = data
& 0xffff;
132 static void xspi_rx32(struct xilinx_spi
*xspi
)
134 u32 data
= xspi
->read_fn(xspi
->regs
+ XSPI_RXD_OFFSET
);
136 *(u32
*)(xspi
->rx_ptr
) = data
;
141 static void xspi_init_hw(struct xilinx_spi
*xspi
)
143 void __iomem
*regs_base
= xspi
->regs
;
145 /* Reset the SPI device */
146 xspi
->write_fn(XIPIF_V123B_RESET_MASK
,
147 regs_base
+ XIPIF_V123B_RESETR_OFFSET
);
148 /* Disable all the interrupts just in case */
149 xspi
->write_fn(0, regs_base
+ XIPIF_V123B_IIER_OFFSET
);
150 /* Enable the global IPIF interrupt */
151 xspi
->write_fn(XIPIF_V123B_GINTR_ENABLE
,
152 regs_base
+ XIPIF_V123B_DGIER_OFFSET
);
153 /* Deselect the slave on the SPI bus */
154 xspi
->write_fn(0xffff, regs_base
+ XSPI_SSR_OFFSET
);
155 /* Disable the transmitter, enable Manual Slave Select Assertion,
156 * put SPI controller into master mode, and enable it */
157 xspi
->write_fn(XSPI_CR_TRANS_INHIBIT
| XSPI_CR_MANUAL_SSELECT
|
158 XSPI_CR_MASTER_MODE
| XSPI_CR_ENABLE
| XSPI_CR_TXFIFO_RESET
|
159 XSPI_CR_RXFIFO_RESET
, regs_base
+ XSPI_CR_OFFSET
);
162 static void xilinx_spi_chipselect(struct spi_device
*spi
, int is_on
)
164 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
166 if (is_on
== BITBANG_CS_INACTIVE
) {
167 /* Deselect the slave on the SPI bus */
168 xspi
->write_fn(0xffff, xspi
->regs
+ XSPI_SSR_OFFSET
);
169 } else if (is_on
== BITBANG_CS_ACTIVE
) {
170 /* Set the SPI clock phase and polarity */
171 u16 cr
= xspi
->read_fn(xspi
->regs
+ XSPI_CR_OFFSET
)
172 & ~XSPI_CR_MODE_MASK
;
173 if (spi
->mode
& SPI_CPHA
)
175 if (spi
->mode
& SPI_CPOL
)
177 xspi
->write_fn(cr
, xspi
->regs
+ XSPI_CR_OFFSET
);
179 /* We do not check spi->max_speed_hz here as the SPI clock
180 * frequency is not software programmable (the IP block design
184 /* Activate the chip select */
185 xspi
->write_fn(~(0x0001 << spi
->chip_select
),
186 xspi
->regs
+ XSPI_SSR_OFFSET
);
190 /* spi_bitbang requires custom setup_transfer() to be defined if there is a
191 * custom txrx_bufs(). We have nothing to setup here as the SPI IP block
192 * supports 8 or 16 bits per word which cannot be changed in software.
193 * SPI clock can't be changed in software either.
194 * Check for correct bits per word. Chip select delay calculations could be
195 * added here as soon as bitbang_work() can be made aware of the delay value.
197 static int xilinx_spi_setup_transfer(struct spi_device
*spi
,
198 struct spi_transfer
*t
)
200 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
203 bits_per_word
= (t
&& t
->bits_per_word
)
204 ? t
->bits_per_word
: spi
->bits_per_word
;
205 if (bits_per_word
!= xspi
->bits_per_word
) {
206 dev_err(&spi
->dev
, "%s, unsupported bits_per_word=%d\n",
207 __func__
, bits_per_word
);
214 static int xilinx_spi_setup(struct spi_device
*spi
)
216 /* always return 0, we can not check the number of bits.
217 * There are cases when SPI setup is called before any driver is
218 * there, in that case the SPI core defaults to 8 bits, which we
219 * do not support in some cases. But if we return an error, the
220 * SPI device would not be registered and no driver can get hold of it
221 * When the driver is there, it will call SPI setup again with the
222 * correct number of bits per transfer.
223 * If a driver setups with the wrong bit number, it will fail when
224 * it tries to do a transfer
229 static void xilinx_spi_fill_tx_fifo(struct xilinx_spi
*xspi
)
233 /* Fill the Tx FIFO with as many bytes as possible */
234 sr
= xspi
->read_fn(xspi
->regs
+ XSPI_SR_OFFSET
);
235 while ((sr
& XSPI_SR_TX_FULL_MASK
) == 0 && xspi
->remaining_bytes
> 0) {
239 xspi
->write_fn(0, xspi
->regs
+ XSPI_TXD_OFFSET
);
240 xspi
->remaining_bytes
-= xspi
->bits_per_word
/ 8;
241 sr
= xspi
->read_fn(xspi
->regs
+ XSPI_SR_OFFSET
);
245 static int xilinx_spi_txrx_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
247 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
251 /* We get here with transmitter inhibited */
253 xspi
->tx_ptr
= t
->tx_buf
;
254 xspi
->rx_ptr
= t
->rx_buf
;
255 xspi
->remaining_bytes
= t
->len
;
256 INIT_COMPLETION(xspi
->done
);
258 xilinx_spi_fill_tx_fifo(xspi
);
260 /* Enable the transmit empty interrupt, which we use to determine
261 * progress on the transmission.
263 ipif_ier
= xspi
->read_fn(xspi
->regs
+ XIPIF_V123B_IIER_OFFSET
);
264 xspi
->write_fn(ipif_ier
| XSPI_INTR_TX_EMPTY
,
265 xspi
->regs
+ XIPIF_V123B_IIER_OFFSET
);
267 /* Start the transfer by not inhibiting the transmitter any longer */
268 cr
= xspi
->read_fn(xspi
->regs
+ XSPI_CR_OFFSET
) &
269 ~XSPI_CR_TRANS_INHIBIT
;
270 xspi
->write_fn(cr
, xspi
->regs
+ XSPI_CR_OFFSET
);
272 wait_for_completion(&xspi
->done
);
274 /* Disable the transmit empty interrupt */
275 xspi
->write_fn(ipif_ier
, xspi
->regs
+ XIPIF_V123B_IIER_OFFSET
);
277 return t
->len
- xspi
->remaining_bytes
;
281 /* This driver supports single master mode only. Hence Tx FIFO Empty
282 * is the only interrupt we care about.
283 * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
284 * Fault are not to happen.
286 static irqreturn_t
xilinx_spi_irq(int irq
, void *dev_id
)
288 struct xilinx_spi
*xspi
= dev_id
;
291 /* Get the IPIF interrupts, and clear them immediately */
292 ipif_isr
= xspi
->read_fn(xspi
->regs
+ XIPIF_V123B_IISR_OFFSET
);
293 xspi
->write_fn(ipif_isr
, xspi
->regs
+ XIPIF_V123B_IISR_OFFSET
);
295 if (ipif_isr
& XSPI_INTR_TX_EMPTY
) { /* Transmission completed */
299 /* A transmit has just completed. Process received data and
300 * check for more data to transmit. Always inhibit the
301 * transmitter while the Isr refills the transmit register/FIFO,
302 * or make sure it is stopped if we're done.
304 cr
= xspi
->read_fn(xspi
->regs
+ XSPI_CR_OFFSET
);
305 xspi
->write_fn(cr
| XSPI_CR_TRANS_INHIBIT
,
306 xspi
->regs
+ XSPI_CR_OFFSET
);
308 /* Read out all the data from the Rx FIFO */
309 sr
= xspi
->read_fn(xspi
->regs
+ XSPI_SR_OFFSET
);
310 while ((sr
& XSPI_SR_RX_EMPTY_MASK
) == 0) {
312 sr
= xspi
->read_fn(xspi
->regs
+ XSPI_SR_OFFSET
);
315 /* See if there is more data to send */
316 if (xspi
->remaining_bytes
> 0) {
317 xilinx_spi_fill_tx_fifo(xspi
);
318 /* Start the transfer by not inhibiting the
319 * transmitter any longer
321 xspi
->write_fn(cr
, xspi
->regs
+ XSPI_CR_OFFSET
);
323 /* No more data to send.
324 * Indicate the transfer is completed.
326 complete(&xspi
->done
);
333 struct spi_master
*xilinx_spi_init(struct device
*dev
, struct resource
*mem
,
334 u32 irq
, s16 bus_num
)
336 struct spi_master
*master
;
337 struct xilinx_spi
*xspi
;
338 struct xspi_platform_data
*pdata
= dev
->platform_data
;
342 dev_err(dev
, "No platform data attached\n");
346 master
= spi_alloc_master(dev
, sizeof(struct xilinx_spi
));
350 /* the spi->mode bits understood by this driver: */
351 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
;
353 xspi
= spi_master_get_devdata(master
);
354 xspi
->bitbang
.master
= spi_master_get(master
);
355 xspi
->bitbang
.chipselect
= xilinx_spi_chipselect
;
356 xspi
->bitbang
.setup_transfer
= xilinx_spi_setup_transfer
;
357 xspi
->bitbang
.txrx_bufs
= xilinx_spi_txrx_bufs
;
358 xspi
->bitbang
.master
->setup
= xilinx_spi_setup
;
359 init_completion(&xspi
->done
);
361 if (!request_mem_region(mem
->start
, resource_size(mem
),
365 xspi
->regs
= ioremap(mem
->start
, resource_size(mem
));
366 if (xspi
->regs
== NULL
) {
367 dev_warn(dev
, "ioremap failure\n");
371 master
->bus_num
= bus_num
;
372 master
->num_chipselect
= pdata
->num_chipselect
;
376 if (pdata
->little_endian
) {
377 xspi
->read_fn
= ioread32
;
378 xspi
->write_fn
= iowrite32
;
380 xspi
->read_fn
= ioread32be
;
381 xspi
->write_fn
= iowrite32be
;
383 xspi
->bits_per_word
= pdata
->bits_per_word
;
384 if (xspi
->bits_per_word
== 8) {
385 xspi
->tx_fn
= xspi_tx8
;
386 xspi
->rx_fn
= xspi_rx8
;
387 } else if (xspi
->bits_per_word
== 16) {
388 xspi
->tx_fn
= xspi_tx16
;
389 xspi
->rx_fn
= xspi_rx16
;
390 } else if (xspi
->bits_per_word
== 32) {
391 xspi
->tx_fn
= xspi_tx32
;
392 xspi
->rx_fn
= xspi_rx32
;
397 /* SPI controller initializations */
400 /* Register for SPI Interrupt */
401 ret
= request_irq(xspi
->irq
, xilinx_spi_irq
, 0, XILINX_SPI_NAME
, xspi
);
405 ret
= spi_bitbang_start(&xspi
->bitbang
);
407 dev_err(dev
, "spi_bitbang_start FAILED\n");
411 dev_info(dev
, "at 0x%08llX mapped to 0x%p, irq=%d\n",
412 (unsigned long long)mem
->start
, xspi
->regs
, xspi
->irq
);
416 free_irq(xspi
->irq
, xspi
);
420 release_mem_region(mem
->start
, resource_size(mem
));
422 spi_master_put(master
);
425 EXPORT_SYMBOL(xilinx_spi_init
);
427 void xilinx_spi_deinit(struct spi_master
*master
)
429 struct xilinx_spi
*xspi
;
431 xspi
= spi_master_get_devdata(master
);
433 spi_bitbang_stop(&xspi
->bitbang
);
434 free_irq(xspi
->irq
, xspi
);
437 release_mem_region(xspi
->mem
.start
, resource_size(&xspi
->mem
));
438 spi_master_put(xspi
->bitbang
.master
);
440 EXPORT_SYMBOL(xilinx_spi_deinit
);
442 MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
443 MODULE_DESCRIPTION("Xilinx SPI driver");
444 MODULE_LICENSE("GPL");