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/interrupt.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/delay.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/dmaengine.h>
31 #include <linux/omap-dma.h>
32 #include <linux/platform_device.h>
33 #include <linux/err.h>
34 #include <linux/clk.h>
36 #include <linux/slab.h>
37 #include <linux/pm_runtime.h>
39 #include <linux/of_device.h>
40 #include <linux/gcd.h>
42 #include <linux/spi/spi.h>
44 #include <linux/platform_data/spi-omap2-mcspi.h>
46 #define OMAP2_MCSPI_MAX_FREQ 48000000
47 #define OMAP2_MCSPI_MAX_FIFODEPTH 64
48 #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
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
58 #define OMAP2_MCSPI_XFERLEVEL 0x7c
60 /* per-channel banks, 0x14 bytes each, first is: */
61 #define OMAP2_MCSPI_CHCONF0 0x2c
62 #define OMAP2_MCSPI_CHSTAT0 0x30
63 #define OMAP2_MCSPI_CHCTRL0 0x34
64 #define OMAP2_MCSPI_TX0 0x38
65 #define OMAP2_MCSPI_RX0 0x3c
67 /* per-register bitmasks: */
68 #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
70 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
71 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
72 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
74 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
75 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
76 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
77 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
78 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
79 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
80 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
81 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
82 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
83 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
84 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
85 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
86 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
87 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
88 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
89 #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
90 #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
92 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
93 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
94 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
95 #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
97 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
99 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
101 /* We have 2 DMA channels per CS, one for RX and one for TX */
102 struct omap2_mcspi_dma {
103 struct dma_chan *dma_tx;
104 struct dma_chan *dma_rx;
109 struct completion dma_tx_completion;
110 struct completion dma_rx_completion;
112 char dma_rx_ch_name[14];
113 char dma_tx_ch_name[14];
116 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
117 * cache operations; better heuristics consider wordsize and bitrate.
119 #define DMA_MIN_BYTES 160
123 * Used for context save and restore, structure members to be updated whenever
124 * corresponding registers are modified.
126 struct omap2_mcspi_regs {
133 struct spi_master *master;
134 /* Virtual base address of the controller */
137 /* SPI1 has 4 channels, while SPI2 has 2 */
138 struct omap2_mcspi_dma *dma_channels;
140 struct omap2_mcspi_regs ctx;
142 unsigned int pin_dir:1;
145 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 writel_relaxed(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 readl_relaxed(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 writel_relaxed(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 readl_relaxed(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 max_fifo_depth, 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 if (t->rx_buf != NULL && t->tx_buf != NULL)
291 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
293 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
295 fifo_depth = gcd(t->len, max_fifo_depth);
296 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
299 wcnt = t->len / bytes_per_word;
300 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
303 xferlevel = wcnt << 16;
304 if (t->rx_buf != NULL) {
305 chconf |= OMAP2_MCSPI_CHCONF_FFER;
306 xferlevel |= (fifo_depth - 1) << 8;
308 if (t->tx_buf != NULL) {
309 chconf |= OMAP2_MCSPI_CHCONF_FFET;
310 xferlevel |= fifo_depth - 1;
313 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
314 mcspi_write_chconf0(spi, chconf);
315 mcspi->fifo_depth = fifo_depth;
321 if (t->rx_buf != NULL)
322 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
324 if (t->tx_buf != NULL)
325 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
327 mcspi_write_chconf0(spi, chconf);
328 mcspi->fifo_depth = 0;
331 static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
333 struct spi_master *spi_cntrl = mcspi->master;
334 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
335 struct omap2_mcspi_cs *cs;
337 /* McSPI: context restore */
338 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
339 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
341 list_for_each_entry(cs, &ctx->cs, node)
342 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
345 static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
347 unsigned long timeout;
349 timeout = jiffies + msecs_to_jiffies(1000);
350 while (!(readl_relaxed(reg) & bit)) {
351 if (time_after(jiffies, timeout)) {
352 if (!(readl_relaxed(reg) & bit))
362 static void omap2_mcspi_rx_callback(void *data)
364 struct spi_device *spi = data;
365 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
366 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
368 /* We must disable the DMA RX request */
369 omap2_mcspi_set_dma_req(spi, 1, 0);
371 complete(&mcspi_dma->dma_rx_completion);
374 static void omap2_mcspi_tx_callback(void *data)
376 struct spi_device *spi = data;
377 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
378 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
380 /* We must disable the DMA TX request */
381 omap2_mcspi_set_dma_req(spi, 0, 0);
383 complete(&mcspi_dma->dma_tx_completion);
386 static void omap2_mcspi_tx_dma(struct spi_device *spi,
387 struct spi_transfer *xfer,
388 struct dma_slave_config cfg)
390 struct omap2_mcspi *mcspi;
391 struct omap2_mcspi_dma *mcspi_dma;
394 mcspi = spi_master_get_devdata(spi->master);
395 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
398 if (mcspi_dma->dma_tx) {
399 struct dma_async_tx_descriptor *tx;
400 struct scatterlist sg;
402 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
404 sg_init_table(&sg, 1);
405 sg_dma_address(&sg) = xfer->tx_dma;
406 sg_dma_len(&sg) = xfer->len;
408 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
409 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
411 tx->callback = omap2_mcspi_tx_callback;
412 tx->callback_param = spi;
413 dmaengine_submit(tx);
415 /* FIXME: fall back to PIO? */
418 dma_async_issue_pending(mcspi_dma->dma_tx);
419 omap2_mcspi_set_dma_req(spi, 0, 1);
424 omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
425 struct dma_slave_config cfg,
428 struct omap2_mcspi *mcspi;
429 struct omap2_mcspi_dma *mcspi_dma;
430 unsigned int count, dma_count;
433 int word_len, element_count;
434 struct omap2_mcspi_cs *cs = spi->controller_state;
435 mcspi = spi_master_get_devdata(spi->master);
436 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
438 dma_count = xfer->len;
440 if (mcspi->fifo_depth == 0)
443 word_len = cs->word_len;
444 l = mcspi_cached_chconf0(spi);
447 element_count = count;
448 else if (word_len <= 16)
449 element_count = count >> 1;
450 else /* word_len <= 32 */
451 element_count = count >> 2;
453 if (mcspi_dma->dma_rx) {
454 struct dma_async_tx_descriptor *tx;
455 struct scatterlist sg;
457 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
459 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
462 sg_init_table(&sg, 1);
463 sg_dma_address(&sg) = xfer->rx_dma;
464 sg_dma_len(&sg) = dma_count;
466 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
467 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
470 tx->callback = omap2_mcspi_rx_callback;
471 tx->callback_param = spi;
472 dmaengine_submit(tx);
474 /* FIXME: fall back to PIO? */
478 dma_async_issue_pending(mcspi_dma->dma_rx);
479 omap2_mcspi_set_dma_req(spi, 1, 1);
481 wait_for_completion(&mcspi_dma->dma_rx_completion);
482 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
485 if (mcspi->fifo_depth > 0)
488 omap2_mcspi_set_enable(spi, 0);
490 elements = element_count - 1;
492 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
495 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
496 & OMAP2_MCSPI_CHSTAT_RXS)) {
499 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
501 ((u8 *)xfer->rx_buf)[elements++] = w;
502 else if (word_len <= 16)
503 ((u16 *)xfer->rx_buf)[elements++] = w;
504 else /* word_len <= 32 */
505 ((u32 *)xfer->rx_buf)[elements++] = w;
507 int bytes_per_word = mcspi_bytes_per_word(word_len);
508 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
509 count -= (bytes_per_word << 1);
510 omap2_mcspi_set_enable(spi, 1);
514 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
515 & OMAP2_MCSPI_CHSTAT_RXS)) {
518 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
520 ((u8 *)xfer->rx_buf)[elements] = w;
521 else if (word_len <= 16)
522 ((u16 *)xfer->rx_buf)[elements] = w;
523 else /* word_len <= 32 */
524 ((u32 *)xfer->rx_buf)[elements] = w;
526 dev_err(&spi->dev, "DMA RX last word empty\n");
527 count -= mcspi_bytes_per_word(word_len);
529 omap2_mcspi_set_enable(spi, 1);
534 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
536 struct omap2_mcspi *mcspi;
537 struct omap2_mcspi_cs *cs = spi->controller_state;
538 struct omap2_mcspi_dma *mcspi_dma;
543 struct dma_slave_config cfg;
544 enum dma_slave_buswidth width;
547 void __iomem *chstat_reg;
548 void __iomem *irqstat_reg;
551 mcspi = spi_master_get_devdata(spi->master);
552 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
553 l = mcspi_cached_chconf0(spi);
556 if (cs->word_len <= 8) {
557 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
559 } else if (cs->word_len <= 16) {
560 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
563 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
570 if (mcspi->fifo_depth > 0) {
571 if (count > mcspi->fifo_depth)
572 burst = mcspi->fifo_depth / es;
577 memset(&cfg, 0, sizeof(cfg));
578 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
579 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
580 cfg.src_addr_width = width;
581 cfg.dst_addr_width = width;
582 cfg.src_maxburst = burst;
583 cfg.dst_maxburst = burst;
589 omap2_mcspi_tx_dma(spi, xfer, cfg);
592 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
595 wait_for_completion(&mcspi_dma->dma_tx_completion);
596 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
599 if (mcspi->fifo_depth > 0) {
600 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
602 if (mcspi_wait_for_reg_bit(irqstat_reg,
603 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
604 dev_err(&spi->dev, "EOW timed out\n");
606 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
607 OMAP2_MCSPI_IRQSTATUS_EOW);
610 /* for TX_ONLY mode, be sure all words have shifted out */
612 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
613 if (mcspi->fifo_depth > 0) {
614 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
615 OMAP2_MCSPI_CHSTAT_TXFFE);
617 dev_err(&spi->dev, "TXFFE timed out\n");
619 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
620 OMAP2_MCSPI_CHSTAT_TXS);
622 dev_err(&spi->dev, "TXS timed out\n");
625 (mcspi_wait_for_reg_bit(chstat_reg,
626 OMAP2_MCSPI_CHSTAT_EOT) < 0))
627 dev_err(&spi->dev, "EOT timed out\n");
634 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
636 struct omap2_mcspi *mcspi;
637 struct omap2_mcspi_cs *cs = spi->controller_state;
638 unsigned int count, c;
640 void __iomem *base = cs->base;
641 void __iomem *tx_reg;
642 void __iomem *rx_reg;
643 void __iomem *chstat_reg;
646 mcspi = spi_master_get_devdata(spi->master);
649 word_len = cs->word_len;
651 l = mcspi_cached_chconf0(spi);
653 /* We store the pre-calculated register addresses on stack to speed
654 * up the transfer loop. */
655 tx_reg = base + OMAP2_MCSPI_TX0;
656 rx_reg = base + OMAP2_MCSPI_RX0;
657 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
659 if (c < (word_len>>3))
672 if (mcspi_wait_for_reg_bit(chstat_reg,
673 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
674 dev_err(&spi->dev, "TXS timed out\n");
677 dev_vdbg(&spi->dev, "write-%d %02x\n",
679 writel_relaxed(*tx++, tx_reg);
682 if (mcspi_wait_for_reg_bit(chstat_reg,
683 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
684 dev_err(&spi->dev, "RXS timed out\n");
688 if (c == 1 && tx == NULL &&
689 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
690 omap2_mcspi_set_enable(spi, 0);
691 *rx++ = readl_relaxed(rx_reg);
692 dev_vdbg(&spi->dev, "read-%d %02x\n",
693 word_len, *(rx - 1));
694 if (mcspi_wait_for_reg_bit(chstat_reg,
695 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
701 } else if (c == 0 && tx == NULL) {
702 omap2_mcspi_set_enable(spi, 0);
705 *rx++ = readl_relaxed(rx_reg);
706 dev_vdbg(&spi->dev, "read-%d %02x\n",
707 word_len, *(rx - 1));
710 } else if (word_len <= 16) {
719 if (mcspi_wait_for_reg_bit(chstat_reg,
720 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
721 dev_err(&spi->dev, "TXS timed out\n");
724 dev_vdbg(&spi->dev, "write-%d %04x\n",
726 writel_relaxed(*tx++, tx_reg);
729 if (mcspi_wait_for_reg_bit(chstat_reg,
730 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
731 dev_err(&spi->dev, "RXS timed out\n");
735 if (c == 2 && tx == NULL &&
736 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
737 omap2_mcspi_set_enable(spi, 0);
738 *rx++ = readl_relaxed(rx_reg);
739 dev_vdbg(&spi->dev, "read-%d %04x\n",
740 word_len, *(rx - 1));
741 if (mcspi_wait_for_reg_bit(chstat_reg,
742 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
748 } else if (c == 0 && tx == NULL) {
749 omap2_mcspi_set_enable(spi, 0);
752 *rx++ = readl_relaxed(rx_reg);
753 dev_vdbg(&spi->dev, "read-%d %04x\n",
754 word_len, *(rx - 1));
757 } else if (word_len <= 32) {
766 if (mcspi_wait_for_reg_bit(chstat_reg,
767 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
768 dev_err(&spi->dev, "TXS timed out\n");
771 dev_vdbg(&spi->dev, "write-%d %08x\n",
773 writel_relaxed(*tx++, tx_reg);
776 if (mcspi_wait_for_reg_bit(chstat_reg,
777 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
778 dev_err(&spi->dev, "RXS timed out\n");
782 if (c == 4 && tx == NULL &&
783 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
784 omap2_mcspi_set_enable(spi, 0);
785 *rx++ = readl_relaxed(rx_reg);
786 dev_vdbg(&spi->dev, "read-%d %08x\n",
787 word_len, *(rx - 1));
788 if (mcspi_wait_for_reg_bit(chstat_reg,
789 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
795 } else if (c == 0 && tx == NULL) {
796 omap2_mcspi_set_enable(spi, 0);
799 *rx++ = readl_relaxed(rx_reg);
800 dev_vdbg(&spi->dev, "read-%d %08x\n",
801 word_len, *(rx - 1));
806 /* for TX_ONLY mode, be sure all words have shifted out */
807 if (xfer->rx_buf == NULL) {
808 if (mcspi_wait_for_reg_bit(chstat_reg,
809 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
810 dev_err(&spi->dev, "TXS timed out\n");
811 } else if (mcspi_wait_for_reg_bit(chstat_reg,
812 OMAP2_MCSPI_CHSTAT_EOT) < 0)
813 dev_err(&spi->dev, "EOT timed out\n");
815 /* disable chan to purge rx datas received in TX_ONLY transfer,
816 * otherwise these rx datas will affect the direct following
819 omap2_mcspi_set_enable(spi, 0);
822 omap2_mcspi_set_enable(spi, 1);
826 static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
830 for (div = 0; div < 15; div++)
831 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
837 /* called only when no transfer is active to this device */
838 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
839 struct spi_transfer *t)
841 struct omap2_mcspi_cs *cs = spi->controller_state;
842 struct omap2_mcspi *mcspi;
843 struct spi_master *spi_cntrl;
845 u8 word_len = spi->bits_per_word;
846 u32 speed_hz = spi->max_speed_hz;
848 mcspi = spi_master_get_devdata(spi->master);
849 spi_cntrl = mcspi->master;
851 if (t != NULL && t->bits_per_word)
852 word_len = t->bits_per_word;
854 cs->word_len = word_len;
856 if (t && t->speed_hz)
857 speed_hz = t->speed_hz;
859 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
860 div = omap2_mcspi_calc_divisor(speed_hz);
862 l = mcspi_cached_chconf0(spi);
864 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
865 * REVISIT: this controller could support SPI_3WIRE mode.
867 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
868 l &= ~OMAP2_MCSPI_CHCONF_IS;
869 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
870 l |= OMAP2_MCSPI_CHCONF_DPE0;
872 l |= OMAP2_MCSPI_CHCONF_IS;
873 l |= OMAP2_MCSPI_CHCONF_DPE1;
874 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
878 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
879 l |= (word_len - 1) << 7;
881 /* set chipselect polarity; manage with FORCE */
882 if (!(spi->mode & SPI_CS_HIGH))
883 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
885 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
887 /* set clock divisor */
888 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
891 /* set SPI mode 0..3 */
892 if (spi->mode & SPI_CPOL)
893 l |= OMAP2_MCSPI_CHCONF_POL;
895 l &= ~OMAP2_MCSPI_CHCONF_POL;
896 if (spi->mode & SPI_CPHA)
897 l |= OMAP2_MCSPI_CHCONF_PHA;
899 l &= ~OMAP2_MCSPI_CHCONF_PHA;
901 mcspi_write_chconf0(spi, l);
903 cs->mode = spi->mode;
905 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
906 OMAP2_MCSPI_MAX_FREQ >> div,
907 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
908 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
914 * Note that we currently allow DMA only if we get a channel
915 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
917 static int omap2_mcspi_request_dma(struct spi_device *spi)
919 struct spi_master *master = spi->master;
920 struct omap2_mcspi *mcspi;
921 struct omap2_mcspi_dma *mcspi_dma;
925 mcspi = spi_master_get_devdata(master);
926 mcspi_dma = mcspi->dma_channels + spi->chip_select;
928 init_completion(&mcspi_dma->dma_rx_completion);
929 init_completion(&mcspi_dma->dma_tx_completion);
932 dma_cap_set(DMA_SLAVE, mask);
933 sig = mcspi_dma->dma_rx_sync_dev;
936 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
938 mcspi_dma->dma_rx_ch_name);
939 if (!mcspi_dma->dma_rx)
942 sig = mcspi_dma->dma_tx_sync_dev;
944 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
946 mcspi_dma->dma_tx_ch_name);
948 if (!mcspi_dma->dma_tx) {
949 dma_release_channel(mcspi_dma->dma_rx);
950 mcspi_dma->dma_rx = NULL;
957 dev_warn(&spi->dev, "not using DMA for McSPI\n");
961 static int omap2_mcspi_setup(struct spi_device *spi)
964 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
965 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
966 struct omap2_mcspi_dma *mcspi_dma;
967 struct omap2_mcspi_cs *cs = spi->controller_state;
969 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
972 cs = kzalloc(sizeof *cs, GFP_KERNEL);
975 cs->base = mcspi->base + spi->chip_select * 0x14;
976 cs->phys = mcspi->phys + spi->chip_select * 0x14;
979 spi->controller_state = cs;
980 /* Link this to context save list */
981 list_add_tail(&cs->node, &ctx->cs);
984 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
985 ret = omap2_mcspi_request_dma(spi);
986 if (ret < 0 && ret != -EAGAIN)
990 ret = pm_runtime_get_sync(mcspi->dev);
994 ret = omap2_mcspi_setup_transfer(spi, NULL);
995 pm_runtime_mark_last_busy(mcspi->dev);
996 pm_runtime_put_autosuspend(mcspi->dev);
1001 static void omap2_mcspi_cleanup(struct spi_device *spi)
1003 struct omap2_mcspi *mcspi;
1004 struct omap2_mcspi_dma *mcspi_dma;
1005 struct omap2_mcspi_cs *cs;
1007 mcspi = spi_master_get_devdata(spi->master);
1009 if (spi->controller_state) {
1010 /* Unlink controller state from context save list */
1011 cs = spi->controller_state;
1012 list_del(&cs->node);
1017 if (spi->chip_select < spi->master->num_chipselect) {
1018 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1020 if (mcspi_dma->dma_rx) {
1021 dma_release_channel(mcspi_dma->dma_rx);
1022 mcspi_dma->dma_rx = NULL;
1024 if (mcspi_dma->dma_tx) {
1025 dma_release_channel(mcspi_dma->dma_tx);
1026 mcspi_dma->dma_tx = NULL;
1031 static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
1034 /* We only enable one channel at a time -- the one whose message is
1035 * -- although this controller would gladly
1036 * arbitrate among multiple channels. This corresponds to "single
1037 * channel" master mode. As a side effect, we need to manage the
1038 * chipselect with the FORCE bit ... CS != channel enable.
1041 struct spi_device *spi;
1042 struct spi_transfer *t = NULL;
1043 struct spi_master *master;
1044 struct omap2_mcspi_dma *mcspi_dma;
1046 struct omap2_mcspi_cs *cs;
1047 struct omap2_mcspi_device_config *cd;
1048 int par_override = 0;
1053 master = spi->master;
1054 mcspi_dma = mcspi->dma_channels + spi->chip_select;
1055 cs = spi->controller_state;
1056 cd = spi->controller_data;
1059 * The slave driver could have changed spi->mode in which case
1060 * it will be different from cs->mode (the current hardware setup).
1061 * If so, set par_override (even though its not a parity issue) so
1062 * omap2_mcspi_setup_transfer will be called to configure the hardware
1063 * with the correct mode on the first iteration of the loop below.
1065 if (spi->mode != cs->mode)
1068 omap2_mcspi_set_enable(spi, 0);
1069 list_for_each_entry(t, &m->transfers, transfer_list) {
1070 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
1074 if (par_override || t->speed_hz || t->bits_per_word) {
1076 status = omap2_mcspi_setup_transfer(spi, t);
1079 if (!t->speed_hz && !t->bits_per_word)
1082 if (cd && cd->cs_per_word) {
1083 chconf = mcspi->ctx.modulctrl;
1084 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1085 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1086 mcspi->ctx.modulctrl =
1087 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1092 omap2_mcspi_force_cs(spi, 1);
1096 chconf = mcspi_cached_chconf0(spi);
1097 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1098 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1100 if (t->tx_buf == NULL)
1101 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1102 else if (t->rx_buf == NULL)
1103 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1105 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1106 /* Turbo mode is for more than one word */
1107 if (t->len > ((cs->word_len + 7) >> 3))
1108 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1111 mcspi_write_chconf0(spi, chconf);
1116 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1117 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
1118 omap2_mcspi_set_fifo(spi, t, 1);
1120 omap2_mcspi_set_enable(spi, 1);
1122 /* RX_ONLY mode needs dummy data in TX reg */
1123 if (t->tx_buf == NULL)
1124 writel_relaxed(0, cs->base
1127 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1128 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
1129 count = omap2_mcspi_txrx_dma(spi, t);
1131 count = omap2_mcspi_txrx_pio(spi, t);
1132 m->actual_length += count;
1134 if (count != t->len) {
1141 udelay(t->delay_usecs);
1143 /* ignore the "leave it on after last xfer" hint */
1145 omap2_mcspi_force_cs(spi, 0);
1149 omap2_mcspi_set_enable(spi, 0);
1151 if (mcspi->fifo_depth > 0)
1152 omap2_mcspi_set_fifo(spi, t, 0);
1154 /* Restore defaults if they were overriden */
1157 status = omap2_mcspi_setup_transfer(spi, NULL);
1161 omap2_mcspi_force_cs(spi, 0);
1163 if (cd && cd->cs_per_word) {
1164 chconf = mcspi->ctx.modulctrl;
1165 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1166 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1167 mcspi->ctx.modulctrl =
1168 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1171 omap2_mcspi_set_enable(spi, 0);
1173 if (mcspi->fifo_depth > 0 && t)
1174 omap2_mcspi_set_fifo(spi, t, 0);
1179 static int omap2_mcspi_transfer_one_message(struct spi_master *master,
1180 struct spi_message *m)
1182 struct spi_device *spi;
1183 struct omap2_mcspi *mcspi;
1184 struct omap2_mcspi_dma *mcspi_dma;
1185 struct spi_transfer *t;
1188 mcspi = spi_master_get_devdata(master);
1189 mcspi_dma = mcspi->dma_channels + spi->chip_select;
1190 m->actual_length = 0;
1193 /* reject invalid messages and transfers */
1194 if (list_empty(&m->transfers))
1196 list_for_each_entry(t, &m->transfers, transfer_list) {
1197 const void *tx_buf = t->tx_buf;
1198 void *rx_buf = t->rx_buf;
1199 unsigned len = t->len;
1201 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
1202 || (len && !(rx_buf || tx_buf))) {
1203 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1211 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1212 dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
1214 OMAP2_MCSPI_MAX_FREQ >> 15);
1218 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1221 if (mcspi_dma->dma_tx && tx_buf != NULL) {
1222 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1223 len, DMA_TO_DEVICE);
1224 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1225 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1230 if (mcspi_dma->dma_rx && rx_buf != NULL) {
1231 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1233 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1234 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1237 dma_unmap_single(mcspi->dev, t->tx_dma,
1238 len, DMA_TO_DEVICE);
1244 omap2_mcspi_work(mcspi, m);
1245 spi_finalize_current_message(master);
1249 static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
1251 struct spi_master *master = mcspi->master;
1252 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1255 ret = pm_runtime_get_sync(mcspi->dev);
1259 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
1260 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
1261 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1263 omap2_mcspi_set_master_mode(master);
1264 pm_runtime_mark_last_busy(mcspi->dev);
1265 pm_runtime_put_autosuspend(mcspi->dev);
1269 static int omap_mcspi_runtime_resume(struct device *dev)
1271 struct omap2_mcspi *mcspi;
1272 struct spi_master *master;
1274 master = dev_get_drvdata(dev);
1275 mcspi = spi_master_get_devdata(master);
1276 omap2_mcspi_restore_ctx(mcspi);
1281 static struct omap2_mcspi_platform_config omap2_pdata = {
1285 static struct omap2_mcspi_platform_config omap4_pdata = {
1286 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1289 static const struct of_device_id omap_mcspi_of_match[] = {
1291 .compatible = "ti,omap2-mcspi",
1292 .data = &omap2_pdata,
1295 .compatible = "ti,omap4-mcspi",
1296 .data = &omap4_pdata,
1300 MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
1302 static int omap2_mcspi_probe(struct platform_device *pdev)
1304 struct spi_master *master;
1305 const struct omap2_mcspi_platform_config *pdata;
1306 struct omap2_mcspi *mcspi;
1309 u32 regs_offset = 0;
1310 static int bus_num = 1;
1311 struct device_node *node = pdev->dev.of_node;
1312 const struct of_device_id *match;
1314 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1315 if (master == NULL) {
1316 dev_dbg(&pdev->dev, "master allocation failed\n");
1320 /* the spi->mode bits understood by this driver: */
1321 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1322 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1323 master->setup = omap2_mcspi_setup;
1324 master->auto_runtime_pm = true;
1325 master->transfer_one_message = omap2_mcspi_transfer_one_message;
1326 master->cleanup = omap2_mcspi_cleanup;
1327 master->dev.of_node = node;
1329 platform_set_drvdata(pdev, master);
1331 mcspi = spi_master_get_devdata(master);
1332 mcspi->master = master;
1334 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1336 u32 num_cs = 1; /* default number of chipselect */
1337 pdata = match->data;
1339 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1340 master->num_chipselect = num_cs;
1341 master->bus_num = bus_num++;
1342 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1343 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
1345 pdata = dev_get_platdata(&pdev->dev);
1346 master->num_chipselect = pdata->num_cs;
1348 master->bus_num = pdev->id;
1349 mcspi->pin_dir = pdata->pin_dir;
1351 regs_offset = pdata->regs_offset;
1353 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1359 r->start += regs_offset;
1360 r->end += regs_offset;
1361 mcspi->phys = r->start;
1363 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1364 if (IS_ERR(mcspi->base)) {
1365 status = PTR_ERR(mcspi->base);
1369 mcspi->dev = &pdev->dev;
1371 INIT_LIST_HEAD(&mcspi->ctx.cs);
1373 mcspi->dma_channels = kcalloc(master->num_chipselect,
1374 sizeof(struct omap2_mcspi_dma),
1377 if (mcspi->dma_channels == NULL)
1380 for (i = 0; i < master->num_chipselect; i++) {
1381 char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
1382 char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
1383 struct resource *dma_res;
1385 sprintf(dma_rx_ch_name, "rx%d", i);
1386 if (!pdev->dev.of_node) {
1388 platform_get_resource_byname(pdev,
1393 "cannot get DMA RX channel\n");
1398 mcspi->dma_channels[i].dma_rx_sync_dev =
1401 sprintf(dma_tx_ch_name, "tx%d", i);
1402 if (!pdev->dev.of_node) {
1404 platform_get_resource_byname(pdev,
1409 "cannot get DMA TX channel\n");
1414 mcspi->dma_channels[i].dma_tx_sync_dev =
1422 pm_runtime_use_autosuspend(&pdev->dev);
1423 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1424 pm_runtime_enable(&pdev->dev);
1426 status = omap2_mcspi_master_setup(mcspi);
1430 status = devm_spi_register_master(&pdev->dev, master);
1437 pm_runtime_disable(&pdev->dev);
1439 kfree(mcspi->dma_channels);
1441 spi_master_put(master);
1445 static int omap2_mcspi_remove(struct platform_device *pdev)
1447 struct spi_master *master;
1448 struct omap2_mcspi *mcspi;
1449 struct omap2_mcspi_dma *dma_channels;
1451 master = platform_get_drvdata(pdev);
1452 mcspi = spi_master_get_devdata(master);
1453 dma_channels = mcspi->dma_channels;
1455 pm_runtime_put_sync(mcspi->dev);
1456 pm_runtime_disable(&pdev->dev);
1458 kfree(dma_channels);
1463 /* work with hotplug and coldplug */
1464 MODULE_ALIAS("platform:omap2_mcspi");
1466 #ifdef CONFIG_SUSPEND
1468 * When SPI wake up from off-mode, CS is in activate state. If it was in
1469 * unactive state when driver was suspend, then force it to unactive state at
1472 static int omap2_mcspi_resume(struct device *dev)
1474 struct spi_master *master = dev_get_drvdata(dev);
1475 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1476 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1477 struct omap2_mcspi_cs *cs;
1479 pm_runtime_get_sync(mcspi->dev);
1480 list_for_each_entry(cs, &ctx->cs, node) {
1481 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1483 * We need to toggle CS state for OMAP take this
1484 * change in account.
1486 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1487 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1488 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1489 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1492 pm_runtime_mark_last_busy(mcspi->dev);
1493 pm_runtime_put_autosuspend(mcspi->dev);
1497 #define omap2_mcspi_resume NULL
1500 static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1501 .resume = omap2_mcspi_resume,
1502 .runtime_resume = omap_mcspi_runtime_resume,
1505 static struct platform_driver omap2_mcspi_driver = {
1507 .name = "omap2_mcspi",
1508 .owner = THIS_MODULE,
1509 .pm = &omap2_mcspi_pm_ops,
1510 .of_match_table = omap_mcspi_of_match,
1512 .probe = omap2_mcspi_probe,
1513 .remove = omap2_mcspi_remove,
1516 module_platform_driver(omap2_mcspi_driver);
1517 MODULE_LICENSE("GPL");