blob: 9bd3fd1652f744bc6132198414a2e790f3866a81 [file] [log] [blame]
Cezary Gapinskid57a9842018-12-24 23:00:27 +01001// 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
Amelie Delaunay5a380b82021-02-05 19:59:27 +01008#include <linux/bitfield.h>
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02009#include <linux/debugfs.h>
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/dmaengine.h>
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +020013#include <linux/interrupt.h>
14#include <linux/iopoll.h>
15#include <linux/module.h>
16#include <linux/of_platform.h>
Amelie Delaunaydb96bf92020-08-10 09:12:37 +020017#include <linux/pinctrl/consumer.h>
Amelie Delaunay038ac862017-06-27 17:45:18 +020018#include <linux/pm_runtime.h>
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +020019#include <linux/reset.h>
20#include <linux/spi/spi.h>
21
22#define DRIVER_NAME "spi_stm32"
23
Cezary Gapinski00505ed2018-12-24 23:00:38 +010024/* 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
Cezary Gapinski86026632018-12-24 23:00:33 +010077/* 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
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +020088
Cezary Gapinski86026632018-12-24 23:00:33 +010089/* 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)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +020096
Cezary Gapinski86026632018-12-24 23:00:33 +010097/* STM32H7_SPI_CR2 bit fields */
Cezary Gapinski86026632018-12-24 23:00:33 +010098#define STM32H7_SPI_CR2_TSIZE GENMASK(15, 0)
Amelie Delaunay5a380b82021-02-05 19:59:27 +010099#define STM32H7_SPI_TSIZE_MAX GENMASK(15, 0)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200100
Cezary Gapinski86026632018-12-24 23:00:33 +0100101/* STM32H7_SPI_CFG1 bit fields */
Cezary Gapinski86026632018-12-24 23:00:33 +0100102#define STM32H7_SPI_CFG1_DSIZE GENMASK(4, 0)
Cezary Gapinski86026632018-12-24 23:00:33 +0100103#define STM32H7_SPI_CFG1_FTHLV GENMASK(8, 5)
104#define STM32H7_SPI_CFG1_RXDMAEN BIT(14)
105#define STM32H7_SPI_CFG1_TXDMAEN BIT(15)
Cezary Gapinski86026632018-12-24 23:00:33 +0100106#define STM32H7_SPI_CFG1_MBR GENMASK(30, 28)
Amelie Delaunay5a380b82021-02-05 19:59:27 +0100107#define STM32H7_SPI_CFG1_MBR_SHIFT 28
Cezary Gapinski86026632018-12-24 23:00:33 +0100108#define STM32H7_SPI_CFG1_MBR_MIN 0
109#define STM32H7_SPI_CFG1_MBR_MAX (GENMASK(30, 28) >> 28)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200110
Cezary Gapinski86026632018-12-24 23:00:33 +0100111/* STM32H7_SPI_CFG2 bit fields */
Cezary Gapinski86026632018-12-24 23:00:33 +0100112#define STM32H7_SPI_CFG2_MIDI GENMASK(7, 4)
Cezary Gapinski86026632018-12-24 23:00:33 +0100113#define STM32H7_SPI_CFG2_COMM GENMASK(18, 17)
Cezary Gapinski86026632018-12-24 23:00:33 +0100114#define STM32H7_SPI_CFG2_SP GENMASK(21, 19)
115#define STM32H7_SPI_CFG2_MASTER BIT(22)
116#define STM32H7_SPI_CFG2_LSBFRST BIT(23)
117#define STM32H7_SPI_CFG2_CPHA BIT(24)
118#define STM32H7_SPI_CFG2_CPOL BIT(25)
119#define STM32H7_SPI_CFG2_SSM BIT(26)
120#define STM32H7_SPI_CFG2_AFCNTR BIT(31)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200121
Cezary Gapinski86026632018-12-24 23:00:33 +0100122/* STM32H7_SPI_IER bit fields */
123#define STM32H7_SPI_IER_RXPIE BIT(0)
124#define STM32H7_SPI_IER_TXPIE BIT(1)
125#define STM32H7_SPI_IER_DXPIE BIT(2)
126#define STM32H7_SPI_IER_EOTIE BIT(3)
127#define STM32H7_SPI_IER_TXTFIE BIT(4)
128#define STM32H7_SPI_IER_OVRIE BIT(6)
129#define STM32H7_SPI_IER_MODFIE BIT(9)
130#define STM32H7_SPI_IER_ALL GENMASK(10, 0)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200131
Cezary Gapinski86026632018-12-24 23:00:33 +0100132/* STM32H7_SPI_SR bit fields */
133#define STM32H7_SPI_SR_RXP BIT(0)
134#define STM32H7_SPI_SR_TXP BIT(1)
135#define STM32H7_SPI_SR_EOT BIT(3)
136#define STM32H7_SPI_SR_OVR BIT(6)
137#define STM32H7_SPI_SR_MODF BIT(9)
138#define STM32H7_SPI_SR_SUSP BIT(11)
Cezary Gapinski86026632018-12-24 23:00:33 +0100139#define STM32H7_SPI_SR_RXPLVL GENMASK(14, 13)
140#define STM32H7_SPI_SR_RXWNE BIT(15)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200141
Cezary Gapinski86026632018-12-24 23:00:33 +0100142/* STM32H7_SPI_IFCR bit fields */
143#define STM32H7_SPI_IFCR_ALL GENMASK(11, 3)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200144
Cezary Gapinski86026632018-12-24 23:00:33 +0100145/* STM32H7_SPI_I2SCFGR bit fields */
146#define STM32H7_SPI_I2SCFGR_I2SMOD BIT(0)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200147
Cezary Gapinski86026632018-12-24 23:00:33 +0100148/* STM32H7 SPI Master Baud Rate min/max divisor */
149#define STM32H7_SPI_MBR_DIV_MIN (2 << STM32H7_SPI_CFG1_MBR_MIN)
150#define STM32H7_SPI_MBR_DIV_MAX (2 << STM32H7_SPI_CFG1_MBR_MAX)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200151
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100152/* STM32H7 SPI Communication mode */
153#define STM32H7_SPI_FULL_DUPLEX 0
154#define STM32H7_SPI_SIMPLEX_TX 1
155#define STM32H7_SPI_SIMPLEX_RX 2
156#define STM32H7_SPI_HALF_DUPLEX 3
157
158/* SPI Communication type */
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200159#define SPI_FULL_DUPLEX 0
160#define SPI_SIMPLEX_TX 1
161#define SPI_SIMPLEX_RX 2
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100162#define SPI_3WIRE_TX 3
163#define SPI_3WIRE_RX 4
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200164
Alain Volmat9d535412021-07-07 10:27:01 +0200165#define STM32_SPI_AUTOSUSPEND_DELAY 1 /* 1 ms */
166
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100167/*
168 * use PIO for small transfers, avoiding DMA setup/teardown overhead for drivers
169 * without fifo buffers.
170 */
171#define SPI_DMA_MIN_BYTES 16
172
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200173/**
Alain Volmat1c52be82020-03-20 14:44:17 +0100174 * struct stm32_spi_reg - stm32 SPI register & bitfield desc
Cezary Gapinski55166852018-12-24 23:00:37 +0100175 * @reg: register offset
176 * @mask: bitfield mask
177 * @shift: left shift
178 */
179struct stm32_spi_reg {
180 int reg;
181 int mask;
182 int shift;
183};
184
185/**
Alain Volmat1c52be82020-03-20 14:44:17 +0100186 * struct stm32_spi_regspec - stm32 registers definition, compatible dependent data
187 * @en: enable register and SPI enable bit
188 * @dma_rx_en: SPI DMA RX enable register end SPI DMA RX enable bit
189 * @dma_tx_en: SPI DMA TX enable register end SPI DMA TX enable bit
190 * @cpol: clock polarity register and polarity bit
191 * @cpha: clock phase register and phase bit
192 * @lsb_first: LSB transmitted first register and bit
193 * @br: baud rate register and bitfields
194 * @rx: SPI RX data register
195 * @tx: SPI TX data register
Cezary Gapinski55166852018-12-24 23:00:37 +0100196 */
197struct stm32_spi_regspec {
198 const struct stm32_spi_reg en;
199 const struct stm32_spi_reg dma_rx_en;
200 const struct stm32_spi_reg dma_tx_en;
201 const struct stm32_spi_reg cpol;
202 const struct stm32_spi_reg cpha;
203 const struct stm32_spi_reg lsb_first;
204 const struct stm32_spi_reg br;
205 const struct stm32_spi_reg rx;
206 const struct stm32_spi_reg tx;
207};
208
209struct stm32_spi;
210
211/**
Alain Volmat1c52be82020-03-20 14:44:17 +0100212 * struct stm32_spi_cfg - stm32 compatible configuration data
Cezary Gapinski55166852018-12-24 23:00:37 +0100213 * @regs: registers descriptions
214 * @get_fifo_size: routine to get fifo size
215 * @get_bpw_mask: routine to get bits per word mask
216 * @disable: routine to disable controller
217 * @config: routine to configure controller as SPI Master
218 * @set_bpw: routine to configure registers to for bits per word
219 * @set_mode: routine to configure registers to desired mode
220 * @set_data_idleness: optional routine to configure registers to desired idle
221 * time between frames (if driver has this functionality)
Alain Volmat1c52be82020-03-20 14:44:17 +0100222 * @set_number_of_data: optional routine to configure registers to desired
Cezary Gapinski55166852018-12-24 23:00:37 +0100223 * number of data (if driver has this functionality)
224 * @can_dma: routine to determine if the transfer is eligible for DMA use
225 * @transfer_one_dma_start: routine to start transfer a single spi_transfer
226 * using DMA
Alain Volmat1c52be82020-03-20 14:44:17 +0100227 * @dma_rx_cb: routine to call after DMA RX channel operation is complete
228 * @dma_tx_cb: routine to call after DMA TX channel operation is complete
Cezary Gapinski55166852018-12-24 23:00:37 +0100229 * @transfer_one_irq: routine to configure interrupts for driver
230 * @irq_handler_event: Interrupt handler for SPI controller events
231 * @irq_handler_thread: thread of interrupt handler for SPI controller
232 * @baud_rate_div_min: minimum baud rate divisor
233 * @baud_rate_div_max: maximum baud rate divisor
234 * @has_fifo: boolean to know if fifo is used for driver
235 * @has_startbit: boolean to know if start bit is used to start transfer
236 */
237struct stm32_spi_cfg {
238 const struct stm32_spi_regspec *regs;
239 int (*get_fifo_size)(struct stm32_spi *spi);
240 int (*get_bpw_mask)(struct stm32_spi *spi);
241 void (*disable)(struct stm32_spi *spi);
242 int (*config)(struct stm32_spi *spi);
243 void (*set_bpw)(struct stm32_spi *spi);
244 int (*set_mode)(struct stm32_spi *spi, unsigned int comm_type);
245 void (*set_data_idleness)(struct stm32_spi *spi, u32 length);
246 int (*set_number_of_data)(struct stm32_spi *spi, u32 length);
247 void (*transfer_one_dma_start)(struct stm32_spi *spi);
248 void (*dma_rx_cb)(void *data);
249 void (*dma_tx_cb)(void *data);
250 int (*transfer_one_irq)(struct stm32_spi *spi);
251 irqreturn_t (*irq_handler_event)(int irq, void *dev_id);
252 irqreturn_t (*irq_handler_thread)(int irq, void *dev_id);
253 unsigned int baud_rate_div_min;
254 unsigned int baud_rate_div_max;
255 bool has_fifo;
256};
257
258/**
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200259 * struct stm32_spi - private data of the SPI controller
260 * @dev: driver model representation of the controller
261 * @master: controller master interface
Cezary Gapinski55166852018-12-24 23:00:37 +0100262 * @cfg: compatible configuration data
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200263 * @base: virtual memory area
264 * @clk: hw kernel clock feeding the SPI clock generator
265 * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200266 * @lock: prevent I/O concurrent access
267 * @irq: SPI controller interrupt line
268 * @fifo_size: size of the embedded fifo in bytes
269 * @cur_midi: master inter-data idleness in ns
270 * @cur_speed: speed configured in Hz
271 * @cur_bpw: number of bits in a single SPI data frame
272 * @cur_fthlv: fifo threshold level (data frames in a single data packet)
273 * @cur_comm: SPI communication mode
274 * @cur_xferlen: current transfer length in bytes
275 * @cur_usedma: boolean to know if dma is used in current transfer
276 * @tx_buf: data to be written, or NULL
277 * @rx_buf: data to be read, or NULL
278 * @tx_len: number of data to be written in bytes
279 * @rx_len: number of data to be read in bytes
280 * @dma_tx: dma channel for TX transfer
281 * @dma_rx: dma channel for RX transfer
282 * @phys_addr: SPI registers physical base address
283 */
284struct stm32_spi {
285 struct device *dev;
286 struct spi_master *master;
Cezary Gapinski55166852018-12-24 23:00:37 +0100287 const struct stm32_spi_cfg *cfg;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200288 void __iomem *base;
289 struct clk *clk;
290 u32 clk_rate;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200291 spinlock_t lock; /* prevent I/O concurrent access */
292 int irq;
293 unsigned int fifo_size;
294
295 unsigned int cur_midi;
296 unsigned int cur_speed;
297 unsigned int cur_bpw;
298 unsigned int cur_fthlv;
299 unsigned int cur_comm;
300 unsigned int cur_xferlen;
301 bool cur_usedma;
302
303 const void *tx_buf;
304 void *rx_buf;
305 int tx_len;
306 int rx_len;
307 struct dma_chan *dma_tx;
308 struct dma_chan *dma_rx;
309 dma_addr_t phys_addr;
310};
311
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100312static const struct stm32_spi_regspec stm32f4_spi_regspec = {
313 .en = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE },
314
315 .dma_rx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_RXDMAEN },
316 .dma_tx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN },
317
318 .cpol = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPOL },
319 .cpha = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPHA },
320 .lsb_first = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_LSBFRST },
321 .br = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_BR, STM32F4_SPI_CR1_BR_SHIFT },
322
323 .rx = { STM32F4_SPI_DR },
324 .tx = { STM32F4_SPI_DR },
325};
326
Cezary Gapinski55166852018-12-24 23:00:37 +0100327static const struct stm32_spi_regspec stm32h7_spi_regspec = {
328 /* SPI data transfer is enabled but spi_ker_ck is idle.
329 * CFG1 and CFG2 registers are write protected when SPE is enabled.
330 */
331 .en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
332
333 .dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
334 .dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
335
336 .cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
337 .cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
338 .lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
339 .br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
340 STM32H7_SPI_CFG1_MBR_SHIFT },
341
342 .rx = { STM32H7_SPI_RXDR },
343 .tx = { STM32H7_SPI_TXDR },
344};
345
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200346static inline void stm32_spi_set_bits(struct stm32_spi *spi,
347 u32 offset, u32 bits)
348{
349 writel_relaxed(readl_relaxed(spi->base + offset) | bits,
350 spi->base + offset);
351}
352
353static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
354 u32 offset, u32 bits)
355{
356 writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
357 spi->base + offset);
358}
359
360/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100361 * stm32h7_spi_get_fifo_size - Return fifo size
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200362 * @spi: pointer to the spi controller data structure
363 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100364static int stm32h7_spi_get_fifo_size(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200365{
366 unsigned long flags;
367 u32 count = 0;
368
369 spin_lock_irqsave(&spi->lock, flags);
370
Cezary Gapinski86026632018-12-24 23:00:33 +0100371 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200372
Cezary Gapinski86026632018-12-24 23:00:33 +0100373 while (readl_relaxed(spi->base + STM32H7_SPI_SR) & STM32H7_SPI_SR_TXP)
374 writeb_relaxed(++count, spi->base + STM32H7_SPI_TXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200375
Cezary Gapinski86026632018-12-24 23:00:33 +0100376 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200377
378 spin_unlock_irqrestore(&spi->lock, flags);
379
380 dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
381
382 return count;
383}
384
385/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100386 * stm32f4_spi_get_bpw_mask - Return bits per word mask
387 * @spi: pointer to the spi controller data structure
388 */
389static int stm32f4_spi_get_bpw_mask(struct stm32_spi *spi)
390{
391 dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
392 return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
393}
394
395/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100396 * stm32h7_spi_get_bpw_mask - Return bits per word mask
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200397 * @spi: pointer to the spi controller data structure
398 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100399static int stm32h7_spi_get_bpw_mask(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200400{
401 unsigned long flags;
402 u32 cfg1, max_bpw;
403
404 spin_lock_irqsave(&spi->lock, flags);
405
406 /*
407 * The most significant bit at DSIZE bit field is reserved when the
408 * maximum data size of periperal instances is limited to 16-bit
409 */
Cezary Gapinski86026632018-12-24 23:00:33 +0100410 stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200411
Cezary Gapinski86026632018-12-24 23:00:33 +0100412 cfg1 = readl_relaxed(spi->base + STM32H7_SPI_CFG1);
Amelie Delaunay5a380b82021-02-05 19:59:27 +0100413 max_bpw = FIELD_GET(STM32H7_SPI_CFG1_DSIZE, cfg1) + 1;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200414
415 spin_unlock_irqrestore(&spi->lock, flags);
416
417 dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
418
419 return SPI_BPW_RANGE_MASK(4, max_bpw);
420}
421
422/**
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100423 * stm32_spi_prepare_mbr - Determine baud rate divisor value
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200424 * @spi: pointer to the spi controller data structure
425 * @speed_hz: requested speed
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100426 * @min_div: minimum baud rate divisor
427 * @max_div: maximum baud rate divisor
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200428 *
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100429 * Return baud rate divisor value in case of success or -EINVAL
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200430 */
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100431static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
432 u32 min_div, u32 max_div)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200433{
434 u32 div, mbrdiv;
435
Amelie Delaunay9cc61972020-08-10 09:12:36 +0200436 /* Ensure spi->clk_rate is even */
437 div = DIV_ROUND_UP(spi->clk_rate & ~0x1, speed_hz);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200438
439 /*
440 * SPI framework set xfer->speed_hz to master->max_speed_hz if
441 * xfer->speed_hz is greater than master->max_speed_hz, and it returns
442 * an error when xfer->speed_hz is lower than master->min_speed_hz, so
443 * no need to check it there.
444 * However, we need to ensure the following calculations.
445 */
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100446 if ((div < min_div) || (div > max_div))
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200447 return -EINVAL;
448
449 /* Determine the first power of 2 greater than or equal to div */
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200450 if (div & (div - 1))
451 mbrdiv = fls(div);
452 else
453 mbrdiv = fls(div) - 1;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200454
455 spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
456
457 return mbrdiv - 1;
458}
459
460/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100461 * stm32h7_spi_prepare_fthlv - Determine FIFO threshold level
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200462 * @spi: pointer to the spi controller data structure
Amelie Delaunay3373e902020-08-10 09:12:35 +0200463 * @xfer_len: length of the message to be transferred
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200464 */
Amelie Delaunay3373e902020-08-10 09:12:35 +0200465static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi, u32 xfer_len)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200466{
Marek Vasut970e8ea2021-01-04 13:31:14 +0100467 u32 packet, bpw;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200468
469 /* data packet should not exceed 1/2 of fifo space */
Marek Vasut970e8ea2021-01-04 13:31:14 +0100470 packet = clamp(xfer_len, 1U, spi->fifo_size / 2);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200471
472 /* align packet size with data registers access */
Marek Vasut970e8ea2021-01-04 13:31:14 +0100473 bpw = DIV_ROUND_UP(spi->cur_bpw, 8);
474 return DIV_ROUND_UP(packet, bpw);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200475}
476
477/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100478 * stm32f4_spi_write_tx - Write bytes to Transmit Data Register
479 * @spi: pointer to the spi controller data structure
480 *
481 * Read from tx_buf depends on remaining bytes to avoid to read beyond
482 * tx_buf end.
483 */
484static void stm32f4_spi_write_tx(struct stm32_spi *spi)
485{
486 if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
487 STM32F4_SPI_SR_TXE)) {
488 u32 offs = spi->cur_xferlen - spi->tx_len;
489
490 if (spi->cur_bpw == 16) {
491 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
492
493 writew_relaxed(*tx_buf16, spi->base + STM32F4_SPI_DR);
494 spi->tx_len -= sizeof(u16);
495 } else {
496 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
497
498 writeb_relaxed(*tx_buf8, spi->base + STM32F4_SPI_DR);
499 spi->tx_len -= sizeof(u8);
500 }
501 }
502
503 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
504}
505
506/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100507 * stm32h7_spi_write_txfifo - Write bytes in Transmit Data Register
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200508 * @spi: pointer to the spi controller data structure
509 *
510 * Read from tx_buf depends on remaining bytes to avoid to read beyond
511 * tx_buf end.
512 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100513static void stm32h7_spi_write_txfifo(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200514{
515 while ((spi->tx_len > 0) &&
Cezary Gapinski86026632018-12-24 23:00:33 +0100516 (readl_relaxed(spi->base + STM32H7_SPI_SR) &
517 STM32H7_SPI_SR_TXP)) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200518 u32 offs = spi->cur_xferlen - spi->tx_len;
519
520 if (spi->tx_len >= sizeof(u32)) {
521 const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
522
Cezary Gapinski86026632018-12-24 23:00:33 +0100523 writel_relaxed(*tx_buf32, spi->base + STM32H7_SPI_TXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200524 spi->tx_len -= sizeof(u32);
525 } else if (spi->tx_len >= sizeof(u16)) {
526 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
527
Cezary Gapinski86026632018-12-24 23:00:33 +0100528 writew_relaxed(*tx_buf16, spi->base + STM32H7_SPI_TXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200529 spi->tx_len -= sizeof(u16);
530 } else {
531 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
532
Cezary Gapinski86026632018-12-24 23:00:33 +0100533 writeb_relaxed(*tx_buf8, spi->base + STM32H7_SPI_TXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200534 spi->tx_len -= sizeof(u8);
535 }
536 }
537
538 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
539}
540
541/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100542 * stm32f4_spi_read_rx - Read bytes from Receive Data Register
543 * @spi: pointer to the spi controller data structure
544 *
545 * Write in rx_buf depends on remaining bytes to avoid to write beyond
546 * rx_buf end.
547 */
548static void stm32f4_spi_read_rx(struct stm32_spi *spi)
549{
550 if ((spi->rx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
551 STM32F4_SPI_SR_RXNE)) {
552 u32 offs = spi->cur_xferlen - spi->rx_len;
553
554 if (spi->cur_bpw == 16) {
555 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
556
557 *rx_buf16 = readw_relaxed(spi->base + STM32F4_SPI_DR);
558 spi->rx_len -= sizeof(u16);
559 } else {
560 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
561
562 *rx_buf8 = readb_relaxed(spi->base + STM32F4_SPI_DR);
563 spi->rx_len -= sizeof(u8);
564 }
565 }
566
567 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->rx_len);
568}
569
570/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100571 * stm32h7_spi_read_rxfifo - Read bytes in Receive Data Register
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200572 * @spi: pointer to the spi controller data structure
573 *
574 * Write in rx_buf depends on remaining bytes to avoid to write beyond
575 * rx_buf end.
576 */
Amelie Delaunayd87a5d62021-07-07 10:27:04 +0200577static void stm32h7_spi_read_rxfifo(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200578{
Cezary Gapinski86026632018-12-24 23:00:33 +0100579 u32 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
Amelie Delaunay5a380b82021-02-05 19:59:27 +0100580 u32 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200581
582 while ((spi->rx_len > 0) &&
Cezary Gapinski86026632018-12-24 23:00:33 +0100583 ((sr & STM32H7_SPI_SR_RXP) ||
Amelie Delaunayd87a5d62021-07-07 10:27:04 +0200584 ((sr & STM32H7_SPI_SR_EOT) &&
585 ((sr & STM32H7_SPI_SR_RXWNE) || (rxplvl > 0))))) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200586 u32 offs = spi->cur_xferlen - spi->rx_len;
587
588 if ((spi->rx_len >= sizeof(u32)) ||
Amelie Delaunayd87a5d62021-07-07 10:27:04 +0200589 (sr & STM32H7_SPI_SR_RXWNE)) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200590 u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
591
Cezary Gapinski86026632018-12-24 23:00:33 +0100592 *rx_buf32 = readl_relaxed(spi->base + STM32H7_SPI_RXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200593 spi->rx_len -= sizeof(u32);
594 } else if ((spi->rx_len >= sizeof(u16)) ||
Amelie Delaunayd87a5d62021-07-07 10:27:04 +0200595 (!(sr & STM32H7_SPI_SR_RXWNE) &&
596 (rxplvl >= 2 || spi->cur_bpw > 8))) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200597 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
598
Cezary Gapinski86026632018-12-24 23:00:33 +0100599 *rx_buf16 = readw_relaxed(spi->base + STM32H7_SPI_RXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200600 spi->rx_len -= sizeof(u16);
601 } else {
602 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
603
Cezary Gapinski86026632018-12-24 23:00:33 +0100604 *rx_buf8 = readb_relaxed(spi->base + STM32H7_SPI_RXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200605 spi->rx_len -= sizeof(u8);
606 }
607
Cezary Gapinski86026632018-12-24 23:00:33 +0100608 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
Amelie Delaunay5a380b82021-02-05 19:59:27 +0100609 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200610 }
611
Amelie Delaunayd87a5d62021-07-07 10:27:04 +0200612 dev_dbg(spi->dev, "%s: %d bytes left (sr=%08x)\n",
613 __func__, spi->rx_len, sr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200614}
615
616/**
617 * stm32_spi_enable - Enable SPI controller
618 * @spi: pointer to the spi controller data structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200619 */
620static void stm32_spi_enable(struct stm32_spi *spi)
621{
622 dev_dbg(spi->dev, "enable controller\n");
623
Cezary Gapinski55166852018-12-24 23:00:37 +0100624 stm32_spi_set_bits(spi, spi->cfg->regs->en.reg,
625 spi->cfg->regs->en.mask);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200626}
627
628/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100629 * stm32f4_spi_disable - Disable SPI controller
630 * @spi: pointer to the spi controller data structure
631 */
632static void stm32f4_spi_disable(struct stm32_spi *spi)
633{
634 unsigned long flags;
635 u32 sr;
636
637 dev_dbg(spi->dev, "disable controller\n");
638
639 spin_lock_irqsave(&spi->lock, flags);
640
641 if (!(readl_relaxed(spi->base + STM32F4_SPI_CR1) &
642 STM32F4_SPI_CR1_SPE)) {
643 spin_unlock_irqrestore(&spi->lock, flags);
644 return;
645 }
646
647 /* Disable interrupts */
648 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXEIE |
649 STM32F4_SPI_CR2_RXNEIE |
650 STM32F4_SPI_CR2_ERRIE);
651
652 /* Wait until BSY = 0 */
653 if (readl_relaxed_poll_timeout_atomic(spi->base + STM32F4_SPI_SR,
654 sr, !(sr & STM32F4_SPI_SR_BSY),
655 10, 100000) < 0) {
656 dev_warn(spi->dev, "disabling condition timeout\n");
657 }
658
659 if (spi->cur_usedma && spi->dma_tx)
660 dmaengine_terminate_all(spi->dma_tx);
661 if (spi->cur_usedma && spi->dma_rx)
662 dmaengine_terminate_all(spi->dma_rx);
663
664 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE);
665
666 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN |
667 STM32F4_SPI_CR2_RXDMAEN);
668
669 /* Sequence to clear OVR flag */
670 readl_relaxed(spi->base + STM32F4_SPI_DR);
671 readl_relaxed(spi->base + STM32F4_SPI_SR);
672
673 spin_unlock_irqrestore(&spi->lock, flags);
674}
675
676/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100677 * stm32h7_spi_disable - Disable SPI controller
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200678 * @spi: pointer to the spi controller data structure
679 *
Alain Volmatdc6620c2021-07-07 10:27:05 +0200680 * RX-Fifo is flushed when SPI controller is disabled.
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200681 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100682static void stm32h7_spi_disable(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200683{
684 unsigned long flags;
Alain Volmatdc6620c2021-07-07 10:27:05 +0200685 u32 cr1;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200686
687 dev_dbg(spi->dev, "disable controller\n");
688
689 spin_lock_irqsave(&spi->lock, flags);
690
Cezary Gapinski86026632018-12-24 23:00:33 +0100691 cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200692
Cezary Gapinski86026632018-12-24 23:00:33 +0100693 if (!(cr1 & STM32H7_SPI_CR1_SPE)) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200694 spin_unlock_irqrestore(&spi->lock, flags);
695 return;
696 }
697
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100698 if (spi->cur_usedma && spi->dma_tx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200699 dmaengine_terminate_all(spi->dma_tx);
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100700 if (spi->cur_usedma && spi->dma_rx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200701 dmaengine_terminate_all(spi->dma_rx);
702
Cezary Gapinski86026632018-12-24 23:00:33 +0100703 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200704
Cezary Gapinski86026632018-12-24 23:00:33 +0100705 stm32_spi_clr_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN |
706 STM32H7_SPI_CFG1_RXDMAEN);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200707
708 /* Disable interrupts and clear status flags */
Cezary Gapinski86026632018-12-24 23:00:33 +0100709 writel_relaxed(0, spi->base + STM32H7_SPI_IER);
710 writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200711
712 spin_unlock_irqrestore(&spi->lock, flags);
713}
714
715/**
716 * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use
Alain Volmat1c52be82020-03-20 14:44:17 +0100717 * @master: controller master interface
718 * @spi_dev: pointer to the spi device
719 * @transfer: pointer to spi transfer
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200720 *
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100721 * If driver has fifo and the current transfer size is greater than fifo size,
722 * use DMA. Otherwise use DMA for transfer longer than defined DMA min bytes.
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200723 */
724static bool stm32_spi_can_dma(struct spi_master *master,
725 struct spi_device *spi_dev,
726 struct spi_transfer *transfer)
727{
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100728 unsigned int dma_size;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200729 struct stm32_spi *spi = spi_master_get_devdata(master);
730
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100731 if (spi->cfg->has_fifo)
732 dma_size = spi->fifo_size;
733 else
734 dma_size = SPI_DMA_MIN_BYTES;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200735
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100736 dev_dbg(spi->dev, "%s: %s\n", __func__,
737 (transfer->len > dma_size) ? "true" : "false");
738
739 return (transfer->len > dma_size);
740}
741
742/**
743 * stm32f4_spi_irq_event - Interrupt handler for SPI controller events
744 * @irq: interrupt line
745 * @dev_id: SPI controller master interface
746 */
747static irqreturn_t stm32f4_spi_irq_event(int irq, void *dev_id)
748{
749 struct spi_master *master = dev_id;
750 struct stm32_spi *spi = spi_master_get_devdata(master);
751 u32 sr, mask = 0;
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100752 bool end = false;
753
Barry Songe2368932020-09-26 12:16:16 +1200754 spin_lock(&spi->lock);
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100755
756 sr = readl_relaxed(spi->base + STM32F4_SPI_SR);
757 /*
758 * BSY flag is not handled in interrupt but it is normal behavior when
759 * this flag is set.
760 */
761 sr &= ~STM32F4_SPI_SR_BSY;
762
763 if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX ||
764 spi->cur_comm == SPI_3WIRE_TX)) {
765 /* OVR flag shouldn't be handled for TX only mode */
766 sr &= ~STM32F4_SPI_SR_OVR | STM32F4_SPI_SR_RXNE;
767 mask |= STM32F4_SPI_SR_TXE;
768 }
769
dillon min61367d02020-05-25 11:45:47 +0800770 if (!spi->cur_usedma && (spi->cur_comm == SPI_FULL_DUPLEX ||
771 spi->cur_comm == SPI_SIMPLEX_RX ||
772 spi->cur_comm == SPI_3WIRE_RX)) {
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100773 /* TXE flag is set and is handled when RXNE flag occurs */
774 sr &= ~STM32F4_SPI_SR_TXE;
775 mask |= STM32F4_SPI_SR_RXNE | STM32F4_SPI_SR_OVR;
776 }
777
778 if (!(sr & mask)) {
779 dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr);
Barry Songe2368932020-09-26 12:16:16 +1200780 spin_unlock(&spi->lock);
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100781 return IRQ_NONE;
782 }
783
784 if (sr & STM32F4_SPI_SR_OVR) {
785 dev_warn(spi->dev, "Overrun: received value discarded\n");
786
787 /* Sequence to clear OVR flag */
788 readl_relaxed(spi->base + STM32F4_SPI_DR);
789 readl_relaxed(spi->base + STM32F4_SPI_SR);
790
791 /*
792 * If overrun is detected, it means that something went wrong,
793 * so stop the current transfer. Transfer can wait for next
794 * RXNE but DR is already read and end never happens.
795 */
796 end = true;
797 goto end_irq;
798 }
799
800 if (sr & STM32F4_SPI_SR_TXE) {
801 if (spi->tx_buf)
802 stm32f4_spi_write_tx(spi);
803 if (spi->tx_len == 0)
804 end = true;
805 }
806
807 if (sr & STM32F4_SPI_SR_RXNE) {
808 stm32f4_spi_read_rx(spi);
809 if (spi->rx_len == 0)
810 end = true;
dillon min61367d02020-05-25 11:45:47 +0800811 else if (spi->tx_buf)/* Load data for discontinuous mode */
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100812 stm32f4_spi_write_tx(spi);
813 }
814
815end_irq:
816 if (end) {
817 /* Immediately disable interrupts to do not generate new one */
818 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2,
819 STM32F4_SPI_CR2_TXEIE |
820 STM32F4_SPI_CR2_RXNEIE |
821 STM32F4_SPI_CR2_ERRIE);
Barry Songe2368932020-09-26 12:16:16 +1200822 spin_unlock(&spi->lock);
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100823 return IRQ_WAKE_THREAD;
824 }
825
Barry Songe2368932020-09-26 12:16:16 +1200826 spin_unlock(&spi->lock);
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100827 return IRQ_HANDLED;
828}
829
830/**
831 * stm32f4_spi_irq_thread - Thread of interrupt handler for SPI controller
832 * @irq: interrupt line
833 * @dev_id: SPI controller master interface
834 */
835static irqreturn_t stm32f4_spi_irq_thread(int irq, void *dev_id)
836{
837 struct spi_master *master = dev_id;
838 struct stm32_spi *spi = spi_master_get_devdata(master);
839
840 spi_finalize_current_transfer(master);
841 stm32f4_spi_disable(spi);
842
843 return IRQ_HANDLED;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200844}
845
846/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100847 * stm32h7_spi_irq_thread - Thread of interrupt handler for SPI controller
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200848 * @irq: interrupt line
849 * @dev_id: SPI controller master interface
850 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100851static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200852{
853 struct spi_master *master = dev_id;
854 struct stm32_spi *spi = spi_master_get_devdata(master);
855 u32 sr, ier, mask;
856 unsigned long flags;
857 bool end = false;
858
859 spin_lock_irqsave(&spi->lock, flags);
860
Cezary Gapinski86026632018-12-24 23:00:33 +0100861 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
862 ier = readl_relaxed(spi->base + STM32H7_SPI_IER);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200863
864 mask = ier;
Alain Volmate4a5c192021-06-30 10:45:19 +0200865 /*
866 * EOTIE enables irq from EOT, SUSP and TXC events. We need to set
867 * SUSP to acknowledge it later. TXC is automatically cleared
868 */
869
Cezary Gapinski86026632018-12-24 23:00:33 +0100870 mask |= STM32H7_SPI_SR_SUSP;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200871 /*
Alain Volmate4a5c192021-06-30 10:45:19 +0200872 * DXPIE is set in Full-Duplex, one IT will be raised if TXP and RXP
873 * are set. So in case of Full-Duplex, need to poll TXP and RXP event.
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200874 */
Alain Volmate4a5c192021-06-30 10:45:19 +0200875 if ((spi->cur_comm == SPI_FULL_DUPLEX) && !spi->cur_usedma)
876 mask |= STM32H7_SPI_SR_TXP | STM32H7_SPI_SR_RXP;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200877
878 if (!(sr & mask)) {
Alain Volmatc64e7ef2021-02-05 19:59:32 +0100879 dev_warn(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
880 sr, ier);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200881 spin_unlock_irqrestore(&spi->lock, flags);
882 return IRQ_NONE;
883 }
884
Cezary Gapinski86026632018-12-24 23:00:33 +0100885 if (sr & STM32H7_SPI_SR_SUSP) {
Marek Vasutea8be082020-09-05 17:19:13 +0200886 static DEFINE_RATELIMIT_STATE(rs,
887 DEFAULT_RATELIMIT_INTERVAL * 10,
888 1);
889 if (__ratelimit(&rs))
890 dev_dbg_ratelimited(spi->dev, "Communication suspended\n");
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200891 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
Amelie Delaunayd87a5d62021-07-07 10:27:04 +0200892 stm32h7_spi_read_rxfifo(spi);
Amelie Delaunayc67ad362017-06-27 17:45:19 +0200893 /*
894 * If communication is suspended while using DMA, it means
895 * that something went wrong, so stop the current transfer
896 */
897 if (spi->cur_usedma)
898 end = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200899 }
900
Cezary Gapinski86026632018-12-24 23:00:33 +0100901 if (sr & STM32H7_SPI_SR_MODF) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200902 dev_warn(spi->dev, "Mode fault: transfer aborted\n");
903 end = true;
904 }
905
Cezary Gapinski86026632018-12-24 23:00:33 +0100906 if (sr & STM32H7_SPI_SR_OVR) {
Alain Volmatc64e7ef2021-02-05 19:59:32 +0100907 dev_err(spi->dev, "Overrun: RX data lost\n");
908 end = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200909 }
910
Cezary Gapinski86026632018-12-24 23:00:33 +0100911 if (sr & STM32H7_SPI_SR_EOT) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200912 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
Amelie Delaunayd87a5d62021-07-07 10:27:04 +0200913 stm32h7_spi_read_rxfifo(spi);
Alain Volmat7ceb0b82021-07-07 10:27:06 +0200914 if (!spi->cur_usedma ||
kernel test robot7a4697b2021-07-14 03:10:04 +0800915 (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX))
Alain Volmat7ceb0b82021-07-07 10:27:06 +0200916 end = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200917 }
918
Cezary Gapinski86026632018-12-24 23:00:33 +0100919 if (sr & STM32H7_SPI_SR_TXP)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200920 if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
Cezary Gapinski55166852018-12-24 23:00:37 +0100921 stm32h7_spi_write_txfifo(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200922
Cezary Gapinski86026632018-12-24 23:00:33 +0100923 if (sr & STM32H7_SPI_SR_RXP)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200924 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
Amelie Delaunayd87a5d62021-07-07 10:27:04 +0200925 stm32h7_spi_read_rxfifo(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200926
Tobias Schrammae1ba502020-08-04 21:51:36 +0200927 writel_relaxed(sr & mask, spi->base + STM32H7_SPI_IFCR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200928
929 spin_unlock_irqrestore(&spi->lock, flags);
930
931 if (end) {
Cezary Gapinski55166852018-12-24 23:00:37 +0100932 stm32h7_spi_disable(spi);
Antonio Borneo135dd872020-08-10 09:12:34 +0200933 spi_finalize_current_transfer(master);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200934 }
935
936 return IRQ_HANDLED;
937}
938
939/**
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200940 * stm32_spi_prepare_msg - set up the controller to transfer a single message
Alain Volmat1c52be82020-03-20 14:44:17 +0100941 * @master: controller master interface
942 * @msg: pointer to spi message
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200943 */
944static int stm32_spi_prepare_msg(struct spi_master *master,
945 struct spi_message *msg)
946{
947 struct stm32_spi *spi = spi_master_get_devdata(master);
948 struct spi_device *spi_dev = msg->spi;
949 struct device_node *np = spi_dev->dev.of_node;
950 unsigned long flags;
Cezary Gapinski55166852018-12-24 23:00:37 +0100951 u32 clrb = 0, setb = 0;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200952
953 /* SPI slave device may need time between data frames */
954 spi->cur_midi = 0;
Amelie Delaunay042c1c62017-06-27 17:45:16 +0200955 if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200956 dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
957
958 if (spi_dev->mode & SPI_CPOL)
Cezary Gapinski55166852018-12-24 23:00:37 +0100959 setb |= spi->cfg->regs->cpol.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200960 else
Cezary Gapinski55166852018-12-24 23:00:37 +0100961 clrb |= spi->cfg->regs->cpol.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200962
963 if (spi_dev->mode & SPI_CPHA)
Cezary Gapinski55166852018-12-24 23:00:37 +0100964 setb |= spi->cfg->regs->cpha.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200965 else
Cezary Gapinski55166852018-12-24 23:00:37 +0100966 clrb |= spi->cfg->regs->cpha.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200967
968 if (spi_dev->mode & SPI_LSB_FIRST)
Cezary Gapinski55166852018-12-24 23:00:37 +0100969 setb |= spi->cfg->regs->lsb_first.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200970 else
Cezary Gapinski55166852018-12-24 23:00:37 +0100971 clrb |= spi->cfg->regs->lsb_first.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200972
973 dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n",
Alexandru Ardelean8b835da2021-01-04 16:31:03 +0200974 !!(spi_dev->mode & SPI_CPOL),
975 !!(spi_dev->mode & SPI_CPHA),
976 !!(spi_dev->mode & SPI_LSB_FIRST),
977 !!(spi_dev->mode & SPI_CS_HIGH));
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200978
Alain Volmat084de522021-02-05 19:59:28 +0100979 /* On STM32H7, messages should not exceed a maximum size setted
980 * afterward via the set_number_of_data function. In order to
981 * ensure that, split large messages into several messages
982 */
983 if (spi->cfg->set_number_of_data) {
984 int ret;
985
986 ret = spi_split_transfers_maxsize(master, msg,
987 STM32H7_SPI_TSIZE_MAX,
988 GFP_KERNEL | GFP_DMA);
989 if (ret)
990 return ret;
991 }
992
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200993 spin_lock_irqsave(&spi->lock, flags);
994
Cezary Gapinski55166852018-12-24 23:00:37 +0100995 /* CPOL, CPHA and LSB FIRST bits have common register */
996 if (clrb || setb)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200997 writel_relaxed(
Cezary Gapinski55166852018-12-24 23:00:37 +0100998 (readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) &
999 ~clrb) | setb,
1000 spi->base + spi->cfg->regs->cpol.reg);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001001
1002 spin_unlock_irqrestore(&spi->lock, flags);
1003
1004 return 0;
1005}
1006
1007/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001008 * stm32f4_spi_dma_tx_cb - dma callback
Alain Volmat1c52be82020-03-20 14:44:17 +01001009 * @data: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001010 *
1011 * DMA callback is called when the transfer is complete for DMA TX channel.
1012 */
1013static void stm32f4_spi_dma_tx_cb(void *data)
1014{
1015 struct stm32_spi *spi = data;
1016
1017 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1018 spi_finalize_current_transfer(spi->master);
1019 stm32f4_spi_disable(spi);
1020 }
1021}
1022
1023/**
Alain Volmat7ceb0b82021-07-07 10:27:06 +02001024 * stm32_spi_dma_rx_cb - dma callback
Alain Volmat1c52be82020-03-20 14:44:17 +01001025 * @data: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001026 *
1027 * DMA callback is called when the transfer is complete for DMA RX channel.
1028 */
Alain Volmat7ceb0b82021-07-07 10:27:06 +02001029static void stm32_spi_dma_rx_cb(void *data)
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001030{
1031 struct stm32_spi *spi = data;
1032
1033 spi_finalize_current_transfer(spi->master);
Alain Volmat7ceb0b82021-07-07 10:27:06 +02001034 spi->cfg->disable(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001035}
1036
1037/**
1038 * stm32_spi_dma_config - configure dma slave channel depending on current
1039 * transfer bits_per_word.
Alain Volmat1c52be82020-03-20 14:44:17 +01001040 * @spi: pointer to the spi controller data structure
1041 * @dma_conf: pointer to the dma_slave_config structure
1042 * @dir: direction of the dma transfer
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001043 */
1044static void stm32_spi_dma_config(struct stm32_spi *spi,
1045 struct dma_slave_config *dma_conf,
1046 enum dma_transfer_direction dir)
1047{
1048 enum dma_slave_buswidth buswidth;
1049 u32 maxburst;
1050
Amelie Delaunay128ebb82017-06-27 17:45:17 +02001051 if (spi->cur_bpw <= 8)
1052 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1053 else if (spi->cur_bpw <= 16)
1054 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1055 else
1056 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001057
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001058 if (spi->cfg->has_fifo) {
1059 /* Valid for DMA Half or Full Fifo threshold */
1060 if (spi->cur_fthlv == 2)
1061 maxburst = 1;
1062 else
1063 maxburst = spi->cur_fthlv;
1064 } else {
Amelie Delaunay128ebb82017-06-27 17:45:17 +02001065 maxburst = 1;
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001066 }
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001067
1068 memset(dma_conf, 0, sizeof(struct dma_slave_config));
1069 dma_conf->direction = dir;
1070 if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */
Cezary Gapinski55166852018-12-24 23:00:37 +01001071 dma_conf->src_addr = spi->phys_addr + spi->cfg->regs->rx.reg;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001072 dma_conf->src_addr_width = buswidth;
1073 dma_conf->src_maxburst = maxburst;
1074
1075 dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
1076 buswidth, maxburst);
1077 } else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */
Cezary Gapinski55166852018-12-24 23:00:37 +01001078 dma_conf->dst_addr = spi->phys_addr + spi->cfg->regs->tx.reg;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001079 dma_conf->dst_addr_width = buswidth;
1080 dma_conf->dst_maxburst = maxburst;
1081
1082 dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
1083 buswidth, maxburst);
1084 }
1085}
1086
1087/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001088 * stm32f4_spi_transfer_one_irq - transfer a single spi_transfer using
1089 * interrupts
Alain Volmat1c52be82020-03-20 14:44:17 +01001090 * @spi: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001091 *
1092 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1093 * in progress.
1094 */
1095static int stm32f4_spi_transfer_one_irq(struct stm32_spi *spi)
1096{
1097 unsigned long flags;
1098 u32 cr2 = 0;
1099
1100 /* Enable the interrupts relative to the current communication mode */
1101 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1102 cr2 |= STM32F4_SPI_CR2_TXEIE;
dillon min61367d02020-05-25 11:45:47 +08001103 } else if (spi->cur_comm == SPI_FULL_DUPLEX ||
1104 spi->cur_comm == SPI_SIMPLEX_RX ||
1105 spi->cur_comm == SPI_3WIRE_RX) {
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001106 /* In transmit-only mode, the OVR flag is set in the SR register
1107 * since the received data are never read. Therefore set OVR
1108 * interrupt only when rx buffer is available.
1109 */
1110 cr2 |= STM32F4_SPI_CR2_RXNEIE | STM32F4_SPI_CR2_ERRIE;
1111 } else {
1112 return -EINVAL;
1113 }
1114
1115 spin_lock_irqsave(&spi->lock, flags);
1116
1117 stm32_spi_set_bits(spi, STM32F4_SPI_CR2, cr2);
1118
1119 stm32_spi_enable(spi);
1120
1121 /* starting data transfer when buffer is loaded */
1122 if (spi->tx_buf)
1123 stm32f4_spi_write_tx(spi);
1124
1125 spin_unlock_irqrestore(&spi->lock, flags);
1126
1127 return 1;
1128}
1129
1130/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001131 * stm32h7_spi_transfer_one_irq - transfer a single spi_transfer using
1132 * interrupts
Alain Volmat1c52be82020-03-20 14:44:17 +01001133 * @spi: pointer to the spi controller data structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001134 *
1135 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1136 * in progress.
1137 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001138static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001139{
1140 unsigned long flags;
1141 u32 ier = 0;
1142
1143 /* Enable the interrupts relative to the current communication mode */
1144 if (spi->tx_buf && spi->rx_buf) /* Full Duplex */
Cezary Gapinski86026632018-12-24 23:00:33 +01001145 ier |= STM32H7_SPI_IER_DXPIE;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001146 else if (spi->tx_buf) /* Half-Duplex TX dir or Simplex TX */
Cezary Gapinski86026632018-12-24 23:00:33 +01001147 ier |= STM32H7_SPI_IER_TXPIE;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001148 else if (spi->rx_buf) /* Half-Duplex RX dir or Simplex RX */
Cezary Gapinski86026632018-12-24 23:00:33 +01001149 ier |= STM32H7_SPI_IER_RXPIE;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001150
1151 /* Enable the interrupts relative to the end of transfer */
Cezary Gapinski86026632018-12-24 23:00:33 +01001152 ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE |
1153 STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001154
1155 spin_lock_irqsave(&spi->lock, flags);
1156
1157 stm32_spi_enable(spi);
1158
1159 /* Be sure to have data in fifo before starting data transfer */
1160 if (spi->tx_buf)
Cezary Gapinski55166852018-12-24 23:00:37 +01001161 stm32h7_spi_write_txfifo(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001162
Cezary Gapinski86026632018-12-24 23:00:33 +01001163 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001164
Cezary Gapinski86026632018-12-24 23:00:33 +01001165 writel_relaxed(ier, spi->base + STM32H7_SPI_IER);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001166
1167 spin_unlock_irqrestore(&spi->lock, flags);
1168
1169 return 1;
1170}
1171
1172/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001173 * stm32f4_spi_transfer_one_dma_start - Set SPI driver registers to start
1174 * transfer using DMA
Alain Volmat1c52be82020-03-20 14:44:17 +01001175 * @spi: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001176 */
1177static void stm32f4_spi_transfer_one_dma_start(struct stm32_spi *spi)
1178{
1179 /* In DMA mode end of transfer is handled by DMA TX or RX callback. */
1180 if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX ||
1181 spi->cur_comm == SPI_FULL_DUPLEX) {
1182 /*
1183 * In transmit-only mode, the OVR flag is set in the SR register
1184 * since the received data are never read. Therefore set OVR
1185 * interrupt only when rx buffer is available.
1186 */
1187 stm32_spi_set_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_ERRIE);
1188 }
1189
1190 stm32_spi_enable(spi);
1191}
1192
1193/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001194 * stm32h7_spi_transfer_one_dma_start - Set SPI driver registers to start
1195 * transfer using DMA
Alain Volmat1c52be82020-03-20 14:44:17 +01001196 * @spi: pointer to the spi controller data structure
Cezary Gapinskif8bb12f2018-12-24 23:00:36 +01001197 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001198static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
Cezary Gapinskif8bb12f2018-12-24 23:00:36 +01001199{
Alain Volmat7ceb0b82021-07-07 10:27:06 +02001200 uint32_t ier = STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
1201
1202 /* Enable the interrupts */
1203 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX)
1204 ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE;
1205
1206 stm32_spi_set_bits(spi, STM32H7_SPI_IER, ier);
Cezary Gapinskif8bb12f2018-12-24 23:00:36 +01001207
1208 stm32_spi_enable(spi);
1209
1210 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1211}
1212
1213/**
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001214 * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
Alain Volmat1c52be82020-03-20 14:44:17 +01001215 * @spi: pointer to the spi controller data structure
1216 * @xfer: pointer to the spi_transfer structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001217 *
1218 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1219 * in progress.
1220 */
1221static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
1222 struct spi_transfer *xfer)
1223{
1224 struct dma_slave_config tx_dma_conf, rx_dma_conf;
1225 struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc;
1226 unsigned long flags;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001227
1228 spin_lock_irqsave(&spi->lock, flags);
1229
1230 rx_dma_desc = NULL;
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001231 if (spi->rx_buf && spi->dma_rx) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001232 stm32_spi_dma_config(spi, &rx_dma_conf, DMA_DEV_TO_MEM);
1233 dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1234
1235 /* Enable Rx DMA request */
Cezary Gapinski55166852018-12-24 23:00:37 +01001236 stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1237 spi->cfg->regs->dma_rx_en.mask);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001238
1239 rx_dma_desc = dmaengine_prep_slave_sg(
1240 spi->dma_rx, xfer->rx_sg.sgl,
1241 xfer->rx_sg.nents,
1242 rx_dma_conf.direction,
1243 DMA_PREP_INTERRUPT);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001244 }
1245
1246 tx_dma_desc = NULL;
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001247 if (spi->tx_buf && spi->dma_tx) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001248 stm32_spi_dma_config(spi, &tx_dma_conf, DMA_MEM_TO_DEV);
1249 dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
1250
1251 tx_dma_desc = dmaengine_prep_slave_sg(
1252 spi->dma_tx, xfer->tx_sg.sgl,
1253 xfer->tx_sg.nents,
1254 tx_dma_conf.direction,
1255 DMA_PREP_INTERRUPT);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001256 }
1257
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001258 if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
1259 (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
1260 goto dma_desc_error;
1261
1262 if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001263 goto dma_desc_error;
1264
1265 if (rx_dma_desc) {
Cezary Gapinski55166852018-12-24 23:00:37 +01001266 rx_dma_desc->callback = spi->cfg->dma_rx_cb;
Amelie Delaunay7b821a62017-06-27 17:45:20 +02001267 rx_dma_desc->callback_param = spi;
1268
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001269 if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
1270 dev_err(spi->dev, "Rx DMA submit failed\n");
1271 goto dma_desc_error;
1272 }
1273 /* Enable Rx DMA channel */
1274 dma_async_issue_pending(spi->dma_rx);
1275 }
1276
1277 if (tx_dma_desc) {
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001278 if (spi->cur_comm == SPI_SIMPLEX_TX ||
1279 spi->cur_comm == SPI_3WIRE_TX) {
Cezary Gapinski55166852018-12-24 23:00:37 +01001280 tx_dma_desc->callback = spi->cfg->dma_tx_cb;
Amelie Delaunay7b821a62017-06-27 17:45:20 +02001281 tx_dma_desc->callback_param = spi;
1282 }
1283
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001284 if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
1285 dev_err(spi->dev, "Tx DMA submit failed\n");
1286 goto dma_submit_error;
1287 }
1288 /* Enable Tx DMA channel */
1289 dma_async_issue_pending(spi->dma_tx);
1290
1291 /* Enable Tx DMA request */
Cezary Gapinski55166852018-12-24 23:00:37 +01001292 stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg,
1293 spi->cfg->regs->dma_tx_en.mask);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001294 }
1295
Cezary Gapinski55166852018-12-24 23:00:37 +01001296 spi->cfg->transfer_one_dma_start(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001297
1298 spin_unlock_irqrestore(&spi->lock, flags);
1299
1300 return 1;
1301
1302dma_submit_error:
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001303 if (spi->dma_rx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001304 dmaengine_terminate_all(spi->dma_rx);
1305
1306dma_desc_error:
Cezary Gapinski55166852018-12-24 23:00:37 +01001307 stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1308 spi->cfg->regs->dma_rx_en.mask);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001309
1310 spin_unlock_irqrestore(&spi->lock, flags);
1311
1312 dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
1313
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001314 spi->cur_usedma = false;
Cezary Gapinski55166852018-12-24 23:00:37 +01001315 return spi->cfg->transfer_one_irq(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001316}
1317
1318/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001319 * stm32f4_spi_set_bpw - Configure bits per word
1320 * @spi: pointer to the spi controller data structure
1321 */
1322static void stm32f4_spi_set_bpw(struct stm32_spi *spi)
1323{
1324 if (spi->cur_bpw == 16)
1325 stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1326 else
1327 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1328}
1329
1330/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001331 * stm32h7_spi_set_bpw - configure bits per word
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001332 * @spi: pointer to the spi controller data structure
1333 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001334static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001335{
1336 u32 bpw, fthlv;
1337 u32 cfg1_clrb = 0, cfg1_setb = 0;
1338
1339 bpw = spi->cur_bpw - 1;
1340
1341 cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE;
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001342 cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_DSIZE, bpw);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001343
Amelie Delaunay3373e902020-08-10 09:12:35 +02001344 spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001345 fthlv = spi->cur_fthlv - 1;
1346
1347 cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001348 cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_FTHLV, fthlv);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001349
1350 writel_relaxed(
1351 (readl_relaxed(spi->base + STM32H7_SPI_CFG1) &
1352 ~cfg1_clrb) | cfg1_setb,
1353 spi->base + STM32H7_SPI_CFG1);
1354}
1355
1356/**
1357 * stm32_spi_set_mbr - Configure baud rate divisor in master mode
1358 * @spi: pointer to the spi controller data structure
1359 * @mbrdiv: baud rate divisor value
1360 */
1361static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv)
1362{
Cezary Gapinski55166852018-12-24 23:00:37 +01001363 u32 clrb = 0, setb = 0;
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001364
Cezary Gapinski55166852018-12-24 23:00:37 +01001365 clrb |= spi->cfg->regs->br.mask;
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001366 setb |= (mbrdiv << spi->cfg->regs->br.shift) & spi->cfg->regs->br.mask;
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001367
Cezary Gapinski55166852018-12-24 23:00:37 +01001368 writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) &
1369 ~clrb) | setb,
1370 spi->base + spi->cfg->regs->br.reg);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001371}
1372
1373/**
1374 * stm32_spi_communication_type - return transfer communication type
1375 * @spi_dev: pointer to the spi device
Alain Volmat1c52be82020-03-20 14:44:17 +01001376 * @transfer: pointer to spi transfer
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001377 */
1378static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev,
1379 struct spi_transfer *transfer)
1380{
1381 unsigned int type = SPI_FULL_DUPLEX;
1382
1383 if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
1384 /*
1385 * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
1386 * is forbidden and unvalidated by SPI subsystem so depending
1387 * on the valid buffer, we can determine the direction of the
1388 * transfer.
1389 */
1390 if (!transfer->tx_buf)
1391 type = SPI_3WIRE_RX;
1392 else
1393 type = SPI_3WIRE_TX;
1394 } else {
1395 if (!transfer->tx_buf)
1396 type = SPI_SIMPLEX_RX;
1397 else if (!transfer->rx_buf)
1398 type = SPI_SIMPLEX_TX;
1399 }
1400
1401 return type;
1402}
1403
1404/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001405 * stm32f4_spi_set_mode - configure communication mode
1406 * @spi: pointer to the spi controller data structure
1407 * @comm_type: type of communication to configure
1408 */
1409static int stm32f4_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1410{
1411 if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
1412 stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1413 STM32F4_SPI_CR1_BIDIMODE |
1414 STM32F4_SPI_CR1_BIDIOE);
dillon min61367d02020-05-25 11:45:47 +08001415 } else if (comm_type == SPI_FULL_DUPLEX ||
1416 comm_type == SPI_SIMPLEX_RX) {
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001417 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1418 STM32F4_SPI_CR1_BIDIMODE |
1419 STM32F4_SPI_CR1_BIDIOE);
dillon min61367d02020-05-25 11:45:47 +08001420 } else if (comm_type == SPI_3WIRE_RX) {
1421 stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1422 STM32F4_SPI_CR1_BIDIMODE);
1423 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1424 STM32F4_SPI_CR1_BIDIOE);
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001425 } else {
1426 return -EINVAL;
1427 }
1428
1429 return 0;
1430}
1431
1432/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001433 * stm32h7_spi_set_mode - configure communication mode
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001434 * @spi: pointer to the spi controller data structure
1435 * @comm_type: type of communication to configure
1436 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001437static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001438{
1439 u32 mode;
1440 u32 cfg2_clrb = 0, cfg2_setb = 0;
1441
1442 if (comm_type == SPI_3WIRE_RX) {
1443 mode = STM32H7_SPI_HALF_DUPLEX;
1444 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1445 } else if (comm_type == SPI_3WIRE_TX) {
1446 mode = STM32H7_SPI_HALF_DUPLEX;
1447 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1448 } else if (comm_type == SPI_SIMPLEX_RX) {
1449 mode = STM32H7_SPI_SIMPLEX_RX;
1450 } else if (comm_type == SPI_SIMPLEX_TX) {
1451 mode = STM32H7_SPI_SIMPLEX_TX;
1452 } else {
1453 mode = STM32H7_SPI_FULL_DUPLEX;
1454 }
1455
1456 cfg2_clrb |= STM32H7_SPI_CFG2_COMM;
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001457 cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_COMM, mode);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001458
1459 writel_relaxed(
1460 (readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1461 ~cfg2_clrb) | cfg2_setb,
1462 spi->base + STM32H7_SPI_CFG2);
1463
1464 return 0;
1465}
1466
1467/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001468 * stm32h7_spi_data_idleness - configure minimum time delay inserted between two
1469 * consecutive data frames in master mode
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001470 * @spi: pointer to the spi controller data structure
1471 * @len: transfer len
1472 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001473static void stm32h7_spi_data_idleness(struct stm32_spi *spi, u32 len)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001474{
1475 u32 cfg2_clrb = 0, cfg2_setb = 0;
1476
1477 cfg2_clrb |= STM32H7_SPI_CFG2_MIDI;
1478 if ((len > 1) && (spi->cur_midi > 0)) {
Amelie Delaunaye1e20932021-02-05 19:59:31 +01001479 u32 sck_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->cur_speed);
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001480 u32 midi = min_t(u32,
1481 DIV_ROUND_UP(spi->cur_midi, sck_period_ns),
1482 FIELD_GET(STM32H7_SPI_CFG2_MIDI,
1483 STM32H7_SPI_CFG2_MIDI));
1484
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001485
1486 dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
1487 sck_period_ns, midi, midi * sck_period_ns);
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001488 cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_MIDI, midi);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001489 }
1490
1491 writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1492 ~cfg2_clrb) | cfg2_setb,
1493 spi->base + STM32H7_SPI_CFG2);
1494}
1495
1496/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001497 * stm32h7_spi_number_of_data - configure number of data at current transfer
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001498 * @spi: pointer to the spi controller data structure
Alain Volmat1c52be82020-03-20 14:44:17 +01001499 * @nb_words: transfer length (in words)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001500 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001501static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001502{
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001503 if (nb_words <= STM32H7_SPI_TSIZE_MAX) {
1504 writel_relaxed(FIELD_PREP(STM32H7_SPI_CR2_TSIZE, nb_words),
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001505 spi->base + STM32H7_SPI_CR2);
1506 } else {
1507 return -EMSGSIZE;
1508 }
1509
1510 return 0;
1511}
1512
1513/**
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001514 * stm32_spi_transfer_one_setup - common setup to transfer a single
1515 * spi_transfer either using DMA or
1516 * interrupts.
Alain Volmat1c52be82020-03-20 14:44:17 +01001517 * @spi: pointer to the spi controller data structure
1518 * @spi_dev: pointer to the spi device
1519 * @transfer: pointer to spi transfer
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001520 */
1521static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
1522 struct spi_device *spi_dev,
1523 struct spi_transfer *transfer)
1524{
1525 unsigned long flags;
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001526 unsigned int comm_type;
1527 int nb_words, ret = 0;
Alain Volmat60ccb352020-08-10 09:12:38 +02001528 int mbr;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001529
1530 spin_lock_irqsave(&spi->lock, flags);
1531
Amelie Delaunay3373e902020-08-10 09:12:35 +02001532 spi->cur_xferlen = transfer->len;
1533
Alain Volmat60ccb352020-08-10 09:12:38 +02001534 spi->cur_bpw = transfer->bits_per_word;
1535 spi->cfg->set_bpw(spi);
1536
1537 /* Update spi->cur_speed with real clock speed */
1538 mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz,
1539 spi->cfg->baud_rate_div_min,
1540 spi->cfg->baud_rate_div_max);
1541 if (mbr < 0) {
1542 ret = mbr;
1543 goto out;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001544 }
1545
Alain Volmat60ccb352020-08-10 09:12:38 +02001546 transfer->speed_hz = spi->cur_speed;
1547 stm32_spi_set_mbr(spi, mbr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001548
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001549 comm_type = stm32_spi_communication_type(spi_dev, transfer);
Alain Volmat60ccb352020-08-10 09:12:38 +02001550 ret = spi->cfg->set_mode(spi, comm_type);
1551 if (ret < 0)
1552 goto out;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001553
Alain Volmat60ccb352020-08-10 09:12:38 +02001554 spi->cur_comm = comm_type;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001555
Cezary Gapinski55166852018-12-24 23:00:37 +01001556 if (spi->cfg->set_data_idleness)
1557 spi->cfg->set_data_idleness(spi, transfer->len);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001558
Amelie Delaunay128ebb82017-06-27 17:45:17 +02001559 if (spi->cur_bpw <= 8)
1560 nb_words = transfer->len;
1561 else if (spi->cur_bpw <= 16)
1562 nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
1563 else
1564 nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001565
Cezary Gapinski55166852018-12-24 23:00:37 +01001566 if (spi->cfg->set_number_of_data) {
1567 ret = spi->cfg->set_number_of_data(spi, nb_words);
1568 if (ret < 0)
1569 goto out;
1570 }
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001571
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001572 dev_dbg(spi->dev, "transfer communication mode set to %d\n",
1573 spi->cur_comm);
1574 dev_dbg(spi->dev,
1575 "data frame of %d-bit, data packet of %d data frames\n",
1576 spi->cur_bpw, spi->cur_fthlv);
1577 dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
1578 dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
1579 spi->cur_xferlen, nb_words);
1580 dev_dbg(spi->dev, "dma %s\n",
1581 (spi->cur_usedma) ? "enabled" : "disabled");
1582
1583out:
1584 spin_unlock_irqrestore(&spi->lock, flags);
1585
1586 return ret;
1587}
1588
1589/**
1590 * stm32_spi_transfer_one - transfer a single spi_transfer
Alain Volmat1c52be82020-03-20 14:44:17 +01001591 * @master: controller master interface
1592 * @spi_dev: pointer to the spi device
1593 * @transfer: pointer to spi transfer
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001594 *
1595 * It must return 0 if the transfer is finished or 1 if the transfer is still
1596 * in progress.
1597 */
1598static int stm32_spi_transfer_one(struct spi_master *master,
1599 struct spi_device *spi_dev,
1600 struct spi_transfer *transfer)
1601{
1602 struct stm32_spi *spi = spi_master_get_devdata(master);
1603 int ret;
1604
1605 spi->tx_buf = transfer->tx_buf;
1606 spi->rx_buf = transfer->rx_buf;
1607 spi->tx_len = spi->tx_buf ? transfer->len : 0;
1608 spi->rx_len = spi->rx_buf ? transfer->len : 0;
1609
Amelie Delaunayc67ad362017-06-27 17:45:19 +02001610 spi->cur_usedma = (master->can_dma &&
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001611 master->can_dma(master, spi_dev, transfer));
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001612
1613 ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
1614 if (ret) {
1615 dev_err(spi->dev, "SPI transfer setup failed\n");
1616 return ret;
1617 }
1618
1619 if (spi->cur_usedma)
1620 return stm32_spi_transfer_one_dma(spi, transfer);
1621 else
Cezary Gapinski55166852018-12-24 23:00:37 +01001622 return spi->cfg->transfer_one_irq(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001623}
1624
1625/**
1626 * stm32_spi_unprepare_msg - relax the hardware
Alain Volmat1c52be82020-03-20 14:44:17 +01001627 * @master: controller master interface
1628 * @msg: pointer to the spi message
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001629 */
1630static int stm32_spi_unprepare_msg(struct spi_master *master,
1631 struct spi_message *msg)
1632{
1633 struct stm32_spi *spi = spi_master_get_devdata(master);
1634
Cezary Gapinski55166852018-12-24 23:00:37 +01001635 spi->cfg->disable(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001636
1637 return 0;
1638}
1639
1640/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001641 * stm32f4_spi_config - Configure SPI controller as SPI master
Alain Volmat1c52be82020-03-20 14:44:17 +01001642 * @spi: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001643 */
1644static int stm32f4_spi_config(struct stm32_spi *spi)
1645{
1646 unsigned long flags;
1647
1648 spin_lock_irqsave(&spi->lock, flags);
1649
1650 /* Ensure I2SMOD bit is kept cleared */
1651 stm32_spi_clr_bits(spi, STM32F4_SPI_I2SCFGR,
1652 STM32F4_SPI_I2SCFGR_I2SMOD);
1653
1654 /*
1655 * - SS input value high
1656 * - transmitter half duplex direction
1657 * - Set the master mode (default Motorola mode)
1658 * - Consider 1 master/n slaves configuration and
1659 * SS input value is determined by the SSI bit
1660 */
1661 stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SSI |
1662 STM32F4_SPI_CR1_BIDIOE |
1663 STM32F4_SPI_CR1_MSTR |
1664 STM32F4_SPI_CR1_SSM);
1665
1666 spin_unlock_irqrestore(&spi->lock, flags);
1667
1668 return 0;
1669}
1670
1671/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001672 * stm32h7_spi_config - Configure SPI controller as SPI master
Alain Volmat1c52be82020-03-20 14:44:17 +01001673 * @spi: pointer to the spi controller data structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001674 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001675static int stm32h7_spi_config(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001676{
1677 unsigned long flags;
1678
1679 spin_lock_irqsave(&spi->lock, flags);
1680
1681 /* Ensure I2SMOD bit is kept cleared */
Cezary Gapinski86026632018-12-24 23:00:33 +01001682 stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
1683 STM32H7_SPI_I2SCFGR_I2SMOD);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001684
1685 /*
1686 * - SS input value high
1687 * - transmitter half duplex direction
1688 * - automatic communication suspend when RX-Fifo is full
1689 */
Cezary Gapinski86026632018-12-24 23:00:33 +01001690 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SSI |
1691 STM32H7_SPI_CR1_HDDIR |
1692 STM32H7_SPI_CR1_MASRX);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001693
1694 /*
1695 * - Set the master mode (default Motorola mode)
1696 * - Consider 1 master/n slaves configuration and
1697 * SS input value is determined by the SSI bit
1698 * - keep control of all associated GPIOs
1699 */
Cezary Gapinski86026632018-12-24 23:00:33 +01001700 stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_MASTER |
1701 STM32H7_SPI_CFG2_SSM |
1702 STM32H7_SPI_CFG2_AFCNTR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001703
1704 spin_unlock_irqrestore(&spi->lock, flags);
1705
1706 return 0;
1707}
1708
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001709static const struct stm32_spi_cfg stm32f4_spi_cfg = {
1710 .regs = &stm32f4_spi_regspec,
1711 .get_bpw_mask = stm32f4_spi_get_bpw_mask,
1712 .disable = stm32f4_spi_disable,
1713 .config = stm32f4_spi_config,
1714 .set_bpw = stm32f4_spi_set_bpw,
1715 .set_mode = stm32f4_spi_set_mode,
1716 .transfer_one_dma_start = stm32f4_spi_transfer_one_dma_start,
1717 .dma_tx_cb = stm32f4_spi_dma_tx_cb,
Alain Volmat7ceb0b82021-07-07 10:27:06 +02001718 .dma_rx_cb = stm32_spi_dma_rx_cb,
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001719 .transfer_one_irq = stm32f4_spi_transfer_one_irq,
1720 .irq_handler_event = stm32f4_spi_irq_event,
1721 .irq_handler_thread = stm32f4_spi_irq_thread,
1722 .baud_rate_div_min = STM32F4_SPI_BR_DIV_MIN,
1723 .baud_rate_div_max = STM32F4_SPI_BR_DIV_MAX,
1724 .has_fifo = false,
1725};
1726
Cezary Gapinski55166852018-12-24 23:00:37 +01001727static const struct stm32_spi_cfg stm32h7_spi_cfg = {
1728 .regs = &stm32h7_spi_regspec,
1729 .get_fifo_size = stm32h7_spi_get_fifo_size,
1730 .get_bpw_mask = stm32h7_spi_get_bpw_mask,
1731 .disable = stm32h7_spi_disable,
1732 .config = stm32h7_spi_config,
1733 .set_bpw = stm32h7_spi_set_bpw,
1734 .set_mode = stm32h7_spi_set_mode,
1735 .set_data_idleness = stm32h7_spi_data_idleness,
1736 .set_number_of_data = stm32h7_spi_number_of_data,
1737 .transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
Alain Volmat7ceb0b82021-07-07 10:27:06 +02001738 .dma_rx_cb = stm32_spi_dma_rx_cb,
1739 /*
1740 * dma_tx_cb is not necessary since in case of TX, dma is followed by
1741 * SPI access hence handling is performed within the SPI interrupt
1742 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001743 .transfer_one_irq = stm32h7_spi_transfer_one_irq,
1744 .irq_handler_thread = stm32h7_spi_irq_thread,
1745 .baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
1746 .baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
1747 .has_fifo = true,
1748};
1749
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001750static const struct of_device_id stm32_spi_of_match[] = {
Cezary Gapinski55166852018-12-24 23:00:37 +01001751 { .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg },
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001752 { .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg },
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001753 {},
1754};
1755MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
1756
1757static int stm32_spi_probe(struct platform_device *pdev)
1758{
1759 struct spi_master *master;
1760 struct stm32_spi *spi;
1761 struct resource *res;
Etienne Carriere1c75cfd2021-02-05 19:59:29 +01001762 struct reset_control *rst;
Linus Walleij8a6553e2019-12-05 09:34:01 +01001763 int ret;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001764
Alain Volmat79c62462021-03-18 08:24:50 +01001765 master = devm_spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi));
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001766 if (!master) {
1767 dev_err(&pdev->dev, "spi master allocation failed\n");
1768 return -ENOMEM;
1769 }
1770 platform_set_drvdata(pdev, master);
1771
1772 spi = spi_master_get_devdata(master);
1773 spi->dev = &pdev->dev;
1774 spi->master = master;
1775 spin_lock_init(&spi->lock);
1776
Cezary Gapinski55166852018-12-24 23:00:37 +01001777 spi->cfg = (const struct stm32_spi_cfg *)
1778 of_match_device(pdev->dev.driver->of_match_table,
1779 &pdev->dev)->data;
1780
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001781 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1782 spi->base = devm_ioremap_resource(&pdev->dev, res);
Alain Volmat79c62462021-03-18 08:24:50 +01001783 if (IS_ERR(spi->base))
1784 return PTR_ERR(spi->base);
Cezary Gapinski55166852018-12-24 23:00:37 +01001785
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001786 spi->phys_addr = (dma_addr_t)res->start;
1787
1788 spi->irq = platform_get_irq(pdev, 0);
Alain Volmat79c62462021-03-18 08:24:50 +01001789 if (spi->irq <= 0)
1790 return dev_err_probe(&pdev->dev, spi->irq,
1791 "failed to get irq\n");
1792
Cezary Gapinski55166852018-12-24 23:00:37 +01001793 ret = devm_request_threaded_irq(&pdev->dev, spi->irq,
1794 spi->cfg->irq_handler_event,
1795 spi->cfg->irq_handler_thread,
1796 IRQF_ONESHOT, pdev->name, master);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001797 if (ret) {
1798 dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
1799 ret);
Alain Volmat79c62462021-03-18 08:24:50 +01001800 return ret;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001801 }
1802
Cezary Gapinskid4c91342018-12-24 23:00:28 +01001803 spi->clk = devm_clk_get(&pdev->dev, NULL);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001804 if (IS_ERR(spi->clk)) {
1805 ret = PTR_ERR(spi->clk);
1806 dev_err(&pdev->dev, "clk get failed: %d\n", ret);
Alain Volmat79c62462021-03-18 08:24:50 +01001807 return ret;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001808 }
1809
1810 ret = clk_prepare_enable(spi->clk);
1811 if (ret) {
1812 dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
Alain Volmat79c62462021-03-18 08:24:50 +01001813 return ret;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001814 }
1815 spi->clk_rate = clk_get_rate(spi->clk);
1816 if (!spi->clk_rate) {
1817 dev_err(&pdev->dev, "clk rate = 0\n");
1818 ret = -EINVAL;
Alexey Khoroshilov3dbb3ee2018-03-30 22:54:44 +03001819 goto err_clk_disable;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001820 }
1821
Alain Volmatc63b95b2021-02-05 19:59:30 +01001822 rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
1823 if (rst) {
1824 if (IS_ERR(rst)) {
1825 ret = dev_err_probe(&pdev->dev, PTR_ERR(rst),
1826 "failed to get reset\n");
1827 goto err_clk_disable;
1828 }
1829
Etienne Carriere1c75cfd2021-02-05 19:59:29 +01001830 reset_control_assert(rst);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001831 udelay(2);
Etienne Carriere1c75cfd2021-02-05 19:59:29 +01001832 reset_control_deassert(rst);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001833 }
1834
Cezary Gapinski55166852018-12-24 23:00:37 +01001835 if (spi->cfg->has_fifo)
1836 spi->fifo_size = spi->cfg->get_fifo_size(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001837
Cezary Gapinski55166852018-12-24 23:00:37 +01001838 ret = spi->cfg->config(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001839 if (ret) {
1840 dev_err(&pdev->dev, "controller configuration failed: %d\n",
1841 ret);
1842 goto err_clk_disable;
1843 }
1844
1845 master->dev.of_node = pdev->dev.of_node;
1846 master->auto_runtime_pm = true;
1847 master->bus_num = pdev->id;
Cezary Gapinskid6cea112018-12-24 23:00:31 +01001848 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
Cezary Gapinski6962b052018-12-24 23:00:32 +01001849 SPI_3WIRE;
Cezary Gapinski55166852018-12-24 23:00:37 +01001850 master->bits_per_word_mask = spi->cfg->get_bpw_mask(spi);
1851 master->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min;
1852 master->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max;
Linus Walleij8a6553e2019-12-05 09:34:01 +01001853 master->use_gpio_descriptors = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001854 master->prepare_message = stm32_spi_prepare_msg;
1855 master->transfer_one = stm32_spi_transfer_one;
1856 master->unprepare_message = stm32_spi_unprepare_msg;
dillon min61367d02020-05-25 11:45:47 +08001857 master->flags = SPI_MASTER_MUST_TX;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001858
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001859 spi->dma_tx = dma_request_chan(spi->dev, "tx");
1860 if (IS_ERR(spi->dma_tx)) {
1861 ret = PTR_ERR(spi->dma_tx);
1862 spi->dma_tx = NULL;
1863 if (ret == -EPROBE_DEFER)
1864 goto err_clk_disable;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001865
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001866 dev_warn(&pdev->dev, "failed to request tx dma channel\n");
1867 } else {
1868 master->dma_tx = spi->dma_tx;
1869 }
1870
1871 spi->dma_rx = dma_request_chan(spi->dev, "rx");
1872 if (IS_ERR(spi->dma_rx)) {
1873 ret = PTR_ERR(spi->dma_rx);
1874 spi->dma_rx = NULL;
1875 if (ret == -EPROBE_DEFER)
1876 goto err_dma_release;
1877
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001878 dev_warn(&pdev->dev, "failed to request rx dma channel\n");
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001879 } else {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001880 master->dma_rx = spi->dma_rx;
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001881 }
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001882
1883 if (spi->dma_tx || spi->dma_rx)
1884 master->can_dma = stm32_spi_can_dma;
1885
Alain Volmat9d535412021-07-07 10:27:01 +02001886 pm_runtime_set_autosuspend_delay(&pdev->dev,
1887 STM32_SPI_AUTOSUSPEND_DELAY);
1888 pm_runtime_use_autosuspend(&pdev->dev);
Amelie Delaunay038ac862017-06-27 17:45:18 +02001889 pm_runtime_set_active(&pdev->dev);
Alain Volmat7999d252021-07-07 10:27:00 +02001890 pm_runtime_get_noresume(&pdev->dev);
Amelie Delaunay038ac862017-06-27 17:45:18 +02001891 pm_runtime_enable(&pdev->dev);
1892
Antonio Borneo8d559a62021-03-12 11:34:46 +01001893 ret = spi_register_master(master);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001894 if (ret) {
1895 dev_err(&pdev->dev, "spi master registration failed: %d\n",
1896 ret);
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001897 goto err_pm_disable;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001898 }
1899
Alain Volmat9d535412021-07-07 10:27:01 +02001900 pm_runtime_mark_last_busy(&pdev->dev);
1901 pm_runtime_put_autosuspend(&pdev->dev);
1902
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001903 dev_info(&pdev->dev, "driver initialized\n");
1904
1905 return 0;
1906
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001907err_pm_disable:
1908 pm_runtime_disable(&pdev->dev);
Alain Volmat7999d252021-07-07 10:27:00 +02001909 pm_runtime_put_noidle(&pdev->dev);
1910 pm_runtime_set_suspended(&pdev->dev);
Alain Volmat9d535412021-07-07 10:27:01 +02001911 pm_runtime_dont_use_autosuspend(&pdev->dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001912err_dma_release:
1913 if (spi->dma_tx)
1914 dma_release_channel(spi->dma_tx);
1915 if (spi->dma_rx)
1916 dma_release_channel(spi->dma_rx);
1917err_clk_disable:
1918 clk_disable_unprepare(spi->clk);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001919
1920 return ret;
1921}
1922
1923static int stm32_spi_remove(struct platform_device *pdev)
1924{
1925 struct spi_master *master = platform_get_drvdata(pdev);
1926 struct stm32_spi *spi = spi_master_get_devdata(master);
1927
Alain Volmat7999d252021-07-07 10:27:00 +02001928 pm_runtime_get_sync(&pdev->dev);
1929
Antonio Borneo8d559a62021-03-12 11:34:46 +01001930 spi_unregister_master(master);
Cezary Gapinski55166852018-12-24 23:00:37 +01001931 spi->cfg->disable(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001932
Alain Volmat7999d252021-07-07 10:27:00 +02001933 pm_runtime_disable(&pdev->dev);
1934 pm_runtime_put_noidle(&pdev->dev);
1935 pm_runtime_set_suspended(&pdev->dev);
Alain Volmat9d535412021-07-07 10:27:01 +02001936 pm_runtime_dont_use_autosuspend(&pdev->dev);
1937
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001938 if (master->dma_tx)
1939 dma_release_channel(master->dma_tx);
1940 if (master->dma_rx)
1941 dma_release_channel(master->dma_rx);
1942
1943 clk_disable_unprepare(spi->clk);
1944
Amelie Delaunay038ac862017-06-27 17:45:18 +02001945
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02001946 pinctrl_pm_select_sleep_state(&pdev->dev);
1947
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001948 return 0;
1949}
1950
Alain Volmat12ef51b2021-03-12 11:35:29 +01001951static int __maybe_unused stm32_spi_runtime_suspend(struct device *dev)
Amelie Delaunay038ac862017-06-27 17:45:18 +02001952{
1953 struct spi_master *master = dev_get_drvdata(dev);
1954 struct stm32_spi *spi = spi_master_get_devdata(master);
1955
1956 clk_disable_unprepare(spi->clk);
1957
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02001958 return pinctrl_pm_select_sleep_state(dev);
Amelie Delaunay038ac862017-06-27 17:45:18 +02001959}
1960
Alain Volmat12ef51b2021-03-12 11:35:29 +01001961static int __maybe_unused stm32_spi_runtime_resume(struct device *dev)
Amelie Delaunay038ac862017-06-27 17:45:18 +02001962{
1963 struct spi_master *master = dev_get_drvdata(dev);
1964 struct stm32_spi *spi = spi_master_get_devdata(master);
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02001965 int ret;
1966
1967 ret = pinctrl_pm_select_default_state(dev);
1968 if (ret)
1969 return ret;
Amelie Delaunay038ac862017-06-27 17:45:18 +02001970
1971 return clk_prepare_enable(spi->clk);
1972}
Amelie Delaunay038ac862017-06-27 17:45:18 +02001973
Alain Volmat12ef51b2021-03-12 11:35:29 +01001974static int __maybe_unused stm32_spi_suspend(struct device *dev)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001975{
1976 struct spi_master *master = dev_get_drvdata(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001977 int ret;
1978
1979 ret = spi_master_suspend(master);
1980 if (ret)
1981 return ret;
1982
Amelie Delaunay038ac862017-06-27 17:45:18 +02001983 return pm_runtime_force_suspend(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001984}
1985
Alain Volmat12ef51b2021-03-12 11:35:29 +01001986static int __maybe_unused stm32_spi_resume(struct device *dev)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001987{
1988 struct spi_master *master = dev_get_drvdata(dev);
1989 struct stm32_spi *spi = spi_master_get_devdata(master);
1990 int ret;
1991
Amelie Delaunay038ac862017-06-27 17:45:18 +02001992 ret = pm_runtime_force_resume(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001993 if (ret)
1994 return ret;
Amelie Delaunay038ac862017-06-27 17:45:18 +02001995
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001996 ret = spi_master_resume(master);
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02001997 if (ret) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001998 clk_disable_unprepare(spi->clk);
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02001999 return ret;
2000 }
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002001
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02002002 ret = pm_runtime_get_sync(dev);
Dan Carpenterc170a5a2020-09-09 12:43:04 +03002003 if (ret < 0) {
Zhang Qilong900ccdc2020-11-06 09:52:17 +08002004 pm_runtime_put_noidle(dev);
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02002005 dev_err(dev, "Unable to power device:%d\n", ret);
2006 return ret;
2007 }
2008
2009 spi->cfg->config(spi);
2010
2011 pm_runtime_mark_last_busy(dev);
2012 pm_runtime_put_autosuspend(dev);
2013
2014 return 0;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002015}
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002016
Amelie Delaunay038ac862017-06-27 17:45:18 +02002017static const struct dev_pm_ops stm32_spi_pm_ops = {
2018 SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
2019 SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend,
2020 stm32_spi_runtime_resume, NULL)
2021};
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002022
2023static struct platform_driver stm32_spi_driver = {
2024 .probe = stm32_spi_probe,
2025 .remove = stm32_spi_remove,
2026 .driver = {
2027 .name = DRIVER_NAME,
2028 .pm = &stm32_spi_pm_ops,
2029 .of_match_table = stm32_spi_of_match,
2030 },
2031};
2032
2033module_platform_driver(stm32_spi_driver);
2034
2035MODULE_ALIAS("platform:" DRIVER_NAME);
2036MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
2037MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
2038MODULE_LICENSE("GPL v2");