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.
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/dmaengine.h>
26 #include <linux/omap-dma.h>
27 #include <linux/platform_device.h>
28 #include <linux/err.h>
29 #include <linux/clk.h>
31 #include <linux/slab.h>
32 #include <linux/pm_runtime.h>
34 #include <linux/of_device.h>
35 #include <linux/gcd.h>
37 #include <linux/spi/spi.h>
38 #include <linux/gpio.h>
40 #include <linux/platform_data/spi-omap2-mcspi.h>
42 #define OMAP2_MCSPI_MAX_FREQ 48000000
43 #define OMAP2_MCSPI_MAX_DIVIDER 4096
44 #define OMAP2_MCSPI_MAX_FIFODEPTH 64
45 #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
46 #define SPI_AUTOSUSPEND_TIMEOUT 2000
48 #define OMAP2_MCSPI_REVISION 0x00
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
55 #define OMAP2_MCSPI_XFERLEVEL 0x7c
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: */
65 #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
67 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
68 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
69 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
71 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
72 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
73 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
74 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
75 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
76 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
77 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
78 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
79 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
80 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
81 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
82 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
83 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
84 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
85 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
86 #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
87 #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
88 #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
90 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
91 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
92 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
93 #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
95 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
96 #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
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
{
102 struct dma_chan
*dma_tx
;
103 struct dma_chan
*dma_rx
;
108 struct completion dma_tx_completion
;
109 struct completion dma_rx_completion
;
111 char dma_rx_ch_name
[14];
112 char dma_tx_ch_name
[14];
115 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
116 * cache operations; better heuristics consider wordsize and bitrate.
118 #define DMA_MIN_BYTES 160
122 * Used for context save and restore, structure members to be updated whenever
123 * corresponding registers are modified.
125 struct omap2_mcspi_regs
{
132 struct spi_master
*master
;
133 /* Virtual base address of the controller */
136 /* SPI1 has 4 channels, while SPI2 has 2 */
137 struct omap2_mcspi_dma
*dma_channels
;
139 struct omap2_mcspi_regs ctx
;
141 unsigned int pin_dir
:1;
144 struct omap2_mcspi_cs
{
149 struct list_head node
;
150 /* Context save and restore shadow register */
151 u32 chconf0
, chctrl0
;
154 static inline void mcspi_write_reg(struct spi_master
*master
,
157 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
159 writel_relaxed(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 readl_relaxed(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 writel_relaxed(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 readl_relaxed(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 inline int mcspi_bytes_per_word(int word_len
)
204 else if (word_len
<= 16)
206 else /* word_len <= 32 */
210 static void omap2_mcspi_set_dma_req(const struct spi_device
*spi
,
211 int is_read
, int enable
)
215 l
= mcspi_cached_chconf0(spi
);
217 if (is_read
) /* 1 is read, 0 write */
218 rw
= OMAP2_MCSPI_CHCONF_DMAR
;
220 rw
= OMAP2_MCSPI_CHCONF_DMAW
;
227 mcspi_write_chconf0(spi
, l
);
230 static void omap2_mcspi_set_enable(const struct spi_device
*spi
, int enable
)
232 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
237 l
|= OMAP2_MCSPI_CHCTRL_EN
;
239 l
&= ~OMAP2_MCSPI_CHCTRL_EN
;
241 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
242 /* Flash post-writes */
243 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
);
246 static void omap2_mcspi_set_cs(struct spi_device
*spi
, bool enable
)
248 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
251 /* The controller handles the inverted chip selects
252 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
253 * the inversion from the core spi_set_cs function.
255 if (spi
->mode
& SPI_CS_HIGH
)
258 if (spi
->controller_state
) {
259 int err
= pm_runtime_get_sync(mcspi
->dev
);
261 dev_err(mcspi
->dev
, "failed to get sync: %d\n", err
);
265 l
= mcspi_cached_chconf0(spi
);
268 l
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
270 l
|= OMAP2_MCSPI_CHCONF_FORCE
;
272 mcspi_write_chconf0(spi
, l
);
274 pm_runtime_mark_last_busy(mcspi
->dev
);
275 pm_runtime_put_autosuspend(mcspi
->dev
);
279 static void omap2_mcspi_set_master_mode(struct spi_master
*master
)
281 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
282 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
286 * Setup when switching from (reset default) slave mode
287 * to single-channel master mode
289 l
= mcspi_read_reg(master
, OMAP2_MCSPI_MODULCTRL
);
290 l
&= ~(OMAP2_MCSPI_MODULCTRL_STEST
| OMAP2_MCSPI_MODULCTRL_MS
);
291 l
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
292 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, l
);
297 static void omap2_mcspi_set_fifo(const struct spi_device
*spi
,
298 struct spi_transfer
*t
, int enable
)
300 struct spi_master
*master
= spi
->master
;
301 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
302 struct omap2_mcspi
*mcspi
;
304 int max_fifo_depth
, fifo_depth
, bytes_per_word
;
305 u32 chconf
, xferlevel
;
307 mcspi
= spi_master_get_devdata(master
);
309 chconf
= mcspi_cached_chconf0(spi
);
311 bytes_per_word
= mcspi_bytes_per_word(cs
->word_len
);
312 if (t
->len
% bytes_per_word
!= 0)
315 if (t
->rx_buf
!= NULL
&& t
->tx_buf
!= NULL
)
316 max_fifo_depth
= OMAP2_MCSPI_MAX_FIFODEPTH
/ 2;
318 max_fifo_depth
= OMAP2_MCSPI_MAX_FIFODEPTH
;
320 fifo_depth
= gcd(t
->len
, max_fifo_depth
);
321 if (fifo_depth
< 2 || fifo_depth
% bytes_per_word
!= 0)
324 wcnt
= t
->len
/ bytes_per_word
;
325 if (wcnt
> OMAP2_MCSPI_MAX_FIFOWCNT
)
328 xferlevel
= wcnt
<< 16;
329 if (t
->rx_buf
!= NULL
) {
330 chconf
|= OMAP2_MCSPI_CHCONF_FFER
;
331 xferlevel
|= (fifo_depth
- 1) << 8;
333 if (t
->tx_buf
!= NULL
) {
334 chconf
|= OMAP2_MCSPI_CHCONF_FFET
;
335 xferlevel
|= fifo_depth
- 1;
338 mcspi_write_reg(master
, OMAP2_MCSPI_XFERLEVEL
, xferlevel
);
339 mcspi_write_chconf0(spi
, chconf
);
340 mcspi
->fifo_depth
= fifo_depth
;
346 if (t
->rx_buf
!= NULL
)
347 chconf
&= ~OMAP2_MCSPI_CHCONF_FFER
;
349 if (t
->tx_buf
!= NULL
)
350 chconf
&= ~OMAP2_MCSPI_CHCONF_FFET
;
352 mcspi_write_chconf0(spi
, chconf
);
353 mcspi
->fifo_depth
= 0;
356 static void omap2_mcspi_restore_ctx(struct omap2_mcspi
*mcspi
)
358 struct spi_master
*spi_cntrl
= mcspi
->master
;
359 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
360 struct omap2_mcspi_cs
*cs
;
362 /* McSPI: context restore */
363 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_MODULCTRL
, ctx
->modulctrl
);
364 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_WAKEUPENABLE
, ctx
->wakeupenable
);
366 list_for_each_entry(cs
, &ctx
->cs
, node
)
367 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
370 static int mcspi_wait_for_reg_bit(void __iomem
*reg
, unsigned long bit
)
372 unsigned long timeout
;
374 timeout
= jiffies
+ msecs_to_jiffies(1000);
375 while (!(readl_relaxed(reg
) & bit
)) {
376 if (time_after(jiffies
, timeout
)) {
377 if (!(readl_relaxed(reg
) & bit
))
387 static void omap2_mcspi_rx_callback(void *data
)
389 struct spi_device
*spi
= data
;
390 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
391 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
393 /* We must disable the DMA RX request */
394 omap2_mcspi_set_dma_req(spi
, 1, 0);
396 complete(&mcspi_dma
->dma_rx_completion
);
399 static void omap2_mcspi_tx_callback(void *data
)
401 struct spi_device
*spi
= data
;
402 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
403 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
405 /* We must disable the DMA TX request */
406 omap2_mcspi_set_dma_req(spi
, 0, 0);
408 complete(&mcspi_dma
->dma_tx_completion
);
411 static void omap2_mcspi_tx_dma(struct spi_device
*spi
,
412 struct spi_transfer
*xfer
,
413 struct dma_slave_config cfg
)
415 struct omap2_mcspi
*mcspi
;
416 struct omap2_mcspi_dma
*mcspi_dma
;
419 mcspi
= spi_master_get_devdata(spi
->master
);
420 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
423 if (mcspi_dma
->dma_tx
) {
424 struct dma_async_tx_descriptor
*tx
;
425 struct scatterlist sg
;
427 dmaengine_slave_config(mcspi_dma
->dma_tx
, &cfg
);
429 sg_init_table(&sg
, 1);
430 sg_dma_address(&sg
) = xfer
->tx_dma
;
431 sg_dma_len(&sg
) = xfer
->len
;
433 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_tx
, &sg
, 1,
434 DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
436 tx
->callback
= omap2_mcspi_tx_callback
;
437 tx
->callback_param
= spi
;
438 dmaengine_submit(tx
);
440 /* FIXME: fall back to PIO? */
443 dma_async_issue_pending(mcspi_dma
->dma_tx
);
444 omap2_mcspi_set_dma_req(spi
, 0, 1);
449 omap2_mcspi_rx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
,
450 struct dma_slave_config cfg
,
453 struct omap2_mcspi
*mcspi
;
454 struct omap2_mcspi_dma
*mcspi_dma
;
455 unsigned int count
, dma_count
;
458 int word_len
, element_count
;
459 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
460 mcspi
= spi_master_get_devdata(spi
->master
);
461 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
463 dma_count
= xfer
->len
;
465 if (mcspi
->fifo_depth
== 0)
468 word_len
= cs
->word_len
;
469 l
= mcspi_cached_chconf0(spi
);
472 element_count
= count
;
473 else if (word_len
<= 16)
474 element_count
= count
>> 1;
475 else /* word_len <= 32 */
476 element_count
= count
>> 2;
478 if (mcspi_dma
->dma_rx
) {
479 struct dma_async_tx_descriptor
*tx
;
480 struct scatterlist sg
;
482 dmaengine_slave_config(mcspi_dma
->dma_rx
, &cfg
);
484 if ((l
& OMAP2_MCSPI_CHCONF_TURBO
) && mcspi
->fifo_depth
== 0)
487 sg_init_table(&sg
, 1);
488 sg_dma_address(&sg
) = xfer
->rx_dma
;
489 sg_dma_len(&sg
) = dma_count
;
491 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_rx
, &sg
, 1,
492 DMA_DEV_TO_MEM
, DMA_PREP_INTERRUPT
|
495 tx
->callback
= omap2_mcspi_rx_callback
;
496 tx
->callback_param
= spi
;
497 dmaengine_submit(tx
);
499 /* FIXME: fall back to PIO? */
503 dma_async_issue_pending(mcspi_dma
->dma_rx
);
504 omap2_mcspi_set_dma_req(spi
, 1, 1);
506 wait_for_completion(&mcspi_dma
->dma_rx_completion
);
507 dma_unmap_single(mcspi
->dev
, xfer
->rx_dma
, count
,
510 if (mcspi
->fifo_depth
> 0)
513 omap2_mcspi_set_enable(spi
, 0);
515 elements
= element_count
- 1;
517 if (l
& OMAP2_MCSPI_CHCONF_TURBO
) {
520 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
521 & OMAP2_MCSPI_CHSTAT_RXS
)) {
524 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
526 ((u8
*)xfer
->rx_buf
)[elements
++] = w
;
527 else if (word_len
<= 16)
528 ((u16
*)xfer
->rx_buf
)[elements
++] = w
;
529 else /* word_len <= 32 */
530 ((u32
*)xfer
->rx_buf
)[elements
++] = w
;
532 int bytes_per_word
= mcspi_bytes_per_word(word_len
);
533 dev_err(&spi
->dev
, "DMA RX penultimate word empty\n");
534 count
-= (bytes_per_word
<< 1);
535 omap2_mcspi_set_enable(spi
, 1);
539 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
540 & OMAP2_MCSPI_CHSTAT_RXS
)) {
543 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
545 ((u8
*)xfer
->rx_buf
)[elements
] = w
;
546 else if (word_len
<= 16)
547 ((u16
*)xfer
->rx_buf
)[elements
] = w
;
548 else /* word_len <= 32 */
549 ((u32
*)xfer
->rx_buf
)[elements
] = w
;
551 dev_err(&spi
->dev
, "DMA RX last word empty\n");
552 count
-= mcspi_bytes_per_word(word_len
);
554 omap2_mcspi_set_enable(spi
, 1);
559 omap2_mcspi_txrx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
)
561 struct omap2_mcspi
*mcspi
;
562 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
563 struct omap2_mcspi_dma
*mcspi_dma
;
568 struct dma_slave_config cfg
;
569 enum dma_slave_buswidth width
;
572 void __iomem
*chstat_reg
;
573 void __iomem
*irqstat_reg
;
576 mcspi
= spi_master_get_devdata(spi
->master
);
577 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
578 l
= mcspi_cached_chconf0(spi
);
581 if (cs
->word_len
<= 8) {
582 width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
584 } else if (cs
->word_len
<= 16) {
585 width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
588 width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
595 if (mcspi
->fifo_depth
> 0) {
596 if (count
> mcspi
->fifo_depth
)
597 burst
= mcspi
->fifo_depth
/ es
;
602 memset(&cfg
, 0, sizeof(cfg
));
603 cfg
.src_addr
= cs
->phys
+ OMAP2_MCSPI_RX0
;
604 cfg
.dst_addr
= cs
->phys
+ OMAP2_MCSPI_TX0
;
605 cfg
.src_addr_width
= width
;
606 cfg
.dst_addr_width
= width
;
607 cfg
.src_maxburst
= burst
;
608 cfg
.dst_maxburst
= burst
;
614 omap2_mcspi_tx_dma(spi
, xfer
, cfg
);
617 count
= omap2_mcspi_rx_dma(spi
, xfer
, cfg
, es
);
620 wait_for_completion(&mcspi_dma
->dma_tx_completion
);
621 dma_unmap_single(mcspi
->dev
, xfer
->tx_dma
, xfer
->len
,
624 if (mcspi
->fifo_depth
> 0) {
625 irqstat_reg
= mcspi
->base
+ OMAP2_MCSPI_IRQSTATUS
;
627 if (mcspi_wait_for_reg_bit(irqstat_reg
,
628 OMAP2_MCSPI_IRQSTATUS_EOW
) < 0)
629 dev_err(&spi
->dev
, "EOW timed out\n");
631 mcspi_write_reg(mcspi
->master
, OMAP2_MCSPI_IRQSTATUS
,
632 OMAP2_MCSPI_IRQSTATUS_EOW
);
635 /* for TX_ONLY mode, be sure all words have shifted out */
637 chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
638 if (mcspi
->fifo_depth
> 0) {
639 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
640 OMAP2_MCSPI_CHSTAT_TXFFE
);
642 dev_err(&spi
->dev
, "TXFFE timed out\n");
644 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
645 OMAP2_MCSPI_CHSTAT_TXS
);
647 dev_err(&spi
->dev
, "TXS timed out\n");
650 (mcspi_wait_for_reg_bit(chstat_reg
,
651 OMAP2_MCSPI_CHSTAT_EOT
) < 0))
652 dev_err(&spi
->dev
, "EOT timed out\n");
659 omap2_mcspi_txrx_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
)
661 struct omap2_mcspi
*mcspi
;
662 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
663 unsigned int count
, c
;
665 void __iomem
*base
= cs
->base
;
666 void __iomem
*tx_reg
;
667 void __iomem
*rx_reg
;
668 void __iomem
*chstat_reg
;
671 mcspi
= spi_master_get_devdata(spi
->master
);
674 word_len
= cs
->word_len
;
676 l
= mcspi_cached_chconf0(spi
);
678 /* We store the pre-calculated register addresses on stack to speed
679 * up the transfer loop. */
680 tx_reg
= base
+ OMAP2_MCSPI_TX0
;
681 rx_reg
= base
+ OMAP2_MCSPI_RX0
;
682 chstat_reg
= base
+ OMAP2_MCSPI_CHSTAT0
;
684 if (c
< (word_len
>>3))
697 if (mcspi_wait_for_reg_bit(chstat_reg
,
698 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
699 dev_err(&spi
->dev
, "TXS timed out\n");
702 dev_vdbg(&spi
->dev
, "write-%d %02x\n",
704 writel_relaxed(*tx
++, tx_reg
);
707 if (mcspi_wait_for_reg_bit(chstat_reg
,
708 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
709 dev_err(&spi
->dev
, "RXS timed out\n");
713 if (c
== 1 && tx
== NULL
&&
714 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
715 omap2_mcspi_set_enable(spi
, 0);
716 *rx
++ = readl_relaxed(rx_reg
);
717 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
718 word_len
, *(rx
- 1));
719 if (mcspi_wait_for_reg_bit(chstat_reg
,
720 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
726 } else if (c
== 0 && tx
== NULL
) {
727 omap2_mcspi_set_enable(spi
, 0);
730 *rx
++ = readl_relaxed(rx_reg
);
731 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
732 word_len
, *(rx
- 1));
735 } else if (word_len
<= 16) {
744 if (mcspi_wait_for_reg_bit(chstat_reg
,
745 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
746 dev_err(&spi
->dev
, "TXS timed out\n");
749 dev_vdbg(&spi
->dev
, "write-%d %04x\n",
751 writel_relaxed(*tx
++, tx_reg
);
754 if (mcspi_wait_for_reg_bit(chstat_reg
,
755 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
756 dev_err(&spi
->dev
, "RXS timed out\n");
760 if (c
== 2 && tx
== NULL
&&
761 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
762 omap2_mcspi_set_enable(spi
, 0);
763 *rx
++ = readl_relaxed(rx_reg
);
764 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
765 word_len
, *(rx
- 1));
766 if (mcspi_wait_for_reg_bit(chstat_reg
,
767 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
773 } else if (c
== 0 && tx
== NULL
) {
774 omap2_mcspi_set_enable(spi
, 0);
777 *rx
++ = readl_relaxed(rx_reg
);
778 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
779 word_len
, *(rx
- 1));
782 } else if (word_len
<= 32) {
791 if (mcspi_wait_for_reg_bit(chstat_reg
,
792 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
793 dev_err(&spi
->dev
, "TXS timed out\n");
796 dev_vdbg(&spi
->dev
, "write-%d %08x\n",
798 writel_relaxed(*tx
++, tx_reg
);
801 if (mcspi_wait_for_reg_bit(chstat_reg
,
802 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
803 dev_err(&spi
->dev
, "RXS timed out\n");
807 if (c
== 4 && tx
== NULL
&&
808 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
809 omap2_mcspi_set_enable(spi
, 0);
810 *rx
++ = readl_relaxed(rx_reg
);
811 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
812 word_len
, *(rx
- 1));
813 if (mcspi_wait_for_reg_bit(chstat_reg
,
814 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
820 } else if (c
== 0 && tx
== NULL
) {
821 omap2_mcspi_set_enable(spi
, 0);
824 *rx
++ = readl_relaxed(rx_reg
);
825 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
826 word_len
, *(rx
- 1));
831 /* for TX_ONLY mode, be sure all words have shifted out */
832 if (xfer
->rx_buf
== NULL
) {
833 if (mcspi_wait_for_reg_bit(chstat_reg
,
834 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
835 dev_err(&spi
->dev
, "TXS timed out\n");
836 } else if (mcspi_wait_for_reg_bit(chstat_reg
,
837 OMAP2_MCSPI_CHSTAT_EOT
) < 0)
838 dev_err(&spi
->dev
, "EOT timed out\n");
840 /* disable chan to purge rx datas received in TX_ONLY transfer,
841 * otherwise these rx datas will affect the direct following
844 omap2_mcspi_set_enable(spi
, 0);
847 omap2_mcspi_set_enable(spi
, 1);
851 static u32
omap2_mcspi_calc_divisor(u32 speed_hz
)
855 for (div
= 0; div
< 15; div
++)
856 if (speed_hz
>= (OMAP2_MCSPI_MAX_FREQ
>> div
))
862 /* called only when no transfer is active to this device */
863 static int omap2_mcspi_setup_transfer(struct spi_device
*spi
,
864 struct spi_transfer
*t
)
866 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
867 struct omap2_mcspi
*mcspi
;
868 struct spi_master
*spi_cntrl
;
869 u32 l
= 0, clkd
= 0, div
, extclk
= 0, clkg
= 0;
870 u8 word_len
= spi
->bits_per_word
;
871 u32 speed_hz
= spi
->max_speed_hz
;
873 mcspi
= spi_master_get_devdata(spi
->master
);
874 spi_cntrl
= mcspi
->master
;
876 if (t
!= NULL
&& t
->bits_per_word
)
877 word_len
= t
->bits_per_word
;
879 cs
->word_len
= word_len
;
881 if (t
&& t
->speed_hz
)
882 speed_hz
= t
->speed_hz
;
884 speed_hz
= min_t(u32
, speed_hz
, OMAP2_MCSPI_MAX_FREQ
);
885 if (speed_hz
< (OMAP2_MCSPI_MAX_FREQ
/ OMAP2_MCSPI_MAX_DIVIDER
)) {
886 clkd
= omap2_mcspi_calc_divisor(speed_hz
);
887 speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> clkd
;
890 div
= (OMAP2_MCSPI_MAX_FREQ
+ speed_hz
- 1) / speed_hz
;
891 speed_hz
= OMAP2_MCSPI_MAX_FREQ
/ div
;
892 clkd
= (div
- 1) & 0xf;
893 extclk
= (div
- 1) >> 4;
894 clkg
= OMAP2_MCSPI_CHCONF_CLKG
;
897 l
= mcspi_cached_chconf0(spi
);
899 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
900 * REVISIT: this controller could support SPI_3WIRE mode.
902 if (mcspi
->pin_dir
== MCSPI_PINDIR_D0_IN_D1_OUT
) {
903 l
&= ~OMAP2_MCSPI_CHCONF_IS
;
904 l
&= ~OMAP2_MCSPI_CHCONF_DPE1
;
905 l
|= OMAP2_MCSPI_CHCONF_DPE0
;
907 l
|= OMAP2_MCSPI_CHCONF_IS
;
908 l
|= OMAP2_MCSPI_CHCONF_DPE1
;
909 l
&= ~OMAP2_MCSPI_CHCONF_DPE0
;
913 l
&= ~OMAP2_MCSPI_CHCONF_WL_MASK
;
914 l
|= (word_len
- 1) << 7;
916 /* set chipselect polarity; manage with FORCE */
917 if (!(spi
->mode
& SPI_CS_HIGH
))
918 l
|= OMAP2_MCSPI_CHCONF_EPOL
; /* active-low; normal */
920 l
&= ~OMAP2_MCSPI_CHCONF_EPOL
;
922 /* set clock divisor */
923 l
&= ~OMAP2_MCSPI_CHCONF_CLKD_MASK
;
926 /* set clock granularity */
927 l
&= ~OMAP2_MCSPI_CHCONF_CLKG
;
930 cs
->chctrl0
&= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK
;
931 cs
->chctrl0
|= extclk
<< 8;
932 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
935 /* set SPI mode 0..3 */
936 if (spi
->mode
& SPI_CPOL
)
937 l
|= OMAP2_MCSPI_CHCONF_POL
;
939 l
&= ~OMAP2_MCSPI_CHCONF_POL
;
940 if (spi
->mode
& SPI_CPHA
)
941 l
|= OMAP2_MCSPI_CHCONF_PHA
;
943 l
&= ~OMAP2_MCSPI_CHCONF_PHA
;
945 mcspi_write_chconf0(spi
, l
);
947 cs
->mode
= spi
->mode
;
949 dev_dbg(&spi
->dev
, "setup: speed %d, sample %s edge, clk %s\n",
951 (spi
->mode
& SPI_CPHA
) ? "trailing" : "leading",
952 (spi
->mode
& SPI_CPOL
) ? "inverted" : "normal");
958 * Note that we currently allow DMA only if we get a channel
959 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
961 static int omap2_mcspi_request_dma(struct spi_device
*spi
)
963 struct spi_master
*master
= spi
->master
;
964 struct omap2_mcspi
*mcspi
;
965 struct omap2_mcspi_dma
*mcspi_dma
;
969 mcspi
= spi_master_get_devdata(master
);
970 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
972 init_completion(&mcspi_dma
->dma_rx_completion
);
973 init_completion(&mcspi_dma
->dma_tx_completion
);
976 dma_cap_set(DMA_SLAVE
, mask
);
977 sig
= mcspi_dma
->dma_rx_sync_dev
;
980 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
982 mcspi_dma
->dma_rx_ch_name
);
983 if (!mcspi_dma
->dma_rx
)
986 sig
= mcspi_dma
->dma_tx_sync_dev
;
988 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
990 mcspi_dma
->dma_tx_ch_name
);
992 if (!mcspi_dma
->dma_tx
) {
993 dma_release_channel(mcspi_dma
->dma_rx
);
994 mcspi_dma
->dma_rx
= NULL
;
1001 dev_warn(&spi
->dev
, "not using DMA for McSPI\n");
1005 static int omap2_mcspi_setup(struct spi_device
*spi
)
1008 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
1009 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1010 struct omap2_mcspi_dma
*mcspi_dma
;
1011 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
1013 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1016 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
1019 cs
->base
= mcspi
->base
+ spi
->chip_select
* 0x14;
1020 cs
->phys
= mcspi
->phys
+ spi
->chip_select
* 0x14;
1024 spi
->controller_state
= cs
;
1025 /* Link this to context save list */
1026 list_add_tail(&cs
->node
, &ctx
->cs
);
1029 if (!mcspi_dma
->dma_rx
|| !mcspi_dma
->dma_tx
) {
1030 ret
= omap2_mcspi_request_dma(spi
);
1031 if (ret
< 0 && ret
!= -EAGAIN
)
1035 if (gpio_is_valid(spi
->cs_gpio
)) {
1036 ret
= gpio_request(spi
->cs_gpio
, dev_name(&spi
->dev
));
1038 dev_err(&spi
->dev
, "failed to request gpio\n");
1041 gpio_direction_output(spi
->cs_gpio
, !(spi
->mode
& SPI_CS_HIGH
));
1044 ret
= pm_runtime_get_sync(mcspi
->dev
);
1048 ret
= omap2_mcspi_setup_transfer(spi
, NULL
);
1049 pm_runtime_mark_last_busy(mcspi
->dev
);
1050 pm_runtime_put_autosuspend(mcspi
->dev
);
1055 static void omap2_mcspi_cleanup(struct spi_device
*spi
)
1057 struct omap2_mcspi
*mcspi
;
1058 struct omap2_mcspi_dma
*mcspi_dma
;
1059 struct omap2_mcspi_cs
*cs
;
1061 mcspi
= spi_master_get_devdata(spi
->master
);
1063 if (spi
->controller_state
) {
1064 /* Unlink controller state from context save list */
1065 cs
= spi
->controller_state
;
1066 list_del(&cs
->node
);
1071 if (spi
->chip_select
< spi
->master
->num_chipselect
) {
1072 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1074 if (mcspi_dma
->dma_rx
) {
1075 dma_release_channel(mcspi_dma
->dma_rx
);
1076 mcspi_dma
->dma_rx
= NULL
;
1078 if (mcspi_dma
->dma_tx
) {
1079 dma_release_channel(mcspi_dma
->dma_tx
);
1080 mcspi_dma
->dma_tx
= NULL
;
1084 if (gpio_is_valid(spi
->cs_gpio
))
1085 gpio_free(spi
->cs_gpio
);
1088 static int omap2_mcspi_work_one(struct omap2_mcspi
*mcspi
,
1089 struct spi_device
*spi
, struct spi_transfer
*t
)
1092 /* We only enable one channel at a time -- the one whose message is
1093 * -- although this controller would gladly
1094 * arbitrate among multiple channels. This corresponds to "single
1095 * channel" master mode. As a side effect, we need to manage the
1096 * chipselect with the FORCE bit ... CS != channel enable.
1099 struct spi_master
*master
;
1100 struct omap2_mcspi_dma
*mcspi_dma
;
1101 struct omap2_mcspi_cs
*cs
;
1102 struct omap2_mcspi_device_config
*cd
;
1103 int par_override
= 0;
1107 master
= spi
->master
;
1108 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1109 cs
= spi
->controller_state
;
1110 cd
= spi
->controller_data
;
1113 * The slave driver could have changed spi->mode in which case
1114 * it will be different from cs->mode (the current hardware setup).
1115 * If so, set par_override (even though its not a parity issue) so
1116 * omap2_mcspi_setup_transfer will be called to configure the hardware
1117 * with the correct mode on the first iteration of the loop below.
1119 if (spi
->mode
!= cs
->mode
)
1122 omap2_mcspi_set_enable(spi
, 0);
1124 if (gpio_is_valid(spi
->cs_gpio
))
1125 omap2_mcspi_set_cs(spi
, spi
->mode
& SPI_CS_HIGH
);
1128 (t
->speed_hz
!= spi
->max_speed_hz
) ||
1129 (t
->bits_per_word
!= spi
->bits_per_word
)) {
1131 status
= omap2_mcspi_setup_transfer(spi
, t
);
1134 if (t
->speed_hz
== spi
->max_speed_hz
&&
1135 t
->bits_per_word
== spi
->bits_per_word
)
1138 if (cd
&& cd
->cs_per_word
) {
1139 chconf
= mcspi
->ctx
.modulctrl
;
1140 chconf
&= ~OMAP2_MCSPI_MODULCTRL_SINGLE
;
1141 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1142 mcspi
->ctx
.modulctrl
=
1143 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1146 chconf
= mcspi_cached_chconf0(spi
);
1147 chconf
&= ~OMAP2_MCSPI_CHCONF_TRM_MASK
;
1148 chconf
&= ~OMAP2_MCSPI_CHCONF_TURBO
;
1150 if (t
->tx_buf
== NULL
)
1151 chconf
|= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY
;
1152 else if (t
->rx_buf
== NULL
)
1153 chconf
|= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY
;
1155 if (cd
&& cd
->turbo_mode
&& t
->tx_buf
== NULL
) {
1156 /* Turbo mode is for more than one word */
1157 if (t
->len
> ((cs
->word_len
+ 7) >> 3))
1158 chconf
|= OMAP2_MCSPI_CHCONF_TURBO
;
1161 mcspi_write_chconf0(spi
, chconf
);
1166 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1167 (t
->len
>= DMA_MIN_BYTES
))
1168 omap2_mcspi_set_fifo(spi
, t
, 1);
1170 omap2_mcspi_set_enable(spi
, 1);
1172 /* RX_ONLY mode needs dummy data in TX reg */
1173 if (t
->tx_buf
== NULL
)
1174 writel_relaxed(0, cs
->base
1177 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1178 (t
->len
>= DMA_MIN_BYTES
))
1179 count
= omap2_mcspi_txrx_dma(spi
, t
);
1181 count
= omap2_mcspi_txrx_pio(spi
, t
);
1183 if (count
!= t
->len
) {
1189 omap2_mcspi_set_enable(spi
, 0);
1191 if (mcspi
->fifo_depth
> 0)
1192 omap2_mcspi_set_fifo(spi
, t
, 0);
1195 /* Restore defaults if they were overriden */
1198 status
= omap2_mcspi_setup_transfer(spi
, NULL
);
1201 if (cd
&& cd
->cs_per_word
) {
1202 chconf
= mcspi
->ctx
.modulctrl
;
1203 chconf
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
1204 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1205 mcspi
->ctx
.modulctrl
=
1206 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1209 omap2_mcspi_set_enable(spi
, 0);
1211 if (gpio_is_valid(spi
->cs_gpio
))
1212 omap2_mcspi_set_cs(spi
, !(spi
->mode
& SPI_CS_HIGH
));
1214 if (mcspi
->fifo_depth
> 0 && t
)
1215 omap2_mcspi_set_fifo(spi
, t
, 0);
1220 static int omap2_mcspi_transfer_one(struct spi_master
*master
,
1221 struct spi_device
*spi
, struct spi_transfer
*t
)
1223 struct omap2_mcspi
*mcspi
;
1224 struct omap2_mcspi_dma
*mcspi_dma
;
1225 const void *tx_buf
= t
->tx_buf
;
1226 void *rx_buf
= t
->rx_buf
;
1227 unsigned len
= t
->len
;
1229 mcspi
= spi_master_get_devdata(master
);
1230 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1232 if ((len
&& !(rx_buf
|| tx_buf
))) {
1233 dev_dbg(mcspi
->dev
, "transfer: %d Hz, %d %s%s, %d bpw\n",
1242 if (len
< DMA_MIN_BYTES
)
1245 if (mcspi_dma
->dma_tx
&& tx_buf
!= NULL
) {
1246 t
->tx_dma
= dma_map_single(mcspi
->dev
, (void *) tx_buf
,
1247 len
, DMA_TO_DEVICE
);
1248 if (dma_mapping_error(mcspi
->dev
, t
->tx_dma
)) {
1249 dev_dbg(mcspi
->dev
, "dma %cX %d bytes error\n",
1254 if (mcspi_dma
->dma_rx
&& rx_buf
!= NULL
) {
1255 t
->rx_dma
= dma_map_single(mcspi
->dev
, rx_buf
, t
->len
,
1257 if (dma_mapping_error(mcspi
->dev
, t
->rx_dma
)) {
1258 dev_dbg(mcspi
->dev
, "dma %cX %d bytes error\n",
1261 dma_unmap_single(mcspi
->dev
, t
->tx_dma
,
1262 len
, DMA_TO_DEVICE
);
1268 return omap2_mcspi_work_one(mcspi
, spi
, t
);
1271 static int omap2_mcspi_master_setup(struct omap2_mcspi
*mcspi
)
1273 struct spi_master
*master
= mcspi
->master
;
1274 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1277 ret
= pm_runtime_get_sync(mcspi
->dev
);
1281 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
,
1282 OMAP2_MCSPI_WAKEUPENABLE_WKEN
);
1283 ctx
->wakeupenable
= OMAP2_MCSPI_WAKEUPENABLE_WKEN
;
1285 omap2_mcspi_set_master_mode(master
);
1286 pm_runtime_mark_last_busy(mcspi
->dev
);
1287 pm_runtime_put_autosuspend(mcspi
->dev
);
1291 static int omap_mcspi_runtime_resume(struct device
*dev
)
1293 struct omap2_mcspi
*mcspi
;
1294 struct spi_master
*master
;
1296 master
= dev_get_drvdata(dev
);
1297 mcspi
= spi_master_get_devdata(master
);
1298 omap2_mcspi_restore_ctx(mcspi
);
1303 static struct omap2_mcspi_platform_config omap2_pdata
= {
1307 static struct omap2_mcspi_platform_config omap4_pdata
= {
1308 .regs_offset
= OMAP4_MCSPI_REG_OFFSET
,
1311 static const struct of_device_id omap_mcspi_of_match
[] = {
1313 .compatible
= "ti,omap2-mcspi",
1314 .data
= &omap2_pdata
,
1317 .compatible
= "ti,omap4-mcspi",
1318 .data
= &omap4_pdata
,
1322 MODULE_DEVICE_TABLE(of
, omap_mcspi_of_match
);
1324 static int omap2_mcspi_probe(struct platform_device
*pdev
)
1326 struct spi_master
*master
;
1327 const struct omap2_mcspi_platform_config
*pdata
;
1328 struct omap2_mcspi
*mcspi
;
1331 u32 regs_offset
= 0;
1332 static int bus_num
= 1;
1333 struct device_node
*node
= pdev
->dev
.of_node
;
1334 const struct of_device_id
*match
;
1336 master
= spi_alloc_master(&pdev
->dev
, sizeof *mcspi
);
1337 if (master
== NULL
) {
1338 dev_dbg(&pdev
->dev
, "master allocation failed\n");
1342 /* the spi->mode bits understood by this driver: */
1343 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1344 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1345 master
->setup
= omap2_mcspi_setup
;
1346 master
->auto_runtime_pm
= true;
1347 master
->transfer_one
= omap2_mcspi_transfer_one
;
1348 master
->set_cs
= omap2_mcspi_set_cs
;
1349 master
->cleanup
= omap2_mcspi_cleanup
;
1350 master
->dev
.of_node
= node
;
1351 master
->max_speed_hz
= OMAP2_MCSPI_MAX_FREQ
;
1352 master
->min_speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> 15;
1354 platform_set_drvdata(pdev
, master
);
1356 mcspi
= spi_master_get_devdata(master
);
1357 mcspi
->master
= master
;
1359 match
= of_match_device(omap_mcspi_of_match
, &pdev
->dev
);
1361 u32 num_cs
= 1; /* default number of chipselect */
1362 pdata
= match
->data
;
1364 of_property_read_u32(node
, "ti,spi-num-cs", &num_cs
);
1365 master
->num_chipselect
= num_cs
;
1366 master
->bus_num
= bus_num
++;
1367 if (of_get_property(node
, "ti,pindir-d0-out-d1-in", NULL
))
1368 mcspi
->pin_dir
= MCSPI_PINDIR_D0_OUT_D1_IN
;
1370 pdata
= dev_get_platdata(&pdev
->dev
);
1371 master
->num_chipselect
= pdata
->num_cs
;
1373 master
->bus_num
= pdev
->id
;
1374 mcspi
->pin_dir
= pdata
->pin_dir
;
1376 regs_offset
= pdata
->regs_offset
;
1378 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1384 r
->start
+= regs_offset
;
1385 r
->end
+= regs_offset
;
1386 mcspi
->phys
= r
->start
;
1388 mcspi
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
1389 if (IS_ERR(mcspi
->base
)) {
1390 status
= PTR_ERR(mcspi
->base
);
1394 mcspi
->dev
= &pdev
->dev
;
1396 INIT_LIST_HEAD(&mcspi
->ctx
.cs
);
1398 mcspi
->dma_channels
= devm_kcalloc(&pdev
->dev
, master
->num_chipselect
,
1399 sizeof(struct omap2_mcspi_dma
),
1401 if (mcspi
->dma_channels
== NULL
) {
1406 for (i
= 0; i
< master
->num_chipselect
; i
++) {
1407 char *dma_rx_ch_name
= mcspi
->dma_channels
[i
].dma_rx_ch_name
;
1408 char *dma_tx_ch_name
= mcspi
->dma_channels
[i
].dma_tx_ch_name
;
1409 struct resource
*dma_res
;
1411 sprintf(dma_rx_ch_name
, "rx%d", i
);
1412 if (!pdev
->dev
.of_node
) {
1414 platform_get_resource_byname(pdev
,
1419 "cannot get DMA RX channel\n");
1424 mcspi
->dma_channels
[i
].dma_rx_sync_dev
=
1427 sprintf(dma_tx_ch_name
, "tx%d", i
);
1428 if (!pdev
->dev
.of_node
) {
1430 platform_get_resource_byname(pdev
,
1435 "cannot get DMA TX channel\n");
1440 mcspi
->dma_channels
[i
].dma_tx_sync_dev
=
1448 pm_runtime_use_autosuspend(&pdev
->dev
);
1449 pm_runtime_set_autosuspend_delay(&pdev
->dev
, SPI_AUTOSUSPEND_TIMEOUT
);
1450 pm_runtime_enable(&pdev
->dev
);
1452 status
= omap2_mcspi_master_setup(mcspi
);
1456 status
= devm_spi_register_master(&pdev
->dev
, master
);
1463 pm_runtime_disable(&pdev
->dev
);
1465 spi_master_put(master
);
1469 static int omap2_mcspi_remove(struct platform_device
*pdev
)
1471 struct spi_master
*master
= platform_get_drvdata(pdev
);
1472 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1474 pm_runtime_put_sync(mcspi
->dev
);
1475 pm_runtime_disable(&pdev
->dev
);
1480 /* work with hotplug and coldplug */
1481 MODULE_ALIAS("platform:omap2_mcspi");
1483 #ifdef CONFIG_SUSPEND
1485 * When SPI wake up from off-mode, CS is in activate state. If it was in
1486 * unactive state when driver was suspend, then force it to unactive state at
1489 static int omap2_mcspi_resume(struct device
*dev
)
1491 struct spi_master
*master
= dev_get_drvdata(dev
);
1492 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1493 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1494 struct omap2_mcspi_cs
*cs
;
1496 pm_runtime_get_sync(mcspi
->dev
);
1497 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1498 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
) == 0) {
1500 * We need to toggle CS state for OMAP take this
1501 * change in account.
1503 cs
->chconf0
|= OMAP2_MCSPI_CHCONF_FORCE
;
1504 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1505 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1506 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1509 pm_runtime_mark_last_busy(mcspi
->dev
);
1510 pm_runtime_put_autosuspend(mcspi
->dev
);
1514 #define omap2_mcspi_resume NULL
1517 static const struct dev_pm_ops omap2_mcspi_pm_ops
= {
1518 .resume
= omap2_mcspi_resume
,
1519 .runtime_resume
= omap_mcspi_runtime_resume
,
1522 static struct platform_driver omap2_mcspi_driver
= {
1524 .name
= "omap2_mcspi",
1525 .pm
= &omap2_mcspi_pm_ops
,
1526 .of_match_table
= omap_mcspi_of_match
,
1528 .probe
= omap2_mcspi_probe
,
1529 .remove
= omap2_mcspi_remove
,
1532 module_platform_driver(omap2_mcspi_driver
);
1533 MODULE_LICENSE("GPL");