xref: /openbmc/linux/drivers/spi/spi-bcm2835.c (revision 8102d64c)
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>
27749396cbSRob Herring #include <linux/platform_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
86c45c1e82SAlexandru Tachici  * @clk_hz: core clock cached speed
87acf0f856SLukas Wunner  * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full
883bd7f658SLukas Wunner  * @tfr: SPI transfer currently processed
89afe7e363SRobin Murphy  * @ctlr: SPI controller reverse lookup
90acf0f856SLukas Wunner  * @tx_buf: pointer whence next transmitted byte is read
91acf0f856SLukas Wunner  * @rx_buf: pointer where next received byte is written
92acf0f856SLukas Wunner  * @tx_len: remaining bytes to transmit
93acf0f856SLukas Wunner  * @rx_len: remaining bytes to receive
943bd7f658SLukas Wunner  * @tx_prologue: bytes transmitted without DMA if first TX sglist entry's
953bd7f658SLukas Wunner  *	length is not a multiple of 4 (to overcome hardware limitation)
963bd7f658SLukas Wunner  * @rx_prologue: bytes received without DMA if first RX sglist entry's
973bd7f658SLukas Wunner  *	length is not a multiple of 4 (to overcome hardware limitation)
983bd7f658SLukas Wunner  * @tx_spillover: whether @tx_prologue spills over to second TX sglist entry
99154f7da5SMartin Sperl  * @debugfs_dir: the debugfs directory - neede to remove debugfs when
100154f7da5SMartin Sperl  *      unloading the module
101154f7da5SMartin Sperl  * @count_transfer_polling: count of how often polling mode is used
102154f7da5SMartin Sperl  * @count_transfer_irq: count of how often interrupt mode is used
103154f7da5SMartin Sperl  * @count_transfer_irq_after_polling: count of how often we fall back to
104154f7da5SMartin Sperl  *      interrupt mode after starting in polling mode.
105154f7da5SMartin Sperl  *      These are counted as well in @count_transfer_polling and
106154f7da5SMartin Sperl  *      @count_transfer_irq
107154f7da5SMartin Sperl  * @count_transfer_dma: count how often dma mode is used
108ec679bdaSLukas Wunner  * @target: SPI target currently selected
1098259bf66SLukas Wunner  *	(used by bcm2835_spi_dma_tx_done() to write @clear_rx_cs)
1108259bf66SLukas Wunner  * @tx_dma_active: whether a TX DMA descriptor is in progress
1118259bf66SLukas Wunner  * @rx_dma_active: whether a RX DMA descriptor is in progress
1128259bf66SLukas Wunner  *	(used by bcm2835_spi_dma_tx_done() to handle a race)
1132b8279aeSLukas Wunner  * @fill_tx_desc: preallocated TX DMA descriptor used for RX-only transfers
1142b8279aeSLukas Wunner  *	(cyclically copies from zero page to TX FIFO)
1152b8279aeSLukas Wunner  * @fill_tx_addr: bus address of zero page
116acf0f856SLukas Wunner  */
117f8043872SChris Boot struct bcm2835_spi {
118f8043872SChris Boot 	void __iomem *regs;
119f8043872SChris Boot 	struct clk *clk;
120c45c1e82SAlexandru Tachici 	unsigned long clk_hz;
121f8043872SChris Boot 	int irq;
1223bd7f658SLukas Wunner 	struct spi_transfer *tfr;
123afe7e363SRobin Murphy 	struct spi_controller *ctlr;
124f8043872SChris Boot 	const u8 *tx_buf;
125f8043872SChris Boot 	u8 *rx_buf;
126e34ff011SMartin Sperl 	int tx_len;
127e34ff011SMartin Sperl 	int rx_len;
1283bd7f658SLukas Wunner 	int tx_prologue;
1293bd7f658SLukas Wunner 	int rx_prologue;
130b31a9299SLukas Wunner 	unsigned int tx_spillover;
131154f7da5SMartin Sperl 
132154f7da5SMartin Sperl 	struct dentry *debugfs_dir;
133154f7da5SMartin Sperl 	u64 count_transfer_polling;
134154f7da5SMartin Sperl 	u64 count_transfer_irq;
135154f7da5SMartin Sperl 	u64 count_transfer_irq_after_polling;
136154f7da5SMartin Sperl 	u64 count_transfer_dma;
1378259bf66SLukas Wunner 
138ec679bdaSLukas Wunner 	struct bcm2835_spidev *target;
1398259bf66SLukas Wunner 	unsigned int tx_dma_active;
1408259bf66SLukas Wunner 	unsigned int rx_dma_active;
1412b8279aeSLukas Wunner 	struct dma_async_tx_descriptor *fill_tx_desc;
1422b8279aeSLukas Wunner 	dma_addr_t fill_tx_addr;
143ec679bdaSLukas Wunner };
144ec679bdaSLukas Wunner 
145ec679bdaSLukas Wunner /**
146ec679bdaSLukas Wunner  * struct bcm2835_spidev - BCM2835 SPI target
147ec679bdaSLukas Wunner  * @prepare_cs: precalculated CS register value for ->prepare_message()
148ec679bdaSLukas Wunner  *	(uses target-specific clock polarity and phase settings)
149ec679bdaSLukas Wunner  * @clear_rx_desc: preallocated RX DMA descriptor used for TX-only transfers
150ec679bdaSLukas Wunner  *	(cyclically clears RX FIFO by writing @clear_rx_cs to CS register)
151ec679bdaSLukas Wunner  * @clear_rx_addr: bus address of @clear_rx_cs
152ec679bdaSLukas Wunner  * @clear_rx_cs: precalculated CS register value to clear RX FIFO
153ec679bdaSLukas Wunner  *	(uses target-specific clock polarity and phase settings)
154ec679bdaSLukas Wunner  */
155ec679bdaSLukas Wunner struct bcm2835_spidev {
156ec679bdaSLukas Wunner 	u32 prepare_cs;
157ec679bdaSLukas Wunner 	struct dma_async_tx_descriptor *clear_rx_desc;
1588259bf66SLukas Wunner 	dma_addr_t clear_rx_addr;
159ec679bdaSLukas Wunner 	u32 clear_rx_cs ____cacheline_aligned;
160f8043872SChris Boot };
161f8043872SChris Boot 
162154f7da5SMartin Sperl #if defined(CONFIG_DEBUG_FS)
bcm2835_debugfs_create(struct bcm2835_spi * bs,const char * dname)163154f7da5SMartin Sperl static void bcm2835_debugfs_create(struct bcm2835_spi *bs,
164154f7da5SMartin Sperl 				   const char *dname)
165154f7da5SMartin Sperl {
166154f7da5SMartin Sperl 	char name[64];
167154f7da5SMartin Sperl 	struct dentry *dir;
168154f7da5SMartin Sperl 
169154f7da5SMartin Sperl 	/* get full name */
170154f7da5SMartin Sperl 	snprintf(name, sizeof(name), "spi-bcm2835-%s", dname);
171154f7da5SMartin Sperl 
172154f7da5SMartin Sperl 	/* the base directory */
173154f7da5SMartin Sperl 	dir = debugfs_create_dir(name, NULL);
174154f7da5SMartin Sperl 	bs->debugfs_dir = dir;
175154f7da5SMartin Sperl 
176154f7da5SMartin Sperl 	/* the counters */
177154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_polling", 0444, dir,
178154f7da5SMartin Sperl 			   &bs->count_transfer_polling);
179154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_irq", 0444, dir,
180154f7da5SMartin Sperl 			   &bs->count_transfer_irq);
181154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_irq_after_polling", 0444, dir,
182154f7da5SMartin Sperl 			   &bs->count_transfer_irq_after_polling);
183154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_dma", 0444, dir,
184154f7da5SMartin Sperl 			   &bs->count_transfer_dma);
185154f7da5SMartin Sperl }
186154f7da5SMartin Sperl 
bcm2835_debugfs_remove(struct bcm2835_spi * bs)187154f7da5SMartin Sperl static void bcm2835_debugfs_remove(struct bcm2835_spi *bs)
188154f7da5SMartin Sperl {
189154f7da5SMartin Sperl 	debugfs_remove_recursive(bs->debugfs_dir);
190154f7da5SMartin Sperl 	bs->debugfs_dir = NULL;
191154f7da5SMartin Sperl }
192154f7da5SMartin Sperl #else
bcm2835_debugfs_create(struct bcm2835_spi * bs,const char * dname)193154f7da5SMartin Sperl static void bcm2835_debugfs_create(struct bcm2835_spi *bs,
194154f7da5SMartin Sperl 				   const char *dname)
195154f7da5SMartin Sperl {
196154f7da5SMartin Sperl }
197154f7da5SMartin Sperl 
bcm2835_debugfs_remove(struct bcm2835_spi * bs)198154f7da5SMartin Sperl static void bcm2835_debugfs_remove(struct bcm2835_spi *bs)
199154f7da5SMartin Sperl {
200154f7da5SMartin Sperl }
201154f7da5SMartin Sperl #endif /* CONFIG_DEBUG_FS */
202154f7da5SMartin Sperl 
bcm2835_rd(struct bcm2835_spi * bs,unsigned int reg)203e37687c9SJacko Dirks static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned int reg)
204f8043872SChris Boot {
205f8043872SChris Boot 	return readl(bs->regs + reg);
206f8043872SChris Boot }
207f8043872SChris Boot 
bcm2835_wr(struct bcm2835_spi * bs,unsigned int reg,u32 val)208e37687c9SJacko Dirks static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned int reg, u32 val)
209f8043872SChris Boot {
210f8043872SChris Boot 	writel(val, bs->regs + reg);
211f8043872SChris Boot }
212f8043872SChris Boot 
bcm2835_rd_fifo(struct bcm2835_spi * bs)2134adf3129SMartin Sperl static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs)
214f8043872SChris Boot {
215f8043872SChris Boot 	u8 byte;
216f8043872SChris Boot 
217e34ff011SMartin Sperl 	while ((bs->rx_len) &&
218e34ff011SMartin Sperl 	       (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
219f8043872SChris Boot 		byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
220f8043872SChris Boot 		if (bs->rx_buf)
221f8043872SChris Boot 			*bs->rx_buf++ = byte;
222e34ff011SMartin Sperl 		bs->rx_len--;
223f8043872SChris Boot 	}
224f8043872SChris Boot }
225f8043872SChris Boot 
bcm2835_wr_fifo(struct bcm2835_spi * bs)2264adf3129SMartin Sperl static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
227f8043872SChris Boot {
228f8043872SChris Boot 	u8 byte;
229f8043872SChris Boot 
230e34ff011SMartin Sperl 	while ((bs->tx_len) &&
2314adf3129SMartin Sperl 	       (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
232f8043872SChris Boot 		byte = bs->tx_buf ? *bs->tx_buf++ : 0;
233f8043872SChris Boot 		bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
234e34ff011SMartin Sperl 		bs->tx_len--;
235f8043872SChris Boot 	}
236f8043872SChris Boot }
237f8043872SChris Boot 
2383bd7f658SLukas Wunner /**
2393bd7f658SLukas Wunner  * bcm2835_rd_fifo_count() - blindly read exactly @count bytes from RX FIFO
2403bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
2413bd7f658SLukas Wunner  * @count: bytes to read from RX FIFO
2423bd7f658SLukas Wunner  *
2433bd7f658SLukas Wunner  * The caller must ensure that @bs->rx_len is greater than or equal to @count,
2443bd7f658SLukas Wunner  * that the RX FIFO contains at least @count bytes and that the DMA Enable flag
2453bd7f658SLukas Wunner  * in the CS register is set (such that a read from the FIFO register receives
246b31a9299SLukas Wunner  * 32-bit instead of just 8-bit).  Moreover @bs->rx_buf must not be %NULL.
2473bd7f658SLukas Wunner  */
bcm2835_rd_fifo_count(struct bcm2835_spi * bs,int count)2483bd7f658SLukas Wunner static inline void bcm2835_rd_fifo_count(struct bcm2835_spi *bs, int count)
2493bd7f658SLukas Wunner {
2503bd7f658SLukas Wunner 	u32 val;
251b31a9299SLukas Wunner 	int len;
2523bd7f658SLukas Wunner 
2533bd7f658SLukas Wunner 	bs->rx_len -= count;
2543bd7f658SLukas Wunner 
25526751de2SRobin Murphy 	do {
2563bd7f658SLukas Wunner 		val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
257b31a9299SLukas Wunner 		len = min(count, 4);
2583bd7f658SLukas Wunner 		memcpy(bs->rx_buf, &val, len);
2593bd7f658SLukas Wunner 		bs->rx_buf += len;
2603bd7f658SLukas Wunner 		count -= 4;
26126751de2SRobin Murphy 	} while (count > 0);
2623bd7f658SLukas Wunner }
2633bd7f658SLukas Wunner 
2643bd7f658SLukas Wunner /**
2653bd7f658SLukas Wunner  * bcm2835_wr_fifo_count() - blindly write exactly @count bytes to TX FIFO
2663bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
2673bd7f658SLukas Wunner  * @count: bytes to write to TX FIFO
2683bd7f658SLukas Wunner  *
2693bd7f658SLukas Wunner  * The caller must ensure that @bs->tx_len is greater than or equal to @count,
2703bd7f658SLukas Wunner  * that the TX FIFO can accommodate @count bytes and that the DMA Enable flag
2713bd7f658SLukas Wunner  * in the CS register is set (such that a write to the FIFO register transmits
2723bd7f658SLukas Wunner  * 32-bit instead of just 8-bit).
2733bd7f658SLukas Wunner  */
bcm2835_wr_fifo_count(struct bcm2835_spi * bs,int count)2743bd7f658SLukas Wunner static inline void bcm2835_wr_fifo_count(struct bcm2835_spi *bs, int count)
2753bd7f658SLukas Wunner {
2763bd7f658SLukas Wunner 	u32 val;
277b31a9299SLukas Wunner 	int len;
2783bd7f658SLukas Wunner 
2793bd7f658SLukas Wunner 	bs->tx_len -= count;
2803bd7f658SLukas Wunner 
28126751de2SRobin Murphy 	do {
2823bd7f658SLukas Wunner 		if (bs->tx_buf) {
283b31a9299SLukas Wunner 			len = min(count, 4);
2843bd7f658SLukas Wunner 			memcpy(&val, bs->tx_buf, len);
2853bd7f658SLukas Wunner 			bs->tx_buf += len;
2863bd7f658SLukas Wunner 		} else {
2873bd7f658SLukas Wunner 			val = 0;
2883bd7f658SLukas Wunner 		}
2893bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
2903bd7f658SLukas Wunner 		count -= 4;
29126751de2SRobin Murphy 	} while (count > 0);
2923bd7f658SLukas Wunner }
2933bd7f658SLukas Wunner 
2943bd7f658SLukas Wunner /**
2953bd7f658SLukas Wunner  * bcm2835_wait_tx_fifo_empty() - busy-wait for TX FIFO to empty
2963bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
297b31a9299SLukas Wunner  *
298b31a9299SLukas Wunner  * The caller must ensure that the RX FIFO can accommodate as many bytes
299b31a9299SLukas Wunner  * as have been written to the TX FIFO:  Transmission is halted once the
300b31a9299SLukas Wunner  * RX FIFO is full, causing this function to spin forever.
3013bd7f658SLukas Wunner  */
bcm2835_wait_tx_fifo_empty(struct bcm2835_spi * bs)3023bd7f658SLukas Wunner static inline void bcm2835_wait_tx_fifo_empty(struct bcm2835_spi *bs)
3033bd7f658SLukas Wunner {
3043bd7f658SLukas Wunner 	while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE))
3053bd7f658SLukas Wunner 		cpu_relax();
3063bd7f658SLukas Wunner }
3073bd7f658SLukas Wunner 
3082e0733bcSLukas Wunner /**
3092e0733bcSLukas Wunner  * bcm2835_rd_fifo_blind() - blindly read up to @count bytes from RX FIFO
3102e0733bcSLukas Wunner  * @bs: BCM2835 SPI controller
3112e0733bcSLukas Wunner  * @count: bytes available for reading in RX FIFO
3122e0733bcSLukas Wunner  */
bcm2835_rd_fifo_blind(struct bcm2835_spi * bs,int count)3132e0733bcSLukas Wunner static inline void bcm2835_rd_fifo_blind(struct bcm2835_spi *bs, int count)
3142e0733bcSLukas Wunner {
3152e0733bcSLukas Wunner 	u8 val;
3162e0733bcSLukas Wunner 
3172e0733bcSLukas Wunner 	count = min(count, bs->rx_len);
3182e0733bcSLukas Wunner 	bs->rx_len -= count;
3192e0733bcSLukas Wunner 
32026751de2SRobin Murphy 	do {
3212e0733bcSLukas Wunner 		val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
3222e0733bcSLukas Wunner 		if (bs->rx_buf)
3232e0733bcSLukas Wunner 			*bs->rx_buf++ = val;
32426751de2SRobin Murphy 	} while (--count);
3252e0733bcSLukas Wunner }
3262e0733bcSLukas Wunner 
3272e0733bcSLukas Wunner /**
3282e0733bcSLukas Wunner  * bcm2835_wr_fifo_blind() - blindly write up to @count bytes to TX FIFO
3292e0733bcSLukas Wunner  * @bs: BCM2835 SPI controller
3302e0733bcSLukas Wunner  * @count: bytes available for writing in TX FIFO
3312e0733bcSLukas Wunner  */
bcm2835_wr_fifo_blind(struct bcm2835_spi * bs,int count)3322e0733bcSLukas Wunner static inline void bcm2835_wr_fifo_blind(struct bcm2835_spi *bs, int count)
3332e0733bcSLukas Wunner {
3342e0733bcSLukas Wunner 	u8 val;
3352e0733bcSLukas Wunner 
3362e0733bcSLukas Wunner 	count = min(count, bs->tx_len);
3372e0733bcSLukas Wunner 	bs->tx_len -= count;
3382e0733bcSLukas Wunner 
33926751de2SRobin Murphy 	do {
3402e0733bcSLukas Wunner 		val = bs->tx_buf ? *bs->tx_buf++ : 0;
3412e0733bcSLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
34226751de2SRobin Murphy 	} while (--count);
3432e0733bcSLukas Wunner }
3442e0733bcSLukas Wunner 
bcm2835_spi_reset_hw(struct bcm2835_spi * bs)345ac4648b5SRobin Murphy static void bcm2835_spi_reset_hw(struct bcm2835_spi *bs)
346e34ff011SMartin Sperl {
347e34ff011SMartin Sperl 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
348e34ff011SMartin Sperl 
349e34ff011SMartin Sperl 	/* Disable SPI interrupts and transfer */
350e34ff011SMartin Sperl 	cs &= ~(BCM2835_SPI_CS_INTR |
351e34ff011SMartin Sperl 		BCM2835_SPI_CS_INTD |
3523ecd37edSMartin Sperl 		BCM2835_SPI_CS_DMAEN |
353e34ff011SMartin Sperl 		BCM2835_SPI_CS_TA);
3544c524191SLukas Wunner 	/*
3554c524191SLukas Wunner 	 * Transmission sometimes breaks unless the DONE bit is written at the
3564c524191SLukas Wunner 	 * end of every transfer.  The spec says it's a RO bit.  Either the
3574c524191SLukas Wunner 	 * spec is wrong and the bit is actually of type RW1C, or it's a
3584c524191SLukas Wunner 	 * hardware erratum.
3594c524191SLukas Wunner 	 */
3604c524191SLukas Wunner 	cs |= BCM2835_SPI_CS_DONE;
361e34ff011SMartin Sperl 	/* and reset RX/TX FIFOS */
362e34ff011SMartin Sperl 	cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
363e34ff011SMartin Sperl 
364e34ff011SMartin Sperl 	/* and reset the SPI_HW */
365e34ff011SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS, cs);
3663ecd37edSMartin Sperl 	/* as well as DLEN */
3673ecd37edSMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_DLEN, 0);
368e34ff011SMartin Sperl }
369e34ff011SMartin Sperl 
bcm2835_spi_interrupt(int irq,void * dev_id)370f8043872SChris Boot static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
371f8043872SChris Boot {
372afe7e363SRobin Murphy 	struct bcm2835_spi *bs = dev_id;
3732e0733bcSLukas Wunner 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
3742e0733bcSLukas Wunner 
37589fcdd53SMartin Sperl 	/* Bail out early if interrupts are not enabled */
37689fcdd53SMartin Sperl 	if (!(cs & BCM2835_SPI_CS_INTR))
37789fcdd53SMartin Sperl 		return IRQ_NONE;
37889fcdd53SMartin Sperl 
3792e0733bcSLukas Wunner 	/*
3802e0733bcSLukas Wunner 	 * An interrupt is signaled either if DONE is set (TX FIFO empty)
3812e0733bcSLukas Wunner 	 * or if RXR is set (RX FIFO >= ¾ full).
3822e0733bcSLukas Wunner 	 */
3832e0733bcSLukas Wunner 	if (cs & BCM2835_SPI_CS_RXF)
3842e0733bcSLukas Wunner 		bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
3852e0733bcSLukas Wunner 	else if (cs & BCM2835_SPI_CS_RXR)
3862e0733bcSLukas Wunner 		bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE_3_4);
3872e0733bcSLukas Wunner 
3882e0733bcSLukas Wunner 	if (bs->tx_len && cs & BCM2835_SPI_CS_DONE)
3892e0733bcSLukas Wunner 		bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
390f8043872SChris Boot 
3914adf3129SMartin Sperl 	/* Read as many bytes as possible from FIFO */
3924adf3129SMartin Sperl 	bcm2835_rd_fifo(bs);
393e34ff011SMartin Sperl 	/* Write as many bytes as possible to FIFO */
3944adf3129SMartin Sperl 	bcm2835_wr_fifo(bs);
395f8043872SChris Boot 
39656c17234SLukas Wunner 	if (!bs->rx_len) {
397e34ff011SMartin Sperl 		/* Transfer complete - reset SPI HW */
398ac4648b5SRobin Murphy 		bcm2835_spi_reset_hw(bs);
399e34ff011SMartin Sperl 		/* wake up the framework */
400ccae0b40SVincent Pelletier 		spi_finalize_current_transfer(bs->ctlr);
401f8043872SChris Boot 	}
402f8043872SChris Boot 
403f8043872SChris Boot 	return IRQ_HANDLED;
404f8043872SChris Boot }
405f8043872SChris Boot 
bcm2835_spi_transfer_one_irq(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * tfr,u32 cs,bool fifo_empty)4065f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_irq(struct spi_controller *ctlr,
407704f32d4SMartin Sperl 					struct spi_device *spi,
408704f32d4SMartin Sperl 					struct spi_transfer *tfr,
4092e0733bcSLukas Wunner 					u32 cs, bool fifo_empty)
410704f32d4SMartin Sperl {
4115f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
412f8043872SChris Boot 
413154f7da5SMartin Sperl 	/* update usage statistics */
414154f7da5SMartin Sperl 	bs->count_transfer_irq++;
415154f7da5SMartin Sperl 
416f8043872SChris Boot 	/*
4175c09e42fSLukas Wunner 	 * Enable HW block, but with interrupts still disabled.
4185c09e42fSLukas Wunner 	 * Otherwise the empty TX FIFO would immediately trigger an interrupt.
419f8043872SChris Boot 	 */
4205c09e42fSLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
4215c09e42fSLukas Wunner 
4225c09e42fSLukas Wunner 	/* fill TX FIFO as much as possible */
4232e0733bcSLukas Wunner 	if (fifo_empty)
4242e0733bcSLukas Wunner 		bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
4255c09e42fSLukas Wunner 	bcm2835_wr_fifo(bs);
4265c09e42fSLukas Wunner 
4275c09e42fSLukas Wunner 	/* enable interrupts */
428e34ff011SMartin Sperl 	cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
429f8043872SChris Boot 	bcm2835_wr(bs, BCM2835_SPI_CS, cs);
430f8043872SChris Boot 
431e34ff011SMartin Sperl 	/* signal that we need to wait for completion */
432e34ff011SMartin Sperl 	return 1;
433f8043872SChris Boot }
434f8043872SChris Boot 
4353bd7f658SLukas Wunner /**
4363bd7f658SLukas Wunner  * bcm2835_spi_transfer_prologue() - transfer first few bytes without DMA
4375f336ea5SLukas Wunner  * @ctlr: SPI host controller
4383bd7f658SLukas Wunner  * @tfr: SPI transfer
4393bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
4403bd7f658SLukas Wunner  * @cs: CS register
4413bd7f658SLukas Wunner  *
4423bd7f658SLukas Wunner  * A limitation in DMA mode is that the FIFO must be accessed in 4 byte chunks.
4433bd7f658SLukas Wunner  * Only the final write access is permitted to transmit less than 4 bytes, the
4443bd7f658SLukas Wunner  * SPI controller deduces its intended size from the DLEN register.
4453bd7f658SLukas Wunner  *
4463bd7f658SLukas Wunner  * If a TX or RX sglist contains multiple entries, one per page, and the first
4473bd7f658SLukas Wunner  * entry starts in the middle of a page, that first entry's length may not be
4483bd7f658SLukas Wunner  * a multiple of 4.  Subsequent entries are fine because they span an entire
4493bd7f658SLukas Wunner  * page, hence do have a length that's a multiple of 4.
4503bd7f658SLukas Wunner  *
4513bd7f658SLukas Wunner  * This cannot happen with kmalloc'ed buffers (which is what most clients use)
4523bd7f658SLukas Wunner  * because they are contiguous in physical memory and therefore not split on
4533bd7f658SLukas Wunner  * page boundaries by spi_map_buf().  But it *can* happen with vmalloc'ed
4543bd7f658SLukas Wunner  * buffers.
4553bd7f658SLukas Wunner  *
4563bd7f658SLukas Wunner  * The DMA engine is incapable of combining sglist entries into a continuous
4573bd7f658SLukas Wunner  * stream of 4 byte chunks, it treats every entry separately:  A TX entry is
4583bd7f658SLukas Wunner  * rounded up a to a multiple of 4 bytes by transmitting surplus bytes, an RX
4593bd7f658SLukas Wunner  * entry is rounded up by throwing away received bytes.
4603bd7f658SLukas Wunner  *
4613bd7f658SLukas Wunner  * Overcome this limitation by transferring the first few bytes without DMA:
4623bd7f658SLukas Wunner  * E.g. if the first TX sglist entry's length is 23 and the first RX's is 42,
4633bd7f658SLukas Wunner  * write 3 bytes to the TX FIFO but read only 2 bytes from the RX FIFO.
4643bd7f658SLukas Wunner  * The residue of 1 byte in the RX FIFO is picked up by DMA.  Together with
4653bd7f658SLukas Wunner  * the rest of the first RX sglist entry it makes up a multiple of 4 bytes.
4663bd7f658SLukas Wunner  *
4673bd7f658SLukas Wunner  * Should the RX prologue be larger, say, 3 vis-à-vis a TX prologue of 1,
4683bd7f658SLukas Wunner  * write 1 + 4 = 5 bytes to the TX FIFO and read 3 bytes from the RX FIFO.
4693bd7f658SLukas Wunner  * Caution, the additional 4 bytes spill over to the second TX sglist entry
4703bd7f658SLukas Wunner  * if the length of the first is *exactly* 1.
4713bd7f658SLukas Wunner  *
4723bd7f658SLukas Wunner  * At most 6 bytes are written and at most 3 bytes read.  Do we know the
4733bd7f658SLukas Wunner  * transfer has this many bytes?  Yes, see BCM2835_SPI_DMA_MIN_LENGTH.
4743bd7f658SLukas Wunner  *
4753bd7f658SLukas Wunner  * The FIFO is normally accessed with 8-bit width by the CPU and 32-bit width
4763bd7f658SLukas Wunner  * by the DMA engine.  Toggling the DMA Enable flag in the CS register switches
4773bd7f658SLukas Wunner  * the width but also garbles the FIFO's contents.  The prologue must therefore
4783bd7f658SLukas Wunner  * be transmitted in 32-bit width to ensure that the following DMA transfer can
4793bd7f658SLukas Wunner  * pick up the residue in the RX FIFO in ungarbled form.
4803bd7f658SLukas Wunner  */
bcm2835_spi_transfer_prologue(struct spi_controller * ctlr,struct spi_transfer * tfr,struct bcm2835_spi * bs,u32 cs)4815f336ea5SLukas Wunner static void bcm2835_spi_transfer_prologue(struct spi_controller *ctlr,
4823bd7f658SLukas Wunner 					  struct spi_transfer *tfr,
4833bd7f658SLukas Wunner 					  struct bcm2835_spi *bs,
4843bd7f658SLukas Wunner 					  u32 cs)
4853bd7f658SLukas Wunner {
4863bd7f658SLukas Wunner 	int tx_remaining;
4873bd7f658SLukas Wunner 
4883bd7f658SLukas Wunner 	bs->tfr		 = tfr;
4893bd7f658SLukas Wunner 	bs->tx_prologue  = 0;
4903bd7f658SLukas Wunner 	bs->rx_prologue  = 0;
4913bd7f658SLukas Wunner 	bs->tx_spillover = false;
4923bd7f658SLukas Wunner 
4932b8279aeSLukas Wunner 	if (bs->tx_buf && !sg_is_last(&tfr->tx_sg.sgl[0]))
4943bd7f658SLukas Wunner 		bs->tx_prologue = sg_dma_len(&tfr->tx_sg.sgl[0]) & 3;
4953bd7f658SLukas Wunner 
4968259bf66SLukas Wunner 	if (bs->rx_buf && !sg_is_last(&tfr->rx_sg.sgl[0])) {
4973bd7f658SLukas Wunner 		bs->rx_prologue = sg_dma_len(&tfr->rx_sg.sgl[0]) & 3;
4983bd7f658SLukas Wunner 
4993bd7f658SLukas Wunner 		if (bs->rx_prologue > bs->tx_prologue) {
5002b8279aeSLukas Wunner 			if (!bs->tx_buf || sg_is_last(&tfr->tx_sg.sgl[0])) {
5013bd7f658SLukas Wunner 				bs->tx_prologue  = bs->rx_prologue;
5023bd7f658SLukas Wunner 			} else {
5033bd7f658SLukas Wunner 				bs->tx_prologue += 4;
5043bd7f658SLukas Wunner 				bs->tx_spillover =
5053bd7f658SLukas Wunner 					!(sg_dma_len(&tfr->tx_sg.sgl[0]) & ~3);
5063bd7f658SLukas Wunner 			}
5073bd7f658SLukas Wunner 		}
5083bd7f658SLukas Wunner 	}
5093bd7f658SLukas Wunner 
5103bd7f658SLukas Wunner 	/* rx_prologue > 0 implies tx_prologue > 0, so check only the latter */
5113bd7f658SLukas Wunner 	if (!bs->tx_prologue)
5123bd7f658SLukas Wunner 		return;
5133bd7f658SLukas Wunner 
5143bd7f658SLukas Wunner 	/* Write and read RX prologue.  Adjust first entry in RX sglist. */
5153bd7f658SLukas Wunner 	if (bs->rx_prologue) {
5163bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->rx_prologue);
5173bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
5183bd7f658SLukas Wunner 						  | BCM2835_SPI_CS_DMAEN);
5193bd7f658SLukas Wunner 		bcm2835_wr_fifo_count(bs, bs->rx_prologue);
5203bd7f658SLukas Wunner 		bcm2835_wait_tx_fifo_empty(bs);
5213bd7f658SLukas Wunner 		bcm2835_rd_fifo_count(bs, bs->rx_prologue);
5224c524191SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_RX
5234c524191SLukas Wunner 						  | BCM2835_SPI_CS_CLEAR_TX
5244c524191SLukas Wunner 						  | BCM2835_SPI_CS_DONE);
5253bd7f658SLukas Wunner 
5265f336ea5SLukas Wunner 		dma_sync_single_for_device(ctlr->dma_rx->device->dev,
527b31a9299SLukas Wunner 					   sg_dma_address(&tfr->rx_sg.sgl[0]),
528b31a9299SLukas Wunner 					   bs->rx_prologue, DMA_FROM_DEVICE);
5293bd7f658SLukas Wunner 
530b31a9299SLukas Wunner 		sg_dma_address(&tfr->rx_sg.sgl[0]) += bs->rx_prologue;
531b31a9299SLukas Wunner 		sg_dma_len(&tfr->rx_sg.sgl[0])     -= bs->rx_prologue;
5323bd7f658SLukas Wunner 	}
5333bd7f658SLukas Wunner 
5342b8279aeSLukas Wunner 	if (!bs->tx_buf)
5352b8279aeSLukas Wunner 		return;
5362b8279aeSLukas Wunner 
5373bd7f658SLukas Wunner 	/*
5383bd7f658SLukas Wunner 	 * Write remaining TX prologue.  Adjust first entry in TX sglist.
5393bd7f658SLukas Wunner 	 * Also adjust second entry if prologue spills over to it.
5403bd7f658SLukas Wunner 	 */
5413bd7f658SLukas Wunner 	tx_remaining = bs->tx_prologue - bs->rx_prologue;
5423bd7f658SLukas Wunner 	if (tx_remaining) {
5433bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_DLEN, tx_remaining);
5443bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
5453bd7f658SLukas Wunner 						  | BCM2835_SPI_CS_DMAEN);
5463bd7f658SLukas Wunner 		bcm2835_wr_fifo_count(bs, tx_remaining);
5473bd7f658SLukas Wunner 		bcm2835_wait_tx_fifo_empty(bs);
5484c524191SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_TX
5494c524191SLukas Wunner 						  | BCM2835_SPI_CS_DONE);
5503bd7f658SLukas Wunner 	}
5513bd7f658SLukas Wunner 
5523bd7f658SLukas Wunner 	if (likely(!bs->tx_spillover)) {
553b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[0]) += bs->tx_prologue;
554b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])     -= bs->tx_prologue;
5553bd7f658SLukas Wunner 	} else {
556b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])      = 0;
557b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[1]) += 4;
558b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[1])     -= 4;
5593bd7f658SLukas Wunner 	}
5603bd7f658SLukas Wunner }
5613bd7f658SLukas Wunner 
5623bd7f658SLukas Wunner /**
5633bd7f658SLukas Wunner  * bcm2835_spi_undo_prologue() - reconstruct original sglist state
5643bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
5653bd7f658SLukas Wunner  *
5663bd7f658SLukas Wunner  * Undo changes which were made to an SPI transfer's sglist when transmitting
5673bd7f658SLukas Wunner  * the prologue.  This is necessary to ensure the same memory ranges are
5683bd7f658SLukas Wunner  * unmapped that were originally mapped.
5693bd7f658SLukas Wunner  */
bcm2835_spi_undo_prologue(struct bcm2835_spi * bs)5703bd7f658SLukas Wunner static void bcm2835_spi_undo_prologue(struct bcm2835_spi *bs)
5713bd7f658SLukas Wunner {
5723bd7f658SLukas Wunner 	struct spi_transfer *tfr = bs->tfr;
5733bd7f658SLukas Wunner 
5743bd7f658SLukas Wunner 	if (!bs->tx_prologue)
5753bd7f658SLukas Wunner 		return;
5763bd7f658SLukas Wunner 
5773bd7f658SLukas Wunner 	if (bs->rx_prologue) {
578b31a9299SLukas Wunner 		sg_dma_address(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue;
579b31a9299SLukas Wunner 		sg_dma_len(&tfr->rx_sg.sgl[0])     += bs->rx_prologue;
5803bd7f658SLukas Wunner 	}
5813bd7f658SLukas Wunner 
5822b8279aeSLukas Wunner 	if (!bs->tx_buf)
5832b8279aeSLukas Wunner 		goto out;
5842b8279aeSLukas Wunner 
5853bd7f658SLukas Wunner 	if (likely(!bs->tx_spillover)) {
586b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue;
587b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])     += bs->tx_prologue;
5883bd7f658SLukas Wunner 	} else {
589b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])      = bs->tx_prologue - 4;
590b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[1]) -= 4;
591b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[1])     += 4;
5923bd7f658SLukas Wunner 	}
5932b8279aeSLukas Wunner out:
5941513ceeeSLukas Wunner 	bs->tx_prologue = 0;
5953bd7f658SLukas Wunner }
5963bd7f658SLukas Wunner 
5978259bf66SLukas Wunner /**
5988259bf66SLukas Wunner  * bcm2835_spi_dma_rx_done() - callback for DMA RX channel
5998259bf66SLukas Wunner  * @data: SPI host controller
6008259bf66SLukas Wunner  *
6018259bf66SLukas Wunner  * Used for bidirectional and RX-only transfers.
6028259bf66SLukas Wunner  */
bcm2835_spi_dma_rx_done(void * data)6038259bf66SLukas Wunner static void bcm2835_spi_dma_rx_done(void *data)
6043ecd37edSMartin Sperl {
6055f336ea5SLukas Wunner 	struct spi_controller *ctlr = data;
6065f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
6073ecd37edSMartin Sperl 
6082b8279aeSLukas Wunner 	/* terminate tx-dma as we do not have an irq for it
6093ecd37edSMartin Sperl 	 * because when the rx dma will terminate and this callback
6103ecd37edSMartin Sperl 	 * is called the tx-dma must have finished - can't get to this
6113ecd37edSMartin Sperl 	 * situation otherwise...
6123ecd37edSMartin Sperl 	 */
6135f336ea5SLukas Wunner 	dmaengine_terminate_async(ctlr->dma_tx);
6148259bf66SLukas Wunner 	bs->tx_dma_active = false;
6158259bf66SLukas Wunner 	bs->rx_dma_active = false;
6163bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
6173ecd37edSMartin Sperl 
6182b8279aeSLukas Wunner 	/* reset fifo and HW */
619ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
6203ecd37edSMartin Sperl 
6213ecd37edSMartin Sperl 	/* and mark as completed */;
622ccae0b40SVincent Pelletier 	spi_finalize_current_transfer(ctlr);
6233ecd37edSMartin Sperl }
6243ecd37edSMartin Sperl 
6258259bf66SLukas Wunner /**
6268259bf66SLukas Wunner  * bcm2835_spi_dma_tx_done() - callback for DMA TX channel
6278259bf66SLukas Wunner  * @data: SPI host controller
6288259bf66SLukas Wunner  *
6298259bf66SLukas Wunner  * Used for TX-only transfers.
6308259bf66SLukas Wunner  */
bcm2835_spi_dma_tx_done(void * data)6318259bf66SLukas Wunner static void bcm2835_spi_dma_tx_done(void *data)
6328259bf66SLukas Wunner {
6338259bf66SLukas Wunner 	struct spi_controller *ctlr = data;
6348259bf66SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
6358259bf66SLukas Wunner 
6368259bf66SLukas Wunner 	/* busy-wait for TX FIFO to empty */
6378259bf66SLukas Wunner 	while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE))
638ec679bdaSLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, bs->target->clear_rx_cs);
6398259bf66SLukas Wunner 
6408259bf66SLukas Wunner 	bs->tx_dma_active = false;
6418259bf66SLukas Wunner 	smp_wmb();
6428259bf66SLukas Wunner 
6438259bf66SLukas Wunner 	/*
6448259bf66SLukas Wunner 	 * In case of a very short transfer, RX DMA may not have been
6458259bf66SLukas Wunner 	 * issued yet.  The onus is then on bcm2835_spi_transfer_one_dma()
6468259bf66SLukas Wunner 	 * to terminate it immediately after issuing.
6478259bf66SLukas Wunner 	 */
6488259bf66SLukas Wunner 	if (cmpxchg(&bs->rx_dma_active, true, false))
6498259bf66SLukas Wunner 		dmaengine_terminate_async(ctlr->dma_rx);
6508259bf66SLukas Wunner 
6518259bf66SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
652ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
653ccae0b40SVincent Pelletier 	spi_finalize_current_transfer(ctlr);
6548259bf66SLukas Wunner }
6558259bf66SLukas Wunner 
6568259bf66SLukas Wunner /**
6578259bf66SLukas Wunner  * bcm2835_spi_prepare_sg() - prepare and submit DMA descriptor for sglist
6588259bf66SLukas Wunner  * @ctlr: SPI host controller
6598259bf66SLukas Wunner  * @tfr: SPI transfer
6608259bf66SLukas Wunner  * @bs: BCM2835 SPI controller
661ec679bdaSLukas Wunner  * @target: BCM2835 SPI target
6628259bf66SLukas Wunner  * @is_tx: whether to submit DMA descriptor for TX or RX sglist
6638259bf66SLukas Wunner  *
6648259bf66SLukas Wunner  * Prepare and submit a DMA descriptor for the TX or RX sglist of @tfr.
6658259bf66SLukas Wunner  * Return 0 on success or a negative error number.
6668259bf66SLukas Wunner  */
bcm2835_spi_prepare_sg(struct spi_controller * ctlr,struct spi_transfer * tfr,struct bcm2835_spi * bs,struct bcm2835_spidev * target,bool is_tx)6675f336ea5SLukas Wunner static int bcm2835_spi_prepare_sg(struct spi_controller *ctlr,
6683ecd37edSMartin Sperl 				  struct spi_transfer *tfr,
6698259bf66SLukas Wunner 				  struct bcm2835_spi *bs,
670ec679bdaSLukas Wunner 				  struct bcm2835_spidev *target,
6713ecd37edSMartin Sperl 				  bool is_tx)
6723ecd37edSMartin Sperl {
6733ecd37edSMartin Sperl 	struct dma_chan *chan;
6743ecd37edSMartin Sperl 	struct scatterlist *sgl;
6753ecd37edSMartin Sperl 	unsigned int nents;
6763ecd37edSMartin Sperl 	enum dma_transfer_direction dir;
6773ecd37edSMartin Sperl 	unsigned long flags;
6783ecd37edSMartin Sperl 
6793ecd37edSMartin Sperl 	struct dma_async_tx_descriptor *desc;
6803ecd37edSMartin Sperl 	dma_cookie_t cookie;
6813ecd37edSMartin Sperl 
6823ecd37edSMartin Sperl 	if (is_tx) {
6833ecd37edSMartin Sperl 		dir   = DMA_MEM_TO_DEV;
6845f336ea5SLukas Wunner 		chan  = ctlr->dma_tx;
6853ecd37edSMartin Sperl 		nents = tfr->tx_sg.nents;
6863ecd37edSMartin Sperl 		sgl   = tfr->tx_sg.sgl;
6878259bf66SLukas Wunner 		flags = tfr->rx_buf ? 0 : DMA_PREP_INTERRUPT;
6883ecd37edSMartin Sperl 	} else {
6893ecd37edSMartin Sperl 		dir   = DMA_DEV_TO_MEM;
6905f336ea5SLukas Wunner 		chan  = ctlr->dma_rx;
6913ecd37edSMartin Sperl 		nents = tfr->rx_sg.nents;
6923ecd37edSMartin Sperl 		sgl   = tfr->rx_sg.sgl;
6933ecd37edSMartin Sperl 		flags = DMA_PREP_INTERRUPT;
6943ecd37edSMartin Sperl 	}
6953ecd37edSMartin Sperl 	/* prepare the channel */
6963ecd37edSMartin Sperl 	desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
6973ecd37edSMartin Sperl 	if (!desc)
6983ecd37edSMartin Sperl 		return -EINVAL;
6993ecd37edSMartin Sperl 
7008259bf66SLukas Wunner 	/*
7018259bf66SLukas Wunner 	 * Completion is signaled by the RX channel for bidirectional and
7028259bf66SLukas Wunner 	 * RX-only transfers; else by the TX channel for TX-only transfers.
7038259bf66SLukas Wunner 	 */
7043ecd37edSMartin Sperl 	if (!is_tx) {
7058259bf66SLukas Wunner 		desc->callback = bcm2835_spi_dma_rx_done;
7065f336ea5SLukas Wunner 		desc->callback_param = ctlr;
7078259bf66SLukas Wunner 	} else if (!tfr->rx_buf) {
7088259bf66SLukas Wunner 		desc->callback = bcm2835_spi_dma_tx_done;
7098259bf66SLukas Wunner 		desc->callback_param = ctlr;
710ec679bdaSLukas Wunner 		bs->target = target;
7113ecd37edSMartin Sperl 	}
7123ecd37edSMartin Sperl 
7133ecd37edSMartin Sperl 	/* submit it to DMA-engine */
7143ecd37edSMartin Sperl 	cookie = dmaengine_submit(desc);
7153ecd37edSMartin Sperl 
7163ecd37edSMartin Sperl 	return dma_submit_error(cookie);
7173ecd37edSMartin Sperl }
7183ecd37edSMartin Sperl 
7198259bf66SLukas Wunner /**
7208259bf66SLukas Wunner  * bcm2835_spi_transfer_one_dma() - perform SPI transfer using DMA engine
7218259bf66SLukas Wunner  * @ctlr: SPI host controller
7228259bf66SLukas Wunner  * @tfr: SPI transfer
723ec679bdaSLukas Wunner  * @target: BCM2835 SPI target
7248259bf66SLukas Wunner  * @cs: CS register
7258259bf66SLukas Wunner  *
7268259bf66SLukas Wunner  * For *bidirectional* transfers (both tx_buf and rx_buf are non-%NULL), set up
7278259bf66SLukas Wunner  * the TX and RX DMA channel to copy between memory and FIFO register.
7288259bf66SLukas Wunner  *
7298259bf66SLukas Wunner  * For *TX-only* transfers (rx_buf is %NULL), copying the RX FIFO's contents to
7308259bf66SLukas Wunner  * memory is pointless.  However not reading the RX FIFO isn't an option either
7318259bf66SLukas Wunner  * because transmission is halted once it's full.  As a workaround, cyclically
7328259bf66SLukas Wunner  * clear the RX FIFO by setting the CLEAR_RX bit in the CS register.
7338259bf66SLukas Wunner  *
7348259bf66SLukas Wunner  * The CS register value is precalculated in bcm2835_spi_setup().  Normally
7358259bf66SLukas Wunner  * this is called only once, on target registration.  A DMA descriptor to write
7368259bf66SLukas Wunner  * this value is preallocated in bcm2835_dma_init().  All that's left to do
7378259bf66SLukas Wunner  * when performing a TX-only transfer is to submit this descriptor to the RX
7388259bf66SLukas Wunner  * DMA channel.  Latency is thereby minimized.  The descriptor does not
7398259bf66SLukas Wunner  * generate any interrupts while running.  It must be terminated once the
7408259bf66SLukas Wunner  * TX DMA channel is done.
7418259bf66SLukas Wunner  *
7428259bf66SLukas Wunner  * Clearing the RX FIFO is paced by the DREQ signal.  The signal is asserted
7438259bf66SLukas Wunner  * when the RX FIFO becomes half full, i.e. 32 bytes.  (Tuneable with the DC
7448259bf66SLukas Wunner  * register.)  Reading 32 bytes from the RX FIFO would normally require 8 bus
7458259bf66SLukas Wunner  * accesses, whereas clearing it requires only 1 bus access.  So an 8-fold
7468259bf66SLukas Wunner  * reduction in bus traffic and thus energy consumption is achieved.
7472b8279aeSLukas Wunner  *
7482b8279aeSLukas Wunner  * For *RX-only* transfers (tx_buf is %NULL), fill the TX FIFO by cyclically
7492b8279aeSLukas Wunner  * copying from the zero page.  The DMA descriptor to do this is preallocated
7502b8279aeSLukas Wunner  * in bcm2835_dma_init().  It must be terminated once the RX DMA channel is
7512b8279aeSLukas Wunner  * done and can then be reused.
7522b8279aeSLukas Wunner  *
7532b8279aeSLukas Wunner  * The BCM2835 DMA driver autodetects when a transaction copies from the zero
7542b8279aeSLukas Wunner  * page and utilizes the DMA controller's ability to synthesize zeroes instead
7552b8279aeSLukas Wunner  * of copying them from memory.  This reduces traffic on the memory bus.  The
7562b8279aeSLukas Wunner  * feature is not available on so-called "lite" channels, but normally TX DMA
7572b8279aeSLukas Wunner  * is backed by a full-featured channel.
7582b8279aeSLukas Wunner  *
7592b8279aeSLukas Wunner  * Zero-filling the TX FIFO is paced by the DREQ signal.  Unfortunately the
7602b8279aeSLukas Wunner  * BCM2835 SPI controller continues to assert DREQ even after the DLEN register
7612b8279aeSLukas Wunner  * has been counted down to zero (hardware erratum).  Thus, when the transfer
7622b8279aeSLukas Wunner  * has finished, the DMA engine zero-fills the TX FIFO until it is half full.
7632b8279aeSLukas Wunner  * (Tuneable with the DC register.)  So up to 9 gratuitous bus accesses are
7642b8279aeSLukas Wunner  * performed at the end of an RX-only transfer.
7658259bf66SLukas Wunner  */
bcm2835_spi_transfer_one_dma(struct spi_controller * ctlr,struct spi_transfer * tfr,struct bcm2835_spidev * target,u32 cs)7665f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_dma(struct spi_controller *ctlr,
7673ecd37edSMartin Sperl 					struct spi_transfer *tfr,
768ec679bdaSLukas Wunner 					struct bcm2835_spidev *target,
7693ecd37edSMartin Sperl 					u32 cs)
7703ecd37edSMartin Sperl {
7715f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
7728259bf66SLukas Wunner 	dma_cookie_t cookie;
7733ecd37edSMartin Sperl 	int ret;
7743ecd37edSMartin Sperl 
775154f7da5SMartin Sperl 	/* update usage statistics */
776154f7da5SMartin Sperl 	bs->count_transfer_dma++;
777154f7da5SMartin Sperl 
7783bd7f658SLukas Wunner 	/*
7793bd7f658SLukas Wunner 	 * Transfer first few bytes without DMA if length of first TX or RX
7803bd7f658SLukas Wunner 	 * sglist entry is not a multiple of 4 bytes (hardware limitation).
7813bd7f658SLukas Wunner 	 */
7825f336ea5SLukas Wunner 	bcm2835_spi_transfer_prologue(ctlr, tfr, bs, cs);
7833ecd37edSMartin Sperl 
7843ecd37edSMartin Sperl 	/* setup tx-DMA */
7852b8279aeSLukas Wunner 	if (bs->tx_buf) {
786ec679bdaSLukas Wunner 		ret = bcm2835_spi_prepare_sg(ctlr, tfr, bs, target, true);
7872b8279aeSLukas Wunner 	} else {
7882b8279aeSLukas Wunner 		cookie = dmaengine_submit(bs->fill_tx_desc);
7892b8279aeSLukas Wunner 		ret = dma_submit_error(cookie);
7902b8279aeSLukas Wunner 	}
7913ecd37edSMartin Sperl 	if (ret)
7923bd7f658SLukas Wunner 		goto err_reset_hw;
7933ecd37edSMartin Sperl 
7943ecd37edSMartin Sperl 	/* set the DMA length */
7953bd7f658SLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->tx_len);
7963ecd37edSMartin Sperl 
7973ecd37edSMartin Sperl 	/* start the HW */
7983ecd37edSMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS,
7993ecd37edSMartin Sperl 		   cs | BCM2835_SPI_CS_TA | BCM2835_SPI_CS_DMAEN);
8003ecd37edSMartin Sperl 
8018259bf66SLukas Wunner 	bs->tx_dma_active = true;
8028259bf66SLukas Wunner 	smp_wmb();
8038259bf66SLukas Wunner 
8048259bf66SLukas Wunner 	/* start TX early */
8058259bf66SLukas Wunner 	dma_async_issue_pending(ctlr->dma_tx);
8068259bf66SLukas Wunner 
8073ecd37edSMartin Sperl 	/* setup rx-DMA late - to run transfers while
8083ecd37edSMartin Sperl 	 * mapping of the rx buffers still takes place
8093ecd37edSMartin Sperl 	 * this saves 10us or more.
8103ecd37edSMartin Sperl 	 */
8118259bf66SLukas Wunner 	if (bs->rx_buf) {
812ec679bdaSLukas Wunner 		ret = bcm2835_spi_prepare_sg(ctlr, tfr, bs, target, false);
8138259bf66SLukas Wunner 	} else {
814ec679bdaSLukas Wunner 		cookie = dmaengine_submit(target->clear_rx_desc);
8158259bf66SLukas Wunner 		ret = dma_submit_error(cookie);
8168259bf66SLukas Wunner 	}
8173ecd37edSMartin Sperl 	if (ret) {
8183ecd37edSMartin Sperl 		/* need to reset on errors */
8195f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_tx);
8208259bf66SLukas Wunner 		bs->tx_dma_active = false;
8213bd7f658SLukas Wunner 		goto err_reset_hw;
8223ecd37edSMartin Sperl 	}
8233ecd37edSMartin Sperl 
8243ecd37edSMartin Sperl 	/* start rx dma late */
8255f336ea5SLukas Wunner 	dma_async_issue_pending(ctlr->dma_rx);
8268259bf66SLukas Wunner 	bs->rx_dma_active = true;
8278259bf66SLukas Wunner 	smp_mb();
8288259bf66SLukas Wunner 
8298259bf66SLukas Wunner 	/*
8308259bf66SLukas Wunner 	 * In case of a very short TX-only transfer, bcm2835_spi_dma_tx_done()
8318259bf66SLukas Wunner 	 * may run before RX DMA is issued.  Terminate RX DMA if so.
8328259bf66SLukas Wunner 	 */
8338259bf66SLukas Wunner 	if (!bs->rx_buf && !bs->tx_dma_active &&
8348259bf66SLukas Wunner 	    cmpxchg(&bs->rx_dma_active, true, false)) {
8358259bf66SLukas Wunner 		dmaengine_terminate_async(ctlr->dma_rx);
836ac4648b5SRobin Murphy 		bcm2835_spi_reset_hw(bs);
8378259bf66SLukas Wunner 	}
8383ecd37edSMartin Sperl 
8393ecd37edSMartin Sperl 	/* wait for wakeup in framework */
8403ecd37edSMartin Sperl 	return 1;
8413bd7f658SLukas Wunner 
8423bd7f658SLukas Wunner err_reset_hw:
843ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
8443bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
8453bd7f658SLukas Wunner 	return ret;
8463ecd37edSMartin Sperl }
8473ecd37edSMartin Sperl 
bcm2835_spi_can_dma(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * tfr)8485f336ea5SLukas Wunner static bool bcm2835_spi_can_dma(struct spi_controller *ctlr,
8493ecd37edSMartin Sperl 				struct spi_device *spi,
8503ecd37edSMartin Sperl 				struct spi_transfer *tfr)
8513ecd37edSMartin Sperl {
8523ecd37edSMartin Sperl 	/* we start DMA efforts only on bigger transfers */
8533ecd37edSMartin Sperl 	if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
8543ecd37edSMartin Sperl 		return false;
8553ecd37edSMartin Sperl 
8563ecd37edSMartin Sperl 	/* return OK */
8573ecd37edSMartin Sperl 	return true;
8583ecd37edSMartin Sperl }
8593ecd37edSMartin Sperl 
bcm2835_dma_release(struct spi_controller * ctlr,struct bcm2835_spi * bs)8608259bf66SLukas Wunner static void bcm2835_dma_release(struct spi_controller *ctlr,
8618259bf66SLukas Wunner 				struct bcm2835_spi *bs)
8623ecd37edSMartin Sperl {
8635f336ea5SLukas Wunner 	if (ctlr->dma_tx) {
8645f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_tx);
8652b8279aeSLukas Wunner 
8662b8279aeSLukas Wunner 		if (bs->fill_tx_desc)
8672b8279aeSLukas Wunner 			dmaengine_desc_free(bs->fill_tx_desc);
8682b8279aeSLukas Wunner 
8692b8279aeSLukas Wunner 		if (bs->fill_tx_addr)
8702b8279aeSLukas Wunner 			dma_unmap_page_attrs(ctlr->dma_tx->device->dev,
8712b8279aeSLukas Wunner 					     bs->fill_tx_addr, sizeof(u32),
8722b8279aeSLukas Wunner 					     DMA_TO_DEVICE,
8732b8279aeSLukas Wunner 					     DMA_ATTR_SKIP_CPU_SYNC);
8742b8279aeSLukas Wunner 
8755f336ea5SLukas Wunner 		dma_release_channel(ctlr->dma_tx);
8765f336ea5SLukas Wunner 		ctlr->dma_tx = NULL;
8773ecd37edSMartin Sperl 	}
8788259bf66SLukas Wunner 
8795f336ea5SLukas Wunner 	if (ctlr->dma_rx) {
8805f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_rx);
8815f336ea5SLukas Wunner 		dma_release_channel(ctlr->dma_rx);
8825f336ea5SLukas Wunner 		ctlr->dma_rx = NULL;
8833ecd37edSMartin Sperl 	}
8843ecd37edSMartin Sperl }
8853ecd37edSMartin Sperl 
bcm2835_dma_init(struct spi_controller * ctlr,struct device * dev,struct bcm2835_spi * bs)8866133fed0SPeter Ujfalusi static int bcm2835_dma_init(struct spi_controller *ctlr, struct device *dev,
8878259bf66SLukas Wunner 			    struct bcm2835_spi *bs)
8883ecd37edSMartin Sperl {
8893ecd37edSMartin Sperl 	struct dma_slave_config slave_config;
8903ecd37edSMartin Sperl 	const __be32 *addr;
8913ecd37edSMartin Sperl 	dma_addr_t dma_reg_base;
892ec679bdaSLukas Wunner 	int ret;
8933ecd37edSMartin Sperl 
8943ecd37edSMartin Sperl 	/* base address in dma-space */
8955f336ea5SLukas Wunner 	addr = of_get_address(ctlr->dev.of_node, 0, NULL, NULL);
8963ecd37edSMartin Sperl 	if (!addr) {
8973ecd37edSMartin Sperl 		dev_err(dev, "could not get DMA-register address - not using dma mode\n");
8986133fed0SPeter Ujfalusi 		/* Fall back to interrupt mode */
8996133fed0SPeter Ujfalusi 		return 0;
9003ecd37edSMartin Sperl 	}
9013ecd37edSMartin Sperl 	dma_reg_base = be32_to_cpup(addr);
9023ecd37edSMartin Sperl 
9033ecd37edSMartin Sperl 	/* get tx/rx dma */
9046133fed0SPeter Ujfalusi 	ctlr->dma_tx = dma_request_chan(dev, "tx");
9056133fed0SPeter Ujfalusi 	if (IS_ERR(ctlr->dma_tx)) {
906893aa09eSWang Ming 		ret = dev_err_probe(dev, PTR_ERR(ctlr->dma_tx),
907893aa09eSWang Ming 			"no tx-dma configuration found - not using dma mode\n");
9086133fed0SPeter Ujfalusi 		ctlr->dma_tx = NULL;
9093ecd37edSMartin Sperl 		goto err;
9103ecd37edSMartin Sperl 	}
9116133fed0SPeter Ujfalusi 	ctlr->dma_rx = dma_request_chan(dev, "rx");
9126133fed0SPeter Ujfalusi 	if (IS_ERR(ctlr->dma_rx)) {
913893aa09eSWang Ming 		ret = dev_err_probe(dev, PTR_ERR(ctlr->dma_rx),
914893aa09eSWang Ming 			"no rx-dma configuration found - not using dma mode\n");
9156133fed0SPeter Ujfalusi 		ctlr->dma_rx = NULL;
9163ecd37edSMartin Sperl 		goto err_release;
9173ecd37edSMartin Sperl 	}
9183ecd37edSMartin Sperl 
9192b8279aeSLukas Wunner 	/*
9202b8279aeSLukas Wunner 	 * The TX DMA channel either copies a transfer's TX buffer to the FIFO
9212b8279aeSLukas Wunner 	 * or, in case of an RX-only transfer, cyclically copies from the zero
9222b8279aeSLukas Wunner 	 * page to the FIFO using a preallocated, reusable descriptor.
9232b8279aeSLukas Wunner 	 */
9243ecd37edSMartin Sperl 	slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
9253ecd37edSMartin Sperl 	slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9263ecd37edSMartin Sperl 
9275f336ea5SLukas Wunner 	ret = dmaengine_slave_config(ctlr->dma_tx, &slave_config);
9283ecd37edSMartin Sperl 	if (ret)
9293ecd37edSMartin Sperl 		goto err_config;
9303ecd37edSMartin Sperl 
9312b8279aeSLukas Wunner 	bs->fill_tx_addr = dma_map_page_attrs(ctlr->dma_tx->device->dev,
9322b8279aeSLukas Wunner 					      ZERO_PAGE(0), 0, sizeof(u32),
9332b8279aeSLukas Wunner 					      DMA_TO_DEVICE,
9342b8279aeSLukas Wunner 					      DMA_ATTR_SKIP_CPU_SYNC);
9352b8279aeSLukas Wunner 	if (dma_mapping_error(ctlr->dma_tx->device->dev, bs->fill_tx_addr)) {
9362b8279aeSLukas Wunner 		dev_err(dev, "cannot map zero page - not using DMA mode\n");
9372b8279aeSLukas Wunner 		bs->fill_tx_addr = 0;
938dd4441abSWei Yongjun 		ret = -ENOMEM;
9392b8279aeSLukas Wunner 		goto err_release;
9402b8279aeSLukas Wunner 	}
9412b8279aeSLukas Wunner 
9422b8279aeSLukas Wunner 	bs->fill_tx_desc = dmaengine_prep_dma_cyclic(ctlr->dma_tx,
9432b8279aeSLukas Wunner 						     bs->fill_tx_addr,
9442b8279aeSLukas Wunner 						     sizeof(u32), 0,
9452b8279aeSLukas Wunner 						     DMA_MEM_TO_DEV, 0);
9462b8279aeSLukas Wunner 	if (!bs->fill_tx_desc) {
9472b8279aeSLukas Wunner 		dev_err(dev, "cannot prepare fill_tx_desc - not using DMA mode\n");
948dd4441abSWei Yongjun 		ret = -ENOMEM;
9492b8279aeSLukas Wunner 		goto err_release;
9502b8279aeSLukas Wunner 	}
9512b8279aeSLukas Wunner 
9522b8279aeSLukas Wunner 	ret = dmaengine_desc_set_reuse(bs->fill_tx_desc);
9532b8279aeSLukas Wunner 	if (ret) {
9542b8279aeSLukas Wunner 		dev_err(dev, "cannot reuse fill_tx_desc - not using DMA mode\n");
9552b8279aeSLukas Wunner 		goto err_release;
9562b8279aeSLukas Wunner 	}
9572b8279aeSLukas Wunner 
9588259bf66SLukas Wunner 	/*
9598259bf66SLukas Wunner 	 * The RX DMA channel is used bidirectionally:  It either reads the
9608259bf66SLukas Wunner 	 * RX FIFO or, in case of a TX-only transfer, cyclically writes a
9618259bf66SLukas Wunner 	 * precalculated value to the CS register to clear the RX FIFO.
9628259bf66SLukas Wunner 	 */
9633ecd37edSMartin Sperl 	slave_config.src_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
9643ecd37edSMartin Sperl 	slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9658259bf66SLukas Wunner 	slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_CS);
9668259bf66SLukas Wunner 	slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9673ecd37edSMartin Sperl 
9685f336ea5SLukas Wunner 	ret = dmaengine_slave_config(ctlr->dma_rx, &slave_config);
9693ecd37edSMartin Sperl 	if (ret)
9703ecd37edSMartin Sperl 		goto err_config;
9713ecd37edSMartin Sperl 
9723ecd37edSMartin Sperl 	/* all went well, so set can_dma */
9735f336ea5SLukas Wunner 	ctlr->can_dma = bcm2835_spi_can_dma;
9743ecd37edSMartin Sperl 
9756133fed0SPeter Ujfalusi 	return 0;
9763ecd37edSMartin Sperl 
9773ecd37edSMartin Sperl err_config:
9783ecd37edSMartin Sperl 	dev_err(dev, "issue configuring dma: %d - not using DMA mode\n",
9793ecd37edSMartin Sperl 		ret);
9803ecd37edSMartin Sperl err_release:
9818259bf66SLukas Wunner 	bcm2835_dma_release(ctlr, bs);
9823ecd37edSMartin Sperl err:
9836133fed0SPeter Ujfalusi 	/*
9846133fed0SPeter Ujfalusi 	 * Only report error for deferred probing, otherwise fall back to
9856133fed0SPeter Ujfalusi 	 * interrupt mode
9866133fed0SPeter Ujfalusi 	 */
9876133fed0SPeter Ujfalusi 	if (ret != -EPROBE_DEFER)
9886133fed0SPeter Ujfalusi 		ret = 0;
9896133fed0SPeter Ujfalusi 
9906133fed0SPeter Ujfalusi 	return ret;
9913ecd37edSMartin Sperl }
9923ecd37edSMartin Sperl 
bcm2835_spi_transfer_one_poll(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * tfr,u32 cs)9935f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_poll(struct spi_controller *ctlr,
994a750b124SMartin Sperl 					 struct spi_device *spi,
995a750b124SMartin Sperl 					 struct spi_transfer *tfr,
9969ac3f90dSMartin Sperl 					 u32 cs)
997a750b124SMartin Sperl {
9985f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
999a750b124SMartin Sperl 	unsigned long timeout;
1000a750b124SMartin Sperl 
1001154f7da5SMartin Sperl 	/* update usage statistics */
1002154f7da5SMartin Sperl 	bs->count_transfer_polling++;
1003154f7da5SMartin Sperl 
1004a750b124SMartin Sperl 	/* enable HW block without interrupts */
1005a750b124SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
1006a750b124SMartin Sperl 
1007a750b124SMartin Sperl 	/* fill in the fifo before timeout calculations
1008a750b124SMartin Sperl 	 * if we are interrupted here, then the data is
1009a750b124SMartin Sperl 	 * getting transferred by the HW while we are interrupted
1010a750b124SMartin Sperl 	 */
10112e0733bcSLukas Wunner 	bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
1012a750b124SMartin Sperl 
1013ff245d90SMartin Sperl 	/* set the timeout to at least 2 jiffies */
1014ff245d90SMartin Sperl 	timeout = jiffies + 2 + HZ * polling_limit_us / 1000000;
1015a750b124SMartin Sperl 
1016a750b124SMartin Sperl 	/* loop until finished the transfer */
1017a750b124SMartin Sperl 	while (bs->rx_len) {
1018a750b124SMartin Sperl 		/* fill in tx fifo with remaining data */
1019a750b124SMartin Sperl 		bcm2835_wr_fifo(bs);
1020a750b124SMartin Sperl 
1021a750b124SMartin Sperl 		/* read from fifo as much as possible */
1022a750b124SMartin Sperl 		bcm2835_rd_fifo(bs);
1023a750b124SMartin Sperl 
1024a750b124SMartin Sperl 		/* if there is still data pending to read
1025a750b124SMartin Sperl 		 * then check the timeout
1026a750b124SMartin Sperl 		 */
1027a750b124SMartin Sperl 		if (bs->rx_len && time_after(jiffies, timeout)) {
1028a750b124SMartin Sperl 			dev_dbg_ratelimited(&spi->dev,
1029a750b124SMartin Sperl 					    "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
1030a750b124SMartin Sperl 					    jiffies - timeout,
1031a750b124SMartin Sperl 					    bs->tx_len, bs->rx_len);
1032a750b124SMartin Sperl 			/* fall back to interrupt mode */
1033154f7da5SMartin Sperl 
1034154f7da5SMartin Sperl 			/* update usage statistics */
1035154f7da5SMartin Sperl 			bs->count_transfer_irq_after_polling++;
1036154f7da5SMartin Sperl 
10375f336ea5SLukas Wunner 			return bcm2835_spi_transfer_one_irq(ctlr, spi,
10382e0733bcSLukas Wunner 							    tfr, cs, false);
1039a750b124SMartin Sperl 		}
1040a750b124SMartin Sperl 	}
1041a750b124SMartin Sperl 
1042a750b124SMartin Sperl 	/* Transfer complete - reset SPI HW */
1043ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
1044a750b124SMartin Sperl 	/* and return without waiting for completion */
1045a750b124SMartin Sperl 	return 0;
1046a750b124SMartin Sperl }
1047a750b124SMartin Sperl 
bcm2835_spi_transfer_one(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * tfr)10485f336ea5SLukas Wunner static int bcm2835_spi_transfer_one(struct spi_controller *ctlr,
1049704f32d4SMartin Sperl 				    struct spi_device *spi,
1050704f32d4SMartin Sperl 				    struct spi_transfer *tfr)
1051704f32d4SMartin Sperl {
10525f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1053ec679bdaSLukas Wunner 	struct bcm2835_spidev *target = spi_get_ctldata(spi);
1054c45c1e82SAlexandru Tachici 	unsigned long spi_hz, cdiv;
1055ff245d90SMartin Sperl 	unsigned long hz_per_byte, byte_limit;
1056ec679bdaSLukas Wunner 	u32 cs = target->prepare_cs;
1057704f32d4SMartin Sperl 
1058704f32d4SMartin Sperl 	/* set clock */
1059704f32d4SMartin Sperl 	spi_hz = tfr->speed_hz;
1060704f32d4SMartin Sperl 
1061c45c1e82SAlexandru Tachici 	if (spi_hz >= bs->clk_hz / 2) {
1062704f32d4SMartin Sperl 		cdiv = 2; /* clk_hz/2 is the fastest we can go */
1063704f32d4SMartin Sperl 	} else if (spi_hz) {
1064704f32d4SMartin Sperl 		/* CDIV must be a multiple of two */
1065c45c1e82SAlexandru Tachici 		cdiv = DIV_ROUND_UP(bs->clk_hz, spi_hz);
1066704f32d4SMartin Sperl 		cdiv += (cdiv % 2);
1067704f32d4SMartin Sperl 
1068704f32d4SMartin Sperl 		if (cdiv >= 65536)
1069704f32d4SMartin Sperl 			cdiv = 0; /* 0 is the slowest we can go */
1070704f32d4SMartin Sperl 	} else {
1071704f32d4SMartin Sperl 		cdiv = 0; /* 0 is the slowest we can go */
1072704f32d4SMartin Sperl 	}
1073c45c1e82SAlexandru Tachici 	tfr->effective_speed_hz = cdiv ? (bs->clk_hz / cdiv) : (bs->clk_hz / 65536);
1074704f32d4SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
1075704f32d4SMartin Sperl 
1076acace73dSMartin Sperl 	/* handle all the 3-wire mode */
10778259bf66SLukas Wunner 	if (spi->mode & SPI_3WIRE && tfr->rx_buf)
1078704f32d4SMartin Sperl 		cs |= BCM2835_SPI_CS_REN;
1079704f32d4SMartin Sperl 
1080704f32d4SMartin Sperl 	/* set transmit buffers and length */
1081704f32d4SMartin Sperl 	bs->tx_buf = tfr->tx_buf;
1082704f32d4SMartin Sperl 	bs->rx_buf = tfr->rx_buf;
1083704f32d4SMartin Sperl 	bs->tx_len = tfr->len;
1084704f32d4SMartin Sperl 	bs->rx_len = tfr->len;
1085704f32d4SMartin Sperl 
10867f1922ebSMartin Sperl 	/* Calculate the estimated time in us the transfer runs.  Note that
10877f1922ebSMartin Sperl 	 * there is 1 idle clocks cycles after each byte getting transferred
10887f1922ebSMartin Sperl 	 * so we have 9 cycles/byte.  This is used to find the number of Hz
10897f1922ebSMartin Sperl 	 * per byte per polling limit.  E.g., we can transfer 1 byte in 30 us
10907f1922ebSMartin Sperl 	 * per 300,000 Hz of bus clock.
10917f1922ebSMartin Sperl 	 */
1092ff245d90SMartin Sperl 	hz_per_byte = polling_limit_us ? (9 * 1000000) / polling_limit_us : 0;
10939df2003dSMartin Sperl 	byte_limit = hz_per_byte ? tfr->effective_speed_hz / hz_per_byte : 1;
1094ff245d90SMartin Sperl 
10957f1922ebSMartin Sperl 	/* run in polling mode for short transfers */
1096ff245d90SMartin Sperl 	if (tfr->len < byte_limit)
10975f336ea5SLukas Wunner 		return bcm2835_spi_transfer_one_poll(ctlr, spi, tfr, cs);
1098704f32d4SMartin Sperl 
1099c41d62b0SMartin Sperl 	/* run in dma mode if conditions are right
1100c41d62b0SMartin Sperl 	 * Note that unlike poll or interrupt mode DMA mode does not have
1101c41d62b0SMartin Sperl 	 * this 1 idle clock cycle pattern but runs the spi clock without gaps
1102c41d62b0SMartin Sperl 	 */
11035f336ea5SLukas Wunner 	if (ctlr->can_dma && bcm2835_spi_can_dma(ctlr, spi, tfr))
1104ec679bdaSLukas Wunner 		return bcm2835_spi_transfer_one_dma(ctlr, tfr, target, cs);
11053ecd37edSMartin Sperl 
11063ecd37edSMartin Sperl 	/* run in interrupt-mode */
11075f336ea5SLukas Wunner 	return bcm2835_spi_transfer_one_irq(ctlr, spi, tfr, cs, true);
1108704f32d4SMartin Sperl }
1109704f32d4SMartin Sperl 
bcm2835_spi_prepare_message(struct spi_controller * ctlr,struct spi_message * msg)11105f336ea5SLukas Wunner static int bcm2835_spi_prepare_message(struct spi_controller *ctlr,
1111acace73dSMartin Sperl 				       struct spi_message *msg)
1112acace73dSMartin Sperl {
1113acace73dSMartin Sperl 	struct spi_device *spi = msg->spi;
11145f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1115ec679bdaSLukas Wunner 	struct bcm2835_spidev *target = spi_get_ctldata(spi);
11168b7bd10eSMeghana Madhyastha 	int ret;
11178b7bd10eSMeghana Madhyastha 
11185f336ea5SLukas Wunner 	if (ctlr->can_dma) {
11198b7bd10eSMeghana Madhyastha 		/*
11203393f7d9SNicolas Saenz Julienne 		 * DMA transfers are limited to 16 bit (0 to 65535 bytes) by
11213393f7d9SNicolas Saenz Julienne 		 * the SPI HW due to DLEN. Split up transfers (32-bit FIFO
11223393f7d9SNicolas Saenz Julienne 		 * aligned) if the limit is exceeded.
11238b7bd10eSMeghana Madhyastha 		 */
11245f336ea5SLukas Wunner 		ret = spi_split_transfers_maxsize(ctlr, msg, 65532,
11258b7bd10eSMeghana Madhyastha 						  GFP_KERNEL | GFP_DMA);
11268b7bd10eSMeghana Madhyastha 		if (ret)
11278b7bd10eSMeghana Madhyastha 			return ret;
11283393f7d9SNicolas Saenz Julienne 	}
1129acace73dSMartin Sperl 
1130571e31faSLukas Wunner 	/*
1131571e31faSLukas Wunner 	 * Set up clock polarity before spi_transfer_one_message() asserts
1132571e31faSLukas Wunner 	 * chip select to avoid a gratuitous clock signal edge.
1133571e31faSLukas Wunner 	 */
1134ec679bdaSLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_CS, target->prepare_cs);
1135acace73dSMartin Sperl 
1136acace73dSMartin Sperl 	return 0;
1137acace73dSMartin Sperl }
1138acace73dSMartin Sperl 
bcm2835_spi_handle_err(struct spi_controller * ctlr,struct spi_message * msg)11395f336ea5SLukas Wunner static void bcm2835_spi_handle_err(struct spi_controller *ctlr,
1140e34ff011SMartin Sperl 				   struct spi_message *msg)
1141f8043872SChris Boot {
11425f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
11433ecd37edSMartin Sperl 
11443ecd37edSMartin Sperl 	/* if an error occurred and we have an active dma, then terminate */
11454ceaa684SMarc Kleine-Budde 	if (ctlr->dma_tx) {
11465f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_tx);
11478259bf66SLukas Wunner 		bs->tx_dma_active = false;
11484ceaa684SMarc Kleine-Budde 	}
11494ceaa684SMarc Kleine-Budde 	if (ctlr->dma_rx) {
11505f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_rx);
11518259bf66SLukas Wunner 		bs->rx_dma_active = false;
11524ceaa684SMarc Kleine-Budde 	}
11533bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
11541513ceeeSLukas Wunner 
11553ecd37edSMartin Sperl 	/* and reset */
1156ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
1157f8043872SChris Boot }
1158f8043872SChris Boot 
chip_match_name(struct gpio_chip * chip,void * data)1159a30a555dSMartin Sperl static int chip_match_name(struct gpio_chip *chip, void *data)
1160a30a555dSMartin Sperl {
1161a30a555dSMartin Sperl 	return !strcmp(chip->label, data);
1162a30a555dSMartin Sperl }
1163a30a555dSMartin Sperl 
bcm2835_spi_cleanup(struct spi_device * spi)1164ec679bdaSLukas Wunner static void bcm2835_spi_cleanup(struct spi_device *spi)
1165ec679bdaSLukas Wunner {
1166ec679bdaSLukas Wunner 	struct bcm2835_spidev *target = spi_get_ctldata(spi);
1167ec679bdaSLukas Wunner 	struct spi_controller *ctlr = spi->controller;
1168ec679bdaSLukas Wunner 
1169ec679bdaSLukas Wunner 	if (target->clear_rx_desc)
1170ec679bdaSLukas Wunner 		dmaengine_desc_free(target->clear_rx_desc);
1171ec679bdaSLukas Wunner 
1172ec679bdaSLukas Wunner 	if (target->clear_rx_addr)
1173ec679bdaSLukas Wunner 		dma_unmap_single(ctlr->dma_rx->device->dev,
1174ec679bdaSLukas Wunner 				 target->clear_rx_addr,
1175ec679bdaSLukas Wunner 				 sizeof(u32),
1176ec679bdaSLukas Wunner 				 DMA_TO_DEVICE);
1177ec679bdaSLukas Wunner 
1178ec679bdaSLukas Wunner 	kfree(target);
1179ec679bdaSLukas Wunner }
1180ec679bdaSLukas Wunner 
bcm2835_spi_setup_dma(struct spi_controller * ctlr,struct spi_device * spi,struct bcm2835_spi * bs,struct bcm2835_spidev * target)1181ec679bdaSLukas Wunner static int bcm2835_spi_setup_dma(struct spi_controller *ctlr,
1182ec679bdaSLukas Wunner 				 struct spi_device *spi,
1183ec679bdaSLukas Wunner 				 struct bcm2835_spi *bs,
1184ec679bdaSLukas Wunner 				 struct bcm2835_spidev *target)
1185ec679bdaSLukas Wunner {
1186ec679bdaSLukas Wunner 	int ret;
1187ec679bdaSLukas Wunner 
1188ec679bdaSLukas Wunner 	if (!ctlr->dma_rx)
1189ec679bdaSLukas Wunner 		return 0;
1190ec679bdaSLukas Wunner 
1191ec679bdaSLukas Wunner 	target->clear_rx_addr = dma_map_single(ctlr->dma_rx->device->dev,
1192ec679bdaSLukas Wunner 					       &target->clear_rx_cs,
1193ec679bdaSLukas Wunner 					       sizeof(u32),
1194ec679bdaSLukas Wunner 					       DMA_TO_DEVICE);
1195ec679bdaSLukas Wunner 	if (dma_mapping_error(ctlr->dma_rx->device->dev, target->clear_rx_addr)) {
1196ec679bdaSLukas Wunner 		dev_err(&spi->dev, "cannot map clear_rx_cs\n");
1197ec679bdaSLukas Wunner 		target->clear_rx_addr = 0;
1198ec679bdaSLukas Wunner 		return -ENOMEM;
1199ec679bdaSLukas Wunner 	}
1200ec679bdaSLukas Wunner 
1201ec679bdaSLukas Wunner 	target->clear_rx_desc = dmaengine_prep_dma_cyclic(ctlr->dma_rx,
1202ec679bdaSLukas Wunner 						          target->clear_rx_addr,
1203ec679bdaSLukas Wunner 						          sizeof(u32), 0,
1204ec679bdaSLukas Wunner 						          DMA_MEM_TO_DEV, 0);
1205ec679bdaSLukas Wunner 	if (!target->clear_rx_desc) {
1206ec679bdaSLukas Wunner 		dev_err(&spi->dev, "cannot prepare clear_rx_desc\n");
1207ec679bdaSLukas Wunner 		return -ENOMEM;
1208ec679bdaSLukas Wunner 	}
1209ec679bdaSLukas Wunner 
1210ec679bdaSLukas Wunner 	ret = dmaengine_desc_set_reuse(target->clear_rx_desc);
1211ec679bdaSLukas Wunner 	if (ret) {
1212ec679bdaSLukas Wunner 		dev_err(&spi->dev, "cannot reuse clear_rx_desc\n");
1213ec679bdaSLukas Wunner 		return ret;
1214ec679bdaSLukas Wunner 	}
1215ec679bdaSLukas Wunner 
1216ec679bdaSLukas Wunner 	return 0;
1217ec679bdaSLukas Wunner }
1218ec679bdaSLukas Wunner 
bcm2835_spi_setup(struct spi_device * spi)1219e34ff011SMartin Sperl static int bcm2835_spi_setup(struct spi_device *spi)
1220e34ff011SMartin Sperl {
12218259bf66SLukas Wunner 	struct spi_controller *ctlr = spi->controller;
12228259bf66SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1223ec679bdaSLukas Wunner 	struct bcm2835_spidev *target = spi_get_ctldata(spi);
1224a30a555dSMartin Sperl 	struct gpio_chip *chip;
1225ec679bdaSLukas Wunner 	int ret;
1226571e31faSLukas Wunner 	u32 cs;
1227571e31faSLukas Wunner 
1228ec679bdaSLukas Wunner 	if (!target) {
1229ec679bdaSLukas Wunner 		target = kzalloc(ALIGN(sizeof(*target), dma_get_cache_alignment()),
1230ec679bdaSLukas Wunner 			      GFP_KERNEL);
1231ec679bdaSLukas Wunner 		if (!target)
1232ec679bdaSLukas Wunner 			return -ENOMEM;
1233ec679bdaSLukas Wunner 
1234ec679bdaSLukas Wunner 		spi_set_ctldata(spi, target);
1235ec679bdaSLukas Wunner 
1236ec679bdaSLukas Wunner 		ret = bcm2835_spi_setup_dma(ctlr, spi, bs, target);
1237ec679bdaSLukas Wunner 		if (ret)
1238ec679bdaSLukas Wunner 			goto err_cleanup;
123913817d46SLukas Wunner 	}
124013817d46SLukas Wunner 
1241571e31faSLukas Wunner 	/*
1242571e31faSLukas Wunner 	 * Precalculate SPI target's CS register value for ->prepare_message():
1243571e31faSLukas Wunner 	 * The driver always uses software-controlled GPIO chip select, hence
1244571e31faSLukas Wunner 	 * set the hardware-controlled native chip select to an invalid value
1245571e31faSLukas Wunner 	 * to prevent it from interfering.
1246571e31faSLukas Wunner 	 */
1247571e31faSLukas Wunner 	cs = BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
1248571e31faSLukas Wunner 	if (spi->mode & SPI_CPOL)
1249571e31faSLukas Wunner 		cs |= BCM2835_SPI_CS_CPOL;
1250571e31faSLukas Wunner 	if (spi->mode & SPI_CPHA)
1251571e31faSLukas Wunner 		cs |= BCM2835_SPI_CS_CPHA;
1252ec679bdaSLukas Wunner 	target->prepare_cs = cs;
12533bd158c5SLinus Walleij 
1254e34ff011SMartin Sperl 	/*
12558259bf66SLukas Wunner 	 * Precalculate SPI target's CS register value to clear RX FIFO
12568259bf66SLukas Wunner 	 * in case of a TX-only DMA transfer.
12578259bf66SLukas Wunner 	 */
12588259bf66SLukas Wunner 	if (ctlr->dma_rx) {
1259ec679bdaSLukas Wunner 		target->clear_rx_cs = cs | BCM2835_SPI_CS_TA |
12608259bf66SLukas Wunner 					BCM2835_SPI_CS_DMAEN |
12618259bf66SLukas Wunner 					BCM2835_SPI_CS_CLEAR_RX;
12628259bf66SLukas Wunner 		dma_sync_single_for_device(ctlr->dma_rx->device->dev,
1263ec679bdaSLukas Wunner 					   target->clear_rx_addr,
1264ec679bdaSLukas Wunner 					   sizeof(u32),
12658259bf66SLukas Wunner 					   DMA_TO_DEVICE);
12668259bf66SLukas Wunner 	}
12678259bf66SLukas Wunner 
1268e34ff011SMartin Sperl 	/*
1269e34ff011SMartin Sperl 	 * sanity checking the native-chipselects
1270e34ff011SMartin Sperl 	 */
1271e34ff011SMartin Sperl 	if (spi->mode & SPI_NO_CS)
1272f8043872SChris Boot 		return 0;
12733bd158c5SLinus Walleij 	/*
12743bd158c5SLinus Walleij 	 * The SPI core has successfully requested the CS GPIO line from the
12753bd158c5SLinus Walleij 	 * device tree, so we are done.
12763bd158c5SLinus Walleij 	 */
12779e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (spi_get_csgpiod(spi, 0))
1278e34ff011SMartin Sperl 		return 0;
12799e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (spi_get_chipselect(spi, 0) > 1) {
1280a30a555dSMartin Sperl 		/* error in the case of native CS requested with CS > 1
1281a30a555dSMartin Sperl 		 * officially there is a CS2, but it is not documented
1282a30a555dSMartin Sperl 		 * which GPIO is connected with that...
1283a30a555dSMartin Sperl 		 */
1284a30a555dSMartin Sperl 		dev_err(&spi->dev,
1285a30a555dSMartin Sperl 			"setup: only two native chip-selects are supported\n");
1286ec679bdaSLukas Wunner 		ret = -EINVAL;
1287ec679bdaSLukas Wunner 		goto err_cleanup;
1288a30a555dSMartin Sperl 	}
12893bd158c5SLinus Walleij 
12903bd158c5SLinus Walleij 	/*
12913bd158c5SLinus Walleij 	 * Translate native CS to GPIO
12923bd158c5SLinus Walleij 	 *
12933bd158c5SLinus Walleij 	 * FIXME: poking around in the gpiolib internals like this is
12943bd158c5SLinus Walleij 	 * not very good practice. Find a way to locate the real problem
12953bd158c5SLinus Walleij 	 * and fix it. Why is the GPIO descriptor in spi->cs_gpiod
12963bd158c5SLinus Walleij 	 * sometimes not assigned correctly? Erroneous device trees?
12973bd158c5SLinus Walleij 	 */
1298a30a555dSMartin Sperl 
1299a30a555dSMartin Sperl 	/* get the gpio chip for the base */
1300a30a555dSMartin Sperl 	chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
1301a30a555dSMartin Sperl 	if (!chip)
1302e34ff011SMartin Sperl 		return 0;
1303e34ff011SMartin Sperl 
13049e264f3fSAmit Kumar Mahapatra via Alsa-devel 	spi_set_csgpiod(spi, 0, gpiochip_request_own_desc(chip,
13059e264f3fSAmit Kumar Mahapatra via Alsa-devel 							  8 - (spi_get_chipselect(spi, 0)),
13063bd158c5SLinus Walleij 							  DRV_NAME,
1307bc7f2cd7SMartin Hundebøll 							  GPIO_LOOKUP_FLAGS_DEFAULT,
13089e264f3fSAmit Kumar Mahapatra via Alsa-devel 							  GPIOD_OUT_LOW));
13099e264f3fSAmit Kumar Mahapatra via Alsa-devel 	if (IS_ERR(spi_get_csgpiod(spi, 0))) {
13109e264f3fSAmit Kumar Mahapatra via Alsa-devel 		ret = PTR_ERR(spi_get_csgpiod(spi, 0));
1311ec679bdaSLukas Wunner 		goto err_cleanup;
1312ec679bdaSLukas Wunner 	}
1313a30a555dSMartin Sperl 
1314a30a555dSMartin Sperl 	/* and set up the "mode" and level */
13153bd158c5SLinus Walleij 	dev_info(&spi->dev, "setting up native-CS%i to use GPIO\n",
13169e264f3fSAmit Kumar Mahapatra via Alsa-devel 		 spi_get_chipselect(spi, 0));
1317a30a555dSMartin Sperl 
1318a30a555dSMartin Sperl 	return 0;
1319ec679bdaSLukas Wunner 
1320ec679bdaSLukas Wunner err_cleanup:
1321ec679bdaSLukas Wunner 	bcm2835_spi_cleanup(spi);
1322ec679bdaSLukas Wunner 	return ret;
1323f8043872SChris Boot }
1324f8043872SChris Boot 
bcm2835_spi_probe(struct platform_device * pdev)1325f8043872SChris Boot static int bcm2835_spi_probe(struct platform_device *pdev)
1326f8043872SChris Boot {
13275f336ea5SLukas Wunner 	struct spi_controller *ctlr;
1328f8043872SChris Boot 	struct bcm2835_spi *bs;
1329f8043872SChris Boot 	int err;
1330f8043872SChris Boot 
1331ec679bdaSLukas Wunner 	ctlr = devm_spi_alloc_host(&pdev->dev, sizeof(*bs));
13325f336ea5SLukas Wunner 	if (!ctlr)
1333f8043872SChris Boot 		return -ENOMEM;
1334f8043872SChris Boot 
13355f336ea5SLukas Wunner 	platform_set_drvdata(pdev, ctlr);
1336f8043872SChris Boot 
13373bd158c5SLinus Walleij 	ctlr->use_gpio_descriptors = true;
13385f336ea5SLukas Wunner 	ctlr->mode_bits = BCM2835_SPI_MODE_BITS;
13395f336ea5SLukas Wunner 	ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
134013817d46SLukas Wunner 	ctlr->num_chipselect = 3;
13415f336ea5SLukas Wunner 	ctlr->setup = bcm2835_spi_setup;
1342ec679bdaSLukas Wunner 	ctlr->cleanup = bcm2835_spi_cleanup;
13435f336ea5SLukas Wunner 	ctlr->transfer_one = bcm2835_spi_transfer_one;
13445f336ea5SLukas Wunner 	ctlr->handle_err = bcm2835_spi_handle_err;
13455f336ea5SLukas Wunner 	ctlr->prepare_message = bcm2835_spi_prepare_message;
13465f336ea5SLukas Wunner 	ctlr->dev.of_node = pdev->dev.of_node;
1347f8043872SChris Boot 
13485f336ea5SLukas Wunner 	bs = spi_controller_get_devdata(ctlr);
1349afe7e363SRobin Murphy 	bs->ctlr = ctlr;
1350f8043872SChris Boot 
13516ba794dfSYueHaibing 	bs->regs = devm_platform_ioremap_resource(pdev, 0);
1352e1483ac0SLukas Wunner 	if (IS_ERR(bs->regs))
1353e1483ac0SLukas Wunner 		return PTR_ERR(bs->regs);
1354f8043872SChris Boot 
1355f8043872SChris Boot 	bs->clk = devm_clk_get(&pdev->dev, NULL);
1356e1483ac0SLukas Wunner 	if (IS_ERR(bs->clk))
1357e1483ac0SLukas Wunner 		return dev_err_probe(&pdev->dev, PTR_ERR(bs->clk),
135865acd82cSKrzysztof Kozlowski 				     "could not get clk\n");
1359f8043872SChris Boot 
1360c6892892SRichard Fitzgerald 	ctlr->max_speed_hz = clk_get_rate(bs->clk) / 2;
1361c6892892SRichard Fitzgerald 
1362ddf0e1c2SMartin Sperl 	bs->irq = platform_get_irq(pdev, 0);
1363*8102d64cSRuan Jinjie 	if (bs->irq < 0)
1364*8102d64cSRuan Jinjie 		return bs->irq;
1365f8043872SChris Boot 
13661e7dae68SYuanjun Gong 	err = clk_prepare_enable(bs->clk);
13671e7dae68SYuanjun Gong 	if (err)
13681e7dae68SYuanjun Gong 		return err;
1369c45c1e82SAlexandru Tachici 	bs->clk_hz = clk_get_rate(bs->clk);
1370f8043872SChris Boot 
13716133fed0SPeter Ujfalusi 	err = bcm2835_dma_init(ctlr, &pdev->dev, bs);
13726133fed0SPeter Ujfalusi 	if (err)
13736133fed0SPeter Ujfalusi 		goto out_clk_disable;
1374ddf0e1c2SMartin Sperl 
1375ddf0e1c2SMartin Sperl 	/* initialise the hardware with the default polarities */
1376ddf0e1c2SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS,
1377ddf0e1c2SMartin Sperl 		   BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
1378ddf0e1c2SMartin Sperl 
137989fcdd53SMartin Sperl 	err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt,
138089fcdd53SMartin Sperl 			       IRQF_SHARED, dev_name(&pdev->dev), bs);
1381f8043872SChris Boot 	if (err) {
1382f8043872SChris Boot 		dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
1383666224b4SPeter Ujfalusi 		goto out_dma_release;
1384f8043872SChris Boot 	}
1385f8043872SChris Boot 
13869dd277ffSLukas Wunner 	err = spi_register_controller(ctlr);
1387f8043872SChris Boot 	if (err) {
13885f336ea5SLukas Wunner 		dev_err(&pdev->dev, "could not register SPI controller: %d\n",
13895f336ea5SLukas Wunner 			err);
1390666224b4SPeter Ujfalusi 		goto out_dma_release;
1391f8043872SChris Boot 	}
1392f8043872SChris Boot 
1393154f7da5SMartin Sperl 	bcm2835_debugfs_create(bs, dev_name(&pdev->dev));
1394154f7da5SMartin Sperl 
1395f8043872SChris Boot 	return 0;
1396f8043872SChris Boot 
1397666224b4SPeter Ujfalusi out_dma_release:
1398666224b4SPeter Ujfalusi 	bcm2835_dma_release(ctlr, bs);
1399f8043872SChris Boot out_clk_disable:
1400f8043872SChris Boot 	clk_disable_unprepare(bs->clk);
1401f8043872SChris Boot 	return err;
1402f8043872SChris Boot }
1403f8043872SChris Boot 
bcm2835_spi_remove(struct platform_device * pdev)1404497667abSUwe Kleine-König static void bcm2835_spi_remove(struct platform_device *pdev)
1405f8043872SChris Boot {
14065f336ea5SLukas Wunner 	struct spi_controller *ctlr = platform_get_drvdata(pdev);
14075f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1408f8043872SChris Boot 
1409154f7da5SMartin Sperl 	bcm2835_debugfs_remove(bs);
1410154f7da5SMartin Sperl 
14119dd277ffSLukas Wunner 	spi_unregister_controller(ctlr);
14129dd277ffSLukas Wunner 
141305897c71SLukas Wunner 	bcm2835_dma_release(ctlr, bs);
141405897c71SLukas Wunner 
1415f8043872SChris Boot 	/* Clear FIFOs, and disable the HW block */
1416f8043872SChris Boot 	bcm2835_wr(bs, BCM2835_SPI_CS,
1417f8043872SChris Boot 		   BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
1418f8043872SChris Boot 
1419f8043872SChris Boot 	clk_disable_unprepare(bs->clk);
1420118eb0e5SFlorian Fainelli }
1421118eb0e5SFlorian Fainelli 
1422f8043872SChris Boot static const struct of_device_id bcm2835_spi_match[] = {
1423f8043872SChris Boot 	{ .compatible = "brcm,bcm2835-spi", },
1424f8043872SChris Boot 	{}
1425f8043872SChris Boot };
1426f8043872SChris Boot MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
1427f8043872SChris Boot 
1428f8043872SChris Boot static struct platform_driver bcm2835_spi_driver = {
1429f8043872SChris Boot 	.driver		= {
1430f8043872SChris Boot 		.name		= DRV_NAME,
1431f8043872SChris Boot 		.of_match_table	= bcm2835_spi_match,
1432f8043872SChris Boot 	},
1433f8043872SChris Boot 	.probe		= bcm2835_spi_probe,
1434497667abSUwe Kleine-König 	.remove_new	= bcm2835_spi_remove,
1435497667abSUwe Kleine-König 	.shutdown	= bcm2835_spi_remove,
1436f8043872SChris Boot };
1437f8043872SChris Boot module_platform_driver(bcm2835_spi_driver);
1438f8043872SChris Boot 
1439f8043872SChris Boot MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
1440f8043872SChris Boot MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
144122bf6cd2SStefan Wahren MODULE_LICENSE("GPL");
1442