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>
17 #include <linux/platform_device.h>
18 #include <linux/spi/spi.h>
19 #include <linux/spi/spi_bitbang.h>
22 #include <syslib/virtex_devices.h>
24 #define XILINX_SPI_NAME "xilinx_spi"
26 /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
27 * Product Specification", DS464
29 #define XSPI_CR_OFFSET 0x62 /* 16-bit Control Register */
31 #define XSPI_CR_ENABLE 0x02
32 #define XSPI_CR_MASTER_MODE 0x04
33 #define XSPI_CR_CPOL 0x08
34 #define XSPI_CR_CPHA 0x10
35 #define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL)
36 #define XSPI_CR_TXFIFO_RESET 0x20
37 #define XSPI_CR_RXFIFO_RESET 0x40
38 #define XSPI_CR_MANUAL_SSELECT 0x80
39 #define XSPI_CR_TRANS_INHIBIT 0x100
41 #define XSPI_SR_OFFSET 0x67 /* 8-bit Status Register */
43 #define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
44 #define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
45 #define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
46 #define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
47 #define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
49 #define XSPI_TXD_OFFSET 0x6b /* 8-bit Data Transmit Register */
50 #define XSPI_RXD_OFFSET 0x6f /* 8-bit Data Receive Register */
52 #define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
54 /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
55 * IPIF registers are 32 bit
57 #define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
58 #define XIPIF_V123B_GINTR_ENABLE 0x80000000
60 #define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
61 #define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
63 #define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
64 #define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
66 #define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
67 #define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
68 #define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
69 #define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
71 #define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
72 #define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
75 /* bitbang has to be first */
76 struct spi_bitbang bitbang
;
77 struct completion done
;
79 void __iomem
*regs
; /* virt. address of the control registers */
83 u32 speed_hz
; /* SCK has a fixed frequency of speed_hz Hz */
85 u8
*rx_ptr
; /* pointer in the Tx buffer */
86 const u8
*tx_ptr
; /* pointer in the Rx buffer */
87 int remaining_bytes
; /* the number of bytes left to transfer */
90 static void xspi_init_hw(void __iomem
*regs_base
)
92 /* Reset the SPI device */
93 out_be32(regs_base
+ XIPIF_V123B_RESETR_OFFSET
,
94 XIPIF_V123B_RESET_MASK
);
95 /* Disable all the interrupts just in case */
96 out_be32(regs_base
+ XIPIF_V123B_IIER_OFFSET
, 0);
97 /* Enable the global IPIF interrupt */
98 out_be32(regs_base
+ XIPIF_V123B_DGIER_OFFSET
,
99 XIPIF_V123B_GINTR_ENABLE
);
100 /* Deselect the slave on the SPI bus */
101 out_be32(regs_base
+ XSPI_SSR_OFFSET
, 0xffff);
102 /* Disable the transmitter, enable Manual Slave Select Assertion,
103 * put SPI controller into master mode, and enable it */
104 out_be16(regs_base
+ XSPI_CR_OFFSET
,
105 XSPI_CR_TRANS_INHIBIT
| XSPI_CR_MANUAL_SSELECT
106 | XSPI_CR_MASTER_MODE
| XSPI_CR_ENABLE
);
109 static void xilinx_spi_chipselect(struct spi_device
*spi
, int is_on
)
111 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
113 if (is_on
== BITBANG_CS_INACTIVE
) {
114 /* Deselect the slave on the SPI bus */
115 out_be32(xspi
->regs
+ XSPI_SSR_OFFSET
, 0xffff);
116 } else if (is_on
== BITBANG_CS_ACTIVE
) {
117 /* Set the SPI clock phase and polarity */
118 u16 cr
= in_be16(xspi
->regs
+ XSPI_CR_OFFSET
)
119 & ~XSPI_CR_MODE_MASK
;
120 if (spi
->mode
& SPI_CPHA
)
122 if (spi
->mode
& SPI_CPOL
)
124 out_be16(xspi
->regs
+ XSPI_CR_OFFSET
, cr
);
126 /* We do not check spi->max_speed_hz here as the SPI clock
127 * frequency is not software programmable (the IP block design
131 /* Activate the chip select */
132 out_be32(xspi
->regs
+ XSPI_SSR_OFFSET
,
133 ~(0x0001 << spi
->chip_select
));
137 /* spi_bitbang requires custom setup_transfer() to be defined if there is a
138 * custom txrx_bufs(). We have nothing to setup here as the SPI IP block
139 * supports just 8 bits per word, and SPI clock can't be changed in software.
140 * Check for 8 bits per word. Chip select delay calculations could be
141 * added here as soon as bitbang_work() can be made aware of the delay value.
143 static int xilinx_spi_setup_transfer(struct spi_device
*spi
,
144 struct spi_transfer
*t
)
148 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
150 bits_per_word
= (t
) ? t
->bits_per_word
: spi
->bits_per_word
;
151 hz
= (t
) ? t
->speed_hz
: spi
->max_speed_hz
;
152 if (bits_per_word
!= 8) {
153 dev_err(&spi
->dev
, "%s, unsupported bits_per_word=%d\n",
154 __FUNCTION__
, bits_per_word
);
158 if (hz
&& xspi
->speed_hz
> hz
) {
159 dev_err(&spi
->dev
, "%s, unsupported clock rate %uHz\n",
167 /* the spi->mode bits understood by this driver: */
168 #define MODEBITS (SPI_CPOL | SPI_CPHA)
170 static int xilinx_spi_setup(struct spi_device
*spi
)
172 struct spi_bitbang
*bitbang
;
173 struct xilinx_spi
*xspi
;
176 xspi
= spi_master_get_devdata(spi
->master
);
177 bitbang
= &xspi
->bitbang
;
179 if (!spi
->bits_per_word
)
180 spi
->bits_per_word
= 8;
182 if (spi
->mode
& ~MODEBITS
) {
183 dev_err(&spi
->dev
, "%s, unsupported mode bits %x\n",
184 __FUNCTION__
, spi
->mode
& ~MODEBITS
);
188 retval
= xilinx_spi_setup_transfer(spi
, NULL
);
192 dev_dbg(&spi
->dev
, "%s, mode %d, %u bits/w, %u nsec/bit\n",
193 __FUNCTION__
, spi
->mode
& MODEBITS
, spi
->bits_per_word
, 0);
198 static void xilinx_spi_fill_tx_fifo(struct xilinx_spi
*xspi
)
202 /* Fill the Tx FIFO with as many bytes as possible */
203 sr
= in_8(xspi
->regs
+ XSPI_SR_OFFSET
);
204 while ((sr
& XSPI_SR_TX_FULL_MASK
) == 0 && xspi
->remaining_bytes
> 0) {
206 out_8(xspi
->regs
+ XSPI_TXD_OFFSET
, *xspi
->tx_ptr
++);
208 out_8(xspi
->regs
+ XSPI_TXD_OFFSET
, 0);
210 xspi
->remaining_bytes
--;
211 sr
= in_8(xspi
->regs
+ XSPI_SR_OFFSET
);
215 static int xilinx_spi_txrx_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
217 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
221 /* We get here with transmitter inhibited */
223 xspi
->tx_ptr
= t
->tx_buf
;
224 xspi
->rx_ptr
= t
->rx_buf
;
225 xspi
->remaining_bytes
= t
->len
;
226 INIT_COMPLETION(xspi
->done
);
228 xilinx_spi_fill_tx_fifo(xspi
);
230 /* Enable the transmit empty interrupt, which we use to determine
231 * progress on the transmission.
233 ipif_ier
= in_be32(xspi
->regs
+ XIPIF_V123B_IIER_OFFSET
);
234 out_be32(xspi
->regs
+ XIPIF_V123B_IIER_OFFSET
,
235 ipif_ier
| XSPI_INTR_TX_EMPTY
);
237 /* Start the transfer by not inhibiting the transmitter any longer */
238 cr
= in_be16(xspi
->regs
+ XSPI_CR_OFFSET
) & ~XSPI_CR_TRANS_INHIBIT
;
239 out_be16(xspi
->regs
+ XSPI_CR_OFFSET
, cr
);
241 wait_for_completion(&xspi
->done
);
243 /* Disable the transmit empty interrupt */
244 out_be32(xspi
->regs
+ XIPIF_V123B_IIER_OFFSET
, ipif_ier
);
246 return t
->len
- xspi
->remaining_bytes
;
250 /* This driver supports single master mode only. Hence Tx FIFO Empty
251 * is the only interrupt we care about.
252 * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
253 * Fault are not to happen.
255 static irqreturn_t
xilinx_spi_irq(int irq
, void *dev_id
)
257 struct xilinx_spi
*xspi
= dev_id
;
260 /* Get the IPIF interrupts, and clear them immediately */
261 ipif_isr
= in_be32(xspi
->regs
+ XIPIF_V123B_IISR_OFFSET
);
262 out_be32(xspi
->regs
+ XIPIF_V123B_IISR_OFFSET
, ipif_isr
);
264 if (ipif_isr
& XSPI_INTR_TX_EMPTY
) { /* Transmission completed */
268 /* A transmit has just completed. Process received data and
269 * check for more data to transmit. Always inhibit the
270 * transmitter while the Isr refills the transmit register/FIFO,
271 * or make sure it is stopped if we're done.
273 cr
= in_be16(xspi
->regs
+ XSPI_CR_OFFSET
);
274 out_be16(xspi
->regs
+ XSPI_CR_OFFSET
,
275 cr
| XSPI_CR_TRANS_INHIBIT
);
277 /* Read out all the data from the Rx FIFO */
278 sr
= in_8(xspi
->regs
+ XSPI_SR_OFFSET
);
279 while ((sr
& XSPI_SR_RX_EMPTY_MASK
) == 0) {
282 data
= in_8(xspi
->regs
+ XSPI_RXD_OFFSET
);
284 *xspi
->rx_ptr
++ = data
;
286 sr
= in_8(xspi
->regs
+ XSPI_SR_OFFSET
);
289 /* See if there is more data to send */
290 if (xspi
->remaining_bytes
> 0) {
291 xilinx_spi_fill_tx_fifo(xspi
);
292 /* Start the transfer by not inhibiting the
293 * transmitter any longer
295 out_be16(xspi
->regs
+ XSPI_CR_OFFSET
, cr
);
297 /* No more data to send.
298 * Indicate the transfer is completed.
300 complete(&xspi
->done
);
307 static int __init
xilinx_spi_probe(struct platform_device
*dev
)
310 struct spi_master
*master
;
311 struct xilinx_spi
*xspi
;
312 struct xspi_platform_data
*pdata
;
315 /* Get resources(memory, IRQ) associated with the device */
316 master
= spi_alloc_master(&dev
->dev
, sizeof(struct xilinx_spi
));
318 if (master
== NULL
) {
322 platform_set_drvdata(dev
, master
);
323 pdata
= dev
->dev
.platform_data
;
330 r
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
336 xspi
= spi_master_get_devdata(master
);
337 xspi
->bitbang
.master
= spi_master_get(master
);
338 xspi
->bitbang
.chipselect
= xilinx_spi_chipselect
;
339 xspi
->bitbang
.setup_transfer
= xilinx_spi_setup_transfer
;
340 xspi
->bitbang
.txrx_bufs
= xilinx_spi_txrx_bufs
;
341 xspi
->bitbang
.master
->setup
= xilinx_spi_setup
;
342 init_completion(&xspi
->done
);
344 if (!request_mem_region(r
->start
,
345 r
->end
- r
->start
+ 1, XILINX_SPI_NAME
)) {
350 xspi
->regs
= ioremap(r
->start
, r
->end
- r
->start
+ 1);
351 if (xspi
->regs
== NULL
) {
356 xspi
->irq
= platform_get_irq(dev
, 0);
362 master
->bus_num
= pdata
->bus_num
;
363 master
->num_chipselect
= pdata
->num_chipselect
;
364 xspi
->speed_hz
= pdata
->speed_hz
;
366 /* SPI controller initializations */
367 xspi_init_hw(xspi
->regs
);
369 /* Register for SPI Interrupt */
370 ret
= request_irq(xspi
->irq
, xilinx_spi_irq
, 0, XILINX_SPI_NAME
, xspi
);
374 ret
= spi_bitbang_start(&xspi
->bitbang
);
376 dev_err(&dev
->dev
, "spi_bitbang_start FAILED\n");
380 dev_info(&dev
->dev
, "at 0x%08X mapped to 0x%08X, irq=%d\n",
381 r
->start
, (u32
)xspi
->regs
, xspi
->irq
);
386 free_irq(xspi
->irq
, xspi
);
390 spi_master_put(master
);
394 static int __devexit
xilinx_spi_remove(struct platform_device
*dev
)
396 struct xilinx_spi
*xspi
;
397 struct spi_master
*master
;
399 master
= platform_get_drvdata(dev
);
400 xspi
= spi_master_get_devdata(master
);
402 spi_bitbang_stop(&xspi
->bitbang
);
403 free_irq(xspi
->irq
, xspi
);
405 platform_set_drvdata(dev
, 0);
406 spi_master_put(xspi
->bitbang
.master
);
411 /* work with hotplug and coldplug */
412 MODULE_ALIAS("platform:" XILINX_SPI_NAME
);
414 static struct platform_driver xilinx_spi_driver
= {
415 .probe
= xilinx_spi_probe
,
416 .remove
= __devexit_p(xilinx_spi_remove
),
418 .name
= XILINX_SPI_NAME
,
419 .owner
= THIS_MODULE
,
423 static int __init
xilinx_spi_init(void)
425 return platform_driver_register(&xilinx_spi_driver
);
427 module_init(xilinx_spi_init
);
429 static void __exit
xilinx_spi_exit(void)
431 platform_driver_unregister(&xilinx_spi_driver
);
433 module_exit(xilinx_spi_exit
);
435 MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
436 MODULE_DESCRIPTION("Xilinx SPI driver");
437 MODULE_LICENSE("GPL");