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>
42 #include <linux/spi/spi.h>
44 #include <plat/clock.h>
45 #include <plat/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;
134 struct omap2_mcspi_cs {
138 struct list_head node;
139 /* Context save and restore shadow register */
143 #define MOD_REG_BIT(val, mask, set) do { \
150 static inline void mcspi_write_reg(struct spi_master *master,
153 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
155 __raw_writel(val, mcspi->base + idx);
158 static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
160 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
162 return __raw_readl(mcspi->base + idx);
165 static inline void mcspi_write_cs_reg(const struct spi_device *spi,
168 struct omap2_mcspi_cs *cs = spi->controller_state;
170 __raw_writel(val, cs->base + idx);
173 static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
175 struct omap2_mcspi_cs *cs = spi->controller_state;
177 return __raw_readl(cs->base + idx);
180 static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
182 struct omap2_mcspi_cs *cs = spi->controller_state;
187 static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
189 struct omap2_mcspi_cs *cs = spi->controller_state;
192 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
193 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
196 static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
197 int is_read, int enable)
201 l = mcspi_cached_chconf0(spi);
203 if (is_read) /* 1 is read, 0 write */
204 rw = OMAP2_MCSPI_CHCONF_DMAR;
206 rw = OMAP2_MCSPI_CHCONF_DMAW;
208 MOD_REG_BIT(l, rw, enable);
209 mcspi_write_chconf0(spi, l);
212 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
216 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
217 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
218 /* Flash post-writes */
219 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
222 static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
226 l = mcspi_cached_chconf0(spi);
227 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
228 mcspi_write_chconf0(spi, l);
231 static void omap2_mcspi_set_master_mode(struct spi_master *master)
233 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
234 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
238 * Setup when switching from (reset default) slave mode
239 * to single-channel master mode
241 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
242 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
243 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
244 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
245 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
250 static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
252 struct spi_master *spi_cntrl = mcspi->master;
253 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
254 struct omap2_mcspi_cs *cs;
256 /* McSPI: context restore */
257 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
258 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
260 list_for_each_entry(cs, &ctx->cs, node)
261 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
263 static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
265 pm_runtime_mark_last_busy(mcspi->dev);
266 pm_runtime_put_autosuspend(mcspi->dev);
269 static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
271 return pm_runtime_get_sync(mcspi->dev);
274 static int omap2_prepare_transfer(struct spi_master *master)
276 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
278 pm_runtime_get_sync(mcspi->dev);
282 static int omap2_unprepare_transfer(struct spi_master *master)
284 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
286 pm_runtime_mark_last_busy(mcspi->dev);
287 pm_runtime_put_autosuspend(mcspi->dev);
291 static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
293 unsigned long timeout;
295 timeout = jiffies + msecs_to_jiffies(1000);
296 while (!(__raw_readl(reg) & bit)) {
297 if (time_after(jiffies, timeout))
304 static void omap2_mcspi_rx_callback(void *data)
306 struct spi_device *spi = data;
307 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
308 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
310 complete(&mcspi_dma->dma_rx_completion);
312 /* We must disable the DMA RX request */
313 omap2_mcspi_set_dma_req(spi, 1, 0);
316 static void omap2_mcspi_tx_callback(void *data)
318 struct spi_device *spi = data;
319 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
320 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
322 complete(&mcspi_dma->dma_tx_completion);
324 /* We must disable the DMA TX request */
325 omap2_mcspi_set_dma_req(spi, 0, 0);
329 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
331 struct omap2_mcspi *mcspi;
332 struct omap2_mcspi_cs *cs = spi->controller_state;
333 struct omap2_mcspi_dma *mcspi_dma;
335 int word_len, element_count;
340 void __iomem *chstat_reg;
341 struct dma_slave_config cfg;
342 enum dma_slave_buswidth width;
345 mcspi = spi_master_get_devdata(spi->master);
346 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
347 l = mcspi_cached_chconf0(spi);
349 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
351 if (cs->word_len <= 8) {
352 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
354 } else if (cs->word_len <= 16) {
355 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
358 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
362 memset(&cfg, 0, sizeof(cfg));
363 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
364 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
365 cfg.src_addr_width = width;
366 cfg.dst_addr_width = width;
367 cfg.src_maxburst = 1;
368 cfg.dst_maxburst = 1;
370 if (xfer->tx_buf && mcspi_dma->dma_tx) {
371 struct dma_async_tx_descriptor *tx;
372 struct scatterlist sg;
374 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
376 sg_init_table(&sg, 1);
377 sg_dma_address(&sg) = xfer->tx_dma;
378 sg_dma_len(&sg) = xfer->len;
380 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
381 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
383 tx->callback = omap2_mcspi_tx_callback;
384 tx->callback_param = spi;
385 dmaengine_submit(tx);
387 /* FIXME: fall back to PIO? */
391 if (xfer->rx_buf && mcspi_dma->dma_rx) {
392 struct dma_async_tx_descriptor *tx;
393 struct scatterlist sg;
394 size_t len = xfer->len - es;
396 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
398 if (l & OMAP2_MCSPI_CHCONF_TURBO)
401 sg_init_table(&sg, 1);
402 sg_dma_address(&sg) = xfer->rx_dma;
403 sg_dma_len(&sg) = len;
405 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
406 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
408 tx->callback = omap2_mcspi_rx_callback;
409 tx->callback_param = spi;
410 dmaengine_submit(tx);
412 /* FIXME: fall back to PIO? */
417 word_len = cs->word_len;
423 element_count = count;
424 } else if (word_len <= 16) {
425 element_count = count >> 1;
426 } else /* word_len <= 32 */ {
427 element_count = count >> 2;
431 dma_async_issue_pending(mcspi_dma->dma_tx);
432 omap2_mcspi_set_dma_req(spi, 0, 1);
436 dma_async_issue_pending(mcspi_dma->dma_rx);
437 omap2_mcspi_set_dma_req(spi, 1, 1);
441 wait_for_completion(&mcspi_dma->dma_tx_completion);
442 dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
444 /* for TX_ONLY mode, be sure all words have shifted out */
446 if (mcspi_wait_for_reg_bit(chstat_reg,
447 OMAP2_MCSPI_CHSTAT_TXS) < 0)
448 dev_err(&spi->dev, "TXS timed out\n");
449 else if (mcspi_wait_for_reg_bit(chstat_reg,
450 OMAP2_MCSPI_CHSTAT_EOT) < 0)
451 dev_err(&spi->dev, "EOT timed out\n");
456 wait_for_completion(&mcspi_dma->dma_rx_completion);
457 dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
458 omap2_mcspi_set_enable(spi, 0);
460 elements = element_count - 1;
462 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
465 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
466 & OMAP2_MCSPI_CHSTAT_RXS)) {
469 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
471 ((u8 *)xfer->rx_buf)[elements++] = w;
472 else if (word_len <= 16)
473 ((u16 *)xfer->rx_buf)[elements++] = w;
474 else /* word_len <= 32 */
475 ((u32 *)xfer->rx_buf)[elements++] = w;
478 "DMA RX penultimate word empty");
479 count -= (word_len <= 8) ? 2 :
480 (word_len <= 16) ? 4 :
481 /* word_len <= 32 */ 8;
482 omap2_mcspi_set_enable(spi, 1);
487 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
488 & OMAP2_MCSPI_CHSTAT_RXS)) {
491 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
493 ((u8 *)xfer->rx_buf)[elements] = w;
494 else if (word_len <= 16)
495 ((u16 *)xfer->rx_buf)[elements] = w;
496 else /* word_len <= 32 */
497 ((u32 *)xfer->rx_buf)[elements] = w;
499 dev_err(&spi->dev, "DMA RX last word empty");
500 count -= (word_len <= 8) ? 1 :
501 (word_len <= 16) ? 2 :
502 /* word_len <= 32 */ 4;
504 omap2_mcspi_set_enable(spi, 1);
510 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
512 struct omap2_mcspi *mcspi;
513 struct omap2_mcspi_cs *cs = spi->controller_state;
514 unsigned int count, c;
516 void __iomem *base = cs->base;
517 void __iomem *tx_reg;
518 void __iomem *rx_reg;
519 void __iomem *chstat_reg;
522 mcspi = spi_master_get_devdata(spi->master);
525 word_len = cs->word_len;
527 l = mcspi_cached_chconf0(spi);
529 /* We store the pre-calculated register addresses on stack to speed
530 * up the transfer loop. */
531 tx_reg = base + OMAP2_MCSPI_TX0;
532 rx_reg = base + OMAP2_MCSPI_RX0;
533 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
535 if (c < (word_len>>3))
548 if (mcspi_wait_for_reg_bit(chstat_reg,
549 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
550 dev_err(&spi->dev, "TXS timed out\n");
553 dev_vdbg(&spi->dev, "write-%d %02x\n",
555 __raw_writel(*tx++, tx_reg);
558 if (mcspi_wait_for_reg_bit(chstat_reg,
559 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
560 dev_err(&spi->dev, "RXS timed out\n");
564 if (c == 1 && tx == NULL &&
565 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
566 omap2_mcspi_set_enable(spi, 0);
567 *rx++ = __raw_readl(rx_reg);
568 dev_vdbg(&spi->dev, "read-%d %02x\n",
569 word_len, *(rx - 1));
570 if (mcspi_wait_for_reg_bit(chstat_reg,
571 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
577 } else if (c == 0 && tx == NULL) {
578 omap2_mcspi_set_enable(spi, 0);
581 *rx++ = __raw_readl(rx_reg);
582 dev_vdbg(&spi->dev, "read-%d %02x\n",
583 word_len, *(rx - 1));
586 } else if (word_len <= 16) {
595 if (mcspi_wait_for_reg_bit(chstat_reg,
596 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
597 dev_err(&spi->dev, "TXS timed out\n");
600 dev_vdbg(&spi->dev, "write-%d %04x\n",
602 __raw_writel(*tx++, tx_reg);
605 if (mcspi_wait_for_reg_bit(chstat_reg,
606 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
607 dev_err(&spi->dev, "RXS timed out\n");
611 if (c == 2 && tx == NULL &&
612 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
613 omap2_mcspi_set_enable(spi, 0);
614 *rx++ = __raw_readl(rx_reg);
615 dev_vdbg(&spi->dev, "read-%d %04x\n",
616 word_len, *(rx - 1));
617 if (mcspi_wait_for_reg_bit(chstat_reg,
618 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
624 } else if (c == 0 && tx == NULL) {
625 omap2_mcspi_set_enable(spi, 0);
628 *rx++ = __raw_readl(rx_reg);
629 dev_vdbg(&spi->dev, "read-%d %04x\n",
630 word_len, *(rx - 1));
633 } else if (word_len <= 32) {
642 if (mcspi_wait_for_reg_bit(chstat_reg,
643 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
644 dev_err(&spi->dev, "TXS timed out\n");
647 dev_vdbg(&spi->dev, "write-%d %08x\n",
649 __raw_writel(*tx++, tx_reg);
652 if (mcspi_wait_for_reg_bit(chstat_reg,
653 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
654 dev_err(&spi->dev, "RXS timed out\n");
658 if (c == 4 && tx == NULL &&
659 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
660 omap2_mcspi_set_enable(spi, 0);
661 *rx++ = __raw_readl(rx_reg);
662 dev_vdbg(&spi->dev, "read-%d %08x\n",
663 word_len, *(rx - 1));
664 if (mcspi_wait_for_reg_bit(chstat_reg,
665 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
671 } else if (c == 0 && tx == NULL) {
672 omap2_mcspi_set_enable(spi, 0);
675 *rx++ = __raw_readl(rx_reg);
676 dev_vdbg(&spi->dev, "read-%d %08x\n",
677 word_len, *(rx - 1));
682 /* for TX_ONLY mode, be sure all words have shifted out */
683 if (xfer->rx_buf == NULL) {
684 if (mcspi_wait_for_reg_bit(chstat_reg,
685 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
686 dev_err(&spi->dev, "TXS timed out\n");
687 } else if (mcspi_wait_for_reg_bit(chstat_reg,
688 OMAP2_MCSPI_CHSTAT_EOT) < 0)
689 dev_err(&spi->dev, "EOT timed out\n");
691 /* disable chan to purge rx datas received in TX_ONLY transfer,
692 * otherwise these rx datas will affect the direct following
695 omap2_mcspi_set_enable(spi, 0);
698 omap2_mcspi_set_enable(spi, 1);
702 static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
706 for (div = 0; div < 15; div++)
707 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
713 /* called only when no transfer is active to this device */
714 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
715 struct spi_transfer *t)
717 struct omap2_mcspi_cs *cs = spi->controller_state;
718 struct omap2_mcspi *mcspi;
719 struct spi_master *spi_cntrl;
721 u8 word_len = spi->bits_per_word;
722 u32 speed_hz = spi->max_speed_hz;
724 mcspi = spi_master_get_devdata(spi->master);
725 spi_cntrl = mcspi->master;
727 if (t != NULL && t->bits_per_word)
728 word_len = t->bits_per_word;
730 cs->word_len = word_len;
732 if (t && t->speed_hz)
733 speed_hz = t->speed_hz;
735 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
736 div = omap2_mcspi_calc_divisor(speed_hz);
738 l = mcspi_cached_chconf0(spi);
740 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
741 * REVISIT: this controller could support SPI_3WIRE mode.
743 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
744 l |= OMAP2_MCSPI_CHCONF_DPE0;
747 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
748 l |= (word_len - 1) << 7;
750 /* set chipselect polarity; manage with FORCE */
751 if (!(spi->mode & SPI_CS_HIGH))
752 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
754 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
756 /* set clock divisor */
757 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
760 /* set SPI mode 0..3 */
761 if (spi->mode & SPI_CPOL)
762 l |= OMAP2_MCSPI_CHCONF_POL;
764 l &= ~OMAP2_MCSPI_CHCONF_POL;
765 if (spi->mode & SPI_CPHA)
766 l |= OMAP2_MCSPI_CHCONF_PHA;
768 l &= ~OMAP2_MCSPI_CHCONF_PHA;
770 mcspi_write_chconf0(spi, l);
772 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
773 OMAP2_MCSPI_MAX_FREQ >> div,
774 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
775 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
780 static int omap2_mcspi_request_dma(struct spi_device *spi)
782 struct spi_master *master = spi->master;
783 struct omap2_mcspi *mcspi;
784 struct omap2_mcspi_dma *mcspi_dma;
788 mcspi = spi_master_get_devdata(master);
789 mcspi_dma = mcspi->dma_channels + spi->chip_select;
791 init_completion(&mcspi_dma->dma_rx_completion);
792 init_completion(&mcspi_dma->dma_tx_completion);
795 dma_cap_set(DMA_SLAVE, mask);
796 sig = mcspi_dma->dma_rx_sync_dev;
797 mcspi_dma->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
798 if (!mcspi_dma->dma_rx) {
799 dev_err(&spi->dev, "no RX DMA engine channel for McSPI\n");
803 sig = mcspi_dma->dma_tx_sync_dev;
804 mcspi_dma->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
805 if (!mcspi_dma->dma_tx) {
806 dev_err(&spi->dev, "no TX DMA engine channel for McSPI\n");
807 dma_release_channel(mcspi_dma->dma_rx);
808 mcspi_dma->dma_rx = NULL;
815 static int omap2_mcspi_setup(struct spi_device *spi)
818 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
819 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
820 struct omap2_mcspi_dma *mcspi_dma;
821 struct omap2_mcspi_cs *cs = spi->controller_state;
823 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
824 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
829 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
832 cs = kzalloc(sizeof *cs, GFP_KERNEL);
835 cs->base = mcspi->base + spi->chip_select * 0x14;
836 cs->phys = mcspi->phys + spi->chip_select * 0x14;
838 spi->controller_state = cs;
839 /* Link this to context save list */
840 list_add_tail(&cs->node, &ctx->cs);
843 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
844 ret = omap2_mcspi_request_dma(spi);
849 ret = omap2_mcspi_enable_clocks(mcspi);
853 ret = omap2_mcspi_setup_transfer(spi, NULL);
854 omap2_mcspi_disable_clocks(mcspi);
859 static void omap2_mcspi_cleanup(struct spi_device *spi)
861 struct omap2_mcspi *mcspi;
862 struct omap2_mcspi_dma *mcspi_dma;
863 struct omap2_mcspi_cs *cs;
865 mcspi = spi_master_get_devdata(spi->master);
867 if (spi->controller_state) {
868 /* Unlink controller state from context save list */
869 cs = spi->controller_state;
875 if (spi->chip_select < spi->master->num_chipselect) {
876 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
878 if (mcspi_dma->dma_rx) {
879 dma_release_channel(mcspi_dma->dma_rx);
880 mcspi_dma->dma_rx = NULL;
882 if (mcspi_dma->dma_tx) {
883 dma_release_channel(mcspi_dma->dma_tx);
884 mcspi_dma->dma_tx = NULL;
889 static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
892 /* We only enable one channel at a time -- the one whose message is
893 * -- although this controller would gladly
894 * arbitrate among multiple channels. This corresponds to "single
895 * channel" master mode. As a side effect, we need to manage the
896 * chipselect with the FORCE bit ... CS != channel enable.
899 struct spi_device *spi;
900 struct spi_transfer *t = NULL;
902 struct omap2_mcspi_cs *cs;
903 struct omap2_mcspi_device_config *cd;
904 int par_override = 0;
909 cs = spi->controller_state;
910 cd = spi->controller_data;
912 omap2_mcspi_set_enable(spi, 1);
913 list_for_each_entry(t, &m->transfers, transfer_list) {
914 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
918 if (par_override || t->speed_hz || t->bits_per_word) {
920 status = omap2_mcspi_setup_transfer(spi, t);
923 if (!t->speed_hz && !t->bits_per_word)
928 omap2_mcspi_force_cs(spi, 1);
932 chconf = mcspi_cached_chconf0(spi);
933 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
934 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
936 if (t->tx_buf == NULL)
937 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
938 else if (t->rx_buf == NULL)
939 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
941 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
942 /* Turbo mode is for more than one word */
943 if (t->len > ((cs->word_len + 7) >> 3))
944 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
947 mcspi_write_chconf0(spi, chconf);
952 /* RX_ONLY mode needs dummy data in TX reg */
953 if (t->tx_buf == NULL)
954 __raw_writel(0, cs->base
957 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
958 count = omap2_mcspi_txrx_dma(spi, t);
960 count = omap2_mcspi_txrx_pio(spi, t);
961 m->actual_length += count;
963 if (count != t->len) {
970 udelay(t->delay_usecs);
972 /* ignore the "leave it on after last xfer" hint */
974 omap2_mcspi_force_cs(spi, 0);
978 /* Restore defaults if they were overriden */
981 status = omap2_mcspi_setup_transfer(spi, NULL);
985 omap2_mcspi_force_cs(spi, 0);
987 omap2_mcspi_set_enable(spi, 0);
993 static int omap2_mcspi_transfer_one_message(struct spi_master *master,
994 struct spi_message *m)
996 struct omap2_mcspi *mcspi;
997 struct spi_transfer *t;
999 mcspi = spi_master_get_devdata(master);
1000 m->actual_length = 0;
1003 /* reject invalid messages and transfers */
1004 if (list_empty(&m->transfers))
1006 list_for_each_entry(t, &m->transfers, transfer_list) {
1007 const void *tx_buf = t->tx_buf;
1008 void *rx_buf = t->rx_buf;
1009 unsigned len = t->len;
1011 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
1012 || (len && !(rx_buf || tx_buf))
1013 || (t->bits_per_word &&
1014 ( t->bits_per_word < 4
1015 || t->bits_per_word > 32))) {
1016 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1024 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1025 dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
1027 OMAP2_MCSPI_MAX_FREQ >> 15);
1031 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1034 if (tx_buf != NULL) {
1035 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1036 len, DMA_TO_DEVICE);
1037 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1038 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1043 if (rx_buf != NULL) {
1044 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1046 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1047 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1050 dma_unmap_single(mcspi->dev, t->tx_dma,
1051 len, DMA_TO_DEVICE);
1057 omap2_mcspi_work(mcspi, m);
1058 spi_finalize_current_message(master);
1062 static int __init omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
1064 struct spi_master *master = mcspi->master;
1065 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1068 ret = omap2_mcspi_enable_clocks(mcspi);
1072 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
1073 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
1074 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1076 omap2_mcspi_set_master_mode(master);
1077 omap2_mcspi_disable_clocks(mcspi);
1081 static int omap_mcspi_runtime_resume(struct device *dev)
1083 struct omap2_mcspi *mcspi;
1084 struct spi_master *master;
1086 master = dev_get_drvdata(dev);
1087 mcspi = spi_master_get_devdata(master);
1088 omap2_mcspi_restore_ctx(mcspi);
1093 static struct omap2_mcspi_platform_config omap2_pdata = {
1097 static struct omap2_mcspi_platform_config omap4_pdata = {
1098 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1101 static const struct of_device_id omap_mcspi_of_match[] = {
1103 .compatible = "ti,omap2-mcspi",
1104 .data = &omap2_pdata,
1107 .compatible = "ti,omap4-mcspi",
1108 .data = &omap4_pdata,
1112 MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
1114 static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
1116 struct spi_master *master;
1117 struct omap2_mcspi_platform_config *pdata;
1118 struct omap2_mcspi *mcspi;
1121 u32 regs_offset = 0;
1122 static int bus_num = 1;
1123 struct device_node *node = pdev->dev.of_node;
1124 const struct of_device_id *match;
1126 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1127 if (master == NULL) {
1128 dev_dbg(&pdev->dev, "master allocation failed\n");
1132 /* the spi->mode bits understood by this driver: */
1133 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1135 master->setup = omap2_mcspi_setup;
1136 master->prepare_transfer_hardware = omap2_prepare_transfer;
1137 master->unprepare_transfer_hardware = omap2_unprepare_transfer;
1138 master->transfer_one_message = omap2_mcspi_transfer_one_message;
1139 master->cleanup = omap2_mcspi_cleanup;
1140 master->dev.of_node = node;
1142 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1144 u32 num_cs = 1; /* default number of chipselect */
1145 pdata = match->data;
1147 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1148 master->num_chipselect = num_cs;
1149 master->bus_num = bus_num++;
1151 pdata = pdev->dev.platform_data;
1152 master->num_chipselect = pdata->num_cs;
1154 master->bus_num = pdev->id;
1156 regs_offset = pdata->regs_offset;
1158 dev_set_drvdata(&pdev->dev, master);
1160 mcspi = spi_master_get_devdata(master);
1161 mcspi->master = master;
1163 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1169 r->start += regs_offset;
1170 r->end += regs_offset;
1171 mcspi->phys = r->start;
1173 mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
1175 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1180 mcspi->dev = &pdev->dev;
1182 INIT_LIST_HEAD(&mcspi->ctx.cs);
1184 mcspi->dma_channels = kcalloc(master->num_chipselect,
1185 sizeof(struct omap2_mcspi_dma),
1188 if (mcspi->dma_channels == NULL)
1191 for (i = 0; i < master->num_chipselect; i++) {
1192 char dma_ch_name[14];
1193 struct resource *dma_res;
1195 sprintf(dma_ch_name, "rx%d", i);
1196 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1199 dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1204 mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1205 sprintf(dma_ch_name, "tx%d", i);
1206 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1209 dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1214 mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
1220 pm_runtime_use_autosuspend(&pdev->dev);
1221 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1222 pm_runtime_enable(&pdev->dev);
1224 if (status || omap2_mcspi_master_setup(mcspi) < 0)
1227 status = spi_register_master(master);
1229 goto err_spi_register;
1234 spi_master_put(master);
1236 pm_runtime_disable(&pdev->dev);
1238 kfree(mcspi->dma_channels);
1241 platform_set_drvdata(pdev, NULL);
1245 static int __devexit omap2_mcspi_remove(struct platform_device *pdev)
1247 struct spi_master *master;
1248 struct omap2_mcspi *mcspi;
1249 struct omap2_mcspi_dma *dma_channels;
1251 master = dev_get_drvdata(&pdev->dev);
1252 mcspi = spi_master_get_devdata(master);
1253 dma_channels = mcspi->dma_channels;
1255 omap2_mcspi_disable_clocks(mcspi);
1256 pm_runtime_disable(&pdev->dev);
1258 spi_unregister_master(master);
1259 kfree(dma_channels);
1260 platform_set_drvdata(pdev, NULL);
1265 /* work with hotplug and coldplug */
1266 MODULE_ALIAS("platform:omap2_mcspi");
1268 #ifdef CONFIG_SUSPEND
1270 * When SPI wake up from off-mode, CS is in activate state. If it was in
1271 * unactive state when driver was suspend, then force it to unactive state at
1274 static int omap2_mcspi_resume(struct device *dev)
1276 struct spi_master *master = dev_get_drvdata(dev);
1277 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1278 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1279 struct omap2_mcspi_cs *cs;
1281 omap2_mcspi_enable_clocks(mcspi);
1282 list_for_each_entry(cs, &ctx->cs, node) {
1283 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1285 * We need to toggle CS state for OMAP take this
1286 * change in account.
1288 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
1289 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1290 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
1291 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1294 omap2_mcspi_disable_clocks(mcspi);
1298 #define omap2_mcspi_resume NULL
1301 static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1302 .resume = omap2_mcspi_resume,
1303 .runtime_resume = omap_mcspi_runtime_resume,
1306 static struct platform_driver omap2_mcspi_driver = {
1308 .name = "omap2_mcspi",
1309 .owner = THIS_MODULE,
1310 .pm = &omap2_mcspi_pm_ops,
1311 .of_match_table = omap_mcspi_of_match,
1313 .probe = omap2_mcspi_probe,
1314 .remove = __devexit_p(omap2_mcspi_remove),
1317 module_platform_driver(omap2_mcspi_driver);
1318 MODULE_LICENSE("GPL");