blob: 8310f14fe273fe3703d0b8a6a7e51d2d80d1ea7f [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 Delaunaydcbe0d82017-06-21 16:32:06 +02008#include <linux/debugfs.h>
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/dmaengine.h>
12#include <linux/gpio.h>
13#include <linux/interrupt.h>
14#include <linux/iopoll.h>
15#include <linux/module.h>
16#include <linux/of_platform.h>
Amelie Delaunay038ac862017-06-27 17:45:18 +020017#include <linux/pm_runtime.h>
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +020018#include <linux/reset.h>
19#include <linux/spi/spi.h>
20
21#define DRIVER_NAME "spi_stm32"
22
23/* STM32 SPI registers */
24#define STM32_SPI_CR1 0x00
25#define STM32_SPI_CR2 0x04
26#define STM32_SPI_CFG1 0x08
27#define STM32_SPI_CFG2 0x0C
28#define STM32_SPI_IER 0x10
29#define STM32_SPI_SR 0x14
30#define STM32_SPI_IFCR 0x18
31#define STM32_SPI_TXDR 0x20
32#define STM32_SPI_RXDR 0x30
33#define STM32_SPI_I2SCFGR 0x50
34
35/* STM32_SPI_CR1 bit fields */
36#define SPI_CR1_SPE BIT(0)
37#define SPI_CR1_MASRX BIT(8)
38#define SPI_CR1_CSTART BIT(9)
39#define SPI_CR1_CSUSP BIT(10)
40#define SPI_CR1_HDDIR BIT(11)
41#define SPI_CR1_SSI BIT(12)
42
43/* STM32_SPI_CR2 bit fields */
44#define SPI_CR2_TSIZE_SHIFT 0
45#define SPI_CR2_TSIZE GENMASK(15, 0)
46
47/* STM32_SPI_CFG1 bit fields */
48#define SPI_CFG1_DSIZE_SHIFT 0
49#define SPI_CFG1_DSIZE GENMASK(4, 0)
50#define SPI_CFG1_FTHLV_SHIFT 5
51#define SPI_CFG1_FTHLV GENMASK(8, 5)
52#define SPI_CFG1_RXDMAEN BIT(14)
53#define SPI_CFG1_TXDMAEN BIT(15)
54#define SPI_CFG1_MBR_SHIFT 28
55#define SPI_CFG1_MBR GENMASK(30, 28)
56#define SPI_CFG1_MBR_MIN 0
57#define SPI_CFG1_MBR_MAX (GENMASK(30, 28) >> 28)
58
59/* STM32_SPI_CFG2 bit fields */
60#define SPI_CFG2_MIDI_SHIFT 4
61#define SPI_CFG2_MIDI GENMASK(7, 4)
62#define SPI_CFG2_COMM_SHIFT 17
63#define SPI_CFG2_COMM GENMASK(18, 17)
64#define SPI_CFG2_SP_SHIFT 19
65#define SPI_CFG2_SP GENMASK(21, 19)
66#define SPI_CFG2_MASTER BIT(22)
67#define SPI_CFG2_LSBFRST BIT(23)
68#define SPI_CFG2_CPHA BIT(24)
69#define SPI_CFG2_CPOL BIT(25)
70#define SPI_CFG2_SSM BIT(26)
71#define SPI_CFG2_AFCNTR BIT(31)
72
73/* STM32_SPI_IER bit fields */
74#define SPI_IER_RXPIE BIT(0)
75#define SPI_IER_TXPIE BIT(1)
76#define SPI_IER_DXPIE BIT(2)
77#define SPI_IER_EOTIE BIT(3)
78#define SPI_IER_TXTFIE BIT(4)
79#define SPI_IER_OVRIE BIT(6)
80#define SPI_IER_MODFIE BIT(9)
81#define SPI_IER_ALL GENMASK(10, 0)
82
83/* STM32_SPI_SR bit fields */
84#define SPI_SR_RXP BIT(0)
85#define SPI_SR_TXP BIT(1)
86#define SPI_SR_EOT BIT(3)
87#define SPI_SR_OVR BIT(6)
88#define SPI_SR_MODF BIT(9)
89#define SPI_SR_SUSP BIT(11)
90#define SPI_SR_RXPLVL_SHIFT 13
91#define SPI_SR_RXPLVL GENMASK(14, 13)
92#define SPI_SR_RXWNE BIT(15)
93
94/* STM32_SPI_IFCR bit fields */
95#define SPI_IFCR_ALL GENMASK(11, 3)
96
97/* STM32_SPI_I2SCFGR bit fields */
98#define SPI_I2SCFGR_I2SMOD BIT(0)
99
100/* SPI Master Baud Rate min/max divisor */
101#define SPI_MBR_DIV_MIN (2 << SPI_CFG1_MBR_MIN)
102#define SPI_MBR_DIV_MAX (2 << SPI_CFG1_MBR_MAX)
103
104/* SPI Communication mode */
105#define SPI_FULL_DUPLEX 0
106#define SPI_SIMPLEX_TX 1
107#define SPI_SIMPLEX_RX 2
108#define SPI_HALF_DUPLEX 3
109
110#define SPI_1HZ_NS 1000000000
111
112/**
113 * struct stm32_spi - private data of the SPI controller
114 * @dev: driver model representation of the controller
115 * @master: controller master interface
116 * @base: virtual memory area
117 * @clk: hw kernel clock feeding the SPI clock generator
118 * @clk_rate: rate of the hw kernel clock feeding the SPI clock generator
119 * @rst: SPI controller reset line
120 * @lock: prevent I/O concurrent access
121 * @irq: SPI controller interrupt line
122 * @fifo_size: size of the embedded fifo in bytes
123 * @cur_midi: master inter-data idleness in ns
124 * @cur_speed: speed configured in Hz
125 * @cur_bpw: number of bits in a single SPI data frame
126 * @cur_fthlv: fifo threshold level (data frames in a single data packet)
127 * @cur_comm: SPI communication mode
128 * @cur_xferlen: current transfer length in bytes
129 * @cur_usedma: boolean to know if dma is used in current transfer
130 * @tx_buf: data to be written, or NULL
131 * @rx_buf: data to be read, or NULL
132 * @tx_len: number of data to be written in bytes
133 * @rx_len: number of data to be read in bytes
134 * @dma_tx: dma channel for TX transfer
135 * @dma_rx: dma channel for RX transfer
136 * @phys_addr: SPI registers physical base address
137 */
138struct stm32_spi {
139 struct device *dev;
140 struct spi_master *master;
141 void __iomem *base;
142 struct clk *clk;
143 u32 clk_rate;
144 struct reset_control *rst;
145 spinlock_t lock; /* prevent I/O concurrent access */
146 int irq;
147 unsigned int fifo_size;
148
149 unsigned int cur_midi;
150 unsigned int cur_speed;
151 unsigned int cur_bpw;
152 unsigned int cur_fthlv;
153 unsigned int cur_comm;
154 unsigned int cur_xferlen;
155 bool cur_usedma;
156
157 const void *tx_buf;
158 void *rx_buf;
159 int tx_len;
160 int rx_len;
161 struct dma_chan *dma_tx;
162 struct dma_chan *dma_rx;
163 dma_addr_t phys_addr;
164};
165
166static inline void stm32_spi_set_bits(struct stm32_spi *spi,
167 u32 offset, u32 bits)
168{
169 writel_relaxed(readl_relaxed(spi->base + offset) | bits,
170 spi->base + offset);
171}
172
173static inline void stm32_spi_clr_bits(struct stm32_spi *spi,
174 u32 offset, u32 bits)
175{
176 writel_relaxed(readl_relaxed(spi->base + offset) & ~bits,
177 spi->base + offset);
178}
179
180/**
181 * stm32_spi_get_fifo_size - Return fifo size
182 * @spi: pointer to the spi controller data structure
183 */
184static int stm32_spi_get_fifo_size(struct stm32_spi *spi)
185{
186 unsigned long flags;
187 u32 count = 0;
188
189 spin_lock_irqsave(&spi->lock, flags);
190
191 stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_SPE);
192
193 while (readl_relaxed(spi->base + STM32_SPI_SR) & SPI_SR_TXP)
194 writeb_relaxed(++count, spi->base + STM32_SPI_TXDR);
195
196 stm32_spi_clr_bits(spi, STM32_SPI_CR1, SPI_CR1_SPE);
197
198 spin_unlock_irqrestore(&spi->lock, flags);
199
200 dev_dbg(spi->dev, "%d x 8-bit fifo size\n", count);
201
202 return count;
203}
204
205/**
206 * stm32_spi_get_bpw_mask - Return bits per word mask
207 * @spi: pointer to the spi controller data structure
208 */
209static int stm32_spi_get_bpw_mask(struct stm32_spi *spi)
210{
211 unsigned long flags;
212 u32 cfg1, max_bpw;
213
214 spin_lock_irqsave(&spi->lock, flags);
215
216 /*
217 * The most significant bit at DSIZE bit field is reserved when the
218 * maximum data size of periperal instances is limited to 16-bit
219 */
220 stm32_spi_set_bits(spi, STM32_SPI_CFG1, SPI_CFG1_DSIZE);
221
222 cfg1 = readl_relaxed(spi->base + STM32_SPI_CFG1);
223 max_bpw = (cfg1 & SPI_CFG1_DSIZE) >> SPI_CFG1_DSIZE_SHIFT;
224 max_bpw += 1;
225
226 spin_unlock_irqrestore(&spi->lock, flags);
227
228 dev_dbg(spi->dev, "%d-bit maximum data frame\n", max_bpw);
229
230 return SPI_BPW_RANGE_MASK(4, max_bpw);
231}
232
233/**
234 * stm32_spi_prepare_mbr - Determine SPI_CFG1.MBR value
235 * @spi: pointer to the spi controller data structure
236 * @speed_hz: requested speed
237 *
238 * Return SPI_CFG1.MBR value in case of success or -EINVAL
239 */
240static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz)
241{
242 u32 div, mbrdiv;
243
244 div = DIV_ROUND_UP(spi->clk_rate, speed_hz);
245
246 /*
247 * SPI framework set xfer->speed_hz to master->max_speed_hz if
248 * xfer->speed_hz is greater than master->max_speed_hz, and it returns
249 * an error when xfer->speed_hz is lower than master->min_speed_hz, so
250 * no need to check it there.
251 * However, we need to ensure the following calculations.
252 */
Christos Gkekas8b5d7292017-09-10 14:55:29 +0100253 if (div < SPI_MBR_DIV_MIN ||
254 div > SPI_MBR_DIV_MAX)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200255 return -EINVAL;
256
257 /* Determine the first power of 2 greater than or equal to div */
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200258 if (div & (div - 1))
259 mbrdiv = fls(div);
260 else
261 mbrdiv = fls(div) - 1;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200262
263 spi->cur_speed = spi->clk_rate / (1 << mbrdiv);
264
265 return mbrdiv - 1;
266}
267
268/**
269 * stm32_spi_prepare_fthlv - Determine FIFO threshold level
270 * @spi: pointer to the spi controller data structure
271 */
272static u32 stm32_spi_prepare_fthlv(struct stm32_spi *spi)
273{
274 u32 fthlv, half_fifo;
275
276 /* data packet should not exceed 1/2 of fifo space */
277 half_fifo = (spi->fifo_size / 2);
278
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200279 if (spi->cur_bpw <= 8)
280 fthlv = half_fifo;
281 else if (spi->cur_bpw <= 16)
282 fthlv = half_fifo / 2;
283 else
284 fthlv = half_fifo / 4;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200285
286 /* align packet size with data registers access */
287 if (spi->cur_bpw > 8)
288 fthlv -= (fthlv % 2); /* multiple of 2 */
289 else
290 fthlv -= (fthlv % 4); /* multiple of 4 */
291
292 return fthlv;
293}
294
295/**
296 * stm32_spi_write_txfifo - Write bytes in Transmit Data Register
297 * @spi: pointer to the spi controller data structure
298 *
299 * Read from tx_buf depends on remaining bytes to avoid to read beyond
300 * tx_buf end.
301 */
302static void stm32_spi_write_txfifo(struct stm32_spi *spi)
303{
304 while ((spi->tx_len > 0) &&
305 (readl_relaxed(spi->base + STM32_SPI_SR) & SPI_SR_TXP)) {
306 u32 offs = spi->cur_xferlen - spi->tx_len;
307
308 if (spi->tx_len >= sizeof(u32)) {
309 const u32 *tx_buf32 = (const u32 *)(spi->tx_buf + offs);
310
311 writel_relaxed(*tx_buf32, spi->base + STM32_SPI_TXDR);
312 spi->tx_len -= sizeof(u32);
313 } else if (spi->tx_len >= sizeof(u16)) {
314 const u16 *tx_buf16 = (const u16 *)(spi->tx_buf + offs);
315
316 writew_relaxed(*tx_buf16, spi->base + STM32_SPI_TXDR);
317 spi->tx_len -= sizeof(u16);
318 } else {
319 const u8 *tx_buf8 = (const u8 *)(spi->tx_buf + offs);
320
321 writeb_relaxed(*tx_buf8, spi->base + STM32_SPI_TXDR);
322 spi->tx_len -= sizeof(u8);
323 }
324 }
325
326 dev_dbg(spi->dev, "%s: %d bytes left\n", __func__, spi->tx_len);
327}
328
329/**
330 * stm32_spi_read_rxfifo - Read bytes in Receive Data Register
331 * @spi: pointer to the spi controller data structure
332 *
333 * Write in rx_buf depends on remaining bytes to avoid to write beyond
334 * rx_buf end.
335 */
336static void stm32_spi_read_rxfifo(struct stm32_spi *spi, bool flush)
337{
338 u32 sr = readl_relaxed(spi->base + STM32_SPI_SR);
339 u32 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
340
341 while ((spi->rx_len > 0) &&
342 ((sr & SPI_SR_RXP) ||
343 (flush && ((sr & SPI_SR_RXWNE) || (rxplvl > 0))))) {
344 u32 offs = spi->cur_xferlen - spi->rx_len;
345
346 if ((spi->rx_len >= sizeof(u32)) ||
347 (flush && (sr & SPI_SR_RXWNE))) {
348 u32 *rx_buf32 = (u32 *)(spi->rx_buf + offs);
349
350 *rx_buf32 = readl_relaxed(spi->base + STM32_SPI_RXDR);
351 spi->rx_len -= sizeof(u32);
352 } else if ((spi->rx_len >= sizeof(u16)) ||
353 (flush && (rxplvl >= 2 || spi->cur_bpw > 8))) {
354 u16 *rx_buf16 = (u16 *)(spi->rx_buf + offs);
355
356 *rx_buf16 = readw_relaxed(spi->base + STM32_SPI_RXDR);
357 spi->rx_len -= sizeof(u16);
358 } else {
359 u8 *rx_buf8 = (u8 *)(spi->rx_buf + offs);
360
361 *rx_buf8 = readb_relaxed(spi->base + STM32_SPI_RXDR);
362 spi->rx_len -= sizeof(u8);
363 }
364
365 sr = readl_relaxed(spi->base + STM32_SPI_SR);
366 rxplvl = (sr & SPI_SR_RXPLVL) >> SPI_SR_RXPLVL_SHIFT;
367 }
368
369 dev_dbg(spi->dev, "%s%s: %d bytes left\n", __func__,
370 flush ? "(flush)" : "", spi->rx_len);
371}
372
373/**
374 * stm32_spi_enable - Enable SPI controller
375 * @spi: pointer to the spi controller data structure
376 *
377 * SPI data transfer is enabled but spi_ker_ck is idle.
378 * SPI_CFG1 and SPI_CFG2 are now write protected.
379 */
380static void stm32_spi_enable(struct stm32_spi *spi)
381{
382 dev_dbg(spi->dev, "enable controller\n");
383
384 stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_SPE);
385}
386
387/**
388 * stm32_spi_disable - Disable SPI controller
389 * @spi: pointer to the spi controller data structure
390 *
391 * RX-Fifo is flushed when SPI controller is disabled. To prevent any data
392 * loss, use stm32_spi_read_rxfifo(flush) to read the remaining bytes in
393 * RX-Fifo.
394 */
395static void stm32_spi_disable(struct stm32_spi *spi)
396{
397 unsigned long flags;
398 u32 cr1, sr;
399
400 dev_dbg(spi->dev, "disable controller\n");
401
402 spin_lock_irqsave(&spi->lock, flags);
403
404 cr1 = readl_relaxed(spi->base + STM32_SPI_CR1);
405
406 if (!(cr1 & SPI_CR1_SPE)) {
407 spin_unlock_irqrestore(&spi->lock, flags);
408 return;
409 }
410
411 /* Wait on EOT or suspend the flow */
412 if (readl_relaxed_poll_timeout_atomic(spi->base + STM32_SPI_SR,
413 sr, !(sr & SPI_SR_EOT),
414 10, 100000) < 0) {
415 if (cr1 & SPI_CR1_CSTART) {
416 writel_relaxed(cr1 | SPI_CR1_CSUSP,
417 spi->base + STM32_SPI_CR1);
418 if (readl_relaxed_poll_timeout_atomic(
419 spi->base + STM32_SPI_SR,
420 sr, !(sr & SPI_SR_SUSP),
421 10, 100000) < 0)
422 dev_warn(spi->dev,
423 "Suspend request timeout\n");
424 }
425 }
426
427 if (!spi->cur_usedma && spi->rx_buf && (spi->rx_len > 0))
428 stm32_spi_read_rxfifo(spi, true);
429
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100430 if (spi->cur_usedma && spi->dma_tx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200431 dmaengine_terminate_all(spi->dma_tx);
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100432 if (spi->cur_usedma && spi->dma_rx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200433 dmaengine_terminate_all(spi->dma_rx);
434
435 stm32_spi_clr_bits(spi, STM32_SPI_CR1, SPI_CR1_SPE);
436
437 stm32_spi_clr_bits(spi, STM32_SPI_CFG1, SPI_CFG1_TXDMAEN |
438 SPI_CFG1_RXDMAEN);
439
440 /* Disable interrupts and clear status flags */
441 writel_relaxed(0, spi->base + STM32_SPI_IER);
442 writel_relaxed(SPI_IFCR_ALL, spi->base + STM32_SPI_IFCR);
443
444 spin_unlock_irqrestore(&spi->lock, flags);
445}
446
447/**
448 * stm32_spi_can_dma - Determine if the transfer is eligible for DMA use
449 *
450 * If the current transfer size is greater than fifo size, use DMA.
451 */
452static bool stm32_spi_can_dma(struct spi_master *master,
453 struct spi_device *spi_dev,
454 struct spi_transfer *transfer)
455{
456 struct stm32_spi *spi = spi_master_get_devdata(master);
457
458 dev_dbg(spi->dev, "%s: %s\n", __func__,
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200459 (transfer->len > spi->fifo_size) ? "true" : "false");
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200460
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200461 return (transfer->len > spi->fifo_size);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200462}
463
464/**
465 * stm32_spi_irq - Interrupt handler for SPI controller events
466 * @irq: interrupt line
467 * @dev_id: SPI controller master interface
468 */
469static irqreturn_t stm32_spi_irq(int irq, void *dev_id)
470{
471 struct spi_master *master = dev_id;
472 struct stm32_spi *spi = spi_master_get_devdata(master);
473 u32 sr, ier, mask;
474 unsigned long flags;
475 bool end = false;
476
477 spin_lock_irqsave(&spi->lock, flags);
478
479 sr = readl_relaxed(spi->base + STM32_SPI_SR);
480 ier = readl_relaxed(spi->base + STM32_SPI_IER);
481
482 mask = ier;
483 /* EOTIE is triggered on EOT, SUSP and TXC events. */
484 mask |= SPI_SR_SUSP;
485 /*
486 * When TXTF is set, DXPIE and TXPIE are cleared. So in case of
487 * Full-Duplex, need to poll RXP event to know if there are remaining
488 * data, before disabling SPI.
489 */
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200490 if (spi->rx_buf && !spi->cur_usedma)
491 mask |= SPI_SR_RXP;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200492
493 if (!(sr & mask)) {
494 dev_dbg(spi->dev, "spurious IT (sr=0x%08x, ier=0x%08x)\n",
495 sr, ier);
496 spin_unlock_irqrestore(&spi->lock, flags);
497 return IRQ_NONE;
498 }
499
500 if (sr & SPI_SR_SUSP) {
501 dev_warn(spi->dev, "Communication suspended\n");
502 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
503 stm32_spi_read_rxfifo(spi, false);
Amelie Delaunayc67ad362017-06-27 17:45:19 +0200504 /*
505 * If communication is suspended while using DMA, it means
506 * that something went wrong, so stop the current transfer
507 */
508 if (spi->cur_usedma)
509 end = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200510 }
511
512 if (sr & SPI_SR_MODF) {
513 dev_warn(spi->dev, "Mode fault: transfer aborted\n");
514 end = true;
515 }
516
517 if (sr & SPI_SR_OVR) {
518 dev_warn(spi->dev, "Overrun: received value discarded\n");
519 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
520 stm32_spi_read_rxfifo(spi, false);
Amelie Delaunayc67ad362017-06-27 17:45:19 +0200521 /*
522 * If overrun is detected while using DMA, it means that
523 * something went wrong, so stop the current transfer
524 */
525 if (spi->cur_usedma)
526 end = true;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200527 }
528
529 if (sr & SPI_SR_EOT) {
530 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
531 stm32_spi_read_rxfifo(spi, true);
532 end = true;
533 }
534
535 if (sr & SPI_SR_TXP)
536 if (!spi->cur_usedma && (spi->tx_buf && (spi->tx_len > 0)))
537 stm32_spi_write_txfifo(spi);
538
539 if (sr & SPI_SR_RXP)
540 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0)))
541 stm32_spi_read_rxfifo(spi, false);
542
543 writel_relaxed(mask, spi->base + STM32_SPI_IFCR);
544
545 spin_unlock_irqrestore(&spi->lock, flags);
546
547 if (end) {
548 spi_finalize_current_transfer(master);
549 stm32_spi_disable(spi);
550 }
551
552 return IRQ_HANDLED;
553}
554
555/**
556 * stm32_spi_setup - setup device chip select
557 */
558static int stm32_spi_setup(struct spi_device *spi_dev)
559{
560 int ret = 0;
561
562 if (!gpio_is_valid(spi_dev->cs_gpio)) {
563 dev_err(&spi_dev->dev, "%d is not a valid gpio\n",
564 spi_dev->cs_gpio);
565 return -EINVAL;
566 }
567
568 dev_dbg(&spi_dev->dev, "%s: set gpio%d output %s\n", __func__,
569 spi_dev->cs_gpio,
570 (spi_dev->mode & SPI_CS_HIGH) ? "low" : "high");
571
572 ret = gpio_direction_output(spi_dev->cs_gpio,
573 !(spi_dev->mode & SPI_CS_HIGH));
574
575 return ret;
576}
577
578/**
579 * stm32_spi_prepare_msg - set up the controller to transfer a single message
580 */
581static int stm32_spi_prepare_msg(struct spi_master *master,
582 struct spi_message *msg)
583{
584 struct stm32_spi *spi = spi_master_get_devdata(master);
585 struct spi_device *spi_dev = msg->spi;
586 struct device_node *np = spi_dev->dev.of_node;
587 unsigned long flags;
588 u32 cfg2_clrb = 0, cfg2_setb = 0;
589
590 /* SPI slave device may need time between data frames */
591 spi->cur_midi = 0;
Amelie Delaunay042c1c62017-06-27 17:45:16 +0200592 if (np && !of_property_read_u32(np, "st,spi-midi-ns", &spi->cur_midi))
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200593 dev_dbg(spi->dev, "%dns inter-data idleness\n", spi->cur_midi);
594
595 if (spi_dev->mode & SPI_CPOL)
596 cfg2_setb |= SPI_CFG2_CPOL;
597 else
598 cfg2_clrb |= SPI_CFG2_CPOL;
599
600 if (spi_dev->mode & SPI_CPHA)
601 cfg2_setb |= SPI_CFG2_CPHA;
602 else
603 cfg2_clrb |= SPI_CFG2_CPHA;
604
605 if (spi_dev->mode & SPI_LSB_FIRST)
606 cfg2_setb |= SPI_CFG2_LSBFRST;
607 else
608 cfg2_clrb |= SPI_CFG2_LSBFRST;
609
610 dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n",
611 spi_dev->mode & SPI_CPOL,
612 spi_dev->mode & SPI_CPHA,
613 spi_dev->mode & SPI_LSB_FIRST,
614 spi_dev->mode & SPI_CS_HIGH);
615
616 spin_lock_irqsave(&spi->lock, flags);
617
618 if (cfg2_clrb || cfg2_setb)
619 writel_relaxed(
620 (readl_relaxed(spi->base + STM32_SPI_CFG2) &
621 ~cfg2_clrb) | cfg2_setb,
622 spi->base + STM32_SPI_CFG2);
623
624 spin_unlock_irqrestore(&spi->lock, flags);
625
626 return 0;
627}
628
629/**
630 * stm32_spi_dma_cb - dma callback
631 *
632 * DMA callback is called when the transfer is complete or when an error
633 * occurs. If the transfer is complete, EOT flag is raised.
634 */
635static void stm32_spi_dma_cb(void *data)
636{
637 struct stm32_spi *spi = data;
638 unsigned long flags;
639 u32 sr;
640
641 spin_lock_irqsave(&spi->lock, flags);
642
643 sr = readl_relaxed(spi->base + STM32_SPI_SR);
644
645 spin_unlock_irqrestore(&spi->lock, flags);
646
Amelie Delaunayc67ad362017-06-27 17:45:19 +0200647 if (!(sr & SPI_SR_EOT))
648 dev_warn(spi->dev, "DMA error (sr=0x%08x)\n", sr);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200649
Amelie Delaunayc67ad362017-06-27 17:45:19 +0200650 /* Now wait for EOT, or SUSP or OVR in case of error */
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200651}
652
653/**
654 * stm32_spi_dma_config - configure dma slave channel depending on current
655 * transfer bits_per_word.
656 */
657static void stm32_spi_dma_config(struct stm32_spi *spi,
658 struct dma_slave_config *dma_conf,
659 enum dma_transfer_direction dir)
660{
661 enum dma_slave_buswidth buswidth;
662 u32 maxburst;
663
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200664 if (spi->cur_bpw <= 8)
665 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
666 else if (spi->cur_bpw <= 16)
667 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
668 else
669 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200670
671 /* Valid for DMA Half or Full Fifo threshold */
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200672 if (spi->cur_fthlv == 2)
673 maxburst = 1;
674 else
675 maxburst = spi->cur_fthlv;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200676
677 memset(dma_conf, 0, sizeof(struct dma_slave_config));
678 dma_conf->direction = dir;
679 if (dma_conf->direction == DMA_DEV_TO_MEM) { /* RX */
680 dma_conf->src_addr = spi->phys_addr + STM32_SPI_RXDR;
681 dma_conf->src_addr_width = buswidth;
682 dma_conf->src_maxburst = maxburst;
683
684 dev_dbg(spi->dev, "Rx DMA config buswidth=%d, maxburst=%d\n",
685 buswidth, maxburst);
686 } else if (dma_conf->direction == DMA_MEM_TO_DEV) { /* TX */
687 dma_conf->dst_addr = spi->phys_addr + STM32_SPI_TXDR;
688 dma_conf->dst_addr_width = buswidth;
689 dma_conf->dst_maxburst = maxburst;
690
691 dev_dbg(spi->dev, "Tx DMA config buswidth=%d, maxburst=%d\n",
692 buswidth, maxburst);
693 }
694}
695
696/**
697 * stm32_spi_transfer_one_irq - transfer a single spi_transfer using
698 * interrupts
699 *
700 * It must returns 0 if the transfer is finished or 1 if the transfer is still
701 * in progress.
702 */
703static int stm32_spi_transfer_one_irq(struct stm32_spi *spi)
704{
705 unsigned long flags;
706 u32 ier = 0;
707
708 /* Enable the interrupts relative to the current communication mode */
709 if (spi->tx_buf && spi->rx_buf) /* Full Duplex */
710 ier |= SPI_IER_DXPIE;
711 else if (spi->tx_buf) /* Half-Duplex TX dir or Simplex TX */
712 ier |= SPI_IER_TXPIE;
713 else if (spi->rx_buf) /* Half-Duplex RX dir or Simplex RX */
714 ier |= SPI_IER_RXPIE;
715
716 /* Enable the interrupts relative to the end of transfer */
717 ier |= SPI_IER_EOTIE | SPI_IER_TXTFIE | SPI_IER_OVRIE | SPI_IER_MODFIE;
718
719 spin_lock_irqsave(&spi->lock, flags);
720
721 stm32_spi_enable(spi);
722
723 /* Be sure to have data in fifo before starting data transfer */
724 if (spi->tx_buf)
725 stm32_spi_write_txfifo(spi);
726
727 stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_CSTART);
728
729 writel_relaxed(ier, spi->base + STM32_SPI_IER);
730
731 spin_unlock_irqrestore(&spi->lock, flags);
732
733 return 1;
734}
735
736/**
737 * stm32_spi_transfer_one_dma - transfer a single spi_transfer using DMA
738 *
739 * It must returns 0 if the transfer is finished or 1 if the transfer is still
740 * in progress.
741 */
742static int stm32_spi_transfer_one_dma(struct stm32_spi *spi,
743 struct spi_transfer *xfer)
744{
745 struct dma_slave_config tx_dma_conf, rx_dma_conf;
746 struct dma_async_tx_descriptor *tx_dma_desc, *rx_dma_desc;
747 unsigned long flags;
748 u32 ier = 0;
749
750 spin_lock_irqsave(&spi->lock, flags);
751
752 rx_dma_desc = NULL;
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100753 if (spi->rx_buf && spi->dma_rx) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200754 stm32_spi_dma_config(spi, &rx_dma_conf, DMA_DEV_TO_MEM);
755 dmaengine_slave_config(spi->dma_rx, &rx_dma_conf);
756
757 /* Enable Rx DMA request */
758 stm32_spi_set_bits(spi, STM32_SPI_CFG1, SPI_CFG1_RXDMAEN);
759
760 rx_dma_desc = dmaengine_prep_slave_sg(
761 spi->dma_rx, xfer->rx_sg.sgl,
762 xfer->rx_sg.nents,
763 rx_dma_conf.direction,
764 DMA_PREP_INTERRUPT);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200765 }
766
767 tx_dma_desc = NULL;
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100768 if (spi->tx_buf && spi->dma_tx) {
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200769 stm32_spi_dma_config(spi, &tx_dma_conf, DMA_MEM_TO_DEV);
770 dmaengine_slave_config(spi->dma_tx, &tx_dma_conf);
771
772 tx_dma_desc = dmaengine_prep_slave_sg(
773 spi->dma_tx, xfer->tx_sg.sgl,
774 xfer->tx_sg.nents,
775 tx_dma_conf.direction,
776 DMA_PREP_INTERRUPT);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200777 }
778
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100779 if ((spi->tx_buf && spi->dma_tx && !tx_dma_desc) ||
780 (spi->rx_buf && spi->dma_rx && !rx_dma_desc))
781 goto dma_desc_error;
782
783 if (spi->cur_comm == SPI_FULL_DUPLEX && (!tx_dma_desc || !rx_dma_desc))
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200784 goto dma_desc_error;
785
786 if (rx_dma_desc) {
Amelie Delaunay7b821a62017-06-27 17:45:20 +0200787 rx_dma_desc->callback = stm32_spi_dma_cb;
788 rx_dma_desc->callback_param = spi;
789
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200790 if (dma_submit_error(dmaengine_submit(rx_dma_desc))) {
791 dev_err(spi->dev, "Rx DMA submit failed\n");
792 goto dma_desc_error;
793 }
794 /* Enable Rx DMA channel */
795 dma_async_issue_pending(spi->dma_rx);
796 }
797
798 if (tx_dma_desc) {
Amelie Delaunay7b821a62017-06-27 17:45:20 +0200799 if (spi->cur_comm == SPI_SIMPLEX_TX) {
800 tx_dma_desc->callback = stm32_spi_dma_cb;
801 tx_dma_desc->callback_param = spi;
802 }
803
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200804 if (dma_submit_error(dmaengine_submit(tx_dma_desc))) {
805 dev_err(spi->dev, "Tx DMA submit failed\n");
806 goto dma_submit_error;
807 }
808 /* Enable Tx DMA channel */
809 dma_async_issue_pending(spi->dma_tx);
810
811 /* Enable Tx DMA request */
812 stm32_spi_set_bits(spi, STM32_SPI_CFG1, SPI_CFG1_TXDMAEN);
813 }
814
815 /* Enable the interrupts relative to the end of transfer */
816 ier |= SPI_IER_EOTIE | SPI_IER_TXTFIE | SPI_IER_OVRIE | SPI_IER_MODFIE;
817 writel_relaxed(ier, spi->base + STM32_SPI_IER);
818
819 stm32_spi_enable(spi);
820
821 stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_CSTART);
822
823 spin_unlock_irqrestore(&spi->lock, flags);
824
825 return 1;
826
827dma_submit_error:
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100828 if (spi->dma_rx)
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200829 dmaengine_terminate_all(spi->dma_rx);
830
831dma_desc_error:
832 stm32_spi_clr_bits(spi, STM32_SPI_CFG1, SPI_CFG1_RXDMAEN);
833
834 spin_unlock_irqrestore(&spi->lock, flags);
835
836 dev_info(spi->dev, "DMA issue: fall back to irq transfer\n");
837
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100838 spi->cur_usedma = false;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200839 return stm32_spi_transfer_one_irq(spi);
840}
841
842/**
843 * stm32_spi_transfer_one_setup - common setup to transfer a single
844 * spi_transfer either using DMA or
845 * interrupts.
846 */
847static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
848 struct spi_device *spi_dev,
849 struct spi_transfer *transfer)
850{
851 unsigned long flags;
852 u32 cfg1_clrb = 0, cfg1_setb = 0, cfg2_clrb = 0, cfg2_setb = 0;
853 u32 mode, nb_words;
854 int ret = 0;
855
856 spin_lock_irqsave(&spi->lock, flags);
857
858 if (spi->cur_bpw != transfer->bits_per_word) {
859 u32 bpw, fthlv;
860
861 spi->cur_bpw = transfer->bits_per_word;
862 bpw = spi->cur_bpw - 1;
863
864 cfg1_clrb |= SPI_CFG1_DSIZE;
865 cfg1_setb |= (bpw << SPI_CFG1_DSIZE_SHIFT) & SPI_CFG1_DSIZE;
866
867 spi->cur_fthlv = stm32_spi_prepare_fthlv(spi);
868 fthlv = spi->cur_fthlv - 1;
869
870 cfg1_clrb |= SPI_CFG1_FTHLV;
871 cfg1_setb |= (fthlv << SPI_CFG1_FTHLV_SHIFT) & SPI_CFG1_FTHLV;
872 }
873
874 if (spi->cur_speed != transfer->speed_hz) {
Colin Ian Kinga2f07d382017-06-22 17:34:49 +0100875 int mbr;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200876
877 /* Update spi->cur_speed with real clock speed */
878 mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz);
879 if (mbr < 0) {
880 ret = mbr;
881 goto out;
882 }
883
884 transfer->speed_hz = spi->cur_speed;
885
886 cfg1_clrb |= SPI_CFG1_MBR;
Colin Ian Kinga2f07d382017-06-22 17:34:49 +0100887 cfg1_setb |= ((u32)mbr << SPI_CFG1_MBR_SHIFT) & SPI_CFG1_MBR;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200888 }
889
890 if (cfg1_clrb || cfg1_setb)
891 writel_relaxed((readl_relaxed(spi->base + STM32_SPI_CFG1) &
892 ~cfg1_clrb) | cfg1_setb,
893 spi->base + STM32_SPI_CFG1);
894
895 mode = SPI_FULL_DUPLEX;
896 if (spi_dev->mode & SPI_3WIRE) { /* MISO/MOSI signals shared */
897 /*
898 * SPI_3WIRE and xfer->tx_buf != NULL and xfer->rx_buf != NULL
899 * is forbidden und unvalidated by SPI subsystem so depending
900 * on the valid buffer, we can determine the direction of the
901 * transfer.
902 */
903 mode = SPI_HALF_DUPLEX;
904 if (!transfer->tx_buf)
905 stm32_spi_clr_bits(spi, STM32_SPI_CR1, SPI_CR1_HDDIR);
906 else if (!transfer->rx_buf)
907 stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_HDDIR);
908 } else {
909 if (!transfer->tx_buf)
910 mode = SPI_SIMPLEX_RX;
911 else if (!transfer->rx_buf)
912 mode = SPI_SIMPLEX_TX;
913 }
914 if (spi->cur_comm != mode) {
915 spi->cur_comm = mode;
916
917 cfg2_clrb |= SPI_CFG2_COMM;
918 cfg2_setb |= (mode << SPI_CFG2_COMM_SHIFT) & SPI_CFG2_COMM;
919 }
920
921 cfg2_clrb |= SPI_CFG2_MIDI;
922 if ((transfer->len > 1) && (spi->cur_midi > 0)) {
923 u32 sck_period_ns = DIV_ROUND_UP(SPI_1HZ_NS, spi->cur_speed);
924 u32 midi = min((u32)DIV_ROUND_UP(spi->cur_midi, sck_period_ns),
925 (u32)SPI_CFG2_MIDI >> SPI_CFG2_MIDI_SHIFT);
926
927 dev_dbg(spi->dev, "period=%dns, midi=%d(=%dns)\n",
928 sck_period_ns, midi, midi * sck_period_ns);
929
930 cfg2_setb |= (midi << SPI_CFG2_MIDI_SHIFT) & SPI_CFG2_MIDI;
931 }
932
933 if (cfg2_clrb || cfg2_setb)
934 writel_relaxed((readl_relaxed(spi->base + STM32_SPI_CFG2) &
935 ~cfg2_clrb) | cfg2_setb,
936 spi->base + STM32_SPI_CFG2);
937
Amelie Delaunay128ebb82017-06-27 17:45:17 +0200938 if (spi->cur_bpw <= 8)
939 nb_words = transfer->len;
940 else if (spi->cur_bpw <= 16)
941 nb_words = DIV_ROUND_UP(transfer->len * 8, 16);
942 else
943 nb_words = DIV_ROUND_UP(transfer->len * 8, 32);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200944 nb_words <<= SPI_CR2_TSIZE_SHIFT;
945
946 if (nb_words <= SPI_CR2_TSIZE) {
947 writel_relaxed(nb_words, spi->base + STM32_SPI_CR2);
948 } else {
949 ret = -EMSGSIZE;
950 goto out;
951 }
952
953 spi->cur_xferlen = transfer->len;
954
955 dev_dbg(spi->dev, "transfer communication mode set to %d\n",
956 spi->cur_comm);
957 dev_dbg(spi->dev,
958 "data frame of %d-bit, data packet of %d data frames\n",
959 spi->cur_bpw, spi->cur_fthlv);
960 dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed);
961 dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n",
962 spi->cur_xferlen, nb_words);
963 dev_dbg(spi->dev, "dma %s\n",
964 (spi->cur_usedma) ? "enabled" : "disabled");
965
966out:
967 spin_unlock_irqrestore(&spi->lock, flags);
968
969 return ret;
970}
971
972/**
973 * stm32_spi_transfer_one - transfer a single spi_transfer
974 *
975 * It must return 0 if the transfer is finished or 1 if the transfer is still
976 * in progress.
977 */
978static int stm32_spi_transfer_one(struct spi_master *master,
979 struct spi_device *spi_dev,
980 struct spi_transfer *transfer)
981{
982 struct stm32_spi *spi = spi_master_get_devdata(master);
983 int ret;
984
985 spi->tx_buf = transfer->tx_buf;
986 spi->rx_buf = transfer->rx_buf;
987 spi->tx_len = spi->tx_buf ? transfer->len : 0;
988 spi->rx_len = spi->rx_buf ? transfer->len : 0;
989
Amelie Delaunayc67ad362017-06-27 17:45:19 +0200990 spi->cur_usedma = (master->can_dma &&
Cezary Gapinski2cbee7f2018-12-24 23:00:29 +0100991 master->can_dma(master, spi_dev, transfer));
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +0200992
993 ret = stm32_spi_transfer_one_setup(spi, spi_dev, transfer);
994 if (ret) {
995 dev_err(spi->dev, "SPI transfer setup failed\n");
996 return ret;
997 }
998
999 if (spi->cur_usedma)
1000 return stm32_spi_transfer_one_dma(spi, transfer);
1001 else
1002 return stm32_spi_transfer_one_irq(spi);
1003}
1004
1005/**
1006 * stm32_spi_unprepare_msg - relax the hardware
1007 *
1008 * Normally, if TSIZE has been configured, we should relax the hardware at the
1009 * reception of the EOT interrupt. But in case of error, EOT will not be
1010 * raised. So the subsystem unprepare_message call allows us to properly
1011 * complete the transfer from an hardware point of view.
1012 */
1013static int stm32_spi_unprepare_msg(struct spi_master *master,
1014 struct spi_message *msg)
1015{
1016 struct stm32_spi *spi = spi_master_get_devdata(master);
1017
1018 stm32_spi_disable(spi);
1019
1020 return 0;
1021}
1022
1023/**
1024 * stm32_spi_config - Configure SPI controller as SPI master
1025 */
1026static int stm32_spi_config(struct stm32_spi *spi)
1027{
1028 unsigned long flags;
1029
1030 spin_lock_irqsave(&spi->lock, flags);
1031
1032 /* Ensure I2SMOD bit is kept cleared */
1033 stm32_spi_clr_bits(spi, STM32_SPI_I2SCFGR, SPI_I2SCFGR_I2SMOD);
1034
1035 /*
1036 * - SS input value high
1037 * - transmitter half duplex direction
1038 * - automatic communication suspend when RX-Fifo is full
1039 */
1040 stm32_spi_set_bits(spi, STM32_SPI_CR1, SPI_CR1_SSI |
1041 SPI_CR1_HDDIR |
1042 SPI_CR1_MASRX);
1043
1044 /*
1045 * - Set the master mode (default Motorola mode)
1046 * - Consider 1 master/n slaves configuration and
1047 * SS input value is determined by the SSI bit
1048 * - keep control of all associated GPIOs
1049 */
1050 stm32_spi_set_bits(spi, STM32_SPI_CFG2, SPI_CFG2_MASTER |
1051 SPI_CFG2_SSM |
1052 SPI_CFG2_AFCNTR);
1053
1054 spin_unlock_irqrestore(&spi->lock, flags);
1055
1056 return 0;
1057}
1058
1059static const struct of_device_id stm32_spi_of_match[] = {
Amelie Delaunayc5fe2fa2017-06-27 17:45:14 +02001060 { .compatible = "st,stm32h7-spi", },
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001061 {},
1062};
1063MODULE_DEVICE_TABLE(of, stm32_spi_of_match);
1064
1065static int stm32_spi_probe(struct platform_device *pdev)
1066{
1067 struct spi_master *master;
1068 struct stm32_spi *spi;
1069 struct resource *res;
1070 int i, ret;
1071
1072 master = spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi));
1073 if (!master) {
1074 dev_err(&pdev->dev, "spi master allocation failed\n");
1075 return -ENOMEM;
1076 }
1077 platform_set_drvdata(pdev, master);
1078
1079 spi = spi_master_get_devdata(master);
1080 spi->dev = &pdev->dev;
1081 spi->master = master;
1082 spin_lock_init(&spi->lock);
1083
1084 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1085 spi->base = devm_ioremap_resource(&pdev->dev, res);
1086 if (IS_ERR(spi->base)) {
1087 ret = PTR_ERR(spi->base);
1088 goto err_master_put;
1089 }
1090 spi->phys_addr = (dma_addr_t)res->start;
1091
1092 spi->irq = platform_get_irq(pdev, 0);
1093 if (spi->irq <= 0) {
1094 dev_err(&pdev->dev, "no irq: %d\n", spi->irq);
1095 ret = -ENOENT;
1096 goto err_master_put;
1097 }
1098 ret = devm_request_threaded_irq(&pdev->dev, spi->irq, NULL,
1099 stm32_spi_irq, IRQF_ONESHOT,
1100 pdev->name, master);
1101 if (ret) {
1102 dev_err(&pdev->dev, "irq%d request failed: %d\n", spi->irq,
1103 ret);
1104 goto err_master_put;
1105 }
1106
Cezary Gapinskid4c91342018-12-24 23:00:28 +01001107 spi->clk = devm_clk_get(&pdev->dev, NULL);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001108 if (IS_ERR(spi->clk)) {
1109 ret = PTR_ERR(spi->clk);
1110 dev_err(&pdev->dev, "clk get failed: %d\n", ret);
1111 goto err_master_put;
1112 }
1113
1114 ret = clk_prepare_enable(spi->clk);
1115 if (ret) {
1116 dev_err(&pdev->dev, "clk enable failed: %d\n", ret);
1117 goto err_master_put;
1118 }
1119 spi->clk_rate = clk_get_rate(spi->clk);
1120 if (!spi->clk_rate) {
1121 dev_err(&pdev->dev, "clk rate = 0\n");
1122 ret = -EINVAL;
Alexey Khoroshilov3dbb3ee2018-03-30 22:54:44 +03001123 goto err_clk_disable;
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001124 }
1125
Philipp Zabeld5e9a4a2017-07-19 17:26:20 +02001126 spi->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001127 if (!IS_ERR(spi->rst)) {
1128 reset_control_assert(spi->rst);
1129 udelay(2);
1130 reset_control_deassert(spi->rst);
1131 }
1132
1133 spi->fifo_size = stm32_spi_get_fifo_size(spi);
1134
1135 ret = stm32_spi_config(spi);
1136 if (ret) {
1137 dev_err(&pdev->dev, "controller configuration failed: %d\n",
1138 ret);
1139 goto err_clk_disable;
1140 }
1141
1142 master->dev.of_node = pdev->dev.of_node;
1143 master->auto_runtime_pm = true;
1144 master->bus_num = pdev->id;
1145 master->mode_bits = SPI_MODE_3 | SPI_CS_HIGH | SPI_LSB_FIRST |
1146 SPI_3WIRE | SPI_LOOP;
1147 master->bits_per_word_mask = stm32_spi_get_bpw_mask(spi);
1148 master->max_speed_hz = spi->clk_rate / SPI_MBR_DIV_MIN;
1149 master->min_speed_hz = spi->clk_rate / SPI_MBR_DIV_MAX;
1150 master->setup = stm32_spi_setup;
1151 master->prepare_message = stm32_spi_prepare_msg;
1152 master->transfer_one = stm32_spi_transfer_one;
1153 master->unprepare_message = stm32_spi_unprepare_msg;
1154
1155 spi->dma_tx = dma_request_slave_channel(spi->dev, "tx");
1156 if (!spi->dma_tx)
1157 dev_warn(&pdev->dev, "failed to request tx dma channel\n");
1158 else
1159 master->dma_tx = spi->dma_tx;
1160
1161 spi->dma_rx = dma_request_slave_channel(spi->dev, "rx");
1162 if (!spi->dma_rx)
1163 dev_warn(&pdev->dev, "failed to request rx dma channel\n");
1164 else
1165 master->dma_rx = spi->dma_rx;
1166
1167 if (spi->dma_tx || spi->dma_rx)
1168 master->can_dma = stm32_spi_can_dma;
1169
Amelie Delaunay038ac862017-06-27 17:45:18 +02001170 pm_runtime_set_active(&pdev->dev);
1171 pm_runtime_enable(&pdev->dev);
1172
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001173 ret = devm_spi_register_master(&pdev->dev, master);
1174 if (ret) {
1175 dev_err(&pdev->dev, "spi master registration failed: %d\n",
1176 ret);
1177 goto err_dma_release;
1178 }
1179
1180 if (!master->cs_gpios) {
1181 dev_err(&pdev->dev, "no CS gpios available\n");
1182 ret = -EINVAL;
1183 goto err_dma_release;
1184 }
1185
1186 for (i = 0; i < master->num_chipselect; i++) {
1187 if (!gpio_is_valid(master->cs_gpios[i])) {
1188 dev_err(&pdev->dev, "%i is not a valid gpio\n",
1189 master->cs_gpios[i]);
1190 ret = -EINVAL;
1191 goto err_dma_release;
1192 }
1193
1194 ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i],
1195 DRIVER_NAME);
1196 if (ret) {
1197 dev_err(&pdev->dev, "can't get CS gpio %i\n",
1198 master->cs_gpios[i]);
1199 goto err_dma_release;
1200 }
1201 }
1202
1203 dev_info(&pdev->dev, "driver initialized\n");
1204
1205 return 0;
1206
1207err_dma_release:
1208 if (spi->dma_tx)
1209 dma_release_channel(spi->dma_tx);
1210 if (spi->dma_rx)
1211 dma_release_channel(spi->dma_rx);
Amelie Delaunay038ac862017-06-27 17:45:18 +02001212
1213 pm_runtime_disable(&pdev->dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001214err_clk_disable:
1215 clk_disable_unprepare(spi->clk);
1216err_master_put:
1217 spi_master_put(master);
1218
1219 return ret;
1220}
1221
1222static int stm32_spi_remove(struct platform_device *pdev)
1223{
1224 struct spi_master *master = platform_get_drvdata(pdev);
1225 struct stm32_spi *spi = spi_master_get_devdata(master);
1226
1227 stm32_spi_disable(spi);
1228
1229 if (master->dma_tx)
1230 dma_release_channel(master->dma_tx);
1231 if (master->dma_rx)
1232 dma_release_channel(master->dma_rx);
1233
1234 clk_disable_unprepare(spi->clk);
1235
Amelie Delaunay038ac862017-06-27 17:45:18 +02001236 pm_runtime_disable(&pdev->dev);
1237
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001238 return 0;
1239}
1240
Amelie Delaunay038ac862017-06-27 17:45:18 +02001241#ifdef CONFIG_PM
1242static int stm32_spi_runtime_suspend(struct device *dev)
1243{
1244 struct spi_master *master = dev_get_drvdata(dev);
1245 struct stm32_spi *spi = spi_master_get_devdata(master);
1246
1247 clk_disable_unprepare(spi->clk);
1248
1249 return 0;
1250}
1251
1252static int stm32_spi_runtime_resume(struct device *dev)
1253{
1254 struct spi_master *master = dev_get_drvdata(dev);
1255 struct stm32_spi *spi = spi_master_get_devdata(master);
1256
1257 return clk_prepare_enable(spi->clk);
1258}
1259#endif
1260
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001261#ifdef CONFIG_PM_SLEEP
1262static int stm32_spi_suspend(struct device *dev)
1263{
1264 struct spi_master *master = dev_get_drvdata(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001265 int ret;
1266
1267 ret = spi_master_suspend(master);
1268 if (ret)
1269 return ret;
1270
Amelie Delaunay038ac862017-06-27 17:45:18 +02001271 return pm_runtime_force_suspend(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001272}
1273
1274static int stm32_spi_resume(struct device *dev)
1275{
1276 struct spi_master *master = dev_get_drvdata(dev);
1277 struct stm32_spi *spi = spi_master_get_devdata(master);
1278 int ret;
1279
Amelie Delaunay038ac862017-06-27 17:45:18 +02001280 ret = pm_runtime_force_resume(dev);
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001281 if (ret)
1282 return ret;
Amelie Delaunay038ac862017-06-27 17:45:18 +02001283
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001284 ret = spi_master_resume(master);
1285 if (ret)
1286 clk_disable_unprepare(spi->clk);
1287
1288 return ret;
1289}
1290#endif
1291
Amelie Delaunay038ac862017-06-27 17:45:18 +02001292static const struct dev_pm_ops stm32_spi_pm_ops = {
1293 SET_SYSTEM_SLEEP_PM_OPS(stm32_spi_suspend, stm32_spi_resume)
1294 SET_RUNTIME_PM_OPS(stm32_spi_runtime_suspend,
1295 stm32_spi_runtime_resume, NULL)
1296};
Amelie Delaunaydcbe0d82017-06-21 16:32:06 +02001297
1298static struct platform_driver stm32_spi_driver = {
1299 .probe = stm32_spi_probe,
1300 .remove = stm32_spi_remove,
1301 .driver = {
1302 .name = DRIVER_NAME,
1303 .pm = &stm32_spi_pm_ops,
1304 .of_match_table = stm32_spi_of_match,
1305 },
1306};
1307
1308module_platform_driver(stm32_spi_driver);
1309
1310MODULE_ALIAS("platform:" DRIVER_NAME);
1311MODULE_DESCRIPTION("STMicroelectronics STM32 SPI Controller driver");
1312MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
1313MODULE_LICENSE("GPL v2");