xref: /linux/drivers/spi/spi-stm32.c (revision 3974a585be78a1dc90a19d5cf1846a99954e3842)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // STMicroelectronics STM32 SPI Controller driver
4 //
5 // Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 // Author(s): Amelie Delaunay <amelie.delaunay@st.com> for STMicroelectronics.
7 
8 #include <linux/bitfield.h>
9 #include <linux/debugfs.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmaengine.h>
14 #include <linux/genalloc.h>
15 #include <linux/interrupt.h>
16 #include <linux/iopoll.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/reset.h>
23 #include <linux/spi/spi.h>
24 
25 #define DRIVER_NAME "spi_stm32"
26 
27 /* STM32F4/7 SPI registers */
28 #define STM32FX_SPI_CR1			0x00
29 #define STM32FX_SPI_CR2			0x04
30 #define STM32FX_SPI_SR			0x08
31 #define STM32FX_SPI_DR			0x0C
32 #define STM32FX_SPI_I2SCFGR		0x1C
33 
34 /* STM32FX_SPI_CR1 bit fields */
35 #define STM32FX_SPI_CR1_CPHA		BIT(0)
36 #define STM32FX_SPI_CR1_CPOL		BIT(1)
37 #define STM32FX_SPI_CR1_MSTR		BIT(2)
38 #define STM32FX_SPI_CR1_BR_SHIFT	3
39 #define STM32FX_SPI_CR1_BR		GENMASK(5, 3)
40 #define STM32FX_SPI_CR1_SPE		BIT(6)
41 #define STM32FX_SPI_CR1_LSBFRST		BIT(7)
42 #define STM32FX_SPI_CR1_SSI		BIT(8)
43 #define STM32FX_SPI_CR1_SSM		BIT(9)
44 #define STM32FX_SPI_CR1_RXONLY		BIT(10)
45 #define STM32F4_SPI_CR1_DFF		BIT(11)
46 #define STM32F7_SPI_CR1_CRCL		BIT(11)
47 #define STM32FX_SPI_CR1_CRCNEXT		BIT(12)
48 #define STM32FX_SPI_CR1_CRCEN		BIT(13)
49 #define STM32FX_SPI_CR1_BIDIOE		BIT(14)
50 #define STM32FX_SPI_CR1_BIDIMODE	BIT(15)
51 #define STM32FX_SPI_CR1_BR_MIN		0
52 #define STM32FX_SPI_CR1_BR_MAX		(GENMASK(5, 3) >> 3)
53 
54 /* STM32FX_SPI_CR2 bit fields */
55 #define STM32FX_SPI_CR2_RXDMAEN		BIT(0)
56 #define STM32FX_SPI_CR2_TXDMAEN		BIT(1)
57 #define STM32FX_SPI_CR2_SSOE		BIT(2)
58 #define STM32FX_SPI_CR2_FRF		BIT(4)
59 #define STM32FX_SPI_CR2_ERRIE		BIT(5)
60 #define STM32FX_SPI_CR2_RXNEIE		BIT(6)
61 #define STM32FX_SPI_CR2_TXEIE		BIT(7)
62 #define STM32F7_SPI_CR2_DS		GENMASK(11, 8)
63 #define STM32F7_SPI_CR2_FRXTH		BIT(12)
64 #define STM32F7_SPI_CR2_LDMA_RX		BIT(13)
65 #define STM32F7_SPI_CR2_LDMA_TX		BIT(14)
66 
67 /* STM32FX_SPI_SR bit fields */
68 #define STM32FX_SPI_SR_RXNE		BIT(0)
69 #define STM32FX_SPI_SR_TXE		BIT(1)
70 #define STM32FX_SPI_SR_CHSIDE		BIT(2)
71 #define STM32FX_SPI_SR_UDR		BIT(3)
72 #define STM32FX_SPI_SR_CRCERR		BIT(4)
73 #define STM32FX_SPI_SR_MODF		BIT(5)
74 #define STM32FX_SPI_SR_OVR		BIT(6)
75 #define STM32FX_SPI_SR_BSY		BIT(7)
76 #define STM32FX_SPI_SR_FRE		BIT(8)
77 #define STM32F7_SPI_SR_FRLVL		GENMASK(10, 9)
78 #define STM32F7_SPI_SR_FTLVL		GENMASK(12, 11)
79 
80 /* STM32FX_SPI_I2SCFGR bit fields */
81 #define STM32FX_SPI_I2SCFGR_I2SMOD	BIT(11)
82 
83 /* STM32F4 SPI Baud Rate min/max divisor */
84 #define STM32FX_SPI_BR_DIV_MIN		(2 << STM32FX_SPI_CR1_BR_MIN)
85 #define STM32FX_SPI_BR_DIV_MAX		(2 << STM32FX_SPI_CR1_BR_MAX)
86 
87 /* STM32H7 SPI registers */
88 #define STM32H7_SPI_CR1			0x00
89 #define STM32H7_SPI_CR2			0x04
90 #define STM32H7_SPI_CFG1		0x08
91 #define STM32H7_SPI_CFG2		0x0C
92 #define STM32H7_SPI_IER			0x10
93 #define STM32H7_SPI_SR			0x14
94 #define STM32H7_SPI_IFCR		0x18
95 #define STM32H7_SPI_TXDR		0x20
96 #define STM32H7_SPI_RXDR		0x30
97 #define STM32H7_SPI_I2SCFGR		0x50
98 
99 /* STM32H7_SPI_CR1 bit fields */
100 #define STM32H7_SPI_CR1_SPE		BIT(0)
101 #define STM32H7_SPI_CR1_MASRX		BIT(8)
102 #define STM32H7_SPI_CR1_CSTART		BIT(9)
103 #define STM32H7_SPI_CR1_CSUSP		BIT(10)
104 #define STM32H7_SPI_CR1_HDDIR		BIT(11)
105 #define STM32H7_SPI_CR1_SSI		BIT(12)
106 
107 /* STM32H7_SPI_CR2 bit fields */
108 #define STM32H7_SPI_CR2_TSIZE		GENMASK(15, 0)
109 #define STM32H7_SPI_TSIZE_MAX		GENMASK(15, 0)
110 
111 /* STM32H7_SPI_CFG1 bit fields */
112 #define STM32H7_SPI_CFG1_DSIZE		GENMASK(4, 0)
113 #define STM32H7_SPI_CFG1_FTHLV		GENMASK(8, 5)
114 #define STM32H7_SPI_CFG1_RXDMAEN	BIT(14)
115 #define STM32H7_SPI_CFG1_TXDMAEN	BIT(15)
116 #define STM32H7_SPI_CFG1_MBR		GENMASK(30, 28)
117 #define STM32H7_SPI_CFG1_MBR_SHIFT	28
118 #define STM32H7_SPI_CFG1_MBR_MIN	0
119 #define STM32H7_SPI_CFG1_MBR_MAX	(GENMASK(30, 28) >> 28)
120 
121 /* STM32H7_SPI_CFG2 bit fields */
122 #define STM32H7_SPI_CFG2_MIDI		GENMASK(7, 4)
123 #define STM32H7_SPI_CFG2_COMM		GENMASK(18, 17)
124 #define STM32H7_SPI_CFG2_SP		GENMASK(21, 19)
125 #define STM32H7_SPI_CFG2_MASTER		BIT(22)
126 #define STM32H7_SPI_CFG2_LSBFRST	BIT(23)
127 #define STM32H7_SPI_CFG2_CPHA		BIT(24)
128 #define STM32H7_SPI_CFG2_CPOL		BIT(25)
129 #define STM32H7_SPI_CFG2_SSM		BIT(26)
130 #define STM32H7_SPI_CFG2_SSIOP		BIT(28)
131 #define STM32H7_SPI_CFG2_AFCNTR		BIT(31)
132 
133 /* STM32H7_SPI_IER bit fields */
134 #define STM32H7_SPI_IER_RXPIE		BIT(0)
135 #define STM32H7_SPI_IER_TXPIE		BIT(1)
136 #define STM32H7_SPI_IER_DXPIE		BIT(2)
137 #define STM32H7_SPI_IER_EOTIE		BIT(3)
138 #define STM32H7_SPI_IER_TXTFIE		BIT(4)
139 #define STM32H7_SPI_IER_OVRIE		BIT(6)
140 #define STM32H7_SPI_IER_MODFIE		BIT(9)
141 #define STM32H7_SPI_IER_ALL		GENMASK(10, 0)
142 
143 /* STM32H7_SPI_SR bit fields */
144 #define STM32H7_SPI_SR_RXP		BIT(0)
145 #define STM32H7_SPI_SR_TXP		BIT(1)
146 #define STM32H7_SPI_SR_EOT		BIT(3)
147 #define STM32H7_SPI_SR_OVR		BIT(6)
148 #define STM32H7_SPI_SR_MODF		BIT(9)
149 #define STM32H7_SPI_SR_SUSP		BIT(11)
150 #define STM32H7_SPI_SR_RXPLVL		GENMASK(14, 13)
151 #define STM32H7_SPI_SR_RXWNE		BIT(15)
152 
153 /* STM32H7_SPI_IFCR bit fields */
154 #define STM32H7_SPI_IFCR_ALL		GENMASK(11, 3)
155 
156 /* STM32H7_SPI_I2SCFGR bit fields */
157 #define STM32H7_SPI_I2SCFGR_I2SMOD	BIT(0)
158 
159 /* STM32MP25_SPICFG2 bit fields */
160 #define STM32MP25_SPI_CFG2_RDIOM	BIT(13)
161 
162 /* STM32MP25 SPI registers bit fields */
163 #define STM32MP25_SPI_HWCFGR1			0x3F0
164 
165 /* STM32MP25_SPI_CR2 bit fields */
166 #define STM32MP25_SPI_TSIZE_MAX_LIMITED		GENMASK(9, 0)
167 
168 /* STM32MP25_SPI_HWCFGR1 */
169 #define STM32MP25_SPI_HWCFGR1_FULLCFG		GENMASK(27, 24)
170 #define STM32MP25_SPI_HWCFGR1_FULLCFG_LIMITED	0x0
171 #define STM32MP25_SPI_HWCFGR1_FULLCFG_FULL	0x1
172 #define STM32MP25_SPI_HWCFGR1_DSCFG		GENMASK(19, 16)
173 #define STM32MP25_SPI_HWCFGR1_DSCFG_16_B	0x0
174 #define STM32MP25_SPI_HWCFGR1_DSCFG_32_B	0x1
175 
176 /* STM32H7 SPI Master Baud Rate min/max divisor */
177 #define STM32H7_SPI_MBR_DIV_MIN		(2 << STM32H7_SPI_CFG1_MBR_MIN)
178 #define STM32H7_SPI_MBR_DIV_MAX		(2 << STM32H7_SPI_CFG1_MBR_MAX)
179 
180 /* STM32H7 SPI Communication mode */
181 #define STM32H7_SPI_FULL_DUPLEX		0
182 #define STM32H7_SPI_SIMPLEX_TX		1
183 #define STM32H7_SPI_SIMPLEX_RX		2
184 #define STM32H7_SPI_HALF_DUPLEX		3
185 
186 /* SPI Communication type */
187 #define SPI_FULL_DUPLEX		0
188 #define SPI_SIMPLEX_TX		1
189 #define SPI_SIMPLEX_RX		2
190 #define SPI_3WIRE_TX		3
191 #define SPI_3WIRE_RX		4
192 
193 #define STM32_SPI_AUTOSUSPEND_DELAY		1	/* 1 ms */
194 
195 /*
196  * use PIO for small transfers, avoiding DMA setup/teardown overhead for drivers
197  * without fifo buffers.
198  */
199 #define SPI_DMA_MIN_BYTES	16
200 
201 /* STM32 SPI driver helpers */
202 #define STM32_SPI_HOST_MODE(stm32_spi) (!(stm32_spi)->device_mode)
203 #define STM32_SPI_DEVICE_MODE(stm32_spi) ((stm32_spi)->device_mode)
204 
205 static unsigned int polling_limit_us = 30;
206 module_param(polling_limit_us, uint, 0664);
207 MODULE_PARM_DESC(polling_limit_us, "maximum time in us to run a transfer in polling mode\n");
208 
209 /**
210  * struct stm32_spi_reg - stm32 SPI register & bitfield desc
211  * @reg:		register offset
212  * @mask:		bitfield mask
213  * @shift:		left shift
214  */
215 struct stm32_spi_reg {
216 	int reg;
217 	int mask;
218 	int shift;
219 };
220 
221 /**
222  * struct stm32_spi_regspec - stm32 registers definition, compatible dependent data
223  * @en: enable register and SPI enable bit
224  * @dma_rx_en: SPI DMA RX enable register end SPI DMA RX enable bit
225  * @dma_tx_en: SPI DMA TX enable register end SPI DMA TX enable bit
226  * @cpol: clock polarity register and polarity bit
227  * @cpha: clock phase register and phase bit
228  * @lsb_first: LSB transmitted first register and bit
229  * @cs_high: chips select active value
230  * @br: baud rate register and bitfields
231  * @rx: SPI RX data register
232  * @tx: SPI TX data register
233  * @fullcfg: SPI full or limited feature set register
234  * @rdy_en: SPI ready feature register
235  */
236 struct stm32_spi_regspec {
237 	const struct stm32_spi_reg en;
238 	const struct stm32_spi_reg dma_rx_en;
239 	const struct stm32_spi_reg dma_tx_en;
240 	const struct stm32_spi_reg cpol;
241 	const struct stm32_spi_reg cpha;
242 	const struct stm32_spi_reg lsb_first;
243 	const struct stm32_spi_reg cs_high;
244 	const struct stm32_spi_reg br;
245 	const struct stm32_spi_reg rx;
246 	const struct stm32_spi_reg tx;
247 	const struct stm32_spi_reg fullcfg;
248 	const struct stm32_spi_reg rdy_en;
249 };
250 
251 struct stm32_spi;
252 
253 /**
254  * struct stm32_spi_cfg - stm32 compatible configuration data
255  * @regs: registers descriptions
256  * @get_fifo_size: routine to get fifo size
257  * @get_bpw_mask: routine to get bits per word mask
258  * @disable: routine to disable controller
259  * @config: routine to configure controller as SPI Host
260  * @set_bpw: routine to configure registers to for bits per word
261  * @set_mode: routine to configure registers to desired mode
262  * @set_data_idleness: optional routine to configure registers to desired idle
263  * time between frames (if driver has this functionality)
264  * @set_number_of_data: optional routine to configure registers to desired
265  * number of data (if driver has this functionality)
266  * @write_tx: routine to write to transmit register/FIFO
267  * @read_rx: routine to read from receive register/FIFO
268  * @transfer_one_dma_start: routine to start transfer a single spi_transfer
269  * using DMA
270  * @dma_rx_cb: routine to call after DMA RX channel operation is complete
271  * @dma_tx_cb: routine to call after DMA TX channel operation is complete
272  * @transfer_one_irq: routine to configure interrupts for driver
273  * @transfer_one_poll: routine to perform a transfer via register polling
274  * @irq_handler_event: Interrupt handler for SPI controller events
275  * @irq_handler_thread: thread of interrupt handler for SPI controller
276  * @baud_rate_div_min: minimum baud rate divisor
277  * @baud_rate_div_max: maximum baud rate divisor
278  * @has_fifo: boolean to know if fifo is used for driver
279  * @has_device_mode: is this compatible capable to switch on device mode
280  * @flags: compatible specific SPI controller flags used at registration time
281  * @prevent_dma_burst: boolean to indicate to prevent DMA burst
282  */
283 struct stm32_spi_cfg {
284 	const struct stm32_spi_regspec *regs;
285 	int (*get_fifo_size)(struct stm32_spi *spi);
286 	int (*get_bpw_mask)(struct stm32_spi *spi);
287 	void (*disable)(struct stm32_spi *spi);
288 	int (*config)(struct stm32_spi *spi);
289 	void (*set_bpw)(struct stm32_spi *spi);
290 	int (*set_mode)(struct stm32_spi *spi, unsigned int comm_type);
291 	void (*set_data_idleness)(struct stm32_spi *spi, struct spi_transfer *xfer);
292 	int (*set_number_of_data)(struct stm32_spi *spi, u32 length);
293 	void (*write_tx)(struct stm32_spi *spi);
294 	void (*read_rx)(struct stm32_spi *spi);
295 	void (*transfer_one_dma_start)(struct stm32_spi *spi);
296 	void (*dma_rx_cb)(void *data);
297 	void (*dma_tx_cb)(void *data);
298 	int (*transfer_one_irq)(struct stm32_spi *spi);
299 	int (*transfer_one_poll)(struct stm32_spi *spi);
300 	irqreturn_t (*irq_handler_event)(int irq, void *dev_id);
301 	irqreturn_t (*irq_handler_thread)(int irq, void *dev_id);
302 	unsigned int baud_rate_div_min;
303 	unsigned int baud_rate_div_max;
304 	bool has_fifo;
305 	bool has_device_mode;
306 	u16 flags;
307 	bool prevent_dma_burst;
308 };
309 
310 /**
311  * struct stm32_spi - private data of the SPI controller
312  * @dev: driver model representation of the controller
313  * @ctrl: controller interface
314  * @cfg: compatible configuration data
315  * @base: virtual memory area
316  * @clk: hw kernel clock feeding the SPI clock generator
317  * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator
318  * @lock: prevent I/O concurrent access
319  * @irq: SPI controller interrupt line
320  * @fifo_size: size of the embedded fifo in bytes
321  * @t_size_max: maximum number of data of one transfer
322  * @feature_set: SPI full or limited feature set
323  * @cur_midi: host inter-data idleness in ns
324  * @cur_speed: speed configured in Hz
325  * @cur_half_period: time of a half bit in us
326  * @cur_bpw: number of bits in a single SPI data frame
327  * @cur_fthlv: fifo threshold level (data frames in a single data packet)
328  * @cur_comm: SPI communication mode
329  * @cur_xferlen: current transfer length in bytes
330  * @cur_usedma: boolean to know if dma is used in current transfer
331  * @tx_buf: data to be written, or NULL
332  * @rx_buf: data to be read, or NULL
333  * @tx_len: number of data to be written in bytes
334  * @rx_len: number of data to be read in bytes
335  * @dma_tx: dma channel for TX transfer
336  * @dma_rx: dma channel for RX transfer
337  * @phys_addr: SPI registers physical base address
338  * @device_mode: the controller is configured as SPI device
339  * @sram_pool: SRAM pool for DMA transfers
340  * @sram_rx_buf_size: size of SRAM buffer for RX transfer
341  * @sram_rx_buf: SRAM buffer for RX transfer
342  * @sram_dma_rx_buf: SRAM buffer physical address for RX transfer
343  * @mdma_rx: MDMA channel for RX transfer
344  */
345 struct stm32_spi {
346 	struct device *dev;
347 	struct spi_controller *ctrl;
348 	const struct stm32_spi_cfg *cfg;
349 	void __iomem *base;
350 	struct clk *clk;
351 	u32 clk_rate;
352 	spinlock_t lock; /* prevent I/O concurrent access */
353 	int irq;
354 	unsigned int fifo_size;
355 	unsigned int t_size_max;
356 	unsigned int feature_set;
357 #define STM32_SPI_FEATURE_LIMITED	STM32MP25_SPI_HWCFGR1_FULLCFG_LIMITED	/* 0x0 */
358 #define STM32_SPI_FEATURE_FULL		STM32MP25_SPI_HWCFGR1_FULLCFG_FULL	/* 0x1 */
359 
360 	unsigned int cur_midi;
361 	unsigned int cur_speed;
362 	unsigned int cur_half_period;
363 	unsigned int cur_bpw;
364 	unsigned int cur_fthlv;
365 	unsigned int cur_comm;
366 	unsigned int cur_xferlen;
367 	bool cur_usedma;
368 
369 	const void *tx_buf;
370 	void *rx_buf;
371 	int tx_len;
372 	int rx_len;
373 	struct dma_chan *dma_tx;
374 	struct dma_chan *dma_rx;
375 	dma_addr_t phys_addr;
376 
377 	bool device_mode;
378 
379 	struct gen_pool *sram_pool;
380 	size_t sram_rx_buf_size;
381 	void *sram_rx_buf;
382 	dma_addr_t sram_dma_rx_buf;
383 	struct dma_chan *mdma_rx;
384 };
385 
386 static const struct stm32_spi_regspec stm32fx_spi_regspec = {
387 	.en = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_SPE },
388 
389 	.dma_rx_en = { STM32FX_SPI_CR2, STM32FX_SPI_CR2_RXDMAEN },
390 	.dma_tx_en = { STM32FX_SPI_CR2, STM32FX_SPI_CR2_TXDMAEN },
391 
392 	.cpol = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_CPOL },
393 	.cpha = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_CPHA },
394 	.lsb_first = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_LSBFRST },
395 	.cs_high = {},
396 	.br = { STM32FX_SPI_CR1, STM32FX_SPI_CR1_BR, STM32FX_SPI_CR1_BR_SHIFT },
397 
398 	.rx = { STM32FX_SPI_DR },
399 	.tx = { STM32FX_SPI_DR },
400 };
401 
402 static const struct stm32_spi_regspec stm32h7_spi_regspec = {
403 	/* SPI data transfer is enabled but spi_ker_ck is idle.
404 	 * CFG1 and CFG2 registers are write protected when SPE is enabled.
405 	 */
406 	.en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
407 
408 	.dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
409 	.dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
410 
411 	.cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
412 	.cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
413 	.lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
414 	.cs_high = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_SSIOP },
415 	.br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
416 		STM32H7_SPI_CFG1_MBR_SHIFT },
417 
418 	.rx = { STM32H7_SPI_RXDR },
419 	.tx = { STM32H7_SPI_TXDR },
420 };
421 
422 static const struct stm32_spi_regspec stm32mp25_spi_regspec = {
423 	/* SPI data transfer is enabled but spi_ker_ck is idle.
424 	 * CFG1 and CFG2 registers are write protected when SPE is enabled.
425 	 */
426 	.en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
427 
428 	.dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
429 	.dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
430 
431 	.cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
432 	.cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
433 	.lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
434 	.cs_high = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_SSIOP },
435 	.br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
436 		STM32H7_SPI_CFG1_MBR_SHIFT },
437 
438 	.rx = { STM32H7_SPI_RXDR },
439 	.tx = { STM32H7_SPI_TXDR },
440 
441 	.fullcfg = { STM32MP25_SPI_HWCFGR1, STM32MP25_SPI_HWCFGR1_FULLCFG },
442 
443 	.rdy_en = { STM32H7_SPI_CFG2, STM32MP25_SPI_CFG2_RDIOM },
444 };
445 
446 static inline void stm32_spi_set_bits(struct stm32_spi *spi,
447 				      u32 offset, u32 bits)
448 {
449 	writel_relaxed(readl_relaxed(spi->base + offset) | bits,
450 		       spi->base + offset);
451 }
452 
453 static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
454 				      u32 offset, u32 bits)
455 {
456 	writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
457 		       spi->base + offset);
458 }
459 
460 /**
461  * stm32h7_spi_get_fifo_size - Return fifo size
462  * @spi: pointer to the spi controller data structure
463  */
464 static int stm32h7_spi_get_fifo_size(struct stm32_spi *spi)
465 {
466 	unsigned long flags;
467 	u32 count = 0;
468 
469 	spin_lock_irqsave(&spi->lock, flags);
470 
471 	stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
472 
473 	while (readl_relaxed(spi->base + STM32H7_SPI_SR) & STM32H7_SPI_SR_TXP)
474 		writeb_relaxed(++count, spi->base + STM32H7_SPI_TXDR);
475 
476 	stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
477 
478 	spin_unlock_irqrestore(&spi->lock, flags);
479 
480 	dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
481 
482 	return count;
483 }
484 
485 /**
486  * stm32f4_spi_get_bpw_mask - Return bits per word mask
487  * @spi: pointer to the spi controller data structure
488  */
489 static int stm32f4_spi_get_bpw_mask(struct stm32_spi *spi)
490 {
491 	dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
492 	return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
493 }
494 
495 /**
496  * stm32f7_spi_get_bpw_mask - Return bits per word mask
497  * @spi: pointer to the spi controller data structure
498  */
499 static int stm32f7_spi_get_bpw_mask(struct stm32_spi *spi)
500 {
501 	dev_dbg(spi->dev, "16-bit maximum data frame\n");
502 	return SPI_BPW_RANGE_MASK(4, 16);
503 }
504 
505 /**
506  * stm32h7_spi_get_bpw_mask - Return bits per word mask
507  * @spi: pointer to the spi controller data structure
508  */
509 static int stm32h7_spi_get_bpw_mask(struct stm32_spi *spi)
510 {
511 	unsigned long flags;
512 	u32 cfg1, max_bpw;
513 
514 	spin_lock_irqsave(&spi->lock, flags);
515 
516 	/*
517 	 * The most significant bit at DSIZE bit field is reserved when the
518 	 * maximum data size of periperal instances is limited to 16-bit
519 	 */
520 	stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE);
521 
522 	cfg1 = readl_relaxed(spi->base + STM32H7_SPI_CFG1);
523 	max_bpw = FIELD_GET(STM32H7_SPI_CFG1_DSIZE, cfg1) + 1;
524 
525 	spin_unlock_irqrestore(&spi->lock, flags);
526 
527 	dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
528 
529 	return SPI_BPW_RANGE_MASK(4, max_bpw);
530 }
531 
532 /**
533  * stm32mp25_spi_get_bpw_mask - Return bits per word mask
534  * @spi: pointer to the spi controller data structure
535  */
536 static int stm32mp25_spi_get_bpw_mask(struct stm32_spi *spi)
537 {
538 	u32 dscfg, max_bpw;
539 
540 	if (spi->feature_set == STM32_SPI_FEATURE_LIMITED) {
541 		dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
542 		return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
543 	}
544 
545 	dscfg = FIELD_GET(STM32MP25_SPI_HWCFGR1_DSCFG,
546 			  readl_relaxed(spi->base + STM32MP25_SPI_HWCFGR1));
547 	max_bpw = 16;
548 	if (dscfg == STM32MP25_SPI_HWCFGR1_DSCFG_32_B)
549 		max_bpw = 32;
550 	dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
551 	return SPI_BPW_RANGE_MASK(4, max_bpw);
552 }
553 
554 /**
555  * stm32_spi_prepare_mbr - Determine baud rate divisor value
556  * @spi: pointer to the spi controller data structure
557  * @speed_hz: requested speed
558  * @min_div: minimum baud rate divisor
559  * @max_div: maximum baud rate divisor
560  *
561  * Return baud rate divisor value in case of success or -EINVAL
562  */
563 static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
564 				 u32 min_div, u32 max_div)
565 {
566 	u32 div, mbrdiv;
567 
568 	/* Ensure spi->clk_rate is even */
569 	div = DIV_ROUND_CLOSEST(spi->clk_rate & ~0x1, speed_hz);
570 
571 	/*
572 	 * SPI framework set xfer->speed_hz to ctrl->max_speed_hz if
573 	 * xfer->speed_hz is greater than ctrl->max_speed_hz, and it returns
574 	 * an error when xfer->speed_hz is lower than ctrl->min_speed_hz, so
575 	 * no need to check it there.
576 	 * However, we need to ensure the following calculations.
577 	 */
578 	if ((div < min_div) || (div > max_div))
579 		return -EINVAL;
580 
581 	/* Determine the first power of 2 greater than or equal to div */
582 	if (div & (div - 1))
583 		mbrdiv = fls(div);
584 	else
585 		mbrdiv = fls(div) - 1;
586 
587 	spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
588 
589 	spi->cur_half_period = DIV_ROUND_CLOSEST(USEC_PER_SEC, 2 * spi->cur_speed);
590 
591 	return mbrdiv - 1;
592 }
593 
594 /**
595  * stm32h7_spi_prepare_fthlv - Determine FIFO threshold level
596  * @spi: pointer to the spi controller data structure
597  * @xfer_len: length of the message to be transferred
598  */
599 static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi, u32 xfer_len)
600 {
601 	u32 packet, bpw;
602 
603 	/* data packet should not exceed 1/2 of fifo space */
604 	packet = clamp(xfer_len, 1U, spi->fifo_size / 2);
605 
606 	/* align packet size with data registers access */
607 	bpw = DIV_ROUND_UP(spi->cur_bpw, 8);
608 	return DIV_ROUND_UP(packet, bpw);
609 }
610 
611 /**
612  * stm32f4_spi_write_tx - Write bytes to Transmit Data Register
613  * @spi: pointer to the spi controller data structure
614  *
615  * Read from tx_buf depends on remaining bytes to avoid to read beyond
616  * tx_buf end.
617  */
618 static void stm32f4_spi_write_tx(struct stm32_spi *spi)
619 {
620 	if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32FX_SPI_SR) &
621 				  STM32FX_SPI_SR_TXE)) {
622 		u32 offs = spi->cur_xferlen - spi->tx_len;
623 
624 		if (spi->cur_bpw == 16) {
625 			const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
626 
627 			writew_relaxed(*tx_buf16, spi->base + STM32FX_SPI_DR);
628 			spi->tx_len -= sizeof(u16);
629 		} else {
630 			const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
631 
632 			writeb_relaxed(*tx_buf8, spi->base + STM32FX_SPI_DR);
633 			spi->tx_len -= sizeof(u8);
634 		}
635 	}
636 
637 	dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
638 }
639 
640 /**
641  * stm32f7_spi_write_tx - Write bytes to Transmit Data Register
642  * @spi: pointer to the spi controller data structure
643  *
644  * Read from tx_buf depends on remaining bytes to avoid to read beyond
645  * tx_buf end.
646  */
647 static void stm32f7_spi_write_tx(struct stm32_spi *spi)
648 {
649 	if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32FX_SPI_SR) &
650 				  STM32FX_SPI_SR_TXE)) {
651 		u32 offs = spi->cur_xferlen - spi->tx_len;
652 
653 		if (spi->tx_len >= sizeof(u16)) {
654 			const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
655 
656 			writew_relaxed(*tx_buf16, spi->base + STM32FX_SPI_DR);
657 			spi->tx_len -= sizeof(u16);
658 		} else {
659 			const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
660 
661 			writeb_relaxed(*tx_buf8, spi->base + STM32FX_SPI_DR);
662 			spi->tx_len -= sizeof(u8);
663 		}
664 	}
665 
666 	dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
667 }
668 
669 /**
670  * stm32h7_spi_write_txfifo - Write bytes in Transmit Data Register
671  * @spi: pointer to the spi controller data structure
672  *
673  * Read from tx_buf depends on remaining bytes to avoid to read beyond
674  * tx_buf end.
675  */
676 static void stm32h7_spi_write_txfifo(struct stm32_spi *spi)
677 {
678 	while ((spi->tx_len > 0) &&
679 		       (readl_relaxed(spi->base + STM32H7_SPI_SR) &
680 			STM32H7_SPI_SR_TXP)) {
681 		u32 offs = spi->cur_xferlen - spi->tx_len;
682 
683 		if (spi->tx_len >= sizeof(u32)) {
684 			const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
685 
686 			writel_relaxed(*tx_buf32, spi->base + STM32H7_SPI_TXDR);
687 			spi->tx_len -= sizeof(u32);
688 		} else if (spi->tx_len >= sizeof(u16)) {
689 			const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
690 
691 			writew_relaxed(*tx_buf16, spi->base + STM32H7_SPI_TXDR);
692 			spi->tx_len -= sizeof(u16);
693 		} else {
694 			const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
695 
696 			writeb_relaxed(*tx_buf8, spi->base + STM32H7_SPI_TXDR);
697 			spi->tx_len -= sizeof(u8);
698 		}
699 	}
700 
701 	dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
702 }
703 
704 /**
705  * stm32f4_spi_read_rx - Read bytes from Receive Data Register
706  * @spi: pointer to the spi controller data structure
707  *
708  * Write in rx_buf depends on remaining bytes to avoid to write beyond
709  * rx_buf end.
710  */
711 static void stm32f4_spi_read_rx(struct stm32_spi *spi)
712 {
713 	if ((spi->rx_len > 0) && (readl_relaxed(spi->base + STM32FX_SPI_SR) &
714 				  STM32FX_SPI_SR_RXNE)) {
715 		u32 offs = spi->cur_xferlen - spi->rx_len;
716 
717 		if (spi->cur_bpw == 16) {
718 			u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
719 
720 			*rx_buf16 = readw_relaxed(spi->base + STM32FX_SPI_DR);
721 			spi->rx_len -= sizeof(u16);
722 		} else {
723 			u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
724 
725 			*rx_buf8 = readb_relaxed(spi->base + STM32FX_SPI_DR);
726 			spi->rx_len -= sizeof(u8);
727 		}
728 	}
729 
730 	dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->rx_len);
731 }
732 
733 /**
734  * stm32f7_spi_read_rx - Read bytes from Receive Data Register
735  * @spi: pointer to the spi controller data structure
736  *
737  * Write in rx_buf depends on remaining bytes to avoid to write beyond
738  * rx_buf end.
739  */
740 static void stm32f7_spi_read_rx(struct stm32_spi *spi)
741 {
742 	u32 sr = readl_relaxed(spi->base + STM32FX_SPI_SR);
743 	u32 frlvl = FIELD_GET(STM32F7_SPI_SR_FRLVL, sr);
744 
745 	while ((spi->rx_len > 0) && (frlvl > 0)) {
746 		u32 offs = spi->cur_xferlen - spi->rx_len;
747 
748 		if ((spi->rx_len >= sizeof(u16)) && (frlvl >= 2)) {
749 			u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
750 
751 			*rx_buf16 = readw_relaxed(spi->base + STM32FX_SPI_DR);
752 			spi->rx_len -= sizeof(u16);
753 		} else {
754 			u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
755 
756 			*rx_buf8 = readb_relaxed(spi->base + STM32FX_SPI_DR);
757 			spi->rx_len -= sizeof(u8);
758 		}
759 
760 		sr = readl_relaxed(spi->base + STM32FX_SPI_SR);
761 		frlvl = FIELD_GET(STM32F7_SPI_SR_FRLVL, sr);
762 	}
763 
764 	if (spi->rx_len >= sizeof(u16))
765 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
766 	else
767 		stm32_spi_set_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
768 
769 	dev_dbg(spi->dev, "%s: %d bytes left (sr=%08x)\n",
770 		__func__, spi->rx_len, sr);
771 }
772 
773 /**
774  * stm32h7_spi_read_rxfifo - Read bytes in Receive Data Register
775  * @spi: pointer to the spi controller data structure
776  *
777  * Write in rx_buf depends on remaining bytes to avoid to write beyond
778  * rx_buf end.
779  */
780 static void stm32h7_spi_read_rxfifo(struct stm32_spi *spi)
781 {
782 	u32 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
783 	u32 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
784 
785 	while ((spi->rx_len > 0) &&
786 	       ((sr & STM32H7_SPI_SR_RXP) ||
787 		((sr & STM32H7_SPI_SR_EOT) &&
788 		 ((sr & STM32H7_SPI_SR_RXWNE) || (rxplvl > 0))))) {
789 		u32 offs = spi->cur_xferlen - spi->rx_len;
790 
791 		if ((spi->rx_len >= sizeof(u32)) ||
792 		    (sr & STM32H7_SPI_SR_RXWNE)) {
793 			u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
794 
795 			*rx_buf32 = readl_relaxed(spi->base + STM32H7_SPI_RXDR);
796 			spi->rx_len -= sizeof(u32);
797 		} else if ((spi->rx_len >= sizeof(u16)) ||
798 			   (!(sr & STM32H7_SPI_SR_RXWNE) &&
799 			    (rxplvl >= 2 || spi->cur_bpw > 8))) {
800 			u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
801 
802 			*rx_buf16 = readw_relaxed(spi->base + STM32H7_SPI_RXDR);
803 			spi->rx_len -= sizeof(u16);
804 		} else {
805 			u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
806 
807 			*rx_buf8 = readb_relaxed(spi->base + STM32H7_SPI_RXDR);
808 			spi->rx_len -= sizeof(u8);
809 		}
810 
811 		sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
812 		rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
813 	}
814 
815 	dev_dbg(spi->dev, "%s: %d bytes left (sr=%08x)\n",
816 		__func__, spi->rx_len, sr);
817 }
818 
819 /**
820  * stm32_spi_enable - Enable SPI controller
821  * @spi: pointer to the spi controller data structure
822  */
823 static void stm32_spi_enable(struct stm32_spi *spi)
824 {
825 	dev_dbg(spi->dev, "enable controller\n");
826 
827 	stm32_spi_set_bits(spi, spi->cfg->regs->en.reg,
828 			   spi->cfg->regs->en.mask);
829 }
830 
831 /**
832  * stm32fx_spi_disable - Disable SPI controller
833  * @spi: pointer to the spi controller data structure
834  */
835 static void stm32fx_spi_disable(struct stm32_spi *spi)
836 {
837 	unsigned long flags;
838 	u32 sr;
839 
840 	dev_dbg(spi->dev, "disable controller\n");
841 
842 	spin_lock_irqsave(&spi->lock, flags);
843 
844 	if (!(readl_relaxed(spi->base + STM32FX_SPI_CR1) &
845 	      STM32FX_SPI_CR1_SPE)) {
846 		spin_unlock_irqrestore(&spi->lock, flags);
847 		return;
848 	}
849 
850 	/* Disable interrupts */
851 	stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32FX_SPI_CR2_TXEIE |
852 						 STM32FX_SPI_CR2_RXNEIE |
853 						 STM32FX_SPI_CR2_ERRIE);
854 
855 	/* Wait until BSY = 0 */
856 	if (readl_relaxed_poll_timeout_atomic(spi->base + STM32FX_SPI_SR,
857 					      sr, !(sr & STM32FX_SPI_SR_BSY),
858 					      10, 100000) < 0) {
859 		dev_warn(spi->dev, "disabling condition timeout\n");
860 	}
861 
862 	if (spi->cur_usedma && spi->dma_tx)
863 		dmaengine_terminate_async(spi->dma_tx);
864 	if (spi->cur_usedma && spi->dma_rx)
865 		dmaengine_terminate_async(spi->dma_rx);
866 
867 	stm32_spi_clr_bits(spi, STM32FX_SPI_CR1, STM32FX_SPI_CR1_SPE);
868 
869 	stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32FX_SPI_CR2_TXDMAEN |
870 						 STM32FX_SPI_CR2_RXDMAEN);
871 
872 	/* Sequence to clear OVR flag */
873 	readl_relaxed(spi->base + STM32FX_SPI_DR);
874 	readl_relaxed(spi->base + STM32FX_SPI_SR);
875 
876 	spin_unlock_irqrestore(&spi->lock, flags);
877 }
878 
879 /**
880  * stm32h7_spi_disable - Disable SPI controller
881  * @spi: pointer to the spi controller data structure
882  *
883  * RX-Fifo is flushed when SPI controller is disabled.
884  */
885 static void stm32h7_spi_disable(struct stm32_spi *spi)
886 {
887 	unsigned long flags;
888 	u32 cr1;
889 
890 	dev_dbg(spi->dev, "disable controller\n");
891 
892 	spin_lock_irqsave(&spi->lock, flags);
893 
894 	cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1);
895 
896 	if (!(cr1 & STM32H7_SPI_CR1_SPE)) {
897 		spin_unlock_irqrestore(&spi->lock, flags);
898 		return;
899 	}
900 
901 	/* Add a delay to make sure that transmission is ended. */
902 	if (spi->cur_half_period)
903 		udelay(spi->cur_half_period);
904 
905 	if (spi->cur_usedma && spi->dma_tx)
906 		dmaengine_terminate_async(spi->dma_tx);
907 	if (spi->cur_usedma && spi->dma_rx) {
908 		dmaengine_terminate_async(spi->dma_rx);
909 		if (spi->mdma_rx)
910 			dmaengine_terminate_async(spi->mdma_rx);
911 	}
912 
913 	stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
914 
915 	stm32_spi_clr_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN |
916 						STM32H7_SPI_CFG1_RXDMAEN);
917 
918 	/* Disable interrupts and clear status flags */
919 	writel_relaxed(0, spi->base + STM32H7_SPI_IER);
920 	writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR);
921 
922 	spin_unlock_irqrestore(&spi->lock, flags);
923 }
924 
925 /**
926  * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use
927  * @ctrl: controller interface
928  * @spi_dev: pointer to the spi device
929  * @transfer: pointer to spi transfer
930  *
931  * If driver has fifo and the current transfer size is greater than fifo size,
932  * use DMA. Otherwise use DMA for transfer longer than defined DMA min bytes.
933  */
934 static bool stm32_spi_can_dma(struct spi_controller *ctrl,
935 			      struct spi_device *spi_dev,
936 			      struct spi_transfer *transfer)
937 {
938 	unsigned int dma_size;
939 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
940 
941 	if (spi->cfg->has_fifo)
942 		dma_size = spi->fifo_size;
943 	else
944 		dma_size = SPI_DMA_MIN_BYTES;
945 
946 	dev_dbg(spi->dev, "%s: %s\n", __func__,
947 		(transfer->len > dma_size) ? "true" : "false");
948 
949 	return (transfer->len > dma_size);
950 }
951 
952 /**
953  * stm32fx_spi_irq_event - Interrupt handler for SPI controller events
954  * @irq: interrupt line
955  * @dev_id: SPI controller ctrl interface
956  */
957 static irqreturn_t stm32fx_spi_irq_event(int irq, void *dev_id)
958 {
959 	struct spi_controller *ctrl = dev_id;
960 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
961 	u32 sr, mask = 0;
962 	bool end = false;
963 
964 	spin_lock(&spi->lock);
965 
966 	sr = readl_relaxed(spi->base + STM32FX_SPI_SR);
967 	/*
968 	 * BSY flag is not handled in interrupt but it is normal behavior when
969 	 * this flag is set.
970 	 */
971 	sr &= ~STM32FX_SPI_SR_BSY;
972 
973 	if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX ||
974 				 spi->cur_comm == SPI_3WIRE_TX)) {
975 		/* OVR flag shouldn't be handled for TX only mode */
976 		sr &= ~(STM32FX_SPI_SR_OVR | STM32FX_SPI_SR_RXNE);
977 		mask |= STM32FX_SPI_SR_TXE;
978 	}
979 
980 	if (!spi->cur_usedma && (spi->cur_comm == SPI_FULL_DUPLEX ||
981 				spi->cur_comm == SPI_SIMPLEX_RX ||
982 				spi->cur_comm == SPI_3WIRE_RX)) {
983 		/* TXE flag is set and is handled when RXNE flag occurs */
984 		sr &= ~STM32FX_SPI_SR_TXE;
985 		mask |= STM32FX_SPI_SR_RXNE | STM32FX_SPI_SR_OVR;
986 	}
987 
988 	if (!(sr & mask)) {
989 		dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr);
990 		spin_unlock(&spi->lock);
991 		return IRQ_NONE;
992 	}
993 
994 	if (sr & STM32FX_SPI_SR_OVR) {
995 		dev_warn(spi->dev, "Overrun: received value discarded\n");
996 
997 		/* Sequence to clear OVR flag */
998 		readl_relaxed(spi->base + STM32FX_SPI_DR);
999 		readl_relaxed(spi->base + STM32FX_SPI_SR);
1000 
1001 		/*
1002 		 * If overrun is detected, it means that something went wrong,
1003 		 * so stop the current transfer. Transfer can wait for next
1004 		 * RXNE but DR is already read and end never happens.
1005 		 */
1006 		end = true;
1007 		goto end_irq;
1008 	}
1009 
1010 	if (sr & STM32FX_SPI_SR_TXE) {
1011 		if (spi->tx_buf)
1012 			spi->cfg->write_tx(spi);
1013 		if (spi->tx_len == 0)
1014 			end = true;
1015 	}
1016 
1017 	if (sr & STM32FX_SPI_SR_RXNE) {
1018 		spi->cfg->read_rx(spi);
1019 		if (spi->rx_len == 0)
1020 			end = true;
1021 		else if (spi->tx_buf)/* Load data for discontinuous mode */
1022 			spi->cfg->write_tx(spi);
1023 	}
1024 
1025 end_irq:
1026 	if (end) {
1027 		/* Immediately disable interrupts to do not generate new one */
1028 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR2,
1029 					STM32FX_SPI_CR2_TXEIE |
1030 					STM32FX_SPI_CR2_RXNEIE |
1031 					STM32FX_SPI_CR2_ERRIE);
1032 		spin_unlock(&spi->lock);
1033 		return IRQ_WAKE_THREAD;
1034 	}
1035 
1036 	spin_unlock(&spi->lock);
1037 	return IRQ_HANDLED;
1038 }
1039 
1040 /**
1041  * stm32fx_spi_irq_thread - Thread of interrupt handler for SPI controller
1042  * @irq: interrupt line
1043  * @dev_id: SPI controller interface
1044  */
1045 static irqreturn_t stm32fx_spi_irq_thread(int irq, void *dev_id)
1046 {
1047 	struct spi_controller *ctrl = dev_id;
1048 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
1049 
1050 	spi_finalize_current_transfer(ctrl);
1051 	stm32fx_spi_disable(spi);
1052 
1053 	return IRQ_HANDLED;
1054 }
1055 
1056 /**
1057  * stm32h7_spi_irq_thread - Thread of interrupt handler for SPI controller
1058  * @irq: interrupt line
1059  * @dev_id: SPI controller interface
1060  */
1061 static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id)
1062 {
1063 	struct spi_controller *ctrl = dev_id;
1064 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
1065 	u32 sr, ier, mask;
1066 	unsigned long flags;
1067 	bool end = false;
1068 
1069 	spin_lock_irqsave(&spi->lock, flags);
1070 
1071 	sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
1072 	ier = readl_relaxed(spi->base + STM32H7_SPI_IER);
1073 
1074 	mask = ier;
1075 	/*
1076 	 * EOTIE enables irq from EOT, SUSP and TXC events. We need to set
1077 	 * SUSP to acknowledge it later. TXC is automatically cleared
1078 	 */
1079 
1080 	mask |= STM32H7_SPI_SR_SUSP;
1081 	/*
1082 	 * DXPIE is set in Full-Duplex, one IT will be raised if TXP and RXP
1083 	 * are set. So in case of Full-Duplex, need to poll TXP and RXP event.
1084 	 */
1085 	if ((spi->cur_comm == SPI_FULL_DUPLEX) && !spi->cur_usedma)
1086 		mask |= STM32H7_SPI_SR_TXP | STM32H7_SPI_SR_RXP;
1087 
1088 	if (!(sr & mask)) {
1089 		dev_vdbg(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
1090 			 sr, ier);
1091 		spin_unlock_irqrestore(&spi->lock, flags);
1092 		return IRQ_NONE;
1093 	}
1094 
1095 	if (sr & STM32H7_SPI_SR_SUSP) {
1096 		static DEFINE_RATELIMIT_STATE(rs,
1097 					      DEFAULT_RATELIMIT_INTERVAL * 10,
1098 					      1);
1099 		ratelimit_set_flags(&rs, RATELIMIT_MSG_ON_RELEASE);
1100 		if (__ratelimit(&rs))
1101 			dev_dbg_ratelimited(spi->dev, "Communication suspended\n");
1102 		if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
1103 			stm32h7_spi_read_rxfifo(spi);
1104 		/*
1105 		 * If communication is suspended while using DMA, it means
1106 		 * that something went wrong, so stop the current transfer
1107 		 */
1108 		if (spi->cur_usedma)
1109 			end = true;
1110 	}
1111 
1112 	if (sr & STM32H7_SPI_SR_MODF) {
1113 		dev_warn(spi->dev, "Mode fault: transfer aborted\n");
1114 		end = true;
1115 	}
1116 
1117 	if (sr & STM32H7_SPI_SR_OVR) {
1118 		dev_err(spi->dev, "Overrun: RX data lost\n");
1119 		end = true;
1120 	}
1121 
1122 	if (sr & STM32H7_SPI_SR_EOT) {
1123 		dev_dbg(spi->dev, "End of transfer\n");
1124 		if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
1125 			stm32h7_spi_read_rxfifo(spi);
1126 		if (!spi->cur_usedma ||
1127 		    (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) ||
1128 		    (spi->mdma_rx && (spi->cur_comm == SPI_SIMPLEX_RX ||
1129 		     spi->cur_comm == SPI_FULL_DUPLEX)))
1130 			end = true;
1131 	}
1132 
1133 	if (sr & STM32H7_SPI_SR_TXP)
1134 		if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
1135 			stm32h7_spi_write_txfifo(spi);
1136 
1137 	if (sr & STM32H7_SPI_SR_RXP)
1138 		if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
1139 			stm32h7_spi_read_rxfifo(spi);
1140 
1141 	writel_relaxed(sr & mask, spi->base + STM32H7_SPI_IFCR);
1142 
1143 	spin_unlock_irqrestore(&spi->lock, flags);
1144 
1145 	if (end) {
1146 		if (spi->cur_usedma && spi->mdma_rx) {
1147 			dmaengine_pause(spi->dma_rx);
1148 			/* Wait for callback */
1149 			return IRQ_HANDLED;
1150 		}
1151 		stm32h7_spi_disable(spi);
1152 		spi_finalize_current_transfer(ctrl);
1153 	}
1154 
1155 	return IRQ_HANDLED;
1156 }
1157 
1158 static int stm32_spi_optimize_message(struct spi_message *msg)
1159 {
1160 	struct spi_controller *ctrl = msg->spi->controller;
1161 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
1162 
1163 	/* On STM32H7, messages should not exceed a maximum size set
1164 	 * later via the set_number_of_data function. In order to
1165 	 * ensure that, split large messages into several messages
1166 	 */
1167 	if (spi->cfg->set_number_of_data)
1168 		return spi_split_transfers_maxwords(ctrl, msg, spi->t_size_max);
1169 
1170 	return 0;
1171 }
1172 
1173 /**
1174  * stm32_spi_prepare_msg - set up the controller to transfer a single message
1175  * @ctrl: controller interface
1176  * @msg: pointer to spi message
1177  */
1178 static int stm32_spi_prepare_msg(struct spi_controller *ctrl,
1179 				 struct spi_message *msg)
1180 {
1181 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
1182 	struct spi_device *spi_dev = msg->spi;
1183 	struct device_node *np = spi_dev->dev.of_node;
1184 	unsigned long flags;
1185 	u32 clrb = 0, setb = 0;
1186 
1187 	/* SPI target device may need time between data frames */
1188 	spi->cur_midi = 0;
1189 	if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
1190 		dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
1191 
1192 	if (spi_dev->mode & SPI_CPOL)
1193 		setb |= spi->cfg->regs->cpol.mask;
1194 	else
1195 		clrb |= spi->cfg->regs->cpol.mask;
1196 
1197 	if (spi_dev->mode & SPI_CPHA)
1198 		setb |= spi->cfg->regs->cpha.mask;
1199 	else
1200 		clrb |= spi->cfg->regs->cpha.mask;
1201 
1202 	if (spi_dev->mode & SPI_LSB_FIRST)
1203 		setb |= spi->cfg->regs->lsb_first.mask;
1204 	else
1205 		clrb |= spi->cfg->regs->lsb_first.mask;
1206 
1207 	if (STM32_SPI_DEVICE_MODE(spi) && spi_dev->mode & SPI_CS_HIGH)
1208 		setb |= spi->cfg->regs->cs_high.mask;
1209 	else
1210 		clrb |= spi->cfg->regs->cs_high.mask;
1211 
1212 	if (spi_dev->mode & SPI_READY)
1213 		setb |= spi->cfg->regs->rdy_en.mask;
1214 	else
1215 		clrb |= spi->cfg->regs->rdy_en.mask;
1216 
1217 	dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d rdy=%d\n",
1218 		!!(spi_dev->mode & SPI_CPOL),
1219 		!!(spi_dev->mode & SPI_CPHA),
1220 		!!(spi_dev->mode & SPI_LSB_FIRST),
1221 		!!(spi_dev->mode & SPI_CS_HIGH),
1222 		!!(spi_dev->mode & SPI_READY));
1223 
1224 	spin_lock_irqsave(&spi->lock, flags);
1225 
1226 	/* CPOL, CPHA, LSB FIRST, CS_HIGH and RDY_EN bits have common register */
1227 	if (clrb || setb)
1228 		writel_relaxed(
1229 			(readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) &
1230 			 ~clrb) | setb,
1231 			spi->base + spi->cfg->regs->cpol.reg);
1232 
1233 	spin_unlock_irqrestore(&spi->lock, flags);
1234 
1235 	return 0;
1236 }
1237 
1238 /**
1239  * stm32fx_spi_dma_tx_cb - dma callback
1240  * @data: pointer to the spi controller data structure
1241  *
1242  * DMA callback is called when the transfer is complete for DMA TX channel.
1243  */
1244 static void stm32fx_spi_dma_tx_cb(void *data)
1245 {
1246 	struct stm32_spi *spi = data;
1247 
1248 	if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1249 		spi_finalize_current_transfer(spi->ctrl);
1250 		stm32fx_spi_disable(spi);
1251 	}
1252 }
1253 
1254 /**
1255  * stm32_spi_dma_rx_cb - dma callback
1256  * @data: pointer to the spi controller data structure
1257  *
1258  * DMA callback is called when the transfer is complete for DMA RX channel.
1259  */
1260 static void stm32_spi_dma_rx_cb(void *data)
1261 {
1262 	struct stm32_spi *spi = data;
1263 
1264 	spi_finalize_current_transfer(spi->ctrl);
1265 	spi->cfg->disable(spi);
1266 }
1267 
1268 /**
1269  * stm32_spi_dma_config - configure dma slave channel depending on current
1270  *			  transfer bits_per_word.
1271  * @spi: pointer to the spi controller data structure
1272  * @dma_chan: pointer to the DMA channel
1273  * @dma_conf: pointer to the dma_slave_config structure
1274  * @dir: direction of the dma transfer
1275  */
1276 static void stm32_spi_dma_config(struct stm32_spi *spi,
1277 				 struct dma_chan *dma_chan,
1278 				 struct dma_slave_config *dma_conf,
1279 				 enum dma_transfer_direction dir)
1280 {
1281 	enum dma_slave_buswidth buswidth;
1282 	struct dma_slave_caps caps;
1283 	u32 maxburst = 1;
1284 	int ret;
1285 
1286 	if (spi->cur_bpw <= 8)
1287 		buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1288 	else if (spi->cur_bpw <= 16)
1289 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1290 	else
1291 		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1292 
1293 	/* Valid for DMA Half or Full Fifo threshold */
1294 	if (!spi->cfg->prevent_dma_burst && spi->cfg->has_fifo && spi->cur_fthlv != 2)
1295 		maxburst = spi->cur_fthlv;
1296 
1297 	/* Get the DMA channel caps, and adjust maxburst if possible */
1298 	ret = dma_get_slave_caps(dma_chan, &caps);
1299 	if (!ret)
1300 		maxburst = min(maxburst, caps.max_burst);
1301 
1302 	memset(dma_conf, 0, sizeof(struct dma_slave_config));
1303 	dma_conf->direction = dir;
1304 	if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */
1305 		dma_conf->src_addr = spi->phys_addr + spi->cfg->regs->rx.reg;
1306 		dma_conf->src_addr_width = buswidth;
1307 		dma_conf->src_maxburst = maxburst;
1308 
1309 		dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
1310 			buswidth, maxburst);
1311 	} else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */
1312 		dma_conf->dst_addr = spi->phys_addr + spi->cfg->regs->tx.reg;
1313 		dma_conf->dst_addr_width = buswidth;
1314 		dma_conf->dst_maxburst = maxburst;
1315 
1316 		dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
1317 			buswidth, maxburst);
1318 	}
1319 }
1320 
1321 /**
1322  * stm32fx_spi_transfer_one_irq - transfer a single spi_transfer using
1323  *				  interrupts
1324  * @spi: pointer to the spi controller data structure
1325  *
1326  * It must returns 0 if the transfer is finished or 1 if the transfer is still
1327  * in progress.
1328  */
1329 static int stm32fx_spi_transfer_one_irq(struct stm32_spi *spi)
1330 {
1331 	unsigned long flags;
1332 	u32 cr2 = 0;
1333 
1334 	/* Enable the interrupts relative to the current communication mode */
1335 	if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1336 		cr2 |= STM32FX_SPI_CR2_TXEIE;
1337 	} else if (spi->cur_comm == SPI_FULL_DUPLEX ||
1338 				spi->cur_comm == SPI_SIMPLEX_RX ||
1339 				spi->cur_comm == SPI_3WIRE_RX) {
1340 		/* In transmit-only mode, the OVR flag is set in the SR register
1341 		 * since the received data are never read. Therefore set OVR
1342 		 * interrupt only when rx buffer is available.
1343 		 */
1344 		cr2 |= STM32FX_SPI_CR2_RXNEIE | STM32FX_SPI_CR2_ERRIE;
1345 	} else {
1346 		return -EINVAL;
1347 	}
1348 
1349 	spin_lock_irqsave(&spi->lock, flags);
1350 
1351 	stm32_spi_set_bits(spi, STM32FX_SPI_CR2, cr2);
1352 
1353 	stm32_spi_enable(spi);
1354 
1355 	/* starting data transfer when buffer is loaded */
1356 	if (spi->tx_buf)
1357 		spi->cfg->write_tx(spi);
1358 
1359 	spin_unlock_irqrestore(&spi->lock, flags);
1360 
1361 	return 1;
1362 }
1363 
1364 /**
1365  * stm32h7_spi_transfer_one_poll - transfer a single spi_transfer by direct
1366  *				   register access without interrupt usage
1367  * @spi: pointer to the spi controller data structure
1368  *
1369  * It must returns 0 if the transfer is finished or 1 if the transfer is still
1370  * in progress.
1371  */
1372 static int stm32h7_spi_transfer_one_poll(struct stm32_spi *spi)
1373 {
1374 	unsigned long flags;
1375 	u32 sr;
1376 
1377 	spin_lock_irqsave(&spi->lock, flags);
1378 
1379 	stm32_spi_enable(spi);
1380 
1381 	/* Be sure to have data in fifo before starting data transfer */
1382 	if (spi->tx_buf)
1383 		stm32h7_spi_write_txfifo(spi);
1384 
1385 	if (STM32_SPI_HOST_MODE(spi))
1386 		stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1387 
1388 	sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
1389 	/* Keep writing / reading while waiting for the end of transfer */
1390 	while (spi->tx_len || spi->rx_len || !(sr & STM32H7_SPI_SR_EOT)) {
1391 		if (spi->rx_len && (sr & (STM32H7_SPI_SR_RXP | STM32H7_SPI_SR_RXWNE |
1392 					  STM32H7_SPI_SR_RXPLVL)))
1393 			stm32h7_spi_read_rxfifo(spi);
1394 
1395 		if (spi->tx_len && (sr & STM32H7_SPI_SR_TXP))
1396 			stm32h7_spi_write_txfifo(spi);
1397 
1398 		sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
1399 
1400 		/* Clear suspension bit if necessary */
1401 		if (sr & STM32H7_SPI_SR_SUSP)
1402 			writel_relaxed(sr & STM32H7_SPI_SR_SUSP, spi->base + STM32H7_SPI_IFCR);
1403 	}
1404 
1405 	spin_unlock_irqrestore(&spi->lock, flags);
1406 
1407 	stm32h7_spi_disable(spi);
1408 	spi_finalize_current_transfer(spi->ctrl);
1409 
1410 	return 0;
1411 }
1412 
1413 /**
1414  * stm32h7_spi_transfer_one_irq - transfer a single spi_transfer using
1415  *				  interrupts
1416  * @spi: pointer to the spi controller data structure
1417  *
1418  * It must returns 0 if the transfer is finished or 1 if the transfer is still
1419  * in progress.
1420  */
1421 static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
1422 {
1423 	unsigned long flags;
1424 	u32 ier = 0;
1425 
1426 	/* Enable the interrupts relative to the current communication mode */
1427 	if (spi->tx_buf && spi->rx_buf)	/* Full Duplex */
1428 		ier |= STM32H7_SPI_IER_DXPIE;
1429 	else if (spi->tx_buf)		/* Half-Duplex TX dir or Simplex TX */
1430 		ier |= STM32H7_SPI_IER_TXPIE;
1431 	else if (spi->rx_buf)		/* Half-Duplex RX dir or Simplex RX */
1432 		ier |= STM32H7_SPI_IER_RXPIE;
1433 
1434 	/* Enable the interrupts relative to the end of transfer */
1435 	ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE |
1436 	       STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
1437 
1438 	spin_lock_irqsave(&spi->lock, flags);
1439 
1440 	stm32_spi_enable(spi);
1441 
1442 	/* Be sure to have data in fifo before starting data transfer */
1443 	if (spi->tx_buf)
1444 		stm32h7_spi_write_txfifo(spi);
1445 
1446 	if (STM32_SPI_HOST_MODE(spi))
1447 		stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1448 
1449 	writel_relaxed(ier, spi->base + STM32H7_SPI_IER);
1450 
1451 	spin_unlock_irqrestore(&spi->lock, flags);
1452 
1453 	return 1;
1454 }
1455 
1456 /**
1457  * stm32fx_spi_transfer_one_dma_start - Set SPI driver registers to start
1458  *					transfer using DMA
1459  * @spi: pointer to the spi controller data structure
1460  */
1461 static void stm32fx_spi_transfer_one_dma_start(struct stm32_spi *spi)
1462 {
1463 	/* In DMA mode end of transfer is handled by DMA TX or RX callback. */
1464 	if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX ||
1465 	    spi->cur_comm == SPI_FULL_DUPLEX) {
1466 		/*
1467 		 * In transmit-only mode, the OVR flag is set in the SR register
1468 		 * since the received data are never read. Therefore set OVR
1469 		 * interrupt only when rx buffer is available.
1470 		 */
1471 		stm32_spi_set_bits(spi, STM32FX_SPI_CR2, STM32FX_SPI_CR2_ERRIE);
1472 	}
1473 
1474 	stm32_spi_enable(spi);
1475 }
1476 
1477 /**
1478  * stm32f7_spi_transfer_one_dma_start - Set SPI driver registers to start
1479  *					transfer using DMA
1480  * @spi: pointer to the spi controller data structure
1481  */
1482 static void stm32f7_spi_transfer_one_dma_start(struct stm32_spi *spi)
1483 {
1484 	/* Configure DMA request trigger threshold according to DMA width */
1485 	if (spi->cur_bpw <= 8)
1486 		stm32_spi_set_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
1487 	else
1488 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR2, STM32F7_SPI_CR2_FRXTH);
1489 
1490 	stm32fx_spi_transfer_one_dma_start(spi);
1491 }
1492 
1493 /**
1494  * stm32h7_spi_transfer_one_dma_start - Set SPI driver registers to start
1495  *					transfer using DMA
1496  * @spi: pointer to the spi controller data structure
1497  */
1498 static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
1499 {
1500 	uint32_t ier = STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
1501 
1502 	/* Enable the interrupts */
1503 	if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX)
1504 		ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE;
1505 	if (spi->mdma_rx && (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_FULL_DUPLEX))
1506 		ier |= STM32H7_SPI_IER_EOTIE;
1507 
1508 	stm32_spi_set_bits(spi, STM32H7_SPI_IER, ier);
1509 
1510 	stm32_spi_enable(spi);
1511 
1512 	if (STM32_SPI_HOST_MODE(spi))
1513 		stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1514 }
1515 
1516 /**
1517  * stm32_spi_prepare_rx_dma_mdma_chaining - Prepare RX DMA and MDMA chaining
1518  * @spi: pointer to the spi controller data structure
1519  * @xfer: pointer to the spi transfer
1520  * @rx_dma_conf: pointer to the DMA configuration for RX channel
1521  * @rx_dma_desc: pointer to the RX DMA descriptor
1522  * @rx_mdma_desc: pointer to the RX MDMA descriptor
1523  *
1524  * It must return 0 if the chaining is possible or an error code if not.
1525  */
1526 static int stm32_spi_prepare_rx_dma_mdma_chaining(struct stm32_spi *spi,
1527 						  struct spi_transfer *xfer,
1528 						  struct dma_slave_config *rx_dma_conf,
1529 						  struct dma_async_tx_descriptor **rx_dma_desc,
1530 						  struct dma_async_tx_descriptor **rx_mdma_desc)
1531 {
1532 	struct dma_async_tx_descriptor *_mdma_desc = *rx_mdma_desc;
1533 	struct dma_async_tx_descriptor *_dma_desc = *rx_dma_desc;
1534 	struct dma_slave_config rx_mdma_conf = {0};
1535 	u32 sram_period, nents = 0, spi_s_len;
1536 	struct sg_table dma_sgt, mdma_sgt;
1537 	struct scatterlist *spi_s, *s;
1538 	dma_addr_t dma_buf;
1539 	int i, ret;
1540 
1541 	sram_period = spi->sram_rx_buf_size / 2;
1542 
1543 	/* Configure MDMA RX channel */
1544 	rx_mdma_conf.direction = rx_dma_conf->direction;
1545 	rx_mdma_conf.src_addr = spi->sram_dma_rx_buf;
1546 	rx_mdma_conf.peripheral_config = rx_dma_conf->peripheral_config;
1547 	rx_mdma_conf.peripheral_size = rx_dma_conf->peripheral_size;
1548 	dmaengine_slave_config(spi->mdma_rx, &rx_mdma_conf);
1549 
1550 	/* Count the number of entries needed */
1551 	for_each_sg(xfer->rx_sg.sgl, spi_s, xfer->rx_sg.nents, i)
1552 		if (sg_dma_len(spi_s) > sram_period)
1553 			nents += DIV_ROUND_UP(sg_dma_len(spi_s), sram_period);
1554 		else
1555 			nents++;
1556 
1557 	/* Prepare DMA slave_sg DBM transfer DEV_TO_MEM (RX>MEM=SRAM) */
1558 	ret = sg_alloc_table(&dma_sgt, nents, GFP_ATOMIC);
1559 	if (ret)
1560 		return ret;
1561 
1562 	spi_s = xfer->rx_sg.sgl;
1563 	spi_s_len = sg_dma_len(spi_s);
1564 	dma_buf = spi->sram_dma_rx_buf;
1565 	for_each_sg(dma_sgt.sgl, s, dma_sgt.nents, i) {
1566 		size_t bytes = min_t(size_t, spi_s_len, sram_period);
1567 
1568 		sg_dma_len(s) = bytes;
1569 		sg_dma_address(s) = dma_buf;
1570 		spi_s_len -= bytes;
1571 
1572 		if (!spi_s_len && sg_next(spi_s)) {
1573 			spi_s = sg_next(spi_s);
1574 			spi_s_len = sg_dma_len(spi_s);
1575 			dma_buf = spi->sram_dma_rx_buf;
1576 		} else { /* DMA configured in DBM: it will swap between the SRAM periods */
1577 			if (i & 1)
1578 				dma_buf += sram_period;
1579 			else
1580 				dma_buf = spi->sram_dma_rx_buf;
1581 		}
1582 	}
1583 
1584 	_dma_desc = dmaengine_prep_slave_sg(spi->dma_rx, dma_sgt.sgl,
1585 					    dma_sgt.nents, rx_dma_conf->direction,
1586 					    DMA_PREP_INTERRUPT);
1587 	sg_free_table(&dma_sgt);
1588 
1589 	if (!_dma_desc)
1590 		return -EINVAL;
1591 
1592 	/* Prepare MDMA slave_sg transfer MEM_TO_MEM (SRAM>DDR) */
1593 	ret = sg_alloc_table(&mdma_sgt, nents, GFP_ATOMIC);
1594 	if (ret) {
1595 		_dma_desc = NULL;
1596 		return ret;
1597 	}
1598 
1599 	spi_s = xfer->rx_sg.sgl;
1600 	spi_s_len = sg_dma_len(spi_s);
1601 	dma_buf = sg_dma_address(spi_s);
1602 	for_each_sg(mdma_sgt.sgl, s, mdma_sgt.nents, i) {
1603 		size_t bytes = min_t(size_t, spi_s_len, sram_period);
1604 
1605 		sg_dma_len(s) = bytes;
1606 		sg_dma_address(s) = dma_buf;
1607 		spi_s_len -= bytes;
1608 
1609 		if (!spi_s_len && sg_next(spi_s)) {
1610 			spi_s = sg_next(spi_s);
1611 			spi_s_len = sg_dma_len(spi_s);
1612 			dma_buf = sg_dma_address(spi_s);
1613 		} else {
1614 			dma_buf += bytes;
1615 		}
1616 	}
1617 
1618 	_mdma_desc = dmaengine_prep_slave_sg(spi->mdma_rx, mdma_sgt.sgl,
1619 					     mdma_sgt.nents, rx_mdma_conf.direction,
1620 					     DMA_PREP_INTERRUPT);
1621 	sg_free_table(&mdma_sgt);
1622 
1623 	if (!_mdma_desc) {
1624 		_dma_desc = NULL;
1625 		return -EINVAL;
1626 	}
1627 
1628 	return 0;
1629 }
1630 
1631 /**
1632  * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
1633  * @spi: pointer to the spi controller data structure
1634  * @xfer: pointer to the spi_transfer structure
1635  *
1636  * It must returns 0 if the transfer is finished or 1 if the transfer is still
1637  * in progress.
1638  */
1639 static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
1640 				      struct spi_transfer *xfer)
1641 {
1642 	struct dma_async_tx_descriptor *rx_mdma_desc = NULL, *rx_dma_desc = NULL;
1643 	struct dma_async_tx_descriptor *tx_dma_desc = NULL;
1644 	struct dma_slave_config tx_dma_conf, rx_dma_conf;
1645 	unsigned long flags;
1646 	int ret = 0;
1647 
1648 	spin_lock_irqsave(&spi->lock, flags);
1649 
1650 	if (spi->rx_buf && spi->dma_rx) {
1651 		stm32_spi_dma_config(spi, spi->dma_rx, &rx_dma_conf, DMA_DEV_TO_MEM);
1652 		if (spi->mdma_rx) {
1653 			rx_dma_conf.peripheral_size = 1;
1654 			dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1655 
1656 			ret = stm32_spi_prepare_rx_dma_mdma_chaining(spi, xfer, &rx_dma_conf,
1657 								     &rx_dma_desc, &rx_mdma_desc);
1658 			if (ret) { /* RX DMA MDMA chaining not possible, fallback to DMA only */
1659 				rx_dma_conf.peripheral_config = 0;
1660 				rx_dma_desc = NULL;
1661 			}
1662 		}
1663 		if (!rx_dma_desc) {
1664 			dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1665 			rx_dma_desc = dmaengine_prep_slave_sg(spi->dma_rx, xfer->rx_sg.sgl,
1666 							      xfer->rx_sg.nents,
1667 							      rx_dma_conf.direction,
1668 							      DMA_PREP_INTERRUPT);
1669 		}
1670 	}
1671 
1672 	if (spi->tx_buf && spi->dma_tx) {
1673 		stm32_spi_dma_config(spi, spi->dma_tx, &tx_dma_conf, DMA_MEM_TO_DEV);
1674 		dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
1675 		tx_dma_desc = dmaengine_prep_slave_sg(spi->dma_tx, xfer->tx_sg.sgl,
1676 						      xfer->tx_sg.nents,
1677 						      tx_dma_conf.direction,
1678 						      DMA_PREP_INTERRUPT);
1679 	}
1680 
1681 	if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
1682 	    (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
1683 		goto dma_desc_error;
1684 
1685 	if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
1686 		goto dma_desc_error;
1687 
1688 	if (rx_dma_desc) {
1689 		if (rx_mdma_desc) {
1690 			rx_mdma_desc->callback = spi->cfg->dma_rx_cb;
1691 			rx_mdma_desc->callback_param = spi;
1692 		} else {
1693 			rx_dma_desc->callback = spi->cfg->dma_rx_cb;
1694 			rx_dma_desc->callback_param = spi;
1695 		}
1696 
1697 		/* Enable Rx DMA request */
1698 		stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1699 				   spi->cfg->regs->dma_rx_en.mask);
1700 		if (rx_mdma_desc) {
1701 			if (dma_submit_error(dmaengine_submit(rx_mdma_desc))) {
1702 				dev_err(spi->dev, "Rx MDMA submit failed\n");
1703 				goto dma_desc_error;
1704 			}
1705 			/* Enable Rx MDMA channel */
1706 			dma_async_issue_pending(spi->mdma_rx);
1707 		}
1708 		if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
1709 			dev_err(spi->dev, "Rx DMA submit failed\n");
1710 			goto dma_desc_error;
1711 		}
1712 		/* Enable Rx DMA channel */
1713 		dma_async_issue_pending(spi->dma_rx);
1714 	}
1715 
1716 	if (tx_dma_desc) {
1717 		if (spi->cur_comm == SPI_SIMPLEX_TX ||
1718 		    spi->cur_comm == SPI_3WIRE_TX) {
1719 			tx_dma_desc->callback = spi->cfg->dma_tx_cb;
1720 			tx_dma_desc->callback_param = spi;
1721 		}
1722 
1723 		if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
1724 			dev_err(spi->dev, "Tx DMA submit failed\n");
1725 			goto dma_submit_error;
1726 		}
1727 		/* Enable Tx DMA channel */
1728 		dma_async_issue_pending(spi->dma_tx);
1729 
1730 		/* Enable Tx DMA request */
1731 		stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg,
1732 				   spi->cfg->regs->dma_tx_en.mask);
1733 	}
1734 
1735 	spi->cfg->transfer_one_dma_start(spi);
1736 
1737 	spin_unlock_irqrestore(&spi->lock, flags);
1738 
1739 	return 1;
1740 
1741 dma_submit_error:
1742 	if (spi->mdma_rx)
1743 		dmaengine_terminate_sync(spi->mdma_rx);
1744 	if (spi->dma_rx)
1745 		dmaengine_terminate_sync(spi->dma_rx);
1746 
1747 dma_desc_error:
1748 	stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1749 			   spi->cfg->regs->dma_rx_en.mask);
1750 
1751 	spin_unlock_irqrestore(&spi->lock, flags);
1752 
1753 	dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
1754 
1755 	if (spi->sram_rx_buf)
1756 		memset(spi->sram_rx_buf, 0, spi->sram_rx_buf_size);
1757 
1758 	spi->cur_usedma = false;
1759 	return spi->cfg->transfer_one_irq(spi);
1760 }
1761 
1762 /**
1763  * stm32f4_spi_set_bpw - Configure bits per word
1764  * @spi: pointer to the spi controller data structure
1765  */
1766 static void stm32f4_spi_set_bpw(struct stm32_spi *spi)
1767 {
1768 	if (spi->cur_bpw == 16)
1769 		stm32_spi_set_bits(spi, STM32FX_SPI_CR1, STM32F4_SPI_CR1_DFF);
1770 	else
1771 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR1, STM32F4_SPI_CR1_DFF);
1772 }
1773 
1774 /**
1775  * stm32f7_spi_set_bpw - Configure bits per word
1776  * @spi: pointer to the spi controller data structure
1777  */
1778 static void stm32f7_spi_set_bpw(struct stm32_spi *spi)
1779 {
1780 	u32 bpw;
1781 	u32 cr2_clrb = 0, cr2_setb = 0;
1782 
1783 	bpw = spi->cur_bpw - 1;
1784 
1785 	cr2_clrb |= STM32F7_SPI_CR2_DS;
1786 	cr2_setb |= FIELD_PREP(STM32F7_SPI_CR2_DS, bpw);
1787 
1788 	if (spi->rx_len >= sizeof(u16))
1789 		cr2_clrb |= STM32F7_SPI_CR2_FRXTH;
1790 	else
1791 		cr2_setb |= STM32F7_SPI_CR2_FRXTH;
1792 
1793 	writel_relaxed(
1794 		(readl_relaxed(spi->base + STM32FX_SPI_CR2) &
1795 		 ~cr2_clrb) | cr2_setb,
1796 		spi->base + STM32FX_SPI_CR2);
1797 }
1798 
1799 /**
1800  * stm32h7_spi_set_bpw - configure bits per word
1801  * @spi: pointer to the spi controller data structure
1802  */
1803 static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
1804 {
1805 	u32 bpw, fthlv;
1806 	u32 cfg1_clrb = 0, cfg1_setb = 0;
1807 
1808 	bpw = spi->cur_bpw - 1;
1809 
1810 	cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE;
1811 	cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_DSIZE, bpw);
1812 
1813 	spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen);
1814 	fthlv = spi->cur_fthlv - 1;
1815 
1816 	cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
1817 	cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_FTHLV, fthlv);
1818 
1819 	writel_relaxed(
1820 		(readl_relaxed(spi->base + STM32H7_SPI_CFG1) &
1821 		 ~cfg1_clrb) | cfg1_setb,
1822 		spi->base + STM32H7_SPI_CFG1);
1823 }
1824 
1825 /**
1826  * stm32_spi_set_mbr - Configure baud rate divisor in host mode
1827  * @spi: pointer to the spi controller data structure
1828  * @mbrdiv: baud rate divisor value
1829  */
1830 static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv)
1831 {
1832 	u32 clrb = 0, setb = 0;
1833 
1834 	clrb |= spi->cfg->regs->br.mask;
1835 	setb |= (mbrdiv << spi->cfg->regs->br.shift) & spi->cfg->regs->br.mask;
1836 
1837 	writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) &
1838 			~clrb) | setb,
1839 		       spi->base + spi->cfg->regs->br.reg);
1840 }
1841 
1842 /**
1843  * stm32_spi_communication_type - return transfer communication type
1844  * @spi_dev: pointer to the spi device
1845  * @transfer: pointer to spi transfer
1846  */
1847 static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev,
1848 						 struct spi_transfer *transfer)
1849 {
1850 	unsigned int type = SPI_FULL_DUPLEX;
1851 
1852 	if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
1853 		/*
1854 		 * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
1855 		 * is forbidden and unvalidated by SPI subsystem so depending
1856 		 * on the valid buffer, we can determine the direction of the
1857 		 * transfer.
1858 		 */
1859 		if (!transfer->tx_buf)
1860 			type = SPI_3WIRE_RX;
1861 		else
1862 			type = SPI_3WIRE_TX;
1863 	} else {
1864 		if (!transfer->tx_buf)
1865 			type = SPI_SIMPLEX_RX;
1866 		else if (!transfer->rx_buf)
1867 			type = SPI_SIMPLEX_TX;
1868 	}
1869 
1870 	return type;
1871 }
1872 
1873 /**
1874  * stm32fx_spi_set_mode - configure communication mode
1875  * @spi: pointer to the spi controller data structure
1876  * @comm_type: type of communication to configure
1877  */
1878 static int stm32fx_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1879 {
1880 	if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
1881 		stm32_spi_set_bits(spi, STM32FX_SPI_CR1,
1882 					STM32FX_SPI_CR1_BIDIMODE |
1883 					STM32FX_SPI_CR1_BIDIOE);
1884 	} else if (comm_type == SPI_FULL_DUPLEX ||
1885 				comm_type == SPI_SIMPLEX_RX) {
1886 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR1,
1887 					STM32FX_SPI_CR1_BIDIMODE |
1888 					STM32FX_SPI_CR1_BIDIOE);
1889 	} else if (comm_type == SPI_3WIRE_RX) {
1890 		stm32_spi_set_bits(spi, STM32FX_SPI_CR1,
1891 					STM32FX_SPI_CR1_BIDIMODE);
1892 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR1,
1893 					STM32FX_SPI_CR1_BIDIOE);
1894 	} else {
1895 		return -EINVAL;
1896 	}
1897 
1898 	return 0;
1899 }
1900 
1901 /**
1902  * stm32h7_spi_set_mode - configure communication mode
1903  * @spi: pointer to the spi controller data structure
1904  * @comm_type: type of communication to configure
1905  */
1906 static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1907 {
1908 	u32 mode;
1909 	u32 cfg2_clrb = 0, cfg2_setb = 0;
1910 
1911 	if (comm_type == SPI_3WIRE_RX) {
1912 		mode = STM32H7_SPI_HALF_DUPLEX;
1913 		stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1914 	} else if (comm_type == SPI_3WIRE_TX) {
1915 		mode = STM32H7_SPI_HALF_DUPLEX;
1916 		stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1917 	} else if (comm_type == SPI_SIMPLEX_RX) {
1918 		mode = STM32H7_SPI_SIMPLEX_RX;
1919 	} else if (comm_type == SPI_SIMPLEX_TX) {
1920 		mode = STM32H7_SPI_SIMPLEX_TX;
1921 	} else {
1922 		mode = STM32H7_SPI_FULL_DUPLEX;
1923 	}
1924 
1925 	cfg2_clrb |= STM32H7_SPI_CFG2_COMM;
1926 	cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_COMM, mode);
1927 
1928 	writel_relaxed(
1929 		(readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1930 		 ~cfg2_clrb) | cfg2_setb,
1931 		spi->base + STM32H7_SPI_CFG2);
1932 
1933 	return 0;
1934 }
1935 
1936 /**
1937  * stm32h7_spi_data_idleness - configure minimum time delay inserted between two
1938  *			       consecutive data frames in host mode
1939  * @spi: pointer to the spi controller data structure
1940  * @xfer: pointer to spi transfer
1941  */
1942 static void stm32h7_spi_data_idleness(struct stm32_spi *spi, struct spi_transfer *xfer)
1943 {
1944 	u32 cfg2_clrb = 0, cfg2_setb = 0;
1945 	u32 len = xfer->len;
1946 	u32 spi_delay_ns;
1947 
1948 	spi_delay_ns = spi_delay_to_ns(&xfer->word_delay, xfer);
1949 
1950 	if (spi->cur_midi != 0) {
1951 		dev_warn(spi->dev, "st,spi-midi-ns DT property is deprecated\n");
1952 		if (spi_delay_ns) {
1953 			dev_warn(spi->dev, "Overriding st,spi-midi-ns with word_delay_ns %d\n",
1954 				 spi_delay_ns);
1955 			spi->cur_midi = spi_delay_ns;
1956 		}
1957 	} else {
1958 		spi->cur_midi = spi_delay_ns;
1959 	}
1960 
1961 	cfg2_clrb |= STM32H7_SPI_CFG2_MIDI;
1962 	if ((len > 1) && (spi->cur_midi > 0)) {
1963 		u32 sck_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->cur_speed);
1964 		u32 midi = DIV_ROUND_UP(spi->cur_midi, sck_period_ns);
1965 
1966 		if ((spi->cur_bpw + midi) < 8)
1967 			midi = 8 - spi->cur_bpw;
1968 
1969 		midi = min_t(u32, midi, FIELD_MAX(STM32H7_SPI_CFG2_MIDI));
1970 
1971 		dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
1972 			sck_period_ns, midi, midi * sck_period_ns);
1973 		cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_MIDI, midi);
1974 	}
1975 
1976 	writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1977 			~cfg2_clrb) | cfg2_setb,
1978 		       spi->base + STM32H7_SPI_CFG2);
1979 }
1980 
1981 /**
1982  * stm32h7_spi_number_of_data - configure number of data at current transfer
1983  * @spi: pointer to the spi controller data structure
1984  * @nb_words: transfer length (in words)
1985  */
1986 static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
1987 {
1988 	if (nb_words <= spi->t_size_max) {
1989 		writel_relaxed(FIELD_PREP(STM32H7_SPI_CR2_TSIZE, nb_words),
1990 			       spi->base + STM32H7_SPI_CR2);
1991 	} else {
1992 		return -EMSGSIZE;
1993 	}
1994 
1995 	return 0;
1996 }
1997 
1998 /**
1999  * stm32_spi_transfer_one_setup - common setup to transfer a single
2000  *				  spi_transfer either using DMA or
2001  *				  interrupts.
2002  * @spi: pointer to the spi controller data structure
2003  * @spi_dev: pointer to the spi device
2004  * @transfer: pointer to spi transfer
2005  */
2006 static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
2007 					struct spi_device *spi_dev,
2008 					struct spi_transfer *transfer)
2009 {
2010 	unsigned long flags;
2011 	unsigned int comm_type;
2012 	int nb_words, ret = 0;
2013 	int mbr;
2014 
2015 	spin_lock_irqsave(&spi->lock, flags);
2016 
2017 	spi->cur_xferlen = transfer->len;
2018 
2019 	spi->cur_bpw = transfer->bits_per_word;
2020 	spi->cfg->set_bpw(spi);
2021 
2022 	if (spi_dev->mode & SPI_READY && spi->cur_bpw < 8) {
2023 		writel_relaxed(readl_relaxed(spi->base + spi->cfg->regs->rdy_en.reg) &
2024 				~spi->cfg->regs->rdy_en.mask,
2025 					spi->base + spi->cfg->regs->rdy_en.reg);
2026 		dev_dbg(spi->dev, "RDY logic disabled as bits per word < 8\n");
2027 	}
2028 
2029 	/* Update spi->cur_speed with real clock speed */
2030 	if (STM32_SPI_HOST_MODE(spi)) {
2031 		mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz,
2032 					    spi->cfg->baud_rate_div_min,
2033 					    spi->cfg->baud_rate_div_max);
2034 		if (mbr < 0) {
2035 			ret = mbr;
2036 			goto out;
2037 		}
2038 
2039 		transfer->speed_hz = spi->cur_speed;
2040 		stm32_spi_set_mbr(spi, mbr);
2041 	}
2042 
2043 	comm_type = stm32_spi_communication_type(spi_dev, transfer);
2044 	ret = spi->cfg->set_mode(spi, comm_type);
2045 	if (ret < 0)
2046 		goto out;
2047 
2048 	spi->cur_comm = comm_type;
2049 
2050 	if (STM32_SPI_HOST_MODE(spi) && spi->cfg->set_data_idleness)
2051 		spi->cfg->set_data_idleness(spi, transfer);
2052 
2053 	if (spi->cur_bpw <= 8)
2054 		nb_words = transfer->len;
2055 	else if (spi->cur_bpw <= 16)
2056 		nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
2057 	else
2058 		nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
2059 
2060 	if (spi->cfg->set_number_of_data) {
2061 		ret = spi->cfg->set_number_of_data(spi, nb_words);
2062 		if (ret < 0)
2063 			goto out;
2064 	}
2065 
2066 	dev_dbg(spi->dev, "transfer communication mode set to %d\n",
2067 		spi->cur_comm);
2068 	dev_dbg(spi->dev,
2069 		"data frame of %d-bit, data packet of %d data frames\n",
2070 		spi->cur_bpw, spi->cur_fthlv);
2071 	if (STM32_SPI_HOST_MODE(spi))
2072 		dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
2073 	dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
2074 		spi->cur_xferlen, nb_words);
2075 	dev_dbg(spi->dev, "dma %s\n",
2076 		(spi->cur_usedma) ? "enabled" : "disabled");
2077 
2078 out:
2079 	spin_unlock_irqrestore(&spi->lock, flags);
2080 
2081 	return ret;
2082 }
2083 
2084 /**
2085  * stm32_spi_can_poll - detect if poll based transfer is appropriate
2086  * @spi: pointer to the spi controller data structure
2087  *
2088  * Returns true is poll is more appropriate, false otherwise.
2089  */
2090 static bool stm32_spi_can_poll(struct stm32_spi *spi)
2091 {
2092 	unsigned long hz_per_byte, byte_limit;
2093 
2094 	/* Evaluate the transfer time and use polling if applicable */
2095 	hz_per_byte = polling_limit_us ?
2096 		      DIV_ROUND_UP(8 * USEC_PER_SEC, polling_limit_us) : 0;
2097 	byte_limit = hz_per_byte ? spi->cur_speed / hz_per_byte : 1;
2098 
2099 	return (spi->cur_xferlen < byte_limit) ? true : false;
2100 }
2101 
2102 /**
2103  * stm32_spi_transfer_one - transfer a single spi_transfer
2104  * @ctrl: controller interface
2105  * @spi_dev: pointer to the spi device
2106  * @transfer: pointer to spi transfer
2107  *
2108  * It must return 0 if the transfer is finished or 1 if the transfer is still
2109  * in progress.
2110  */
2111 static int stm32_spi_transfer_one(struct spi_controller *ctrl,
2112 				  struct spi_device *spi_dev,
2113 				  struct spi_transfer *transfer)
2114 {
2115 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2116 	int ret;
2117 
2118 	spi->tx_buf = transfer->tx_buf;
2119 	spi->rx_buf = transfer->rx_buf;
2120 	spi->tx_len = spi->tx_buf ? transfer->len : 0;
2121 	spi->rx_len = spi->rx_buf ? transfer->len : 0;
2122 
2123 	spi->cur_usedma = (ctrl->can_dma &&
2124 			   ctrl->can_dma(ctrl, spi_dev, transfer));
2125 
2126 	ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
2127 	if (ret) {
2128 		dev_err(spi->dev, "SPI transfer setup failed\n");
2129 		return ret;
2130 	}
2131 
2132 	if (spi->cur_usedma)
2133 		return stm32_spi_transfer_one_dma(spi, transfer);
2134 	else if (spi->cfg->transfer_one_poll && stm32_spi_can_poll(spi))
2135 		return spi->cfg->transfer_one_poll(spi);
2136 	else
2137 		return spi->cfg->transfer_one_irq(spi);
2138 }
2139 
2140 /**
2141  * stm32_spi_unprepare_msg - relax the hardware
2142  * @ctrl: controller interface
2143  * @msg: pointer to the spi message
2144  */
2145 static int stm32_spi_unprepare_msg(struct spi_controller *ctrl,
2146 				   struct spi_message *msg)
2147 {
2148 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2149 
2150 	spi->cfg->disable(spi);
2151 
2152 	if (spi->sram_rx_buf)
2153 		memset(spi->sram_rx_buf, 0, spi->sram_rx_buf_size);
2154 
2155 	return 0;
2156 }
2157 
2158 /**
2159  * stm32fx_spi_config - Configure SPI controller as SPI host
2160  * @spi: pointer to the spi controller data structure
2161  */
2162 static int stm32fx_spi_config(struct stm32_spi *spi)
2163 {
2164 	unsigned long flags;
2165 
2166 	spin_lock_irqsave(&spi->lock, flags);
2167 
2168 	/* Ensure I2SMOD bit is kept cleared */
2169 	stm32_spi_clr_bits(spi, STM32FX_SPI_I2SCFGR,
2170 			   STM32FX_SPI_I2SCFGR_I2SMOD);
2171 
2172 	/*
2173 	 * - SS input value high
2174 	 * - transmitter half duplex direction
2175 	 * - Set the host mode (default Motorola mode)
2176 	 * - Consider 1 host/n targets configuration and
2177 	 *   SS input value is determined by the SSI bit
2178 	 */
2179 	stm32_spi_set_bits(spi, STM32FX_SPI_CR1, STM32FX_SPI_CR1_SSI |
2180 						 STM32FX_SPI_CR1_BIDIOE |
2181 						 STM32FX_SPI_CR1_MSTR |
2182 						 STM32FX_SPI_CR1_SSM);
2183 
2184 	spin_unlock_irqrestore(&spi->lock, flags);
2185 
2186 	return 0;
2187 }
2188 
2189 /**
2190  * stm32h7_spi_config - Configure SPI controller
2191  * @spi: pointer to the spi controller data structure
2192  */
2193 static int stm32h7_spi_config(struct stm32_spi *spi)
2194 {
2195 	unsigned long flags;
2196 	u32 cr1 = 0, cfg2 = 0;
2197 
2198 	spin_lock_irqsave(&spi->lock, flags);
2199 
2200 	/* Ensure I2SMOD bit is kept cleared */
2201 	stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
2202 			   STM32H7_SPI_I2SCFGR_I2SMOD);
2203 
2204 	if (STM32_SPI_DEVICE_MODE(spi)) {
2205 		/* Use native device select */
2206 		cfg2 &= ~STM32H7_SPI_CFG2_SSM;
2207 	} else {
2208 		/*
2209 		 * - Transmitter half duplex direction
2210 		 * - Automatic communication suspend when RX-Fifo is full
2211 		 * - SS input value high
2212 		 */
2213 		cr1 |= STM32H7_SPI_CR1_HDDIR | STM32H7_SPI_CR1_MASRX | STM32H7_SPI_CR1_SSI;
2214 
2215 		/*
2216 		 * - Set the host mode (default Motorola mode)
2217 		 * - Consider 1 host/n devices configuration and
2218 		 *   SS input value is determined by the SSI bit
2219 		 * - keep control of all associated GPIOs
2220 		 */
2221 		cfg2 |= STM32H7_SPI_CFG2_MASTER | STM32H7_SPI_CFG2_SSM | STM32H7_SPI_CFG2_AFCNTR;
2222 	}
2223 
2224 	stm32_spi_set_bits(spi, STM32H7_SPI_CR1, cr1);
2225 	stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, cfg2);
2226 
2227 	spin_unlock_irqrestore(&spi->lock, flags);
2228 
2229 	return 0;
2230 }
2231 
2232 static const struct stm32_spi_cfg stm32f4_spi_cfg = {
2233 	.regs = &stm32fx_spi_regspec,
2234 	.get_bpw_mask = stm32f4_spi_get_bpw_mask,
2235 	.disable = stm32fx_spi_disable,
2236 	.config = stm32fx_spi_config,
2237 	.set_bpw = stm32f4_spi_set_bpw,
2238 	.set_mode = stm32fx_spi_set_mode,
2239 	.write_tx = stm32f4_spi_write_tx,
2240 	.read_rx = stm32f4_spi_read_rx,
2241 	.transfer_one_dma_start = stm32fx_spi_transfer_one_dma_start,
2242 	.dma_tx_cb = stm32fx_spi_dma_tx_cb,
2243 	.dma_rx_cb = stm32_spi_dma_rx_cb,
2244 	.transfer_one_irq = stm32fx_spi_transfer_one_irq,
2245 	.irq_handler_event = stm32fx_spi_irq_event,
2246 	.irq_handler_thread = stm32fx_spi_irq_thread,
2247 	.baud_rate_div_min = STM32FX_SPI_BR_DIV_MIN,
2248 	.baud_rate_div_max = STM32FX_SPI_BR_DIV_MAX,
2249 	.has_fifo = false,
2250 	.has_device_mode = false,
2251 	.flags = SPI_CONTROLLER_MUST_TX,
2252 };
2253 
2254 static const struct stm32_spi_cfg stm32f7_spi_cfg = {
2255 	.regs = &stm32fx_spi_regspec,
2256 	.get_bpw_mask = stm32f7_spi_get_bpw_mask,
2257 	.disable = stm32fx_spi_disable,
2258 	.config = stm32fx_spi_config,
2259 	.set_bpw = stm32f7_spi_set_bpw,
2260 	.set_mode = stm32fx_spi_set_mode,
2261 	.write_tx = stm32f7_spi_write_tx,
2262 	.read_rx = stm32f7_spi_read_rx,
2263 	.transfer_one_dma_start = stm32f7_spi_transfer_one_dma_start,
2264 	.dma_tx_cb = stm32fx_spi_dma_tx_cb,
2265 	.dma_rx_cb = stm32_spi_dma_rx_cb,
2266 	.transfer_one_irq = stm32fx_spi_transfer_one_irq,
2267 	.irq_handler_event = stm32fx_spi_irq_event,
2268 	.irq_handler_thread = stm32fx_spi_irq_thread,
2269 	.baud_rate_div_min = STM32FX_SPI_BR_DIV_MIN,
2270 	.baud_rate_div_max = STM32FX_SPI_BR_DIV_MAX,
2271 	.has_fifo = false,
2272 	.flags = SPI_CONTROLLER_MUST_TX,
2273 };
2274 
2275 static const struct stm32_spi_cfg stm32h7_spi_cfg = {
2276 	.regs = &stm32h7_spi_regspec,
2277 	.get_fifo_size = stm32h7_spi_get_fifo_size,
2278 	.get_bpw_mask = stm32h7_spi_get_bpw_mask,
2279 	.disable = stm32h7_spi_disable,
2280 	.config = stm32h7_spi_config,
2281 	.set_bpw = stm32h7_spi_set_bpw,
2282 	.set_mode = stm32h7_spi_set_mode,
2283 	.set_data_idleness = stm32h7_spi_data_idleness,
2284 	.set_number_of_data = stm32h7_spi_number_of_data,
2285 	.write_tx = stm32h7_spi_write_txfifo,
2286 	.read_rx = stm32h7_spi_read_rxfifo,
2287 	.transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
2288 	.dma_rx_cb = stm32_spi_dma_rx_cb,
2289 	/*
2290 	 * dma_tx_cb is not necessary since in case of TX, dma is followed by
2291 	 * SPI access hence handling is performed within the SPI interrupt
2292 	 */
2293 	.transfer_one_irq = stm32h7_spi_transfer_one_irq,
2294 	.transfer_one_poll = stm32h7_spi_transfer_one_poll,
2295 	.irq_handler_thread = stm32h7_spi_irq_thread,
2296 	.baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
2297 	.baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
2298 	.has_fifo = true,
2299 	.has_device_mode = true,
2300 };
2301 
2302 /*
2303  * STM32MP2 is compatible with the STM32H7 except:
2304  * - enforce the DMA maxburst value to 1
2305  * - spi8 have limited feature set (TSIZE_MAX = 1024, BPW of 8 OR 16)
2306  */
2307 static const struct stm32_spi_cfg stm32mp25_spi_cfg = {
2308 	.regs = &stm32mp25_spi_regspec,
2309 	.get_fifo_size = stm32h7_spi_get_fifo_size,
2310 	.get_bpw_mask = stm32mp25_spi_get_bpw_mask,
2311 	.disable = stm32h7_spi_disable,
2312 	.config = stm32h7_spi_config,
2313 	.set_bpw = stm32h7_spi_set_bpw,
2314 	.set_mode = stm32h7_spi_set_mode,
2315 	.set_data_idleness = stm32h7_spi_data_idleness,
2316 	.set_number_of_data = stm32h7_spi_number_of_data,
2317 	.transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
2318 	.dma_rx_cb = stm32_spi_dma_rx_cb,
2319 	/*
2320 	 * dma_tx_cb is not necessary since in case of TX, dma is followed by
2321 	 * SPI access hence handling is performed within the SPI interrupt
2322 	 */
2323 	.transfer_one_irq = stm32h7_spi_transfer_one_irq,
2324 	.transfer_one_poll = stm32h7_spi_transfer_one_poll,
2325 	.irq_handler_thread = stm32h7_spi_irq_thread,
2326 	.baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
2327 	.baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
2328 	.has_fifo = true,
2329 	.prevent_dma_burst = true,
2330 	.has_device_mode = true,
2331 };
2332 
2333 static const struct of_device_id stm32_spi_of_match[] = {
2334 	{ .compatible = "st,stm32mp25-spi", .data = (void *)&stm32mp25_spi_cfg },
2335 	{ .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg },
2336 	{ .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg },
2337 	{ .compatible = "st,stm32f7-spi", .data = (void *)&stm32f7_spi_cfg },
2338 	{},
2339 };
2340 MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
2341 
2342 static int stm32h7_spi_device_abort(struct spi_controller *ctrl)
2343 {
2344 	spi_finalize_current_transfer(ctrl);
2345 	return 0;
2346 }
2347 
2348 static int stm32_spi_probe(struct platform_device *pdev)
2349 {
2350 	struct spi_controller *ctrl;
2351 	struct stm32_spi *spi;
2352 	struct resource *res;
2353 	struct reset_control *rst;
2354 	struct device_node *np = pdev->dev.of_node;
2355 	const struct stm32_spi_cfg *cfg;
2356 	bool device_mode;
2357 	int ret;
2358 
2359 	cfg = of_device_get_match_data(&pdev->dev);
2360 	if (!cfg) {
2361 		dev_err(&pdev->dev, "Failed to get match data for platform\n");
2362 		return -ENODEV;
2363 	}
2364 
2365 	device_mode = of_property_read_bool(np, "spi-slave");
2366 	if (!cfg->has_device_mode && device_mode) {
2367 		dev_err(&pdev->dev, "spi-slave not supported\n");
2368 		return -EPERM;
2369 	}
2370 
2371 	if (device_mode)
2372 		ctrl = devm_spi_alloc_target(&pdev->dev, sizeof(struct stm32_spi));
2373 	else
2374 		ctrl = devm_spi_alloc_host(&pdev->dev, sizeof(struct stm32_spi));
2375 	if (!ctrl) {
2376 		dev_err(&pdev->dev, "spi controller allocation failed\n");
2377 		return -ENOMEM;
2378 	}
2379 	platform_set_drvdata(pdev, ctrl);
2380 
2381 	spi = spi_controller_get_devdata(ctrl);
2382 	spi->dev = &pdev->dev;
2383 	spi->ctrl = ctrl;
2384 	spi->device_mode = device_mode;
2385 	spin_lock_init(&spi->lock);
2386 
2387 	spi->cfg = cfg;
2388 
2389 	spi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2390 	if (IS_ERR(spi->base))
2391 		return PTR_ERR(spi->base);
2392 
2393 	spi->phys_addr = (dma_addr_t)res->start;
2394 
2395 	spi->irq = platform_get_irq(pdev, 0);
2396 	if (spi->irq <= 0)
2397 		return spi->irq;
2398 
2399 	ret = devm_request_threaded_irq(&pdev->dev, spi->irq,
2400 					spi->cfg->irq_handler_event,
2401 					spi->cfg->irq_handler_thread,
2402 					IRQF_ONESHOT, pdev->name, ctrl);
2403 	if (ret) {
2404 		dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
2405 			ret);
2406 		return ret;
2407 	}
2408 
2409 	spi->clk = devm_clk_get(&pdev->dev, NULL);
2410 	if (IS_ERR(spi->clk)) {
2411 		ret = PTR_ERR(spi->clk);
2412 		dev_err(&pdev->dev, "clk get failed: %d\n", ret);
2413 		return ret;
2414 	}
2415 
2416 	ret = clk_prepare_enable(spi->clk);
2417 	if (ret) {
2418 		dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
2419 		return ret;
2420 	}
2421 	spi->clk_rate = clk_get_rate(spi->clk);
2422 	if (!spi->clk_rate) {
2423 		dev_err(&pdev->dev, "clk rate = 0\n");
2424 		ret = -EINVAL;
2425 		goto err_clk_disable;
2426 	}
2427 
2428 	rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
2429 	if (rst) {
2430 		if (IS_ERR(rst)) {
2431 			ret = dev_err_probe(&pdev->dev, PTR_ERR(rst),
2432 					    "failed to get reset\n");
2433 			goto err_clk_disable;
2434 		}
2435 
2436 		reset_control_assert(rst);
2437 		udelay(2);
2438 		reset_control_deassert(rst);
2439 	}
2440 
2441 	if (spi->cfg->has_fifo)
2442 		spi->fifo_size = spi->cfg->get_fifo_size(spi);
2443 
2444 	spi->feature_set = STM32_SPI_FEATURE_FULL;
2445 	if (spi->cfg->regs->fullcfg.reg) {
2446 		spi->feature_set =
2447 			FIELD_GET(STM32MP25_SPI_HWCFGR1_FULLCFG,
2448 				  readl_relaxed(spi->base + spi->cfg->regs->fullcfg.reg));
2449 
2450 		dev_dbg(spi->dev, "%s feature set\n",
2451 			spi->feature_set == STM32_SPI_FEATURE_FULL ? "full" : "limited");
2452 	}
2453 
2454 	/* Only for STM32H7 and after */
2455 	spi->t_size_max = spi->feature_set == STM32_SPI_FEATURE_FULL ?
2456 				STM32H7_SPI_TSIZE_MAX :
2457 				STM32MP25_SPI_TSIZE_MAX_LIMITED;
2458 	dev_dbg(spi->dev, "one message max size %d\n", spi->t_size_max);
2459 
2460 	ret = spi->cfg->config(spi);
2461 	if (ret) {
2462 		dev_err(&pdev->dev, "controller configuration failed: %d\n",
2463 			ret);
2464 		goto err_clk_disable;
2465 	}
2466 
2467 	ctrl->auto_runtime_pm = true;
2468 	ctrl->bus_num = pdev->id;
2469 	ctrl->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
2470 			  SPI_3WIRE | SPI_READY;
2471 	ctrl->bits_per_word_mask = spi->cfg->get_bpw_mask(spi);
2472 	ctrl->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min;
2473 	ctrl->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max;
2474 	ctrl->use_gpio_descriptors = true;
2475 	ctrl->optimize_message = stm32_spi_optimize_message;
2476 	ctrl->prepare_message = stm32_spi_prepare_msg;
2477 	ctrl->transfer_one = stm32_spi_transfer_one;
2478 	ctrl->unprepare_message = stm32_spi_unprepare_msg;
2479 	ctrl->flags = spi->cfg->flags;
2480 	if (STM32_SPI_DEVICE_MODE(spi))
2481 		ctrl->target_abort = stm32h7_spi_device_abort;
2482 
2483 	spi->dma_tx = dma_request_chan(spi->dev, "tx");
2484 	if (IS_ERR(spi->dma_tx)) {
2485 		ret = PTR_ERR(spi->dma_tx);
2486 		if (ret == -ENODEV) {
2487 			dev_info(&pdev->dev, "tx dma disabled\n");
2488 			spi->dma_tx = NULL;
2489 		} else {
2490 			dev_err_probe(&pdev->dev, ret, "failed to request tx dma channel\n");
2491 			goto err_clk_disable;
2492 		}
2493 	} else {
2494 		ctrl->dma_tx = spi->dma_tx;
2495 	}
2496 
2497 	spi->dma_rx = dma_request_chan(spi->dev, "rx");
2498 	if (IS_ERR(spi->dma_rx)) {
2499 		ret = PTR_ERR(spi->dma_rx);
2500 		if (ret == -ENODEV) {
2501 			dev_info(&pdev->dev, "rx dma disabled\n");
2502 			spi->dma_rx = NULL;
2503 		} else {
2504 			dev_err_probe(&pdev->dev, ret, "failed to request rx dma channel\n");
2505 			goto err_dma_release;
2506 		}
2507 	} else {
2508 		ctrl->dma_rx = spi->dma_rx;
2509 	}
2510 
2511 	if (spi->dma_tx || spi->dma_rx)
2512 		ctrl->can_dma = stm32_spi_can_dma;
2513 
2514 	spi->sram_pool = of_gen_pool_get(pdev->dev.of_node, "sram", 0);
2515 	if (spi->sram_pool) {
2516 		spi->sram_rx_buf_size = gen_pool_size(spi->sram_pool);
2517 		dev_info(&pdev->dev, "SRAM pool: %zu KiB for RX DMA/MDMA chaining\n",
2518 			 spi->sram_rx_buf_size / 1024);
2519 		spi->sram_rx_buf = gen_pool_dma_zalloc(spi->sram_pool, spi->sram_rx_buf_size,
2520 						       &spi->sram_dma_rx_buf);
2521 		if (!spi->sram_rx_buf) {
2522 			dev_err(&pdev->dev, "failed to allocate SRAM buffer\n");
2523 		} else {
2524 			spi->mdma_rx = dma_request_chan(spi->dev, "rxm2m");
2525 			if (IS_ERR(spi->mdma_rx)) {
2526 				ret = PTR_ERR(spi->mdma_rx);
2527 				spi->mdma_rx = NULL;
2528 				if (ret == -EPROBE_DEFER) {
2529 					goto err_pool_free;
2530 				} else {
2531 					gen_pool_free(spi->sram_pool,
2532 						      (unsigned long)spi->sram_rx_buf,
2533 						      spi->sram_rx_buf_size);
2534 					dev_warn(&pdev->dev,
2535 						 "failed to request rx mdma channel, DMA only\n");
2536 				}
2537 			}
2538 		}
2539 	}
2540 
2541 	pm_runtime_set_autosuspend_delay(&pdev->dev,
2542 					 STM32_SPI_AUTOSUSPEND_DELAY);
2543 	pm_runtime_use_autosuspend(&pdev->dev);
2544 	pm_runtime_set_active(&pdev->dev);
2545 	pm_runtime_get_noresume(&pdev->dev);
2546 	pm_runtime_enable(&pdev->dev);
2547 
2548 	ret = spi_register_controller(ctrl);
2549 	if (ret) {
2550 		dev_err(&pdev->dev, "spi controller registration failed: %d\n",
2551 			ret);
2552 		goto err_pm_disable;
2553 	}
2554 
2555 	pm_runtime_put_autosuspend(&pdev->dev);
2556 
2557 	dev_info(&pdev->dev, "driver initialized (%s mode)\n",
2558 		 STM32_SPI_HOST_MODE(spi) ? "host" : "device");
2559 
2560 	return 0;
2561 
2562 err_pm_disable:
2563 	pm_runtime_disable(&pdev->dev);
2564 	pm_runtime_put_noidle(&pdev->dev);
2565 	pm_runtime_set_suspended(&pdev->dev);
2566 	pm_runtime_dont_use_autosuspend(&pdev->dev);
2567 
2568 	if (spi->mdma_rx)
2569 		dma_release_channel(spi->mdma_rx);
2570 err_pool_free:
2571 	if (spi->sram_pool)
2572 		gen_pool_free(spi->sram_pool, (unsigned long)spi->sram_rx_buf,
2573 			      spi->sram_rx_buf_size);
2574 err_dma_release:
2575 	if (spi->dma_tx)
2576 		dma_release_channel(spi->dma_tx);
2577 	if (spi->dma_rx)
2578 		dma_release_channel(spi->dma_rx);
2579 err_clk_disable:
2580 	clk_disable_unprepare(spi->clk);
2581 
2582 	return ret;
2583 }
2584 
2585 static void stm32_spi_remove(struct platform_device *pdev)
2586 {
2587 	struct spi_controller *ctrl = platform_get_drvdata(pdev);
2588 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2589 
2590 	pm_runtime_get_sync(&pdev->dev);
2591 
2592 	spi_unregister_controller(ctrl);
2593 	spi->cfg->disable(spi);
2594 
2595 	pm_runtime_disable(&pdev->dev);
2596 	pm_runtime_put_noidle(&pdev->dev);
2597 	pm_runtime_set_suspended(&pdev->dev);
2598 	pm_runtime_dont_use_autosuspend(&pdev->dev);
2599 
2600 	if (ctrl->dma_tx)
2601 		dma_release_channel(ctrl->dma_tx);
2602 	if (ctrl->dma_rx)
2603 		dma_release_channel(ctrl->dma_rx);
2604 	if (spi->mdma_rx)
2605 		dma_release_channel(spi->mdma_rx);
2606 	if (spi->sram_rx_buf)
2607 		gen_pool_free(spi->sram_pool, (unsigned long)spi->sram_rx_buf,
2608 			      spi->sram_rx_buf_size);
2609 
2610 	clk_disable_unprepare(spi->clk);
2611 
2612 
2613 	pinctrl_pm_select_sleep_state(&pdev->dev);
2614 }
2615 
2616 static int stm32_spi_runtime_suspend(struct device *dev)
2617 {
2618 	struct spi_controller *ctrl = dev_get_drvdata(dev);
2619 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2620 
2621 	clk_disable_unprepare(spi->clk);
2622 
2623 	return pinctrl_pm_select_sleep_state(dev);
2624 }
2625 
2626 static int stm32_spi_runtime_resume(struct device *dev)
2627 {
2628 	struct spi_controller *ctrl = dev_get_drvdata(dev);
2629 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2630 	int ret;
2631 
2632 	ret = pinctrl_pm_select_default_state(dev);
2633 	if (ret)
2634 		return ret;
2635 
2636 	return clk_prepare_enable(spi->clk);
2637 }
2638 
2639 static int stm32_spi_suspend(struct device *dev)
2640 {
2641 	struct spi_controller *ctrl = dev_get_drvdata(dev);
2642 	int ret;
2643 
2644 	ret = spi_controller_suspend(ctrl);
2645 	if (ret)
2646 		return ret;
2647 
2648 	return pm_runtime_force_suspend(dev);
2649 }
2650 
2651 static int stm32_spi_resume(struct device *dev)
2652 {
2653 	struct spi_controller *ctrl = dev_get_drvdata(dev);
2654 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2655 	int ret;
2656 
2657 	ret = pm_runtime_force_resume(dev);
2658 	if (ret)
2659 		return ret;
2660 
2661 	ret = spi_controller_resume(ctrl);
2662 	if (ret) {
2663 		clk_disable_unprepare(spi->clk);
2664 		return ret;
2665 	}
2666 
2667 	ret = pm_runtime_resume_and_get(dev);
2668 	if (ret < 0) {
2669 		dev_err(dev, "Unable to power device:%d\n", ret);
2670 		return ret;
2671 	}
2672 
2673 	spi->cfg->config(spi);
2674 
2675 	pm_runtime_put_autosuspend(dev);
2676 
2677 	return 0;
2678 }
2679 
2680 static const struct dev_pm_ops stm32_spi_pm_ops = {
2681 	SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
2682 	RUNTIME_PM_OPS(stm32_spi_runtime_suspend, stm32_spi_runtime_resume, NULL)
2683 };
2684 
2685 static struct platform_driver stm32_spi_driver = {
2686 	.probe = stm32_spi_probe,
2687 	.remove = stm32_spi_remove,
2688 	.driver = {
2689 		.name = DRIVER_NAME,
2690 		.pm = pm_ptr(&stm32_spi_pm_ops),
2691 		.of_match_table = stm32_spi_of_match,
2692 	},
2693 };
2694 
2695 module_platform_driver(stm32_spi_driver);
2696 
2697 MODULE_ALIAS("platform:" DRIVER_NAME);
2698 MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
2699 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
2700 MODULE_LICENSE("GPL v2");
2701