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"
25 /* bitbang has to be first */
26 struct spi_bitbang bitbang
;
27 struct completion done
;
28 struct resource mem
; /* phys mem */
29 void __iomem
*regs
; /* virt. address of the control registers */
31 u8
*rx_ptr
; /* pointer in the Tx buffer */
32 const u8
*tx_ptr
; /* pointer in the Rx buffer */
33 int remaining_bytes
; /* the number of bytes left to transfer */
34 /* offset to the XSPI regs, these might vary... */
36 bool big_endian
; /* The device could be accessed big or little
42 /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
43 * Product Specification", DS464
45 #define XSPI_CR_OFFSET 0x60 /* Control Register */
47 #define XSPI_CR_ENABLE 0x02
48 #define XSPI_CR_MASTER_MODE 0x04
49 #define XSPI_CR_CPOL 0x08
50 #define XSPI_CR_CPHA 0x10
51 #define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL)
52 #define XSPI_CR_TXFIFO_RESET 0x20
53 #define XSPI_CR_RXFIFO_RESET 0x40
54 #define XSPI_CR_MANUAL_SSELECT 0x80
55 #define XSPI_CR_TRANS_INHIBIT 0x100
56 #define XSPI_CR_LSB_FIRST 0x200
58 #define XSPI_SR_OFFSET 0x64 /* Status Register */
60 #define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
61 #define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
62 #define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
63 #define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
64 #define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
66 #define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */
67 #define XSPI_RXD_OFFSET 0x6C /* Data Receive Register */
69 #define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
71 /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
72 * IPIF registers are 32 bit
74 #define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
75 #define XIPIF_V123B_GINTR_ENABLE 0x80000000
77 #define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
78 #define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
80 #define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
81 #define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
83 #define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
84 #define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
85 #define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
86 #define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
87 #define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */
89 #define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
90 #define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
92 /* to follow are some functions that does little of big endian read and
93 * write depending on the config of the device.
95 static inline void xspi_write8(struct xilinx_spi
*xspi
, u32 offs
, u8 val
)
97 iowrite8(val
, xspi
->regs
+ offs
+ ((xspi
->big_endian
) ? 3 : 0));
100 static inline void xspi_write16(struct xilinx_spi
*xspi
, u32 offs
, u16 val
)
102 if (xspi
->big_endian
)
103 iowrite16be(val
, xspi
->regs
+ offs
+ 2);
105 iowrite16(val
, xspi
->regs
+ offs
);
108 static inline void xspi_write32(struct xilinx_spi
*xspi
, u32 offs
, u32 val
)
110 if (xspi
->big_endian
)
111 iowrite32be(val
, xspi
->regs
+ offs
);
113 iowrite32(val
, xspi
->regs
+ offs
);
116 static inline u8
xspi_read8(struct xilinx_spi
*xspi
, u32 offs
)
118 return ioread8(xspi
->regs
+ offs
+ ((xspi
->big_endian
) ? 3 : 0));
121 static inline u16
xspi_read16(struct xilinx_spi
*xspi
, u32 offs
)
123 if (xspi
->big_endian
)
124 return ioread16be(xspi
->regs
+ offs
+ 2);
126 return ioread16(xspi
->regs
+ offs
);
129 static inline u32
xspi_read32(struct xilinx_spi
*xspi
, u32 offs
)
131 if (xspi
->big_endian
)
132 return ioread32be(xspi
->regs
+ offs
);
134 return ioread32(xspi
->regs
+ offs
);
137 static void xspi_init_hw(struct xilinx_spi
*xspi
)
139 /* Reset the SPI device */
140 xspi_write32(xspi
, XIPIF_V123B_RESETR_OFFSET
, XIPIF_V123B_RESET_MASK
);
141 /* Disable all the interrupts just in case */
142 xspi_write32(xspi
, XIPIF_V123B_IIER_OFFSET
, 0);
143 /* Enable the global IPIF interrupt */
144 xspi_write32(xspi
, XIPIF_V123B_DGIER_OFFSET
, XIPIF_V123B_GINTR_ENABLE
);
145 /* Deselect the slave on the SPI bus */
146 xspi_write32(xspi
, XSPI_SSR_OFFSET
, 0xffff);
147 /* Disable the transmitter, enable Manual Slave Select Assertion,
148 * put SPI controller into master mode, and enable it */
149 xspi_write16(xspi
, XSPI_CR_OFFSET
,
150 XSPI_CR_TRANS_INHIBIT
| XSPI_CR_MANUAL_SSELECT
|
151 XSPI_CR_MASTER_MODE
| XSPI_CR_ENABLE
| XSPI_CR_TXFIFO_RESET
|
152 XSPI_CR_RXFIFO_RESET
);
155 static void xilinx_spi_chipselect(struct spi_device
*spi
, int is_on
)
157 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
159 if (is_on
== BITBANG_CS_INACTIVE
) {
160 /* Deselect the slave on the SPI bus */
161 xspi_write32(xspi
, XSPI_SSR_OFFSET
, 0xffff);
162 } else if (is_on
== BITBANG_CS_ACTIVE
) {
163 /* Set the SPI clock phase and polarity */
164 u16 cr
= xspi_read16(xspi
, XSPI_CR_OFFSET
)
165 & ~XSPI_CR_MODE_MASK
;
166 if (spi
->mode
& SPI_CPHA
)
168 if (spi
->mode
& SPI_CPOL
)
170 xspi_write16(xspi
, XSPI_CR_OFFSET
, cr
);
172 /* We do not check spi->max_speed_hz here as the SPI clock
173 * frequency is not software programmable (the IP block design
177 /* Activate the chip select */
178 xspi_write32(xspi
, XSPI_SSR_OFFSET
,
179 ~(0x0001 << spi
->chip_select
));
183 /* spi_bitbang requires custom setup_transfer() to be defined if there is a
184 * custom txrx_bufs(). We have nothing to setup here as the SPI IP block
185 * supports 8 or 16 bits per word, which can not be changed in software.
186 * SPI clock can't be changed in software.
187 * Check for correct bits per word. Chip select delay calculations could be
188 * added here as soon as bitbang_work() can be made aware of the delay value.
190 static int xilinx_spi_setup_transfer(struct spi_device
*spi
,
191 struct spi_transfer
*t
)
194 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
196 bits_per_word
= (t
->bits_per_word
) ? t
->bits_per_word
:
198 if (bits_per_word
!= xspi
->bits_per_word
) {
199 dev_err(&spi
->dev
, "%s, unsupported bits_per_word=%d\n",
200 __func__
, bits_per_word
);
207 static int xilinx_spi_setup(struct spi_device
*spi
)
209 /* always return 0, we can not check the number of bits.
210 * There are cases when SPI setup is called before any driver is
211 * there, in that case the SPI core defaults to 8 bits, which we
212 * do not support in some cases. But if we return an error, the
213 * SPI device would not be registered and no driver can get hold of it
214 * When the driver is there, it will call SPI setup again with the
215 * correct number of bits per transfer.
216 * If a driver setups with the wrong bit number, it will fail when
217 * it tries to do a transfer
222 static void xilinx_spi_fill_tx_fifo(struct xilinx_spi
*xspi
)
226 if (xspi
->bits_per_word
== 8)
228 else if (xspi
->bits_per_word
== 16)
233 /* Fill the Tx FIFO with as many bytes as possible */
234 sr
= xspi_read8(xspi
, XSPI_SR_OFFSET
);
235 while ((sr
& XSPI_SR_TX_FULL_MASK
) == 0 &&
236 xspi
->remaining_bytes
> 0) {
239 xspi_write8(xspi
, XSPI_TXD_OFFSET
,
242 xspi_write16(xspi
, XSPI_TXD_OFFSET
,
243 *(u16
*)(xspi
->tx_ptr
));
245 xspi_write32(xspi
, XSPI_TXD_OFFSET
,
246 *(u32
*)(xspi
->tx_ptr
));
248 xspi
->tx_ptr
+= wsize
;
250 xspi_write8(xspi
, XSPI_TXD_OFFSET
, 0);
251 xspi
->remaining_bytes
-= wsize
;
252 sr
= xspi_read8(xspi
, XSPI_SR_OFFSET
);
256 static int xilinx_spi_txrx_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
258 struct xilinx_spi
*xspi
= spi_master_get_devdata(spi
->master
);
262 /* We get here with transmitter inhibited */
264 xspi
->tx_ptr
= t
->tx_buf
;
265 xspi
->rx_ptr
= t
->rx_buf
;
266 xspi
->remaining_bytes
= t
->len
;
267 INIT_COMPLETION(xspi
->done
);
269 xilinx_spi_fill_tx_fifo(xspi
);
271 /* Enable the transmit empty interrupt, which we use to determine
272 * progress on the transmission.
274 ipif_ier
= xspi_read32(xspi
, XIPIF_V123B_IIER_OFFSET
);
275 xspi_write32(xspi
, XIPIF_V123B_IIER_OFFSET
,
276 ipif_ier
| XSPI_INTR_TX_EMPTY
);
278 /* Start the transfer by not inhibiting the transmitter any longer */
279 cr
= xspi_read16(xspi
, XSPI_CR_OFFSET
) & ~XSPI_CR_TRANS_INHIBIT
;
280 xspi_write16(xspi
, XSPI_CR_OFFSET
, cr
);
282 wait_for_completion(&xspi
->done
);
284 /* Disable the transmit empty interrupt */
285 xspi_write32(xspi
, XIPIF_V123B_IIER_OFFSET
, ipif_ier
);
287 return t
->len
- xspi
->remaining_bytes
;
290 /* This driver supports single master mode only. Hence Tx FIFO Empty
291 * is the only interrupt we care about.
292 * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
293 * Fault are not to happen.
295 static irqreturn_t
xilinx_spi_irq(int irq
, void *dev_id
)
297 struct xilinx_spi
*xspi
= dev_id
;
300 /* Get the IPIF interrupts, and clear them immediately */
301 ipif_isr
= xspi_read32(xspi
, XIPIF_V123B_IISR_OFFSET
);
302 xspi_write32(xspi
, XIPIF_V123B_IISR_OFFSET
, ipif_isr
);
304 if (ipif_isr
& XSPI_INTR_TX_EMPTY
) { /* Transmission completed */
308 if (xspi
->bits_per_word
== 8)
310 else if (xspi
->bits_per_word
== 16)
315 /* A transmit has just completed. Process received data and
316 * check for more data to transmit. Always inhibit the
317 * transmitter while the Isr refills the transmit register/FIFO,
318 * or make sure it is stopped if we're done.
320 cr
= xspi_read16(xspi
, XSPI_CR_OFFSET
);
321 xspi_write16(xspi
, XSPI_CR_OFFSET
, cr
| XSPI_CR_TRANS_INHIBIT
);
323 /* Read out all the data from the Rx FIFO */
324 sr
= xspi_read8(xspi
, XSPI_SR_OFFSET
);
325 while ((sr
& XSPI_SR_RX_EMPTY_MASK
) == 0) {
328 data
= xspi_read8(xspi
, XSPI_RXD_OFFSET
);
330 data
= xspi_read16(xspi
, XSPI_RXD_OFFSET
);
332 data
= xspi_read32(xspi
, XSPI_RXD_OFFSET
);
336 *xspi
->rx_ptr
= data
& 0xff;
338 *(u16
*)(xspi
->rx_ptr
) = data
& 0xffff;
340 *((u32
*)(xspi
->rx_ptr
)) = data
;
341 xspi
->rx_ptr
+= rsize
;
344 sr
= xspi_read8(xspi
, XSPI_SR_OFFSET
);
347 /* See if there is more data to send */
348 if (xspi
->remaining_bytes
> 0) {
349 xilinx_spi_fill_tx_fifo(xspi
);
350 /* Start the transfer by not inhibiting the
351 * transmitter any longer
353 xspi_write16(xspi
, XSPI_CR_OFFSET
, cr
);
355 /* No more data to send.
356 * Indicate the transfer is completed.
358 complete(&xspi
->done
);
364 struct spi_master
*xilinx_spi_init(struct device
*dev
, struct resource
*mem
,
365 u32 irq
, s16 bus_num
, u16 num_chipselect
, u8 bits_per_word
,
368 struct spi_master
*master
;
369 struct xilinx_spi
*xspi
;
372 master
= spi_alloc_master(dev
, sizeof(struct xilinx_spi
));
375 return ERR_PTR(-ENOMEM
);
377 /* the spi->mode bits understood by this driver: */
378 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
;
380 xspi
= spi_master_get_devdata(master
);
381 xspi
->bitbang
.master
= spi_master_get(master
);
382 xspi
->bitbang
.chipselect
= xilinx_spi_chipselect
;
383 xspi
->bitbang
.setup_transfer
= xilinx_spi_setup_transfer
;
384 xspi
->bitbang
.txrx_bufs
= xilinx_spi_txrx_bufs
;
385 xspi
->bitbang
.master
->setup
= xilinx_spi_setup
;
386 init_completion(&xspi
->done
);
388 if (!request_mem_region(mem
->start
, resource_size(mem
),
394 xspi
->regs
= ioremap(mem
->start
, resource_size(mem
));
395 if (xspi
->regs
== NULL
) {
397 dev_warn(dev
, "ioremap failure\n");
401 master
->bus_num
= bus_num
;
402 master
->num_chipselect
= num_chipselect
;
406 xspi
->bits_per_word
= bits_per_word
;
407 xspi
->big_endian
= big_endian
;
409 /* SPI controller initializations */
412 /* Register for SPI Interrupt */
413 ret
= request_irq(xspi
->irq
, xilinx_spi_irq
, 0, XILINX_SPI_NAME
, xspi
);
417 ret
= spi_bitbang_start(&xspi
->bitbang
);
419 dev_err(dev
, "spi_bitbang_start FAILED\n");
423 dev_info(dev
, "at 0x%08X mapped to 0x%08X, irq=%d\n",
424 (u32
)mem
->start
, (u32
)xspi
->regs
, xspi
->irq
);
428 free_irq(xspi
->irq
, xspi
);
432 release_mem_region(mem
->start
, resource_size(mem
));
434 spi_master_put(master
);
437 EXPORT_SYMBOL(xilinx_spi_init
);
439 void xilinx_spi_deinit(struct spi_master
*master
)
441 struct xilinx_spi
*xspi
;
443 xspi
= spi_master_get_devdata(master
);
445 spi_bitbang_stop(&xspi
->bitbang
);
446 free_irq(xspi
->irq
, xspi
);
449 release_mem_region(xspi
->mem
.start
, resource_size(&xspi
->mem
));
450 spi_master_put(xspi
->bitbang
.master
);
452 EXPORT_SYMBOL(xilinx_spi_deinit
);
454 MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
455 MODULE_DESCRIPTION("Xilinx SPI driver");
456 MODULE_LICENSE("GPL");