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/platform_device.h>
32 #include <linux/err.h>
33 #include <linux/clk.h>
35 #include <linux/slab.h>
36 #include <linux/pm_runtime.h>
38 #include <linux/spi/spi.h>
41 #include <plat/clock.h>
42 #include <plat/mcspi.h>
44 #define OMAP2_MCSPI_MAX_FREQ 48000000
46 /* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
47 #define OMAP2_MCSPI_MAX_CTRL 4
49 #define OMAP2_MCSPI_REVISION 0x00
50 #define OMAP2_MCSPI_SYSSTATUS 0x14
51 #define OMAP2_MCSPI_IRQSTATUS 0x18
52 #define OMAP2_MCSPI_IRQENABLE 0x1c
53 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
54 #define OMAP2_MCSPI_SYST 0x24
55 #define OMAP2_MCSPI_MODULCTRL 0x28
57 /* per-channel banks, 0x14 bytes each, first is: */
58 #define OMAP2_MCSPI_CHCONF0 0x2c
59 #define OMAP2_MCSPI_CHSTAT0 0x30
60 #define OMAP2_MCSPI_CHCTRL0 0x34
61 #define OMAP2_MCSPI_TX0 0x38
62 #define OMAP2_MCSPI_RX0 0x3c
64 /* per-register bitmasks: */
66 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
67 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
68 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
70 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
71 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
72 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
73 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
74 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
75 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
76 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
77 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
78 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
79 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
80 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
81 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
82 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
83 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
84 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
86 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
87 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
88 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
90 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
92 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
94 /* We have 2 DMA channels per CS, one for RX and one for TX */
95 struct omap2_mcspi_dma {
102 struct completion dma_tx_completion;
103 struct completion dma_rx_completion;
106 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
107 * cache operations; better heuristics consider wordsize and bitrate.
109 #define DMA_MIN_BYTES 160
113 struct work_struct work;
114 /* lock protects queue and registers */
116 struct list_head msg_queue;
117 struct spi_master *master;
118 /* Virtual base address of the controller */
121 /* SPI1 has 4 channels, while SPI2 has 2 */
122 struct omap2_mcspi_dma *dma_channels;
126 struct omap2_mcspi_cs {
130 struct list_head node;
131 /* Context save and restore shadow register */
135 /* used for context save and restore, structure members to be updated whenever
136 * corresponding registers are modified.
138 struct omap2_mcspi_regs {
144 static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
146 static struct workqueue_struct *omap2_mcspi_wq;
148 #define MOD_REG_BIT(val, mask, set) do { \
155 static inline void mcspi_write_reg(struct spi_master *master,
158 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
160 __raw_writel(val, mcspi->base + idx);
163 static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
165 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
167 return __raw_readl(mcspi->base + idx);
170 static inline void mcspi_write_cs_reg(const struct spi_device *spi,
173 struct omap2_mcspi_cs *cs = spi->controller_state;
175 __raw_writel(val, cs->base + idx);
178 static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
180 struct omap2_mcspi_cs *cs = spi->controller_state;
182 return __raw_readl(cs->base + idx);
185 static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
187 struct omap2_mcspi_cs *cs = spi->controller_state;
192 static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
194 struct omap2_mcspi_cs *cs = spi->controller_state;
197 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
198 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
201 static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
202 int is_read, int enable)
206 l = mcspi_cached_chconf0(spi);
208 if (is_read) /* 1 is read, 0 write */
209 rw = OMAP2_MCSPI_CHCONF_DMAR;
211 rw = OMAP2_MCSPI_CHCONF_DMAW;
213 MOD_REG_BIT(l, rw, enable);
214 mcspi_write_chconf0(spi, l);
217 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
221 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
222 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
223 /* Flash post-writes */
224 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
227 static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
231 l = mcspi_cached_chconf0(spi);
232 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
233 mcspi_write_chconf0(spi, l);
236 static void omap2_mcspi_set_master_mode(struct spi_master *master)
240 /* setup when switching from (reset default) slave mode
241 * to single-channel master mode
243 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
244 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
245 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
246 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
247 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
249 omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
252 static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
254 struct spi_master *spi_cntrl;
255 struct omap2_mcspi_cs *cs;
256 spi_cntrl = mcspi->master;
258 /* McSPI: context restore */
259 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
260 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
262 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
263 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
265 list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
267 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
269 static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
271 pm_runtime_put_sync(mcspi->dev);
274 static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
276 return pm_runtime_get_sync(mcspi->dev);
279 static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
281 unsigned long timeout;
283 timeout = jiffies + msecs_to_jiffies(1000);
284 while (!(__raw_readl(reg) & bit)) {
285 if (time_after(jiffies, timeout))
293 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
295 struct omap2_mcspi *mcspi;
296 struct omap2_mcspi_cs *cs = spi->controller_state;
297 struct omap2_mcspi_dma *mcspi_dma;
298 unsigned int count, c;
299 unsigned long base, tx_reg, rx_reg;
300 int word_len, data_type, element_count;
305 void __iomem *chstat_reg;
307 mcspi = spi_master_get_devdata(spi->master);
308 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
309 l = mcspi_cached_chconf0(spi);
311 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
315 word_len = cs->word_len;
318 tx_reg = base + OMAP2_MCSPI_TX0;
319 rx_reg = base + OMAP2_MCSPI_RX0;
324 data_type = OMAP_DMA_DATA_TYPE_S8;
325 element_count = count;
326 } else if (word_len <= 16) {
327 data_type = OMAP_DMA_DATA_TYPE_S16;
328 element_count = count >> 1;
329 } else /* word_len <= 32 */ {
330 data_type = OMAP_DMA_DATA_TYPE_S32;
331 element_count = count >> 2;
335 omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
336 data_type, element_count, 1,
337 OMAP_DMA_SYNC_ELEMENT,
338 mcspi_dma->dma_tx_sync_dev, 0);
340 omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
341 OMAP_DMA_AMODE_CONSTANT,
344 omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
345 OMAP_DMA_AMODE_POST_INC,
350 elements = element_count - 1;
351 if (l & OMAP2_MCSPI_CHCONF_TURBO)
354 omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
355 data_type, elements, 1,
356 OMAP_DMA_SYNC_ELEMENT,
357 mcspi_dma->dma_rx_sync_dev, 1);
359 omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
360 OMAP_DMA_AMODE_CONSTANT,
363 omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
364 OMAP_DMA_AMODE_POST_INC,
369 omap_start_dma(mcspi_dma->dma_tx_channel);
370 omap2_mcspi_set_dma_req(spi, 0, 1);
374 omap_start_dma(mcspi_dma->dma_rx_channel);
375 omap2_mcspi_set_dma_req(spi, 1, 1);
379 wait_for_completion(&mcspi_dma->dma_tx_completion);
380 dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
382 /* for TX_ONLY mode, be sure all words have shifted out */
384 if (mcspi_wait_for_reg_bit(chstat_reg,
385 OMAP2_MCSPI_CHSTAT_TXS) < 0)
386 dev_err(&spi->dev, "TXS timed out\n");
387 else if (mcspi_wait_for_reg_bit(chstat_reg,
388 OMAP2_MCSPI_CHSTAT_EOT) < 0)
389 dev_err(&spi->dev, "EOT timed out\n");
394 wait_for_completion(&mcspi_dma->dma_rx_completion);
395 dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
396 omap2_mcspi_set_enable(spi, 0);
398 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
400 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
401 & OMAP2_MCSPI_CHSTAT_RXS)) {
404 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
406 ((u8 *)xfer->rx_buf)[elements++] = w;
407 else if (word_len <= 16)
408 ((u16 *)xfer->rx_buf)[elements++] = w;
409 else /* word_len <= 32 */
410 ((u32 *)xfer->rx_buf)[elements++] = w;
413 "DMA RX penultimate word empty");
414 count -= (word_len <= 8) ? 2 :
415 (word_len <= 16) ? 4 :
416 /* word_len <= 32 */ 8;
417 omap2_mcspi_set_enable(spi, 1);
422 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
423 & OMAP2_MCSPI_CHSTAT_RXS)) {
426 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
428 ((u8 *)xfer->rx_buf)[elements] = w;
429 else if (word_len <= 16)
430 ((u16 *)xfer->rx_buf)[elements] = w;
431 else /* word_len <= 32 */
432 ((u32 *)xfer->rx_buf)[elements] = w;
434 dev_err(&spi->dev, "DMA RX last word empty");
435 count -= (word_len <= 8) ? 1 :
436 (word_len <= 16) ? 2 :
437 /* word_len <= 32 */ 4;
439 omap2_mcspi_set_enable(spi, 1);
445 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
447 struct omap2_mcspi *mcspi;
448 struct omap2_mcspi_cs *cs = spi->controller_state;
449 unsigned int count, c;
451 void __iomem *base = cs->base;
452 void __iomem *tx_reg;
453 void __iomem *rx_reg;
454 void __iomem *chstat_reg;
457 mcspi = spi_master_get_devdata(spi->master);
460 word_len = cs->word_len;
462 l = mcspi_cached_chconf0(spi);
464 /* We store the pre-calculated register addresses on stack to speed
465 * up the transfer loop. */
466 tx_reg = base + OMAP2_MCSPI_TX0;
467 rx_reg = base + OMAP2_MCSPI_RX0;
468 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
480 if (mcspi_wait_for_reg_bit(chstat_reg,
481 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
482 dev_err(&spi->dev, "TXS timed out\n");
485 dev_vdbg(&spi->dev, "write-%d %02x\n",
487 __raw_writel(*tx++, tx_reg);
490 if (mcspi_wait_for_reg_bit(chstat_reg,
491 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
492 dev_err(&spi->dev, "RXS timed out\n");
496 if (c == 1 && tx == NULL &&
497 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
498 omap2_mcspi_set_enable(spi, 0);
499 *rx++ = __raw_readl(rx_reg);
500 dev_vdbg(&spi->dev, "read-%d %02x\n",
501 word_len, *(rx - 1));
502 if (mcspi_wait_for_reg_bit(chstat_reg,
503 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
509 } else if (c == 0 && tx == NULL) {
510 omap2_mcspi_set_enable(spi, 0);
513 *rx++ = __raw_readl(rx_reg);
514 dev_vdbg(&spi->dev, "read-%d %02x\n",
515 word_len, *(rx - 1));
518 } else if (word_len <= 16) {
527 if (mcspi_wait_for_reg_bit(chstat_reg,
528 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
529 dev_err(&spi->dev, "TXS timed out\n");
532 dev_vdbg(&spi->dev, "write-%d %04x\n",
534 __raw_writel(*tx++, tx_reg);
537 if (mcspi_wait_for_reg_bit(chstat_reg,
538 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
539 dev_err(&spi->dev, "RXS timed out\n");
543 if (c == 2 && tx == NULL &&
544 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
545 omap2_mcspi_set_enable(spi, 0);
546 *rx++ = __raw_readl(rx_reg);
547 dev_vdbg(&spi->dev, "read-%d %04x\n",
548 word_len, *(rx - 1));
549 if (mcspi_wait_for_reg_bit(chstat_reg,
550 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
556 } else if (c == 0 && tx == NULL) {
557 omap2_mcspi_set_enable(spi, 0);
560 *rx++ = __raw_readl(rx_reg);
561 dev_vdbg(&spi->dev, "read-%d %04x\n",
562 word_len, *(rx - 1));
565 } else if (word_len <= 32) {
574 if (mcspi_wait_for_reg_bit(chstat_reg,
575 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
576 dev_err(&spi->dev, "TXS timed out\n");
579 dev_vdbg(&spi->dev, "write-%d %08x\n",
581 __raw_writel(*tx++, tx_reg);
584 if (mcspi_wait_for_reg_bit(chstat_reg,
585 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
586 dev_err(&spi->dev, "RXS timed out\n");
590 if (c == 4 && tx == NULL &&
591 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
592 omap2_mcspi_set_enable(spi, 0);
593 *rx++ = __raw_readl(rx_reg);
594 dev_vdbg(&spi->dev, "read-%d %08x\n",
595 word_len, *(rx - 1));
596 if (mcspi_wait_for_reg_bit(chstat_reg,
597 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
603 } else if (c == 0 && tx == NULL) {
604 omap2_mcspi_set_enable(spi, 0);
607 *rx++ = __raw_readl(rx_reg);
608 dev_vdbg(&spi->dev, "read-%d %08x\n",
609 word_len, *(rx - 1));
614 /* for TX_ONLY mode, be sure all words have shifted out */
615 if (xfer->rx_buf == NULL) {
616 if (mcspi_wait_for_reg_bit(chstat_reg,
617 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
618 dev_err(&spi->dev, "TXS timed out\n");
619 } else if (mcspi_wait_for_reg_bit(chstat_reg,
620 OMAP2_MCSPI_CHSTAT_EOT) < 0)
621 dev_err(&spi->dev, "EOT timed out\n");
623 /* disable chan to purge rx datas received in TX_ONLY transfer,
624 * otherwise these rx datas will affect the direct following
627 omap2_mcspi_set_enable(spi, 0);
630 omap2_mcspi_set_enable(spi, 1);
634 /* called only when no transfer is active to this device */
635 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
636 struct spi_transfer *t)
638 struct omap2_mcspi_cs *cs = spi->controller_state;
639 struct omap2_mcspi *mcspi;
640 struct spi_master *spi_cntrl;
642 u8 word_len = spi->bits_per_word;
643 u32 speed_hz = spi->max_speed_hz;
645 mcspi = spi_master_get_devdata(spi->master);
646 spi_cntrl = mcspi->master;
648 if (t != NULL && t->bits_per_word)
649 word_len = t->bits_per_word;
651 cs->word_len = word_len;
653 if (t && t->speed_hz)
654 speed_hz = t->speed_hz;
657 while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
663 l = mcspi_cached_chconf0(spi);
665 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
666 * REVISIT: this controller could support SPI_3WIRE mode.
668 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
669 l |= OMAP2_MCSPI_CHCONF_DPE0;
672 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
673 l |= (word_len - 1) << 7;
675 /* set chipselect polarity; manage with FORCE */
676 if (!(spi->mode & SPI_CS_HIGH))
677 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
679 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
681 /* set clock divisor */
682 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
685 /* set SPI mode 0..3 */
686 if (spi->mode & SPI_CPOL)
687 l |= OMAP2_MCSPI_CHCONF_POL;
689 l &= ~OMAP2_MCSPI_CHCONF_POL;
690 if (spi->mode & SPI_CPHA)
691 l |= OMAP2_MCSPI_CHCONF_PHA;
693 l &= ~OMAP2_MCSPI_CHCONF_PHA;
695 mcspi_write_chconf0(spi, l);
697 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
698 OMAP2_MCSPI_MAX_FREQ / (1 << div),
699 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
700 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
705 static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
707 struct spi_device *spi = data;
708 struct omap2_mcspi *mcspi;
709 struct omap2_mcspi_dma *mcspi_dma;
711 mcspi = spi_master_get_devdata(spi->master);
712 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
714 complete(&mcspi_dma->dma_rx_completion);
716 /* We must disable the DMA RX request */
717 omap2_mcspi_set_dma_req(spi, 1, 0);
720 static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
722 struct spi_device *spi = data;
723 struct omap2_mcspi *mcspi;
724 struct omap2_mcspi_dma *mcspi_dma;
726 mcspi = spi_master_get_devdata(spi->master);
727 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
729 complete(&mcspi_dma->dma_tx_completion);
731 /* We must disable the DMA TX request */
732 omap2_mcspi_set_dma_req(spi, 0, 0);
735 static int omap2_mcspi_request_dma(struct spi_device *spi)
737 struct spi_master *master = spi->master;
738 struct omap2_mcspi *mcspi;
739 struct omap2_mcspi_dma *mcspi_dma;
741 mcspi = spi_master_get_devdata(master);
742 mcspi_dma = mcspi->dma_channels + spi->chip_select;
744 if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
745 omap2_mcspi_dma_rx_callback, spi,
746 &mcspi_dma->dma_rx_channel)) {
747 dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
751 if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
752 omap2_mcspi_dma_tx_callback, spi,
753 &mcspi_dma->dma_tx_channel)) {
754 omap_free_dma(mcspi_dma->dma_rx_channel);
755 mcspi_dma->dma_rx_channel = -1;
756 dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
760 init_completion(&mcspi_dma->dma_rx_completion);
761 init_completion(&mcspi_dma->dma_tx_completion);
766 static int omap2_mcspi_setup(struct spi_device *spi)
769 struct omap2_mcspi *mcspi;
770 struct omap2_mcspi_dma *mcspi_dma;
771 struct omap2_mcspi_cs *cs = spi->controller_state;
773 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
774 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
779 mcspi = spi_master_get_devdata(spi->master);
780 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
783 cs = kzalloc(sizeof *cs, GFP_KERNEL);
786 cs->base = mcspi->base + spi->chip_select * 0x14;
787 cs->phys = mcspi->phys + spi->chip_select * 0x14;
789 spi->controller_state = cs;
790 /* Link this to context save list */
791 list_add_tail(&cs->node,
792 &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
795 if (mcspi_dma->dma_rx_channel == -1
796 || mcspi_dma->dma_tx_channel == -1) {
797 ret = omap2_mcspi_request_dma(spi);
802 ret = omap2_mcspi_enable_clocks(mcspi);
806 ret = omap2_mcspi_setup_transfer(spi, NULL);
807 omap2_mcspi_disable_clocks(mcspi);
812 static void omap2_mcspi_cleanup(struct spi_device *spi)
814 struct omap2_mcspi *mcspi;
815 struct omap2_mcspi_dma *mcspi_dma;
816 struct omap2_mcspi_cs *cs;
818 mcspi = spi_master_get_devdata(spi->master);
820 if (spi->controller_state) {
821 /* Unlink controller state from context save list */
822 cs = spi->controller_state;
825 kfree(spi->controller_state);
828 if (spi->chip_select < spi->master->num_chipselect) {
829 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
831 if (mcspi_dma->dma_rx_channel != -1) {
832 omap_free_dma(mcspi_dma->dma_rx_channel);
833 mcspi_dma->dma_rx_channel = -1;
835 if (mcspi_dma->dma_tx_channel != -1) {
836 omap_free_dma(mcspi_dma->dma_tx_channel);
837 mcspi_dma->dma_tx_channel = -1;
842 static void omap2_mcspi_work(struct work_struct *work)
844 struct omap2_mcspi *mcspi;
846 mcspi = container_of(work, struct omap2_mcspi, work);
848 if (omap2_mcspi_enable_clocks(mcspi) < 0)
851 spin_lock_irq(&mcspi->lock);
853 /* We only enable one channel at a time -- the one whose message is
854 * at the head of the queue -- although this controller would gladly
855 * arbitrate among multiple channels. This corresponds to "single
856 * channel" master mode. As a side effect, we need to manage the
857 * chipselect with the FORCE bit ... CS != channel enable.
859 while (!list_empty(&mcspi->msg_queue)) {
860 struct spi_message *m;
861 struct spi_device *spi;
862 struct spi_transfer *t = NULL;
864 struct omap2_mcspi_cs *cs;
865 struct omap2_mcspi_device_config *cd;
866 int par_override = 0;
870 m = container_of(mcspi->msg_queue.next, struct spi_message,
873 list_del_init(&m->queue);
874 spin_unlock_irq(&mcspi->lock);
877 cs = spi->controller_state;
878 cd = spi->controller_data;
880 omap2_mcspi_set_enable(spi, 1);
881 list_for_each_entry(t, &m->transfers, transfer_list) {
882 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
886 if (par_override || t->speed_hz || t->bits_per_word) {
888 status = omap2_mcspi_setup_transfer(spi, t);
891 if (!t->speed_hz && !t->bits_per_word)
896 omap2_mcspi_force_cs(spi, 1);
900 chconf = mcspi_cached_chconf0(spi);
901 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
902 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
904 if (t->tx_buf == NULL)
905 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
906 else if (t->rx_buf == NULL)
907 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
909 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
910 /* Turbo mode is for more than one word */
911 if (t->len > ((cs->word_len + 7) >> 3))
912 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
915 mcspi_write_chconf0(spi, chconf);
920 /* RX_ONLY mode needs dummy data in TX reg */
921 if (t->tx_buf == NULL)
922 __raw_writel(0, cs->base
925 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
926 count = omap2_mcspi_txrx_dma(spi, t);
928 count = omap2_mcspi_txrx_pio(spi, t);
929 m->actual_length += count;
931 if (count != t->len) {
938 udelay(t->delay_usecs);
940 /* ignore the "leave it on after last xfer" hint */
942 omap2_mcspi_force_cs(spi, 0);
947 /* Restore defaults if they were overriden */
950 status = omap2_mcspi_setup_transfer(spi, NULL);
954 omap2_mcspi_force_cs(spi, 0);
956 omap2_mcspi_set_enable(spi, 0);
959 m->complete(m->context);
961 spin_lock_irq(&mcspi->lock);
964 spin_unlock_irq(&mcspi->lock);
966 omap2_mcspi_disable_clocks(mcspi);
969 static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
971 struct omap2_mcspi *mcspi;
973 struct spi_transfer *t;
975 m->actual_length = 0;
978 /* reject invalid messages and transfers */
979 if (list_empty(&m->transfers) || !m->complete)
981 list_for_each_entry(t, &m->transfers, transfer_list) {
982 const void *tx_buf = t->tx_buf;
983 void *rx_buf = t->rx_buf;
984 unsigned len = t->len;
986 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
987 || (len && !(rx_buf || tx_buf))
988 || (t->bits_per_word &&
989 ( t->bits_per_word < 4
990 || t->bits_per_word > 32))) {
991 dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
999 if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
1000 dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
1002 OMAP2_MCSPI_MAX_FREQ/(1<<16));
1006 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1009 if (tx_buf != NULL) {
1010 t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
1011 len, DMA_TO_DEVICE);
1012 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
1013 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1018 if (rx_buf != NULL) {
1019 t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
1021 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
1022 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1025 dma_unmap_single(&spi->dev, t->tx_dma,
1026 len, DMA_TO_DEVICE);
1032 mcspi = spi_master_get_devdata(spi->master);
1034 spin_lock_irqsave(&mcspi->lock, flags);
1035 list_add_tail(&m->queue, &mcspi->msg_queue);
1036 queue_work(omap2_mcspi_wq, &mcspi->work);
1037 spin_unlock_irqrestore(&mcspi->lock, flags);
1042 static int __init omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
1044 struct spi_master *master = mcspi->master;
1048 ret = omap2_mcspi_enable_clocks(mcspi);
1052 tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1053 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
1054 omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
1056 omap2_mcspi_set_master_mode(master);
1057 omap2_mcspi_disable_clocks(mcspi);
1061 static int omap_mcspi_runtime_resume(struct device *dev)
1063 struct omap2_mcspi *mcspi;
1064 struct spi_master *master;
1066 master = dev_get_drvdata(dev);
1067 mcspi = spi_master_get_devdata(master);
1068 omap2_mcspi_restore_ctx(mcspi);
1074 static int __init omap2_mcspi_probe(struct platform_device *pdev)
1076 struct spi_master *master;
1077 struct omap2_mcspi_platform_config *pdata = pdev->dev.platform_data;
1078 struct omap2_mcspi *mcspi;
1082 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1083 if (master == NULL) {
1084 dev_dbg(&pdev->dev, "master allocation failed\n");
1088 /* the spi->mode bits understood by this driver: */
1089 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1092 master->bus_num = pdev->id;
1094 master->setup = omap2_mcspi_setup;
1095 master->transfer = omap2_mcspi_transfer;
1096 master->cleanup = omap2_mcspi_cleanup;
1097 master->num_chipselect = pdata->num_cs;
1099 dev_set_drvdata(&pdev->dev, master);
1101 mcspi = spi_master_get_devdata(master);
1102 mcspi->master = master;
1104 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1109 if (!request_mem_region(r->start, (r->end - r->start) + 1,
1110 dev_name(&pdev->dev))) {
1115 r->start += pdata->regs_offset;
1116 r->end += pdata->regs_offset;
1117 mcspi->phys = r->start;
1118 mcspi->base = ioremap(r->start, r->end - r->start + 1);
1120 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1125 mcspi->dev = &pdev->dev;
1126 INIT_WORK(&mcspi->work, omap2_mcspi_work);
1128 spin_lock_init(&mcspi->lock);
1129 INIT_LIST_HEAD(&mcspi->msg_queue);
1130 INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
1132 mcspi->dma_channels = kcalloc(master->num_chipselect,
1133 sizeof(struct omap2_mcspi_dma),
1136 if (mcspi->dma_channels == NULL)
1139 for (i = 0; i < master->num_chipselect; i++) {
1140 char dma_ch_name[14];
1141 struct resource *dma_res;
1143 sprintf(dma_ch_name, "rx%d", i);
1144 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1147 dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1152 mcspi->dma_channels[i].dma_rx_channel = -1;
1153 mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1154 sprintf(dma_ch_name, "tx%d", i);
1155 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1158 dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1163 mcspi->dma_channels[i].dma_tx_channel = -1;
1164 mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
1167 pm_runtime_enable(&pdev->dev);
1169 if (status || omap2_mcspi_master_setup(mcspi) < 0)
1172 status = spi_register_master(master);
1179 spi_master_put(master);
1181 kfree(mcspi->dma_channels);
1183 release_mem_region(r->start, (r->end - r->start) + 1);
1184 iounmap(mcspi->base);
1189 static int __exit omap2_mcspi_remove(struct platform_device *pdev)
1191 struct spi_master *master;
1192 struct omap2_mcspi *mcspi;
1193 struct omap2_mcspi_dma *dma_channels;
1197 master = dev_get_drvdata(&pdev->dev);
1198 mcspi = spi_master_get_devdata(master);
1199 dma_channels = mcspi->dma_channels;
1201 omap2_mcspi_disable_clocks(mcspi);
1202 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1203 release_mem_region(r->start, (r->end - r->start) + 1);
1206 spi_unregister_master(master);
1208 kfree(dma_channels);
1213 /* work with hotplug and coldplug */
1214 MODULE_ALIAS("platform:omap2_mcspi");
1216 #ifdef CONFIG_SUSPEND
1218 * When SPI wake up from off-mode, CS is in activate state. If it was in
1219 * unactive state when driver was suspend, then force it to unactive state at
1222 static int omap2_mcspi_resume(struct device *dev)
1224 struct spi_master *master = dev_get_drvdata(dev);
1225 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1226 struct omap2_mcspi_cs *cs;
1228 omap2_mcspi_enable_clocks(mcspi);
1229 list_for_each_entry(cs, &omap2_mcspi_ctx[master->bus_num - 1].cs,
1231 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1234 * We need to toggle CS state for OMAP take this
1235 * change in account.
1237 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
1238 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1239 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
1240 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1243 omap2_mcspi_disable_clocks(mcspi);
1247 #define omap2_mcspi_resume NULL
1250 static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1251 .resume = omap2_mcspi_resume,
1252 .runtime_resume = omap_mcspi_runtime_resume,
1255 static struct platform_driver omap2_mcspi_driver = {
1257 .name = "omap2_mcspi",
1258 .owner = THIS_MODULE,
1259 .pm = &omap2_mcspi_pm_ops
1261 .remove = __exit_p(omap2_mcspi_remove),
1265 static int __init omap2_mcspi_init(void)
1267 omap2_mcspi_wq = create_singlethread_workqueue(
1268 omap2_mcspi_driver.driver.name);
1269 if (omap2_mcspi_wq == NULL)
1271 return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
1273 subsys_initcall(omap2_mcspi_init);
1275 static void __exit omap2_mcspi_exit(void)
1277 platform_driver_unregister(&omap2_mcspi_driver);
1279 destroy_workqueue(omap2_mcspi_wq);
1281 module_exit(omap2_mcspi_exit);
1283 MODULE_LICENSE("GPL");