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>
36 #include <linux/spi/spi.h>
39 #include <mach/clock.h>
42 #define OMAP2_MCSPI_MAX_FREQ 48000000
44 #define OMAP2_MCSPI_REVISION 0x00
45 #define OMAP2_MCSPI_SYSCONFIG 0x10
46 #define OMAP2_MCSPI_SYSSTATUS 0x14
47 #define OMAP2_MCSPI_IRQSTATUS 0x18
48 #define OMAP2_MCSPI_IRQENABLE 0x1c
49 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
50 #define OMAP2_MCSPI_SYST 0x24
51 #define OMAP2_MCSPI_MODULCTRL 0x28
53 /* per-channel banks, 0x14 bytes each, first is: */
54 #define OMAP2_MCSPI_CHCONF0 0x2c
55 #define OMAP2_MCSPI_CHSTAT0 0x30
56 #define OMAP2_MCSPI_CHCTRL0 0x34
57 #define OMAP2_MCSPI_TX0 0x38
58 #define OMAP2_MCSPI_RX0 0x3c
60 /* per-register bitmasks: */
62 #define OMAP2_MCSPI_SYSCONFIG_SMARTIDLE (2 << 3)
63 #define OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP (1 << 2)
64 #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE (1 << 0)
65 #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET (1 << 1)
67 #define OMAP2_MCSPI_SYSSTATUS_RESETDONE (1 << 0)
69 #define OMAP2_MCSPI_MODULCTRL_SINGLE (1 << 0)
70 #define OMAP2_MCSPI_MODULCTRL_MS (1 << 2)
71 #define OMAP2_MCSPI_MODULCTRL_STEST (1 << 3)
73 #define OMAP2_MCSPI_CHCONF_PHA (1 << 0)
74 #define OMAP2_MCSPI_CHCONF_POL (1 << 1)
75 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
76 #define OMAP2_MCSPI_CHCONF_EPOL (1 << 6)
77 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
78 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY (0x01 << 12)
79 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY (0x02 << 12)
80 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
81 #define OMAP2_MCSPI_CHCONF_DMAW (1 << 14)
82 #define OMAP2_MCSPI_CHCONF_DMAR (1 << 15)
83 #define OMAP2_MCSPI_CHCONF_DPE0 (1 << 16)
84 #define OMAP2_MCSPI_CHCONF_DPE1 (1 << 17)
85 #define OMAP2_MCSPI_CHCONF_IS (1 << 18)
86 #define OMAP2_MCSPI_CHCONF_TURBO (1 << 19)
87 #define OMAP2_MCSPI_CHCONF_FORCE (1 << 20)
89 #define OMAP2_MCSPI_CHSTAT_RXS (1 << 0)
90 #define OMAP2_MCSPI_CHSTAT_TXS (1 << 1)
91 #define OMAP2_MCSPI_CHSTAT_EOT (1 << 2)
93 #define OMAP2_MCSPI_CHCTRL_EN (1 << 0)
95 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN (1 << 0)
97 /* We have 2 DMA channels per CS, one for RX and one for TX */
98 struct omap2_mcspi_dma {
105 struct completion dma_tx_completion;
106 struct completion dma_rx_completion;
109 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
110 * cache operations; better heuristics consider wordsize and bitrate.
112 #define DMA_MIN_BYTES 8
116 struct work_struct work;
117 /* lock protects queue and registers */
119 struct list_head msg_queue;
120 struct spi_master *master;
123 /* Virtual base address of the controller */
126 /* SPI1 has 4 channels, while SPI2 has 2 */
127 struct omap2_mcspi_dma *dma_channels;
130 struct omap2_mcspi_cs {
136 static struct workqueue_struct *omap2_mcspi_wq;
138 #define MOD_REG_BIT(val, mask, set) do { \
145 static inline void mcspi_write_reg(struct spi_master *master,
148 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
150 __raw_writel(val, mcspi->base + idx);
153 static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
155 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
157 return __raw_readl(mcspi->base + idx);
160 static inline void mcspi_write_cs_reg(const struct spi_device *spi,
163 struct omap2_mcspi_cs *cs = spi->controller_state;
165 __raw_writel(val, cs->base + idx);
168 static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
170 struct omap2_mcspi_cs *cs = spi->controller_state;
172 return __raw_readl(cs->base + idx);
175 static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
176 int is_read, int enable)
180 l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
182 if (is_read) /* 1 is read, 0 write */
183 rw = OMAP2_MCSPI_CHCONF_DMAR;
185 rw = OMAP2_MCSPI_CHCONF_DMAW;
187 MOD_REG_BIT(l, rw, enable);
188 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
191 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
195 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
196 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
199 static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
203 l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
204 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
205 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
208 static void omap2_mcspi_set_master_mode(struct spi_master *master)
212 /* setup when switching from (reset default) slave mode
213 * to single-channel master mode
215 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
216 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
217 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
218 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
219 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
223 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
225 struct omap2_mcspi *mcspi;
226 struct omap2_mcspi_cs *cs = spi->controller_state;
227 struct omap2_mcspi_dma *mcspi_dma;
228 unsigned int count, c;
229 unsigned long base, tx_reg, rx_reg;
230 int word_len, data_type, element_count;
234 mcspi = spi_master_get_devdata(spi->master);
235 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
239 word_len = cs->word_len;
242 tx_reg = base + OMAP2_MCSPI_TX0;
243 rx_reg = base + OMAP2_MCSPI_RX0;
248 data_type = OMAP_DMA_DATA_TYPE_S8;
249 element_count = count;
250 } else if (word_len <= 16) {
251 data_type = OMAP_DMA_DATA_TYPE_S16;
252 element_count = count >> 1;
253 } else /* word_len <= 32 */ {
254 data_type = OMAP_DMA_DATA_TYPE_S32;
255 element_count = count >> 2;
259 omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
260 data_type, element_count, 1,
261 OMAP_DMA_SYNC_ELEMENT,
262 mcspi_dma->dma_tx_sync_dev, 0);
264 omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
265 OMAP_DMA_AMODE_CONSTANT,
268 omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
269 OMAP_DMA_AMODE_POST_INC,
274 omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
275 data_type, element_count, 1,
276 OMAP_DMA_SYNC_ELEMENT,
277 mcspi_dma->dma_rx_sync_dev, 1);
279 omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
280 OMAP_DMA_AMODE_CONSTANT,
283 omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
284 OMAP_DMA_AMODE_POST_INC,
289 omap_start_dma(mcspi_dma->dma_tx_channel);
290 omap2_mcspi_set_dma_req(spi, 0, 1);
294 omap_start_dma(mcspi_dma->dma_rx_channel);
295 omap2_mcspi_set_dma_req(spi, 1, 1);
299 wait_for_completion(&mcspi_dma->dma_tx_completion);
300 dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
304 wait_for_completion(&mcspi_dma->dma_rx_completion);
305 dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
310 static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
312 unsigned long timeout;
314 timeout = jiffies + msecs_to_jiffies(1000);
315 while (!(__raw_readl(reg) & bit)) {
316 if (time_after(jiffies, timeout))
324 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
326 struct omap2_mcspi *mcspi;
327 struct omap2_mcspi_cs *cs = spi->controller_state;
328 unsigned int count, c;
330 void __iomem *base = cs->base;
331 void __iomem *tx_reg;
332 void __iomem *rx_reg;
333 void __iomem *chstat_reg;
336 mcspi = spi_master_get_devdata(spi->master);
339 word_len = cs->word_len;
341 l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
342 l &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
344 /* We store the pre-calculated register addresses on stack to speed
345 * up the transfer loop. */
346 tx_reg = base + OMAP2_MCSPI_TX0;
347 rx_reg = base + OMAP2_MCSPI_RX0;
348 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
360 if (mcspi_wait_for_reg_bit(chstat_reg,
361 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
362 dev_err(&spi->dev, "TXS timed out\n");
366 dev_dbg(&spi->dev, "write-%d %02x\n",
369 __raw_writel(*tx++, tx_reg);
372 if (mcspi_wait_for_reg_bit(chstat_reg,
373 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
374 dev_err(&spi->dev, "RXS timed out\n");
377 /* prevent last RX_ONLY read from triggering
378 * more word i/o: switch to rx+tx
380 if (c == 0 && tx == NULL)
381 mcspi_write_cs_reg(spi,
382 OMAP2_MCSPI_CHCONF0, l);
383 *rx++ = __raw_readl(rx_reg);
385 dev_dbg(&spi->dev, "read-%d %02x\n",
386 word_len, *(rx - 1));
390 } else if (word_len <= 16) {
399 if (mcspi_wait_for_reg_bit(chstat_reg,
400 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
401 dev_err(&spi->dev, "TXS timed out\n");
405 dev_dbg(&spi->dev, "write-%d %04x\n",
408 __raw_writel(*tx++, tx_reg);
411 if (mcspi_wait_for_reg_bit(chstat_reg,
412 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
413 dev_err(&spi->dev, "RXS timed out\n");
416 /* prevent last RX_ONLY read from triggering
417 * more word i/o: switch to rx+tx
419 if (c == 0 && tx == NULL)
420 mcspi_write_cs_reg(spi,
421 OMAP2_MCSPI_CHCONF0, l);
422 *rx++ = __raw_readl(rx_reg);
424 dev_dbg(&spi->dev, "read-%d %04x\n",
425 word_len, *(rx - 1));
429 } else if (word_len <= 32) {
438 if (mcspi_wait_for_reg_bit(chstat_reg,
439 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
440 dev_err(&spi->dev, "TXS timed out\n");
444 dev_dbg(&spi->dev, "write-%d %04x\n",
447 __raw_writel(*tx++, tx_reg);
450 if (mcspi_wait_for_reg_bit(chstat_reg,
451 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
452 dev_err(&spi->dev, "RXS timed out\n");
455 /* prevent last RX_ONLY read from triggering
456 * more word i/o: switch to rx+tx
458 if (c == 0 && tx == NULL)
459 mcspi_write_cs_reg(spi,
460 OMAP2_MCSPI_CHCONF0, l);
461 *rx++ = __raw_readl(rx_reg);
463 dev_dbg(&spi->dev, "read-%d %04x\n",
464 word_len, *(rx - 1));
470 /* for TX_ONLY mode, be sure all words have shifted out */
471 if (xfer->rx_buf == NULL) {
472 if (mcspi_wait_for_reg_bit(chstat_reg,
473 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
474 dev_err(&spi->dev, "TXS timed out\n");
475 } else if (mcspi_wait_for_reg_bit(chstat_reg,
476 OMAP2_MCSPI_CHSTAT_EOT) < 0)
477 dev_err(&spi->dev, "EOT timed out\n");
483 /* called only when no transfer is active to this device */
484 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
485 struct spi_transfer *t)
487 struct omap2_mcspi_cs *cs = spi->controller_state;
488 struct omap2_mcspi *mcspi;
490 u8 word_len = spi->bits_per_word;
492 mcspi = spi_master_get_devdata(spi->master);
494 if (t != NULL && t->bits_per_word)
495 word_len = t->bits_per_word;
497 cs->word_len = word_len;
499 if (spi->max_speed_hz) {
500 while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
506 l = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
508 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
509 * REVISIT: this controller could support SPI_3WIRE mode.
511 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
512 l |= OMAP2_MCSPI_CHCONF_DPE0;
515 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
516 l |= (word_len - 1) << 7;
518 /* set chipselect polarity; manage with FORCE */
519 if (!(spi->mode & SPI_CS_HIGH))
520 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
522 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
524 /* set clock divisor */
525 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
528 /* set SPI mode 0..3 */
529 if (spi->mode & SPI_CPOL)
530 l |= OMAP2_MCSPI_CHCONF_POL;
532 l &= ~OMAP2_MCSPI_CHCONF_POL;
533 if (spi->mode & SPI_CPHA)
534 l |= OMAP2_MCSPI_CHCONF_PHA;
536 l &= ~OMAP2_MCSPI_CHCONF_PHA;
538 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, l);
540 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
541 OMAP2_MCSPI_MAX_FREQ / (1 << div),
542 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
543 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
548 static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
550 struct spi_device *spi = data;
551 struct omap2_mcspi *mcspi;
552 struct omap2_mcspi_dma *mcspi_dma;
554 mcspi = spi_master_get_devdata(spi->master);
555 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
557 complete(&mcspi_dma->dma_rx_completion);
559 /* We must disable the DMA RX request */
560 omap2_mcspi_set_dma_req(spi, 1, 0);
563 static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
565 struct spi_device *spi = data;
566 struct omap2_mcspi *mcspi;
567 struct omap2_mcspi_dma *mcspi_dma;
569 mcspi = spi_master_get_devdata(spi->master);
570 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
572 complete(&mcspi_dma->dma_tx_completion);
574 /* We must disable the DMA TX request */
575 omap2_mcspi_set_dma_req(spi, 0, 0);
578 static int omap2_mcspi_request_dma(struct spi_device *spi)
580 struct spi_master *master = spi->master;
581 struct omap2_mcspi *mcspi;
582 struct omap2_mcspi_dma *mcspi_dma;
584 mcspi = spi_master_get_devdata(master);
585 mcspi_dma = mcspi->dma_channels + spi->chip_select;
587 if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
588 omap2_mcspi_dma_rx_callback, spi,
589 &mcspi_dma->dma_rx_channel)) {
590 dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
594 if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
595 omap2_mcspi_dma_tx_callback, spi,
596 &mcspi_dma->dma_tx_channel)) {
597 omap_free_dma(mcspi_dma->dma_rx_channel);
598 mcspi_dma->dma_rx_channel = -1;
599 dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
603 init_completion(&mcspi_dma->dma_rx_completion);
604 init_completion(&mcspi_dma->dma_tx_completion);
609 static int omap2_mcspi_setup(struct spi_device *spi)
612 struct omap2_mcspi *mcspi;
613 struct omap2_mcspi_dma *mcspi_dma;
614 struct omap2_mcspi_cs *cs = spi->controller_state;
616 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
617 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
622 mcspi = spi_master_get_devdata(spi->master);
623 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
626 cs = kzalloc(sizeof *cs, GFP_KERNEL);
629 cs->base = mcspi->base + spi->chip_select * 0x14;
630 cs->phys = mcspi->phys + spi->chip_select * 0x14;
631 spi->controller_state = cs;
634 if (mcspi_dma->dma_rx_channel == -1
635 || mcspi_dma->dma_tx_channel == -1) {
636 ret = omap2_mcspi_request_dma(spi);
641 clk_enable(mcspi->ick);
642 clk_enable(mcspi->fck);
643 ret = omap2_mcspi_setup_transfer(spi, NULL);
644 clk_disable(mcspi->fck);
645 clk_disable(mcspi->ick);
650 static void omap2_mcspi_cleanup(struct spi_device *spi)
652 struct omap2_mcspi *mcspi;
653 struct omap2_mcspi_dma *mcspi_dma;
655 mcspi = spi_master_get_devdata(spi->master);
656 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
658 kfree(spi->controller_state);
660 if (mcspi_dma->dma_rx_channel != -1) {
661 omap_free_dma(mcspi_dma->dma_rx_channel);
662 mcspi_dma->dma_rx_channel = -1;
664 if (mcspi_dma->dma_tx_channel != -1) {
665 omap_free_dma(mcspi_dma->dma_tx_channel);
666 mcspi_dma->dma_tx_channel = -1;
670 static void omap2_mcspi_work(struct work_struct *work)
672 struct omap2_mcspi *mcspi;
674 mcspi = container_of(work, struct omap2_mcspi, work);
675 spin_lock_irq(&mcspi->lock);
677 clk_enable(mcspi->ick);
678 clk_enable(mcspi->fck);
680 /* We only enable one channel at a time -- the one whose message is
681 * at the head of the queue -- although this controller would gladly
682 * arbitrate among multiple channels. This corresponds to "single
683 * channel" master mode. As a side effect, we need to manage the
684 * chipselect with the FORCE bit ... CS != channel enable.
686 while (!list_empty(&mcspi->msg_queue)) {
687 struct spi_message *m;
688 struct spi_device *spi;
689 struct spi_transfer *t = NULL;
691 struct omap2_mcspi_cs *cs;
692 int par_override = 0;
696 m = container_of(mcspi->msg_queue.next, struct spi_message,
699 list_del_init(&m->queue);
700 spin_unlock_irq(&mcspi->lock);
703 cs = spi->controller_state;
705 omap2_mcspi_set_enable(spi, 1);
706 list_for_each_entry(t, &m->transfers, transfer_list) {
707 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
711 if (par_override || t->speed_hz || t->bits_per_word) {
713 status = omap2_mcspi_setup_transfer(spi, t);
716 if (!t->speed_hz && !t->bits_per_word)
721 omap2_mcspi_force_cs(spi, 1);
725 chconf = mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
726 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
727 if (t->tx_buf == NULL)
728 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
729 else if (t->rx_buf == NULL)
730 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
731 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, chconf);
736 /* RX_ONLY mode needs dummy data in TX reg */
737 if (t->tx_buf == NULL)
738 __raw_writel(0, cs->base
741 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
742 count = omap2_mcspi_txrx_dma(spi, t);
744 count = omap2_mcspi_txrx_pio(spi, t);
745 m->actual_length += count;
747 if (count != t->len) {
754 udelay(t->delay_usecs);
756 /* ignore the "leave it on after last xfer" hint */
758 omap2_mcspi_force_cs(spi, 0);
763 /* Restore defaults if they were overriden */
766 status = omap2_mcspi_setup_transfer(spi, NULL);
770 omap2_mcspi_force_cs(spi, 0);
772 omap2_mcspi_set_enable(spi, 0);
775 m->complete(m->context);
777 spin_lock_irq(&mcspi->lock);
780 clk_disable(mcspi->fck);
781 clk_disable(mcspi->ick);
783 spin_unlock_irq(&mcspi->lock);
786 static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
788 struct omap2_mcspi *mcspi;
790 struct spi_transfer *t;
792 m->actual_length = 0;
795 /* reject invalid messages and transfers */
796 if (list_empty(&m->transfers) || !m->complete)
798 list_for_each_entry(t, &m->transfers, transfer_list) {
799 const void *tx_buf = t->tx_buf;
800 void *rx_buf = t->rx_buf;
801 unsigned len = t->len;
803 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
804 || (len && !(rx_buf || tx_buf))
805 || (t->bits_per_word &&
806 ( t->bits_per_word < 4
807 || t->bits_per_word > 32))) {
808 dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
816 if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
817 dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
819 OMAP2_MCSPI_MAX_FREQ/(1<<16));
823 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
826 /* Do DMA mapping "early" for better error reporting and
827 * dcache use. Note that if dma_unmap_single() ever starts
828 * to do real work on ARM, we'd need to clean up mappings
829 * for previous transfers on *ALL* exits of this loop...
831 if (tx_buf != NULL) {
832 t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
834 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
835 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
840 if (rx_buf != NULL) {
841 t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
843 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
844 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
847 dma_unmap_single(NULL, t->tx_dma,
854 mcspi = spi_master_get_devdata(spi->master);
856 spin_lock_irqsave(&mcspi->lock, flags);
857 list_add_tail(&m->queue, &mcspi->msg_queue);
858 queue_work(omap2_mcspi_wq, &mcspi->work);
859 spin_unlock_irqrestore(&mcspi->lock, flags);
864 static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
866 struct spi_master *master = mcspi->master;
869 clk_enable(mcspi->ick);
870 clk_enable(mcspi->fck);
872 mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
873 OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
875 tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
876 } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
878 mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
879 OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
880 OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
881 OMAP2_MCSPI_SYSCONFIG_SMARTIDLE);
883 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
884 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
886 omap2_mcspi_set_master_mode(master);
888 clk_disable(mcspi->fck);
889 clk_disable(mcspi->ick);
893 static u8 __initdata spi1_rxdma_id [] = {
894 OMAP24XX_DMA_SPI1_RX0,
895 OMAP24XX_DMA_SPI1_RX1,
896 OMAP24XX_DMA_SPI1_RX2,
897 OMAP24XX_DMA_SPI1_RX3,
900 static u8 __initdata spi1_txdma_id [] = {
901 OMAP24XX_DMA_SPI1_TX0,
902 OMAP24XX_DMA_SPI1_TX1,
903 OMAP24XX_DMA_SPI1_TX2,
904 OMAP24XX_DMA_SPI1_TX3,
907 static u8 __initdata spi2_rxdma_id[] = {
908 OMAP24XX_DMA_SPI2_RX0,
909 OMAP24XX_DMA_SPI2_RX1,
912 static u8 __initdata spi2_txdma_id[] = {
913 OMAP24XX_DMA_SPI2_TX0,
914 OMAP24XX_DMA_SPI2_TX1,
917 #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
918 static u8 __initdata spi3_rxdma_id[] = {
919 OMAP24XX_DMA_SPI3_RX0,
920 OMAP24XX_DMA_SPI3_RX1,
923 static u8 __initdata spi3_txdma_id[] = {
924 OMAP24XX_DMA_SPI3_TX0,
925 OMAP24XX_DMA_SPI3_TX1,
929 #ifdef CONFIG_ARCH_OMAP3
930 static u8 __initdata spi4_rxdma_id[] = {
931 OMAP34XX_DMA_SPI4_RX0,
934 static u8 __initdata spi4_txdma_id[] = {
935 OMAP34XX_DMA_SPI4_TX0,
939 static int __init omap2_mcspi_probe(struct platform_device *pdev)
941 struct spi_master *master;
942 struct omap2_mcspi *mcspi;
945 const u8 *rxdma_id, *txdma_id;
946 unsigned num_chipselect;
950 rxdma_id = spi1_rxdma_id;
951 txdma_id = spi1_txdma_id;
955 rxdma_id = spi2_rxdma_id;
956 txdma_id = spi2_txdma_id;
959 #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
961 rxdma_id = spi3_rxdma_id;
962 txdma_id = spi3_txdma_id;
966 #ifdef CONFIG_ARCH_OMAP3
968 rxdma_id = spi4_rxdma_id;
969 txdma_id = spi4_txdma_id;
977 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
978 if (master == NULL) {
979 dev_dbg(&pdev->dev, "master allocation failed\n");
983 /* the spi->mode bits understood by this driver: */
984 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
987 master->bus_num = pdev->id;
989 master->setup = omap2_mcspi_setup;
990 master->transfer = omap2_mcspi_transfer;
991 master->cleanup = omap2_mcspi_cleanup;
992 master->num_chipselect = num_chipselect;
994 dev_set_drvdata(&pdev->dev, master);
996 mcspi = spi_master_get_devdata(master);
997 mcspi->master = master;
999 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1004 if (!request_mem_region(r->start, (r->end - r->start) + 1,
1005 dev_name(&pdev->dev))) {
1010 mcspi->phys = r->start;
1011 mcspi->base = ioremap(r->start, r->end - r->start + 1);
1013 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1018 INIT_WORK(&mcspi->work, omap2_mcspi_work);
1020 spin_lock_init(&mcspi->lock);
1021 INIT_LIST_HEAD(&mcspi->msg_queue);
1023 mcspi->ick = clk_get(&pdev->dev, "ick");
1024 if (IS_ERR(mcspi->ick)) {
1025 dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
1026 status = PTR_ERR(mcspi->ick);
1029 mcspi->fck = clk_get(&pdev->dev, "fck");
1030 if (IS_ERR(mcspi->fck)) {
1031 dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
1032 status = PTR_ERR(mcspi->fck);
1036 mcspi->dma_channels = kcalloc(master->num_chipselect,
1037 sizeof(struct omap2_mcspi_dma),
1040 if (mcspi->dma_channels == NULL)
1043 for (i = 0; i < num_chipselect; i++) {
1044 mcspi->dma_channels[i].dma_rx_channel = -1;
1045 mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
1046 mcspi->dma_channels[i].dma_tx_channel = -1;
1047 mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
1050 if (omap2_mcspi_reset(mcspi) < 0)
1053 status = spi_register_master(master);
1060 kfree(mcspi->dma_channels);
1062 clk_put(mcspi->fck);
1064 clk_put(mcspi->ick);
1066 iounmap(mcspi->base);
1068 release_mem_region(r->start, (r->end - r->start) + 1);
1070 spi_master_put(master);
1074 static int __exit omap2_mcspi_remove(struct platform_device *pdev)
1076 struct spi_master *master;
1077 struct omap2_mcspi *mcspi;
1078 struct omap2_mcspi_dma *dma_channels;
1082 master = dev_get_drvdata(&pdev->dev);
1083 mcspi = spi_master_get_devdata(master);
1084 dma_channels = mcspi->dma_channels;
1086 clk_put(mcspi->fck);
1087 clk_put(mcspi->ick);
1089 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1090 release_mem_region(r->start, (r->end - r->start) + 1);
1093 spi_unregister_master(master);
1095 kfree(dma_channels);
1100 /* work with hotplug and coldplug */
1101 MODULE_ALIAS("platform:omap2_mcspi");
1103 static struct platform_driver omap2_mcspi_driver = {
1105 .name = "omap2_mcspi",
1106 .owner = THIS_MODULE,
1108 .remove = __exit_p(omap2_mcspi_remove),
1112 static int __init omap2_mcspi_init(void)
1114 omap2_mcspi_wq = create_singlethread_workqueue(
1115 omap2_mcspi_driver.driver.name);
1116 if (omap2_mcspi_wq == NULL)
1118 return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
1120 subsys_initcall(omap2_mcspi_init);
1122 static void __exit omap2_mcspi_exit(void)
1124 platform_driver_unregister(&omap2_mcspi_driver);
1126 destroy_workqueue(omap2_mcspi_wq);
1128 module_exit(omap2_mcspi_exit);
1130 MODULE_LICENSE("GPL");