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 /* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
45 #define OMAP2_MCSPI_MAX_CTRL 4
47 #define OMAP2_MCSPI_REVISION 0x00
48 #define OMAP2_MCSPI_SYSCONFIG 0x10
49 #define OMAP2_MCSPI_SYSSTATUS 0x14
50 #define OMAP2_MCSPI_IRQSTATUS 0x18
51 #define OMAP2_MCSPI_IRQENABLE 0x1c
52 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
53 #define OMAP2_MCSPI_SYST 0x24
54 #define OMAP2_MCSPI_MODULCTRL 0x28
56 /* per-channel banks, 0x14 bytes each, first is: */
57 #define OMAP2_MCSPI_CHCONF0 0x2c
58 #define OMAP2_MCSPI_CHSTAT0 0x30
59 #define OMAP2_MCSPI_CHCTRL0 0x34
60 #define OMAP2_MCSPI_TX0 0x38
61 #define OMAP2_MCSPI_RX0 0x3c
63 /* per-register bitmasks: */
65 #define OMAP2_MCSPI_SYSCONFIG_SMARTIDLE BIT(4)
66 #define OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
67 #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
68 #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
70 #define OMAP2_MCSPI_SYSSTATUS_RESETDONE BIT(0)
72 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
73 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
74 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
76 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
77 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
78 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
79 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
80 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
81 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
82 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
83 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
84 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
85 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
86 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
87 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
88 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
89 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
90 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
92 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
93 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
94 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
96 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
98 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
100 /* We have 2 DMA channels per CS, one for RX and one for TX */
101 struct omap2_mcspi_dma {
108 struct completion dma_tx_completion;
109 struct completion dma_rx_completion;
112 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
113 * cache operations; better heuristics consider wordsize and bitrate.
115 #define DMA_MIN_BYTES 8
119 struct work_struct work;
120 /* lock protects queue and registers */
122 struct list_head msg_queue;
123 struct spi_master *master;
126 /* Virtual base address of the controller */
129 /* SPI1 has 4 channels, while SPI2 has 2 */
130 struct omap2_mcspi_dma *dma_channels;
133 struct omap2_mcspi_cs {
137 /* Context save and restore shadow register */
141 /* used for context save and restore, structure members to be updated whenever
142 * corresponding registers are modified.
144 struct omap2_mcspi_regs {
150 static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
152 static struct workqueue_struct *omap2_mcspi_wq;
154 #define MOD_REG_BIT(val, mask, set) do { \
161 static inline void mcspi_write_reg(struct spi_master *master,
164 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
166 __raw_writel(val, mcspi->base + idx);
169 static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
171 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
173 return __raw_readl(mcspi->base + idx);
176 static inline void mcspi_write_cs_reg(const struct spi_device *spi,
179 struct omap2_mcspi_cs *cs = spi->controller_state;
181 __raw_writel(val, cs->base + idx);
184 static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
186 struct omap2_mcspi_cs *cs = spi->controller_state;
188 return __raw_readl(cs->base + idx);
191 static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
193 struct omap2_mcspi_cs *cs = spi->controller_state;
198 static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
200 struct omap2_mcspi_cs *cs = spi->controller_state;
203 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
206 static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
207 int is_read, int enable)
211 l = mcspi_cached_chconf0(spi);
213 if (is_read) /* 1 is read, 0 write */
214 rw = OMAP2_MCSPI_CHCONF_DMAR;
216 rw = OMAP2_MCSPI_CHCONF_DMAW;
218 MOD_REG_BIT(l, rw, enable);
219 mcspi_write_chconf0(spi, l);
222 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
226 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
227 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
230 static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
234 l = mcspi_cached_chconf0(spi);
235 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
236 mcspi_write_chconf0(spi, l);
239 static void omap2_mcspi_set_master_mode(struct spi_master *master)
243 /* setup when switching from (reset default) slave mode
244 * to single-channel master mode
246 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
247 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
248 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
249 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
250 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
252 omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
255 static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
257 struct spi_master *spi_cntrl;
258 spi_cntrl = mcspi->master;
260 /* McSPI: context restore */
261 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
262 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
264 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_SYSCONFIG,
265 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].sysconfig);
267 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
268 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
270 static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
272 clk_disable(mcspi->ick);
273 clk_disable(mcspi->fck);
276 static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
278 if (clk_enable(mcspi->ick))
280 if (clk_enable(mcspi->fck))
283 omap2_mcspi_restore_ctx(mcspi);
289 omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
291 struct omap2_mcspi *mcspi;
292 struct omap2_mcspi_cs *cs = spi->controller_state;
293 struct omap2_mcspi_dma *mcspi_dma;
294 unsigned int count, c;
295 unsigned long base, tx_reg, rx_reg;
296 int word_len, data_type, element_count;
300 mcspi = spi_master_get_devdata(spi->master);
301 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
305 word_len = cs->word_len;
308 tx_reg = base + OMAP2_MCSPI_TX0;
309 rx_reg = base + OMAP2_MCSPI_RX0;
314 data_type = OMAP_DMA_DATA_TYPE_S8;
315 element_count = count;
316 } else if (word_len <= 16) {
317 data_type = OMAP_DMA_DATA_TYPE_S16;
318 element_count = count >> 1;
319 } else /* word_len <= 32 */ {
320 data_type = OMAP_DMA_DATA_TYPE_S32;
321 element_count = count >> 2;
325 omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
326 data_type, element_count, 1,
327 OMAP_DMA_SYNC_ELEMENT,
328 mcspi_dma->dma_tx_sync_dev, 0);
330 omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
331 OMAP_DMA_AMODE_CONSTANT,
334 omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
335 OMAP_DMA_AMODE_POST_INC,
340 omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
341 data_type, element_count - 1, 1,
342 OMAP_DMA_SYNC_ELEMENT,
343 mcspi_dma->dma_rx_sync_dev, 1);
345 omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
346 OMAP_DMA_AMODE_CONSTANT,
349 omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
350 OMAP_DMA_AMODE_POST_INC,
355 omap_start_dma(mcspi_dma->dma_tx_channel);
356 omap2_mcspi_set_dma_req(spi, 0, 1);
360 omap_start_dma(mcspi_dma->dma_rx_channel);
361 omap2_mcspi_set_dma_req(spi, 1, 1);
365 wait_for_completion(&mcspi_dma->dma_tx_completion);
366 dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
370 wait_for_completion(&mcspi_dma->dma_rx_completion);
371 dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
372 omap2_mcspi_set_enable(spi, 0);
373 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
374 & OMAP2_MCSPI_CHSTAT_RXS)) {
377 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
379 ((u8 *)xfer->rx_buf)[element_count - 1] = w;
380 else if (word_len <= 16)
381 ((u16 *)xfer->rx_buf)[element_count - 1] = w;
382 else /* word_len <= 32 */
383 ((u32 *)xfer->rx_buf)[element_count - 1] = w;
385 dev_err(&spi->dev, "DMA RX last word empty");
386 count -= (word_len <= 8) ? 1 :
387 (word_len <= 16) ? 2 :
388 /* word_len <= 32 */ 4;
390 omap2_mcspi_set_enable(spi, 1);
395 static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
397 unsigned long timeout;
399 timeout = jiffies + msecs_to_jiffies(1000);
400 while (!(__raw_readl(reg) & bit)) {
401 if (time_after(jiffies, timeout))
409 omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
411 struct omap2_mcspi *mcspi;
412 struct omap2_mcspi_cs *cs = spi->controller_state;
413 unsigned int count, c;
415 void __iomem *base = cs->base;
416 void __iomem *tx_reg;
417 void __iomem *rx_reg;
418 void __iomem *chstat_reg;
421 mcspi = spi_master_get_devdata(spi->master);
424 word_len = cs->word_len;
426 l = mcspi_cached_chconf0(spi);
427 l &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
429 /* We store the pre-calculated register addresses on stack to speed
430 * up the transfer loop. */
431 tx_reg = base + OMAP2_MCSPI_TX0;
432 rx_reg = base + OMAP2_MCSPI_RX0;
433 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
445 if (mcspi_wait_for_reg_bit(chstat_reg,
446 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
447 dev_err(&spi->dev, "TXS timed out\n");
451 dev_dbg(&spi->dev, "write-%d %02x\n",
454 __raw_writel(*tx++, tx_reg);
457 if (mcspi_wait_for_reg_bit(chstat_reg,
458 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
459 dev_err(&spi->dev, "RXS timed out\n");
462 /* prevent last RX_ONLY read from triggering
463 * more word i/o: switch to rx+tx
465 if (c == 0 && tx == NULL)
466 mcspi_write_chconf0(spi, l);
467 *rx++ = __raw_readl(rx_reg);
469 dev_dbg(&spi->dev, "read-%d %02x\n",
470 word_len, *(rx - 1));
474 } else if (word_len <= 16) {
483 if (mcspi_wait_for_reg_bit(chstat_reg,
484 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
485 dev_err(&spi->dev, "TXS timed out\n");
489 dev_dbg(&spi->dev, "write-%d %04x\n",
492 __raw_writel(*tx++, tx_reg);
495 if (mcspi_wait_for_reg_bit(chstat_reg,
496 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
497 dev_err(&spi->dev, "RXS timed out\n");
500 /* prevent last RX_ONLY read from triggering
501 * more word i/o: switch to rx+tx
503 if (c == 0 && tx == NULL)
504 mcspi_write_chconf0(spi, l);
505 *rx++ = __raw_readl(rx_reg);
507 dev_dbg(&spi->dev, "read-%d %04x\n",
508 word_len, *(rx - 1));
512 } else if (word_len <= 32) {
521 if (mcspi_wait_for_reg_bit(chstat_reg,
522 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
523 dev_err(&spi->dev, "TXS timed out\n");
527 dev_dbg(&spi->dev, "write-%d %04x\n",
530 __raw_writel(*tx++, tx_reg);
533 if (mcspi_wait_for_reg_bit(chstat_reg,
534 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
535 dev_err(&spi->dev, "RXS timed out\n");
538 /* prevent last RX_ONLY read from triggering
539 * more word i/o: switch to rx+tx
541 if (c == 0 && tx == NULL)
542 mcspi_write_chconf0(spi, l);
543 *rx++ = __raw_readl(rx_reg);
545 dev_dbg(&spi->dev, "read-%d %04x\n",
546 word_len, *(rx - 1));
552 /* for TX_ONLY mode, be sure all words have shifted out */
553 if (xfer->rx_buf == NULL) {
554 if (mcspi_wait_for_reg_bit(chstat_reg,
555 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
556 dev_err(&spi->dev, "TXS timed out\n");
557 } else if (mcspi_wait_for_reg_bit(chstat_reg,
558 OMAP2_MCSPI_CHSTAT_EOT) < 0)
559 dev_err(&spi->dev, "EOT timed out\n");
565 /* called only when no transfer is active to this device */
566 static int omap2_mcspi_setup_transfer(struct spi_device *spi,
567 struct spi_transfer *t)
569 struct omap2_mcspi_cs *cs = spi->controller_state;
570 struct omap2_mcspi *mcspi;
571 struct spi_master *spi_cntrl;
573 u8 word_len = spi->bits_per_word;
575 mcspi = spi_master_get_devdata(spi->master);
576 spi_cntrl = mcspi->master;
578 if (t != NULL && t->bits_per_word)
579 word_len = t->bits_per_word;
581 cs->word_len = word_len;
583 if (spi->max_speed_hz) {
584 while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
590 l = mcspi_cached_chconf0(spi);
592 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
593 * REVISIT: this controller could support SPI_3WIRE mode.
595 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
596 l |= OMAP2_MCSPI_CHCONF_DPE0;
599 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
600 l |= (word_len - 1) << 7;
602 /* set chipselect polarity; manage with FORCE */
603 if (!(spi->mode & SPI_CS_HIGH))
604 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
606 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
608 /* set clock divisor */
609 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
612 /* set SPI mode 0..3 */
613 if (spi->mode & SPI_CPOL)
614 l |= OMAP2_MCSPI_CHCONF_POL;
616 l &= ~OMAP2_MCSPI_CHCONF_POL;
617 if (spi->mode & SPI_CPHA)
618 l |= OMAP2_MCSPI_CHCONF_PHA;
620 l &= ~OMAP2_MCSPI_CHCONF_PHA;
622 mcspi_write_chconf0(spi, l);
624 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
625 OMAP2_MCSPI_MAX_FREQ / (1 << div),
626 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
627 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
632 static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
634 struct spi_device *spi = data;
635 struct omap2_mcspi *mcspi;
636 struct omap2_mcspi_dma *mcspi_dma;
638 mcspi = spi_master_get_devdata(spi->master);
639 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
641 complete(&mcspi_dma->dma_rx_completion);
643 /* We must disable the DMA RX request */
644 omap2_mcspi_set_dma_req(spi, 1, 0);
647 static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
649 struct spi_device *spi = data;
650 struct omap2_mcspi *mcspi;
651 struct omap2_mcspi_dma *mcspi_dma;
653 mcspi = spi_master_get_devdata(spi->master);
654 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
656 complete(&mcspi_dma->dma_tx_completion);
658 /* We must disable the DMA TX request */
659 omap2_mcspi_set_dma_req(spi, 0, 0);
662 static int omap2_mcspi_request_dma(struct spi_device *spi)
664 struct spi_master *master = spi->master;
665 struct omap2_mcspi *mcspi;
666 struct omap2_mcspi_dma *mcspi_dma;
668 mcspi = spi_master_get_devdata(master);
669 mcspi_dma = mcspi->dma_channels + spi->chip_select;
671 if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
672 omap2_mcspi_dma_rx_callback, spi,
673 &mcspi_dma->dma_rx_channel)) {
674 dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
678 if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
679 omap2_mcspi_dma_tx_callback, spi,
680 &mcspi_dma->dma_tx_channel)) {
681 omap_free_dma(mcspi_dma->dma_rx_channel);
682 mcspi_dma->dma_rx_channel = -1;
683 dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
687 init_completion(&mcspi_dma->dma_rx_completion);
688 init_completion(&mcspi_dma->dma_tx_completion);
693 static int omap2_mcspi_setup(struct spi_device *spi)
696 struct omap2_mcspi *mcspi;
697 struct omap2_mcspi_dma *mcspi_dma;
698 struct omap2_mcspi_cs *cs = spi->controller_state;
700 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
701 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
706 mcspi = spi_master_get_devdata(spi->master);
707 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
710 cs = kzalloc(sizeof *cs, GFP_KERNEL);
713 cs->base = mcspi->base + spi->chip_select * 0x14;
714 cs->phys = mcspi->phys + spi->chip_select * 0x14;
716 spi->controller_state = cs;
719 if (mcspi_dma->dma_rx_channel == -1
720 || mcspi_dma->dma_tx_channel == -1) {
721 ret = omap2_mcspi_request_dma(spi);
726 if (omap2_mcspi_enable_clocks(mcspi))
729 ret = omap2_mcspi_setup_transfer(spi, NULL);
730 omap2_mcspi_disable_clocks(mcspi);
735 static void omap2_mcspi_cleanup(struct spi_device *spi)
737 struct omap2_mcspi *mcspi;
738 struct omap2_mcspi_dma *mcspi_dma;
740 mcspi = spi_master_get_devdata(spi->master);
741 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
743 kfree(spi->controller_state);
745 if (mcspi_dma->dma_rx_channel != -1) {
746 omap_free_dma(mcspi_dma->dma_rx_channel);
747 mcspi_dma->dma_rx_channel = -1;
749 if (mcspi_dma->dma_tx_channel != -1) {
750 omap_free_dma(mcspi_dma->dma_tx_channel);
751 mcspi_dma->dma_tx_channel = -1;
755 static void omap2_mcspi_work(struct work_struct *work)
757 struct omap2_mcspi *mcspi;
759 mcspi = container_of(work, struct omap2_mcspi, work);
760 spin_lock_irq(&mcspi->lock);
762 if (omap2_mcspi_enable_clocks(mcspi))
765 /* We only enable one channel at a time -- the one whose message is
766 * at the head of the queue -- although this controller would gladly
767 * arbitrate among multiple channels. This corresponds to "single
768 * channel" master mode. As a side effect, we need to manage the
769 * chipselect with the FORCE bit ... CS != channel enable.
771 while (!list_empty(&mcspi->msg_queue)) {
772 struct spi_message *m;
773 struct spi_device *spi;
774 struct spi_transfer *t = NULL;
776 struct omap2_mcspi_cs *cs;
777 int par_override = 0;
781 m = container_of(mcspi->msg_queue.next, struct spi_message,
784 list_del_init(&m->queue);
785 spin_unlock_irq(&mcspi->lock);
788 cs = spi->controller_state;
790 omap2_mcspi_set_enable(spi, 1);
791 list_for_each_entry(t, &m->transfers, transfer_list) {
792 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
796 if (par_override || t->speed_hz || t->bits_per_word) {
798 status = omap2_mcspi_setup_transfer(spi, t);
801 if (!t->speed_hz && !t->bits_per_word)
806 omap2_mcspi_force_cs(spi, 1);
810 chconf = mcspi_cached_chconf0(spi);
811 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
812 if (t->tx_buf == NULL)
813 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
814 else if (t->rx_buf == NULL)
815 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
816 mcspi_write_chconf0(spi, chconf);
821 /* RX_ONLY mode needs dummy data in TX reg */
822 if (t->tx_buf == NULL)
823 __raw_writel(0, cs->base
826 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
827 count = omap2_mcspi_txrx_dma(spi, t);
829 count = omap2_mcspi_txrx_pio(spi, t);
830 m->actual_length += count;
832 if (count != t->len) {
839 udelay(t->delay_usecs);
841 /* ignore the "leave it on after last xfer" hint */
843 omap2_mcspi_force_cs(spi, 0);
848 /* Restore defaults if they were overriden */
851 status = omap2_mcspi_setup_transfer(spi, NULL);
855 omap2_mcspi_force_cs(spi, 0);
857 omap2_mcspi_set_enable(spi, 0);
860 m->complete(m->context);
862 spin_lock_irq(&mcspi->lock);
865 omap2_mcspi_disable_clocks(mcspi);
868 spin_unlock_irq(&mcspi->lock);
871 static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
873 struct omap2_mcspi *mcspi;
875 struct spi_transfer *t;
877 m->actual_length = 0;
880 /* reject invalid messages and transfers */
881 if (list_empty(&m->transfers) || !m->complete)
883 list_for_each_entry(t, &m->transfers, transfer_list) {
884 const void *tx_buf = t->tx_buf;
885 void *rx_buf = t->rx_buf;
886 unsigned len = t->len;
888 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
889 || (len && !(rx_buf || tx_buf))
890 || (t->bits_per_word &&
891 ( t->bits_per_word < 4
892 || t->bits_per_word > 32))) {
893 dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
901 if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
902 dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
904 OMAP2_MCSPI_MAX_FREQ/(1<<16));
908 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
911 /* Do DMA mapping "early" for better error reporting and
912 * dcache use. Note that if dma_unmap_single() ever starts
913 * to do real work on ARM, we'd need to clean up mappings
914 * for previous transfers on *ALL* exits of this loop...
916 if (tx_buf != NULL) {
917 t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
919 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
920 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
925 if (rx_buf != NULL) {
926 t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
928 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
929 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
932 dma_unmap_single(NULL, t->tx_dma,
939 mcspi = spi_master_get_devdata(spi->master);
941 spin_lock_irqsave(&mcspi->lock, flags);
942 list_add_tail(&m->queue, &mcspi->msg_queue);
943 queue_work(omap2_mcspi_wq, &mcspi->work);
944 spin_unlock_irqrestore(&mcspi->lock, flags);
949 static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
951 struct spi_master *master = mcspi->master;
954 if (omap2_mcspi_enable_clocks(mcspi))
957 mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
958 OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
960 tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
961 } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
963 tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
964 OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
965 OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
966 mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
967 omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
969 tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
970 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
971 omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
973 omap2_mcspi_set_master_mode(master);
974 omap2_mcspi_disable_clocks(mcspi);
978 static u8 __initdata spi1_rxdma_id [] = {
979 OMAP24XX_DMA_SPI1_RX0,
980 OMAP24XX_DMA_SPI1_RX1,
981 OMAP24XX_DMA_SPI1_RX2,
982 OMAP24XX_DMA_SPI1_RX3,
985 static u8 __initdata spi1_txdma_id [] = {
986 OMAP24XX_DMA_SPI1_TX0,
987 OMAP24XX_DMA_SPI1_TX1,
988 OMAP24XX_DMA_SPI1_TX2,
989 OMAP24XX_DMA_SPI1_TX3,
992 static u8 __initdata spi2_rxdma_id[] = {
993 OMAP24XX_DMA_SPI2_RX0,
994 OMAP24XX_DMA_SPI2_RX1,
997 static u8 __initdata spi2_txdma_id[] = {
998 OMAP24XX_DMA_SPI2_TX0,
999 OMAP24XX_DMA_SPI2_TX1,
1002 #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
1003 static u8 __initdata spi3_rxdma_id[] = {
1004 OMAP24XX_DMA_SPI3_RX0,
1005 OMAP24XX_DMA_SPI3_RX1,
1008 static u8 __initdata spi3_txdma_id[] = {
1009 OMAP24XX_DMA_SPI3_TX0,
1010 OMAP24XX_DMA_SPI3_TX1,
1014 #ifdef CONFIG_ARCH_OMAP3
1015 static u8 __initdata spi4_rxdma_id[] = {
1016 OMAP34XX_DMA_SPI4_RX0,
1019 static u8 __initdata spi4_txdma_id[] = {
1020 OMAP34XX_DMA_SPI4_TX0,
1024 static int __init omap2_mcspi_probe(struct platform_device *pdev)
1026 struct spi_master *master;
1027 struct omap2_mcspi *mcspi;
1030 const u8 *rxdma_id, *txdma_id;
1031 unsigned num_chipselect;
1035 rxdma_id = spi1_rxdma_id;
1036 txdma_id = spi1_txdma_id;
1040 rxdma_id = spi2_rxdma_id;
1041 txdma_id = spi2_txdma_id;
1044 #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
1046 rxdma_id = spi3_rxdma_id;
1047 txdma_id = spi3_txdma_id;
1051 #ifdef CONFIG_ARCH_OMAP3
1053 rxdma_id = spi4_rxdma_id;
1054 txdma_id = spi4_txdma_id;
1062 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1063 if (master == NULL) {
1064 dev_dbg(&pdev->dev, "master allocation failed\n");
1068 /* the spi->mode bits understood by this driver: */
1069 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1072 master->bus_num = pdev->id;
1074 master->setup = omap2_mcspi_setup;
1075 master->transfer = omap2_mcspi_transfer;
1076 master->cleanup = omap2_mcspi_cleanup;
1077 master->num_chipselect = num_chipselect;
1079 dev_set_drvdata(&pdev->dev, master);
1081 mcspi = spi_master_get_devdata(master);
1082 mcspi->master = master;
1084 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1089 if (!request_mem_region(r->start, (r->end - r->start) + 1,
1090 dev_name(&pdev->dev))) {
1095 mcspi->phys = r->start;
1096 mcspi->base = ioremap(r->start, r->end - r->start + 1);
1098 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1103 INIT_WORK(&mcspi->work, omap2_mcspi_work);
1105 spin_lock_init(&mcspi->lock);
1106 INIT_LIST_HEAD(&mcspi->msg_queue);
1108 mcspi->ick = clk_get(&pdev->dev, "ick");
1109 if (IS_ERR(mcspi->ick)) {
1110 dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
1111 status = PTR_ERR(mcspi->ick);
1114 mcspi->fck = clk_get(&pdev->dev, "fck");
1115 if (IS_ERR(mcspi->fck)) {
1116 dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
1117 status = PTR_ERR(mcspi->fck);
1121 mcspi->dma_channels = kcalloc(master->num_chipselect,
1122 sizeof(struct omap2_mcspi_dma),
1125 if (mcspi->dma_channels == NULL)
1128 for (i = 0; i < num_chipselect; i++) {
1129 mcspi->dma_channels[i].dma_rx_channel = -1;
1130 mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
1131 mcspi->dma_channels[i].dma_tx_channel = -1;
1132 mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
1135 if (omap2_mcspi_reset(mcspi) < 0)
1138 status = spi_register_master(master);
1145 kfree(mcspi->dma_channels);
1147 clk_put(mcspi->fck);
1149 clk_put(mcspi->ick);
1151 iounmap(mcspi->base);
1153 release_mem_region(r->start, (r->end - r->start) + 1);
1155 spi_master_put(master);
1159 static int __exit omap2_mcspi_remove(struct platform_device *pdev)
1161 struct spi_master *master;
1162 struct omap2_mcspi *mcspi;
1163 struct omap2_mcspi_dma *dma_channels;
1167 master = dev_get_drvdata(&pdev->dev);
1168 mcspi = spi_master_get_devdata(master);
1169 dma_channels = mcspi->dma_channels;
1171 clk_put(mcspi->fck);
1172 clk_put(mcspi->ick);
1174 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1175 release_mem_region(r->start, (r->end - r->start) + 1);
1178 spi_unregister_master(master);
1180 kfree(dma_channels);
1185 /* work with hotplug and coldplug */
1186 MODULE_ALIAS("platform:omap2_mcspi");
1188 static struct platform_driver omap2_mcspi_driver = {
1190 .name = "omap2_mcspi",
1191 .owner = THIS_MODULE,
1193 .remove = __exit_p(omap2_mcspi_remove),
1197 static int __init omap2_mcspi_init(void)
1199 omap2_mcspi_wq = create_singlethread_workqueue(
1200 omap2_mcspi_driver.driver.name);
1201 if (omap2_mcspi_wq == NULL)
1203 return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
1205 subsys_initcall(omap2_mcspi_init);
1207 static void __exit omap2_mcspi_exit(void)
1209 platform_driver_unregister(&omap2_mcspi_driver);
1211 destroy_workqueue(omap2_mcspi_wq);
1213 module_exit(omap2_mcspi_exit);
1215 MODULE_LICENSE("GPL");