xref: /openbmc/linux/drivers/spi/spi-stm32.c (revision 002dff36)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // STMicroelectronics STM32 SPI Controller driver (master mode only)
4 //
5 // Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 // Author(s): Amelie Delaunay <amelie.delaunay@st.com> for STMicroelectronics.
7 
8 #include <linux/debugfs.h>
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/interrupt.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
18 #include <linux/spi/spi.h>
19 
20 #define DRIVER_NAME "spi_stm32"
21 
22 /* STM32F4 SPI registers */
23 #define STM32F4_SPI_CR1			0x00
24 #define STM32F4_SPI_CR2			0x04
25 #define STM32F4_SPI_SR			0x08
26 #define STM32F4_SPI_DR			0x0C
27 #define STM32F4_SPI_I2SCFGR		0x1C
28 
29 /* STM32F4_SPI_CR1 bit fields */
30 #define STM32F4_SPI_CR1_CPHA		BIT(0)
31 #define STM32F4_SPI_CR1_CPOL		BIT(1)
32 #define STM32F4_SPI_CR1_MSTR		BIT(2)
33 #define STM32F4_SPI_CR1_BR_SHIFT	3
34 #define STM32F4_SPI_CR1_BR		GENMASK(5, 3)
35 #define STM32F4_SPI_CR1_SPE		BIT(6)
36 #define STM32F4_SPI_CR1_LSBFRST		BIT(7)
37 #define STM32F4_SPI_CR1_SSI		BIT(8)
38 #define STM32F4_SPI_CR1_SSM		BIT(9)
39 #define STM32F4_SPI_CR1_RXONLY		BIT(10)
40 #define STM32F4_SPI_CR1_DFF		BIT(11)
41 #define STM32F4_SPI_CR1_CRCNEXT		BIT(12)
42 #define STM32F4_SPI_CR1_CRCEN		BIT(13)
43 #define STM32F4_SPI_CR1_BIDIOE		BIT(14)
44 #define STM32F4_SPI_CR1_BIDIMODE	BIT(15)
45 #define STM32F4_SPI_CR1_BR_MIN		0
46 #define STM32F4_SPI_CR1_BR_MAX		(GENMASK(5, 3) >> 3)
47 
48 /* STM32F4_SPI_CR2 bit fields */
49 #define STM32F4_SPI_CR2_RXDMAEN		BIT(0)
50 #define STM32F4_SPI_CR2_TXDMAEN		BIT(1)
51 #define STM32F4_SPI_CR2_SSOE		BIT(2)
52 #define STM32F4_SPI_CR2_FRF		BIT(4)
53 #define STM32F4_SPI_CR2_ERRIE		BIT(5)
54 #define STM32F4_SPI_CR2_RXNEIE		BIT(6)
55 #define STM32F4_SPI_CR2_TXEIE		BIT(7)
56 
57 /* STM32F4_SPI_SR bit fields */
58 #define STM32F4_SPI_SR_RXNE		BIT(0)
59 #define STM32F4_SPI_SR_TXE		BIT(1)
60 #define STM32F4_SPI_SR_CHSIDE		BIT(2)
61 #define STM32F4_SPI_SR_UDR		BIT(3)
62 #define STM32F4_SPI_SR_CRCERR		BIT(4)
63 #define STM32F4_SPI_SR_MODF		BIT(5)
64 #define STM32F4_SPI_SR_OVR		BIT(6)
65 #define STM32F4_SPI_SR_BSY		BIT(7)
66 #define STM32F4_SPI_SR_FRE		BIT(8)
67 
68 /* STM32F4_SPI_I2SCFGR bit fields */
69 #define STM32F4_SPI_I2SCFGR_I2SMOD	BIT(11)
70 
71 /* STM32F4 SPI Baud Rate min/max divisor */
72 #define STM32F4_SPI_BR_DIV_MIN		(2 << STM32F4_SPI_CR1_BR_MIN)
73 #define STM32F4_SPI_BR_DIV_MAX		(2 << STM32F4_SPI_CR1_BR_MAX)
74 
75 /* STM32H7 SPI registers */
76 #define STM32H7_SPI_CR1			0x00
77 #define STM32H7_SPI_CR2			0x04
78 #define STM32H7_SPI_CFG1		0x08
79 #define STM32H7_SPI_CFG2		0x0C
80 #define STM32H7_SPI_IER			0x10
81 #define STM32H7_SPI_SR			0x14
82 #define STM32H7_SPI_IFCR		0x18
83 #define STM32H7_SPI_TXDR		0x20
84 #define STM32H7_SPI_RXDR		0x30
85 #define STM32H7_SPI_I2SCFGR		0x50
86 
87 /* STM32H7_SPI_CR1 bit fields */
88 #define STM32H7_SPI_CR1_SPE		BIT(0)
89 #define STM32H7_SPI_CR1_MASRX		BIT(8)
90 #define STM32H7_SPI_CR1_CSTART		BIT(9)
91 #define STM32H7_SPI_CR1_CSUSP		BIT(10)
92 #define STM32H7_SPI_CR1_HDDIR		BIT(11)
93 #define STM32H7_SPI_CR1_SSI		BIT(12)
94 
95 /* STM32H7_SPI_CR2 bit fields */
96 #define STM32H7_SPI_CR2_TSIZE_SHIFT	0
97 #define STM32H7_SPI_CR2_TSIZE		GENMASK(15, 0)
98 
99 /* STM32H7_SPI_CFG1 bit fields */
100 #define STM32H7_SPI_CFG1_DSIZE_SHIFT	0
101 #define STM32H7_SPI_CFG1_DSIZE		GENMASK(4, 0)
102 #define STM32H7_SPI_CFG1_FTHLV_SHIFT	5
103 #define STM32H7_SPI_CFG1_FTHLV		GENMASK(8, 5)
104 #define STM32H7_SPI_CFG1_RXDMAEN	BIT(14)
105 #define STM32H7_SPI_CFG1_TXDMAEN	BIT(15)
106 #define STM32H7_SPI_CFG1_MBR_SHIFT	28
107 #define STM32H7_SPI_CFG1_MBR		GENMASK(30, 28)
108 #define STM32H7_SPI_CFG1_MBR_MIN	0
109 #define STM32H7_SPI_CFG1_MBR_MAX	(GENMASK(30, 28) >> 28)
110 
111 /* STM32H7_SPI_CFG2 bit fields */
112 #define STM32H7_SPI_CFG2_MIDI_SHIFT	4
113 #define STM32H7_SPI_CFG2_MIDI		GENMASK(7, 4)
114 #define STM32H7_SPI_CFG2_COMM_SHIFT	17
115 #define STM32H7_SPI_CFG2_COMM		GENMASK(18, 17)
116 #define STM32H7_SPI_CFG2_SP_SHIFT	19
117 #define STM32H7_SPI_CFG2_SP		GENMASK(21, 19)
118 #define STM32H7_SPI_CFG2_MASTER		BIT(22)
119 #define STM32H7_SPI_CFG2_LSBFRST	BIT(23)
120 #define STM32H7_SPI_CFG2_CPHA		BIT(24)
121 #define STM32H7_SPI_CFG2_CPOL		BIT(25)
122 #define STM32H7_SPI_CFG2_SSM		BIT(26)
123 #define STM32H7_SPI_CFG2_AFCNTR		BIT(31)
124 
125 /* STM32H7_SPI_IER bit fields */
126 #define STM32H7_SPI_IER_RXPIE		BIT(0)
127 #define STM32H7_SPI_IER_TXPIE		BIT(1)
128 #define STM32H7_SPI_IER_DXPIE		BIT(2)
129 #define STM32H7_SPI_IER_EOTIE		BIT(3)
130 #define STM32H7_SPI_IER_TXTFIE		BIT(4)
131 #define STM32H7_SPI_IER_OVRIE		BIT(6)
132 #define STM32H7_SPI_IER_MODFIE		BIT(9)
133 #define STM32H7_SPI_IER_ALL		GENMASK(10, 0)
134 
135 /* STM32H7_SPI_SR bit fields */
136 #define STM32H7_SPI_SR_RXP		BIT(0)
137 #define STM32H7_SPI_SR_TXP		BIT(1)
138 #define STM32H7_SPI_SR_EOT		BIT(3)
139 #define STM32H7_SPI_SR_OVR		BIT(6)
140 #define STM32H7_SPI_SR_MODF		BIT(9)
141 #define STM32H7_SPI_SR_SUSP		BIT(11)
142 #define STM32H7_SPI_SR_RXPLVL_SHIFT	13
143 #define STM32H7_SPI_SR_RXPLVL		GENMASK(14, 13)
144 #define STM32H7_SPI_SR_RXWNE		BIT(15)
145 
146 /* STM32H7_SPI_IFCR bit fields */
147 #define STM32H7_SPI_IFCR_ALL		GENMASK(11, 3)
148 
149 /* STM32H7_SPI_I2SCFGR bit fields */
150 #define STM32H7_SPI_I2SCFGR_I2SMOD	BIT(0)
151 
152 /* STM32H7 SPI Master Baud Rate min/max divisor */
153 #define STM32H7_SPI_MBR_DIV_MIN		(2 << STM32H7_SPI_CFG1_MBR_MIN)
154 #define STM32H7_SPI_MBR_DIV_MAX		(2 << STM32H7_SPI_CFG1_MBR_MAX)
155 
156 /* STM32H7 SPI Communication mode */
157 #define STM32H7_SPI_FULL_DUPLEX		0
158 #define STM32H7_SPI_SIMPLEX_TX		1
159 #define STM32H7_SPI_SIMPLEX_RX		2
160 #define STM32H7_SPI_HALF_DUPLEX		3
161 
162 /* SPI Communication type */
163 #define SPI_FULL_DUPLEX		0
164 #define SPI_SIMPLEX_TX		1
165 #define SPI_SIMPLEX_RX		2
166 #define SPI_3WIRE_TX		3
167 #define SPI_3WIRE_RX		4
168 
169 #define SPI_1HZ_NS		1000000000
170 
171 /*
172  * use PIO for small transfers, avoiding DMA setup/teardown overhead for drivers
173  * without fifo buffers.
174  */
175 #define SPI_DMA_MIN_BYTES	16
176 
177 /**
178  * struct stm32_spi_reg - stm32 SPI register & bitfield desc
179  * @reg:		register offset
180  * @mask:		bitfield mask
181  * @shift:		left shift
182  */
183 struct stm32_spi_reg {
184 	int reg;
185 	int mask;
186 	int shift;
187 };
188 
189 /**
190  * struct stm32_spi_regspec - stm32 registers definition, compatible dependent data
191  * @en: enable register and SPI enable bit
192  * @dma_rx_en: SPI DMA RX enable register end SPI DMA RX enable bit
193  * @dma_tx_en: SPI DMA TX enable register end SPI DMA TX enable bit
194  * @cpol: clock polarity register and polarity bit
195  * @cpha: clock phase register and phase bit
196  * @lsb_first: LSB transmitted first register and bit
197  * @br: baud rate register and bitfields
198  * @rx: SPI RX data register
199  * @tx: SPI TX data register
200  */
201 struct stm32_spi_regspec {
202 	const struct stm32_spi_reg en;
203 	const struct stm32_spi_reg dma_rx_en;
204 	const struct stm32_spi_reg dma_tx_en;
205 	const struct stm32_spi_reg cpol;
206 	const struct stm32_spi_reg cpha;
207 	const struct stm32_spi_reg lsb_first;
208 	const struct stm32_spi_reg br;
209 	const struct stm32_spi_reg rx;
210 	const struct stm32_spi_reg tx;
211 };
212 
213 struct stm32_spi;
214 
215 /**
216  * struct stm32_spi_cfg - stm32 compatible configuration data
217  * @regs: registers descriptions
218  * @get_fifo_size: routine to get fifo size
219  * @get_bpw_mask: routine to get bits per word mask
220  * @disable: routine to disable controller
221  * @config: routine to configure controller as SPI Master
222  * @set_bpw: routine to configure registers to for bits per word
223  * @set_mode: routine to configure registers to desired mode
224  * @set_data_idleness: optional routine to configure registers to desired idle
225  * time between frames (if driver has this functionality)
226  * @set_number_of_data: optional routine to configure registers to desired
227  * number of data (if driver has this functionality)
228  * @can_dma: routine to determine if the transfer is eligible for DMA use
229  * @transfer_one_dma_start: routine to start transfer a single spi_transfer
230  * using DMA
231  * @dma_rx_cb: routine to call after DMA RX channel operation is complete
232  * @dma_tx_cb: routine to call after DMA TX channel operation is complete
233  * @transfer_one_irq: routine to configure interrupts for driver
234  * @irq_handler_event: Interrupt handler for SPI controller events
235  * @irq_handler_thread: thread of interrupt handler for SPI controller
236  * @baud_rate_div_min: minimum baud rate divisor
237  * @baud_rate_div_max: maximum baud rate divisor
238  * @has_fifo: boolean to know if fifo is used for driver
239  * @has_startbit: boolean to know if start bit is used to start transfer
240  */
241 struct stm32_spi_cfg {
242 	const struct stm32_spi_regspec *regs;
243 	int (*get_fifo_size)(struct stm32_spi *spi);
244 	int (*get_bpw_mask)(struct stm32_spi *spi);
245 	void (*disable)(struct stm32_spi *spi);
246 	int (*config)(struct stm32_spi *spi);
247 	void (*set_bpw)(struct stm32_spi *spi);
248 	int (*set_mode)(struct stm32_spi *spi, unsigned int comm_type);
249 	void (*set_data_idleness)(struct stm32_spi *spi, u32 length);
250 	int (*set_number_of_data)(struct stm32_spi *spi, u32 length);
251 	void (*transfer_one_dma_start)(struct stm32_spi *spi);
252 	void (*dma_rx_cb)(void *data);
253 	void (*dma_tx_cb)(void *data);
254 	int (*transfer_one_irq)(struct stm32_spi *spi);
255 	irqreturn_t (*irq_handler_event)(int irq, void *dev_id);
256 	irqreturn_t (*irq_handler_thread)(int irq, void *dev_id);
257 	unsigned int baud_rate_div_min;
258 	unsigned int baud_rate_div_max;
259 	bool has_fifo;
260 };
261 
262 /**
263  * struct stm32_spi - private data of the SPI controller
264  * @dev: driver model representation of the controller
265  * @master: controller master interface
266  * @cfg: compatible configuration data
267  * @base: virtual memory area
268  * @clk: hw kernel clock feeding the SPI clock generator
269  * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator
270  * @rst: SPI controller reset line
271  * @lock: prevent I/O concurrent access
272  * @irq: SPI controller interrupt line
273  * @fifo_size: size of the embedded fifo in bytes
274  * @cur_midi: master inter-data idleness in ns
275  * @cur_speed: speed configured in Hz
276  * @cur_bpw: number of bits in a single SPI data frame
277  * @cur_fthlv: fifo threshold level (data frames in a single data packet)
278  * @cur_comm: SPI communication mode
279  * @cur_xferlen: current transfer length in bytes
280  * @cur_usedma: boolean to know if dma is used in current transfer
281  * @tx_buf: data to be written, or NULL
282  * @rx_buf: data to be read, or NULL
283  * @tx_len: number of data to be written in bytes
284  * @rx_len: number of data to be read in bytes
285  * @dma_tx: dma channel for TX transfer
286  * @dma_rx: dma channel for RX transfer
287  * @phys_addr: SPI registers physical base address
288  */
289 struct stm32_spi {
290 	struct device *dev;
291 	struct spi_master *master;
292 	const struct stm32_spi_cfg *cfg;
293 	void __iomem *base;
294 	struct clk *clk;
295 	u32 clk_rate;
296 	struct reset_control *rst;
297 	spinlock_t lock; /* prevent I/O concurrent access */
298 	int irq;
299 	unsigned int fifo_size;
300 
301 	unsigned int cur_midi;
302 	unsigned int cur_speed;
303 	unsigned int cur_bpw;
304 	unsigned int cur_fthlv;
305 	unsigned int cur_comm;
306 	unsigned int cur_xferlen;
307 	bool cur_usedma;
308 
309 	const void *tx_buf;
310 	void *rx_buf;
311 	int tx_len;
312 	int rx_len;
313 	struct dma_chan *dma_tx;
314 	struct dma_chan *dma_rx;
315 	dma_addr_t phys_addr;
316 };
317 
318 static const struct stm32_spi_regspec stm32f4_spi_regspec = {
319 	.en = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE },
320 
321 	.dma_rx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_RXDMAEN },
322 	.dma_tx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN },
323 
324 	.cpol = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPOL },
325 	.cpha = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPHA },
326 	.lsb_first = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_LSBFRST },
327 	.br = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_BR, STM32F4_SPI_CR1_BR_SHIFT },
328 
329 	.rx = { STM32F4_SPI_DR },
330 	.tx = { STM32F4_SPI_DR },
331 };
332 
333 static const struct stm32_spi_regspec stm32h7_spi_regspec = {
334 	/* SPI data transfer is enabled but spi_ker_ck is idle.
335 	 * CFG1 and CFG2 registers are write protected when SPE is enabled.
336 	 */
337 	.en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
338 
339 	.dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
340 	.dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
341 
342 	.cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
343 	.cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
344 	.lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
345 	.br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
346 		STM32H7_SPI_CFG1_MBR_SHIFT },
347 
348 	.rx = { STM32H7_SPI_RXDR },
349 	.tx = { STM32H7_SPI_TXDR },
350 };
351 
352 static inline void stm32_spi_set_bits(struct stm32_spi *spi,
353 				      u32 offset, u32 bits)
354 {
355 	writel_relaxed(readl_relaxed(spi->base + offset) | bits,
356 		       spi->base + offset);
357 }
358 
359 static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
360 				      u32 offset, u32 bits)
361 {
362 	writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
363 		       spi->base + offset);
364 }
365 
366 /**
367  * stm32h7_spi_get_fifo_size - Return fifo size
368  * @spi: pointer to the spi controller data structure
369  */
370 static int stm32h7_spi_get_fifo_size(struct stm32_spi *spi)
371 {
372 	unsigned long flags;
373 	u32 count = 0;
374 
375 	spin_lock_irqsave(&spi->lock, flags);
376 
377 	stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
378 
379 	while (readl_relaxed(spi->base + STM32H7_SPI_SR) & STM32H7_SPI_SR_TXP)
380 		writeb_relaxed(++count, spi->base + STM32H7_SPI_TXDR);
381 
382 	stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
383 
384 	spin_unlock_irqrestore(&spi->lock, flags);
385 
386 	dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
387 
388 	return count;
389 }
390 
391 /**
392  * stm32f4_spi_get_bpw_mask - Return bits per word mask
393  * @spi: pointer to the spi controller data structure
394  */
395 static int stm32f4_spi_get_bpw_mask(struct stm32_spi *spi)
396 {
397 	dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
398 	return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
399 }
400 
401 /**
402  * stm32h7_spi_get_bpw_mask - Return bits per word mask
403  * @spi: pointer to the spi controller data structure
404  */
405 static int stm32h7_spi_get_bpw_mask(struct stm32_spi *spi)
406 {
407 	unsigned long flags;
408 	u32 cfg1, max_bpw;
409 
410 	spin_lock_irqsave(&spi->lock, flags);
411 
412 	/*
413 	 * The most significant bit at DSIZE bit field is reserved when the
414 	 * maximum data size of periperal instances is limited to 16-bit
415 	 */
416 	stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE);
417 
418 	cfg1 = readl_relaxed(spi->base + STM32H7_SPI_CFG1);
419 	max_bpw = (cfg1 & STM32H7_SPI_CFG1_DSIZE) >>
420 		  STM32H7_SPI_CFG1_DSIZE_SHIFT;
421 	max_bpw += 1;
422 
423 	spin_unlock_irqrestore(&spi->lock, flags);
424 
425 	dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
426 
427 	return SPI_BPW_RANGE_MASK(4, max_bpw);
428 }
429 
430 /**
431  * stm32_spi_prepare_mbr - Determine baud rate divisor value
432  * @spi: pointer to the spi controller data structure
433  * @speed_hz: requested speed
434  * @min_div: minimum baud rate divisor
435  * @max_div: maximum baud rate divisor
436  *
437  * Return baud rate divisor value in case of success or -EINVAL
438  */
439 static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
440 				 u32 min_div, u32 max_div)
441 {
442 	u32 div, mbrdiv;
443 
444 	div = DIV_ROUND_UP(spi->clk_rate, speed_hz);
445 
446 	/*
447 	 * SPI framework set xfer->speed_hz to master->max_speed_hz if
448 	 * xfer->speed_hz is greater than master->max_speed_hz, and it returns
449 	 * an error when xfer->speed_hz is lower than master->min_speed_hz, so
450 	 * no need to check it there.
451 	 * However, we need to ensure the following calculations.
452 	 */
453 	if ((div < min_div) || (div > max_div))
454 		return -EINVAL;
455 
456 	/* Determine the first power of 2 greater than or equal to div */
457 	if (div & (div - 1))
458 		mbrdiv = fls(div);
459 	else
460 		mbrdiv = fls(div) - 1;
461 
462 	spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
463 
464 	return mbrdiv - 1;
465 }
466 
467 /**
468  * stm32h7_spi_prepare_fthlv - Determine FIFO threshold level
469  * @spi: pointer to the spi controller data structure
470  */
471 static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi)
472 {
473 	u32 fthlv, half_fifo;
474 
475 	/* data packet should not exceed 1/2 of fifo space */
476 	half_fifo = (spi->fifo_size / 2);
477 
478 	if (spi->cur_bpw <= 8)
479 		fthlv = half_fifo;
480 	else if (spi->cur_bpw <= 16)
481 		fthlv = half_fifo / 2;
482 	else
483 		fthlv = half_fifo / 4;
484 
485 	/* align packet size with data registers access */
486 	if (spi->cur_bpw > 8)
487 		fthlv -= (fthlv % 2); /* multiple of 2 */
488 	else
489 		fthlv -= (fthlv % 4); /* multiple of 4 */
490 
491 	return fthlv;
492 }
493 
494 /**
495  * stm32f4_spi_write_tx - Write bytes to Transmit Data Register
496  * @spi: pointer to the spi controller data structure
497  *
498  * Read from tx_buf depends on remaining bytes to avoid to read beyond
499  * tx_buf end.
500  */
501 static void stm32f4_spi_write_tx(struct stm32_spi *spi)
502 {
503 	if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
504 				  STM32F4_SPI_SR_TXE)) {
505 		u32 offs = spi->cur_xferlen - spi->tx_len;
506 
507 		if (spi->cur_bpw == 16) {
508 			const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
509 
510 			writew_relaxed(*tx_buf16, spi->base + STM32F4_SPI_DR);
511 			spi->tx_len -= sizeof(u16);
512 		} else {
513 			const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
514 
515 			writeb_relaxed(*tx_buf8, spi->base + STM32F4_SPI_DR);
516 			spi->tx_len -= sizeof(u8);
517 		}
518 	}
519 
520 	dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
521 }
522 
523 /**
524  * stm32h7_spi_write_txfifo - Write bytes in Transmit Data Register
525  * @spi: pointer to the spi controller data structure
526  *
527  * Read from tx_buf depends on remaining bytes to avoid to read beyond
528  * tx_buf end.
529  */
530 static void stm32h7_spi_write_txfifo(struct stm32_spi *spi)
531 {
532 	while ((spi->tx_len > 0) &&
533 		       (readl_relaxed(spi->base + STM32H7_SPI_SR) &
534 			STM32H7_SPI_SR_TXP)) {
535 		u32 offs = spi->cur_xferlen - spi->tx_len;
536 
537 		if (spi->tx_len >= sizeof(u32)) {
538 			const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
539 
540 			writel_relaxed(*tx_buf32, spi->base + STM32H7_SPI_TXDR);
541 			spi->tx_len -= sizeof(u32);
542 		} else if (spi->tx_len >= sizeof(u16)) {
543 			const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
544 
545 			writew_relaxed(*tx_buf16, spi->base + STM32H7_SPI_TXDR);
546 			spi->tx_len -= sizeof(u16);
547 		} else {
548 			const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
549 
550 			writeb_relaxed(*tx_buf8, spi->base + STM32H7_SPI_TXDR);
551 			spi->tx_len -= sizeof(u8);
552 		}
553 	}
554 
555 	dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
556 }
557 
558 /**
559  * stm32f4_spi_read_rx - Read bytes from Receive Data Register
560  * @spi: pointer to the spi controller data structure
561  *
562  * Write in rx_buf depends on remaining bytes to avoid to write beyond
563  * rx_buf end.
564  */
565 static void stm32f4_spi_read_rx(struct stm32_spi *spi)
566 {
567 	if ((spi->rx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
568 				  STM32F4_SPI_SR_RXNE)) {
569 		u32 offs = spi->cur_xferlen - spi->rx_len;
570 
571 		if (spi->cur_bpw == 16) {
572 			u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
573 
574 			*rx_buf16 = readw_relaxed(spi->base + STM32F4_SPI_DR);
575 			spi->rx_len -= sizeof(u16);
576 		} else {
577 			u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
578 
579 			*rx_buf8 = readb_relaxed(spi->base + STM32F4_SPI_DR);
580 			spi->rx_len -= sizeof(u8);
581 		}
582 	}
583 
584 	dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->rx_len);
585 }
586 
587 /**
588  * stm32h7_spi_read_rxfifo - Read bytes in Receive Data Register
589  * @spi: pointer to the spi controller data structure
590  * @flush: boolean indicating that FIFO should be flushed
591  *
592  * Write in rx_buf depends on remaining bytes to avoid to write beyond
593  * rx_buf end.
594  */
595 static void stm32h7_spi_read_rxfifo(struct stm32_spi *spi, bool flush)
596 {
597 	u32 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
598 	u32 rxplvl = (sr & STM32H7_SPI_SR_RXPLVL) >>
599 		     STM32H7_SPI_SR_RXPLVL_SHIFT;
600 
601 	while ((spi->rx_len > 0) &&
602 	       ((sr & STM32H7_SPI_SR_RXP) ||
603 		(flush && ((sr & STM32H7_SPI_SR_RXWNE) || (rxplvl > 0))))) {
604 		u32 offs = spi->cur_xferlen - spi->rx_len;
605 
606 		if ((spi->rx_len >= sizeof(u32)) ||
607 		    (flush && (sr & STM32H7_SPI_SR_RXWNE))) {
608 			u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
609 
610 			*rx_buf32 = readl_relaxed(spi->base + STM32H7_SPI_RXDR);
611 			spi->rx_len -= sizeof(u32);
612 		} else if ((spi->rx_len >= sizeof(u16)) ||
613 			   (flush && (rxplvl >= 2 || spi->cur_bpw > 8))) {
614 			u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
615 
616 			*rx_buf16 = readw_relaxed(spi->base + STM32H7_SPI_RXDR);
617 			spi->rx_len -= sizeof(u16);
618 		} else {
619 			u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
620 
621 			*rx_buf8 = readb_relaxed(spi->base + STM32H7_SPI_RXDR);
622 			spi->rx_len -= sizeof(u8);
623 		}
624 
625 		sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
626 		rxplvl = (sr & STM32H7_SPI_SR_RXPLVL) >>
627 			 STM32H7_SPI_SR_RXPLVL_SHIFT;
628 	}
629 
630 	dev_dbg(spi->dev, "%s%s: %d bytes left\n", __func__,
631 		flush ? "(flush)" : "", spi->rx_len);
632 }
633 
634 /**
635  * stm32_spi_enable - Enable SPI controller
636  * @spi: pointer to the spi controller data structure
637  */
638 static void stm32_spi_enable(struct stm32_spi *spi)
639 {
640 	dev_dbg(spi->dev, "enable controller\n");
641 
642 	stm32_spi_set_bits(spi, spi->cfg->regs->en.reg,
643 			   spi->cfg->regs->en.mask);
644 }
645 
646 /**
647  * stm32f4_spi_disable - Disable SPI controller
648  * @spi: pointer to the spi controller data structure
649  */
650 static void stm32f4_spi_disable(struct stm32_spi *spi)
651 {
652 	unsigned long flags;
653 	u32 sr;
654 
655 	dev_dbg(spi->dev, "disable controller\n");
656 
657 	spin_lock_irqsave(&spi->lock, flags);
658 
659 	if (!(readl_relaxed(spi->base + STM32F4_SPI_CR1) &
660 	      STM32F4_SPI_CR1_SPE)) {
661 		spin_unlock_irqrestore(&spi->lock, flags);
662 		return;
663 	}
664 
665 	/* Disable interrupts */
666 	stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXEIE |
667 						 STM32F4_SPI_CR2_RXNEIE |
668 						 STM32F4_SPI_CR2_ERRIE);
669 
670 	/* Wait until BSY = 0 */
671 	if (readl_relaxed_poll_timeout_atomic(spi->base + STM32F4_SPI_SR,
672 					      sr, !(sr & STM32F4_SPI_SR_BSY),
673 					      10, 100000) < 0) {
674 		dev_warn(spi->dev, "disabling condition timeout\n");
675 	}
676 
677 	if (spi->cur_usedma && spi->dma_tx)
678 		dmaengine_terminate_all(spi->dma_tx);
679 	if (spi->cur_usedma && spi->dma_rx)
680 		dmaengine_terminate_all(spi->dma_rx);
681 
682 	stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE);
683 
684 	stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN |
685 						 STM32F4_SPI_CR2_RXDMAEN);
686 
687 	/* Sequence to clear OVR flag */
688 	readl_relaxed(spi->base + STM32F4_SPI_DR);
689 	readl_relaxed(spi->base + STM32F4_SPI_SR);
690 
691 	spin_unlock_irqrestore(&spi->lock, flags);
692 }
693 
694 /**
695  * stm32h7_spi_disable - Disable SPI controller
696  * @spi: pointer to the spi controller data structure
697  *
698  * RX-Fifo is flushed when SPI controller is disabled. To prevent any data
699  * loss, use stm32h7_spi_read_rxfifo(flush) to read the remaining bytes in
700  * RX-Fifo.
701  * Normally, if TSIZE has been configured, we should relax the hardware at the
702  * reception of the EOT interrupt. But in case of error, EOT will not be
703  * raised. So the subsystem unprepare_message call allows us to properly
704  * complete the transfer from an hardware point of view.
705  */
706 static void stm32h7_spi_disable(struct stm32_spi *spi)
707 {
708 	unsigned long flags;
709 	u32 cr1, sr;
710 
711 	dev_dbg(spi->dev, "disable controller\n");
712 
713 	spin_lock_irqsave(&spi->lock, flags);
714 
715 	cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1);
716 
717 	if (!(cr1 & STM32H7_SPI_CR1_SPE)) {
718 		spin_unlock_irqrestore(&spi->lock, flags);
719 		return;
720 	}
721 
722 	/* Wait on EOT or suspend the flow */
723 	if (readl_relaxed_poll_timeout_atomic(spi->base + STM32H7_SPI_SR,
724 					      sr, !(sr & STM32H7_SPI_SR_EOT),
725 					      10, 100000) < 0) {
726 		if (cr1 & STM32H7_SPI_CR1_CSTART) {
727 			writel_relaxed(cr1 | STM32H7_SPI_CR1_CSUSP,
728 				       spi->base + STM32H7_SPI_CR1);
729 			if (readl_relaxed_poll_timeout_atomic(
730 						spi->base + STM32H7_SPI_SR,
731 						sr, !(sr & STM32H7_SPI_SR_SUSP),
732 						10, 100000) < 0)
733 				dev_warn(spi->dev,
734 					 "Suspend request timeout\n");
735 		}
736 	}
737 
738 	if (!spi->cur_usedma && spi->rx_buf && (spi->rx_len > 0))
739 		stm32h7_spi_read_rxfifo(spi, true);
740 
741 	if (spi->cur_usedma && spi->dma_tx)
742 		dmaengine_terminate_all(spi->dma_tx);
743 	if (spi->cur_usedma && spi->dma_rx)
744 		dmaengine_terminate_all(spi->dma_rx);
745 
746 	stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
747 
748 	stm32_spi_clr_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN |
749 						STM32H7_SPI_CFG1_RXDMAEN);
750 
751 	/* Disable interrupts and clear status flags */
752 	writel_relaxed(0, spi->base + STM32H7_SPI_IER);
753 	writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR);
754 
755 	spin_unlock_irqrestore(&spi->lock, flags);
756 }
757 
758 /**
759  * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use
760  * @master: controller master interface
761  * @spi_dev: pointer to the spi device
762  * @transfer: pointer to spi transfer
763  *
764  * If driver has fifo and the current transfer size is greater than fifo size,
765  * use DMA. Otherwise use DMA for transfer longer than defined DMA min bytes.
766  */
767 static bool stm32_spi_can_dma(struct spi_master *master,
768 			      struct spi_device *spi_dev,
769 			      struct spi_transfer *transfer)
770 {
771 	unsigned int dma_size;
772 	struct stm32_spi *spi = spi_master_get_devdata(master);
773 
774 	if (spi->cfg->has_fifo)
775 		dma_size = spi->fifo_size;
776 	else
777 		dma_size = SPI_DMA_MIN_BYTES;
778 
779 	dev_dbg(spi->dev, "%s: %s\n", __func__,
780 		(transfer->len > dma_size) ? "true" : "false");
781 
782 	return (transfer->len > dma_size);
783 }
784 
785 /**
786  * stm32f4_spi_irq_event - Interrupt handler for SPI controller events
787  * @irq: interrupt line
788  * @dev_id: SPI controller master interface
789  */
790 static irqreturn_t stm32f4_spi_irq_event(int irq, void *dev_id)
791 {
792 	struct spi_master *master = dev_id;
793 	struct stm32_spi *spi = spi_master_get_devdata(master);
794 	u32 sr, mask = 0;
795 	unsigned long flags;
796 	bool end = false;
797 
798 	spin_lock_irqsave(&spi->lock, flags);
799 
800 	sr = readl_relaxed(spi->base + STM32F4_SPI_SR);
801 	/*
802 	 * BSY flag is not handled in interrupt but it is normal behavior when
803 	 * this flag is set.
804 	 */
805 	sr &= ~STM32F4_SPI_SR_BSY;
806 
807 	if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX ||
808 				 spi->cur_comm == SPI_3WIRE_TX)) {
809 		/* OVR flag shouldn't be handled for TX only mode */
810 		sr &= ~STM32F4_SPI_SR_OVR | STM32F4_SPI_SR_RXNE;
811 		mask |= STM32F4_SPI_SR_TXE;
812 	}
813 
814 	if (!spi->cur_usedma && (spi->cur_comm == SPI_FULL_DUPLEX ||
815 				spi->cur_comm == SPI_SIMPLEX_RX ||
816 				spi->cur_comm == SPI_3WIRE_RX)) {
817 		/* TXE flag is set and is handled when RXNE flag occurs */
818 		sr &= ~STM32F4_SPI_SR_TXE;
819 		mask |= STM32F4_SPI_SR_RXNE | STM32F4_SPI_SR_OVR;
820 	}
821 
822 	if (!(sr & mask)) {
823 		dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr);
824 		spin_unlock_irqrestore(&spi->lock, flags);
825 		return IRQ_NONE;
826 	}
827 
828 	if (sr & STM32F4_SPI_SR_OVR) {
829 		dev_warn(spi->dev, "Overrun: received value discarded\n");
830 
831 		/* Sequence to clear OVR flag */
832 		readl_relaxed(spi->base + STM32F4_SPI_DR);
833 		readl_relaxed(spi->base + STM32F4_SPI_SR);
834 
835 		/*
836 		 * If overrun is detected, it means that something went wrong,
837 		 * so stop the current transfer. Transfer can wait for next
838 		 * RXNE but DR is already read and end never happens.
839 		 */
840 		end = true;
841 		goto end_irq;
842 	}
843 
844 	if (sr & STM32F4_SPI_SR_TXE) {
845 		if (spi->tx_buf)
846 			stm32f4_spi_write_tx(spi);
847 		if (spi->tx_len == 0)
848 			end = true;
849 	}
850 
851 	if (sr & STM32F4_SPI_SR_RXNE) {
852 		stm32f4_spi_read_rx(spi);
853 		if (spi->rx_len == 0)
854 			end = true;
855 		else if (spi->tx_buf)/* Load data for discontinuous mode */
856 			stm32f4_spi_write_tx(spi);
857 	}
858 
859 end_irq:
860 	if (end) {
861 		/* Immediately disable interrupts to do not generate new one */
862 		stm32_spi_clr_bits(spi, STM32F4_SPI_CR2,
863 					STM32F4_SPI_CR2_TXEIE |
864 					STM32F4_SPI_CR2_RXNEIE |
865 					STM32F4_SPI_CR2_ERRIE);
866 		spin_unlock_irqrestore(&spi->lock, flags);
867 		return IRQ_WAKE_THREAD;
868 	}
869 
870 	spin_unlock_irqrestore(&spi->lock, flags);
871 	return IRQ_HANDLED;
872 }
873 
874 /**
875  * stm32f4_spi_irq_thread - Thread of interrupt handler for SPI controller
876  * @irq: interrupt line
877  * @dev_id: SPI controller master interface
878  */
879 static irqreturn_t stm32f4_spi_irq_thread(int irq, void *dev_id)
880 {
881 	struct spi_master *master = dev_id;
882 	struct stm32_spi *spi = spi_master_get_devdata(master);
883 
884 	spi_finalize_current_transfer(master);
885 	stm32f4_spi_disable(spi);
886 
887 	return IRQ_HANDLED;
888 }
889 
890 /**
891  * stm32h7_spi_irq_thread - Thread of interrupt handler for SPI controller
892  * @irq: interrupt line
893  * @dev_id: SPI controller master interface
894  */
895 static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id)
896 {
897 	struct spi_master *master = dev_id;
898 	struct stm32_spi *spi = spi_master_get_devdata(master);
899 	u32 sr, ier, mask;
900 	unsigned long flags;
901 	bool end = false;
902 
903 	spin_lock_irqsave(&spi->lock, flags);
904 
905 	sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
906 	ier = readl_relaxed(spi->base + STM32H7_SPI_IER);
907 
908 	mask = ier;
909 	/* EOTIE is triggered on EOT, SUSP and TXC events. */
910 	mask |= STM32H7_SPI_SR_SUSP;
911 	/*
912 	 * When TXTF is set, DXPIE and TXPIE are cleared. So in case of
913 	 * Full-Duplex, need to poll RXP event to know if there are remaining
914 	 * data, before disabling SPI.
915 	 */
916 	if (spi->rx_buf && !spi->cur_usedma)
917 		mask |= STM32H7_SPI_SR_RXP;
918 
919 	if (!(sr & mask)) {
920 		dev_dbg(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
921 			sr, ier);
922 		spin_unlock_irqrestore(&spi->lock, flags);
923 		return IRQ_NONE;
924 	}
925 
926 	if (sr & STM32H7_SPI_SR_SUSP) {
927 		dev_warn(spi->dev, "Communication suspended\n");
928 		if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
929 			stm32h7_spi_read_rxfifo(spi, false);
930 		/*
931 		 * If communication is suspended while using DMA, it means
932 		 * that something went wrong, so stop the current transfer
933 		 */
934 		if (spi->cur_usedma)
935 			end = true;
936 	}
937 
938 	if (sr & STM32H7_SPI_SR_MODF) {
939 		dev_warn(spi->dev, "Mode fault: transfer aborted\n");
940 		end = true;
941 	}
942 
943 	if (sr & STM32H7_SPI_SR_OVR) {
944 		dev_warn(spi->dev, "Overrun: received value discarded\n");
945 		if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
946 			stm32h7_spi_read_rxfifo(spi, false);
947 		/*
948 		 * If overrun is detected while using DMA, it means that
949 		 * something went wrong, so stop the current transfer
950 		 */
951 		if (spi->cur_usedma)
952 			end = true;
953 	}
954 
955 	if (sr & STM32H7_SPI_SR_EOT) {
956 		if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
957 			stm32h7_spi_read_rxfifo(spi, true);
958 		end = true;
959 	}
960 
961 	if (sr & STM32H7_SPI_SR_TXP)
962 		if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
963 			stm32h7_spi_write_txfifo(spi);
964 
965 	if (sr & STM32H7_SPI_SR_RXP)
966 		if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
967 			stm32h7_spi_read_rxfifo(spi, false);
968 
969 	writel_relaxed(mask, spi->base + STM32H7_SPI_IFCR);
970 
971 	spin_unlock_irqrestore(&spi->lock, flags);
972 
973 	if (end) {
974 		spi_finalize_current_transfer(master);
975 		stm32h7_spi_disable(spi);
976 	}
977 
978 	return IRQ_HANDLED;
979 }
980 
981 /**
982  * stm32_spi_prepare_msg - set up the controller to transfer a single message
983  * @master: controller master interface
984  * @msg: pointer to spi message
985  */
986 static int stm32_spi_prepare_msg(struct spi_master *master,
987 				 struct spi_message *msg)
988 {
989 	struct stm32_spi *spi = spi_master_get_devdata(master);
990 	struct spi_device *spi_dev = msg->spi;
991 	struct device_node *np = spi_dev->dev.of_node;
992 	unsigned long flags;
993 	u32 clrb = 0, setb = 0;
994 
995 	/* SPI slave device may need time between data frames */
996 	spi->cur_midi = 0;
997 	if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
998 		dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
999 
1000 	if (spi_dev->mode & SPI_CPOL)
1001 		setb |= spi->cfg->regs->cpol.mask;
1002 	else
1003 		clrb |= spi->cfg->regs->cpol.mask;
1004 
1005 	if (spi_dev->mode & SPI_CPHA)
1006 		setb |= spi->cfg->regs->cpha.mask;
1007 	else
1008 		clrb |= spi->cfg->regs->cpha.mask;
1009 
1010 	if (spi_dev->mode & SPI_LSB_FIRST)
1011 		setb |= spi->cfg->regs->lsb_first.mask;
1012 	else
1013 		clrb |= spi->cfg->regs->lsb_first.mask;
1014 
1015 	dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n",
1016 		spi_dev->mode & SPI_CPOL,
1017 		spi_dev->mode & SPI_CPHA,
1018 		spi_dev->mode & SPI_LSB_FIRST,
1019 		spi_dev->mode & SPI_CS_HIGH);
1020 
1021 	spin_lock_irqsave(&spi->lock, flags);
1022 
1023 	/* CPOL, CPHA and LSB FIRST bits have common register */
1024 	if (clrb || setb)
1025 		writel_relaxed(
1026 			(readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) &
1027 			 ~clrb) | setb,
1028 			spi->base + spi->cfg->regs->cpol.reg);
1029 
1030 	spin_unlock_irqrestore(&spi->lock, flags);
1031 
1032 	return 0;
1033 }
1034 
1035 /**
1036  * stm32f4_spi_dma_tx_cb - dma callback
1037  * @data: pointer to the spi controller data structure
1038  *
1039  * DMA callback is called when the transfer is complete for DMA TX channel.
1040  */
1041 static void stm32f4_spi_dma_tx_cb(void *data)
1042 {
1043 	struct stm32_spi *spi = data;
1044 
1045 	if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1046 		spi_finalize_current_transfer(spi->master);
1047 		stm32f4_spi_disable(spi);
1048 	}
1049 }
1050 
1051 /**
1052  * stm32f4_spi_dma_rx_cb - dma callback
1053  * @data: pointer to the spi controller data structure
1054  *
1055  * DMA callback is called when the transfer is complete for DMA RX channel.
1056  */
1057 static void stm32f4_spi_dma_rx_cb(void *data)
1058 {
1059 	struct stm32_spi *spi = data;
1060 
1061 	spi_finalize_current_transfer(spi->master);
1062 	stm32f4_spi_disable(spi);
1063 }
1064 
1065 /**
1066  * stm32h7_spi_dma_cb - dma callback
1067  * @data: pointer to the spi controller data structure
1068  *
1069  * DMA callback is called when the transfer is complete or when an error
1070  * occurs. If the transfer is complete, EOT flag is raised.
1071  */
1072 static void stm32h7_spi_dma_cb(void *data)
1073 {
1074 	struct stm32_spi *spi = data;
1075 	unsigned long flags;
1076 	u32 sr;
1077 
1078 	spin_lock_irqsave(&spi->lock, flags);
1079 
1080 	sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
1081 
1082 	spin_unlock_irqrestore(&spi->lock, flags);
1083 
1084 	if (!(sr & STM32H7_SPI_SR_EOT))
1085 		dev_warn(spi->dev, "DMA error (sr=0x%08x)\n", sr);
1086 
1087 	/* Now wait for EOT, or SUSP or OVR in case of error */
1088 }
1089 
1090 /**
1091  * stm32_spi_dma_config - configure dma slave channel depending on current
1092  *			  transfer bits_per_word.
1093  * @spi: pointer to the spi controller data structure
1094  * @dma_conf: pointer to the dma_slave_config structure
1095  * @dir: direction of the dma transfer
1096  */
1097 static void stm32_spi_dma_config(struct stm32_spi *spi,
1098 				 struct dma_slave_config *dma_conf,
1099 				 enum dma_transfer_direction dir)
1100 {
1101 	enum dma_slave_buswidth buswidth;
1102 	u32 maxburst;
1103 
1104 	if (spi->cur_bpw <= 8)
1105 		buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1106 	else if (spi->cur_bpw <= 16)
1107 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1108 	else
1109 		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1110 
1111 	if (spi->cfg->has_fifo) {
1112 		/* Valid for DMA Half or Full Fifo threshold */
1113 		if (spi->cur_fthlv == 2)
1114 			maxburst = 1;
1115 		else
1116 			maxburst = spi->cur_fthlv;
1117 	} else {
1118 		maxburst = 1;
1119 	}
1120 
1121 	memset(dma_conf, 0, sizeof(struct dma_slave_config));
1122 	dma_conf->direction = dir;
1123 	if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */
1124 		dma_conf->src_addr = spi->phys_addr + spi->cfg->regs->rx.reg;
1125 		dma_conf->src_addr_width = buswidth;
1126 		dma_conf->src_maxburst = maxburst;
1127 
1128 		dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
1129 			buswidth, maxburst);
1130 	} else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */
1131 		dma_conf->dst_addr = spi->phys_addr + spi->cfg->regs->tx.reg;
1132 		dma_conf->dst_addr_width = buswidth;
1133 		dma_conf->dst_maxburst = maxburst;
1134 
1135 		dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
1136 			buswidth, maxburst);
1137 	}
1138 }
1139 
1140 /**
1141  * stm32f4_spi_transfer_one_irq - transfer a single spi_transfer using
1142  *				  interrupts
1143  * @spi: pointer to the spi controller data structure
1144  *
1145  * It must returns 0 if the transfer is finished or 1 if the transfer is still
1146  * in progress.
1147  */
1148 static int stm32f4_spi_transfer_one_irq(struct stm32_spi *spi)
1149 {
1150 	unsigned long flags;
1151 	u32 cr2 = 0;
1152 
1153 	/* Enable the interrupts relative to the current communication mode */
1154 	if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1155 		cr2 |= STM32F4_SPI_CR2_TXEIE;
1156 	} else if (spi->cur_comm == SPI_FULL_DUPLEX ||
1157 				spi->cur_comm == SPI_SIMPLEX_RX ||
1158 				spi->cur_comm == SPI_3WIRE_RX) {
1159 		/* In transmit-only mode, the OVR flag is set in the SR register
1160 		 * since the received data are never read. Therefore set OVR
1161 		 * interrupt only when rx buffer is available.
1162 		 */
1163 		cr2 |= STM32F4_SPI_CR2_RXNEIE | STM32F4_SPI_CR2_ERRIE;
1164 	} else {
1165 		return -EINVAL;
1166 	}
1167 
1168 	spin_lock_irqsave(&spi->lock, flags);
1169 
1170 	stm32_spi_set_bits(spi, STM32F4_SPI_CR2, cr2);
1171 
1172 	stm32_spi_enable(spi);
1173 
1174 	/* starting data transfer when buffer is loaded */
1175 	if (spi->tx_buf)
1176 		stm32f4_spi_write_tx(spi);
1177 
1178 	spin_unlock_irqrestore(&spi->lock, flags);
1179 
1180 	return 1;
1181 }
1182 
1183 /**
1184  * stm32h7_spi_transfer_one_irq - transfer a single spi_transfer using
1185  *				  interrupts
1186  * @spi: pointer to the spi controller data structure
1187  *
1188  * It must returns 0 if the transfer is finished or 1 if the transfer is still
1189  * in progress.
1190  */
1191 static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
1192 {
1193 	unsigned long flags;
1194 	u32 ier = 0;
1195 
1196 	/* Enable the interrupts relative to the current communication mode */
1197 	if (spi->tx_buf && spi->rx_buf)	/* Full Duplex */
1198 		ier |= STM32H7_SPI_IER_DXPIE;
1199 	else if (spi->tx_buf)		/* Half-Duplex TX dir or Simplex TX */
1200 		ier |= STM32H7_SPI_IER_TXPIE;
1201 	else if (spi->rx_buf)		/* Half-Duplex RX dir or Simplex RX */
1202 		ier |= STM32H7_SPI_IER_RXPIE;
1203 
1204 	/* Enable the interrupts relative to the end of transfer */
1205 	ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE |
1206 	       STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
1207 
1208 	spin_lock_irqsave(&spi->lock, flags);
1209 
1210 	stm32_spi_enable(spi);
1211 
1212 	/* Be sure to have data in fifo before starting data transfer */
1213 	if (spi->tx_buf)
1214 		stm32h7_spi_write_txfifo(spi);
1215 
1216 	stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1217 
1218 	writel_relaxed(ier, spi->base + STM32H7_SPI_IER);
1219 
1220 	spin_unlock_irqrestore(&spi->lock, flags);
1221 
1222 	return 1;
1223 }
1224 
1225 /**
1226  * stm32f4_spi_transfer_one_dma_start - Set SPI driver registers to start
1227  *					transfer using DMA
1228  * @spi: pointer to the spi controller data structure
1229  */
1230 static void stm32f4_spi_transfer_one_dma_start(struct stm32_spi *spi)
1231 {
1232 	/* In DMA mode end of transfer is handled by DMA TX or RX callback. */
1233 	if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX ||
1234 	    spi->cur_comm == SPI_FULL_DUPLEX) {
1235 		/*
1236 		 * In transmit-only mode, the OVR flag is set in the SR register
1237 		 * since the received data are never read. Therefore set OVR
1238 		 * interrupt only when rx buffer is available.
1239 		 */
1240 		stm32_spi_set_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_ERRIE);
1241 	}
1242 
1243 	stm32_spi_enable(spi);
1244 }
1245 
1246 /**
1247  * stm32h7_spi_transfer_one_dma_start - Set SPI driver registers to start
1248  *					transfer using DMA
1249  * @spi: pointer to the spi controller data structure
1250  */
1251 static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
1252 {
1253 	/* Enable the interrupts relative to the end of transfer */
1254 	stm32_spi_set_bits(spi, STM32H7_SPI_IER, STM32H7_SPI_IER_EOTIE |
1255 						 STM32H7_SPI_IER_TXTFIE |
1256 						 STM32H7_SPI_IER_OVRIE |
1257 						 STM32H7_SPI_IER_MODFIE);
1258 
1259 	stm32_spi_enable(spi);
1260 
1261 	stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1262 }
1263 
1264 /**
1265  * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
1266  * @spi: pointer to the spi controller data structure
1267  * @xfer: pointer to the spi_transfer structure
1268  *
1269  * It must returns 0 if the transfer is finished or 1 if the transfer is still
1270  * in progress.
1271  */
1272 static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
1273 				      struct spi_transfer *xfer)
1274 {
1275 	struct dma_slave_config tx_dma_conf, rx_dma_conf;
1276 	struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc;
1277 	unsigned long flags;
1278 
1279 	spin_lock_irqsave(&spi->lock, flags);
1280 
1281 	rx_dma_desc = NULL;
1282 	if (spi->rx_buf && spi->dma_rx) {
1283 		stm32_spi_dma_config(spi, &rx_dma_conf, DMA_DEV_TO_MEM);
1284 		dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1285 
1286 		/* Enable Rx DMA request */
1287 		stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1288 				   spi->cfg->regs->dma_rx_en.mask);
1289 
1290 		rx_dma_desc = dmaengine_prep_slave_sg(
1291 					spi->dma_rx, xfer->rx_sg.sgl,
1292 					xfer->rx_sg.nents,
1293 					rx_dma_conf.direction,
1294 					DMA_PREP_INTERRUPT);
1295 	}
1296 
1297 	tx_dma_desc = NULL;
1298 	if (spi->tx_buf && spi->dma_tx) {
1299 		stm32_spi_dma_config(spi, &tx_dma_conf, DMA_MEM_TO_DEV);
1300 		dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
1301 
1302 		tx_dma_desc = dmaengine_prep_slave_sg(
1303 					spi->dma_tx, xfer->tx_sg.sgl,
1304 					xfer->tx_sg.nents,
1305 					tx_dma_conf.direction,
1306 					DMA_PREP_INTERRUPT);
1307 	}
1308 
1309 	if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
1310 	    (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
1311 		goto dma_desc_error;
1312 
1313 	if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
1314 		goto dma_desc_error;
1315 
1316 	if (rx_dma_desc) {
1317 		rx_dma_desc->callback = spi->cfg->dma_rx_cb;
1318 		rx_dma_desc->callback_param = spi;
1319 
1320 		if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
1321 			dev_err(spi->dev, "Rx DMA submit failed\n");
1322 			goto dma_desc_error;
1323 		}
1324 		/* Enable Rx DMA channel */
1325 		dma_async_issue_pending(spi->dma_rx);
1326 	}
1327 
1328 	if (tx_dma_desc) {
1329 		if (spi->cur_comm == SPI_SIMPLEX_TX ||
1330 		    spi->cur_comm == SPI_3WIRE_TX) {
1331 			tx_dma_desc->callback = spi->cfg->dma_tx_cb;
1332 			tx_dma_desc->callback_param = spi;
1333 		}
1334 
1335 		if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
1336 			dev_err(spi->dev, "Tx DMA submit failed\n");
1337 			goto dma_submit_error;
1338 		}
1339 		/* Enable Tx DMA channel */
1340 		dma_async_issue_pending(spi->dma_tx);
1341 
1342 		/* Enable Tx DMA request */
1343 		stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg,
1344 				   spi->cfg->regs->dma_tx_en.mask);
1345 	}
1346 
1347 	spi->cfg->transfer_one_dma_start(spi);
1348 
1349 	spin_unlock_irqrestore(&spi->lock, flags);
1350 
1351 	return 1;
1352 
1353 dma_submit_error:
1354 	if (spi->dma_rx)
1355 		dmaengine_terminate_all(spi->dma_rx);
1356 
1357 dma_desc_error:
1358 	stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1359 			   spi->cfg->regs->dma_rx_en.mask);
1360 
1361 	spin_unlock_irqrestore(&spi->lock, flags);
1362 
1363 	dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
1364 
1365 	spi->cur_usedma = false;
1366 	return spi->cfg->transfer_one_irq(spi);
1367 }
1368 
1369 /**
1370  * stm32f4_spi_set_bpw - Configure bits per word
1371  * @spi: pointer to the spi controller data structure
1372  */
1373 static void stm32f4_spi_set_bpw(struct stm32_spi *spi)
1374 {
1375 	if (spi->cur_bpw == 16)
1376 		stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1377 	else
1378 		stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1379 }
1380 
1381 /**
1382  * stm32h7_spi_set_bpw - configure bits per word
1383  * @spi: pointer to the spi controller data structure
1384  */
1385 static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
1386 {
1387 	u32 bpw, fthlv;
1388 	u32 cfg1_clrb = 0, cfg1_setb = 0;
1389 
1390 	bpw = spi->cur_bpw - 1;
1391 
1392 	cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE;
1393 	cfg1_setb |= (bpw << STM32H7_SPI_CFG1_DSIZE_SHIFT) &
1394 		     STM32H7_SPI_CFG1_DSIZE;
1395 
1396 	spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi);
1397 	fthlv = spi->cur_fthlv - 1;
1398 
1399 	cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
1400 	cfg1_setb |= (fthlv << STM32H7_SPI_CFG1_FTHLV_SHIFT) &
1401 		     STM32H7_SPI_CFG1_FTHLV;
1402 
1403 	writel_relaxed(
1404 		(readl_relaxed(spi->base + STM32H7_SPI_CFG1) &
1405 		 ~cfg1_clrb) | cfg1_setb,
1406 		spi->base + STM32H7_SPI_CFG1);
1407 }
1408 
1409 /**
1410  * stm32_spi_set_mbr - Configure baud rate divisor in master mode
1411  * @spi: pointer to the spi controller data structure
1412  * @mbrdiv: baud rate divisor value
1413  */
1414 static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv)
1415 {
1416 	u32 clrb = 0, setb = 0;
1417 
1418 	clrb |= spi->cfg->regs->br.mask;
1419 	setb |= ((u32)mbrdiv << spi->cfg->regs->br.shift) &
1420 		spi->cfg->regs->br.mask;
1421 
1422 	writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) &
1423 			~clrb) | setb,
1424 		       spi->base + spi->cfg->regs->br.reg);
1425 }
1426 
1427 /**
1428  * stm32_spi_communication_type - return transfer communication type
1429  * @spi_dev: pointer to the spi device
1430  * @transfer: pointer to spi transfer
1431  */
1432 static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev,
1433 						 struct spi_transfer *transfer)
1434 {
1435 	unsigned int type = SPI_FULL_DUPLEX;
1436 
1437 	if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
1438 		/*
1439 		 * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
1440 		 * is forbidden and unvalidated by SPI subsystem so depending
1441 		 * on the valid buffer, we can determine the direction of the
1442 		 * transfer.
1443 		 */
1444 		if (!transfer->tx_buf)
1445 			type = SPI_3WIRE_RX;
1446 		else
1447 			type = SPI_3WIRE_TX;
1448 	} else {
1449 		if (!transfer->tx_buf)
1450 			type = SPI_SIMPLEX_RX;
1451 		else if (!transfer->rx_buf)
1452 			type = SPI_SIMPLEX_TX;
1453 	}
1454 
1455 	return type;
1456 }
1457 
1458 /**
1459  * stm32f4_spi_set_mode - configure communication mode
1460  * @spi: pointer to the spi controller data structure
1461  * @comm_type: type of communication to configure
1462  */
1463 static int stm32f4_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1464 {
1465 	if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
1466 		stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1467 					STM32F4_SPI_CR1_BIDIMODE |
1468 					STM32F4_SPI_CR1_BIDIOE);
1469 	} else if (comm_type == SPI_FULL_DUPLEX ||
1470 				comm_type == SPI_SIMPLEX_RX) {
1471 		stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1472 					STM32F4_SPI_CR1_BIDIMODE |
1473 					STM32F4_SPI_CR1_BIDIOE);
1474 	} else if (comm_type == SPI_3WIRE_RX) {
1475 		stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1476 					STM32F4_SPI_CR1_BIDIMODE);
1477 		stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1478 					STM32F4_SPI_CR1_BIDIOE);
1479 	} else {
1480 		return -EINVAL;
1481 	}
1482 
1483 	return 0;
1484 }
1485 
1486 /**
1487  * stm32h7_spi_set_mode - configure communication mode
1488  * @spi: pointer to the spi controller data structure
1489  * @comm_type: type of communication to configure
1490  */
1491 static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1492 {
1493 	u32 mode;
1494 	u32 cfg2_clrb = 0, cfg2_setb = 0;
1495 
1496 	if (comm_type == SPI_3WIRE_RX) {
1497 		mode = STM32H7_SPI_HALF_DUPLEX;
1498 		stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1499 	} else if (comm_type == SPI_3WIRE_TX) {
1500 		mode = STM32H7_SPI_HALF_DUPLEX;
1501 		stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1502 	} else if (comm_type == SPI_SIMPLEX_RX) {
1503 		mode = STM32H7_SPI_SIMPLEX_RX;
1504 	} else if (comm_type == SPI_SIMPLEX_TX) {
1505 		mode = STM32H7_SPI_SIMPLEX_TX;
1506 	} else {
1507 		mode = STM32H7_SPI_FULL_DUPLEX;
1508 	}
1509 
1510 	cfg2_clrb |= STM32H7_SPI_CFG2_COMM;
1511 	cfg2_setb |= (mode << STM32H7_SPI_CFG2_COMM_SHIFT) &
1512 		     STM32H7_SPI_CFG2_COMM;
1513 
1514 	writel_relaxed(
1515 		(readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1516 		 ~cfg2_clrb) | cfg2_setb,
1517 		spi->base + STM32H7_SPI_CFG2);
1518 
1519 	return 0;
1520 }
1521 
1522 /**
1523  * stm32h7_spi_data_idleness - configure minimum time delay inserted between two
1524  *			       consecutive data frames in master mode
1525  * @spi: pointer to the spi controller data structure
1526  * @len: transfer len
1527  */
1528 static void stm32h7_spi_data_idleness(struct stm32_spi *spi, u32 len)
1529 {
1530 	u32 cfg2_clrb = 0, cfg2_setb = 0;
1531 
1532 	cfg2_clrb |= STM32H7_SPI_CFG2_MIDI;
1533 	if ((len > 1) && (spi->cur_midi > 0)) {
1534 		u32 sck_period_ns = DIV_ROUND_UP(SPI_1HZ_NS, spi->cur_speed);
1535 		u32 midi = min((u32)DIV_ROUND_UP(spi->cur_midi, sck_period_ns),
1536 			       (u32)STM32H7_SPI_CFG2_MIDI >>
1537 			       STM32H7_SPI_CFG2_MIDI_SHIFT);
1538 
1539 		dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
1540 			sck_period_ns, midi, midi * sck_period_ns);
1541 		cfg2_setb |= (midi << STM32H7_SPI_CFG2_MIDI_SHIFT) &
1542 			     STM32H7_SPI_CFG2_MIDI;
1543 	}
1544 
1545 	writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1546 			~cfg2_clrb) | cfg2_setb,
1547 		       spi->base + STM32H7_SPI_CFG2);
1548 }
1549 
1550 /**
1551  * stm32h7_spi_number_of_data - configure number of data at current transfer
1552  * @spi: pointer to the spi controller data structure
1553  * @nb_words: transfer length (in words)
1554  */
1555 static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
1556 {
1557 	u32 cr2_clrb = 0, cr2_setb = 0;
1558 
1559 	if (nb_words <= (STM32H7_SPI_CR2_TSIZE >>
1560 			 STM32H7_SPI_CR2_TSIZE_SHIFT)) {
1561 		cr2_clrb |= STM32H7_SPI_CR2_TSIZE;
1562 		cr2_setb = nb_words << STM32H7_SPI_CR2_TSIZE_SHIFT;
1563 		writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CR2) &
1564 				~cr2_clrb) | cr2_setb,
1565 			       spi->base + STM32H7_SPI_CR2);
1566 	} else {
1567 		return -EMSGSIZE;
1568 	}
1569 
1570 	return 0;
1571 }
1572 
1573 /**
1574  * stm32_spi_transfer_one_setup - common setup to transfer a single
1575  *				  spi_transfer either using DMA or
1576  *				  interrupts.
1577  * @spi: pointer to the spi controller data structure
1578  * @spi_dev: pointer to the spi device
1579  * @transfer: pointer to spi transfer
1580  */
1581 static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
1582 					struct spi_device *spi_dev,
1583 					struct spi_transfer *transfer)
1584 {
1585 	unsigned long flags;
1586 	unsigned int comm_type;
1587 	int nb_words, ret = 0;
1588 
1589 	spin_lock_irqsave(&spi->lock, flags);
1590 
1591 	if (spi->cur_bpw != transfer->bits_per_word) {
1592 		spi->cur_bpw = transfer->bits_per_word;
1593 		spi->cfg->set_bpw(spi);
1594 	}
1595 
1596 	if (spi->cur_speed != transfer->speed_hz) {
1597 		int mbr;
1598 
1599 		/* Update spi->cur_speed with real clock speed */
1600 		mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz,
1601 					    spi->cfg->baud_rate_div_min,
1602 					    spi->cfg->baud_rate_div_max);
1603 		if (mbr < 0) {
1604 			ret = mbr;
1605 			goto out;
1606 		}
1607 
1608 		transfer->speed_hz = spi->cur_speed;
1609 		stm32_spi_set_mbr(spi, mbr);
1610 	}
1611 
1612 	comm_type = stm32_spi_communication_type(spi_dev, transfer);
1613 	if (spi->cur_comm != comm_type) {
1614 		ret = spi->cfg->set_mode(spi, comm_type);
1615 
1616 		if (ret < 0)
1617 			goto out;
1618 
1619 		spi->cur_comm = comm_type;
1620 	}
1621 
1622 	if (spi->cfg->set_data_idleness)
1623 		spi->cfg->set_data_idleness(spi, transfer->len);
1624 
1625 	if (spi->cur_bpw <= 8)
1626 		nb_words = transfer->len;
1627 	else if (spi->cur_bpw <= 16)
1628 		nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
1629 	else
1630 		nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
1631 
1632 	if (spi->cfg->set_number_of_data) {
1633 		ret = spi->cfg->set_number_of_data(spi, nb_words);
1634 		if (ret < 0)
1635 			goto out;
1636 	}
1637 
1638 	spi->cur_xferlen = transfer->len;
1639 
1640 	dev_dbg(spi->dev, "transfer communication mode set to %d\n",
1641 		spi->cur_comm);
1642 	dev_dbg(spi->dev,
1643 		"data frame of %d-bit, data packet of %d data frames\n",
1644 		spi->cur_bpw, spi->cur_fthlv);
1645 	dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
1646 	dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
1647 		spi->cur_xferlen, nb_words);
1648 	dev_dbg(spi->dev, "dma %s\n",
1649 		(spi->cur_usedma) ? "enabled" : "disabled");
1650 
1651 out:
1652 	spin_unlock_irqrestore(&spi->lock, flags);
1653 
1654 	return ret;
1655 }
1656 
1657 /**
1658  * stm32_spi_transfer_one - transfer a single spi_transfer
1659  * @master: controller master interface
1660  * @spi_dev: pointer to the spi device
1661  * @transfer: pointer to spi transfer
1662  *
1663  * It must return 0 if the transfer is finished or 1 if the transfer is still
1664  * in progress.
1665  */
1666 static int stm32_spi_transfer_one(struct spi_master *master,
1667 				  struct spi_device *spi_dev,
1668 				  struct spi_transfer *transfer)
1669 {
1670 	struct stm32_spi *spi = spi_master_get_devdata(master);
1671 	int ret;
1672 
1673 	spi->tx_buf = transfer->tx_buf;
1674 	spi->rx_buf = transfer->rx_buf;
1675 	spi->tx_len = spi->tx_buf ? transfer->len : 0;
1676 	spi->rx_len = spi->rx_buf ? transfer->len : 0;
1677 
1678 	spi->cur_usedma = (master->can_dma &&
1679 			   master->can_dma(master, spi_dev, transfer));
1680 
1681 	ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
1682 	if (ret) {
1683 		dev_err(spi->dev, "SPI transfer setup failed\n");
1684 		return ret;
1685 	}
1686 
1687 	if (spi->cur_usedma)
1688 		return stm32_spi_transfer_one_dma(spi, transfer);
1689 	else
1690 		return spi->cfg->transfer_one_irq(spi);
1691 }
1692 
1693 /**
1694  * stm32_spi_unprepare_msg - relax the hardware
1695  * @master: controller master interface
1696  * @msg: pointer to the spi message
1697  */
1698 static int stm32_spi_unprepare_msg(struct spi_master *master,
1699 				   struct spi_message *msg)
1700 {
1701 	struct stm32_spi *spi = spi_master_get_devdata(master);
1702 
1703 	spi->cfg->disable(spi);
1704 
1705 	return 0;
1706 }
1707 
1708 /**
1709  * stm32f4_spi_config - Configure SPI controller as SPI master
1710  * @spi: pointer to the spi controller data structure
1711  */
1712 static int stm32f4_spi_config(struct stm32_spi *spi)
1713 {
1714 	unsigned long flags;
1715 
1716 	spin_lock_irqsave(&spi->lock, flags);
1717 
1718 	/* Ensure I2SMOD bit is kept cleared */
1719 	stm32_spi_clr_bits(spi, STM32F4_SPI_I2SCFGR,
1720 			   STM32F4_SPI_I2SCFGR_I2SMOD);
1721 
1722 	/*
1723 	 * - SS input value high
1724 	 * - transmitter half duplex direction
1725 	 * - Set the master mode (default Motorola mode)
1726 	 * - Consider 1 master/n slaves configuration and
1727 	 *   SS input value is determined by the SSI bit
1728 	 */
1729 	stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SSI |
1730 						 STM32F4_SPI_CR1_BIDIOE |
1731 						 STM32F4_SPI_CR1_MSTR |
1732 						 STM32F4_SPI_CR1_SSM);
1733 
1734 	spin_unlock_irqrestore(&spi->lock, flags);
1735 
1736 	return 0;
1737 }
1738 
1739 /**
1740  * stm32h7_spi_config - Configure SPI controller as SPI master
1741  * @spi: pointer to the spi controller data structure
1742  */
1743 static int stm32h7_spi_config(struct stm32_spi *spi)
1744 {
1745 	unsigned long flags;
1746 
1747 	spin_lock_irqsave(&spi->lock, flags);
1748 
1749 	/* Ensure I2SMOD bit is kept cleared */
1750 	stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
1751 			   STM32H7_SPI_I2SCFGR_I2SMOD);
1752 
1753 	/*
1754 	 * - SS input value high
1755 	 * - transmitter half duplex direction
1756 	 * - automatic communication suspend when RX-Fifo is full
1757 	 */
1758 	stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SSI |
1759 						 STM32H7_SPI_CR1_HDDIR |
1760 						 STM32H7_SPI_CR1_MASRX);
1761 
1762 	/*
1763 	 * - Set the master mode (default Motorola mode)
1764 	 * - Consider 1 master/n slaves configuration and
1765 	 *   SS input value is determined by the SSI bit
1766 	 * - keep control of all associated GPIOs
1767 	 */
1768 	stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_MASTER |
1769 						  STM32H7_SPI_CFG2_SSM |
1770 						  STM32H7_SPI_CFG2_AFCNTR);
1771 
1772 	spin_unlock_irqrestore(&spi->lock, flags);
1773 
1774 	return 0;
1775 }
1776 
1777 static const struct stm32_spi_cfg stm32f4_spi_cfg = {
1778 	.regs = &stm32f4_spi_regspec,
1779 	.get_bpw_mask = stm32f4_spi_get_bpw_mask,
1780 	.disable = stm32f4_spi_disable,
1781 	.config = stm32f4_spi_config,
1782 	.set_bpw = stm32f4_spi_set_bpw,
1783 	.set_mode = stm32f4_spi_set_mode,
1784 	.transfer_one_dma_start = stm32f4_spi_transfer_one_dma_start,
1785 	.dma_tx_cb = stm32f4_spi_dma_tx_cb,
1786 	.dma_rx_cb = stm32f4_spi_dma_rx_cb,
1787 	.transfer_one_irq = stm32f4_spi_transfer_one_irq,
1788 	.irq_handler_event = stm32f4_spi_irq_event,
1789 	.irq_handler_thread = stm32f4_spi_irq_thread,
1790 	.baud_rate_div_min = STM32F4_SPI_BR_DIV_MIN,
1791 	.baud_rate_div_max = STM32F4_SPI_BR_DIV_MAX,
1792 	.has_fifo = false,
1793 };
1794 
1795 static const struct stm32_spi_cfg stm32h7_spi_cfg = {
1796 	.regs = &stm32h7_spi_regspec,
1797 	.get_fifo_size = stm32h7_spi_get_fifo_size,
1798 	.get_bpw_mask = stm32h7_spi_get_bpw_mask,
1799 	.disable = stm32h7_spi_disable,
1800 	.config = stm32h7_spi_config,
1801 	.set_bpw = stm32h7_spi_set_bpw,
1802 	.set_mode = stm32h7_spi_set_mode,
1803 	.set_data_idleness = stm32h7_spi_data_idleness,
1804 	.set_number_of_data = stm32h7_spi_number_of_data,
1805 	.transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
1806 	.dma_rx_cb = stm32h7_spi_dma_cb,
1807 	.dma_tx_cb = stm32h7_spi_dma_cb,
1808 	.transfer_one_irq = stm32h7_spi_transfer_one_irq,
1809 	.irq_handler_thread = stm32h7_spi_irq_thread,
1810 	.baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
1811 	.baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
1812 	.has_fifo = true,
1813 };
1814 
1815 static const struct of_device_id stm32_spi_of_match[] = {
1816 	{ .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg },
1817 	{ .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg },
1818 	{},
1819 };
1820 MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
1821 
1822 static int stm32_spi_probe(struct platform_device *pdev)
1823 {
1824 	struct spi_master *master;
1825 	struct stm32_spi *spi;
1826 	struct resource *res;
1827 	int ret;
1828 
1829 	master = spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi));
1830 	if (!master) {
1831 		dev_err(&pdev->dev, "spi master allocation failed\n");
1832 		return -ENOMEM;
1833 	}
1834 	platform_set_drvdata(pdev, master);
1835 
1836 	spi = spi_master_get_devdata(master);
1837 	spi->dev = &pdev->dev;
1838 	spi->master = master;
1839 	spin_lock_init(&spi->lock);
1840 
1841 	spi->cfg = (const struct stm32_spi_cfg *)
1842 		of_match_device(pdev->dev.driver->of_match_table,
1843 				&pdev->dev)->data;
1844 
1845 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1846 	spi->base = devm_ioremap_resource(&pdev->dev, res);
1847 	if (IS_ERR(spi->base)) {
1848 		ret = PTR_ERR(spi->base);
1849 		goto err_master_put;
1850 	}
1851 
1852 	spi->phys_addr = (dma_addr_t)res->start;
1853 
1854 	spi->irq = platform_get_irq(pdev, 0);
1855 	if (spi->irq <= 0) {
1856 		ret = spi->irq;
1857 		if (ret != -EPROBE_DEFER)
1858 			dev_err(&pdev->dev, "failed to get irq: %d\n", ret);
1859 		goto err_master_put;
1860 	}
1861 	ret = devm_request_threaded_irq(&pdev->dev, spi->irq,
1862 					spi->cfg->irq_handler_event,
1863 					spi->cfg->irq_handler_thread,
1864 					IRQF_ONESHOT, pdev->name, master);
1865 	if (ret) {
1866 		dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
1867 			ret);
1868 		goto err_master_put;
1869 	}
1870 
1871 	spi->clk = devm_clk_get(&pdev->dev, NULL);
1872 	if (IS_ERR(spi->clk)) {
1873 		ret = PTR_ERR(spi->clk);
1874 		dev_err(&pdev->dev, "clk get failed: %d\n", ret);
1875 		goto err_master_put;
1876 	}
1877 
1878 	ret = clk_prepare_enable(spi->clk);
1879 	if (ret) {
1880 		dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
1881 		goto err_master_put;
1882 	}
1883 	spi->clk_rate = clk_get_rate(spi->clk);
1884 	if (!spi->clk_rate) {
1885 		dev_err(&pdev->dev, "clk rate = 0\n");
1886 		ret = -EINVAL;
1887 		goto err_clk_disable;
1888 	}
1889 
1890 	spi->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
1891 	if (!IS_ERR(spi->rst)) {
1892 		reset_control_assert(spi->rst);
1893 		udelay(2);
1894 		reset_control_deassert(spi->rst);
1895 	}
1896 
1897 	if (spi->cfg->has_fifo)
1898 		spi->fifo_size = spi->cfg->get_fifo_size(spi);
1899 
1900 	ret = spi->cfg->config(spi);
1901 	if (ret) {
1902 		dev_err(&pdev->dev, "controller configuration failed: %d\n",
1903 			ret);
1904 		goto err_clk_disable;
1905 	}
1906 
1907 	master->dev.of_node = pdev->dev.of_node;
1908 	master->auto_runtime_pm = true;
1909 	master->bus_num = pdev->id;
1910 	master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
1911 			    SPI_3WIRE;
1912 	master->bits_per_word_mask = spi->cfg->get_bpw_mask(spi);
1913 	master->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min;
1914 	master->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max;
1915 	master->use_gpio_descriptors = true;
1916 	master->prepare_message = stm32_spi_prepare_msg;
1917 	master->transfer_one = stm32_spi_transfer_one;
1918 	master->unprepare_message = stm32_spi_unprepare_msg;
1919 	master->flags = SPI_MASTER_MUST_TX;
1920 
1921 	spi->dma_tx = dma_request_chan(spi->dev, "tx");
1922 	if (IS_ERR(spi->dma_tx)) {
1923 		ret = PTR_ERR(spi->dma_tx);
1924 		spi->dma_tx = NULL;
1925 		if (ret == -EPROBE_DEFER)
1926 			goto err_clk_disable;
1927 
1928 		dev_warn(&pdev->dev, "failed to request tx dma channel\n");
1929 	} else {
1930 		master->dma_tx = spi->dma_tx;
1931 	}
1932 
1933 	spi->dma_rx = dma_request_chan(spi->dev, "rx");
1934 	if (IS_ERR(spi->dma_rx)) {
1935 		ret = PTR_ERR(spi->dma_rx);
1936 		spi->dma_rx = NULL;
1937 		if (ret == -EPROBE_DEFER)
1938 			goto err_dma_release;
1939 
1940 		dev_warn(&pdev->dev, "failed to request rx dma channel\n");
1941 	} else {
1942 		master->dma_rx = spi->dma_rx;
1943 	}
1944 
1945 	if (spi->dma_tx || spi->dma_rx)
1946 		master->can_dma = stm32_spi_can_dma;
1947 
1948 	pm_runtime_set_active(&pdev->dev);
1949 	pm_runtime_enable(&pdev->dev);
1950 
1951 	ret = devm_spi_register_master(&pdev->dev, master);
1952 	if (ret) {
1953 		dev_err(&pdev->dev, "spi master registration failed: %d\n",
1954 			ret);
1955 		goto err_pm_disable;
1956 	}
1957 
1958 	if (!master->cs_gpiods) {
1959 		dev_err(&pdev->dev, "no CS gpios available\n");
1960 		ret = -EINVAL;
1961 		goto err_pm_disable;
1962 	}
1963 
1964 	dev_info(&pdev->dev, "driver initialized\n");
1965 
1966 	return 0;
1967 
1968 err_pm_disable:
1969 	pm_runtime_disable(&pdev->dev);
1970 err_dma_release:
1971 	if (spi->dma_tx)
1972 		dma_release_channel(spi->dma_tx);
1973 	if (spi->dma_rx)
1974 		dma_release_channel(spi->dma_rx);
1975 err_clk_disable:
1976 	clk_disable_unprepare(spi->clk);
1977 err_master_put:
1978 	spi_master_put(master);
1979 
1980 	return ret;
1981 }
1982 
1983 static int stm32_spi_remove(struct platform_device *pdev)
1984 {
1985 	struct spi_master *master = platform_get_drvdata(pdev);
1986 	struct stm32_spi *spi = spi_master_get_devdata(master);
1987 
1988 	spi->cfg->disable(spi);
1989 
1990 	if (master->dma_tx)
1991 		dma_release_channel(master->dma_tx);
1992 	if (master->dma_rx)
1993 		dma_release_channel(master->dma_rx);
1994 
1995 	clk_disable_unprepare(spi->clk);
1996 
1997 	pm_runtime_disable(&pdev->dev);
1998 
1999 	return 0;
2000 }
2001 
2002 #ifdef CONFIG_PM
2003 static int stm32_spi_runtime_suspend(struct device *dev)
2004 {
2005 	struct spi_master *master = dev_get_drvdata(dev);
2006 	struct stm32_spi *spi = spi_master_get_devdata(master);
2007 
2008 	clk_disable_unprepare(spi->clk);
2009 
2010 	return 0;
2011 }
2012 
2013 static int stm32_spi_runtime_resume(struct device *dev)
2014 {
2015 	struct spi_master *master = dev_get_drvdata(dev);
2016 	struct stm32_spi *spi = spi_master_get_devdata(master);
2017 
2018 	return clk_prepare_enable(spi->clk);
2019 }
2020 #endif
2021 
2022 #ifdef CONFIG_PM_SLEEP
2023 static int stm32_spi_suspend(struct device *dev)
2024 {
2025 	struct spi_master *master = dev_get_drvdata(dev);
2026 	int ret;
2027 
2028 	ret = spi_master_suspend(master);
2029 	if (ret)
2030 		return ret;
2031 
2032 	return pm_runtime_force_suspend(dev);
2033 }
2034 
2035 static int stm32_spi_resume(struct device *dev)
2036 {
2037 	struct spi_master *master = dev_get_drvdata(dev);
2038 	struct stm32_spi *spi = spi_master_get_devdata(master);
2039 	int ret;
2040 
2041 	ret = pm_runtime_force_resume(dev);
2042 	if (ret)
2043 		return ret;
2044 
2045 	ret = spi_master_resume(master);
2046 	if (ret)
2047 		clk_disable_unprepare(spi->clk);
2048 
2049 	return ret;
2050 }
2051 #endif
2052 
2053 static const struct dev_pm_ops stm32_spi_pm_ops = {
2054 	SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
2055 	SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend,
2056 			   stm32_spi_runtime_resume, NULL)
2057 };
2058 
2059 static struct platform_driver stm32_spi_driver = {
2060 	.probe = stm32_spi_probe,
2061 	.remove = stm32_spi_remove,
2062 	.driver = {
2063 		.name = DRIVER_NAME,
2064 		.pm = &stm32_spi_pm_ops,
2065 		.of_match_table = stm32_spi_of_match,
2066 	},
2067 };
2068 
2069 module_platform_driver(stm32_spi_driver);
2070 
2071 MODULE_ALIAS("platform:" DRIVER_NAME);
2072 MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
2073 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
2074 MODULE_LICENSE("GPL v2");
2075