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/pinctrl/consumer.h>
28 #include <linux/platform_device.h>
29 #include <linux/err.h>
30 #include <linux/clk.h>
32 #include <linux/slab.h>
33 #include <linux/pm_runtime.h>
35 #include <linux/of_device.h>
36 #include <linux/gcd.h>
38 #include <linux/spi/spi.h>
39 #include <linux/gpio.h>
41 #include <linux/platform_data/spi-omap2-mcspi.h>
43 #define OMAP2_MCSPI_MAX_FREQ 48000000
44 #define OMAP2_MCSPI_MAX_DIVIDER 4096
45 #define OMAP2_MCSPI_MAX_FIFODEPTH 64
46 #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
47 #define SPI_AUTOSUSPEND_TIMEOUT 2000
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
56 #define OMAP2_MCSPI_XFERLEVEL 0x7c
58 /* per-channel banks, 0x14 bytes each, first is: */
59 #define OMAP2_MCSPI_CHCONF0 0x2c
60 #define OMAP2_MCSPI_CHSTAT0 0x30
61 #define OMAP2_MCSPI_CHCTRL0 0x34
62 #define OMAP2_MCSPI_TX0 0x38
63 #define OMAP2_MCSPI_RX0 0x3c
65 /* per-register bitmasks: */
66 #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
68 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
69 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
70 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
72 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
73 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
74 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
75 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
76 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
77 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
78 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
79 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
80 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
81 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
82 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
83 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
84 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
85 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
86 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
87 #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
88 #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
89 #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
91 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
92 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
93 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
94 #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
96 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
97 #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
99 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
101 /* We have 2 DMA channels per CS, one for RX and one for TX */
102 struct omap2_mcspi_dma
{
103 struct dma_chan
*dma_tx
;
104 struct dma_chan
*dma_rx
;
109 struct completion dma_tx_completion
;
110 struct completion dma_rx_completion
;
112 char dma_rx_ch_name
[14];
113 char dma_tx_ch_name
[14];
116 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
117 * cache operations; better heuristics consider wordsize and bitrate.
119 #define DMA_MIN_BYTES 160
123 * Used for context save and restore, structure members to be updated whenever
124 * corresponding registers are modified.
126 struct omap2_mcspi_regs
{
133 struct spi_master
*master
;
134 /* Virtual base address of the controller */
137 /* SPI1 has 4 channels, while SPI2 has 2 */
138 struct omap2_mcspi_dma
*dma_channels
;
140 struct omap2_mcspi_regs ctx
;
142 unsigned int pin_dir
:1;
145 struct omap2_mcspi_cs
{
150 struct list_head node
;
151 /* Context save and restore shadow register */
152 u32 chconf0
, chctrl0
;
155 static inline void mcspi_write_reg(struct spi_master
*master
,
158 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
160 writel_relaxed(val
, mcspi
->base
+ idx
);
163 static inline u32
mcspi_read_reg(struct spi_master
*master
, int idx
)
165 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
167 return readl_relaxed(mcspi
->base
+ idx
);
170 static inline void mcspi_write_cs_reg(const struct spi_device
*spi
,
173 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
175 writel_relaxed(val
, cs
->base
+ idx
);
178 static inline u32
mcspi_read_cs_reg(const struct spi_device
*spi
, int idx
)
180 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
182 return readl_relaxed(cs
->base
+ idx
);
185 static inline u32
mcspi_cached_chconf0(const struct spi_device
*spi
)
187 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
192 static inline void mcspi_write_chconf0(const struct spi_device
*spi
, u32 val
)
194 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
197 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
, val
);
198 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
);
201 static inline int mcspi_bytes_per_word(int word_len
)
205 else if (word_len
<= 16)
207 else /* word_len <= 32 */
211 static void omap2_mcspi_set_dma_req(const struct spi_device
*spi
,
212 int is_read
, int enable
)
216 l
= mcspi_cached_chconf0(spi
);
218 if (is_read
) /* 1 is read, 0 write */
219 rw
= OMAP2_MCSPI_CHCONF_DMAR
;
221 rw
= OMAP2_MCSPI_CHCONF_DMAW
;
228 mcspi_write_chconf0(spi
, l
);
231 static void omap2_mcspi_set_enable(const struct spi_device
*spi
, int enable
)
233 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
238 l
|= OMAP2_MCSPI_CHCTRL_EN
;
240 l
&= ~OMAP2_MCSPI_CHCTRL_EN
;
242 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
243 /* Flash post-writes */
244 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
);
247 static void omap2_mcspi_set_cs(struct spi_device
*spi
, bool enable
)
249 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
252 /* The controller handles the inverted chip selects
253 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
254 * the inversion from the core spi_set_cs function.
256 if (spi
->mode
& SPI_CS_HIGH
)
259 if (spi
->controller_state
) {
260 int err
= pm_runtime_get_sync(mcspi
->dev
);
262 dev_err(mcspi
->dev
, "failed to get sync: %d\n", err
);
266 l
= mcspi_cached_chconf0(spi
);
269 l
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
271 l
|= OMAP2_MCSPI_CHCONF_FORCE
;
273 mcspi_write_chconf0(spi
, l
);
275 pm_runtime_mark_last_busy(mcspi
->dev
);
276 pm_runtime_put_autosuspend(mcspi
->dev
);
280 static void omap2_mcspi_set_master_mode(struct spi_master
*master
)
282 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
283 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
287 * Setup when switching from (reset default) slave mode
288 * to single-channel master mode
290 l
= mcspi_read_reg(master
, OMAP2_MCSPI_MODULCTRL
);
291 l
&= ~(OMAP2_MCSPI_MODULCTRL_STEST
| OMAP2_MCSPI_MODULCTRL_MS
);
292 l
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
293 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, l
);
298 static void omap2_mcspi_set_fifo(const struct spi_device
*spi
,
299 struct spi_transfer
*t
, int enable
)
301 struct spi_master
*master
= spi
->master
;
302 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
303 struct omap2_mcspi
*mcspi
;
305 int max_fifo_depth
, fifo_depth
, bytes_per_word
;
306 u32 chconf
, xferlevel
;
308 mcspi
= spi_master_get_devdata(master
);
310 chconf
= mcspi_cached_chconf0(spi
);
312 bytes_per_word
= mcspi_bytes_per_word(cs
->word_len
);
313 if (t
->len
% bytes_per_word
!= 0)
316 if (t
->rx_buf
!= NULL
&& t
->tx_buf
!= NULL
)
317 max_fifo_depth
= OMAP2_MCSPI_MAX_FIFODEPTH
/ 2;
319 max_fifo_depth
= OMAP2_MCSPI_MAX_FIFODEPTH
;
321 fifo_depth
= gcd(t
->len
, max_fifo_depth
);
322 if (fifo_depth
< 2 || fifo_depth
% bytes_per_word
!= 0)
325 wcnt
= t
->len
/ bytes_per_word
;
326 if (wcnt
> OMAP2_MCSPI_MAX_FIFOWCNT
)
329 xferlevel
= wcnt
<< 16;
330 if (t
->rx_buf
!= NULL
) {
331 chconf
|= OMAP2_MCSPI_CHCONF_FFER
;
332 xferlevel
|= (fifo_depth
- 1) << 8;
334 if (t
->tx_buf
!= NULL
) {
335 chconf
|= OMAP2_MCSPI_CHCONF_FFET
;
336 xferlevel
|= fifo_depth
- 1;
339 mcspi_write_reg(master
, OMAP2_MCSPI_XFERLEVEL
, xferlevel
);
340 mcspi_write_chconf0(spi
, chconf
);
341 mcspi
->fifo_depth
= fifo_depth
;
347 if (t
->rx_buf
!= NULL
)
348 chconf
&= ~OMAP2_MCSPI_CHCONF_FFER
;
350 if (t
->tx_buf
!= NULL
)
351 chconf
&= ~OMAP2_MCSPI_CHCONF_FFET
;
353 mcspi_write_chconf0(spi
, chconf
);
354 mcspi
->fifo_depth
= 0;
357 static void omap2_mcspi_restore_ctx(struct omap2_mcspi
*mcspi
)
359 struct spi_master
*spi_cntrl
= mcspi
->master
;
360 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
361 struct omap2_mcspi_cs
*cs
;
363 /* McSPI: context restore */
364 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_MODULCTRL
, ctx
->modulctrl
);
365 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_WAKEUPENABLE
, ctx
->wakeupenable
);
367 list_for_each_entry(cs
, &ctx
->cs
, node
)
368 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
371 static int mcspi_wait_for_reg_bit(void __iomem
*reg
, unsigned long bit
)
373 unsigned long timeout
;
375 timeout
= jiffies
+ msecs_to_jiffies(1000);
376 while (!(readl_relaxed(reg
) & bit
)) {
377 if (time_after(jiffies
, timeout
)) {
378 if (!(readl_relaxed(reg
) & bit
))
388 static void omap2_mcspi_rx_callback(void *data
)
390 struct spi_device
*spi
= data
;
391 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
392 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
394 /* We must disable the DMA RX request */
395 omap2_mcspi_set_dma_req(spi
, 1, 0);
397 complete(&mcspi_dma
->dma_rx_completion
);
400 static void omap2_mcspi_tx_callback(void *data
)
402 struct spi_device
*spi
= data
;
403 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
404 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
406 /* We must disable the DMA TX request */
407 omap2_mcspi_set_dma_req(spi
, 0, 0);
409 complete(&mcspi_dma
->dma_tx_completion
);
412 static void omap2_mcspi_tx_dma(struct spi_device
*spi
,
413 struct spi_transfer
*xfer
,
414 struct dma_slave_config cfg
)
416 struct omap2_mcspi
*mcspi
;
417 struct omap2_mcspi_dma
*mcspi_dma
;
420 mcspi
= spi_master_get_devdata(spi
->master
);
421 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
424 if (mcspi_dma
->dma_tx
) {
425 struct dma_async_tx_descriptor
*tx
;
427 dmaengine_slave_config(mcspi_dma
->dma_tx
, &cfg
);
429 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_tx
, xfer
->tx_sg
.sgl
,
430 xfer
->tx_sg
.nents
, DMA_MEM_TO_DEV
,
431 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
433 tx
->callback
= omap2_mcspi_tx_callback
;
434 tx
->callback_param
= spi
;
435 dmaengine_submit(tx
);
437 /* FIXME: fall back to PIO? */
440 dma_async_issue_pending(mcspi_dma
->dma_tx
);
441 omap2_mcspi_set_dma_req(spi
, 0, 1);
446 omap2_mcspi_rx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
,
447 struct dma_slave_config cfg
,
450 struct omap2_mcspi
*mcspi
;
451 struct omap2_mcspi_dma
*mcspi_dma
;
452 unsigned int count
, dma_count
;
455 int word_len
, element_count
;
456 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
457 mcspi
= spi_master_get_devdata(spi
->master
);
458 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
460 dma_count
= xfer
->len
;
462 if (mcspi
->fifo_depth
== 0)
465 word_len
= cs
->word_len
;
466 l
= mcspi_cached_chconf0(spi
);
469 element_count
= count
;
470 else if (word_len
<= 16)
471 element_count
= count
>> 1;
472 else /* word_len <= 32 */
473 element_count
= count
>> 2;
475 if (mcspi_dma
->dma_rx
) {
476 struct dma_async_tx_descriptor
*tx
;
478 dmaengine_slave_config(mcspi_dma
->dma_rx
, &cfg
);
480 if ((l
& OMAP2_MCSPI_CHCONF_TURBO
) && mcspi
->fifo_depth
== 0)
483 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_rx
, xfer
->rx_sg
.sgl
,
484 xfer
->rx_sg
.nents
, DMA_DEV_TO_MEM
,
485 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
487 tx
->callback
= omap2_mcspi_rx_callback
;
488 tx
->callback_param
= spi
;
489 dmaengine_submit(tx
);
491 /* FIXME: fall back to PIO? */
495 dma_async_issue_pending(mcspi_dma
->dma_rx
);
496 omap2_mcspi_set_dma_req(spi
, 1, 1);
498 wait_for_completion(&mcspi_dma
->dma_rx_completion
);
500 if (mcspi
->fifo_depth
> 0)
503 omap2_mcspi_set_enable(spi
, 0);
505 elements
= element_count
- 1;
507 if (l
& OMAP2_MCSPI_CHCONF_TURBO
) {
510 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
511 & OMAP2_MCSPI_CHSTAT_RXS
)) {
514 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
516 ((u8
*)xfer
->rx_buf
)[elements
++] = w
;
517 else if (word_len
<= 16)
518 ((u16
*)xfer
->rx_buf
)[elements
++] = w
;
519 else /* word_len <= 32 */
520 ((u32
*)xfer
->rx_buf
)[elements
++] = w
;
522 int bytes_per_word
= mcspi_bytes_per_word(word_len
);
523 dev_err(&spi
->dev
, "DMA RX penultimate word empty\n");
524 count
-= (bytes_per_word
<< 1);
525 omap2_mcspi_set_enable(spi
, 1);
529 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
530 & OMAP2_MCSPI_CHSTAT_RXS
)) {
533 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
535 ((u8
*)xfer
->rx_buf
)[elements
] = w
;
536 else if (word_len
<= 16)
537 ((u16
*)xfer
->rx_buf
)[elements
] = w
;
538 else /* word_len <= 32 */
539 ((u32
*)xfer
->rx_buf
)[elements
] = w
;
541 dev_err(&spi
->dev
, "DMA RX last word empty\n");
542 count
-= mcspi_bytes_per_word(word_len
);
544 omap2_mcspi_set_enable(spi
, 1);
549 omap2_mcspi_txrx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
)
551 struct omap2_mcspi
*mcspi
;
552 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
553 struct omap2_mcspi_dma
*mcspi_dma
;
558 struct dma_slave_config cfg
;
559 enum dma_slave_buswidth width
;
562 void __iomem
*chstat_reg
;
563 void __iomem
*irqstat_reg
;
566 mcspi
= spi_master_get_devdata(spi
->master
);
567 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
568 l
= mcspi_cached_chconf0(spi
);
571 if (cs
->word_len
<= 8) {
572 width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
574 } else if (cs
->word_len
<= 16) {
575 width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
578 width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
585 if (mcspi
->fifo_depth
> 0) {
586 if (count
> mcspi
->fifo_depth
)
587 burst
= mcspi
->fifo_depth
/ es
;
592 memset(&cfg
, 0, sizeof(cfg
));
593 cfg
.src_addr
= cs
->phys
+ OMAP2_MCSPI_RX0
;
594 cfg
.dst_addr
= cs
->phys
+ OMAP2_MCSPI_TX0
;
595 cfg
.src_addr_width
= width
;
596 cfg
.dst_addr_width
= width
;
597 cfg
.src_maxburst
= burst
;
598 cfg
.dst_maxburst
= burst
;
604 omap2_mcspi_tx_dma(spi
, xfer
, cfg
);
607 count
= omap2_mcspi_rx_dma(spi
, xfer
, cfg
, es
);
610 wait_for_completion(&mcspi_dma
->dma_tx_completion
);
612 if (mcspi
->fifo_depth
> 0) {
613 irqstat_reg
= mcspi
->base
+ OMAP2_MCSPI_IRQSTATUS
;
615 if (mcspi_wait_for_reg_bit(irqstat_reg
,
616 OMAP2_MCSPI_IRQSTATUS_EOW
) < 0)
617 dev_err(&spi
->dev
, "EOW timed out\n");
619 mcspi_write_reg(mcspi
->master
, OMAP2_MCSPI_IRQSTATUS
,
620 OMAP2_MCSPI_IRQSTATUS_EOW
);
623 /* for TX_ONLY mode, be sure all words have shifted out */
625 chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
626 if (mcspi
->fifo_depth
> 0) {
627 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
628 OMAP2_MCSPI_CHSTAT_TXFFE
);
630 dev_err(&spi
->dev
, "TXFFE timed out\n");
632 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
633 OMAP2_MCSPI_CHSTAT_TXS
);
635 dev_err(&spi
->dev
, "TXS timed out\n");
638 (mcspi_wait_for_reg_bit(chstat_reg
,
639 OMAP2_MCSPI_CHSTAT_EOT
) < 0))
640 dev_err(&spi
->dev
, "EOT timed out\n");
647 omap2_mcspi_txrx_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
)
649 struct omap2_mcspi
*mcspi
;
650 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
651 unsigned int count
, c
;
653 void __iomem
*base
= cs
->base
;
654 void __iomem
*tx_reg
;
655 void __iomem
*rx_reg
;
656 void __iomem
*chstat_reg
;
659 mcspi
= spi_master_get_devdata(spi
->master
);
662 word_len
= cs
->word_len
;
664 l
= mcspi_cached_chconf0(spi
);
666 /* We store the pre-calculated register addresses on stack to speed
667 * up the transfer loop. */
668 tx_reg
= base
+ OMAP2_MCSPI_TX0
;
669 rx_reg
= base
+ OMAP2_MCSPI_RX0
;
670 chstat_reg
= base
+ OMAP2_MCSPI_CHSTAT0
;
672 if (c
< (word_len
>>3))
685 if (mcspi_wait_for_reg_bit(chstat_reg
,
686 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
687 dev_err(&spi
->dev
, "TXS timed out\n");
690 dev_vdbg(&spi
->dev
, "write-%d %02x\n",
692 writel_relaxed(*tx
++, tx_reg
);
695 if (mcspi_wait_for_reg_bit(chstat_reg
,
696 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
697 dev_err(&spi
->dev
, "RXS timed out\n");
701 if (c
== 1 && tx
== NULL
&&
702 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
703 omap2_mcspi_set_enable(spi
, 0);
704 *rx
++ = readl_relaxed(rx_reg
);
705 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
706 word_len
, *(rx
- 1));
707 if (mcspi_wait_for_reg_bit(chstat_reg
,
708 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
714 } else if (c
== 0 && tx
== NULL
) {
715 omap2_mcspi_set_enable(spi
, 0);
718 *rx
++ = readl_relaxed(rx_reg
);
719 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
720 word_len
, *(rx
- 1));
723 } else if (word_len
<= 16) {
732 if (mcspi_wait_for_reg_bit(chstat_reg
,
733 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
734 dev_err(&spi
->dev
, "TXS timed out\n");
737 dev_vdbg(&spi
->dev
, "write-%d %04x\n",
739 writel_relaxed(*tx
++, tx_reg
);
742 if (mcspi_wait_for_reg_bit(chstat_reg
,
743 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
744 dev_err(&spi
->dev
, "RXS timed out\n");
748 if (c
== 2 && tx
== NULL
&&
749 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
750 omap2_mcspi_set_enable(spi
, 0);
751 *rx
++ = readl_relaxed(rx_reg
);
752 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
753 word_len
, *(rx
- 1));
754 if (mcspi_wait_for_reg_bit(chstat_reg
,
755 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
761 } else if (c
== 0 && tx
== NULL
) {
762 omap2_mcspi_set_enable(spi
, 0);
765 *rx
++ = readl_relaxed(rx_reg
);
766 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
767 word_len
, *(rx
- 1));
770 } else if (word_len
<= 32) {
779 if (mcspi_wait_for_reg_bit(chstat_reg
,
780 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
781 dev_err(&spi
->dev
, "TXS timed out\n");
784 dev_vdbg(&spi
->dev
, "write-%d %08x\n",
786 writel_relaxed(*tx
++, tx_reg
);
789 if (mcspi_wait_for_reg_bit(chstat_reg
,
790 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
791 dev_err(&spi
->dev
, "RXS timed out\n");
795 if (c
== 4 && tx
== NULL
&&
796 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
797 omap2_mcspi_set_enable(spi
, 0);
798 *rx
++ = readl_relaxed(rx_reg
);
799 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
800 word_len
, *(rx
- 1));
801 if (mcspi_wait_for_reg_bit(chstat_reg
,
802 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
808 } else if (c
== 0 && tx
== NULL
) {
809 omap2_mcspi_set_enable(spi
, 0);
812 *rx
++ = readl_relaxed(rx_reg
);
813 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
814 word_len
, *(rx
- 1));
819 /* for TX_ONLY mode, be sure all words have shifted out */
820 if (xfer
->rx_buf
== NULL
) {
821 if (mcspi_wait_for_reg_bit(chstat_reg
,
822 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
823 dev_err(&spi
->dev
, "TXS timed out\n");
824 } else if (mcspi_wait_for_reg_bit(chstat_reg
,
825 OMAP2_MCSPI_CHSTAT_EOT
) < 0)
826 dev_err(&spi
->dev
, "EOT timed out\n");
828 /* disable chan to purge rx datas received in TX_ONLY transfer,
829 * otherwise these rx datas will affect the direct following
832 omap2_mcspi_set_enable(spi
, 0);
835 omap2_mcspi_set_enable(spi
, 1);
839 static u32
omap2_mcspi_calc_divisor(u32 speed_hz
)
843 for (div
= 0; div
< 15; div
++)
844 if (speed_hz
>= (OMAP2_MCSPI_MAX_FREQ
>> div
))
850 /* called only when no transfer is active to this device */
851 static int omap2_mcspi_setup_transfer(struct spi_device
*spi
,
852 struct spi_transfer
*t
)
854 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
855 struct omap2_mcspi
*mcspi
;
856 struct spi_master
*spi_cntrl
;
857 u32 l
= 0, clkd
= 0, div
, extclk
= 0, clkg
= 0;
858 u8 word_len
= spi
->bits_per_word
;
859 u32 speed_hz
= spi
->max_speed_hz
;
861 mcspi
= spi_master_get_devdata(spi
->master
);
862 spi_cntrl
= mcspi
->master
;
864 if (t
!= NULL
&& t
->bits_per_word
)
865 word_len
= t
->bits_per_word
;
867 cs
->word_len
= word_len
;
869 if (t
&& t
->speed_hz
)
870 speed_hz
= t
->speed_hz
;
872 speed_hz
= min_t(u32
, speed_hz
, OMAP2_MCSPI_MAX_FREQ
);
873 if (speed_hz
< (OMAP2_MCSPI_MAX_FREQ
/ OMAP2_MCSPI_MAX_DIVIDER
)) {
874 clkd
= omap2_mcspi_calc_divisor(speed_hz
);
875 speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> clkd
;
878 div
= (OMAP2_MCSPI_MAX_FREQ
+ speed_hz
- 1) / speed_hz
;
879 speed_hz
= OMAP2_MCSPI_MAX_FREQ
/ div
;
880 clkd
= (div
- 1) & 0xf;
881 extclk
= (div
- 1) >> 4;
882 clkg
= OMAP2_MCSPI_CHCONF_CLKG
;
885 l
= mcspi_cached_chconf0(spi
);
887 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
888 * REVISIT: this controller could support SPI_3WIRE mode.
890 if (mcspi
->pin_dir
== MCSPI_PINDIR_D0_IN_D1_OUT
) {
891 l
&= ~OMAP2_MCSPI_CHCONF_IS
;
892 l
&= ~OMAP2_MCSPI_CHCONF_DPE1
;
893 l
|= OMAP2_MCSPI_CHCONF_DPE0
;
895 l
|= OMAP2_MCSPI_CHCONF_IS
;
896 l
|= OMAP2_MCSPI_CHCONF_DPE1
;
897 l
&= ~OMAP2_MCSPI_CHCONF_DPE0
;
901 l
&= ~OMAP2_MCSPI_CHCONF_WL_MASK
;
902 l
|= (word_len
- 1) << 7;
904 /* set chipselect polarity; manage with FORCE */
905 if (!(spi
->mode
& SPI_CS_HIGH
))
906 l
|= OMAP2_MCSPI_CHCONF_EPOL
; /* active-low; normal */
908 l
&= ~OMAP2_MCSPI_CHCONF_EPOL
;
910 /* set clock divisor */
911 l
&= ~OMAP2_MCSPI_CHCONF_CLKD_MASK
;
914 /* set clock granularity */
915 l
&= ~OMAP2_MCSPI_CHCONF_CLKG
;
918 cs
->chctrl0
&= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK
;
919 cs
->chctrl0
|= extclk
<< 8;
920 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
923 /* set SPI mode 0..3 */
924 if (spi
->mode
& SPI_CPOL
)
925 l
|= OMAP2_MCSPI_CHCONF_POL
;
927 l
&= ~OMAP2_MCSPI_CHCONF_POL
;
928 if (spi
->mode
& SPI_CPHA
)
929 l
|= OMAP2_MCSPI_CHCONF_PHA
;
931 l
&= ~OMAP2_MCSPI_CHCONF_PHA
;
933 mcspi_write_chconf0(spi
, l
);
935 cs
->mode
= spi
->mode
;
937 dev_dbg(&spi
->dev
, "setup: speed %d, sample %s edge, clk %s\n",
939 (spi
->mode
& SPI_CPHA
) ? "trailing" : "leading",
940 (spi
->mode
& SPI_CPOL
) ? "inverted" : "normal");
946 * Note that we currently allow DMA only if we get a channel
947 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
949 static int omap2_mcspi_request_dma(struct spi_device
*spi
)
951 struct spi_master
*master
= spi
->master
;
952 struct omap2_mcspi
*mcspi
;
953 struct omap2_mcspi_dma
*mcspi_dma
;
957 mcspi
= spi_master_get_devdata(master
);
958 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
960 init_completion(&mcspi_dma
->dma_rx_completion
);
961 init_completion(&mcspi_dma
->dma_tx_completion
);
964 dma_cap_set(DMA_SLAVE
, mask
);
965 sig
= mcspi_dma
->dma_rx_sync_dev
;
968 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
970 mcspi_dma
->dma_rx_ch_name
);
971 if (!mcspi_dma
->dma_rx
)
974 sig
= mcspi_dma
->dma_tx_sync_dev
;
976 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
978 mcspi_dma
->dma_tx_ch_name
);
980 if (!mcspi_dma
->dma_tx
) {
981 dma_release_channel(mcspi_dma
->dma_rx
);
982 mcspi_dma
->dma_rx
= NULL
;
989 dev_warn(&spi
->dev
, "not using DMA for McSPI\n");
993 static int omap2_mcspi_setup(struct spi_device
*spi
)
996 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
997 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
998 struct omap2_mcspi_dma
*mcspi_dma
;
999 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
1001 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1004 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
1007 cs
->base
= mcspi
->base
+ spi
->chip_select
* 0x14;
1008 cs
->phys
= mcspi
->phys
+ spi
->chip_select
* 0x14;
1012 spi
->controller_state
= cs
;
1013 /* Link this to context save list */
1014 list_add_tail(&cs
->node
, &ctx
->cs
);
1016 if (gpio_is_valid(spi
->cs_gpio
)) {
1017 ret
= gpio_request(spi
->cs_gpio
, dev_name(&spi
->dev
));
1019 dev_err(&spi
->dev
, "failed to request gpio\n");
1022 gpio_direction_output(spi
->cs_gpio
,
1023 !(spi
->mode
& SPI_CS_HIGH
));
1027 if (!mcspi_dma
->dma_rx
|| !mcspi_dma
->dma_tx
) {
1028 ret
= omap2_mcspi_request_dma(spi
);
1029 if (ret
< 0 && ret
!= -EAGAIN
)
1033 ret
= pm_runtime_get_sync(mcspi
->dev
);
1037 ret
= omap2_mcspi_setup_transfer(spi
, NULL
);
1038 pm_runtime_mark_last_busy(mcspi
->dev
);
1039 pm_runtime_put_autosuspend(mcspi
->dev
);
1044 static void omap2_mcspi_cleanup(struct spi_device
*spi
)
1046 struct omap2_mcspi
*mcspi
;
1047 struct omap2_mcspi_dma
*mcspi_dma
;
1048 struct omap2_mcspi_cs
*cs
;
1050 mcspi
= spi_master_get_devdata(spi
->master
);
1052 if (spi
->controller_state
) {
1053 /* Unlink controller state from context save list */
1054 cs
= spi
->controller_state
;
1055 list_del(&cs
->node
);
1060 if (spi
->chip_select
< spi
->master
->num_chipselect
) {
1061 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1063 if (mcspi_dma
->dma_rx
) {
1064 dma_release_channel(mcspi_dma
->dma_rx
);
1065 mcspi_dma
->dma_rx
= NULL
;
1067 if (mcspi_dma
->dma_tx
) {
1068 dma_release_channel(mcspi_dma
->dma_tx
);
1069 mcspi_dma
->dma_tx
= NULL
;
1073 if (gpio_is_valid(spi
->cs_gpio
))
1074 gpio_free(spi
->cs_gpio
);
1077 static bool omap2_mcspi_can_dma(struct spi_master
*master
,
1078 struct spi_device
*spi
,
1079 struct spi_transfer
*xfer
)
1081 if (xfer
->len
< DMA_MIN_BYTES
)
1087 static int omap2_mcspi_work_one(struct omap2_mcspi
*mcspi
,
1088 struct spi_device
*spi
, struct spi_transfer
*t
)
1091 /* We only enable one channel at a time -- the one whose message is
1092 * -- although this controller would gladly
1093 * arbitrate among multiple channels. This corresponds to "single
1094 * channel" master mode. As a side effect, we need to manage the
1095 * chipselect with the FORCE bit ... CS != channel enable.
1098 struct spi_master
*master
;
1099 struct omap2_mcspi_dma
*mcspi_dma
;
1100 struct omap2_mcspi_cs
*cs
;
1101 struct omap2_mcspi_device_config
*cd
;
1102 int par_override
= 0;
1106 master
= spi
->master
;
1107 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1108 cs
= spi
->controller_state
;
1109 cd
= spi
->controller_data
;
1112 * The slave driver could have changed spi->mode in which case
1113 * it will be different from cs->mode (the current hardware setup).
1114 * If so, set par_override (even though its not a parity issue) so
1115 * omap2_mcspi_setup_transfer will be called to configure the hardware
1116 * with the correct mode on the first iteration of the loop below.
1118 if (spi
->mode
!= cs
->mode
)
1121 omap2_mcspi_set_enable(spi
, 0);
1123 if (gpio_is_valid(spi
->cs_gpio
))
1124 omap2_mcspi_set_cs(spi
, spi
->mode
& SPI_CS_HIGH
);
1127 (t
->speed_hz
!= spi
->max_speed_hz
) ||
1128 (t
->bits_per_word
!= spi
->bits_per_word
)) {
1130 status
= omap2_mcspi_setup_transfer(spi
, t
);
1133 if (t
->speed_hz
== spi
->max_speed_hz
&&
1134 t
->bits_per_word
== spi
->bits_per_word
)
1137 if (cd
&& cd
->cs_per_word
) {
1138 chconf
= mcspi
->ctx
.modulctrl
;
1139 chconf
&= ~OMAP2_MCSPI_MODULCTRL_SINGLE
;
1140 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1141 mcspi
->ctx
.modulctrl
=
1142 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1145 chconf
= mcspi_cached_chconf0(spi
);
1146 chconf
&= ~OMAP2_MCSPI_CHCONF_TRM_MASK
;
1147 chconf
&= ~OMAP2_MCSPI_CHCONF_TURBO
;
1149 if (t
->tx_buf
== NULL
)
1150 chconf
|= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY
;
1151 else if (t
->rx_buf
== NULL
)
1152 chconf
|= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY
;
1154 if (cd
&& cd
->turbo_mode
&& t
->tx_buf
== NULL
) {
1155 /* Turbo mode is for more than one word */
1156 if (t
->len
> ((cs
->word_len
+ 7) >> 3))
1157 chconf
|= OMAP2_MCSPI_CHCONF_TURBO
;
1160 mcspi_write_chconf0(spi
, chconf
);
1165 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1166 (t
->len
>= DMA_MIN_BYTES
))
1167 omap2_mcspi_set_fifo(spi
, t
, 1);
1169 omap2_mcspi_set_enable(spi
, 1);
1171 /* RX_ONLY mode needs dummy data in TX reg */
1172 if (t
->tx_buf
== NULL
)
1173 writel_relaxed(0, cs
->base
1176 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1177 (t
->len
>= DMA_MIN_BYTES
))
1178 count
= omap2_mcspi_txrx_dma(spi
, t
);
1180 count
= omap2_mcspi_txrx_pio(spi
, t
);
1182 if (count
!= t
->len
) {
1188 omap2_mcspi_set_enable(spi
, 0);
1190 if (mcspi
->fifo_depth
> 0)
1191 omap2_mcspi_set_fifo(spi
, t
, 0);
1194 /* Restore defaults if they were overriden */
1197 status
= omap2_mcspi_setup_transfer(spi
, NULL
);
1200 if (cd
&& cd
->cs_per_word
) {
1201 chconf
= mcspi
->ctx
.modulctrl
;
1202 chconf
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
1203 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1204 mcspi
->ctx
.modulctrl
=
1205 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1208 omap2_mcspi_set_enable(spi
, 0);
1210 if (gpio_is_valid(spi
->cs_gpio
))
1211 omap2_mcspi_set_cs(spi
, !(spi
->mode
& SPI_CS_HIGH
));
1213 if (mcspi
->fifo_depth
> 0 && t
)
1214 omap2_mcspi_set_fifo(spi
, t
, 0);
1219 static int omap2_mcspi_prepare_message(struct spi_master
*master
,
1220 struct spi_message
*msg
)
1222 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1223 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1224 struct omap2_mcspi_cs
*cs
;
1226 /* Only a single channel can have the FORCE bit enabled
1227 * in its chconf0 register.
1228 * Scan all channels and disable them except the current one.
1229 * A FORCE can remain from a last transfer having cs_change enabled
1231 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1232 if (msg
->spi
->controller_state
== cs
)
1235 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
)) {
1236 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1237 writel_relaxed(cs
->chconf0
,
1238 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1239 readl_relaxed(cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1246 static int omap2_mcspi_transfer_one(struct spi_master
*master
,
1247 struct spi_device
*spi
, struct spi_transfer
*t
)
1249 struct omap2_mcspi
*mcspi
;
1250 struct omap2_mcspi_dma
*mcspi_dma
;
1251 const void *tx_buf
= t
->tx_buf
;
1252 void *rx_buf
= t
->rx_buf
;
1253 unsigned len
= t
->len
;
1255 mcspi
= spi_master_get_devdata(master
);
1256 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1258 if ((len
&& !(rx_buf
|| tx_buf
))) {
1259 dev_dbg(mcspi
->dev
, "transfer: %d Hz, %d %s%s, %d bpw\n",
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
->prepare_message
= omap2_mcspi_prepare_message
;
1348 master
->transfer_one
= omap2_mcspi_transfer_one
;
1349 master
->set_cs
= omap2_mcspi_set_cs
;
1350 master
->cleanup
= omap2_mcspi_cleanup
;
1351 master
->can_dma
= omap2_mcspi_can_dma
;
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_dont_use_autosuspend(&pdev
->dev
);
1466 pm_runtime_put_sync(&pdev
->dev
);
1467 pm_runtime_disable(&pdev
->dev
);
1469 spi_master_put(master
);
1473 static int omap2_mcspi_remove(struct platform_device
*pdev
)
1475 struct spi_master
*master
= platform_get_drvdata(pdev
);
1476 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1478 pm_runtime_dont_use_autosuspend(mcspi
->dev
);
1479 pm_runtime_put_sync(mcspi
->dev
);
1480 pm_runtime_disable(&pdev
->dev
);
1485 /* work with hotplug and coldplug */
1486 MODULE_ALIAS("platform:omap2_mcspi");
1488 #ifdef CONFIG_SUSPEND
1490 * When SPI wake up from off-mode, CS is in activate state. If it was in
1491 * unactive state when driver was suspend, then force it to unactive state at
1494 static int omap2_mcspi_resume(struct device
*dev
)
1496 struct spi_master
*master
= dev_get_drvdata(dev
);
1497 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1498 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1499 struct omap2_mcspi_cs
*cs
;
1501 pm_runtime_get_sync(mcspi
->dev
);
1502 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1503 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
) == 0) {
1505 * We need to toggle CS state for OMAP take this
1506 * change in account.
1508 cs
->chconf0
|= OMAP2_MCSPI_CHCONF_FORCE
;
1509 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1510 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1511 writel_relaxed(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1514 pm_runtime_mark_last_busy(mcspi
->dev
);
1515 pm_runtime_put_autosuspend(mcspi
->dev
);
1517 return pinctrl_pm_select_default_state(dev
);
1520 static int omap2_mcspi_suspend(struct device
*dev
)
1522 return pinctrl_pm_select_sleep_state(dev
);
1526 #define omap2_mcspi_suspend NULL
1527 #define omap2_mcspi_resume NULL
1530 static const struct dev_pm_ops omap2_mcspi_pm_ops
= {
1531 .resume
= omap2_mcspi_resume
,
1532 .suspend
= omap2_mcspi_suspend
,
1533 .runtime_resume
= omap_mcspi_runtime_resume
,
1536 static struct platform_driver omap2_mcspi_driver
= {
1538 .name
= "omap2_mcspi",
1539 .pm
= &omap2_mcspi_pm_ops
,
1540 .of_match_table
= omap_mcspi_of_match
,
1542 .probe
= omap2_mcspi_probe
,
1543 .remove
= omap2_mcspi_remove
,
1546 module_platform_driver(omap2_mcspi_driver
);
1547 MODULE_LICENSE("GPL");