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/pinctrl/consumer.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 SPI_AUTOSUSPEND_TIMEOUT 2000
50 #define OMAP2_MCSPI_REVISION 0x00
51 #define OMAP2_MCSPI_SYSSTATUS 0x14
52 #define OMAP2_MCSPI_IRQSTATUS 0x18
53 #define OMAP2_MCSPI_IRQENABLE 0x1c
54 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
55 #define OMAP2_MCSPI_SYST 0x24
56 #define OMAP2_MCSPI_MODULCTRL 0x28
58 /* per-channel banks, 0x14 bytes each, first is: */
59 #define OMAP2_MCSPI_CHCONF0 0x2c
60 #define OMAP2_MCSPI_CHSTAT0 0x30
61 #define OMAP2_MCSPI_CHCTRL0 0x34
62 #define OMAP2_MCSPI_TX0 0x38
63 #define OMAP2_MCSPI_RX0 0x3c
65 /* per-register bitmasks: */
67 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
68 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
69 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
71 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
72 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
73 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
74 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
75 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
76 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
77 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
78 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
79 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
80 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
81 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
82 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
83 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
84 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
85 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
87 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
88 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
89 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
91 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
93 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
95 /* We have 2 DMA channels per CS, one for RX and one for TX */
96 struct omap2_mcspi_dma {
97 struct dma_chan *dma_tx;
98 struct dma_chan *dma_rx;
103 struct completion dma_tx_completion;
104 struct completion dma_rx_completion;
107 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
108 * cache operations; better heuristics consider wordsize and bitrate.
110 #define DMA_MIN_BYTES 160
114 * Used for context save and restore, structure members to be updated whenever
115 * corresponding registers are modified.
117 struct omap2_mcspi_regs {
124 struct spi_master *master;
125 /* Virtual base address of the controller */
128 /* SPI1 has 4 channels, while SPI2 has 2 */
129 struct omap2_mcspi_dma *dma_channels;
131 struct omap2_mcspi_regs ctx;
132 unsigned int pin_dir:1;
135 struct omap2_mcspi_cs {
140 struct list_head node;
141 /* Context save and restore shadow register */
145 static inline void mcspi_write_reg(struct spi_master *master,
148 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
150 __raw_writel(val, mcspi->base + idx);
153 static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
155 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
157 return __raw_readl(mcspi->base + idx);
160 static inline void mcspi_write_cs_reg(const struct spi_device *spi,
163 struct omap2_mcspi_cs *cs = spi->controller_state;
165 __raw_writel(val, cs->base + idx);
168 static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
170 struct omap2_mcspi_cs *cs = spi->controller_state;
172 return __raw_readl(cs->base + idx);
175 static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
177 struct omap2_mcspi_cs *cs = spi->controller_state;
182 static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
184 struct omap2_mcspi_cs *cs = spi->controller_state;
187 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
188 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
191 static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
192 int is_read, int enable)
196 l = mcspi_cached_chconf0(spi);
198 if (is_read) /* 1 is read, 0 write */
199 rw = OMAP2_MCSPI_CHCONF_DMAR;
201 rw = OMAP2_MCSPI_CHCONF_DMAW;
208 mcspi_write_chconf0(spi, l);
211 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
215 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
216 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
217 /* Flash post-writes */
218 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
221 static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
225 l = mcspi_cached_chconf0(spi);
227 l |= OMAP2_MCSPI_CHCONF_FORCE;
229 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
231 mcspi_write_chconf0(spi, l);
234 static void omap2_mcspi_set_master_mode(struct spi_master *master)
236 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
237 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
241 * Setup when switching from (reset default) slave mode
242 * to single-channel master mode
244 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
245 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
246 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
247 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
252 static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
254 struct spi_master *spi_cntrl = mcspi->master;
255 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
256 struct omap2_mcspi_cs *cs;
258 /* McSPI: context restore */
259 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
260 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
262 list_for_each_entry(cs, &ctx->cs, node)
263 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
266 static int omap2_prepare_transfer(struct spi_master *master)
268 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
270 pm_runtime_get_sync(mcspi->dev);
274 static int omap2_unprepare_transfer(struct spi_master *master)
276 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
278 pm_runtime_mark_last_busy(mcspi->dev);
279 pm_runtime_put_autosuspend(mcspi->dev);
283 static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
285 unsigned long timeout;
287 timeout = jiffies + msecs_to_jiffies(1000);
288 while (!(__raw_readl(reg) & bit)) {
289 if (time_after(jiffies, timeout)) {
290 if (!(__raw_readl(reg) & bit))
300 static void omap2_mcspi_rx_callback(void *data)
302 struct spi_device *spi = data;
303 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
304 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
306 /* We must disable the DMA RX request */
307 omap2_mcspi_set_dma_req(spi, 1, 0);
309 complete(&mcspi_dma->dma_rx_completion);
312 static void omap2_mcspi_tx_callback(void *data)
314 struct spi_device *spi = data;
315 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
316 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
318 /* We must disable the DMA TX request */
319 omap2_mcspi_set_dma_req(spi, 0, 0);
321 complete(&mcspi_dma->dma_tx_completion);
324 static void omap2_mcspi_tx_dma(struct spi_device *spi,
325 struct spi_transfer *xfer,
326 struct dma_slave_config cfg)
328 struct omap2_mcspi *mcspi;
329 struct omap2_mcspi_dma *mcspi_dma;
332 mcspi = spi_master_get_devdata(spi->master);
333 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
336 if (mcspi_dma->dma_tx) {
337 struct dma_async_tx_descriptor *tx;
338 struct scatterlist sg;
340 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
342 sg_init_table(&sg, 1);
343 sg_dma_address(&sg) = xfer->tx_dma;
344 sg_dma_len(&sg) = xfer->len;
346 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
347 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
349 tx->callback = omap2_mcspi_tx_callback;
350 tx->callback_param = spi;
351 dmaengine_submit(tx);
353 /* FIXME: fall back to PIO? */
356 dma_async_issue_pending(mcspi_dma->dma_tx);
357 omap2_mcspi_set_dma_req(spi, 0, 1);
362 omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
363 struct dma_slave_config cfg,
366 struct omap2_mcspi *mcspi;
367 struct omap2_mcspi_dma *mcspi_dma;
371 int word_len, element_count;
372 struct omap2_mcspi_cs *cs = spi->controller_state;
373 mcspi = spi_master_get_devdata(spi->master);
374 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
376 word_len = cs->word_len;
377 l = mcspi_cached_chconf0(spi);
380 element_count = count;
381 else if (word_len <= 16)
382 element_count = count >> 1;
383 else /* word_len <= 32 */
384 element_count = count >> 2;
386 if (mcspi_dma->dma_rx) {
387 struct dma_async_tx_descriptor *tx;
388 struct scatterlist sg;
389 size_t len = xfer->len - es;
391 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
393 if (l & OMAP2_MCSPI_CHCONF_TURBO)
396 sg_init_table(&sg, 1);
397 sg_dma_address(&sg) = xfer->rx_dma;
398 sg_dma_len(&sg) = len;
400 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
401 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
404 tx->callback = omap2_mcspi_rx_callback;
405 tx->callback_param = spi;
406 dmaengine_submit(tx);
408 /* FIXME: fall back to PIO? */
412 dma_async_issue_pending(mcspi_dma->dma_rx);
413 omap2_mcspi_set_dma_req(spi, 1, 1);
415 wait_for_completion(&mcspi_dma->dma_rx_completion);
416 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
418 omap2_mcspi_set_enable(spi, 0);
420 elements = element_count - 1;
422 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
425 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
426 & OMAP2_MCSPI_CHSTAT_RXS)) {
429 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
431 ((u8 *)xfer->rx_buf)[elements++] = w;
432 else if (word_len <= 16)
433 ((u16 *)xfer->rx_buf)[elements++] = w;
434 else /* word_len <= 32 */
435 ((u32 *)xfer->rx_buf)[elements++] = w;
437 dev_err(&spi->dev, "DMA RX penultimate word empty");
438 count -= (word_len <= 8) ? 2 :
439 (word_len <= 16) ? 4 :
440 /* word_len <= 32 */ 8;
441 omap2_mcspi_set_enable(spi, 1);
445 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
446 & OMAP2_MCSPI_CHSTAT_RXS)) {
449 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
451 ((u8 *)xfer->rx_buf)[elements] = w;
452 else if (word_len <= 16)
453 ((u16 *)xfer->rx_buf)[elements] = w;
454 else /* word_len <= 32 */
455 ((u32 *)xfer->rx_buf)[elements] = w;
457 dev_err(&spi->dev, "DMA RX last word empty");
458 count -= (word_len <= 8) ? 1 :
459 (word_len <= 16) ? 2 :
460 /* word_len <= 32 */ 4;
462 omap2_mcspi_set_enable(spi, 1);
467 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
469 struct omap2_mcspi *mcspi;
470 struct omap2_mcspi_cs *cs = spi->controller_state;
471 struct omap2_mcspi_dma *mcspi_dma;
476 struct dma_slave_config cfg;
477 enum dma_slave_buswidth width;
479 void __iomem *chstat_reg;
481 mcspi = spi_master_get_devdata(spi->master);
482 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
483 l = mcspi_cached_chconf0(spi);
486 if (cs->word_len <= 8) {
487 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
489 } else if (cs->word_len <= 16) {
490 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
493 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
497 memset(&cfg, 0, sizeof(cfg));
498 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
499 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
500 cfg.src_addr_width = width;
501 cfg.dst_addr_width = width;
502 cfg.src_maxburst = 1;
503 cfg.dst_maxburst = 1;
511 omap2_mcspi_tx_dma(spi, xfer, cfg);
514 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
517 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
518 wait_for_completion(&mcspi_dma->dma_tx_completion);
519 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
522 /* for TX_ONLY mode, be sure all words have shifted out */
524 if (mcspi_wait_for_reg_bit(chstat_reg,
525 OMAP2_MCSPI_CHSTAT_TXS) < 0)
526 dev_err(&spi->dev, "TXS timed out\n");
527 else if (mcspi_wait_for_reg_bit(chstat_reg,
528 OMAP2_MCSPI_CHSTAT_EOT) < 0)
529 dev_err(&spi->dev, "EOT timed out\n");
536 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
538 struct omap2_mcspi *mcspi;
539 struct omap2_mcspi_cs *cs = spi->controller_state;
540 unsigned int count, c;
542 void __iomem *base = cs->base;
543 void __iomem *tx_reg;
544 void __iomem *rx_reg;
545 void __iomem *chstat_reg;
548 mcspi = spi_master_get_devdata(spi->master);
551 word_len = cs->word_len;
553 l = mcspi_cached_chconf0(spi);
555 /* We store the pre-calculated register addresses on stack to speed
556 * up the transfer loop. */
557 tx_reg = base + OMAP2_MCSPI_TX0;
558 rx_reg = base + OMAP2_MCSPI_RX0;
559 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
561 if (c < (word_len>>3))
574 if (mcspi_wait_for_reg_bit(chstat_reg,
575 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
576 dev_err(&spi->dev, "TXS timed out\n");
579 dev_vdbg(&spi->dev, "write-%d %02x\n",
581 __raw_writel(*tx++, tx_reg);
584 if (mcspi_wait_for_reg_bit(chstat_reg,
585 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
586 dev_err(&spi->dev, "RXS timed out\n");
590 if (c == 1 && tx == NULL &&
591 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
592 omap2_mcspi_set_enable(spi, 0);
593 *rx++ = __raw_readl(rx_reg);
594 dev_vdbg(&spi->dev, "read-%d %02x\n",
595 word_len, *(rx - 1));
596 if (mcspi_wait_for_reg_bit(chstat_reg,
597 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
603 } else if (c == 0 && tx == NULL) {
604 omap2_mcspi_set_enable(spi, 0);
607 *rx++ = __raw_readl(rx_reg);
608 dev_vdbg(&spi->dev, "read-%d %02x\n",
609 word_len, *(rx - 1));
612 } else if (word_len <= 16) {
621 if (mcspi_wait_for_reg_bit(chstat_reg,
622 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
623 dev_err(&spi->dev, "TXS timed out\n");
626 dev_vdbg(&spi->dev, "write-%d %04x\n",
628 __raw_writel(*tx++, tx_reg);
631 if (mcspi_wait_for_reg_bit(chstat_reg,
632 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
633 dev_err(&spi->dev, "RXS timed out\n");
637 if (c == 2 && tx == NULL &&
638 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
639 omap2_mcspi_set_enable(spi, 0);
640 *rx++ = __raw_readl(rx_reg);
641 dev_vdbg(&spi->dev, "read-%d %04x\n",
642 word_len, *(rx - 1));
643 if (mcspi_wait_for_reg_bit(chstat_reg,
644 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
650 } else if (c == 0 && tx == NULL) {
651 omap2_mcspi_set_enable(spi, 0);
654 *rx++ = __raw_readl(rx_reg);
655 dev_vdbg(&spi->dev, "read-%d %04x\n",
656 word_len, *(rx - 1));
659 } else if (word_len <= 32) {
668 if (mcspi_wait_for_reg_bit(chstat_reg,
669 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
670 dev_err(&spi->dev, "TXS timed out\n");
673 dev_vdbg(&spi->dev, "write-%d %08x\n",
675 __raw_writel(*tx++, tx_reg);
678 if (mcspi_wait_for_reg_bit(chstat_reg,
679 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
680 dev_err(&spi->dev, "RXS timed out\n");
684 if (c == 4 && tx == NULL &&
685 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
686 omap2_mcspi_set_enable(spi, 0);
687 *rx++ = __raw_readl(rx_reg);
688 dev_vdbg(&spi->dev, "read-%d %08x\n",
689 word_len, *(rx - 1));
690 if (mcspi_wait_for_reg_bit(chstat_reg,
691 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
697 } else if (c == 0 && tx == NULL) {
698 omap2_mcspi_set_enable(spi, 0);
701 *rx++ = __raw_readl(rx_reg);
702 dev_vdbg(&spi->dev, "read-%d %08x\n",
703 word_len, *(rx - 1));
708 /* for TX_ONLY mode, be sure all words have shifted out */
709 if (xfer->rx_buf == NULL) {
710 if (mcspi_wait_for_reg_bit(chstat_reg,
711 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
712 dev_err(&spi->dev, "TXS timed out\n");
713 } else if (mcspi_wait_for_reg_bit(chstat_reg,
714 OMAP2_MCSPI_CHSTAT_EOT) < 0)
715 dev_err(&spi->dev, "EOT timed out\n");
717 /* disable chan to purge rx datas received in TX_ONLY transfer,
718 * otherwise these rx datas will affect the direct following
721 omap2_mcspi_set_enable(spi, 0);
724 omap2_mcspi_set_enable(spi, 1);
728 static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
732 for (div = 0; div < 15; div++)
733 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
739 /* called only when no transfer is active to this device */
740 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
741 struct spi_transfer *t)
743 struct omap2_mcspi_cs *cs = spi->controller_state;
744 struct omap2_mcspi *mcspi;
745 struct spi_master *spi_cntrl;
747 u8 word_len = spi->bits_per_word;
748 u32 speed_hz = spi->max_speed_hz;
750 mcspi = spi_master_get_devdata(spi->master);
751 spi_cntrl = mcspi->master;
753 if (t != NULL && t->bits_per_word)
754 word_len = t->bits_per_word;
756 cs->word_len = word_len;
758 if (t && t->speed_hz)
759 speed_hz = t->speed_hz;
761 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
762 div = omap2_mcspi_calc_divisor(speed_hz);
764 l = mcspi_cached_chconf0(spi);
766 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
767 * REVISIT: this controller could support SPI_3WIRE mode.
769 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
770 l &= ~OMAP2_MCSPI_CHCONF_IS;
771 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
772 l |= OMAP2_MCSPI_CHCONF_DPE0;
774 l |= OMAP2_MCSPI_CHCONF_IS;
775 l |= OMAP2_MCSPI_CHCONF_DPE1;
776 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
780 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
781 l |= (word_len - 1) << 7;
783 /* set chipselect polarity; manage with FORCE */
784 if (!(spi->mode & SPI_CS_HIGH))
785 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
787 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
789 /* set clock divisor */
790 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
793 /* set SPI mode 0..3 */
794 if (spi->mode & SPI_CPOL)
795 l |= OMAP2_MCSPI_CHCONF_POL;
797 l &= ~OMAP2_MCSPI_CHCONF_POL;
798 if (spi->mode & SPI_CPHA)
799 l |= OMAP2_MCSPI_CHCONF_PHA;
801 l &= ~OMAP2_MCSPI_CHCONF_PHA;
803 mcspi_write_chconf0(spi, l);
805 cs->mode = spi->mode;
807 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
808 OMAP2_MCSPI_MAX_FREQ >> div,
809 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
810 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
816 * Note that we currently allow DMA only if we get a channel
817 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
819 static int omap2_mcspi_request_dma(struct spi_device *spi)
821 struct spi_master *master = spi->master;
822 struct omap2_mcspi *mcspi;
823 struct omap2_mcspi_dma *mcspi_dma;
827 mcspi = spi_master_get_devdata(master);
828 mcspi_dma = mcspi->dma_channels + spi->chip_select;
830 init_completion(&mcspi_dma->dma_rx_completion);
831 init_completion(&mcspi_dma->dma_tx_completion);
834 dma_cap_set(DMA_SLAVE, mask);
835 sig = mcspi_dma->dma_rx_sync_dev;
836 mcspi_dma->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
837 if (!mcspi_dma->dma_rx)
840 sig = mcspi_dma->dma_tx_sync_dev;
841 mcspi_dma->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
842 if (!mcspi_dma->dma_tx) {
843 dma_release_channel(mcspi_dma->dma_rx);
844 mcspi_dma->dma_rx = NULL;
851 dev_warn(&spi->dev, "not using DMA for McSPI\n");
855 static int omap2_mcspi_setup(struct spi_device *spi)
858 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
859 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
860 struct omap2_mcspi_dma *mcspi_dma;
861 struct omap2_mcspi_cs *cs = spi->controller_state;
863 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
864 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
869 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
872 cs = kzalloc(sizeof *cs, GFP_KERNEL);
875 cs->base = mcspi->base + spi->chip_select * 0x14;
876 cs->phys = mcspi->phys + spi->chip_select * 0x14;
879 spi->controller_state = cs;
880 /* Link this to context save list */
881 list_add_tail(&cs->node, &ctx->cs);
884 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
885 ret = omap2_mcspi_request_dma(spi);
886 if (ret < 0 && ret != -EAGAIN)
890 ret = pm_runtime_get_sync(mcspi->dev);
894 ret = omap2_mcspi_setup_transfer(spi, NULL);
895 pm_runtime_mark_last_busy(mcspi->dev);
896 pm_runtime_put_autosuspend(mcspi->dev);
901 static void omap2_mcspi_cleanup(struct spi_device *spi)
903 struct omap2_mcspi *mcspi;
904 struct omap2_mcspi_dma *mcspi_dma;
905 struct omap2_mcspi_cs *cs;
907 mcspi = spi_master_get_devdata(spi->master);
909 if (spi->controller_state) {
910 /* Unlink controller state from context save list */
911 cs = spi->controller_state;
917 if (spi->chip_select < spi->master->num_chipselect) {
918 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
920 if (mcspi_dma->dma_rx) {
921 dma_release_channel(mcspi_dma->dma_rx);
922 mcspi_dma->dma_rx = NULL;
924 if (mcspi_dma->dma_tx) {
925 dma_release_channel(mcspi_dma->dma_tx);
926 mcspi_dma->dma_tx = NULL;
931 static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
934 /* We only enable one channel at a time -- the one whose message is
935 * -- although this controller would gladly
936 * arbitrate among multiple channels. This corresponds to "single
937 * channel" master mode. As a side effect, we need to manage the
938 * chipselect with the FORCE bit ... CS != channel enable.
941 struct spi_device *spi;
942 struct spi_transfer *t = NULL;
943 struct spi_master *master;
944 struct omap2_mcspi_dma *mcspi_dma;
946 struct omap2_mcspi_cs *cs;
947 struct omap2_mcspi_device_config *cd;
948 int par_override = 0;
953 master = spi->master;
954 mcspi_dma = mcspi->dma_channels + spi->chip_select;
955 cs = spi->controller_state;
956 cd = spi->controller_data;
958 omap2_mcspi_set_enable(spi, 1);
959 list_for_each_entry(t, &m->transfers, transfer_list) {
960 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
964 if (par_override || t->speed_hz || t->bits_per_word) {
966 status = omap2_mcspi_setup_transfer(spi, t);
969 if (!t->speed_hz && !t->bits_per_word)
972 if (cd && cd->cs_per_word) {
973 chconf = mcspi->ctx.modulctrl;
974 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
975 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
976 mcspi->ctx.modulctrl =
977 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
982 omap2_mcspi_force_cs(spi, 1);
986 chconf = mcspi_cached_chconf0(spi);
987 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
988 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
990 if (t->tx_buf == NULL)
991 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
992 else if (t->rx_buf == NULL)
993 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
995 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
996 /* Turbo mode is for more than one word */
997 if (t->len > ((cs->word_len + 7) >> 3))
998 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1001 mcspi_write_chconf0(spi, chconf);
1006 /* RX_ONLY mode needs dummy data in TX reg */
1007 if (t->tx_buf == NULL)
1008 __raw_writel(0, cs->base
1011 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1012 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
1013 count = omap2_mcspi_txrx_dma(spi, t);
1015 count = omap2_mcspi_txrx_pio(spi, t);
1016 m->actual_length += count;
1018 if (count != t->len) {
1025 udelay(t->delay_usecs);
1027 /* ignore the "leave it on after last xfer" hint */
1029 omap2_mcspi_force_cs(spi, 0);
1033 /* Restore defaults if they were overriden */
1036 status = omap2_mcspi_setup_transfer(spi, NULL);
1040 omap2_mcspi_force_cs(spi, 0);
1042 if (cd && cd->cs_per_word) {
1043 chconf = mcspi->ctx.modulctrl;
1044 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1045 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1046 mcspi->ctx.modulctrl =
1047 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1051 * The slave driver could have changed spi->mode in which case
1052 * it will be different from cs->mode (the current hardware setup).
1053 * If so, set par_override (even though its not a parity issue) so
1054 * omap2_mcspi_setup_transfer will be called to configure the hardware
1055 * with the correct mode on the first iteration of the loop below.
1057 if (spi->mode != cs->mode)
1060 omap2_mcspi_set_enable(spi, 0);
1066 static int omap2_mcspi_transfer_one_message(struct spi_master *master,
1067 struct spi_message *m)
1069 struct spi_device *spi;
1070 struct omap2_mcspi *mcspi;
1071 struct omap2_mcspi_dma *mcspi_dma;
1072 struct spi_transfer *t;
1075 mcspi = spi_master_get_devdata(master);
1076 mcspi_dma = mcspi->dma_channels + spi->chip_select;
1077 m->actual_length = 0;
1080 /* reject invalid messages and transfers */
1081 if (list_empty(&m->transfers))
1083 list_for_each_entry(t, &m->transfers, transfer_list) {
1084 const void *tx_buf = t->tx_buf;
1085 void *rx_buf = t->rx_buf;
1086 unsigned len = t->len;
1088 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
1089 || (len && !(rx_buf || tx_buf))
1090 || (t->bits_per_word &&
1091 ( t->bits_per_word < 4
1092 || t->bits_per_word > 32))) {
1093 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1101 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1102 dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
1104 OMAP2_MCSPI_MAX_FREQ >> 15);
1108 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1111 if (mcspi_dma->dma_tx && tx_buf != NULL) {
1112 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1113 len, DMA_TO_DEVICE);
1114 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1115 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1120 if (mcspi_dma->dma_rx && rx_buf != NULL) {
1121 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1123 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1124 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1127 dma_unmap_single(mcspi->dev, t->tx_dma,
1128 len, DMA_TO_DEVICE);
1134 omap2_mcspi_work(mcspi, m);
1135 spi_finalize_current_message(master);
1139 static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
1141 struct spi_master *master = mcspi->master;
1142 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1145 ret = pm_runtime_get_sync(mcspi->dev);
1149 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
1150 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
1151 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1153 omap2_mcspi_set_master_mode(master);
1154 pm_runtime_mark_last_busy(mcspi->dev);
1155 pm_runtime_put_autosuspend(mcspi->dev);
1159 static int omap_mcspi_runtime_resume(struct device *dev)
1161 struct omap2_mcspi *mcspi;
1162 struct spi_master *master;
1164 master = dev_get_drvdata(dev);
1165 mcspi = spi_master_get_devdata(master);
1166 omap2_mcspi_restore_ctx(mcspi);
1171 static struct omap2_mcspi_platform_config omap2_pdata = {
1175 static struct omap2_mcspi_platform_config omap4_pdata = {
1176 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1179 static const struct of_device_id omap_mcspi_of_match[] = {
1181 .compatible = "ti,omap2-mcspi",
1182 .data = &omap2_pdata,
1185 .compatible = "ti,omap4-mcspi",
1186 .data = &omap4_pdata,
1190 MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
1192 static int omap2_mcspi_probe(struct platform_device *pdev)
1194 struct spi_master *master;
1195 const struct omap2_mcspi_platform_config *pdata;
1196 struct omap2_mcspi *mcspi;
1199 u32 regs_offset = 0;
1200 static int bus_num = 1;
1201 struct device_node *node = pdev->dev.of_node;
1202 const struct of_device_id *match;
1203 struct pinctrl *pinctrl;
1205 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1206 if (master == NULL) {
1207 dev_dbg(&pdev->dev, "master allocation failed\n");
1211 /* the spi->mode bits understood by this driver: */
1212 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1214 master->setup = omap2_mcspi_setup;
1215 master->prepare_transfer_hardware = omap2_prepare_transfer;
1216 master->unprepare_transfer_hardware = omap2_unprepare_transfer;
1217 master->transfer_one_message = omap2_mcspi_transfer_one_message;
1218 master->cleanup = omap2_mcspi_cleanup;
1219 master->dev.of_node = node;
1221 dev_set_drvdata(&pdev->dev, master);
1223 mcspi = spi_master_get_devdata(master);
1224 mcspi->master = master;
1226 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1228 u32 num_cs = 1; /* default number of chipselect */
1229 pdata = match->data;
1231 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1232 master->num_chipselect = num_cs;
1233 master->bus_num = bus_num++;
1234 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1235 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
1237 pdata = pdev->dev.platform_data;
1238 master->num_chipselect = pdata->num_cs;
1240 master->bus_num = pdev->id;
1241 mcspi->pin_dir = pdata->pin_dir;
1243 regs_offset = pdata->regs_offset;
1245 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1251 r->start += regs_offset;
1252 r->end += regs_offset;
1253 mcspi->phys = r->start;
1255 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1256 if (IS_ERR(mcspi->base)) {
1257 status = PTR_ERR(mcspi->base);
1261 mcspi->dev = &pdev->dev;
1263 INIT_LIST_HEAD(&mcspi->ctx.cs);
1265 mcspi->dma_channels = kcalloc(master->num_chipselect,
1266 sizeof(struct omap2_mcspi_dma),
1269 if (mcspi->dma_channels == NULL)
1272 for (i = 0; i < master->num_chipselect; i++) {
1273 char dma_ch_name[14];
1274 struct resource *dma_res;
1276 sprintf(dma_ch_name, "rx%d", i);
1277 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1280 dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1285 mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1286 sprintf(dma_ch_name, "tx%d", i);
1287 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1290 dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1295 mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
1301 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1302 if (IS_ERR(pinctrl))
1303 dev_warn(&pdev->dev,
1304 "pins are not configured from the driver\n");
1306 pm_runtime_use_autosuspend(&pdev->dev);
1307 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1308 pm_runtime_enable(&pdev->dev);
1310 status = omap2_mcspi_master_setup(mcspi);
1314 status = spi_register_master(master);
1321 pm_runtime_disable(&pdev->dev);
1323 kfree(mcspi->dma_channels);
1325 spi_master_put(master);
1329 static int omap2_mcspi_remove(struct platform_device *pdev)
1331 struct spi_master *master;
1332 struct omap2_mcspi *mcspi;
1333 struct omap2_mcspi_dma *dma_channels;
1335 master = dev_get_drvdata(&pdev->dev);
1336 mcspi = spi_master_get_devdata(master);
1337 dma_channels = mcspi->dma_channels;
1339 pm_runtime_put_sync(mcspi->dev);
1340 pm_runtime_disable(&pdev->dev);
1342 spi_unregister_master(master);
1343 kfree(dma_channels);
1348 /* work with hotplug and coldplug */
1349 MODULE_ALIAS("platform:omap2_mcspi");
1351 #ifdef CONFIG_SUSPEND
1353 * When SPI wake up from off-mode, CS is in activate state. If it was in
1354 * unactive state when driver was suspend, then force it to unactive state at
1357 static int omap2_mcspi_resume(struct device *dev)
1359 struct spi_master *master = dev_get_drvdata(dev);
1360 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1361 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1362 struct omap2_mcspi_cs *cs;
1364 pm_runtime_get_sync(mcspi->dev);
1365 list_for_each_entry(cs, &ctx->cs, node) {
1366 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1368 * We need to toggle CS state for OMAP take this
1369 * change in account.
1371 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1372 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1373 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1374 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1377 pm_runtime_mark_last_busy(mcspi->dev);
1378 pm_runtime_put_autosuspend(mcspi->dev);
1382 #define omap2_mcspi_resume NULL
1385 static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1386 .resume = omap2_mcspi_resume,
1387 .runtime_resume = omap_mcspi_runtime_resume,
1390 static struct platform_driver omap2_mcspi_driver = {
1392 .name = "omap2_mcspi",
1393 .owner = THIS_MODULE,
1394 .pm = &omap2_mcspi_pm_ops,
1395 .of_match_table = omap_mcspi_of_match,
1397 .probe = omap2_mcspi_probe,
1398 .remove = omap2_mcspi_remove,
1401 module_platform_driver(omap2_mcspi_driver);
1402 MODULE_LICENSE("GPL");