xref: /openbmc/linux/drivers/spi/spi-xilinx.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely  * Xilinx SPI controller driver (master mode only)
4ca632f55SGrant Likely  *
5ca632f55SGrant Likely  * Author: MontaVista Software, Inc.
6ca632f55SGrant Likely  *	source@mvista.com
7ca632f55SGrant Likely  *
8ca632f55SGrant Likely  * Copyright (c) 2010 Secret Lab Technologies, Ltd.
9ca632f55SGrant Likely  * Copyright (c) 2009 Intel Corporation
10ca632f55SGrant Likely  * 2002-2007 (c) MontaVista Software, Inc.
11ca632f55SGrant Likely 
12ca632f55SGrant Likely  */
13ca632f55SGrant Likely 
14ca632f55SGrant Likely #include <linux/module.h>
15ca632f55SGrant Likely #include <linux/interrupt.h>
16ca632f55SGrant Likely #include <linux/of.h>
17ca632f55SGrant Likely #include <linux/platform_device.h>
18ca632f55SGrant Likely #include <linux/spi/spi.h>
19ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h>
20ca632f55SGrant Likely #include <linux/spi/xilinx_spi.h>
21ca632f55SGrant Likely #include <linux/io.h>
22ca632f55SGrant Likely 
23eb25f16cSRicardo Ribalda #define XILINX_SPI_MAX_CS	32
24eb25f16cSRicardo Ribalda 
25ca632f55SGrant Likely #define XILINX_SPI_NAME "xilinx_spi"
26ca632f55SGrant Likely 
27ca632f55SGrant Likely /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
28ca632f55SGrant Likely  * Product Specification", DS464
29ca632f55SGrant Likely  */
30ca632f55SGrant Likely #define XSPI_CR_OFFSET		0x60	/* Control Register */
31ca632f55SGrant Likely 
32082339bcSMichal Simek #define XSPI_CR_LOOP		0x01
33ca632f55SGrant Likely #define XSPI_CR_ENABLE		0x02
34ca632f55SGrant Likely #define XSPI_CR_MASTER_MODE	0x04
35ca632f55SGrant Likely #define XSPI_CR_CPOL		0x08
36ca632f55SGrant Likely #define XSPI_CR_CPHA		0x10
37bca690dbSRicardo Ribalda Delgado #define XSPI_CR_MODE_MASK	(XSPI_CR_CPHA | XSPI_CR_CPOL | \
380240f945SRicardo Ribalda Delgado 				 XSPI_CR_LSB_FIRST | XSPI_CR_LOOP)
39ca632f55SGrant Likely #define XSPI_CR_TXFIFO_RESET	0x20
40ca632f55SGrant Likely #define XSPI_CR_RXFIFO_RESET	0x40
41ca632f55SGrant Likely #define XSPI_CR_MANUAL_SSELECT	0x80
42ca632f55SGrant Likely #define XSPI_CR_TRANS_INHIBIT	0x100
43ca632f55SGrant Likely #define XSPI_CR_LSB_FIRST	0x200
44ca632f55SGrant Likely 
45ca632f55SGrant Likely #define XSPI_SR_OFFSET		0x64	/* Status Register */
46ca632f55SGrant Likely 
47ca632f55SGrant Likely #define XSPI_SR_RX_EMPTY_MASK	0x01	/* Receive FIFO is empty */
48ca632f55SGrant Likely #define XSPI_SR_RX_FULL_MASK	0x02	/* Receive FIFO is full */
49ca632f55SGrant Likely #define XSPI_SR_TX_EMPTY_MASK	0x04	/* Transmit FIFO is empty */
50ca632f55SGrant Likely #define XSPI_SR_TX_FULL_MASK	0x08	/* Transmit FIFO is full */
51ca632f55SGrant Likely #define XSPI_SR_MODE_FAULT_MASK	0x10	/* Mode fault error */
52ca632f55SGrant Likely 
53ca632f55SGrant Likely #define XSPI_TXD_OFFSET		0x68	/* Data Transmit Register */
54ca632f55SGrant Likely #define XSPI_RXD_OFFSET		0x6c	/* Data Receive Register */
55ca632f55SGrant Likely 
56ca632f55SGrant Likely #define XSPI_SSR_OFFSET		0x70	/* 32-bit Slave Select Register */
57ca632f55SGrant Likely 
58ca632f55SGrant Likely /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
59ca632f55SGrant Likely  * IPIF registers are 32 bit
60ca632f55SGrant Likely  */
61ca632f55SGrant Likely #define XIPIF_V123B_DGIER_OFFSET	0x1c	/* IPIF global int enable reg */
62ca632f55SGrant Likely #define XIPIF_V123B_GINTR_ENABLE	0x80000000
63ca632f55SGrant Likely 
64ca632f55SGrant Likely #define XIPIF_V123B_IISR_OFFSET		0x20	/* IPIF interrupt status reg */
65ca632f55SGrant Likely #define XIPIF_V123B_IIER_OFFSET		0x28	/* IPIF interrupt enable reg */
66ca632f55SGrant Likely 
67ca632f55SGrant Likely #define XSPI_INTR_MODE_FAULT		0x01	/* Mode fault error */
68ca632f55SGrant Likely #define XSPI_INTR_SLAVE_MODE_FAULT	0x02	/* Selected as slave while
69ca632f55SGrant Likely 						 * disabled */
70ca632f55SGrant Likely #define XSPI_INTR_TX_EMPTY		0x04	/* TxFIFO is empty */
71ca632f55SGrant Likely #define XSPI_INTR_TX_UNDERRUN		0x08	/* TxFIFO was underrun */
72ca632f55SGrant Likely #define XSPI_INTR_RX_FULL		0x10	/* RxFIFO is full */
73ca632f55SGrant Likely #define XSPI_INTR_RX_OVERRUN		0x20	/* RxFIFO was overrun */
74ca632f55SGrant Likely #define XSPI_INTR_TX_HALF_EMPTY		0x40	/* TxFIFO is half empty */
75ca632f55SGrant Likely 
76ca632f55SGrant Likely #define XIPIF_V123B_RESETR_OFFSET	0x40	/* IPIF reset register */
77ca632f55SGrant Likely #define XIPIF_V123B_RESET_MASK		0x0a	/* the value to write */
78ca632f55SGrant Likely 
79ca632f55SGrant Likely struct xilinx_spi {
80ca632f55SGrant Likely 	/* bitbang has to be first */
81ca632f55SGrant Likely 	struct spi_bitbang bitbang;
82ca632f55SGrant Likely 	struct completion done;
83ca632f55SGrant Likely 	void __iomem	*regs;	/* virt. address of the control registers */
84ca632f55SGrant Likely 
859ca1273bSDan Carpenter 	int		irq;
861dd46599SVadim Fedorenko 	bool force_irq;		/* force irq to setup master inhibit */
87ca632f55SGrant Likely 	u8 *rx_ptr;		/* pointer in the Tx buffer */
88ca632f55SGrant Likely 	const u8 *tx_ptr;	/* pointer in the Rx buffer */
8917aaaa80SRicardo Ribalda Delgado 	u8 bytes_per_word;
904c9a7614SRicardo Ribalda Delgado 	int buffer_size;	/* buffer size in words */
91f9c6ef6cSRicardo Ribalda Delgado 	u32 cs_inactive;	/* Level of the CS pins when inactive*/
92ca632f55SGrant Likely 	unsigned int (*read_fn)(void __iomem *);
93ca632f55SGrant Likely 	void (*write_fn)(u32, void __iomem *);
94ca632f55SGrant Likely };
95ca632f55SGrant Likely 
xspi_write32(u32 val,void __iomem * addr)960635287aSMark Brown static void xspi_write32(u32 val, void __iomem *addr)
970635287aSMark Brown {
980635287aSMark Brown 	iowrite32(val, addr);
990635287aSMark Brown }
1000635287aSMark Brown 
xspi_read32(void __iomem * addr)1010635287aSMark Brown static unsigned int xspi_read32(void __iomem *addr)
1020635287aSMark Brown {
1030635287aSMark Brown 	return ioread32(addr);
1040635287aSMark Brown }
1050635287aSMark Brown 
xspi_write32_be(u32 val,void __iomem * addr)1060635287aSMark Brown static void xspi_write32_be(u32 val, void __iomem *addr)
1070635287aSMark Brown {
1080635287aSMark Brown 	iowrite32be(val, addr);
1090635287aSMark Brown }
1100635287aSMark Brown 
xspi_read32_be(void __iomem * addr)1110635287aSMark Brown static unsigned int xspi_read32_be(void __iomem *addr)
1120635287aSMark Brown {
1130635287aSMark Brown 	return ioread32be(addr);
1140635287aSMark Brown }
1150635287aSMark Brown 
xilinx_spi_tx(struct xilinx_spi * xspi)11624ba5e59SRicardo Ribalda Delgado static void xilinx_spi_tx(struct xilinx_spi *xspi)
117ca632f55SGrant Likely {
11834093cb9SRicardo Ribalda Delgado 	u32 data = 0;
11934093cb9SRicardo Ribalda Delgado 
120c3092941SRicardo Ribalda Delgado 	if (!xspi->tx_ptr) {
121c3092941SRicardo Ribalda Delgado 		xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
122c3092941SRicardo Ribalda Delgado 		return;
123c3092941SRicardo Ribalda Delgado 	}
12434093cb9SRicardo Ribalda Delgado 
12534093cb9SRicardo Ribalda Delgado 	switch (xspi->bytes_per_word) {
12634093cb9SRicardo Ribalda Delgado 	case 1:
12734093cb9SRicardo Ribalda Delgado 		data = *(u8 *)(xspi->tx_ptr);
12834093cb9SRicardo Ribalda Delgado 		break;
12934093cb9SRicardo Ribalda Delgado 	case 2:
13034093cb9SRicardo Ribalda Delgado 		data = *(u16 *)(xspi->tx_ptr);
13134093cb9SRicardo Ribalda Delgado 		break;
13234093cb9SRicardo Ribalda Delgado 	case 4:
13334093cb9SRicardo Ribalda Delgado 		data = *(u32 *)(xspi->tx_ptr);
13434093cb9SRicardo Ribalda Delgado 		break;
13534093cb9SRicardo Ribalda Delgado 	}
13634093cb9SRicardo Ribalda Delgado 
13734093cb9SRicardo Ribalda Delgado 	xspi->write_fn(data, xspi->regs + XSPI_TXD_OFFSET);
13817aaaa80SRicardo Ribalda Delgado 	xspi->tx_ptr += xspi->bytes_per_word;
139ca632f55SGrant Likely }
140ca632f55SGrant Likely 
xilinx_spi_rx(struct xilinx_spi * xspi)14124ba5e59SRicardo Ribalda Delgado static void xilinx_spi_rx(struct xilinx_spi *xspi)
142ca632f55SGrant Likely {
143ca632f55SGrant Likely 	u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
144ca632f55SGrant Likely 
14524ba5e59SRicardo Ribalda Delgado 	if (!xspi->rx_ptr)
14624ba5e59SRicardo Ribalda Delgado 		return;
147ca632f55SGrant Likely 
14817aaaa80SRicardo Ribalda Delgado 	switch (xspi->bytes_per_word) {
14917aaaa80SRicardo Ribalda Delgado 	case 1:
15024ba5e59SRicardo Ribalda Delgado 		*(u8 *)(xspi->rx_ptr) = data;
15124ba5e59SRicardo Ribalda Delgado 		break;
15217aaaa80SRicardo Ribalda Delgado 	case 2:
15324ba5e59SRicardo Ribalda Delgado 		*(u16 *)(xspi->rx_ptr) = data;
15424ba5e59SRicardo Ribalda Delgado 		break;
15517aaaa80SRicardo Ribalda Delgado 	case 4:
156ca632f55SGrant Likely 		*(u32 *)(xspi->rx_ptr) = data;
15724ba5e59SRicardo Ribalda Delgado 		break;
158ca632f55SGrant Likely 	}
15924ba5e59SRicardo Ribalda Delgado 
16017aaaa80SRicardo Ribalda Delgado 	xspi->rx_ptr += xspi->bytes_per_word;
161ca632f55SGrant Likely }
162ca632f55SGrant Likely 
xspi_init_hw(struct xilinx_spi * xspi)163ca632f55SGrant Likely static void xspi_init_hw(struct xilinx_spi *xspi)
164ca632f55SGrant Likely {
165ca632f55SGrant Likely 	void __iomem *regs_base = xspi->regs;
166ca632f55SGrant Likely 
167ca632f55SGrant Likely 	/* Reset the SPI device */
168ca632f55SGrant Likely 	xspi->write_fn(XIPIF_V123B_RESET_MASK,
169ca632f55SGrant Likely 		regs_base + XIPIF_V123B_RESETR_OFFSET);
170899929baSRicardo Ribalda Delgado 	/* Enable the transmit empty interrupt, which we use to determine
171899929baSRicardo Ribalda Delgado 	 * progress on the transmission.
172899929baSRicardo Ribalda Delgado 	 */
173899929baSRicardo Ribalda Delgado 	xspi->write_fn(XSPI_INTR_TX_EMPTY,
174899929baSRicardo Ribalda Delgado 			regs_base + XIPIF_V123B_IIER_OFFSET);
17522417352SRicardo Ribalda Delgado 	/* Disable the global IPIF interrupt */
1765fe11cc0SRicardo Ribalda Delgado 	xspi->write_fn(0, regs_base + XIPIF_V123B_DGIER_OFFSET);
177ca632f55SGrant Likely 	/* Deselect the slave on the SPI bus */
178ca632f55SGrant Likely 	xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET);
179ca632f55SGrant Likely 	/* Disable the transmitter, enable Manual Slave Select Assertion,
180ca632f55SGrant Likely 	 * put SPI controller into master mode, and enable it */
18122417352SRicardo Ribalda Delgado 	xspi->write_fn(XSPI_CR_MANUAL_SSELECT |	XSPI_CR_MASTER_MODE |
18222417352SRicardo Ribalda Delgado 		XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET |	XSPI_CR_RXFIFO_RESET,
18322417352SRicardo Ribalda Delgado 		regs_base + XSPI_CR_OFFSET);
184ca632f55SGrant Likely }
185ca632f55SGrant Likely 
xilinx_spi_chipselect(struct spi_device * spi,int is_on)186ca632f55SGrant Likely static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
187ca632f55SGrant Likely {
188ca632f55SGrant Likely 	struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
189f9c6ef6cSRicardo Ribalda Delgado 	u16 cr;
190f9c6ef6cSRicardo Ribalda Delgado 	u32 cs;
191ca632f55SGrant Likely 
192ca632f55SGrant Likely 	if (is_on == BITBANG_CS_INACTIVE) {
193ca632f55SGrant Likely 		/* Deselect the slave on the SPI bus */
194f9c6ef6cSRicardo Ribalda Delgado 		xspi->write_fn(xspi->cs_inactive, xspi->regs + XSPI_SSR_OFFSET);
195f9c6ef6cSRicardo Ribalda Delgado 		return;
196f9c6ef6cSRicardo Ribalda Delgado 	}
197f9c6ef6cSRicardo Ribalda Delgado 
198ca632f55SGrant Likely 	/* Set the SPI clock phase and polarity */
199f9c6ef6cSRicardo Ribalda Delgado 	cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET)	& ~XSPI_CR_MODE_MASK;
200ca632f55SGrant Likely 	if (spi->mode & SPI_CPHA)
201ca632f55SGrant Likely 		cr |= XSPI_CR_CPHA;
202ca632f55SGrant Likely 	if (spi->mode & SPI_CPOL)
203ca632f55SGrant Likely 		cr |= XSPI_CR_CPOL;
204bca690dbSRicardo Ribalda Delgado 	if (spi->mode & SPI_LSB_FIRST)
205bca690dbSRicardo Ribalda Delgado 		cr |= XSPI_CR_LSB_FIRST;
2060240f945SRicardo Ribalda Delgado 	if (spi->mode & SPI_LOOP)
2070240f945SRicardo Ribalda Delgado 		cr |= XSPI_CR_LOOP;
208ca632f55SGrant Likely 	xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
209ca632f55SGrant Likely 
210ca632f55SGrant Likely 	/* We do not check spi->max_speed_hz here as the SPI clock
211ca632f55SGrant Likely 	 * frequency is not software programmable (the IP block design
212ca632f55SGrant Likely 	 * parameter)
213ca632f55SGrant Likely 	 */
214ca632f55SGrant Likely 
215f9c6ef6cSRicardo Ribalda Delgado 	cs = xspi->cs_inactive;
2169e264f3fSAmit Kumar Mahapatra via Alsa-devel 	cs ^= BIT(spi_get_chipselect(spi, 0));
217f9c6ef6cSRicardo Ribalda Delgado 
218ca632f55SGrant Likely 	/* Activate the chip select */
219f9c6ef6cSRicardo Ribalda Delgado 	xspi->write_fn(cs, xspi->regs + XSPI_SSR_OFFSET);
220ca632f55SGrant Likely }
221ca632f55SGrant Likely 
222ca632f55SGrant Likely /* spi_bitbang requires custom setup_transfer() to be defined if there is a
2239bf46f6dSAxel Lin  * custom txrx_bufs().
224ca632f55SGrant Likely  */
xilinx_spi_setup_transfer(struct spi_device * spi,struct spi_transfer * t)225ca632f55SGrant Likely static int xilinx_spi_setup_transfer(struct spi_device *spi,
226ca632f55SGrant Likely 		struct spi_transfer *t)
227ca632f55SGrant Likely {
228f9c6ef6cSRicardo Ribalda Delgado 	struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
229f9c6ef6cSRicardo Ribalda Delgado 
230f9c6ef6cSRicardo Ribalda Delgado 	if (spi->mode & SPI_CS_HIGH)
2319e264f3fSAmit Kumar Mahapatra via Alsa-devel 		xspi->cs_inactive &= ~BIT(spi_get_chipselect(spi, 0));
232f9c6ef6cSRicardo Ribalda Delgado 	else
2339e264f3fSAmit Kumar Mahapatra via Alsa-devel 		xspi->cs_inactive |= BIT(spi_get_chipselect(spi, 0));
234f9c6ef6cSRicardo Ribalda Delgado 
235ca632f55SGrant Likely 	return 0;
236ca632f55SGrant Likely }
237ca632f55SGrant Likely 
xilinx_spi_txrx_bufs(struct spi_device * spi,struct spi_transfer * t)238ca632f55SGrant Likely static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
239ca632f55SGrant Likely {
240ca632f55SGrant Likely 	struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
241b563bfb8SRicardo Ribalda Delgado 	int remaining_words;	/* the number of words left to transfer */
24222417352SRicardo Ribalda Delgado 	bool use_irq = false;
24322417352SRicardo Ribalda Delgado 	u16 cr = 0;
244ca632f55SGrant Likely 
245ca632f55SGrant Likely 	/* We get here with transmitter inhibited */
246ca632f55SGrant Likely 
247ca632f55SGrant Likely 	xspi->tx_ptr = t->tx_buf;
248ca632f55SGrant Likely 	xspi->rx_ptr = t->rx_buf;
249b563bfb8SRicardo Ribalda Delgado 	remaining_words = t->len / xspi->bytes_per_word;
250ca632f55SGrant Likely 
2511dd46599SVadim Fedorenko 	if (xspi->irq >= 0 &&
2521dd46599SVadim Fedorenko 	    (xspi->force_irq || remaining_words > xspi->buffer_size)) {
25374346841SRicardo Ribalda Delgado 		u32 isr;
25422417352SRicardo Ribalda Delgado 		use_irq = true;
25522417352SRicardo Ribalda Delgado 		/* Inhibit irq to avoid spurious irqs on tx_empty*/
25622417352SRicardo Ribalda Delgado 		cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
25722417352SRicardo Ribalda Delgado 		xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
25822417352SRicardo Ribalda Delgado 			       xspi->regs + XSPI_CR_OFFSET);
25974346841SRicardo Ribalda Delgado 		/* ACK old irqs (if any) */
26074346841SRicardo Ribalda Delgado 		isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
26174346841SRicardo Ribalda Delgado 		if (isr)
26274346841SRicardo Ribalda Delgado 			xspi->write_fn(isr,
26374346841SRicardo Ribalda Delgado 				       xspi->regs + XIPIF_V123B_IISR_OFFSET);
26474346841SRicardo Ribalda Delgado 		/* Enable the global IPIF interrupt */
26574346841SRicardo Ribalda Delgado 		xspi->write_fn(XIPIF_V123B_GINTR_ENABLE,
26674346841SRicardo Ribalda Delgado 				xspi->regs + XIPIF_V123B_DGIER_OFFSET);
26774346841SRicardo Ribalda Delgado 		reinit_completion(&xspi->done);
26822417352SRicardo Ribalda Delgado 	}
26922417352SRicardo Ribalda Delgado 
270b563bfb8SRicardo Ribalda Delgado 	while (remaining_words) {
271b563bfb8SRicardo Ribalda Delgado 		int n_words, tx_words, rx_words;
272eca37c7cSRicardo Ribalda Delgado 		u32 sr;
2735a1314faSRicardo Ribalda 		int stalled;
27468c315bbSPeter Crosthwaite 
275b563bfb8SRicardo Ribalda Delgado 		n_words = min(remaining_words, xspi->buffer_size);
2764c9a7614SRicardo Ribalda Delgado 
277b563bfb8SRicardo Ribalda Delgado 		tx_words = n_words;
278b563bfb8SRicardo Ribalda Delgado 		while (tx_words--)
279b563bfb8SRicardo Ribalda Delgado 			xilinx_spi_tx(xspi);
28068c315bbSPeter Crosthwaite 
28168c315bbSPeter Crosthwaite 		/* Start the transfer by not inhibiting the transmitter any
28268c315bbSPeter Crosthwaite 		 * longer
28368c315bbSPeter Crosthwaite 		 */
284d9f58812SRicardo Ribalda Delgado 
28522417352SRicardo Ribalda Delgado 		if (use_irq) {
286ca632f55SGrant Likely 			xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
287ca632f55SGrant Likely 			wait_for_completion(&xspi->done);
288eca37c7cSRicardo Ribalda Delgado 			/* A transmit has just completed. Process received data
289eca37c7cSRicardo Ribalda Delgado 			 * and check for more data to transmit. Always inhibit
290eca37c7cSRicardo Ribalda Delgado 			 * the transmitter while the Isr refills the transmit
291eca37c7cSRicardo Ribalda Delgado 			 * register/FIFO, or make sure it is stopped if we're
292eca37c7cSRicardo Ribalda Delgado 			 * done.
29368c315bbSPeter Crosthwaite 			 */
29468c315bbSPeter Crosthwaite 			xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
29568c315bbSPeter Crosthwaite 				       xspi->regs + XSPI_CR_OFFSET);
296eca37c7cSRicardo Ribalda Delgado 			sr = XSPI_SR_TX_EMPTY_MASK;
297eca37c7cSRicardo Ribalda Delgado 		} else
298eca37c7cSRicardo Ribalda Delgado 			sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
29968c315bbSPeter Crosthwaite 
30068c315bbSPeter Crosthwaite 		/* Read out all the data from the Rx FIFO */
301b563bfb8SRicardo Ribalda Delgado 		rx_words = n_words;
3025a1314faSRicardo Ribalda 		stalled = 10;
303eca37c7cSRicardo Ribalda Delgado 		while (rx_words) {
3045a1314faSRicardo Ribalda 			if (rx_words == n_words && !(stalled--) &&
3055a1314faSRicardo Ribalda 			    !(sr & XSPI_SR_TX_EMPTY_MASK) &&
3065a1314faSRicardo Ribalda 			    (sr & XSPI_SR_RX_EMPTY_MASK)) {
3075a1314faSRicardo Ribalda 				dev_err(&spi->dev,
3085a1314faSRicardo Ribalda 					"Detected stall. Check C_SPI_MODE and C_SPI_MEMORY\n");
3095a1314faSRicardo Ribalda 				xspi_init_hw(xspi);
3105a1314faSRicardo Ribalda 				return -EIO;
3115a1314faSRicardo Ribalda 			}
3125a1314faSRicardo Ribalda 
313eca37c7cSRicardo Ribalda Delgado 			if ((sr & XSPI_SR_TX_EMPTY_MASK) && (rx_words > 1)) {
31424ba5e59SRicardo Ribalda Delgado 				xilinx_spi_rx(xspi);
315eca37c7cSRicardo Ribalda Delgado 				rx_words--;
316eca37c7cSRicardo Ribalda Delgado 				continue;
317eca37c7cSRicardo Ribalda Delgado 			}
318eca37c7cSRicardo Ribalda Delgado 
319eca37c7cSRicardo Ribalda Delgado 			sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
320eca37c7cSRicardo Ribalda Delgado 			if (!(sr & XSPI_SR_RX_EMPTY_MASK)) {
321eca37c7cSRicardo Ribalda Delgado 				xilinx_spi_rx(xspi);
322eca37c7cSRicardo Ribalda Delgado 				rx_words--;
323eca37c7cSRicardo Ribalda Delgado 			}
324eca37c7cSRicardo Ribalda Delgado 		}
325b563bfb8SRicardo Ribalda Delgado 
326b563bfb8SRicardo Ribalda Delgado 		remaining_words -= n_words;
32768c315bbSPeter Crosthwaite 	}
32868c315bbSPeter Crosthwaite 
32916ea9b8aSRicardo Ribalda Delgado 	if (use_irq) {
33022417352SRicardo Ribalda Delgado 		xspi->write_fn(0, xspi->regs + XIPIF_V123B_DGIER_OFFSET);
33116ea9b8aSRicardo Ribalda Delgado 		xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
33216ea9b8aSRicardo Ribalda Delgado 	}
33322417352SRicardo Ribalda Delgado 
334d79b2d07SRicardo Ribalda Delgado 	return t->len;
335ca632f55SGrant Likely }
336ca632f55SGrant Likely 
337ca632f55SGrant Likely 
338ca632f55SGrant Likely /* This driver supports single master mode only. Hence Tx FIFO Empty
339ca632f55SGrant Likely  * is the only interrupt we care about.
340ca632f55SGrant Likely  * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
341ca632f55SGrant Likely  * Fault are not to happen.
342ca632f55SGrant Likely  */
xilinx_spi_irq(int irq,void * dev_id)343ca632f55SGrant Likely static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
344ca632f55SGrant Likely {
345ca632f55SGrant Likely 	struct xilinx_spi *xspi = dev_id;
346ca632f55SGrant Likely 	u32 ipif_isr;
347ca632f55SGrant Likely 
348ca632f55SGrant Likely 	/* Get the IPIF interrupts, and clear them immediately */
349ca632f55SGrant Likely 	ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
350ca632f55SGrant Likely 	xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET);
351ca632f55SGrant Likely 
352ca632f55SGrant Likely 	if (ipif_isr & XSPI_INTR_TX_EMPTY) {	/* Transmission completed */
353ca632f55SGrant Likely 		complete(&xspi->done);
354d3364847SLars-Peter Clausen 		return IRQ_HANDLED;
355ca632f55SGrant Likely 	}
356ca632f55SGrant Likely 
357d3364847SLars-Peter Clausen 	return IRQ_NONE;
358ca632f55SGrant Likely }
359ca632f55SGrant Likely 
xilinx_spi_find_buffer_size(struct xilinx_spi * xspi)3604c9a7614SRicardo Ribalda Delgado static int xilinx_spi_find_buffer_size(struct xilinx_spi *xspi)
3614c9a7614SRicardo Ribalda Delgado {
3624c9a7614SRicardo Ribalda Delgado 	u8 sr;
3634c9a7614SRicardo Ribalda Delgado 	int n_words = 0;
3644c9a7614SRicardo Ribalda Delgado 
3654c9a7614SRicardo Ribalda Delgado 	/*
3664c9a7614SRicardo Ribalda Delgado 	 * Before the buffer_size detection we reset the core
3674c9a7614SRicardo Ribalda Delgado 	 * to make sure we start with a clean state.
3684c9a7614SRicardo Ribalda Delgado 	 */
3694c9a7614SRicardo Ribalda Delgado 	xspi->write_fn(XIPIF_V123B_RESET_MASK,
3704c9a7614SRicardo Ribalda Delgado 		xspi->regs + XIPIF_V123B_RESETR_OFFSET);
3714c9a7614SRicardo Ribalda Delgado 
3724c9a7614SRicardo Ribalda Delgado 	/* Fill the Tx FIFO with as many words as possible */
3734c9a7614SRicardo Ribalda Delgado 	do {
3744c9a7614SRicardo Ribalda Delgado 		xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
3754c9a7614SRicardo Ribalda Delgado 		sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
3764c9a7614SRicardo Ribalda Delgado 		n_words++;
3774c9a7614SRicardo Ribalda Delgado 	} while (!(sr & XSPI_SR_TX_FULL_MASK));
3784c9a7614SRicardo Ribalda Delgado 
3794c9a7614SRicardo Ribalda Delgado 	return n_words;
3804c9a7614SRicardo Ribalda Delgado }
3814c9a7614SRicardo Ribalda Delgado 
382ca632f55SGrant Likely static const struct of_device_id xilinx_spi_of_match[] = {
383a094c2faSRicardo Ribalda 	{ .compatible = "xlnx,axi-quad-spi-1.00.a", },
384ca632f55SGrant Likely 	{ .compatible = "xlnx,xps-spi-2.00.a", },
385ca632f55SGrant Likely 	{ .compatible = "xlnx,xps-spi-2.00.b", },
386ca632f55SGrant Likely 	{}
387ca632f55SGrant Likely };
388ca632f55SGrant Likely MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
389ca632f55SGrant Likely 
xilinx_spi_probe(struct platform_device * pdev)3907cb2abd0SMark Brown static int xilinx_spi_probe(struct platform_device *pdev)
391ca632f55SGrant Likely {
392ca632f55SGrant Likely 	struct xilinx_spi *xspi;
393d81c0bbbSMark Brown 	struct xspi_platform_data *pdata;
394ad3fdbcaSMichal Simek 	struct resource *res;
395e58f7d15SAlvaro Gamez Machado 	int ret, num_cs = 0, bits_per_word;
396d81c0bbbSMark Brown 	struct spi_master *master;
3971dd46599SVadim Fedorenko 	bool force_irq = false;
398082339bcSMichal Simek 	u32 tmp;
399d81c0bbbSMark Brown 	u8 i;
400ca632f55SGrant Likely 
4018074cf06SJingoo Han 	pdata = dev_get_platdata(&pdev->dev);
402d81c0bbbSMark Brown 	if (pdata) {
403d81c0bbbSMark Brown 		num_cs = pdata->num_chipselect;
404d81c0bbbSMark Brown 		bits_per_word = pdata->bits_per_word;
4051dd46599SVadim Fedorenko 		force_irq = pdata->force_irq;
406be3acdffSMichal Simek 	} else {
407be3acdffSMichal Simek 		of_property_read_u32(pdev->dev.of_node, "xlnx,num-ss-bits",
408be3acdffSMichal Simek 					  &num_cs);
409e58f7d15SAlvaro Gamez Machado 		ret = of_property_read_u32(pdev->dev.of_node,
410e58f7d15SAlvaro Gamez Machado 					   "xlnx,num-transfer-bits",
411e58f7d15SAlvaro Gamez Machado 					   &bits_per_word);
412e58f7d15SAlvaro Gamez Machado 		if (ret)
413e58f7d15SAlvaro Gamez Machado 			bits_per_word = 8;
414d81c0bbbSMark Brown 	}
415d81c0bbbSMark Brown 
416d81c0bbbSMark Brown 	if (!num_cs) {
4177cb2abd0SMark Brown 		dev_err(&pdev->dev,
4187cb2abd0SMark Brown 			"Missing slave select configuration data\n");
419d81c0bbbSMark Brown 		return -EINVAL;
420d81c0bbbSMark Brown 	}
421d81c0bbbSMark Brown 
422eb25f16cSRicardo Ribalda 	if (num_cs > XILINX_SPI_MAX_CS) {
423eb25f16cSRicardo Ribalda 		dev_err(&pdev->dev, "Invalid number of spi slaves\n");
424eb25f16cSRicardo Ribalda 		return -EINVAL;
425eb25f16cSRicardo Ribalda 	}
426eb25f16cSRicardo Ribalda 
4272d064581SYang Yingliang 	master = devm_spi_alloc_master(&pdev->dev, sizeof(struct xilinx_spi));
428ca632f55SGrant Likely 	if (!master)
429d81c0bbbSMark Brown 		return -ENODEV;
430ca632f55SGrant Likely 
431ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
432f9c6ef6cSRicardo Ribalda Delgado 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP |
433f9c6ef6cSRicardo Ribalda Delgado 			    SPI_CS_HIGH;
434ca632f55SGrant Likely 
435ca632f55SGrant Likely 	xspi = spi_master_get_devdata(master);
436f9c6ef6cSRicardo Ribalda Delgado 	xspi->cs_inactive = 0xffffffff;
43794c69f76SAxel Lin 	xspi->bitbang.master = master;
438ca632f55SGrant Likely 	xspi->bitbang.chipselect = xilinx_spi_chipselect;
439ca632f55SGrant Likely 	xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
440ca632f55SGrant Likely 	xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
441ca632f55SGrant Likely 	init_completion(&xspi->done);
442ca632f55SGrant Likely 
443*0623ec17SYang Li 	xspi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
4442d064581SYang Yingliang 	if (IS_ERR(xspi->regs))
4452d064581SYang Yingliang 		return PTR_ERR(xspi->regs);
446ca632f55SGrant Likely 
4474b153a21SLars-Peter Clausen 	master->bus_num = pdev->id;
448ca632f55SGrant Likely 	master->num_chipselect = num_cs;
4497cb2abd0SMark Brown 	master->dev.of_node = pdev->dev.of_node;
450082339bcSMichal Simek 
451082339bcSMichal Simek 	/*
452082339bcSMichal Simek 	 * Detect endianess on the IP via loop bit in CR. Detection
453082339bcSMichal Simek 	 * must be done before reset is sent because incorrect reset
454082339bcSMichal Simek 	 * value generates error interrupt.
455082339bcSMichal Simek 	 * Setup little endian helper functions first and try to use them
456082339bcSMichal Simek 	 * and check if bit was correctly setup or not.
457082339bcSMichal Simek 	 */
4580635287aSMark Brown 	xspi->read_fn = xspi_read32;
4590635287aSMark Brown 	xspi->write_fn = xspi_write32;
460082339bcSMichal Simek 
461082339bcSMichal Simek 	xspi->write_fn(XSPI_CR_LOOP, xspi->regs + XSPI_CR_OFFSET);
462082339bcSMichal Simek 	tmp = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
463082339bcSMichal Simek 	tmp &= XSPI_CR_LOOP;
464082339bcSMichal Simek 	if (tmp != XSPI_CR_LOOP) {
4650635287aSMark Brown 		xspi->read_fn = xspi_read32_be;
4660635287aSMark Brown 		xspi->write_fn = xspi_write32_be;
467ca632f55SGrant Likely 	}
468082339bcSMichal Simek 
4699bf46f6dSAxel Lin 	master->bits_per_word_mask = SPI_BPW_MASK(bits_per_word);
47017aaaa80SRicardo Ribalda Delgado 	xspi->bytes_per_word = bits_per_word / 8;
4714c9a7614SRicardo Ribalda Delgado 	xspi->buffer_size = xilinx_spi_find_buffer_size(xspi);
4724c9a7614SRicardo Ribalda Delgado 
4737b3b7432SMichal Simek 	xspi->irq = platform_get_irq(pdev, 0);
4744db9bf54SLars-Peter Clausen 	if (xspi->irq < 0 && xspi->irq != -ENXIO) {
4752d064581SYang Yingliang 		return xspi->irq;
4764db9bf54SLars-Peter Clausen 	} else if (xspi->irq >= 0) {
477ca632f55SGrant Likely 		/* Register for SPI Interrupt */
4787b3b7432SMichal Simek 		ret = devm_request_irq(&pdev->dev, xspi->irq, xilinx_spi_irq, 0,
4797b3b7432SMichal Simek 				dev_name(&pdev->dev), xspi);
480ca632f55SGrant Likely 		if (ret)
4812d064581SYang Yingliang 			return ret;
4821dd46599SVadim Fedorenko 
4831dd46599SVadim Fedorenko 		xspi->force_irq = force_irq;
4845fe11cc0SRicardo Ribalda Delgado 	}
4855fe11cc0SRicardo Ribalda Delgado 
4865fe11cc0SRicardo Ribalda Delgado 	/* SPI controller initializations */
4875fe11cc0SRicardo Ribalda Delgado 	xspi_init_hw(xspi);
488ca632f55SGrant Likely 
489ca632f55SGrant Likely 	ret = spi_bitbang_start(&xspi->bitbang);
490ca632f55SGrant Likely 	if (ret) {
4917cb2abd0SMark Brown 		dev_err(&pdev->dev, "spi_bitbang_start FAILED\n");
4922d064581SYang Yingliang 		return ret;
493ca632f55SGrant Likely 	}
494ca632f55SGrant Likely 
495985be7ebSRicardo Ribalda 	dev_info(&pdev->dev, "at %pR, irq=%d\n", res, xspi->irq);
496ca632f55SGrant Likely 
497ca632f55SGrant Likely 	if (pdata) {
498ca632f55SGrant Likely 		for (i = 0; i < pdata->num_devices; i++)
499ca632f55SGrant Likely 			spi_new_device(master, pdata->devices + i);
500ca632f55SGrant Likely 	}
501ca632f55SGrant Likely 
5027cb2abd0SMark Brown 	platform_set_drvdata(pdev, master);
503ca632f55SGrant Likely 	return 0;
504ca632f55SGrant Likely }
505ca632f55SGrant Likely 
xilinx_spi_remove(struct platform_device * pdev)5063b1d7e11SUwe Kleine-König static void xilinx_spi_remove(struct platform_device *pdev)
507ca632f55SGrant Likely {
5087cb2abd0SMark Brown 	struct spi_master *master = platform_get_drvdata(pdev);
509d81c0bbbSMark Brown 	struct xilinx_spi *xspi = spi_master_get_devdata(master);
5107b3b7432SMichal Simek 	void __iomem *regs_base = xspi->regs;
511d81c0bbbSMark Brown 
512d81c0bbbSMark Brown 	spi_bitbang_stop(&xspi->bitbang);
5137b3b7432SMichal Simek 
5147b3b7432SMichal Simek 	/* Disable all the interrupts just in case */
5157b3b7432SMichal Simek 	xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
5167b3b7432SMichal Simek 	/* Disable the global IPIF interrupt */
5177b3b7432SMichal Simek 	xspi->write_fn(0, regs_base + XIPIF_V123B_DGIER_OFFSET);
518d81c0bbbSMark Brown 
519d81c0bbbSMark Brown 	spi_master_put(xspi->bitbang.master);
520ca632f55SGrant Likely }
521ca632f55SGrant Likely 
522ca632f55SGrant Likely /* work with hotplug and coldplug */
523ca632f55SGrant Likely MODULE_ALIAS("platform:" XILINX_SPI_NAME);
524ca632f55SGrant Likely 
525ca632f55SGrant Likely static struct platform_driver xilinx_spi_driver = {
526ca632f55SGrant Likely 	.probe = xilinx_spi_probe,
5273b1d7e11SUwe Kleine-König 	.remove_new = xilinx_spi_remove,
528ca632f55SGrant Likely 	.driver = {
529ca632f55SGrant Likely 		.name = XILINX_SPI_NAME,
530ca632f55SGrant Likely 		.of_match_table = xilinx_spi_of_match,
531ca632f55SGrant Likely 	},
532ca632f55SGrant Likely };
533940ab889SGrant Likely module_platform_driver(xilinx_spi_driver);
534ca632f55SGrant Likely 
535ca632f55SGrant Likely MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
536ca632f55SGrant Likely MODULE_DESCRIPTION("Xilinx SPI driver");
537ca632f55SGrant Likely MODULE_LICENSE("GPL");
538