1 // SPDX-License-Identifier: GPL-2.0-only
3 * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
9 #include <linux/completion.h>
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmapool.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/kthread.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_opp.h>
22 #include <linux/pm_runtime.h>
24 #include <linux/of_device.h>
25 #include <linux/reset.h>
26 #include <linux/spi/spi.h>
28 #include <soc/tegra/common.h>
30 #define SLINK_COMMAND 0x000
31 #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
32 #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
33 #define SLINK_BOTH_EN (1 << 10)
34 #define SLINK_CS_SW (1 << 11)
35 #define SLINK_CS_VALUE (1 << 12)
36 #define SLINK_CS_POLARITY (1 << 13)
37 #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
38 #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
39 #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
40 #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
41 #define SLINK_IDLE_SDA_MASK (3 << 16)
42 #define SLINK_CS_POLARITY1 (1 << 20)
43 #define SLINK_CK_SDA (1 << 21)
44 #define SLINK_CS_POLARITY2 (1 << 22)
45 #define SLINK_CS_POLARITY3 (1 << 23)
46 #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
47 #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
48 #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
49 #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
50 #define SLINK_IDLE_SCLK_MASK (3 << 24)
51 #define SLINK_M_S (1 << 28)
52 #define SLINK_WAIT (1 << 29)
53 #define SLINK_GO (1 << 30)
54 #define SLINK_ENB (1 << 31)
56 #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
58 #define SLINK_COMMAND2 0x004
59 #define SLINK_LSBFE (1 << 0)
60 #define SLINK_SSOE (1 << 1)
61 #define SLINK_SPIE (1 << 4)
62 #define SLINK_BIDIROE (1 << 6)
63 #define SLINK_MODFEN (1 << 7)
64 #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
65 #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
66 #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
67 #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
68 #define SLINK_FIFO_REFILLS_0 (0 << 22)
69 #define SLINK_FIFO_REFILLS_1 (1 << 22)
70 #define SLINK_FIFO_REFILLS_2 (2 << 22)
71 #define SLINK_FIFO_REFILLS_3 (3 << 22)
72 #define SLINK_FIFO_REFILLS_MASK (3 << 22)
73 #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
74 #define SLINK_SPC0 (1 << 29)
75 #define SLINK_TXEN (1 << 30)
76 #define SLINK_RXEN (1 << 31)
78 #define SLINK_STATUS 0x008
79 #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
80 #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
81 #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
82 #define SLINK_MODF (1 << 16)
83 #define SLINK_RX_UNF (1 << 18)
84 #define SLINK_TX_OVF (1 << 19)
85 #define SLINK_TX_FULL (1 << 20)
86 #define SLINK_TX_EMPTY (1 << 21)
87 #define SLINK_RX_FULL (1 << 22)
88 #define SLINK_RX_EMPTY (1 << 23)
89 #define SLINK_TX_UNF (1 << 24)
90 #define SLINK_RX_OVF (1 << 25)
91 #define SLINK_TX_FLUSH (1 << 26)
92 #define SLINK_RX_FLUSH (1 << 27)
93 #define SLINK_SCLK (1 << 28)
94 #define SLINK_ERR (1 << 29)
95 #define SLINK_RDY (1 << 30)
96 #define SLINK_BSY (1 << 31)
97 #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
98 SLINK_TX_UNF | SLINK_RX_OVF)
100 #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
102 #define SLINK_MAS_DATA 0x010
103 #define SLINK_SLAVE_DATA 0x014
105 #define SLINK_DMA_CTL 0x018
106 #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
107 #define SLINK_TX_TRIG_1 (0 << 16)
108 #define SLINK_TX_TRIG_4 (1 << 16)
109 #define SLINK_TX_TRIG_8 (2 << 16)
110 #define SLINK_TX_TRIG_16 (3 << 16)
111 #define SLINK_TX_TRIG_MASK (3 << 16)
112 #define SLINK_RX_TRIG_1 (0 << 18)
113 #define SLINK_RX_TRIG_4 (1 << 18)
114 #define SLINK_RX_TRIG_8 (2 << 18)
115 #define SLINK_RX_TRIG_16 (3 << 18)
116 #define SLINK_RX_TRIG_MASK (3 << 18)
117 #define SLINK_PACKED (1 << 20)
118 #define SLINK_PACK_SIZE_4 (0 << 21)
119 #define SLINK_PACK_SIZE_8 (1 << 21)
120 #define SLINK_PACK_SIZE_16 (2 << 21)
121 #define SLINK_PACK_SIZE_32 (3 << 21)
122 #define SLINK_PACK_SIZE_MASK (3 << 21)
123 #define SLINK_IE_TXC (1 << 26)
124 #define SLINK_IE_RXC (1 << 27)
125 #define SLINK_DMA_EN (1 << 31)
127 #define SLINK_STATUS2 0x01c
128 #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
129 #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
130 #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
132 #define SLINK_TX_FIFO 0x100
133 #define SLINK_RX_FIFO 0x180
135 #define DATA_DIR_TX (1 << 0)
136 #define DATA_DIR_RX (1 << 1)
138 #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
140 #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
141 #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
142 #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
144 #define SLINK_STATUS2_RESET \
145 (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
147 #define MAX_CHIP_SELECT 4
148 #define SLINK_FIFO_DEPTH 32
150 struct tegra_slink_chip_data {
154 struct tegra_slink_data {
156 struct spi_master *master;
157 const struct tegra_slink_chip_data *chip_data;
161 struct reset_control *rst;
167 struct spi_device *cur_spi;
170 unsigned words_per_32bit;
171 unsigned bytes_per_word;
172 unsigned curr_dma_words;
173 unsigned cur_direction;
178 unsigned dma_buf_size;
179 unsigned max_buf_size;
180 bool is_curr_dma_xfer;
182 struct completion rx_dma_complete;
183 struct completion tx_dma_complete;
195 u32 def_command2_reg;
197 struct completion xfer_completion;
198 struct spi_transfer *curr_xfer;
199 struct dma_chan *rx_dma_chan;
201 dma_addr_t rx_dma_phys;
202 struct dma_async_tx_descriptor *rx_dma_desc;
204 struct dma_chan *tx_dma_chan;
206 dma_addr_t tx_dma_phys;
207 struct dma_async_tx_descriptor *tx_dma_desc;
210 static inline u32 tegra_slink_readl(struct tegra_slink_data *tspi,
213 return readl(tspi->base + reg);
216 static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
217 u32 val, unsigned long reg)
219 writel(val, tspi->base + reg);
221 /* Read back register to make sure that register writes completed */
222 if (reg != SLINK_TX_FIFO)
223 readl(tspi->base + SLINK_MAS_DATA);
226 static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
230 tegra_slink_readl(tspi, SLINK_STATUS);
232 /* Write 1 to clear status register */
233 val_write = SLINK_RDY | SLINK_FIFO_ERROR;
234 tegra_slink_writel(tspi, val_write, SLINK_STATUS);
237 static u32 tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
238 struct spi_transfer *t)
240 switch (tspi->bytes_per_word) {
242 return SLINK_PACK_SIZE_4;
244 return SLINK_PACK_SIZE_8;
246 return SLINK_PACK_SIZE_16;
248 return SLINK_PACK_SIZE_32;
254 static unsigned tegra_slink_calculate_curr_xfer_param(
255 struct spi_device *spi, struct tegra_slink_data *tspi,
256 struct spi_transfer *t)
258 unsigned remain_len = t->len - tspi->cur_pos;
260 unsigned bits_per_word;
262 unsigned total_fifo_words;
264 bits_per_word = t->bits_per_word;
265 tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
267 if (bits_per_word == 8 || bits_per_word == 16) {
268 tspi->is_packed = true;
269 tspi->words_per_32bit = 32/bits_per_word;
271 tspi->is_packed = false;
272 tspi->words_per_32bit = 1;
274 tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
276 if (tspi->is_packed) {
277 max_len = min(remain_len, tspi->max_buf_size);
278 tspi->curr_dma_words = max_len/tspi->bytes_per_word;
279 total_fifo_words = max_len/4;
281 max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
282 max_word = min(max_word, tspi->max_buf_size/4);
283 tspi->curr_dma_words = max_word;
284 total_fifo_words = max_word;
286 return total_fifo_words;
289 static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
290 struct tegra_slink_data *tspi, struct spi_transfer *t)
293 unsigned tx_empty_count;
295 unsigned max_n_32bit;
297 unsigned int written_words;
298 unsigned fifo_words_left;
299 u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
301 fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
302 tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
304 if (tspi->is_packed) {
305 fifo_words_left = tx_empty_count * tspi->words_per_32bit;
306 written_words = min(fifo_words_left, tspi->curr_dma_words);
307 nbytes = written_words * tspi->bytes_per_word;
308 max_n_32bit = DIV_ROUND_UP(nbytes, 4);
309 for (count = 0; count < max_n_32bit; count++) {
311 for (i = 0; (i < 4) && nbytes; i++, nbytes--)
312 x |= (u32)(*tx_buf++) << (i * 8);
313 tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
316 max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
317 written_words = max_n_32bit;
318 nbytes = written_words * tspi->bytes_per_word;
319 for (count = 0; count < max_n_32bit; count++) {
321 for (i = 0; nbytes && (i < tspi->bytes_per_word);
323 x |= (u32)(*tx_buf++) << (i * 8);
324 tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
327 tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
328 return written_words;
331 static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
332 struct tegra_slink_data *tspi, struct spi_transfer *t)
334 unsigned rx_full_count;
337 unsigned int read_words = 0;
339 u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
341 fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
342 rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
343 if (tspi->is_packed) {
344 len = tspi->curr_dma_words * tspi->bytes_per_word;
345 for (count = 0; count < rx_full_count; count++) {
346 u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
347 for (i = 0; len && (i < 4); i++, len--)
348 *rx_buf++ = (x >> i*8) & 0xFF;
350 tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
351 read_words += tspi->curr_dma_words;
353 for (count = 0; count < rx_full_count; count++) {
354 u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
355 for (i = 0; (i < tspi->bytes_per_word); i++)
356 *rx_buf++ = (x >> (i*8)) & 0xFF;
358 tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
359 read_words += rx_full_count;
364 static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
365 struct tegra_slink_data *tspi, struct spi_transfer *t)
367 /* Make the dma buffer to read by cpu */
368 dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
369 tspi->dma_buf_size, DMA_TO_DEVICE);
371 if (tspi->is_packed) {
372 unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
373 memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
377 u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
378 unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
380 for (count = 0; count < tspi->curr_dma_words; count++) {
382 for (i = 0; consume && (i < tspi->bytes_per_word);
384 x |= (u32)(*tx_buf++) << (i * 8);
385 tspi->tx_dma_buf[count] = x;
388 tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
390 /* Make the dma buffer to read by dma */
391 dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
392 tspi->dma_buf_size, DMA_TO_DEVICE);
395 static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
396 struct tegra_slink_data *tspi, struct spi_transfer *t)
400 /* Make the dma buffer to read by cpu */
401 dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
402 tspi->dma_buf_size, DMA_FROM_DEVICE);
404 if (tspi->is_packed) {
405 len = tspi->curr_dma_words * tspi->bytes_per_word;
406 memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
410 unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
411 u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
413 for (count = 0; count < tspi->curr_dma_words; count++) {
414 u32 x = tspi->rx_dma_buf[count] & rx_mask;
415 for (i = 0; (i < tspi->bytes_per_word); i++)
416 *rx_buf++ = (x >> (i*8)) & 0xFF;
419 tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
421 /* Make the dma buffer to read by dma */
422 dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
423 tspi->dma_buf_size, DMA_FROM_DEVICE);
426 static void tegra_slink_dma_complete(void *args)
428 struct completion *dma_complete = args;
430 complete(dma_complete);
433 static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
435 reinit_completion(&tspi->tx_dma_complete);
436 tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
437 tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
438 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
439 if (!tspi->tx_dma_desc) {
440 dev_err(tspi->dev, "Not able to get desc for Tx\n");
444 tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
445 tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
447 dmaengine_submit(tspi->tx_dma_desc);
448 dma_async_issue_pending(tspi->tx_dma_chan);
452 static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
454 reinit_completion(&tspi->rx_dma_complete);
455 tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
456 tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
457 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
458 if (!tspi->rx_dma_desc) {
459 dev_err(tspi->dev, "Not able to get desc for Rx\n");
463 tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
464 tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
466 dmaengine_submit(tspi->rx_dma_desc);
467 dma_async_issue_pending(tspi->rx_dma_chan);
471 static int tegra_slink_start_dma_based_transfer(
472 struct tegra_slink_data *tspi, struct spi_transfer *t)
479 /* Make sure that Rx and Tx fifo are empty */
480 status = tegra_slink_readl(tspi, SLINK_STATUS);
481 if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
482 dev_err(tspi->dev, "Rx/Tx fifo are not empty status 0x%08x\n",
487 val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
488 val |= tspi->packed_size;
490 len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
493 len = tspi->curr_dma_words * 4;
495 /* Set attention level based on length of transfer */
497 val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
498 else if (((len) >> 4) & 0x1)
499 val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
501 val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
503 if (tspi->cur_direction & DATA_DIR_TX)
506 if (tspi->cur_direction & DATA_DIR_RX)
509 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
510 tspi->dma_control_reg = val;
512 if (tspi->cur_direction & DATA_DIR_TX) {
513 tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
515 ret = tegra_slink_start_tx_dma(tspi, len);
518 "Starting tx dma failed, err %d\n", ret);
522 /* Wait for tx fifo to be fill before starting slink */
523 status = tegra_slink_readl(tspi, SLINK_STATUS);
524 while (!(status & SLINK_TX_FULL))
525 status = tegra_slink_readl(tspi, SLINK_STATUS);
528 if (tspi->cur_direction & DATA_DIR_RX) {
529 /* Make the dma buffer to read by dma */
530 dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
531 tspi->dma_buf_size, DMA_FROM_DEVICE);
533 ret = tegra_slink_start_rx_dma(tspi, len);
536 "Starting rx dma failed, err %d\n", ret);
537 if (tspi->cur_direction & DATA_DIR_TX)
538 dmaengine_terminate_all(tspi->tx_dma_chan);
542 tspi->is_curr_dma_xfer = true;
543 if (tspi->is_packed) {
545 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
546 /* HW need small delay after settign Packed mode */
549 tspi->dma_control_reg = val;
552 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
556 static int tegra_slink_start_cpu_based_transfer(
557 struct tegra_slink_data *tspi, struct spi_transfer *t)
562 val = tspi->packed_size;
563 if (tspi->cur_direction & DATA_DIR_TX)
566 if (tspi->cur_direction & DATA_DIR_RX)
569 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
570 tspi->dma_control_reg = val;
572 if (tspi->cur_direction & DATA_DIR_TX)
573 cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
575 cur_words = tspi->curr_dma_words;
576 val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
577 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
578 tspi->dma_control_reg = val;
580 tspi->is_curr_dma_xfer = false;
581 if (tspi->is_packed) {
583 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
587 tspi->dma_control_reg = val;
589 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
593 static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
596 struct dma_chan *dma_chan;
600 struct dma_slave_config dma_sconfig;
602 dma_chan = dma_request_chan(tspi->dev, dma_to_memory ? "rx" : "tx");
603 if (IS_ERR(dma_chan))
604 return dev_err_probe(tspi->dev, PTR_ERR(dma_chan),
605 "Dma channel is not available\n");
607 dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
608 &dma_phys, GFP_KERNEL);
610 dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
611 dma_release_channel(dma_chan);
616 dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
617 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
618 dma_sconfig.src_maxburst = 0;
620 dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
621 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
622 dma_sconfig.dst_maxburst = 0;
625 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
629 tspi->rx_dma_chan = dma_chan;
630 tspi->rx_dma_buf = dma_buf;
631 tspi->rx_dma_phys = dma_phys;
633 tspi->tx_dma_chan = dma_chan;
634 tspi->tx_dma_buf = dma_buf;
635 tspi->tx_dma_phys = dma_phys;
640 dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
641 dma_release_channel(dma_chan);
645 static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
650 struct dma_chan *dma_chan;
653 dma_buf = tspi->rx_dma_buf;
654 dma_chan = tspi->rx_dma_chan;
655 dma_phys = tspi->rx_dma_phys;
656 tspi->rx_dma_chan = NULL;
657 tspi->rx_dma_buf = NULL;
659 dma_buf = tspi->tx_dma_buf;
660 dma_chan = tspi->tx_dma_chan;
661 dma_phys = tspi->tx_dma_phys;
662 tspi->tx_dma_buf = NULL;
663 tspi->tx_dma_chan = NULL;
668 dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
669 dma_release_channel(dma_chan);
672 static int tegra_slink_start_transfer_one(struct spi_device *spi,
673 struct spi_transfer *t)
675 struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
678 unsigned total_fifo_words;
683 bits_per_word = t->bits_per_word;
685 if (speed != tspi->cur_speed) {
686 dev_pm_opp_set_rate(tspi->dev, speed * 4);
687 tspi->cur_speed = speed;
692 tspi->cur_rx_pos = 0;
693 tspi->cur_tx_pos = 0;
695 total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
697 command = tspi->command_reg;
698 command &= ~SLINK_BIT_LENGTH(~0);
699 command |= SLINK_BIT_LENGTH(bits_per_word - 1);
701 command2 = tspi->command2_reg;
702 command2 &= ~(SLINK_RXEN | SLINK_TXEN);
704 tspi->cur_direction = 0;
706 command2 |= SLINK_RXEN;
707 tspi->cur_direction |= DATA_DIR_RX;
710 command2 |= SLINK_TXEN;
711 tspi->cur_direction |= DATA_DIR_TX;
715 * Writing to the command2 register bevore the command register prevents
716 * a spike in chip_select line 0. This selects the chip_select line
717 * before changing the chip_select value.
719 tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
720 tspi->command2_reg = command2;
722 tegra_slink_writel(tspi, command, SLINK_COMMAND);
723 tspi->command_reg = command;
725 if (total_fifo_words > SLINK_FIFO_DEPTH)
726 ret = tegra_slink_start_dma_based_transfer(tspi, t);
728 ret = tegra_slink_start_cpu_based_transfer(tspi, t);
732 static int tegra_slink_setup(struct spi_device *spi)
734 static const u32 cs_pol_bit[MAX_CHIP_SELECT] = {
741 struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
746 dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
748 spi->mode & SPI_CPOL ? "" : "~",
749 spi->mode & SPI_CPHA ? "" : "~",
752 ret = pm_runtime_resume_and_get(tspi->dev);
754 dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
758 spin_lock_irqsave(&tspi->lock, flags);
759 val = tspi->def_command_reg;
760 if (spi->mode & SPI_CS_HIGH)
761 val |= cs_pol_bit[spi->chip_select];
763 val &= ~cs_pol_bit[spi->chip_select];
764 tspi->def_command_reg = val;
765 tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
766 spin_unlock_irqrestore(&tspi->lock, flags);
768 pm_runtime_put(tspi->dev);
772 static int tegra_slink_prepare_message(struct spi_master *master,
773 struct spi_message *msg)
775 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
776 struct spi_device *spi = msg->spi;
778 tegra_slink_clear_status(tspi);
780 tspi->command_reg = tspi->def_command_reg;
781 tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
783 tspi->command2_reg = tspi->def_command2_reg;
784 tspi->command2_reg |= SLINK_SS_EN_CS(spi->chip_select);
786 tspi->command_reg &= ~SLINK_MODES;
787 if (spi->mode & SPI_CPHA)
788 tspi->command_reg |= SLINK_CK_SDA;
790 if (spi->mode & SPI_CPOL)
791 tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
793 tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
798 static int tegra_slink_transfer_one(struct spi_master *master,
799 struct spi_device *spi,
800 struct spi_transfer *xfer)
802 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
805 reinit_completion(&tspi->xfer_completion);
806 ret = tegra_slink_start_transfer_one(spi, xfer);
809 "spi can not start transfer, err %d\n", ret);
813 ret = wait_for_completion_timeout(&tspi->xfer_completion,
815 if (WARN_ON(ret == 0)) {
817 "spi transfer timeout, err %d\n", ret);
822 return tspi->tx_status;
824 return tspi->rx_status;
829 static int tegra_slink_unprepare_message(struct spi_master *master,
830 struct spi_message *msg)
832 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
834 tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
835 tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
840 static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
842 struct spi_transfer *t = tspi->curr_xfer;
845 spin_lock_irqsave(&tspi->lock, flags);
846 if (tspi->tx_status || tspi->rx_status ||
847 (tspi->status_reg & SLINK_BSY)) {
849 "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
851 "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
852 tspi->command2_reg, tspi->dma_control_reg);
853 reset_control_assert(tspi->rst);
855 reset_control_deassert(tspi->rst);
856 complete(&tspi->xfer_completion);
860 if (tspi->cur_direction & DATA_DIR_RX)
861 tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
863 if (tspi->cur_direction & DATA_DIR_TX)
864 tspi->cur_pos = tspi->cur_tx_pos;
866 tspi->cur_pos = tspi->cur_rx_pos;
868 if (tspi->cur_pos == t->len) {
869 complete(&tspi->xfer_completion);
873 tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
874 tegra_slink_start_cpu_based_transfer(tspi, t);
876 spin_unlock_irqrestore(&tspi->lock, flags);
880 static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
882 struct spi_transfer *t = tspi->curr_xfer;
885 unsigned total_fifo_words;
888 /* Abort dmas if any error */
889 if (tspi->cur_direction & DATA_DIR_TX) {
890 if (tspi->tx_status) {
891 dmaengine_terminate_all(tspi->tx_dma_chan);
894 wait_status = wait_for_completion_interruptible_timeout(
895 &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
896 if (wait_status <= 0) {
897 dmaengine_terminate_all(tspi->tx_dma_chan);
898 dev_err(tspi->dev, "TxDma Xfer failed\n");
904 if (tspi->cur_direction & DATA_DIR_RX) {
905 if (tspi->rx_status) {
906 dmaengine_terminate_all(tspi->rx_dma_chan);
909 wait_status = wait_for_completion_interruptible_timeout(
910 &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
911 if (wait_status <= 0) {
912 dmaengine_terminate_all(tspi->rx_dma_chan);
913 dev_err(tspi->dev, "RxDma Xfer failed\n");
919 spin_lock_irqsave(&tspi->lock, flags);
922 "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
924 "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
925 tspi->command2_reg, tspi->dma_control_reg);
926 reset_control_assert(tspi->rst);
928 reset_control_assert(tspi->rst);
929 complete(&tspi->xfer_completion);
930 spin_unlock_irqrestore(&tspi->lock, flags);
934 if (tspi->cur_direction & DATA_DIR_RX)
935 tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
937 if (tspi->cur_direction & DATA_DIR_TX)
938 tspi->cur_pos = tspi->cur_tx_pos;
940 tspi->cur_pos = tspi->cur_rx_pos;
942 if (tspi->cur_pos == t->len) {
943 complete(&tspi->xfer_completion);
947 /* Continue transfer in current message */
948 total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
950 if (total_fifo_words > SLINK_FIFO_DEPTH)
951 err = tegra_slink_start_dma_based_transfer(tspi, t);
953 err = tegra_slink_start_cpu_based_transfer(tspi, t);
956 spin_unlock_irqrestore(&tspi->lock, flags);
960 static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
962 struct tegra_slink_data *tspi = context_data;
964 if (!tspi->is_curr_dma_xfer)
965 return handle_cpu_based_xfer(tspi);
966 return handle_dma_based_xfer(tspi);
969 static irqreturn_t tegra_slink_isr(int irq, void *context_data)
971 struct tegra_slink_data *tspi = context_data;
973 tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
974 if (tspi->cur_direction & DATA_DIR_TX)
975 tspi->tx_status = tspi->status_reg &
976 (SLINK_TX_OVF | SLINK_TX_UNF);
978 if (tspi->cur_direction & DATA_DIR_RX)
979 tspi->rx_status = tspi->status_reg &
980 (SLINK_RX_OVF | SLINK_RX_UNF);
981 tegra_slink_clear_status(tspi);
983 return IRQ_WAKE_THREAD;
986 static const struct tegra_slink_chip_data tegra30_spi_cdata = {
987 .cs_hold_time = true,
990 static const struct tegra_slink_chip_data tegra20_spi_cdata = {
991 .cs_hold_time = false,
994 static const struct of_device_id tegra_slink_of_match[] = {
995 { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
996 { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
999 MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
1001 static int tegra_slink_probe(struct platform_device *pdev)
1003 struct spi_master *master;
1004 struct tegra_slink_data *tspi;
1007 const struct tegra_slink_chip_data *cdata = NULL;
1009 cdata = of_device_get_match_data(&pdev->dev);
1011 master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
1013 dev_err(&pdev->dev, "master allocation failed\n");
1017 /* the spi->mode bits understood by this driver: */
1018 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1019 master->setup = tegra_slink_setup;
1020 master->prepare_message = tegra_slink_prepare_message;
1021 master->transfer_one = tegra_slink_transfer_one;
1022 master->unprepare_message = tegra_slink_unprepare_message;
1023 master->auto_runtime_pm = true;
1024 master->num_chipselect = MAX_CHIP_SELECT;
1026 platform_set_drvdata(pdev, master);
1027 tspi = spi_master_get_devdata(master);
1028 tspi->master = master;
1029 tspi->dev = &pdev->dev;
1030 tspi->chip_data = cdata;
1031 spin_lock_init(&tspi->lock);
1033 if (of_property_read_u32(tspi->dev->of_node, "spi-max-frequency",
1034 &master->max_speed_hz))
1035 master->max_speed_hz = 25000000; /* 25MHz */
1037 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1039 dev_err(&pdev->dev, "No IO memory resource\n");
1041 goto exit_free_master;
1043 tspi->phys = r->start;
1044 tspi->base = devm_ioremap_resource(&pdev->dev, r);
1045 if (IS_ERR(tspi->base)) {
1046 ret = PTR_ERR(tspi->base);
1047 goto exit_free_master;
1050 /* disabled clock may cause interrupt storm upon request */
1051 tspi->clk = devm_clk_get(&pdev->dev, NULL);
1052 if (IS_ERR(tspi->clk)) {
1053 ret = PTR_ERR(tspi->clk);
1054 dev_err(&pdev->dev, "Can not get clock %d\n", ret);
1055 goto exit_free_master;
1058 tspi->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
1059 if (IS_ERR(tspi->rst)) {
1060 dev_err(&pdev->dev, "can not get reset\n");
1061 ret = PTR_ERR(tspi->rst);
1062 goto exit_free_master;
1065 ret = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
1067 goto exit_free_master;
1069 tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
1070 tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
1072 ret = tegra_slink_init_dma_param(tspi, true);
1074 goto exit_free_master;
1075 ret = tegra_slink_init_dma_param(tspi, false);
1077 goto exit_rx_dma_free;
1078 tspi->max_buf_size = tspi->dma_buf_size;
1079 init_completion(&tspi->tx_dma_complete);
1080 init_completion(&tspi->rx_dma_complete);
1082 init_completion(&tspi->xfer_completion);
1084 pm_runtime_enable(&pdev->dev);
1085 ret = pm_runtime_resume_and_get(&pdev->dev);
1087 dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
1088 goto exit_pm_disable;
1091 reset_control_assert(tspi->rst);
1093 reset_control_deassert(tspi->rst);
1095 spi_irq = platform_get_irq(pdev, 0);
1096 tspi->irq = spi_irq;
1097 ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
1098 tegra_slink_isr_thread, IRQF_ONESHOT,
1099 dev_name(&pdev->dev), tspi);
1101 dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
1106 tspi->def_command_reg = SLINK_M_S;
1107 tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
1108 tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
1109 tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
1111 master->dev.of_node = pdev->dev.of_node;
1112 ret = spi_register_master(master);
1114 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
1118 pm_runtime_put(&pdev->dev);
1123 free_irq(spi_irq, tspi);
1125 pm_runtime_put(&pdev->dev);
1127 pm_runtime_force_suspend(&pdev->dev);
1129 tegra_slink_deinit_dma_param(tspi, false);
1131 tegra_slink_deinit_dma_param(tspi, true);
1133 spi_master_put(master);
1137 static int tegra_slink_remove(struct platform_device *pdev)
1139 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
1140 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
1142 spi_unregister_master(master);
1144 free_irq(tspi->irq, tspi);
1146 pm_runtime_force_suspend(&pdev->dev);
1148 if (tspi->tx_dma_chan)
1149 tegra_slink_deinit_dma_param(tspi, false);
1151 if (tspi->rx_dma_chan)
1152 tegra_slink_deinit_dma_param(tspi, true);
1154 spi_master_put(master);
1158 #ifdef CONFIG_PM_SLEEP
1159 static int tegra_slink_suspend(struct device *dev)
1161 struct spi_master *master = dev_get_drvdata(dev);
1163 return spi_master_suspend(master);
1166 static int tegra_slink_resume(struct device *dev)
1168 struct spi_master *master = dev_get_drvdata(dev);
1169 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
1172 ret = pm_runtime_resume_and_get(dev);
1174 dev_err(dev, "pm runtime failed, e = %d\n", ret);
1177 tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
1178 tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
1179 pm_runtime_put(dev);
1181 return spi_master_resume(master);
1185 static int __maybe_unused tegra_slink_runtime_suspend(struct device *dev)
1187 struct spi_master *master = dev_get_drvdata(dev);
1188 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
1190 /* Flush all write which are in PPSB queue by reading back */
1191 tegra_slink_readl(tspi, SLINK_MAS_DATA);
1193 clk_disable_unprepare(tspi->clk);
1197 static int __maybe_unused tegra_slink_runtime_resume(struct device *dev)
1199 struct spi_master *master = dev_get_drvdata(dev);
1200 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
1203 ret = clk_prepare_enable(tspi->clk);
1205 dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
1211 static const struct dev_pm_ops slink_pm_ops = {
1212 SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
1213 tegra_slink_runtime_resume, NULL)
1214 SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
1216 static struct platform_driver tegra_slink_driver = {
1218 .name = "spi-tegra-slink",
1219 .pm = &slink_pm_ops,
1220 .of_match_table = tegra_slink_of_match,
1222 .probe = tegra_slink_probe,
1223 .remove = tegra_slink_remove,
1225 module_platform_driver(tegra_slink_driver);
1227 MODULE_ALIAS("platform:spi-tegra-slink");
1228 MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
1229 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1230 MODULE_LICENSE("GPL v2");