19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dc4dc360SLaxman Dewangan /*
3dc4dc360SLaxman Dewangan * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
4dc4dc360SLaxman Dewangan *
5dc4dc360SLaxman Dewangan * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6dc4dc360SLaxman Dewangan */
7dc4dc360SLaxman Dewangan
8dc4dc360SLaxman Dewangan #include <linux/clk.h>
9dc4dc360SLaxman Dewangan #include <linux/completion.h>
10dc4dc360SLaxman Dewangan #include <linux/delay.h>
11dc4dc360SLaxman Dewangan #include <linux/dmaengine.h>
12dc4dc360SLaxman Dewangan #include <linux/dma-mapping.h>
13dc4dc360SLaxman Dewangan #include <linux/dmapool.h>
14dc4dc360SLaxman Dewangan #include <linux/err.h>
15dc4dc360SLaxman Dewangan #include <linux/interrupt.h>
16dc4dc360SLaxman Dewangan #include <linux/io.h>
17dc4dc360SLaxman Dewangan #include <linux/kernel.h>
18dc4dc360SLaxman Dewangan #include <linux/kthread.h>
19dc4dc360SLaxman Dewangan #include <linux/module.h>
20dc4dc360SLaxman Dewangan #include <linux/platform_device.h>
2107f83755SDmitry Osipenko #include <linux/pm_opp.h>
22dc4dc360SLaxman Dewangan #include <linux/pm_runtime.h>
23dc4dc360SLaxman Dewangan #include <linux/of.h>
24ff2251e3SStephen Warren #include <linux/reset.h>
25dc4dc360SLaxman Dewangan #include <linux/spi/spi.h>
26dc4dc360SLaxman Dewangan
2707f83755SDmitry Osipenko #include <soc/tegra/common.h>
2807f83755SDmitry Osipenko
29dc4dc360SLaxman Dewangan #define SLINK_COMMAND 0x000
30dc4dc360SLaxman Dewangan #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
31dc4dc360SLaxman Dewangan #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
32dc4dc360SLaxman Dewangan #define SLINK_BOTH_EN (1 << 10)
33dc4dc360SLaxman Dewangan #define SLINK_CS_SW (1 << 11)
34dc4dc360SLaxman Dewangan #define SLINK_CS_VALUE (1 << 12)
35dc4dc360SLaxman Dewangan #define SLINK_CS_POLARITY (1 << 13)
36dc4dc360SLaxman Dewangan #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
37dc4dc360SLaxman Dewangan #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
38dc4dc360SLaxman Dewangan #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
39dc4dc360SLaxman Dewangan #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
40dc4dc360SLaxman Dewangan #define SLINK_IDLE_SDA_MASK (3 << 16)
41dc4dc360SLaxman Dewangan #define SLINK_CS_POLARITY1 (1 << 20)
42dc4dc360SLaxman Dewangan #define SLINK_CK_SDA (1 << 21)
43dc4dc360SLaxman Dewangan #define SLINK_CS_POLARITY2 (1 << 22)
44dc4dc360SLaxman Dewangan #define SLINK_CS_POLARITY3 (1 << 23)
45dc4dc360SLaxman Dewangan #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
46dc4dc360SLaxman Dewangan #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
47dc4dc360SLaxman Dewangan #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
48dc4dc360SLaxman Dewangan #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
49dc4dc360SLaxman Dewangan #define SLINK_IDLE_SCLK_MASK (3 << 24)
50dc4dc360SLaxman Dewangan #define SLINK_M_S (1 << 28)
51dc4dc360SLaxman Dewangan #define SLINK_WAIT (1 << 29)
52dc4dc360SLaxman Dewangan #define SLINK_GO (1 << 30)
53dc4dc360SLaxman Dewangan #define SLINK_ENB (1 << 31)
54dc4dc360SLaxman Dewangan
55dc4dc360SLaxman Dewangan #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
56dc4dc360SLaxman Dewangan
57dc4dc360SLaxman Dewangan #define SLINK_COMMAND2 0x004
58dc4dc360SLaxman Dewangan #define SLINK_LSBFE (1 << 0)
59dc4dc360SLaxman Dewangan #define SLINK_SSOE (1 << 1)
60dc4dc360SLaxman Dewangan #define SLINK_SPIE (1 << 4)
61dc4dc360SLaxman Dewangan #define SLINK_BIDIROE (1 << 6)
62dc4dc360SLaxman Dewangan #define SLINK_MODFEN (1 << 7)
63dc4dc360SLaxman Dewangan #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
64dc4dc360SLaxman Dewangan #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
65dc4dc360SLaxman Dewangan #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
66dc4dc360SLaxman Dewangan #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
67dc4dc360SLaxman Dewangan #define SLINK_FIFO_REFILLS_0 (0 << 22)
68dc4dc360SLaxman Dewangan #define SLINK_FIFO_REFILLS_1 (1 << 22)
69dc4dc360SLaxman Dewangan #define SLINK_FIFO_REFILLS_2 (2 << 22)
70dc4dc360SLaxman Dewangan #define SLINK_FIFO_REFILLS_3 (3 << 22)
71dc4dc360SLaxman Dewangan #define SLINK_FIFO_REFILLS_MASK (3 << 22)
72dc4dc360SLaxman Dewangan #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
73dc4dc360SLaxman Dewangan #define SLINK_SPC0 (1 << 29)
74dc4dc360SLaxman Dewangan #define SLINK_TXEN (1 << 30)
75dc4dc360SLaxman Dewangan #define SLINK_RXEN (1 << 31)
76dc4dc360SLaxman Dewangan
77dc4dc360SLaxman Dewangan #define SLINK_STATUS 0x008
78dc4dc360SLaxman Dewangan #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
79dc4dc360SLaxman Dewangan #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
80dc4dc360SLaxman Dewangan #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
81dc4dc360SLaxman Dewangan #define SLINK_MODF (1 << 16)
82dc4dc360SLaxman Dewangan #define SLINK_RX_UNF (1 << 18)
83dc4dc360SLaxman Dewangan #define SLINK_TX_OVF (1 << 19)
84dc4dc360SLaxman Dewangan #define SLINK_TX_FULL (1 << 20)
85dc4dc360SLaxman Dewangan #define SLINK_TX_EMPTY (1 << 21)
86dc4dc360SLaxman Dewangan #define SLINK_RX_FULL (1 << 22)
87dc4dc360SLaxman Dewangan #define SLINK_RX_EMPTY (1 << 23)
88dc4dc360SLaxman Dewangan #define SLINK_TX_UNF (1 << 24)
89dc4dc360SLaxman Dewangan #define SLINK_RX_OVF (1 << 25)
90dc4dc360SLaxman Dewangan #define SLINK_TX_FLUSH (1 << 26)
91dc4dc360SLaxman Dewangan #define SLINK_RX_FLUSH (1 << 27)
92dc4dc360SLaxman Dewangan #define SLINK_SCLK (1 << 28)
93dc4dc360SLaxman Dewangan #define SLINK_ERR (1 << 29)
94dc4dc360SLaxman Dewangan #define SLINK_RDY (1 << 30)
95dc4dc360SLaxman Dewangan #define SLINK_BSY (1 << 31)
96dc4dc360SLaxman Dewangan #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
97dc4dc360SLaxman Dewangan SLINK_TX_UNF | SLINK_RX_OVF)
98dc4dc360SLaxman Dewangan
99dc4dc360SLaxman Dewangan #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
100dc4dc360SLaxman Dewangan
101dc4dc360SLaxman Dewangan #define SLINK_MAS_DATA 0x010
102dc4dc360SLaxman Dewangan #define SLINK_SLAVE_DATA 0x014
103dc4dc360SLaxman Dewangan
104dc4dc360SLaxman Dewangan #define SLINK_DMA_CTL 0x018
105dc4dc360SLaxman Dewangan #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
106dc4dc360SLaxman Dewangan #define SLINK_TX_TRIG_1 (0 << 16)
107dc4dc360SLaxman Dewangan #define SLINK_TX_TRIG_4 (1 << 16)
108dc4dc360SLaxman Dewangan #define SLINK_TX_TRIG_8 (2 << 16)
109dc4dc360SLaxman Dewangan #define SLINK_TX_TRIG_16 (3 << 16)
110dc4dc360SLaxman Dewangan #define SLINK_TX_TRIG_MASK (3 << 16)
111dc4dc360SLaxman Dewangan #define SLINK_RX_TRIG_1 (0 << 18)
112dc4dc360SLaxman Dewangan #define SLINK_RX_TRIG_4 (1 << 18)
113dc4dc360SLaxman Dewangan #define SLINK_RX_TRIG_8 (2 << 18)
114dc4dc360SLaxman Dewangan #define SLINK_RX_TRIG_16 (3 << 18)
115dc4dc360SLaxman Dewangan #define SLINK_RX_TRIG_MASK (3 << 18)
116dc4dc360SLaxman Dewangan #define SLINK_PACKED (1 << 20)
117dc4dc360SLaxman Dewangan #define SLINK_PACK_SIZE_4 (0 << 21)
118dc4dc360SLaxman Dewangan #define SLINK_PACK_SIZE_8 (1 << 21)
119dc4dc360SLaxman Dewangan #define SLINK_PACK_SIZE_16 (2 << 21)
120dc4dc360SLaxman Dewangan #define SLINK_PACK_SIZE_32 (3 << 21)
121dc4dc360SLaxman Dewangan #define SLINK_PACK_SIZE_MASK (3 << 21)
122dc4dc360SLaxman Dewangan #define SLINK_IE_TXC (1 << 26)
123dc4dc360SLaxman Dewangan #define SLINK_IE_RXC (1 << 27)
124dc4dc360SLaxman Dewangan #define SLINK_DMA_EN (1 << 31)
125dc4dc360SLaxman Dewangan
126dc4dc360SLaxman Dewangan #define SLINK_STATUS2 0x01c
127dc4dc360SLaxman Dewangan #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
128dc4dc360SLaxman Dewangan #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
129dc4dc360SLaxman Dewangan #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
130dc4dc360SLaxman Dewangan
131dc4dc360SLaxman Dewangan #define SLINK_TX_FIFO 0x100
132dc4dc360SLaxman Dewangan #define SLINK_RX_FIFO 0x180
133dc4dc360SLaxman Dewangan
134dc4dc360SLaxman Dewangan #define DATA_DIR_TX (1 << 0)
135dc4dc360SLaxman Dewangan #define DATA_DIR_RX (1 << 1)
136dc4dc360SLaxman Dewangan
137dc4dc360SLaxman Dewangan #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
138dc4dc360SLaxman Dewangan
139dc4dc360SLaxman Dewangan #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
140dc4dc360SLaxman Dewangan #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
141dc4dc360SLaxman Dewangan #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
142dc4dc360SLaxman Dewangan
143dc4dc360SLaxman Dewangan #define SLINK_STATUS2_RESET \
144dc4dc360SLaxman Dewangan (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
145dc4dc360SLaxman Dewangan
146dc4dc360SLaxman Dewangan #define MAX_CHIP_SELECT 4
147dc4dc360SLaxman Dewangan #define SLINK_FIFO_DEPTH 32
148dc4dc360SLaxman Dewangan
149dc4dc360SLaxman Dewangan struct tegra_slink_chip_data {
150dc4dc360SLaxman Dewangan bool cs_hold_time;
151dc4dc360SLaxman Dewangan };
152dc4dc360SLaxman Dewangan
153dc4dc360SLaxman Dewangan struct tegra_slink_data {
154dc4dc360SLaxman Dewangan struct device *dev;
155*db34aad4SYang Yingliang struct spi_controller *host;
156dc4dc360SLaxman Dewangan const struct tegra_slink_chip_data *chip_data;
157dc4dc360SLaxman Dewangan spinlock_t lock;
158dc4dc360SLaxman Dewangan
159dc4dc360SLaxman Dewangan struct clk *clk;
160ff2251e3SStephen Warren struct reset_control *rst;
161dc4dc360SLaxman Dewangan void __iomem *base;
162dc4dc360SLaxman Dewangan phys_addr_t phys;
163dc4dc360SLaxman Dewangan unsigned irq;
164dc4dc360SLaxman Dewangan u32 cur_speed;
165dc4dc360SLaxman Dewangan
166dc4dc360SLaxman Dewangan struct spi_device *cur_spi;
167dc4dc360SLaxman Dewangan unsigned cur_pos;
168dc4dc360SLaxman Dewangan unsigned cur_len;
169dc4dc360SLaxman Dewangan unsigned words_per_32bit;
170dc4dc360SLaxman Dewangan unsigned bytes_per_word;
171dc4dc360SLaxman Dewangan unsigned curr_dma_words;
172dc4dc360SLaxman Dewangan unsigned cur_direction;
173dc4dc360SLaxman Dewangan
174dc4dc360SLaxman Dewangan unsigned cur_rx_pos;
175dc4dc360SLaxman Dewangan unsigned cur_tx_pos;
176dc4dc360SLaxman Dewangan
177dc4dc360SLaxman Dewangan unsigned dma_buf_size;
178dc4dc360SLaxman Dewangan unsigned max_buf_size;
179dc4dc360SLaxman Dewangan bool is_curr_dma_xfer;
180dc4dc360SLaxman Dewangan
181dc4dc360SLaxman Dewangan struct completion rx_dma_complete;
182dc4dc360SLaxman Dewangan struct completion tx_dma_complete;
183dc4dc360SLaxman Dewangan
184dc4dc360SLaxman Dewangan u32 tx_status;
185dc4dc360SLaxman Dewangan u32 rx_status;
186dc4dc360SLaxman Dewangan u32 status_reg;
187dc4dc360SLaxman Dewangan bool is_packed;
1885fd38677SMichal Nazarewicz u32 packed_size;
189dc4dc360SLaxman Dewangan
190dc4dc360SLaxman Dewangan u32 command_reg;
191dc4dc360SLaxman Dewangan u32 command2_reg;
192dc4dc360SLaxman Dewangan u32 dma_control_reg;
193dc4dc360SLaxman Dewangan u32 def_command_reg;
194dc4dc360SLaxman Dewangan u32 def_command2_reg;
195dc4dc360SLaxman Dewangan
196dc4dc360SLaxman Dewangan struct completion xfer_completion;
197dc4dc360SLaxman Dewangan struct spi_transfer *curr_xfer;
198dc4dc360SLaxman Dewangan struct dma_chan *rx_dma_chan;
199dc4dc360SLaxman Dewangan u32 *rx_dma_buf;
200dc4dc360SLaxman Dewangan dma_addr_t rx_dma_phys;
201dc4dc360SLaxman Dewangan struct dma_async_tx_descriptor *rx_dma_desc;
202dc4dc360SLaxman Dewangan
203dc4dc360SLaxman Dewangan struct dma_chan *tx_dma_chan;
204dc4dc360SLaxman Dewangan u32 *tx_dma_buf;
205dc4dc360SLaxman Dewangan dma_addr_t tx_dma_phys;
206dc4dc360SLaxman Dewangan struct dma_async_tx_descriptor *tx_dma_desc;
207dc4dc360SLaxman Dewangan };
208dc4dc360SLaxman Dewangan
tegra_slink_readl(struct tegra_slink_data * tspi,unsigned long reg)2095fd38677SMichal Nazarewicz static inline u32 tegra_slink_readl(struct tegra_slink_data *tspi,
210dc4dc360SLaxman Dewangan unsigned long reg)
211dc4dc360SLaxman Dewangan {
212dc4dc360SLaxman Dewangan return readl(tspi->base + reg);
213dc4dc360SLaxman Dewangan }
214dc4dc360SLaxman Dewangan
tegra_slink_writel(struct tegra_slink_data * tspi,u32 val,unsigned long reg)215dc4dc360SLaxman Dewangan static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
2165fd38677SMichal Nazarewicz u32 val, unsigned long reg)
217dc4dc360SLaxman Dewangan {
218dc4dc360SLaxman Dewangan writel(val, tspi->base + reg);
219dc4dc360SLaxman Dewangan
220dc4dc360SLaxman Dewangan /* Read back register to make sure that register writes completed */
221dc4dc360SLaxman Dewangan if (reg != SLINK_TX_FIFO)
222dc4dc360SLaxman Dewangan readl(tspi->base + SLINK_MAS_DATA);
223dc4dc360SLaxman Dewangan }
224dc4dc360SLaxman Dewangan
tegra_slink_clear_status(struct tegra_slink_data * tspi)225dc4dc360SLaxman Dewangan static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
226dc4dc360SLaxman Dewangan {
2275fd38677SMichal Nazarewicz u32 val_write;
228dc4dc360SLaxman Dewangan
2295fd38677SMichal Nazarewicz tegra_slink_readl(tspi, SLINK_STATUS);
230dc4dc360SLaxman Dewangan
231dc4dc360SLaxman Dewangan /* Write 1 to clear status register */
232dc4dc360SLaxman Dewangan val_write = SLINK_RDY | SLINK_FIFO_ERROR;
233dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, val_write, SLINK_STATUS);
234dc4dc360SLaxman Dewangan }
235dc4dc360SLaxman Dewangan
tegra_slink_get_packed_size(struct tegra_slink_data * tspi,struct spi_transfer * t)2365fd38677SMichal Nazarewicz static u32 tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
237dc4dc360SLaxman Dewangan struct spi_transfer *t)
238dc4dc360SLaxman Dewangan {
239dc4dc360SLaxman Dewangan switch (tspi->bytes_per_word) {
240dc4dc360SLaxman Dewangan case 0:
2415fd38677SMichal Nazarewicz return SLINK_PACK_SIZE_4;
242dc4dc360SLaxman Dewangan case 1:
2435fd38677SMichal Nazarewicz return SLINK_PACK_SIZE_8;
244dc4dc360SLaxman Dewangan case 2:
2455fd38677SMichal Nazarewicz return SLINK_PACK_SIZE_16;
246dc4dc360SLaxman Dewangan case 4:
2475fd38677SMichal Nazarewicz return SLINK_PACK_SIZE_32;
248dc4dc360SLaxman Dewangan default:
2495fd38677SMichal Nazarewicz return 0;
250dc4dc360SLaxman Dewangan }
251dc4dc360SLaxman Dewangan }
252dc4dc360SLaxman Dewangan
tegra_slink_calculate_curr_xfer_param(struct spi_device * spi,struct tegra_slink_data * tspi,struct spi_transfer * t)253dc4dc360SLaxman Dewangan static unsigned tegra_slink_calculate_curr_xfer_param(
254dc4dc360SLaxman Dewangan struct spi_device *spi, struct tegra_slink_data *tspi,
255dc4dc360SLaxman Dewangan struct spi_transfer *t)
256dc4dc360SLaxman Dewangan {
257dc4dc360SLaxman Dewangan unsigned remain_len = t->len - tspi->cur_pos;
258dc4dc360SLaxman Dewangan unsigned max_word;
259dc4dc360SLaxman Dewangan unsigned bits_per_word;
260dc4dc360SLaxman Dewangan unsigned max_len;
261dc4dc360SLaxman Dewangan unsigned total_fifo_words;
262dc4dc360SLaxman Dewangan
263766ed704SLaxman Dewangan bits_per_word = t->bits_per_word;
264e91d2352SAxel Lin tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
265dc4dc360SLaxman Dewangan
266dc4dc360SLaxman Dewangan if (bits_per_word == 8 || bits_per_word == 16) {
2672172a332SGustavo A. R. Silva tspi->is_packed = true;
268dc4dc360SLaxman Dewangan tspi->words_per_32bit = 32/bits_per_word;
269dc4dc360SLaxman Dewangan } else {
2702172a332SGustavo A. R. Silva tspi->is_packed = false;
271dc4dc360SLaxman Dewangan tspi->words_per_32bit = 1;
272dc4dc360SLaxman Dewangan }
273dc4dc360SLaxman Dewangan tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
274dc4dc360SLaxman Dewangan
275dc4dc360SLaxman Dewangan if (tspi->is_packed) {
276dc4dc360SLaxman Dewangan max_len = min(remain_len, tspi->max_buf_size);
277dc4dc360SLaxman Dewangan tspi->curr_dma_words = max_len/tspi->bytes_per_word;
278dc4dc360SLaxman Dewangan total_fifo_words = max_len/4;
279dc4dc360SLaxman Dewangan } else {
280dc4dc360SLaxman Dewangan max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
281dc4dc360SLaxman Dewangan max_word = min(max_word, tspi->max_buf_size/4);
282dc4dc360SLaxman Dewangan tspi->curr_dma_words = max_word;
283dc4dc360SLaxman Dewangan total_fifo_words = max_word;
284dc4dc360SLaxman Dewangan }
285dc4dc360SLaxman Dewangan return total_fifo_words;
286dc4dc360SLaxman Dewangan }
287dc4dc360SLaxman Dewangan
tegra_slink_fill_tx_fifo_from_client_txbuf(struct tegra_slink_data * tspi,struct spi_transfer * t)288dc4dc360SLaxman Dewangan static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
289dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi, struct spi_transfer *t)
290dc4dc360SLaxman Dewangan {
291dc4dc360SLaxman Dewangan unsigned nbytes;
292dc4dc360SLaxman Dewangan unsigned tx_empty_count;
2935fd38677SMichal Nazarewicz u32 fifo_status;
294dc4dc360SLaxman Dewangan unsigned max_n_32bit;
295dc4dc360SLaxman Dewangan unsigned i, count;
296dc4dc360SLaxman Dewangan unsigned int written_words;
297dc4dc360SLaxman Dewangan unsigned fifo_words_left;
298dc4dc360SLaxman Dewangan u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
299dc4dc360SLaxman Dewangan
300dc4dc360SLaxman Dewangan fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
301dc4dc360SLaxman Dewangan tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
302dc4dc360SLaxman Dewangan
303dc4dc360SLaxman Dewangan if (tspi->is_packed) {
304dc4dc360SLaxman Dewangan fifo_words_left = tx_empty_count * tspi->words_per_32bit;
305dc4dc360SLaxman Dewangan written_words = min(fifo_words_left, tspi->curr_dma_words);
306dc4dc360SLaxman Dewangan nbytes = written_words * tspi->bytes_per_word;
307dc4dc360SLaxman Dewangan max_n_32bit = DIV_ROUND_UP(nbytes, 4);
308dc4dc360SLaxman Dewangan for (count = 0; count < max_n_32bit; count++) {
3095fd38677SMichal Nazarewicz u32 x = 0;
310dc4dc360SLaxman Dewangan for (i = 0; (i < 4) && nbytes; i++, nbytes--)
3115fd38677SMichal Nazarewicz x |= (u32)(*tx_buf++) << (i * 8);
312dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
313dc4dc360SLaxman Dewangan }
314dc4dc360SLaxman Dewangan } else {
315dc4dc360SLaxman Dewangan max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
316dc4dc360SLaxman Dewangan written_words = max_n_32bit;
317dc4dc360SLaxman Dewangan nbytes = written_words * tspi->bytes_per_word;
318dc4dc360SLaxman Dewangan for (count = 0; count < max_n_32bit; count++) {
3195fd38677SMichal Nazarewicz u32 x = 0;
320dc4dc360SLaxman Dewangan for (i = 0; nbytes && (i < tspi->bytes_per_word);
321dc4dc360SLaxman Dewangan i++, nbytes--)
3225fd38677SMichal Nazarewicz x |= (u32)(*tx_buf++) << (i * 8);
323dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
324dc4dc360SLaxman Dewangan }
325dc4dc360SLaxman Dewangan }
326dc4dc360SLaxman Dewangan tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
327dc4dc360SLaxman Dewangan return written_words;
328dc4dc360SLaxman Dewangan }
329dc4dc360SLaxman Dewangan
tegra_slink_read_rx_fifo_to_client_rxbuf(struct tegra_slink_data * tspi,struct spi_transfer * t)330dc4dc360SLaxman Dewangan static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
331dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi, struct spi_transfer *t)
332dc4dc360SLaxman Dewangan {
333dc4dc360SLaxman Dewangan unsigned rx_full_count;
3345fd38677SMichal Nazarewicz u32 fifo_status;
335dc4dc360SLaxman Dewangan unsigned i, count;
336dc4dc360SLaxman Dewangan unsigned int read_words = 0;
337dc4dc360SLaxman Dewangan unsigned len;
338dc4dc360SLaxman Dewangan u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
339dc4dc360SLaxman Dewangan
340dc4dc360SLaxman Dewangan fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
341dc4dc360SLaxman Dewangan rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
342dc4dc360SLaxman Dewangan if (tspi->is_packed) {
343dc4dc360SLaxman Dewangan len = tspi->curr_dma_words * tspi->bytes_per_word;
344dc4dc360SLaxman Dewangan for (count = 0; count < rx_full_count; count++) {
3455fd38677SMichal Nazarewicz u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
346dc4dc360SLaxman Dewangan for (i = 0; len && (i < 4); i++, len--)
347dc4dc360SLaxman Dewangan *rx_buf++ = (x >> i*8) & 0xFF;
348dc4dc360SLaxman Dewangan }
349dc4dc360SLaxman Dewangan tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
350dc4dc360SLaxman Dewangan read_words += tspi->curr_dma_words;
351dc4dc360SLaxman Dewangan } else {
352dc4dc360SLaxman Dewangan for (count = 0; count < rx_full_count; count++) {
3535fd38677SMichal Nazarewicz u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
354dc4dc360SLaxman Dewangan for (i = 0; (i < tspi->bytes_per_word); i++)
355dc4dc360SLaxman Dewangan *rx_buf++ = (x >> (i*8)) & 0xFF;
356dc4dc360SLaxman Dewangan }
357dc4dc360SLaxman Dewangan tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
358dc4dc360SLaxman Dewangan read_words += rx_full_count;
359dc4dc360SLaxman Dewangan }
360dc4dc360SLaxman Dewangan return read_words;
361dc4dc360SLaxman Dewangan }
362dc4dc360SLaxman Dewangan
tegra_slink_copy_client_txbuf_to_spi_txbuf(struct tegra_slink_data * tspi,struct spi_transfer * t)363dc4dc360SLaxman Dewangan static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
364dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi, struct spi_transfer *t)
365dc4dc360SLaxman Dewangan {
366dc4dc360SLaxman Dewangan /* Make the dma buffer to read by cpu */
367dc4dc360SLaxman Dewangan dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
368dc4dc360SLaxman Dewangan tspi->dma_buf_size, DMA_TO_DEVICE);
369dc4dc360SLaxman Dewangan
370dc4dc360SLaxman Dewangan if (tspi->is_packed) {
3715fd38677SMichal Nazarewicz unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
372dc4dc360SLaxman Dewangan memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
373dc4dc360SLaxman Dewangan } else {
374dc4dc360SLaxman Dewangan unsigned int i;
375dc4dc360SLaxman Dewangan unsigned int count;
376dc4dc360SLaxman Dewangan u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
377dc4dc360SLaxman Dewangan unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
378dc4dc360SLaxman Dewangan
379dc4dc360SLaxman Dewangan for (count = 0; count < tspi->curr_dma_words; count++) {
3805fd38677SMichal Nazarewicz u32 x = 0;
381dc4dc360SLaxman Dewangan for (i = 0; consume && (i < tspi->bytes_per_word);
382dc4dc360SLaxman Dewangan i++, consume--)
3835fd38677SMichal Nazarewicz x |= (u32)(*tx_buf++) << (i * 8);
384dc4dc360SLaxman Dewangan tspi->tx_dma_buf[count] = x;
385dc4dc360SLaxman Dewangan }
386dc4dc360SLaxman Dewangan }
387dc4dc360SLaxman Dewangan tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
388dc4dc360SLaxman Dewangan
389dc4dc360SLaxman Dewangan /* Make the dma buffer to read by dma */
390dc4dc360SLaxman Dewangan dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
391dc4dc360SLaxman Dewangan tspi->dma_buf_size, DMA_TO_DEVICE);
392dc4dc360SLaxman Dewangan }
393dc4dc360SLaxman Dewangan
tegra_slink_copy_spi_rxbuf_to_client_rxbuf(struct tegra_slink_data * tspi,struct spi_transfer * t)394dc4dc360SLaxman Dewangan static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
395dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi, struct spi_transfer *t)
396dc4dc360SLaxman Dewangan {
397dc4dc360SLaxman Dewangan unsigned len;
398dc4dc360SLaxman Dewangan
399dc4dc360SLaxman Dewangan /* Make the dma buffer to read by cpu */
400dc4dc360SLaxman Dewangan dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
401dc4dc360SLaxman Dewangan tspi->dma_buf_size, DMA_FROM_DEVICE);
402dc4dc360SLaxman Dewangan
403dc4dc360SLaxman Dewangan if (tspi->is_packed) {
404dc4dc360SLaxman Dewangan len = tspi->curr_dma_words * tspi->bytes_per_word;
405dc4dc360SLaxman Dewangan memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
406dc4dc360SLaxman Dewangan } else {
407dc4dc360SLaxman Dewangan unsigned int i;
408dc4dc360SLaxman Dewangan unsigned int count;
409dc4dc360SLaxman Dewangan unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
4105fd38677SMichal Nazarewicz u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
411dc4dc360SLaxman Dewangan
412dc4dc360SLaxman Dewangan for (count = 0; count < tspi->curr_dma_words; count++) {
4135fd38677SMichal Nazarewicz u32 x = tspi->rx_dma_buf[count] & rx_mask;
414dc4dc360SLaxman Dewangan for (i = 0; (i < tspi->bytes_per_word); i++)
415dc4dc360SLaxman Dewangan *rx_buf++ = (x >> (i*8)) & 0xFF;
416dc4dc360SLaxman Dewangan }
417dc4dc360SLaxman Dewangan }
418dc4dc360SLaxman Dewangan tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
419dc4dc360SLaxman Dewangan
420dc4dc360SLaxman Dewangan /* Make the dma buffer to read by dma */
421dc4dc360SLaxman Dewangan dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
422dc4dc360SLaxman Dewangan tspi->dma_buf_size, DMA_FROM_DEVICE);
423dc4dc360SLaxman Dewangan }
424dc4dc360SLaxman Dewangan
tegra_slink_dma_complete(void * args)425dc4dc360SLaxman Dewangan static void tegra_slink_dma_complete(void *args)
426dc4dc360SLaxman Dewangan {
427dc4dc360SLaxman Dewangan struct completion *dma_complete = args;
428dc4dc360SLaxman Dewangan
429dc4dc360SLaxman Dewangan complete(dma_complete);
430dc4dc360SLaxman Dewangan }
431dc4dc360SLaxman Dewangan
tegra_slink_start_tx_dma(struct tegra_slink_data * tspi,int len)432dc4dc360SLaxman Dewangan static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
433dc4dc360SLaxman Dewangan {
43416735d02SWolfram Sang reinit_completion(&tspi->tx_dma_complete);
435dc4dc360SLaxman Dewangan tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
436dc4dc360SLaxman Dewangan tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
43772919f34SMark Brown DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
438dc4dc360SLaxman Dewangan if (!tspi->tx_dma_desc) {
439dc4dc360SLaxman Dewangan dev_err(tspi->dev, "Not able to get desc for Tx\n");
440dc4dc360SLaxman Dewangan return -EIO;
441dc4dc360SLaxman Dewangan }
442dc4dc360SLaxman Dewangan
443dc4dc360SLaxman Dewangan tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
444dc4dc360SLaxman Dewangan tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
445dc4dc360SLaxman Dewangan
446dc4dc360SLaxman Dewangan dmaengine_submit(tspi->tx_dma_desc);
447dc4dc360SLaxman Dewangan dma_async_issue_pending(tspi->tx_dma_chan);
448dc4dc360SLaxman Dewangan return 0;
449dc4dc360SLaxman Dewangan }
450dc4dc360SLaxman Dewangan
tegra_slink_start_rx_dma(struct tegra_slink_data * tspi,int len)451dc4dc360SLaxman Dewangan static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
452dc4dc360SLaxman Dewangan {
45316735d02SWolfram Sang reinit_completion(&tspi->rx_dma_complete);
454dc4dc360SLaxman Dewangan tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
455dc4dc360SLaxman Dewangan tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
45672919f34SMark Brown DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
457dc4dc360SLaxman Dewangan if (!tspi->rx_dma_desc) {
458dc4dc360SLaxman Dewangan dev_err(tspi->dev, "Not able to get desc for Rx\n");
459dc4dc360SLaxman Dewangan return -EIO;
460dc4dc360SLaxman Dewangan }
461dc4dc360SLaxman Dewangan
462dc4dc360SLaxman Dewangan tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
463dc4dc360SLaxman Dewangan tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
464dc4dc360SLaxman Dewangan
465dc4dc360SLaxman Dewangan dmaengine_submit(tspi->rx_dma_desc);
466dc4dc360SLaxman Dewangan dma_async_issue_pending(tspi->rx_dma_chan);
467dc4dc360SLaxman Dewangan return 0;
468dc4dc360SLaxman Dewangan }
469dc4dc360SLaxman Dewangan
tegra_slink_start_dma_based_transfer(struct tegra_slink_data * tspi,struct spi_transfer * t)470dc4dc360SLaxman Dewangan static int tegra_slink_start_dma_based_transfer(
471dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi, struct spi_transfer *t)
472dc4dc360SLaxman Dewangan {
4735fd38677SMichal Nazarewicz u32 val;
474dc4dc360SLaxman Dewangan unsigned int len;
475dc4dc360SLaxman Dewangan int ret = 0;
4765fd38677SMichal Nazarewicz u32 status;
477dc4dc360SLaxman Dewangan
478dc4dc360SLaxman Dewangan /* Make sure that Rx and Tx fifo are empty */
479dc4dc360SLaxman Dewangan status = tegra_slink_readl(tspi, SLINK_STATUS);
480dc4dc360SLaxman Dewangan if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
4815fd38677SMichal Nazarewicz dev_err(tspi->dev, "Rx/Tx fifo are not empty status 0x%08x\n",
4825fd38677SMichal Nazarewicz (unsigned)status);
483dc4dc360SLaxman Dewangan return -EIO;
484dc4dc360SLaxman Dewangan }
485dc4dc360SLaxman Dewangan
486dc4dc360SLaxman Dewangan val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
487dc4dc360SLaxman Dewangan val |= tspi->packed_size;
488dc4dc360SLaxman Dewangan if (tspi->is_packed)
489dc4dc360SLaxman Dewangan len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
490dc4dc360SLaxman Dewangan 4) * 4;
491dc4dc360SLaxman Dewangan else
492dc4dc360SLaxman Dewangan len = tspi->curr_dma_words * 4;
493dc4dc360SLaxman Dewangan
494dc4dc360SLaxman Dewangan /* Set attention level based on length of transfer */
495dc4dc360SLaxman Dewangan if (len & 0xF)
496dc4dc360SLaxman Dewangan val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
497dc4dc360SLaxman Dewangan else if (((len) >> 4) & 0x1)
498dc4dc360SLaxman Dewangan val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
499dc4dc360SLaxman Dewangan else
500dc4dc360SLaxman Dewangan val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
501dc4dc360SLaxman Dewangan
502dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX)
503dc4dc360SLaxman Dewangan val |= SLINK_IE_TXC;
504dc4dc360SLaxman Dewangan
505dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_RX)
506dc4dc360SLaxman Dewangan val |= SLINK_IE_RXC;
507dc4dc360SLaxman Dewangan
508dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
509dc4dc360SLaxman Dewangan tspi->dma_control_reg = val;
510dc4dc360SLaxman Dewangan
511dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX) {
512dc4dc360SLaxman Dewangan tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
513dc4dc360SLaxman Dewangan wmb();
514dc4dc360SLaxman Dewangan ret = tegra_slink_start_tx_dma(tspi, len);
515dc4dc360SLaxman Dewangan if (ret < 0) {
516dc4dc360SLaxman Dewangan dev_err(tspi->dev,
517dc4dc360SLaxman Dewangan "Starting tx dma failed, err %d\n", ret);
518dc4dc360SLaxman Dewangan return ret;
519dc4dc360SLaxman Dewangan }
520dc4dc360SLaxman Dewangan
521dc4dc360SLaxman Dewangan /* Wait for tx fifo to be fill before starting slink */
5225fd38677SMichal Nazarewicz status = tegra_slink_readl(tspi, SLINK_STATUS);
5235fd38677SMichal Nazarewicz while (!(status & SLINK_TX_FULL))
5245fd38677SMichal Nazarewicz status = tegra_slink_readl(tspi, SLINK_STATUS);
525dc4dc360SLaxman Dewangan }
526dc4dc360SLaxman Dewangan
527dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_RX) {
528dc4dc360SLaxman Dewangan /* Make the dma buffer to read by dma */
529dc4dc360SLaxman Dewangan dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
530dc4dc360SLaxman Dewangan tspi->dma_buf_size, DMA_FROM_DEVICE);
531dc4dc360SLaxman Dewangan
532dc4dc360SLaxman Dewangan ret = tegra_slink_start_rx_dma(tspi, len);
533dc4dc360SLaxman Dewangan if (ret < 0) {
534dc4dc360SLaxman Dewangan dev_err(tspi->dev,
535dc4dc360SLaxman Dewangan "Starting rx dma failed, err %d\n", ret);
536dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX)
537dc4dc360SLaxman Dewangan dmaengine_terminate_all(tspi->tx_dma_chan);
538dc4dc360SLaxman Dewangan return ret;
539dc4dc360SLaxman Dewangan }
540dc4dc360SLaxman Dewangan }
541dc4dc360SLaxman Dewangan tspi->is_curr_dma_xfer = true;
542dc4dc360SLaxman Dewangan if (tspi->is_packed) {
543dc4dc360SLaxman Dewangan val |= SLINK_PACKED;
544dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
545dc4dc360SLaxman Dewangan /* HW need small delay after settign Packed mode */
546dc4dc360SLaxman Dewangan udelay(1);
547dc4dc360SLaxman Dewangan }
548dc4dc360SLaxman Dewangan tspi->dma_control_reg = val;
549dc4dc360SLaxman Dewangan
550dc4dc360SLaxman Dewangan val |= SLINK_DMA_EN;
551dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
552dc4dc360SLaxman Dewangan return ret;
553dc4dc360SLaxman Dewangan }
554dc4dc360SLaxman Dewangan
tegra_slink_start_cpu_based_transfer(struct tegra_slink_data * tspi,struct spi_transfer * t)555dc4dc360SLaxman Dewangan static int tegra_slink_start_cpu_based_transfer(
556dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi, struct spi_transfer *t)
557dc4dc360SLaxman Dewangan {
5585fd38677SMichal Nazarewicz u32 val;
559dc4dc360SLaxman Dewangan unsigned cur_words;
560dc4dc360SLaxman Dewangan
561dc4dc360SLaxman Dewangan val = tspi->packed_size;
562dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX)
563dc4dc360SLaxman Dewangan val |= SLINK_IE_TXC;
564dc4dc360SLaxman Dewangan
565dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_RX)
566dc4dc360SLaxman Dewangan val |= SLINK_IE_RXC;
567dc4dc360SLaxman Dewangan
568dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
569dc4dc360SLaxman Dewangan tspi->dma_control_reg = val;
570dc4dc360SLaxman Dewangan
571dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX)
572dc4dc360SLaxman Dewangan cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
573dc4dc360SLaxman Dewangan else
574dc4dc360SLaxman Dewangan cur_words = tspi->curr_dma_words;
575dc4dc360SLaxman Dewangan val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
576dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
577dc4dc360SLaxman Dewangan tspi->dma_control_reg = val;
578dc4dc360SLaxman Dewangan
579dc4dc360SLaxman Dewangan tspi->is_curr_dma_xfer = false;
580dc4dc360SLaxman Dewangan if (tspi->is_packed) {
581dc4dc360SLaxman Dewangan val |= SLINK_PACKED;
582dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
583dc4dc360SLaxman Dewangan udelay(1);
584dc4dc360SLaxman Dewangan wmb();
585dc4dc360SLaxman Dewangan }
586dc4dc360SLaxman Dewangan tspi->dma_control_reg = val;
587dc4dc360SLaxman Dewangan val |= SLINK_DMA_EN;
588dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
589dc4dc360SLaxman Dewangan return 0;
590dc4dc360SLaxman Dewangan }
591dc4dc360SLaxman Dewangan
tegra_slink_init_dma_param(struct tegra_slink_data * tspi,bool dma_to_memory)592dc4dc360SLaxman Dewangan static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
593dc4dc360SLaxman Dewangan bool dma_to_memory)
594dc4dc360SLaxman Dewangan {
595dc4dc360SLaxman Dewangan struct dma_chan *dma_chan;
596dc4dc360SLaxman Dewangan u32 *dma_buf;
597dc4dc360SLaxman Dewangan dma_addr_t dma_phys;
598dc4dc360SLaxman Dewangan int ret;
599dc4dc360SLaxman Dewangan struct dma_slave_config dma_sconfig;
600dc4dc360SLaxman Dewangan
601912a7df4SPeter Ujfalusi dma_chan = dma_request_chan(tspi->dev, dma_to_memory ? "rx" : "tx");
6027708aff1SKrzysztof Kozlowski if (IS_ERR(dma_chan))
6037708aff1SKrzysztof Kozlowski return dev_err_probe(tspi->dev, PTR_ERR(dma_chan),
6047708aff1SKrzysztof Kozlowski "Dma channel is not available\n");
605dc4dc360SLaxman Dewangan
606dc4dc360SLaxman Dewangan dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
607dc4dc360SLaxman Dewangan &dma_phys, GFP_KERNEL);
608dc4dc360SLaxman Dewangan if (!dma_buf) {
609dc4dc360SLaxman Dewangan dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
610dc4dc360SLaxman Dewangan dma_release_channel(dma_chan);
611dc4dc360SLaxman Dewangan return -ENOMEM;
612dc4dc360SLaxman Dewangan }
613dc4dc360SLaxman Dewangan
614dc4dc360SLaxman Dewangan if (dma_to_memory) {
615dc4dc360SLaxman Dewangan dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
616dc4dc360SLaxman Dewangan dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
617dc4dc360SLaxman Dewangan dma_sconfig.src_maxburst = 0;
618dc4dc360SLaxman Dewangan } else {
619dc4dc360SLaxman Dewangan dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
620dc4dc360SLaxman Dewangan dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
621dc4dc360SLaxman Dewangan dma_sconfig.dst_maxburst = 0;
622dc4dc360SLaxman Dewangan }
623dc4dc360SLaxman Dewangan
624dc4dc360SLaxman Dewangan ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
625dc4dc360SLaxman Dewangan if (ret)
626dc4dc360SLaxman Dewangan goto scrub;
627dc4dc360SLaxman Dewangan if (dma_to_memory) {
628dc4dc360SLaxman Dewangan tspi->rx_dma_chan = dma_chan;
629dc4dc360SLaxman Dewangan tspi->rx_dma_buf = dma_buf;
630dc4dc360SLaxman Dewangan tspi->rx_dma_phys = dma_phys;
631dc4dc360SLaxman Dewangan } else {
632dc4dc360SLaxman Dewangan tspi->tx_dma_chan = dma_chan;
633dc4dc360SLaxman Dewangan tspi->tx_dma_buf = dma_buf;
634dc4dc360SLaxman Dewangan tspi->tx_dma_phys = dma_phys;
635dc4dc360SLaxman Dewangan }
636dc4dc360SLaxman Dewangan return 0;
637dc4dc360SLaxman Dewangan
638dc4dc360SLaxman Dewangan scrub:
639dc4dc360SLaxman Dewangan dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
640dc4dc360SLaxman Dewangan dma_release_channel(dma_chan);
641dc4dc360SLaxman Dewangan return ret;
642dc4dc360SLaxman Dewangan }
643dc4dc360SLaxman Dewangan
tegra_slink_deinit_dma_param(struct tegra_slink_data * tspi,bool dma_to_memory)644dc4dc360SLaxman Dewangan static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
645dc4dc360SLaxman Dewangan bool dma_to_memory)
646dc4dc360SLaxman Dewangan {
647dc4dc360SLaxman Dewangan u32 *dma_buf;
648dc4dc360SLaxman Dewangan dma_addr_t dma_phys;
649dc4dc360SLaxman Dewangan struct dma_chan *dma_chan;
650dc4dc360SLaxman Dewangan
651dc4dc360SLaxman Dewangan if (dma_to_memory) {
652dc4dc360SLaxman Dewangan dma_buf = tspi->rx_dma_buf;
653dc4dc360SLaxman Dewangan dma_chan = tspi->rx_dma_chan;
654dc4dc360SLaxman Dewangan dma_phys = tspi->rx_dma_phys;
655dc4dc360SLaxman Dewangan tspi->rx_dma_chan = NULL;
656dc4dc360SLaxman Dewangan tspi->rx_dma_buf = NULL;
657dc4dc360SLaxman Dewangan } else {
658dc4dc360SLaxman Dewangan dma_buf = tspi->tx_dma_buf;
659dc4dc360SLaxman Dewangan dma_chan = tspi->tx_dma_chan;
660dc4dc360SLaxman Dewangan dma_phys = tspi->tx_dma_phys;
661dc4dc360SLaxman Dewangan tspi->tx_dma_buf = NULL;
662dc4dc360SLaxman Dewangan tspi->tx_dma_chan = NULL;
663dc4dc360SLaxman Dewangan }
664dc4dc360SLaxman Dewangan if (!dma_chan)
665dc4dc360SLaxman Dewangan return;
666dc4dc360SLaxman Dewangan
667dc4dc360SLaxman Dewangan dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
668dc4dc360SLaxman Dewangan dma_release_channel(dma_chan);
669dc4dc360SLaxman Dewangan }
670dc4dc360SLaxman Dewangan
tegra_slink_start_transfer_one(struct spi_device * spi,struct spi_transfer * t)671dc4dc360SLaxman Dewangan static int tegra_slink_start_transfer_one(struct spi_device *spi,
672f178e3deSMark Brown struct spi_transfer *t)
673dc4dc360SLaxman Dewangan {
674*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(spi->controller);
675dc4dc360SLaxman Dewangan u32 speed;
676dc4dc360SLaxman Dewangan u8 bits_per_word;
677dc4dc360SLaxman Dewangan unsigned total_fifo_words;
678dc4dc360SLaxman Dewangan int ret;
6795fd38677SMichal Nazarewicz u32 command;
6805fd38677SMichal Nazarewicz u32 command2;
681dc4dc360SLaxman Dewangan
682e6811d1dSLaxman Dewangan bits_per_word = t->bits_per_word;
683beb96c2aSLaxman Dewangan speed = t->speed_hz;
684dc4dc360SLaxman Dewangan if (speed != tspi->cur_speed) {
68507f83755SDmitry Osipenko dev_pm_opp_set_rate(tspi->dev, speed * 4);
686dc4dc360SLaxman Dewangan tspi->cur_speed = speed;
687dc4dc360SLaxman Dewangan }
688dc4dc360SLaxman Dewangan
689dc4dc360SLaxman Dewangan tspi->cur_spi = spi;
690dc4dc360SLaxman Dewangan tspi->cur_pos = 0;
691dc4dc360SLaxman Dewangan tspi->cur_rx_pos = 0;
692dc4dc360SLaxman Dewangan tspi->cur_tx_pos = 0;
693dc4dc360SLaxman Dewangan tspi->curr_xfer = t;
694dc4dc360SLaxman Dewangan total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
695dc4dc360SLaxman Dewangan
696dc4dc360SLaxman Dewangan command = tspi->command_reg;
697dc4dc360SLaxman Dewangan command &= ~SLINK_BIT_LENGTH(~0);
698dc4dc360SLaxman Dewangan command |= SLINK_BIT_LENGTH(bits_per_word - 1);
699dc4dc360SLaxman Dewangan
700dc4dc360SLaxman Dewangan command2 = tspi->command2_reg;
701dc4dc360SLaxman Dewangan command2 &= ~(SLINK_RXEN | SLINK_TXEN);
702dc4dc360SLaxman Dewangan
703dc4dc360SLaxman Dewangan tspi->cur_direction = 0;
704dc4dc360SLaxman Dewangan if (t->rx_buf) {
705dc4dc360SLaxman Dewangan command2 |= SLINK_RXEN;
706dc4dc360SLaxman Dewangan tspi->cur_direction |= DATA_DIR_RX;
707dc4dc360SLaxman Dewangan }
708dc4dc360SLaxman Dewangan if (t->tx_buf) {
709dc4dc360SLaxman Dewangan command2 |= SLINK_TXEN;
710dc4dc360SLaxman Dewangan tspi->cur_direction |= DATA_DIR_TX;
711dc4dc360SLaxman Dewangan }
7120e694df3SRandolph Maaßen
7130e694df3SRandolph Maaßen /*
7140e694df3SRandolph Maaßen * Writing to the command2 register bevore the command register prevents
7150e694df3SRandolph Maaßen * a spike in chip_select line 0. This selects the chip_select line
7160e694df3SRandolph Maaßen * before changing the chip_select value.
7170e694df3SRandolph Maaßen */
718dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
719dc4dc360SLaxman Dewangan tspi->command2_reg = command2;
720dc4dc360SLaxman Dewangan
7210e694df3SRandolph Maaßen tegra_slink_writel(tspi, command, SLINK_COMMAND);
7220e694df3SRandolph Maaßen tspi->command_reg = command;
7230e694df3SRandolph Maaßen
724dc4dc360SLaxman Dewangan if (total_fifo_words > SLINK_FIFO_DEPTH)
725dc4dc360SLaxman Dewangan ret = tegra_slink_start_dma_based_transfer(tspi, t);
726dc4dc360SLaxman Dewangan else
727dc4dc360SLaxman Dewangan ret = tegra_slink_start_cpu_based_transfer(tspi, t);
728dc4dc360SLaxman Dewangan return ret;
729dc4dc360SLaxman Dewangan }
730dc4dc360SLaxman Dewangan
tegra_slink_setup(struct spi_device * spi)731dc4dc360SLaxman Dewangan static int tegra_slink_setup(struct spi_device *spi)
732dc4dc360SLaxman Dewangan {
7335fd38677SMichal Nazarewicz static const u32 cs_pol_bit[MAX_CHIP_SELECT] = {
734dc4dc360SLaxman Dewangan SLINK_CS_POLARITY,
735dc4dc360SLaxman Dewangan SLINK_CS_POLARITY1,
736dc4dc360SLaxman Dewangan SLINK_CS_POLARITY2,
737dc4dc360SLaxman Dewangan SLINK_CS_POLARITY3,
738dc4dc360SLaxman Dewangan };
739dc4dc360SLaxman Dewangan
740*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(spi->controller);
7415fd38677SMichal Nazarewicz u32 val;
7425fd38677SMichal Nazarewicz unsigned long flags;
7435fd38677SMichal Nazarewicz int ret;
7445fd38677SMichal Nazarewicz
745dc4dc360SLaxman Dewangan dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
746dc4dc360SLaxman Dewangan spi->bits_per_word,
747dc4dc360SLaxman Dewangan spi->mode & SPI_CPOL ? "" : "~",
748dc4dc360SLaxman Dewangan spi->mode & SPI_CPHA ? "" : "~",
749dc4dc360SLaxman Dewangan spi->max_speed_hz);
750dc4dc360SLaxman Dewangan
7511e6f8bd1SMinghao Chi ret = pm_runtime_resume_and_get(tspi->dev);
752dc4dc360SLaxman Dewangan if (ret < 0) {
753dc4dc360SLaxman Dewangan dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
754dc4dc360SLaxman Dewangan return ret;
755dc4dc360SLaxman Dewangan }
756dc4dc360SLaxman Dewangan
757dc4dc360SLaxman Dewangan spin_lock_irqsave(&tspi->lock, flags);
758dc4dc360SLaxman Dewangan val = tspi->def_command_reg;
759dc4dc360SLaxman Dewangan if (spi->mode & SPI_CS_HIGH)
7609e264f3fSAmit Kumar Mahapatra via Alsa-devel val |= cs_pol_bit[spi_get_chipselect(spi, 0)];
761dc4dc360SLaxman Dewangan else
7629e264f3fSAmit Kumar Mahapatra via Alsa-devel val &= ~cs_pol_bit[spi_get_chipselect(spi, 0)];
763dc4dc360SLaxman Dewangan tspi->def_command_reg = val;
764dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
765dc4dc360SLaxman Dewangan spin_unlock_irqrestore(&tspi->lock, flags);
766dc4dc360SLaxman Dewangan
767dc4dc360SLaxman Dewangan pm_runtime_put(tspi->dev);
768dc4dc360SLaxman Dewangan return 0;
769dc4dc360SLaxman Dewangan }
770dc4dc360SLaxman Dewangan
tegra_slink_prepare_message(struct spi_controller * host,struct spi_message * msg)771*db34aad4SYang Yingliang static int tegra_slink_prepare_message(struct spi_controller *host,
772dc4dc360SLaxman Dewangan struct spi_message *msg)
773dc4dc360SLaxman Dewangan {
774*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(host);
775dc4dc360SLaxman Dewangan struct spi_device *spi = msg->spi;
77663fc184cSMark Brown
777f178e3deSMark Brown tegra_slink_clear_status(tspi);
778f178e3deSMark Brown
779f178e3deSMark Brown tspi->command_reg = tspi->def_command_reg;
780f178e3deSMark Brown tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
781f178e3deSMark Brown
782f178e3deSMark Brown tspi->command2_reg = tspi->def_command2_reg;
7839e264f3fSAmit Kumar Mahapatra via Alsa-devel tspi->command2_reg |= SLINK_SS_EN_CS(spi_get_chipselect(spi, 0));
784f178e3deSMark Brown
785f178e3deSMark Brown tspi->command_reg &= ~SLINK_MODES;
786f178e3deSMark Brown if (spi->mode & SPI_CPHA)
787f178e3deSMark Brown tspi->command_reg |= SLINK_CK_SDA;
788f178e3deSMark Brown
789f178e3deSMark Brown if (spi->mode & SPI_CPOL)
790f178e3deSMark Brown tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
791f178e3deSMark Brown else
792f178e3deSMark Brown tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
79363fc184cSMark Brown
79463fc184cSMark Brown return 0;
79563fc184cSMark Brown }
79663fc184cSMark Brown
tegra_slink_transfer_one(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)797*db34aad4SYang Yingliang static int tegra_slink_transfer_one(struct spi_controller *host,
79863fc184cSMark Brown struct spi_device *spi,
79963fc184cSMark Brown struct spi_transfer *xfer)
80063fc184cSMark Brown {
801*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(host);
802dc4dc360SLaxman Dewangan int ret;
803dc4dc360SLaxman Dewangan
80416735d02SWolfram Sang reinit_completion(&tspi->xfer_completion);
805f178e3deSMark Brown ret = tegra_slink_start_transfer_one(spi, xfer);
806dc4dc360SLaxman Dewangan if (ret < 0) {
807dc4dc360SLaxman Dewangan dev_err(tspi->dev,
808dc4dc360SLaxman Dewangan "spi can not start transfer, err %d\n", ret);
80963fc184cSMark Brown return ret;
810dc4dc360SLaxman Dewangan }
811f178e3deSMark Brown
812dc4dc360SLaxman Dewangan ret = wait_for_completion_timeout(&tspi->xfer_completion,
813dc4dc360SLaxman Dewangan SLINK_DMA_TIMEOUT);
814dc4dc360SLaxman Dewangan if (WARN_ON(ret == 0)) {
815dc4dc360SLaxman Dewangan dev_err(tspi->dev,
816bfca7618SColin Ian King "spi transfer timeout, err %d\n", ret);
81763fc184cSMark Brown return -EIO;
818dc4dc360SLaxman Dewangan }
819dc4dc360SLaxman Dewangan
82063fc184cSMark Brown if (tspi->tx_status)
82163fc184cSMark Brown return tspi->tx_status;
82263fc184cSMark Brown if (tspi->rx_status)
82363fc184cSMark Brown return tspi->rx_status;
82463fc184cSMark Brown
82563fc184cSMark Brown return 0;
826dc4dc360SLaxman Dewangan }
82763fc184cSMark Brown
tegra_slink_unprepare_message(struct spi_controller * host,struct spi_message * msg)828*db34aad4SYang Yingliang static int tegra_slink_unprepare_message(struct spi_controller *host,
82963fc184cSMark Brown struct spi_message *msg)
83063fc184cSMark Brown {
831*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(host);
83263fc184cSMark Brown
833dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
834dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
83563fc184cSMark Brown
83663fc184cSMark Brown return 0;
837dc4dc360SLaxman Dewangan }
838dc4dc360SLaxman Dewangan
handle_cpu_based_xfer(struct tegra_slink_data * tspi)839dc4dc360SLaxman Dewangan static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
840dc4dc360SLaxman Dewangan {
841dc4dc360SLaxman Dewangan struct spi_transfer *t = tspi->curr_xfer;
842dc4dc360SLaxman Dewangan unsigned long flags;
843dc4dc360SLaxman Dewangan
844dc4dc360SLaxman Dewangan spin_lock_irqsave(&tspi->lock, flags);
845dc4dc360SLaxman Dewangan if (tspi->tx_status || tspi->rx_status ||
846dc4dc360SLaxman Dewangan (tspi->status_reg & SLINK_BSY)) {
847dc4dc360SLaxman Dewangan dev_err(tspi->dev,
848dc4dc360SLaxman Dewangan "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
849dc4dc360SLaxman Dewangan dev_err(tspi->dev,
850dc4dc360SLaxman Dewangan "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
851dc4dc360SLaxman Dewangan tspi->command2_reg, tspi->dma_control_reg);
852ff2251e3SStephen Warren reset_control_assert(tspi->rst);
853dc4dc360SLaxman Dewangan udelay(2);
854ff2251e3SStephen Warren reset_control_deassert(tspi->rst);
855dc4dc360SLaxman Dewangan complete(&tspi->xfer_completion);
856dc4dc360SLaxman Dewangan goto exit;
857dc4dc360SLaxman Dewangan }
858dc4dc360SLaxman Dewangan
859dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_RX)
860dc4dc360SLaxman Dewangan tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
861dc4dc360SLaxman Dewangan
862dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX)
863dc4dc360SLaxman Dewangan tspi->cur_pos = tspi->cur_tx_pos;
864dc4dc360SLaxman Dewangan else
865dc4dc360SLaxman Dewangan tspi->cur_pos = tspi->cur_rx_pos;
866dc4dc360SLaxman Dewangan
867dc4dc360SLaxman Dewangan if (tspi->cur_pos == t->len) {
868dc4dc360SLaxman Dewangan complete(&tspi->xfer_completion);
869dc4dc360SLaxman Dewangan goto exit;
870dc4dc360SLaxman Dewangan }
871dc4dc360SLaxman Dewangan
872dc4dc360SLaxman Dewangan tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
873dc4dc360SLaxman Dewangan tegra_slink_start_cpu_based_transfer(tspi, t);
874dc4dc360SLaxman Dewangan exit:
875dc4dc360SLaxman Dewangan spin_unlock_irqrestore(&tspi->lock, flags);
876dc4dc360SLaxman Dewangan return IRQ_HANDLED;
877dc4dc360SLaxman Dewangan }
878dc4dc360SLaxman Dewangan
handle_dma_based_xfer(struct tegra_slink_data * tspi)879dc4dc360SLaxman Dewangan static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
880dc4dc360SLaxman Dewangan {
881dc4dc360SLaxman Dewangan struct spi_transfer *t = tspi->curr_xfer;
882dc4dc360SLaxman Dewangan long wait_status;
883dc4dc360SLaxman Dewangan int err = 0;
884dc4dc360SLaxman Dewangan unsigned total_fifo_words;
885dc4dc360SLaxman Dewangan unsigned long flags;
886dc4dc360SLaxman Dewangan
887dc4dc360SLaxman Dewangan /* Abort dmas if any error */
888dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX) {
889dc4dc360SLaxman Dewangan if (tspi->tx_status) {
890dc4dc360SLaxman Dewangan dmaengine_terminate_all(tspi->tx_dma_chan);
891dc4dc360SLaxman Dewangan err += 1;
892dc4dc360SLaxman Dewangan } else {
893dc4dc360SLaxman Dewangan wait_status = wait_for_completion_interruptible_timeout(
894dc4dc360SLaxman Dewangan &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
895dc4dc360SLaxman Dewangan if (wait_status <= 0) {
896dc4dc360SLaxman Dewangan dmaengine_terminate_all(tspi->tx_dma_chan);
897dc4dc360SLaxman Dewangan dev_err(tspi->dev, "TxDma Xfer failed\n");
898dc4dc360SLaxman Dewangan err += 1;
899dc4dc360SLaxman Dewangan }
900dc4dc360SLaxman Dewangan }
901dc4dc360SLaxman Dewangan }
902dc4dc360SLaxman Dewangan
903dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_RX) {
904dc4dc360SLaxman Dewangan if (tspi->rx_status) {
905dc4dc360SLaxman Dewangan dmaengine_terminate_all(tspi->rx_dma_chan);
906dc4dc360SLaxman Dewangan err += 2;
907dc4dc360SLaxman Dewangan } else {
908dc4dc360SLaxman Dewangan wait_status = wait_for_completion_interruptible_timeout(
909dc4dc360SLaxman Dewangan &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
910dc4dc360SLaxman Dewangan if (wait_status <= 0) {
911dc4dc360SLaxman Dewangan dmaengine_terminate_all(tspi->rx_dma_chan);
912dc4dc360SLaxman Dewangan dev_err(tspi->dev, "RxDma Xfer failed\n");
913dc4dc360SLaxman Dewangan err += 2;
914dc4dc360SLaxman Dewangan }
915dc4dc360SLaxman Dewangan }
916dc4dc360SLaxman Dewangan }
917dc4dc360SLaxman Dewangan
918dc4dc360SLaxman Dewangan spin_lock_irqsave(&tspi->lock, flags);
919dc4dc360SLaxman Dewangan if (err) {
920dc4dc360SLaxman Dewangan dev_err(tspi->dev,
921dc4dc360SLaxman Dewangan "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
922dc4dc360SLaxman Dewangan dev_err(tspi->dev,
923dc4dc360SLaxman Dewangan "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
924dc4dc360SLaxman Dewangan tspi->command2_reg, tspi->dma_control_reg);
925ff2251e3SStephen Warren reset_control_assert(tspi->rst);
926dc4dc360SLaxman Dewangan udelay(2);
927ff2251e3SStephen Warren reset_control_assert(tspi->rst);
928dc4dc360SLaxman Dewangan complete(&tspi->xfer_completion);
929dc4dc360SLaxman Dewangan spin_unlock_irqrestore(&tspi->lock, flags);
930dc4dc360SLaxman Dewangan return IRQ_HANDLED;
931dc4dc360SLaxman Dewangan }
932dc4dc360SLaxman Dewangan
933dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_RX)
934dc4dc360SLaxman Dewangan tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
935dc4dc360SLaxman Dewangan
936dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX)
937dc4dc360SLaxman Dewangan tspi->cur_pos = tspi->cur_tx_pos;
938dc4dc360SLaxman Dewangan else
939dc4dc360SLaxman Dewangan tspi->cur_pos = tspi->cur_rx_pos;
940dc4dc360SLaxman Dewangan
941dc4dc360SLaxman Dewangan if (tspi->cur_pos == t->len) {
942dc4dc360SLaxman Dewangan complete(&tspi->xfer_completion);
943dc4dc360SLaxman Dewangan goto exit;
944dc4dc360SLaxman Dewangan }
945dc4dc360SLaxman Dewangan
946dc4dc360SLaxman Dewangan /* Continue transfer in current message */
947dc4dc360SLaxman Dewangan total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
948dc4dc360SLaxman Dewangan tspi, t);
949dc4dc360SLaxman Dewangan if (total_fifo_words > SLINK_FIFO_DEPTH)
950dc4dc360SLaxman Dewangan err = tegra_slink_start_dma_based_transfer(tspi, t);
951dc4dc360SLaxman Dewangan else
952dc4dc360SLaxman Dewangan err = tegra_slink_start_cpu_based_transfer(tspi, t);
953dc4dc360SLaxman Dewangan
954dc4dc360SLaxman Dewangan exit:
955dc4dc360SLaxman Dewangan spin_unlock_irqrestore(&tspi->lock, flags);
956dc4dc360SLaxman Dewangan return IRQ_HANDLED;
957dc4dc360SLaxman Dewangan }
958dc4dc360SLaxman Dewangan
tegra_slink_isr_thread(int irq,void * context_data)959dc4dc360SLaxman Dewangan static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
960dc4dc360SLaxman Dewangan {
961dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi = context_data;
962dc4dc360SLaxman Dewangan
963dc4dc360SLaxman Dewangan if (!tspi->is_curr_dma_xfer)
964dc4dc360SLaxman Dewangan return handle_cpu_based_xfer(tspi);
965dc4dc360SLaxman Dewangan return handle_dma_based_xfer(tspi);
966dc4dc360SLaxman Dewangan }
967dc4dc360SLaxman Dewangan
tegra_slink_isr(int irq,void * context_data)968dc4dc360SLaxman Dewangan static irqreturn_t tegra_slink_isr(int irq, void *context_data)
969dc4dc360SLaxman Dewangan {
970dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi = context_data;
971dc4dc360SLaxman Dewangan
972dc4dc360SLaxman Dewangan tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
973dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_TX)
974dc4dc360SLaxman Dewangan tspi->tx_status = tspi->status_reg &
975dc4dc360SLaxman Dewangan (SLINK_TX_OVF | SLINK_TX_UNF);
976dc4dc360SLaxman Dewangan
977dc4dc360SLaxman Dewangan if (tspi->cur_direction & DATA_DIR_RX)
978dc4dc360SLaxman Dewangan tspi->rx_status = tspi->status_reg &
979dc4dc360SLaxman Dewangan (SLINK_RX_OVF | SLINK_RX_UNF);
980dc4dc360SLaxman Dewangan tegra_slink_clear_status(tspi);
981dc4dc360SLaxman Dewangan
982dc4dc360SLaxman Dewangan return IRQ_WAKE_THREAD;
983dc4dc360SLaxman Dewangan }
984dc4dc360SLaxman Dewangan
9858b0bebe2SWei Yongjun static const struct tegra_slink_chip_data tegra30_spi_cdata = {
986dc4dc360SLaxman Dewangan .cs_hold_time = true,
987dc4dc360SLaxman Dewangan };
988dc4dc360SLaxman Dewangan
9898b0bebe2SWei Yongjun static const struct tegra_slink_chip_data tegra20_spi_cdata = {
990dc4dc360SLaxman Dewangan .cs_hold_time = false,
991dc4dc360SLaxman Dewangan };
992dc4dc360SLaxman Dewangan
993b2fb1872SJingoo Han static const struct of_device_id tegra_slink_of_match[] = {
994dc4dc360SLaxman Dewangan { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
99524bc8971SLaxman Dewangan { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
996dc4dc360SLaxman Dewangan {}
997dc4dc360SLaxman Dewangan };
998dc4dc360SLaxman Dewangan MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
999dc4dc360SLaxman Dewangan
tegra_slink_probe(struct platform_device * pdev)1000fd4a319bSGrant Likely static int tegra_slink_probe(struct platform_device *pdev)
1001dc4dc360SLaxman Dewangan {
1002*db34aad4SYang Yingliang struct spi_controller *host;
1003dc4dc360SLaxman Dewangan struct tegra_slink_data *tspi;
1004dc4dc360SLaxman Dewangan struct resource *r;
1005dc4dc360SLaxman Dewangan int ret, spi_irq;
1006dc4dc360SLaxman Dewangan const struct tegra_slink_chip_data *cdata = NULL;
1007dc4dc360SLaxman Dewangan
1008c9839acfSMinghao Chi cdata = of_device_get_match_data(&pdev->dev);
1009dc4dc360SLaxman Dewangan
1010*db34aad4SYang Yingliang host = spi_alloc_host(&pdev->dev, sizeof(*tspi));
1011*db34aad4SYang Yingliang if (!host) {
1012*db34aad4SYang Yingliang dev_err(&pdev->dev, "host allocation failed\n");
1013dc4dc360SLaxman Dewangan return -ENOMEM;
1014dc4dc360SLaxman Dewangan }
1015dc4dc360SLaxman Dewangan
1016dc4dc360SLaxman Dewangan /* the spi->mode bits understood by this driver: */
1017*db34aad4SYang Yingliang host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1018*db34aad4SYang Yingliang host->setup = tegra_slink_setup;
1019*db34aad4SYang Yingliang host->prepare_message = tegra_slink_prepare_message;
1020*db34aad4SYang Yingliang host->transfer_one = tegra_slink_transfer_one;
1021*db34aad4SYang Yingliang host->unprepare_message = tegra_slink_unprepare_message;
1022*db34aad4SYang Yingliang host->auto_runtime_pm = true;
1023*db34aad4SYang Yingliang host->num_chipselect = MAX_CHIP_SELECT;
1024dc4dc360SLaxman Dewangan
1025*db34aad4SYang Yingliang platform_set_drvdata(pdev, host);
1026*db34aad4SYang Yingliang tspi = spi_controller_get_devdata(host);
1027*db34aad4SYang Yingliang tspi->host = host;
1028dc4dc360SLaxman Dewangan tspi->dev = &pdev->dev;
1029dc4dc360SLaxman Dewangan tspi->chip_data = cdata;
1030dc4dc360SLaxman Dewangan spin_lock_init(&tspi->lock);
1031dc4dc360SLaxman Dewangan
10323c604de4SAxel Lin if (of_property_read_u32(tspi->dev->of_node, "spi-max-frequency",
1033*db34aad4SYang Yingliang &host->max_speed_hz))
1034*db34aad4SYang Yingliang host->max_speed_hz = 25000000; /* 25MHz */
1035c60fea02SStephen Warren
10362e4ed257SYangtao Li tspi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
1037b0ee5605SThierry Reding if (IS_ERR(tspi->base)) {
1038b0ee5605SThierry Reding ret = PTR_ERR(tspi->base);
1039*db34aad4SYang Yingliang goto exit_free_host;
1040dc4dc360SLaxman Dewangan }
10412e4ed257SYangtao Li tspi->phys = r->start;
1042dc4dc360SLaxman Dewangan
10437001cab1SMarcel Ziswiler /* disabled clock may cause interrupt storm upon request */
10447001cab1SMarcel Ziswiler tspi->clk = devm_clk_get(&pdev->dev, NULL);
10457001cab1SMarcel Ziswiler if (IS_ERR(tspi->clk)) {
10467001cab1SMarcel Ziswiler ret = PTR_ERR(tspi->clk);
10477001cab1SMarcel Ziswiler dev_err(&pdev->dev, "Can not get clock %d\n", ret);
1048*db34aad4SYang Yingliang goto exit_free_host;
10497001cab1SMarcel Ziswiler }
1050dc4dc360SLaxman Dewangan
105173b32756SPhilipp Zabel tspi->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
1052ff2251e3SStephen Warren if (IS_ERR(tspi->rst)) {
1053ff2251e3SStephen Warren dev_err(&pdev->dev, "can not get reset\n");
1054ff2251e3SStephen Warren ret = PTR_ERR(tspi->rst);
1055*db34aad4SYang Yingliang goto exit_free_host;
1056ff2251e3SStephen Warren }
1057ff2251e3SStephen Warren
105807f83755SDmitry Osipenko ret = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
105907f83755SDmitry Osipenko if (ret)
1060*db34aad4SYang Yingliang goto exit_free_host;
106107f83755SDmitry Osipenko
1062dc4dc360SLaxman Dewangan tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
1063dc4dc360SLaxman Dewangan tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
1064dc4dc360SLaxman Dewangan
1065dc4dc360SLaxman Dewangan ret = tegra_slink_init_dma_param(tspi, true);
1066a915d150SStephen Warren if (ret < 0)
1067*db34aad4SYang Yingliang goto exit_free_host;
1068dc4dc360SLaxman Dewangan ret = tegra_slink_init_dma_param(tspi, false);
1069a915d150SStephen Warren if (ret < 0)
1070dc4dc360SLaxman Dewangan goto exit_rx_dma_free;
1071dc4dc360SLaxman Dewangan tspi->max_buf_size = tspi->dma_buf_size;
1072dc4dc360SLaxman Dewangan init_completion(&tspi->tx_dma_complete);
1073dc4dc360SLaxman Dewangan init_completion(&tspi->rx_dma_complete);
1074dc4dc360SLaxman Dewangan
1075dc4dc360SLaxman Dewangan init_completion(&tspi->xfer_completion);
1076dc4dc360SLaxman Dewangan
1077dc4dc360SLaxman Dewangan pm_runtime_enable(&pdev->dev);
1078e4bb903fSDmitry Osipenko ret = pm_runtime_resume_and_get(&pdev->dev);
1079e4bb903fSDmitry Osipenko if (ret) {
1080dc4dc360SLaxman Dewangan dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
1081dc4dc360SLaxman Dewangan goto exit_pm_disable;
1082dc4dc360SLaxman Dewangan }
1083aceda401SJon Hunter
1084aceda401SJon Hunter reset_control_assert(tspi->rst);
1085aceda401SJon Hunter udelay(2);
1086aceda401SJon Hunter reset_control_deassert(tspi->rst);
1087aceda401SJon Hunter
1088e4bb903fSDmitry Osipenko spi_irq = platform_get_irq(pdev, 0);
1089eb9913b5SZhang Shurong if (spi_irq < 0)
1090eb9913b5SZhang Shurong return spi_irq;
1091e4bb903fSDmitry Osipenko tspi->irq = spi_irq;
1092e4bb903fSDmitry Osipenko ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
1093e4bb903fSDmitry Osipenko tegra_slink_isr_thread, IRQF_ONESHOT,
1094e4bb903fSDmitry Osipenko dev_name(&pdev->dev), tspi);
1095e4bb903fSDmitry Osipenko if (ret < 0) {
1096e4bb903fSDmitry Osipenko dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
1097e4bb903fSDmitry Osipenko tspi->irq);
1098e4bb903fSDmitry Osipenko goto exit_pm_put;
1099e4bb903fSDmitry Osipenko }
1100e4bb903fSDmitry Osipenko
1101dc4dc360SLaxman Dewangan tspi->def_command_reg = SLINK_M_S;
1102dc4dc360SLaxman Dewangan tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
1103dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
1104dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
1105dc4dc360SLaxman Dewangan
1106*db34aad4SYang Yingliang host->dev.of_node = pdev->dev.of_node;
1107*db34aad4SYang Yingliang ret = spi_register_controller(host);
1108dc4dc360SLaxman Dewangan if (ret < 0) {
1109*db34aad4SYang Yingliang dev_err(&pdev->dev, "can not register to host err %d\n", ret);
1110e4bb903fSDmitry Osipenko goto exit_free_irq;
1111dc4dc360SLaxman Dewangan }
1112e4bb903fSDmitry Osipenko
1113e4bb903fSDmitry Osipenko pm_runtime_put(&pdev->dev);
1114e4bb903fSDmitry Osipenko
1115dc4dc360SLaxman Dewangan return ret;
1116dc4dc360SLaxman Dewangan
1117e4bb903fSDmitry Osipenko exit_free_irq:
1118e4bb903fSDmitry Osipenko free_irq(spi_irq, tspi);
1119e4bb903fSDmitry Osipenko exit_pm_put:
1120e4bb903fSDmitry Osipenko pm_runtime_put(&pdev->dev);
1121dc4dc360SLaxman Dewangan exit_pm_disable:
11223cc1cb30SDmitry Osipenko pm_runtime_force_suspend(&pdev->dev);
1123e4bb903fSDmitry Osipenko
1124dc4dc360SLaxman Dewangan tegra_slink_deinit_dma_param(tspi, false);
1125dc4dc360SLaxman Dewangan exit_rx_dma_free:
1126dc4dc360SLaxman Dewangan tegra_slink_deinit_dma_param(tspi, true);
1127*db34aad4SYang Yingliang exit_free_host:
1128*db34aad4SYang Yingliang spi_controller_put(host);
1129dc4dc360SLaxman Dewangan return ret;
1130dc4dc360SLaxman Dewangan }
1131dc4dc360SLaxman Dewangan
tegra_slink_remove(struct platform_device * pdev)11327f47f7a2SUwe Kleine-König static void tegra_slink_remove(struct platform_device *pdev)
1133dc4dc360SLaxman Dewangan {
1134*db34aad4SYang Yingliang struct spi_controller *host = spi_controller_get(platform_get_drvdata(pdev));
1135*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(host);
1136dc4dc360SLaxman Dewangan
1137*db34aad4SYang Yingliang spi_unregister_controller(host);
113826c86341SDmitry Osipenko
1139dc4dc360SLaxman Dewangan free_irq(tspi->irq, tspi);
1140dc4dc360SLaxman Dewangan
11413cc1cb30SDmitry Osipenko pm_runtime_force_suspend(&pdev->dev);
11427001cab1SMarcel Ziswiler
1143dc4dc360SLaxman Dewangan if (tspi->tx_dma_chan)
1144dc4dc360SLaxman Dewangan tegra_slink_deinit_dma_param(tspi, false);
1145dc4dc360SLaxman Dewangan
1146dc4dc360SLaxman Dewangan if (tspi->rx_dma_chan)
1147dc4dc360SLaxman Dewangan tegra_slink_deinit_dma_param(tspi, true);
1148dc4dc360SLaxman Dewangan
1149*db34aad4SYang Yingliang spi_controller_put(host);
1150dc4dc360SLaxman Dewangan }
1151dc4dc360SLaxman Dewangan
1152dc4dc360SLaxman Dewangan #ifdef CONFIG_PM_SLEEP
tegra_slink_suspend(struct device * dev)1153dc4dc360SLaxman Dewangan static int tegra_slink_suspend(struct device *dev)
1154dc4dc360SLaxman Dewangan {
1155*db34aad4SYang Yingliang struct spi_controller *host = dev_get_drvdata(dev);
1156dc4dc360SLaxman Dewangan
1157*db34aad4SYang Yingliang return spi_controller_suspend(host);
1158dc4dc360SLaxman Dewangan }
1159dc4dc360SLaxman Dewangan
tegra_slink_resume(struct device * dev)1160dc4dc360SLaxman Dewangan static int tegra_slink_resume(struct device *dev)
1161dc4dc360SLaxman Dewangan {
1162*db34aad4SYang Yingliang struct spi_controller *host = dev_get_drvdata(dev);
1163*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(host);
1164dc4dc360SLaxman Dewangan int ret;
1165dc4dc360SLaxman Dewangan
11661e6f8bd1SMinghao Chi ret = pm_runtime_resume_and_get(dev);
1167dc4dc360SLaxman Dewangan if (ret < 0) {
1168dc4dc360SLaxman Dewangan dev_err(dev, "pm runtime failed, e = %d\n", ret);
1169dc4dc360SLaxman Dewangan return ret;
1170dc4dc360SLaxman Dewangan }
1171dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
1172dc4dc360SLaxman Dewangan tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
1173dc4dc360SLaxman Dewangan pm_runtime_put(dev);
1174dc4dc360SLaxman Dewangan
1175*db34aad4SYang Yingliang return spi_controller_resume(host);
1176dc4dc360SLaxman Dewangan }
1177dc4dc360SLaxman Dewangan #endif
1178dc4dc360SLaxman Dewangan
tegra_slink_runtime_suspend(struct device * dev)1179efafec27SLinus Torvalds static int __maybe_unused tegra_slink_runtime_suspend(struct device *dev)
1180dc4dc360SLaxman Dewangan {
1181*db34aad4SYang Yingliang struct spi_controller *host = dev_get_drvdata(dev);
1182*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(host);
1183dc4dc360SLaxman Dewangan
1184dc4dc360SLaxman Dewangan /* Flush all write which are in PPSB queue by reading back */
1185dc4dc360SLaxman Dewangan tegra_slink_readl(tspi, SLINK_MAS_DATA);
1186dc4dc360SLaxman Dewangan
1187dc4dc360SLaxman Dewangan clk_disable_unprepare(tspi->clk);
1188dc4dc360SLaxman Dewangan return 0;
1189dc4dc360SLaxman Dewangan }
1190dc4dc360SLaxman Dewangan
tegra_slink_runtime_resume(struct device * dev)1191ac8a6ebaSLinus Torvalds static int __maybe_unused tegra_slink_runtime_resume(struct device *dev)
1192dc4dc360SLaxman Dewangan {
1193*db34aad4SYang Yingliang struct spi_controller *host = dev_get_drvdata(dev);
1194*db34aad4SYang Yingliang struct tegra_slink_data *tspi = spi_controller_get_devdata(host);
1195dc4dc360SLaxman Dewangan int ret;
1196dc4dc360SLaxman Dewangan
1197dc4dc360SLaxman Dewangan ret = clk_prepare_enable(tspi->clk);
1198dc4dc360SLaxman Dewangan if (ret < 0) {
1199dc4dc360SLaxman Dewangan dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
1200dc4dc360SLaxman Dewangan return ret;
1201dc4dc360SLaxman Dewangan }
1202dc4dc360SLaxman Dewangan return 0;
1203dc4dc360SLaxman Dewangan }
1204dc4dc360SLaxman Dewangan
1205dc4dc360SLaxman Dewangan static const struct dev_pm_ops slink_pm_ops = {
1206dc4dc360SLaxman Dewangan SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
1207dc4dc360SLaxman Dewangan tegra_slink_runtime_resume, NULL)
1208dc4dc360SLaxman Dewangan SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
1209dc4dc360SLaxman Dewangan };
1210dc4dc360SLaxman Dewangan static struct platform_driver tegra_slink_driver = {
1211dc4dc360SLaxman Dewangan .driver = {
1212dc4dc360SLaxman Dewangan .name = "spi-tegra-slink",
1213dc4dc360SLaxman Dewangan .pm = &slink_pm_ops,
1214c60fea02SStephen Warren .of_match_table = tegra_slink_of_match,
1215dc4dc360SLaxman Dewangan },
1216dc4dc360SLaxman Dewangan .probe = tegra_slink_probe,
12177f47f7a2SUwe Kleine-König .remove_new = tegra_slink_remove,
1218dc4dc360SLaxman Dewangan };
1219dc4dc360SLaxman Dewangan module_platform_driver(tegra_slink_driver);
1220dc4dc360SLaxman Dewangan
1221dc4dc360SLaxman Dewangan MODULE_ALIAS("platform:spi-tegra-slink");
1222dc4dc360SLaxman Dewangan MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
1223dc4dc360SLaxman Dewangan MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1224dc4dc360SLaxman Dewangan MODULE_LICENSE("GPL v2");
1225