Blame view

drivers/spi/spi-tegra20-slink.c 33.8 KB
9952f6918   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
2
3
4
5
  /*
   * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
   *
   * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
6
7
8
9
10
11
12
13
14
   */
  
  #include <linux/clk.h>
  #include <linux/completion.h>
  #include <linux/delay.h>
  #include <linux/dmaengine.h>
  #include <linux/dma-mapping.h>
  #include <linux/dmapool.h>
  #include <linux/err.h>
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
15
16
17
18
19
20
21
22
23
  #include <linux/interrupt.h>
  #include <linux/io.h>
  #include <linux/kernel.h>
  #include <linux/kthread.h>
  #include <linux/module.h>
  #include <linux/platform_device.h>
  #include <linux/pm_runtime.h>
  #include <linux/of.h>
  #include <linux/of_device.h>
ff2251e3d   Stephen Warren   spi: tegra: use r...
24
  #include <linux/reset.h>
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
25
  #include <linux/spi/spi.h>
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  
  #define SLINK_COMMAND			0x000
  #define SLINK_BIT_LENGTH(x)		(((x) & 0x1f) << 0)
  #define SLINK_WORD_SIZE(x)		(((x) & 0x1f) << 5)
  #define SLINK_BOTH_EN			(1 << 10)
  #define SLINK_CS_SW			(1 << 11)
  #define SLINK_CS_VALUE			(1 << 12)
  #define SLINK_CS_POLARITY		(1 << 13)
  #define SLINK_IDLE_SDA_DRIVE_LOW	(0 << 16)
  #define SLINK_IDLE_SDA_DRIVE_HIGH	(1 << 16)
  #define SLINK_IDLE_SDA_PULL_LOW		(2 << 16)
  #define SLINK_IDLE_SDA_PULL_HIGH	(3 << 16)
  #define SLINK_IDLE_SDA_MASK		(3 << 16)
  #define SLINK_CS_POLARITY1		(1 << 20)
  #define SLINK_CK_SDA			(1 << 21)
  #define SLINK_CS_POLARITY2		(1 << 22)
  #define SLINK_CS_POLARITY3		(1 << 23)
  #define SLINK_IDLE_SCLK_DRIVE_LOW	(0 << 24)
  #define SLINK_IDLE_SCLK_DRIVE_HIGH	(1 << 24)
  #define SLINK_IDLE_SCLK_PULL_LOW	(2 << 24)
  #define SLINK_IDLE_SCLK_PULL_HIGH	(3 << 24)
  #define SLINK_IDLE_SCLK_MASK		(3 << 24)
  #define SLINK_M_S			(1 << 28)
  #define SLINK_WAIT			(1 << 29)
  #define SLINK_GO			(1 << 30)
  #define SLINK_ENB			(1 << 31)
  
  #define SLINK_MODES			(SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
  
  #define SLINK_COMMAND2			0x004
  #define SLINK_LSBFE			(1 << 0)
  #define SLINK_SSOE			(1 << 1)
  #define SLINK_SPIE			(1 << 4)
  #define SLINK_BIDIROE			(1 << 6)
  #define SLINK_MODFEN			(1 << 7)
  #define SLINK_INT_SIZE(x)		(((x) & 0x1f) << 8)
  #define SLINK_CS_ACTIVE_BETWEEN		(1 << 17)
  #define SLINK_SS_EN_CS(x)		(((x) & 0x3) << 18)
  #define SLINK_SS_SETUP(x)		(((x) & 0x3) << 20)
  #define SLINK_FIFO_REFILLS_0		(0 << 22)
  #define SLINK_FIFO_REFILLS_1		(1 << 22)
  #define SLINK_FIFO_REFILLS_2		(2 << 22)
  #define SLINK_FIFO_REFILLS_3		(3 << 22)
  #define SLINK_FIFO_REFILLS_MASK		(3 << 22)
  #define SLINK_WAIT_PACK_INT(x)		(((x) & 0x7) << 26)
  #define SLINK_SPC0			(1 << 29)
  #define SLINK_TXEN			(1 << 30)
  #define SLINK_RXEN			(1 << 31)
  
  #define SLINK_STATUS			0x008
  #define SLINK_COUNT(val)		(((val) >> 0) & 0x1f)
  #define SLINK_WORD(val)			(((val) >> 5) & 0x1f)
  #define SLINK_BLK_CNT(val)		(((val) >> 0) & 0xffff)
  #define SLINK_MODF			(1 << 16)
  #define SLINK_RX_UNF			(1 << 18)
  #define SLINK_TX_OVF			(1 << 19)
  #define SLINK_TX_FULL			(1 << 20)
  #define SLINK_TX_EMPTY			(1 << 21)
  #define SLINK_RX_FULL			(1 << 22)
  #define SLINK_RX_EMPTY			(1 << 23)
  #define SLINK_TX_UNF			(1 << 24)
  #define SLINK_RX_OVF			(1 << 25)
  #define SLINK_TX_FLUSH			(1 << 26)
  #define SLINK_RX_FLUSH			(1 << 27)
  #define SLINK_SCLK			(1 << 28)
  #define SLINK_ERR			(1 << 29)
  #define SLINK_RDY			(1 << 30)
  #define SLINK_BSY			(1 << 31)
  #define SLINK_FIFO_ERROR		(SLINK_TX_OVF | SLINK_RX_UNF |	\
  					SLINK_TX_UNF | SLINK_RX_OVF)
  
  #define SLINK_FIFO_EMPTY		(SLINK_TX_EMPTY | SLINK_RX_EMPTY)
  
  #define SLINK_MAS_DATA			0x010
  #define SLINK_SLAVE_DATA		0x014
  
  #define SLINK_DMA_CTL			0x018
  #define SLINK_DMA_BLOCK_SIZE(x)		(((x) & 0xffff) << 0)
  #define SLINK_TX_TRIG_1			(0 << 16)
  #define SLINK_TX_TRIG_4			(1 << 16)
  #define SLINK_TX_TRIG_8			(2 << 16)
  #define SLINK_TX_TRIG_16		(3 << 16)
  #define SLINK_TX_TRIG_MASK		(3 << 16)
  #define SLINK_RX_TRIG_1			(0 << 18)
  #define SLINK_RX_TRIG_4			(1 << 18)
  #define SLINK_RX_TRIG_8			(2 << 18)
  #define SLINK_RX_TRIG_16		(3 << 18)
  #define SLINK_RX_TRIG_MASK		(3 << 18)
  #define SLINK_PACKED			(1 << 20)
  #define SLINK_PACK_SIZE_4		(0 << 21)
  #define SLINK_PACK_SIZE_8		(1 << 21)
  #define SLINK_PACK_SIZE_16		(2 << 21)
  #define SLINK_PACK_SIZE_32		(3 << 21)
  #define SLINK_PACK_SIZE_MASK		(3 << 21)
  #define SLINK_IE_TXC			(1 << 26)
  #define SLINK_IE_RXC			(1 << 27)
  #define SLINK_DMA_EN			(1 << 31)
  
  #define SLINK_STATUS2			0x01c
  #define SLINK_TX_FIFO_EMPTY_COUNT(val)	(((val) & 0x3f) >> 0)
  #define SLINK_RX_FIFO_FULL_COUNT(val)	(((val) & 0x3f0000) >> 16)
  #define SLINK_SS_HOLD_TIME(val)		(((val) & 0xF) << 6)
  
  #define SLINK_TX_FIFO			0x100
  #define SLINK_RX_FIFO			0x180
  
  #define DATA_DIR_TX			(1 << 0)
  #define DATA_DIR_RX			(1 << 1)
  
  #define SLINK_DMA_TIMEOUT		(msecs_to_jiffies(1000))
  
  #define DEFAULT_SPI_DMA_BUF_LEN		(16*1024)
  #define TX_FIFO_EMPTY_COUNT_MAX		SLINK_TX_FIFO_EMPTY_COUNT(0x20)
  #define RX_FIFO_FULL_COUNT_ZERO		SLINK_RX_FIFO_FULL_COUNT(0)
  
  #define SLINK_STATUS2_RESET \
  	(TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
  
  #define MAX_CHIP_SELECT			4
  #define SLINK_FIFO_DEPTH		32
  
  struct tegra_slink_chip_data {
  	bool cs_hold_time;
  };
  
  struct tegra_slink_data {
  	struct device				*dev;
  	struct spi_master			*master;
  	const struct tegra_slink_chip_data	*chip_data;
  	spinlock_t				lock;
  
  	struct clk				*clk;
ff2251e3d   Stephen Warren   spi: tegra: use r...
158
  	struct reset_control			*rst;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
159
160
161
  	void __iomem				*base;
  	phys_addr_t				phys;
  	unsigned				irq;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  	u32					cur_speed;
  
  	struct spi_device			*cur_spi;
  	unsigned				cur_pos;
  	unsigned				cur_len;
  	unsigned				words_per_32bit;
  	unsigned				bytes_per_word;
  	unsigned				curr_dma_words;
  	unsigned				cur_direction;
  
  	unsigned				cur_rx_pos;
  	unsigned				cur_tx_pos;
  
  	unsigned				dma_buf_size;
  	unsigned				max_buf_size;
  	bool					is_curr_dma_xfer;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
178
179
180
181
182
183
184
185
  
  	struct completion			rx_dma_complete;
  	struct completion			tx_dma_complete;
  
  	u32					tx_status;
  	u32					rx_status;
  	u32					status_reg;
  	bool					is_packed;
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
186
  	u32					packed_size;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  
  	u32					command_reg;
  	u32					command2_reg;
  	u32					dma_control_reg;
  	u32					def_command_reg;
  	u32					def_command2_reg;
  
  	struct completion			xfer_completion;
  	struct spi_transfer			*curr_xfer;
  	struct dma_chan				*rx_dma_chan;
  	u32					*rx_dma_buf;
  	dma_addr_t				rx_dma_phys;
  	struct dma_async_tx_descriptor		*rx_dma_desc;
  
  	struct dma_chan				*tx_dma_chan;
  	u32					*tx_dma_buf;
  	dma_addr_t				tx_dma_phys;
  	struct dma_async_tx_descriptor		*tx_dma_desc;
  };
  
  static int tegra_slink_runtime_suspend(struct device *dev);
  static int tegra_slink_runtime_resume(struct device *dev);
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
209
  static inline u32 tegra_slink_readl(struct tegra_slink_data *tspi,
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
210
211
212
213
214
215
  		unsigned long reg)
  {
  	return readl(tspi->base + reg);
  }
  
  static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
216
  		u32 val, unsigned long reg)
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
217
218
219
220
221
222
223
224
225
226
  {
  	writel(val, tspi->base + reg);
  
  	/* Read back register to make sure that register writes completed */
  	if (reg != SLINK_TX_FIFO)
  		readl(tspi->base + SLINK_MAS_DATA);
  }
  
  static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
  {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
227
  	u32 val_write;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
228

5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
229
  	tegra_slink_readl(tspi, SLINK_STATUS);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
230
231
232
233
234
  
  	/* Write 1 to clear status register */
  	val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  	tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  }
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
235
  static u32 tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
236
237
  				  struct spi_transfer *t)
  {
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
238
239
  	switch (tspi->bytes_per_word) {
  	case 0:
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
240
  		return SLINK_PACK_SIZE_4;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
241
  	case 1:
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
242
  		return SLINK_PACK_SIZE_8;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
243
  	case 2:
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
244
  		return SLINK_PACK_SIZE_16;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
245
  	case 4:
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
246
  		return SLINK_PACK_SIZE_32;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
247
  	default:
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
248
  		return 0;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
249
  	}
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
250
251
252
253
254
255
256
257
  }
  
  static unsigned tegra_slink_calculate_curr_xfer_param(
  	struct spi_device *spi, struct tegra_slink_data *tspi,
  	struct spi_transfer *t)
  {
  	unsigned remain_len = t->len - tspi->cur_pos;
  	unsigned max_word;
3cb7b407c   Jingoo Han   spi: tegra20-slin...
258
  	unsigned bits_per_word;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
259
260
  	unsigned max_len;
  	unsigned total_fifo_words;
766ed7044   Laxman Dewangan   spi: remove check...
261
  	bits_per_word = t->bits_per_word;
e91d2352b   Axel Lin   spi: tegra: Use D...
262
  	tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
263
264
  
  	if (bits_per_word == 8 || bits_per_word == 16) {
2172a3327   Gustavo A. R. Silva   spi: tegra20-slin...
265
  		tspi->is_packed = true;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
266
267
  		tspi->words_per_32bit = 32/bits_per_word;
  	} else {
2172a3327   Gustavo A. R. Silva   spi: tegra20-slin...
268
  		tspi->is_packed = false;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  		tspi->words_per_32bit = 1;
  	}
  	tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  
  	if (tspi->is_packed) {
  		max_len = min(remain_len, tspi->max_buf_size);
  		tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  		total_fifo_words = max_len/4;
  	} else {
  		max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  		max_word = min(max_word, tspi->max_buf_size/4);
  		tspi->curr_dma_words = max_word;
  		total_fifo_words = max_word;
  	}
  	return total_fifo_words;
  }
  
  static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  	struct tegra_slink_data *tspi, struct spi_transfer *t)
  {
  	unsigned nbytes;
  	unsigned tx_empty_count;
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
291
  	u32 fifo_status;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
292
293
  	unsigned max_n_32bit;
  	unsigned i, count;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
294
295
296
297
298
299
300
301
302
303
304
305
306
  	unsigned int written_words;
  	unsigned fifo_words_left;
  	u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  
  	fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  	tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  
  	if (tspi->is_packed) {
  		fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  		written_words = min(fifo_words_left, tspi->curr_dma_words);
  		nbytes = written_words * tspi->bytes_per_word;
  		max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  		for (count = 0; count < max_n_32bit; count++) {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
307
  			u32 x = 0;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
308
  			for (i = 0; (i < 4) && nbytes; i++, nbytes--)
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
309
  				x |= (u32)(*tx_buf++) << (i * 8);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
310
311
312
313
314
315
316
  			tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  		}
  	} else {
  		max_n_32bit = min(tspi->curr_dma_words,  tx_empty_count);
  		written_words = max_n_32bit;
  		nbytes = written_words * tspi->bytes_per_word;
  		for (count = 0; count < max_n_32bit; count++) {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
317
  			u32 x = 0;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
318
319
  			for (i = 0; nbytes && (i < tspi->bytes_per_word);
  							i++, nbytes--)
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
320
  				x |= (u32)(*tx_buf++) << (i * 8);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
321
322
323
324
325
326
327
328
329
330
331
  			tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  		}
  	}
  	tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  	return written_words;
  }
  
  static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  		struct tegra_slink_data *tspi, struct spi_transfer *t)
  {
  	unsigned rx_full_count;
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
332
  	u32 fifo_status;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
333
  	unsigned i, count;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
334
335
336
337
338
339
340
341
342
  	unsigned int read_words = 0;
  	unsigned len;
  	u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  
  	fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  	rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  	if (tspi->is_packed) {
  		len = tspi->curr_dma_words * tspi->bytes_per_word;
  		for (count = 0; count < rx_full_count; count++) {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
343
  			u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
344
345
346
347
348
349
  			for (i = 0; len && (i < 4); i++, len--)
  				*rx_buf++ = (x >> i*8) & 0xFF;
  		}
  		tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  		read_words += tspi->curr_dma_words;
  	} else {
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
350
  		for (count = 0; count < rx_full_count; count++) {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
351
  			u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
352
353
354
355
356
357
358
359
360
361
362
363
  			for (i = 0; (i < tspi->bytes_per_word); i++)
  				*rx_buf++ = (x >> (i*8)) & 0xFF;
  		}
  		tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  		read_words += rx_full_count;
  	}
  	return read_words;
  }
  
  static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  		struct tegra_slink_data *tspi, struct spi_transfer *t)
  {
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
364
365
366
367
368
  	/* Make the dma buffer to read by cpu */
  	dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  				tspi->dma_buf_size, DMA_TO_DEVICE);
  
  	if (tspi->is_packed) {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
369
  		unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
370
371
372
373
374
375
  		memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  	} else {
  		unsigned int i;
  		unsigned int count;
  		u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  		unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
376
377
  
  		for (count = 0; count < tspi->curr_dma_words; count++) {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
378
  			u32 x = 0;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
379
380
  			for (i = 0; consume && (i < tspi->bytes_per_word);
  							i++, consume--)
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
381
  				x |= (u32)(*tx_buf++) << (i * 8);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  			tspi->tx_dma_buf[count] = x;
  		}
  	}
  	tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  
  	/* Make the dma buffer to read by dma */
  	dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  				tspi->dma_buf_size, DMA_TO_DEVICE);
  }
  
  static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  		struct tegra_slink_data *tspi, struct spi_transfer *t)
  {
  	unsigned len;
  
  	/* Make the dma buffer to read by cpu */
  	dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  		tspi->dma_buf_size, DMA_FROM_DEVICE);
  
  	if (tspi->is_packed) {
  		len = tspi->curr_dma_words * tspi->bytes_per_word;
  		memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  	} else {
  		unsigned int i;
  		unsigned int count;
  		unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
408
  		u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
409

dc4dc3605   Laxman Dewangan   spi: tegra: add s...
410
  		for (count = 0; count < tspi->curr_dma_words; count++) {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
411
  			u32 x = tspi->rx_dma_buf[count] & rx_mask;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  			for (i = 0; (i < tspi->bytes_per_word); i++)
  				*rx_buf++ = (x >> (i*8)) & 0xFF;
  		}
  	}
  	tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  
  	/* Make the dma buffer to read by dma */
  	dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  		tspi->dma_buf_size, DMA_FROM_DEVICE);
  }
  
  static void tegra_slink_dma_complete(void *args)
  {
  	struct completion *dma_complete = args;
  
  	complete(dma_complete);
  }
  
  static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  {
16735d022   Wolfram Sang   tree-wide: use re...
432
  	reinit_completion(&tspi->tx_dma_complete);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
433
434
  	tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  				tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
72919f340   Mark Brown   spi: tegra: slink...
435
  				DMA_PREP_INTERRUPT |  DMA_CTRL_ACK);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
  	if (!tspi->tx_dma_desc) {
  		dev_err(tspi->dev, "Not able to get desc for Tx
  ");
  		return -EIO;
  	}
  
  	tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  	tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  
  	dmaengine_submit(tspi->tx_dma_desc);
  	dma_async_issue_pending(tspi->tx_dma_chan);
  	return 0;
  }
  
  static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  {
16735d022   Wolfram Sang   tree-wide: use re...
452
  	reinit_completion(&tspi->rx_dma_complete);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
453
454
  	tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  				tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
72919f340   Mark Brown   spi: tegra: slink...
455
  				DMA_PREP_INTERRUPT |  DMA_CTRL_ACK);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  	if (!tspi->rx_dma_desc) {
  		dev_err(tspi->dev, "Not able to get desc for Rx
  ");
  		return -EIO;
  	}
  
  	tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  	tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  
  	dmaengine_submit(tspi->rx_dma_desc);
  	dma_async_issue_pending(tspi->rx_dma_chan);
  	return 0;
  }
  
  static int tegra_slink_start_dma_based_transfer(
  		struct tegra_slink_data *tspi, struct spi_transfer *t)
  {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
473
  	u32 val;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
474
475
  	unsigned int len;
  	int ret = 0;
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
476
  	u32 status;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
477
478
479
480
  
  	/* Make sure that Rx and Tx fifo are empty */
  	status = tegra_slink_readl(tspi, SLINK_STATUS);
  	if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
481
482
483
  		dev_err(tspi->dev, "Rx/Tx fifo are not empty status 0x%08x
  ",
  			(unsigned)status);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  		return -EIO;
  	}
  
  	val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  	val |= tspi->packed_size;
  	if (tspi->is_packed)
  		len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  					4) * 4;
  	else
  		len = tspi->curr_dma_words * 4;
  
  	/* Set attention level based on length of transfer */
  	if (len & 0xF)
  		val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  	else if (((len) >> 4) & 0x1)
  		val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  	else
  		val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  
  	if (tspi->cur_direction & DATA_DIR_TX)
  		val |= SLINK_IE_TXC;
  
  	if (tspi->cur_direction & DATA_DIR_RX)
  		val |= SLINK_IE_RXC;
  
  	tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  	tspi->dma_control_reg = val;
  
  	if (tspi->cur_direction & DATA_DIR_TX) {
  		tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  		wmb();
  		ret = tegra_slink_start_tx_dma(tspi, len);
  		if (ret < 0) {
  			dev_err(tspi->dev,
  				"Starting tx dma failed, err %d
  ", ret);
  			return ret;
  		}
  
  		/* Wait for tx fifo to be fill before starting slink */
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
524
525
526
  		status = tegra_slink_readl(tspi, SLINK_STATUS);
  		while (!(status & SLINK_TX_FULL))
  			status = tegra_slink_readl(tspi, SLINK_STATUS);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
  	}
  
  	if (tspi->cur_direction & DATA_DIR_RX) {
  		/* Make the dma buffer to read by dma */
  		dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  				tspi->dma_buf_size, DMA_FROM_DEVICE);
  
  		ret = tegra_slink_start_rx_dma(tspi, len);
  		if (ret < 0) {
  			dev_err(tspi->dev,
  				"Starting rx dma failed, err %d
  ", ret);
  			if (tspi->cur_direction & DATA_DIR_TX)
  				dmaengine_terminate_all(tspi->tx_dma_chan);
  			return ret;
  		}
  	}
  	tspi->is_curr_dma_xfer = true;
  	if (tspi->is_packed) {
  		val |= SLINK_PACKED;
  		tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  		/* HW need small delay after settign Packed mode */
  		udelay(1);
  	}
  	tspi->dma_control_reg = val;
  
  	val |= SLINK_DMA_EN;
  	tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  	return ret;
  }
  
  static int tegra_slink_start_cpu_based_transfer(
  		struct tegra_slink_data *tspi, struct spi_transfer *t)
  {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
561
  	u32 val;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
  	unsigned cur_words;
  
  	val = tspi->packed_size;
  	if (tspi->cur_direction & DATA_DIR_TX)
  		val |= SLINK_IE_TXC;
  
  	if (tspi->cur_direction & DATA_DIR_RX)
  		val |= SLINK_IE_RXC;
  
  	tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  	tspi->dma_control_reg = val;
  
  	if (tspi->cur_direction & DATA_DIR_TX)
  		cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  	else
  		cur_words = tspi->curr_dma_words;
  	val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  	tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  	tspi->dma_control_reg = val;
  
  	tspi->is_curr_dma_xfer = false;
  	if (tspi->is_packed) {
  		val |= SLINK_PACKED;
  		tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  		udelay(1);
  		wmb();
  	}
  	tspi->dma_control_reg = val;
  	val |= SLINK_DMA_EN;
  	tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  	return 0;
  }
  
  static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  			bool dma_to_memory)
  {
  	struct dma_chan *dma_chan;
  	u32 *dma_buf;
  	dma_addr_t dma_phys;
  	int ret;
  	struct dma_slave_config dma_sconfig;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
603

912a7df47   Peter Ujfalusi   spi: tegra20-slin...
604
  	dma_chan = dma_request_chan(tspi->dev, dma_to_memory ? "rx" : "tx");
7708aff1e   Krzysztof Kozlowski   spi: tegra20: Sim...
605
606
607
608
  	if (IS_ERR(dma_chan))
  		return dev_err_probe(tspi->dev, PTR_ERR(dma_chan),
  				     "Dma channel is not available
  ");
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
609
610
611
612
613
614
615
616
617
  
  	dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  				&dma_phys, GFP_KERNEL);
  	if (!dma_buf) {
  		dev_err(tspi->dev, " Not able to allocate the dma buffer
  ");
  		dma_release_channel(dma_chan);
  		return -ENOMEM;
  	}
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
  	if (dma_to_memory) {
  		dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  		dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  		dma_sconfig.src_maxburst = 0;
  	} else {
  		dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  		dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  		dma_sconfig.dst_maxburst = 0;
  	}
  
  	ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  	if (ret)
  		goto scrub;
  	if (dma_to_memory) {
  		tspi->rx_dma_chan = dma_chan;
  		tspi->rx_dma_buf = dma_buf;
  		tspi->rx_dma_phys = dma_phys;
  	} else {
  		tspi->tx_dma_chan = dma_chan;
  		tspi->tx_dma_buf = dma_buf;
  		tspi->tx_dma_phys = dma_phys;
  	}
  	return 0;
  
  scrub:
  	dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  	dma_release_channel(dma_chan);
  	return ret;
  }
  
  static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  	bool dma_to_memory)
  {
  	u32 *dma_buf;
  	dma_addr_t dma_phys;
  	struct dma_chan *dma_chan;
  
  	if (dma_to_memory) {
  		dma_buf = tspi->rx_dma_buf;
  		dma_chan = tspi->rx_dma_chan;
  		dma_phys = tspi->rx_dma_phys;
  		tspi->rx_dma_chan = NULL;
  		tspi->rx_dma_buf = NULL;
  	} else {
  		dma_buf = tspi->tx_dma_buf;
  		dma_chan = tspi->tx_dma_chan;
  		dma_phys = tspi->tx_dma_phys;
  		tspi->tx_dma_buf = NULL;
  		tspi->tx_dma_chan = NULL;
  	}
  	if (!dma_chan)
  		return;
  
  	dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  	dma_release_channel(dma_chan);
  }
  
  static int tegra_slink_start_transfer_one(struct spi_device *spi,
f178e3dec   Mark Brown   spi/tegra20-slink...
676
  		struct spi_transfer *t)
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
677
678
679
680
681
682
  {
  	struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  	u32 speed;
  	u8 bits_per_word;
  	unsigned total_fifo_words;
  	int ret;
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
683
684
  	u32 command;
  	u32 command2;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
685

e6811d1d7   Laxman Dewangan   spi: make sure al...
686
  	bits_per_word = t->bits_per_word;
beb96c2ad   Laxman Dewangan   spi/tegra: remove...
687
  	speed = t->speed_hz;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
688
689
690
691
692
693
694
695
696
697
698
  	if (speed != tspi->cur_speed) {
  		clk_set_rate(tspi->clk, speed * 4);
  		tspi->cur_speed = speed;
  	}
  
  	tspi->cur_spi = spi;
  	tspi->cur_pos = 0;
  	tspi->cur_rx_pos = 0;
  	tspi->cur_tx_pos = 0;
  	tspi->curr_xfer = t;
  	total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
f178e3dec   Mark Brown   spi/tegra20-slink...
699
700
701
  	command = tspi->command_reg;
  	command &= ~SLINK_BIT_LENGTH(~0);
  	command |= SLINK_BIT_LENGTH(bits_per_word - 1);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
702

f178e3dec   Mark Brown   spi/tegra20-slink...
703
704
  	command2 = tspi->command2_reg;
  	command2 &= ~(SLINK_RXEN | SLINK_TXEN);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
705

dc4dc3605   Laxman Dewangan   spi: tegra: add s...
706
707
708
709
710
711
712
713
714
  	tspi->cur_direction = 0;
  	if (t->rx_buf) {
  		command2 |= SLINK_RXEN;
  		tspi->cur_direction |= DATA_DIR_RX;
  	}
  	if (t->tx_buf) {
  		command2 |= SLINK_TXEN;
  		tspi->cur_direction |= DATA_DIR_TX;
  	}
0e694df35   Randolph Maaßen   spi: tegra20-slin...
715
716
717
718
719
720
  
  	/*
  	 * Writing to the command2 register bevore the command register prevents
  	 * a spike in chip_select line 0. This selects the chip_select line
  	 * before changing the chip_select value.
  	 */
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
721
722
  	tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  	tspi->command2_reg = command2;
0e694df35   Randolph Maaßen   spi: tegra20-slin...
723
724
  	tegra_slink_writel(tspi, command, SLINK_COMMAND);
  	tspi->command_reg = command;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
725
726
727
728
729
730
731
732
733
  	if (total_fifo_words > SLINK_FIFO_DEPTH)
  		ret = tegra_slink_start_dma_based_transfer(tspi, t);
  	else
  		ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  	return ret;
  }
  
  static int tegra_slink_setup(struct spi_device *spi)
  {
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
734
  	static const u32 cs_pol_bit[MAX_CHIP_SELECT] = {
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
735
736
737
738
739
  			SLINK_CS_POLARITY,
  			SLINK_CS_POLARITY1,
  			SLINK_CS_POLARITY2,
  			SLINK_CS_POLARITY3,
  	};
5fd38677a   Michal Nazarewicz   spi: tegra20-slin...
740
741
742
743
  	struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  	u32 val;
  	unsigned long flags;
  	int ret;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
744
745
746
747
748
749
  	dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz
  ",
  		spi->bits_per_word,
  		spi->mode & SPI_CPOL ? "" : "~",
  		spi->mode & SPI_CPHA ? "" : "~",
  		spi->max_speed_hz);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
750
751
  	ret = pm_runtime_get_sync(tspi->dev);
  	if (ret < 0) {
0c26f7b29   Zhang Qilong   spi: tegra20-slin...
752
  		pm_runtime_put_noidle(tspi->dev);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
  		dev_err(tspi->dev, "pm runtime failed, e = %d
  ", ret);
  		return ret;
  	}
  
  	spin_lock_irqsave(&tspi->lock, flags);
  	val = tspi->def_command_reg;
  	if (spi->mode & SPI_CS_HIGH)
  		val |= cs_pol_bit[spi->chip_select];
  	else
  		val &= ~cs_pol_bit[spi->chip_select];
  	tspi->def_command_reg = val;
  	tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  	spin_unlock_irqrestore(&tspi->lock, flags);
  
  	pm_runtime_put(tspi->dev);
  	return 0;
  }
63fc184cd   Mark Brown   spi/tegra20-slink...
771
772
  static int tegra_slink_prepare_message(struct spi_master *master,
  				       struct spi_message *msg)
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
773
  {
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
774
  	struct tegra_slink_data *tspi = spi_master_get_devdata(master);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
775
  	struct spi_device *spi = msg->spi;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
776

f178e3dec   Mark Brown   spi/tegra20-slink...
777
  	tegra_slink_clear_status(tspi);
d558c4733   Laxman Dewangan   spi: slink-tegra2...
778

f178e3dec   Mark Brown   spi/tegra20-slink...
779
780
  	tspi->command_reg = tspi->def_command_reg;
  	tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
781

f178e3dec   Mark Brown   spi/tegra20-slink...
782
783
784
785
786
787
788
789
790
791
792
  	tspi->command2_reg = tspi->def_command2_reg;
  	tspi->command2_reg |= SLINK_SS_EN_CS(spi->chip_select);
  
  	tspi->command_reg &= ~SLINK_MODES;
  	if (spi->mode & SPI_CPHA)
  		tspi->command_reg |= SLINK_CK_SDA;
  
  	if (spi->mode & SPI_CPOL)
  		tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  	else
  		tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
d558c4733   Laxman Dewangan   spi: slink-tegra2...
793

63fc184cd   Mark Brown   spi/tegra20-slink...
794
795
  	return 0;
  }
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
796

63fc184cd   Mark Brown   spi/tegra20-slink...
797
798
799
800
801
802
  static int tegra_slink_transfer_one(struct spi_master *master,
  				    struct spi_device *spi,
  				    struct spi_transfer *xfer)
  {
  	struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  	int ret;
16735d022   Wolfram Sang   tree-wide: use re...
803
  	reinit_completion(&tspi->xfer_completion);
f178e3dec   Mark Brown   spi/tegra20-slink...
804
  	ret = tegra_slink_start_transfer_one(spi, xfer);
63fc184cd   Mark Brown   spi/tegra20-slink...
805
806
807
808
809
  	if (ret < 0) {
  		dev_err(tspi->dev,
  			"spi can not start transfer, err %d
  ", ret);
  		return ret;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
810
  	}
f178e3dec   Mark Brown   spi/tegra20-slink...
811

63fc184cd   Mark Brown   spi/tegra20-slink...
812
813
814
815
  	ret = wait_for_completion_timeout(&tspi->xfer_completion,
  					  SLINK_DMA_TIMEOUT);
  	if (WARN_ON(ret == 0)) {
  		dev_err(tspi->dev,
bfca76185   Colin Ian King   spi: tegra: fix s...
816
817
  			"spi transfer timeout, err %d
  ", ret);
63fc184cd   Mark Brown   spi/tegra20-slink...
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
  		return -EIO;
  	}
  
  	if (tspi->tx_status)
  		return tspi->tx_status;
  	if (tspi->rx_status)
  		return tspi->rx_status;
  
  	return 0;
  }
  
  static int tegra_slink_unprepare_message(struct spi_master *master,
  					 struct spi_message *msg)
  {
  	struct tegra_slink_data *tspi = spi_master_get_devdata(master);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
833
834
  	tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  	tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
63fc184cd   Mark Brown   spi/tegra20-slink...
835
836
  
  	return 0;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
  }
  
  static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  {
  	struct spi_transfer *t = tspi->curr_xfer;
  	unsigned long flags;
  
  	spin_lock_irqsave(&tspi->lock, flags);
  	if (tspi->tx_status ||  tspi->rx_status ||
  				(tspi->status_reg & SLINK_BSY)) {
  		dev_err(tspi->dev,
  			"CpuXfer ERROR bit set 0x%x
  ", tspi->status_reg);
  		dev_err(tspi->dev,
  			"CpuXfer 0x%08x:0x%08x:0x%08x
  ", tspi->command_reg,
  				tspi->command2_reg, tspi->dma_control_reg);
ff2251e3d   Stephen Warren   spi: tegra: use r...
854
  		reset_control_assert(tspi->rst);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
855
  		udelay(2);
ff2251e3d   Stephen Warren   spi: tegra: use r...
856
  		reset_control_deassert(tspi->rst);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
  		complete(&tspi->xfer_completion);
  		goto exit;
  	}
  
  	if (tspi->cur_direction & DATA_DIR_RX)
  		tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  
  	if (tspi->cur_direction & DATA_DIR_TX)
  		tspi->cur_pos = tspi->cur_tx_pos;
  	else
  		tspi->cur_pos = tspi->cur_rx_pos;
  
  	if (tspi->cur_pos == t->len) {
  		complete(&tspi->xfer_completion);
  		goto exit;
  	}
  
  	tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  	tegra_slink_start_cpu_based_transfer(tspi, t);
  exit:
  	spin_unlock_irqrestore(&tspi->lock, flags);
  	return IRQ_HANDLED;
  }
  
  static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  {
  	struct spi_transfer *t = tspi->curr_xfer;
  	long wait_status;
  	int err = 0;
  	unsigned total_fifo_words;
  	unsigned long flags;
  
  	/* Abort dmas if any error */
  	if (tspi->cur_direction & DATA_DIR_TX) {
  		if (tspi->tx_status) {
  			dmaengine_terminate_all(tspi->tx_dma_chan);
  			err += 1;
  		} else {
  			wait_status = wait_for_completion_interruptible_timeout(
  				&tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  			if (wait_status <= 0) {
  				dmaengine_terminate_all(tspi->tx_dma_chan);
  				dev_err(tspi->dev, "TxDma Xfer failed
  ");
  				err += 1;
  			}
  		}
  	}
  
  	if (tspi->cur_direction & DATA_DIR_RX) {
  		if (tspi->rx_status) {
  			dmaengine_terminate_all(tspi->rx_dma_chan);
  			err += 2;
  		} else {
  			wait_status = wait_for_completion_interruptible_timeout(
  				&tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  			if (wait_status <= 0) {
  				dmaengine_terminate_all(tspi->rx_dma_chan);
  				dev_err(tspi->dev, "RxDma Xfer failed
  ");
  				err += 2;
  			}
  		}
  	}
  
  	spin_lock_irqsave(&tspi->lock, flags);
  	if (err) {
  		dev_err(tspi->dev,
  			"DmaXfer: ERROR bit set 0x%x
  ", tspi->status_reg);
  		dev_err(tspi->dev,
  			"DmaXfer 0x%08x:0x%08x:0x%08x
  ", tspi->command_reg,
  				tspi->command2_reg, tspi->dma_control_reg);
ff2251e3d   Stephen Warren   spi: tegra: use r...
931
  		reset_control_assert(tspi->rst);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
932
  		udelay(2);
ff2251e3d   Stephen Warren   spi: tegra: use r...
933
  		reset_control_assert(tspi->rst);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
  		complete(&tspi->xfer_completion);
  		spin_unlock_irqrestore(&tspi->lock, flags);
  		return IRQ_HANDLED;
  	}
  
  	if (tspi->cur_direction & DATA_DIR_RX)
  		tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  
  	if (tspi->cur_direction & DATA_DIR_TX)
  		tspi->cur_pos = tspi->cur_tx_pos;
  	else
  		tspi->cur_pos = tspi->cur_rx_pos;
  
  	if (tspi->cur_pos == t->len) {
  		complete(&tspi->xfer_completion);
  		goto exit;
  	}
  
  	/* Continue transfer in current message */
  	total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  							tspi, t);
  	if (total_fifo_words > SLINK_FIFO_DEPTH)
  		err = tegra_slink_start_dma_based_transfer(tspi, t);
  	else
  		err = tegra_slink_start_cpu_based_transfer(tspi, t);
  
  exit:
  	spin_unlock_irqrestore(&tspi->lock, flags);
  	return IRQ_HANDLED;
  }
  
  static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  {
  	struct tegra_slink_data *tspi = context_data;
  
  	if (!tspi->is_curr_dma_xfer)
  		return handle_cpu_based_xfer(tspi);
  	return handle_dma_based_xfer(tspi);
  }
  
  static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  {
  	struct tegra_slink_data *tspi = context_data;
  
  	tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  	if (tspi->cur_direction & DATA_DIR_TX)
  		tspi->tx_status = tspi->status_reg &
  					(SLINK_TX_OVF | SLINK_TX_UNF);
  
  	if (tspi->cur_direction & DATA_DIR_RX)
  		tspi->rx_status = tspi->status_reg &
  					(SLINK_RX_OVF | SLINK_RX_UNF);
  	tegra_slink_clear_status(tspi);
  
  	return IRQ_WAKE_THREAD;
  }
8b0bebe2c   Wei Yongjun   spi: tegra: slink...
990
  static const struct tegra_slink_chip_data tegra30_spi_cdata = {
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
991
992
  	.cs_hold_time = true,
  };
8b0bebe2c   Wei Yongjun   spi: tegra: slink...
993
  static const struct tegra_slink_chip_data tegra20_spi_cdata = {
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
994
995
  	.cs_hold_time = false,
  };
b2fb1872d   Jingoo Han   spi: tegra20-slin...
996
  static const struct of_device_id tegra_slink_of_match[] = {
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
997
  	{ .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
24bc89716   Laxman Dewangan   spi: tegra: seque...
998
  	{ .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
999
1000
1001
  	{}
  };
  MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
fd4a319bc   Grant Likely   spi: Remove HOTPL...
1002
  static int tegra_slink_probe(struct platform_device *pdev)
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1003
1004
1005
1006
  {
  	struct spi_master	*master;
  	struct tegra_slink_data	*tspi;
  	struct resource		*r;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1007
1008
1009
  	int ret, spi_irq;
  	const struct tegra_slink_chip_data *cdata = NULL;
  	const struct of_device_id *match;
c60fea021   Stephen Warren   spi/tegra-slink: ...
1010
  	match = of_match_device(tegra_slink_of_match, &pdev->dev);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1011
1012
1013
1014
1015
1016
  	if (!match) {
  		dev_err(&pdev->dev, "Error: No device match found
  ");
  		return -ENODEV;
  	}
  	cdata = match->data;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
  
  	master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  	if (!master) {
  		dev_err(&pdev->dev, "master allocation failed
  ");
  		return -ENOMEM;
  	}
  
  	/* the spi->mode bits understood by this driver: */
  	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  	master->setup = tegra_slink_setup;
63fc184cd   Mark Brown   spi/tegra20-slink...
1028
1029
1030
  	master->prepare_message = tegra_slink_prepare_message;
  	master->transfer_one = tegra_slink_transfer_one;
  	master->unprepare_message = tegra_slink_unprepare_message;
ce74ac80d   Mark Brown   spi/tegra20-slink...
1031
  	master->auto_runtime_pm = true;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1032
  	master->num_chipselect = MAX_CHIP_SELECT;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1033

24b5a82cf   Jingoo Han   spi: use platform...
1034
  	platform_set_drvdata(pdev, master);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1035
1036
  	tspi = spi_master_get_devdata(master);
  	tspi->master = master;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1037
1038
1039
  	tspi->dev = &pdev->dev;
  	tspi->chip_data = cdata;
  	spin_lock_init(&tspi->lock);
3c604de49   Axel Lin   spi: tegra20-slin...
1040
1041
1042
  	if (of_property_read_u32(tspi->dev->of_node, "spi-max-frequency",
  				 &master->max_speed_hz))
  		master->max_speed_hz = 25000000; /* 25MHz */
c60fea021   Stephen Warren   spi/tegra-slink: ...
1043

dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1044
1045
1046
1047
1048
1049
1050
1051
  	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (!r) {
  		dev_err(&pdev->dev, "No IO memory resource
  ");
  		ret = -ENODEV;
  		goto exit_free_master;
  	}
  	tspi->phys = r->start;
b0ee56052   Thierry Reding   spi: Convert to d...
1052
1053
1054
  	tspi->base = devm_ioremap_resource(&pdev->dev, r);
  	if (IS_ERR(tspi->base)) {
  		ret = PTR_ERR(tspi->base);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1055
1056
  		goto exit_free_master;
  	}
7001cab1d   Marcel Ziswiler   spi: tegra20-slin...
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
  	/* disabled clock may cause interrupt storm upon request */
  	tspi->clk = devm_clk_get(&pdev->dev, NULL);
  	if (IS_ERR(tspi->clk)) {
  		ret = PTR_ERR(tspi->clk);
  		dev_err(&pdev->dev, "Can not get clock %d
  ", ret);
  		goto exit_free_master;
  	}
  	ret = clk_prepare(tspi->clk);
  	if (ret < 0) {
  		dev_err(&pdev->dev, "Clock prepare failed %d
  ", ret);
  		goto exit_free_master;
  	}
  	ret = clk_enable(tspi->clk);
  	if (ret < 0) {
  		dev_err(&pdev->dev, "Clock enable failed %d
  ", ret);
04358e40b   Chuhong Yuan   spi: tegra20-slin...
1075
  		goto exit_clk_unprepare;
7001cab1d   Marcel Ziswiler   spi: tegra20-slin...
1076
  	}
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1077
1078
1079
1080
1081
1082
1083
1084
1085
  	spi_irq = platform_get_irq(pdev, 0);
  	tspi->irq = spi_irq;
  	ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  			tegra_slink_isr_thread, IRQF_ONESHOT,
  			dev_name(&pdev->dev), tspi);
  	if (ret < 0) {
  		dev_err(&pdev->dev, "Failed to register ISR for IRQ %d
  ",
  					tspi->irq);
7001cab1d   Marcel Ziswiler   spi: tegra20-slin...
1086
  		goto exit_clk_disable;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1087
  	}
73b32756c   Philipp Zabel   spi: tegra20-slin...
1088
  	tspi->rst = devm_reset_control_get_exclusive(&pdev->dev, "spi");
ff2251e3d   Stephen Warren   spi: tegra: use r...
1089
1090
1091
1092
1093
1094
  	if (IS_ERR(tspi->rst)) {
  		dev_err(&pdev->dev, "can not get reset
  ");
  		ret = PTR_ERR(tspi->rst);
  		goto exit_free_irq;
  	}
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1095
1096
  	tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  	tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1097

a915d150f   Stephen Warren   spi: tegra: conve...
1098
1099
1100
1101
1102
1103
1104
1105
1106
  	ret = tegra_slink_init_dma_param(tspi, true);
  	if (ret < 0)
  		goto exit_free_irq;
  	ret = tegra_slink_init_dma_param(tspi, false);
  	if (ret < 0)
  		goto exit_rx_dma_free;
  	tspi->max_buf_size = tspi->dma_buf_size;
  	init_completion(&tspi->tx_dma_complete);
  	init_completion(&tspi->rx_dma_complete);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
  
  	init_completion(&tspi->xfer_completion);
  
  	pm_runtime_enable(&pdev->dev);
  	if (!pm_runtime_enabled(&pdev->dev)) {
  		ret = tegra_slink_runtime_resume(&pdev->dev);
  		if (ret)
  			goto exit_pm_disable;
  	}
  
  	ret = pm_runtime_get_sync(&pdev->dev);
  	if (ret < 0) {
  		dev_err(&pdev->dev, "pm runtime get failed, e = %d
  ", ret);
faedcc17a   Dinghao Liu   spi: tegra20-slin...
1121
  		pm_runtime_put_noidle(&pdev->dev);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1122
1123
1124
1125
1126
1127
1128
1129
1130
  		goto exit_pm_disable;
  	}
  	tspi->def_command_reg  = SLINK_M_S;
  	tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  	tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  	tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  	pm_runtime_put(&pdev->dev);
  
  	master->dev.of_node = pdev->dev.of_node;
716db5d64   Jingoo Han   spi: tegra20-slin...
1131
  	ret = devm_spi_register_master(&pdev->dev, master);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
  	if (ret < 0) {
  		dev_err(&pdev->dev, "can not register to master err %d
  ", ret);
  		goto exit_pm_disable;
  	}
  	return ret;
  
  exit_pm_disable:
  	pm_runtime_disable(&pdev->dev);
  	if (!pm_runtime_status_suspended(&pdev->dev))
  		tegra_slink_runtime_suspend(&pdev->dev);
  	tegra_slink_deinit_dma_param(tspi, false);
  exit_rx_dma_free:
  	tegra_slink_deinit_dma_param(tspi, true);
  exit_free_irq:
  	free_irq(spi_irq, tspi);
7001cab1d   Marcel Ziswiler   spi: tegra20-slin...
1148
1149
  exit_clk_disable:
  	clk_disable(tspi->clk);
04358e40b   Chuhong Yuan   spi: tegra20-slin...
1150
1151
  exit_clk_unprepare:
  	clk_unprepare(tspi->clk);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1152
1153
1154
1155
  exit_free_master:
  	spi_master_put(master);
  	return ret;
  }
fd4a319bc   Grant Likely   spi: Remove HOTPL...
1156
  static int tegra_slink_remove(struct platform_device *pdev)
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1157
  {
24b5a82cf   Jingoo Han   spi: use platform...
1158
  	struct spi_master *master = platform_get_drvdata(pdev);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1159
1160
1161
  	struct tegra_slink_data	*tspi = spi_master_get_devdata(master);
  
  	free_irq(tspi->irq, tspi);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1162

7001cab1d   Marcel Ziswiler   spi: tegra20-slin...
1163
  	clk_disable(tspi->clk);
04358e40b   Chuhong Yuan   spi: tegra20-slin...
1164
  	clk_unprepare(tspi->clk);
7001cab1d   Marcel Ziswiler   spi: tegra20-slin...
1165

dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  	if (tspi->tx_dma_chan)
  		tegra_slink_deinit_dma_param(tspi, false);
  
  	if (tspi->rx_dma_chan)
  		tegra_slink_deinit_dma_param(tspi, true);
  
  	pm_runtime_disable(&pdev->dev);
  	if (!pm_runtime_status_suspended(&pdev->dev))
  		tegra_slink_runtime_suspend(&pdev->dev);
  
  	return 0;
  }
  
  #ifdef CONFIG_PM_SLEEP
  static int tegra_slink_suspend(struct device *dev)
  {
  	struct spi_master *master = dev_get_drvdata(dev);
  
  	return spi_master_suspend(master);
  }
  
  static int tegra_slink_resume(struct device *dev)
  {
  	struct spi_master *master = dev_get_drvdata(dev);
  	struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  	int ret;
  
  	ret = pm_runtime_get_sync(dev);
  	if (ret < 0) {
0c26f7b29   Zhang Qilong   spi: tegra20-slin...
1195
  		pm_runtime_put_noidle(dev);
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
  		dev_err(dev, "pm runtime failed, e = %d
  ", ret);
  		return ret;
  	}
  	tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  	tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  	pm_runtime_put(dev);
  
  	return spi_master_resume(master);
  }
  #endif
  
  static int tegra_slink_runtime_suspend(struct device *dev)
  {
  	struct spi_master *master = dev_get_drvdata(dev);
  	struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  
  	/* Flush all write which are in PPSB queue by reading back */
  	tegra_slink_readl(tspi, SLINK_MAS_DATA);
  
  	clk_disable_unprepare(tspi->clk);
  	return 0;
  }
  
  static int tegra_slink_runtime_resume(struct device *dev)
  {
  	struct spi_master *master = dev_get_drvdata(dev);
  	struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  	int ret;
  
  	ret = clk_prepare_enable(tspi->clk);
  	if (ret < 0) {
  		dev_err(tspi->dev, "clk_prepare failed: %d
  ", ret);
  		return ret;
  	}
  	return 0;
  }
  
  static const struct dev_pm_ops slink_pm_ops = {
  	SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  		tegra_slink_runtime_resume, NULL)
  	SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  };
  static struct platform_driver tegra_slink_driver = {
  	.driver = {
  		.name		= "spi-tegra-slink",
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1243
  		.pm		= &slink_pm_ops,
c60fea021   Stephen Warren   spi/tegra-slink: ...
1244
  		.of_match_table	= tegra_slink_of_match,
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1245
1246
  	},
  	.probe =	tegra_slink_probe,
fd4a319bc   Grant Likely   spi: Remove HOTPL...
1247
  	.remove =	tegra_slink_remove,
dc4dc3605   Laxman Dewangan   spi: tegra: add s...
1248
1249
1250
1251
1252
1253
1254
  };
  module_platform_driver(tegra_slink_driver);
  
  MODULE_ALIAS("platform:spi-tegra-slink");
  MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  MODULE_LICENSE("GPL v2");