blob: 38f4be20923463c37d67a3909d42538bcebcdcad [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * OMAP2 McSPI controller driver
4 *
5 * Copyright (C) 2005, 2006 Nokia Corporation
6 * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
7 * Juha Yrj�l� <juha.yrjola@nokia.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9
10#include <linux/kernel.h>
11#include <linux/interrupt.h>
12#include <linux/module.h>
13#include <linux/device.h>
14#include <linux/delay.h>
15#include <linux/dma-mapping.h>
16#include <linux/dmaengine.h>
17#include <linux/pinctrl/consumer.h>
18#include <linux/platform_device.h>
19#include <linux/err.h>
20#include <linux/clk.h>
21#include <linux/io.h>
22#include <linux/slab.h>
23#include <linux/pm_runtime.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/gcd.h>
27
28#include <linux/spi/spi.h>
29#include <linux/gpio.h>
30
31#include <linux/platform_data/spi-omap2-mcspi.h>
32
33#define OMAP2_MCSPI_MAX_FREQ 48000000
34#define OMAP2_MCSPI_MAX_DIVIDER 4096
35#define OMAP2_MCSPI_MAX_FIFODEPTH 64
36#define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
37#define SPI_AUTOSUSPEND_TIMEOUT 2000
38
39#define OMAP2_MCSPI_REVISION 0x00
40#define OMAP2_MCSPI_SYSSTATUS 0x14
41#define OMAP2_MCSPI_IRQSTATUS 0x18
42#define OMAP2_MCSPI_IRQENABLE 0x1c
43#define OMAP2_MCSPI_WAKEUPENABLE 0x20
44#define OMAP2_MCSPI_SYST 0x24
45#define OMAP2_MCSPI_MODULCTRL 0x28
46#define OMAP2_MCSPI_XFERLEVEL 0x7c
47
48/* per-channel banks, 0x14 bytes each, first is: */
49#define OMAP2_MCSPI_CHCONF0 0x2c
50#define OMAP2_MCSPI_CHSTAT0 0x30
51#define OMAP2_MCSPI_CHCTRL0 0x34
52#define OMAP2_MCSPI_TX0 0x38
53#define OMAP2_MCSPI_RX0 0x3c
54
55/* per-register bitmasks: */
56#define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
57
58#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
59#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
60#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
61
62#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
63#define OMAP2_MCSPI_CHCONF_POL BIT(1)
64#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
65#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
66#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
67#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
68#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
69#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
70#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
71#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
72#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
73#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
74#define OMAP2_MCSPI_CHCONF_IS BIT(18)
75#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
76#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
77#define OMAP2_MCSPI_CHCONF_FFET BIT(27)
78#define OMAP2_MCSPI_CHCONF_FFER BIT(28)
79#define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
80
81#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
82#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
83#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
84#define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
85
86#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
87#define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
88
89#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
90
91/* We have 2 DMA channels per CS, one for RX and one for TX */
92struct omap2_mcspi_dma {
93 struct dma_chan *dma_tx;
94 struct dma_chan *dma_rx;
95
96 struct completion dma_tx_completion;
97 struct completion dma_rx_completion;
98
99 char dma_rx_ch_name[14];
100 char dma_tx_ch_name[14];
101};
102
103/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
104 * cache operations; better heuristics consider wordsize and bitrate.
105 */
106#define DMA_MIN_BYTES 160
107
108
109/*
110 * Used for context save and restore, structure members to be updated whenever
111 * corresponding registers are modified.
112 */
113struct omap2_mcspi_regs {
114 u32 modulctrl;
115 u32 wakeupenable;
116 struct list_head cs;
117};
118
119struct omap2_mcspi {
David Brazdil0f672f62019-12-10 10:32:29 +0000120 struct completion txdone;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121 struct spi_master *master;
122 /* Virtual base address of the controller */
123 void __iomem *base;
124 unsigned long phys;
125 /* SPI1 has 4 channels, while SPI2 has 2 */
126 struct omap2_mcspi_dma *dma_channels;
127 struct device *dev;
128 struct omap2_mcspi_regs ctx;
129 int fifo_depth;
David Brazdil0f672f62019-12-10 10:32:29 +0000130 bool slave_aborted;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131 unsigned int pin_dir:1;
132};
133
134struct omap2_mcspi_cs {
135 void __iomem *base;
136 unsigned long phys;
137 int word_len;
138 u16 mode;
139 struct list_head node;
140 /* Context save and restore shadow register */
141 u32 chconf0, chctrl0;
142};
143
144static inline void mcspi_write_reg(struct spi_master *master,
145 int idx, u32 val)
146{
147 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
148
149 writel_relaxed(val, mcspi->base + idx);
150}
151
152static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
153{
154 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
155
156 return readl_relaxed(mcspi->base + idx);
157}
158
159static inline void mcspi_write_cs_reg(const struct spi_device *spi,
160 int idx, u32 val)
161{
162 struct omap2_mcspi_cs *cs = spi->controller_state;
163
164 writel_relaxed(val, cs->base + idx);
165}
166
167static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
168{
169 struct omap2_mcspi_cs *cs = spi->controller_state;
170
171 return readl_relaxed(cs->base + idx);
172}
173
174static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
175{
176 struct omap2_mcspi_cs *cs = spi->controller_state;
177
178 return cs->chconf0;
179}
180
181static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
182{
183 struct omap2_mcspi_cs *cs = spi->controller_state;
184
185 cs->chconf0 = val;
186 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
187 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
188}
189
190static inline int mcspi_bytes_per_word(int word_len)
191{
192 if (word_len <= 8)
193 return 1;
194 else if (word_len <= 16)
195 return 2;
196 else /* word_len <= 32 */
197 return 4;
198}
199
200static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
201 int is_read, int enable)
202{
203 u32 l, rw;
204
205 l = mcspi_cached_chconf0(spi);
206
207 if (is_read) /* 1 is read, 0 write */
208 rw = OMAP2_MCSPI_CHCONF_DMAR;
209 else
210 rw = OMAP2_MCSPI_CHCONF_DMAW;
211
212 if (enable)
213 l |= rw;
214 else
215 l &= ~rw;
216
217 mcspi_write_chconf0(spi, l);
218}
219
220static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
221{
222 struct omap2_mcspi_cs *cs = spi->controller_state;
223 u32 l;
224
225 l = cs->chctrl0;
226 if (enable)
227 l |= OMAP2_MCSPI_CHCTRL_EN;
228 else
229 l &= ~OMAP2_MCSPI_CHCTRL_EN;
230 cs->chctrl0 = l;
231 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
232 /* Flash post-writes */
233 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
234}
235
236static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
237{
238 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
239 u32 l;
240
241 /* The controller handles the inverted chip selects
242 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
243 * the inversion from the core spi_set_cs function.
244 */
245 if (spi->mode & SPI_CS_HIGH)
246 enable = !enable;
247
248 if (spi->controller_state) {
249 int err = pm_runtime_get_sync(mcspi->dev);
250 if (err < 0) {
251 pm_runtime_put_noidle(mcspi->dev);
252 dev_err(mcspi->dev, "failed to get sync: %d\n", err);
253 return;
254 }
255
256 l = mcspi_cached_chconf0(spi);
257
258 if (enable)
259 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
260 else
261 l |= OMAP2_MCSPI_CHCONF_FORCE;
262
263 mcspi_write_chconf0(spi, l);
264
265 pm_runtime_mark_last_busy(mcspi->dev);
266 pm_runtime_put_autosuspend(mcspi->dev);
267 }
268}
269
David Brazdil0f672f62019-12-10 10:32:29 +0000270static void omap2_mcspi_set_mode(struct spi_master *master)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271{
272 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
273 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
274 u32 l;
275
276 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000277 * Choose master or slave mode
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000278 */
279 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
David Brazdil0f672f62019-12-10 10:32:29 +0000280 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST);
281 if (spi_controller_is_slave(master)) {
282 l |= (OMAP2_MCSPI_MODULCTRL_MS);
283 } else {
284 l &= ~(OMAP2_MCSPI_MODULCTRL_MS);
285 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
286 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000287 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
288
289 ctx->modulctrl = l;
290}
291
292static void omap2_mcspi_set_fifo(const struct spi_device *spi,
293 struct spi_transfer *t, int enable)
294{
295 struct spi_master *master = spi->master;
296 struct omap2_mcspi_cs *cs = spi->controller_state;
297 struct omap2_mcspi *mcspi;
298 unsigned int wcnt;
David Brazdil0f672f62019-12-10 10:32:29 +0000299 int max_fifo_depth, bytes_per_word;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000300 u32 chconf, xferlevel;
301
302 mcspi = spi_master_get_devdata(master);
303
304 chconf = mcspi_cached_chconf0(spi);
305 if (enable) {
306 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
307 if (t->len % bytes_per_word != 0)
308 goto disable_fifo;
309
310 if (t->rx_buf != NULL && t->tx_buf != NULL)
311 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
312 else
313 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
314
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 wcnt = t->len / bytes_per_word;
316 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
317 goto disable_fifo;
318
319 xferlevel = wcnt << 16;
320 if (t->rx_buf != NULL) {
321 chconf |= OMAP2_MCSPI_CHCONF_FFER;
David Brazdil0f672f62019-12-10 10:32:29 +0000322 xferlevel |= (bytes_per_word - 1) << 8;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000323 }
David Brazdil0f672f62019-12-10 10:32:29 +0000324
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325 if (t->tx_buf != NULL) {
326 chconf |= OMAP2_MCSPI_CHCONF_FFET;
David Brazdil0f672f62019-12-10 10:32:29 +0000327 xferlevel |= bytes_per_word - 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000328 }
329
330 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
331 mcspi_write_chconf0(spi, chconf);
David Brazdil0f672f62019-12-10 10:32:29 +0000332 mcspi->fifo_depth = max_fifo_depth;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000333
334 return;
335 }
336
337disable_fifo:
338 if (t->rx_buf != NULL)
339 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
340
341 if (t->tx_buf != NULL)
342 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
343
344 mcspi_write_chconf0(spi, chconf);
345 mcspi->fifo_depth = 0;
346}
347
348static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
349{
Olivier Deprez0e641232021-09-23 10:07:05 +0200350 unsigned long timeout;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000351
Olivier Deprez0e641232021-09-23 10:07:05 +0200352 timeout = jiffies + msecs_to_jiffies(1000);
353 while (!(readl_relaxed(reg) & bit)) {
354 if (time_after(jiffies, timeout)) {
355 if (!(readl_relaxed(reg) & bit))
356 return -ETIMEDOUT;
357 else
358 return 0;
359 }
360 cpu_relax();
361 }
362 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000363}
364
365static int mcspi_wait_for_completion(struct omap2_mcspi *mcspi,
366 struct completion *x)
367{
368 if (spi_controller_is_slave(mcspi->master)) {
369 if (wait_for_completion_interruptible(x) ||
370 mcspi->slave_aborted)
371 return -EINTR;
372 } else {
373 wait_for_completion(x);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374 }
David Brazdil0f672f62019-12-10 10:32:29 +0000375
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000376 return 0;
377}
378
379static void omap2_mcspi_rx_callback(void *data)
380{
381 struct spi_device *spi = data;
382 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
383 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
384
385 /* We must disable the DMA RX request */
386 omap2_mcspi_set_dma_req(spi, 1, 0);
387
388 complete(&mcspi_dma->dma_rx_completion);
389}
390
391static void omap2_mcspi_tx_callback(void *data)
392{
393 struct spi_device *spi = data;
394 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
395 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
396
397 /* We must disable the DMA TX request */
398 omap2_mcspi_set_dma_req(spi, 0, 0);
399
400 complete(&mcspi_dma->dma_tx_completion);
401}
402
403static void omap2_mcspi_tx_dma(struct spi_device *spi,
404 struct spi_transfer *xfer,
405 struct dma_slave_config cfg)
406{
407 struct omap2_mcspi *mcspi;
408 struct omap2_mcspi_dma *mcspi_dma;
409
410 mcspi = spi_master_get_devdata(spi->master);
411 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
412
413 if (mcspi_dma->dma_tx) {
414 struct dma_async_tx_descriptor *tx;
415
416 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
417
418 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
419 xfer->tx_sg.nents,
420 DMA_MEM_TO_DEV,
421 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
422 if (tx) {
423 tx->callback = omap2_mcspi_tx_callback;
424 tx->callback_param = spi;
425 dmaengine_submit(tx);
426 } else {
427 /* FIXME: fall back to PIO? */
428 }
429 }
430 dma_async_issue_pending(mcspi_dma->dma_tx);
431 omap2_mcspi_set_dma_req(spi, 0, 1);
432
433}
434
435static unsigned
436omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
437 struct dma_slave_config cfg,
438 unsigned es)
439{
440 struct omap2_mcspi *mcspi;
441 struct omap2_mcspi_dma *mcspi_dma;
442 unsigned int count, transfer_reduction = 0;
443 struct scatterlist *sg_out[2];
444 int nb_sizes = 0, out_mapped_nents[2], ret, x;
445 size_t sizes[2];
446 u32 l;
447 int elements = 0;
448 int word_len, element_count;
449 struct omap2_mcspi_cs *cs = spi->controller_state;
450 void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
451
452 mcspi = spi_master_get_devdata(spi->master);
453 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
454 count = xfer->len;
455
456 /*
457 * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
458 * it mentions reducing DMA transfer length by one element in master
459 * normal mode.
460 */
461 if (mcspi->fifo_depth == 0)
462 transfer_reduction = es;
463
464 word_len = cs->word_len;
465 l = mcspi_cached_chconf0(spi);
466
467 if (word_len <= 8)
468 element_count = count;
469 else if (word_len <= 16)
470 element_count = count >> 1;
471 else /* word_len <= 32 */
472 element_count = count >> 2;
473
474 if (mcspi_dma->dma_rx) {
475 struct dma_async_tx_descriptor *tx;
476
477 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
478
479 /*
480 * Reduce DMA transfer length by one more if McSPI is
481 * configured in turbo mode.
482 */
483 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
484 transfer_reduction += es;
485
486 if (transfer_reduction) {
487 /* Split sgl into two. The second sgl won't be used. */
488 sizes[0] = count - transfer_reduction;
489 sizes[1] = transfer_reduction;
490 nb_sizes = 2;
491 } else {
492 /*
493 * Don't bother splitting the sgl. This essentially
494 * clones the original sgl.
495 */
496 sizes[0] = count;
497 nb_sizes = 1;
498 }
499
500 ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents,
501 0, nb_sizes,
502 sizes,
503 sg_out, out_mapped_nents,
504 GFP_KERNEL);
505
506 if (ret < 0) {
507 dev_err(&spi->dev, "sg_split failed\n");
508 return 0;
509 }
510
511 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx,
512 sg_out[0],
513 out_mapped_nents[0],
514 DMA_DEV_TO_MEM,
515 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
516 if (tx) {
517 tx->callback = omap2_mcspi_rx_callback;
518 tx->callback_param = spi;
519 dmaengine_submit(tx);
520 } else {
521 /* FIXME: fall back to PIO? */
522 }
523 }
524
525 dma_async_issue_pending(mcspi_dma->dma_rx);
526 omap2_mcspi_set_dma_req(spi, 1, 1);
527
David Brazdil0f672f62019-12-10 10:32:29 +0000528 ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_rx_completion);
529 if (ret || mcspi->slave_aborted) {
530 dmaengine_terminate_sync(mcspi_dma->dma_rx);
531 omap2_mcspi_set_dma_req(spi, 1, 0);
532 return 0;
533 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534
535 for (x = 0; x < nb_sizes; x++)
536 kfree(sg_out[x]);
537
538 if (mcspi->fifo_depth > 0)
539 return count;
540
541 /*
542 * Due to the DMA transfer length reduction the missing bytes must
543 * be read manually to receive all of the expected data.
544 */
545 omap2_mcspi_set_enable(spi, 0);
546
547 elements = element_count - 1;
548
549 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
550 elements--;
551
552 if (!mcspi_wait_for_reg_bit(chstat_reg,
553 OMAP2_MCSPI_CHSTAT_RXS)) {
554 u32 w;
555
556 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
557 if (word_len <= 8)
558 ((u8 *)xfer->rx_buf)[elements++] = w;
559 else if (word_len <= 16)
560 ((u16 *)xfer->rx_buf)[elements++] = w;
561 else /* word_len <= 32 */
562 ((u32 *)xfer->rx_buf)[elements++] = w;
563 } else {
564 int bytes_per_word = mcspi_bytes_per_word(word_len);
565 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
566 count -= (bytes_per_word << 1);
567 omap2_mcspi_set_enable(spi, 1);
568 return count;
569 }
570 }
571 if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
572 u32 w;
573
574 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
575 if (word_len <= 8)
576 ((u8 *)xfer->rx_buf)[elements] = w;
577 else if (word_len <= 16)
578 ((u16 *)xfer->rx_buf)[elements] = w;
579 else /* word_len <= 32 */
580 ((u32 *)xfer->rx_buf)[elements] = w;
581 } else {
582 dev_err(&spi->dev, "DMA RX last word empty\n");
583 count -= mcspi_bytes_per_word(word_len);
584 }
585 omap2_mcspi_set_enable(spi, 1);
586 return count;
587}
588
589static unsigned
590omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
591{
592 struct omap2_mcspi *mcspi;
593 struct omap2_mcspi_cs *cs = spi->controller_state;
594 struct omap2_mcspi_dma *mcspi_dma;
595 unsigned int count;
596 u8 *rx;
597 const u8 *tx;
598 struct dma_slave_config cfg;
599 enum dma_slave_buswidth width;
600 unsigned es;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000601 void __iomem *chstat_reg;
602 void __iomem *irqstat_reg;
603 int wait_res;
604
605 mcspi = spi_master_get_devdata(spi->master);
606 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
607
608 if (cs->word_len <= 8) {
609 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
610 es = 1;
611 } else if (cs->word_len <= 16) {
612 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
613 es = 2;
614 } else {
615 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
616 es = 4;
617 }
618
619 count = xfer->len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000620
621 memset(&cfg, 0, sizeof(cfg));
622 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
623 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
624 cfg.src_addr_width = width;
625 cfg.dst_addr_width = width;
David Brazdil0f672f62019-12-10 10:32:29 +0000626 cfg.src_maxburst = 1;
627 cfg.dst_maxburst = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000628
629 rx = xfer->rx_buf;
630 tx = xfer->tx_buf;
631
David Brazdil0f672f62019-12-10 10:32:29 +0000632 mcspi->slave_aborted = false;
633 reinit_completion(&mcspi_dma->dma_tx_completion);
634 reinit_completion(&mcspi_dma->dma_rx_completion);
635 reinit_completion(&mcspi->txdone);
636 if (tx) {
637 /* Enable EOW IRQ to know end of tx in slave mode */
638 if (spi_controller_is_slave(spi->master))
639 mcspi_write_reg(spi->master,
640 OMAP2_MCSPI_IRQENABLE,
641 OMAP2_MCSPI_IRQSTATUS_EOW);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000642 omap2_mcspi_tx_dma(spi, xfer, cfg);
David Brazdil0f672f62019-12-10 10:32:29 +0000643 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644
645 if (rx != NULL)
646 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
647
648 if (tx != NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000649 int ret;
650
651 ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_tx_completion);
652 if (ret || mcspi->slave_aborted) {
653 dmaengine_terminate_sync(mcspi_dma->dma_tx);
654 omap2_mcspi_set_dma_req(spi, 0, 0);
655 return 0;
656 }
657
658 if (spi_controller_is_slave(mcspi->master)) {
659 ret = mcspi_wait_for_completion(mcspi, &mcspi->txdone);
660 if (ret || mcspi->slave_aborted)
661 return 0;
662 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000663
664 if (mcspi->fifo_depth > 0) {
665 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
666
667 if (mcspi_wait_for_reg_bit(irqstat_reg,
668 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
669 dev_err(&spi->dev, "EOW timed out\n");
670
671 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
672 OMAP2_MCSPI_IRQSTATUS_EOW);
673 }
674
675 /* for TX_ONLY mode, be sure all words have shifted out */
676 if (rx == NULL) {
677 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
678 if (mcspi->fifo_depth > 0) {
679 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
680 OMAP2_MCSPI_CHSTAT_TXFFE);
681 if (wait_res < 0)
682 dev_err(&spi->dev, "TXFFE timed out\n");
683 } else {
684 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
685 OMAP2_MCSPI_CHSTAT_TXS);
686 if (wait_res < 0)
687 dev_err(&spi->dev, "TXS timed out\n");
688 }
689 if (wait_res >= 0 &&
690 (mcspi_wait_for_reg_bit(chstat_reg,
691 OMAP2_MCSPI_CHSTAT_EOT) < 0))
692 dev_err(&spi->dev, "EOT timed out\n");
693 }
694 }
695 return count;
696}
697
698static unsigned
699omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
700{
701 struct omap2_mcspi_cs *cs = spi->controller_state;
702 unsigned int count, c;
703 u32 l;
704 void __iomem *base = cs->base;
705 void __iomem *tx_reg;
706 void __iomem *rx_reg;
707 void __iomem *chstat_reg;
708 int word_len;
709
710 count = xfer->len;
711 c = count;
712 word_len = cs->word_len;
713
714 l = mcspi_cached_chconf0(spi);
715
716 /* We store the pre-calculated register addresses on stack to speed
717 * up the transfer loop. */
718 tx_reg = base + OMAP2_MCSPI_TX0;
719 rx_reg = base + OMAP2_MCSPI_RX0;
720 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
721
722 if (c < (word_len>>3))
723 return 0;
724
725 if (word_len <= 8) {
726 u8 *rx;
727 const u8 *tx;
728
729 rx = xfer->rx_buf;
730 tx = xfer->tx_buf;
731
732 do {
733 c -= 1;
734 if (tx != NULL) {
735 if (mcspi_wait_for_reg_bit(chstat_reg,
736 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
737 dev_err(&spi->dev, "TXS timed out\n");
738 goto out;
739 }
740 dev_vdbg(&spi->dev, "write-%d %02x\n",
741 word_len, *tx);
742 writel_relaxed(*tx++, tx_reg);
743 }
744 if (rx != NULL) {
745 if (mcspi_wait_for_reg_bit(chstat_reg,
746 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
747 dev_err(&spi->dev, "RXS timed out\n");
748 goto out;
749 }
750
751 if (c == 1 && tx == NULL &&
752 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
753 omap2_mcspi_set_enable(spi, 0);
754 *rx++ = readl_relaxed(rx_reg);
755 dev_vdbg(&spi->dev, "read-%d %02x\n",
756 word_len, *(rx - 1));
757 if (mcspi_wait_for_reg_bit(chstat_reg,
758 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
759 dev_err(&spi->dev,
760 "RXS timed out\n");
761 goto out;
762 }
763 c = 0;
764 } else if (c == 0 && tx == NULL) {
765 omap2_mcspi_set_enable(spi, 0);
766 }
767
768 *rx++ = readl_relaxed(rx_reg);
769 dev_vdbg(&spi->dev, "read-%d %02x\n",
770 word_len, *(rx - 1));
771 }
772 } while (c);
773 } else if (word_len <= 16) {
774 u16 *rx;
775 const u16 *tx;
776
777 rx = xfer->rx_buf;
778 tx = xfer->tx_buf;
779 do {
780 c -= 2;
781 if (tx != NULL) {
782 if (mcspi_wait_for_reg_bit(chstat_reg,
783 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
784 dev_err(&spi->dev, "TXS timed out\n");
785 goto out;
786 }
787 dev_vdbg(&spi->dev, "write-%d %04x\n",
788 word_len, *tx);
789 writel_relaxed(*tx++, tx_reg);
790 }
791 if (rx != NULL) {
792 if (mcspi_wait_for_reg_bit(chstat_reg,
793 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
794 dev_err(&spi->dev, "RXS timed out\n");
795 goto out;
796 }
797
798 if (c == 2 && tx == NULL &&
799 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
800 omap2_mcspi_set_enable(spi, 0);
801 *rx++ = readl_relaxed(rx_reg);
802 dev_vdbg(&spi->dev, "read-%d %04x\n",
803 word_len, *(rx - 1));
804 if (mcspi_wait_for_reg_bit(chstat_reg,
805 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
806 dev_err(&spi->dev,
807 "RXS timed out\n");
808 goto out;
809 }
810 c = 0;
811 } else if (c == 0 && tx == NULL) {
812 omap2_mcspi_set_enable(spi, 0);
813 }
814
815 *rx++ = readl_relaxed(rx_reg);
816 dev_vdbg(&spi->dev, "read-%d %04x\n",
817 word_len, *(rx - 1));
818 }
819 } while (c >= 2);
820 } else if (word_len <= 32) {
821 u32 *rx;
822 const u32 *tx;
823
824 rx = xfer->rx_buf;
825 tx = xfer->tx_buf;
826 do {
827 c -= 4;
828 if (tx != NULL) {
829 if (mcspi_wait_for_reg_bit(chstat_reg,
830 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
831 dev_err(&spi->dev, "TXS timed out\n");
832 goto out;
833 }
834 dev_vdbg(&spi->dev, "write-%d %08x\n",
835 word_len, *tx);
836 writel_relaxed(*tx++, tx_reg);
837 }
838 if (rx != NULL) {
839 if (mcspi_wait_for_reg_bit(chstat_reg,
840 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
841 dev_err(&spi->dev, "RXS timed out\n");
842 goto out;
843 }
844
845 if (c == 4 && tx == NULL &&
846 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
847 omap2_mcspi_set_enable(spi, 0);
848 *rx++ = readl_relaxed(rx_reg);
849 dev_vdbg(&spi->dev, "read-%d %08x\n",
850 word_len, *(rx - 1));
851 if (mcspi_wait_for_reg_bit(chstat_reg,
852 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
853 dev_err(&spi->dev,
854 "RXS timed out\n");
855 goto out;
856 }
857 c = 0;
858 } else if (c == 0 && tx == NULL) {
859 omap2_mcspi_set_enable(spi, 0);
860 }
861
862 *rx++ = readl_relaxed(rx_reg);
863 dev_vdbg(&spi->dev, "read-%d %08x\n",
864 word_len, *(rx - 1));
865 }
866 } while (c >= 4);
867 }
868
869 /* for TX_ONLY mode, be sure all words have shifted out */
870 if (xfer->rx_buf == NULL) {
871 if (mcspi_wait_for_reg_bit(chstat_reg,
872 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
873 dev_err(&spi->dev, "TXS timed out\n");
874 } else if (mcspi_wait_for_reg_bit(chstat_reg,
875 OMAP2_MCSPI_CHSTAT_EOT) < 0)
876 dev_err(&spi->dev, "EOT timed out\n");
877
878 /* disable chan to purge rx datas received in TX_ONLY transfer,
879 * otherwise these rx datas will affect the direct following
880 * RX_ONLY transfer.
881 */
882 omap2_mcspi_set_enable(spi, 0);
883 }
884out:
885 omap2_mcspi_set_enable(spi, 1);
886 return count - c;
887}
888
889static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
890{
891 u32 div;
892
893 for (div = 0; div < 15; div++)
894 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
895 return div;
896
897 return 15;
898}
899
900/* called only when no transfer is active to this device */
901static int omap2_mcspi_setup_transfer(struct spi_device *spi,
902 struct spi_transfer *t)
903{
904 struct omap2_mcspi_cs *cs = spi->controller_state;
905 struct omap2_mcspi *mcspi;
906 u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
907 u8 word_len = spi->bits_per_word;
908 u32 speed_hz = spi->max_speed_hz;
909
910 mcspi = spi_master_get_devdata(spi->master);
911
912 if (t != NULL && t->bits_per_word)
913 word_len = t->bits_per_word;
914
915 cs->word_len = word_len;
916
917 if (t && t->speed_hz)
918 speed_hz = t->speed_hz;
919
920 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
921 if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
922 clkd = omap2_mcspi_calc_divisor(speed_hz);
923 speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
924 clkg = 0;
925 } else {
926 div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
927 speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
928 clkd = (div - 1) & 0xf;
929 extclk = (div - 1) >> 4;
930 clkg = OMAP2_MCSPI_CHCONF_CLKG;
931 }
932
933 l = mcspi_cached_chconf0(spi);
934
935 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
936 * REVISIT: this controller could support SPI_3WIRE mode.
937 */
938 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
939 l &= ~OMAP2_MCSPI_CHCONF_IS;
940 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
941 l |= OMAP2_MCSPI_CHCONF_DPE0;
942 } else {
943 l |= OMAP2_MCSPI_CHCONF_IS;
944 l |= OMAP2_MCSPI_CHCONF_DPE1;
945 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
946 }
947
948 /* wordlength */
949 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
950 l |= (word_len - 1) << 7;
951
952 /* set chipselect polarity; manage with FORCE */
953 if (!(spi->mode & SPI_CS_HIGH))
954 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
955 else
956 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
957
958 /* set clock divisor */
959 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
960 l |= clkd << 2;
961
962 /* set clock granularity */
963 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
964 l |= clkg;
965 if (clkg) {
966 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
967 cs->chctrl0 |= extclk << 8;
968 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
969 }
970
971 /* set SPI mode 0..3 */
972 if (spi->mode & SPI_CPOL)
973 l |= OMAP2_MCSPI_CHCONF_POL;
974 else
975 l &= ~OMAP2_MCSPI_CHCONF_POL;
976 if (spi->mode & SPI_CPHA)
977 l |= OMAP2_MCSPI_CHCONF_PHA;
978 else
979 l &= ~OMAP2_MCSPI_CHCONF_PHA;
980
981 mcspi_write_chconf0(spi, l);
982
983 cs->mode = spi->mode;
984
985 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
986 speed_hz,
987 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
988 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
989
990 return 0;
991}
992
993/*
994 * Note that we currently allow DMA only if we get a channel
995 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
996 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200997static int omap2_mcspi_request_dma(struct omap2_mcspi *mcspi,
998 struct omap2_mcspi_dma *mcspi_dma)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000999{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001000 int ret = 0;
1001
Olivier Deprez0e641232021-09-23 10:07:05 +02001002 mcspi_dma->dma_rx = dma_request_chan(mcspi->dev,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001003 mcspi_dma->dma_rx_ch_name);
1004 if (IS_ERR(mcspi_dma->dma_rx)) {
1005 ret = PTR_ERR(mcspi_dma->dma_rx);
1006 mcspi_dma->dma_rx = NULL;
1007 goto no_dma;
1008 }
1009
Olivier Deprez0e641232021-09-23 10:07:05 +02001010 mcspi_dma->dma_tx = dma_request_chan(mcspi->dev,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001011 mcspi_dma->dma_tx_ch_name);
1012 if (IS_ERR(mcspi_dma->dma_tx)) {
1013 ret = PTR_ERR(mcspi_dma->dma_tx);
1014 mcspi_dma->dma_tx = NULL;
1015 dma_release_channel(mcspi_dma->dma_rx);
1016 mcspi_dma->dma_rx = NULL;
1017 }
1018
Olivier Deprez0e641232021-09-23 10:07:05 +02001019 init_completion(&mcspi_dma->dma_rx_completion);
1020 init_completion(&mcspi_dma->dma_tx_completion);
1021
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001022no_dma:
1023 return ret;
1024}
1025
Olivier Deprez0e641232021-09-23 10:07:05 +02001026static void omap2_mcspi_release_dma(struct spi_master *master)
1027{
1028 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1029 struct omap2_mcspi_dma *mcspi_dma;
1030 int i;
1031
1032 for (i = 0; i < master->num_chipselect; i++) {
1033 mcspi_dma = &mcspi->dma_channels[i];
1034
1035 if (mcspi_dma->dma_rx) {
1036 dma_release_channel(mcspi_dma->dma_rx);
1037 mcspi_dma->dma_rx = NULL;
1038 }
1039 if (mcspi_dma->dma_tx) {
1040 dma_release_channel(mcspi_dma->dma_tx);
1041 mcspi_dma->dma_tx = NULL;
1042 }
1043 }
1044}
1045
1046static void omap2_mcspi_cleanup(struct spi_device *spi)
1047{
1048 struct omap2_mcspi_cs *cs;
1049
1050 if (spi->controller_state) {
1051 /* Unlink controller state from context save list */
1052 cs = spi->controller_state;
1053 list_del(&cs->node);
1054
1055 kfree(cs);
1056 }
1057
1058 if (gpio_is_valid(spi->cs_gpio))
1059 gpio_free(spi->cs_gpio);
1060}
1061
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001062static int omap2_mcspi_setup(struct spi_device *spi)
1063{
Olivier Deprez0e641232021-09-23 10:07:05 +02001064 bool initial_setup = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001065 int ret;
1066 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
1067 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001068 struct omap2_mcspi_cs *cs = spi->controller_state;
1069
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001070 if (!cs) {
1071 cs = kzalloc(sizeof *cs, GFP_KERNEL);
1072 if (!cs)
1073 return -ENOMEM;
1074 cs->base = mcspi->base + spi->chip_select * 0x14;
1075 cs->phys = mcspi->phys + spi->chip_select * 0x14;
1076 cs->mode = 0;
1077 cs->chconf0 = 0;
1078 cs->chctrl0 = 0;
1079 spi->controller_state = cs;
1080 /* Link this to context save list */
1081 list_add_tail(&cs->node, &ctx->cs);
Olivier Deprez0e641232021-09-23 10:07:05 +02001082 initial_setup = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001083
1084 if (gpio_is_valid(spi->cs_gpio)) {
1085 ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
1086 if (ret) {
1087 dev_err(&spi->dev, "failed to request gpio\n");
1088 return ret;
1089 }
1090 gpio_direction_output(spi->cs_gpio,
1091 !(spi->mode & SPI_CS_HIGH));
1092 }
1093 }
1094
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001095 ret = pm_runtime_get_sync(mcspi->dev);
1096 if (ret < 0) {
1097 pm_runtime_put_noidle(mcspi->dev);
Olivier Deprez0e641232021-09-23 10:07:05 +02001098 if (initial_setup)
1099 omap2_mcspi_cleanup(spi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001100
1101 return ret;
1102 }
1103
1104 ret = omap2_mcspi_setup_transfer(spi, NULL);
Olivier Deprez0e641232021-09-23 10:07:05 +02001105 if (ret && initial_setup)
1106 omap2_mcspi_cleanup(spi);
1107
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001108 pm_runtime_mark_last_busy(mcspi->dev);
1109 pm_runtime_put_autosuspend(mcspi->dev);
1110
1111 return ret;
1112}
1113
David Brazdil0f672f62019-12-10 10:32:29 +00001114static irqreturn_t omap2_mcspi_irq_handler(int irq, void *data)
1115{
1116 struct omap2_mcspi *mcspi = data;
1117 u32 irqstat;
1118
1119 irqstat = mcspi_read_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS);
1120 if (!irqstat)
1121 return IRQ_NONE;
1122
1123 /* Disable IRQ and wakeup slave xfer task */
1124 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQENABLE, 0);
1125 if (irqstat & OMAP2_MCSPI_IRQSTATUS_EOW)
1126 complete(&mcspi->txdone);
1127
1128 return IRQ_HANDLED;
1129}
1130
1131static int omap2_mcspi_slave_abort(struct spi_master *master)
1132{
1133 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1134 struct omap2_mcspi_dma *mcspi_dma = mcspi->dma_channels;
1135
1136 mcspi->slave_aborted = true;
1137 complete(&mcspi_dma->dma_rx_completion);
1138 complete(&mcspi_dma->dma_tx_completion);
1139 complete(&mcspi->txdone);
1140
1141 return 0;
1142}
1143
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001144static int omap2_mcspi_transfer_one(struct spi_master *master,
1145 struct spi_device *spi,
1146 struct spi_transfer *t)
1147{
1148
1149 /* We only enable one channel at a time -- the one whose message is
1150 * -- although this controller would gladly
1151 * arbitrate among multiple channels. This corresponds to "single
1152 * channel" master mode. As a side effect, we need to manage the
1153 * chipselect with the FORCE bit ... CS != channel enable.
1154 */
1155
1156 struct omap2_mcspi *mcspi;
1157 struct omap2_mcspi_dma *mcspi_dma;
1158 struct omap2_mcspi_cs *cs;
1159 struct omap2_mcspi_device_config *cd;
1160 int par_override = 0;
1161 int status = 0;
1162 u32 chconf;
1163
1164 mcspi = spi_master_get_devdata(master);
1165 mcspi_dma = mcspi->dma_channels + spi->chip_select;
1166 cs = spi->controller_state;
1167 cd = spi->controller_data;
1168
1169 /*
1170 * The slave driver could have changed spi->mode in which case
1171 * it will be different from cs->mode (the current hardware setup).
1172 * If so, set par_override (even though its not a parity issue) so
1173 * omap2_mcspi_setup_transfer will be called to configure the hardware
1174 * with the correct mode on the first iteration of the loop below.
1175 */
1176 if (spi->mode != cs->mode)
1177 par_override = 1;
1178
1179 omap2_mcspi_set_enable(spi, 0);
1180
1181 if (gpio_is_valid(spi->cs_gpio))
1182 omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
1183
1184 if (par_override ||
1185 (t->speed_hz != spi->max_speed_hz) ||
1186 (t->bits_per_word != spi->bits_per_word)) {
1187 par_override = 1;
1188 status = omap2_mcspi_setup_transfer(spi, t);
1189 if (status < 0)
1190 goto out;
1191 if (t->speed_hz == spi->max_speed_hz &&
1192 t->bits_per_word == spi->bits_per_word)
1193 par_override = 0;
1194 }
1195 if (cd && cd->cs_per_word) {
1196 chconf = mcspi->ctx.modulctrl;
1197 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1198 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1199 mcspi->ctx.modulctrl =
1200 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1201 }
1202
1203 chconf = mcspi_cached_chconf0(spi);
1204 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1205 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1206
1207 if (t->tx_buf == NULL)
1208 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1209 else if (t->rx_buf == NULL)
1210 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1211
1212 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1213 /* Turbo mode is for more than one word */
1214 if (t->len > ((cs->word_len + 7) >> 3))
1215 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1216 }
1217
1218 mcspi_write_chconf0(spi, chconf);
1219
1220 if (t->len) {
1221 unsigned count;
1222
1223 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1224 master->cur_msg_mapped &&
1225 master->can_dma(master, spi, t))
1226 omap2_mcspi_set_fifo(spi, t, 1);
1227
1228 omap2_mcspi_set_enable(spi, 1);
1229
1230 /* RX_ONLY mode needs dummy data in TX reg */
1231 if (t->tx_buf == NULL)
1232 writel_relaxed(0, cs->base
1233 + OMAP2_MCSPI_TX0);
1234
1235 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1236 master->cur_msg_mapped &&
1237 master->can_dma(master, spi, t))
1238 count = omap2_mcspi_txrx_dma(spi, t);
1239 else
1240 count = omap2_mcspi_txrx_pio(spi, t);
1241
1242 if (count != t->len) {
1243 status = -EIO;
1244 goto out;
1245 }
1246 }
1247
1248 omap2_mcspi_set_enable(spi, 0);
1249
1250 if (mcspi->fifo_depth > 0)
1251 omap2_mcspi_set_fifo(spi, t, 0);
1252
1253out:
1254 /* Restore defaults if they were overriden */
1255 if (par_override) {
1256 par_override = 0;
1257 status = omap2_mcspi_setup_transfer(spi, NULL);
1258 }
1259
1260 if (cd && cd->cs_per_word) {
1261 chconf = mcspi->ctx.modulctrl;
1262 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1263 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1264 mcspi->ctx.modulctrl =
1265 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1266 }
1267
1268 omap2_mcspi_set_enable(spi, 0);
1269
1270 if (gpio_is_valid(spi->cs_gpio))
1271 omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
1272
1273 if (mcspi->fifo_depth > 0 && t)
1274 omap2_mcspi_set_fifo(spi, t, 0);
1275
1276 return status;
1277}
1278
1279static int omap2_mcspi_prepare_message(struct spi_master *master,
1280 struct spi_message *msg)
1281{
1282 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1283 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1284 struct omap2_mcspi_cs *cs;
1285
1286 /* Only a single channel can have the FORCE bit enabled
1287 * in its chconf0 register.
1288 * Scan all channels and disable them except the current one.
1289 * A FORCE can remain from a last transfer having cs_change enabled
1290 */
1291 list_for_each_entry(cs, &ctx->cs, node) {
1292 if (msg->spi->controller_state == cs)
1293 continue;
1294
1295 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
1296 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1297 writel_relaxed(cs->chconf0,
1298 cs->base + OMAP2_MCSPI_CHCONF0);
1299 readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
1300 }
1301 }
1302
1303 return 0;
1304}
1305
1306static bool omap2_mcspi_can_dma(struct spi_master *master,
1307 struct spi_device *spi,
1308 struct spi_transfer *xfer)
1309{
David Brazdil0f672f62019-12-10 10:32:29 +00001310 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
1311 struct omap2_mcspi_dma *mcspi_dma =
1312 &mcspi->dma_channels[spi->chip_select];
1313
1314 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx)
1315 return false;
1316
1317 if (spi_controller_is_slave(master))
1318 return true;
1319
Olivier Deprez0e641232021-09-23 10:07:05 +02001320 master->dma_rx = mcspi_dma->dma_rx;
1321 master->dma_tx = mcspi_dma->dma_tx;
1322
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001323 return (xfer->len >= DMA_MIN_BYTES);
1324}
1325
David Brazdil0f672f62019-12-10 10:32:29 +00001326static int omap2_mcspi_controller_setup(struct omap2_mcspi *mcspi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001327{
1328 struct spi_master *master = mcspi->master;
1329 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1330 int ret = 0;
1331
1332 ret = pm_runtime_get_sync(mcspi->dev);
1333 if (ret < 0) {
1334 pm_runtime_put_noidle(mcspi->dev);
1335
1336 return ret;
1337 }
1338
1339 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
1340 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
1341 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1342
David Brazdil0f672f62019-12-10 10:32:29 +00001343 omap2_mcspi_set_mode(master);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001344 pm_runtime_mark_last_busy(mcspi->dev);
1345 pm_runtime_put_autosuspend(mcspi->dev);
1346 return 0;
1347}
1348
1349/*
1350 * When SPI wake up from off-mode, CS is in activate state. If it was in
1351 * inactive state when driver was suspend, then force it to inactive state at
1352 * wake up.
1353 */
1354static int omap_mcspi_runtime_resume(struct device *dev)
1355{
1356 struct spi_master *master = dev_get_drvdata(dev);
1357 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1358 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1359 struct omap2_mcspi_cs *cs;
1360
1361 /* McSPI: context restore */
1362 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
1363 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
1364
1365 list_for_each_entry(cs, &ctx->cs, node) {
1366 /*
1367 * We need to toggle CS state for OMAP take this
1368 * change in account.
1369 */
1370 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1371 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1372 writel_relaxed(cs->chconf0,
1373 cs->base + OMAP2_MCSPI_CHCONF0);
1374 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1375 writel_relaxed(cs->chconf0,
1376 cs->base + OMAP2_MCSPI_CHCONF0);
1377 } else {
1378 writel_relaxed(cs->chconf0,
1379 cs->base + OMAP2_MCSPI_CHCONF0);
1380 }
1381 }
1382
1383 return 0;
1384}
1385
1386static struct omap2_mcspi_platform_config omap2_pdata = {
1387 .regs_offset = 0,
1388};
1389
1390static struct omap2_mcspi_platform_config omap4_pdata = {
1391 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1392};
1393
1394static const struct of_device_id omap_mcspi_of_match[] = {
1395 {
1396 .compatible = "ti,omap2-mcspi",
1397 .data = &omap2_pdata,
1398 },
1399 {
1400 .compatible = "ti,omap4-mcspi",
1401 .data = &omap4_pdata,
1402 },
1403 { },
1404};
1405MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
1406
1407static int omap2_mcspi_probe(struct platform_device *pdev)
1408{
1409 struct spi_master *master;
1410 const struct omap2_mcspi_platform_config *pdata;
1411 struct omap2_mcspi *mcspi;
1412 struct resource *r;
1413 int status = 0, i;
1414 u32 regs_offset = 0;
1415 struct device_node *node = pdev->dev.of_node;
1416 const struct of_device_id *match;
1417
David Brazdil0f672f62019-12-10 10:32:29 +00001418 if (of_property_read_bool(node, "spi-slave"))
1419 master = spi_alloc_slave(&pdev->dev, sizeof(*mcspi));
1420 else
1421 master = spi_alloc_master(&pdev->dev, sizeof(*mcspi));
1422 if (!master)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001423 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001424
1425 /* the spi->mode bits understood by this driver: */
1426 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1427 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1428 master->setup = omap2_mcspi_setup;
1429 master->auto_runtime_pm = true;
1430 master->prepare_message = omap2_mcspi_prepare_message;
1431 master->can_dma = omap2_mcspi_can_dma;
1432 master->transfer_one = omap2_mcspi_transfer_one;
1433 master->set_cs = omap2_mcspi_set_cs;
1434 master->cleanup = omap2_mcspi_cleanup;
David Brazdil0f672f62019-12-10 10:32:29 +00001435 master->slave_abort = omap2_mcspi_slave_abort;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001436 master->dev.of_node = node;
1437 master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
1438 master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
1439
1440 platform_set_drvdata(pdev, master);
1441
1442 mcspi = spi_master_get_devdata(master);
1443 mcspi->master = master;
1444
1445 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1446 if (match) {
1447 u32 num_cs = 1; /* default number of chipselect */
1448 pdata = match->data;
1449
1450 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1451 master->num_chipselect = num_cs;
1452 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1453 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
1454 } else {
1455 pdata = dev_get_platdata(&pdev->dev);
1456 master->num_chipselect = pdata->num_cs;
1457 mcspi->pin_dir = pdata->pin_dir;
1458 }
1459 regs_offset = pdata->regs_offset;
1460
1461 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1462 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1463 if (IS_ERR(mcspi->base)) {
1464 status = PTR_ERR(mcspi->base);
1465 goto free_master;
1466 }
1467 mcspi->phys = r->start + regs_offset;
1468 mcspi->base += regs_offset;
1469
1470 mcspi->dev = &pdev->dev;
1471
1472 INIT_LIST_HEAD(&mcspi->ctx.cs);
1473
1474 mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
1475 sizeof(struct omap2_mcspi_dma),
1476 GFP_KERNEL);
1477 if (mcspi->dma_channels == NULL) {
1478 status = -ENOMEM;
1479 goto free_master;
1480 }
1481
1482 for (i = 0; i < master->num_chipselect; i++) {
1483 sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
1484 sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
Olivier Deprez0e641232021-09-23 10:07:05 +02001485
1486 status = omap2_mcspi_request_dma(mcspi,
1487 &mcspi->dma_channels[i]);
1488 if (status == -EPROBE_DEFER)
1489 goto free_master;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001490 }
1491
David Brazdil0f672f62019-12-10 10:32:29 +00001492 status = platform_get_irq(pdev, 0);
1493 if (status == -EPROBE_DEFER)
1494 goto free_master;
1495 if (status < 0) {
1496 dev_err(&pdev->dev, "no irq resource found\n");
1497 goto free_master;
1498 }
1499 init_completion(&mcspi->txdone);
1500 status = devm_request_irq(&pdev->dev, status,
1501 omap2_mcspi_irq_handler, 0, pdev->name,
1502 mcspi);
1503 if (status) {
1504 dev_err(&pdev->dev, "Cannot request IRQ");
1505 goto free_master;
1506 }
1507
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001508 pm_runtime_use_autosuspend(&pdev->dev);
1509 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1510 pm_runtime_enable(&pdev->dev);
1511
David Brazdil0f672f62019-12-10 10:32:29 +00001512 status = omap2_mcspi_controller_setup(mcspi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001513 if (status < 0)
1514 goto disable_pm;
1515
David Brazdil0f672f62019-12-10 10:32:29 +00001516 status = devm_spi_register_controller(&pdev->dev, master);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001517 if (status < 0)
1518 goto disable_pm;
1519
1520 return status;
1521
1522disable_pm:
1523 pm_runtime_dont_use_autosuspend(&pdev->dev);
1524 pm_runtime_put_sync(&pdev->dev);
1525 pm_runtime_disable(&pdev->dev);
1526free_master:
Olivier Deprez0e641232021-09-23 10:07:05 +02001527 omap2_mcspi_release_dma(master);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001528 spi_master_put(master);
1529 return status;
1530}
1531
1532static int omap2_mcspi_remove(struct platform_device *pdev)
1533{
1534 struct spi_master *master = platform_get_drvdata(pdev);
1535 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1536
Olivier Deprez0e641232021-09-23 10:07:05 +02001537 omap2_mcspi_release_dma(master);
1538
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001539 pm_runtime_dont_use_autosuspend(mcspi->dev);
1540 pm_runtime_put_sync(mcspi->dev);
1541 pm_runtime_disable(&pdev->dev);
1542
1543 return 0;
1544}
1545
1546/* work with hotplug and coldplug */
1547MODULE_ALIAS("platform:omap2_mcspi");
1548
1549static int __maybe_unused omap2_mcspi_suspend(struct device *dev)
1550{
1551 struct spi_master *master = dev_get_drvdata(dev);
1552 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1553 int error;
1554
1555 error = pinctrl_pm_select_sleep_state(dev);
1556 if (error)
1557 dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
1558 __func__, error);
1559
1560 error = spi_master_suspend(master);
1561 if (error)
1562 dev_warn(mcspi->dev, "%s: master suspend failed: %i\n",
1563 __func__, error);
1564
1565 return pm_runtime_force_suspend(dev);
1566}
1567
1568static int __maybe_unused omap2_mcspi_resume(struct device *dev)
1569{
1570 struct spi_master *master = dev_get_drvdata(dev);
1571 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1572 int error;
1573
1574 error = pinctrl_pm_select_default_state(dev);
1575 if (error)
1576 dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
1577 __func__, error);
1578
1579 error = spi_master_resume(master);
1580 if (error)
1581 dev_warn(mcspi->dev, "%s: master resume failed: %i\n",
1582 __func__, error);
1583
1584 return pm_runtime_force_resume(dev);
1585}
1586
1587static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1588 SET_SYSTEM_SLEEP_PM_OPS(omap2_mcspi_suspend,
1589 omap2_mcspi_resume)
1590 .runtime_resume = omap_mcspi_runtime_resume,
1591};
1592
1593static struct platform_driver omap2_mcspi_driver = {
1594 .driver = {
1595 .name = "omap2_mcspi",
1596 .pm = &omap2_mcspi_pm_ops,
1597 .of_match_table = omap_mcspi_of_match,
1598 },
1599 .probe = omap2_mcspi_probe,
1600 .remove = omap2_mcspi_remove,
1601};
1602
1603module_platform_driver(omap2_mcspi_driver);
1604MODULE_LICENSE("GPL");