blob: 05618a618939c0114094aa0ee7340c93abcb9eef [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
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100165/*
166 * use PIO for small transfers, avoiding DMA setup/teardown overhead for drivers
167 * without fifo buffers.
168 */
169#define SPI_DMA_MIN_BYTES 16
170
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200171/**
Alain Volmat1c52be82020-03-20 14:44:17 +0100172 * struct stm32_spi_reg - stm32 SPI register & bitfield desc
Cezary Gapinski55166852018-12-24 23:00:37 +0100173 * @reg: register offset
174 * @mask: bitfield mask
175 * @shift: left shift
176 */
177struct stm32_spi_reg {
178 int reg;
179 int mask;
180 int shift;
181};
182
183/**
Alain Volmat1c52be82020-03-20 14:44:17 +0100184 * struct stm32_spi_regspec - stm32 registers definition, compatible dependent data
185 * @en: enable register and SPI enable bit
186 * @dma_rx_en: SPI DMA RX enable register end SPI DMA RX enable bit
187 * @dma_tx_en: SPI DMA TX enable register end SPI DMA TX enable bit
188 * @cpol: clock polarity register and polarity bit
189 * @cpha: clock phase register and phase bit
190 * @lsb_first: LSB transmitted first register and bit
191 * @br: baud rate register and bitfields
192 * @rx: SPI RX data register
193 * @tx: SPI TX data register
Cezary Gapinski55166852018-12-24 23:00:37 +0100194 */
195struct stm32_spi_regspec {
196 const struct stm32_spi_reg en;
197 const struct stm32_spi_reg dma_rx_en;
198 const struct stm32_spi_reg dma_tx_en;
199 const struct stm32_spi_reg cpol;
200 const struct stm32_spi_reg cpha;
201 const struct stm32_spi_reg lsb_first;
202 const struct stm32_spi_reg br;
203 const struct stm32_spi_reg rx;
204 const struct stm32_spi_reg tx;
205};
206
207struct stm32_spi;
208
209/**
Alain Volmat1c52be82020-03-20 14:44:17 +0100210 * struct stm32_spi_cfg - stm32 compatible configuration data
Cezary Gapinski55166852018-12-24 23:00:37 +0100211 * @regs: registers descriptions
212 * @get_fifo_size: routine to get fifo size
213 * @get_bpw_mask: routine to get bits per word mask
214 * @disable: routine to disable controller
215 * @config: routine to configure controller as SPI Master
216 * @set_bpw: routine to configure registers to for bits per word
217 * @set_mode: routine to configure registers to desired mode
218 * @set_data_idleness: optional routine to configure registers to desired idle
219 * time between frames (if driver has this functionality)
Alain Volmat1c52be82020-03-20 14:44:17 +0100220 * @set_number_of_data: optional routine to configure registers to desired
Cezary Gapinski55166852018-12-24 23:00:37 +0100221 * number of data (if driver has this functionality)
222 * @can_dma: routine to determine if the transfer is eligible for DMA use
223 * @transfer_one_dma_start: routine to start transfer a single spi_transfer
224 * using DMA
Alain Volmat1c52be82020-03-20 14:44:17 +0100225 * @dma_rx_cb: routine to call after DMA RX channel operation is complete
226 * @dma_tx_cb: routine to call after DMA TX channel operation is complete
Cezary Gapinski55166852018-12-24 23:00:37 +0100227 * @transfer_one_irq: routine to configure interrupts for driver
228 * @irq_handler_event: Interrupt handler for SPI controller events
229 * @irq_handler_thread: thread of interrupt handler for SPI controller
230 * @baud_rate_div_min: minimum baud rate divisor
231 * @baud_rate_div_max: maximum baud rate divisor
232 * @has_fifo: boolean to know if fifo is used for driver
233 * @has_startbit: boolean to know if start bit is used to start transfer
234 */
235struct stm32_spi_cfg {
236 const struct stm32_spi_regspec *regs;
237 int (*get_fifo_size)(struct stm32_spi *spi);
238 int (*get_bpw_mask)(struct stm32_spi *spi);
239 void (*disable)(struct stm32_spi *spi);
240 int (*config)(struct stm32_spi *spi);
241 void (*set_bpw)(struct stm32_spi *spi);
242 int (*set_mode)(struct stm32_spi *spi, unsigned int comm_type);
243 void (*set_data_idleness)(struct stm32_spi *spi, u32 length);
244 int (*set_number_of_data)(struct stm32_spi *spi, u32 length);
245 void (*transfer_one_dma_start)(struct stm32_spi *spi);
246 void (*dma_rx_cb)(void *data);
247 void (*dma_tx_cb)(void *data);
248 int (*transfer_one_irq)(struct stm32_spi *spi);
249 irqreturn_t (*irq_handler_event)(int irq, void *dev_id);
250 irqreturn_t (*irq_handler_thread)(int irq, void *dev_id);
251 unsigned int baud_rate_div_min;
252 unsigned int baud_rate_div_max;
253 bool has_fifo;
254};
255
256/**
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200257 * struct stm32_spi - private data of the SPI controller
258 * @dev: driver model representation of the controller
259 * @master: controller master interface
Cezary Gapinski55166852018-12-24 23:00:37 +0100260 * @cfg: compatible configuration data
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200261 * @base: virtual memory area
262 * @clk: hw kernel clock feeding the SPI clock generator
263 * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200264 * @lock: prevent I/O concurrent access
265 * @irq: SPI controller interrupt line
266 * @fifo_size: size of the embedded fifo in bytes
267 * @cur_midi: master inter-data idleness in ns
268 * @cur_speed: speed configured in Hz
269 * @cur_bpw: number of bits in a single SPI data frame
270 * @cur_fthlv: fifo threshold level (data frames in a single data packet)
271 * @cur_comm: SPI communication mode
272 * @cur_xferlen: current transfer length in bytes
273 * @cur_usedma: boolean to know if dma is used in current transfer
274 * @tx_buf: data to be written, or NULL
275 * @rx_buf: data to be read, or NULL
276 * @tx_len: number of data to be written in bytes
277 * @rx_len: number of data to be read in bytes
278 * @dma_tx: dma channel for TX transfer
279 * @dma_rx: dma channel for RX transfer
280 * @phys_addr: SPI registers physical base address
281 */
282struct stm32_spi {
283 struct device *dev;
284 struct spi_master *master;
Cezary Gapinski55166852018-12-24 23:00:37 +0100285 const struct stm32_spi_cfg *cfg;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200286 void __iomem *base;
287 struct clk *clk;
288 u32 clk_rate;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200289 spinlock_t lock; /* prevent I/O concurrent access */
290 int irq;
291 unsigned int fifo_size;
292
293 unsigned int cur_midi;
294 unsigned int cur_speed;
295 unsigned int cur_bpw;
296 unsigned int cur_fthlv;
297 unsigned int cur_comm;
298 unsigned int cur_xferlen;
299 bool cur_usedma;
300
301 const void *tx_buf;
302 void *rx_buf;
303 int tx_len;
304 int rx_len;
305 struct dma_chan *dma_tx;
306 struct dma_chan *dma_rx;
307 dma_addr_t phys_addr;
308};
309
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100310static const struct stm32_spi_regspec stm32f4_spi_regspec = {
311 .en = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE },
312
313 .dma_rx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_RXDMAEN },
314 .dma_tx_en = { STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN },
315
316 .cpol = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPOL },
317 .cpha = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPHA },
318 .lsb_first = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_LSBFRST },
319 .br = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_BR, STM32F4_SPI_CR1_BR_SHIFT },
320
321 .rx = { STM32F4_SPI_DR },
322 .tx = { STM32F4_SPI_DR },
323};
324
Cezary Gapinski55166852018-12-24 23:00:37 +0100325static const struct stm32_spi_regspec stm32h7_spi_regspec = {
326 /* SPI data transfer is enabled but spi_ker_ck is idle.
327 * CFG1 and CFG2 registers are write protected when SPE is enabled.
328 */
329 .en = { STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE },
330
331 .dma_rx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_RXDMAEN },
332 .dma_tx_en = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN },
333
334 .cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL },
335 .cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA },
336 .lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST },
337 .br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR,
338 STM32H7_SPI_CFG1_MBR_SHIFT },
339
340 .rx = { STM32H7_SPI_RXDR },
341 .tx = { STM32H7_SPI_TXDR },
342};
343
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200344static inline void stm32_spi_set_bits(struct stm32_spi *spi,
345 u32 offset, u32 bits)
346{
347 writel_relaxed(readl_relaxed(spi->base + offset) | bits,
348 spi->base + offset);
349}
350
351static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
352 u32 offset, u32 bits)
353{
354 writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
355 spi->base + offset);
356}
357
358/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100359 * stm32h7_spi_get_fifo_size - Return fifo size
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200360 * @spi: pointer to the spi controller data structure
361 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100362static int stm32h7_spi_get_fifo_size(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200363{
364 unsigned long flags;
365 u32 count = 0;
366
367 spin_lock_irqsave(&spi->lock, flags);
368
Cezary Gapinski86026632018-12-24 23:00:33 +0100369 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200370
Cezary Gapinski86026632018-12-24 23:00:33 +0100371 while (readl_relaxed(spi->base + STM32H7_SPI_SR) & STM32H7_SPI_SR_TXP)
372 writeb_relaxed(++count, spi->base + STM32H7_SPI_TXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200373
Cezary Gapinski86026632018-12-24 23:00:33 +0100374 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200375
376 spin_unlock_irqrestore(&spi->lock, flags);
377
378 dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
379
380 return count;
381}
382
383/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100384 * stm32f4_spi_get_bpw_mask - Return bits per word mask
385 * @spi: pointer to the spi controller data structure
386 */
387static int stm32f4_spi_get_bpw_mask(struct stm32_spi *spi)
388{
389 dev_dbg(spi->dev, "8-bit or 16-bit data frame supported\n");
390 return SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
391}
392
393/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100394 * stm32h7_spi_get_bpw_mask - Return bits per word mask
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200395 * @spi: pointer to the spi controller data structure
396 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100397static int stm32h7_spi_get_bpw_mask(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200398{
399 unsigned long flags;
400 u32 cfg1, max_bpw;
401
402 spin_lock_irqsave(&spi->lock, flags);
403
404 /*
405 * The most significant bit at DSIZE bit field is reserved when the
406 * maximum data size of periperal instances is limited to 16-bit
407 */
Cezary Gapinski86026632018-12-24 23:00:33 +0100408 stm32_spi_set_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_DSIZE);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200409
Cezary Gapinski86026632018-12-24 23:00:33 +0100410 cfg1 = readl_relaxed(spi->base + STM32H7_SPI_CFG1);
Amelie Delaunay5a380b82021-02-05 19:59:27 +0100411 max_bpw = FIELD_GET(STM32H7_SPI_CFG1_DSIZE, cfg1) + 1;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200412
413 spin_unlock_irqrestore(&spi->lock, flags);
414
415 dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
416
417 return SPI_BPW_RANGE_MASK(4, max_bpw);
418}
419
420/**
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100421 * stm32_spi_prepare_mbr - Determine baud rate divisor value
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200422 * @spi: pointer to the spi controller data structure
423 * @speed_hz: requested speed
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100424 * @min_div: minimum baud rate divisor
425 * @max_div: maximum baud rate divisor
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200426 *
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100427 * Return baud rate divisor value in case of success or -EINVAL
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200428 */
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100429static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
430 u32 min_div, u32 max_div)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200431{
432 u32 div, mbrdiv;
433
Amelie Delaunay9cc61972020-08-10 09:12:36 +0200434 /* Ensure spi->clk_rate is even */
435 div = DIV_ROUND_UP(spi->clk_rate & ~0x1, speed_hz);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200436
437 /*
438 * SPI framework set xfer->speed_hz to master->max_speed_hz if
439 * xfer->speed_hz is greater than master->max_speed_hz, and it returns
440 * an error when xfer->speed_hz is lower than master->min_speed_hz, so
441 * no need to check it there.
442 * However, we need to ensure the following calculations.
443 */
Cezary Gapinski9d5fce12018-12-24 23:00:35 +0100444 if ((div < min_div) || (div > max_div))
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200445 return -EINVAL;
446
447 /* Determine the first power of 2 greater than or equal to div */
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200448 if (div & (div - 1))
449 mbrdiv = fls(div);
450 else
451 mbrdiv = fls(div) - 1;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200452
453 spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
454
455 return mbrdiv - 1;
456}
457
458/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100459 * stm32h7_spi_prepare_fthlv - Determine FIFO threshold level
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200460 * @spi: pointer to the spi controller data structure
Amelie Delaunay3373e902020-08-10 09:12:35 +0200461 * @xfer_len: length of the message to be transferred
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200462 */
Amelie Delaunay3373e902020-08-10 09:12:35 +0200463static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi, u32 xfer_len)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200464{
Marek Vasut970e8ea2021-01-04 13:31:14 +0100465 u32 packet, bpw;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200466
467 /* data packet should not exceed 1/2 of fifo space */
Marek Vasut970e8ea2021-01-04 13:31:14 +0100468 packet = clamp(xfer_len, 1U, spi->fifo_size / 2);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200469
470 /* align packet size with data registers access */
Marek Vasut970e8ea2021-01-04 13:31:14 +0100471 bpw = DIV_ROUND_UP(spi->cur_bpw, 8);
472 return DIV_ROUND_UP(packet, bpw);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200473}
474
475/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100476 * stm32f4_spi_write_tx - Write bytes to Transmit Data Register
477 * @spi: pointer to the spi controller data structure
478 *
479 * Read from tx_buf depends on remaining bytes to avoid to read beyond
480 * tx_buf end.
481 */
482static void stm32f4_spi_write_tx(struct stm32_spi *spi)
483{
484 if ((spi->tx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
485 STM32F4_SPI_SR_TXE)) {
486 u32 offs = spi->cur_xferlen - spi->tx_len;
487
488 if (spi->cur_bpw == 16) {
489 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
490
491 writew_relaxed(*tx_buf16, spi->base + STM32F4_SPI_DR);
492 spi->tx_len -= sizeof(u16);
493 } else {
494 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
495
496 writeb_relaxed(*tx_buf8, spi->base + STM32F4_SPI_DR);
497 spi->tx_len -= sizeof(u8);
498 }
499 }
500
501 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
502}
503
504/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100505 * stm32h7_spi_write_txfifo - Write bytes in Transmit Data Register
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200506 * @spi: pointer to the spi controller data structure
507 *
508 * Read from tx_buf depends on remaining bytes to avoid to read beyond
509 * tx_buf end.
510 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100511static void stm32h7_spi_write_txfifo(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200512{
513 while ((spi->tx_len > 0) &&
Cezary Gapinski86026632018-12-24 23:00:33 +0100514 (readl_relaxed(spi->base + STM32H7_SPI_SR) &
515 STM32H7_SPI_SR_TXP)) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200516 u32 offs = spi->cur_xferlen - spi->tx_len;
517
518 if (spi->tx_len >= sizeof(u32)) {
519 const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
520
Cezary Gapinski86026632018-12-24 23:00:33 +0100521 writel_relaxed(*tx_buf32, spi->base + STM32H7_SPI_TXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200522 spi->tx_len -= sizeof(u32);
523 } else if (spi->tx_len >= sizeof(u16)) {
524 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
525
Cezary Gapinski86026632018-12-24 23:00:33 +0100526 writew_relaxed(*tx_buf16, spi->base + STM32H7_SPI_TXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200527 spi->tx_len -= sizeof(u16);
528 } else {
529 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
530
Cezary Gapinski86026632018-12-24 23:00:33 +0100531 writeb_relaxed(*tx_buf8, spi->base + STM32H7_SPI_TXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200532 spi->tx_len -= sizeof(u8);
533 }
534 }
535
536 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
537}
538
539/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100540 * stm32f4_spi_read_rx - Read bytes from Receive Data Register
541 * @spi: pointer to the spi controller data structure
542 *
543 * Write in rx_buf depends on remaining bytes to avoid to write beyond
544 * rx_buf end.
545 */
546static void stm32f4_spi_read_rx(struct stm32_spi *spi)
547{
548 if ((spi->rx_len > 0) && (readl_relaxed(spi->base + STM32F4_SPI_SR) &
549 STM32F4_SPI_SR_RXNE)) {
550 u32 offs = spi->cur_xferlen - spi->rx_len;
551
552 if (spi->cur_bpw == 16) {
553 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
554
555 *rx_buf16 = readw_relaxed(spi->base + STM32F4_SPI_DR);
556 spi->rx_len -= sizeof(u16);
557 } else {
558 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
559
560 *rx_buf8 = readb_relaxed(spi->base + STM32F4_SPI_DR);
561 spi->rx_len -= sizeof(u8);
562 }
563 }
564
565 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->rx_len);
566}
567
568/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100569 * stm32h7_spi_read_rxfifo - Read bytes in Receive Data Register
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200570 * @spi: pointer to the spi controller data structure
Alain Volmat1c52be82020-03-20 14:44:17 +0100571 * @flush: boolean indicating that FIFO should be flushed
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200572 *
573 * Write in rx_buf depends on remaining bytes to avoid to write beyond
574 * rx_buf end.
575 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100576static void stm32h7_spi_read_rxfifo(struct stm32_spi *spi, bool flush)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200577{
Cezary Gapinski86026632018-12-24 23:00:33 +0100578 u32 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
Amelie Delaunay5a380b82021-02-05 19:59:27 +0100579 u32 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200580
581 while ((spi->rx_len > 0) &&
Cezary Gapinski86026632018-12-24 23:00:33 +0100582 ((sr & STM32H7_SPI_SR_RXP) ||
583 (flush && ((sr & STM32H7_SPI_SR_RXWNE) || (rxplvl > 0))))) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200584 u32 offs = spi->cur_xferlen - spi->rx_len;
585
586 if ((spi->rx_len >= sizeof(u32)) ||
Cezary Gapinski86026632018-12-24 23:00:33 +0100587 (flush && (sr & STM32H7_SPI_SR_RXWNE))) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200588 u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
589
Cezary Gapinski86026632018-12-24 23:00:33 +0100590 *rx_buf32 = readl_relaxed(spi->base + STM32H7_SPI_RXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200591 spi->rx_len -= sizeof(u32);
592 } else if ((spi->rx_len >= sizeof(u16)) ||
593 (flush && (rxplvl >= 2 || spi->cur_bpw > 8))) {
594 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
595
Cezary Gapinski86026632018-12-24 23:00:33 +0100596 *rx_buf16 = readw_relaxed(spi->base + STM32H7_SPI_RXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200597 spi->rx_len -= sizeof(u16);
598 } else {
599 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
600
Cezary Gapinski86026632018-12-24 23:00:33 +0100601 *rx_buf8 = readb_relaxed(spi->base + STM32H7_SPI_RXDR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200602 spi->rx_len -= sizeof(u8);
603 }
604
Cezary Gapinski86026632018-12-24 23:00:33 +0100605 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
Amelie Delaunay5a380b82021-02-05 19:59:27 +0100606 rxplvl = FIELD_GET(STM32H7_SPI_SR_RXPLVL, sr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200607 }
608
609 dev_dbg(spi->dev, "%s%s: %d bytes left\n", __func__,
610 flush ? "(flush)" : "", spi->rx_len);
611}
612
613/**
614 * stm32_spi_enable - Enable SPI controller
615 * @spi: pointer to the spi controller data structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200616 */
617static void stm32_spi_enable(struct stm32_spi *spi)
618{
619 dev_dbg(spi->dev, "enable controller\n");
620
Cezary Gapinski55166852018-12-24 23:00:37 +0100621 stm32_spi_set_bits(spi, spi->cfg->regs->en.reg,
622 spi->cfg->regs->en.mask);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200623}
624
625/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100626 * stm32f4_spi_disable - Disable SPI controller
627 * @spi: pointer to the spi controller data structure
628 */
629static void stm32f4_spi_disable(struct stm32_spi *spi)
630{
631 unsigned long flags;
632 u32 sr;
633
634 dev_dbg(spi->dev, "disable controller\n");
635
636 spin_lock_irqsave(&spi->lock, flags);
637
638 if (!(readl_relaxed(spi->base + STM32F4_SPI_CR1) &
639 STM32F4_SPI_CR1_SPE)) {
640 spin_unlock_irqrestore(&spi->lock, flags);
641 return;
642 }
643
644 /* Disable interrupts */
645 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXEIE |
646 STM32F4_SPI_CR2_RXNEIE |
647 STM32F4_SPI_CR2_ERRIE);
648
649 /* Wait until BSY = 0 */
650 if (readl_relaxed_poll_timeout_atomic(spi->base + STM32F4_SPI_SR,
651 sr, !(sr & STM32F4_SPI_SR_BSY),
652 10, 100000) < 0) {
653 dev_warn(spi->dev, "disabling condition timeout\n");
654 }
655
656 if (spi->cur_usedma && spi->dma_tx)
657 dmaengine_terminate_all(spi->dma_tx);
658 if (spi->cur_usedma && spi->dma_rx)
659 dmaengine_terminate_all(spi->dma_rx);
660
661 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SPE);
662
663 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_TXDMAEN |
664 STM32F4_SPI_CR2_RXDMAEN);
665
666 /* Sequence to clear OVR flag */
667 readl_relaxed(spi->base + STM32F4_SPI_DR);
668 readl_relaxed(spi->base + STM32F4_SPI_SR);
669
670 spin_unlock_irqrestore(&spi->lock, flags);
671}
672
673/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100674 * stm32h7_spi_disable - Disable SPI controller
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200675 * @spi: pointer to the spi controller data structure
676 *
677 * RX-Fifo is flushed when SPI controller is disabled. To prevent any data
Cezary Gapinski55166852018-12-24 23:00:37 +0100678 * loss, use stm32h7_spi_read_rxfifo(flush) to read the remaining bytes in
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200679 * RX-Fifo.
Cezary Gapinski55166852018-12-24 23:00:37 +0100680 * Normally, if TSIZE has been configured, we should relax the hardware at the
681 * reception of the EOT interrupt. But in case of error, EOT will not be
682 * raised. So the subsystem unprepare_message call allows us to properly
683 * complete the transfer from an hardware point of view.
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200684 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100685static void stm32h7_spi_disable(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200686{
687 unsigned long flags;
688 u32 cr1, sr;
689
690 dev_dbg(spi->dev, "disable controller\n");
691
692 spin_lock_irqsave(&spi->lock, flags);
693
Cezary Gapinski86026632018-12-24 23:00:33 +0100694 cr1 = readl_relaxed(spi->base + STM32H7_SPI_CR1);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200695
Cezary Gapinski86026632018-12-24 23:00:33 +0100696 if (!(cr1 & STM32H7_SPI_CR1_SPE)) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200697 spin_unlock_irqrestore(&spi->lock, flags);
698 return;
699 }
700
701 /* Wait on EOT or suspend the flow */
Cezary Gapinski86026632018-12-24 23:00:33 +0100702 if (readl_relaxed_poll_timeout_atomic(spi->base + STM32H7_SPI_SR,
703 sr, !(sr & STM32H7_SPI_SR_EOT),
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200704 10, 100000) < 0) {
Cezary Gapinski86026632018-12-24 23:00:33 +0100705 if (cr1 & STM32H7_SPI_CR1_CSTART) {
706 writel_relaxed(cr1 | STM32H7_SPI_CR1_CSUSP,
707 spi->base + STM32H7_SPI_CR1);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200708 if (readl_relaxed_poll_timeout_atomic(
Cezary Gapinski86026632018-12-24 23:00:33 +0100709 spi->base + STM32H7_SPI_SR,
710 sr, !(sr & STM32H7_SPI_SR_SUSP),
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200711 10, 100000) < 0)
712 dev_warn(spi->dev,
713 "Suspend request timeout\n");
714 }
715 }
716
717 if (!spi->cur_usedma && spi->rx_buf && (spi->rx_len > 0))
Cezary Gapinski55166852018-12-24 23:00:37 +0100718 stm32h7_spi_read_rxfifo(spi, true);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200719
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100720 if (spi->cur_usedma && spi->dma_tx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200721 dmaengine_terminate_all(spi->dma_tx);
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100722 if (spi->cur_usedma && spi->dma_rx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200723 dmaengine_terminate_all(spi->dma_rx);
724
Cezary Gapinski86026632018-12-24 23:00:33 +0100725 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SPE);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200726
Cezary Gapinski86026632018-12-24 23:00:33 +0100727 stm32_spi_clr_bits(spi, STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_TXDMAEN |
728 STM32H7_SPI_CFG1_RXDMAEN);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200729
730 /* Disable interrupts and clear status flags */
Cezary Gapinski86026632018-12-24 23:00:33 +0100731 writel_relaxed(0, spi->base + STM32H7_SPI_IER);
732 writel_relaxed(STM32H7_SPI_IFCR_ALL, spi->base + STM32H7_SPI_IFCR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200733
734 spin_unlock_irqrestore(&spi->lock, flags);
735}
736
737/**
738 * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use
Alain Volmat1c52be82020-03-20 14:44:17 +0100739 * @master: controller master interface
740 * @spi_dev: pointer to the spi device
741 * @transfer: pointer to spi transfer
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200742 *
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100743 * If driver has fifo and the current transfer size is greater than fifo size,
744 * use DMA. Otherwise use DMA for transfer longer than defined DMA min bytes.
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200745 */
746static bool stm32_spi_can_dma(struct spi_master *master,
747 struct spi_device *spi_dev,
748 struct spi_transfer *transfer)
749{
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100750 unsigned int dma_size;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200751 struct stm32_spi *spi = spi_master_get_devdata(master);
752
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100753 if (spi->cfg->has_fifo)
754 dma_size = spi->fifo_size;
755 else
756 dma_size = SPI_DMA_MIN_BYTES;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200757
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100758 dev_dbg(spi->dev, "%s: %s\n", __func__,
759 (transfer->len > dma_size) ? "true" : "false");
760
761 return (transfer->len > dma_size);
762}
763
764/**
765 * stm32f4_spi_irq_event - Interrupt handler for SPI controller events
766 * @irq: interrupt line
767 * @dev_id: SPI controller master interface
768 */
769static irqreturn_t stm32f4_spi_irq_event(int irq, void *dev_id)
770{
771 struct spi_master *master = dev_id;
772 struct stm32_spi *spi = spi_master_get_devdata(master);
773 u32 sr, mask = 0;
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100774 bool end = false;
775
Barry Songe2368932020-09-26 12:16:16 +1200776 spin_lock(&spi->lock);
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100777
778 sr = readl_relaxed(spi->base + STM32F4_SPI_SR);
779 /*
780 * BSY flag is not handled in interrupt but it is normal behavior when
781 * this flag is set.
782 */
783 sr &= ~STM32F4_SPI_SR_BSY;
784
785 if (!spi->cur_usedma && (spi->cur_comm == SPI_SIMPLEX_TX ||
786 spi->cur_comm == SPI_3WIRE_TX)) {
787 /* OVR flag shouldn't be handled for TX only mode */
788 sr &= ~STM32F4_SPI_SR_OVR | STM32F4_SPI_SR_RXNE;
789 mask |= STM32F4_SPI_SR_TXE;
790 }
791
dillon min61367d02020-05-25 11:45:47 +0800792 if (!spi->cur_usedma && (spi->cur_comm == SPI_FULL_DUPLEX ||
793 spi->cur_comm == SPI_SIMPLEX_RX ||
794 spi->cur_comm == SPI_3WIRE_RX)) {
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100795 /* TXE flag is set and is handled when RXNE flag occurs */
796 sr &= ~STM32F4_SPI_SR_TXE;
797 mask |= STM32F4_SPI_SR_RXNE | STM32F4_SPI_SR_OVR;
798 }
799
800 if (!(sr & mask)) {
801 dev_dbg(spi->dev, "spurious IT (sr=0x%08x)\n", sr);
Barry Songe2368932020-09-26 12:16:16 +1200802 spin_unlock(&spi->lock);
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100803 return IRQ_NONE;
804 }
805
806 if (sr & STM32F4_SPI_SR_OVR) {
807 dev_warn(spi->dev, "Overrun: received value discarded\n");
808
809 /* Sequence to clear OVR flag */
810 readl_relaxed(spi->base + STM32F4_SPI_DR);
811 readl_relaxed(spi->base + STM32F4_SPI_SR);
812
813 /*
814 * If overrun is detected, it means that something went wrong,
815 * so stop the current transfer. Transfer can wait for next
816 * RXNE but DR is already read and end never happens.
817 */
818 end = true;
819 goto end_irq;
820 }
821
822 if (sr & STM32F4_SPI_SR_TXE) {
823 if (spi->tx_buf)
824 stm32f4_spi_write_tx(spi);
825 if (spi->tx_len == 0)
826 end = true;
827 }
828
829 if (sr & STM32F4_SPI_SR_RXNE) {
830 stm32f4_spi_read_rx(spi);
831 if (spi->rx_len == 0)
832 end = true;
dillon min61367d02020-05-25 11:45:47 +0800833 else if (spi->tx_buf)/* Load data for discontinuous mode */
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100834 stm32f4_spi_write_tx(spi);
835 }
836
837end_irq:
838 if (end) {
839 /* Immediately disable interrupts to do not generate new one */
840 stm32_spi_clr_bits(spi, STM32F4_SPI_CR2,
841 STM32F4_SPI_CR2_TXEIE |
842 STM32F4_SPI_CR2_RXNEIE |
843 STM32F4_SPI_CR2_ERRIE);
Barry Songe2368932020-09-26 12:16:16 +1200844 spin_unlock(&spi->lock);
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100845 return IRQ_WAKE_THREAD;
846 }
847
Barry Songe2368932020-09-26 12:16:16 +1200848 spin_unlock(&spi->lock);
Cezary Gapinski00505ed2018-12-24 23:00:38 +0100849 return IRQ_HANDLED;
850}
851
852/**
853 * stm32f4_spi_irq_thread - Thread of interrupt handler for SPI controller
854 * @irq: interrupt line
855 * @dev_id: SPI controller master interface
856 */
857static irqreturn_t stm32f4_spi_irq_thread(int irq, void *dev_id)
858{
859 struct spi_master *master = dev_id;
860 struct stm32_spi *spi = spi_master_get_devdata(master);
861
862 spi_finalize_current_transfer(master);
863 stm32f4_spi_disable(spi);
864
865 return IRQ_HANDLED;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200866}
867
868/**
Cezary Gapinski55166852018-12-24 23:00:37 +0100869 * stm32h7_spi_irq_thread - Thread of interrupt handler for SPI controller
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200870 * @irq: interrupt line
871 * @dev_id: SPI controller master interface
872 */
Cezary Gapinski55166852018-12-24 23:00:37 +0100873static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200874{
875 struct spi_master *master = dev_id;
876 struct stm32_spi *spi = spi_master_get_devdata(master);
877 u32 sr, ier, mask;
878 unsigned long flags;
879 bool end = false;
880
881 spin_lock_irqsave(&spi->lock, flags);
882
Cezary Gapinski86026632018-12-24 23:00:33 +0100883 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
884 ier = readl_relaxed(spi->base + STM32H7_SPI_IER);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200885
886 mask = ier;
Alain Volmate4a5c192021-06-30 10:45:19 +0200887 /*
888 * EOTIE enables irq from EOT, SUSP and TXC events. We need to set
889 * SUSP to acknowledge it later. TXC is automatically cleared
890 */
891
Cezary Gapinski86026632018-12-24 23:00:33 +0100892 mask |= STM32H7_SPI_SR_SUSP;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200893 /*
Alain Volmate4a5c192021-06-30 10:45:19 +0200894 * DXPIE is set in Full-Duplex, one IT will be raised if TXP and RXP
895 * are set. So in case of Full-Duplex, need to poll TXP and RXP event.
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200896 */
Alain Volmate4a5c192021-06-30 10:45:19 +0200897 if ((spi->cur_comm == SPI_FULL_DUPLEX) && !spi->cur_usedma)
898 mask |= STM32H7_SPI_SR_TXP | STM32H7_SPI_SR_RXP;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200899
900 if (!(sr & mask)) {
Alain Volmatc64e7ef2021-02-05 19:59:32 +0100901 dev_warn(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
902 sr, ier);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200903 spin_unlock_irqrestore(&spi->lock, flags);
904 return IRQ_NONE;
905 }
906
Cezary Gapinski86026632018-12-24 23:00:33 +0100907 if (sr & STM32H7_SPI_SR_SUSP) {
Marek Vasutea8be082020-09-05 17:19:13 +0200908 static DEFINE_RATELIMIT_STATE(rs,
909 DEFAULT_RATELIMIT_INTERVAL * 10,
910 1);
911 if (__ratelimit(&rs))
912 dev_dbg_ratelimited(spi->dev, "Communication suspended\n");
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200913 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
Cezary Gapinski55166852018-12-24 23:00:37 +0100914 stm32h7_spi_read_rxfifo(spi, false);
Amelie Delaunayc67ad362017-06-27 17:45:19 +0200915 /*
916 * If communication is suspended while using DMA, it means
917 * that something went wrong, so stop the current transfer
918 */
919 if (spi->cur_usedma)
920 end = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200921 }
922
Cezary Gapinski86026632018-12-24 23:00:33 +0100923 if (sr & STM32H7_SPI_SR_MODF) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200924 dev_warn(spi->dev, "Mode fault: transfer aborted\n");
925 end = true;
926 }
927
Cezary Gapinski86026632018-12-24 23:00:33 +0100928 if (sr & STM32H7_SPI_SR_OVR) {
Alain Volmatc64e7ef2021-02-05 19:59:32 +0100929 dev_err(spi->dev, "Overrun: RX data lost\n");
930 end = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200931 }
932
Cezary Gapinski86026632018-12-24 23:00:33 +0100933 if (sr & STM32H7_SPI_SR_EOT) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200934 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
Cezary Gapinski55166852018-12-24 23:00:37 +0100935 stm32h7_spi_read_rxfifo(spi, true);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200936 end = true;
937 }
938
Cezary Gapinski86026632018-12-24 23:00:33 +0100939 if (sr & STM32H7_SPI_SR_TXP)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200940 if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
Cezary Gapinski55166852018-12-24 23:00:37 +0100941 stm32h7_spi_write_txfifo(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200942
Cezary Gapinski86026632018-12-24 23:00:33 +0100943 if (sr & STM32H7_SPI_SR_RXP)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200944 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
Cezary Gapinski55166852018-12-24 23:00:37 +0100945 stm32h7_spi_read_rxfifo(spi, false);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200946
Tobias Schrammae1ba502020-08-04 21:51:36 +0200947 writel_relaxed(sr & mask, spi->base + STM32H7_SPI_IFCR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200948
949 spin_unlock_irqrestore(&spi->lock, flags);
950
951 if (end) {
Cezary Gapinski55166852018-12-24 23:00:37 +0100952 stm32h7_spi_disable(spi);
Antonio Borneo135dd872020-08-10 09:12:34 +0200953 spi_finalize_current_transfer(master);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200954 }
955
956 return IRQ_HANDLED;
957}
958
959/**
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200960 * stm32_spi_prepare_msg - set up the controller to transfer a single message
Alain Volmat1c52be82020-03-20 14:44:17 +0100961 * @master: controller master interface
962 * @msg: pointer to spi message
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200963 */
964static int stm32_spi_prepare_msg(struct spi_master *master,
965 struct spi_message *msg)
966{
967 struct stm32_spi *spi = spi_master_get_devdata(master);
968 struct spi_device *spi_dev = msg->spi;
969 struct device_node *np = spi_dev->dev.of_node;
970 unsigned long flags;
Cezary Gapinski55166852018-12-24 23:00:37 +0100971 u32 clrb = 0, setb = 0;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200972
973 /* SPI slave device may need time between data frames */
974 spi->cur_midi = 0;
Amelie Delaunay042c1c62017-06-27 17:45:16 +0200975 if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200976 dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
977
978 if (spi_dev->mode & SPI_CPOL)
Cezary Gapinski55166852018-12-24 23:00:37 +0100979 setb |= spi->cfg->regs->cpol.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200980 else
Cezary Gapinski55166852018-12-24 23:00:37 +0100981 clrb |= spi->cfg->regs->cpol.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200982
983 if (spi_dev->mode & SPI_CPHA)
Cezary Gapinski55166852018-12-24 23:00:37 +0100984 setb |= spi->cfg->regs->cpha.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200985 else
Cezary Gapinski55166852018-12-24 23:00:37 +0100986 clrb |= spi->cfg->regs->cpha.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200987
988 if (spi_dev->mode & SPI_LSB_FIRST)
Cezary Gapinski55166852018-12-24 23:00:37 +0100989 setb |= spi->cfg->regs->lsb_first.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200990 else
Cezary Gapinski55166852018-12-24 23:00:37 +0100991 clrb |= spi->cfg->regs->lsb_first.mask;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200992
993 dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n",
Alexandru Ardelean8b835da2021-01-04 16:31:03 +0200994 !!(spi_dev->mode & SPI_CPOL),
995 !!(spi_dev->mode & SPI_CPHA),
996 !!(spi_dev->mode & SPI_LSB_FIRST),
997 !!(spi_dev->mode & SPI_CS_HIGH));
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200998
Alain Volmat084de522021-02-05 19:59:28 +0100999 /* On STM32H7, messages should not exceed a maximum size setted
1000 * afterward via the set_number_of_data function. In order to
1001 * ensure that, split large messages into several messages
1002 */
1003 if (spi->cfg->set_number_of_data) {
1004 int ret;
1005
1006 ret = spi_split_transfers_maxsize(master, msg,
1007 STM32H7_SPI_TSIZE_MAX,
1008 GFP_KERNEL | GFP_DMA);
1009 if (ret)
1010 return ret;
1011 }
1012
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001013 spin_lock_irqsave(&spi->lock, flags);
1014
Cezary Gapinski55166852018-12-24 23:00:37 +01001015 /* CPOL, CPHA and LSB FIRST bits have common register */
1016 if (clrb || setb)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001017 writel_relaxed(
Cezary Gapinski55166852018-12-24 23:00:37 +01001018 (readl_relaxed(spi->base + spi->cfg->regs->cpol.reg) &
1019 ~clrb) | setb,
1020 spi->base + spi->cfg->regs->cpol.reg);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001021
1022 spin_unlock_irqrestore(&spi->lock, flags);
1023
1024 return 0;
1025}
1026
1027/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001028 * stm32f4_spi_dma_tx_cb - dma callback
Alain Volmat1c52be82020-03-20 14:44:17 +01001029 * @data: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001030 *
1031 * DMA callback is called when the transfer is complete for DMA TX channel.
1032 */
1033static void stm32f4_spi_dma_tx_cb(void *data)
1034{
1035 struct stm32_spi *spi = data;
1036
1037 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1038 spi_finalize_current_transfer(spi->master);
1039 stm32f4_spi_disable(spi);
1040 }
1041}
1042
1043/**
1044 * stm32f4_spi_dma_rx_cb - dma callback
Alain Volmat1c52be82020-03-20 14:44:17 +01001045 * @data: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001046 *
1047 * DMA callback is called when the transfer is complete for DMA RX channel.
1048 */
1049static void stm32f4_spi_dma_rx_cb(void *data)
1050{
1051 struct stm32_spi *spi = data;
1052
1053 spi_finalize_current_transfer(spi->master);
1054 stm32f4_spi_disable(spi);
1055}
1056
1057/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001058 * stm32h7_spi_dma_cb - dma callback
Alain Volmat1c52be82020-03-20 14:44:17 +01001059 * @data: pointer to the spi controller data structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001060 *
1061 * DMA callback is called when the transfer is complete or when an error
1062 * occurs. If the transfer is complete, EOT flag is raised.
1063 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001064static void stm32h7_spi_dma_cb(void *data)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001065{
1066 struct stm32_spi *spi = data;
1067 unsigned long flags;
1068 u32 sr;
1069
1070 spin_lock_irqsave(&spi->lock, flags);
1071
Cezary Gapinski86026632018-12-24 23:00:33 +01001072 sr = readl_relaxed(spi->base + STM32H7_SPI_SR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001073
1074 spin_unlock_irqrestore(&spi->lock, flags);
1075
Cezary Gapinski86026632018-12-24 23:00:33 +01001076 if (!(sr & STM32H7_SPI_SR_EOT))
Amelie Delaunayc67ad362017-06-27 17:45:19 +02001077 dev_warn(spi->dev, "DMA error (sr=0x%08x)\n", sr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001078
Amelie Delaunayc67ad362017-06-27 17:45:19 +02001079 /* Now wait for EOT, or SUSP or OVR in case of error */
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001080}
1081
1082/**
1083 * stm32_spi_dma_config - configure dma slave channel depending on current
1084 * transfer bits_per_word.
Alain Volmat1c52be82020-03-20 14:44:17 +01001085 * @spi: pointer to the spi controller data structure
1086 * @dma_conf: pointer to the dma_slave_config structure
1087 * @dir: direction of the dma transfer
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001088 */
1089static void stm32_spi_dma_config(struct stm32_spi *spi,
1090 struct dma_slave_config *dma_conf,
1091 enum dma_transfer_direction dir)
1092{
1093 enum dma_slave_buswidth buswidth;
1094 u32 maxburst;
1095
Amelie Delaunay128ebb82017-06-27 17:45:17 +02001096 if (spi->cur_bpw <= 8)
1097 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1098 else if (spi->cur_bpw <= 16)
1099 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1100 else
1101 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001102
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001103 if (spi->cfg->has_fifo) {
1104 /* Valid for DMA Half or Full Fifo threshold */
1105 if (spi->cur_fthlv == 2)
1106 maxburst = 1;
1107 else
1108 maxburst = spi->cur_fthlv;
1109 } else {
Amelie Delaunay128ebb82017-06-27 17:45:17 +02001110 maxburst = 1;
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001111 }
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001112
1113 memset(dma_conf, 0, sizeof(struct dma_slave_config));
1114 dma_conf->direction = dir;
1115 if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */
Cezary Gapinski55166852018-12-24 23:00:37 +01001116 dma_conf->src_addr = spi->phys_addr + spi->cfg->regs->rx.reg;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001117 dma_conf->src_addr_width = buswidth;
1118 dma_conf->src_maxburst = maxburst;
1119
1120 dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
1121 buswidth, maxburst);
1122 } else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */
Cezary Gapinski55166852018-12-24 23:00:37 +01001123 dma_conf->dst_addr = spi->phys_addr + spi->cfg->regs->tx.reg;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001124 dma_conf->dst_addr_width = buswidth;
1125 dma_conf->dst_maxburst = maxburst;
1126
1127 dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
1128 buswidth, maxburst);
1129 }
1130}
1131
1132/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001133 * stm32f4_spi_transfer_one_irq - transfer a single spi_transfer using
1134 * interrupts
Alain Volmat1c52be82020-03-20 14:44:17 +01001135 * @spi: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001136 *
1137 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1138 * in progress.
1139 */
1140static int stm32f4_spi_transfer_one_irq(struct stm32_spi *spi)
1141{
1142 unsigned long flags;
1143 u32 cr2 = 0;
1144
1145 /* Enable the interrupts relative to the current communication mode */
1146 if (spi->cur_comm == SPI_SIMPLEX_TX || spi->cur_comm == SPI_3WIRE_TX) {
1147 cr2 |= STM32F4_SPI_CR2_TXEIE;
dillon min61367d02020-05-25 11:45:47 +08001148 } else if (spi->cur_comm == SPI_FULL_DUPLEX ||
1149 spi->cur_comm == SPI_SIMPLEX_RX ||
1150 spi->cur_comm == SPI_3WIRE_RX) {
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001151 /* In transmit-only mode, the OVR flag is set in the SR register
1152 * since the received data are never read. Therefore set OVR
1153 * interrupt only when rx buffer is available.
1154 */
1155 cr2 |= STM32F4_SPI_CR2_RXNEIE | STM32F4_SPI_CR2_ERRIE;
1156 } else {
1157 return -EINVAL;
1158 }
1159
1160 spin_lock_irqsave(&spi->lock, flags);
1161
1162 stm32_spi_set_bits(spi, STM32F4_SPI_CR2, cr2);
1163
1164 stm32_spi_enable(spi);
1165
1166 /* starting data transfer when buffer is loaded */
1167 if (spi->tx_buf)
1168 stm32f4_spi_write_tx(spi);
1169
1170 spin_unlock_irqrestore(&spi->lock, flags);
1171
1172 return 1;
1173}
1174
1175/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001176 * stm32h7_spi_transfer_one_irq - transfer a single spi_transfer using
1177 * interrupts
Alain Volmat1c52be82020-03-20 14:44:17 +01001178 * @spi: pointer to the spi controller data structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001179 *
1180 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1181 * in progress.
1182 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001183static int stm32h7_spi_transfer_one_irq(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001184{
1185 unsigned long flags;
1186 u32 ier = 0;
1187
1188 /* Enable the interrupts relative to the current communication mode */
1189 if (spi->tx_buf && spi->rx_buf) /* Full Duplex */
Cezary Gapinski86026632018-12-24 23:00:33 +01001190 ier |= STM32H7_SPI_IER_DXPIE;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001191 else if (spi->tx_buf) /* Half-Duplex TX dir or Simplex TX */
Cezary Gapinski86026632018-12-24 23:00:33 +01001192 ier |= STM32H7_SPI_IER_TXPIE;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001193 else if (spi->rx_buf) /* Half-Duplex RX dir or Simplex RX */
Cezary Gapinski86026632018-12-24 23:00:33 +01001194 ier |= STM32H7_SPI_IER_RXPIE;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001195
1196 /* Enable the interrupts relative to the end of transfer */
Cezary Gapinski86026632018-12-24 23:00:33 +01001197 ier |= STM32H7_SPI_IER_EOTIE | STM32H7_SPI_IER_TXTFIE |
1198 STM32H7_SPI_IER_OVRIE | STM32H7_SPI_IER_MODFIE;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001199
1200 spin_lock_irqsave(&spi->lock, flags);
1201
1202 stm32_spi_enable(spi);
1203
1204 /* Be sure to have data in fifo before starting data transfer */
1205 if (spi->tx_buf)
Cezary Gapinski55166852018-12-24 23:00:37 +01001206 stm32h7_spi_write_txfifo(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001207
Cezary Gapinski86026632018-12-24 23:00:33 +01001208 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001209
Cezary Gapinski86026632018-12-24 23:00:33 +01001210 writel_relaxed(ier, spi->base + STM32H7_SPI_IER);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001211
1212 spin_unlock_irqrestore(&spi->lock, flags);
1213
1214 return 1;
1215}
1216
1217/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001218 * stm32f4_spi_transfer_one_dma_start - Set SPI driver registers to start
1219 * transfer using DMA
Alain Volmat1c52be82020-03-20 14:44:17 +01001220 * @spi: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001221 */
1222static void stm32f4_spi_transfer_one_dma_start(struct stm32_spi *spi)
1223{
1224 /* In DMA mode end of transfer is handled by DMA TX or RX callback. */
1225 if (spi->cur_comm == SPI_SIMPLEX_RX || spi->cur_comm == SPI_3WIRE_RX ||
1226 spi->cur_comm == SPI_FULL_DUPLEX) {
1227 /*
1228 * In transmit-only mode, the OVR flag is set in the SR register
1229 * since the received data are never read. Therefore set OVR
1230 * interrupt only when rx buffer is available.
1231 */
1232 stm32_spi_set_bits(spi, STM32F4_SPI_CR2, STM32F4_SPI_CR2_ERRIE);
1233 }
1234
1235 stm32_spi_enable(spi);
1236}
1237
1238/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001239 * stm32h7_spi_transfer_one_dma_start - Set SPI driver registers to start
1240 * transfer using DMA
Alain Volmat1c52be82020-03-20 14:44:17 +01001241 * @spi: pointer to the spi controller data structure
Cezary Gapinskif8bb12f2018-12-24 23:00:36 +01001242 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001243static void stm32h7_spi_transfer_one_dma_start(struct stm32_spi *spi)
Cezary Gapinskif8bb12f2018-12-24 23:00:36 +01001244{
1245 /* Enable the interrupts relative to the end of transfer */
1246 stm32_spi_set_bits(spi, STM32H7_SPI_IER, STM32H7_SPI_IER_EOTIE |
1247 STM32H7_SPI_IER_TXTFIE |
1248 STM32H7_SPI_IER_OVRIE |
1249 STM32H7_SPI_IER_MODFIE);
1250
1251 stm32_spi_enable(spi);
1252
1253 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART);
1254}
1255
1256/**
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001257 * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
Alain Volmat1c52be82020-03-20 14:44:17 +01001258 * @spi: pointer to the spi controller data structure
1259 * @xfer: pointer to the spi_transfer structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001260 *
1261 * It must returns 0 if the transfer is finished or 1 if the transfer is still
1262 * in progress.
1263 */
1264static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
1265 struct spi_transfer *xfer)
1266{
1267 struct dma_slave_config tx_dma_conf, rx_dma_conf;
1268 struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc;
1269 unsigned long flags;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001270
1271 spin_lock_irqsave(&spi->lock, flags);
1272
1273 rx_dma_desc = NULL;
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001274 if (spi->rx_buf && spi->dma_rx) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001275 stm32_spi_dma_config(spi, &rx_dma_conf, DMA_DEV_TO_MEM);
1276 dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
1277
1278 /* Enable Rx DMA request */
Cezary Gapinski55166852018-12-24 23:00:37 +01001279 stm32_spi_set_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1280 spi->cfg->regs->dma_rx_en.mask);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001281
1282 rx_dma_desc = dmaengine_prep_slave_sg(
1283 spi->dma_rx, xfer->rx_sg.sgl,
1284 xfer->rx_sg.nents,
1285 rx_dma_conf.direction,
1286 DMA_PREP_INTERRUPT);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001287 }
1288
1289 tx_dma_desc = NULL;
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001290 if (spi->tx_buf && spi->dma_tx) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001291 stm32_spi_dma_config(spi, &tx_dma_conf, DMA_MEM_TO_DEV);
1292 dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
1293
1294 tx_dma_desc = dmaengine_prep_slave_sg(
1295 spi->dma_tx, xfer->tx_sg.sgl,
1296 xfer->tx_sg.nents,
1297 tx_dma_conf.direction,
1298 DMA_PREP_INTERRUPT);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001299 }
1300
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001301 if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
1302 (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
1303 goto dma_desc_error;
1304
1305 if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001306 goto dma_desc_error;
1307
1308 if (rx_dma_desc) {
Cezary Gapinski55166852018-12-24 23:00:37 +01001309 rx_dma_desc->callback = spi->cfg->dma_rx_cb;
Amelie Delaunay7b821a62017-06-27 17:45:20 +02001310 rx_dma_desc->callback_param = spi;
1311
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001312 if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
1313 dev_err(spi->dev, "Rx DMA submit failed\n");
1314 goto dma_desc_error;
1315 }
1316 /* Enable Rx DMA channel */
1317 dma_async_issue_pending(spi->dma_rx);
1318 }
1319
1320 if (tx_dma_desc) {
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001321 if (spi->cur_comm == SPI_SIMPLEX_TX ||
1322 spi->cur_comm == SPI_3WIRE_TX) {
Cezary Gapinski55166852018-12-24 23:00:37 +01001323 tx_dma_desc->callback = spi->cfg->dma_tx_cb;
Amelie Delaunay7b821a62017-06-27 17:45:20 +02001324 tx_dma_desc->callback_param = spi;
1325 }
1326
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001327 if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
1328 dev_err(spi->dev, "Tx DMA submit failed\n");
1329 goto dma_submit_error;
1330 }
1331 /* Enable Tx DMA channel */
1332 dma_async_issue_pending(spi->dma_tx);
1333
1334 /* Enable Tx DMA request */
Cezary Gapinski55166852018-12-24 23:00:37 +01001335 stm32_spi_set_bits(spi, spi->cfg->regs->dma_tx_en.reg,
1336 spi->cfg->regs->dma_tx_en.mask);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001337 }
1338
Cezary Gapinski55166852018-12-24 23:00:37 +01001339 spi->cfg->transfer_one_dma_start(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001340
1341 spin_unlock_irqrestore(&spi->lock, flags);
1342
1343 return 1;
1344
1345dma_submit_error:
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001346 if (spi->dma_rx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001347 dmaengine_terminate_all(spi->dma_rx);
1348
1349dma_desc_error:
Cezary Gapinski55166852018-12-24 23:00:37 +01001350 stm32_spi_clr_bits(spi, spi->cfg->regs->dma_rx_en.reg,
1351 spi->cfg->regs->dma_rx_en.mask);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001352
1353 spin_unlock_irqrestore(&spi->lock, flags);
1354
1355 dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
1356
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001357 spi->cur_usedma = false;
Cezary Gapinski55166852018-12-24 23:00:37 +01001358 return spi->cfg->transfer_one_irq(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001359}
1360
1361/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001362 * stm32f4_spi_set_bpw - Configure bits per word
1363 * @spi: pointer to the spi controller data structure
1364 */
1365static void stm32f4_spi_set_bpw(struct stm32_spi *spi)
1366{
1367 if (spi->cur_bpw == 16)
1368 stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1369 else
1370 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_DFF);
1371}
1372
1373/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001374 * stm32h7_spi_set_bpw - configure bits per word
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001375 * @spi: pointer to the spi controller data structure
1376 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001377static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001378{
1379 u32 bpw, fthlv;
1380 u32 cfg1_clrb = 0, cfg1_setb = 0;
1381
1382 bpw = spi->cur_bpw - 1;
1383
1384 cfg1_clrb |= STM32H7_SPI_CFG1_DSIZE;
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001385 cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_DSIZE, bpw);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001386
Amelie Delaunay3373e902020-08-10 09:12:35 +02001387 spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001388 fthlv = spi->cur_fthlv - 1;
1389
1390 cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001391 cfg1_setb |= FIELD_PREP(STM32H7_SPI_CFG1_FTHLV, fthlv);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001392
1393 writel_relaxed(
1394 (readl_relaxed(spi->base + STM32H7_SPI_CFG1) &
1395 ~cfg1_clrb) | cfg1_setb,
1396 spi->base + STM32H7_SPI_CFG1);
1397}
1398
1399/**
1400 * stm32_spi_set_mbr - Configure baud rate divisor in master mode
1401 * @spi: pointer to the spi controller data structure
1402 * @mbrdiv: baud rate divisor value
1403 */
1404static void stm32_spi_set_mbr(struct stm32_spi *spi, u32 mbrdiv)
1405{
Cezary Gapinski55166852018-12-24 23:00:37 +01001406 u32 clrb = 0, setb = 0;
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001407
Cezary Gapinski55166852018-12-24 23:00:37 +01001408 clrb |= spi->cfg->regs->br.mask;
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001409 setb |= (mbrdiv << spi->cfg->regs->br.shift) & spi->cfg->regs->br.mask;
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001410
Cezary Gapinski55166852018-12-24 23:00:37 +01001411 writel_relaxed((readl_relaxed(spi->base + spi->cfg->regs->br.reg) &
1412 ~clrb) | setb,
1413 spi->base + spi->cfg->regs->br.reg);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001414}
1415
1416/**
1417 * stm32_spi_communication_type - return transfer communication type
1418 * @spi_dev: pointer to the spi device
Alain Volmat1c52be82020-03-20 14:44:17 +01001419 * @transfer: pointer to spi transfer
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001420 */
1421static unsigned int stm32_spi_communication_type(struct spi_device *spi_dev,
1422 struct spi_transfer *transfer)
1423{
1424 unsigned int type = SPI_FULL_DUPLEX;
1425
1426 if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
1427 /*
1428 * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
1429 * is forbidden and unvalidated by SPI subsystem so depending
1430 * on the valid buffer, we can determine the direction of the
1431 * transfer.
1432 */
1433 if (!transfer->tx_buf)
1434 type = SPI_3WIRE_RX;
1435 else
1436 type = SPI_3WIRE_TX;
1437 } else {
1438 if (!transfer->tx_buf)
1439 type = SPI_SIMPLEX_RX;
1440 else if (!transfer->rx_buf)
1441 type = SPI_SIMPLEX_TX;
1442 }
1443
1444 return type;
1445}
1446
1447/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001448 * stm32f4_spi_set_mode - configure communication mode
1449 * @spi: pointer to the spi controller data structure
1450 * @comm_type: type of communication to configure
1451 */
1452static int stm32f4_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
1453{
1454 if (comm_type == SPI_3WIRE_TX || comm_type == SPI_SIMPLEX_TX) {
1455 stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1456 STM32F4_SPI_CR1_BIDIMODE |
1457 STM32F4_SPI_CR1_BIDIOE);
dillon min61367d02020-05-25 11:45:47 +08001458 } else if (comm_type == SPI_FULL_DUPLEX ||
1459 comm_type == SPI_SIMPLEX_RX) {
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001460 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1461 STM32F4_SPI_CR1_BIDIMODE |
1462 STM32F4_SPI_CR1_BIDIOE);
dillon min61367d02020-05-25 11:45:47 +08001463 } else if (comm_type == SPI_3WIRE_RX) {
1464 stm32_spi_set_bits(spi, STM32F4_SPI_CR1,
1465 STM32F4_SPI_CR1_BIDIMODE);
1466 stm32_spi_clr_bits(spi, STM32F4_SPI_CR1,
1467 STM32F4_SPI_CR1_BIDIOE);
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001468 } else {
1469 return -EINVAL;
1470 }
1471
1472 return 0;
1473}
1474
1475/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001476 * stm32h7_spi_set_mode - configure communication mode
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001477 * @spi: pointer to the spi controller data structure
1478 * @comm_type: type of communication to configure
1479 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001480static int stm32h7_spi_set_mode(struct stm32_spi *spi, unsigned int comm_type)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001481{
1482 u32 mode;
1483 u32 cfg2_clrb = 0, cfg2_setb = 0;
1484
1485 if (comm_type == SPI_3WIRE_RX) {
1486 mode = STM32H7_SPI_HALF_DUPLEX;
1487 stm32_spi_clr_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1488 } else if (comm_type == SPI_3WIRE_TX) {
1489 mode = STM32H7_SPI_HALF_DUPLEX;
1490 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_HDDIR);
1491 } else if (comm_type == SPI_SIMPLEX_RX) {
1492 mode = STM32H7_SPI_SIMPLEX_RX;
1493 } else if (comm_type == SPI_SIMPLEX_TX) {
1494 mode = STM32H7_SPI_SIMPLEX_TX;
1495 } else {
1496 mode = STM32H7_SPI_FULL_DUPLEX;
1497 }
1498
1499 cfg2_clrb |= STM32H7_SPI_CFG2_COMM;
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001500 cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_COMM, mode);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001501
1502 writel_relaxed(
1503 (readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1504 ~cfg2_clrb) | cfg2_setb,
1505 spi->base + STM32H7_SPI_CFG2);
1506
1507 return 0;
1508}
1509
1510/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001511 * stm32h7_spi_data_idleness - configure minimum time delay inserted between two
1512 * consecutive data frames in master mode
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001513 * @spi: pointer to the spi controller data structure
1514 * @len: transfer len
1515 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001516static void stm32h7_spi_data_idleness(struct stm32_spi *spi, u32 len)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001517{
1518 u32 cfg2_clrb = 0, cfg2_setb = 0;
1519
1520 cfg2_clrb |= STM32H7_SPI_CFG2_MIDI;
1521 if ((len > 1) && (spi->cur_midi > 0)) {
Amelie Delaunaye1e20932021-02-05 19:59:31 +01001522 u32 sck_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->cur_speed);
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001523 u32 midi = min_t(u32,
1524 DIV_ROUND_UP(spi->cur_midi, sck_period_ns),
1525 FIELD_GET(STM32H7_SPI_CFG2_MIDI,
1526 STM32H7_SPI_CFG2_MIDI));
1527
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001528
1529 dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
1530 sck_period_ns, midi, midi * sck_period_ns);
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001531 cfg2_setb |= FIELD_PREP(STM32H7_SPI_CFG2_MIDI, midi);
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001532 }
1533
1534 writel_relaxed((readl_relaxed(spi->base + STM32H7_SPI_CFG2) &
1535 ~cfg2_clrb) | cfg2_setb,
1536 spi->base + STM32H7_SPI_CFG2);
1537}
1538
1539/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001540 * stm32h7_spi_number_of_data - configure number of data at current transfer
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001541 * @spi: pointer to the spi controller data structure
Alain Volmat1c52be82020-03-20 14:44:17 +01001542 * @nb_words: transfer length (in words)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001543 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001544static int stm32h7_spi_number_of_data(struct stm32_spi *spi, u32 nb_words)
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001545{
Amelie Delaunay5a380b82021-02-05 19:59:27 +01001546 if (nb_words <= STM32H7_SPI_TSIZE_MAX) {
1547 writel_relaxed(FIELD_PREP(STM32H7_SPI_CR2_TSIZE, nb_words),
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001548 spi->base + STM32H7_SPI_CR2);
1549 } else {
1550 return -EMSGSIZE;
1551 }
1552
1553 return 0;
1554}
1555
1556/**
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001557 * stm32_spi_transfer_one_setup - common setup to transfer a single
1558 * spi_transfer either using DMA or
1559 * interrupts.
Alain Volmat1c52be82020-03-20 14:44:17 +01001560 * @spi: pointer to the spi controller data structure
1561 * @spi_dev: pointer to the spi device
1562 * @transfer: pointer to spi transfer
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001563 */
1564static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
1565 struct spi_device *spi_dev,
1566 struct spi_transfer *transfer)
1567{
1568 unsigned long flags;
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001569 unsigned int comm_type;
1570 int nb_words, ret = 0;
Alain Volmat60ccb352020-08-10 09:12:38 +02001571 int mbr;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001572
1573 spin_lock_irqsave(&spi->lock, flags);
1574
Amelie Delaunay3373e902020-08-10 09:12:35 +02001575 spi->cur_xferlen = transfer->len;
1576
Alain Volmat60ccb352020-08-10 09:12:38 +02001577 spi->cur_bpw = transfer->bits_per_word;
1578 spi->cfg->set_bpw(spi);
1579
1580 /* Update spi->cur_speed with real clock speed */
1581 mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz,
1582 spi->cfg->baud_rate_div_min,
1583 spi->cfg->baud_rate_div_max);
1584 if (mbr < 0) {
1585 ret = mbr;
1586 goto out;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001587 }
1588
Alain Volmat60ccb352020-08-10 09:12:38 +02001589 transfer->speed_hz = spi->cur_speed;
1590 stm32_spi_set_mbr(spi, mbr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001591
Cezary Gapinski9d5fce12018-12-24 23:00:35 +01001592 comm_type = stm32_spi_communication_type(spi_dev, transfer);
Alain Volmat60ccb352020-08-10 09:12:38 +02001593 ret = spi->cfg->set_mode(spi, comm_type);
1594 if (ret < 0)
1595 goto out;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001596
Alain Volmat60ccb352020-08-10 09:12:38 +02001597 spi->cur_comm = comm_type;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001598
Cezary Gapinski55166852018-12-24 23:00:37 +01001599 if (spi->cfg->set_data_idleness)
1600 spi->cfg->set_data_idleness(spi, transfer->len);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001601
Amelie Delaunay128ebb82017-06-27 17:45:17 +02001602 if (spi->cur_bpw <= 8)
1603 nb_words = transfer->len;
1604 else if (spi->cur_bpw <= 16)
1605 nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
1606 else
1607 nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001608
Cezary Gapinski55166852018-12-24 23:00:37 +01001609 if (spi->cfg->set_number_of_data) {
1610 ret = spi->cfg->set_number_of_data(spi, nb_words);
1611 if (ret < 0)
1612 goto out;
1613 }
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001614
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001615 dev_dbg(spi->dev, "transfer communication mode set to %d\n",
1616 spi->cur_comm);
1617 dev_dbg(spi->dev,
1618 "data frame of %d-bit, data packet of %d data frames\n",
1619 spi->cur_bpw, spi->cur_fthlv);
1620 dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
1621 dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
1622 spi->cur_xferlen, nb_words);
1623 dev_dbg(spi->dev, "dma %s\n",
1624 (spi->cur_usedma) ? "enabled" : "disabled");
1625
1626out:
1627 spin_unlock_irqrestore(&spi->lock, flags);
1628
1629 return ret;
1630}
1631
1632/**
1633 * stm32_spi_transfer_one - transfer a single spi_transfer
Alain Volmat1c52be82020-03-20 14:44:17 +01001634 * @master: controller master interface
1635 * @spi_dev: pointer to the spi device
1636 * @transfer: pointer to spi transfer
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001637 *
1638 * It must return 0 if the transfer is finished or 1 if the transfer is still
1639 * in progress.
1640 */
1641static int stm32_spi_transfer_one(struct spi_master *master,
1642 struct spi_device *spi_dev,
1643 struct spi_transfer *transfer)
1644{
1645 struct stm32_spi *spi = spi_master_get_devdata(master);
1646 int ret;
1647
Alain Volmat2269f5a2021-02-05 19:59:25 +01001648 /* Don't do anything on 0 bytes transfers */
1649 if (transfer->len == 0)
1650 return 0;
1651
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001652 spi->tx_buf = transfer->tx_buf;
1653 spi->rx_buf = transfer->rx_buf;
1654 spi->tx_len = spi->tx_buf ? transfer->len : 0;
1655 spi->rx_len = spi->rx_buf ? transfer->len : 0;
1656
Amelie Delaunayc67ad362017-06-27 17:45:19 +02001657 spi->cur_usedma = (master->can_dma &&
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +01001658 master->can_dma(master, spi_dev, transfer));
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001659
1660 ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
1661 if (ret) {
1662 dev_err(spi->dev, "SPI transfer setup failed\n");
1663 return ret;
1664 }
1665
1666 if (spi->cur_usedma)
1667 return stm32_spi_transfer_one_dma(spi, transfer);
1668 else
Cezary Gapinski55166852018-12-24 23:00:37 +01001669 return spi->cfg->transfer_one_irq(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001670}
1671
1672/**
1673 * stm32_spi_unprepare_msg - relax the hardware
Alain Volmat1c52be82020-03-20 14:44:17 +01001674 * @master: controller master interface
1675 * @msg: pointer to the spi message
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001676 */
1677static int stm32_spi_unprepare_msg(struct spi_master *master,
1678 struct spi_message *msg)
1679{
1680 struct stm32_spi *spi = spi_master_get_devdata(master);
1681
Cezary Gapinski55166852018-12-24 23:00:37 +01001682 spi->cfg->disable(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001683
1684 return 0;
1685}
1686
1687/**
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001688 * stm32f4_spi_config - Configure SPI controller as SPI master
Alain Volmat1c52be82020-03-20 14:44:17 +01001689 * @spi: pointer to the spi controller data structure
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001690 */
1691static int stm32f4_spi_config(struct stm32_spi *spi)
1692{
1693 unsigned long flags;
1694
1695 spin_lock_irqsave(&spi->lock, flags);
1696
1697 /* Ensure I2SMOD bit is kept cleared */
1698 stm32_spi_clr_bits(spi, STM32F4_SPI_I2SCFGR,
1699 STM32F4_SPI_I2SCFGR_I2SMOD);
1700
1701 /*
1702 * - SS input value high
1703 * - transmitter half duplex direction
1704 * - Set the master mode (default Motorola mode)
1705 * - Consider 1 master/n slaves configuration and
1706 * SS input value is determined by the SSI bit
1707 */
1708 stm32_spi_set_bits(spi, STM32F4_SPI_CR1, STM32F4_SPI_CR1_SSI |
1709 STM32F4_SPI_CR1_BIDIOE |
1710 STM32F4_SPI_CR1_MSTR |
1711 STM32F4_SPI_CR1_SSM);
1712
1713 spin_unlock_irqrestore(&spi->lock, flags);
1714
1715 return 0;
1716}
1717
1718/**
Cezary Gapinski55166852018-12-24 23:00:37 +01001719 * stm32h7_spi_config - Configure SPI controller as SPI master
Alain Volmat1c52be82020-03-20 14:44:17 +01001720 * @spi: pointer to the spi controller data structure
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001721 */
Cezary Gapinski55166852018-12-24 23:00:37 +01001722static int stm32h7_spi_config(struct stm32_spi *spi)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001723{
1724 unsigned long flags;
1725
1726 spin_lock_irqsave(&spi->lock, flags);
1727
1728 /* Ensure I2SMOD bit is kept cleared */
Cezary Gapinski86026632018-12-24 23:00:33 +01001729 stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR,
1730 STM32H7_SPI_I2SCFGR_I2SMOD);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001731
1732 /*
1733 * - SS input value high
1734 * - transmitter half duplex direction
1735 * - automatic communication suspend when RX-Fifo is full
1736 */
Cezary Gapinski86026632018-12-24 23:00:33 +01001737 stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SSI |
1738 STM32H7_SPI_CR1_HDDIR |
1739 STM32H7_SPI_CR1_MASRX);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001740
1741 /*
1742 * - Set the master mode (default Motorola mode)
1743 * - Consider 1 master/n slaves configuration and
1744 * SS input value is determined by the SSI bit
1745 * - keep control of all associated GPIOs
1746 */
Cezary Gapinski86026632018-12-24 23:00:33 +01001747 stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_MASTER |
1748 STM32H7_SPI_CFG2_SSM |
1749 STM32H7_SPI_CFG2_AFCNTR);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001750
1751 spin_unlock_irqrestore(&spi->lock, flags);
1752
1753 return 0;
1754}
1755
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001756static const struct stm32_spi_cfg stm32f4_spi_cfg = {
1757 .regs = &stm32f4_spi_regspec,
1758 .get_bpw_mask = stm32f4_spi_get_bpw_mask,
1759 .disable = stm32f4_spi_disable,
1760 .config = stm32f4_spi_config,
1761 .set_bpw = stm32f4_spi_set_bpw,
1762 .set_mode = stm32f4_spi_set_mode,
1763 .transfer_one_dma_start = stm32f4_spi_transfer_one_dma_start,
1764 .dma_tx_cb = stm32f4_spi_dma_tx_cb,
1765 .dma_rx_cb = stm32f4_spi_dma_rx_cb,
1766 .transfer_one_irq = stm32f4_spi_transfer_one_irq,
1767 .irq_handler_event = stm32f4_spi_irq_event,
1768 .irq_handler_thread = stm32f4_spi_irq_thread,
1769 .baud_rate_div_min = STM32F4_SPI_BR_DIV_MIN,
1770 .baud_rate_div_max = STM32F4_SPI_BR_DIV_MAX,
1771 .has_fifo = false,
1772};
1773
Cezary Gapinski55166852018-12-24 23:00:37 +01001774static const struct stm32_spi_cfg stm32h7_spi_cfg = {
1775 .regs = &stm32h7_spi_regspec,
1776 .get_fifo_size = stm32h7_spi_get_fifo_size,
1777 .get_bpw_mask = stm32h7_spi_get_bpw_mask,
1778 .disable = stm32h7_spi_disable,
1779 .config = stm32h7_spi_config,
1780 .set_bpw = stm32h7_spi_set_bpw,
1781 .set_mode = stm32h7_spi_set_mode,
1782 .set_data_idleness = stm32h7_spi_data_idleness,
1783 .set_number_of_data = stm32h7_spi_number_of_data,
1784 .transfer_one_dma_start = stm32h7_spi_transfer_one_dma_start,
1785 .dma_rx_cb = stm32h7_spi_dma_cb,
1786 .dma_tx_cb = stm32h7_spi_dma_cb,
1787 .transfer_one_irq = stm32h7_spi_transfer_one_irq,
1788 .irq_handler_thread = stm32h7_spi_irq_thread,
1789 .baud_rate_div_min = STM32H7_SPI_MBR_DIV_MIN,
1790 .baud_rate_div_max = STM32H7_SPI_MBR_DIV_MAX,
1791 .has_fifo = true,
1792};
1793
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001794static const struct of_device_id stm32_spi_of_match[] = {
Cezary Gapinski55166852018-12-24 23:00:37 +01001795 { .compatible = "st,stm32h7-spi", .data = (void *)&stm32h7_spi_cfg },
Cezary Gapinski00505ed2018-12-24 23:00:38 +01001796 { .compatible = "st,stm32f4-spi", .data = (void *)&stm32f4_spi_cfg },
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001797 {},
1798};
1799MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
1800
1801static int stm32_spi_probe(struct platform_device *pdev)
1802{
1803 struct spi_master *master;
1804 struct stm32_spi *spi;
1805 struct resource *res;
Etienne Carriere1c75cfd2021-02-05 19:59:29 +01001806 struct reset_control *rst;
Linus Walleij8a6553e2019-12-05 09:34:01 +01001807 int ret;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001808
Alain Volmat79c62462021-03-18 08:24:50 +01001809 master = devm_spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi));
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001810 if (!master) {
1811 dev_err(&pdev->dev, "spi master allocation failed\n");
1812 return -ENOMEM;
1813 }
1814 platform_set_drvdata(pdev, master);
1815
1816 spi = spi_master_get_devdata(master);
1817 spi->dev = &pdev->dev;
1818 spi->master = master;
1819 spin_lock_init(&spi->lock);
1820
Cezary Gapinski55166852018-12-24 23:00:37 +01001821 spi->cfg = (const struct stm32_spi_cfg *)
1822 of_match_device(pdev->dev.driver->of_match_table,
1823 &pdev->dev)->data;
1824
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001825 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1826 spi->base = devm_ioremap_resource(&pdev->dev, res);
Alain Volmat79c62462021-03-18 08:24:50 +01001827 if (IS_ERR(spi->base))
1828 return PTR_ERR(spi->base);
Cezary Gapinski55166852018-12-24 23:00:37 +01001829
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001830 spi->phys_addr = (dma_addr_t)res->start;
1831
1832 spi->irq = platform_get_irq(pdev, 0);
Alain Volmat79c62462021-03-18 08:24:50 +01001833 if (spi->irq <= 0)
1834 return dev_err_probe(&pdev->dev, spi->irq,
1835 "failed to get irq\n");
1836
Cezary Gapinski55166852018-12-24 23:00:37 +01001837 ret = devm_request_threaded_irq(&pdev->dev, spi->irq,
1838 spi->cfg->irq_handler_event,
1839 spi->cfg->irq_handler_thread,
1840 IRQF_ONESHOT, pdev->name, master);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001841 if (ret) {
1842 dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
1843 ret);
Alain Volmat79c62462021-03-18 08:24:50 +01001844 return ret;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001845 }
1846
Cezary Gapinskid4c91342018-12-24 23:00:28 +01001847 spi->clk = devm_clk_get(&pdev->dev, NULL);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001848 if (IS_ERR(spi->clk)) {
1849 ret = PTR_ERR(spi->clk);
1850 dev_err(&pdev->dev, "clk get failed: %d\n", ret);
Alain Volmat79c62462021-03-18 08:24:50 +01001851 return ret;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001852 }
1853
1854 ret = clk_prepare_enable(spi->clk);
1855 if (ret) {
1856 dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
Alain Volmat79c62462021-03-18 08:24:50 +01001857 return ret;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001858 }
1859 spi->clk_rate = clk_get_rate(spi->clk);
1860 if (!spi->clk_rate) {
1861 dev_err(&pdev->dev, "clk rate = 0\n");
1862 ret = -EINVAL;
Alexey Khoroshilov3dbb3ee2018-03-30 22:54:44 +03001863 goto err_clk_disable;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001864 }
1865
Alain Volmatc63b95b2021-02-05 19:59:30 +01001866 rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
1867 if (rst) {
1868 if (IS_ERR(rst)) {
1869 ret = dev_err_probe(&pdev->dev, PTR_ERR(rst),
1870 "failed to get reset\n");
1871 goto err_clk_disable;
1872 }
1873
Etienne Carriere1c75cfd2021-02-05 19:59:29 +01001874 reset_control_assert(rst);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001875 udelay(2);
Etienne Carriere1c75cfd2021-02-05 19:59:29 +01001876 reset_control_deassert(rst);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001877 }
1878
Cezary Gapinski55166852018-12-24 23:00:37 +01001879 if (spi->cfg->has_fifo)
1880 spi->fifo_size = spi->cfg->get_fifo_size(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001881
Cezary Gapinski55166852018-12-24 23:00:37 +01001882 ret = spi->cfg->config(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001883 if (ret) {
1884 dev_err(&pdev->dev, "controller configuration failed: %d\n",
1885 ret);
1886 goto err_clk_disable;
1887 }
1888
1889 master->dev.of_node = pdev->dev.of_node;
1890 master->auto_runtime_pm = true;
1891 master->bus_num = pdev->id;
Cezary Gapinskid6cea112018-12-24 23:00:31 +01001892 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
Cezary Gapinski6962b052018-12-24 23:00:32 +01001893 SPI_3WIRE;
Cezary Gapinski55166852018-12-24 23:00:37 +01001894 master->bits_per_word_mask = spi->cfg->get_bpw_mask(spi);
1895 master->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min;
1896 master->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max;
Linus Walleij8a6553e2019-12-05 09:34:01 +01001897 master->use_gpio_descriptors = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001898 master->prepare_message = stm32_spi_prepare_msg;
1899 master->transfer_one = stm32_spi_transfer_one;
1900 master->unprepare_message = stm32_spi_unprepare_msg;
dillon min61367d02020-05-25 11:45:47 +08001901 master->flags = SPI_MASTER_MUST_TX;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001902
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001903 spi->dma_tx = dma_request_chan(spi->dev, "tx");
1904 if (IS_ERR(spi->dma_tx)) {
1905 ret = PTR_ERR(spi->dma_tx);
1906 spi->dma_tx = NULL;
1907 if (ret == -EPROBE_DEFER)
1908 goto err_clk_disable;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001909
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001910 dev_warn(&pdev->dev, "failed to request tx dma channel\n");
1911 } else {
1912 master->dma_tx = spi->dma_tx;
1913 }
1914
1915 spi->dma_rx = dma_request_chan(spi->dev, "rx");
1916 if (IS_ERR(spi->dma_rx)) {
1917 ret = PTR_ERR(spi->dma_rx);
1918 spi->dma_rx = NULL;
1919 if (ret == -EPROBE_DEFER)
1920 goto err_dma_release;
1921
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001922 dev_warn(&pdev->dev, "failed to request rx dma channel\n");
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001923 } else {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001924 master->dma_rx = spi->dma_rx;
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001925 }
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001926
1927 if (spi->dma_tx || spi->dma_rx)
1928 master->can_dma = stm32_spi_can_dma;
1929
Amelie Delaunay038ac862017-06-27 17:45:18 +02001930 pm_runtime_set_active(&pdev->dev);
Alain Volmat7999d252021-07-07 10:27:00 +02001931 pm_runtime_get_noresume(&pdev->dev);
Amelie Delaunay038ac862017-06-27 17:45:18 +02001932 pm_runtime_enable(&pdev->dev);
1933
Antonio Borneo8d559a62021-03-12 11:34:46 +01001934 ret = spi_register_master(master);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001935 if (ret) {
1936 dev_err(&pdev->dev, "spi master registration failed: %d\n",
1937 ret);
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001938 goto err_pm_disable;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001939 }
1940
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001941 dev_info(&pdev->dev, "driver initialized\n");
1942
1943 return 0;
1944
Peter Ujfalusi0a454252019-12-12 15:55:50 +02001945err_pm_disable:
1946 pm_runtime_disable(&pdev->dev);
Alain Volmat7999d252021-07-07 10:27:00 +02001947 pm_runtime_put_noidle(&pdev->dev);
1948 pm_runtime_set_suspended(&pdev->dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001949err_dma_release:
1950 if (spi->dma_tx)
1951 dma_release_channel(spi->dma_tx);
1952 if (spi->dma_rx)
1953 dma_release_channel(spi->dma_rx);
1954err_clk_disable:
1955 clk_disable_unprepare(spi->clk);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001956
1957 return ret;
1958}
1959
1960static int stm32_spi_remove(struct platform_device *pdev)
1961{
1962 struct spi_master *master = platform_get_drvdata(pdev);
1963 struct stm32_spi *spi = spi_master_get_devdata(master);
1964
Alain Volmat7999d252021-07-07 10:27:00 +02001965 pm_runtime_get_sync(&pdev->dev);
1966
Antonio Borneo8d559a62021-03-12 11:34:46 +01001967 spi_unregister_master(master);
Cezary Gapinski55166852018-12-24 23:00:37 +01001968 spi->cfg->disable(spi);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001969
Alain Volmat7999d252021-07-07 10:27:00 +02001970 pm_runtime_disable(&pdev->dev);
1971 pm_runtime_put_noidle(&pdev->dev);
1972 pm_runtime_set_suspended(&pdev->dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001973 if (master->dma_tx)
1974 dma_release_channel(master->dma_tx);
1975 if (master->dma_rx)
1976 dma_release_channel(master->dma_rx);
1977
1978 clk_disable_unprepare(spi->clk);
1979
Amelie Delaunay038ac862017-06-27 17:45:18 +02001980
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02001981 pinctrl_pm_select_sleep_state(&pdev->dev);
1982
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001983 return 0;
1984}
1985
Alain Volmat12ef51b2021-03-12 11:35:29 +01001986static int __maybe_unused stm32_spi_runtime_suspend(struct device *dev)
Amelie Delaunay038ac862017-06-27 17:45:18 +02001987{
1988 struct spi_master *master = dev_get_drvdata(dev);
1989 struct stm32_spi *spi = spi_master_get_devdata(master);
1990
1991 clk_disable_unprepare(spi->clk);
1992
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02001993 return pinctrl_pm_select_sleep_state(dev);
Amelie Delaunay038ac862017-06-27 17:45:18 +02001994}
1995
Alain Volmat12ef51b2021-03-12 11:35:29 +01001996static int __maybe_unused stm32_spi_runtime_resume(struct device *dev)
Amelie Delaunay038ac862017-06-27 17:45:18 +02001997{
1998 struct spi_master *master = dev_get_drvdata(dev);
1999 struct stm32_spi *spi = spi_master_get_devdata(master);
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02002000 int ret;
2001
2002 ret = pinctrl_pm_select_default_state(dev);
2003 if (ret)
2004 return ret;
Amelie Delaunay038ac862017-06-27 17:45:18 +02002005
2006 return clk_prepare_enable(spi->clk);
2007}
Amelie Delaunay038ac862017-06-27 17:45:18 +02002008
Alain Volmat12ef51b2021-03-12 11:35:29 +01002009static int __maybe_unused stm32_spi_suspend(struct device *dev)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002010{
2011 struct spi_master *master = dev_get_drvdata(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002012 int ret;
2013
2014 ret = spi_master_suspend(master);
2015 if (ret)
2016 return ret;
2017
Amelie Delaunay038ac862017-06-27 17:45:18 +02002018 return pm_runtime_force_suspend(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002019}
2020
Alain Volmat12ef51b2021-03-12 11:35:29 +01002021static int __maybe_unused stm32_spi_resume(struct device *dev)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002022{
2023 struct spi_master *master = dev_get_drvdata(dev);
2024 struct stm32_spi *spi = spi_master_get_devdata(master);
2025 int ret;
2026
Amelie Delaunay038ac862017-06-27 17:45:18 +02002027 ret = pm_runtime_force_resume(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002028 if (ret)
2029 return ret;
Amelie Delaunay038ac862017-06-27 17:45:18 +02002030
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002031 ret = spi_master_resume(master);
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02002032 if (ret) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002033 clk_disable_unprepare(spi->clk);
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02002034 return ret;
2035 }
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002036
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02002037 ret = pm_runtime_get_sync(dev);
Dan Carpenterc170a5a2020-09-09 12:43:04 +03002038 if (ret < 0) {
Zhang Qilong900ccdc2020-11-06 09:52:17 +08002039 pm_runtime_put_noidle(dev);
Amelie Delaunaydb96bf92020-08-10 09:12:37 +02002040 dev_err(dev, "Unable to power device:%d\n", ret);
2041 return ret;
2042 }
2043
2044 spi->cfg->config(spi);
2045
2046 pm_runtime_mark_last_busy(dev);
2047 pm_runtime_put_autosuspend(dev);
2048
2049 return 0;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002050}
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002051
Amelie Delaunay038ac862017-06-27 17:45:18 +02002052static const struct dev_pm_ops stm32_spi_pm_ops = {
2053 SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
2054 SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend,
2055 stm32_spi_runtime_resume, NULL)
2056};
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02002057
2058static struct platform_driver stm32_spi_driver = {
2059 .probe = stm32_spi_probe,
2060 .remove = stm32_spi_remove,
2061 .driver = {
2062 .name = DRIVER_NAME,
2063 .pm = &stm32_spi_pm_ops,
2064 .of_match_table = stm32_spi_of_match,
2065 },
2066};
2067
2068module_platform_driver(stm32_spi_driver);
2069
2070MODULE_ALIAS("platform:" DRIVER_NAME);
2071MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
2072MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
2073MODULE_LICENSE("GPL v2");