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>
42 #include <linux/err.h>
44 #include <linux/spi/spi.h>
46 #include <linux/platform_data/spi-omap2-mcspi.h>
48 #define OMAP2_MCSPI_MAX_FREQ 48000000
49 #define SPI_AUTOSUSPEND_TIMEOUT 2000
51 #define OMAP2_MCSPI_REVISION 0x00
52 #define OMAP2_MCSPI_SYSSTATUS 0x14
53 #define OMAP2_MCSPI_IRQSTATUS 0x18
54 #define OMAP2_MCSPI_IRQENABLE 0x1c
55 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
56 #define OMAP2_MCSPI_SYST 0x24
57 #define OMAP2_MCSPI_MODULCTRL 0x28
59 /* per-channel banks, 0x14 bytes each, first is: */
60 #define OMAP2_MCSPI_CHCONF0 0x2c
61 #define OMAP2_MCSPI_CHSTAT0 0x30
62 #define OMAP2_MCSPI_CHCTRL0 0x34
63 #define OMAP2_MCSPI_TX0 0x38
64 #define OMAP2_MCSPI_RX0 0x3c
66 /* per-register bitmasks: */
68 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
69 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
70 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
72 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
73 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
74 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
75 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
76 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
77 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
78 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
79 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
80 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
81 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
82 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
83 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
84 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
85 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
86 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
88 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
89 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
90 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
92 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
94 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
96 /* We have 2 DMA channels per CS, one for RX and one for TX */
97 struct omap2_mcspi_dma {
98 struct dma_chan *dma_tx;
99 struct dma_chan *dma_rx;
104 struct completion dma_tx_completion;
105 struct completion dma_rx_completion;
108 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
109 * cache operations; better heuristics consider wordsize and bitrate.
111 #define DMA_MIN_BYTES 160
115 * Used for context save and restore, structure members to be updated whenever
116 * corresponding registers are modified.
118 struct omap2_mcspi_regs {
125 struct spi_master *master;
126 /* Virtual base address of the controller */
129 /* SPI1 has 4 channels, while SPI2 has 2 */
130 struct omap2_mcspi_dma *dma_channels;
132 struct omap2_mcspi_regs ctx;
133 unsigned int pin_dir:1;
136 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))
296 static void omap2_mcspi_rx_callback(void *data)
298 struct spi_device *spi = data;
299 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
300 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
302 complete(&mcspi_dma->dma_rx_completion);
304 /* We must disable the DMA RX request */
305 omap2_mcspi_set_dma_req(spi, 1, 0);
308 static void omap2_mcspi_tx_callback(void *data)
310 struct spi_device *spi = data;
311 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
312 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
314 complete(&mcspi_dma->dma_tx_completion);
316 /* We must disable the DMA TX request */
317 omap2_mcspi_set_dma_req(spi, 0, 0);
320 static void omap2_mcspi_tx_dma(struct spi_device *spi,
321 struct spi_transfer *xfer,
322 struct dma_slave_config cfg)
324 struct omap2_mcspi *mcspi;
325 struct omap2_mcspi_dma *mcspi_dma;
329 mcspi = spi_master_get_devdata(spi->master);
330 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
335 if (mcspi_dma->dma_tx) {
336 struct dma_async_tx_descriptor *tx;
337 struct scatterlist sg;
339 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
341 sg_init_table(&sg, 1);
342 sg_dma_address(&sg) = xfer->tx_dma;
343 sg_dma_len(&sg) = xfer->len;
345 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
346 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
348 tx->callback = omap2_mcspi_tx_callback;
349 tx->callback_param = spi;
350 dmaengine_submit(tx);
352 /* FIXME: fall back to PIO? */
355 dma_async_issue_pending(mcspi_dma->dma_tx);
356 omap2_mcspi_set_dma_req(spi, 0, 1);
361 omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
362 struct dma_slave_config cfg,
365 struct omap2_mcspi *mcspi;
366 struct omap2_mcspi_dma *mcspi_dma;
370 int word_len, element_count;
371 struct omap2_mcspi_cs *cs = spi->controller_state;
372 mcspi = spi_master_get_devdata(spi->master);
373 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
375 word_len = cs->word_len;
376 l = mcspi_cached_chconf0(spi);
379 element_count = count;
380 else if (word_len <= 16)
381 element_count = count >> 1;
382 else /* word_len <= 32 */
383 element_count = count >> 2;
385 if (mcspi_dma->dma_rx) {
386 struct dma_async_tx_descriptor *tx;
387 struct scatterlist sg;
388 size_t len = xfer->len - es;
390 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
392 if (l & OMAP2_MCSPI_CHCONF_TURBO)
395 sg_init_table(&sg, 1);
396 sg_dma_address(&sg) = xfer->rx_dma;
397 sg_dma_len(&sg) = len;
399 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
400 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
403 tx->callback = omap2_mcspi_rx_callback;
404 tx->callback_param = spi;
405 dmaengine_submit(tx);
407 /* FIXME: fall back to PIO? */
411 dma_async_issue_pending(mcspi_dma->dma_rx);
412 omap2_mcspi_set_dma_req(spi, 1, 1);
414 wait_for_completion(&mcspi_dma->dma_rx_completion);
415 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
417 omap2_mcspi_set_enable(spi, 0);
419 elements = element_count - 1;
421 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
424 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
425 & OMAP2_MCSPI_CHSTAT_RXS)) {
428 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
430 ((u8 *)xfer->rx_buf)[elements++] = w;
431 else if (word_len <= 16)
432 ((u16 *)xfer->rx_buf)[elements++] = w;
433 else /* word_len <= 32 */
434 ((u32 *)xfer->rx_buf)[elements++] = w;
436 dev_err(&spi->dev, "DMA RX penultimate word empty");
437 count -= (word_len <= 8) ? 2 :
438 (word_len <= 16) ? 4 :
439 /* word_len <= 32 */ 8;
440 omap2_mcspi_set_enable(spi, 1);
444 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
445 & OMAP2_MCSPI_CHSTAT_RXS)) {
448 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
450 ((u8 *)xfer->rx_buf)[elements] = w;
451 else if (word_len <= 16)
452 ((u16 *)xfer->rx_buf)[elements] = w;
453 else /* word_len <= 32 */
454 ((u32 *)xfer->rx_buf)[elements] = w;
456 dev_err(&spi->dev, "DMA RX last word empty");
457 count -= (word_len <= 8) ? 1 :
458 (word_len <= 16) ? 2 :
459 /* word_len <= 32 */ 4;
461 omap2_mcspi_set_enable(spi, 1);
466 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
468 struct omap2_mcspi *mcspi;
469 struct omap2_mcspi_cs *cs = spi->controller_state;
470 struct omap2_mcspi_dma *mcspi_dma;
475 struct dma_slave_config cfg;
476 enum dma_slave_buswidth width;
478 void __iomem *chstat_reg;
480 mcspi = spi_master_get_devdata(spi->master);
481 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
482 l = mcspi_cached_chconf0(spi);
485 if (cs->word_len <= 8) {
486 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
488 } else if (cs->word_len <= 16) {
489 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
492 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
496 memset(&cfg, 0, sizeof(cfg));
497 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
498 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
499 cfg.src_addr_width = width;
500 cfg.dst_addr_width = width;
501 cfg.src_maxburst = 1;
502 cfg.dst_maxburst = 1;
510 omap2_mcspi_tx_dma(spi, xfer, cfg);
513 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
516 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
517 wait_for_completion(&mcspi_dma->dma_tx_completion);
518 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
521 /* for TX_ONLY mode, be sure all words have shifted out */
523 if (mcspi_wait_for_reg_bit(chstat_reg,
524 OMAP2_MCSPI_CHSTAT_TXS) < 0)
525 dev_err(&spi->dev, "TXS timed out\n");
526 else if (mcspi_wait_for_reg_bit(chstat_reg,
527 OMAP2_MCSPI_CHSTAT_EOT) < 0)
528 dev_err(&spi->dev, "EOT timed out\n");
535 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
537 struct omap2_mcspi *mcspi;
538 struct omap2_mcspi_cs *cs = spi->controller_state;
539 unsigned int count, c;
541 void __iomem *base = cs->base;
542 void __iomem *tx_reg;
543 void __iomem *rx_reg;
544 void __iomem *chstat_reg;
547 mcspi = spi_master_get_devdata(spi->master);
550 word_len = cs->word_len;
552 l = mcspi_cached_chconf0(spi);
554 /* We store the pre-calculated register addresses on stack to speed
555 * up the transfer loop. */
556 tx_reg = base + OMAP2_MCSPI_TX0;
557 rx_reg = base + OMAP2_MCSPI_RX0;
558 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
560 if (c < (word_len>>3))
573 if (mcspi_wait_for_reg_bit(chstat_reg,
574 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
575 dev_err(&spi->dev, "TXS timed out\n");
578 dev_vdbg(&spi->dev, "write-%d %02x\n",
580 __raw_writel(*tx++, tx_reg);
583 if (mcspi_wait_for_reg_bit(chstat_reg,
584 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
585 dev_err(&spi->dev, "RXS timed out\n");
589 if (c == 1 && tx == NULL &&
590 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
591 omap2_mcspi_set_enable(spi, 0);
592 *rx++ = __raw_readl(rx_reg);
593 dev_vdbg(&spi->dev, "read-%d %02x\n",
594 word_len, *(rx - 1));
595 if (mcspi_wait_for_reg_bit(chstat_reg,
596 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
602 } else if (c == 0 && tx == NULL) {
603 omap2_mcspi_set_enable(spi, 0);
606 *rx++ = __raw_readl(rx_reg);
607 dev_vdbg(&spi->dev, "read-%d %02x\n",
608 word_len, *(rx - 1));
611 } else if (word_len <= 16) {
620 if (mcspi_wait_for_reg_bit(chstat_reg,
621 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
622 dev_err(&spi->dev, "TXS timed out\n");
625 dev_vdbg(&spi->dev, "write-%d %04x\n",
627 __raw_writel(*tx++, tx_reg);
630 if (mcspi_wait_for_reg_bit(chstat_reg,
631 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
632 dev_err(&spi->dev, "RXS timed out\n");
636 if (c == 2 && tx == NULL &&
637 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
638 omap2_mcspi_set_enable(spi, 0);
639 *rx++ = __raw_readl(rx_reg);
640 dev_vdbg(&spi->dev, "read-%d %04x\n",
641 word_len, *(rx - 1));
642 if (mcspi_wait_for_reg_bit(chstat_reg,
643 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
649 } else if (c == 0 && tx == NULL) {
650 omap2_mcspi_set_enable(spi, 0);
653 *rx++ = __raw_readl(rx_reg);
654 dev_vdbg(&spi->dev, "read-%d %04x\n",
655 word_len, *(rx - 1));
658 } else if (word_len <= 32) {
667 if (mcspi_wait_for_reg_bit(chstat_reg,
668 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
669 dev_err(&spi->dev, "TXS timed out\n");
672 dev_vdbg(&spi->dev, "write-%d %08x\n",
674 __raw_writel(*tx++, tx_reg);
677 if (mcspi_wait_for_reg_bit(chstat_reg,
678 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
679 dev_err(&spi->dev, "RXS timed out\n");
683 if (c == 4 && tx == NULL &&
684 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
685 omap2_mcspi_set_enable(spi, 0);
686 *rx++ = __raw_readl(rx_reg);
687 dev_vdbg(&spi->dev, "read-%d %08x\n",
688 word_len, *(rx - 1));
689 if (mcspi_wait_for_reg_bit(chstat_reg,
690 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
696 } else if (c == 0 && tx == NULL) {
697 omap2_mcspi_set_enable(spi, 0);
700 *rx++ = __raw_readl(rx_reg);
701 dev_vdbg(&spi->dev, "read-%d %08x\n",
702 word_len, *(rx - 1));
707 /* for TX_ONLY mode, be sure all words have shifted out */
708 if (xfer->rx_buf == NULL) {
709 if (mcspi_wait_for_reg_bit(chstat_reg,
710 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
711 dev_err(&spi->dev, "TXS timed out\n");
712 } else if (mcspi_wait_for_reg_bit(chstat_reg,
713 OMAP2_MCSPI_CHSTAT_EOT) < 0)
714 dev_err(&spi->dev, "EOT timed out\n");
716 /* disable chan to purge rx datas received in TX_ONLY transfer,
717 * otherwise these rx datas will affect the direct following
720 omap2_mcspi_set_enable(spi, 0);
723 omap2_mcspi_set_enable(spi, 1);
727 static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
731 for (div = 0; div < 15; div++)
732 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
738 /* called only when no transfer is active to this device */
739 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
740 struct spi_transfer *t)
742 struct omap2_mcspi_cs *cs = spi->controller_state;
743 struct omap2_mcspi *mcspi;
744 struct spi_master *spi_cntrl;
746 u8 word_len = spi->bits_per_word;
747 u32 speed_hz = spi->max_speed_hz;
749 mcspi = spi_master_get_devdata(spi->master);
750 spi_cntrl = mcspi->master;
752 if (t != NULL && t->bits_per_word)
753 word_len = t->bits_per_word;
755 cs->word_len = word_len;
757 if (t && t->speed_hz)
758 speed_hz = t->speed_hz;
760 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
761 div = omap2_mcspi_calc_divisor(speed_hz);
763 l = mcspi_cached_chconf0(spi);
765 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
766 * REVISIT: this controller could support SPI_3WIRE mode.
768 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
769 l &= ~OMAP2_MCSPI_CHCONF_IS;
770 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
771 l |= OMAP2_MCSPI_CHCONF_DPE0;
773 l |= OMAP2_MCSPI_CHCONF_IS;
774 l |= OMAP2_MCSPI_CHCONF_DPE1;
775 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
779 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
780 l |= (word_len - 1) << 7;
782 /* set chipselect polarity; manage with FORCE */
783 if (!(spi->mode & SPI_CS_HIGH))
784 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
786 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
788 /* set clock divisor */
789 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
792 /* set SPI mode 0..3 */
793 if (spi->mode & SPI_CPOL)
794 l |= OMAP2_MCSPI_CHCONF_POL;
796 l &= ~OMAP2_MCSPI_CHCONF_POL;
797 if (spi->mode & SPI_CPHA)
798 l |= OMAP2_MCSPI_CHCONF_PHA;
800 l &= ~OMAP2_MCSPI_CHCONF_PHA;
802 mcspi_write_chconf0(spi, l);
804 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
805 OMAP2_MCSPI_MAX_FREQ >> div,
806 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
807 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
812 static int omap2_mcspi_request_dma(struct spi_device *spi)
814 struct spi_master *master = spi->master;
815 struct omap2_mcspi *mcspi;
816 struct omap2_mcspi_dma *mcspi_dma;
820 mcspi = spi_master_get_devdata(master);
821 mcspi_dma = mcspi->dma_channels + spi->chip_select;
823 init_completion(&mcspi_dma->dma_rx_completion);
824 init_completion(&mcspi_dma->dma_tx_completion);
827 dma_cap_set(DMA_SLAVE, mask);
828 sig = mcspi_dma->dma_rx_sync_dev;
829 mcspi_dma->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
830 if (!mcspi_dma->dma_rx) {
831 dev_err(&spi->dev, "no RX DMA engine channel for McSPI\n");
835 sig = mcspi_dma->dma_tx_sync_dev;
836 mcspi_dma->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
837 if (!mcspi_dma->dma_tx) {
838 dev_err(&spi->dev, "no TX DMA engine channel for McSPI\n");
839 dma_release_channel(mcspi_dma->dma_rx);
840 mcspi_dma->dma_rx = NULL;
847 static int omap2_mcspi_setup(struct spi_device *spi)
850 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
851 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
852 struct omap2_mcspi_dma *mcspi_dma;
853 struct omap2_mcspi_cs *cs = spi->controller_state;
855 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
856 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
861 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
864 cs = kzalloc(sizeof *cs, GFP_KERNEL);
867 cs->base = mcspi->base + spi->chip_select * 0x14;
868 cs->phys = mcspi->phys + spi->chip_select * 0x14;
870 spi->controller_state = cs;
871 /* Link this to context save list */
872 list_add_tail(&cs->node, &ctx->cs);
875 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
876 ret = omap2_mcspi_request_dma(spi);
881 ret = pm_runtime_get_sync(mcspi->dev);
885 ret = omap2_mcspi_setup_transfer(spi, NULL);
886 pm_runtime_mark_last_busy(mcspi->dev);
887 pm_runtime_put_autosuspend(mcspi->dev);
892 static void omap2_mcspi_cleanup(struct spi_device *spi)
894 struct omap2_mcspi *mcspi;
895 struct omap2_mcspi_dma *mcspi_dma;
896 struct omap2_mcspi_cs *cs;
898 mcspi = spi_master_get_devdata(spi->master);
900 if (spi->controller_state) {
901 /* Unlink controller state from context save list */
902 cs = spi->controller_state;
908 if (spi->chip_select < spi->master->num_chipselect) {
909 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
911 if (mcspi_dma->dma_rx) {
912 dma_release_channel(mcspi_dma->dma_rx);
913 mcspi_dma->dma_rx = NULL;
915 if (mcspi_dma->dma_tx) {
916 dma_release_channel(mcspi_dma->dma_tx);
917 mcspi_dma->dma_tx = NULL;
922 static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
925 /* We only enable one channel at a time -- the one whose message is
926 * -- although this controller would gladly
927 * arbitrate among multiple channels. This corresponds to "single
928 * channel" master mode. As a side effect, we need to manage the
929 * chipselect with the FORCE bit ... CS != channel enable.
932 struct spi_device *spi;
933 struct spi_transfer *t = NULL;
935 struct omap2_mcspi_cs *cs;
936 struct omap2_mcspi_device_config *cd;
937 int par_override = 0;
942 cs = spi->controller_state;
943 cd = spi->controller_data;
945 omap2_mcspi_set_enable(spi, 1);
946 list_for_each_entry(t, &m->transfers, transfer_list) {
947 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
951 if (par_override || t->speed_hz || t->bits_per_word) {
953 status = omap2_mcspi_setup_transfer(spi, t);
956 if (!t->speed_hz && !t->bits_per_word)
961 omap2_mcspi_force_cs(spi, 1);
965 chconf = mcspi_cached_chconf0(spi);
966 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
967 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
969 if (t->tx_buf == NULL)
970 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
971 else if (t->rx_buf == NULL)
972 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
974 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
975 /* Turbo mode is for more than one word */
976 if (t->len > ((cs->word_len + 7) >> 3))
977 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
980 mcspi_write_chconf0(spi, chconf);
985 /* RX_ONLY mode needs dummy data in TX reg */
986 if (t->tx_buf == NULL)
987 __raw_writel(0, cs->base
990 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
991 count = omap2_mcspi_txrx_dma(spi, t);
993 count = omap2_mcspi_txrx_pio(spi, t);
994 m->actual_length += count;
996 if (count != t->len) {
1003 udelay(t->delay_usecs);
1005 /* ignore the "leave it on after last xfer" hint */
1007 omap2_mcspi_force_cs(spi, 0);
1011 /* Restore defaults if they were overriden */
1014 status = omap2_mcspi_setup_transfer(spi, NULL);
1018 omap2_mcspi_force_cs(spi, 0);
1020 omap2_mcspi_set_enable(spi, 0);
1026 static int omap2_mcspi_transfer_one_message(struct spi_master *master,
1027 struct spi_message *m)
1029 struct omap2_mcspi *mcspi;
1030 struct spi_transfer *t;
1032 mcspi = spi_master_get_devdata(master);
1033 m->actual_length = 0;
1036 /* reject invalid messages and transfers */
1037 if (list_empty(&m->transfers))
1039 list_for_each_entry(t, &m->transfers, transfer_list) {
1040 const void *tx_buf = t->tx_buf;
1041 void *rx_buf = t->rx_buf;
1042 unsigned len = t->len;
1044 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
1045 || (len && !(rx_buf || tx_buf))
1046 || (t->bits_per_word &&
1047 ( t->bits_per_word < 4
1048 || t->bits_per_word > 32))) {
1049 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1057 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1058 dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
1060 OMAP2_MCSPI_MAX_FREQ >> 15);
1064 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1067 if (tx_buf != NULL) {
1068 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1069 len, DMA_TO_DEVICE);
1070 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1071 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1076 if (rx_buf != NULL) {
1077 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1079 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1080 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1083 dma_unmap_single(mcspi->dev, t->tx_dma,
1084 len, DMA_TO_DEVICE);
1090 omap2_mcspi_work(mcspi, m);
1091 spi_finalize_current_message(master);
1095 static int __devinit omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
1097 struct spi_master *master = mcspi->master;
1098 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1101 ret = pm_runtime_get_sync(mcspi->dev);
1105 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
1106 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
1107 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1109 omap2_mcspi_set_master_mode(master);
1110 pm_runtime_mark_last_busy(mcspi->dev);
1111 pm_runtime_put_autosuspend(mcspi->dev);
1115 static int omap_mcspi_runtime_resume(struct device *dev)
1117 struct omap2_mcspi *mcspi;
1118 struct spi_master *master;
1120 master = dev_get_drvdata(dev);
1121 mcspi = spi_master_get_devdata(master);
1122 omap2_mcspi_restore_ctx(mcspi);
1127 static struct omap2_mcspi_platform_config omap2_pdata = {
1131 static struct omap2_mcspi_platform_config omap4_pdata = {
1132 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1135 static const struct of_device_id omap_mcspi_of_match[] = {
1137 .compatible = "ti,omap2-mcspi",
1138 .data = &omap2_pdata,
1141 .compatible = "ti,omap4-mcspi",
1142 .data = &omap4_pdata,
1146 MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
1148 static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
1150 struct spi_master *master;
1151 const struct omap2_mcspi_platform_config *pdata;
1152 struct omap2_mcspi *mcspi;
1155 u32 regs_offset = 0;
1156 static int bus_num = 1;
1157 struct device_node *node = pdev->dev.of_node;
1158 const struct of_device_id *match;
1159 struct pinctrl *pinctrl;
1161 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1162 if (master == NULL) {
1163 dev_dbg(&pdev->dev, "master allocation failed\n");
1167 /* the spi->mode bits understood by this driver: */
1168 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1170 master->setup = omap2_mcspi_setup;
1171 master->prepare_transfer_hardware = omap2_prepare_transfer;
1172 master->unprepare_transfer_hardware = omap2_unprepare_transfer;
1173 master->transfer_one_message = omap2_mcspi_transfer_one_message;
1174 master->cleanup = omap2_mcspi_cleanup;
1175 master->dev.of_node = node;
1177 dev_set_drvdata(&pdev->dev, master);
1179 mcspi = spi_master_get_devdata(master);
1180 mcspi->master = master;
1182 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1184 u32 num_cs = 1; /* default number of chipselect */
1185 pdata = match->data;
1187 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1188 master->num_chipselect = num_cs;
1189 master->bus_num = bus_num++;
1190 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1191 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
1193 pdata = pdev->dev.platform_data;
1194 master->num_chipselect = pdata->num_cs;
1196 master->bus_num = pdev->id;
1197 mcspi->pin_dir = pdata->pin_dir;
1199 regs_offset = pdata->regs_offset;
1201 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1207 r->start += regs_offset;
1208 r->end += regs_offset;
1209 mcspi->phys = r->start;
1211 mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
1213 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1218 mcspi->dev = &pdev->dev;
1220 INIT_LIST_HEAD(&mcspi->ctx.cs);
1222 mcspi->dma_channels = kcalloc(master->num_chipselect,
1223 sizeof(struct omap2_mcspi_dma),
1226 if (mcspi->dma_channels == NULL)
1229 for (i = 0; i < master->num_chipselect; i++) {
1230 char dma_ch_name[14];
1231 struct resource *dma_res;
1233 sprintf(dma_ch_name, "rx%d", i);
1234 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1237 dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1242 mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1243 sprintf(dma_ch_name, "tx%d", i);
1244 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1247 dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1252 mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
1258 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1259 if (IS_ERR(pinctrl))
1260 dev_warn(&pdev->dev,
1261 "pins are not configured from the driver\n");
1263 pm_runtime_use_autosuspend(&pdev->dev);
1264 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1265 pm_runtime_enable(&pdev->dev);
1267 if (status || omap2_mcspi_master_setup(mcspi) < 0)
1270 status = spi_register_master(master);
1277 pm_runtime_disable(&pdev->dev);
1279 kfree(mcspi->dma_channels);
1281 spi_master_put(master);
1285 static int __devexit omap2_mcspi_remove(struct platform_device *pdev)
1287 struct spi_master *master;
1288 struct omap2_mcspi *mcspi;
1289 struct omap2_mcspi_dma *dma_channels;
1291 master = dev_get_drvdata(&pdev->dev);
1292 mcspi = spi_master_get_devdata(master);
1293 dma_channels = mcspi->dma_channels;
1295 pm_runtime_put_sync(mcspi->dev);
1296 pm_runtime_disable(&pdev->dev);
1298 spi_unregister_master(master);
1299 kfree(dma_channels);
1304 /* work with hotplug and coldplug */
1305 MODULE_ALIAS("platform:omap2_mcspi");
1307 #ifdef CONFIG_SUSPEND
1309 * When SPI wake up from off-mode, CS is in activate state. If it was in
1310 * unactive state when driver was suspend, then force it to unactive state at
1313 static int omap2_mcspi_resume(struct device *dev)
1315 struct spi_master *master = dev_get_drvdata(dev);
1316 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1317 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1318 struct omap2_mcspi_cs *cs;
1320 pm_runtime_get_sync(mcspi->dev);
1321 list_for_each_entry(cs, &ctx->cs, node) {
1322 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1324 * We need to toggle CS state for OMAP take this
1325 * change in account.
1327 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1328 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1329 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1330 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1333 pm_runtime_mark_last_busy(mcspi->dev);
1334 pm_runtime_put_autosuspend(mcspi->dev);
1338 #define omap2_mcspi_resume NULL
1341 static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1342 .resume = omap2_mcspi_resume,
1343 .runtime_resume = omap_mcspi_runtime_resume,
1346 static struct platform_driver omap2_mcspi_driver = {
1348 .name = "omap2_mcspi",
1349 .owner = THIS_MODULE,
1350 .pm = &omap2_mcspi_pm_ops,
1351 .of_match_table = omap_mcspi_of_match,
1353 .probe = omap2_mcspi_probe,
1354 .remove = __devexit_p(omap2_mcspi_remove),
1357 module_platform_driver(omap2_mcspi_driver);
1358 MODULE_LICENSE("GPL");