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 /* the spi->mode bits understood by this driver: */
610 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
612 static int omap2_mcspi_setup(struct spi_device
*spi
)
615 struct omap2_mcspi
*mcspi
;
616 struct omap2_mcspi_dma
*mcspi_dma
;
617 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
619 if (spi
->mode
& ~MODEBITS
) {
620 dev_dbg(&spi
->dev
, "setup: unsupported mode bits %x\n",
621 spi
->mode
& ~MODEBITS
);
625 if (spi
->bits_per_word
== 0)
626 spi
->bits_per_word
= 8;
627 else if (spi
->bits_per_word
< 4 || spi
->bits_per_word
> 32) {
628 dev_dbg(&spi
->dev
, "setup: unsupported %d bit words\n",
633 mcspi
= spi_master_get_devdata(spi
->master
);
634 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
637 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
640 cs
->base
= mcspi
->base
+ spi
->chip_select
* 0x14;
641 cs
->phys
= mcspi
->phys
+ spi
->chip_select
* 0x14;
642 spi
->controller_state
= cs
;
645 if (mcspi_dma
->dma_rx_channel
== -1
646 || mcspi_dma
->dma_tx_channel
== -1) {
647 ret
= omap2_mcspi_request_dma(spi
);
652 clk_enable(mcspi
->ick
);
653 clk_enable(mcspi
->fck
);
654 ret
= omap2_mcspi_setup_transfer(spi
, NULL
);
655 clk_disable(mcspi
->fck
);
656 clk_disable(mcspi
->ick
);
661 static void omap2_mcspi_cleanup(struct spi_device
*spi
)
663 struct omap2_mcspi
*mcspi
;
664 struct omap2_mcspi_dma
*mcspi_dma
;
666 mcspi
= spi_master_get_devdata(spi
->master
);
667 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
669 kfree(spi
->controller_state
);
671 if (mcspi_dma
->dma_rx_channel
!= -1) {
672 omap_free_dma(mcspi_dma
->dma_rx_channel
);
673 mcspi_dma
->dma_rx_channel
= -1;
675 if (mcspi_dma
->dma_tx_channel
!= -1) {
676 omap_free_dma(mcspi_dma
->dma_tx_channel
);
677 mcspi_dma
->dma_tx_channel
= -1;
681 static void omap2_mcspi_work(struct work_struct
*work
)
683 struct omap2_mcspi
*mcspi
;
685 mcspi
= container_of(work
, struct omap2_mcspi
, work
);
686 spin_lock_irq(&mcspi
->lock
);
688 clk_enable(mcspi
->ick
);
689 clk_enable(mcspi
->fck
);
691 /* We only enable one channel at a time -- the one whose message is
692 * at the head of the queue -- although this controller would gladly
693 * arbitrate among multiple channels. This corresponds to "single
694 * channel" master mode. As a side effect, we need to manage the
695 * chipselect with the FORCE bit ... CS != channel enable.
697 while (!list_empty(&mcspi
->msg_queue
)) {
698 struct spi_message
*m
;
699 struct spi_device
*spi
;
700 struct spi_transfer
*t
= NULL
;
702 struct omap2_mcspi_cs
*cs
;
703 int par_override
= 0;
707 m
= container_of(mcspi
->msg_queue
.next
, struct spi_message
,
710 list_del_init(&m
->queue
);
711 spin_unlock_irq(&mcspi
->lock
);
714 cs
= spi
->controller_state
;
716 omap2_mcspi_set_enable(spi
, 1);
717 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
718 if (t
->tx_buf
== NULL
&& t
->rx_buf
== NULL
&& t
->len
) {
722 if (par_override
|| t
->speed_hz
|| t
->bits_per_word
) {
724 status
= omap2_mcspi_setup_transfer(spi
, t
);
727 if (!t
->speed_hz
&& !t
->bits_per_word
)
732 omap2_mcspi_force_cs(spi
, 1);
736 chconf
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
);
737 chconf
&= ~OMAP2_MCSPI_CHCONF_TRM_MASK
;
738 if (t
->tx_buf
== NULL
)
739 chconf
|= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY
;
740 else if (t
->rx_buf
== NULL
)
741 chconf
|= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY
;
742 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
, chconf
);
747 /* RX_ONLY mode needs dummy data in TX reg */
748 if (t
->tx_buf
== NULL
)
749 __raw_writel(0, cs
->base
752 if (m
->is_dma_mapped
|| t
->len
>= DMA_MIN_BYTES
)
753 count
= omap2_mcspi_txrx_dma(spi
, t
);
755 count
= omap2_mcspi_txrx_pio(spi
, t
);
756 m
->actual_length
+= count
;
758 if (count
!= t
->len
) {
765 udelay(t
->delay_usecs
);
767 /* ignore the "leave it on after last xfer" hint */
769 omap2_mcspi_force_cs(spi
, 0);
774 /* Restore defaults if they were overriden */
777 status
= omap2_mcspi_setup_transfer(spi
, NULL
);
781 omap2_mcspi_force_cs(spi
, 0);
783 omap2_mcspi_set_enable(spi
, 0);
786 m
->complete(m
->context
);
788 spin_lock_irq(&mcspi
->lock
);
791 clk_disable(mcspi
->fck
);
792 clk_disable(mcspi
->ick
);
794 spin_unlock_irq(&mcspi
->lock
);
797 static int omap2_mcspi_transfer(struct spi_device
*spi
, struct spi_message
*m
)
799 struct omap2_mcspi
*mcspi
;
801 struct spi_transfer
*t
;
803 m
->actual_length
= 0;
806 /* reject invalid messages and transfers */
807 if (list_empty(&m
->transfers
) || !m
->complete
)
809 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
810 const void *tx_buf
= t
->tx_buf
;
811 void *rx_buf
= t
->rx_buf
;
812 unsigned len
= t
->len
;
814 if (t
->speed_hz
> OMAP2_MCSPI_MAX_FREQ
815 || (len
&& !(rx_buf
|| tx_buf
))
816 || (t
->bits_per_word
&&
817 ( t
->bits_per_word
< 4
818 || t
->bits_per_word
> 32))) {
819 dev_dbg(&spi
->dev
, "transfer: %d Hz, %d %s%s, %d bpw\n",
827 if (t
->speed_hz
&& t
->speed_hz
< OMAP2_MCSPI_MAX_FREQ
/(1<<16)) {
828 dev_dbg(&spi
->dev
, "%d Hz max exceeds %d\n",
830 OMAP2_MCSPI_MAX_FREQ
/(1<<16));
834 if (m
->is_dma_mapped
|| len
< DMA_MIN_BYTES
)
837 /* Do DMA mapping "early" for better error reporting and
838 * dcache use. Note that if dma_unmap_single() ever starts
839 * to do real work on ARM, we'd need to clean up mappings
840 * for previous transfers on *ALL* exits of this loop...
842 if (tx_buf
!= NULL
) {
843 t
->tx_dma
= dma_map_single(&spi
->dev
, (void *) tx_buf
,
845 if (dma_mapping_error(&spi
->dev
, t
->tx_dma
)) {
846 dev_dbg(&spi
->dev
, "dma %cX %d bytes error\n",
851 if (rx_buf
!= NULL
) {
852 t
->rx_dma
= dma_map_single(&spi
->dev
, rx_buf
, t
->len
,
854 if (dma_mapping_error(&spi
->dev
, t
->rx_dma
)) {
855 dev_dbg(&spi
->dev
, "dma %cX %d bytes error\n",
858 dma_unmap_single(NULL
, t
->tx_dma
,
865 mcspi
= spi_master_get_devdata(spi
->master
);
867 spin_lock_irqsave(&mcspi
->lock
, flags
);
868 list_add_tail(&m
->queue
, &mcspi
->msg_queue
);
869 queue_work(omap2_mcspi_wq
, &mcspi
->work
);
870 spin_unlock_irqrestore(&mcspi
->lock
, flags
);
875 static int __init
omap2_mcspi_reset(struct omap2_mcspi
*mcspi
)
877 struct spi_master
*master
= mcspi
->master
;
880 clk_enable(mcspi
->ick
);
881 clk_enable(mcspi
->fck
);
883 mcspi_write_reg(master
, OMAP2_MCSPI_SYSCONFIG
,
884 OMAP2_MCSPI_SYSCONFIG_SOFTRESET
);
886 tmp
= mcspi_read_reg(master
, OMAP2_MCSPI_SYSSTATUS
);
887 } while (!(tmp
& OMAP2_MCSPI_SYSSTATUS_RESETDONE
));
889 mcspi_write_reg(master
, OMAP2_MCSPI_SYSCONFIG
,
890 OMAP2_MCSPI_SYSCONFIG_AUTOIDLE
|
891 OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP
|
892 OMAP2_MCSPI_SYSCONFIG_SMARTIDLE
);
894 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
,
895 OMAP2_MCSPI_WAKEUPENABLE_WKEN
);
897 omap2_mcspi_set_master_mode(master
);
899 clk_disable(mcspi
->fck
);
900 clk_disable(mcspi
->ick
);
904 static u8 __initdata spi1_rxdma_id
[] = {
905 OMAP24XX_DMA_SPI1_RX0
,
906 OMAP24XX_DMA_SPI1_RX1
,
907 OMAP24XX_DMA_SPI1_RX2
,
908 OMAP24XX_DMA_SPI1_RX3
,
911 static u8 __initdata spi1_txdma_id
[] = {
912 OMAP24XX_DMA_SPI1_TX0
,
913 OMAP24XX_DMA_SPI1_TX1
,
914 OMAP24XX_DMA_SPI1_TX2
,
915 OMAP24XX_DMA_SPI1_TX3
,
918 static u8 __initdata spi2_rxdma_id
[] = {
919 OMAP24XX_DMA_SPI2_RX0
,
920 OMAP24XX_DMA_SPI2_RX1
,
923 static u8 __initdata spi2_txdma_id
[] = {
924 OMAP24XX_DMA_SPI2_TX0
,
925 OMAP24XX_DMA_SPI2_TX1
,
928 #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
929 static u8 __initdata spi3_rxdma_id
[] = {
930 OMAP24XX_DMA_SPI3_RX0
,
931 OMAP24XX_DMA_SPI3_RX1
,
934 static u8 __initdata spi3_txdma_id
[] = {
935 OMAP24XX_DMA_SPI3_TX0
,
936 OMAP24XX_DMA_SPI3_TX1
,
940 #ifdef CONFIG_ARCH_OMAP3
941 static u8 __initdata spi4_rxdma_id
[] = {
942 OMAP34XX_DMA_SPI4_RX0
,
945 static u8 __initdata spi4_txdma_id
[] = {
946 OMAP34XX_DMA_SPI4_TX0
,
950 static int __init
omap2_mcspi_probe(struct platform_device
*pdev
)
952 struct spi_master
*master
;
953 struct omap2_mcspi
*mcspi
;
956 const u8
*rxdma_id
, *txdma_id
;
957 unsigned num_chipselect
;
961 rxdma_id
= spi1_rxdma_id
;
962 txdma_id
= spi1_txdma_id
;
966 rxdma_id
= spi2_rxdma_id
;
967 txdma_id
= spi2_txdma_id
;
970 #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
972 rxdma_id
= spi3_rxdma_id
;
973 txdma_id
= spi3_txdma_id
;
977 #ifdef CONFIG_ARCH_OMAP3
979 rxdma_id
= spi4_rxdma_id
;
980 txdma_id
= spi4_txdma_id
;
988 master
= spi_alloc_master(&pdev
->dev
, sizeof *mcspi
);
989 if (master
== NULL
) {
990 dev_dbg(&pdev
->dev
, "master allocation failed\n");
995 master
->bus_num
= pdev
->id
;
997 master
->setup
= omap2_mcspi_setup
;
998 master
->transfer
= omap2_mcspi_transfer
;
999 master
->cleanup
= omap2_mcspi_cleanup
;
1000 master
->num_chipselect
= num_chipselect
;
1002 dev_set_drvdata(&pdev
->dev
, master
);
1004 mcspi
= spi_master_get_devdata(master
);
1005 mcspi
->master
= master
;
1007 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1012 if (!request_mem_region(r
->start
, (r
->end
- r
->start
) + 1,
1013 pdev
->dev
.bus_id
)) {
1018 mcspi
->phys
= r
->start
;
1019 mcspi
->base
= ioremap(r
->start
, r
->end
- r
->start
+ 1);
1021 dev_dbg(&pdev
->dev
, "can't ioremap MCSPI\n");
1026 INIT_WORK(&mcspi
->work
, omap2_mcspi_work
);
1028 spin_lock_init(&mcspi
->lock
);
1029 INIT_LIST_HEAD(&mcspi
->msg_queue
);
1031 mcspi
->ick
= clk_get(&pdev
->dev
, "mcspi_ick");
1032 if (IS_ERR(mcspi
->ick
)) {
1033 dev_dbg(&pdev
->dev
, "can't get mcspi_ick\n");
1034 status
= PTR_ERR(mcspi
->ick
);
1037 mcspi
->fck
= clk_get(&pdev
->dev
, "mcspi_fck");
1038 if (IS_ERR(mcspi
->fck
)) {
1039 dev_dbg(&pdev
->dev
, "can't get mcspi_fck\n");
1040 status
= PTR_ERR(mcspi
->fck
);
1044 mcspi
->dma_channels
= kcalloc(master
->num_chipselect
,
1045 sizeof(struct omap2_mcspi_dma
),
1048 if (mcspi
->dma_channels
== NULL
)
1051 for (i
= 0; i
< num_chipselect
; i
++) {
1052 mcspi
->dma_channels
[i
].dma_rx_channel
= -1;
1053 mcspi
->dma_channels
[i
].dma_rx_sync_dev
= rxdma_id
[i
];
1054 mcspi
->dma_channels
[i
].dma_tx_channel
= -1;
1055 mcspi
->dma_channels
[i
].dma_tx_sync_dev
= txdma_id
[i
];
1058 if (omap2_mcspi_reset(mcspi
) < 0)
1061 status
= spi_register_master(master
);
1068 kfree(mcspi
->dma_channels
);
1070 clk_put(mcspi
->fck
);
1072 clk_put(mcspi
->ick
);
1074 iounmap(mcspi
->base
);
1076 release_mem_region(r
->start
, (r
->end
- r
->start
) + 1);
1078 spi_master_put(master
);
1082 static int __exit
omap2_mcspi_remove(struct platform_device
*pdev
)
1084 struct spi_master
*master
;
1085 struct omap2_mcspi
*mcspi
;
1086 struct omap2_mcspi_dma
*dma_channels
;
1090 master
= dev_get_drvdata(&pdev
->dev
);
1091 mcspi
= spi_master_get_devdata(master
);
1092 dma_channels
= mcspi
->dma_channels
;
1094 clk_put(mcspi
->fck
);
1095 clk_put(mcspi
->ick
);
1097 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1098 release_mem_region(r
->start
, (r
->end
- r
->start
) + 1);
1101 spi_unregister_master(master
);
1103 kfree(dma_channels
);
1108 /* work with hotplug and coldplug */
1109 MODULE_ALIAS("platform:omap2_mcspi");
1111 static struct platform_driver omap2_mcspi_driver
= {
1113 .name
= "omap2_mcspi",
1114 .owner
= THIS_MODULE
,
1116 .remove
= __exit_p(omap2_mcspi_remove
),
1120 static int __init
omap2_mcspi_init(void)
1122 omap2_mcspi_wq
= create_singlethread_workqueue(
1123 omap2_mcspi_driver
.driver
.name
);
1124 if (omap2_mcspi_wq
== NULL
)
1126 return platform_driver_probe(&omap2_mcspi_driver
, omap2_mcspi_probe
);
1128 subsys_initcall(omap2_mcspi_init
);
1130 static void __exit
omap2_mcspi_exit(void)
1132 platform_driver_unregister(&omap2_mcspi_driver
);
1134 destroy_workqueue(omap2_mcspi_wq
);
1136 module_exit(omap2_mcspi_exit
);
1138 MODULE_LICENSE("GPL");