blob: a07b72e9c344847e38099eff64565f51c6887b86 [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Laxman Dewangandc4dc362012-10-30 12:34:05 +05302/*
3 * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
4 *
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Laxman Dewangandc4dc362012-10-30 12:34:05 +05306 */
7
8#include <linux/clk.h>
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>
Laxman Dewangandc4dc362012-10-30 12:34:05 +053015#include <linux/interrupt.h>
16#include <linux/io.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_runtime.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
Stephen Warrenff2251e2013-11-06 16:31:24 -070024#include <linux/reset.h>
Laxman Dewangandc4dc362012-10-30 12:34:05 +053025#include <linux/spi/spi.h>
Laxman Dewangandc4dc362012-10-30 12:34:05 +053026
27#define SLINK_COMMAND 0x000
28#define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
29#define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
30#define SLINK_BOTH_EN (1 << 10)
31#define SLINK_CS_SW (1 << 11)
32#define SLINK_CS_VALUE (1 << 12)
33#define SLINK_CS_POLARITY (1 << 13)
34#define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
35#define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
36#define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
37#define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
38#define SLINK_IDLE_SDA_MASK (3 << 16)
39#define SLINK_CS_POLARITY1 (1 << 20)
40#define SLINK_CK_SDA (1 << 21)
41#define SLINK_CS_POLARITY2 (1 << 22)
42#define SLINK_CS_POLARITY3 (1 << 23)
43#define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
44#define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
45#define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
46#define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
47#define SLINK_IDLE_SCLK_MASK (3 << 24)
48#define SLINK_M_S (1 << 28)
49#define SLINK_WAIT (1 << 29)
50#define SLINK_GO (1 << 30)
51#define SLINK_ENB (1 << 31)
52
53#define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
54
55#define SLINK_COMMAND2 0x004
56#define SLINK_LSBFE (1 << 0)
57#define SLINK_SSOE (1 << 1)
58#define SLINK_SPIE (1 << 4)
59#define SLINK_BIDIROE (1 << 6)
60#define SLINK_MODFEN (1 << 7)
61#define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
62#define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
63#define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
64#define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
65#define SLINK_FIFO_REFILLS_0 (0 << 22)
66#define SLINK_FIFO_REFILLS_1 (1 << 22)
67#define SLINK_FIFO_REFILLS_2 (2 << 22)
68#define SLINK_FIFO_REFILLS_3 (3 << 22)
69#define SLINK_FIFO_REFILLS_MASK (3 << 22)
70#define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
71#define SLINK_SPC0 (1 << 29)
72#define SLINK_TXEN (1 << 30)
73#define SLINK_RXEN (1 << 31)
74
75#define SLINK_STATUS 0x008
76#define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
77#define SLINK_WORD(val) (((val) >> 5) & 0x1f)
78#define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
79#define SLINK_MODF (1 << 16)
80#define SLINK_RX_UNF (1 << 18)
81#define SLINK_TX_OVF (1 << 19)
82#define SLINK_TX_FULL (1 << 20)
83#define SLINK_TX_EMPTY (1 << 21)
84#define SLINK_RX_FULL (1 << 22)
85#define SLINK_RX_EMPTY (1 << 23)
86#define SLINK_TX_UNF (1 << 24)
87#define SLINK_RX_OVF (1 << 25)
88#define SLINK_TX_FLUSH (1 << 26)
89#define SLINK_RX_FLUSH (1 << 27)
90#define SLINK_SCLK (1 << 28)
91#define SLINK_ERR (1 << 29)
92#define SLINK_RDY (1 << 30)
93#define SLINK_BSY (1 << 31)
94#define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
95 SLINK_TX_UNF | SLINK_RX_OVF)
96
97#define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
98
99#define SLINK_MAS_DATA 0x010
100#define SLINK_SLAVE_DATA 0x014
101
102#define SLINK_DMA_CTL 0x018
103#define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
104#define SLINK_TX_TRIG_1 (0 << 16)
105#define SLINK_TX_TRIG_4 (1 << 16)
106#define SLINK_TX_TRIG_8 (2 << 16)
107#define SLINK_TX_TRIG_16 (3 << 16)
108#define SLINK_TX_TRIG_MASK (3 << 16)
109#define SLINK_RX_TRIG_1 (0 << 18)
110#define SLINK_RX_TRIG_4 (1 << 18)
111#define SLINK_RX_TRIG_8 (2 << 18)
112#define SLINK_RX_TRIG_16 (3 << 18)
113#define SLINK_RX_TRIG_MASK (3 << 18)
114#define SLINK_PACKED (1 << 20)
115#define SLINK_PACK_SIZE_4 (0 << 21)
116#define SLINK_PACK_SIZE_8 (1 << 21)
117#define SLINK_PACK_SIZE_16 (2 << 21)
118#define SLINK_PACK_SIZE_32 (3 << 21)
119#define SLINK_PACK_SIZE_MASK (3 << 21)
120#define SLINK_IE_TXC (1 << 26)
121#define SLINK_IE_RXC (1 << 27)
122#define SLINK_DMA_EN (1 << 31)
123
124#define SLINK_STATUS2 0x01c
125#define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
126#define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
127#define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
128
129#define SLINK_TX_FIFO 0x100
130#define SLINK_RX_FIFO 0x180
131
132#define DATA_DIR_TX (1 << 0)
133#define DATA_DIR_RX (1 << 1)
134
135#define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
136
137#define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
138#define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
139#define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
140
141#define SLINK_STATUS2_RESET \
142 (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
143
144#define MAX_CHIP_SELECT 4
145#define SLINK_FIFO_DEPTH 32
146
147struct tegra_slink_chip_data {
148 bool cs_hold_time;
149};
150
151struct tegra_slink_data {
152 struct device *dev;
153 struct spi_master *master;
154 const struct tegra_slink_chip_data *chip_data;
155 spinlock_t lock;
156
157 struct clk *clk;
Stephen Warrenff2251e2013-11-06 16:31:24 -0700158 struct reset_control *rst;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530159 void __iomem *base;
160 phys_addr_t phys;
161 unsigned irq;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530162 u32 cur_speed;
163
164 struct spi_device *cur_spi;
165 unsigned cur_pos;
166 unsigned cur_len;
167 unsigned words_per_32bit;
168 unsigned bytes_per_word;
169 unsigned curr_dma_words;
170 unsigned cur_direction;
171
172 unsigned cur_rx_pos;
173 unsigned cur_tx_pos;
174
175 unsigned dma_buf_size;
176 unsigned max_buf_size;
177 bool is_curr_dma_xfer;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530178
179 struct completion rx_dma_complete;
180 struct completion tx_dma_complete;
181
182 u32 tx_status;
183 u32 rx_status;
184 u32 status_reg;
185 bool is_packed;
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100186 u32 packed_size;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530187
188 u32 command_reg;
189 u32 command2_reg;
190 u32 dma_control_reg;
191 u32 def_command_reg;
192 u32 def_command2_reg;
193
194 struct completion xfer_completion;
195 struct spi_transfer *curr_xfer;
196 struct dma_chan *rx_dma_chan;
197 u32 *rx_dma_buf;
198 dma_addr_t rx_dma_phys;
199 struct dma_async_tx_descriptor *rx_dma_desc;
200
201 struct dma_chan *tx_dma_chan;
202 u32 *tx_dma_buf;
203 dma_addr_t tx_dma_phys;
204 struct dma_async_tx_descriptor *tx_dma_desc;
205};
206
207static int tegra_slink_runtime_suspend(struct device *dev);
208static int tegra_slink_runtime_resume(struct device *dev);
209
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100210static inline u32 tegra_slink_readl(struct tegra_slink_data *tspi,
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530211 unsigned long reg)
212{
213 return readl(tspi->base + reg);
214}
215
216static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100217 u32 val, unsigned long reg)
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530218{
219 writel(val, tspi->base + reg);
220
221 /* Read back register to make sure that register writes completed */
222 if (reg != SLINK_TX_FIFO)
223 readl(tspi->base + SLINK_MAS_DATA);
224}
225
226static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
227{
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100228 u32 val_write;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530229
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100230 tegra_slink_readl(tspi, SLINK_STATUS);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530231
232 /* Write 1 to clear status register */
233 val_write = SLINK_RDY | SLINK_FIFO_ERROR;
234 tegra_slink_writel(tspi, val_write, SLINK_STATUS);
235}
236
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100237static u32 tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530238 struct spi_transfer *t)
239{
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530240 switch (tspi->bytes_per_word) {
241 case 0:
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100242 return SLINK_PACK_SIZE_4;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530243 case 1:
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100244 return SLINK_PACK_SIZE_8;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530245 case 2:
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100246 return SLINK_PACK_SIZE_16;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530247 case 4:
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100248 return SLINK_PACK_SIZE_32;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530249 default:
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100250 return 0;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530251 }
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530252}
253
254static unsigned tegra_slink_calculate_curr_xfer_param(
255 struct spi_device *spi, struct tegra_slink_data *tspi,
256 struct spi_transfer *t)
257{
258 unsigned remain_len = t->len - tspi->cur_pos;
259 unsigned max_word;
Jingoo Han3cb7b402013-10-14 10:36:10 +0900260 unsigned bits_per_word;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530261 unsigned max_len;
262 unsigned total_fifo_words;
263
Laxman Dewangan766ed702012-12-18 14:25:43 +0530264 bits_per_word = t->bits_per_word;
Axel Line91d2352013-08-30 11:00:23 +0800265 tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530266
267 if (bits_per_word == 8 || bits_per_word == 16) {
Gustavo A. R. Silva2172a332018-03-05 17:53:39 -0600268 tspi->is_packed = true;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530269 tspi->words_per_32bit = 32/bits_per_word;
270 } else {
Gustavo A. R. Silva2172a332018-03-05 17:53:39 -0600271 tspi->is_packed = false;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530272 tspi->words_per_32bit = 1;
273 }
274 tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
275
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;
280 } else {
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;
285 }
286 return total_fifo_words;
287}
288
289static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
290 struct tegra_slink_data *tspi, struct spi_transfer *t)
291{
292 unsigned nbytes;
293 unsigned tx_empty_count;
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100294 u32 fifo_status;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530295 unsigned max_n_32bit;
296 unsigned i, count;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530297 unsigned int written_words;
298 unsigned fifo_words_left;
299 u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
300
301 fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
302 tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
303
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++) {
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100310 u32 x = 0;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530311 for (i = 0; (i < 4) && nbytes; i++, nbytes--)
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100312 x |= (u32)(*tx_buf++) << (i * 8);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530313 tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
314 }
315 } else {
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++) {
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100320 u32 x = 0;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530321 for (i = 0; nbytes && (i < tspi->bytes_per_word);
322 i++, nbytes--)
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100323 x |= (u32)(*tx_buf++) << (i * 8);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530324 tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
325 }
326 }
327 tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
328 return written_words;
329}
330
331static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
332 struct tegra_slink_data *tspi, struct spi_transfer *t)
333{
334 unsigned rx_full_count;
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100335 u32 fifo_status;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530336 unsigned i, count;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530337 unsigned int read_words = 0;
338 unsigned len;
339 u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
340
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++) {
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100346 u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530347 for (i = 0; len && (i < 4); i++, len--)
348 *rx_buf++ = (x >> i*8) & 0xFF;
349 }
350 tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
351 read_words += tspi->curr_dma_words;
352 } else {
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530353 for (count = 0; count < rx_full_count; count++) {
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100354 u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530355 for (i = 0; (i < tspi->bytes_per_word); i++)
356 *rx_buf++ = (x >> (i*8)) & 0xFF;
357 }
358 tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
359 read_words += rx_full_count;
360 }
361 return read_words;
362}
363
364static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
365 struct tegra_slink_data *tspi, struct spi_transfer *t)
366{
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530367 /* 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);
370
371 if (tspi->is_packed) {
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100372 unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530373 memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
374 } else {
375 unsigned int i;
376 unsigned int count;
377 u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
378 unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530379
380 for (count = 0; count < tspi->curr_dma_words; count++) {
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100381 u32 x = 0;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530382 for (i = 0; consume && (i < tspi->bytes_per_word);
383 i++, consume--)
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100384 x |= (u32)(*tx_buf++) << (i * 8);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530385 tspi->tx_dma_buf[count] = x;
386 }
387 }
388 tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
389
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);
393}
394
395static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
396 struct tegra_slink_data *tspi, struct spi_transfer *t)
397{
398 unsigned len;
399
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);
403
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);
407 } else {
408 unsigned int i;
409 unsigned int count;
410 unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100411 u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530412
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530413 for (count = 0; count < tspi->curr_dma_words; count++) {
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100414 u32 x = tspi->rx_dma_buf[count] & rx_mask;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530415 for (i = 0; (i < tspi->bytes_per_word); i++)
416 *rx_buf++ = (x >> (i*8)) & 0xFF;
417 }
418 }
419 tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
420
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);
424}
425
426static void tegra_slink_dma_complete(void *args)
427{
428 struct completion *dma_complete = args;
429
430 complete(dma_complete);
431}
432
433static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
434{
Wolfram Sang16735d02013-11-14 14:32:02 -0800435 reinit_completion(&tspi->tx_dma_complete);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530436 tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
437 tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
Mark Brown72919f32013-04-03 18:30:31 +0100438 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530439 if (!tspi->tx_dma_desc) {
440 dev_err(tspi->dev, "Not able to get desc for Tx\n");
441 return -EIO;
442 }
443
444 tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
445 tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
446
447 dmaengine_submit(tspi->tx_dma_desc);
448 dma_async_issue_pending(tspi->tx_dma_chan);
449 return 0;
450}
451
452static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
453{
Wolfram Sang16735d02013-11-14 14:32:02 -0800454 reinit_completion(&tspi->rx_dma_complete);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530455 tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
456 tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
Mark Brown72919f32013-04-03 18:30:31 +0100457 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530458 if (!tspi->rx_dma_desc) {
459 dev_err(tspi->dev, "Not able to get desc for Rx\n");
460 return -EIO;
461 }
462
463 tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
464 tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
465
466 dmaengine_submit(tspi->rx_dma_desc);
467 dma_async_issue_pending(tspi->rx_dma_chan);
468 return 0;
469}
470
471static int tegra_slink_start_dma_based_transfer(
472 struct tegra_slink_data *tspi, struct spi_transfer *t)
473{
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100474 u32 val;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530475 unsigned int len;
476 int ret = 0;
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100477 u32 status;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530478
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) {
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100482 dev_err(tspi->dev, "Rx/Tx fifo are not empty status 0x%08x\n",
483 (unsigned)status);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530484 return -EIO;
485 }
486
487 val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
488 val |= tspi->packed_size;
489 if (tspi->is_packed)
490 len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
491 4) * 4;
492 else
493 len = tspi->curr_dma_words * 4;
494
495 /* Set attention level based on length of transfer */
496 if (len & 0xF)
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;
500 else
501 val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
502
503 if (tspi->cur_direction & DATA_DIR_TX)
504 val |= SLINK_IE_TXC;
505
506 if (tspi->cur_direction & DATA_DIR_RX)
507 val |= SLINK_IE_RXC;
508
509 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
510 tspi->dma_control_reg = val;
511
512 if (tspi->cur_direction & DATA_DIR_TX) {
513 tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
514 wmb();
515 ret = tegra_slink_start_tx_dma(tspi, len);
516 if (ret < 0) {
517 dev_err(tspi->dev,
518 "Starting tx dma failed, err %d\n", ret);
519 return ret;
520 }
521
522 /* Wait for tx fifo to be fill before starting slink */
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100523 status = tegra_slink_readl(tspi, SLINK_STATUS);
524 while (!(status & SLINK_TX_FULL))
525 status = tegra_slink_readl(tspi, SLINK_STATUS);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530526 }
527
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);
532
533 ret = tegra_slink_start_rx_dma(tspi, len);
534 if (ret < 0) {
535 dev_err(tspi->dev,
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);
539 return ret;
540 }
541 }
542 tspi->is_curr_dma_xfer = true;
543 if (tspi->is_packed) {
544 val |= SLINK_PACKED;
545 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
546 /* HW need small delay after settign Packed mode */
547 udelay(1);
548 }
549 tspi->dma_control_reg = val;
550
551 val |= SLINK_DMA_EN;
552 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
553 return ret;
554}
555
556static int tegra_slink_start_cpu_based_transfer(
557 struct tegra_slink_data *tspi, struct spi_transfer *t)
558{
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100559 u32 val;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530560 unsigned cur_words;
561
562 val = tspi->packed_size;
563 if (tspi->cur_direction & DATA_DIR_TX)
564 val |= SLINK_IE_TXC;
565
566 if (tspi->cur_direction & DATA_DIR_RX)
567 val |= SLINK_IE_RXC;
568
569 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
570 tspi->dma_control_reg = val;
571
572 if (tspi->cur_direction & DATA_DIR_TX)
573 cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
574 else
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;
579
580 tspi->is_curr_dma_xfer = false;
581 if (tspi->is_packed) {
582 val |= SLINK_PACKED;
583 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
584 udelay(1);
585 wmb();
586 }
587 tspi->dma_control_reg = val;
588 val |= SLINK_DMA_EN;
589 tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
590 return 0;
591}
592
593static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
594 bool dma_to_memory)
595{
596 struct dma_chan *dma_chan;
597 u32 *dma_buf;
598 dma_addr_t dma_phys;
599 int ret;
600 struct dma_slave_config dma_sconfig;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530601
Peter Ujfalusi912a7df2019-11-13 11:42:56 +0200602 dma_chan = dma_request_chan(tspi->dev, dma_to_memory ? "rx" : "tx");
Stephen Warrena915d152013-11-11 13:13:47 -0700603 if (IS_ERR(dma_chan)) {
604 ret = PTR_ERR(dma_chan);
605 if (ret != -EPROBE_DEFER)
606 dev_err(tspi->dev,
607 "Dma channel is not available: %d\n", ret);
608 return ret;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530609 }
610
611 dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
612 &dma_phys, GFP_KERNEL);
613 if (!dma_buf) {
614 dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
615 dma_release_channel(dma_chan);
616 return -ENOMEM;
617 }
618
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530619 if (dma_to_memory) {
620 dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
621 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
622 dma_sconfig.src_maxburst = 0;
623 } else {
624 dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
625 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
626 dma_sconfig.dst_maxburst = 0;
627 }
628
629 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
630 if (ret)
631 goto scrub;
632 if (dma_to_memory) {
633 tspi->rx_dma_chan = dma_chan;
634 tspi->rx_dma_buf = dma_buf;
635 tspi->rx_dma_phys = dma_phys;
636 } else {
637 tspi->tx_dma_chan = dma_chan;
638 tspi->tx_dma_buf = dma_buf;
639 tspi->tx_dma_phys = dma_phys;
640 }
641 return 0;
642
643scrub:
644 dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
645 dma_release_channel(dma_chan);
646 return ret;
647}
648
649static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
650 bool dma_to_memory)
651{
652 u32 *dma_buf;
653 dma_addr_t dma_phys;
654 struct dma_chan *dma_chan;
655
656 if (dma_to_memory) {
657 dma_buf = tspi->rx_dma_buf;
658 dma_chan = tspi->rx_dma_chan;
659 dma_phys = tspi->rx_dma_phys;
660 tspi->rx_dma_chan = NULL;
661 tspi->rx_dma_buf = NULL;
662 } else {
663 dma_buf = tspi->tx_dma_buf;
664 dma_chan = tspi->tx_dma_chan;
665 dma_phys = tspi->tx_dma_phys;
666 tspi->tx_dma_buf = NULL;
667 tspi->tx_dma_chan = NULL;
668 }
669 if (!dma_chan)
670 return;
671
672 dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
673 dma_release_channel(dma_chan);
674}
675
676static int tegra_slink_start_transfer_one(struct spi_device *spi,
Mark Brownf178e3d2013-10-05 12:30:42 +0100677 struct spi_transfer *t)
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530678{
679 struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
680 u32 speed;
681 u8 bits_per_word;
682 unsigned total_fifo_words;
683 int ret;
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100684 u32 command;
685 u32 command2;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530686
Laxman Dewangane6811d12012-11-09 14:36:45 +0530687 bits_per_word = t->bits_per_word;
Laxman Dewanganbeb96c22013-01-05 00:17:15 +0530688 speed = t->speed_hz;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530689 if (speed != tspi->cur_speed) {
690 clk_set_rate(tspi->clk, speed * 4);
691 tspi->cur_speed = speed;
692 }
693
694 tspi->cur_spi = spi;
695 tspi->cur_pos = 0;
696 tspi->cur_rx_pos = 0;
697 tspi->cur_tx_pos = 0;
698 tspi->curr_xfer = t;
699 total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
700
Mark Brownf178e3d2013-10-05 12:30:42 +0100701 command = tspi->command_reg;
702 command &= ~SLINK_BIT_LENGTH(~0);
703 command |= SLINK_BIT_LENGTH(bits_per_word - 1);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530704
Mark Brownf178e3d2013-10-05 12:30:42 +0100705 command2 = tspi->command2_reg;
706 command2 &= ~(SLINK_RXEN | SLINK_TXEN);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530707
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530708 tspi->cur_direction = 0;
709 if (t->rx_buf) {
710 command2 |= SLINK_RXEN;
711 tspi->cur_direction |= DATA_DIR_RX;
712 }
713 if (t->tx_buf) {
714 command2 |= SLINK_TXEN;
715 tspi->cur_direction |= DATA_DIR_TX;
716 }
Randolph Maaßen0e694df2019-03-26 15:30:50 +0100717
718 /*
719 * Writing to the command2 register bevore the command register prevents
720 * a spike in chip_select line 0. This selects the chip_select line
721 * before changing the chip_select value.
722 */
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530723 tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
724 tspi->command2_reg = command2;
725
Randolph Maaßen0e694df2019-03-26 15:30:50 +0100726 tegra_slink_writel(tspi, command, SLINK_COMMAND);
727 tspi->command_reg = command;
728
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530729 if (total_fifo_words > SLINK_FIFO_DEPTH)
730 ret = tegra_slink_start_dma_based_transfer(tspi, t);
731 else
732 ret = tegra_slink_start_cpu_based_transfer(tspi, t);
733 return ret;
734}
735
736static int tegra_slink_setup(struct spi_device *spi)
737{
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100738 static const u32 cs_pol_bit[MAX_CHIP_SELECT] = {
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530739 SLINK_CS_POLARITY,
740 SLINK_CS_POLARITY1,
741 SLINK_CS_POLARITY2,
742 SLINK_CS_POLARITY3,
743 };
744
Michal Nazarewicz5fd38672013-12-08 16:35:10 +0100745 struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
746 u32 val;
747 unsigned long flags;
748 int ret;
749
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530750 dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
751 spi->bits_per_word,
752 spi->mode & SPI_CPOL ? "" : "~",
753 spi->mode & SPI_CPHA ? "" : "~",
754 spi->max_speed_hz);
755
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530756 ret = pm_runtime_get_sync(tspi->dev);
757 if (ret < 0) {
758 dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
759 return ret;
760 }
761
762 spin_lock_irqsave(&tspi->lock, flags);
763 val = tspi->def_command_reg;
764 if (spi->mode & SPI_CS_HIGH)
765 val |= cs_pol_bit[spi->chip_select];
766 else
767 val &= ~cs_pol_bit[spi->chip_select];
768 tspi->def_command_reg = val;
769 tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
770 spin_unlock_irqrestore(&tspi->lock, flags);
771
772 pm_runtime_put(tspi->dev);
773 return 0;
774}
775
Mark Brown63fc1842013-10-05 12:23:38 +0100776static int tegra_slink_prepare_message(struct spi_master *master,
777 struct spi_message *msg)
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530778{
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530779 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530780 struct spi_device *spi = msg->spi;
Mark Brown63fc1842013-10-05 12:23:38 +0100781
Mark Brownf178e3d2013-10-05 12:30:42 +0100782 tegra_slink_clear_status(tspi);
783
784 tspi->command_reg = tspi->def_command_reg;
785 tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
786
787 tspi->command2_reg = tspi->def_command2_reg;
788 tspi->command2_reg |= SLINK_SS_EN_CS(spi->chip_select);
789
790 tspi->command_reg &= ~SLINK_MODES;
791 if (spi->mode & SPI_CPHA)
792 tspi->command_reg |= SLINK_CK_SDA;
793
794 if (spi->mode & SPI_CPOL)
795 tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
796 else
797 tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
Mark Brown63fc1842013-10-05 12:23:38 +0100798
799 return 0;
800}
801
802static int tegra_slink_transfer_one(struct spi_master *master,
803 struct spi_device *spi,
804 struct spi_transfer *xfer)
805{
806 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530807 int ret;
808
Wolfram Sang16735d02013-11-14 14:32:02 -0800809 reinit_completion(&tspi->xfer_completion);
Mark Brownf178e3d2013-10-05 12:30:42 +0100810 ret = tegra_slink_start_transfer_one(spi, xfer);
Mark Brown63fc1842013-10-05 12:23:38 +0100811 if (ret < 0) {
812 dev_err(tspi->dev,
813 "spi can not start transfer, err %d\n", ret);
814 return ret;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530815 }
Mark Brownf178e3d2013-10-05 12:30:42 +0100816
Mark Brown63fc1842013-10-05 12:23:38 +0100817 ret = wait_for_completion_timeout(&tspi->xfer_completion,
818 SLINK_DMA_TIMEOUT);
819 if (WARN_ON(ret == 0)) {
820 dev_err(tspi->dev,
Colin Ian Kingbfca7612017-04-23 18:14:36 +0100821 "spi transfer timeout, err %d\n", ret);
Mark Brown63fc1842013-10-05 12:23:38 +0100822 return -EIO;
823 }
824
825 if (tspi->tx_status)
826 return tspi->tx_status;
827 if (tspi->rx_status)
828 return tspi->rx_status;
829
830 return 0;
831}
832
833static int tegra_slink_unprepare_message(struct spi_master *master,
834 struct spi_message *msg)
835{
836 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
837
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530838 tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
839 tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
Mark Brown63fc1842013-10-05 12:23:38 +0100840
841 return 0;
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530842}
843
844static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
845{
846 struct spi_transfer *t = tspi->curr_xfer;
847 unsigned long flags;
848
849 spin_lock_irqsave(&tspi->lock, flags);
850 if (tspi->tx_status || tspi->rx_status ||
851 (tspi->status_reg & SLINK_BSY)) {
852 dev_err(tspi->dev,
853 "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
854 dev_err(tspi->dev,
855 "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
856 tspi->command2_reg, tspi->dma_control_reg);
Stephen Warrenff2251e2013-11-06 16:31:24 -0700857 reset_control_assert(tspi->rst);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530858 udelay(2);
Stephen Warrenff2251e2013-11-06 16:31:24 -0700859 reset_control_deassert(tspi->rst);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530860 complete(&tspi->xfer_completion);
861 goto exit;
862 }
863
864 if (tspi->cur_direction & DATA_DIR_RX)
865 tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
866
867 if (tspi->cur_direction & DATA_DIR_TX)
868 tspi->cur_pos = tspi->cur_tx_pos;
869 else
870 tspi->cur_pos = tspi->cur_rx_pos;
871
872 if (tspi->cur_pos == t->len) {
873 complete(&tspi->xfer_completion);
874 goto exit;
875 }
876
877 tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
878 tegra_slink_start_cpu_based_transfer(tspi, t);
879exit:
880 spin_unlock_irqrestore(&tspi->lock, flags);
881 return IRQ_HANDLED;
882}
883
884static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
885{
886 struct spi_transfer *t = tspi->curr_xfer;
887 long wait_status;
888 int err = 0;
889 unsigned total_fifo_words;
890 unsigned long flags;
891
892 /* Abort dmas if any error */
893 if (tspi->cur_direction & DATA_DIR_TX) {
894 if (tspi->tx_status) {
895 dmaengine_terminate_all(tspi->tx_dma_chan);
896 err += 1;
897 } else {
898 wait_status = wait_for_completion_interruptible_timeout(
899 &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
900 if (wait_status <= 0) {
901 dmaengine_terminate_all(tspi->tx_dma_chan);
902 dev_err(tspi->dev, "TxDma Xfer failed\n");
903 err += 1;
904 }
905 }
906 }
907
908 if (tspi->cur_direction & DATA_DIR_RX) {
909 if (tspi->rx_status) {
910 dmaengine_terminate_all(tspi->rx_dma_chan);
911 err += 2;
912 } else {
913 wait_status = wait_for_completion_interruptible_timeout(
914 &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
915 if (wait_status <= 0) {
916 dmaengine_terminate_all(tspi->rx_dma_chan);
917 dev_err(tspi->dev, "RxDma Xfer failed\n");
918 err += 2;
919 }
920 }
921 }
922
923 spin_lock_irqsave(&tspi->lock, flags);
924 if (err) {
925 dev_err(tspi->dev,
926 "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
927 dev_err(tspi->dev,
928 "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
929 tspi->command2_reg, tspi->dma_control_reg);
Stephen Warrenff2251e2013-11-06 16:31:24 -0700930 reset_control_assert(tspi->rst);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530931 udelay(2);
Stephen Warrenff2251e2013-11-06 16:31:24 -0700932 reset_control_assert(tspi->rst);
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530933 complete(&tspi->xfer_completion);
934 spin_unlock_irqrestore(&tspi->lock, flags);
935 return IRQ_HANDLED;
936 }
937
938 if (tspi->cur_direction & DATA_DIR_RX)
939 tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
940
941 if (tspi->cur_direction & DATA_DIR_TX)
942 tspi->cur_pos = tspi->cur_tx_pos;
943 else
944 tspi->cur_pos = tspi->cur_rx_pos;
945
946 if (tspi->cur_pos == t->len) {
947 complete(&tspi->xfer_completion);
948 goto exit;
949 }
950
951 /* Continue transfer in current message */
952 total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
953 tspi, t);
954 if (total_fifo_words > SLINK_FIFO_DEPTH)
955 err = tegra_slink_start_dma_based_transfer(tspi, t);
956 else
957 err = tegra_slink_start_cpu_based_transfer(tspi, t);
958
959exit:
960 spin_unlock_irqrestore(&tspi->lock, flags);
961 return IRQ_HANDLED;
962}
963
964static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
965{
966 struct tegra_slink_data *tspi = context_data;
967
968 if (!tspi->is_curr_dma_xfer)
969 return handle_cpu_based_xfer(tspi);
970 return handle_dma_based_xfer(tspi);
971}
972
973static irqreturn_t tegra_slink_isr(int irq, void *context_data)
974{
975 struct tegra_slink_data *tspi = context_data;
976
977 tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
978 if (tspi->cur_direction & DATA_DIR_TX)
979 tspi->tx_status = tspi->status_reg &
980 (SLINK_TX_OVF | SLINK_TX_UNF);
981
982 if (tspi->cur_direction & DATA_DIR_RX)
983 tspi->rx_status = tspi->status_reg &
984 (SLINK_RX_OVF | SLINK_RX_UNF);
985 tegra_slink_clear_status(tspi);
986
987 return IRQ_WAKE_THREAD;
988}
989
Wei Yongjun8b0bebe2013-04-05 21:45:36 +0800990static const struct tegra_slink_chip_data tegra30_spi_cdata = {
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530991 .cs_hold_time = true,
992};
993
Wei Yongjun8b0bebe2013-04-05 21:45:36 +0800994static const struct tegra_slink_chip_data tegra20_spi_cdata = {
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530995 .cs_hold_time = false,
996};
997
Jingoo Hanb2fb1872014-05-07 16:52:36 +0900998static const struct of_device_id tegra_slink_of_match[] = {
Laxman Dewangandc4dc362012-10-30 12:34:05 +0530999 { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
Laxman Dewangan24bc8972012-11-09 14:37:32 +05301000 { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301001 {}
1002};
1003MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
1004
Grant Likelyfd4a3192012-12-07 16:57:14 +00001005static int tegra_slink_probe(struct platform_device *pdev)
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301006{
1007 struct spi_master *master;
1008 struct tegra_slink_data *tspi;
1009 struct resource *r;
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301010 int ret, spi_irq;
1011 const struct tegra_slink_chip_data *cdata = NULL;
1012 const struct of_device_id *match;
1013
Stephen Warrenc60fea02013-02-15 15:03:49 -07001014 match = of_match_device(tegra_slink_of_match, &pdev->dev);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301015 if (!match) {
1016 dev_err(&pdev->dev, "Error: No device match found\n");
1017 return -ENODEV;
1018 }
1019 cdata = match->data;
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301020
1021 master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
1022 if (!master) {
1023 dev_err(&pdev->dev, "master allocation failed\n");
1024 return -ENOMEM;
1025 }
1026
1027 /* the spi->mode bits understood by this driver: */
1028 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1029 master->setup = tegra_slink_setup;
Mark Brown63fc1842013-10-05 12:23:38 +01001030 master->prepare_message = tegra_slink_prepare_message;
1031 master->transfer_one = tegra_slink_transfer_one;
1032 master->unprepare_message = tegra_slink_unprepare_message;
Mark Brownce74ac82013-07-28 15:37:59 +01001033 master->auto_runtime_pm = true;
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301034 master->num_chipselect = MAX_CHIP_SELECT;
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301035
Jingoo Han24b5a822013-05-23 19:20:40 +09001036 platform_set_drvdata(pdev, master);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301037 tspi = spi_master_get_devdata(master);
1038 tspi->master = master;
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301039 tspi->dev = &pdev->dev;
1040 tspi->chip_data = cdata;
1041 spin_lock_init(&tspi->lock);
1042
Axel Lin3c604de2014-02-10 21:51:13 +08001043 if (of_property_read_u32(tspi->dev->of_node, "spi-max-frequency",
1044 &master->max_speed_hz))
1045 master->max_speed_hz = 25000000; /* 25MHz */
Stephen Warrenc60fea02013-02-15 15:03:49 -07001046
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301047 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1048 if (!r) {
1049 dev_err(&pdev->dev, "No IO memory resource\n");
1050 ret = -ENODEV;
1051 goto exit_free_master;
1052 }
1053 tspi->phys = r->start;
Thierry Redingb0ee5602013-01-21 11:09:18 +01001054 tspi->base = devm_ioremap_resource(&pdev->dev, r);
1055 if (IS_ERR(tspi->base)) {
1056 ret = PTR_ERR(tspi->base);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301057 goto exit_free_master;
1058 }
1059
Marcel Ziswiler7001cab2018-08-29 08:47:57 +02001060 /* disabled clock may cause interrupt storm upon request */
1061 tspi->clk = devm_clk_get(&pdev->dev, NULL);
1062 if (IS_ERR(tspi->clk)) {
1063 ret = PTR_ERR(tspi->clk);
1064 dev_err(&pdev->dev, "Can not get clock %d\n", ret);
1065 goto exit_free_master;
1066 }
1067 ret = clk_prepare(tspi->clk);
1068 if (ret < 0) {
1069 dev_err(&pdev->dev, "Clock prepare failed %d\n", ret);
1070 goto exit_free_master;
1071 }
1072 ret = clk_enable(tspi->clk);
1073 if (ret < 0) {
1074 dev_err(&pdev->dev, "Clock enable failed %d\n", ret);
Chuhong Yuan04358e42019-11-15 16:31:22 +08001075 goto exit_clk_unprepare;
Marcel Ziswiler7001cab2018-08-29 08:47:57 +02001076 }
1077
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301078 spi_irq = platform_get_irq(pdev, 0);
1079 tspi->irq = spi_irq;
1080 ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
1081 tegra_slink_isr_thread, IRQF_ONESHOT,
1082 dev_name(&pdev->dev), tspi);
1083 if (ret < 0) {
1084 dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
1085 tspi->irq);
Marcel Ziswiler7001cab2018-08-29 08:47:57 +02001086 goto exit_clk_disable;
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301087 }
1088
Philipp Zabel73b32752017-07-19 17:26:22 +02001089 tspi->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
Stephen Warrenff2251e2013-11-06 16:31:24 -07001090 if (IS_ERR(tspi->rst)) {
1091 dev_err(&pdev->dev, "can not get reset\n");
1092 ret = PTR_ERR(tspi->rst);
1093 goto exit_free_irq;
1094 }
1095
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301096 tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
1097 tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301098
Stephen Warrena915d152013-11-11 13:13:47 -07001099 ret = tegra_slink_init_dma_param(tspi, true);
1100 if (ret < 0)
1101 goto exit_free_irq;
1102 ret = tegra_slink_init_dma_param(tspi, false);
1103 if (ret < 0)
1104 goto exit_rx_dma_free;
1105 tspi->max_buf_size = tspi->dma_buf_size;
1106 init_completion(&tspi->tx_dma_complete);
1107 init_completion(&tspi->rx_dma_complete);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301108
1109 init_completion(&tspi->xfer_completion);
1110
1111 pm_runtime_enable(&pdev->dev);
1112 if (!pm_runtime_enabled(&pdev->dev)) {
1113 ret = tegra_slink_runtime_resume(&pdev->dev);
1114 if (ret)
1115 goto exit_pm_disable;
1116 }
1117
1118 ret = pm_runtime_get_sync(&pdev->dev);
1119 if (ret < 0) {
1120 dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
Dinghao Liufaedcc12020-05-23 20:29:09 +08001121 pm_runtime_put_noidle(&pdev->dev);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301122 goto exit_pm_disable;
1123 }
1124 tspi->def_command_reg = SLINK_M_S;
1125 tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
1126 tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
1127 tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
1128 pm_runtime_put(&pdev->dev);
1129
1130 master->dev.of_node = pdev->dev.of_node;
Jingoo Han716db5d2013-09-24 13:51:32 +09001131 ret = devm_spi_register_master(&pdev->dev, master);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301132 if (ret < 0) {
1133 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
1134 goto exit_pm_disable;
1135 }
1136 return ret;
1137
1138exit_pm_disable:
1139 pm_runtime_disable(&pdev->dev);
1140 if (!pm_runtime_status_suspended(&pdev->dev))
1141 tegra_slink_runtime_suspend(&pdev->dev);
1142 tegra_slink_deinit_dma_param(tspi, false);
1143exit_rx_dma_free:
1144 tegra_slink_deinit_dma_param(tspi, true);
1145exit_free_irq:
1146 free_irq(spi_irq, tspi);
Marcel Ziswiler7001cab2018-08-29 08:47:57 +02001147exit_clk_disable:
1148 clk_disable(tspi->clk);
Chuhong Yuan04358e42019-11-15 16:31:22 +08001149exit_clk_unprepare:
1150 clk_unprepare(tspi->clk);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301151exit_free_master:
1152 spi_master_put(master);
1153 return ret;
1154}
1155
Grant Likelyfd4a3192012-12-07 16:57:14 +00001156static int tegra_slink_remove(struct platform_device *pdev)
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301157{
Jingoo Han24b5a822013-05-23 19:20:40 +09001158 struct spi_master *master = platform_get_drvdata(pdev);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301159 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
1160
1161 free_irq(tspi->irq, tspi);
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301162
Marcel Ziswiler7001cab2018-08-29 08:47:57 +02001163 clk_disable(tspi->clk);
Chuhong Yuan04358e42019-11-15 16:31:22 +08001164 clk_unprepare(tspi->clk);
Marcel Ziswiler7001cab2018-08-29 08:47:57 +02001165
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301166 if (tspi->tx_dma_chan)
1167 tegra_slink_deinit_dma_param(tspi, false);
1168
1169 if (tspi->rx_dma_chan)
1170 tegra_slink_deinit_dma_param(tspi, true);
1171
1172 pm_runtime_disable(&pdev->dev);
1173 if (!pm_runtime_status_suspended(&pdev->dev))
1174 tegra_slink_runtime_suspend(&pdev->dev);
1175
1176 return 0;
1177}
1178
1179#ifdef CONFIG_PM_SLEEP
1180static int tegra_slink_suspend(struct device *dev)
1181{
1182 struct spi_master *master = dev_get_drvdata(dev);
1183
1184 return spi_master_suspend(master);
1185}
1186
1187static int tegra_slink_resume(struct device *dev)
1188{
1189 struct spi_master *master = dev_get_drvdata(dev);
1190 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
1191 int ret;
1192
1193 ret = pm_runtime_get_sync(dev);
1194 if (ret < 0) {
1195 dev_err(dev, "pm runtime failed, e = %d\n", ret);
1196 return ret;
1197 }
1198 tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
1199 tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
1200 pm_runtime_put(dev);
1201
1202 return spi_master_resume(master);
1203}
1204#endif
1205
1206static int tegra_slink_runtime_suspend(struct device *dev)
1207{
1208 struct spi_master *master = dev_get_drvdata(dev);
1209 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
1210
1211 /* Flush all write which are in PPSB queue by reading back */
1212 tegra_slink_readl(tspi, SLINK_MAS_DATA);
1213
1214 clk_disable_unprepare(tspi->clk);
1215 return 0;
1216}
1217
1218static int tegra_slink_runtime_resume(struct device *dev)
1219{
1220 struct spi_master *master = dev_get_drvdata(dev);
1221 struct tegra_slink_data *tspi = spi_master_get_devdata(master);
1222 int ret;
1223
1224 ret = clk_prepare_enable(tspi->clk);
1225 if (ret < 0) {
1226 dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
1227 return ret;
1228 }
1229 return 0;
1230}
1231
1232static const struct dev_pm_ops slink_pm_ops = {
1233 SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
1234 tegra_slink_runtime_resume, NULL)
1235 SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
1236};
1237static struct platform_driver tegra_slink_driver = {
1238 .driver = {
1239 .name = "spi-tegra-slink",
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301240 .pm = &slink_pm_ops,
Stephen Warrenc60fea02013-02-15 15:03:49 -07001241 .of_match_table = tegra_slink_of_match,
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301242 },
1243 .probe = tegra_slink_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001244 .remove = tegra_slink_remove,
Laxman Dewangandc4dc362012-10-30 12:34:05 +05301245};
1246module_platform_driver(tegra_slink_driver);
1247
1248MODULE_ALIAS("platform:spi-tegra-slink");
1249MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
1250MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1251MODULE_LICENSE("GPL v2");