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