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
;
124 struct workqueue_struct
*wq
;
127 struct omap2_mcspi_cs
{
131 struct list_head node
;
132 /* Context save and restore shadow register */
136 /* used for context save and restore, structure members to be updated whenever
137 * corresponding registers are modified.
139 struct omap2_mcspi_regs
{
145 static struct omap2_mcspi_regs omap2_mcspi_ctx
[OMAP2_MCSPI_MAX_CTRL
];
147 #define MOD_REG_BIT(val, mask, set) do { \
154 static inline void mcspi_write_reg(struct spi_master
*master
,
157 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
159 __raw_writel(val
, mcspi
->base
+ idx
);
162 static inline u32
mcspi_read_reg(struct spi_master
*master
, int idx
)
164 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
166 return __raw_readl(mcspi
->base
+ idx
);
169 static inline void mcspi_write_cs_reg(const struct spi_device
*spi
,
172 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
174 __raw_writel(val
, cs
->base
+ idx
);
177 static inline u32
mcspi_read_cs_reg(const struct spi_device
*spi
, int idx
)
179 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
181 return __raw_readl(cs
->base
+ idx
);
184 static inline u32
mcspi_cached_chconf0(const struct spi_device
*spi
)
186 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
191 static inline void mcspi_write_chconf0(const struct spi_device
*spi
, u32 val
)
193 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
196 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
, val
);
197 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
);
200 static void omap2_mcspi_set_dma_req(const struct spi_device
*spi
,
201 int is_read
, int enable
)
205 l
= mcspi_cached_chconf0(spi
);
207 if (is_read
) /* 1 is read, 0 write */
208 rw
= OMAP2_MCSPI_CHCONF_DMAR
;
210 rw
= OMAP2_MCSPI_CHCONF_DMAW
;
212 MOD_REG_BIT(l
, rw
, enable
);
213 mcspi_write_chconf0(spi
, l
);
216 static void omap2_mcspi_set_enable(const struct spi_device
*spi
, int enable
)
220 l
= enable
? OMAP2_MCSPI_CHCTRL_EN
: 0;
221 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, l
);
222 /* Flash post-writes */
223 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
);
226 static void omap2_mcspi_force_cs(struct spi_device
*spi
, int cs_active
)
230 l
= mcspi_cached_chconf0(spi
);
231 MOD_REG_BIT(l
, OMAP2_MCSPI_CHCONF_FORCE
, cs_active
);
232 mcspi_write_chconf0(spi
, l
);
235 static void omap2_mcspi_set_master_mode(struct spi_master
*master
)
239 /* setup when switching from (reset default) slave mode
240 * to single-channel master mode
242 l
= mcspi_read_reg(master
, OMAP2_MCSPI_MODULCTRL
);
243 MOD_REG_BIT(l
, OMAP2_MCSPI_MODULCTRL_STEST
, 0);
244 MOD_REG_BIT(l
, OMAP2_MCSPI_MODULCTRL_MS
, 0);
245 MOD_REG_BIT(l
, OMAP2_MCSPI_MODULCTRL_SINGLE
, 1);
246 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, l
);
248 omap2_mcspi_ctx
[master
->bus_num
- 1].modulctrl
= l
;
251 static void omap2_mcspi_restore_ctx(struct omap2_mcspi
*mcspi
)
253 struct spi_master
*spi_cntrl
;
254 struct omap2_mcspi_cs
*cs
;
255 spi_cntrl
= mcspi
->master
;
257 /* McSPI: context restore */
258 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_MODULCTRL
,
259 omap2_mcspi_ctx
[spi_cntrl
->bus_num
- 1].modulctrl
);
261 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_WAKEUPENABLE
,
262 omap2_mcspi_ctx
[spi_cntrl
->bus_num
- 1].wakeupenable
);
264 list_for_each_entry(cs
, &omap2_mcspi_ctx
[spi_cntrl
->bus_num
- 1].cs
,
266 __raw_writel(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
268 static void omap2_mcspi_disable_clocks(struct omap2_mcspi
*mcspi
)
270 pm_runtime_put_sync(mcspi
->dev
);
273 static int omap2_mcspi_enable_clocks(struct omap2_mcspi
*mcspi
)
275 return pm_runtime_get_sync(mcspi
->dev
);
278 static int mcspi_wait_for_reg_bit(void __iomem
*reg
, unsigned long bit
)
280 unsigned long timeout
;
282 timeout
= jiffies
+ msecs_to_jiffies(1000);
283 while (!(__raw_readl(reg
) & bit
)) {
284 if (time_after(jiffies
, timeout
))
292 omap2_mcspi_txrx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
)
294 struct omap2_mcspi
*mcspi
;
295 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
296 struct omap2_mcspi_dma
*mcspi_dma
;
297 unsigned int count
, c
;
298 unsigned long base
, tx_reg
, rx_reg
;
299 int word_len
, data_type
, element_count
;
304 void __iomem
*chstat_reg
;
306 mcspi
= spi_master_get_devdata(spi
->master
);
307 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
308 l
= mcspi_cached_chconf0(spi
);
310 chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
314 word_len
= cs
->word_len
;
317 tx_reg
= base
+ OMAP2_MCSPI_TX0
;
318 rx_reg
= base
+ OMAP2_MCSPI_RX0
;
323 data_type
= OMAP_DMA_DATA_TYPE_S8
;
324 element_count
= count
;
325 } else if (word_len
<= 16) {
326 data_type
= OMAP_DMA_DATA_TYPE_S16
;
327 element_count
= count
>> 1;
328 } else /* word_len <= 32 */ {
329 data_type
= OMAP_DMA_DATA_TYPE_S32
;
330 element_count
= count
>> 2;
334 omap_set_dma_transfer_params(mcspi_dma
->dma_tx_channel
,
335 data_type
, element_count
, 1,
336 OMAP_DMA_SYNC_ELEMENT
,
337 mcspi_dma
->dma_tx_sync_dev
, 0);
339 omap_set_dma_dest_params(mcspi_dma
->dma_tx_channel
, 0,
340 OMAP_DMA_AMODE_CONSTANT
,
343 omap_set_dma_src_params(mcspi_dma
->dma_tx_channel
, 0,
344 OMAP_DMA_AMODE_POST_INC
,
349 elements
= element_count
- 1;
350 if (l
& OMAP2_MCSPI_CHCONF_TURBO
)
353 omap_set_dma_transfer_params(mcspi_dma
->dma_rx_channel
,
354 data_type
, elements
, 1,
355 OMAP_DMA_SYNC_ELEMENT
,
356 mcspi_dma
->dma_rx_sync_dev
, 1);
358 omap_set_dma_src_params(mcspi_dma
->dma_rx_channel
, 0,
359 OMAP_DMA_AMODE_CONSTANT
,
362 omap_set_dma_dest_params(mcspi_dma
->dma_rx_channel
, 0,
363 OMAP_DMA_AMODE_POST_INC
,
368 omap_start_dma(mcspi_dma
->dma_tx_channel
);
369 omap2_mcspi_set_dma_req(spi
, 0, 1);
373 omap_start_dma(mcspi_dma
->dma_rx_channel
);
374 omap2_mcspi_set_dma_req(spi
, 1, 1);
378 wait_for_completion(&mcspi_dma
->dma_tx_completion
);
379 dma_unmap_single(&spi
->dev
, xfer
->tx_dma
, count
, DMA_TO_DEVICE
);
381 /* for TX_ONLY mode, be sure all words have shifted out */
383 if (mcspi_wait_for_reg_bit(chstat_reg
,
384 OMAP2_MCSPI_CHSTAT_TXS
) < 0)
385 dev_err(&spi
->dev
, "TXS timed out\n");
386 else if (mcspi_wait_for_reg_bit(chstat_reg
,
387 OMAP2_MCSPI_CHSTAT_EOT
) < 0)
388 dev_err(&spi
->dev
, "EOT timed out\n");
393 wait_for_completion(&mcspi_dma
->dma_rx_completion
);
394 dma_unmap_single(&spi
->dev
, xfer
->rx_dma
, count
, DMA_FROM_DEVICE
);
395 omap2_mcspi_set_enable(spi
, 0);
397 if (l
& OMAP2_MCSPI_CHCONF_TURBO
) {
399 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
400 & OMAP2_MCSPI_CHSTAT_RXS
)) {
403 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
405 ((u8
*)xfer
->rx_buf
)[elements
++] = w
;
406 else if (word_len
<= 16)
407 ((u16
*)xfer
->rx_buf
)[elements
++] = w
;
408 else /* word_len <= 32 */
409 ((u32
*)xfer
->rx_buf
)[elements
++] = w
;
412 "DMA RX penultimate word empty");
413 count
-= (word_len
<= 8) ? 2 :
414 (word_len
<= 16) ? 4 :
415 /* word_len <= 32 */ 8;
416 omap2_mcspi_set_enable(spi
, 1);
421 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
422 & OMAP2_MCSPI_CHSTAT_RXS
)) {
425 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
427 ((u8
*)xfer
->rx_buf
)[elements
] = w
;
428 else if (word_len
<= 16)
429 ((u16
*)xfer
->rx_buf
)[elements
] = w
;
430 else /* word_len <= 32 */
431 ((u32
*)xfer
->rx_buf
)[elements
] = w
;
433 dev_err(&spi
->dev
, "DMA RX last word empty");
434 count
-= (word_len
<= 8) ? 1 :
435 (word_len
<= 16) ? 2 :
436 /* word_len <= 32 */ 4;
438 omap2_mcspi_set_enable(spi
, 1);
444 omap2_mcspi_txrx_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
)
446 struct omap2_mcspi
*mcspi
;
447 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
448 unsigned int count
, c
;
450 void __iomem
*base
= cs
->base
;
451 void __iomem
*tx_reg
;
452 void __iomem
*rx_reg
;
453 void __iomem
*chstat_reg
;
456 mcspi
= spi_master_get_devdata(spi
->master
);
459 word_len
= cs
->word_len
;
461 l
= mcspi_cached_chconf0(spi
);
463 /* We store the pre-calculated register addresses on stack to speed
464 * up the transfer loop. */
465 tx_reg
= base
+ OMAP2_MCSPI_TX0
;
466 rx_reg
= base
+ OMAP2_MCSPI_RX0
;
467 chstat_reg
= base
+ OMAP2_MCSPI_CHSTAT0
;
469 if (c
< (word_len
>>3))
482 if (mcspi_wait_for_reg_bit(chstat_reg
,
483 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
484 dev_err(&spi
->dev
, "TXS timed out\n");
487 dev_vdbg(&spi
->dev
, "write-%d %02x\n",
489 __raw_writel(*tx
++, tx_reg
);
492 if (mcspi_wait_for_reg_bit(chstat_reg
,
493 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
494 dev_err(&spi
->dev
, "RXS timed out\n");
498 if (c
== 1 && tx
== NULL
&&
499 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
500 omap2_mcspi_set_enable(spi
, 0);
501 *rx
++ = __raw_readl(rx_reg
);
502 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
503 word_len
, *(rx
- 1));
504 if (mcspi_wait_for_reg_bit(chstat_reg
,
505 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
511 } else if (c
== 0 && tx
== NULL
) {
512 omap2_mcspi_set_enable(spi
, 0);
515 *rx
++ = __raw_readl(rx_reg
);
516 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
517 word_len
, *(rx
- 1));
520 } else if (word_len
<= 16) {
529 if (mcspi_wait_for_reg_bit(chstat_reg
,
530 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
531 dev_err(&spi
->dev
, "TXS timed out\n");
534 dev_vdbg(&spi
->dev
, "write-%d %04x\n",
536 __raw_writel(*tx
++, tx_reg
);
539 if (mcspi_wait_for_reg_bit(chstat_reg
,
540 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
541 dev_err(&spi
->dev
, "RXS timed out\n");
545 if (c
== 2 && tx
== NULL
&&
546 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
547 omap2_mcspi_set_enable(spi
, 0);
548 *rx
++ = __raw_readl(rx_reg
);
549 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
550 word_len
, *(rx
- 1));
551 if (mcspi_wait_for_reg_bit(chstat_reg
,
552 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
558 } else if (c
== 0 && tx
== NULL
) {
559 omap2_mcspi_set_enable(spi
, 0);
562 *rx
++ = __raw_readl(rx_reg
);
563 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
564 word_len
, *(rx
- 1));
567 } else if (word_len
<= 32) {
576 if (mcspi_wait_for_reg_bit(chstat_reg
,
577 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
578 dev_err(&spi
->dev
, "TXS timed out\n");
581 dev_vdbg(&spi
->dev
, "write-%d %08x\n",
583 __raw_writel(*tx
++, tx_reg
);
586 if (mcspi_wait_for_reg_bit(chstat_reg
,
587 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
588 dev_err(&spi
->dev
, "RXS timed out\n");
592 if (c
== 4 && tx
== NULL
&&
593 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
594 omap2_mcspi_set_enable(spi
, 0);
595 *rx
++ = __raw_readl(rx_reg
);
596 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
597 word_len
, *(rx
- 1));
598 if (mcspi_wait_for_reg_bit(chstat_reg
,
599 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
605 } else if (c
== 0 && tx
== NULL
) {
606 omap2_mcspi_set_enable(spi
, 0);
609 *rx
++ = __raw_readl(rx_reg
);
610 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
611 word_len
, *(rx
- 1));
616 /* for TX_ONLY mode, be sure all words have shifted out */
617 if (xfer
->rx_buf
== NULL
) {
618 if (mcspi_wait_for_reg_bit(chstat_reg
,
619 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
620 dev_err(&spi
->dev
, "TXS timed out\n");
621 } else if (mcspi_wait_for_reg_bit(chstat_reg
,
622 OMAP2_MCSPI_CHSTAT_EOT
) < 0)
623 dev_err(&spi
->dev
, "EOT timed out\n");
625 /* disable chan to purge rx datas received in TX_ONLY transfer,
626 * otherwise these rx datas will affect the direct following
629 omap2_mcspi_set_enable(spi
, 0);
632 omap2_mcspi_set_enable(spi
, 1);
636 static u32
omap2_mcspi_calc_divisor(u32 speed_hz
)
640 for (div
= 0; div
< 15; div
++)
641 if (speed_hz
>= (OMAP2_MCSPI_MAX_FREQ
>> div
))
647 /* called only when no transfer is active to this device */
648 static int omap2_mcspi_setup_transfer(struct spi_device
*spi
,
649 struct spi_transfer
*t
)
651 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
652 struct omap2_mcspi
*mcspi
;
653 struct spi_master
*spi_cntrl
;
655 u8 word_len
= spi
->bits_per_word
;
656 u32 speed_hz
= spi
->max_speed_hz
;
658 mcspi
= spi_master_get_devdata(spi
->master
);
659 spi_cntrl
= mcspi
->master
;
661 if (t
!= NULL
&& t
->bits_per_word
)
662 word_len
= t
->bits_per_word
;
664 cs
->word_len
= word_len
;
666 if (t
&& t
->speed_hz
)
667 speed_hz
= t
->speed_hz
;
669 speed_hz
= min_t(u32
, speed_hz
, OMAP2_MCSPI_MAX_FREQ
);
670 div
= omap2_mcspi_calc_divisor(speed_hz
);
672 l
= mcspi_cached_chconf0(spi
);
674 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
675 * REVISIT: this controller could support SPI_3WIRE mode.
677 l
&= ~(OMAP2_MCSPI_CHCONF_IS
|OMAP2_MCSPI_CHCONF_DPE1
);
678 l
|= OMAP2_MCSPI_CHCONF_DPE0
;
681 l
&= ~OMAP2_MCSPI_CHCONF_WL_MASK
;
682 l
|= (word_len
- 1) << 7;
684 /* set chipselect polarity; manage with FORCE */
685 if (!(spi
->mode
& SPI_CS_HIGH
))
686 l
|= OMAP2_MCSPI_CHCONF_EPOL
; /* active-low; normal */
688 l
&= ~OMAP2_MCSPI_CHCONF_EPOL
;
690 /* set clock divisor */
691 l
&= ~OMAP2_MCSPI_CHCONF_CLKD_MASK
;
694 /* set SPI mode 0..3 */
695 if (spi
->mode
& SPI_CPOL
)
696 l
|= OMAP2_MCSPI_CHCONF_POL
;
698 l
&= ~OMAP2_MCSPI_CHCONF_POL
;
699 if (spi
->mode
& SPI_CPHA
)
700 l
|= OMAP2_MCSPI_CHCONF_PHA
;
702 l
&= ~OMAP2_MCSPI_CHCONF_PHA
;
704 mcspi_write_chconf0(spi
, l
);
706 dev_dbg(&spi
->dev
, "setup: speed %d, sample %s edge, clk %s\n",
707 OMAP2_MCSPI_MAX_FREQ
>> div
,
708 (spi
->mode
& SPI_CPHA
) ? "trailing" : "leading",
709 (spi
->mode
& SPI_CPOL
) ? "inverted" : "normal");
714 static void omap2_mcspi_dma_rx_callback(int lch
, u16 ch_status
, void *data
)
716 struct spi_device
*spi
= data
;
717 struct omap2_mcspi
*mcspi
;
718 struct omap2_mcspi_dma
*mcspi_dma
;
720 mcspi
= spi_master_get_devdata(spi
->master
);
721 mcspi_dma
= &(mcspi
->dma_channels
[spi
->chip_select
]);
723 complete(&mcspi_dma
->dma_rx_completion
);
725 /* We must disable the DMA RX request */
726 omap2_mcspi_set_dma_req(spi
, 1, 0);
729 static void omap2_mcspi_dma_tx_callback(int lch
, u16 ch_status
, void *data
)
731 struct spi_device
*spi
= data
;
732 struct omap2_mcspi
*mcspi
;
733 struct omap2_mcspi_dma
*mcspi_dma
;
735 mcspi
= spi_master_get_devdata(spi
->master
);
736 mcspi_dma
= &(mcspi
->dma_channels
[spi
->chip_select
]);
738 complete(&mcspi_dma
->dma_tx_completion
);
740 /* We must disable the DMA TX request */
741 omap2_mcspi_set_dma_req(spi
, 0, 0);
744 static int omap2_mcspi_request_dma(struct spi_device
*spi
)
746 struct spi_master
*master
= spi
->master
;
747 struct omap2_mcspi
*mcspi
;
748 struct omap2_mcspi_dma
*mcspi_dma
;
750 mcspi
= spi_master_get_devdata(master
);
751 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
753 if (omap_request_dma(mcspi_dma
->dma_rx_sync_dev
, "McSPI RX",
754 omap2_mcspi_dma_rx_callback
, spi
,
755 &mcspi_dma
->dma_rx_channel
)) {
756 dev_err(&spi
->dev
, "no RX DMA channel for McSPI\n");
760 if (omap_request_dma(mcspi_dma
->dma_tx_sync_dev
, "McSPI TX",
761 omap2_mcspi_dma_tx_callback
, spi
,
762 &mcspi_dma
->dma_tx_channel
)) {
763 omap_free_dma(mcspi_dma
->dma_rx_channel
);
764 mcspi_dma
->dma_rx_channel
= -1;
765 dev_err(&spi
->dev
, "no TX DMA channel for McSPI\n");
769 init_completion(&mcspi_dma
->dma_rx_completion
);
770 init_completion(&mcspi_dma
->dma_tx_completion
);
775 static int omap2_mcspi_setup(struct spi_device
*spi
)
778 struct omap2_mcspi
*mcspi
;
779 struct omap2_mcspi_dma
*mcspi_dma
;
780 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
782 if (spi
->bits_per_word
< 4 || spi
->bits_per_word
> 32) {
783 dev_dbg(&spi
->dev
, "setup: unsupported %d bit words\n",
788 mcspi
= spi_master_get_devdata(spi
->master
);
789 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
792 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
795 cs
->base
= mcspi
->base
+ spi
->chip_select
* 0x14;
796 cs
->phys
= mcspi
->phys
+ spi
->chip_select
* 0x14;
798 spi
->controller_state
= cs
;
799 /* Link this to context save list */
800 list_add_tail(&cs
->node
,
801 &omap2_mcspi_ctx
[mcspi
->master
->bus_num
- 1].cs
);
804 if (mcspi_dma
->dma_rx_channel
== -1
805 || mcspi_dma
->dma_tx_channel
== -1) {
806 ret
= omap2_mcspi_request_dma(spi
);
811 ret
= omap2_mcspi_enable_clocks(mcspi
);
815 ret
= omap2_mcspi_setup_transfer(spi
, NULL
);
816 omap2_mcspi_disable_clocks(mcspi
);
821 static void omap2_mcspi_cleanup(struct spi_device
*spi
)
823 struct omap2_mcspi
*mcspi
;
824 struct omap2_mcspi_dma
*mcspi_dma
;
825 struct omap2_mcspi_cs
*cs
;
827 mcspi
= spi_master_get_devdata(spi
->master
);
829 if (spi
->controller_state
) {
830 /* Unlink controller state from context save list */
831 cs
= spi
->controller_state
;
834 kfree(spi
->controller_state
);
837 if (spi
->chip_select
< spi
->master
->num_chipselect
) {
838 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
840 if (mcspi_dma
->dma_rx_channel
!= -1) {
841 omap_free_dma(mcspi_dma
->dma_rx_channel
);
842 mcspi_dma
->dma_rx_channel
= -1;
844 if (mcspi_dma
->dma_tx_channel
!= -1) {
845 omap_free_dma(mcspi_dma
->dma_tx_channel
);
846 mcspi_dma
->dma_tx_channel
= -1;
851 static void omap2_mcspi_work(struct work_struct
*work
)
853 struct omap2_mcspi
*mcspi
;
855 mcspi
= container_of(work
, struct omap2_mcspi
, work
);
857 if (omap2_mcspi_enable_clocks(mcspi
) < 0)
860 spin_lock_irq(&mcspi
->lock
);
862 /* We only enable one channel at a time -- the one whose message is
863 * at the head of the queue -- although this controller would gladly
864 * arbitrate among multiple channels. This corresponds to "single
865 * channel" master mode. As a side effect, we need to manage the
866 * chipselect with the FORCE bit ... CS != channel enable.
868 while (!list_empty(&mcspi
->msg_queue
)) {
869 struct spi_message
*m
;
870 struct spi_device
*spi
;
871 struct spi_transfer
*t
= NULL
;
873 struct omap2_mcspi_cs
*cs
;
874 struct omap2_mcspi_device_config
*cd
;
875 int par_override
= 0;
879 m
= container_of(mcspi
->msg_queue
.next
, struct spi_message
,
882 list_del_init(&m
->queue
);
883 spin_unlock_irq(&mcspi
->lock
);
886 cs
= spi
->controller_state
;
887 cd
= spi
->controller_data
;
889 omap2_mcspi_set_enable(spi
, 1);
890 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
891 if (t
->tx_buf
== NULL
&& t
->rx_buf
== NULL
&& t
->len
) {
895 if (par_override
|| t
->speed_hz
|| t
->bits_per_word
) {
897 status
= omap2_mcspi_setup_transfer(spi
, t
);
900 if (!t
->speed_hz
&& !t
->bits_per_word
)
905 omap2_mcspi_force_cs(spi
, 1);
909 chconf
= mcspi_cached_chconf0(spi
);
910 chconf
&= ~OMAP2_MCSPI_CHCONF_TRM_MASK
;
911 chconf
&= ~OMAP2_MCSPI_CHCONF_TURBO
;
913 if (t
->tx_buf
== NULL
)
914 chconf
|= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY
;
915 else if (t
->rx_buf
== NULL
)
916 chconf
|= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY
;
918 if (cd
&& cd
->turbo_mode
&& t
->tx_buf
== NULL
) {
919 /* Turbo mode is for more than one word */
920 if (t
->len
> ((cs
->word_len
+ 7) >> 3))
921 chconf
|= OMAP2_MCSPI_CHCONF_TURBO
;
924 mcspi_write_chconf0(spi
, chconf
);
929 /* RX_ONLY mode needs dummy data in TX reg */
930 if (t
->tx_buf
== NULL
)
931 __raw_writel(0, cs
->base
934 if (m
->is_dma_mapped
|| t
->len
>= DMA_MIN_BYTES
)
935 count
= omap2_mcspi_txrx_dma(spi
, t
);
937 count
= omap2_mcspi_txrx_pio(spi
, t
);
938 m
->actual_length
+= count
;
940 if (count
!= t
->len
) {
947 udelay(t
->delay_usecs
);
949 /* ignore the "leave it on after last xfer" hint */
951 omap2_mcspi_force_cs(spi
, 0);
956 /* Restore defaults if they were overriden */
959 status
= omap2_mcspi_setup_transfer(spi
, NULL
);
963 omap2_mcspi_force_cs(spi
, 0);
965 omap2_mcspi_set_enable(spi
, 0);
968 m
->complete(m
->context
);
970 spin_lock_irq(&mcspi
->lock
);
973 spin_unlock_irq(&mcspi
->lock
);
975 omap2_mcspi_disable_clocks(mcspi
);
978 static int omap2_mcspi_transfer(struct spi_device
*spi
, struct spi_message
*m
)
980 struct omap2_mcspi
*mcspi
;
982 struct spi_transfer
*t
;
984 m
->actual_length
= 0;
987 /* reject invalid messages and transfers */
988 if (list_empty(&m
->transfers
) || !m
->complete
)
990 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
991 const void *tx_buf
= t
->tx_buf
;
992 void *rx_buf
= t
->rx_buf
;
993 unsigned len
= t
->len
;
995 if (t
->speed_hz
> OMAP2_MCSPI_MAX_FREQ
996 || (len
&& !(rx_buf
|| tx_buf
))
997 || (t
->bits_per_word
&&
998 ( t
->bits_per_word
< 4
999 || t
->bits_per_word
> 32))) {
1000 dev_dbg(&spi
->dev
, "transfer: %d Hz, %d %s%s, %d bpw\n",
1008 if (t
->speed_hz
&& t
->speed_hz
< (OMAP2_MCSPI_MAX_FREQ
>> 15)) {
1009 dev_dbg(&spi
->dev
, "speed_hz %d below minimum %d Hz\n",
1011 OMAP2_MCSPI_MAX_FREQ
>> 15);
1015 if (m
->is_dma_mapped
|| len
< DMA_MIN_BYTES
)
1018 if (tx_buf
!= NULL
) {
1019 t
->tx_dma
= dma_map_single(&spi
->dev
, (void *) tx_buf
,
1020 len
, DMA_TO_DEVICE
);
1021 if (dma_mapping_error(&spi
->dev
, t
->tx_dma
)) {
1022 dev_dbg(&spi
->dev
, "dma %cX %d bytes error\n",
1027 if (rx_buf
!= NULL
) {
1028 t
->rx_dma
= dma_map_single(&spi
->dev
, rx_buf
, t
->len
,
1030 if (dma_mapping_error(&spi
->dev
, t
->rx_dma
)) {
1031 dev_dbg(&spi
->dev
, "dma %cX %d bytes error\n",
1034 dma_unmap_single(&spi
->dev
, t
->tx_dma
,
1035 len
, DMA_TO_DEVICE
);
1041 mcspi
= spi_master_get_devdata(spi
->master
);
1043 spin_lock_irqsave(&mcspi
->lock
, flags
);
1044 list_add_tail(&m
->queue
, &mcspi
->msg_queue
);
1045 queue_work(mcspi
->wq
, &mcspi
->work
);
1046 spin_unlock_irqrestore(&mcspi
->lock
, flags
);
1051 static int __init
omap2_mcspi_master_setup(struct omap2_mcspi
*mcspi
)
1053 struct spi_master
*master
= mcspi
->master
;
1057 ret
= omap2_mcspi_enable_clocks(mcspi
);
1061 tmp
= OMAP2_MCSPI_WAKEUPENABLE_WKEN
;
1062 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
, tmp
);
1063 omap2_mcspi_ctx
[master
->bus_num
- 1].wakeupenable
= tmp
;
1065 omap2_mcspi_set_master_mode(master
);
1066 omap2_mcspi_disable_clocks(mcspi
);
1070 static int omap_mcspi_runtime_resume(struct device
*dev
)
1072 struct omap2_mcspi
*mcspi
;
1073 struct spi_master
*master
;
1075 master
= dev_get_drvdata(dev
);
1076 mcspi
= spi_master_get_devdata(master
);
1077 omap2_mcspi_restore_ctx(mcspi
);
1083 static int __init
omap2_mcspi_probe(struct platform_device
*pdev
)
1085 struct spi_master
*master
;
1086 struct omap2_mcspi_platform_config
*pdata
= pdev
->dev
.platform_data
;
1087 struct omap2_mcspi
*mcspi
;
1092 master
= spi_alloc_master(&pdev
->dev
, sizeof *mcspi
);
1093 if (master
== NULL
) {
1094 dev_dbg(&pdev
->dev
, "master allocation failed\n");
1098 /* the spi->mode bits understood by this driver: */
1099 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1102 master
->bus_num
= pdev
->id
;
1104 master
->setup
= omap2_mcspi_setup
;
1105 master
->transfer
= omap2_mcspi_transfer
;
1106 master
->cleanup
= omap2_mcspi_cleanup
;
1107 master
->num_chipselect
= pdata
->num_cs
;
1109 dev_set_drvdata(&pdev
->dev
, master
);
1111 mcspi
= spi_master_get_devdata(master
);
1112 mcspi
->master
= master
;
1114 sprintf(wq_name
, "omap2_mcspi/%d", master
->bus_num
);
1115 mcspi
->wq
= alloc_workqueue(wq_name
, WQ_MEM_RECLAIM
, 1);
1116 if (mcspi
->wq
== NULL
) {
1121 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1127 r
->start
+= pdata
->regs_offset
;
1128 r
->end
+= pdata
->regs_offset
;
1129 mcspi
->phys
= r
->start
;
1130 if (!request_mem_region(r
->start
, resource_size(r
),
1131 dev_name(&pdev
->dev
))) {
1136 mcspi
->base
= ioremap(r
->start
, resource_size(r
));
1138 dev_dbg(&pdev
->dev
, "can't ioremap MCSPI\n");
1140 goto release_region
;
1143 mcspi
->dev
= &pdev
->dev
;
1144 INIT_WORK(&mcspi
->work
, omap2_mcspi_work
);
1146 spin_lock_init(&mcspi
->lock
);
1147 INIT_LIST_HEAD(&mcspi
->msg_queue
);
1148 INIT_LIST_HEAD(&omap2_mcspi_ctx
[master
->bus_num
- 1].cs
);
1150 mcspi
->dma_channels
= kcalloc(master
->num_chipselect
,
1151 sizeof(struct omap2_mcspi_dma
),
1154 if (mcspi
->dma_channels
== NULL
)
1157 for (i
= 0; i
< master
->num_chipselect
; i
++) {
1158 char dma_ch_name
[14];
1159 struct resource
*dma_res
;
1161 sprintf(dma_ch_name
, "rx%d", i
);
1162 dma_res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
,
1165 dev_dbg(&pdev
->dev
, "cannot get DMA RX channel\n");
1170 mcspi
->dma_channels
[i
].dma_rx_channel
= -1;
1171 mcspi
->dma_channels
[i
].dma_rx_sync_dev
= dma_res
->start
;
1172 sprintf(dma_ch_name
, "tx%d", i
);
1173 dma_res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
,
1176 dev_dbg(&pdev
->dev
, "cannot get DMA TX channel\n");
1181 mcspi
->dma_channels
[i
].dma_tx_channel
= -1;
1182 mcspi
->dma_channels
[i
].dma_tx_sync_dev
= dma_res
->start
;
1188 pm_runtime_enable(&pdev
->dev
);
1190 if (status
|| omap2_mcspi_master_setup(mcspi
) < 0)
1193 status
= spi_register_master(master
);
1195 goto err_spi_register
;
1200 spi_master_put(master
);
1202 pm_runtime_disable(&pdev
->dev
);
1204 kfree(mcspi
->dma_channels
);
1206 iounmap(mcspi
->base
);
1208 release_mem_region(r
->start
, resource_size(r
));
1211 platform_set_drvdata(pdev
, NULL
);
1215 static int __exit
omap2_mcspi_remove(struct platform_device
*pdev
)
1217 struct spi_master
*master
;
1218 struct omap2_mcspi
*mcspi
;
1219 struct omap2_mcspi_dma
*dma_channels
;
1223 master
= dev_get_drvdata(&pdev
->dev
);
1224 mcspi
= spi_master_get_devdata(master
);
1225 dma_channels
= mcspi
->dma_channels
;
1227 omap2_mcspi_disable_clocks(mcspi
);
1228 pm_runtime_disable(&pdev
->dev
);
1229 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1230 release_mem_region(r
->start
, resource_size(r
));
1233 spi_unregister_master(master
);
1235 kfree(dma_channels
);
1236 destroy_workqueue(mcspi
->wq
);
1237 platform_set_drvdata(pdev
, NULL
);
1242 /* work with hotplug and coldplug */
1243 MODULE_ALIAS("platform:omap2_mcspi");
1245 #ifdef CONFIG_SUSPEND
1247 * When SPI wake up from off-mode, CS is in activate state. If it was in
1248 * unactive state when driver was suspend, then force it to unactive state at
1251 static int omap2_mcspi_resume(struct device
*dev
)
1253 struct spi_master
*master
= dev_get_drvdata(dev
);
1254 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1255 struct omap2_mcspi_cs
*cs
;
1257 omap2_mcspi_enable_clocks(mcspi
);
1258 list_for_each_entry(cs
, &omap2_mcspi_ctx
[master
->bus_num
- 1].cs
,
1260 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
) == 0) {
1263 * We need to toggle CS state for OMAP take this
1264 * change in account.
1266 MOD_REG_BIT(cs
->chconf0
, OMAP2_MCSPI_CHCONF_FORCE
, 1);
1267 __raw_writel(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1268 MOD_REG_BIT(cs
->chconf0
, OMAP2_MCSPI_CHCONF_FORCE
, 0);
1269 __raw_writel(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1272 omap2_mcspi_disable_clocks(mcspi
);
1276 #define omap2_mcspi_resume NULL
1279 static const struct dev_pm_ops omap2_mcspi_pm_ops
= {
1280 .resume
= omap2_mcspi_resume
,
1281 .runtime_resume
= omap_mcspi_runtime_resume
,
1284 static struct platform_driver omap2_mcspi_driver
= {
1286 .name
= "omap2_mcspi",
1287 .owner
= THIS_MODULE
,
1288 .pm
= &omap2_mcspi_pm_ops
1290 .remove
= __exit_p(omap2_mcspi_remove
),
1294 static int __init
omap2_mcspi_init(void)
1296 return platform_driver_probe(&omap2_mcspi_driver
, omap2_mcspi_probe
);
1298 subsys_initcall(omap2_mcspi_init
);
1300 static void __exit
omap2_mcspi_exit(void)
1302 platform_driver_unregister(&omap2_mcspi_driver
);
1305 module_exit(omap2_mcspi_exit
);
1307 MODULE_LICENSE("GPL");