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>
39 #include <linux/platform_data/spi-omap2-mcspi.h>
41 #define OMAP2_MCSPI_MAX_FREQ 48000000
42 #define OMAP2_MCSPI_MAX_DIVIDER 4096
43 #define OMAP2_MCSPI_MAX_FIFODEPTH 64
44 #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
45 #define SPI_AUTOSUSPEND_TIMEOUT 2000
47 #define OMAP2_MCSPI_REVISION 0x00
48 #define OMAP2_MCSPI_SYSSTATUS 0x14
49 #define OMAP2_MCSPI_IRQSTATUS 0x18
50 #define OMAP2_MCSPI_IRQENABLE 0x1c
51 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
52 #define OMAP2_MCSPI_SYST 0x24
53 #define OMAP2_MCSPI_MODULCTRL 0x28
54 #define OMAP2_MCSPI_XFERLEVEL 0x7c
56 /* per-channel banks, 0x14 bytes each, first is: */
57 #define OMAP2_MCSPI_CHCONF0 0x2c
58 #define OMAP2_MCSPI_CHSTAT0 0x30
59 #define OMAP2_MCSPI_CHCTRL0 0x34
60 #define OMAP2_MCSPI_TX0 0x38
61 #define OMAP2_MCSPI_RX0 0x3c
63 /* per-register bitmasks: */
64 #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
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)
85 #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
86 #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
87 #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
89 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
90 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
91 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
92 #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
94 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
95 #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
97 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
99 /* We have 2 DMA channels per CS, one for RX and one for TX */
100 struct omap2_mcspi_dma
{
101 struct dma_chan
*dma_tx
;
102 struct dma_chan
*dma_rx
;
107 struct completion dma_tx_completion
;
108 struct completion dma_rx_completion
;
110 char dma_rx_ch_name
[14];
111 char dma_tx_ch_name
[14];
114 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
115 * cache operations; better heuristics consider wordsize and bitrate.
117 #define DMA_MIN_BYTES 160
121 * Used for context save and restore, structure members to be updated whenever
122 * corresponding registers are modified.
124 struct omap2_mcspi_regs
{
131 struct spi_master
*master
;
132 /* Virtual base address of the controller */
135 /* SPI1 has 4 channels, while SPI2 has 2 */
136 struct omap2_mcspi_dma
*dma_channels
;
138 struct omap2_mcspi_regs ctx
;
140 unsigned int pin_dir
:1;
143 struct omap2_mcspi_cs
{
148 struct list_head node
;
149 /* Context save and restore shadow register */
150 u32 chconf0
, chctrl0
;
153 static inline void mcspi_write_reg(struct spi_master
*master
,
156 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
158 writel_relaxed(val
, mcspi
->base
+ idx
);
161 static inline u32
mcspi_read_reg(struct spi_master
*master
, int idx
)
163 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
165 return readl_relaxed(mcspi
->base
+ idx
);
168 static inline void mcspi_write_cs_reg(const struct spi_device
*spi
,
171 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
173 writel_relaxed(val
, cs
->base
+ idx
);
176 static inline u32
mcspi_read_cs_reg(const struct spi_device
*spi
, int idx
)
178 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
180 return readl_relaxed(cs
->base
+ idx
);
183 static inline u32
mcspi_cached_chconf0(const struct spi_device
*spi
)
185 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
190 static inline void mcspi_write_chconf0(const struct spi_device
*spi
, u32 val
)
192 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
195 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
, val
);
196 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
);
199 static inline int mcspi_bytes_per_word(int word_len
)
203 else if (word_len
<= 16)
205 else /* word_len <= 32 */
209 static void omap2_mcspi_set_dma_req(const struct spi_device
*spi
,
210 int is_read
, int enable
)
214 l
= mcspi_cached_chconf0(spi
);
216 if (is_read
) /* 1 is read, 0 write */
217 rw
= OMAP2_MCSPI_CHCONF_DMAR
;
219 rw
= OMAP2_MCSPI_CHCONF_DMAW
;
226 mcspi_write_chconf0(spi
, l
);
229 static void omap2_mcspi_set_enable(const struct spi_device
*spi
, int enable
)
231 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
236 l
|= OMAP2_MCSPI_CHCTRL_EN
;
238 l
&= ~OMAP2_MCSPI_CHCTRL_EN
;
240 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
241 /* Flash post-writes */
242 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
);
245 static void omap2_mcspi_force_cs(struct spi_device
*spi
, int cs_active
)
249 l
= mcspi_cached_chconf0(spi
);
251 l
|= OMAP2_MCSPI_CHCONF_FORCE
;
253 l
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
255 mcspi_write_chconf0(spi
, l
);
258 static void omap2_mcspi_set_master_mode(struct spi_master
*master
)
260 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
261 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
265 * Setup when switching from (reset default) slave mode
266 * to single-channel master mode
268 l
= mcspi_read_reg(master
, OMAP2_MCSPI_MODULCTRL
);
269 l
&= ~(OMAP2_MCSPI_MODULCTRL_STEST
| OMAP2_MCSPI_MODULCTRL_MS
);
270 l
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
271 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, l
);
276 static void omap2_mcspi_set_fifo(const struct spi_device
*spi
,
277 struct spi_transfer
*t
, int enable
)
279 struct spi_master
*master
= spi
->master
;
280 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
281 struct omap2_mcspi
*mcspi
;
283 int max_fifo_depth
, fifo_depth
, bytes_per_word
;
284 u32 chconf
, xferlevel
;
286 mcspi
= spi_master_get_devdata(master
);
288 chconf
= mcspi_cached_chconf0(spi
);
290 bytes_per_word
= mcspi_bytes_per_word(cs
->word_len
);
291 if (t
->len
% bytes_per_word
!= 0)
294 if (t
->rx_buf
!= NULL
&& t
->tx_buf
!= NULL
)
295 max_fifo_depth
= OMAP2_MCSPI_MAX_FIFODEPTH
/ 2;
297 max_fifo_depth
= OMAP2_MCSPI_MAX_FIFODEPTH
;
299 fifo_depth
= gcd(t
->len
, max_fifo_depth
);
300 if (fifo_depth
< 2 || fifo_depth
% bytes_per_word
!= 0)
303 wcnt
= t
->len
/ bytes_per_word
;
304 if (wcnt
> OMAP2_MCSPI_MAX_FIFOWCNT
)
307 xferlevel
= wcnt
<< 16;
308 if (t
->rx_buf
!= NULL
) {
309 chconf
|= OMAP2_MCSPI_CHCONF_FFER
;
310 xferlevel
|= (fifo_depth
- 1) << 8;
312 if (t
->tx_buf
!= NULL
) {
313 chconf
|= OMAP2_MCSPI_CHCONF_FFET
;
314 xferlevel
|= fifo_depth
- 1;
317 mcspi_write_reg(master
, OMAP2_MCSPI_XFERLEVEL
, xferlevel
);
318 mcspi_write_chconf0(spi
, chconf
);
319 mcspi
->fifo_depth
= fifo_depth
;
325 if (t
->rx_buf
!= NULL
)
326 chconf
&= ~OMAP2_MCSPI_CHCONF_FFER
;
328 if (t
->tx_buf
!= NULL
)
329 chconf
&= ~OMAP2_MCSPI_CHCONF_FFET
;
331 mcspi_write_chconf0(spi
, chconf
);
332 mcspi
->fifo_depth
= 0;
335 static void omap2_mcspi_restore_ctx(struct omap2_mcspi
*mcspi
)
337 struct spi_master
*spi_cntrl
= mcspi
->master
;
338 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
339 struct omap2_mcspi_cs
*cs
;
341 /* McSPI: context restore */
342 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_MODULCTRL
, ctx
->modulctrl
);
343 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_WAKEUPENABLE
, ctx
->wakeupenable
);
345 list_for_each_entry(cs
, &ctx
->cs
, node
)
346 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
349 static int mcspi_wait_for_reg_bit(void __iomem
*reg
, unsigned long bit
)
351 unsigned long timeout
;
353 timeout
= jiffies
+ msecs_to_jiffies(1000);
354 while (!(readl_relaxed(reg
) & bit
)) {
355 if (time_after(jiffies
, timeout
)) {
356 if (!(readl_relaxed(reg
) & bit
))
366 static void omap2_mcspi_rx_callback(void *data
)
368 struct spi_device
*spi
= data
;
369 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
370 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
372 /* We must disable the DMA RX request */
373 omap2_mcspi_set_dma_req(spi
, 1, 0);
375 complete(&mcspi_dma
->dma_rx_completion
);
378 static void omap2_mcspi_tx_callback(void *data
)
380 struct spi_device
*spi
= data
;
381 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
382 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
384 /* We must disable the DMA TX request */
385 omap2_mcspi_set_dma_req(spi
, 0, 0);
387 complete(&mcspi_dma
->dma_tx_completion
);
390 static void omap2_mcspi_tx_dma(struct spi_device
*spi
,
391 struct spi_transfer
*xfer
,
392 struct dma_slave_config cfg
)
394 struct omap2_mcspi
*mcspi
;
395 struct omap2_mcspi_dma
*mcspi_dma
;
398 mcspi
= spi_master_get_devdata(spi
->master
);
399 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
402 if (mcspi_dma
->dma_tx
) {
403 struct dma_async_tx_descriptor
*tx
;
404 struct scatterlist sg
;
406 dmaengine_slave_config(mcspi_dma
->dma_tx
, &cfg
);
408 sg_init_table(&sg
, 1);
409 sg_dma_address(&sg
) = xfer
->tx_dma
;
410 sg_dma_len(&sg
) = xfer
->len
;
412 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_tx
, &sg
, 1,
413 DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
415 tx
->callback
= omap2_mcspi_tx_callback
;
416 tx
->callback_param
= spi
;
417 dmaengine_submit(tx
);
419 /* FIXME: fall back to PIO? */
422 dma_async_issue_pending(mcspi_dma
->dma_tx
);
423 omap2_mcspi_set_dma_req(spi
, 0, 1);
428 omap2_mcspi_rx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
,
429 struct dma_slave_config cfg
,
432 struct omap2_mcspi
*mcspi
;
433 struct omap2_mcspi_dma
*mcspi_dma
;
434 unsigned int count
, dma_count
;
437 int word_len
, element_count
;
438 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
439 mcspi
= spi_master_get_devdata(spi
->master
);
440 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
442 dma_count
= xfer
->len
;
444 if (mcspi
->fifo_depth
== 0)
447 word_len
= cs
->word_len
;
448 l
= mcspi_cached_chconf0(spi
);
451 element_count
= count
;
452 else if (word_len
<= 16)
453 element_count
= count
>> 1;
454 else /* word_len <= 32 */
455 element_count
= count
>> 2;
457 if (mcspi_dma
->dma_rx
) {
458 struct dma_async_tx_descriptor
*tx
;
459 struct scatterlist sg
;
461 dmaengine_slave_config(mcspi_dma
->dma_rx
, &cfg
);
463 if ((l
& OMAP2_MCSPI_CHCONF_TURBO
) && mcspi
->fifo_depth
== 0)
466 sg_init_table(&sg
, 1);
467 sg_dma_address(&sg
) = xfer
->rx_dma
;
468 sg_dma_len(&sg
) = dma_count
;
470 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_rx
, &sg
, 1,
471 DMA_DEV_TO_MEM
, DMA_PREP_INTERRUPT
|
474 tx
->callback
= omap2_mcspi_rx_callback
;
475 tx
->callback_param
= spi
;
476 dmaengine_submit(tx
);
478 /* FIXME: fall back to PIO? */
482 dma_async_issue_pending(mcspi_dma
->dma_rx
);
483 omap2_mcspi_set_dma_req(spi
, 1, 1);
485 wait_for_completion(&mcspi_dma
->dma_rx_completion
);
486 dma_unmap_single(mcspi
->dev
, xfer
->rx_dma
, count
,
489 if (mcspi
->fifo_depth
> 0)
492 omap2_mcspi_set_enable(spi
, 0);
494 elements
= element_count
- 1;
496 if (l
& OMAP2_MCSPI_CHCONF_TURBO
) {
499 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
500 & OMAP2_MCSPI_CHSTAT_RXS
)) {
503 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
505 ((u8
*)xfer
->rx_buf
)[elements
++] = w
;
506 else if (word_len
<= 16)
507 ((u16
*)xfer
->rx_buf
)[elements
++] = w
;
508 else /* word_len <= 32 */
509 ((u32
*)xfer
->rx_buf
)[elements
++] = w
;
511 int bytes_per_word
= mcspi_bytes_per_word(word_len
);
512 dev_err(&spi
->dev
, "DMA RX penultimate word empty\n");
513 count
-= (bytes_per_word
<< 1);
514 omap2_mcspi_set_enable(spi
, 1);
518 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
519 & OMAP2_MCSPI_CHSTAT_RXS
)) {
522 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
524 ((u8
*)xfer
->rx_buf
)[elements
] = w
;
525 else if (word_len
<= 16)
526 ((u16
*)xfer
->rx_buf
)[elements
] = w
;
527 else /* word_len <= 32 */
528 ((u32
*)xfer
->rx_buf
)[elements
] = w
;
530 dev_err(&spi
->dev
, "DMA RX last word empty\n");
531 count
-= mcspi_bytes_per_word(word_len
);
533 omap2_mcspi_set_enable(spi
, 1);
538 omap2_mcspi_txrx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
)
540 struct omap2_mcspi
*mcspi
;
541 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
542 struct omap2_mcspi_dma
*mcspi_dma
;
547 struct dma_slave_config cfg
;
548 enum dma_slave_buswidth width
;
551 void __iomem
*chstat_reg
;
552 void __iomem
*irqstat_reg
;
555 mcspi
= spi_master_get_devdata(spi
->master
);
556 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
557 l
= mcspi_cached_chconf0(spi
);
560 if (cs
->word_len
<= 8) {
561 width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
563 } else if (cs
->word_len
<= 16) {
564 width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
567 width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
574 if (mcspi
->fifo_depth
> 0) {
575 if (count
> mcspi
->fifo_depth
)
576 burst
= mcspi
->fifo_depth
/ es
;
581 memset(&cfg
, 0, sizeof(cfg
));
582 cfg
.src_addr
= cs
->phys
+ OMAP2_MCSPI_RX0
;
583 cfg
.dst_addr
= cs
->phys
+ OMAP2_MCSPI_TX0
;
584 cfg
.src_addr_width
= width
;
585 cfg
.dst_addr_width
= width
;
586 cfg
.src_maxburst
= burst
;
587 cfg
.dst_maxburst
= burst
;
593 omap2_mcspi_tx_dma(spi
, xfer
, cfg
);
596 count
= omap2_mcspi_rx_dma(spi
, xfer
, cfg
, es
);
599 wait_for_completion(&mcspi_dma
->dma_tx_completion
);
600 dma_unmap_single(mcspi
->dev
, xfer
->tx_dma
, xfer
->len
,
603 if (mcspi
->fifo_depth
> 0) {
604 irqstat_reg
= mcspi
->base
+ OMAP2_MCSPI_IRQSTATUS
;
606 if (mcspi_wait_for_reg_bit(irqstat_reg
,
607 OMAP2_MCSPI_IRQSTATUS_EOW
) < 0)
608 dev_err(&spi
->dev
, "EOW timed out\n");
610 mcspi_write_reg(mcspi
->master
, OMAP2_MCSPI_IRQSTATUS
,
611 OMAP2_MCSPI_IRQSTATUS_EOW
);
614 /* for TX_ONLY mode, be sure all words have shifted out */
616 chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
617 if (mcspi
->fifo_depth
> 0) {
618 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
619 OMAP2_MCSPI_CHSTAT_TXFFE
);
621 dev_err(&spi
->dev
, "TXFFE timed out\n");
623 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
624 OMAP2_MCSPI_CHSTAT_TXS
);
626 dev_err(&spi
->dev
, "TXS timed out\n");
629 (mcspi_wait_for_reg_bit(chstat_reg
,
630 OMAP2_MCSPI_CHSTAT_EOT
) < 0))
631 dev_err(&spi
->dev
, "EOT timed out\n");
638 omap2_mcspi_txrx_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
)
640 struct omap2_mcspi
*mcspi
;
641 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
642 unsigned int count
, c
;
644 void __iomem
*base
= cs
->base
;
645 void __iomem
*tx_reg
;
646 void __iomem
*rx_reg
;
647 void __iomem
*chstat_reg
;
650 mcspi
= spi_master_get_devdata(spi
->master
);
653 word_len
= cs
->word_len
;
655 l
= mcspi_cached_chconf0(spi
);
657 /* We store the pre-calculated register addresses on stack to speed
658 * up the transfer loop. */
659 tx_reg
= base
+ OMAP2_MCSPI_TX0
;
660 rx_reg
= base
+ OMAP2_MCSPI_RX0
;
661 chstat_reg
= base
+ OMAP2_MCSPI_CHSTAT0
;
663 if (c
< (word_len
>>3))
676 if (mcspi_wait_for_reg_bit(chstat_reg
,
677 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
678 dev_err(&spi
->dev
, "TXS timed out\n");
681 dev_vdbg(&spi
->dev
, "write-%d %02x\n",
683 writel_relaxed(*tx
++, tx_reg
);
686 if (mcspi_wait_for_reg_bit(chstat_reg
,
687 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
688 dev_err(&spi
->dev
, "RXS timed out\n");
692 if (c
== 1 && tx
== NULL
&&
693 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
694 omap2_mcspi_set_enable(spi
, 0);
695 *rx
++ = readl_relaxed(rx_reg
);
696 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
697 word_len
, *(rx
- 1));
698 if (mcspi_wait_for_reg_bit(chstat_reg
,
699 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
705 } else if (c
== 0 && tx
== NULL
) {
706 omap2_mcspi_set_enable(spi
, 0);
709 *rx
++ = readl_relaxed(rx_reg
);
710 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
711 word_len
, *(rx
- 1));
714 } else if (word_len
<= 16) {
723 if (mcspi_wait_for_reg_bit(chstat_reg
,
724 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
725 dev_err(&spi
->dev
, "TXS timed out\n");
728 dev_vdbg(&spi
->dev
, "write-%d %04x\n",
730 writel_relaxed(*tx
++, tx_reg
);
733 if (mcspi_wait_for_reg_bit(chstat_reg
,
734 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
735 dev_err(&spi
->dev
, "RXS timed out\n");
739 if (c
== 2 && tx
== NULL
&&
740 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
741 omap2_mcspi_set_enable(spi
, 0);
742 *rx
++ = readl_relaxed(rx_reg
);
743 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
744 word_len
, *(rx
- 1));
745 if (mcspi_wait_for_reg_bit(chstat_reg
,
746 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
752 } else if (c
== 0 && tx
== NULL
) {
753 omap2_mcspi_set_enable(spi
, 0);
756 *rx
++ = readl_relaxed(rx_reg
);
757 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
758 word_len
, *(rx
- 1));
761 } else if (word_len
<= 32) {
770 if (mcspi_wait_for_reg_bit(chstat_reg
,
771 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
772 dev_err(&spi
->dev
, "TXS timed out\n");
775 dev_vdbg(&spi
->dev
, "write-%d %08x\n",
777 writel_relaxed(*tx
++, tx_reg
);
780 if (mcspi_wait_for_reg_bit(chstat_reg
,
781 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
782 dev_err(&spi
->dev
, "RXS timed out\n");
786 if (c
== 4 && tx
== NULL
&&
787 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
788 omap2_mcspi_set_enable(spi
, 0);
789 *rx
++ = readl_relaxed(rx_reg
);
790 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
791 word_len
, *(rx
- 1));
792 if (mcspi_wait_for_reg_bit(chstat_reg
,
793 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
799 } else if (c
== 0 && tx
== NULL
) {
800 omap2_mcspi_set_enable(spi
, 0);
803 *rx
++ = readl_relaxed(rx_reg
);
804 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
805 word_len
, *(rx
- 1));
810 /* for TX_ONLY mode, be sure all words have shifted out */
811 if (xfer
->rx_buf
== NULL
) {
812 if (mcspi_wait_for_reg_bit(chstat_reg
,
813 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
814 dev_err(&spi
->dev
, "TXS timed out\n");
815 } else if (mcspi_wait_for_reg_bit(chstat_reg
,
816 OMAP2_MCSPI_CHSTAT_EOT
) < 0)
817 dev_err(&spi
->dev
, "EOT timed out\n");
819 /* disable chan to purge rx datas received in TX_ONLY transfer,
820 * otherwise these rx datas will affect the direct following
823 omap2_mcspi_set_enable(spi
, 0);
826 omap2_mcspi_set_enable(spi
, 1);
830 static u32
omap2_mcspi_calc_divisor(u32 speed_hz
)
834 for (div
= 0; div
< 15; div
++)
835 if (speed_hz
>= (OMAP2_MCSPI_MAX_FREQ
>> div
))
841 /* called only when no transfer is active to this device */
842 static int omap2_mcspi_setup_transfer(struct spi_device
*spi
,
843 struct spi_transfer
*t
)
845 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
846 struct omap2_mcspi
*mcspi
;
847 struct spi_master
*spi_cntrl
;
848 u32 l
= 0, clkd
= 0, div
, extclk
= 0, clkg
= 0;
849 u8 word_len
= spi
->bits_per_word
;
850 u32 speed_hz
= spi
->max_speed_hz
;
852 mcspi
= spi_master_get_devdata(spi
->master
);
853 spi_cntrl
= mcspi
->master
;
855 if (t
!= NULL
&& t
->bits_per_word
)
856 word_len
= t
->bits_per_word
;
858 cs
->word_len
= word_len
;
860 if (t
&& t
->speed_hz
)
861 speed_hz
= t
->speed_hz
;
863 speed_hz
= min_t(u32
, speed_hz
, OMAP2_MCSPI_MAX_FREQ
);
864 if (speed_hz
< (OMAP2_MCSPI_MAX_FREQ
/ OMAP2_MCSPI_MAX_DIVIDER
)) {
865 clkd
= omap2_mcspi_calc_divisor(speed_hz
);
866 speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> clkd
;
869 div
= (OMAP2_MCSPI_MAX_FREQ
+ speed_hz
- 1) / speed_hz
;
870 speed_hz
= OMAP2_MCSPI_MAX_FREQ
/ div
;
871 clkd
= (div
- 1) & 0xf;
872 extclk
= (div
- 1) >> 4;
873 clkg
= OMAP2_MCSPI_CHCONF_CLKG
;
876 l
= mcspi_cached_chconf0(spi
);
878 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
879 * REVISIT: this controller could support SPI_3WIRE mode.
881 if (mcspi
->pin_dir
== MCSPI_PINDIR_D0_IN_D1_OUT
) {
882 l
&= ~OMAP2_MCSPI_CHCONF_IS
;
883 l
&= ~OMAP2_MCSPI_CHCONF_DPE1
;
884 l
|= OMAP2_MCSPI_CHCONF_DPE0
;
886 l
|= OMAP2_MCSPI_CHCONF_IS
;
887 l
|= OMAP2_MCSPI_CHCONF_DPE1
;
888 l
&= ~OMAP2_MCSPI_CHCONF_DPE0
;
892 l
&= ~OMAP2_MCSPI_CHCONF_WL_MASK
;
893 l
|= (word_len
- 1) << 7;
895 /* set chipselect polarity; manage with FORCE */
896 if (!(spi
->mode
& SPI_CS_HIGH
))
897 l
|= OMAP2_MCSPI_CHCONF_EPOL
; /* active-low; normal */
899 l
&= ~OMAP2_MCSPI_CHCONF_EPOL
;
901 /* set clock divisor */
902 l
&= ~OMAP2_MCSPI_CHCONF_CLKD_MASK
;
905 /* set clock granularity */
906 l
&= ~OMAP2_MCSPI_CHCONF_CLKG
;
909 cs
->chctrl0
&= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK
;
910 cs
->chctrl0
|= extclk
<< 8;
911 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
914 /* set SPI mode 0..3 */
915 if (spi
->mode
& SPI_CPOL
)
916 l
|= OMAP2_MCSPI_CHCONF_POL
;
918 l
&= ~OMAP2_MCSPI_CHCONF_POL
;
919 if (spi
->mode
& SPI_CPHA
)
920 l
|= OMAP2_MCSPI_CHCONF_PHA
;
922 l
&= ~OMAP2_MCSPI_CHCONF_PHA
;
924 mcspi_write_chconf0(spi
, l
);
926 cs
->mode
= spi
->mode
;
928 dev_dbg(&spi
->dev
, "setup: speed %d, sample %s edge, clk %s\n",
930 (spi
->mode
& SPI_CPHA
) ? "trailing" : "leading",
931 (spi
->mode
& SPI_CPOL
) ? "inverted" : "normal");
937 * Note that we currently allow DMA only if we get a channel
938 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
940 static int omap2_mcspi_request_dma(struct spi_device
*spi
)
942 struct spi_master
*master
= spi
->master
;
943 struct omap2_mcspi
*mcspi
;
944 struct omap2_mcspi_dma
*mcspi_dma
;
948 mcspi
= spi_master_get_devdata(master
);
949 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
951 init_completion(&mcspi_dma
->dma_rx_completion
);
952 init_completion(&mcspi_dma
->dma_tx_completion
);
955 dma_cap_set(DMA_SLAVE
, mask
);
956 sig
= mcspi_dma
->dma_rx_sync_dev
;
959 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
961 mcspi_dma
->dma_rx_ch_name
);
962 if (!mcspi_dma
->dma_rx
)
965 sig
= mcspi_dma
->dma_tx_sync_dev
;
967 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
969 mcspi_dma
->dma_tx_ch_name
);
971 if (!mcspi_dma
->dma_tx
) {
972 dma_release_channel(mcspi_dma
->dma_rx
);
973 mcspi_dma
->dma_rx
= NULL
;
980 dev_warn(&spi
->dev
, "not using DMA for McSPI\n");
984 static int omap2_mcspi_setup(struct spi_device
*spi
)
987 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
988 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
989 struct omap2_mcspi_dma
*mcspi_dma
;
990 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
992 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
995 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
998 cs
->base
= mcspi
->base
+ spi
->chip_select
* 0x14;
999 cs
->phys
= mcspi
->phys
+ spi
->chip_select
* 0x14;
1003 spi
->controller_state
= cs
;
1004 /* Link this to context save list */
1005 list_add_tail(&cs
->node
, &ctx
->cs
);
1008 if (!mcspi_dma
->dma_rx
|| !mcspi_dma
->dma_tx
) {
1009 ret
= omap2_mcspi_request_dma(spi
);
1010 if (ret
< 0 && ret
!= -EAGAIN
)
1014 ret
= pm_runtime_get_sync(mcspi
->dev
);
1018 ret
= omap2_mcspi_setup_transfer(spi
, NULL
);
1019 pm_runtime_mark_last_busy(mcspi
->dev
);
1020 pm_runtime_put_autosuspend(mcspi
->dev
);
1025 static void omap2_mcspi_cleanup(struct spi_device
*spi
)
1027 struct omap2_mcspi
*mcspi
;
1028 struct omap2_mcspi_dma
*mcspi_dma
;
1029 struct omap2_mcspi_cs
*cs
;
1031 mcspi
= spi_master_get_devdata(spi
->master
);
1033 if (spi
->controller_state
) {
1034 /* Unlink controller state from context save list */
1035 cs
= spi
->controller_state
;
1036 list_del(&cs
->node
);
1041 if (spi
->chip_select
< spi
->master
->num_chipselect
) {
1042 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1044 if (mcspi_dma
->dma_rx
) {
1045 dma_release_channel(mcspi_dma
->dma_rx
);
1046 mcspi_dma
->dma_rx
= NULL
;
1048 if (mcspi_dma
->dma_tx
) {
1049 dma_release_channel(mcspi_dma
->dma_tx
);
1050 mcspi_dma
->dma_tx
= NULL
;
1055 static void omap2_mcspi_work(struct omap2_mcspi
*mcspi
, struct spi_message
*m
)
1058 /* We only enable one channel at a time -- the one whose message is
1059 * -- although this controller would gladly
1060 * arbitrate among multiple channels. This corresponds to "single
1061 * channel" master mode. As a side effect, we need to manage the
1062 * chipselect with the FORCE bit ... CS != channel enable.
1065 struct spi_device
*spi
;
1066 struct spi_transfer
*t
= NULL
;
1067 struct spi_master
*master
;
1068 struct omap2_mcspi_dma
*mcspi_dma
;
1070 struct omap2_mcspi_cs
*cs
;
1071 struct omap2_mcspi_device_config
*cd
;
1072 int par_override
= 0;
1077 master
= spi
->master
;
1078 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1079 cs
= spi
->controller_state
;
1080 cd
= spi
->controller_data
;
1083 * The slave driver could have changed spi->mode in which case
1084 * it will be different from cs->mode (the current hardware setup).
1085 * If so, set par_override (even though its not a parity issue) so
1086 * omap2_mcspi_setup_transfer will be called to configure the hardware
1087 * with the correct mode on the first iteration of the loop below.
1089 if (spi
->mode
!= cs
->mode
)
1092 omap2_mcspi_set_enable(spi
, 0);
1093 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
1094 if (t
->tx_buf
== NULL
&& t
->rx_buf
== NULL
&& t
->len
) {
1099 (t
->speed_hz
!= spi
->max_speed_hz
) ||
1100 (t
->bits_per_word
!= spi
->bits_per_word
)) {
1102 status
= omap2_mcspi_setup_transfer(spi
, t
);
1105 if (t
->speed_hz
== spi
->max_speed_hz
&&
1106 t
->bits_per_word
== spi
->bits_per_word
)
1109 if (cd
&& cd
->cs_per_word
) {
1110 chconf
= mcspi
->ctx
.modulctrl
;
1111 chconf
&= ~OMAP2_MCSPI_MODULCTRL_SINGLE
;
1112 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1113 mcspi
->ctx
.modulctrl
=
1114 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1119 omap2_mcspi_force_cs(spi
, 1);
1123 chconf
= mcspi_cached_chconf0(spi
);
1124 chconf
&= ~OMAP2_MCSPI_CHCONF_TRM_MASK
;
1125 chconf
&= ~OMAP2_MCSPI_CHCONF_TURBO
;
1127 if (t
->tx_buf
== NULL
)
1128 chconf
|= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY
;
1129 else if (t
->rx_buf
== NULL
)
1130 chconf
|= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY
;
1132 if (cd
&& cd
->turbo_mode
&& t
->tx_buf
== NULL
) {
1133 /* Turbo mode is for more than one word */
1134 if (t
->len
> ((cs
->word_len
+ 7) >> 3))
1135 chconf
|= OMAP2_MCSPI_CHCONF_TURBO
;
1138 mcspi_write_chconf0(spi
, chconf
);
1143 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1144 (m
->is_dma_mapped
|| t
->len
>= DMA_MIN_BYTES
))
1145 omap2_mcspi_set_fifo(spi
, t
, 1);
1147 omap2_mcspi_set_enable(spi
, 1);
1149 /* RX_ONLY mode needs dummy data in TX reg */
1150 if (t
->tx_buf
== NULL
)
1151 writel_relaxed(0, cs
->base
1154 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1155 (m
->is_dma_mapped
|| t
->len
>= DMA_MIN_BYTES
))
1156 count
= omap2_mcspi_txrx_dma(spi
, t
);
1158 count
= omap2_mcspi_txrx_pio(spi
, t
);
1159 m
->actual_length
+= count
;
1161 if (count
!= t
->len
) {
1168 udelay(t
->delay_usecs
);
1170 /* ignore the "leave it on after last xfer" hint */
1172 omap2_mcspi_force_cs(spi
, 0);
1176 omap2_mcspi_set_enable(spi
, 0);
1178 if (mcspi
->fifo_depth
> 0)
1179 omap2_mcspi_set_fifo(spi
, t
, 0);
1181 /* Restore defaults if they were overriden */
1184 status
= omap2_mcspi_setup_transfer(spi
, NULL
);
1188 omap2_mcspi_force_cs(spi
, 0);
1190 if (cd
&& cd
->cs_per_word
) {
1191 chconf
= mcspi
->ctx
.modulctrl
;
1192 chconf
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
1193 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1194 mcspi
->ctx
.modulctrl
=
1195 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1198 omap2_mcspi_set_enable(spi
, 0);
1200 if (mcspi
->fifo_depth
> 0 && t
)
1201 omap2_mcspi_set_fifo(spi
, t
, 0);
1206 static int omap2_mcspi_transfer_one_message(struct spi_master
*master
,
1207 struct spi_message
*m
)
1209 struct spi_device
*spi
;
1210 struct omap2_mcspi
*mcspi
;
1211 struct omap2_mcspi_dma
*mcspi_dma
;
1212 struct spi_transfer
*t
;
1216 mcspi
= spi_master_get_devdata(master
);
1217 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1218 m
->actual_length
= 0;
1221 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
1222 const void *tx_buf
= t
->tx_buf
;
1223 void *rx_buf
= t
->rx_buf
;
1224 unsigned len
= t
->len
;
1226 if ((len
&& !(rx_buf
|| tx_buf
))) {
1227 dev_dbg(mcspi
->dev
, "transfer: %d Hz, %d %s%s, %d bpw\n",
1237 if (m
->is_dma_mapped
|| len
< DMA_MIN_BYTES
)
1240 if (mcspi_dma
->dma_tx
&& tx_buf
!= NULL
) {
1241 t
->tx_dma
= dma_map_single(mcspi
->dev
, (void *) tx_buf
,
1242 len
, DMA_TO_DEVICE
);
1243 if (dma_mapping_error(mcspi
->dev
, t
->tx_dma
)) {
1244 dev_dbg(mcspi
->dev
, "dma %cX %d bytes error\n",
1250 if (mcspi_dma
->dma_rx
&& rx_buf
!= NULL
) {
1251 t
->rx_dma
= dma_map_single(mcspi
->dev
, rx_buf
, t
->len
,
1253 if (dma_mapping_error(mcspi
->dev
, t
->rx_dma
)) {
1254 dev_dbg(mcspi
->dev
, "dma %cX %d bytes error\n",
1257 dma_unmap_single(mcspi
->dev
, t
->tx_dma
,
1258 len
, DMA_TO_DEVICE
);
1265 omap2_mcspi_work(mcspi
, m
);
1266 /* spi_finalize_current_message() changes the status inside the
1267 * spi_message, save the status here. */
1270 spi_finalize_current_message(master
);
1274 static int omap2_mcspi_master_setup(struct omap2_mcspi
*mcspi
)
1276 struct spi_master
*master
= mcspi
->master
;
1277 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1280 ret
= pm_runtime_get_sync(mcspi
->dev
);
1284 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
,
1285 OMAP2_MCSPI_WAKEUPENABLE_WKEN
);
1286 ctx
->wakeupenable
= OMAP2_MCSPI_WAKEUPENABLE_WKEN
;
1288 omap2_mcspi_set_master_mode(master
);
1289 pm_runtime_mark_last_busy(mcspi
->dev
);
1290 pm_runtime_put_autosuspend(mcspi
->dev
);
1294 static int omap_mcspi_runtime_resume(struct device
*dev
)
1296 struct omap2_mcspi
*mcspi
;
1297 struct spi_master
*master
;
1299 master
= dev_get_drvdata(dev
);
1300 mcspi
= spi_master_get_devdata(master
);
1301 omap2_mcspi_restore_ctx(mcspi
);
1306 static struct omap2_mcspi_platform_config omap2_pdata
= {
1310 static struct omap2_mcspi_platform_config omap4_pdata
= {
1311 .regs_offset
= OMAP4_MCSPI_REG_OFFSET
,
1314 static const struct of_device_id omap_mcspi_of_match
[] = {
1316 .compatible
= "ti,omap2-mcspi",
1317 .data
= &omap2_pdata
,
1320 .compatible
= "ti,omap4-mcspi",
1321 .data
= &omap4_pdata
,
1325 MODULE_DEVICE_TABLE(of
, omap_mcspi_of_match
);
1327 static int omap2_mcspi_probe(struct platform_device
*pdev
)
1329 struct spi_master
*master
;
1330 const struct omap2_mcspi_platform_config
*pdata
;
1331 struct omap2_mcspi
*mcspi
;
1334 u32 regs_offset
= 0;
1335 static int bus_num
= 1;
1336 struct device_node
*node
= pdev
->dev
.of_node
;
1337 const struct of_device_id
*match
;
1339 master
= spi_alloc_master(&pdev
->dev
, sizeof *mcspi
);
1340 if (master
== NULL
) {
1341 dev_dbg(&pdev
->dev
, "master allocation failed\n");
1345 /* the spi->mode bits understood by this driver: */
1346 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1347 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1348 master
->setup
= omap2_mcspi_setup
;
1349 master
->auto_runtime_pm
= true;
1350 master
->transfer_one_message
= omap2_mcspi_transfer_one_message
;
1351 master
->cleanup
= omap2_mcspi_cleanup
;
1352 master
->dev
.of_node
= node
;
1353 master
->max_speed_hz
= OMAP2_MCSPI_MAX_FREQ
;
1354 master
->min_speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> 15;
1356 platform_set_drvdata(pdev
, master
);
1358 mcspi
= spi_master_get_devdata(master
);
1359 mcspi
->master
= master
;
1361 match
= of_match_device(omap_mcspi_of_match
, &pdev
->dev
);
1363 u32 num_cs
= 1; /* default number of chipselect */
1364 pdata
= match
->data
;
1366 of_property_read_u32(node
, "ti,spi-num-cs", &num_cs
);
1367 master
->num_chipselect
= num_cs
;
1368 master
->bus_num
= bus_num
++;
1369 if (of_get_property(node
, "ti,pindir-d0-out-d1-in", NULL
))
1370 mcspi
->pin_dir
= MCSPI_PINDIR_D0_OUT_D1_IN
;
1372 pdata
= dev_get_platdata(&pdev
->dev
);
1373 master
->num_chipselect
= pdata
->num_cs
;
1375 master
->bus_num
= pdev
->id
;
1376 mcspi
->pin_dir
= pdata
->pin_dir
;
1378 regs_offset
= pdata
->regs_offset
;
1380 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1386 r
->start
+= regs_offset
;
1387 r
->end
+= regs_offset
;
1388 mcspi
->phys
= r
->start
;
1390 mcspi
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
1391 if (IS_ERR(mcspi
->base
)) {
1392 status
= PTR_ERR(mcspi
->base
);
1396 mcspi
->dev
= &pdev
->dev
;
1398 INIT_LIST_HEAD(&mcspi
->ctx
.cs
);
1400 mcspi
->dma_channels
= devm_kcalloc(&pdev
->dev
, master
->num_chipselect
,
1401 sizeof(struct omap2_mcspi_dma
),
1403 if (mcspi
->dma_channels
== NULL
) {
1408 for (i
= 0; i
< master
->num_chipselect
; i
++) {
1409 char *dma_rx_ch_name
= mcspi
->dma_channels
[i
].dma_rx_ch_name
;
1410 char *dma_tx_ch_name
= mcspi
->dma_channels
[i
].dma_tx_ch_name
;
1411 struct resource
*dma_res
;
1413 sprintf(dma_rx_ch_name
, "rx%d", i
);
1414 if (!pdev
->dev
.of_node
) {
1416 platform_get_resource_byname(pdev
,
1421 "cannot get DMA RX channel\n");
1426 mcspi
->dma_channels
[i
].dma_rx_sync_dev
=
1429 sprintf(dma_tx_ch_name
, "tx%d", i
);
1430 if (!pdev
->dev
.of_node
) {
1432 platform_get_resource_byname(pdev
,
1437 "cannot get DMA TX channel\n");
1442 mcspi
->dma_channels
[i
].dma_tx_sync_dev
=
1450 pm_runtime_use_autosuspend(&pdev
->dev
);
1451 pm_runtime_set_autosuspend_delay(&pdev
->dev
, SPI_AUTOSUSPEND_TIMEOUT
);
1452 pm_runtime_enable(&pdev
->dev
);
1454 status
= omap2_mcspi_master_setup(mcspi
);
1458 status
= devm_spi_register_master(&pdev
->dev
, master
);
1465 pm_runtime_disable(&pdev
->dev
);
1467 spi_master_put(master
);
1471 static int omap2_mcspi_remove(struct platform_device
*pdev
)
1473 struct spi_master
*master
= platform_get_drvdata(pdev
);
1474 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1476 pm_runtime_put_sync(mcspi
->dev
);
1477 pm_runtime_disable(&pdev
->dev
);
1482 /* work with hotplug and coldplug */
1483 MODULE_ALIAS("platform:omap2_mcspi");
1485 #ifdef CONFIG_SUSPEND
1487 * When SPI wake up from off-mode, CS is in activate state. If it was in
1488 * unactive state when driver was suspend, then force it to unactive state at
1491 static int omap2_mcspi_resume(struct device
*dev
)
1493 struct spi_master
*master
= dev_get_drvdata(dev
);
1494 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1495 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1496 struct omap2_mcspi_cs
*cs
;
1498 pm_runtime_get_sync(mcspi
->dev
);
1499 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1500 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
) == 0) {
1502 * We need to toggle CS state for OMAP take this
1503 * change in account.
1505 cs
->chconf0
|= OMAP2_MCSPI_CHCONF_FORCE
;
1506 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1507 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1508 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1511 pm_runtime_mark_last_busy(mcspi
->dev
);
1512 pm_runtime_put_autosuspend(mcspi
->dev
);
1516 #define omap2_mcspi_resume NULL
1519 static const struct dev_pm_ops omap2_mcspi_pm_ops
= {
1520 .resume
= omap2_mcspi_resume
,
1521 .runtime_resume
= omap_mcspi_runtime_resume
,
1524 static struct platform_driver omap2_mcspi_driver
= {
1526 .name
= "omap2_mcspi",
1527 .pm
= &omap2_mcspi_pm_ops
,
1528 .of_match_table
= omap_mcspi_of_match
,
1530 .probe
= omap2_mcspi_probe
,
1531 .remove
= omap2_mcspi_remove
,
1534 module_platform_driver(omap2_mcspi_driver
);
1535 MODULE_LICENSE("GPL");