2 * OMAP2 McSPI controller driver
4 * Copyright (C) 2005, 2006 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
6 * Juha Yrj�l� <juha.yrjola@nokia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/dmaengine.h>
32 #include <linux/omap-dma.h>
33 #include <linux/platform_device.h>
34 #include <linux/err.h>
35 #include <linux/clk.h>
37 #include <linux/slab.h>
38 #include <linux/pm_runtime.h>
40 #include <linux/of_device.h>
41 #include <linux/gcd.h>
43 #include <linux/spi/spi.h>
45 #include <linux/platform_data/spi-omap2-mcspi.h>
47 #define OMAP2_MCSPI_MAX_FREQ 48000000
48 #define OMAP2_MCSPI_MAX_FIFODEPTH 64
49 #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
50 #define SPI_AUTOSUSPEND_TIMEOUT 2000
52 #define OMAP2_MCSPI_REVISION 0x00
53 #define OMAP2_MCSPI_SYSSTATUS 0x14
54 #define OMAP2_MCSPI_IRQSTATUS 0x18
55 #define OMAP2_MCSPI_IRQENABLE 0x1c
56 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
57 #define OMAP2_MCSPI_SYST 0x24
58 #define OMAP2_MCSPI_MODULCTRL 0x28
59 #define OMAP2_MCSPI_XFERLEVEL 0x7c
61 /* per-channel banks, 0x14 bytes each, first is: */
62 #define OMAP2_MCSPI_CHCONF0 0x2c
63 #define OMAP2_MCSPI_CHSTAT0 0x30
64 #define OMAP2_MCSPI_CHCTRL0 0x34
65 #define OMAP2_MCSPI_TX0 0x38
66 #define OMAP2_MCSPI_RX0 0x3c
68 /* per-register bitmasks: */
69 #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
71 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
72 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
73 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
75 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
76 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
77 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
78 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
79 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
80 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
81 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
82 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
83 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
84 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
85 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
86 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
87 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
88 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
89 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
90 #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
91 #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
93 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
94 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
95 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
96 #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
98 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
100 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
102 /* We have 2 DMA channels per CS, one for RX and one for TX */
103 struct omap2_mcspi_dma {
104 struct dma_chan *dma_tx;
105 struct dma_chan *dma_rx;
110 struct completion dma_tx_completion;
111 struct completion dma_rx_completion;
113 char dma_rx_ch_name[14];
114 char dma_tx_ch_name[14];
117 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
118 * cache operations; better heuristics consider wordsize and bitrate.
120 #define DMA_MIN_BYTES 160
124 * Used for context save and restore, structure members to be updated whenever
125 * corresponding registers are modified.
127 struct omap2_mcspi_regs {
134 struct spi_master *master;
135 /* Virtual base address of the controller */
138 /* SPI1 has 4 channels, while SPI2 has 2 */
139 struct omap2_mcspi_dma *dma_channels;
141 struct omap2_mcspi_regs ctx;
143 unsigned int pin_dir:1;
146 struct omap2_mcspi_cs {
150 struct list_head node;
151 /* Context save and restore shadow register */
155 static inline void mcspi_write_reg(struct spi_master *master,
158 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
160 __raw_writel(val, mcspi->base + idx);
163 static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
165 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
167 return __raw_readl(mcspi->base + idx);
170 static inline void mcspi_write_cs_reg(const struct spi_device *spi,
173 struct omap2_mcspi_cs *cs = spi->controller_state;
175 __raw_writel(val, cs->base + idx);
178 static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
180 struct omap2_mcspi_cs *cs = spi->controller_state;
182 return __raw_readl(cs->base + idx);
185 static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
187 struct omap2_mcspi_cs *cs = spi->controller_state;
192 static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
194 struct omap2_mcspi_cs *cs = spi->controller_state;
197 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
198 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
201 static inline int mcspi_bytes_per_word(int word_len)
205 else if (word_len <= 16)
207 else /* word_len <= 32 */
211 static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
212 int is_read, int enable)
216 l = mcspi_cached_chconf0(spi);
218 if (is_read) /* 1 is read, 0 write */
219 rw = OMAP2_MCSPI_CHCONF_DMAR;
221 rw = OMAP2_MCSPI_CHCONF_DMAW;
228 mcspi_write_chconf0(spi, l);
231 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
235 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
236 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
237 /* Flash post-writes */
238 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
241 static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
245 l = mcspi_cached_chconf0(spi);
247 l |= OMAP2_MCSPI_CHCONF_FORCE;
249 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
251 mcspi_write_chconf0(spi, l);
254 static void omap2_mcspi_set_master_mode(struct spi_master *master)
256 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
257 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
261 * Setup when switching from (reset default) slave mode
262 * to single-channel master mode
264 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
265 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
266 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
267 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
272 static void omap2_mcspi_set_fifo(const struct spi_device *spi,
273 struct spi_transfer *t, int enable)
275 struct spi_master *master = spi->master;
276 struct omap2_mcspi_cs *cs = spi->controller_state;
277 struct omap2_mcspi *mcspi;
279 int fifo_depth, bytes_per_word;
280 u32 chconf, xferlevel;
282 mcspi = spi_master_get_devdata(master);
284 chconf = mcspi_cached_chconf0(spi);
286 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
287 if (t->len % bytes_per_word != 0)
290 fifo_depth = gcd(t->len, OMAP2_MCSPI_MAX_FIFODEPTH);
291 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
294 wcnt = t->len / bytes_per_word;
295 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
298 xferlevel = wcnt << 16;
299 if (t->rx_buf != NULL) {
300 chconf |= OMAP2_MCSPI_CHCONF_FFER;
301 xferlevel |= (fifo_depth - 1) << 8;
303 chconf |= OMAP2_MCSPI_CHCONF_FFET;
304 xferlevel |= fifo_depth - 1;
307 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
308 mcspi_write_chconf0(spi, chconf);
309 mcspi->fifo_depth = fifo_depth;
315 if (t->rx_buf != NULL)
316 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
318 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
320 mcspi_write_chconf0(spi, chconf);
321 mcspi->fifo_depth = 0;
324 static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
326 struct spi_master *spi_cntrl = mcspi->master;
327 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
328 struct omap2_mcspi_cs *cs;
330 /* McSPI: context restore */
331 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
332 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
334 list_for_each_entry(cs, &ctx->cs, node)
335 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
338 static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
340 unsigned long timeout;
342 timeout = jiffies + msecs_to_jiffies(1000);
343 while (!(__raw_readl(reg) & bit)) {
344 if (time_after(jiffies, timeout)) {
345 if (!(__raw_readl(reg) & bit))
355 static void omap2_mcspi_rx_callback(void *data)
357 struct spi_device *spi = data;
358 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
359 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
361 /* We must disable the DMA RX request */
362 omap2_mcspi_set_dma_req(spi, 1, 0);
364 complete(&mcspi_dma->dma_rx_completion);
367 static void omap2_mcspi_tx_callback(void *data)
369 struct spi_device *spi = data;
370 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
371 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
373 /* We must disable the DMA TX request */
374 omap2_mcspi_set_dma_req(spi, 0, 0);
376 complete(&mcspi_dma->dma_tx_completion);
379 static void omap2_mcspi_tx_dma(struct spi_device *spi,
380 struct spi_transfer *xfer,
381 struct dma_slave_config cfg)
383 struct omap2_mcspi *mcspi;
384 struct omap2_mcspi_dma *mcspi_dma;
387 mcspi = spi_master_get_devdata(spi->master);
388 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
391 if (mcspi_dma->dma_tx) {
392 struct dma_async_tx_descriptor *tx;
393 struct scatterlist sg;
395 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
397 sg_init_table(&sg, 1);
398 sg_dma_address(&sg) = xfer->tx_dma;
399 sg_dma_len(&sg) = xfer->len;
401 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
402 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
404 tx->callback = omap2_mcspi_tx_callback;
405 tx->callback_param = spi;
406 dmaengine_submit(tx);
408 /* FIXME: fall back to PIO? */
411 dma_async_issue_pending(mcspi_dma->dma_tx);
412 omap2_mcspi_set_dma_req(spi, 0, 1);
417 omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
418 struct dma_slave_config cfg,
421 struct omap2_mcspi *mcspi;
422 struct omap2_mcspi_dma *mcspi_dma;
423 unsigned int count, dma_count;
426 int word_len, element_count;
427 struct omap2_mcspi_cs *cs = spi->controller_state;
428 mcspi = spi_master_get_devdata(spi->master);
429 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
431 dma_count = xfer->len;
433 if (mcspi->fifo_depth == 0)
436 word_len = cs->word_len;
437 l = mcspi_cached_chconf0(spi);
440 element_count = count;
441 else if (word_len <= 16)
442 element_count = count >> 1;
443 else /* word_len <= 32 */
444 element_count = count >> 2;
446 if (mcspi_dma->dma_rx) {
447 struct dma_async_tx_descriptor *tx;
448 struct scatterlist sg;
450 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
452 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
455 sg_init_table(&sg, 1);
456 sg_dma_address(&sg) = xfer->rx_dma;
457 sg_dma_len(&sg) = dma_count;
459 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
460 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
463 tx->callback = omap2_mcspi_rx_callback;
464 tx->callback_param = spi;
465 dmaengine_submit(tx);
467 /* FIXME: fall back to PIO? */
471 dma_async_issue_pending(mcspi_dma->dma_rx);
472 omap2_mcspi_set_dma_req(spi, 1, 1);
474 wait_for_completion(&mcspi_dma->dma_rx_completion);
475 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
478 if (mcspi->fifo_depth > 0)
481 omap2_mcspi_set_enable(spi, 0);
483 elements = element_count - 1;
485 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
488 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
489 & OMAP2_MCSPI_CHSTAT_RXS)) {
492 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
494 ((u8 *)xfer->rx_buf)[elements++] = w;
495 else if (word_len <= 16)
496 ((u16 *)xfer->rx_buf)[elements++] = w;
497 else /* word_len <= 32 */
498 ((u32 *)xfer->rx_buf)[elements++] = w;
500 int bytes_per_word = mcspi_bytes_per_word(word_len);
501 dev_err(&spi->dev, "DMA RX penultimate word empty");
502 count -= (bytes_per_word << 1);
503 omap2_mcspi_set_enable(spi, 1);
507 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
508 & OMAP2_MCSPI_CHSTAT_RXS)) {
511 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
513 ((u8 *)xfer->rx_buf)[elements] = w;
514 else if (word_len <= 16)
515 ((u16 *)xfer->rx_buf)[elements] = w;
516 else /* word_len <= 32 */
517 ((u32 *)xfer->rx_buf)[elements] = w;
519 dev_err(&spi->dev, "DMA RX last word empty");
520 count -= mcspi_bytes_per_word(word_len);
522 omap2_mcspi_set_enable(spi, 1);
527 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
529 struct omap2_mcspi *mcspi;
530 struct omap2_mcspi_cs *cs = spi->controller_state;
531 struct omap2_mcspi_dma *mcspi_dma;
536 struct dma_slave_config cfg;
537 enum dma_slave_buswidth width;
540 void __iomem *chstat_reg;
541 void __iomem *irqstat_reg;
544 mcspi = spi_master_get_devdata(spi->master);
545 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
546 l = mcspi_cached_chconf0(spi);
549 if (cs->word_len <= 8) {
550 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
552 } else if (cs->word_len <= 16) {
553 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
556 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
563 if (mcspi->fifo_depth > 0) {
564 if (count > mcspi->fifo_depth)
565 burst = mcspi->fifo_depth / es;
570 memset(&cfg, 0, sizeof(cfg));
571 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
572 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
573 cfg.src_addr_width = width;
574 cfg.dst_addr_width = width;
575 cfg.src_maxburst = burst;
576 cfg.dst_maxburst = burst;
582 omap2_mcspi_tx_dma(spi, xfer, cfg);
585 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
588 wait_for_completion(&mcspi_dma->dma_tx_completion);
589 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
592 if (mcspi->fifo_depth > 0) {
593 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
595 if (mcspi_wait_for_reg_bit(irqstat_reg,
596 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
597 dev_err(&spi->dev, "EOW timed out\n");
599 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
600 OMAP2_MCSPI_IRQSTATUS_EOW);
603 /* for TX_ONLY mode, be sure all words have shifted out */
605 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
606 if (mcspi->fifo_depth > 0) {
607 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
608 OMAP2_MCSPI_CHSTAT_TXFFE);
610 dev_err(&spi->dev, "TXFFE timed out\n");
612 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
613 OMAP2_MCSPI_CHSTAT_TXS);
615 dev_err(&spi->dev, "TXS timed out\n");
618 (mcspi_wait_for_reg_bit(chstat_reg,
619 OMAP2_MCSPI_CHSTAT_EOT) < 0))
620 dev_err(&spi->dev, "EOT timed out\n");
627 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
629 struct omap2_mcspi *mcspi;
630 struct omap2_mcspi_cs *cs = spi->controller_state;
631 unsigned int count, c;
633 void __iomem *base = cs->base;
634 void __iomem *tx_reg;
635 void __iomem *rx_reg;
636 void __iomem *chstat_reg;
639 mcspi = spi_master_get_devdata(spi->master);
642 word_len = cs->word_len;
644 l = mcspi_cached_chconf0(spi);
646 /* We store the pre-calculated register addresses on stack to speed
647 * up the transfer loop. */
648 tx_reg = base + OMAP2_MCSPI_TX0;
649 rx_reg = base + OMAP2_MCSPI_RX0;
650 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
652 if (c < (word_len>>3))
665 if (mcspi_wait_for_reg_bit(chstat_reg,
666 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
667 dev_err(&spi->dev, "TXS timed out\n");
670 dev_vdbg(&spi->dev, "write-%d %02x\n",
672 __raw_writel(*tx++, tx_reg);
675 if (mcspi_wait_for_reg_bit(chstat_reg,
676 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
677 dev_err(&spi->dev, "RXS timed out\n");
681 if (c == 1 && tx == NULL &&
682 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
683 omap2_mcspi_set_enable(spi, 0);
684 *rx++ = __raw_readl(rx_reg);
685 dev_vdbg(&spi->dev, "read-%d %02x\n",
686 word_len, *(rx - 1));
687 if (mcspi_wait_for_reg_bit(chstat_reg,
688 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
694 } else if (c == 0 && tx == NULL) {
695 omap2_mcspi_set_enable(spi, 0);
698 *rx++ = __raw_readl(rx_reg);
699 dev_vdbg(&spi->dev, "read-%d %02x\n",
700 word_len, *(rx - 1));
703 } else if (word_len <= 16) {
712 if (mcspi_wait_for_reg_bit(chstat_reg,
713 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
714 dev_err(&spi->dev, "TXS timed out\n");
717 dev_vdbg(&spi->dev, "write-%d %04x\n",
719 __raw_writel(*tx++, tx_reg);
722 if (mcspi_wait_for_reg_bit(chstat_reg,
723 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
724 dev_err(&spi->dev, "RXS timed out\n");
728 if (c == 2 && tx == NULL &&
729 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
730 omap2_mcspi_set_enable(spi, 0);
731 *rx++ = __raw_readl(rx_reg);
732 dev_vdbg(&spi->dev, "read-%d %04x\n",
733 word_len, *(rx - 1));
734 if (mcspi_wait_for_reg_bit(chstat_reg,
735 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
741 } else if (c == 0 && tx == NULL) {
742 omap2_mcspi_set_enable(spi, 0);
745 *rx++ = __raw_readl(rx_reg);
746 dev_vdbg(&spi->dev, "read-%d %04x\n",
747 word_len, *(rx - 1));
750 } else if (word_len <= 32) {
759 if (mcspi_wait_for_reg_bit(chstat_reg,
760 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
761 dev_err(&spi->dev, "TXS timed out\n");
764 dev_vdbg(&spi->dev, "write-%d %08x\n",
766 __raw_writel(*tx++, tx_reg);
769 if (mcspi_wait_for_reg_bit(chstat_reg,
770 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
771 dev_err(&spi->dev, "RXS timed out\n");
775 if (c == 4 && tx == NULL &&
776 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
777 omap2_mcspi_set_enable(spi, 0);
778 *rx++ = __raw_readl(rx_reg);
779 dev_vdbg(&spi->dev, "read-%d %08x\n",
780 word_len, *(rx - 1));
781 if (mcspi_wait_for_reg_bit(chstat_reg,
782 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
788 } else if (c == 0 && tx == NULL) {
789 omap2_mcspi_set_enable(spi, 0);
792 *rx++ = __raw_readl(rx_reg);
793 dev_vdbg(&spi->dev, "read-%d %08x\n",
794 word_len, *(rx - 1));
799 /* for TX_ONLY mode, be sure all words have shifted out */
800 if (xfer->rx_buf == NULL) {
801 if (mcspi_wait_for_reg_bit(chstat_reg,
802 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
803 dev_err(&spi->dev, "TXS timed out\n");
804 } else if (mcspi_wait_for_reg_bit(chstat_reg,
805 OMAP2_MCSPI_CHSTAT_EOT) < 0)
806 dev_err(&spi->dev, "EOT timed out\n");
808 /* disable chan to purge rx datas received in TX_ONLY transfer,
809 * otherwise these rx datas will affect the direct following
812 omap2_mcspi_set_enable(spi, 0);
815 omap2_mcspi_set_enable(spi, 1);
819 static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
823 for (div = 0; div < 15; div++)
824 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
830 /* called only when no transfer is active to this device */
831 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
832 struct spi_transfer *t)
834 struct omap2_mcspi_cs *cs = spi->controller_state;
835 struct omap2_mcspi *mcspi;
836 struct spi_master *spi_cntrl;
838 u8 word_len = spi->bits_per_word;
839 u32 speed_hz = spi->max_speed_hz;
841 mcspi = spi_master_get_devdata(spi->master);
842 spi_cntrl = mcspi->master;
844 if (t != NULL && t->bits_per_word)
845 word_len = t->bits_per_word;
847 cs->word_len = word_len;
849 if (t && t->speed_hz)
850 speed_hz = t->speed_hz;
852 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
853 div = omap2_mcspi_calc_divisor(speed_hz);
855 l = mcspi_cached_chconf0(spi);
857 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
858 * REVISIT: this controller could support SPI_3WIRE mode.
860 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
861 l &= ~OMAP2_MCSPI_CHCONF_IS;
862 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
863 l |= OMAP2_MCSPI_CHCONF_DPE0;
865 l |= OMAP2_MCSPI_CHCONF_IS;
866 l |= OMAP2_MCSPI_CHCONF_DPE1;
867 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
871 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
872 l |= (word_len - 1) << 7;
874 /* set chipselect polarity; manage with FORCE */
875 if (!(spi->mode & SPI_CS_HIGH))
876 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
878 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
880 /* set clock divisor */
881 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
884 /* set SPI mode 0..3 */
885 if (spi->mode & SPI_CPOL)
886 l |= OMAP2_MCSPI_CHCONF_POL;
888 l &= ~OMAP2_MCSPI_CHCONF_POL;
889 if (spi->mode & SPI_CPHA)
890 l |= OMAP2_MCSPI_CHCONF_PHA;
892 l &= ~OMAP2_MCSPI_CHCONF_PHA;
894 mcspi_write_chconf0(spi, l);
896 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
897 OMAP2_MCSPI_MAX_FREQ >> div,
898 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
899 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
905 * Note that we currently allow DMA only if we get a channel
906 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
908 static int omap2_mcspi_request_dma(struct spi_device *spi)
910 struct spi_master *master = spi->master;
911 struct omap2_mcspi *mcspi;
912 struct omap2_mcspi_dma *mcspi_dma;
916 mcspi = spi_master_get_devdata(master);
917 mcspi_dma = mcspi->dma_channels + spi->chip_select;
919 init_completion(&mcspi_dma->dma_rx_completion);
920 init_completion(&mcspi_dma->dma_tx_completion);
923 dma_cap_set(DMA_SLAVE, mask);
924 sig = mcspi_dma->dma_rx_sync_dev;
927 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
929 mcspi_dma->dma_rx_ch_name);
930 if (!mcspi_dma->dma_rx)
933 sig = mcspi_dma->dma_tx_sync_dev;
935 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
937 mcspi_dma->dma_tx_ch_name);
939 if (!mcspi_dma->dma_tx) {
940 dma_release_channel(mcspi_dma->dma_rx);
941 mcspi_dma->dma_rx = NULL;
948 dev_warn(&spi->dev, "not using DMA for McSPI\n");
952 static int omap2_mcspi_setup(struct spi_device *spi)
955 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
956 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
957 struct omap2_mcspi_dma *mcspi_dma;
958 struct omap2_mcspi_cs *cs = spi->controller_state;
960 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
963 cs = kzalloc(sizeof *cs, GFP_KERNEL);
966 cs->base = mcspi->base + spi->chip_select * 0x14;
967 cs->phys = mcspi->phys + spi->chip_select * 0x14;
969 spi->controller_state = cs;
970 /* Link this to context save list */
971 list_add_tail(&cs->node, &ctx->cs);
974 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
975 ret = omap2_mcspi_request_dma(spi);
976 if (ret < 0 && ret != -EAGAIN)
980 ret = pm_runtime_get_sync(mcspi->dev);
984 ret = omap2_mcspi_setup_transfer(spi, NULL);
985 pm_runtime_mark_last_busy(mcspi->dev);
986 pm_runtime_put_autosuspend(mcspi->dev);
991 static void omap2_mcspi_cleanup(struct spi_device *spi)
993 struct omap2_mcspi *mcspi;
994 struct omap2_mcspi_dma *mcspi_dma;
995 struct omap2_mcspi_cs *cs;
997 mcspi = spi_master_get_devdata(spi->master);
999 if (spi->controller_state) {
1000 /* Unlink controller state from context save list */
1001 cs = spi->controller_state;
1002 list_del(&cs->node);
1007 if (spi->chip_select < spi->master->num_chipselect) {
1008 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1010 if (mcspi_dma->dma_rx) {
1011 dma_release_channel(mcspi_dma->dma_rx);
1012 mcspi_dma->dma_rx = NULL;
1014 if (mcspi_dma->dma_tx) {
1015 dma_release_channel(mcspi_dma->dma_tx);
1016 mcspi_dma->dma_tx = NULL;
1021 static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
1024 /* We only enable one channel at a time -- the one whose message is
1025 * -- although this controller would gladly
1026 * arbitrate among multiple channels. This corresponds to "single
1027 * channel" master mode. As a side effect, we need to manage the
1028 * chipselect with the FORCE bit ... CS != channel enable.
1031 struct spi_device *spi;
1032 struct spi_transfer *t = NULL;
1033 struct spi_master *master;
1034 struct omap2_mcspi_dma *mcspi_dma;
1036 struct omap2_mcspi_cs *cs;
1037 struct omap2_mcspi_device_config *cd;
1038 int par_override = 0;
1043 master = spi->master;
1044 mcspi_dma = mcspi->dma_channels + spi->chip_select;
1045 cs = spi->controller_state;
1046 cd = spi->controller_data;
1048 omap2_mcspi_set_enable(spi, 0);
1049 list_for_each_entry(t, &m->transfers, transfer_list) {
1050 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
1054 if (par_override || t->speed_hz || t->bits_per_word) {
1056 status = omap2_mcspi_setup_transfer(spi, t);
1059 if (!t->speed_hz && !t->bits_per_word)
1062 if (cd && cd->cs_per_word) {
1063 chconf = mcspi->ctx.modulctrl;
1064 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1065 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1066 mcspi->ctx.modulctrl =
1067 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1072 omap2_mcspi_force_cs(spi, 1);
1076 chconf = mcspi_cached_chconf0(spi);
1077 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1078 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1080 if (t->tx_buf == NULL)
1081 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1082 else if (t->rx_buf == NULL)
1083 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1085 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1086 /* Turbo mode is for more than one word */
1087 if (t->len > ((cs->word_len + 7) >> 3))
1088 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1091 mcspi_write_chconf0(spi, chconf);
1096 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1097 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
1098 omap2_mcspi_set_fifo(spi, t, 1);
1100 omap2_mcspi_set_enable(spi, 1);
1102 /* RX_ONLY mode needs dummy data in TX reg */
1103 if (t->tx_buf == NULL)
1104 __raw_writel(0, cs->base
1107 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1108 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
1109 count = omap2_mcspi_txrx_dma(spi, t);
1111 count = omap2_mcspi_txrx_pio(spi, t);
1112 m->actual_length += count;
1114 if (count != t->len) {
1121 udelay(t->delay_usecs);
1123 /* ignore the "leave it on after last xfer" hint */
1125 omap2_mcspi_force_cs(spi, 0);
1129 omap2_mcspi_set_enable(spi, 0);
1131 if (mcspi->fifo_depth > 0)
1132 omap2_mcspi_set_fifo(spi, t, 0);
1134 /* Restore defaults if they were overriden */
1137 status = omap2_mcspi_setup_transfer(spi, NULL);
1141 omap2_mcspi_force_cs(spi, 0);
1143 if (cd && cd->cs_per_word) {
1144 chconf = mcspi->ctx.modulctrl;
1145 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1146 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1147 mcspi->ctx.modulctrl =
1148 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1151 omap2_mcspi_set_enable(spi, 0);
1153 if (mcspi->fifo_depth > 0 && t)
1154 omap2_mcspi_set_fifo(spi, t, 0);
1159 static int omap2_mcspi_transfer_one_message(struct spi_master *master,
1160 struct spi_message *m)
1162 struct spi_device *spi;
1163 struct omap2_mcspi *mcspi;
1164 struct omap2_mcspi_dma *mcspi_dma;
1165 struct spi_transfer *t;
1168 mcspi = spi_master_get_devdata(master);
1169 mcspi_dma = mcspi->dma_channels + spi->chip_select;
1170 m->actual_length = 0;
1173 /* reject invalid messages and transfers */
1174 if (list_empty(&m->transfers))
1176 list_for_each_entry(t, &m->transfers, transfer_list) {
1177 const void *tx_buf = t->tx_buf;
1178 void *rx_buf = t->rx_buf;
1179 unsigned len = t->len;
1181 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
1182 || (len && !(rx_buf || tx_buf))) {
1183 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1191 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1192 dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
1194 OMAP2_MCSPI_MAX_FREQ >> 15);
1198 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1201 if (mcspi_dma->dma_tx && tx_buf != NULL) {
1202 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1203 len, DMA_TO_DEVICE);
1204 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1205 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1210 if (mcspi_dma->dma_rx && rx_buf != NULL) {
1211 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1213 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1214 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1217 dma_unmap_single(mcspi->dev, t->tx_dma,
1218 len, DMA_TO_DEVICE);
1224 omap2_mcspi_work(mcspi, m);
1225 spi_finalize_current_message(master);
1229 static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
1231 struct spi_master *master = mcspi->master;
1232 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1235 ret = pm_runtime_get_sync(mcspi->dev);
1239 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
1240 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
1241 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1243 omap2_mcspi_set_master_mode(master);
1244 pm_runtime_mark_last_busy(mcspi->dev);
1245 pm_runtime_put_autosuspend(mcspi->dev);
1249 static int omap_mcspi_runtime_resume(struct device *dev)
1251 struct omap2_mcspi *mcspi;
1252 struct spi_master *master;
1254 master = dev_get_drvdata(dev);
1255 mcspi = spi_master_get_devdata(master);
1256 omap2_mcspi_restore_ctx(mcspi);
1261 static struct omap2_mcspi_platform_config omap2_pdata = {
1265 static struct omap2_mcspi_platform_config omap4_pdata = {
1266 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1269 static const struct of_device_id omap_mcspi_of_match[] = {
1271 .compatible = "ti,omap2-mcspi",
1272 .data = &omap2_pdata,
1275 .compatible = "ti,omap4-mcspi",
1276 .data = &omap4_pdata,
1280 MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
1282 static int omap2_mcspi_probe(struct platform_device *pdev)
1284 struct spi_master *master;
1285 const struct omap2_mcspi_platform_config *pdata;
1286 struct omap2_mcspi *mcspi;
1289 u32 regs_offset = 0;
1290 static int bus_num = 1;
1291 struct device_node *node = pdev->dev.of_node;
1292 const struct of_device_id *match;
1294 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1295 if (master == NULL) {
1296 dev_dbg(&pdev->dev, "master allocation failed\n");
1300 /* the spi->mode bits understood by this driver: */
1301 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1302 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1303 master->setup = omap2_mcspi_setup;
1304 master->auto_runtime_pm = true;
1305 master->transfer_one_message = omap2_mcspi_transfer_one_message;
1306 master->cleanup = omap2_mcspi_cleanup;
1307 master->dev.of_node = node;
1309 platform_set_drvdata(pdev, master);
1311 mcspi = spi_master_get_devdata(master);
1312 mcspi->master = master;
1314 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1316 u32 num_cs = 1; /* default number of chipselect */
1317 pdata = match->data;
1319 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1320 master->num_chipselect = num_cs;
1321 master->bus_num = bus_num++;
1322 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1323 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
1325 pdata = dev_get_platdata(&pdev->dev);
1326 master->num_chipselect = pdata->num_cs;
1328 master->bus_num = pdev->id;
1329 mcspi->pin_dir = pdata->pin_dir;
1331 regs_offset = pdata->regs_offset;
1333 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1339 r->start += regs_offset;
1340 r->end += regs_offset;
1341 mcspi->phys = r->start;
1343 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1344 if (IS_ERR(mcspi->base)) {
1345 status = PTR_ERR(mcspi->base);
1349 mcspi->dev = &pdev->dev;
1351 INIT_LIST_HEAD(&mcspi->ctx.cs);
1353 mcspi->dma_channels = kcalloc(master->num_chipselect,
1354 sizeof(struct omap2_mcspi_dma),
1357 if (mcspi->dma_channels == NULL)
1360 for (i = 0; i < master->num_chipselect; i++) {
1361 char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
1362 char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
1363 struct resource *dma_res;
1365 sprintf(dma_rx_ch_name, "rx%d", i);
1366 if (!pdev->dev.of_node) {
1368 platform_get_resource_byname(pdev,
1373 "cannot get DMA RX channel\n");
1378 mcspi->dma_channels[i].dma_rx_sync_dev =
1381 sprintf(dma_tx_ch_name, "tx%d", i);
1382 if (!pdev->dev.of_node) {
1384 platform_get_resource_byname(pdev,
1389 "cannot get DMA TX channel\n");
1394 mcspi->dma_channels[i].dma_tx_sync_dev =
1402 pm_runtime_use_autosuspend(&pdev->dev);
1403 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1404 pm_runtime_enable(&pdev->dev);
1406 status = omap2_mcspi_master_setup(mcspi);
1410 status = spi_register_master(master);
1417 pm_runtime_disable(&pdev->dev);
1419 kfree(mcspi->dma_channels);
1421 spi_master_put(master);
1425 static int omap2_mcspi_remove(struct platform_device *pdev)
1427 struct spi_master *master;
1428 struct omap2_mcspi *mcspi;
1429 struct omap2_mcspi_dma *dma_channels;
1431 master = platform_get_drvdata(pdev);
1432 mcspi = spi_master_get_devdata(master);
1433 dma_channels = mcspi->dma_channels;
1435 pm_runtime_put_sync(mcspi->dev);
1436 pm_runtime_disable(&pdev->dev);
1438 spi_unregister_master(master);
1439 kfree(dma_channels);
1444 /* work with hotplug and coldplug */
1445 MODULE_ALIAS("platform:omap2_mcspi");
1447 #ifdef CONFIG_SUSPEND
1449 * When SPI wake up from off-mode, CS is in activate state. If it was in
1450 * unactive state when driver was suspend, then force it to unactive state at
1453 static int omap2_mcspi_resume(struct device *dev)
1455 struct spi_master *master = dev_get_drvdata(dev);
1456 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1457 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1458 struct omap2_mcspi_cs *cs;
1460 pm_runtime_get_sync(mcspi->dev);
1461 list_for_each_entry(cs, &ctx->cs, node) {
1462 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1464 * We need to toggle CS state for OMAP take this
1465 * change in account.
1467 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1468 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1469 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1470 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1473 pm_runtime_mark_last_busy(mcspi->dev);
1474 pm_runtime_put_autosuspend(mcspi->dev);
1478 #define omap2_mcspi_resume NULL
1481 static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1482 .resume = omap2_mcspi_resume,
1483 .runtime_resume = omap_mcspi_runtime_resume,
1486 static struct platform_driver omap2_mcspi_driver = {
1488 .name = "omap2_mcspi",
1489 .owner = THIS_MODULE,
1490 .pm = &omap2_mcspi_pm_ops,
1491 .of_match_table = omap_mcspi_of_match,
1493 .probe = omap2_mcspi_probe,
1494 .remove = omap2_mcspi_remove,
1497 module_platform_driver(omap2_mcspi_driver);
1498 MODULE_LICENSE("GPL");