1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OMAP2 McSPI controller driver
5 * Copyright (C) 2005, 2006 Nokia Corporation
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>
22 #include <linux/slab.h>
23 #include <linux/pm_runtime.h>
25 #include <linux/of_device.h>
26 #include <linux/gcd.h>
28 #include <linux/spi/spi.h>
30 #include <linux/platform_data/spi-omap2-mcspi.h>
32 #define OMAP2_MCSPI_MAX_FREQ 48000000
33 #define OMAP2_MCSPI_MAX_DIVIDER 4096
34 #define OMAP2_MCSPI_MAX_FIFODEPTH 64
35 #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
36 #define SPI_AUTOSUSPEND_TIMEOUT 2000
38 #define OMAP2_MCSPI_REVISION 0x00
39 #define OMAP2_MCSPI_SYSSTATUS 0x14
40 #define OMAP2_MCSPI_IRQSTATUS 0x18
41 #define OMAP2_MCSPI_IRQENABLE 0x1c
42 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
43 #define OMAP2_MCSPI_SYST 0x24
44 #define OMAP2_MCSPI_MODULCTRL 0x28
45 #define OMAP2_MCSPI_XFERLEVEL 0x7c
47 /* per-channel banks, 0x14 bytes each, first is: */
48 #define OMAP2_MCSPI_CHCONF0 0x2c
49 #define OMAP2_MCSPI_CHSTAT0 0x30
50 #define OMAP2_MCSPI_CHCTRL0 0x34
51 #define OMAP2_MCSPI_TX0 0x38
52 #define OMAP2_MCSPI_RX0 0x3c
54 /* per-register bitmasks: */
55 #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
56 #define OMAP2_MCSPI_IRQSTATUS_TX0_EMPTY BIT(0)
57 #define OMAP2_MCSPI_IRQSTATUS_RX0_FULL BIT(2)
59 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
60 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
61 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
63 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
64 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
65 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
66 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
67 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
68 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
69 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
70 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
71 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
72 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
73 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
74 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
75 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
76 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
77 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
78 #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
79 #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
80 #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
82 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
83 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
84 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
85 #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
87 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
88 #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
90 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
92 /* We have 2 DMA channels per CS, one for RX and one for TX */
93 struct omap2_mcspi_dma {
94 struct dma_chan *dma_tx;
95 struct dma_chan *dma_rx;
97 struct completion dma_tx_completion;
98 struct completion dma_rx_completion;
100 char dma_rx_ch_name[14];
101 char dma_tx_ch_name[14];
104 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
105 * cache operations; better heuristics consider wordsize and bitrate.
107 #define DMA_MIN_BYTES 160
111 * Used for context save and restore, structure members to be updated whenever
112 * corresponding registers are modified.
114 struct omap2_mcspi_regs {
121 struct completion txdone;
122 struct spi_controller *ctlr;
123 /* Virtual base address of the controller */
126 /* SPI1 has 4 channels, while SPI2 has 2 */
127 struct omap2_mcspi_dma *dma_channels;
129 struct omap2_mcspi_regs ctx;
133 unsigned int pin_dir:1;
138 struct omap2_mcspi_cs {
143 struct list_head node;
144 /* Context save and restore shadow register */
145 u32 chconf0, chctrl0;
148 static inline void mcspi_write_reg(struct spi_controller *ctlr,
151 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
153 writel_relaxed(val, mcspi->base + idx);
156 static inline u32 mcspi_read_reg(struct spi_controller *ctlr, int idx)
158 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
160 return readl_relaxed(mcspi->base + idx);
163 static inline void mcspi_write_cs_reg(const struct spi_device *spi,
166 struct omap2_mcspi_cs *cs = spi->controller_state;
168 writel_relaxed(val, cs->base + idx);
171 static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
173 struct omap2_mcspi_cs *cs = spi->controller_state;
175 return readl_relaxed(cs->base + idx);
178 static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
180 struct omap2_mcspi_cs *cs = spi->controller_state;
185 static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
187 struct omap2_mcspi_cs *cs = spi->controller_state;
190 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
191 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
194 static inline int mcspi_bytes_per_word(int word_len)
198 else if (word_len <= 16)
200 else /* word_len <= 32 */
204 static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
205 int is_read, int enable)
209 l = mcspi_cached_chconf0(spi);
211 if (is_read) /* 1 is read, 0 write */
212 rw = OMAP2_MCSPI_CHCONF_DMAR;
214 rw = OMAP2_MCSPI_CHCONF_DMAW;
221 mcspi_write_chconf0(spi, l);
224 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
226 struct omap2_mcspi_cs *cs = spi->controller_state;
231 l |= OMAP2_MCSPI_CHCTRL_EN;
233 l &= ~OMAP2_MCSPI_CHCTRL_EN;
235 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
236 /* Flash post-writes */
237 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
240 static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
242 struct omap2_mcspi *mcspi = spi_controller_get_devdata(spi->controller);
245 /* The controller handles the inverted chip selects
246 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
247 * the inversion from the core spi_set_cs function.
249 if (spi->mode & SPI_CS_HIGH)
252 if (spi->controller_state) {
253 int err = pm_runtime_resume_and_get(mcspi->dev);
255 dev_err(mcspi->dev, "failed to get sync: %d\n", err);
259 l = mcspi_cached_chconf0(spi);
262 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
264 l |= OMAP2_MCSPI_CHCONF_FORCE;
266 mcspi_write_chconf0(spi, l);
268 pm_runtime_mark_last_busy(mcspi->dev);
269 pm_runtime_put_autosuspend(mcspi->dev);
273 static void omap2_mcspi_set_mode(struct spi_controller *ctlr)
275 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
276 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
280 * Choose host or target mode
282 l = mcspi_read_reg(ctlr, OMAP2_MCSPI_MODULCTRL);
283 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST);
284 if (spi_controller_is_target(ctlr)) {
285 l |= (OMAP2_MCSPI_MODULCTRL_MS);
287 l &= ~(OMAP2_MCSPI_MODULCTRL_MS);
288 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
290 mcspi_write_reg(ctlr, OMAP2_MCSPI_MODULCTRL, l);
295 static void omap2_mcspi_set_fifo(const struct spi_device *spi,
296 struct spi_transfer *t, int enable, int dma_enabled)
298 struct spi_controller *ctlr = spi->controller;
299 struct omap2_mcspi_cs *cs = spi->controller_state;
300 struct omap2_mcspi *mcspi;
302 int max_fifo_depth, bytes_per_word;
303 u32 chconf, xferlevel;
305 mcspi = spi_controller_get_devdata(ctlr);
307 chconf = mcspi_cached_chconf0(spi);
309 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
310 if (t->len % bytes_per_word != 0)
313 if (t->rx_buf != NULL && t->tx_buf != NULL)
314 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
316 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
318 wcnt = t->len / bytes_per_word;
321 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
324 xferlevel = wcnt << 16;
325 if (t->rx_buf != NULL) {
326 chconf |= OMAP2_MCSPI_CHCONF_FFER;
328 xferlevel |= (bytes_per_word - 1) << 8;
330 xferlevel |= (max_fifo_depth - 1) << 8;
333 if (t->tx_buf != NULL) {
334 chconf |= OMAP2_MCSPI_CHCONF_FFET;
336 xferlevel |= bytes_per_word - 1;
338 xferlevel |= (max_fifo_depth - 1);
341 mcspi_write_reg(ctlr, OMAP2_MCSPI_XFERLEVEL, xferlevel);
342 mcspi_write_chconf0(spi, chconf);
343 mcspi->fifo_depth = max_fifo_depth;
349 if (t->rx_buf != NULL)
350 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
352 if (t->tx_buf != NULL)
353 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
355 mcspi_write_chconf0(spi, chconf);
356 mcspi->fifo_depth = 0;
359 static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
361 unsigned long timeout;
363 timeout = jiffies + msecs_to_jiffies(1000);
364 while (!(readl_relaxed(reg) & bit)) {
365 if (time_after(jiffies, timeout)) {
366 if (!(readl_relaxed(reg) & bit))
376 static int mcspi_wait_for_completion(struct omap2_mcspi *mcspi,
377 struct completion *x)
379 if (spi_controller_is_target(mcspi->ctlr)) {
380 if (wait_for_completion_interruptible(x) ||
381 mcspi->target_aborted)
384 wait_for_completion(x);
390 static void omap2_mcspi_rx_callback(void *data)
392 struct spi_device *spi = data;
393 struct omap2_mcspi *mcspi = spi_controller_get_devdata(spi->controller);
394 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi_get_chipselect(spi, 0)];
396 /* We must disable the DMA RX request */
397 omap2_mcspi_set_dma_req(spi, 1, 0);
399 complete(&mcspi_dma->dma_rx_completion);
402 static void omap2_mcspi_tx_callback(void *data)
404 struct spi_device *spi = data;
405 struct omap2_mcspi *mcspi = spi_controller_get_devdata(spi->controller);
406 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi_get_chipselect(spi, 0)];
408 /* We must disable the DMA TX request */
409 omap2_mcspi_set_dma_req(spi, 0, 0);
411 complete(&mcspi_dma->dma_tx_completion);
414 static void omap2_mcspi_tx_dma(struct spi_device *spi,
415 struct spi_transfer *xfer,
416 struct dma_slave_config cfg)
418 struct omap2_mcspi *mcspi;
419 struct omap2_mcspi_dma *mcspi_dma;
420 struct dma_async_tx_descriptor *tx;
422 mcspi = spi_controller_get_devdata(spi->controller);
423 mcspi_dma = &mcspi->dma_channels[spi_get_chipselect(spi, 0)];
425 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
427 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
430 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
432 tx->callback = omap2_mcspi_tx_callback;
433 tx->callback_param = spi;
434 dmaengine_submit(tx);
436 /* FIXME: fall back to PIO? */
438 dma_async_issue_pending(mcspi_dma->dma_tx);
439 omap2_mcspi_set_dma_req(spi, 0, 1);
443 omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
444 struct dma_slave_config cfg,
447 struct omap2_mcspi *mcspi;
448 struct omap2_mcspi_dma *mcspi_dma;
449 unsigned int count, transfer_reduction = 0;
450 struct scatterlist *sg_out[2];
451 int nb_sizes = 0, out_mapped_nents[2], ret, x;
455 int word_len, element_count;
456 struct omap2_mcspi_cs *cs = spi->controller_state;
457 void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
458 struct dma_async_tx_descriptor *tx;
460 mcspi = spi_controller_get_devdata(spi->controller);
461 mcspi_dma = &mcspi->dma_channels[spi_get_chipselect(spi, 0)];
465 * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
466 * it mentions reducing DMA transfer length by one element in host
469 if (mcspi->fifo_depth == 0)
470 transfer_reduction = es;
472 word_len = cs->word_len;
473 l = mcspi_cached_chconf0(spi);
476 element_count = count;
477 else if (word_len <= 16)
478 element_count = count >> 1;
479 else /* word_len <= 32 */
480 element_count = count >> 2;
483 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
486 * Reduce DMA transfer length by one more if McSPI is
487 * configured in turbo mode.
489 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
490 transfer_reduction += es;
492 if (transfer_reduction) {
493 /* Split sgl into two. The second sgl won't be used. */
494 sizes[0] = count - transfer_reduction;
495 sizes[1] = transfer_reduction;
499 * Don't bother splitting the sgl. This essentially
500 * clones the original sgl.
506 ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents, 0, nb_sizes,
507 sizes, sg_out, out_mapped_nents, GFP_KERNEL);
510 dev_err(&spi->dev, "sg_split failed\n");
514 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, sg_out[0],
515 out_mapped_nents[0], DMA_DEV_TO_MEM,
516 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
518 tx->callback = omap2_mcspi_rx_callback;
519 tx->callback_param = spi;
520 dmaengine_submit(tx);
522 /* FIXME: fall back to PIO? */
525 dma_async_issue_pending(mcspi_dma->dma_rx);
526 omap2_mcspi_set_dma_req(spi, 1, 1);
528 ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_rx_completion);
529 if (ret || mcspi->target_aborted) {
530 dmaengine_terminate_sync(mcspi_dma->dma_rx);
531 omap2_mcspi_set_dma_req(spi, 1, 0);
535 for (x = 0; x < nb_sizes; x++)
538 if (mcspi->fifo_depth > 0)
542 * Due to the DMA transfer length reduction the missing bytes must
543 * be read manually to receive all of the expected data.
545 omap2_mcspi_set_enable(spi, 0);
547 elements = element_count - 1;
549 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
552 if (!mcspi_wait_for_reg_bit(chstat_reg,
553 OMAP2_MCSPI_CHSTAT_RXS)) {
556 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
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;
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);
571 if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
574 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
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;
582 dev_err(&spi->dev, "DMA RX last word empty\n");
583 count -= mcspi_bytes_per_word(word_len);
585 omap2_mcspi_set_enable(spi, 1);
590 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
592 struct omap2_mcspi *mcspi;
593 struct omap2_mcspi_cs *cs = spi->controller_state;
594 struct omap2_mcspi_dma *mcspi_dma;
598 struct dma_slave_config cfg;
599 enum dma_slave_buswidth width;
601 void __iomem *chstat_reg;
602 void __iomem *irqstat_reg;
605 mcspi = spi_controller_get_devdata(spi->controller);
606 mcspi_dma = &mcspi->dma_channels[spi_get_chipselect(spi, 0)];
608 if (cs->word_len <= 8) {
609 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
611 } else if (cs->word_len <= 16) {
612 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
615 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
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;
626 cfg.src_maxburst = 1;
627 cfg.dst_maxburst = 1;
632 mcspi->target_aborted = false;
633 reinit_completion(&mcspi_dma->dma_tx_completion);
634 reinit_completion(&mcspi_dma->dma_rx_completion);
635 reinit_completion(&mcspi->txdone);
637 /* Enable EOW IRQ to know end of tx in target mode */
638 if (spi_controller_is_target(spi->controller))
639 mcspi_write_reg(spi->controller,
640 OMAP2_MCSPI_IRQENABLE,
641 OMAP2_MCSPI_IRQSTATUS_EOW);
642 omap2_mcspi_tx_dma(spi, xfer, cfg);
646 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
651 ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_tx_completion);
652 if (ret || mcspi->target_aborted) {
653 dmaengine_terminate_sync(mcspi_dma->dma_tx);
654 omap2_mcspi_set_dma_req(spi, 0, 0);
658 if (spi_controller_is_target(mcspi->ctlr)) {
659 ret = mcspi_wait_for_completion(mcspi, &mcspi->txdone);
660 if (ret || mcspi->target_aborted)
664 if (mcspi->fifo_depth > 0) {
665 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
667 if (mcspi_wait_for_reg_bit(irqstat_reg,
668 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
669 dev_err(&spi->dev, "EOW timed out\n");
671 mcspi_write_reg(mcspi->ctlr, OMAP2_MCSPI_IRQSTATUS,
672 OMAP2_MCSPI_IRQSTATUS_EOW);
675 /* for TX_ONLY mode, be sure all words have shifted out */
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);
682 dev_err(&spi->dev, "TXFFE timed out\n");
684 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
685 OMAP2_MCSPI_CHSTAT_TXS);
687 dev_err(&spi->dev, "TXS timed out\n");
690 (mcspi_wait_for_reg_bit(chstat_reg,
691 OMAP2_MCSPI_CHSTAT_EOT) < 0))
692 dev_err(&spi->dev, "EOT timed out\n");
699 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
701 struct omap2_mcspi_cs *cs = spi->controller_state;
702 unsigned int count, c;
704 void __iomem *base = cs->base;
705 void __iomem *tx_reg;
706 void __iomem *rx_reg;
707 void __iomem *chstat_reg;
712 word_len = cs->word_len;
714 l = mcspi_cached_chconf0(spi);
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;
722 if (c < (word_len>>3))
735 if (mcspi_wait_for_reg_bit(chstat_reg,
736 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
737 dev_err(&spi->dev, "TXS timed out\n");
740 dev_vdbg(&spi->dev, "write-%d %02x\n",
742 writel_relaxed(*tx++, tx_reg);
745 if (mcspi_wait_for_reg_bit(chstat_reg,
746 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
747 dev_err(&spi->dev, "RXS timed out\n");
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) {
764 } else if (c == 0 && tx == NULL) {
765 omap2_mcspi_set_enable(spi, 0);
768 *rx++ = readl_relaxed(rx_reg);
769 dev_vdbg(&spi->dev, "read-%d %02x\n",
770 word_len, *(rx - 1));
772 /* Add word delay between each word */
773 spi_delay_exec(&xfer->word_delay, xfer);
775 } else if (word_len <= 16) {
784 if (mcspi_wait_for_reg_bit(chstat_reg,
785 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
786 dev_err(&spi->dev, "TXS timed out\n");
789 dev_vdbg(&spi->dev, "write-%d %04x\n",
791 writel_relaxed(*tx++, tx_reg);
794 if (mcspi_wait_for_reg_bit(chstat_reg,
795 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
796 dev_err(&spi->dev, "RXS timed out\n");
800 if (c == 2 && tx == NULL &&
801 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
802 omap2_mcspi_set_enable(spi, 0);
803 *rx++ = readl_relaxed(rx_reg);
804 dev_vdbg(&spi->dev, "read-%d %04x\n",
805 word_len, *(rx - 1));
806 if (mcspi_wait_for_reg_bit(chstat_reg,
807 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
813 } else if (c == 0 && tx == NULL) {
814 omap2_mcspi_set_enable(spi, 0);
817 *rx++ = readl_relaxed(rx_reg);
818 dev_vdbg(&spi->dev, "read-%d %04x\n",
819 word_len, *(rx - 1));
821 /* Add word delay between each word */
822 spi_delay_exec(&xfer->word_delay, xfer);
824 } else if (word_len <= 32) {
833 if (mcspi_wait_for_reg_bit(chstat_reg,
834 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
835 dev_err(&spi->dev, "TXS timed out\n");
838 dev_vdbg(&spi->dev, "write-%d %08x\n",
840 writel_relaxed(*tx++, tx_reg);
843 if (mcspi_wait_for_reg_bit(chstat_reg,
844 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
845 dev_err(&spi->dev, "RXS timed out\n");
849 if (c == 4 && tx == NULL &&
850 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
851 omap2_mcspi_set_enable(spi, 0);
852 *rx++ = readl_relaxed(rx_reg);
853 dev_vdbg(&spi->dev, "read-%d %08x\n",
854 word_len, *(rx - 1));
855 if (mcspi_wait_for_reg_bit(chstat_reg,
856 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
862 } else if (c == 0 && tx == NULL) {
863 omap2_mcspi_set_enable(spi, 0);
866 *rx++ = readl_relaxed(rx_reg);
867 dev_vdbg(&spi->dev, "read-%d %08x\n",
868 word_len, *(rx - 1));
870 /* Add word delay between each word */
871 spi_delay_exec(&xfer->word_delay, xfer);
875 /* for TX_ONLY mode, be sure all words have shifted out */
876 if (xfer->rx_buf == NULL) {
877 if (mcspi_wait_for_reg_bit(chstat_reg,
878 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
879 dev_err(&spi->dev, "TXS timed out\n");
880 } else if (mcspi_wait_for_reg_bit(chstat_reg,
881 OMAP2_MCSPI_CHSTAT_EOT) < 0)
882 dev_err(&spi->dev, "EOT timed out\n");
884 /* disable chan to purge rx datas received in TX_ONLY transfer,
885 * otherwise these rx datas will affect the direct following
888 omap2_mcspi_set_enable(spi, 0);
891 omap2_mcspi_set_enable(spi, 1);
896 omap2_mcspi_txrx_piofifo(struct spi_device *spi, struct spi_transfer *xfer)
898 struct omap2_mcspi_cs *cs = spi->controller_state;
899 struct omap2_mcspi *mcspi;
900 unsigned int count, c;
901 unsigned int iter, cwc;
903 void __iomem *base = cs->base;
904 void __iomem *tx_reg;
905 void __iomem *rx_reg;
906 void __iomem *chstat_reg;
907 void __iomem *irqstat_reg;
908 int word_len, bytes_per_word;
912 mcspi = spi_controller_get_devdata(spi->controller);
915 word_len = cs->word_len;
916 bytes_per_word = mcspi_bytes_per_word(word_len);
919 * We store the pre-calculated register addresses on stack to speed
920 * up the transfer loop.
922 tx_reg = base + OMAP2_MCSPI_TX0;
923 rx_reg = base + OMAP2_MCSPI_RX0;
924 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
925 irqstat_reg = base + OMAP2_MCSPI_IRQSTATUS;
927 if (c < (word_len >> 3))
934 /* calculate number of words in current iteration */
935 cwc = min((unsigned int)mcspi->fifo_depth / bytes_per_word,
937 last_request = cwc != (mcspi->fifo_depth / bytes_per_word);
939 if (mcspi_wait_for_reg_bit(irqstat_reg,
940 OMAP2_MCSPI_IRQSTATUS_TX0_EMPTY) < 0) {
941 dev_err(&spi->dev, "TX Empty timed out\n");
944 writel_relaxed(OMAP2_MCSPI_IRQSTATUS_TX0_EMPTY, irqstat_reg);
946 for (iter = 0; iter < cwc; iter++, tx += bytes_per_word) {
947 if (bytes_per_word == 1)
948 writel_relaxed(*tx, tx_reg);
949 else if (bytes_per_word == 2)
950 writel_relaxed(*((u16 *)tx), tx_reg);
951 else if (bytes_per_word == 4)
952 writel_relaxed(*((u32 *)tx), tx_reg);
958 mcspi_wait_for_reg_bit(irqstat_reg,
959 OMAP2_MCSPI_IRQSTATUS_RX0_FULL) < 0) {
960 dev_err(&spi->dev, "RX_FULL timed out\n");
963 writel_relaxed(OMAP2_MCSPI_IRQSTATUS_RX0_FULL, irqstat_reg);
965 for (iter = 0; iter < cwc; iter++, rx += bytes_per_word) {
967 mcspi_wait_for_reg_bit(chstat_reg,
968 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
969 dev_err(&spi->dev, "RXS timed out\n");
972 if (bytes_per_word == 1)
973 *rx = readl_relaxed(rx_reg);
974 else if (bytes_per_word == 2)
975 *((u16 *)rx) = readl_relaxed(rx_reg);
976 else if (bytes_per_word == 4)
977 *((u32 *)rx) = readl_relaxed(rx_reg);
982 if (mcspi_wait_for_reg_bit(chstat_reg,
983 OMAP2_MCSPI_CHSTAT_EOT) < 0) {
984 dev_err(&spi->dev, "EOT timed out\n");
987 if (mcspi_wait_for_reg_bit(chstat_reg,
988 OMAP2_MCSPI_CHSTAT_TXFFE) < 0) {
989 dev_err(&spi->dev, "TXFFE timed out\n");
992 omap2_mcspi_set_enable(spi, 0);
994 c -= cwc * bytes_per_word;
995 } while (c >= bytes_per_word);
998 omap2_mcspi_set_enable(spi, 1);
1002 static u32 omap2_mcspi_calc_divisor(u32 speed_hz, u32 ref_clk_hz)
1006 for (div = 0; div < 15; div++)
1007 if (speed_hz >= (ref_clk_hz >> div))
1013 /* called only when no transfer is active to this device */
1014 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
1015 struct spi_transfer *t)
1017 struct omap2_mcspi_cs *cs = spi->controller_state;
1018 struct omap2_mcspi *mcspi;
1019 u32 ref_clk_hz, l = 0, clkd = 0, div, extclk = 0, clkg = 0;
1020 u8 word_len = spi->bits_per_word;
1021 u32 speed_hz = spi->max_speed_hz;
1023 mcspi = spi_controller_get_devdata(spi->controller);
1025 if (t != NULL && t->bits_per_word)
1026 word_len = t->bits_per_word;
1028 cs->word_len = word_len;
1030 if (t && t->speed_hz)
1031 speed_hz = t->speed_hz;
1033 ref_clk_hz = mcspi->ref_clk_hz;
1034 speed_hz = min_t(u32, speed_hz, ref_clk_hz);
1035 if (speed_hz < (ref_clk_hz / OMAP2_MCSPI_MAX_DIVIDER)) {
1036 clkd = omap2_mcspi_calc_divisor(speed_hz, ref_clk_hz);
1037 speed_hz = ref_clk_hz >> clkd;
1040 div = (ref_clk_hz + speed_hz - 1) / speed_hz;
1041 speed_hz = ref_clk_hz / div;
1042 clkd = (div - 1) & 0xf;
1043 extclk = (div - 1) >> 4;
1044 clkg = OMAP2_MCSPI_CHCONF_CLKG;
1047 l = mcspi_cached_chconf0(spi);
1049 /* standard 4-wire host mode: SCK, MOSI/out, MISO/in, nCS
1050 * REVISIT: this controller could support SPI_3WIRE mode.
1052 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
1053 l &= ~OMAP2_MCSPI_CHCONF_IS;
1054 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
1055 l |= OMAP2_MCSPI_CHCONF_DPE0;
1057 l |= OMAP2_MCSPI_CHCONF_IS;
1058 l |= OMAP2_MCSPI_CHCONF_DPE1;
1059 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
1063 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
1064 l |= (word_len - 1) << 7;
1066 /* set chipselect polarity; manage with FORCE */
1067 if (!(spi->mode & SPI_CS_HIGH))
1068 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
1070 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
1072 /* set clock divisor */
1073 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
1076 /* set clock granularity */
1077 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
1080 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
1081 cs->chctrl0 |= extclk << 8;
1082 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
1085 /* set SPI mode 0..3 */
1086 if (spi->mode & SPI_CPOL)
1087 l |= OMAP2_MCSPI_CHCONF_POL;
1089 l &= ~OMAP2_MCSPI_CHCONF_POL;
1090 if (spi->mode & SPI_CPHA)
1091 l |= OMAP2_MCSPI_CHCONF_PHA;
1093 l &= ~OMAP2_MCSPI_CHCONF_PHA;
1095 mcspi_write_chconf0(spi, l);
1097 cs->mode = spi->mode;
1099 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
1101 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
1102 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
1108 * Note that we currently allow DMA only if we get a channel
1109 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
1111 static int omap2_mcspi_request_dma(struct omap2_mcspi *mcspi,
1112 struct omap2_mcspi_dma *mcspi_dma)
1116 mcspi_dma->dma_rx = dma_request_chan(mcspi->dev,
1117 mcspi_dma->dma_rx_ch_name);
1118 if (IS_ERR(mcspi_dma->dma_rx)) {
1119 ret = PTR_ERR(mcspi_dma->dma_rx);
1120 mcspi_dma->dma_rx = NULL;
1124 mcspi_dma->dma_tx = dma_request_chan(mcspi->dev,
1125 mcspi_dma->dma_tx_ch_name);
1126 if (IS_ERR(mcspi_dma->dma_tx)) {
1127 ret = PTR_ERR(mcspi_dma->dma_tx);
1128 mcspi_dma->dma_tx = NULL;
1129 dma_release_channel(mcspi_dma->dma_rx);
1130 mcspi_dma->dma_rx = NULL;
1133 init_completion(&mcspi_dma->dma_rx_completion);
1134 init_completion(&mcspi_dma->dma_tx_completion);
1140 static void omap2_mcspi_release_dma(struct spi_controller *ctlr)
1142 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
1143 struct omap2_mcspi_dma *mcspi_dma;
1146 for (i = 0; i < ctlr->num_chipselect; i++) {
1147 mcspi_dma = &mcspi->dma_channels[i];
1149 if (mcspi_dma->dma_rx) {
1150 dma_release_channel(mcspi_dma->dma_rx);
1151 mcspi_dma->dma_rx = NULL;
1153 if (mcspi_dma->dma_tx) {
1154 dma_release_channel(mcspi_dma->dma_tx);
1155 mcspi_dma->dma_tx = NULL;
1160 static void omap2_mcspi_cleanup(struct spi_device *spi)
1162 struct omap2_mcspi_cs *cs;
1164 if (spi->controller_state) {
1165 /* Unlink controller state from context save list */
1166 cs = spi->controller_state;
1167 list_del(&cs->node);
1173 static int omap2_mcspi_setup(struct spi_device *spi)
1175 bool initial_setup = false;
1177 struct omap2_mcspi *mcspi = spi_controller_get_devdata(spi->controller);
1178 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1179 struct omap2_mcspi_cs *cs = spi->controller_state;
1182 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
1185 cs->base = mcspi->base + spi_get_chipselect(spi, 0) * 0x14;
1186 cs->phys = mcspi->phys + spi_get_chipselect(spi, 0) * 0x14;
1190 spi->controller_state = cs;
1191 /* Link this to context save list */
1192 list_add_tail(&cs->node, &ctx->cs);
1193 initial_setup = true;
1196 ret = pm_runtime_resume_and_get(mcspi->dev);
1199 omap2_mcspi_cleanup(spi);
1204 ret = omap2_mcspi_setup_transfer(spi, NULL);
1205 if (ret && initial_setup)
1206 omap2_mcspi_cleanup(spi);
1208 pm_runtime_mark_last_busy(mcspi->dev);
1209 pm_runtime_put_autosuspend(mcspi->dev);
1214 static irqreturn_t omap2_mcspi_irq_handler(int irq, void *data)
1216 struct omap2_mcspi *mcspi = data;
1219 irqstat = mcspi_read_reg(mcspi->ctlr, OMAP2_MCSPI_IRQSTATUS);
1223 /* Disable IRQ and wakeup target xfer task */
1224 mcspi_write_reg(mcspi->ctlr, OMAP2_MCSPI_IRQENABLE, 0);
1225 if (irqstat & OMAP2_MCSPI_IRQSTATUS_EOW)
1226 complete(&mcspi->txdone);
1231 static int omap2_mcspi_target_abort(struct spi_controller *ctlr)
1233 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
1234 struct omap2_mcspi_dma *mcspi_dma = mcspi->dma_channels;
1236 mcspi->target_aborted = true;
1237 complete(&mcspi_dma->dma_rx_completion);
1238 complete(&mcspi_dma->dma_tx_completion);
1239 complete(&mcspi->txdone);
1244 static int omap2_mcspi_transfer_one(struct spi_controller *ctlr,
1245 struct spi_device *spi,
1246 struct spi_transfer *t)
1249 /* We only enable one channel at a time -- the one whose message is
1250 * -- although this controller would gladly
1251 * arbitrate among multiple channels. This corresponds to "single
1252 * channel" host mode. As a side effect, we need to manage the
1253 * chipselect with the FORCE bit ... CS != channel enable.
1256 struct omap2_mcspi *mcspi;
1257 struct omap2_mcspi_dma *mcspi_dma;
1258 struct omap2_mcspi_cs *cs;
1259 struct omap2_mcspi_device_config *cd;
1260 int par_override = 0;
1264 mcspi = spi_controller_get_devdata(ctlr);
1265 mcspi_dma = mcspi->dma_channels + spi_get_chipselect(spi, 0);
1266 cs = spi->controller_state;
1267 cd = spi->controller_data;
1270 * The target driver could have changed spi->mode in which case
1271 * it will be different from cs->mode (the current hardware setup).
1272 * If so, set par_override (even though its not a parity issue) so
1273 * omap2_mcspi_setup_transfer will be called to configure the hardware
1274 * with the correct mode on the first iteration of the loop below.
1276 if (spi->mode != cs->mode)
1279 omap2_mcspi_set_enable(spi, 0);
1281 if (spi_get_csgpiod(spi, 0))
1282 omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
1285 (t->speed_hz != spi->max_speed_hz) ||
1286 (t->bits_per_word != spi->bits_per_word)) {
1288 status = omap2_mcspi_setup_transfer(spi, t);
1291 if (t->speed_hz == spi->max_speed_hz &&
1292 t->bits_per_word == spi->bits_per_word)
1295 if (cd && cd->cs_per_word) {
1296 chconf = mcspi->ctx.modulctrl;
1297 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1298 mcspi_write_reg(ctlr, OMAP2_MCSPI_MODULCTRL, chconf);
1299 mcspi->ctx.modulctrl =
1300 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1303 chconf = mcspi_cached_chconf0(spi);
1304 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1305 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1307 if (t->tx_buf == NULL)
1308 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1309 else if (t->rx_buf == NULL)
1310 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1312 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1313 /* Turbo mode is for more than one word */
1314 if (t->len > ((cs->word_len + 7) >> 3))
1315 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1318 mcspi_write_chconf0(spi, chconf);
1323 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1324 ctlr->cur_msg_mapped &&
1325 ctlr->can_dma(ctlr, spi, t))
1326 omap2_mcspi_set_fifo(spi, t, 1, 1);
1327 else if (t->len > OMAP2_MCSPI_MAX_FIFODEPTH)
1328 omap2_mcspi_set_fifo(spi, t, 1, 0);
1330 omap2_mcspi_set_enable(spi, 1);
1332 /* RX_ONLY mode needs dummy data in TX reg */
1333 if (t->tx_buf == NULL)
1334 writel_relaxed(0, cs->base
1337 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1338 ctlr->cur_msg_mapped &&
1339 ctlr->can_dma(ctlr, spi, t))
1340 count = omap2_mcspi_txrx_dma(spi, t);
1341 else if (mcspi->fifo_depth > 0)
1342 count = omap2_mcspi_txrx_piofifo(spi, t);
1344 count = omap2_mcspi_txrx_pio(spi, t);
1346 if (count != t->len) {
1352 omap2_mcspi_set_enable(spi, 0);
1354 if (mcspi->fifo_depth > 0)
1355 omap2_mcspi_set_fifo(spi, t, 0, 0);
1358 /* Restore defaults if they were overriden */
1361 status = omap2_mcspi_setup_transfer(spi, NULL);
1364 if (cd && cd->cs_per_word) {
1365 chconf = mcspi->ctx.modulctrl;
1366 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1367 mcspi_write_reg(ctlr, OMAP2_MCSPI_MODULCTRL, chconf);
1368 mcspi->ctx.modulctrl =
1369 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1372 omap2_mcspi_set_enable(spi, 0);
1374 if (spi_get_csgpiod(spi, 0))
1375 omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
1377 if (mcspi->fifo_depth > 0 && t)
1378 omap2_mcspi_set_fifo(spi, t, 0, 0);
1383 static int omap2_mcspi_prepare_message(struct spi_controller *ctlr,
1384 struct spi_message *msg)
1386 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
1387 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1388 struct omap2_mcspi_cs *cs;
1390 /* Only a single channel can have the FORCE bit enabled
1391 * in its chconf0 register.
1392 * Scan all channels and disable them except the current one.
1393 * A FORCE can remain from a last transfer having cs_change enabled
1395 list_for_each_entry(cs, &ctx->cs, node) {
1396 if (msg->spi->controller_state == cs)
1399 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
1400 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1401 writel_relaxed(cs->chconf0,
1402 cs->base + OMAP2_MCSPI_CHCONF0);
1403 readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
1410 static bool omap2_mcspi_can_dma(struct spi_controller *ctlr,
1411 struct spi_device *spi,
1412 struct spi_transfer *xfer)
1414 struct omap2_mcspi *mcspi = spi_controller_get_devdata(spi->controller);
1415 struct omap2_mcspi_dma *mcspi_dma =
1416 &mcspi->dma_channels[spi_get_chipselect(spi, 0)];
1418 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx)
1421 if (spi_controller_is_target(ctlr))
1424 ctlr->dma_rx = mcspi_dma->dma_rx;
1425 ctlr->dma_tx = mcspi_dma->dma_tx;
1427 return (xfer->len >= DMA_MIN_BYTES);
1430 static size_t omap2_mcspi_max_xfer_size(struct spi_device *spi)
1432 struct omap2_mcspi *mcspi = spi_controller_get_devdata(spi->controller);
1433 struct omap2_mcspi_dma *mcspi_dma =
1434 &mcspi->dma_channels[spi_get_chipselect(spi, 0)];
1436 if (mcspi->max_xfer_len && mcspi_dma->dma_rx)
1437 return mcspi->max_xfer_len;
1442 static int omap2_mcspi_controller_setup(struct omap2_mcspi *mcspi)
1444 struct spi_controller *ctlr = mcspi->ctlr;
1445 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1448 ret = pm_runtime_resume_and_get(mcspi->dev);
1452 mcspi_write_reg(ctlr, OMAP2_MCSPI_WAKEUPENABLE,
1453 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
1454 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1456 omap2_mcspi_set_mode(ctlr);
1457 pm_runtime_mark_last_busy(mcspi->dev);
1458 pm_runtime_put_autosuspend(mcspi->dev);
1462 static int omap_mcspi_runtime_suspend(struct device *dev)
1466 error = pinctrl_pm_select_idle_state(dev);
1468 dev_warn(dev, "%s: failed to set pins: %i\n", __func__, error);
1474 * When SPI wake up from off-mode, CS is in activate state. If it was in
1475 * inactive state when driver was suspend, then force it to inactive state at
1478 static int omap_mcspi_runtime_resume(struct device *dev)
1480 struct spi_controller *ctlr = dev_get_drvdata(dev);
1481 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
1482 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1483 struct omap2_mcspi_cs *cs;
1486 error = pinctrl_pm_select_default_state(dev);
1488 dev_warn(dev, "%s: failed to set pins: %i\n", __func__, error);
1490 /* McSPI: context restore */
1491 mcspi_write_reg(ctlr, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
1492 mcspi_write_reg(ctlr, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
1494 list_for_each_entry(cs, &ctx->cs, node) {
1496 * We need to toggle CS state for OMAP take this
1497 * change in account.
1499 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1500 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1501 writel_relaxed(cs->chconf0,
1502 cs->base + OMAP2_MCSPI_CHCONF0);
1503 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1504 writel_relaxed(cs->chconf0,
1505 cs->base + OMAP2_MCSPI_CHCONF0);
1507 writel_relaxed(cs->chconf0,
1508 cs->base + OMAP2_MCSPI_CHCONF0);
1515 static struct omap2_mcspi_platform_config omap2_pdata = {
1519 static struct omap2_mcspi_platform_config omap4_pdata = {
1520 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1523 static struct omap2_mcspi_platform_config am654_pdata = {
1524 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1525 .max_xfer_len = SZ_4K - 1,
1528 static const struct of_device_id omap_mcspi_of_match[] = {
1530 .compatible = "ti,omap2-mcspi",
1531 .data = &omap2_pdata,
1534 .compatible = "ti,omap4-mcspi",
1535 .data = &omap4_pdata,
1538 .compatible = "ti,am654-mcspi",
1539 .data = &am654_pdata,
1543 MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
1545 static int omap2_mcspi_probe(struct platform_device *pdev)
1547 struct spi_controller *ctlr;
1548 const struct omap2_mcspi_platform_config *pdata;
1549 struct omap2_mcspi *mcspi;
1552 u32 regs_offset = 0;
1553 struct device_node *node = pdev->dev.of_node;
1554 const struct of_device_id *match;
1556 if (of_property_read_bool(node, "spi-slave"))
1557 ctlr = spi_alloc_target(&pdev->dev, sizeof(*mcspi));
1559 ctlr = spi_alloc_host(&pdev->dev, sizeof(*mcspi));
1563 /* the spi->mode bits understood by this driver: */
1564 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1565 ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1566 ctlr->setup = omap2_mcspi_setup;
1567 ctlr->auto_runtime_pm = true;
1568 ctlr->prepare_message = omap2_mcspi_prepare_message;
1569 ctlr->can_dma = omap2_mcspi_can_dma;
1570 ctlr->transfer_one = omap2_mcspi_transfer_one;
1571 ctlr->set_cs = omap2_mcspi_set_cs;
1572 ctlr->cleanup = omap2_mcspi_cleanup;
1573 ctlr->target_abort = omap2_mcspi_target_abort;
1574 ctlr->dev.of_node = node;
1575 ctlr->use_gpio_descriptors = true;
1577 platform_set_drvdata(pdev, ctlr);
1579 mcspi = spi_controller_get_devdata(ctlr);
1582 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1584 u32 num_cs = 1; /* default number of chipselect */
1585 pdata = match->data;
1587 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1588 ctlr->num_chipselect = num_cs;
1589 if (of_property_read_bool(node, "ti,pindir-d0-out-d1-in"))
1590 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
1592 pdata = dev_get_platdata(&pdev->dev);
1593 ctlr->num_chipselect = pdata->num_cs;
1594 mcspi->pin_dir = pdata->pin_dir;
1596 regs_offset = pdata->regs_offset;
1597 if (pdata->max_xfer_len) {
1598 mcspi->max_xfer_len = pdata->max_xfer_len;
1599 ctlr->max_transfer_size = omap2_mcspi_max_xfer_size;
1602 mcspi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
1603 if (IS_ERR(mcspi->base)) {
1604 status = PTR_ERR(mcspi->base);
1607 mcspi->phys = r->start + regs_offset;
1608 mcspi->base += regs_offset;
1610 mcspi->dev = &pdev->dev;
1612 INIT_LIST_HEAD(&mcspi->ctx.cs);
1614 mcspi->dma_channels = devm_kcalloc(&pdev->dev, ctlr->num_chipselect,
1615 sizeof(struct omap2_mcspi_dma),
1617 if (mcspi->dma_channels == NULL) {
1622 for (i = 0; i < ctlr->num_chipselect; i++) {
1623 sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
1624 sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
1626 status = omap2_mcspi_request_dma(mcspi,
1627 &mcspi->dma_channels[i]);
1628 if (status == -EPROBE_DEFER)
1632 status = platform_get_irq(pdev, 0);
1635 init_completion(&mcspi->txdone);
1636 status = devm_request_irq(&pdev->dev, status,
1637 omap2_mcspi_irq_handler, 0, pdev->name,
1640 dev_err(&pdev->dev, "Cannot request IRQ");
1644 mcspi->ref_clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
1646 mcspi->ref_clk_hz = clk_get_rate(mcspi->ref_clk);
1648 mcspi->ref_clk_hz = OMAP2_MCSPI_MAX_FREQ;
1649 ctlr->max_speed_hz = mcspi->ref_clk_hz;
1650 ctlr->min_speed_hz = mcspi->ref_clk_hz >> 15;
1652 pm_runtime_use_autosuspend(&pdev->dev);
1653 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1654 pm_runtime_enable(&pdev->dev);
1656 status = omap2_mcspi_controller_setup(mcspi);
1660 status = devm_spi_register_controller(&pdev->dev, ctlr);
1667 pm_runtime_dont_use_autosuspend(&pdev->dev);
1668 pm_runtime_put_sync(&pdev->dev);
1669 pm_runtime_disable(&pdev->dev);
1671 omap2_mcspi_release_dma(ctlr);
1672 spi_controller_put(ctlr);
1676 static void omap2_mcspi_remove(struct platform_device *pdev)
1678 struct spi_controller *ctlr = platform_get_drvdata(pdev);
1679 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
1681 omap2_mcspi_release_dma(ctlr);
1683 pm_runtime_dont_use_autosuspend(mcspi->dev);
1684 pm_runtime_put_sync(mcspi->dev);
1685 pm_runtime_disable(&pdev->dev);
1688 /* work with hotplug and coldplug */
1689 MODULE_ALIAS("platform:omap2_mcspi");
1691 static int __maybe_unused omap2_mcspi_suspend(struct device *dev)
1693 struct spi_controller *ctlr = dev_get_drvdata(dev);
1694 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
1697 error = pinctrl_pm_select_sleep_state(dev);
1699 dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
1702 error = spi_controller_suspend(ctlr);
1704 dev_warn(mcspi->dev, "%s: controller suspend failed: %i\n",
1707 return pm_runtime_force_suspend(dev);
1710 static int __maybe_unused omap2_mcspi_resume(struct device *dev)
1712 struct spi_controller *ctlr = dev_get_drvdata(dev);
1713 struct omap2_mcspi *mcspi = spi_controller_get_devdata(ctlr);
1716 error = spi_controller_resume(ctlr);
1718 dev_warn(mcspi->dev, "%s: controller resume failed: %i\n",
1721 return pm_runtime_force_resume(dev);
1724 static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1725 SET_SYSTEM_SLEEP_PM_OPS(omap2_mcspi_suspend,
1727 .runtime_suspend = omap_mcspi_runtime_suspend,
1728 .runtime_resume = omap_mcspi_runtime_resume,
1731 static struct platform_driver omap2_mcspi_driver = {
1733 .name = "omap2_mcspi",
1734 .pm = &omap2_mcspi_pm_ops,
1735 .of_match_table = omap_mcspi_of_match,
1737 .probe = omap2_mcspi_probe,
1738 .remove_new = omap2_mcspi_remove,
1741 module_platform_driver(omap2_mcspi_driver);
1742 MODULE_LICENSE("GPL");