xref: /openbmc/linux/drivers/spi/spi-bcm2835.c (revision e1483ac0)
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
71603e92ffSLukas Wunner #define BCM2835_SPI_NUM_CS		4   /* raise as necessary */
726935224dSMartin Sperl #define BCM2835_SPI_MODE_BITS	(SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
736935224dSMartin Sperl 				| SPI_NO_CS | SPI_3WIRE)
74f8043872SChris Boot 
75f8043872SChris Boot #define DRV_NAME	"spi-bcm2835"
76f8043872SChris Boot 
77ff245d90SMartin Sperl /* define polling limits */
78cbd632eaSJason Yan static unsigned int polling_limit_us = 30;
79ff245d90SMartin Sperl module_param(polling_limit_us, uint, 0664);
80ff245d90SMartin Sperl MODULE_PARM_DESC(polling_limit_us,
81ff245d90SMartin Sperl 		 "time in us to run a transfer in polling mode\n");
82ff245d90SMartin Sperl 
83acf0f856SLukas Wunner /**
84acf0f856SLukas Wunner  * struct bcm2835_spi - BCM2835 SPI controller
85acf0f856SLukas Wunner  * @regs: base address of register map
86acf0f856SLukas Wunner  * @clk: core clock, divided to calculate serial clock
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
99571e31faSLukas Wunner  * @prepare_cs: precalculated CS register value for ->prepare_message()
100571e31faSLukas Wunner  *	(uses slave-specific clock polarity and phase settings)
101154f7da5SMartin Sperl  * @debugfs_dir: the debugfs directory - neede to remove debugfs when
102154f7da5SMartin Sperl  *      unloading the module
103154f7da5SMartin Sperl  * @count_transfer_polling: count of how often polling mode is used
104154f7da5SMartin Sperl  * @count_transfer_irq: count of how often interrupt mode is used
105154f7da5SMartin Sperl  * @count_transfer_irq_after_polling: count of how often we fall back to
106154f7da5SMartin Sperl  *      interrupt mode after starting in polling mode.
107154f7da5SMartin Sperl  *      These are counted as well in @count_transfer_polling and
108154f7da5SMartin Sperl  *      @count_transfer_irq
109154f7da5SMartin Sperl  * @count_transfer_dma: count how often dma mode is used
1108259bf66SLukas Wunner  * @chip_select: SPI slave currently selected
1118259bf66SLukas Wunner  *	(used by bcm2835_spi_dma_tx_done() to write @clear_rx_cs)
1128259bf66SLukas Wunner  * @tx_dma_active: whether a TX DMA descriptor is in progress
1138259bf66SLukas Wunner  * @rx_dma_active: whether a RX DMA descriptor is in progress
1148259bf66SLukas Wunner  *	(used by bcm2835_spi_dma_tx_done() to handle a race)
1152b8279aeSLukas Wunner  * @fill_tx_desc: preallocated TX DMA descriptor used for RX-only transfers
1162b8279aeSLukas Wunner  *	(cyclically copies from zero page to TX FIFO)
1172b8279aeSLukas Wunner  * @fill_tx_addr: bus address of zero page
1188259bf66SLukas Wunner  * @clear_rx_desc: preallocated RX DMA descriptor used for TX-only transfers
1198259bf66SLukas Wunner  *	(cyclically clears RX FIFO by writing @clear_rx_cs to CS register)
1208259bf66SLukas Wunner  * @clear_rx_addr: bus address of @clear_rx_cs
1218259bf66SLukas Wunner  * @clear_rx_cs: precalculated CS register value to clear RX FIFO
1228259bf66SLukas Wunner  *	(uses slave-specific clock polarity and phase settings)
123acf0f856SLukas Wunner  */
124f8043872SChris Boot struct bcm2835_spi {
125f8043872SChris Boot 	void __iomem *regs;
126f8043872SChris Boot 	struct clk *clk;
127f8043872SChris Boot 	int irq;
1283bd7f658SLukas Wunner 	struct spi_transfer *tfr;
129afe7e363SRobin Murphy 	struct spi_controller *ctlr;
130f8043872SChris Boot 	const u8 *tx_buf;
131f8043872SChris Boot 	u8 *rx_buf;
132e34ff011SMartin Sperl 	int tx_len;
133e34ff011SMartin Sperl 	int rx_len;
1343bd7f658SLukas Wunner 	int tx_prologue;
1353bd7f658SLukas Wunner 	int rx_prologue;
136b31a9299SLukas Wunner 	unsigned int tx_spillover;
137571e31faSLukas Wunner 	u32 prepare_cs[BCM2835_SPI_NUM_CS];
138154f7da5SMartin Sperl 
139154f7da5SMartin Sperl 	struct dentry *debugfs_dir;
140154f7da5SMartin Sperl 	u64 count_transfer_polling;
141154f7da5SMartin Sperl 	u64 count_transfer_irq;
142154f7da5SMartin Sperl 	u64 count_transfer_irq_after_polling;
143154f7da5SMartin Sperl 	u64 count_transfer_dma;
1448259bf66SLukas Wunner 
1458259bf66SLukas Wunner 	u8 chip_select;
1468259bf66SLukas Wunner 	unsigned int tx_dma_active;
1478259bf66SLukas Wunner 	unsigned int rx_dma_active;
1482b8279aeSLukas Wunner 	struct dma_async_tx_descriptor *fill_tx_desc;
1492b8279aeSLukas Wunner 	dma_addr_t fill_tx_addr;
1508259bf66SLukas Wunner 	struct dma_async_tx_descriptor *clear_rx_desc[BCM2835_SPI_NUM_CS];
1518259bf66SLukas Wunner 	dma_addr_t clear_rx_addr;
1528259bf66SLukas Wunner 	u32 clear_rx_cs[BCM2835_SPI_NUM_CS] ____cacheline_aligned;
153f8043872SChris Boot };
154f8043872SChris Boot 
155154f7da5SMartin Sperl #if defined(CONFIG_DEBUG_FS)
156154f7da5SMartin Sperl static void bcm2835_debugfs_create(struct bcm2835_spi *bs,
157154f7da5SMartin Sperl 				   const char *dname)
158154f7da5SMartin Sperl {
159154f7da5SMartin Sperl 	char name[64];
160154f7da5SMartin Sperl 	struct dentry *dir;
161154f7da5SMartin Sperl 
162154f7da5SMartin Sperl 	/* get full name */
163154f7da5SMartin Sperl 	snprintf(name, sizeof(name), "spi-bcm2835-%s", dname);
164154f7da5SMartin Sperl 
165154f7da5SMartin Sperl 	/* the base directory */
166154f7da5SMartin Sperl 	dir = debugfs_create_dir(name, NULL);
167154f7da5SMartin Sperl 	bs->debugfs_dir = dir;
168154f7da5SMartin Sperl 
169154f7da5SMartin Sperl 	/* the counters */
170154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_polling", 0444, dir,
171154f7da5SMartin Sperl 			   &bs->count_transfer_polling);
172154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_irq", 0444, dir,
173154f7da5SMartin Sperl 			   &bs->count_transfer_irq);
174154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_irq_after_polling", 0444, dir,
175154f7da5SMartin Sperl 			   &bs->count_transfer_irq_after_polling);
176154f7da5SMartin Sperl 	debugfs_create_u64("count_transfer_dma", 0444, dir,
177154f7da5SMartin Sperl 			   &bs->count_transfer_dma);
178154f7da5SMartin Sperl }
179154f7da5SMartin Sperl 
180154f7da5SMartin Sperl static void bcm2835_debugfs_remove(struct bcm2835_spi *bs)
181154f7da5SMartin Sperl {
182154f7da5SMartin Sperl 	debugfs_remove_recursive(bs->debugfs_dir);
183154f7da5SMartin Sperl 	bs->debugfs_dir = NULL;
184154f7da5SMartin Sperl }
185154f7da5SMartin Sperl #else
186154f7da5SMartin Sperl static void bcm2835_debugfs_create(struct bcm2835_spi *bs,
187154f7da5SMartin Sperl 				   const char *dname)
188154f7da5SMartin Sperl {
189154f7da5SMartin Sperl }
190154f7da5SMartin Sperl 
191154f7da5SMartin Sperl static void bcm2835_debugfs_remove(struct bcm2835_spi *bs)
192154f7da5SMartin Sperl {
193154f7da5SMartin Sperl }
194154f7da5SMartin Sperl #endif /* CONFIG_DEBUG_FS */
195154f7da5SMartin Sperl 
196e37687c9SJacko Dirks static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned int reg)
197f8043872SChris Boot {
198f8043872SChris Boot 	return readl(bs->regs + reg);
199f8043872SChris Boot }
200f8043872SChris Boot 
201e37687c9SJacko Dirks static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned int reg, u32 val)
202f8043872SChris Boot {
203f8043872SChris Boot 	writel(val, bs->regs + reg);
204f8043872SChris Boot }
205f8043872SChris Boot 
2064adf3129SMartin Sperl static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs)
207f8043872SChris Boot {
208f8043872SChris Boot 	u8 byte;
209f8043872SChris Boot 
210e34ff011SMartin Sperl 	while ((bs->rx_len) &&
211e34ff011SMartin Sperl 	       (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
212f8043872SChris Boot 		byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
213f8043872SChris Boot 		if (bs->rx_buf)
214f8043872SChris Boot 			*bs->rx_buf++ = byte;
215e34ff011SMartin Sperl 		bs->rx_len--;
216f8043872SChris Boot 	}
217f8043872SChris Boot }
218f8043872SChris Boot 
2194adf3129SMartin Sperl static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
220f8043872SChris Boot {
221f8043872SChris Boot 	u8 byte;
222f8043872SChris Boot 
223e34ff011SMartin Sperl 	while ((bs->tx_len) &&
2244adf3129SMartin Sperl 	       (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
225f8043872SChris Boot 		byte = bs->tx_buf ? *bs->tx_buf++ : 0;
226f8043872SChris Boot 		bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
227e34ff011SMartin Sperl 		bs->tx_len--;
228f8043872SChris Boot 	}
229f8043872SChris Boot }
230f8043872SChris Boot 
2313bd7f658SLukas Wunner /**
2323bd7f658SLukas Wunner  * bcm2835_rd_fifo_count() - blindly read exactly @count bytes from RX FIFO
2333bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
2343bd7f658SLukas Wunner  * @count: bytes to read from RX FIFO
2353bd7f658SLukas Wunner  *
2363bd7f658SLukas Wunner  * The caller must ensure that @bs->rx_len is greater than or equal to @count,
2373bd7f658SLukas Wunner  * that the RX FIFO contains at least @count bytes and that the DMA Enable flag
2383bd7f658SLukas Wunner  * in the CS register is set (such that a read from the FIFO register receives
239b31a9299SLukas Wunner  * 32-bit instead of just 8-bit).  Moreover @bs->rx_buf must not be %NULL.
2403bd7f658SLukas Wunner  */
2413bd7f658SLukas Wunner static inline void bcm2835_rd_fifo_count(struct bcm2835_spi *bs, int count)
2423bd7f658SLukas Wunner {
2433bd7f658SLukas Wunner 	u32 val;
244b31a9299SLukas Wunner 	int len;
2453bd7f658SLukas Wunner 
2463bd7f658SLukas Wunner 	bs->rx_len -= count;
2473bd7f658SLukas Wunner 
24826751de2SRobin Murphy 	do {
2493bd7f658SLukas Wunner 		val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
250b31a9299SLukas Wunner 		len = min(count, 4);
2513bd7f658SLukas Wunner 		memcpy(bs->rx_buf, &val, len);
2523bd7f658SLukas Wunner 		bs->rx_buf += len;
2533bd7f658SLukas Wunner 		count -= 4;
25426751de2SRobin Murphy 	} while (count > 0);
2553bd7f658SLukas Wunner }
2563bd7f658SLukas Wunner 
2573bd7f658SLukas Wunner /**
2583bd7f658SLukas Wunner  * bcm2835_wr_fifo_count() - blindly write exactly @count bytes to TX FIFO
2593bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
2603bd7f658SLukas Wunner  * @count: bytes to write to TX FIFO
2613bd7f658SLukas Wunner  *
2623bd7f658SLukas Wunner  * The caller must ensure that @bs->tx_len is greater than or equal to @count,
2633bd7f658SLukas Wunner  * that the TX FIFO can accommodate @count bytes and that the DMA Enable flag
2643bd7f658SLukas Wunner  * in the CS register is set (such that a write to the FIFO register transmits
2653bd7f658SLukas Wunner  * 32-bit instead of just 8-bit).
2663bd7f658SLukas Wunner  */
2673bd7f658SLukas Wunner static inline void bcm2835_wr_fifo_count(struct bcm2835_spi *bs, int count)
2683bd7f658SLukas Wunner {
2693bd7f658SLukas Wunner 	u32 val;
270b31a9299SLukas Wunner 	int len;
2713bd7f658SLukas Wunner 
2723bd7f658SLukas Wunner 	bs->tx_len -= count;
2733bd7f658SLukas Wunner 
27426751de2SRobin Murphy 	do {
2753bd7f658SLukas Wunner 		if (bs->tx_buf) {
276b31a9299SLukas Wunner 			len = min(count, 4);
2773bd7f658SLukas Wunner 			memcpy(&val, bs->tx_buf, len);
2783bd7f658SLukas Wunner 			bs->tx_buf += len;
2793bd7f658SLukas Wunner 		} else {
2803bd7f658SLukas Wunner 			val = 0;
2813bd7f658SLukas Wunner 		}
2823bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
2833bd7f658SLukas Wunner 		count -= 4;
28426751de2SRobin Murphy 	} while (count > 0);
2853bd7f658SLukas Wunner }
2863bd7f658SLukas Wunner 
2873bd7f658SLukas Wunner /**
2883bd7f658SLukas Wunner  * bcm2835_wait_tx_fifo_empty() - busy-wait for TX FIFO to empty
2893bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
290b31a9299SLukas Wunner  *
291b31a9299SLukas Wunner  * The caller must ensure that the RX FIFO can accommodate as many bytes
292b31a9299SLukas Wunner  * as have been written to the TX FIFO:  Transmission is halted once the
293b31a9299SLukas Wunner  * RX FIFO is full, causing this function to spin forever.
2943bd7f658SLukas Wunner  */
2953bd7f658SLukas Wunner static inline void bcm2835_wait_tx_fifo_empty(struct bcm2835_spi *bs)
2963bd7f658SLukas Wunner {
2973bd7f658SLukas Wunner 	while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE))
2983bd7f658SLukas Wunner 		cpu_relax();
2993bd7f658SLukas Wunner }
3003bd7f658SLukas Wunner 
3012e0733bcSLukas Wunner /**
3022e0733bcSLukas Wunner  * bcm2835_rd_fifo_blind() - blindly read up to @count bytes from RX FIFO
3032e0733bcSLukas Wunner  * @bs: BCM2835 SPI controller
3042e0733bcSLukas Wunner  * @count: bytes available for reading in RX FIFO
3052e0733bcSLukas Wunner  */
3062e0733bcSLukas Wunner static inline void bcm2835_rd_fifo_blind(struct bcm2835_spi *bs, int count)
3072e0733bcSLukas Wunner {
3082e0733bcSLukas Wunner 	u8 val;
3092e0733bcSLukas Wunner 
3102e0733bcSLukas Wunner 	count = min(count, bs->rx_len);
3112e0733bcSLukas Wunner 	bs->rx_len -= count;
3122e0733bcSLukas Wunner 
31326751de2SRobin Murphy 	do {
3142e0733bcSLukas Wunner 		val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
3152e0733bcSLukas Wunner 		if (bs->rx_buf)
3162e0733bcSLukas Wunner 			*bs->rx_buf++ = val;
31726751de2SRobin Murphy 	} while (--count);
3182e0733bcSLukas Wunner }
3192e0733bcSLukas Wunner 
3202e0733bcSLukas Wunner /**
3212e0733bcSLukas Wunner  * bcm2835_wr_fifo_blind() - blindly write up to @count bytes to TX FIFO
3222e0733bcSLukas Wunner  * @bs: BCM2835 SPI controller
3232e0733bcSLukas Wunner  * @count: bytes available for writing in TX FIFO
3242e0733bcSLukas Wunner  */
3252e0733bcSLukas Wunner static inline void bcm2835_wr_fifo_blind(struct bcm2835_spi *bs, int count)
3262e0733bcSLukas Wunner {
3272e0733bcSLukas Wunner 	u8 val;
3282e0733bcSLukas Wunner 
3292e0733bcSLukas Wunner 	count = min(count, bs->tx_len);
3302e0733bcSLukas Wunner 	bs->tx_len -= count;
3312e0733bcSLukas Wunner 
33226751de2SRobin Murphy 	do {
3332e0733bcSLukas Wunner 		val = bs->tx_buf ? *bs->tx_buf++ : 0;
3342e0733bcSLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
33526751de2SRobin Murphy 	} while (--count);
3362e0733bcSLukas Wunner }
3372e0733bcSLukas Wunner 
338ac4648b5SRobin Murphy static void bcm2835_spi_reset_hw(struct bcm2835_spi *bs)
339e34ff011SMartin Sperl {
340e34ff011SMartin Sperl 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
341e34ff011SMartin Sperl 
342e34ff011SMartin Sperl 	/* Disable SPI interrupts and transfer */
343e34ff011SMartin Sperl 	cs &= ~(BCM2835_SPI_CS_INTR |
344e34ff011SMartin Sperl 		BCM2835_SPI_CS_INTD |
3453ecd37edSMartin Sperl 		BCM2835_SPI_CS_DMAEN |
346e34ff011SMartin Sperl 		BCM2835_SPI_CS_TA);
3474c524191SLukas Wunner 	/*
3484c524191SLukas Wunner 	 * Transmission sometimes breaks unless the DONE bit is written at the
3494c524191SLukas Wunner 	 * end of every transfer.  The spec says it's a RO bit.  Either the
3504c524191SLukas Wunner 	 * spec is wrong and the bit is actually of type RW1C, or it's a
3514c524191SLukas Wunner 	 * hardware erratum.
3524c524191SLukas Wunner 	 */
3534c524191SLukas Wunner 	cs |= BCM2835_SPI_CS_DONE;
354e34ff011SMartin Sperl 	/* and reset RX/TX FIFOS */
355e34ff011SMartin Sperl 	cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
356e34ff011SMartin Sperl 
357e34ff011SMartin Sperl 	/* and reset the SPI_HW */
358e34ff011SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS, cs);
3593ecd37edSMartin Sperl 	/* as well as DLEN */
3603ecd37edSMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_DLEN, 0);
361e34ff011SMartin Sperl }
362e34ff011SMartin Sperl 
363f8043872SChris Boot static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
364f8043872SChris Boot {
365afe7e363SRobin Murphy 	struct bcm2835_spi *bs = dev_id;
3662e0733bcSLukas Wunner 	u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
3672e0733bcSLukas Wunner 
3682e0733bcSLukas Wunner 	/*
3692e0733bcSLukas Wunner 	 * An interrupt is signaled either if DONE is set (TX FIFO empty)
3702e0733bcSLukas Wunner 	 * or if RXR is set (RX FIFO >= ¾ full).
3712e0733bcSLukas Wunner 	 */
3722e0733bcSLukas Wunner 	if (cs & BCM2835_SPI_CS_RXF)
3732e0733bcSLukas Wunner 		bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
3742e0733bcSLukas Wunner 	else if (cs & BCM2835_SPI_CS_RXR)
3752e0733bcSLukas Wunner 		bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE_3_4);
3762e0733bcSLukas Wunner 
3772e0733bcSLukas Wunner 	if (bs->tx_len && cs & BCM2835_SPI_CS_DONE)
3782e0733bcSLukas Wunner 		bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
379f8043872SChris Boot 
3804adf3129SMartin Sperl 	/* Read as many bytes as possible from FIFO */
3814adf3129SMartin Sperl 	bcm2835_rd_fifo(bs);
382e34ff011SMartin Sperl 	/* Write as many bytes as possible to FIFO */
3834adf3129SMartin Sperl 	bcm2835_wr_fifo(bs);
384f8043872SChris Boot 
38556c17234SLukas Wunner 	if (!bs->rx_len) {
386e34ff011SMartin Sperl 		/* Transfer complete - reset SPI HW */
387ac4648b5SRobin Murphy 		bcm2835_spi_reset_hw(bs);
388e34ff011SMartin Sperl 		/* wake up the framework */
389afe7e363SRobin Murphy 		complete(&bs->ctlr->xfer_completion);
390f8043872SChris Boot 	}
391f8043872SChris Boot 
392f8043872SChris Boot 	return IRQ_HANDLED;
393f8043872SChris Boot }
394f8043872SChris Boot 
3955f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_irq(struct spi_controller *ctlr,
396704f32d4SMartin Sperl 					struct spi_device *spi,
397704f32d4SMartin Sperl 					struct spi_transfer *tfr,
3982e0733bcSLukas Wunner 					u32 cs, bool fifo_empty)
399704f32d4SMartin Sperl {
4005f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
401f8043872SChris Boot 
402154f7da5SMartin Sperl 	/* update usage statistics */
403154f7da5SMartin Sperl 	bs->count_transfer_irq++;
404154f7da5SMartin Sperl 
405f8043872SChris Boot 	/*
4065c09e42fSLukas Wunner 	 * Enable HW block, but with interrupts still disabled.
4075c09e42fSLukas Wunner 	 * Otherwise the empty TX FIFO would immediately trigger an interrupt.
408f8043872SChris Boot 	 */
4095c09e42fSLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
4105c09e42fSLukas Wunner 
4115c09e42fSLukas Wunner 	/* fill TX FIFO as much as possible */
4122e0733bcSLukas Wunner 	if (fifo_empty)
4132e0733bcSLukas Wunner 		bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
4145c09e42fSLukas Wunner 	bcm2835_wr_fifo(bs);
4155c09e42fSLukas Wunner 
4165c09e42fSLukas Wunner 	/* enable interrupts */
417e34ff011SMartin Sperl 	cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
418f8043872SChris Boot 	bcm2835_wr(bs, BCM2835_SPI_CS, cs);
419f8043872SChris Boot 
420e34ff011SMartin Sperl 	/* signal that we need to wait for completion */
421e34ff011SMartin Sperl 	return 1;
422f8043872SChris Boot }
423f8043872SChris Boot 
4243bd7f658SLukas Wunner /**
4253bd7f658SLukas Wunner  * bcm2835_spi_transfer_prologue() - transfer first few bytes without DMA
4265f336ea5SLukas Wunner  * @ctlr: SPI master controller
4273bd7f658SLukas Wunner  * @tfr: SPI transfer
4283bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
4293bd7f658SLukas Wunner  * @cs: CS register
4303bd7f658SLukas Wunner  *
4313bd7f658SLukas Wunner  * A limitation in DMA mode is that the FIFO must be accessed in 4 byte chunks.
4323bd7f658SLukas Wunner  * Only the final write access is permitted to transmit less than 4 bytes, the
4333bd7f658SLukas Wunner  * SPI controller deduces its intended size from the DLEN register.
4343bd7f658SLukas Wunner  *
4353bd7f658SLukas Wunner  * If a TX or RX sglist contains multiple entries, one per page, and the first
4363bd7f658SLukas Wunner  * entry starts in the middle of a page, that first entry's length may not be
4373bd7f658SLukas Wunner  * a multiple of 4.  Subsequent entries are fine because they span an entire
4383bd7f658SLukas Wunner  * page, hence do have a length that's a multiple of 4.
4393bd7f658SLukas Wunner  *
4403bd7f658SLukas Wunner  * This cannot happen with kmalloc'ed buffers (which is what most clients use)
4413bd7f658SLukas Wunner  * because they are contiguous in physical memory and therefore not split on
4423bd7f658SLukas Wunner  * page boundaries by spi_map_buf().  But it *can* happen with vmalloc'ed
4433bd7f658SLukas Wunner  * buffers.
4443bd7f658SLukas Wunner  *
4453bd7f658SLukas Wunner  * The DMA engine is incapable of combining sglist entries into a continuous
4463bd7f658SLukas Wunner  * stream of 4 byte chunks, it treats every entry separately:  A TX entry is
4473bd7f658SLukas Wunner  * rounded up a to a multiple of 4 bytes by transmitting surplus bytes, an RX
4483bd7f658SLukas Wunner  * entry is rounded up by throwing away received bytes.
4493bd7f658SLukas Wunner  *
4503bd7f658SLukas Wunner  * Overcome this limitation by transferring the first few bytes without DMA:
4513bd7f658SLukas Wunner  * E.g. if the first TX sglist entry's length is 23 and the first RX's is 42,
4523bd7f658SLukas Wunner  * write 3 bytes to the TX FIFO but read only 2 bytes from the RX FIFO.
4533bd7f658SLukas Wunner  * The residue of 1 byte in the RX FIFO is picked up by DMA.  Together with
4543bd7f658SLukas Wunner  * the rest of the first RX sglist entry it makes up a multiple of 4 bytes.
4553bd7f658SLukas Wunner  *
4563bd7f658SLukas Wunner  * Should the RX prologue be larger, say, 3 vis-à-vis a TX prologue of 1,
4573bd7f658SLukas Wunner  * write 1 + 4 = 5 bytes to the TX FIFO and read 3 bytes from the RX FIFO.
4583bd7f658SLukas Wunner  * Caution, the additional 4 bytes spill over to the second TX sglist entry
4593bd7f658SLukas Wunner  * if the length of the first is *exactly* 1.
4603bd7f658SLukas Wunner  *
4613bd7f658SLukas Wunner  * At most 6 bytes are written and at most 3 bytes read.  Do we know the
4623bd7f658SLukas Wunner  * transfer has this many bytes?  Yes, see BCM2835_SPI_DMA_MIN_LENGTH.
4633bd7f658SLukas Wunner  *
4643bd7f658SLukas Wunner  * The FIFO is normally accessed with 8-bit width by the CPU and 32-bit width
4653bd7f658SLukas Wunner  * by the DMA engine.  Toggling the DMA Enable flag in the CS register switches
4663bd7f658SLukas Wunner  * the width but also garbles the FIFO's contents.  The prologue must therefore
4673bd7f658SLukas Wunner  * be transmitted in 32-bit width to ensure that the following DMA transfer can
4683bd7f658SLukas Wunner  * pick up the residue in the RX FIFO in ungarbled form.
4693bd7f658SLukas Wunner  */
4705f336ea5SLukas Wunner static void bcm2835_spi_transfer_prologue(struct spi_controller *ctlr,
4713bd7f658SLukas Wunner 					  struct spi_transfer *tfr,
4723bd7f658SLukas Wunner 					  struct bcm2835_spi *bs,
4733bd7f658SLukas Wunner 					  u32 cs)
4743bd7f658SLukas Wunner {
4753bd7f658SLukas Wunner 	int tx_remaining;
4763bd7f658SLukas Wunner 
4773bd7f658SLukas Wunner 	bs->tfr		 = tfr;
4783bd7f658SLukas Wunner 	bs->tx_prologue  = 0;
4793bd7f658SLukas Wunner 	bs->rx_prologue  = 0;
4803bd7f658SLukas Wunner 	bs->tx_spillover = false;
4813bd7f658SLukas Wunner 
4822b8279aeSLukas Wunner 	if (bs->tx_buf && !sg_is_last(&tfr->tx_sg.sgl[0]))
4833bd7f658SLukas Wunner 		bs->tx_prologue = sg_dma_len(&tfr->tx_sg.sgl[0]) & 3;
4843bd7f658SLukas Wunner 
4858259bf66SLukas Wunner 	if (bs->rx_buf && !sg_is_last(&tfr->rx_sg.sgl[0])) {
4863bd7f658SLukas Wunner 		bs->rx_prologue = sg_dma_len(&tfr->rx_sg.sgl[0]) & 3;
4873bd7f658SLukas Wunner 
4883bd7f658SLukas Wunner 		if (bs->rx_prologue > bs->tx_prologue) {
4892b8279aeSLukas Wunner 			if (!bs->tx_buf || sg_is_last(&tfr->tx_sg.sgl[0])) {
4903bd7f658SLukas Wunner 				bs->tx_prologue  = bs->rx_prologue;
4913bd7f658SLukas Wunner 			} else {
4923bd7f658SLukas Wunner 				bs->tx_prologue += 4;
4933bd7f658SLukas Wunner 				bs->tx_spillover =
4943bd7f658SLukas Wunner 					!(sg_dma_len(&tfr->tx_sg.sgl[0]) & ~3);
4953bd7f658SLukas Wunner 			}
4963bd7f658SLukas Wunner 		}
4973bd7f658SLukas Wunner 	}
4983bd7f658SLukas Wunner 
4993bd7f658SLukas Wunner 	/* rx_prologue > 0 implies tx_prologue > 0, so check only the latter */
5003bd7f658SLukas Wunner 	if (!bs->tx_prologue)
5013bd7f658SLukas Wunner 		return;
5023bd7f658SLukas Wunner 
5033bd7f658SLukas Wunner 	/* Write and read RX prologue.  Adjust first entry in RX sglist. */
5043bd7f658SLukas Wunner 	if (bs->rx_prologue) {
5053bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->rx_prologue);
5063bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
5073bd7f658SLukas Wunner 						  | BCM2835_SPI_CS_DMAEN);
5083bd7f658SLukas Wunner 		bcm2835_wr_fifo_count(bs, bs->rx_prologue);
5093bd7f658SLukas Wunner 		bcm2835_wait_tx_fifo_empty(bs);
5103bd7f658SLukas Wunner 		bcm2835_rd_fifo_count(bs, bs->rx_prologue);
5114c524191SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_RX
5124c524191SLukas Wunner 						  | BCM2835_SPI_CS_CLEAR_TX
5134c524191SLukas Wunner 						  | BCM2835_SPI_CS_DONE);
5143bd7f658SLukas Wunner 
5155f336ea5SLukas Wunner 		dma_sync_single_for_device(ctlr->dma_rx->device->dev,
516b31a9299SLukas Wunner 					   sg_dma_address(&tfr->rx_sg.sgl[0]),
517b31a9299SLukas Wunner 					   bs->rx_prologue, DMA_FROM_DEVICE);
5183bd7f658SLukas Wunner 
519b31a9299SLukas Wunner 		sg_dma_address(&tfr->rx_sg.sgl[0]) += bs->rx_prologue;
520b31a9299SLukas Wunner 		sg_dma_len(&tfr->rx_sg.sgl[0])     -= bs->rx_prologue;
5213bd7f658SLukas Wunner 	}
5223bd7f658SLukas Wunner 
5232b8279aeSLukas Wunner 	if (!bs->tx_buf)
5242b8279aeSLukas Wunner 		return;
5252b8279aeSLukas Wunner 
5263bd7f658SLukas Wunner 	/*
5273bd7f658SLukas Wunner 	 * Write remaining TX prologue.  Adjust first entry in TX sglist.
5283bd7f658SLukas Wunner 	 * Also adjust second entry if prologue spills over to it.
5293bd7f658SLukas Wunner 	 */
5303bd7f658SLukas Wunner 	tx_remaining = bs->tx_prologue - bs->rx_prologue;
5313bd7f658SLukas Wunner 	if (tx_remaining) {
5323bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_DLEN, tx_remaining);
5333bd7f658SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
5343bd7f658SLukas Wunner 						  | BCM2835_SPI_CS_DMAEN);
5353bd7f658SLukas Wunner 		bcm2835_wr_fifo_count(bs, tx_remaining);
5363bd7f658SLukas Wunner 		bcm2835_wait_tx_fifo_empty(bs);
5374c524191SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_TX
5384c524191SLukas Wunner 						  | BCM2835_SPI_CS_DONE);
5393bd7f658SLukas Wunner 	}
5403bd7f658SLukas Wunner 
5413bd7f658SLukas Wunner 	if (likely(!bs->tx_spillover)) {
542b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[0]) += bs->tx_prologue;
543b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])     -= bs->tx_prologue;
5443bd7f658SLukas Wunner 	} else {
545b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])      = 0;
546b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[1]) += 4;
547b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[1])     -= 4;
5483bd7f658SLukas Wunner 	}
5493bd7f658SLukas Wunner }
5503bd7f658SLukas Wunner 
5513bd7f658SLukas Wunner /**
5523bd7f658SLukas Wunner  * bcm2835_spi_undo_prologue() - reconstruct original sglist state
5533bd7f658SLukas Wunner  * @bs: BCM2835 SPI controller
5543bd7f658SLukas Wunner  *
5553bd7f658SLukas Wunner  * Undo changes which were made to an SPI transfer's sglist when transmitting
5563bd7f658SLukas Wunner  * the prologue.  This is necessary to ensure the same memory ranges are
5573bd7f658SLukas Wunner  * unmapped that were originally mapped.
5583bd7f658SLukas Wunner  */
5593bd7f658SLukas Wunner static void bcm2835_spi_undo_prologue(struct bcm2835_spi *bs)
5603bd7f658SLukas Wunner {
5613bd7f658SLukas Wunner 	struct spi_transfer *tfr = bs->tfr;
5623bd7f658SLukas Wunner 
5633bd7f658SLukas Wunner 	if (!bs->tx_prologue)
5643bd7f658SLukas Wunner 		return;
5653bd7f658SLukas Wunner 
5663bd7f658SLukas Wunner 	if (bs->rx_prologue) {
567b31a9299SLukas Wunner 		sg_dma_address(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue;
568b31a9299SLukas Wunner 		sg_dma_len(&tfr->rx_sg.sgl[0])     += bs->rx_prologue;
5693bd7f658SLukas Wunner 	}
5703bd7f658SLukas Wunner 
5712b8279aeSLukas Wunner 	if (!bs->tx_buf)
5722b8279aeSLukas Wunner 		goto out;
5732b8279aeSLukas Wunner 
5743bd7f658SLukas Wunner 	if (likely(!bs->tx_spillover)) {
575b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue;
576b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])     += bs->tx_prologue;
5773bd7f658SLukas Wunner 	} else {
578b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[0])      = bs->tx_prologue - 4;
579b31a9299SLukas Wunner 		sg_dma_address(&tfr->tx_sg.sgl[1]) -= 4;
580b31a9299SLukas Wunner 		sg_dma_len(&tfr->tx_sg.sgl[1])     += 4;
5813bd7f658SLukas Wunner 	}
5822b8279aeSLukas Wunner out:
5831513ceeeSLukas Wunner 	bs->tx_prologue = 0;
5843bd7f658SLukas Wunner }
5853bd7f658SLukas Wunner 
5868259bf66SLukas Wunner /**
5878259bf66SLukas Wunner  * bcm2835_spi_dma_rx_done() - callback for DMA RX channel
5888259bf66SLukas Wunner  * @data: SPI master controller
5898259bf66SLukas Wunner  *
5908259bf66SLukas Wunner  * Used for bidirectional and RX-only transfers.
5918259bf66SLukas Wunner  */
5928259bf66SLukas Wunner static void bcm2835_spi_dma_rx_done(void *data)
5933ecd37edSMartin Sperl {
5945f336ea5SLukas Wunner 	struct spi_controller *ctlr = data;
5955f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
5963ecd37edSMartin Sperl 
5972b8279aeSLukas Wunner 	/* terminate tx-dma as we do not have an irq for it
5983ecd37edSMartin Sperl 	 * because when the rx dma will terminate and this callback
5993ecd37edSMartin Sperl 	 * is called the tx-dma must have finished - can't get to this
6003ecd37edSMartin Sperl 	 * situation otherwise...
6013ecd37edSMartin Sperl 	 */
6025f336ea5SLukas Wunner 	dmaengine_terminate_async(ctlr->dma_tx);
6038259bf66SLukas Wunner 	bs->tx_dma_active = false;
6048259bf66SLukas Wunner 	bs->rx_dma_active = false;
6053bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
6063ecd37edSMartin Sperl 
6072b8279aeSLukas Wunner 	/* reset fifo and HW */
608ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
6093ecd37edSMartin Sperl 
6103ecd37edSMartin Sperl 	/* and mark as completed */;
6115f336ea5SLukas Wunner 	complete(&ctlr->xfer_completion);
6123ecd37edSMartin Sperl }
6133ecd37edSMartin Sperl 
6148259bf66SLukas Wunner /**
6158259bf66SLukas Wunner  * bcm2835_spi_dma_tx_done() - callback for DMA TX channel
6168259bf66SLukas Wunner  * @data: SPI master controller
6178259bf66SLukas Wunner  *
6188259bf66SLukas Wunner  * Used for TX-only transfers.
6198259bf66SLukas Wunner  */
6208259bf66SLukas Wunner static void bcm2835_spi_dma_tx_done(void *data)
6218259bf66SLukas Wunner {
6228259bf66SLukas Wunner 	struct spi_controller *ctlr = data;
6238259bf66SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
6248259bf66SLukas Wunner 
6258259bf66SLukas Wunner 	/* busy-wait for TX FIFO to empty */
6268259bf66SLukas Wunner 	while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE))
6278259bf66SLukas Wunner 		bcm2835_wr(bs, BCM2835_SPI_CS,
6288259bf66SLukas Wunner 			   bs->clear_rx_cs[bs->chip_select]);
6298259bf66SLukas Wunner 
6308259bf66SLukas Wunner 	bs->tx_dma_active = false;
6318259bf66SLukas Wunner 	smp_wmb();
6328259bf66SLukas Wunner 
6338259bf66SLukas Wunner 	/*
6348259bf66SLukas Wunner 	 * In case of a very short transfer, RX DMA may not have been
6358259bf66SLukas Wunner 	 * issued yet.  The onus is then on bcm2835_spi_transfer_one_dma()
6368259bf66SLukas Wunner 	 * to terminate it immediately after issuing.
6378259bf66SLukas Wunner 	 */
6388259bf66SLukas Wunner 	if (cmpxchg(&bs->rx_dma_active, true, false))
6398259bf66SLukas Wunner 		dmaengine_terminate_async(ctlr->dma_rx);
6408259bf66SLukas Wunner 
6418259bf66SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
642ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
6438259bf66SLukas Wunner 	complete(&ctlr->xfer_completion);
6448259bf66SLukas Wunner }
6458259bf66SLukas Wunner 
6468259bf66SLukas Wunner /**
6478259bf66SLukas Wunner  * bcm2835_spi_prepare_sg() - prepare and submit DMA descriptor for sglist
6488259bf66SLukas Wunner  * @ctlr: SPI master controller
6498259bf66SLukas Wunner  * @spi: SPI slave
6508259bf66SLukas Wunner  * @tfr: SPI transfer
6518259bf66SLukas Wunner  * @bs: BCM2835 SPI controller
6528259bf66SLukas Wunner  * @is_tx: whether to submit DMA descriptor for TX or RX sglist
6538259bf66SLukas Wunner  *
6548259bf66SLukas Wunner  * Prepare and submit a DMA descriptor for the TX or RX sglist of @tfr.
6558259bf66SLukas Wunner  * Return 0 on success or a negative error number.
6568259bf66SLukas Wunner  */
6575f336ea5SLukas Wunner static int bcm2835_spi_prepare_sg(struct spi_controller *ctlr,
6588259bf66SLukas Wunner 				  struct spi_device *spi,
6593ecd37edSMartin Sperl 				  struct spi_transfer *tfr,
6608259bf66SLukas Wunner 				  struct bcm2835_spi *bs,
6613ecd37edSMartin Sperl 				  bool is_tx)
6623ecd37edSMartin Sperl {
6633ecd37edSMartin Sperl 	struct dma_chan *chan;
6643ecd37edSMartin Sperl 	struct scatterlist *sgl;
6653ecd37edSMartin Sperl 	unsigned int nents;
6663ecd37edSMartin Sperl 	enum dma_transfer_direction dir;
6673ecd37edSMartin Sperl 	unsigned long flags;
6683ecd37edSMartin Sperl 
6693ecd37edSMartin Sperl 	struct dma_async_tx_descriptor *desc;
6703ecd37edSMartin Sperl 	dma_cookie_t cookie;
6713ecd37edSMartin Sperl 
6723ecd37edSMartin Sperl 	if (is_tx) {
6733ecd37edSMartin Sperl 		dir   = DMA_MEM_TO_DEV;
6745f336ea5SLukas Wunner 		chan  = ctlr->dma_tx;
6753ecd37edSMartin Sperl 		nents = tfr->tx_sg.nents;
6763ecd37edSMartin Sperl 		sgl   = tfr->tx_sg.sgl;
6778259bf66SLukas Wunner 		flags = tfr->rx_buf ? 0 : DMA_PREP_INTERRUPT;
6783ecd37edSMartin Sperl 	} else {
6793ecd37edSMartin Sperl 		dir   = DMA_DEV_TO_MEM;
6805f336ea5SLukas Wunner 		chan  = ctlr->dma_rx;
6813ecd37edSMartin Sperl 		nents = tfr->rx_sg.nents;
6823ecd37edSMartin Sperl 		sgl   = tfr->rx_sg.sgl;
6833ecd37edSMartin Sperl 		flags = DMA_PREP_INTERRUPT;
6843ecd37edSMartin Sperl 	}
6853ecd37edSMartin Sperl 	/* prepare the channel */
6863ecd37edSMartin Sperl 	desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
6873ecd37edSMartin Sperl 	if (!desc)
6883ecd37edSMartin Sperl 		return -EINVAL;
6893ecd37edSMartin Sperl 
6908259bf66SLukas Wunner 	/*
6918259bf66SLukas Wunner 	 * Completion is signaled by the RX channel for bidirectional and
6928259bf66SLukas Wunner 	 * RX-only transfers; else by the TX channel for TX-only transfers.
6938259bf66SLukas Wunner 	 */
6943ecd37edSMartin Sperl 	if (!is_tx) {
6958259bf66SLukas Wunner 		desc->callback = bcm2835_spi_dma_rx_done;
6965f336ea5SLukas Wunner 		desc->callback_param = ctlr;
6978259bf66SLukas Wunner 	} else if (!tfr->rx_buf) {
6988259bf66SLukas Wunner 		desc->callback = bcm2835_spi_dma_tx_done;
6998259bf66SLukas Wunner 		desc->callback_param = ctlr;
7008259bf66SLukas Wunner 		bs->chip_select = spi->chip_select;
7013ecd37edSMartin Sperl 	}
7023ecd37edSMartin Sperl 
7033ecd37edSMartin Sperl 	/* submit it to DMA-engine */
7043ecd37edSMartin Sperl 	cookie = dmaengine_submit(desc);
7053ecd37edSMartin Sperl 
7063ecd37edSMartin Sperl 	return dma_submit_error(cookie);
7073ecd37edSMartin Sperl }
7083ecd37edSMartin Sperl 
7098259bf66SLukas Wunner /**
7108259bf66SLukas Wunner  * bcm2835_spi_transfer_one_dma() - perform SPI transfer using DMA engine
7118259bf66SLukas Wunner  * @ctlr: SPI master controller
7128259bf66SLukas Wunner  * @spi: SPI slave
7138259bf66SLukas Wunner  * @tfr: SPI transfer
7148259bf66SLukas Wunner  * @cs: CS register
7158259bf66SLukas Wunner  *
7168259bf66SLukas Wunner  * For *bidirectional* transfers (both tx_buf and rx_buf are non-%NULL), set up
7178259bf66SLukas Wunner  * the TX and RX DMA channel to copy between memory and FIFO register.
7188259bf66SLukas Wunner  *
7198259bf66SLukas Wunner  * For *TX-only* transfers (rx_buf is %NULL), copying the RX FIFO's contents to
7208259bf66SLukas Wunner  * memory is pointless.  However not reading the RX FIFO isn't an option either
7218259bf66SLukas Wunner  * because transmission is halted once it's full.  As a workaround, cyclically
7228259bf66SLukas Wunner  * clear the RX FIFO by setting the CLEAR_RX bit in the CS register.
7238259bf66SLukas Wunner  *
7248259bf66SLukas Wunner  * The CS register value is precalculated in bcm2835_spi_setup().  Normally
7258259bf66SLukas Wunner  * this is called only once, on slave registration.  A DMA descriptor to write
7268259bf66SLukas Wunner  * this value is preallocated in bcm2835_dma_init().  All that's left to do
7278259bf66SLukas Wunner  * when performing a TX-only transfer is to submit this descriptor to the RX
7288259bf66SLukas Wunner  * DMA channel.  Latency is thereby minimized.  The descriptor does not
7298259bf66SLukas Wunner  * generate any interrupts while running.  It must be terminated once the
7308259bf66SLukas Wunner  * TX DMA channel is done.
7318259bf66SLukas Wunner  *
7328259bf66SLukas Wunner  * Clearing the RX FIFO is paced by the DREQ signal.  The signal is asserted
7338259bf66SLukas Wunner  * when the RX FIFO becomes half full, i.e. 32 bytes.  (Tuneable with the DC
7348259bf66SLukas Wunner  * register.)  Reading 32 bytes from the RX FIFO would normally require 8 bus
7358259bf66SLukas Wunner  * accesses, whereas clearing it requires only 1 bus access.  So an 8-fold
7368259bf66SLukas Wunner  * reduction in bus traffic and thus energy consumption is achieved.
7372b8279aeSLukas Wunner  *
7382b8279aeSLukas Wunner  * For *RX-only* transfers (tx_buf is %NULL), fill the TX FIFO by cyclically
7392b8279aeSLukas Wunner  * copying from the zero page.  The DMA descriptor to do this is preallocated
7402b8279aeSLukas Wunner  * in bcm2835_dma_init().  It must be terminated once the RX DMA channel is
7412b8279aeSLukas Wunner  * done and can then be reused.
7422b8279aeSLukas Wunner  *
7432b8279aeSLukas Wunner  * The BCM2835 DMA driver autodetects when a transaction copies from the zero
7442b8279aeSLukas Wunner  * page and utilizes the DMA controller's ability to synthesize zeroes instead
7452b8279aeSLukas Wunner  * of copying them from memory.  This reduces traffic on the memory bus.  The
7462b8279aeSLukas Wunner  * feature is not available on so-called "lite" channels, but normally TX DMA
7472b8279aeSLukas Wunner  * is backed by a full-featured channel.
7482b8279aeSLukas Wunner  *
7492b8279aeSLukas Wunner  * Zero-filling the TX FIFO is paced by the DREQ signal.  Unfortunately the
7502b8279aeSLukas Wunner  * BCM2835 SPI controller continues to assert DREQ even after the DLEN register
7512b8279aeSLukas Wunner  * has been counted down to zero (hardware erratum).  Thus, when the transfer
7522b8279aeSLukas Wunner  * has finished, the DMA engine zero-fills the TX FIFO until it is half full.
7532b8279aeSLukas Wunner  * (Tuneable with the DC register.)  So up to 9 gratuitous bus accesses are
7542b8279aeSLukas Wunner  * performed at the end of an RX-only transfer.
7558259bf66SLukas Wunner  */
7565f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_dma(struct spi_controller *ctlr,
7573ecd37edSMartin Sperl 					struct spi_device *spi,
7583ecd37edSMartin Sperl 					struct spi_transfer *tfr,
7593ecd37edSMartin Sperl 					u32 cs)
7603ecd37edSMartin Sperl {
7615f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
7628259bf66SLukas Wunner 	dma_cookie_t cookie;
7633ecd37edSMartin Sperl 	int ret;
7643ecd37edSMartin Sperl 
765154f7da5SMartin Sperl 	/* update usage statistics */
766154f7da5SMartin Sperl 	bs->count_transfer_dma++;
767154f7da5SMartin Sperl 
7683bd7f658SLukas Wunner 	/*
7693bd7f658SLukas Wunner 	 * Transfer first few bytes without DMA if length of first TX or RX
7703bd7f658SLukas Wunner 	 * sglist entry is not a multiple of 4 bytes (hardware limitation).
7713bd7f658SLukas Wunner 	 */
7725f336ea5SLukas Wunner 	bcm2835_spi_transfer_prologue(ctlr, tfr, bs, cs);
7733ecd37edSMartin Sperl 
7743ecd37edSMartin Sperl 	/* setup tx-DMA */
7752b8279aeSLukas Wunner 	if (bs->tx_buf) {
7768259bf66SLukas Wunner 		ret = bcm2835_spi_prepare_sg(ctlr, spi, tfr, bs, true);
7772b8279aeSLukas Wunner 	} else {
7782b8279aeSLukas Wunner 		cookie = dmaengine_submit(bs->fill_tx_desc);
7792b8279aeSLukas Wunner 		ret = dma_submit_error(cookie);
7802b8279aeSLukas Wunner 	}
7813ecd37edSMartin Sperl 	if (ret)
7823bd7f658SLukas Wunner 		goto err_reset_hw;
7833ecd37edSMartin Sperl 
7843ecd37edSMartin Sperl 	/* set the DMA length */
7853bd7f658SLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->tx_len);
7863ecd37edSMartin Sperl 
7873ecd37edSMartin Sperl 	/* start the HW */
7883ecd37edSMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS,
7893ecd37edSMartin Sperl 		   cs | BCM2835_SPI_CS_TA | BCM2835_SPI_CS_DMAEN);
7903ecd37edSMartin Sperl 
7918259bf66SLukas Wunner 	bs->tx_dma_active = true;
7928259bf66SLukas Wunner 	smp_wmb();
7938259bf66SLukas Wunner 
7948259bf66SLukas Wunner 	/* start TX early */
7958259bf66SLukas Wunner 	dma_async_issue_pending(ctlr->dma_tx);
7968259bf66SLukas Wunner 
7973ecd37edSMartin Sperl 	/* setup rx-DMA late - to run transfers while
7983ecd37edSMartin Sperl 	 * mapping of the rx buffers still takes place
7993ecd37edSMartin Sperl 	 * this saves 10us or more.
8003ecd37edSMartin Sperl 	 */
8018259bf66SLukas Wunner 	if (bs->rx_buf) {
8028259bf66SLukas Wunner 		ret = bcm2835_spi_prepare_sg(ctlr, spi, tfr, bs, false);
8038259bf66SLukas Wunner 	} else {
8048259bf66SLukas Wunner 		cookie = dmaengine_submit(bs->clear_rx_desc[spi->chip_select]);
8058259bf66SLukas Wunner 		ret = dma_submit_error(cookie);
8068259bf66SLukas Wunner 	}
8073ecd37edSMartin Sperl 	if (ret) {
8083ecd37edSMartin Sperl 		/* need to reset on errors */
8095f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_tx);
8108259bf66SLukas Wunner 		bs->tx_dma_active = false;
8113bd7f658SLukas Wunner 		goto err_reset_hw;
8123ecd37edSMartin Sperl 	}
8133ecd37edSMartin Sperl 
8143ecd37edSMartin Sperl 	/* start rx dma late */
8155f336ea5SLukas Wunner 	dma_async_issue_pending(ctlr->dma_rx);
8168259bf66SLukas Wunner 	bs->rx_dma_active = true;
8178259bf66SLukas Wunner 	smp_mb();
8188259bf66SLukas Wunner 
8198259bf66SLukas Wunner 	/*
8208259bf66SLukas Wunner 	 * In case of a very short TX-only transfer, bcm2835_spi_dma_tx_done()
8218259bf66SLukas Wunner 	 * may run before RX DMA is issued.  Terminate RX DMA if so.
8228259bf66SLukas Wunner 	 */
8238259bf66SLukas Wunner 	if (!bs->rx_buf && !bs->tx_dma_active &&
8248259bf66SLukas Wunner 	    cmpxchg(&bs->rx_dma_active, true, false)) {
8258259bf66SLukas Wunner 		dmaengine_terminate_async(ctlr->dma_rx);
826ac4648b5SRobin Murphy 		bcm2835_spi_reset_hw(bs);
8278259bf66SLukas Wunner 	}
8283ecd37edSMartin Sperl 
8293ecd37edSMartin Sperl 	/* wait for wakeup in framework */
8303ecd37edSMartin Sperl 	return 1;
8313bd7f658SLukas Wunner 
8323bd7f658SLukas Wunner err_reset_hw:
833ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
8343bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
8353bd7f658SLukas Wunner 	return ret;
8363ecd37edSMartin Sperl }
8373ecd37edSMartin Sperl 
8385f336ea5SLukas Wunner static bool bcm2835_spi_can_dma(struct spi_controller *ctlr,
8393ecd37edSMartin Sperl 				struct spi_device *spi,
8403ecd37edSMartin Sperl 				struct spi_transfer *tfr)
8413ecd37edSMartin Sperl {
8423ecd37edSMartin Sperl 	/* we start DMA efforts only on bigger transfers */
8433ecd37edSMartin Sperl 	if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
8443ecd37edSMartin Sperl 		return false;
8453ecd37edSMartin Sperl 
8463ecd37edSMartin Sperl 	/* return OK */
8473ecd37edSMartin Sperl 	return true;
8483ecd37edSMartin Sperl }
8493ecd37edSMartin Sperl 
8508259bf66SLukas Wunner static void bcm2835_dma_release(struct spi_controller *ctlr,
8518259bf66SLukas Wunner 				struct bcm2835_spi *bs)
8523ecd37edSMartin Sperl {
8538259bf66SLukas Wunner 	int i;
8548259bf66SLukas Wunner 
8555f336ea5SLukas Wunner 	if (ctlr->dma_tx) {
8565f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_tx);
8572b8279aeSLukas Wunner 
8582b8279aeSLukas Wunner 		if (bs->fill_tx_desc)
8592b8279aeSLukas Wunner 			dmaengine_desc_free(bs->fill_tx_desc);
8602b8279aeSLukas Wunner 
8612b8279aeSLukas Wunner 		if (bs->fill_tx_addr)
8622b8279aeSLukas Wunner 			dma_unmap_page_attrs(ctlr->dma_tx->device->dev,
8632b8279aeSLukas Wunner 					     bs->fill_tx_addr, sizeof(u32),
8642b8279aeSLukas Wunner 					     DMA_TO_DEVICE,
8652b8279aeSLukas Wunner 					     DMA_ATTR_SKIP_CPU_SYNC);
8662b8279aeSLukas Wunner 
8675f336ea5SLukas Wunner 		dma_release_channel(ctlr->dma_tx);
8685f336ea5SLukas Wunner 		ctlr->dma_tx = NULL;
8693ecd37edSMartin Sperl 	}
8708259bf66SLukas Wunner 
8715f336ea5SLukas Wunner 	if (ctlr->dma_rx) {
8725f336ea5SLukas Wunner 		dmaengine_terminate_sync(ctlr->dma_rx);
8738259bf66SLukas Wunner 
8748259bf66SLukas Wunner 		for (i = 0; i < BCM2835_SPI_NUM_CS; i++)
8758259bf66SLukas Wunner 			if (bs->clear_rx_desc[i])
8768259bf66SLukas Wunner 				dmaengine_desc_free(bs->clear_rx_desc[i]);
8778259bf66SLukas Wunner 
8788259bf66SLukas Wunner 		if (bs->clear_rx_addr)
8798259bf66SLukas Wunner 			dma_unmap_single(ctlr->dma_rx->device->dev,
8808259bf66SLukas Wunner 					 bs->clear_rx_addr,
8818259bf66SLukas Wunner 					 sizeof(bs->clear_rx_cs),
8828259bf66SLukas Wunner 					 DMA_TO_DEVICE);
8838259bf66SLukas Wunner 
8845f336ea5SLukas Wunner 		dma_release_channel(ctlr->dma_rx);
8855f336ea5SLukas Wunner 		ctlr->dma_rx = NULL;
8863ecd37edSMartin Sperl 	}
8873ecd37edSMartin Sperl }
8883ecd37edSMartin Sperl 
8896133fed0SPeter Ujfalusi static int bcm2835_dma_init(struct spi_controller *ctlr, struct device *dev,
8908259bf66SLukas Wunner 			    struct bcm2835_spi *bs)
8913ecd37edSMartin Sperl {
8923ecd37edSMartin Sperl 	struct dma_slave_config slave_config;
8933ecd37edSMartin Sperl 	const __be32 *addr;
8943ecd37edSMartin Sperl 	dma_addr_t dma_reg_base;
8958259bf66SLukas Wunner 	int ret, i;
8963ecd37edSMartin Sperl 
8973ecd37edSMartin Sperl 	/* base address in dma-space */
8985f336ea5SLukas Wunner 	addr = of_get_address(ctlr->dev.of_node, 0, NULL, NULL);
8993ecd37edSMartin Sperl 	if (!addr) {
9003ecd37edSMartin Sperl 		dev_err(dev, "could not get DMA-register address - not using dma mode\n");
9016133fed0SPeter Ujfalusi 		/* Fall back to interrupt mode */
9026133fed0SPeter Ujfalusi 		return 0;
9033ecd37edSMartin Sperl 	}
9043ecd37edSMartin Sperl 	dma_reg_base = be32_to_cpup(addr);
9053ecd37edSMartin Sperl 
9063ecd37edSMartin Sperl 	/* get tx/rx dma */
9076133fed0SPeter Ujfalusi 	ctlr->dma_tx = dma_request_chan(dev, "tx");
9086133fed0SPeter Ujfalusi 	if (IS_ERR(ctlr->dma_tx)) {
9093ecd37edSMartin Sperl 		dev_err(dev, "no tx-dma configuration found - not using dma mode\n");
9106133fed0SPeter Ujfalusi 		ret = PTR_ERR(ctlr->dma_tx);
9116133fed0SPeter Ujfalusi 		ctlr->dma_tx = NULL;
9123ecd37edSMartin Sperl 		goto err;
9133ecd37edSMartin Sperl 	}
9146133fed0SPeter Ujfalusi 	ctlr->dma_rx = dma_request_chan(dev, "rx");
9156133fed0SPeter Ujfalusi 	if (IS_ERR(ctlr->dma_rx)) {
9163ecd37edSMartin Sperl 		dev_err(dev, "no rx-dma configuration found - not using dma mode\n");
9176133fed0SPeter Ujfalusi 		ret = PTR_ERR(ctlr->dma_rx);
9186133fed0SPeter Ujfalusi 		ctlr->dma_rx = NULL;
9193ecd37edSMartin Sperl 		goto err_release;
9203ecd37edSMartin Sperl 	}
9213ecd37edSMartin Sperl 
9222b8279aeSLukas Wunner 	/*
9232b8279aeSLukas Wunner 	 * The TX DMA channel either copies a transfer's TX buffer to the FIFO
9242b8279aeSLukas Wunner 	 * or, in case of an RX-only transfer, cyclically copies from the zero
9252b8279aeSLukas Wunner 	 * page to the FIFO using a preallocated, reusable descriptor.
9262b8279aeSLukas Wunner 	 */
9273ecd37edSMartin Sperl 	slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
9283ecd37edSMartin Sperl 	slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9293ecd37edSMartin Sperl 
9305f336ea5SLukas Wunner 	ret = dmaengine_slave_config(ctlr->dma_tx, &slave_config);
9313ecd37edSMartin Sperl 	if (ret)
9323ecd37edSMartin Sperl 		goto err_config;
9333ecd37edSMartin Sperl 
9342b8279aeSLukas Wunner 	bs->fill_tx_addr = dma_map_page_attrs(ctlr->dma_tx->device->dev,
9352b8279aeSLukas Wunner 					      ZERO_PAGE(0), 0, sizeof(u32),
9362b8279aeSLukas Wunner 					      DMA_TO_DEVICE,
9372b8279aeSLukas Wunner 					      DMA_ATTR_SKIP_CPU_SYNC);
9382b8279aeSLukas Wunner 	if (dma_mapping_error(ctlr->dma_tx->device->dev, bs->fill_tx_addr)) {
9392b8279aeSLukas Wunner 		dev_err(dev, "cannot map zero page - not using DMA mode\n");
9402b8279aeSLukas Wunner 		bs->fill_tx_addr = 0;
941dd4441abSWei Yongjun 		ret = -ENOMEM;
9422b8279aeSLukas Wunner 		goto err_release;
9432b8279aeSLukas Wunner 	}
9442b8279aeSLukas Wunner 
9452b8279aeSLukas Wunner 	bs->fill_tx_desc = dmaengine_prep_dma_cyclic(ctlr->dma_tx,
9462b8279aeSLukas Wunner 						     bs->fill_tx_addr,
9472b8279aeSLukas Wunner 						     sizeof(u32), 0,
9482b8279aeSLukas Wunner 						     DMA_MEM_TO_DEV, 0);
9492b8279aeSLukas Wunner 	if (!bs->fill_tx_desc) {
9502b8279aeSLukas Wunner 		dev_err(dev, "cannot prepare fill_tx_desc - not using DMA mode\n");
951dd4441abSWei Yongjun 		ret = -ENOMEM;
9522b8279aeSLukas Wunner 		goto err_release;
9532b8279aeSLukas Wunner 	}
9542b8279aeSLukas Wunner 
9552b8279aeSLukas Wunner 	ret = dmaengine_desc_set_reuse(bs->fill_tx_desc);
9562b8279aeSLukas Wunner 	if (ret) {
9572b8279aeSLukas Wunner 		dev_err(dev, "cannot reuse fill_tx_desc - not using DMA mode\n");
9582b8279aeSLukas Wunner 		goto err_release;
9592b8279aeSLukas Wunner 	}
9602b8279aeSLukas Wunner 
9618259bf66SLukas Wunner 	/*
9628259bf66SLukas Wunner 	 * The RX DMA channel is used bidirectionally:  It either reads the
9638259bf66SLukas Wunner 	 * RX FIFO or, in case of a TX-only transfer, cyclically writes a
9648259bf66SLukas Wunner 	 * precalculated value to the CS register to clear the RX FIFO.
9658259bf66SLukas Wunner 	 */
9663ecd37edSMartin Sperl 	slave_config.src_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
9673ecd37edSMartin Sperl 	slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9688259bf66SLukas Wunner 	slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_CS);
9698259bf66SLukas Wunner 	slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
9703ecd37edSMartin Sperl 
9715f336ea5SLukas Wunner 	ret = dmaengine_slave_config(ctlr->dma_rx, &slave_config);
9723ecd37edSMartin Sperl 	if (ret)
9733ecd37edSMartin Sperl 		goto err_config;
9743ecd37edSMartin Sperl 
9758259bf66SLukas Wunner 	bs->clear_rx_addr = dma_map_single(ctlr->dma_rx->device->dev,
9768259bf66SLukas Wunner 					   bs->clear_rx_cs,
9778259bf66SLukas Wunner 					   sizeof(bs->clear_rx_cs),
9788259bf66SLukas Wunner 					   DMA_TO_DEVICE);
9798259bf66SLukas Wunner 	if (dma_mapping_error(ctlr->dma_rx->device->dev, bs->clear_rx_addr)) {
9808259bf66SLukas Wunner 		dev_err(dev, "cannot map clear_rx_cs - not using DMA mode\n");
9818259bf66SLukas Wunner 		bs->clear_rx_addr = 0;
982dd4441abSWei Yongjun 		ret = -ENOMEM;
9838259bf66SLukas Wunner 		goto err_release;
9848259bf66SLukas Wunner 	}
9858259bf66SLukas Wunner 
9868259bf66SLukas Wunner 	for (i = 0; i < BCM2835_SPI_NUM_CS; i++) {
9878259bf66SLukas Wunner 		bs->clear_rx_desc[i] = dmaengine_prep_dma_cyclic(ctlr->dma_rx,
9888259bf66SLukas Wunner 					   bs->clear_rx_addr + i * sizeof(u32),
9898259bf66SLukas Wunner 					   sizeof(u32), 0,
9908259bf66SLukas Wunner 					   DMA_MEM_TO_DEV, 0);
9918259bf66SLukas Wunner 		if (!bs->clear_rx_desc[i]) {
9928259bf66SLukas Wunner 			dev_err(dev, "cannot prepare clear_rx_desc - not using DMA mode\n");
993dd4441abSWei Yongjun 			ret = -ENOMEM;
9948259bf66SLukas Wunner 			goto err_release;
9958259bf66SLukas Wunner 		}
9968259bf66SLukas Wunner 
9978259bf66SLukas Wunner 		ret = dmaengine_desc_set_reuse(bs->clear_rx_desc[i]);
9988259bf66SLukas Wunner 		if (ret) {
9998259bf66SLukas Wunner 			dev_err(dev, "cannot reuse clear_rx_desc - not using DMA mode\n");
10008259bf66SLukas Wunner 			goto err_release;
10018259bf66SLukas Wunner 		}
10028259bf66SLukas Wunner 	}
10038259bf66SLukas Wunner 
10043ecd37edSMartin Sperl 	/* all went well, so set can_dma */
10055f336ea5SLukas Wunner 	ctlr->can_dma = bcm2835_spi_can_dma;
10063ecd37edSMartin Sperl 
10076133fed0SPeter Ujfalusi 	return 0;
10083ecd37edSMartin Sperl 
10093ecd37edSMartin Sperl err_config:
10103ecd37edSMartin Sperl 	dev_err(dev, "issue configuring dma: %d - not using DMA mode\n",
10113ecd37edSMartin Sperl 		ret);
10123ecd37edSMartin Sperl err_release:
10138259bf66SLukas Wunner 	bcm2835_dma_release(ctlr, bs);
10143ecd37edSMartin Sperl err:
10156133fed0SPeter Ujfalusi 	/*
10166133fed0SPeter Ujfalusi 	 * Only report error for deferred probing, otherwise fall back to
10176133fed0SPeter Ujfalusi 	 * interrupt mode
10186133fed0SPeter Ujfalusi 	 */
10196133fed0SPeter Ujfalusi 	if (ret != -EPROBE_DEFER)
10206133fed0SPeter Ujfalusi 		ret = 0;
10216133fed0SPeter Ujfalusi 
10226133fed0SPeter Ujfalusi 	return ret;
10233ecd37edSMartin Sperl }
10243ecd37edSMartin Sperl 
10255f336ea5SLukas Wunner static int bcm2835_spi_transfer_one_poll(struct spi_controller *ctlr,
1026a750b124SMartin Sperl 					 struct spi_device *spi,
1027a750b124SMartin Sperl 					 struct spi_transfer *tfr,
10289ac3f90dSMartin Sperl 					 u32 cs)
1029a750b124SMartin Sperl {
10305f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1031a750b124SMartin Sperl 	unsigned long timeout;
1032a750b124SMartin Sperl 
1033154f7da5SMartin Sperl 	/* update usage statistics */
1034154f7da5SMartin Sperl 	bs->count_transfer_polling++;
1035154f7da5SMartin Sperl 
1036a750b124SMartin Sperl 	/* enable HW block without interrupts */
1037a750b124SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
1038a750b124SMartin Sperl 
1039a750b124SMartin Sperl 	/* fill in the fifo before timeout calculations
1040a750b124SMartin Sperl 	 * if we are interrupted here, then the data is
1041a750b124SMartin Sperl 	 * getting transferred by the HW while we are interrupted
1042a750b124SMartin Sperl 	 */
10432e0733bcSLukas Wunner 	bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
1044a750b124SMartin Sperl 
1045ff245d90SMartin Sperl 	/* set the timeout to at least 2 jiffies */
1046ff245d90SMartin Sperl 	timeout = jiffies + 2 + HZ * polling_limit_us / 1000000;
1047a750b124SMartin Sperl 
1048a750b124SMartin Sperl 	/* loop until finished the transfer */
1049a750b124SMartin Sperl 	while (bs->rx_len) {
1050a750b124SMartin Sperl 		/* fill in tx fifo with remaining data */
1051a750b124SMartin Sperl 		bcm2835_wr_fifo(bs);
1052a750b124SMartin Sperl 
1053a750b124SMartin Sperl 		/* read from fifo as much as possible */
1054a750b124SMartin Sperl 		bcm2835_rd_fifo(bs);
1055a750b124SMartin Sperl 
1056a750b124SMartin Sperl 		/* if there is still data pending to read
1057a750b124SMartin Sperl 		 * then check the timeout
1058a750b124SMartin Sperl 		 */
1059a750b124SMartin Sperl 		if (bs->rx_len && time_after(jiffies, timeout)) {
1060a750b124SMartin Sperl 			dev_dbg_ratelimited(&spi->dev,
1061a750b124SMartin Sperl 					    "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
1062a750b124SMartin Sperl 					    jiffies - timeout,
1063a750b124SMartin Sperl 					    bs->tx_len, bs->rx_len);
1064a750b124SMartin Sperl 			/* fall back to interrupt mode */
1065154f7da5SMartin Sperl 
1066154f7da5SMartin Sperl 			/* update usage statistics */
1067154f7da5SMartin Sperl 			bs->count_transfer_irq_after_polling++;
1068154f7da5SMartin Sperl 
10695f336ea5SLukas Wunner 			return bcm2835_spi_transfer_one_irq(ctlr, spi,
10702e0733bcSLukas Wunner 							    tfr, cs, false);
1071a750b124SMartin Sperl 		}
1072a750b124SMartin Sperl 	}
1073a750b124SMartin Sperl 
1074a750b124SMartin Sperl 	/* Transfer complete - reset SPI HW */
1075ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
1076a750b124SMartin Sperl 	/* and return without waiting for completion */
1077a750b124SMartin Sperl 	return 0;
1078a750b124SMartin Sperl }
1079a750b124SMartin Sperl 
10805f336ea5SLukas Wunner static int bcm2835_spi_transfer_one(struct spi_controller *ctlr,
1081704f32d4SMartin Sperl 				    struct spi_device *spi,
1082704f32d4SMartin Sperl 				    struct spi_transfer *tfr)
1083704f32d4SMartin Sperl {
10845f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
10859df2003dSMartin Sperl 	unsigned long spi_hz, clk_hz, cdiv;
1086ff245d90SMartin Sperl 	unsigned long hz_per_byte, byte_limit;
1087571e31faSLukas Wunner 	u32 cs = bs->prepare_cs[spi->chip_select];
1088704f32d4SMartin Sperl 
1089704f32d4SMartin Sperl 	/* set clock */
1090704f32d4SMartin Sperl 	spi_hz = tfr->speed_hz;
1091704f32d4SMartin Sperl 	clk_hz = clk_get_rate(bs->clk);
1092704f32d4SMartin Sperl 
1093704f32d4SMartin Sperl 	if (spi_hz >= clk_hz / 2) {
1094704f32d4SMartin Sperl 		cdiv = 2; /* clk_hz/2 is the fastest we can go */
1095704f32d4SMartin Sperl 	} else if (spi_hz) {
1096704f32d4SMartin Sperl 		/* CDIV must be a multiple of two */
1097704f32d4SMartin Sperl 		cdiv = DIV_ROUND_UP(clk_hz, spi_hz);
1098704f32d4SMartin Sperl 		cdiv += (cdiv % 2);
1099704f32d4SMartin Sperl 
1100704f32d4SMartin Sperl 		if (cdiv >= 65536)
1101704f32d4SMartin Sperl 			cdiv = 0; /* 0 is the slowest we can go */
1102704f32d4SMartin Sperl 	} else {
1103704f32d4SMartin Sperl 		cdiv = 0; /* 0 is the slowest we can go */
1104704f32d4SMartin Sperl 	}
11059df2003dSMartin Sperl 	tfr->effective_speed_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536);
1106704f32d4SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
1107704f32d4SMartin Sperl 
1108acace73dSMartin Sperl 	/* handle all the 3-wire mode */
11098259bf66SLukas Wunner 	if (spi->mode & SPI_3WIRE && tfr->rx_buf)
1110704f32d4SMartin Sperl 		cs |= BCM2835_SPI_CS_REN;
1111704f32d4SMartin Sperl 
1112704f32d4SMartin Sperl 	/* set transmit buffers and length */
1113704f32d4SMartin Sperl 	bs->tx_buf = tfr->tx_buf;
1114704f32d4SMartin Sperl 	bs->rx_buf = tfr->rx_buf;
1115704f32d4SMartin Sperl 	bs->tx_len = tfr->len;
1116704f32d4SMartin Sperl 	bs->rx_len = tfr->len;
1117704f32d4SMartin Sperl 
11187f1922ebSMartin Sperl 	/* Calculate the estimated time in us the transfer runs.  Note that
11197f1922ebSMartin Sperl 	 * there is 1 idle clocks cycles after each byte getting transferred
11207f1922ebSMartin Sperl 	 * so we have 9 cycles/byte.  This is used to find the number of Hz
11217f1922ebSMartin Sperl 	 * per byte per polling limit.  E.g., we can transfer 1 byte in 30 us
11227f1922ebSMartin Sperl 	 * per 300,000 Hz of bus clock.
11237f1922ebSMartin Sperl 	 */
1124ff245d90SMartin Sperl 	hz_per_byte = polling_limit_us ? (9 * 1000000) / polling_limit_us : 0;
11259df2003dSMartin Sperl 	byte_limit = hz_per_byte ? tfr->effective_speed_hz / hz_per_byte : 1;
1126ff245d90SMartin Sperl 
11277f1922ebSMartin Sperl 	/* run in polling mode for short transfers */
1128ff245d90SMartin Sperl 	if (tfr->len < byte_limit)
11295f336ea5SLukas Wunner 		return bcm2835_spi_transfer_one_poll(ctlr, spi, tfr, cs);
1130704f32d4SMartin Sperl 
1131c41d62b0SMartin Sperl 	/* run in dma mode if conditions are right
1132c41d62b0SMartin Sperl 	 * Note that unlike poll or interrupt mode DMA mode does not have
1133c41d62b0SMartin Sperl 	 * this 1 idle clock cycle pattern but runs the spi clock without gaps
1134c41d62b0SMartin Sperl 	 */
11355f336ea5SLukas Wunner 	if (ctlr->can_dma && bcm2835_spi_can_dma(ctlr, spi, tfr))
11365f336ea5SLukas Wunner 		return bcm2835_spi_transfer_one_dma(ctlr, spi, tfr, cs);
11373ecd37edSMartin Sperl 
11383ecd37edSMartin Sperl 	/* run in interrupt-mode */
11395f336ea5SLukas Wunner 	return bcm2835_spi_transfer_one_irq(ctlr, spi, tfr, cs, true);
1140704f32d4SMartin Sperl }
1141704f32d4SMartin Sperl 
11425f336ea5SLukas Wunner static int bcm2835_spi_prepare_message(struct spi_controller *ctlr,
1143acace73dSMartin Sperl 				       struct spi_message *msg)
1144acace73dSMartin Sperl {
1145acace73dSMartin Sperl 	struct spi_device *spi = msg->spi;
11465f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
11478b7bd10eSMeghana Madhyastha 	int ret;
11488b7bd10eSMeghana Madhyastha 
11495f336ea5SLukas Wunner 	if (ctlr->can_dma) {
11508b7bd10eSMeghana Madhyastha 		/*
11513393f7d9SNicolas Saenz Julienne 		 * DMA transfers are limited to 16 bit (0 to 65535 bytes) by
11523393f7d9SNicolas Saenz Julienne 		 * the SPI HW due to DLEN. Split up transfers (32-bit FIFO
11533393f7d9SNicolas Saenz Julienne 		 * aligned) if the limit is exceeded.
11548b7bd10eSMeghana Madhyastha 		 */
11555f336ea5SLukas Wunner 		ret = spi_split_transfers_maxsize(ctlr, msg, 65532,
11568b7bd10eSMeghana Madhyastha 						  GFP_KERNEL | GFP_DMA);
11578b7bd10eSMeghana Madhyastha 		if (ret)
11588b7bd10eSMeghana Madhyastha 			return ret;
11593393f7d9SNicolas Saenz Julienne 	}
1160acace73dSMartin Sperl 
1161571e31faSLukas Wunner 	/*
1162571e31faSLukas Wunner 	 * Set up clock polarity before spi_transfer_one_message() asserts
1163571e31faSLukas Wunner 	 * chip select to avoid a gratuitous clock signal edge.
1164571e31faSLukas Wunner 	 */
1165571e31faSLukas Wunner 	bcm2835_wr(bs, BCM2835_SPI_CS, bs->prepare_cs[spi->chip_select]);
1166acace73dSMartin Sperl 
1167acace73dSMartin Sperl 	return 0;
1168acace73dSMartin Sperl }
1169acace73dSMartin Sperl 
11705f336ea5SLukas Wunner static void bcm2835_spi_handle_err(struct spi_controller *ctlr,
1171e34ff011SMartin Sperl 				   struct spi_message *msg)
1172f8043872SChris Boot {
11735f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
11743ecd37edSMartin Sperl 
11753ecd37edSMartin Sperl 	/* if an error occurred and we have an active dma, then terminate */
11765f336ea5SLukas Wunner 	dmaengine_terminate_sync(ctlr->dma_tx);
11778259bf66SLukas Wunner 	bs->tx_dma_active = false;
11785f336ea5SLukas Wunner 	dmaengine_terminate_sync(ctlr->dma_rx);
11798259bf66SLukas Wunner 	bs->rx_dma_active = false;
11803bd7f658SLukas Wunner 	bcm2835_spi_undo_prologue(bs);
11811513ceeeSLukas Wunner 
11823ecd37edSMartin Sperl 	/* and reset */
1183ac4648b5SRobin Murphy 	bcm2835_spi_reset_hw(bs);
1184f8043872SChris Boot }
1185f8043872SChris Boot 
1186a30a555dSMartin Sperl static int chip_match_name(struct gpio_chip *chip, void *data)
1187a30a555dSMartin Sperl {
1188a30a555dSMartin Sperl 	return !strcmp(chip->label, data);
1189a30a555dSMartin Sperl }
1190a30a555dSMartin Sperl 
1191e34ff011SMartin Sperl static int bcm2835_spi_setup(struct spi_device *spi)
1192e34ff011SMartin Sperl {
11938259bf66SLukas Wunner 	struct spi_controller *ctlr = spi->controller;
11948259bf66SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1195a30a555dSMartin Sperl 	struct gpio_chip *chip;
1196571e31faSLukas Wunner 	u32 cs;
1197571e31faSLukas Wunner 
1198571e31faSLukas Wunner 	/*
1199571e31faSLukas Wunner 	 * Precalculate SPI slave's CS register value for ->prepare_message():
1200571e31faSLukas Wunner 	 * The driver always uses software-controlled GPIO chip select, hence
1201571e31faSLukas Wunner 	 * set the hardware-controlled native chip select to an invalid value
1202571e31faSLukas Wunner 	 * to prevent it from interfering.
1203571e31faSLukas Wunner 	 */
1204571e31faSLukas Wunner 	cs = BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
1205571e31faSLukas Wunner 	if (spi->mode & SPI_CPOL)
1206571e31faSLukas Wunner 		cs |= BCM2835_SPI_CS_CPOL;
1207571e31faSLukas Wunner 	if (spi->mode & SPI_CPHA)
1208571e31faSLukas Wunner 		cs |= BCM2835_SPI_CS_CPHA;
1209571e31faSLukas Wunner 	bs->prepare_cs[spi->chip_select] = cs;
12103bd158c5SLinus Walleij 
1211e34ff011SMartin Sperl 	/*
12128259bf66SLukas Wunner 	 * Precalculate SPI slave's CS register value to clear RX FIFO
12138259bf66SLukas Wunner 	 * in case of a TX-only DMA transfer.
12148259bf66SLukas Wunner 	 */
12158259bf66SLukas Wunner 	if (ctlr->dma_rx) {
12168259bf66SLukas Wunner 		bs->clear_rx_cs[spi->chip_select] = cs |
12178259bf66SLukas Wunner 						    BCM2835_SPI_CS_TA |
12188259bf66SLukas Wunner 						    BCM2835_SPI_CS_DMAEN |
12198259bf66SLukas Wunner 						    BCM2835_SPI_CS_CLEAR_RX;
12208259bf66SLukas Wunner 		dma_sync_single_for_device(ctlr->dma_rx->device->dev,
12218259bf66SLukas Wunner 					   bs->clear_rx_addr,
12228259bf66SLukas Wunner 					   sizeof(bs->clear_rx_cs),
12238259bf66SLukas Wunner 					   DMA_TO_DEVICE);
12248259bf66SLukas Wunner 	}
12258259bf66SLukas Wunner 
1226e34ff011SMartin Sperl 	/*
1227e34ff011SMartin Sperl 	 * sanity checking the native-chipselects
1228e34ff011SMartin Sperl 	 */
1229e34ff011SMartin Sperl 	if (spi->mode & SPI_NO_CS)
1230f8043872SChris Boot 		return 0;
12313bd158c5SLinus Walleij 	/*
12323bd158c5SLinus Walleij 	 * The SPI core has successfully requested the CS GPIO line from the
12333bd158c5SLinus Walleij 	 * device tree, so we are done.
12343bd158c5SLinus Walleij 	 */
12353bd158c5SLinus Walleij 	if (spi->cs_gpiod)
1236e34ff011SMartin Sperl 		return 0;
1237a30a555dSMartin Sperl 	if (spi->chip_select > 1) {
1238a30a555dSMartin Sperl 		/* error in the case of native CS requested with CS > 1
1239a30a555dSMartin Sperl 		 * officially there is a CS2, but it is not documented
1240a30a555dSMartin Sperl 		 * which GPIO is connected with that...
1241a30a555dSMartin Sperl 		 */
1242a30a555dSMartin Sperl 		dev_err(&spi->dev,
1243a30a555dSMartin Sperl 			"setup: only two native chip-selects are supported\n");
1244a30a555dSMartin Sperl 		return -EINVAL;
1245a30a555dSMartin Sperl 	}
12463bd158c5SLinus Walleij 
12473bd158c5SLinus Walleij 	/*
12483bd158c5SLinus Walleij 	 * Translate native CS to GPIO
12493bd158c5SLinus Walleij 	 *
12503bd158c5SLinus Walleij 	 * FIXME: poking around in the gpiolib internals like this is
12513bd158c5SLinus Walleij 	 * not very good practice. Find a way to locate the real problem
12523bd158c5SLinus Walleij 	 * and fix it. Why is the GPIO descriptor in spi->cs_gpiod
12533bd158c5SLinus Walleij 	 * sometimes not assigned correctly? Erroneous device trees?
12543bd158c5SLinus Walleij 	 */
1255a30a555dSMartin Sperl 
1256a30a555dSMartin Sperl 	/* get the gpio chip for the base */
1257a30a555dSMartin Sperl 	chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
1258a30a555dSMartin Sperl 	if (!chip)
1259e34ff011SMartin Sperl 		return 0;
1260e34ff011SMartin Sperl 
12613bd158c5SLinus Walleij 	spi->cs_gpiod = gpiochip_request_own_desc(chip, 8 - spi->chip_select,
12623bd158c5SLinus Walleij 						  DRV_NAME,
1263bc7f2cd7SMartin Hundebøll 						  GPIO_LOOKUP_FLAGS_DEFAULT,
12643bd158c5SLinus Walleij 						  GPIOD_OUT_LOW);
12653bd158c5SLinus Walleij 	if (IS_ERR(spi->cs_gpiod))
12663bd158c5SLinus Walleij 		return PTR_ERR(spi->cs_gpiod);
1267a30a555dSMartin Sperl 
1268a30a555dSMartin Sperl 	/* and set up the "mode" and level */
12693bd158c5SLinus Walleij 	dev_info(&spi->dev, "setting up native-CS%i to use GPIO\n",
12703bd158c5SLinus Walleij 		 spi->chip_select);
1271a30a555dSMartin Sperl 
1272a30a555dSMartin Sperl 	return 0;
1273f8043872SChris Boot }
1274f8043872SChris Boot 
1275f8043872SChris Boot static int bcm2835_spi_probe(struct platform_device *pdev)
1276f8043872SChris Boot {
12775f336ea5SLukas Wunner 	struct spi_controller *ctlr;
1278f8043872SChris Boot 	struct bcm2835_spi *bs;
1279f8043872SChris Boot 	int err;
1280f8043872SChris Boot 
1281e1483ac0SLukas Wunner 	ctlr = devm_spi_alloc_master(&pdev->dev, ALIGN(sizeof(*bs),
12828259bf66SLukas Wunner 						  dma_get_cache_alignment()));
12835f336ea5SLukas Wunner 	if (!ctlr)
1284f8043872SChris Boot 		return -ENOMEM;
1285f8043872SChris Boot 
12865f336ea5SLukas Wunner 	platform_set_drvdata(pdev, ctlr);
1287f8043872SChris Boot 
12883bd158c5SLinus Walleij 	ctlr->use_gpio_descriptors = true;
12895f336ea5SLukas Wunner 	ctlr->mode_bits = BCM2835_SPI_MODE_BITS;
12905f336ea5SLukas Wunner 	ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
1291571e31faSLukas Wunner 	ctlr->num_chipselect = BCM2835_SPI_NUM_CS;
12925f336ea5SLukas Wunner 	ctlr->setup = bcm2835_spi_setup;
12935f336ea5SLukas Wunner 	ctlr->transfer_one = bcm2835_spi_transfer_one;
12945f336ea5SLukas Wunner 	ctlr->handle_err = bcm2835_spi_handle_err;
12955f336ea5SLukas Wunner 	ctlr->prepare_message = bcm2835_spi_prepare_message;
12965f336ea5SLukas Wunner 	ctlr->dev.of_node = pdev->dev.of_node;
1297f8043872SChris Boot 
12985f336ea5SLukas Wunner 	bs = spi_controller_get_devdata(ctlr);
1299afe7e363SRobin Murphy 	bs->ctlr = ctlr;
1300f8043872SChris Boot 
13016ba794dfSYueHaibing 	bs->regs = devm_platform_ioremap_resource(pdev, 0);
1302e1483ac0SLukas Wunner 	if (IS_ERR(bs->regs))
1303e1483ac0SLukas Wunner 		return PTR_ERR(bs->regs);
1304f8043872SChris Boot 
1305f8043872SChris Boot 	bs->clk = devm_clk_get(&pdev->dev, NULL);
1306e1483ac0SLukas Wunner 	if (IS_ERR(bs->clk))
1307e1483ac0SLukas Wunner 		return dev_err_probe(&pdev->dev, PTR_ERR(bs->clk),
130865acd82cSKrzysztof Kozlowski 				     "could not get clk\n");
1309f8043872SChris Boot 
1310ddf0e1c2SMartin Sperl 	bs->irq = platform_get_irq(pdev, 0);
1311e1483ac0SLukas Wunner 	if (bs->irq <= 0)
1312e1483ac0SLukas Wunner 		return bs->irq ? bs->irq : -ENODEV;
1313f8043872SChris Boot 
1314f8043872SChris Boot 	clk_prepare_enable(bs->clk);
1315f8043872SChris Boot 
13166133fed0SPeter Ujfalusi 	err = bcm2835_dma_init(ctlr, &pdev->dev, bs);
13176133fed0SPeter Ujfalusi 	if (err)
13186133fed0SPeter Ujfalusi 		goto out_clk_disable;
1319ddf0e1c2SMartin Sperl 
1320ddf0e1c2SMartin Sperl 	/* initialise the hardware with the default polarities */
1321ddf0e1c2SMartin Sperl 	bcm2835_wr(bs, BCM2835_SPI_CS,
1322ddf0e1c2SMartin Sperl 		   BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
1323ddf0e1c2SMartin Sperl 
1324d62069c2SMark Brown 	err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0,
1325afe7e363SRobin Murphy 			       dev_name(&pdev->dev), bs);
1326f8043872SChris Boot 	if (err) {
1327f8043872SChris Boot 		dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
1328666224b4SPeter Ujfalusi 		goto out_dma_release;
1329f8043872SChris Boot 	}
1330f8043872SChris Boot 
13319dd277ffSLukas Wunner 	err = spi_register_controller(ctlr);
1332f8043872SChris Boot 	if (err) {
13335f336ea5SLukas Wunner 		dev_err(&pdev->dev, "could not register SPI controller: %d\n",
13345f336ea5SLukas Wunner 			err);
1335666224b4SPeter Ujfalusi 		goto out_dma_release;
1336f8043872SChris Boot 	}
1337f8043872SChris Boot 
1338154f7da5SMartin Sperl 	bcm2835_debugfs_create(bs, dev_name(&pdev->dev));
1339154f7da5SMartin Sperl 
1340f8043872SChris Boot 	return 0;
1341f8043872SChris Boot 
1342666224b4SPeter Ujfalusi out_dma_release:
1343666224b4SPeter Ujfalusi 	bcm2835_dma_release(ctlr, bs);
1344f8043872SChris Boot out_clk_disable:
1345f8043872SChris Boot 	clk_disable_unprepare(bs->clk);
1346f8043872SChris Boot 	return err;
1347f8043872SChris Boot }
1348f8043872SChris Boot 
1349f8043872SChris Boot static int bcm2835_spi_remove(struct platform_device *pdev)
1350f8043872SChris Boot {
13515f336ea5SLukas Wunner 	struct spi_controller *ctlr = platform_get_drvdata(pdev);
13525f336ea5SLukas Wunner 	struct bcm2835_spi *bs = spi_controller_get_devdata(ctlr);
1353f8043872SChris Boot 
1354154f7da5SMartin Sperl 	bcm2835_debugfs_remove(bs);
1355154f7da5SMartin Sperl 
13569dd277ffSLukas Wunner 	spi_unregister_controller(ctlr);
13579dd277ffSLukas Wunner 
135805897c71SLukas Wunner 	bcm2835_dma_release(ctlr, bs);
135905897c71SLukas Wunner 
1360f8043872SChris Boot 	/* Clear FIFOs, and disable the HW block */
1361f8043872SChris Boot 	bcm2835_wr(bs, BCM2835_SPI_CS,
1362f8043872SChris Boot 		   BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
1363f8043872SChris Boot 
1364f8043872SChris Boot 	clk_disable_unprepare(bs->clk);
1365f8043872SChris Boot 
1366f8043872SChris Boot 	return 0;
1367f8043872SChris Boot }
1368f8043872SChris Boot 
1369118eb0e5SFlorian Fainelli static void bcm2835_spi_shutdown(struct platform_device *pdev)
1370118eb0e5SFlorian Fainelli {
1371118eb0e5SFlorian Fainelli 	int ret;
1372118eb0e5SFlorian Fainelli 
1373118eb0e5SFlorian Fainelli 	ret = bcm2835_spi_remove(pdev);
1374118eb0e5SFlorian Fainelli 	if (ret)
1375118eb0e5SFlorian Fainelli 		dev_err(&pdev->dev, "failed to shutdown\n");
1376118eb0e5SFlorian Fainelli }
1377118eb0e5SFlorian Fainelli 
1378f8043872SChris Boot static const struct of_device_id bcm2835_spi_match[] = {
1379f8043872SChris Boot 	{ .compatible = "brcm,bcm2835-spi", },
1380f8043872SChris Boot 	{}
1381f8043872SChris Boot };
1382f8043872SChris Boot MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
1383f8043872SChris Boot 
1384f8043872SChris Boot static struct platform_driver bcm2835_spi_driver = {
1385f8043872SChris Boot 	.driver		= {
1386f8043872SChris Boot 		.name		= DRV_NAME,
1387f8043872SChris Boot 		.of_match_table	= bcm2835_spi_match,
1388f8043872SChris Boot 	},
1389f8043872SChris Boot 	.probe		= bcm2835_spi_probe,
1390f8043872SChris Boot 	.remove		= bcm2835_spi_remove,
1391118eb0e5SFlorian Fainelli 	.shutdown	= bcm2835_spi_shutdown,
1392f8043872SChris Boot };
1393f8043872SChris Boot module_platform_driver(bcm2835_spi_driver);
1394f8043872SChris Boot 
1395f8043872SChris Boot MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
1396f8043872SChris Boot MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
139722bf6cd2SStefan Wahren MODULE_LICENSE("GPL");
1398