xref: /linux/drivers/spi/spi-stm32.c (revision 2f9339c052bdbafaa8b02188d069fbb0e8df1f26)
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 
stm32_spi_set_bits(struct stm32_spi * spi,u32 offset,u32 bits)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 
stm32_spi_clr_bits(struct stm32_spi * spi,u32 offset,u32 bits)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  */
stm32h7_spi_get_fifo_size(struct stm32_spi * spi)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  */
stm32f4_spi_get_bpw_mask(struct stm32_spi * spi)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  */
stm32f7_spi_get_bpw_mask(struct stm32_spi * spi)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  */
stm32h7_spi_get_bpw_mask(struct stm32_spi * spi)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  */
stm32mp25_spi_get_bpw_mask(struct stm32_spi * spi)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  */
stm32_spi_prepare_mbr(struct stm32_spi * spi,u32 speed_hz,u32 min_div,u32 max_div)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  */
stm32h7_spi_prepare_fthlv(struct stm32_spi * spi,u32 xfer_len)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  */
stm32f4_spi_write_tx(struct stm32_spi * spi)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  */
stm32f7_spi_write_tx(struct stm32_spi * spi)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  */
stm32h7_spi_write_txfifo(struct stm32_spi * spi)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  */
stm32f4_spi_read_rx(struct stm32_spi * spi)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  */
stm32f7_spi_read_rx(struct stm32_spi * spi)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  */
stm32h7_spi_read_rxfifo(struct stm32_spi * spi)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  */
stm32_spi_enable(struct stm32_spi * spi)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  */
stm32fx_spi_disable(struct stm32_spi * spi)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  */
stm32h7_spi_disable(struct stm32_spi * spi)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  */
stm32_spi_can_dma(struct spi_controller * ctrl,struct spi_device * spi_dev,struct spi_transfer * transfer)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  */
stm32fx_spi_irq_event(int irq,void * dev_id)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  */
stm32fx_spi_irq_thread(int irq,void * dev_id)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  */
stm32h7_spi_irq_thread(int irq,void * dev_id)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 
stm32_spi_optimize_message(struct spi_message * msg)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  */
stm32_spi_prepare_msg(struct spi_controller * ctrl,struct spi_message * msg)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  */
stm32fx_spi_dma_tx_cb(void * data)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  */
stm32_spi_dma_rx_cb(void * data)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  */
stm32_spi_dma_config(struct stm32_spi * spi,struct dma_chan * dma_chan,struct dma_slave_config * dma_conf,enum dma_transfer_direction dir)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  */
stm32fx_spi_transfer_one_irq(struct stm32_spi * spi)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  */
stm32h7_spi_transfer_one_poll(struct stm32_spi * spi)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  */
stm32h7_spi_transfer_one_irq(struct stm32_spi * spi)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  */
stm32fx_spi_transfer_one_dma_start(struct stm32_spi * spi)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  */
stm32f7_spi_transfer_one_dma_start(struct stm32_spi * spi)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  */
stm32h7_spi_transfer_one_dma_start(struct stm32_spi * spi)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  */
stm32_spi_prepare_rx_dma_mdma_chaining(struct stm32_spi * spi,struct spi_transfer * xfer,struct dma_slave_config * rx_dma_conf,struct dma_async_tx_descriptor ** rx_dma_desc,struct dma_async_tx_descriptor ** rx_mdma_desc)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 	*rx_mdma_desc = _mdma_desc;
1629 	*rx_dma_desc = _dma_desc;
1630 
1631 	return 0;
1632 }
1633 
1634 /**
1635  * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
1636  * @spi: pointer to the spi controller data structure
1637  * @xfer: pointer to the spi_transfer structure
1638  *
1639  * It must returns 0 if the transfer is finished or 1 if the transfer is still
1640  * in progress.
1641  */
stm32_spi_transfer_one_dma(struct stm32_spi * spi,struct spi_transfer * xfer)1642 static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
1643 				      struct spi_transfer *xfer)
1644 {
1645 	struct dma_async_tx_descriptor *rx_mdma_desc = NULL, *rx_dma_desc = NULL;
1646 	struct dma_async_tx_descriptor *tx_dma_desc = NULL;
1647 	struct dma_slave_config tx_dma_conf, rx_dma_conf;
1648 	unsigned long flags;
1649 	int ret = 0;
1650 
1651 	spin_lock_irqsave(&spi->lock, flags);
1652 
1653 	if (spi->rx_buf && spi->dma_rx) {
1654 		stm32_spi_dma_config(spi, spi->dma_rx, &rx_dma_conf, DMA_DEV_TO_MEM);
1655 		if (spi->mdma_rx) {
1656 			rx_dma_conf.peripheral_size = 1;
1657 			dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1658 
1659 			ret = stm32_spi_prepare_rx_dma_mdma_chaining(spi, xfer, &rx_dma_conf,
1660 								     &rx_dma_desc, &rx_mdma_desc);
1661 			if (ret) { /* RX DMA MDMA chaining not possible, fallback to DMA only */
1662 				rx_dma_conf.peripheral_config = 0;
1663 				rx_dma_desc = NULL;
1664 			}
1665 		}
1666 		if (!rx_dma_desc) {
1667 			dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1668 			rx_dma_desc = dmaengine_prep_slave_sg(spi->dma_rx, xfer->rx_sg.sgl,
1669 							      xfer->rx_sg.nents,
1670 							      rx_dma_conf.direction,
1671 							      DMA_PREP_INTERRUPT);
1672 		}
1673 	}
1674 
1675 	if (spi->tx_buf && spi->dma_tx) {
1676 		stm32_spi_dma_config(spi, spi->dma_tx, &tx_dma_conf, DMA_MEM_TO_DEV);
1677 		dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
1678 		tx_dma_desc = dmaengine_prep_slave_sg(spi->dma_tx, xfer->tx_sg.sgl,
1679 						      xfer->tx_sg.nents,
1680 						      tx_dma_conf.direction,
1681 						      DMA_PREP_INTERRUPT);
1682 	}
1683 
1684 	if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
1685 	    (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
1686 		goto dma_desc_error;
1687 
1688 	if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
1689 		goto dma_desc_error;
1690 
1691 	if (rx_dma_desc) {
1692 		if (rx_mdma_desc) {
1693 			rx_mdma_desc->callback = spi->cfg->dma_rx_cb;
1694 			rx_mdma_desc->callback_param = spi;
1695 		} else {
1696 			rx_dma_desc->callback = spi->cfg->dma_rx_cb;
1697 			rx_dma_desc->callback_param = spi;
1698 		}
1699 
1700 		/* Enable Rx DMA request */
1701 		stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1702 				   spi->cfg->regs->dma_rx_en.mask);
1703 		if (rx_mdma_desc) {
1704 			if (dma_submit_error(dmaengine_submit(rx_mdma_desc))) {
1705 				dev_err(spi->dev, "Rx MDMA submit failed\n");
1706 				goto dma_desc_error;
1707 			}
1708 			/* Enable Rx MDMA channel */
1709 			dma_async_issue_pending(spi->mdma_rx);
1710 		}
1711 		if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
1712 			dev_err(spi->dev, "Rx DMA submit failed\n");
1713 			goto dma_desc_error;
1714 		}
1715 		/* Enable Rx DMA channel */
1716 		dma_async_issue_pending(spi->dma_rx);
1717 	}
1718 
1719 	if (tx_dma_desc) {
1720 		if (spi->cur_comm == SPI_SIMPLEX_TX ||
1721 		    spi->cur_comm == SPI_3WIRE_TX) {
1722 			tx_dma_desc->callback = spi->cfg->dma_tx_cb;
1723 			tx_dma_desc->callback_param = spi;
1724 		}
1725 
1726 		if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
1727 			dev_err(spi->dev, "Tx DMA submit failed\n");
1728 			goto dma_submit_error;
1729 		}
1730 		/* Enable Tx DMA channel */
1731 		dma_async_issue_pending(spi->dma_tx);
1732 
1733 		/* Enable Tx DMA request */
1734 		stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg,
1735 				   spi->cfg->regs->dma_tx_en.mask);
1736 	}
1737 
1738 	spi->cfg->transfer_one_dma_start(spi);
1739 
1740 	spin_unlock_irqrestore(&spi->lock, flags);
1741 
1742 	return 1;
1743 
1744 dma_submit_error:
1745 	if (spi->mdma_rx)
1746 		dmaengine_terminate_sync(spi->mdma_rx);
1747 	if (spi->dma_rx)
1748 		dmaengine_terminate_sync(spi->dma_rx);
1749 
1750 dma_desc_error:
1751 	stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1752 			   spi->cfg->regs->dma_rx_en.mask);
1753 
1754 	spin_unlock_irqrestore(&spi->lock, flags);
1755 
1756 	dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
1757 
1758 	if (spi->sram_rx_buf)
1759 		memset(spi->sram_rx_buf, 0, spi->sram_rx_buf_size);
1760 
1761 	spi->cur_usedma = false;
1762 	return spi->cfg->transfer_one_irq(spi);
1763 }
1764 
1765 /**
1766  * stm32f4_spi_set_bpw - Configure bits per word
1767  * @spi: pointer to the spi controller data structure
1768  */
stm32f4_spi_set_bpw(struct stm32_spi * spi)1769 static void stm32f4_spi_set_bpw(struct stm32_spi *spi)
1770 {
1771 	if (spi->cur_bpw == 16)
1772 		stm32_spi_set_bits(spi, STM32FX_SPI_CR1, STM32F4_SPI_CR1_DFF);
1773 	else
1774 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR1, STM32F4_SPI_CR1_DFF);
1775 }
1776 
1777 /**
1778  * stm32f7_spi_set_bpw - Configure bits per word
1779  * @spi: pointer to the spi controller data structure
1780  */
stm32f7_spi_set_bpw(struct stm32_spi * spi)1781 static void stm32f7_spi_set_bpw(struct stm32_spi *spi)
1782 {
1783 	u32 bpw;
1784 	u32 cr2_clrb = 0, cr2_setb = 0;
1785 
1786 	bpw = spi->cur_bpw - 1;
1787 
1788 	cr2_clrb |= STM32F7_SPI_CR2_DS;
1789 	cr2_setb |= FIELD_PREP(STM32F7_SPI_CR2_DS, bpw);
1790 
1791 	if (spi->rx_len >= sizeof(u16))
1792 		cr2_clrb |= STM32F7_SPI_CR2_FRXTH;
1793 	else
1794 		cr2_setb |= STM32F7_SPI_CR2_FRXTH;
1795 
1796 	writel_relaxed(
1797 		(readl_relaxed(spi->base + STM32FX_SPI_CR2) &
1798 		 ~cr2_clrb) | cr2_setb,
1799 		spi->base + STM32FX_SPI_CR2);
1800 }
1801 
1802 /**
1803  * stm32h7_spi_set_bpw - configure bits per word
1804  * @spi: pointer to the spi controller data structure
1805  */
stm32h7_spi_set_bpw(struct stm32_spi * spi)1806 static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
1807 {
1808 	u32 bpw, fthlv;
1809 	u32 cfg1_clrb = 0, cfg1_setb = 0;
1810 
1811 	bpw = spi->cur_bpw - 1;
1812 
1813 	cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE;
1814 	cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_DSIZE, bpw);
1815 
1816 	spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen);
1817 	fthlv = spi->cur_fthlv - 1;
1818 
1819 	cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
1820 	cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_FTHLV, fthlv);
1821 
1822 	writel_relaxed(
1823 		(readl_relaxed(spi->base + STM32H7_SPI_CFG1) &
1824 		 ~cfg1_clrb) | cfg1_setb,
1825 		spi->base + STM32H7_SPI_CFG1);
1826 }
1827 
1828 /**
1829  * stm32_spi_set_mbr - Configure baud rate divisor in host mode
1830  * @spi: pointer to the spi controller data structure
1831  * @mbrdiv: baud rate divisor value
1832  */
stm32_spi_set_mbr(struct stm32_spi * spi,u32 mbrdiv)1833 static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv)
1834 {
1835 	u32 clrb = 0, setb = 0;
1836 
1837 	clrb |= spi->cfg->regs->br.mask;
1838 	setb |= (mbrdiv << spi->cfg->regs->br.shift) & spi->cfg->regs->br.mask;
1839 
1840 	writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) &
1841 			~clrb) | setb,
1842 		       spi->base + spi->cfg->regs->br.reg);
1843 }
1844 
1845 /**
1846  * stm32_spi_communication_type - return transfer communication type
1847  * @spi_dev: pointer to the spi device
1848  * @transfer: pointer to spi transfer
1849  */
stm32_spi_communication_type(struct spi_device * spi_dev,struct spi_transfer * transfer)1850 static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev,
1851 						 struct spi_transfer *transfer)
1852 {
1853 	unsigned int type = SPI_FULL_DUPLEX;
1854 
1855 	if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
1856 		/*
1857 		 * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
1858 		 * is forbidden and unvalidated by SPI subsystem so depending
1859 		 * on the valid buffer, we can determine the direction of the
1860 		 * transfer.
1861 		 */
1862 		if (!transfer->tx_buf)
1863 			type = SPI_3WIRE_RX;
1864 		else
1865 			type = SPI_3WIRE_TX;
1866 	} else {
1867 		if (!transfer->tx_buf)
1868 			type = SPI_SIMPLEX_RX;
1869 		else if (!transfer->rx_buf)
1870 			type = SPI_SIMPLEX_TX;
1871 	}
1872 
1873 	return type;
1874 }
1875 
1876 /**
1877  * stm32fx_spi_set_mode - configure communication mode
1878  * @spi: pointer to the spi controller data structure
1879  * @comm_type: type of communication to configure
1880  */
stm32fx_spi_set_mode(struct stm32_spi * spi,unsigned int comm_type)1881 static int stm32fx_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1882 {
1883 	if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
1884 		stm32_spi_set_bits(spi, STM32FX_SPI_CR1,
1885 					STM32FX_SPI_CR1_BIDIMODE |
1886 					STM32FX_SPI_CR1_BIDIOE);
1887 	} else if (comm_type == SPI_FULL_DUPLEX ||
1888 				comm_type == SPI_SIMPLEX_RX) {
1889 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR1,
1890 					STM32FX_SPI_CR1_BIDIMODE |
1891 					STM32FX_SPI_CR1_BIDIOE);
1892 	} else if (comm_type == SPI_3WIRE_RX) {
1893 		stm32_spi_set_bits(spi, STM32FX_SPI_CR1,
1894 					STM32FX_SPI_CR1_BIDIMODE);
1895 		stm32_spi_clr_bits(spi, STM32FX_SPI_CR1,
1896 					STM32FX_SPI_CR1_BIDIOE);
1897 	} else {
1898 		return -EINVAL;
1899 	}
1900 
1901 	return 0;
1902 }
1903 
1904 /**
1905  * stm32h7_spi_set_mode - configure communication mode
1906  * @spi: pointer to the spi controller data structure
1907  * @comm_type: type of communication to configure
1908  */
stm32h7_spi_set_mode(struct stm32_spi * spi,unsigned int comm_type)1909 static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1910 {
1911 	u32 mode;
1912 	u32 cfg2_clrb = 0, cfg2_setb = 0;
1913 
1914 	if (comm_type == SPI_3WIRE_RX) {
1915 		mode = STM32H7_SPI_HALF_DUPLEX;
1916 		stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1917 	} else if (comm_type == SPI_3WIRE_TX) {
1918 		mode = STM32H7_SPI_HALF_DUPLEX;
1919 		stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1920 	} else if (comm_type == SPI_SIMPLEX_RX) {
1921 		mode = STM32H7_SPI_SIMPLEX_RX;
1922 	} else if (comm_type == SPI_SIMPLEX_TX) {
1923 		mode = STM32H7_SPI_SIMPLEX_TX;
1924 	} else {
1925 		mode = STM32H7_SPI_FULL_DUPLEX;
1926 	}
1927 
1928 	cfg2_clrb |= STM32H7_SPI_CFG2_COMM;
1929 	cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_COMM, mode);
1930 
1931 	writel_relaxed(
1932 		(readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1933 		 ~cfg2_clrb) | cfg2_setb,
1934 		spi->base + STM32H7_SPI_CFG2);
1935 
1936 	return 0;
1937 }
1938 
1939 /**
1940  * stm32h7_spi_data_idleness - configure minimum time delay inserted between two
1941  *			       consecutive data frames in host mode
1942  * @spi: pointer to the spi controller data structure
1943  * @xfer: pointer to spi transfer
1944  */
stm32h7_spi_data_idleness(struct stm32_spi * spi,struct spi_transfer * xfer)1945 static void stm32h7_spi_data_idleness(struct stm32_spi *spi, struct spi_transfer *xfer)
1946 {
1947 	u32 cfg2_clrb = 0, cfg2_setb = 0;
1948 	u32 len = xfer->len;
1949 	u32 spi_delay_ns;
1950 
1951 	spi_delay_ns = spi_delay_to_ns(&xfer->word_delay, xfer);
1952 
1953 	if (spi->cur_midi != 0) {
1954 		dev_warn(spi->dev, "st,spi-midi-ns DT property is deprecated\n");
1955 		if (spi_delay_ns) {
1956 			dev_warn(spi->dev, "Overriding st,spi-midi-ns with word_delay_ns %d\n",
1957 				 spi_delay_ns);
1958 			spi->cur_midi = spi_delay_ns;
1959 		}
1960 	} else {
1961 		spi->cur_midi = spi_delay_ns;
1962 	}
1963 
1964 	cfg2_clrb |= STM32H7_SPI_CFG2_MIDI;
1965 	if ((len > 1) && (spi->cur_midi > 0)) {
1966 		u32 sck_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->cur_speed);
1967 		u32 midi = DIV_ROUND_UP(spi->cur_midi, sck_period_ns);
1968 
1969 		if ((spi->cur_bpw + midi) < 8)
1970 			midi = 8 - spi->cur_bpw;
1971 
1972 		midi = min_t(u32, midi, FIELD_MAX(STM32H7_SPI_CFG2_MIDI));
1973 
1974 		dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
1975 			sck_period_ns, midi, midi * sck_period_ns);
1976 		cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_MIDI, midi);
1977 	}
1978 
1979 	writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1980 			~cfg2_clrb) | cfg2_setb,
1981 		       spi->base + STM32H7_SPI_CFG2);
1982 }
1983 
1984 /**
1985  * stm32h7_spi_number_of_data - configure number of data at current transfer
1986  * @spi: pointer to the spi controller data structure
1987  * @nb_words: transfer length (in words)
1988  */
stm32h7_spi_number_of_data(struct stm32_spi * spi,u32 nb_words)1989 static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
1990 {
1991 	if (nb_words <= spi->t_size_max) {
1992 		writel_relaxed(FIELD_PREP(STM32H7_SPI_CR2_TSIZE, nb_words),
1993 			       spi->base + STM32H7_SPI_CR2);
1994 	} else {
1995 		return -EMSGSIZE;
1996 	}
1997 
1998 	return 0;
1999 }
2000 
2001 /**
2002  * stm32_spi_transfer_one_setup - common setup to transfer a single
2003  *				  spi_transfer either using DMA or
2004  *				  interrupts.
2005  * @spi: pointer to the spi controller data structure
2006  * @spi_dev: pointer to the spi device
2007  * @transfer: pointer to spi transfer
2008  */
stm32_spi_transfer_one_setup(struct stm32_spi * spi,struct spi_device * spi_dev,struct spi_transfer * transfer)2009 static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
2010 					struct spi_device *spi_dev,
2011 					struct spi_transfer *transfer)
2012 {
2013 	unsigned long flags;
2014 	unsigned int comm_type;
2015 	int nb_words, ret = 0;
2016 	int mbr;
2017 
2018 	spin_lock_irqsave(&spi->lock, flags);
2019 
2020 	spi->cur_xferlen = transfer->len;
2021 
2022 	spi->cur_bpw = transfer->bits_per_word;
2023 	spi->cfg->set_bpw(spi);
2024 
2025 	if (spi_dev->mode & SPI_READY && spi->cur_bpw < 8) {
2026 		writel_relaxed(readl_relaxed(spi->base + spi->cfg->regs->rdy_en.reg) &
2027 				~spi->cfg->regs->rdy_en.mask,
2028 					spi->base + spi->cfg->regs->rdy_en.reg);
2029 		dev_dbg(spi->dev, "RDY logic disabled as bits per word < 8\n");
2030 	}
2031 
2032 	/* Update spi->cur_speed with real clock speed */
2033 	if (STM32_SPI_HOST_MODE(spi)) {
2034 		mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz,
2035 					    spi->cfg->baud_rate_div_min,
2036 					    spi->cfg->baud_rate_div_max);
2037 		if (mbr < 0) {
2038 			ret = mbr;
2039 			goto out;
2040 		}
2041 
2042 		transfer->speed_hz = spi->cur_speed;
2043 		stm32_spi_set_mbr(spi, mbr);
2044 	}
2045 
2046 	comm_type = stm32_spi_communication_type(spi_dev, transfer);
2047 	ret = spi->cfg->set_mode(spi, comm_type);
2048 	if (ret < 0)
2049 		goto out;
2050 
2051 	spi->cur_comm = comm_type;
2052 
2053 	if (STM32_SPI_HOST_MODE(spi) && spi->cfg->set_data_idleness)
2054 		spi->cfg->set_data_idleness(spi, transfer);
2055 
2056 	if (spi->cur_bpw <= 8)
2057 		nb_words = transfer->len;
2058 	else if (spi->cur_bpw <= 16)
2059 		nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
2060 	else
2061 		nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
2062 
2063 	if (spi->cfg->set_number_of_data) {
2064 		ret = spi->cfg->set_number_of_data(spi, nb_words);
2065 		if (ret < 0)
2066 			goto out;
2067 	}
2068 
2069 	dev_dbg(spi->dev, "transfer communication mode set to %d\n",
2070 		spi->cur_comm);
2071 	dev_dbg(spi->dev,
2072 		"data frame of %d-bit, data packet of %d data frames\n",
2073 		spi->cur_bpw, spi->cur_fthlv);
2074 	if (STM32_SPI_HOST_MODE(spi))
2075 		dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
2076 	dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
2077 		spi->cur_xferlen, nb_words);
2078 	dev_dbg(spi->dev, "dma %s\n",
2079 		(spi->cur_usedma) ? "enabled" : "disabled");
2080 
2081 out:
2082 	spin_unlock_irqrestore(&spi->lock, flags);
2083 
2084 	return ret;
2085 }
2086 
2087 /**
2088  * stm32_spi_can_poll - detect if poll based transfer is appropriate
2089  * @spi: pointer to the spi controller data structure
2090  *
2091  * Returns true is poll is more appropriate, false otherwise.
2092  */
stm32_spi_can_poll(struct stm32_spi * spi)2093 static bool stm32_spi_can_poll(struct stm32_spi *spi)
2094 {
2095 	unsigned long hz_per_byte, byte_limit;
2096 
2097 	/* Evaluate the transfer time and use polling if applicable */
2098 	hz_per_byte = polling_limit_us ?
2099 		      DIV_ROUND_UP(8 * USEC_PER_SEC, polling_limit_us) : 0;
2100 	byte_limit = hz_per_byte ? spi->cur_speed / hz_per_byte : 1;
2101 
2102 	return (spi->cur_xferlen < byte_limit) ? true : false;
2103 }
2104 
2105 /**
2106  * stm32_spi_transfer_one - transfer a single spi_transfer
2107  * @ctrl: controller interface
2108  * @spi_dev: pointer to the spi device
2109  * @transfer: pointer to spi transfer
2110  *
2111  * It must return 0 if the transfer is finished or 1 if the transfer is still
2112  * in progress.
2113  */
stm32_spi_transfer_one(struct spi_controller * ctrl,struct spi_device * spi_dev,struct spi_transfer * transfer)2114 static int stm32_spi_transfer_one(struct spi_controller *ctrl,
2115 				  struct spi_device *spi_dev,
2116 				  struct spi_transfer *transfer)
2117 {
2118 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2119 	int ret;
2120 
2121 	spi->tx_buf = transfer->tx_buf;
2122 	spi->rx_buf = transfer->rx_buf;
2123 	spi->tx_len = spi->tx_buf ? transfer->len : 0;
2124 	spi->rx_len = spi->rx_buf ? transfer->len : 0;
2125 
2126 	spi->cur_usedma = (ctrl->can_dma &&
2127 			   ctrl->can_dma(ctrl, spi_dev, transfer));
2128 
2129 	ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
2130 	if (ret) {
2131 		dev_err(spi->dev, "SPI transfer setup failed\n");
2132 		return ret;
2133 	}
2134 
2135 	if (spi->cur_usedma)
2136 		return stm32_spi_transfer_one_dma(spi, transfer);
2137 	else if (spi->cfg->transfer_one_poll && stm32_spi_can_poll(spi))
2138 		return spi->cfg->transfer_one_poll(spi);
2139 	else
2140 		return spi->cfg->transfer_one_irq(spi);
2141 }
2142 
2143 /**
2144  * stm32_spi_unprepare_msg - relax the hardware
2145  * @ctrl: controller interface
2146  * @msg: pointer to the spi message
2147  */
stm32_spi_unprepare_msg(struct spi_controller * ctrl,struct spi_message * msg)2148 static int stm32_spi_unprepare_msg(struct spi_controller *ctrl,
2149 				   struct spi_message *msg)
2150 {
2151 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2152 
2153 	spi->cfg->disable(spi);
2154 
2155 	if (spi->sram_rx_buf)
2156 		memset(spi->sram_rx_buf, 0, spi->sram_rx_buf_size);
2157 
2158 	return 0;
2159 }
2160 
2161 /**
2162  * stm32fx_spi_config - Configure SPI controller as SPI host
2163  * @spi: pointer to the spi controller data structure
2164  */
stm32fx_spi_config(struct stm32_spi * spi)2165 static int stm32fx_spi_config(struct stm32_spi *spi)
2166 {
2167 	unsigned long flags;
2168 
2169 	spin_lock_irqsave(&spi->lock, flags);
2170 
2171 	/* Ensure I2SMOD bit is kept cleared */
2172 	stm32_spi_clr_bits(spi, STM32FX_SPI_I2SCFGR,
2173 			   STM32FX_SPI_I2SCFGR_I2SMOD);
2174 
2175 	/*
2176 	 * - SS input value high
2177 	 * - transmitter half duplex direction
2178 	 * - Set the host mode (default Motorola mode)
2179 	 * - Consider 1 host/n targets configuration and
2180 	 *   SS input value is determined by the SSI bit
2181 	 */
2182 	stm32_spi_set_bits(spi, STM32FX_SPI_CR1, STM32FX_SPI_CR1_SSI |
2183 						 STM32FX_SPI_CR1_BIDIOE |
2184 						 STM32FX_SPI_CR1_MSTR |
2185 						 STM32FX_SPI_CR1_SSM);
2186 
2187 	spin_unlock_irqrestore(&spi->lock, flags);
2188 
2189 	return 0;
2190 }
2191 
2192 /**
2193  * stm32h7_spi_config - Configure SPI controller
2194  * @spi: pointer to the spi controller data structure
2195  */
stm32h7_spi_config(struct stm32_spi * spi)2196 static int stm32h7_spi_config(struct stm32_spi *spi)
2197 {
2198 	unsigned long flags;
2199 	u32 cr1 = 0, cfg2 = 0;
2200 
2201 	spin_lock_irqsave(&spi->lock, flags);
2202 
2203 	/* Ensure I2SMOD bit is kept cleared */
2204 	stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
2205 			   STM32H7_SPI_I2SCFGR_I2SMOD);
2206 
2207 	if (STM32_SPI_DEVICE_MODE(spi)) {
2208 		/* Use native device select */
2209 		cfg2 &= ~STM32H7_SPI_CFG2_SSM;
2210 	} else {
2211 		/*
2212 		 * - Transmitter half duplex direction
2213 		 * - Automatic communication suspend when RX-Fifo is full
2214 		 * - SS input value high
2215 		 */
2216 		cr1 |= STM32H7_SPI_CR1_HDDIR | STM32H7_SPI_CR1_MASRX | STM32H7_SPI_CR1_SSI;
2217 
2218 		/*
2219 		 * - Set the host mode (default Motorola mode)
2220 		 * - Consider 1 host/n devices configuration and
2221 		 *   SS input value is determined by the SSI bit
2222 		 * - keep control of all associated GPIOs
2223 		 */
2224 		cfg2 |= STM32H7_SPI_CFG2_MASTER | STM32H7_SPI_CFG2_SSM | STM32H7_SPI_CFG2_AFCNTR;
2225 	}
2226 
2227 	stm32_spi_set_bits(spi, STM32H7_SPI_CR1, cr1);
2228 	stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, cfg2);
2229 
2230 	spin_unlock_irqrestore(&spi->lock, flags);
2231 
2232 	return 0;
2233 }
2234 
2235 static const struct stm32_spi_cfg stm32f4_spi_cfg = {
2236 	.regs = &stm32fx_spi_regspec,
2237 	.get_bpw_mask = stm32f4_spi_get_bpw_mask,
2238 	.disable = stm32fx_spi_disable,
2239 	.config = stm32fx_spi_config,
2240 	.set_bpw = stm32f4_spi_set_bpw,
2241 	.set_mode = stm32fx_spi_set_mode,
2242 	.write_tx = stm32f4_spi_write_tx,
2243 	.read_rx = stm32f4_spi_read_rx,
2244 	.transfer_one_dma_start = stm32fx_spi_transfer_one_dma_start,
2245 	.dma_tx_cb = stm32fx_spi_dma_tx_cb,
2246 	.dma_rx_cb = stm32_spi_dma_rx_cb,
2247 	.transfer_one_irq = stm32fx_spi_transfer_one_irq,
2248 	.irq_handler_event = stm32fx_spi_irq_event,
2249 	.irq_handler_thread = stm32fx_spi_irq_thread,
2250 	.baud_rate_div_min = STM32FX_SPI_BR_DIV_MIN,
2251 	.baud_rate_div_max = STM32FX_SPI_BR_DIV_MAX,
2252 	.has_fifo = false,
2253 	.has_device_mode = false,
2254 	.flags = SPI_CONTROLLER_MUST_TX,
2255 };
2256 
2257 static const struct stm32_spi_cfg stm32f7_spi_cfg = {
2258 	.regs = &stm32fx_spi_regspec,
2259 	.get_bpw_mask = stm32f7_spi_get_bpw_mask,
2260 	.disable = stm32fx_spi_disable,
2261 	.config = stm32fx_spi_config,
2262 	.set_bpw = stm32f7_spi_set_bpw,
2263 	.set_mode = stm32fx_spi_set_mode,
2264 	.write_tx = stm32f7_spi_write_tx,
2265 	.read_rx = stm32f7_spi_read_rx,
2266 	.transfer_one_dma_start = stm32f7_spi_transfer_one_dma_start,
2267 	.dma_tx_cb = stm32fx_spi_dma_tx_cb,
2268 	.dma_rx_cb = stm32_spi_dma_rx_cb,
2269 	.transfer_one_irq = stm32fx_spi_transfer_one_irq,
2270 	.irq_handler_event = stm32fx_spi_irq_event,
2271 	.irq_handler_thread = stm32fx_spi_irq_thread,
2272 	.baud_rate_div_min = STM32FX_SPI_BR_DIV_MIN,
2273 	.baud_rate_div_max = STM32FX_SPI_BR_DIV_MAX,
2274 	.has_fifo = false,
2275 	.flags = SPI_CONTROLLER_MUST_TX,
2276 };
2277 
2278 static const struct stm32_spi_cfg stm32h7_spi_cfg = {
2279 	.regs = &stm32h7_spi_regspec,
2280 	.get_fifo_size = stm32h7_spi_get_fifo_size,
2281 	.get_bpw_mask = stm32h7_spi_get_bpw_mask,
2282 	.disable = stm32h7_spi_disable,
2283 	.config = stm32h7_spi_config,
2284 	.set_bpw = stm32h7_spi_set_bpw,
2285 	.set_mode = stm32h7_spi_set_mode,
2286 	.set_data_idleness = stm32h7_spi_data_idleness,
2287 	.set_number_of_data = stm32h7_spi_number_of_data,
2288 	.write_tx = stm32h7_spi_write_txfifo,
2289 	.read_rx = stm32h7_spi_read_rxfifo,
2290 	.transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
2291 	.dma_rx_cb = stm32_spi_dma_rx_cb,
2292 	/*
2293 	 * dma_tx_cb is not necessary since in case of TX, dma is followed by
2294 	 * SPI access hence handling is performed within the SPI interrupt
2295 	 */
2296 	.transfer_one_irq = stm32h7_spi_transfer_one_irq,
2297 	.transfer_one_poll = stm32h7_spi_transfer_one_poll,
2298 	.irq_handler_thread = stm32h7_spi_irq_thread,
2299 	.baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
2300 	.baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
2301 	.has_fifo = true,
2302 	.has_device_mode = true,
2303 };
2304 
2305 /*
2306  * STM32MP2 is compatible with the STM32H7 except:
2307  * - enforce the DMA maxburst value to 1
2308  * - spi8 have limited feature set (TSIZE_MAX = 1024, BPW of 8 OR 16)
2309  */
2310 static const struct stm32_spi_cfg stm32mp25_spi_cfg = {
2311 	.regs = &stm32mp25_spi_regspec,
2312 	.get_fifo_size = stm32h7_spi_get_fifo_size,
2313 	.get_bpw_mask = stm32mp25_spi_get_bpw_mask,
2314 	.disable = stm32h7_spi_disable,
2315 	.config = stm32h7_spi_config,
2316 	.set_bpw = stm32h7_spi_set_bpw,
2317 	.set_mode = stm32h7_spi_set_mode,
2318 	.set_data_idleness = stm32h7_spi_data_idleness,
2319 	.set_number_of_data = stm32h7_spi_number_of_data,
2320 	.transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
2321 	.dma_rx_cb = stm32_spi_dma_rx_cb,
2322 	/*
2323 	 * dma_tx_cb is not necessary since in case of TX, dma is followed by
2324 	 * SPI access hence handling is performed within the SPI interrupt
2325 	 */
2326 	.transfer_one_irq = stm32h7_spi_transfer_one_irq,
2327 	.transfer_one_poll = stm32h7_spi_transfer_one_poll,
2328 	.irq_handler_thread = stm32h7_spi_irq_thread,
2329 	.baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
2330 	.baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
2331 	.has_fifo = true,
2332 	.prevent_dma_burst = true,
2333 	.has_device_mode = true,
2334 };
2335 
2336 static const struct of_device_id stm32_spi_of_match[] = {
2337 	{ .compatible = "st,stm32mp25-spi", .data = (void *)&stm32mp25_spi_cfg },
2338 	{ .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg },
2339 	{ .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg },
2340 	{ .compatible = "st,stm32f7-spi", .data = (void *)&stm32f7_spi_cfg },
2341 	{},
2342 };
2343 MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
2344 
stm32h7_spi_device_abort(struct spi_controller * ctrl)2345 static int stm32h7_spi_device_abort(struct spi_controller *ctrl)
2346 {
2347 	spi_finalize_current_transfer(ctrl);
2348 	return 0;
2349 }
2350 
stm32_spi_probe(struct platform_device * pdev)2351 static int stm32_spi_probe(struct platform_device *pdev)
2352 {
2353 	struct spi_controller *ctrl;
2354 	struct stm32_spi *spi;
2355 	struct resource *res;
2356 	struct reset_control *rst;
2357 	struct device_node *np = pdev->dev.of_node;
2358 	const struct stm32_spi_cfg *cfg;
2359 	bool device_mode;
2360 	int ret;
2361 
2362 	cfg = of_device_get_match_data(&pdev->dev);
2363 	if (!cfg) {
2364 		dev_err(&pdev->dev, "Failed to get match data for platform\n");
2365 		return -ENODEV;
2366 	}
2367 
2368 	device_mode = of_property_read_bool(np, "spi-slave");
2369 	if (!cfg->has_device_mode && device_mode) {
2370 		dev_err(&pdev->dev, "spi-slave not supported\n");
2371 		return -EPERM;
2372 	}
2373 
2374 	if (device_mode)
2375 		ctrl = devm_spi_alloc_target(&pdev->dev, sizeof(struct stm32_spi));
2376 	else
2377 		ctrl = devm_spi_alloc_host(&pdev->dev, sizeof(struct stm32_spi));
2378 	if (!ctrl) {
2379 		dev_err(&pdev->dev, "spi controller allocation failed\n");
2380 		return -ENOMEM;
2381 	}
2382 	platform_set_drvdata(pdev, ctrl);
2383 
2384 	spi = spi_controller_get_devdata(ctrl);
2385 	spi->dev = &pdev->dev;
2386 	spi->ctrl = ctrl;
2387 	spi->device_mode = device_mode;
2388 	spin_lock_init(&spi->lock);
2389 
2390 	spi->cfg = cfg;
2391 
2392 	spi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2393 	if (IS_ERR(spi->base))
2394 		return PTR_ERR(spi->base);
2395 
2396 	spi->phys_addr = (dma_addr_t)res->start;
2397 
2398 	spi->irq = platform_get_irq(pdev, 0);
2399 	if (spi->irq <= 0)
2400 		return spi->irq;
2401 
2402 	ret = devm_request_threaded_irq(&pdev->dev, spi->irq,
2403 					spi->cfg->irq_handler_event,
2404 					spi->cfg->irq_handler_thread,
2405 					IRQF_ONESHOT, pdev->name, ctrl);
2406 	if (ret) {
2407 		dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
2408 			ret);
2409 		return ret;
2410 	}
2411 
2412 	spi->clk = devm_clk_get(&pdev->dev, NULL);
2413 	if (IS_ERR(spi->clk)) {
2414 		ret = PTR_ERR(spi->clk);
2415 		dev_err(&pdev->dev, "clk get failed: %d\n", ret);
2416 		return ret;
2417 	}
2418 
2419 	ret = clk_prepare_enable(spi->clk);
2420 	if (ret) {
2421 		dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
2422 		return ret;
2423 	}
2424 	spi->clk_rate = clk_get_rate(spi->clk);
2425 	if (!spi->clk_rate) {
2426 		dev_err(&pdev->dev, "clk rate = 0\n");
2427 		ret = -EINVAL;
2428 		goto err_clk_disable;
2429 	}
2430 
2431 	rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
2432 	if (rst) {
2433 		if (IS_ERR(rst)) {
2434 			ret = dev_err_probe(&pdev->dev, PTR_ERR(rst),
2435 					    "failed to get reset\n");
2436 			goto err_clk_disable;
2437 		}
2438 
2439 		reset_control_assert(rst);
2440 		udelay(2);
2441 		reset_control_deassert(rst);
2442 	}
2443 
2444 	if (spi->cfg->has_fifo)
2445 		spi->fifo_size = spi->cfg->get_fifo_size(spi);
2446 
2447 	spi->feature_set = STM32_SPI_FEATURE_FULL;
2448 	if (spi->cfg->regs->fullcfg.reg) {
2449 		spi->feature_set =
2450 			FIELD_GET(STM32MP25_SPI_HWCFGR1_FULLCFG,
2451 				  readl_relaxed(spi->base + spi->cfg->regs->fullcfg.reg));
2452 
2453 		dev_dbg(spi->dev, "%s feature set\n",
2454 			spi->feature_set == STM32_SPI_FEATURE_FULL ? "full" : "limited");
2455 	}
2456 
2457 	/* Only for STM32H7 and after */
2458 	spi->t_size_max = spi->feature_set == STM32_SPI_FEATURE_FULL ?
2459 				STM32H7_SPI_TSIZE_MAX :
2460 				STM32MP25_SPI_TSIZE_MAX_LIMITED;
2461 	dev_dbg(spi->dev, "one message max size %d\n", spi->t_size_max);
2462 
2463 	ret = spi->cfg->config(spi);
2464 	if (ret) {
2465 		dev_err(&pdev->dev, "controller configuration failed: %d\n",
2466 			ret);
2467 		goto err_clk_disable;
2468 	}
2469 
2470 	ctrl->auto_runtime_pm = true;
2471 	ctrl->bus_num = pdev->id;
2472 	ctrl->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
2473 			  SPI_3WIRE | SPI_READY;
2474 	ctrl->bits_per_word_mask = spi->cfg->get_bpw_mask(spi);
2475 	ctrl->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min;
2476 	ctrl->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max;
2477 	ctrl->use_gpio_descriptors = true;
2478 	ctrl->optimize_message = stm32_spi_optimize_message;
2479 	ctrl->prepare_message = stm32_spi_prepare_msg;
2480 	ctrl->transfer_one = stm32_spi_transfer_one;
2481 	ctrl->unprepare_message = stm32_spi_unprepare_msg;
2482 	ctrl->flags = spi->cfg->flags;
2483 	if (STM32_SPI_DEVICE_MODE(spi))
2484 		ctrl->target_abort = stm32h7_spi_device_abort;
2485 
2486 	spi->dma_tx = dma_request_chan(spi->dev, "tx");
2487 	if (IS_ERR(spi->dma_tx)) {
2488 		ret = PTR_ERR(spi->dma_tx);
2489 		if (ret == -ENODEV) {
2490 			dev_info(&pdev->dev, "tx dma disabled\n");
2491 			spi->dma_tx = NULL;
2492 		} else {
2493 			dev_err_probe(&pdev->dev, ret, "failed to request tx dma channel\n");
2494 			goto err_clk_disable;
2495 		}
2496 	} else {
2497 		ctrl->dma_tx = spi->dma_tx;
2498 	}
2499 
2500 	spi->dma_rx = dma_request_chan(spi->dev, "rx");
2501 	if (IS_ERR(spi->dma_rx)) {
2502 		ret = PTR_ERR(spi->dma_rx);
2503 		if (ret == -ENODEV) {
2504 			dev_info(&pdev->dev, "rx dma disabled\n");
2505 			spi->dma_rx = NULL;
2506 		} else {
2507 			dev_err_probe(&pdev->dev, ret, "failed to request rx dma channel\n");
2508 			goto err_dma_release;
2509 		}
2510 	} else {
2511 		ctrl->dma_rx = spi->dma_rx;
2512 	}
2513 
2514 	if (spi->dma_tx || spi->dma_rx)
2515 		ctrl->can_dma = stm32_spi_can_dma;
2516 
2517 	spi->sram_pool = of_gen_pool_get(pdev->dev.of_node, "sram", 0);
2518 	if (spi->sram_pool) {
2519 		spi->sram_rx_buf_size = gen_pool_size(spi->sram_pool);
2520 		dev_info(&pdev->dev, "SRAM pool: %zu KiB for RX DMA/MDMA chaining\n",
2521 			 spi->sram_rx_buf_size / 1024);
2522 		spi->sram_rx_buf = gen_pool_dma_zalloc(spi->sram_pool, spi->sram_rx_buf_size,
2523 						       &spi->sram_dma_rx_buf);
2524 		if (!spi->sram_rx_buf) {
2525 			dev_err(&pdev->dev, "failed to allocate SRAM buffer\n");
2526 		} else {
2527 			spi->mdma_rx = dma_request_chan(spi->dev, "rxm2m");
2528 			if (IS_ERR(spi->mdma_rx)) {
2529 				ret = PTR_ERR(spi->mdma_rx);
2530 				spi->mdma_rx = NULL;
2531 				if (ret == -EPROBE_DEFER) {
2532 					goto err_pool_free;
2533 				} else {
2534 					gen_pool_free(spi->sram_pool,
2535 						      (unsigned long)spi->sram_rx_buf,
2536 						      spi->sram_rx_buf_size);
2537 					dev_warn(&pdev->dev,
2538 						 "failed to request rx mdma channel, DMA only\n");
2539 				}
2540 			}
2541 		}
2542 	}
2543 
2544 	pm_runtime_set_autosuspend_delay(&pdev->dev,
2545 					 STM32_SPI_AUTOSUSPEND_DELAY);
2546 	pm_runtime_use_autosuspend(&pdev->dev);
2547 	pm_runtime_set_active(&pdev->dev);
2548 	pm_runtime_get_noresume(&pdev->dev);
2549 	pm_runtime_enable(&pdev->dev);
2550 
2551 	ret = spi_register_controller(ctrl);
2552 	if (ret) {
2553 		dev_err(&pdev->dev, "spi controller registration failed: %d\n",
2554 			ret);
2555 		goto err_pm_disable;
2556 	}
2557 
2558 	pm_runtime_put_autosuspend(&pdev->dev);
2559 
2560 	dev_info(&pdev->dev, "driver initialized (%s mode)\n",
2561 		 STM32_SPI_HOST_MODE(spi) ? "host" : "device");
2562 
2563 	return 0;
2564 
2565 err_pm_disable:
2566 	pm_runtime_disable(&pdev->dev);
2567 	pm_runtime_put_noidle(&pdev->dev);
2568 	pm_runtime_set_suspended(&pdev->dev);
2569 	pm_runtime_dont_use_autosuspend(&pdev->dev);
2570 
2571 	if (spi->mdma_rx)
2572 		dma_release_channel(spi->mdma_rx);
2573 err_pool_free:
2574 	if (spi->sram_pool)
2575 		gen_pool_free(spi->sram_pool, (unsigned long)spi->sram_rx_buf,
2576 			      spi->sram_rx_buf_size);
2577 err_dma_release:
2578 	if (spi->dma_tx)
2579 		dma_release_channel(spi->dma_tx);
2580 	if (spi->dma_rx)
2581 		dma_release_channel(spi->dma_rx);
2582 err_clk_disable:
2583 	clk_disable_unprepare(spi->clk);
2584 
2585 	return ret;
2586 }
2587 
stm32_spi_remove(struct platform_device * pdev)2588 static void stm32_spi_remove(struct platform_device *pdev)
2589 {
2590 	struct spi_controller *ctrl = platform_get_drvdata(pdev);
2591 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2592 
2593 	pm_runtime_get_sync(&pdev->dev);
2594 
2595 	spi_unregister_controller(ctrl);
2596 	spi->cfg->disable(spi);
2597 
2598 	pm_runtime_disable(&pdev->dev);
2599 	pm_runtime_put_noidle(&pdev->dev);
2600 	pm_runtime_set_suspended(&pdev->dev);
2601 	pm_runtime_dont_use_autosuspend(&pdev->dev);
2602 
2603 	if (ctrl->dma_tx)
2604 		dma_release_channel(ctrl->dma_tx);
2605 	if (ctrl->dma_rx)
2606 		dma_release_channel(ctrl->dma_rx);
2607 	if (spi->mdma_rx)
2608 		dma_release_channel(spi->mdma_rx);
2609 	if (spi->sram_rx_buf)
2610 		gen_pool_free(spi->sram_pool, (unsigned long)spi->sram_rx_buf,
2611 			      spi->sram_rx_buf_size);
2612 
2613 	clk_disable_unprepare(spi->clk);
2614 
2615 
2616 	pinctrl_pm_select_sleep_state(&pdev->dev);
2617 }
2618 
stm32_spi_runtime_suspend(struct device * dev)2619 static int stm32_spi_runtime_suspend(struct device *dev)
2620 {
2621 	struct spi_controller *ctrl = dev_get_drvdata(dev);
2622 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2623 
2624 	clk_disable_unprepare(spi->clk);
2625 
2626 	return pinctrl_pm_select_sleep_state(dev);
2627 }
2628 
stm32_spi_runtime_resume(struct device * dev)2629 static int stm32_spi_runtime_resume(struct device *dev)
2630 {
2631 	struct spi_controller *ctrl = dev_get_drvdata(dev);
2632 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2633 	int ret;
2634 
2635 	ret = pinctrl_pm_select_default_state(dev);
2636 	if (ret)
2637 		return ret;
2638 
2639 	return clk_prepare_enable(spi->clk);
2640 }
2641 
stm32_spi_suspend(struct device * dev)2642 static int stm32_spi_suspend(struct device *dev)
2643 {
2644 	struct spi_controller *ctrl = dev_get_drvdata(dev);
2645 	int ret;
2646 
2647 	ret = spi_controller_suspend(ctrl);
2648 	if (ret)
2649 		return ret;
2650 
2651 	return pm_runtime_force_suspend(dev);
2652 }
2653 
stm32_spi_resume(struct device * dev)2654 static int stm32_spi_resume(struct device *dev)
2655 {
2656 	struct spi_controller *ctrl = dev_get_drvdata(dev);
2657 	struct stm32_spi *spi = spi_controller_get_devdata(ctrl);
2658 	int ret;
2659 
2660 	ret = pm_runtime_force_resume(dev);
2661 	if (ret)
2662 		return ret;
2663 
2664 	ret = spi_controller_resume(ctrl);
2665 	if (ret) {
2666 		clk_disable_unprepare(spi->clk);
2667 		return ret;
2668 	}
2669 
2670 	ret = pm_runtime_resume_and_get(dev);
2671 	if (ret < 0) {
2672 		dev_err(dev, "Unable to power device:%d\n", ret);
2673 		return ret;
2674 	}
2675 
2676 	spi->cfg->config(spi);
2677 
2678 	pm_runtime_put_autosuspend(dev);
2679 
2680 	return 0;
2681 }
2682 
2683 static const struct dev_pm_ops stm32_spi_pm_ops = {
2684 	SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
2685 	RUNTIME_PM_OPS(stm32_spi_runtime_suspend, stm32_spi_runtime_resume, NULL)
2686 };
2687 
2688 static struct platform_driver stm32_spi_driver = {
2689 	.probe = stm32_spi_probe,
2690 	.remove = stm32_spi_remove,
2691 	.driver = {
2692 		.name = DRIVER_NAME,
2693 		.pm = pm_ptr(&stm32_spi_pm_ops),
2694 		.of_match_table = stm32_spi_of_match,
2695 	},
2696 };
2697 
2698 module_platform_driver(stm32_spi_driver);
2699 
2700 MODULE_ALIAS("platform:" DRIVER_NAME);
2701 MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
2702 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
2703 MODULE_LICENSE("GPL v2");
2704