xref: /openbmc/linux/drivers/spi/spi-bcm2835.c (revision ec679bda)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f8043872SChris Boot /*
3f8043872SChris Boot  * Driver for Broadcom BCM2835 SPI Controllers
4f8043872SChris Boot  *
5f8043872SChris Boot  * Copyright (C) 2012 Chris Boot
6f8043872SChris Boot  * Copyright (C) 2013 Stephen Warren
7e34ff011SMartin Sperl  * Copyright (C) 2015 Martin Sperl
8f8043872SChris Boot  *
9f8043872SChris Boot  * This driver is inspired by:
10f8043872SChris Boot  * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
11f8043872SChris Boot  * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
12f8043872SChris Boot  */
13f8043872SChris Boot 
14f8043872SChris Boot #include <linux/clk.h>
15f8043872SChris Boot #include <linux/completion.h>
16154f7da5SMartin Sperl #include <linux/debugfs.h>
17f8043872SChris Boot #include <linux/delay.h>
183ecd37edSMartin Sperl #include <linux/dma-mapping.h>
193ecd37edSMartin Sperl #include <linux/dmaengine.h>
20f8043872SChris Boot #include <linux/err.h>
21f8043872SChris Boot #include <linux/interrupt.h>
22f8043872SChris Boot #include <linux/io.h>
23f8043872SChris Boot #include <linux/kernel.h>
24f8043872SChris Boot #include <linux/module.h>
25f8043872SChris Boot #include <linux/of.h>
263ecd37edSMartin Sperl #include <linux/of_address.h>
27f8043872SChris Boot #include <linux/of_device.h>
283bd158c5SLinus Walleij #include <linux/gpio/consumer.h>
293bd158c5SLinus Walleij #include <linux/gpio/machine.h> /* FIXME: using chip internals */
303bd158c5SLinus Walleij #include <linux/gpio/driver.h> /* FIXME: using chip internals */
313ecd37edSMartin Sperl #include <linux/of_irq.h>
32f8043872SChris Boot #include <linux/spi/spi.h>
33f8043872SChris Boot 
34f8043872SChris Boot /* SPI register offsets */
35f8043872SChris Boot #define BCM2835_SPI_CS			0x00
36f8043872SChris Boot #define BCM2835_SPI_FIFO		0x04
37f8043872SChris Boot #define BCM2835_SPI_CLK			0x08
38f8043872SChris Boot #define BCM2835_SPI_DLEN		0x0c
39f8043872SChris Boot #define BCM2835_SPI_LTOH		0x10
40f8043872SChris Boot #define BCM2835_SPI_DC			0x14
41f8043872SChris Boot 
42f8043872SChris Boot /* Bitfields in CS */
43f8043872SChris Boot #define BCM2835_SPI_CS_LEN_LONG		0x02000000
44f8043872SChris Boot #define BCM2835_SPI_CS_DMA_LEN		0x01000000
45f8043872SChris Boot #define BCM2835_SPI_CS_CSPOL2		0x00800000
46f8043872SChris Boot #define BCM2835_SPI_CS_CSPOL1		0x00400000
47f8043872SChris Boot #define BCM2835_SPI_CS_CSPOL0		0x00200000
48f8043872SChris Boot #define BCM2835_SPI_CS_RXF		0x00100000
49f8043872SChris Boot #define BCM2835_SPI_CS_RXR		0x00080000
50f8043872SChris Boot #define BCM2835_SPI_CS_TXD		0x00040000
51f8043872SChris Boot #define BCM2835_SPI_CS_RXD		0x00020000
52f8043872SChris Boot #define BCM2835_SPI_CS_DONE		0x00010000
53f8043872SChris Boot #define BCM2835_SPI_CS_LEN		0x00002000
54f8043872SChris Boot #define BCM2835_SPI_CS_REN		0x00001000
55f8043872SChris Boot #define BCM2835_SPI_CS_ADCS		0x00000800
56f8043872SChris Boot #define BCM2835_SPI_CS_INTR		0x00000400
57f8043872SChris Boot #define BCM2835_SPI_CS_INTD		0x00000200
58f8043872SChris Boot #define BCM2835_SPI_CS_DMAEN		0x00000100
59f8043872SChris Boot #define BCM2835_SPI_CS_TA		0x00000080
60f8043872SChris Boot #define BCM2835_SPI_CS_CSPOL		0x00000040
61f8043872SChris Boot #define BCM2835_SPI_CS_CLEAR_RX		0x00000020
62f8043872SChris Boot #define BCM2835_SPI_CS_CLEAR_TX		0x00000010
63f8043872SChris Boot #define BCM2835_SPI_CS_CPOL		0x00000008
64f8043872SChris Boot #define BCM2835_SPI_CS_CPHA		0x00000004
65f8043872SChris Boot #define BCM2835_SPI_CS_CS_10		0x00000002
66f8043872SChris Boot #define BCM2835_SPI_CS_CS_01		0x00000001
67f8043872SChris Boot 
682e0733bcSLukas Wunner #define BCM2835_SPI_FIFO_SIZE		64
692e0733bcSLukas Wunner #define BCM2835_SPI_FIFO_SIZE_3_4	48
703ecd37edSMartin Sperl #define BCM2835_SPI_DMA_MIN_LENGTH	96
716935224dSMartin Sperl #define BCM2835_SPI_MODE_BITS	(SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
726935224dSMartin Sperl 				| SPI_NO_CS | SPI_3WIRE)
73f8043872SChris Boot 
74f8043872SChris Boot #define DRV_NAME	"spi-bcm2835"
75f8043872SChris Boot 
76ff245d90SMartin Sperl /* define polling limits */
77cbd632eaSJason Yan static unsigned int polling_limit_us = 30;
78ff245d90SMartin Sperl module_param(polling_limit_us, uint, 0664);
79ff245d90SMartin Sperl MODULE_PARM_DESC(polling_limit_us,
80ff245d90SMartin Sperl 		 "time in us to run a transfer in polling mode\n");
81ff245d90SMartin Sperl 
82acf0f856SLukas Wunner /**
83acf0f856SLukas Wunner  * struct bcm2835_spi - BCM2835 SPI controller
84acf0f856SLukas Wunner  * @regs: base address of register map
85acf0f856SLukas Wunner  * @clk: core clock, divided to calculate serial clock
86acf0f856SLukas Wunner  * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full
873bd7f658SLukas Wunner  * @tfr: SPI transfer currently processed
88afe7e363SRobin Murphy  * @ctlr: SPI controller reverse lookup
89acf0f856SLukas Wunner  * @tx_buf: pointer whence next transmitted byte is read
90acf0f856SLukas Wunner  * @rx_buf: pointer where next received byte is written
91acf0f856SLukas Wunner  * @tx_len: remaining bytes to transmit
92acf0f856SLukas Wunner  * @rx_len: remaining bytes to receive
933bd7f658SLukas Wunner  * @tx_prologue: bytes transmitted without DMA if first TX sglist entry's
943bd7f658SLukas Wunner  *	length is not a multiple of 4 (to overcome hardware limitation)
953bd7f658SLukas Wunner  * @rx_prologue: bytes received without DMA if first RX sglist entry's
963bd7f658SLukas Wunner  *	length is not a multiple of 4 (to overcome hardware limitation)
973bd7f658SLukas Wunner  * @tx_spillover: whether @tx_prologue spills over to second TX sglist entry
98154f7da5SMartin Sperl  * @debugfs_dir: the debugfs directory - neede to remove debugfs when
99154f7da5SMartin Sperl  *      unloading the module
100154f7da5SMartin Sperl  * @count_transfer_polling: count of how often polling mode is used
101154f7da5SMartin Sperl  * @count_transfer_irq: count of how often interrupt mode is used
102154f7da5SMartin Sperl  * @count_transfer_irq_after_polling: count of how often we fall back to
103154f7da5SMartin Sperl  *      interrupt mode after starting in polling mode.
104154f7da5SMartin Sperl  *      These are counted as well in @count_transfer_polling and
105154f7da5SMartin Sperl  *      @count_transfer_irq
106154f7da5SMartin Sperl  * @count_transfer_dma: count how often dma mode is used
107*ec679bdaSLukas Wunner  * @slv: SPI slave currently selected
1088259bf66SLukas Wunner  *	(used by bcm2835_spi_dma_tx_done() to write @clear_rx_cs)
1098259bf66SLukas Wunner  * @tx_dma_active: whether a TX DMA descriptor is in progress
1108259bf66SLukas Wunner  * @rx_dma_active: whether a RX DMA descriptor is in progress
1118259bf66SLukas Wunner  *	(used by bcm2835_spi_dma_tx_done() to handle a race)
1122b8279aeSLukas Wunner  * @fill_tx_desc: preallocated TX DMA descriptor used for RX-only transfers
1132b8279aeSLukas Wunner  *	(cyclically copies from zero page to TX FIFO)
1142b8279aeSLukas Wunner  * @fill_tx_addr: bus address of zero page
115acf0f856SLukas Wunner  */
116f8043872SChris Boot struct bcm2835_spi {
117f8043872SChris Boot 	void __iomem *regs;
118f8043872SChris Boot 	struct clk *clk;
119f8043872SChris Boot 	int irq;
1203bd7f658SLukas Wunner 	struct spi_transfer *tfr;
121afe7e363SRobin Murphy 	struct spi_controller *ctlr;
122f8043872SChris Boot 	const u8 *tx_buf;
123f8043872SChris Boot 	u8 *rx_buf;
124e34ff011SMartin Sperl 	int tx_len;
125e34ff011SMartin Sperl 	int rx_len;
1263bd7f658SLukas Wunner 	int tx_prologue;
1273bd7f658SLukas Wunner 	int rx_prologue;
128b31a9299SLukas Wunner 	unsigned int tx_spillover;
129154f7da5SMartin Sperl 
130154f7da5SMartin Sperl 	struct dentry *debugfs_dir;
131154f7da5SMartin Sperl 	u64 count_transfer_polling;
132154f7da5SMartin Sperl 	u64 count_transfer_irq;
133154f7da5SMartin Sperl 	u64 count_transfer_irq_after_polling;
134154f7da5SMartin Sperl 	u64 count_transfer_dma;
1358259bf66SLukas Wunner 
136*ec679bdaSLukas Wunner 	struct bcm2835_spidev *slv;
1378259bf66SLukas Wunner 	unsigned int tx_dma_active;
1388259bf66SLukas Wunner 	unsigned int rx_dma_active;
1392b8279aeSLukas Wunner 	struct dma_async_tx_descriptor *fill_tx_desc;
1402b8279aeSLukas Wunner 	dma_addr_t fill_tx_addr;
141*ec679bdaSLukas Wunner };
142*ec679bdaSLukas Wunner 
143*ec679bdaSLukas Wunner /**
144*ec679bdaSLukas Wunner  * struct bcm2835_spidev - BCM2835 SPI slave
145*ec679bdaSLukas Wunner  * @prepare_cs: precalculated CS register value for ->prepare_message()
146*ec679bdaSLukas Wunner  *	(uses slave-specific clock polarity and phase settings)
147*ec679bdaSLukas Wunner  * @clear_rx_desc: preallocated RX DMA descriptor used for TX-only transfers
148*ec679bdaSLukas Wunner  *	(cyclically clears RX FIFO by writing @clear_rx_cs to CS register)
149*ec679bdaSLukas Wunner  * @clear_rx_addr: bus address of @clear_rx_cs
150*ec679bdaSLukas Wunner  * @clear_rx_cs: precalculated CS register value to clear RX FIFO
151*ec679bdaSLukas Wunner  *	(uses slave-specific clock polarity and phase settings)
152*ec679bdaSLukas Wunner  */
153*ec679bdaSLukas Wunner struct bcm2835_spidev {
154*ec679bdaSLukas Wunner 	u32 prepare_cs;
155*ec679bdaSLukas Wunner 	struct dma_async_tx_descriptor *clear_rx_desc;
1568259bf66SLukas Wunner 	dma_addr_t clear_rx_addr;
157*ec679bdaSLukas Wunner 	u32 clear_rx_cs ____cacheline_aligned;
158f8043872SChris Boot };
159f8043872SChris Boot 
160154f7da5SMartin Sperl #if defined(CONFIG_DEBUG_FS)
161154f7da5SMartin Sperl static void bcm2835_debugfs_create(struct bcm2835_spi *bs,
162154f7da5SMartin Sperl 				   const char *dname)
163154f7da5SMartin Sperl {
164154f7da5SMartin Sperl 	char name[64];
165154f7da5SMartin Sperl 	struct dentry *dir;
166154f7da5SMartin Sperl 
167154f7da5SMartin Sperl 	/* get full name */
168154f7da5SMartin Sperl 	snprintf(name, sizeof(name), "spi-bcm2835-%s", dname);
169154f7da5SMartin Sperl 
170154f7da5SMartin Sperl 	/* the base directory */
171154f7da5SMartin Sperl 	dir = debugfs_create_dir(name, NULL);
172154f7da5SMartin Sperl 	bs->debugfs_dir = dir;
173154f7da5SMartin Sperl 
174154f7da5SMartin Sperl 	/* the counters */
175154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_polling", 0444, dir,
176154f7da5SMartin Sperl 			   &bs->count_transfer_polling);
177154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_irq", 0444, dir,
178154f7da5SMartin Sperl 			   &bs->count_transfer_irq);
179154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_irq_after_polling", 0444, dir,
180154f7da5SMartin Sperl 			   &bs->count_transfer_irq_after_polling);
181154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_dma", 0444, dir,
182154f7da5SMartin Sperl 			   &bs->count_transfer_dma);
183154f7da5SMartin Sperl }
184154f7da5SMartin Sperl 
185154f7da5SMartin Sperl static void bcm2835_debugfs_remove(struct bcm2835_spi *bs)
186154f7da5SMartin Sperl {
187154f7da5SMartin Sperl 	debugfs_remove_recursive(bs->debugfs_dir);
188154f7da5SMartin Sperl 	bs->debugfs_dir = NULL;
189154f7da5SMartin Sperl }
190154f7da5SMartin Sperl #else
191154f7da5SMartin Sperl static void bcm2835_debugfs_create(struct bcm2835_spi *bs,
192154f7da5SMartin Sperl 				   const char *dname)
193154f7da5SMartin Sperl {
194154f7da5SMartin Sperl }
195154f7da5SMartin Sperl 
196154f7da5SMartin Sperl static void bcm2835_debugfs_remove(struct bcm2835_spi *bs)
197154f7da5SMartin Sperl {
198154f7da5SMartin Sperl }
199154f7da5SMartin Sperl #endif /* CONFIG_DEBUG_FS */
200154f7da5SMartin Sperl 
201e37687c9SJacko Dirks static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned int reg)
202f8043872SChris Boot {
203f8043872SChris Boot 	return readl(bs->regs + reg);
204f8043872SChris Boot }
205f8043872SChris Boot 
206e37687c9SJacko Dirks static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned int reg, u32 val)
207f8043872SChris Boot {
208f8043872SChris Boot 	writel(val, bs->regs + reg);
209f8043872SChris Boot }
210f8043872SChris Boot 
2114adf3129SMartin Sperl static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs)
212f8043872SChris Boot {
213f8043872SChris Boot 	u8 byte;
214f8043872SChris Boot 
215e34ff011SMartin Sperl 	while ((bs->rx_len) &&
216e34ff011SMartin Sperl 	       (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
217f8043872SChris Boot 		byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
218f8043872SChris Boot 		if (bs->rx_buf)
219f8043872SChris Boot 			*bs->rx_buf++ = byte;
220e34ff011SMartin Sperl 		bs->rx_len--;
221f8043872SChris Boot 	}
222f8043872SChris Boot }
223f8043872SChris Boot 
2244adf3129SMartin Sperl static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
225f8043872SChris Boot {
226f8043872SChris Boot 	u8 byte;
227f8043872SChris Boot 
228e34ff011SMartin Sperl 	while ((bs->tx_len) &&
2294adf3129SMartin Sperl 	       (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
230f8043872SChris Boot 		byte = bs->tx_buf ? *bs->tx_buf++ : 0;
231f8043872SChris Boot 		bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
232e34ff011SMartin Sperl 		bs->tx_len--;
233f8043872SChris Boot 	}
234f8043872SChris Boot }
235f8043872SChris Boot 
2363bd7f658SLukas Wunner /**
2373bd7f658SLukas Wunner  * bcm2835_rd_fifo_count() - blindly read exactly @count bytes from RX FIFO
2383bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
2393bd7f658SLukas Wunner  * @count: bytes to read from RX FIFO
2403bd7f658SLukas Wunner  *
2413bd7f658SLukas Wunner  * The caller must ensure that @bs->rx_len is greater than or equal to @count,
2423bd7f658SLukas Wunner  * that the RX FIFO contains at least @count bytes and that the DMA Enable flag
2433bd7f658SLukas Wunner  * in the CS register is set (such that a read from the FIFO register receives
244b31a9299SLukas Wunner  * 32-bit instead of just 8-bit).  Moreover @bs->rx_buf must not be %NULL.
2453bd7f658SLukas Wunner  */
2463bd7f658SLukas Wunner static inline void bcm2835_rd_fifo_count(struct bcm2835_spi *bs, int count)
2473bd7f658SLukas Wunner {
2483bd7f658SLukas Wunner 	u32 val;
249b31a9299SLukas Wunner 	int len;
2503bd7f658SLukas Wunner 
2513bd7f658SLukas Wunner 	bs->rx_len -= count;
2523bd7f658SLukas Wunner 
25326751de2SRobin Murphy 	do {
2543bd7f658SLukas Wunner 		val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
255b31a9299SLukas Wunner 		len = min(count, 4);
2563bd7f658SLukas Wunner 		memcpy(bs->rx_buf, &val, len);
2573bd7f658SLukas Wunner 		bs->rx_buf += len;
2583bd7f658SLukas Wunner 		count -= 4;
25926751de2SRobin Murphy 	} while (count > 0);
2603bd7f658SLukas Wunner }
2613bd7f658SLukas Wunner 
2623bd7f658SLukas Wunner /**
2633bd7f658SLukas Wunner  * bcm2835_wr_fifo_count() - blindly write exactly @count bytes to TX FIFO
2643bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
2653bd7f658SLukas Wunner  * @count: bytes to write to TX FIFO
2663bd7f658SLukas Wunner  *
2673bd7f658SLukas Wunner  * The caller must ensure that @bs->tx_len is greater than or equal to @count,
2683bd7f658SLukas Wunner  * that the TX FIFO can accommodate @count bytes and that the DMA Enable flag
2693bd7f658SLukas Wunner  * in the CS register is set (such that a write to the FIFO register transmits
2703bd7f658SLukas Wunner  * 32-bit instead of just 8-bit).
2713bd7f658SLukas Wunner  */
2723bd7f658SLukas Wunner static inline void bcm2835_wr_fifo_count(struct bcm2835_spi *bs, int count)
2733bd7f658SLukas Wunner {
2743bd7f658SLukas Wunner 	u32 val;
275b31a9299SLukas Wunner 	int len;
2763bd7f658SLukas Wunner 
2773bd7f658SLukas Wunner 	bs->tx_len -= count;
2783bd7f658SLukas Wunner 
27926751de2SRobin Murphy 	do {
2803bd7f658SLukas Wunner 		if (bs->tx_buf) {
281b31a9299SLukas Wunner 			len = min(count, 4);
2823bd7f658SLukas Wunner 			memcpy(&val, bs->tx_buf, len);
2833bd7f658SLukas Wunner 			bs->tx_buf += len;
2843bd7f658SLukas Wunner 		} else {
2853bd7f658SLukas Wunner 			val = 0;
2863bd7f658SLukas Wunner 		}
2873bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
2883bd7f658SLukas Wunner 		count -= 4;
28926751de2SRobin Murphy 	} while (count > 0);
2903bd7f658SLukas Wunner }
2913bd7f658SLukas Wunner 
2923bd7f658SLukas Wunner /**
2933bd7f658SLukas Wunner  * bcm2835_wait_tx_fifo_empty() - busy-wait for TX FIFO to empty
2943bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
295b31a9299SLukas Wunner  *
296b31a9299SLukas Wunner  * The caller must ensure that the RX FIFO can accommodate as many bytes
297b31a9299SLukas Wunner  * as have been written to the TX FIFO:  Transmission is halted once the
298b31a9299SLukas Wunner  * RX FIFO is full, causing this function to spin forever.
2993bd7f658SLukas Wunner  */
3003bd7f658SLukas Wunner static inline void bcm2835_wait_tx_fifo_empty(struct bcm2835_spi *bs)
3013bd7f658SLukas Wunner {
3023bd7f658SLukas Wunner 	while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE))
3033bd7f658SLukas Wunner 		cpu_relax();
3043bd7f658SLukas Wunner }
3053bd7f658SLukas Wunner 
3062e0733bcSLukas Wunner /**
3072e0733bcSLukas Wunner  * bcm2835_rd_fifo_blind() - blindly read up to @count bytes from RX FIFO
3082e0733bcSLukas Wunner  * @bs: BCM2835 SPI controller
3092e0733bcSLukas Wunner  * @count: bytes available for reading in RX FIFO
3102e0733bcSLukas Wunner  */
3112e0733bcSLukas Wunner static inline void bcm2835_rd_fifo_blind(struct bcm2835_spi *bs, int count)
3122e0733bcSLukas Wunner {
3132e0733bcSLukas Wunner 	u8 val;
3142e0733bcSLukas Wunner 
3152e0733bcSLukas Wunner 	count = min(count, bs->rx_len);
3162e0733bcSLukas Wunner 	bs->rx_len -= count;
3172e0733bcSLukas Wunner 
31826751de2SRobin Murphy 	do {
3192e0733bcSLukas Wunner 		val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
3202e0733bcSLukas Wunner 		if (bs->rx_buf)
3212e0733bcSLukas Wunner 			*bs->rx_buf++ = val;
32226751de2SRobin Murphy 	} while (--count);
3232e0733bcSLukas Wunner }
3242e0733bcSLukas Wunner 
3252e0733bcSLukas Wunner /**
3262e0733bcSLukas Wunner  * bcm2835_wr_fifo_blind() - blindly write up to @count bytes to TX FIFO
3272e0733bcSLukas Wunner  * @bs: BCM2835 SPI controller
3282e0733bcSLukas Wunner  * @count: bytes available for writing in TX FIFO
3292e0733bcSLukas Wunner  */
3302e0733bcSLukas Wunner static inline void bcm2835_wr_fifo_blind(struct bcm2835_spi *bs, int count)
3312e0733bcSLukas Wunner {
3322e0733bcSLukas Wunner 	u8 val;
3332e0733bcSLukas Wunner 
3342e0733bcSLukas Wunner 	count = min(count, bs->tx_len);
3352e0733bcSLukas Wunner 	bs->tx_len -= count;
3362e0733bcSLukas Wunner 
33726751de2SRobin Murphy 	do {
3382e0733bcSLukas Wunner 		val = bs->tx_buf ? *bs->tx_buf++ : 0;
3392e0733bcSLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
34026751de2SRobin Murphy 	} while (--count);
3412e0733bcSLukas Wunner }
3422e0733bcSLukas Wunner 
343ac4648b5SRobin Murphy static void bcm2835_spi_reset_hw(struct bcm2835_spi *bs)
344e34ff011SMartin Sperl {
345e34ff011SMartin Sperl 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
346e34ff011SMartin Sperl 
347e34ff011SMartin Sperl 	/* Disable SPI interrupts and transfer */
348e34ff011SMartin Sperl 	cs &= ~(BCM2835_SPI_CS_INTR |
349e34ff011SMartin Sperl 		BCM2835_SPI_CS_INTD |
3503ecd37edSMartin Sperl 		BCM2835_SPI_CS_DMAEN |
351e34ff011SMartin Sperl 		BCM2835_SPI_CS_TA);
3524c524191SLukas Wunner 	/*
3534c524191SLukas Wunner 	 * Transmission sometimes breaks unless the DONE bit is written at the
3544c524191SLukas Wunner 	 * end of every transfer.  The spec says it's a RO bit.  Either the
3554c524191SLukas Wunner 	 * spec is wrong and the bit is actually of type RW1C, or it's a
3564c524191SLukas Wunner 	 * hardware erratum.
3574c524191SLukas Wunner 	 */
3584c524191SLukas Wunner 	cs |= BCM2835_SPI_CS_DONE;
359e34ff011SMartin Sperl 	/* and reset RX/TX FIFOS */
360e34ff011SMartin Sperl 	cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
361e34ff011SMartin Sperl 
362e34ff011SMartin Sperl 	/* and reset the SPI_HW */
363e34ff011SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS, cs);
3643ecd37edSMartin Sperl 	/* as well as DLEN */
3653ecd37edSMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_DLEN, 0);
366e34ff011SMartin Sperl }
367e34ff011SMartin Sperl 
368f8043872SChris Boot static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
369f8043872SChris Boot {
370afe7e363SRobin Murphy 	struct bcm2835_spi *bs = dev_id;
3712e0733bcSLukas Wunner 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
3722e0733bcSLukas Wunner 
3732e0733bcSLukas Wunner 	/*
3742e0733bcSLukas Wunner 	 * An interrupt is signaled either if DONE is set (TX FIFO empty)
3752e0733bcSLukas Wunner 	 * or if RXR is set (RX FIFO >= ¾ full).
3762e0733bcSLukas Wunner 	 */
3772e0733bcSLukas Wunner 	if (cs & BCM2835_SPI_CS_RXF)
3782e0733bcSLukas Wunner 		bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
3792e0733bcSLukas Wunner 	else if (cs & BCM2835_SPI_CS_RXR)
3802e0733bcSLukas Wunner 		bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE_3_4);
3812e0733bcSLukas Wunner 
3822e0733bcSLukas Wunner 	if (bs->tx_len && cs & BCM2835_SPI_CS_DONE)
3832e0733bcSLukas Wunner 		bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
384f8043872SChris Boot 
3854adf3129SMartin Sperl 	/* Read as many bytes as possible from FIFO */
3864adf3129SMartin Sperl 	bcm2835_rd_fifo(bs);
387e34ff011SMartin Sperl 	/* Write as many bytes as possible to FIFO */
3884adf3129SMartin Sperl 	bcm2835_wr_fifo(bs);
389f8043872SChris Boot 
39056c17234SLukas Wunner 	if (!bs->rx_len) {
391e34ff011SMartin Sperl 		/* Transfer complete - reset SPI HW */
392ac4648b5SRobin Murphy 		bcm2835_spi_reset_hw(bs);
393e34ff011SMartin Sperl 		/* wake up the framework */
394ccae0b40SVincent Pelletier 		spi_finalize_current_transfer(bs->ctlr);
395f8043872SChris Boot 	}
396f8043872SChris Boot 
397f8043872SChris Boot 	return IRQ_HANDLED;
398f8043872SChris Boot }
399f8043872SChris Boot 
4005f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_irq(struct spi_controller *ctlr,
401704f32d4SMartin Sperl 					struct spi_device *spi,
402704f32d4SMartin Sperl 					struct spi_transfer *tfr,
4032e0733bcSLukas Wunner 					u32 cs, bool fifo_empty)
404704f32d4SMartin Sperl {
4055f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
406f8043872SChris Boot 
407154f7da5SMartin Sperl 	/* update usage statistics */
408154f7da5SMartin Sperl 	bs->count_transfer_irq++;
409154f7da5SMartin Sperl 
410f8043872SChris Boot 	/*
4115c09e42fSLukas Wunner 	 * Enable HW block, but with interrupts still disabled.
4125c09e42fSLukas Wunner 	 * Otherwise the empty TX FIFO would immediately trigger an interrupt.
413f8043872SChris Boot 	 */
4145c09e42fSLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
4155c09e42fSLukas Wunner 
4165c09e42fSLukas Wunner 	/* fill TX FIFO as much as possible */
4172e0733bcSLukas Wunner 	if (fifo_empty)
4182e0733bcSLukas Wunner 		bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
4195c09e42fSLukas Wunner 	bcm2835_wr_fifo(bs);
4205c09e42fSLukas Wunner 
4215c09e42fSLukas Wunner 	/* enable interrupts */
422e34ff011SMartin Sperl 	cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
423f8043872SChris Boot 	bcm2835_wr(bs, BCM2835_SPI_CS, cs);
424f8043872SChris Boot 
425e34ff011SMartin Sperl 	/* signal that we need to wait for completion */
426e34ff011SMartin Sperl 	return 1;
427f8043872SChris Boot }
428f8043872SChris Boot 
4293bd7f658SLukas Wunner /**
4303bd7f658SLukas Wunner  * bcm2835_spi_transfer_prologue() - transfer first few bytes without DMA
4315f336ea5SLukas Wunner  * @ctlr: SPI master controller
4323bd7f658SLukas Wunner  * @tfr: SPI transfer
4333bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
4343bd7f658SLukas Wunner  * @cs: CS register
4353bd7f658SLukas Wunner  *
4363bd7f658SLukas Wunner  * A limitation in DMA mode is that the FIFO must be accessed in 4 byte chunks.
4373bd7f658SLukas Wunner  * Only the final write access is permitted to transmit less than 4 bytes, the
4383bd7f658SLukas Wunner  * SPI controller deduces its intended size from the DLEN register.
4393bd7f658SLukas Wunner  *
4403bd7f658SLukas Wunner  * If a TX or RX sglist contains multiple entries, one per page, and the first
4413bd7f658SLukas Wunner  * entry starts in the middle of a page, that first entry's length may not be
4423bd7f658SLukas Wunner  * a multiple of 4.  Subsequent entries are fine because they span an entire
4433bd7f658SLukas Wunner  * page, hence do have a length that's a multiple of 4.
4443bd7f658SLukas Wunner  *
4453bd7f658SLukas Wunner  * This cannot happen with kmalloc'ed buffers (which is what most clients use)
4463bd7f658SLukas Wunner  * because they are contiguous in physical memory and therefore not split on
4473bd7f658SLukas Wunner  * page boundaries by spi_map_buf().  But it *can* happen with vmalloc'ed
4483bd7f658SLukas Wunner  * buffers.
4493bd7f658SLukas Wunner  *
4503bd7f658SLukas Wunner  * The DMA engine is incapable of combining sglist entries into a continuous
4513bd7f658SLukas Wunner  * stream of 4 byte chunks, it treats every entry separately:  A TX entry is
4523bd7f658SLukas Wunner  * rounded up a to a multiple of 4 bytes by transmitting surplus bytes, an RX
4533bd7f658SLukas Wunner  * entry is rounded up by throwing away received bytes.
4543bd7f658SLukas Wunner  *
4553bd7f658SLukas Wunner  * Overcome this limitation by transferring the first few bytes without DMA:
4563bd7f658SLukas Wunner  * E.g. if the first TX sglist entry's length is 23 and the first RX's is 42,
4573bd7f658SLukas Wunner  * write 3 bytes to the TX FIFO but read only 2 bytes from the RX FIFO.
4583bd7f658SLukas Wunner  * The residue of 1 byte in the RX FIFO is picked up by DMA.  Together with
4593bd7f658SLukas Wunner  * the rest of the first RX sglist entry it makes up a multiple of 4 bytes.
4603bd7f658SLukas Wunner  *
4613bd7f658SLukas Wunner  * Should the RX prologue be larger, say, 3 vis-à-vis a TX prologue of 1,
4623bd7f658SLukas Wunner  * write 1 + 4 = 5 bytes to the TX FIFO and read 3 bytes from the RX FIFO.
4633bd7f658SLukas Wunner  * Caution, the additional 4 bytes spill over to the second TX sglist entry
4643bd7f658SLukas Wunner  * if the length of the first is *exactly* 1.
4653bd7f658SLukas Wunner  *
4663bd7f658SLukas Wunner  * At most 6 bytes are written and at most 3 bytes read.  Do we know the
4673bd7f658SLukas Wunner  * transfer has this many bytes?  Yes, see BCM2835_SPI_DMA_MIN_LENGTH.
4683bd7f658SLukas Wunner  *
4693bd7f658SLukas Wunner  * The FIFO is normally accessed with 8-bit width by the CPU and 32-bit width
4703bd7f658SLukas Wunner  * by the DMA engine.  Toggling the DMA Enable flag in the CS register switches
4713bd7f658SLukas Wunner  * the width but also garbles the FIFO's contents.  The prologue must therefore
4723bd7f658SLukas Wunner  * be transmitted in 32-bit width to ensure that the following DMA transfer can
4733bd7f658SLukas Wunner  * pick up the residue in the RX FIFO in ungarbled form.
4743bd7f658SLukas Wunner  */
4755f336ea5SLukas Wunner static void bcm2835_spi_transfer_prologue(struct spi_controller *ctlr,
4763bd7f658SLukas Wunner 					  struct spi_transfer *tfr,
4773bd7f658SLukas Wunner 					  struct bcm2835_spi *bs,
4783bd7f658SLukas Wunner 					  u32 cs)
4793bd7f658SLukas Wunner {
4803bd7f658SLukas Wunner 	int tx_remaining;
4813bd7f658SLukas Wunner 
4823bd7f658SLukas Wunner 	bs->tfr		 = tfr;
4833bd7f658SLukas Wunner 	bs->tx_prologue  = 0;
4843bd7f658SLukas Wunner 	bs->rx_prologue  = 0;
4853bd7f658SLukas Wunner 	bs->tx_spillover = false;
4863bd7f658SLukas Wunner 
4872b8279aeSLukas Wunner 	if (bs->tx_buf && !sg_is_last(&tfr->tx_sg.sgl[0]))
4883bd7f658SLukas Wunner 		bs->tx_prologue = sg_dma_len(&tfr->tx_sg.sgl[0]) & 3;
4893bd7f658SLukas Wunner 
4908259bf66SLukas Wunner 	if (bs->rx_buf && !sg_is_last(&tfr->rx_sg.sgl[0])) {
4913bd7f658SLukas Wunner 		bs->rx_prologue = sg_dma_len(&tfr->rx_sg.sgl[0]) & 3;
4923bd7f658SLukas Wunner 
4933bd7f658SLukas Wunner 		if (bs->rx_prologue > bs->tx_prologue) {
4942b8279aeSLukas Wunner 			if (!bs->tx_buf || sg_is_last(&tfr->tx_sg.sgl[0])) {
4953bd7f658SLukas Wunner 				bs->tx_prologue  = bs->rx_prologue;
4963bd7f658SLukas Wunner 			} else {
4973bd7f658SLukas Wunner 				bs->tx_prologue += 4;
4983bd7f658SLukas Wunner 				bs->tx_spillover =
4993bd7f658SLukas Wunner 					!(sg_dma_len(&tfr->tx_sg.sgl[0]) & ~3);
5003bd7f658SLukas Wunner 			}
5013bd7f658SLukas Wunner 		}
5023bd7f658SLukas Wunner 	}
5033bd7f658SLukas Wunner 
5043bd7f658SLukas Wunner 	/* rx_prologue > 0 implies tx_prologue > 0, so check only the latter */
5053bd7f658SLukas Wunner 	if (!bs->tx_prologue)
5063bd7f658SLukas Wunner 		return;
5073bd7f658SLukas Wunner 
5083bd7f658SLukas Wunner 	/* Write and read RX prologue.  Adjust first entry in RX sglist. */
5093bd7f658SLukas Wunner 	if (bs->rx_prologue) {
5103bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->rx_prologue);
5113bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
5123bd7f658SLukas Wunner 						  | BCM2835_SPI_CS_DMAEN);
5133bd7f658SLukas Wunner 		bcm2835_wr_fifo_count(bs, bs->rx_prologue);
5143bd7f658SLukas Wunner 		bcm2835_wait_tx_fifo_empty(bs);
5153bd7f658SLukas Wunner 		bcm2835_rd_fifo_count(bs, bs->rx_prologue);
5164c524191SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_RX
5174c524191SLukas Wunner 						  | BCM2835_SPI_CS_CLEAR_TX
5184c524191SLukas Wunner 						  | BCM2835_SPI_CS_DONE);
5193bd7f658SLukas Wunner 
5205f336ea5SLukas Wunner 		dma_sync_single_for_device(ctlr->dma_rx->device->dev,
521b31a9299SLukas Wunner 					   sg_dma_address(&tfr->rx_sg.sgl[0]),
522b31a9299SLukas Wunner 					   bs->rx_prologue, DMA_FROM_DEVICE);
5233bd7f658SLukas Wunner 
524b31a9299SLukas Wunner 		sg_dma_address(&tfr->rx_sg.sgl[0]) += bs->rx_prologue;
525b31a9299SLukas Wunner 		sg_dma_len(&tfr->rx_sg.sgl[0])     -= bs->rx_prologue;
5263bd7f658SLukas Wunner 	}
5273bd7f658SLukas Wunner 
5282b8279aeSLukas Wunner 	if (!bs->tx_buf)
5292b8279aeSLukas Wunner 		return;
5302b8279aeSLukas Wunner 
5313bd7f658SLukas Wunner 	/*
5323bd7f658SLukas Wunner 	 * Write remaining TX prologue.  Adjust first entry in TX sglist.
5333bd7f658SLukas Wunner 	 * Also adjust second entry if prologue spills over to it.
5343bd7f658SLukas Wunner 	 */
5353bd7f658SLukas Wunner 	tx_remaining = bs->tx_prologue - bs->rx_prologue;
5363bd7f658SLukas Wunner 	if (tx_remaining) {
5373bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_DLEN, tx_remaining);
5383bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
5393bd7f658SLukas Wunner 						  | BCM2835_SPI_CS_DMAEN);
5403bd7f658SLukas Wunner 		bcm2835_wr_fifo_count(bs, tx_remaining);
5413bd7f658SLukas Wunner 		bcm2835_wait_tx_fifo_empty(bs);
5424c524191SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_TX
5434c524191SLukas Wunner 						  | BCM2835_SPI_CS_DONE);
5443bd7f658SLukas Wunner 	}
5453bd7f658SLukas Wunner 
5463bd7f658SLukas Wunner 	if (likely(!bs->tx_spillover)) {
547b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[0]) += bs->tx_prologue;
548b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])     -= bs->tx_prologue;
5493bd7f658SLukas Wunner 	} else {
550b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])      = 0;
551b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[1]) += 4;
552b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[1])     -= 4;
5533bd7f658SLukas Wunner 	}
5543bd7f658SLukas Wunner }
5553bd7f658SLukas Wunner 
5563bd7f658SLukas Wunner /**
5573bd7f658SLukas Wunner  * bcm2835_spi_undo_prologue() - reconstruct original sglist state
5583bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
5593bd7f658SLukas Wunner  *
5603bd7f658SLukas Wunner  * Undo changes which were made to an SPI transfer's sglist when transmitting
5613bd7f658SLukas Wunner  * the prologue.  This is necessary to ensure the same memory ranges are
5623bd7f658SLukas Wunner  * unmapped that were originally mapped.
5633bd7f658SLukas Wunner  */
5643bd7f658SLukas Wunner static void bcm2835_spi_undo_prologue(struct bcm2835_spi *bs)
5653bd7f658SLukas Wunner {
5663bd7f658SLukas Wunner 	struct spi_transfer *tfr = bs->tfr;
5673bd7f658SLukas Wunner 
5683bd7f658SLukas Wunner 	if (!bs->tx_prologue)
5693bd7f658SLukas Wunner 		return;
5703bd7f658SLukas Wunner 
5713bd7f658SLukas Wunner 	if (bs->rx_prologue) {
572b31a9299SLukas Wunner 		sg_dma_address(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue;
573b31a9299SLukas Wunner 		sg_dma_len(&tfr->rx_sg.sgl[0])     += bs->rx_prologue;
5743bd7f658SLukas Wunner 	}
5753bd7f658SLukas Wunner 
5762b8279aeSLukas Wunner 	if (!bs->tx_buf)
5772b8279aeSLukas Wunner 		goto out;
5782b8279aeSLukas Wunner 
5793bd7f658SLukas Wunner 	if (likely(!bs->tx_spillover)) {
580b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue;
581b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])     += bs->tx_prologue;
5823bd7f658SLukas Wunner 	} else {
583b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])      = bs->tx_prologue - 4;
584b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[1]) -= 4;
585b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[1])     += 4;
5863bd7f658SLukas Wunner 	}
5872b8279aeSLukas Wunner out:
5881513ceeeSLukas Wunner 	bs->tx_prologue = 0;
5893bd7f658SLukas Wunner }
5903bd7f658SLukas Wunner 
5918259bf66SLukas Wunner /**
5928259bf66SLukas Wunner  * bcm2835_spi_dma_rx_done() - callback for DMA RX channel
5938259bf66SLukas Wunner  * @data: SPI master controller
5948259bf66SLukas Wunner  *
5958259bf66SLukas Wunner  * Used for bidirectional and RX-only transfers.
5968259bf66SLukas Wunner  */
5978259bf66SLukas Wunner static void bcm2835_spi_dma_rx_done(void *data)
5983ecd37edSMartin Sperl {
5995f336ea5SLukas Wunner 	struct spi_controller *ctlr = data;
6005f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
6013ecd37edSMartin Sperl 
6022b8279aeSLukas Wunner 	/* terminate tx-dma as we do not have an irq for it
6033ecd37edSMartin Sperl 	 * because when the rx dma will terminate and this callback
6043ecd37edSMartin Sperl 	 * is called the tx-dma must have finished - can't get to this
6053ecd37edSMartin Sperl 	 * situation otherwise...
6063ecd37edSMartin Sperl 	 */
6075f336ea5SLukas Wunner 	dmaengine_terminate_async(ctlr->dma_tx);
6088259bf66SLukas Wunner 	bs->tx_dma_active = false;
6098259bf66SLukas Wunner 	bs->rx_dma_active = false;
6103bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
6113ecd37edSMartin Sperl 
6122b8279aeSLukas Wunner 	/* reset fifo and HW */
613ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
6143ecd37edSMartin Sperl 
6153ecd37edSMartin Sperl 	/* and mark as completed */;
616ccae0b40SVincent Pelletier 	spi_finalize_current_transfer(ctlr);
6173ecd37edSMartin Sperl }
6183ecd37edSMartin Sperl 
6198259bf66SLukas Wunner /**
6208259bf66SLukas Wunner  * bcm2835_spi_dma_tx_done() - callback for DMA TX channel
6218259bf66SLukas Wunner  * @data: SPI master controller
6228259bf66SLukas Wunner  *
6238259bf66SLukas Wunner  * Used for TX-only transfers.
6248259bf66SLukas Wunner  */
6258259bf66SLukas Wunner static void bcm2835_spi_dma_tx_done(void *data)
6268259bf66SLukas Wunner {
6278259bf66SLukas Wunner 	struct spi_controller *ctlr = data;
6288259bf66SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
6298259bf66SLukas Wunner 
6308259bf66SLukas Wunner 	/* busy-wait for TX FIFO to empty */
6318259bf66SLukas Wunner 	while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE))
632*ec679bdaSLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, bs->slv->clear_rx_cs);
6338259bf66SLukas Wunner 
6348259bf66SLukas Wunner 	bs->tx_dma_active = false;
6358259bf66SLukas Wunner 	smp_wmb();
6368259bf66SLukas Wunner 
6378259bf66SLukas Wunner 	/*
6388259bf66SLukas Wunner 	 * In case of a very short transfer, RX DMA may not have been
6398259bf66SLukas Wunner 	 * issued yet.  The onus is then on bcm2835_spi_transfer_one_dma()
6408259bf66SLukas Wunner 	 * to terminate it immediately after issuing.
6418259bf66SLukas Wunner 	 */
6428259bf66SLukas Wunner 	if (cmpxchg(&bs->rx_dma_active, true, false))
6438259bf66SLukas Wunner 		dmaengine_terminate_async(ctlr->dma_rx);
6448259bf66SLukas Wunner 
6458259bf66SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
646ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
647ccae0b40SVincent Pelletier 	spi_finalize_current_transfer(ctlr);
6488259bf66SLukas Wunner }
6498259bf66SLukas Wunner 
6508259bf66SLukas Wunner /**
6518259bf66SLukas Wunner  * bcm2835_spi_prepare_sg() - prepare and submit DMA descriptor for sglist
6528259bf66SLukas Wunner  * @ctlr: SPI master controller
6538259bf66SLukas Wunner  * @tfr: SPI transfer
6548259bf66SLukas Wunner  * @bs: BCM2835 SPI controller
655*ec679bdaSLukas Wunner  * @slv: BCM2835 SPI slave
6568259bf66SLukas Wunner  * @is_tx: whether to submit DMA descriptor for TX or RX sglist
6578259bf66SLukas Wunner  *
6588259bf66SLukas Wunner  * Prepare and submit a DMA descriptor for the TX or RX sglist of @tfr.
6598259bf66SLukas Wunner  * Return 0 on success or a negative error number.
6608259bf66SLukas Wunner  */
6615f336ea5SLukas Wunner static int bcm2835_spi_prepare_sg(struct spi_controller *ctlr,
6623ecd37edSMartin Sperl 				  struct spi_transfer *tfr,
6638259bf66SLukas Wunner 				  struct bcm2835_spi *bs,
664*ec679bdaSLukas Wunner 				  struct bcm2835_spidev *slv,
6653ecd37edSMartin Sperl 				  bool is_tx)
6663ecd37edSMartin Sperl {
6673ecd37edSMartin Sperl 	struct dma_chan *chan;
6683ecd37edSMartin Sperl 	struct scatterlist *sgl;
6693ecd37edSMartin Sperl 	unsigned int nents;
6703ecd37edSMartin Sperl 	enum dma_transfer_direction dir;
6713ecd37edSMartin Sperl 	unsigned long flags;
6723ecd37edSMartin Sperl 
6733ecd37edSMartin Sperl 	struct dma_async_tx_descriptor *desc;
6743ecd37edSMartin Sperl 	dma_cookie_t cookie;
6753ecd37edSMartin Sperl 
6763ecd37edSMartin Sperl 	if (is_tx) {
6773ecd37edSMartin Sperl 		dir   = DMA_MEM_TO_DEV;
6785f336ea5SLukas Wunner 		chan  = ctlr->dma_tx;
6793ecd37edSMartin Sperl 		nents = tfr->tx_sg.nents;
6803ecd37edSMartin Sperl 		sgl   = tfr->tx_sg.sgl;
6818259bf66SLukas Wunner 		flags = tfr->rx_buf ? 0 : DMA_PREP_INTERRUPT;
6823ecd37edSMartin Sperl 	} else {
6833ecd37edSMartin Sperl 		dir   = DMA_DEV_TO_MEM;
6845f336ea5SLukas Wunner 		chan  = ctlr->dma_rx;
6853ecd37edSMartin Sperl 		nents = tfr->rx_sg.nents;
6863ecd37edSMartin Sperl 		sgl   = tfr->rx_sg.sgl;
6873ecd37edSMartin Sperl 		flags = DMA_PREP_INTERRUPT;
6883ecd37edSMartin Sperl 	}
6893ecd37edSMartin Sperl 	/* prepare the channel */
6903ecd37edSMartin Sperl 	desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
6913ecd37edSMartin Sperl 	if (!desc)
6923ecd37edSMartin Sperl 		return -EINVAL;
6933ecd37edSMartin Sperl 
6948259bf66SLukas Wunner 	/*
6958259bf66SLukas Wunner 	 * Completion is signaled by the RX channel for bidirectional and
6968259bf66SLukas Wunner 	 * RX-only transfers; else by the TX channel for TX-only transfers.
6978259bf66SLukas Wunner 	 */
6983ecd37edSMartin Sperl 	if (!is_tx) {
6998259bf66SLukas Wunner 		desc->callback = bcm2835_spi_dma_rx_done;
7005f336ea5SLukas Wunner 		desc->callback_param = ctlr;
7018259bf66SLukas Wunner 	} else if (!tfr->rx_buf) {
7028259bf66SLukas Wunner 		desc->callback = bcm2835_spi_dma_tx_done;
7038259bf66SLukas Wunner 		desc->callback_param = ctlr;
704*ec679bdaSLukas Wunner 		bs->slv = slv;
7053ecd37edSMartin Sperl 	}
7063ecd37edSMartin Sperl 
7073ecd37edSMartin Sperl 	/* submit it to DMA-engine */
7083ecd37edSMartin Sperl 	cookie = dmaengine_submit(desc);
7093ecd37edSMartin Sperl 
7103ecd37edSMartin Sperl 	return dma_submit_error(cookie);
7113ecd37edSMartin Sperl }
7123ecd37edSMartin Sperl 
7138259bf66SLukas Wunner /**
7148259bf66SLukas Wunner  * bcm2835_spi_transfer_one_dma() - perform SPI transfer using DMA engine
7158259bf66SLukas Wunner  * @ctlr: SPI master controller
7168259bf66SLukas Wunner  * @tfr: SPI transfer
717*ec679bdaSLukas Wunner  * @slv: BCM2835 SPI slave
7188259bf66SLukas Wunner  * @cs: CS register
7198259bf66SLukas Wunner  *
7208259bf66SLukas Wunner  * For *bidirectional* transfers (both tx_buf and rx_buf are non-%NULL), set up
7218259bf66SLukas Wunner  * the TX and RX DMA channel to copy between memory and FIFO register.
7228259bf66SLukas Wunner  *
7238259bf66SLukas Wunner  * For *TX-only* transfers (rx_buf is %NULL), copying the RX FIFO's contents to
7248259bf66SLukas Wunner  * memory is pointless.  However not reading the RX FIFO isn't an option either
7258259bf66SLukas Wunner  * because transmission is halted once it's full.  As a workaround, cyclically
7268259bf66SLukas Wunner  * clear the RX FIFO by setting the CLEAR_RX bit in the CS register.
7278259bf66SLukas Wunner  *
7288259bf66SLukas Wunner  * The CS register value is precalculated in bcm2835_spi_setup().  Normally
7298259bf66SLukas Wunner  * this is called only once, on slave registration.  A DMA descriptor to write
7308259bf66SLukas Wunner  * this value is preallocated in bcm2835_dma_init().  All that's left to do
7318259bf66SLukas Wunner  * when performing a TX-only transfer is to submit this descriptor to the RX
7328259bf66SLukas Wunner  * DMA channel.  Latency is thereby minimized.  The descriptor does not
7338259bf66SLukas Wunner  * generate any interrupts while running.  It must be terminated once the
7348259bf66SLukas Wunner  * TX DMA channel is done.
7358259bf66SLukas Wunner  *
7368259bf66SLukas Wunner  * Clearing the RX FIFO is paced by the DREQ signal.  The signal is asserted
7378259bf66SLukas Wunner  * when the RX FIFO becomes half full, i.e. 32 bytes.  (Tuneable with the DC
7388259bf66SLukas Wunner  * register.)  Reading 32 bytes from the RX FIFO would normally require 8 bus
7398259bf66SLukas Wunner  * accesses, whereas clearing it requires only 1 bus access.  So an 8-fold
7408259bf66SLukas Wunner  * reduction in bus traffic and thus energy consumption is achieved.
7412b8279aeSLukas Wunner  *
7422b8279aeSLukas Wunner  * For *RX-only* transfers (tx_buf is %NULL), fill the TX FIFO by cyclically
7432b8279aeSLukas Wunner  * copying from the zero page.  The DMA descriptor to do this is preallocated
7442b8279aeSLukas Wunner  * in bcm2835_dma_init().  It must be terminated once the RX DMA channel is
7452b8279aeSLukas Wunner  * done and can then be reused.
7462b8279aeSLukas Wunner  *
7472b8279aeSLukas Wunner  * The BCM2835 DMA driver autodetects when a transaction copies from the zero
7482b8279aeSLukas Wunner  * page and utilizes the DMA controller's ability to synthesize zeroes instead
7492b8279aeSLukas Wunner  * of copying them from memory.  This reduces traffic on the memory bus.  The
7502b8279aeSLukas Wunner  * feature is not available on so-called "lite" channels, but normally TX DMA
7512b8279aeSLukas Wunner  * is backed by a full-featured channel.
7522b8279aeSLukas Wunner  *
7532b8279aeSLukas Wunner  * Zero-filling the TX FIFO is paced by the DREQ signal.  Unfortunately the
7542b8279aeSLukas Wunner  * BCM2835 SPI controller continues to assert DREQ even after the DLEN register
7552b8279aeSLukas Wunner  * has been counted down to zero (hardware erratum).  Thus, when the transfer
7562b8279aeSLukas Wunner  * has finished, the DMA engine zero-fills the TX FIFO until it is half full.
7572b8279aeSLukas Wunner  * (Tuneable with the DC register.)  So up to 9 gratuitous bus accesses are
7582b8279aeSLukas Wunner  * performed at the end of an RX-only transfer.
7598259bf66SLukas Wunner  */
7605f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_dma(struct spi_controller *ctlr,
7613ecd37edSMartin Sperl 					struct spi_transfer *tfr,
762*ec679bdaSLukas Wunner 					struct bcm2835_spidev *slv,
7633ecd37edSMartin Sperl 					u32 cs)
7643ecd37edSMartin Sperl {
7655f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
7668259bf66SLukas Wunner 	dma_cookie_t cookie;
7673ecd37edSMartin Sperl 	int ret;
7683ecd37edSMartin Sperl 
769154f7da5SMartin Sperl 	/* update usage statistics */
770154f7da5SMartin Sperl 	bs->count_transfer_dma++;
771154f7da5SMartin Sperl 
7723bd7f658SLukas Wunner 	/*
7733bd7f658SLukas Wunner 	 * Transfer first few bytes without DMA if length of first TX or RX
7743bd7f658SLukas Wunner 	 * sglist entry is not a multiple of 4 bytes (hardware limitation).
7753bd7f658SLukas Wunner 	 */
7765f336ea5SLukas Wunner 	bcm2835_spi_transfer_prologue(ctlr, tfr, bs, cs);
7773ecd37edSMartin Sperl 
7783ecd37edSMartin Sperl 	/* setup tx-DMA */
7792b8279aeSLukas Wunner 	if (bs->tx_buf) {
780*ec679bdaSLukas Wunner 		ret = bcm2835_spi_prepare_sg(ctlr, tfr, bs, slv, true);
7812b8279aeSLukas Wunner 	} else {
7822b8279aeSLukas Wunner 		cookie = dmaengine_submit(bs->fill_tx_desc);
7832b8279aeSLukas Wunner 		ret = dma_submit_error(cookie);
7842b8279aeSLukas Wunner 	}
7853ecd37edSMartin Sperl 	if (ret)
7863bd7f658SLukas Wunner 		goto err_reset_hw;
7873ecd37edSMartin Sperl 
7883ecd37edSMartin Sperl 	/* set the DMA length */
7893bd7f658SLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->tx_len);
7903ecd37edSMartin Sperl 
7913ecd37edSMartin Sperl 	/* start the HW */
7923ecd37edSMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS,
7933ecd37edSMartin Sperl 		   cs | BCM2835_SPI_CS_TA | BCM2835_SPI_CS_DMAEN);
7943ecd37edSMartin Sperl 
7958259bf66SLukas Wunner 	bs->tx_dma_active = true;
7968259bf66SLukas Wunner 	smp_wmb();
7978259bf66SLukas Wunner 
7988259bf66SLukas Wunner 	/* start TX early */
7998259bf66SLukas Wunner 	dma_async_issue_pending(ctlr->dma_tx);
8008259bf66SLukas Wunner 
8013ecd37edSMartin Sperl 	/* setup rx-DMA late - to run transfers while
8023ecd37edSMartin Sperl 	 * mapping of the rx buffers still takes place
8033ecd37edSMartin Sperl 	 * this saves 10us or more.
8043ecd37edSMartin Sperl 	 */
8058259bf66SLukas Wunner 	if (bs->rx_buf) {
806*ec679bdaSLukas Wunner 		ret = bcm2835_spi_prepare_sg(ctlr, tfr, bs, slv, false);
8078259bf66SLukas Wunner 	} else {
808*ec679bdaSLukas Wunner 		cookie = dmaengine_submit(slv->clear_rx_desc);
8098259bf66SLukas Wunner 		ret = dma_submit_error(cookie);
8108259bf66SLukas Wunner 	}
8113ecd37edSMartin Sperl 	if (ret) {
8123ecd37edSMartin Sperl 		/* need to reset on errors */
8135f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_tx);
8148259bf66SLukas Wunner 		bs->tx_dma_active = false;
8153bd7f658SLukas Wunner 		goto err_reset_hw;
8163ecd37edSMartin Sperl 	}
8173ecd37edSMartin Sperl 
8183ecd37edSMartin Sperl 	/* start rx dma late */
8195f336ea5SLukas Wunner 	dma_async_issue_pending(ctlr->dma_rx);
8208259bf66SLukas Wunner 	bs->rx_dma_active = true;
8218259bf66SLukas Wunner 	smp_mb();
8228259bf66SLukas Wunner 
8238259bf66SLukas Wunner 	/*
8248259bf66SLukas Wunner 	 * In case of a very short TX-only transfer, bcm2835_spi_dma_tx_done()
8258259bf66SLukas Wunner 	 * may run before RX DMA is issued.  Terminate RX DMA if so.
8268259bf66SLukas Wunner 	 */
8278259bf66SLukas Wunner 	if (!bs->rx_buf && !bs->tx_dma_active &&
8288259bf66SLukas Wunner 	    cmpxchg(&bs->rx_dma_active, true, false)) {
8298259bf66SLukas Wunner 		dmaengine_terminate_async(ctlr->dma_rx);
830ac4648b5SRobin Murphy 		bcm2835_spi_reset_hw(bs);
8318259bf66SLukas Wunner 	}
8323ecd37edSMartin Sperl 
8333ecd37edSMartin Sperl 	/* wait for wakeup in framework */
8343ecd37edSMartin Sperl 	return 1;
8353bd7f658SLukas Wunner 
8363bd7f658SLukas Wunner err_reset_hw:
837ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
8383bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
8393bd7f658SLukas Wunner 	return ret;
8403ecd37edSMartin Sperl }
8413ecd37edSMartin Sperl 
8425f336ea5SLukas Wunner static bool bcm2835_spi_can_dma(struct spi_controller *ctlr,
8433ecd37edSMartin Sperl 				struct spi_device *spi,
8443ecd37edSMartin Sperl 				struct spi_transfer *tfr)
8453ecd37edSMartin Sperl {
8463ecd37edSMartin Sperl 	/* we start DMA efforts only on bigger transfers */
8473ecd37edSMartin Sperl 	if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
8483ecd37edSMartin Sperl 		return false;
8493ecd37edSMartin Sperl 
8503ecd37edSMartin Sperl 	/* return OK */
8513ecd37edSMartin Sperl 	return true;
8523ecd37edSMartin Sperl }
8533ecd37edSMartin Sperl 
8548259bf66SLukas Wunner static void bcm2835_dma_release(struct spi_controller *ctlr,
8558259bf66SLukas Wunner 				struct bcm2835_spi *bs)
8563ecd37edSMartin Sperl {
8575f336ea5SLukas Wunner 	if (ctlr->dma_tx) {
8585f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_tx);
8592b8279aeSLukas Wunner 
8602b8279aeSLukas Wunner 		if (bs->fill_tx_desc)
8612b8279aeSLukas Wunner 			dmaengine_desc_free(bs->fill_tx_desc);
8622b8279aeSLukas Wunner 
8632b8279aeSLukas Wunner 		if (bs->fill_tx_addr)
8642b8279aeSLukas Wunner 			dma_unmap_page_attrs(ctlr->dma_tx->device->dev,
8652b8279aeSLukas Wunner 					     bs->fill_tx_addr, sizeof(u32),
8662b8279aeSLukas Wunner 					     DMA_TO_DEVICE,
8672b8279aeSLukas Wunner 					     DMA_ATTR_SKIP_CPU_SYNC);
8682b8279aeSLukas Wunner 
8695f336ea5SLukas Wunner 		dma_release_channel(ctlr->dma_tx);
8705f336ea5SLukas Wunner 		ctlr->dma_tx = NULL;
8713ecd37edSMartin Sperl 	}
8728259bf66SLukas Wunner 
8735f336ea5SLukas Wunner 	if (ctlr->dma_rx) {
8745f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_rx);
8755f336ea5SLukas Wunner 		dma_release_channel(ctlr->dma_rx);
8765f336ea5SLukas Wunner 		ctlr->dma_rx = NULL;
8773ecd37edSMartin Sperl 	}
8783ecd37edSMartin Sperl }
8793ecd37edSMartin Sperl 
8806133fed0SPeter Ujfalusi static int bcm2835_dma_init(struct spi_controller *ctlr, struct device *dev,
8818259bf66SLukas Wunner 			    struct bcm2835_spi *bs)
8823ecd37edSMartin Sperl {
8833ecd37edSMartin Sperl 	struct dma_slave_config slave_config;
8843ecd37edSMartin Sperl 	const __be32 *addr;
8853ecd37edSMartin Sperl 	dma_addr_t dma_reg_base;
886*ec679bdaSLukas Wunner 	int ret;
8873ecd37edSMartin Sperl 
8883ecd37edSMartin Sperl 	/* base address in dma-space */
8895f336ea5SLukas Wunner 	addr = of_get_address(ctlr->dev.of_node, 0, NULL, NULL);
8903ecd37edSMartin Sperl 	if (!addr) {
8913ecd37edSMartin Sperl 		dev_err(dev, "could not get DMA-register address - not using dma mode\n");
8926133fed0SPeter Ujfalusi 		/* Fall back to interrupt mode */
8936133fed0SPeter Ujfalusi 		return 0;
8943ecd37edSMartin Sperl 	}
8953ecd37edSMartin Sperl 	dma_reg_base = be32_to_cpup(addr);
8963ecd37edSMartin Sperl 
8973ecd37edSMartin Sperl 	/* get tx/rx dma */
8986133fed0SPeter Ujfalusi 	ctlr->dma_tx = dma_request_chan(dev, "tx");
8996133fed0SPeter Ujfalusi 	if (IS_ERR(ctlr->dma_tx)) {
9003ecd37edSMartin Sperl 		dev_err(dev, "no tx-dma configuration found - not using dma mode\n");
9016133fed0SPeter Ujfalusi 		ret = PTR_ERR(ctlr->dma_tx);
9026133fed0SPeter Ujfalusi 		ctlr->dma_tx = NULL;
9033ecd37edSMartin Sperl 		goto err;
9043ecd37edSMartin Sperl 	}
9056133fed0SPeter Ujfalusi 	ctlr->dma_rx = dma_request_chan(dev, "rx");
9066133fed0SPeter Ujfalusi 	if (IS_ERR(ctlr->dma_rx)) {
9073ecd37edSMartin Sperl 		dev_err(dev, "no rx-dma configuration found - not using dma mode\n");
9086133fed0SPeter Ujfalusi 		ret = PTR_ERR(ctlr->dma_rx);
9096133fed0SPeter Ujfalusi 		ctlr->dma_rx = NULL;
9103ecd37edSMartin Sperl 		goto err_release;
9113ecd37edSMartin Sperl 	}
9123ecd37edSMartin Sperl 
9132b8279aeSLukas Wunner 	/*
9142b8279aeSLukas Wunner 	 * The TX DMA channel either copies a transfer's TX buffer to the FIFO
9152b8279aeSLukas Wunner 	 * or, in case of an RX-only transfer, cyclically copies from the zero
9162b8279aeSLukas Wunner 	 * page to the FIFO using a preallocated, reusable descriptor.
9172b8279aeSLukas Wunner 	 */
9183ecd37edSMartin Sperl 	slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
9193ecd37edSMartin Sperl 	slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9203ecd37edSMartin Sperl 
9215f336ea5SLukas Wunner 	ret = dmaengine_slave_config(ctlr->dma_tx, &slave_config);
9223ecd37edSMartin Sperl 	if (ret)
9233ecd37edSMartin Sperl 		goto err_config;
9243ecd37edSMartin Sperl 
9252b8279aeSLukas Wunner 	bs->fill_tx_addr = dma_map_page_attrs(ctlr->dma_tx->device->dev,
9262b8279aeSLukas Wunner 					      ZERO_PAGE(0), 0, sizeof(u32),
9272b8279aeSLukas Wunner 					      DMA_TO_DEVICE,
9282b8279aeSLukas Wunner 					      DMA_ATTR_SKIP_CPU_SYNC);
9292b8279aeSLukas Wunner 	if (dma_mapping_error(ctlr->dma_tx->device->dev, bs->fill_tx_addr)) {
9302b8279aeSLukas Wunner 		dev_err(dev, "cannot map zero page - not using DMA mode\n");
9312b8279aeSLukas Wunner 		bs->fill_tx_addr = 0;
932dd4441abSWei Yongjun 		ret = -ENOMEM;
9332b8279aeSLukas Wunner 		goto err_release;
9342b8279aeSLukas Wunner 	}
9352b8279aeSLukas Wunner 
9362b8279aeSLukas Wunner 	bs->fill_tx_desc = dmaengine_prep_dma_cyclic(ctlr->dma_tx,
9372b8279aeSLukas Wunner 						     bs->fill_tx_addr,
9382b8279aeSLukas Wunner 						     sizeof(u32), 0,
9392b8279aeSLukas Wunner 						     DMA_MEM_TO_DEV, 0);
9402b8279aeSLukas Wunner 	if (!bs->fill_tx_desc) {
9412b8279aeSLukas Wunner 		dev_err(dev, "cannot prepare fill_tx_desc - not using DMA mode\n");
942dd4441abSWei Yongjun 		ret = -ENOMEM;
9432b8279aeSLukas Wunner 		goto err_release;
9442b8279aeSLukas Wunner 	}
9452b8279aeSLukas Wunner 
9462b8279aeSLukas Wunner 	ret = dmaengine_desc_set_reuse(bs->fill_tx_desc);
9472b8279aeSLukas Wunner 	if (ret) {
9482b8279aeSLukas Wunner 		dev_err(dev, "cannot reuse fill_tx_desc - not using DMA mode\n");
9492b8279aeSLukas Wunner 		goto err_release;
9502b8279aeSLukas Wunner 	}
9512b8279aeSLukas Wunner 
9528259bf66SLukas Wunner 	/*
9538259bf66SLukas Wunner 	 * The RX DMA channel is used bidirectionally:  It either reads the
9548259bf66SLukas Wunner 	 * RX FIFO or, in case of a TX-only transfer, cyclically writes a
9558259bf66SLukas Wunner 	 * precalculated value to the CS register to clear the RX FIFO.
9568259bf66SLukas Wunner 	 */
9573ecd37edSMartin Sperl 	slave_config.src_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
9583ecd37edSMartin Sperl 	slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9598259bf66SLukas Wunner 	slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_CS);
9608259bf66SLukas Wunner 	slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9613ecd37edSMartin Sperl 
9625f336ea5SLukas Wunner 	ret = dmaengine_slave_config(ctlr->dma_rx, &slave_config);
9633ecd37edSMartin Sperl 	if (ret)
9643ecd37edSMartin Sperl 		goto err_config;
9653ecd37edSMartin Sperl 
9663ecd37edSMartin Sperl 	/* all went well, so set can_dma */
9675f336ea5SLukas Wunner 	ctlr->can_dma = bcm2835_spi_can_dma;
9683ecd37edSMartin Sperl 
9696133fed0SPeter Ujfalusi 	return 0;
9703ecd37edSMartin Sperl 
9713ecd37edSMartin Sperl err_config:
9723ecd37edSMartin Sperl 	dev_err(dev, "issue configuring dma: %d - not using DMA mode\n",
9733ecd37edSMartin Sperl 		ret);
9743ecd37edSMartin Sperl err_release:
9758259bf66SLukas Wunner 	bcm2835_dma_release(ctlr, bs);
9763ecd37edSMartin Sperl err:
9776133fed0SPeter Ujfalusi 	/*
9786133fed0SPeter Ujfalusi 	 * Only report error for deferred probing, otherwise fall back to
9796133fed0SPeter Ujfalusi 	 * interrupt mode
9806133fed0SPeter Ujfalusi 	 */
9816133fed0SPeter Ujfalusi 	if (ret != -EPROBE_DEFER)
9826133fed0SPeter Ujfalusi 		ret = 0;
9836133fed0SPeter Ujfalusi 
9846133fed0SPeter Ujfalusi 	return ret;
9853ecd37edSMartin Sperl }
9863ecd37edSMartin Sperl 
9875f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_poll(struct spi_controller *ctlr,
988a750b124SMartin Sperl 					 struct spi_device *spi,
989a750b124SMartin Sperl 					 struct spi_transfer *tfr,
9909ac3f90dSMartin Sperl 					 u32 cs)
991a750b124SMartin Sperl {
9925f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
993a750b124SMartin Sperl 	unsigned long timeout;
994a750b124SMartin Sperl 
995154f7da5SMartin Sperl 	/* update usage statistics */
996154f7da5SMartin Sperl 	bs->count_transfer_polling++;
997154f7da5SMartin Sperl 
998a750b124SMartin Sperl 	/* enable HW block without interrupts */
999a750b124SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
1000a750b124SMartin Sperl 
1001a750b124SMartin Sperl 	/* fill in the fifo before timeout calculations
1002a750b124SMartin Sperl 	 * if we are interrupted here, then the data is
1003a750b124SMartin Sperl 	 * getting transferred by the HW while we are interrupted
1004a750b124SMartin Sperl 	 */
10052e0733bcSLukas Wunner 	bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
1006a750b124SMartin Sperl 
1007ff245d90SMartin Sperl 	/* set the timeout to at least 2 jiffies */
1008ff245d90SMartin Sperl 	timeout = jiffies + 2 + HZ * polling_limit_us / 1000000;
1009a750b124SMartin Sperl 
1010a750b124SMartin Sperl 	/* loop until finished the transfer */
1011a750b124SMartin Sperl 	while (bs->rx_len) {
1012a750b124SMartin Sperl 		/* fill in tx fifo with remaining data */
1013a750b124SMartin Sperl 		bcm2835_wr_fifo(bs);
1014a750b124SMartin Sperl 
1015a750b124SMartin Sperl 		/* read from fifo as much as possible */
1016a750b124SMartin Sperl 		bcm2835_rd_fifo(bs);
1017a750b124SMartin Sperl 
1018a750b124SMartin Sperl 		/* if there is still data pending to read
1019a750b124SMartin Sperl 		 * then check the timeout
1020a750b124SMartin Sperl 		 */
1021a750b124SMartin Sperl 		if (bs->rx_len && time_after(jiffies, timeout)) {
1022a750b124SMartin Sperl 			dev_dbg_ratelimited(&spi->dev,
1023a750b124SMartin Sperl 					    "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
1024a750b124SMartin Sperl 					    jiffies - timeout,
1025a750b124SMartin Sperl 					    bs->tx_len, bs->rx_len);
1026a750b124SMartin Sperl 			/* fall back to interrupt mode */
1027154f7da5SMartin Sperl 
1028154f7da5SMartin Sperl 			/* update usage statistics */
1029154f7da5SMartin Sperl 			bs->count_transfer_irq_after_polling++;
1030154f7da5SMartin Sperl 
10315f336ea5SLukas Wunner 			return bcm2835_spi_transfer_one_irq(ctlr, spi,
10322e0733bcSLukas Wunner 							    tfr, cs, false);
1033a750b124SMartin Sperl 		}
1034a750b124SMartin Sperl 	}
1035a750b124SMartin Sperl 
1036a750b124SMartin Sperl 	/* Transfer complete - reset SPI HW */
1037ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
1038a750b124SMartin Sperl 	/* and return without waiting for completion */
1039a750b124SMartin Sperl 	return 0;
1040a750b124SMartin Sperl }
1041a750b124SMartin Sperl 
10425f336ea5SLukas Wunner static int bcm2835_spi_transfer_one(struct spi_controller *ctlr,
1043704f32d4SMartin Sperl 				    struct spi_device *spi,
1044704f32d4SMartin Sperl 				    struct spi_transfer *tfr)
1045704f32d4SMartin Sperl {
10465f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1047*ec679bdaSLukas Wunner 	struct bcm2835_spidev *slv = spi_get_ctldata(spi);
10489df2003dSMartin Sperl 	unsigned long spi_hz, clk_hz, cdiv;
1049ff245d90SMartin Sperl 	unsigned long hz_per_byte, byte_limit;
1050*ec679bdaSLukas Wunner 	u32 cs = slv->prepare_cs;
1051704f32d4SMartin Sperl 
1052704f32d4SMartin Sperl 	/* set clock */
1053704f32d4SMartin Sperl 	spi_hz = tfr->speed_hz;
1054704f32d4SMartin Sperl 	clk_hz = clk_get_rate(bs->clk);
1055704f32d4SMartin Sperl 
1056704f32d4SMartin Sperl 	if (spi_hz >= clk_hz / 2) {
1057704f32d4SMartin Sperl 		cdiv = 2; /* clk_hz/2 is the fastest we can go */
1058704f32d4SMartin Sperl 	} else if (spi_hz) {
1059704f32d4SMartin Sperl 		/* CDIV must be a multiple of two */
1060704f32d4SMartin Sperl 		cdiv = DIV_ROUND_UP(clk_hz, spi_hz);
1061704f32d4SMartin Sperl 		cdiv += (cdiv % 2);
1062704f32d4SMartin Sperl 
1063704f32d4SMartin Sperl 		if (cdiv >= 65536)
1064704f32d4SMartin Sperl 			cdiv = 0; /* 0 is the slowest we can go */
1065704f32d4SMartin Sperl 	} else {
1066704f32d4SMartin Sperl 		cdiv = 0; /* 0 is the slowest we can go */
1067704f32d4SMartin Sperl 	}
10689df2003dSMartin Sperl 	tfr->effective_speed_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536);
1069704f32d4SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
1070704f32d4SMartin Sperl 
1071acace73dSMartin Sperl 	/* handle all the 3-wire mode */
10728259bf66SLukas Wunner 	if (spi->mode & SPI_3WIRE && tfr->rx_buf)
1073704f32d4SMartin Sperl 		cs |= BCM2835_SPI_CS_REN;
1074704f32d4SMartin Sperl 
1075704f32d4SMartin Sperl 	/* set transmit buffers and length */
1076704f32d4SMartin Sperl 	bs->tx_buf = tfr->tx_buf;
1077704f32d4SMartin Sperl 	bs->rx_buf = tfr->rx_buf;
1078704f32d4SMartin Sperl 	bs->tx_len = tfr->len;
1079704f32d4SMartin Sperl 	bs->rx_len = tfr->len;
1080704f32d4SMartin Sperl 
10817f1922ebSMartin Sperl 	/* Calculate the estimated time in us the transfer runs.  Note that
10827f1922ebSMartin Sperl 	 * there is 1 idle clocks cycles after each byte getting transferred
10837f1922ebSMartin Sperl 	 * so we have 9 cycles/byte.  This is used to find the number of Hz
10847f1922ebSMartin Sperl 	 * per byte per polling limit.  E.g., we can transfer 1 byte in 30 us
10857f1922ebSMartin Sperl 	 * per 300,000 Hz of bus clock.
10867f1922ebSMartin Sperl 	 */
1087ff245d90SMartin Sperl 	hz_per_byte = polling_limit_us ? (9 * 1000000) / polling_limit_us : 0;
10889df2003dSMartin Sperl 	byte_limit = hz_per_byte ? tfr->effective_speed_hz / hz_per_byte : 1;
1089ff245d90SMartin Sperl 
10907f1922ebSMartin Sperl 	/* run in polling mode for short transfers */
1091ff245d90SMartin Sperl 	if (tfr->len < byte_limit)
10925f336ea5SLukas Wunner 		return bcm2835_spi_transfer_one_poll(ctlr, spi, tfr, cs);
1093704f32d4SMartin Sperl 
1094c41d62b0SMartin Sperl 	/* run in dma mode if conditions are right
1095c41d62b0SMartin Sperl 	 * Note that unlike poll or interrupt mode DMA mode does not have
1096c41d62b0SMartin Sperl 	 * this 1 idle clock cycle pattern but runs the spi clock without gaps
1097c41d62b0SMartin Sperl 	 */
10985f336ea5SLukas Wunner 	if (ctlr->can_dma && bcm2835_spi_can_dma(ctlr, spi, tfr))
1099*ec679bdaSLukas Wunner 		return bcm2835_spi_transfer_one_dma(ctlr, tfr, slv, cs);
11003ecd37edSMartin Sperl 
11013ecd37edSMartin Sperl 	/* run in interrupt-mode */
11025f336ea5SLukas Wunner 	return bcm2835_spi_transfer_one_irq(ctlr, spi, tfr, cs, true);
1103704f32d4SMartin Sperl }
1104704f32d4SMartin Sperl 
11055f336ea5SLukas Wunner static int bcm2835_spi_prepare_message(struct spi_controller *ctlr,
1106acace73dSMartin Sperl 				       struct spi_message *msg)
1107acace73dSMartin Sperl {
1108acace73dSMartin Sperl 	struct spi_device *spi = msg->spi;
11095f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1110*ec679bdaSLukas Wunner 	struct bcm2835_spidev *slv = spi_get_ctldata(spi);
11118b7bd10eSMeghana Madhyastha 	int ret;
11128b7bd10eSMeghana Madhyastha 
11135f336ea5SLukas Wunner 	if (ctlr->can_dma) {
11148b7bd10eSMeghana Madhyastha 		/*
11153393f7d9SNicolas Saenz Julienne 		 * DMA transfers are limited to 16 bit (0 to 65535 bytes) by
11163393f7d9SNicolas Saenz Julienne 		 * the SPI HW due to DLEN. Split up transfers (32-bit FIFO
11173393f7d9SNicolas Saenz Julienne 		 * aligned) if the limit is exceeded.
11188b7bd10eSMeghana Madhyastha 		 */
11195f336ea5SLukas Wunner 		ret = spi_split_transfers_maxsize(ctlr, msg, 65532,
11208b7bd10eSMeghana Madhyastha 						  GFP_KERNEL | GFP_DMA);
11218b7bd10eSMeghana Madhyastha 		if (ret)
11228b7bd10eSMeghana Madhyastha 			return ret;
11233393f7d9SNicolas Saenz Julienne 	}
1124acace73dSMartin Sperl 
1125571e31faSLukas Wunner 	/*
1126571e31faSLukas Wunner 	 * Set up clock polarity before spi_transfer_one_message() asserts
1127571e31faSLukas Wunner 	 * chip select to avoid a gratuitous clock signal edge.
1128571e31faSLukas Wunner 	 */
1129*ec679bdaSLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_CS, slv->prepare_cs);
1130acace73dSMartin Sperl 
1131acace73dSMartin Sperl 	return 0;
1132acace73dSMartin Sperl }
1133acace73dSMartin Sperl 
11345f336ea5SLukas Wunner static void bcm2835_spi_handle_err(struct spi_controller *ctlr,
1135e34ff011SMartin Sperl 				   struct spi_message *msg)
1136f8043872SChris Boot {
11375f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
11383ecd37edSMartin Sperl 
11393ecd37edSMartin Sperl 	/* if an error occurred and we have an active dma, then terminate */
11405f336ea5SLukas Wunner 	dmaengine_terminate_sync(ctlr->dma_tx);
11418259bf66SLukas Wunner 	bs->tx_dma_active = false;
11425f336ea5SLukas Wunner 	dmaengine_terminate_sync(ctlr->dma_rx);
11438259bf66SLukas Wunner 	bs->rx_dma_active = false;
11443bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
11451513ceeeSLukas Wunner 
11463ecd37edSMartin Sperl 	/* and reset */
1147ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
1148f8043872SChris Boot }
1149f8043872SChris Boot 
1150a30a555dSMartin Sperl static int chip_match_name(struct gpio_chip *chip, void *data)
1151a30a555dSMartin Sperl {
1152a30a555dSMartin Sperl 	return !strcmp(chip->label, data);
1153a30a555dSMartin Sperl }
1154a30a555dSMartin Sperl 
1155*ec679bdaSLukas Wunner static void bcm2835_spi_cleanup(struct spi_device *spi)
1156*ec679bdaSLukas Wunner {
1157*ec679bdaSLukas Wunner 	struct bcm2835_spidev *slv = spi_get_ctldata(spi);
1158*ec679bdaSLukas Wunner 	struct spi_controller *ctlr = spi->controller;
1159*ec679bdaSLukas Wunner 
1160*ec679bdaSLukas Wunner 	if (slv->clear_rx_desc)
1161*ec679bdaSLukas Wunner 		dmaengine_desc_free(slv->clear_rx_desc);
1162*ec679bdaSLukas Wunner 
1163*ec679bdaSLukas Wunner 	if (slv->clear_rx_addr)
1164*ec679bdaSLukas Wunner 		dma_unmap_single(ctlr->dma_rx->device->dev,
1165*ec679bdaSLukas Wunner 				 slv->clear_rx_addr,
1166*ec679bdaSLukas Wunner 				 sizeof(u32),
1167*ec679bdaSLukas Wunner 				 DMA_TO_DEVICE);
1168*ec679bdaSLukas Wunner 
1169*ec679bdaSLukas Wunner 	kfree(slv);
1170*ec679bdaSLukas Wunner }
1171*ec679bdaSLukas Wunner 
1172*ec679bdaSLukas Wunner static int bcm2835_spi_setup_dma(struct spi_controller *ctlr,
1173*ec679bdaSLukas Wunner 				 struct spi_device *spi,
1174*ec679bdaSLukas Wunner 				 struct bcm2835_spi *bs,
1175*ec679bdaSLukas Wunner 				 struct bcm2835_spidev *slv)
1176*ec679bdaSLukas Wunner {
1177*ec679bdaSLukas Wunner 	int ret;
1178*ec679bdaSLukas Wunner 
1179*ec679bdaSLukas Wunner 	if (!ctlr->dma_rx)
1180*ec679bdaSLukas Wunner 		return 0;
1181*ec679bdaSLukas Wunner 
1182*ec679bdaSLukas Wunner 	slv->clear_rx_addr = dma_map_single(ctlr->dma_rx->device->dev,
1183*ec679bdaSLukas Wunner 					    &slv->clear_rx_cs,
1184*ec679bdaSLukas Wunner 					    sizeof(u32),
1185*ec679bdaSLukas Wunner 					    DMA_TO_DEVICE);
1186*ec679bdaSLukas Wunner 	if (dma_mapping_error(ctlr->dma_rx->device->dev, slv->clear_rx_addr)) {
1187*ec679bdaSLukas Wunner 		dev_err(&spi->dev, "cannot map clear_rx_cs\n");
1188*ec679bdaSLukas Wunner 		slv->clear_rx_addr = 0;
1189*ec679bdaSLukas Wunner 		return -ENOMEM;
1190*ec679bdaSLukas Wunner 	}
1191*ec679bdaSLukas Wunner 
1192*ec679bdaSLukas Wunner 	slv->clear_rx_desc = dmaengine_prep_dma_cyclic(ctlr->dma_rx,
1193*ec679bdaSLukas Wunner 						       slv->clear_rx_addr,
1194*ec679bdaSLukas Wunner 						       sizeof(u32), 0,
1195*ec679bdaSLukas Wunner 						       DMA_MEM_TO_DEV, 0);
1196*ec679bdaSLukas Wunner 	if (!slv->clear_rx_desc) {
1197*ec679bdaSLukas Wunner 		dev_err(&spi->dev, "cannot prepare clear_rx_desc\n");
1198*ec679bdaSLukas Wunner 		return -ENOMEM;
1199*ec679bdaSLukas Wunner 	}
1200*ec679bdaSLukas Wunner 
1201*ec679bdaSLukas Wunner 	ret = dmaengine_desc_set_reuse(slv->clear_rx_desc);
1202*ec679bdaSLukas Wunner 	if (ret) {
1203*ec679bdaSLukas Wunner 		dev_err(&spi->dev, "cannot reuse clear_rx_desc\n");
1204*ec679bdaSLukas Wunner 		return ret;
1205*ec679bdaSLukas Wunner 	}
1206*ec679bdaSLukas Wunner 
1207*ec679bdaSLukas Wunner 	return 0;
1208*ec679bdaSLukas Wunner }
1209*ec679bdaSLukas Wunner 
1210e34ff011SMartin Sperl static int bcm2835_spi_setup(struct spi_device *spi)
1211e34ff011SMartin Sperl {
12128259bf66SLukas Wunner 	struct spi_controller *ctlr = spi->controller;
12138259bf66SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1214*ec679bdaSLukas Wunner 	struct bcm2835_spidev *slv = spi_get_ctldata(spi);
1215a30a555dSMartin Sperl 	struct gpio_chip *chip;
1216*ec679bdaSLukas Wunner 	int ret;
1217571e31faSLukas Wunner 	u32 cs;
1218571e31faSLukas Wunner 
1219*ec679bdaSLukas Wunner 	if (!slv) {
1220*ec679bdaSLukas Wunner 		slv = kzalloc(ALIGN(sizeof(*slv), dma_get_cache_alignment()),
1221*ec679bdaSLukas Wunner 			      GFP_KERNEL);
1222*ec679bdaSLukas Wunner 		if (!slv)
1223*ec679bdaSLukas Wunner 			return -ENOMEM;
1224*ec679bdaSLukas Wunner 
1225*ec679bdaSLukas Wunner 		spi_set_ctldata(spi, slv);
1226*ec679bdaSLukas Wunner 
1227*ec679bdaSLukas Wunner 		ret = bcm2835_spi_setup_dma(ctlr, spi, bs, slv);
1228*ec679bdaSLukas Wunner 		if (ret)
1229*ec679bdaSLukas Wunner 			goto err_cleanup;
123013817d46SLukas Wunner 	}
123113817d46SLukas Wunner 
1232571e31faSLukas Wunner 	/*
1233571e31faSLukas Wunner 	 * Precalculate SPI slave's CS register value for ->prepare_message():
1234571e31faSLukas Wunner 	 * The driver always uses software-controlled GPIO chip select, hence
1235571e31faSLukas Wunner 	 * set the hardware-controlled native chip select to an invalid value
1236571e31faSLukas Wunner 	 * to prevent it from interfering.
1237571e31faSLukas Wunner 	 */
1238571e31faSLukas Wunner 	cs = BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
1239571e31faSLukas Wunner 	if (spi->mode & SPI_CPOL)
1240571e31faSLukas Wunner 		cs |= BCM2835_SPI_CS_CPOL;
1241571e31faSLukas Wunner 	if (spi->mode & SPI_CPHA)
1242571e31faSLukas Wunner 		cs |= BCM2835_SPI_CS_CPHA;
1243*ec679bdaSLukas Wunner 	slv->prepare_cs = cs;
12443bd158c5SLinus Walleij 
1245e34ff011SMartin Sperl 	/*
12468259bf66SLukas Wunner 	 * Precalculate SPI slave's CS register value to clear RX FIFO
12478259bf66SLukas Wunner 	 * in case of a TX-only DMA transfer.
12488259bf66SLukas Wunner 	 */
12498259bf66SLukas Wunner 	if (ctlr->dma_rx) {
1250*ec679bdaSLukas Wunner 		slv->clear_rx_cs = cs | BCM2835_SPI_CS_TA |
12518259bf66SLukas Wunner 					BCM2835_SPI_CS_DMAEN |
12528259bf66SLukas Wunner 					BCM2835_SPI_CS_CLEAR_RX;
12538259bf66SLukas Wunner 		dma_sync_single_for_device(ctlr->dma_rx->device->dev,
1254*ec679bdaSLukas Wunner 					   slv->clear_rx_addr,
1255*ec679bdaSLukas Wunner 					   sizeof(u32),
12568259bf66SLukas Wunner 					   DMA_TO_DEVICE);
12578259bf66SLukas Wunner 	}
12588259bf66SLukas Wunner 
1259e34ff011SMartin Sperl 	/*
1260e34ff011SMartin Sperl 	 * sanity checking the native-chipselects
1261e34ff011SMartin Sperl 	 */
1262e34ff011SMartin Sperl 	if (spi->mode & SPI_NO_CS)
1263f8043872SChris Boot 		return 0;
12643bd158c5SLinus Walleij 	/*
12653bd158c5SLinus Walleij 	 * The SPI core has successfully requested the CS GPIO line from the
12663bd158c5SLinus Walleij 	 * device tree, so we are done.
12673bd158c5SLinus Walleij 	 */
12683bd158c5SLinus Walleij 	if (spi->cs_gpiod)
1269e34ff011SMartin Sperl 		return 0;
1270a30a555dSMartin Sperl 	if (spi->chip_select > 1) {
1271a30a555dSMartin Sperl 		/* error in the case of native CS requested with CS > 1
1272a30a555dSMartin Sperl 		 * officially there is a CS2, but it is not documented
1273a30a555dSMartin Sperl 		 * which GPIO is connected with that...
1274a30a555dSMartin Sperl 		 */
1275a30a555dSMartin Sperl 		dev_err(&spi->dev,
1276a30a555dSMartin Sperl 			"setup: only two native chip-selects are supported\n");
1277*ec679bdaSLukas Wunner 		ret = -EINVAL;
1278*ec679bdaSLukas Wunner 		goto err_cleanup;
1279a30a555dSMartin Sperl 	}
12803bd158c5SLinus Walleij 
12813bd158c5SLinus Walleij 	/*
12823bd158c5SLinus Walleij 	 * Translate native CS to GPIO
12833bd158c5SLinus Walleij 	 *
12843bd158c5SLinus Walleij 	 * FIXME: poking around in the gpiolib internals like this is
12853bd158c5SLinus Walleij 	 * not very good practice. Find a way to locate the real problem
12863bd158c5SLinus Walleij 	 * and fix it. Why is the GPIO descriptor in spi->cs_gpiod
12873bd158c5SLinus Walleij 	 * sometimes not assigned correctly? Erroneous device trees?
12883bd158c5SLinus Walleij 	 */
1289a30a555dSMartin Sperl 
1290a30a555dSMartin Sperl 	/* get the gpio chip for the base */
1291a30a555dSMartin Sperl 	chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
1292a30a555dSMartin Sperl 	if (!chip)
1293e34ff011SMartin Sperl 		return 0;
1294e34ff011SMartin Sperl 
12953bd158c5SLinus Walleij 	spi->cs_gpiod = gpiochip_request_own_desc(chip, 8 - spi->chip_select,
12963bd158c5SLinus Walleij 						  DRV_NAME,
1297bc7f2cd7SMartin Hundebøll 						  GPIO_LOOKUP_FLAGS_DEFAULT,
12983bd158c5SLinus Walleij 						  GPIOD_OUT_LOW);
1299*ec679bdaSLukas Wunner 	if (IS_ERR(spi->cs_gpiod)) {
1300*ec679bdaSLukas Wunner 		ret = PTR_ERR(spi->cs_gpiod);
1301*ec679bdaSLukas Wunner 		goto err_cleanup;
1302*ec679bdaSLukas Wunner 	}
1303a30a555dSMartin Sperl 
1304a30a555dSMartin Sperl 	/* and set up the "mode" and level */
13053bd158c5SLinus Walleij 	dev_info(&spi->dev, "setting up native-CS%i to use GPIO\n",
13063bd158c5SLinus Walleij 		 spi->chip_select);
1307a30a555dSMartin Sperl 
1308a30a555dSMartin Sperl 	return 0;
1309*ec679bdaSLukas Wunner 
1310*ec679bdaSLukas Wunner err_cleanup:
1311*ec679bdaSLukas Wunner 	bcm2835_spi_cleanup(spi);
1312*ec679bdaSLukas Wunner 	return ret;
1313f8043872SChris Boot }
1314f8043872SChris Boot 
1315f8043872SChris Boot static int bcm2835_spi_probe(struct platform_device *pdev)
1316f8043872SChris Boot {
13175f336ea5SLukas Wunner 	struct spi_controller *ctlr;
1318f8043872SChris Boot 	struct bcm2835_spi *bs;
1319f8043872SChris Boot 	int err;
1320f8043872SChris Boot 
1321*ec679bdaSLukas Wunner 	ctlr = devm_spi_alloc_master(&pdev->dev, sizeof(*bs));
13225f336ea5SLukas Wunner 	if (!ctlr)
1323f8043872SChris Boot 		return -ENOMEM;
1324f8043872SChris Boot 
13255f336ea5SLukas Wunner 	platform_set_drvdata(pdev, ctlr);
1326f8043872SChris Boot 
13273bd158c5SLinus Walleij 	ctlr->use_gpio_descriptors = true;
13285f336ea5SLukas Wunner 	ctlr->mode_bits = BCM2835_SPI_MODE_BITS;
13295f336ea5SLukas Wunner 	ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
133013817d46SLukas Wunner 	ctlr->num_chipselect = 3;
13315f336ea5SLukas Wunner 	ctlr->setup = bcm2835_spi_setup;
1332*ec679bdaSLukas Wunner 	ctlr->cleanup = bcm2835_spi_cleanup;
13335f336ea5SLukas Wunner 	ctlr->transfer_one = bcm2835_spi_transfer_one;
13345f336ea5SLukas Wunner 	ctlr->handle_err = bcm2835_spi_handle_err;
13355f336ea5SLukas Wunner 	ctlr->prepare_message = bcm2835_spi_prepare_message;
13365f336ea5SLukas Wunner 	ctlr->dev.of_node = pdev->dev.of_node;
1337f8043872SChris Boot 
13385f336ea5SLukas Wunner 	bs = spi_controller_get_devdata(ctlr);
1339afe7e363SRobin Murphy 	bs->ctlr = ctlr;
1340f8043872SChris Boot 
13416ba794dfSYueHaibing 	bs->regs = devm_platform_ioremap_resource(pdev, 0);
1342e1483ac0SLukas Wunner 	if (IS_ERR(bs->regs))
1343e1483ac0SLukas Wunner 		return PTR_ERR(bs->regs);
1344f8043872SChris Boot 
1345f8043872SChris Boot 	bs->clk = devm_clk_get(&pdev->dev, NULL);
1346e1483ac0SLukas Wunner 	if (IS_ERR(bs->clk))
1347e1483ac0SLukas Wunner 		return dev_err_probe(&pdev->dev, PTR_ERR(bs->clk),
134865acd82cSKrzysztof Kozlowski 				     "could not get clk\n");
1349f8043872SChris Boot 
1350c6892892SRichard Fitzgerald 	ctlr->max_speed_hz = clk_get_rate(bs->clk) / 2;
1351c6892892SRichard Fitzgerald 
1352ddf0e1c2SMartin Sperl 	bs->irq = platform_get_irq(pdev, 0);
1353e1483ac0SLukas Wunner 	if (bs->irq <= 0)
1354e1483ac0SLukas Wunner 		return bs->irq ? bs->irq : -ENODEV;
1355f8043872SChris Boot 
1356f8043872SChris Boot 	clk_prepare_enable(bs->clk);
1357f8043872SChris Boot 
13586133fed0SPeter Ujfalusi 	err = bcm2835_dma_init(ctlr, &pdev->dev, bs);
13596133fed0SPeter Ujfalusi 	if (err)
13606133fed0SPeter Ujfalusi 		goto out_clk_disable;
1361ddf0e1c2SMartin Sperl 
1362ddf0e1c2SMartin Sperl 	/* initialise the hardware with the default polarities */
1363ddf0e1c2SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS,
1364ddf0e1c2SMartin Sperl 		   BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
1365ddf0e1c2SMartin Sperl 
1366d62069c2SMark Brown 	err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0,
1367afe7e363SRobin Murphy 			       dev_name(&pdev->dev), bs);
1368f8043872SChris Boot 	if (err) {
1369f8043872SChris Boot 		dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
1370666224b4SPeter Ujfalusi 		goto out_dma_release;
1371f8043872SChris Boot 	}
1372f8043872SChris Boot 
13739dd277ffSLukas Wunner 	err = spi_register_controller(ctlr);
1374f8043872SChris Boot 	if (err) {
13755f336ea5SLukas Wunner 		dev_err(&pdev->dev, "could not register SPI controller: %d\n",
13765f336ea5SLukas Wunner 			err);
1377666224b4SPeter Ujfalusi 		goto out_dma_release;
1378f8043872SChris Boot 	}
1379f8043872SChris Boot 
1380154f7da5SMartin Sperl 	bcm2835_debugfs_create(bs, dev_name(&pdev->dev));
1381154f7da5SMartin Sperl 
1382f8043872SChris Boot 	return 0;
1383f8043872SChris Boot 
1384666224b4SPeter Ujfalusi out_dma_release:
1385666224b4SPeter Ujfalusi 	bcm2835_dma_release(ctlr, bs);
1386f8043872SChris Boot out_clk_disable:
1387f8043872SChris Boot 	clk_disable_unprepare(bs->clk);
1388f8043872SChris Boot 	return err;
1389f8043872SChris Boot }
1390f8043872SChris Boot 
1391f8043872SChris Boot static int bcm2835_spi_remove(struct platform_device *pdev)
1392f8043872SChris Boot {
13935f336ea5SLukas Wunner 	struct spi_controller *ctlr = platform_get_drvdata(pdev);
13945f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1395f8043872SChris Boot 
1396154f7da5SMartin Sperl 	bcm2835_debugfs_remove(bs);
1397154f7da5SMartin Sperl 
13989dd277ffSLukas Wunner 	spi_unregister_controller(ctlr);
13999dd277ffSLukas Wunner 
140005897c71SLukas Wunner 	bcm2835_dma_release(ctlr, bs);
140105897c71SLukas Wunner 
1402f8043872SChris Boot 	/* Clear FIFOs, and disable the HW block */
1403f8043872SChris Boot 	bcm2835_wr(bs, BCM2835_SPI_CS,
1404f8043872SChris Boot 		   BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
1405f8043872SChris Boot 
1406f8043872SChris Boot 	clk_disable_unprepare(bs->clk);
1407f8043872SChris Boot 
1408f8043872SChris Boot 	return 0;
1409f8043872SChris Boot }
1410f8043872SChris Boot 
1411118eb0e5SFlorian Fainelli static void bcm2835_spi_shutdown(struct platform_device *pdev)
1412118eb0e5SFlorian Fainelli {
1413118eb0e5SFlorian Fainelli 	int ret;
1414118eb0e5SFlorian Fainelli 
1415118eb0e5SFlorian Fainelli 	ret = bcm2835_spi_remove(pdev);
1416118eb0e5SFlorian Fainelli 	if (ret)
1417118eb0e5SFlorian Fainelli 		dev_err(&pdev->dev, "failed to shutdown\n");
1418118eb0e5SFlorian Fainelli }
1419118eb0e5SFlorian Fainelli 
1420f8043872SChris Boot static const struct of_device_id bcm2835_spi_match[] = {
1421f8043872SChris Boot 	{ .compatible = "brcm,bcm2835-spi", },
1422f8043872SChris Boot 	{}
1423f8043872SChris Boot };
1424f8043872SChris Boot MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
1425f8043872SChris Boot 
1426f8043872SChris Boot static struct platform_driver bcm2835_spi_driver = {
1427f8043872SChris Boot 	.driver		= {
1428f8043872SChris Boot 		.name		= DRV_NAME,
1429f8043872SChris Boot 		.of_match_table	= bcm2835_spi_match,
1430f8043872SChris Boot 	},
1431f8043872SChris Boot 	.probe		= bcm2835_spi_probe,
1432f8043872SChris Boot 	.remove		= bcm2835_spi_remove,
1433118eb0e5SFlorian Fainelli 	.shutdown	= bcm2835_spi_shutdown,
1434f8043872SChris Boot };
1435f8043872SChris Boot module_platform_driver(bcm2835_spi_driver);
1436f8043872SChris Boot 
1437f8043872SChris Boot MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
1438f8043872SChris Boot MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
143922bf6cd2SStefan Wahren MODULE_LICENSE("GPL");
1440