blob: f62e6e5e90e36a238248b3df80aa957fda3938d4 [file] [log] [blame]
Laxman Dewanganf333a332013-02-22 18:07:39 +05301/*
2 * SPI driver for NVIDIA's Tegra114 SPI Controller.
3 *
4 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/clk.h>
Laxman Dewanganf333a332013-02-22 18:07:39 +053020#include <linux/completion.h>
21#include <linux/delay.h>
22#include <linux/dmaengine.h>
23#include <linux/dma-mapping.h>
24#include <linux/dmapool.h>
25#include <linux/err.h>
26#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kernel.h>
30#include <linux/kthread.h>
31#include <linux/module.h>
32#include <linux/platform_device.h>
33#include <linux/pm_runtime.h>
34#include <linux/of.h>
35#include <linux/of_device.h>
Stephen Warrenff2251e2013-11-06 16:31:24 -070036#include <linux/reset.h>
Laxman Dewanganf333a332013-02-22 18:07:39 +053037#include <linux/spi/spi.h>
38
39#define SPI_COMMAND1 0x000
40#define SPI_BIT_LENGTH(x) (((x) & 0x1f) << 0)
41#define SPI_PACKED (1 << 5)
42#define SPI_TX_EN (1 << 11)
43#define SPI_RX_EN (1 << 12)
44#define SPI_BOTH_EN_BYTE (1 << 13)
45#define SPI_BOTH_EN_BIT (1 << 14)
46#define SPI_LSBYTE_FE (1 << 15)
47#define SPI_LSBIT_FE (1 << 16)
48#define SPI_BIDIROE (1 << 17)
49#define SPI_IDLE_SDA_DRIVE_LOW (0 << 18)
50#define SPI_IDLE_SDA_DRIVE_HIGH (1 << 18)
51#define SPI_IDLE_SDA_PULL_LOW (2 << 18)
52#define SPI_IDLE_SDA_PULL_HIGH (3 << 18)
53#define SPI_IDLE_SDA_MASK (3 << 18)
54#define SPI_CS_SS_VAL (1 << 20)
55#define SPI_CS_SW_HW (1 << 21)
56/* SPI_CS_POL_INACTIVE bits are default high */
57#define SPI_CS_POL_INACTIVE 22
58#define SPI_CS_POL_INACTIVE_0 (1 << 22)
59#define SPI_CS_POL_INACTIVE_1 (1 << 23)
60#define SPI_CS_POL_INACTIVE_2 (1 << 24)
61#define SPI_CS_POL_INACTIVE_3 (1 << 25)
62#define SPI_CS_POL_INACTIVE_MASK (0xF << 22)
63
64#define SPI_CS_SEL_0 (0 << 26)
65#define SPI_CS_SEL_1 (1 << 26)
66#define SPI_CS_SEL_2 (2 << 26)
67#define SPI_CS_SEL_3 (3 << 26)
68#define SPI_CS_SEL_MASK (3 << 26)
69#define SPI_CS_SEL(x) (((x) & 0x3) << 26)
70#define SPI_CONTROL_MODE_0 (0 << 28)
71#define SPI_CONTROL_MODE_1 (1 << 28)
72#define SPI_CONTROL_MODE_2 (2 << 28)
73#define SPI_CONTROL_MODE_3 (3 << 28)
74#define SPI_CONTROL_MODE_MASK (3 << 28)
75#define SPI_MODE_SEL(x) (((x) & 0x3) << 28)
76#define SPI_M_S (1 << 30)
77#define SPI_PIO (1 << 31)
78
79#define SPI_COMMAND2 0x004
80#define SPI_TX_TAP_DELAY(x) (((x) & 0x3F) << 6)
81#define SPI_RX_TAP_DELAY(x) (((x) & 0x3F) << 0)
82
83#define SPI_CS_TIMING1 0x008
84#define SPI_SETUP_HOLD(setup, hold) (((setup) << 4) | (hold))
85#define SPI_CS_SETUP_HOLD(reg, cs, val) \
86 ((((val) & 0xFFu) << ((cs) * 8)) | \
87 ((reg) & ~(0xFFu << ((cs) * 8))))
88
89#define SPI_CS_TIMING2 0x00C
90#define CYCLES_BETWEEN_PACKETS_0(x) (((x) & 0x1F) << 0)
91#define CS_ACTIVE_BETWEEN_PACKETS_0 (1 << 5)
92#define CYCLES_BETWEEN_PACKETS_1(x) (((x) & 0x1F) << 8)
93#define CS_ACTIVE_BETWEEN_PACKETS_1 (1 << 13)
94#define CYCLES_BETWEEN_PACKETS_2(x) (((x) & 0x1F) << 16)
95#define CS_ACTIVE_BETWEEN_PACKETS_2 (1 << 21)
96#define CYCLES_BETWEEN_PACKETS_3(x) (((x) & 0x1F) << 24)
97#define CS_ACTIVE_BETWEEN_PACKETS_3 (1 << 29)
98#define SPI_SET_CS_ACTIVE_BETWEEN_PACKETS(reg, cs, val) \
99 (reg = (((val) & 0x1) << ((cs) * 8 + 5)) | \
100 ((reg) & ~(1 << ((cs) * 8 + 5))))
101#define SPI_SET_CYCLES_BETWEEN_PACKETS(reg, cs, val) \
102 (reg = (((val) & 0xF) << ((cs) * 8)) | \
103 ((reg) & ~(0xF << ((cs) * 8))))
104
105#define SPI_TRANS_STATUS 0x010
106#define SPI_BLK_CNT(val) (((val) >> 0) & 0xFFFF)
107#define SPI_SLV_IDLE_COUNT(val) (((val) >> 16) & 0xFF)
108#define SPI_RDY (1 << 30)
109
110#define SPI_FIFO_STATUS 0x014
111#define SPI_RX_FIFO_EMPTY (1 << 0)
112#define SPI_RX_FIFO_FULL (1 << 1)
113#define SPI_TX_FIFO_EMPTY (1 << 2)
114#define SPI_TX_FIFO_FULL (1 << 3)
115#define SPI_RX_FIFO_UNF (1 << 4)
116#define SPI_RX_FIFO_OVF (1 << 5)
117#define SPI_TX_FIFO_UNF (1 << 6)
118#define SPI_TX_FIFO_OVF (1 << 7)
119#define SPI_ERR (1 << 8)
120#define SPI_TX_FIFO_FLUSH (1 << 14)
121#define SPI_RX_FIFO_FLUSH (1 << 15)
122#define SPI_TX_FIFO_EMPTY_COUNT(val) (((val) >> 16) & 0x7F)
123#define SPI_RX_FIFO_FULL_COUNT(val) (((val) >> 23) & 0x7F)
124#define SPI_FRAME_END (1 << 30)
125#define SPI_CS_INACTIVE (1 << 31)
126
127#define SPI_FIFO_ERROR (SPI_RX_FIFO_UNF | \
128 SPI_RX_FIFO_OVF | SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF)
129#define SPI_FIFO_EMPTY (SPI_RX_FIFO_EMPTY | SPI_TX_FIFO_EMPTY)
130
131#define SPI_TX_DATA 0x018
132#define SPI_RX_DATA 0x01C
133
134#define SPI_DMA_CTL 0x020
135#define SPI_TX_TRIG_1 (0 << 15)
136#define SPI_TX_TRIG_4 (1 << 15)
137#define SPI_TX_TRIG_8 (2 << 15)
138#define SPI_TX_TRIG_16 (3 << 15)
139#define SPI_TX_TRIG_MASK (3 << 15)
140#define SPI_RX_TRIG_1 (0 << 19)
141#define SPI_RX_TRIG_4 (1 << 19)
142#define SPI_RX_TRIG_8 (2 << 19)
143#define SPI_RX_TRIG_16 (3 << 19)
144#define SPI_RX_TRIG_MASK (3 << 19)
145#define SPI_IE_TX (1 << 28)
146#define SPI_IE_RX (1 << 29)
147#define SPI_CONT (1 << 30)
148#define SPI_DMA (1 << 31)
149#define SPI_DMA_EN SPI_DMA
150
151#define SPI_DMA_BLK 0x024
152#define SPI_DMA_BLK_SET(x) (((x) & 0xFFFF) << 0)
153
154#define SPI_TX_FIFO 0x108
155#define SPI_RX_FIFO 0x188
156#define MAX_CHIP_SELECT 4
157#define SPI_FIFO_DEPTH 64
158#define DATA_DIR_TX (1 << 0)
159#define DATA_DIR_RX (1 << 1)
160
161#define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
162#define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
163#define TX_FIFO_EMPTY_COUNT_MAX SPI_TX_FIFO_EMPTY_COUNT(0x40)
164#define RX_FIFO_FULL_COUNT_ZERO SPI_RX_FIFO_FULL_COUNT(0)
165#define MAX_HOLD_CYCLES 16
166#define SPI_DEFAULT_SPEED 25000000
167
168#define MAX_CHIP_SELECT 4
169#define SPI_FIFO_DEPTH 64
170
171struct tegra_spi_data {
172 struct device *dev;
173 struct spi_master *master;
174 spinlock_t lock;
175
176 struct clk *clk;
Stephen Warrenff2251e2013-11-06 16:31:24 -0700177 struct reset_control *rst;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530178 void __iomem *base;
179 phys_addr_t phys;
180 unsigned irq;
181 int dma_req_sel;
182 u32 spi_max_frequency;
183 u32 cur_speed;
184
185 struct spi_device *cur_spi;
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400186 struct spi_device *cs_control;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530187 unsigned cur_pos;
188 unsigned cur_len;
189 unsigned words_per_32bit;
190 unsigned bytes_per_word;
191 unsigned curr_dma_words;
192 unsigned cur_direction;
193
194 unsigned cur_rx_pos;
195 unsigned cur_tx_pos;
196
197 unsigned dma_buf_size;
198 unsigned max_buf_size;
199 bool is_curr_dma_xfer;
200
201 struct completion rx_dma_complete;
202 struct completion tx_dma_complete;
203
204 u32 tx_status;
205 u32 rx_status;
206 u32 status_reg;
207 bool is_packed;
208 unsigned long packed_size;
209
210 u32 command1_reg;
211 u32 dma_control_reg;
212 u32 def_command1_reg;
213 u32 spi_cs_timing;
214
215 struct completion xfer_completion;
216 struct spi_transfer *curr_xfer;
217 struct dma_chan *rx_dma_chan;
218 u32 *rx_dma_buf;
219 dma_addr_t rx_dma_phys;
220 struct dma_async_tx_descriptor *rx_dma_desc;
221
222 struct dma_chan *tx_dma_chan;
223 u32 *tx_dma_buf;
224 dma_addr_t tx_dma_phys;
225 struct dma_async_tx_descriptor *tx_dma_desc;
226};
227
228static int tegra_spi_runtime_suspend(struct device *dev);
229static int tegra_spi_runtime_resume(struct device *dev);
230
231static inline unsigned long tegra_spi_readl(struct tegra_spi_data *tspi,
232 unsigned long reg)
233{
234 return readl(tspi->base + reg);
235}
236
237static inline void tegra_spi_writel(struct tegra_spi_data *tspi,
238 unsigned long val, unsigned long reg)
239{
240 writel(val, tspi->base + reg);
241
242 /* Read back register to make sure that register writes completed */
243 if (reg != SPI_TX_FIFO)
244 readl(tspi->base + SPI_COMMAND1);
245}
246
247static void tegra_spi_clear_status(struct tegra_spi_data *tspi)
248{
249 unsigned long val;
250
251 /* Write 1 to clear status register */
252 val = tegra_spi_readl(tspi, SPI_TRANS_STATUS);
253 tegra_spi_writel(tspi, val, SPI_TRANS_STATUS);
254
255 /* Clear fifo status error if any */
256 val = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
257 if (val & SPI_ERR)
258 tegra_spi_writel(tspi, SPI_ERR | SPI_FIFO_ERROR,
259 SPI_FIFO_STATUS);
260}
261
262static unsigned tegra_spi_calculate_curr_xfer_param(
263 struct spi_device *spi, struct tegra_spi_data *tspi,
264 struct spi_transfer *t)
265{
266 unsigned remain_len = t->len - tspi->cur_pos;
267 unsigned max_word;
268 unsigned bits_per_word = t->bits_per_word;
269 unsigned max_len;
270 unsigned total_fifo_words;
271
Axel Line91d2352013-08-30 11:00:23 +0800272 tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530273
274 if (bits_per_word == 8 || bits_per_word == 16) {
275 tspi->is_packed = 1;
276 tspi->words_per_32bit = 32/bits_per_word;
277 } else {
278 tspi->is_packed = 0;
279 tspi->words_per_32bit = 1;
280 }
281
282 if (tspi->is_packed) {
283 max_len = min(remain_len, tspi->max_buf_size);
284 tspi->curr_dma_words = max_len/tspi->bytes_per_word;
285 total_fifo_words = (max_len + 3) / 4;
286 } else {
287 max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
288 max_word = min(max_word, tspi->max_buf_size/4);
289 tspi->curr_dma_words = max_word;
290 total_fifo_words = max_word;
291 }
292 return total_fifo_words;
293}
294
295static unsigned tegra_spi_fill_tx_fifo_from_client_txbuf(
296 struct tegra_spi_data *tspi, struct spi_transfer *t)
297{
298 unsigned nbytes;
299 unsigned tx_empty_count;
300 unsigned long fifo_status;
301 unsigned max_n_32bit;
302 unsigned i, count;
303 unsigned long x;
304 unsigned int written_words;
305 unsigned fifo_words_left;
306 u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
307
308 fifo_status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
309 tx_empty_count = SPI_TX_FIFO_EMPTY_COUNT(fifo_status);
310
311 if (tspi->is_packed) {
312 fifo_words_left = tx_empty_count * tspi->words_per_32bit;
313 written_words = min(fifo_words_left, tspi->curr_dma_words);
314 nbytes = written_words * tspi->bytes_per_word;
315 max_n_32bit = DIV_ROUND_UP(nbytes, 4);
316 for (count = 0; count < max_n_32bit; count++) {
317 x = 0;
318 for (i = 0; (i < 4) && nbytes; i++, nbytes--)
319 x |= (*tx_buf++) << (i*8);
320 tegra_spi_writel(tspi, x, SPI_TX_FIFO);
321 }
322 } else {
323 max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
324 written_words = max_n_32bit;
325 nbytes = written_words * tspi->bytes_per_word;
326 for (count = 0; count < max_n_32bit; count++) {
327 x = 0;
328 for (i = 0; nbytes && (i < tspi->bytes_per_word);
329 i++, nbytes--)
330 x |= ((*tx_buf++) << i*8);
331 tegra_spi_writel(tspi, x, SPI_TX_FIFO);
332 }
333 }
334 tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
335 return written_words;
336}
337
338static unsigned int tegra_spi_read_rx_fifo_to_client_rxbuf(
339 struct tegra_spi_data *tspi, struct spi_transfer *t)
340{
341 unsigned rx_full_count;
342 unsigned long fifo_status;
343 unsigned i, count;
344 unsigned long x;
345 unsigned int read_words = 0;
346 unsigned len;
347 u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
348
349 fifo_status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
350 rx_full_count = SPI_RX_FIFO_FULL_COUNT(fifo_status);
351 if (tspi->is_packed) {
352 len = tspi->curr_dma_words * tspi->bytes_per_word;
353 for (count = 0; count < rx_full_count; count++) {
354 x = tegra_spi_readl(tspi, SPI_RX_FIFO);
355 for (i = 0; len && (i < 4); i++, len--)
356 *rx_buf++ = (x >> i*8) & 0xFF;
357 }
358 tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
359 read_words += tspi->curr_dma_words;
360 } else {
361 unsigned int rx_mask;
362 unsigned int bits_per_word = t->bits_per_word;
363
364 rx_mask = (1 << bits_per_word) - 1;
365 for (count = 0; count < rx_full_count; count++) {
366 x = tegra_spi_readl(tspi, SPI_RX_FIFO);
367 x &= rx_mask;
368 for (i = 0; (i < tspi->bytes_per_word); i++)
369 *rx_buf++ = (x >> (i*8)) & 0xFF;
370 }
371 tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
372 read_words += rx_full_count;
373 }
374 return read_words;
375}
376
377static void tegra_spi_copy_client_txbuf_to_spi_txbuf(
378 struct tegra_spi_data *tspi, struct spi_transfer *t)
379{
380 unsigned len;
381
382 /* Make the dma buffer to read by cpu */
383 dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
384 tspi->dma_buf_size, DMA_TO_DEVICE);
385
386 if (tspi->is_packed) {
387 len = tspi->curr_dma_words * tspi->bytes_per_word;
388 memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
389 } else {
390 unsigned int i;
391 unsigned int count;
392 u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
393 unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
394 unsigned int x;
395
396 for (count = 0; count < tspi->curr_dma_words; count++) {
397 x = 0;
398 for (i = 0; consume && (i < tspi->bytes_per_word);
399 i++, consume--)
400 x |= ((*tx_buf++) << i * 8);
401 tspi->tx_dma_buf[count] = x;
402 }
403 }
404 tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
405
406 /* Make the dma buffer to read by dma */
407 dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
408 tspi->dma_buf_size, DMA_TO_DEVICE);
409}
410
411static void tegra_spi_copy_spi_rxbuf_to_client_rxbuf(
412 struct tegra_spi_data *tspi, struct spi_transfer *t)
413{
414 unsigned len;
415
416 /* Make the dma buffer to read by cpu */
417 dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
418 tspi->dma_buf_size, DMA_FROM_DEVICE);
419
420 if (tspi->is_packed) {
421 len = tspi->curr_dma_words * tspi->bytes_per_word;
422 memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
423 } else {
424 unsigned int i;
425 unsigned int count;
426 unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
427 unsigned int x;
428 unsigned int rx_mask;
429 unsigned int bits_per_word = t->bits_per_word;
430
431 rx_mask = (1 << bits_per_word) - 1;
432 for (count = 0; count < tspi->curr_dma_words; count++) {
433 x = tspi->rx_dma_buf[count];
434 x &= rx_mask;
435 for (i = 0; (i < tspi->bytes_per_word); i++)
436 *rx_buf++ = (x >> (i*8)) & 0xFF;
437 }
438 }
439 tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
440
441 /* Make the dma buffer to read by dma */
442 dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
443 tspi->dma_buf_size, DMA_FROM_DEVICE);
444}
445
446static void tegra_spi_dma_complete(void *args)
447{
448 struct completion *dma_complete = args;
449
450 complete(dma_complete);
451}
452
453static int tegra_spi_start_tx_dma(struct tegra_spi_data *tspi, int len)
454{
Wolfram Sang16735d02013-11-14 14:32:02 -0800455 reinit_completion(&tspi->tx_dma_complete);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530456 tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
457 tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
458 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
459 if (!tspi->tx_dma_desc) {
460 dev_err(tspi->dev, "Not able to get desc for Tx\n");
461 return -EIO;
462 }
463
464 tspi->tx_dma_desc->callback = tegra_spi_dma_complete;
465 tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
466
467 dmaengine_submit(tspi->tx_dma_desc);
468 dma_async_issue_pending(tspi->tx_dma_chan);
469 return 0;
470}
471
472static int tegra_spi_start_rx_dma(struct tegra_spi_data *tspi, int len)
473{
Wolfram Sang16735d02013-11-14 14:32:02 -0800474 reinit_completion(&tspi->rx_dma_complete);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530475 tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
476 tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
477 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
478 if (!tspi->rx_dma_desc) {
479 dev_err(tspi->dev, "Not able to get desc for Rx\n");
480 return -EIO;
481 }
482
483 tspi->rx_dma_desc->callback = tegra_spi_dma_complete;
484 tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
485
486 dmaengine_submit(tspi->rx_dma_desc);
487 dma_async_issue_pending(tspi->rx_dma_chan);
488 return 0;
489}
490
491static int tegra_spi_start_dma_based_transfer(
492 struct tegra_spi_data *tspi, struct spi_transfer *t)
493{
494 unsigned long val;
495 unsigned int len;
496 int ret = 0;
497 unsigned long status;
498
499 /* Make sure that Rx and Tx fifo are empty */
500 status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
501 if ((status & SPI_FIFO_EMPTY) != SPI_FIFO_EMPTY) {
502 dev_err(tspi->dev,
503 "Rx/Tx fifo are not empty status 0x%08lx\n", status);
504 return -EIO;
505 }
506
507 val = SPI_DMA_BLK_SET(tspi->curr_dma_words - 1);
508 tegra_spi_writel(tspi, val, SPI_DMA_BLK);
509
510 if (tspi->is_packed)
511 len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
512 4) * 4;
513 else
514 len = tspi->curr_dma_words * 4;
515
516 /* Set attention level based on length of transfer */
517 if (len & 0xF)
518 val |= SPI_TX_TRIG_1 | SPI_RX_TRIG_1;
519 else if (((len) >> 4) & 0x1)
520 val |= SPI_TX_TRIG_4 | SPI_RX_TRIG_4;
521 else
522 val |= SPI_TX_TRIG_8 | SPI_RX_TRIG_8;
523
524 if (tspi->cur_direction & DATA_DIR_TX)
525 val |= SPI_IE_TX;
526
527 if (tspi->cur_direction & DATA_DIR_RX)
528 val |= SPI_IE_RX;
529
530 tegra_spi_writel(tspi, val, SPI_DMA_CTL);
531 tspi->dma_control_reg = val;
532
533 if (tspi->cur_direction & DATA_DIR_TX) {
534 tegra_spi_copy_client_txbuf_to_spi_txbuf(tspi, t);
535 ret = tegra_spi_start_tx_dma(tspi, len);
536 if (ret < 0) {
537 dev_err(tspi->dev,
538 "Starting tx dma failed, err %d\n", ret);
539 return ret;
540 }
541 }
542
543 if (tspi->cur_direction & DATA_DIR_RX) {
544 /* Make the dma buffer to read by dma */
545 dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
546 tspi->dma_buf_size, DMA_FROM_DEVICE);
547
548 ret = tegra_spi_start_rx_dma(tspi, len);
549 if (ret < 0) {
550 dev_err(tspi->dev,
551 "Starting rx dma failed, err %d\n", ret);
552 if (tspi->cur_direction & DATA_DIR_TX)
553 dmaengine_terminate_all(tspi->tx_dma_chan);
554 return ret;
555 }
556 }
557 tspi->is_curr_dma_xfer = true;
558 tspi->dma_control_reg = val;
559
560 val |= SPI_DMA_EN;
561 tegra_spi_writel(tspi, val, SPI_DMA_CTL);
562 return ret;
563}
564
565static int tegra_spi_start_cpu_based_transfer(
566 struct tegra_spi_data *tspi, struct spi_transfer *t)
567{
568 unsigned long val;
569 unsigned cur_words;
570
571 if (tspi->cur_direction & DATA_DIR_TX)
572 cur_words = tegra_spi_fill_tx_fifo_from_client_txbuf(tspi, t);
573 else
574 cur_words = tspi->curr_dma_words;
575
576 val = SPI_DMA_BLK_SET(cur_words - 1);
577 tegra_spi_writel(tspi, val, SPI_DMA_BLK);
578
579 val = 0;
580 if (tspi->cur_direction & DATA_DIR_TX)
581 val |= SPI_IE_TX;
582
583 if (tspi->cur_direction & DATA_DIR_RX)
584 val |= SPI_IE_RX;
585
586 tegra_spi_writel(tspi, val, SPI_DMA_CTL);
587 tspi->dma_control_reg = val;
588
589 tspi->is_curr_dma_xfer = false;
590
591 val |= SPI_DMA_EN;
592 tegra_spi_writel(tspi, val, SPI_DMA_CTL);
593 return 0;
594}
595
596static int tegra_spi_init_dma_param(struct tegra_spi_data *tspi,
597 bool dma_to_memory)
598{
599 struct dma_chan *dma_chan;
600 u32 *dma_buf;
601 dma_addr_t dma_phys;
602 int ret;
603 struct dma_slave_config dma_sconfig;
604 dma_cap_mask_t mask;
605
606 dma_cap_zero(mask);
607 dma_cap_set(DMA_SLAVE, mask);
608 dma_chan = dma_request_channel(mask, NULL, NULL);
609 if (!dma_chan) {
610 dev_err(tspi->dev,
611 "Dma channel is not available, will try later\n");
612 return -EPROBE_DEFER;
613 }
614
615 dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
616 &dma_phys, GFP_KERNEL);
617 if (!dma_buf) {
618 dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
619 dma_release_channel(dma_chan);
620 return -ENOMEM;
621 }
622
623 dma_sconfig.slave_id = tspi->dma_req_sel;
624 if (dma_to_memory) {
625 dma_sconfig.src_addr = tspi->phys + SPI_RX_FIFO;
626 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
627 dma_sconfig.src_maxburst = 0;
628 } else {
629 dma_sconfig.dst_addr = tspi->phys + SPI_TX_FIFO;
630 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
631 dma_sconfig.dst_maxburst = 0;
632 }
633
634 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
635 if (ret)
636 goto scrub;
637 if (dma_to_memory) {
638 tspi->rx_dma_chan = dma_chan;
639 tspi->rx_dma_buf = dma_buf;
640 tspi->rx_dma_phys = dma_phys;
641 } else {
642 tspi->tx_dma_chan = dma_chan;
643 tspi->tx_dma_buf = dma_buf;
644 tspi->tx_dma_phys = dma_phys;
645 }
646 return 0;
647
648scrub:
649 dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
650 dma_release_channel(dma_chan);
651 return ret;
652}
653
654static void tegra_spi_deinit_dma_param(struct tegra_spi_data *tspi,
655 bool dma_to_memory)
656{
657 u32 *dma_buf;
658 dma_addr_t dma_phys;
659 struct dma_chan *dma_chan;
660
661 if (dma_to_memory) {
662 dma_buf = tspi->rx_dma_buf;
663 dma_chan = tspi->rx_dma_chan;
664 dma_phys = tspi->rx_dma_phys;
665 tspi->rx_dma_chan = NULL;
666 tspi->rx_dma_buf = NULL;
667 } else {
668 dma_buf = tspi->tx_dma_buf;
669 dma_chan = tspi->tx_dma_chan;
670 dma_phys = tspi->tx_dma_phys;
671 tspi->tx_dma_buf = NULL;
672 tspi->tx_dma_chan = NULL;
673 }
674 if (!dma_chan)
675 return;
676
677 dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
678 dma_release_channel(dma_chan);
679}
680
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400681static unsigned long tegra_spi_setup_transfer_one(struct spi_device *spi,
682 struct spi_transfer *t, bool is_first_of_msg)
Laxman Dewanganf333a332013-02-22 18:07:39 +0530683{
684 struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
685 u32 speed = t->speed_hz;
686 u8 bits_per_word = t->bits_per_word;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530687 unsigned long command1;
688 int req_mode;
689
690 if (speed != tspi->cur_speed) {
691 clk_set_rate(tspi->clk, speed);
692 tspi->cur_speed = speed;
693 }
694
695 tspi->cur_spi = spi;
696 tspi->cur_pos = 0;
697 tspi->cur_rx_pos = 0;
698 tspi->cur_tx_pos = 0;
699 tspi->curr_xfer = t;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530700
701 if (is_first_of_msg) {
702 tegra_spi_clear_status(tspi);
703
704 command1 = tspi->def_command1_reg;
705 command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
706
707 command1 &= ~SPI_CONTROL_MODE_MASK;
708 req_mode = spi->mode & 0x3;
709 if (req_mode == SPI_MODE_0)
710 command1 |= SPI_CONTROL_MODE_0;
711 else if (req_mode == SPI_MODE_1)
712 command1 |= SPI_CONTROL_MODE_1;
713 else if (req_mode == SPI_MODE_2)
714 command1 |= SPI_CONTROL_MODE_2;
715 else if (req_mode == SPI_MODE_3)
716 command1 |= SPI_CONTROL_MODE_3;
717
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400718 if (tspi->cs_control) {
719 if (tspi->cs_control != spi)
720 tegra_spi_writel(tspi, command1, SPI_COMMAND1);
721 tspi->cs_control = NULL;
722 } else
723 tegra_spi_writel(tspi, command1, SPI_COMMAND1);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530724
725 command1 |= SPI_CS_SW_HW;
726 if (spi->mode & SPI_CS_HIGH)
727 command1 |= SPI_CS_SS_VAL;
728 else
729 command1 &= ~SPI_CS_SS_VAL;
730
731 tegra_spi_writel(tspi, 0, SPI_COMMAND2);
732 } else {
733 command1 = tspi->command1_reg;
734 command1 &= ~SPI_BIT_LENGTH(~0);
735 command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
736 }
737
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400738 return command1;
739}
740
741static int tegra_spi_start_transfer_one(struct spi_device *spi,
742 struct spi_transfer *t, unsigned long command1)
743{
744 struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
745 unsigned total_fifo_words;
746 int ret;
747
748 total_fifo_words = tegra_spi_calculate_curr_xfer_param(spi, tspi, t);
749
Laxman Dewanganf333a332013-02-22 18:07:39 +0530750 if (tspi->is_packed)
751 command1 |= SPI_PACKED;
752
753 command1 &= ~(SPI_CS_SEL_MASK | SPI_TX_EN | SPI_RX_EN);
754 tspi->cur_direction = 0;
755 if (t->rx_buf) {
756 command1 |= SPI_RX_EN;
757 tspi->cur_direction |= DATA_DIR_RX;
758 }
759 if (t->tx_buf) {
760 command1 |= SPI_TX_EN;
761 tspi->cur_direction |= DATA_DIR_TX;
762 }
763 command1 |= SPI_CS_SEL(spi->chip_select);
764 tegra_spi_writel(tspi, command1, SPI_COMMAND1);
765 tspi->command1_reg = command1;
766
767 dev_dbg(tspi->dev, "The def 0x%x and written 0x%lx\n",
768 tspi->def_command1_reg, command1);
769
770 if (total_fifo_words > SPI_FIFO_DEPTH)
771 ret = tegra_spi_start_dma_based_transfer(tspi, t);
772 else
773 ret = tegra_spi_start_cpu_based_transfer(tspi, t);
774 return ret;
775}
776
777static int tegra_spi_setup(struct spi_device *spi)
778{
779 struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
780 unsigned long val;
781 unsigned long flags;
782 int ret;
783 unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
784 SPI_CS_POL_INACTIVE_0,
785 SPI_CS_POL_INACTIVE_1,
786 SPI_CS_POL_INACTIVE_2,
787 SPI_CS_POL_INACTIVE_3,
788 };
789
790 dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
791 spi->bits_per_word,
792 spi->mode & SPI_CPOL ? "" : "~",
793 spi->mode & SPI_CPHA ? "" : "~",
794 spi->max_speed_hz);
795
796 BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
797
798 /* Set speed to the spi max fequency if spi device has not set */
799 spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
800
801 ret = pm_runtime_get_sync(tspi->dev);
802 if (ret < 0) {
803 dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
804 return ret;
805 }
806
807 spin_lock_irqsave(&tspi->lock, flags);
808 val = tspi->def_command1_reg;
809 if (spi->mode & SPI_CS_HIGH)
810 val &= ~cs_pol_bit[spi->chip_select];
811 else
812 val |= cs_pol_bit[spi->chip_select];
813 tspi->def_command1_reg = val;
814 tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
815 spin_unlock_irqrestore(&tspi->lock, flags);
816
817 pm_runtime_put(tspi->dev);
818 return 0;
819}
820
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400821static void tegra_spi_transfer_delay(int delay)
822{
823 if (!delay)
824 return;
825
826 if (delay >= 1000)
827 mdelay(delay / 1000);
828
829 udelay(delay % 1000);
830}
831
Laxman Dewanganf333a332013-02-22 18:07:39 +0530832static int tegra_spi_transfer_one_message(struct spi_master *master,
833 struct spi_message *msg)
834{
835 bool is_first_msg = true;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530836 struct tegra_spi_data *tspi = spi_master_get_devdata(master);
837 struct spi_transfer *xfer;
838 struct spi_device *spi = msg->spi;
839 int ret;
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400840 bool skip = false;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530841
842 msg->status = 0;
843 msg->actual_length = 0;
844
Laxman Dewanganf333a332013-02-22 18:07:39 +0530845 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400846 unsigned long cmd1;
847
Wolfram Sang16735d02013-11-14 14:32:02 -0800848 reinit_completion(&tspi->xfer_completion);
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400849
850 cmd1 = tegra_spi_setup_transfer_one(spi, xfer, is_first_msg);
851
852 if (!xfer->len) {
853 ret = 0;
854 skip = true;
855 goto complete_xfer;
856 }
857
858 ret = tegra_spi_start_transfer_one(spi, xfer, cmd1);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530859 if (ret < 0) {
860 dev_err(tspi->dev,
861 "spi can not start transfer, err %d\n", ret);
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400862 goto complete_xfer;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530863 }
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400864
Laxman Dewanganf333a332013-02-22 18:07:39 +0530865 is_first_msg = false;
866 ret = wait_for_completion_timeout(&tspi->xfer_completion,
867 SPI_DMA_TIMEOUT);
868 if (WARN_ON(ret == 0)) {
869 dev_err(tspi->dev,
870 "spi trasfer timeout, err %d\n", ret);
871 ret = -EIO;
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400872 goto complete_xfer;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530873 }
874
875 if (tspi->tx_status || tspi->rx_status) {
876 dev_err(tspi->dev, "Error in Transfer\n");
877 ret = -EIO;
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400878 goto complete_xfer;
Laxman Dewanganf333a332013-02-22 18:07:39 +0530879 }
880 msg->actual_length += xfer->len;
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400881
882complete_xfer:
883 if (ret < 0 || skip) {
Laxman Dewanganf333a332013-02-22 18:07:39 +0530884 tegra_spi_writel(tspi, tspi->def_command1_reg,
885 SPI_COMMAND1);
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400886 tegra_spi_transfer_delay(xfer->delay_usecs);
887 goto exit;
888 } else if (msg->transfers.prev == &xfer->transfer_list) {
889 /* This is the last transfer in message */
890 if (xfer->cs_change)
891 tspi->cs_control = spi;
892 else {
893 tegra_spi_writel(tspi, tspi->def_command1_reg,
894 SPI_COMMAND1);
895 tegra_spi_transfer_delay(xfer->delay_usecs);
896 }
897 } else if (xfer->cs_change) {
898 tegra_spi_writel(tspi, tspi->def_command1_reg,
899 SPI_COMMAND1);
900 tegra_spi_transfer_delay(xfer->delay_usecs);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530901 }
Rhyland Kleinf4fade12013-09-26 13:01:43 -0400902
Laxman Dewanganf333a332013-02-22 18:07:39 +0530903 }
904 ret = 0;
905exit:
Laxman Dewanganf333a332013-02-22 18:07:39 +0530906 msg->status = ret;
907 spi_finalize_current_message(master);
908 return ret;
909}
910
911static irqreturn_t handle_cpu_based_xfer(struct tegra_spi_data *tspi)
912{
913 struct spi_transfer *t = tspi->curr_xfer;
914 unsigned long flags;
915
916 spin_lock_irqsave(&tspi->lock, flags);
917 if (tspi->tx_status || tspi->rx_status) {
918 dev_err(tspi->dev, "CpuXfer ERROR bit set 0x%x\n",
919 tspi->status_reg);
920 dev_err(tspi->dev, "CpuXfer 0x%08x:0x%08x\n",
921 tspi->command1_reg, tspi->dma_control_reg);
Stephen Warrenff2251e2013-11-06 16:31:24 -0700922 reset_control_assert(tspi->rst);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530923 udelay(2);
Stephen Warrenff2251e2013-11-06 16:31:24 -0700924 reset_control_deassert(tspi->rst);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530925 complete(&tspi->xfer_completion);
926 goto exit;
927 }
928
929 if (tspi->cur_direction & DATA_DIR_RX)
930 tegra_spi_read_rx_fifo_to_client_rxbuf(tspi, t);
931
932 if (tspi->cur_direction & DATA_DIR_TX)
933 tspi->cur_pos = tspi->cur_tx_pos;
934 else
935 tspi->cur_pos = tspi->cur_rx_pos;
936
937 if (tspi->cur_pos == t->len) {
938 complete(&tspi->xfer_completion);
939 goto exit;
940 }
941
942 tegra_spi_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
943 tegra_spi_start_cpu_based_transfer(tspi, t);
944exit:
945 spin_unlock_irqrestore(&tspi->lock, flags);
946 return IRQ_HANDLED;
947}
948
949static irqreturn_t handle_dma_based_xfer(struct tegra_spi_data *tspi)
950{
951 struct spi_transfer *t = tspi->curr_xfer;
952 long wait_status;
953 int err = 0;
954 unsigned total_fifo_words;
955 unsigned long flags;
956
957 /* Abort dmas if any error */
958 if (tspi->cur_direction & DATA_DIR_TX) {
959 if (tspi->tx_status) {
960 dmaengine_terminate_all(tspi->tx_dma_chan);
961 err += 1;
962 } else {
963 wait_status = wait_for_completion_interruptible_timeout(
964 &tspi->tx_dma_complete, SPI_DMA_TIMEOUT);
965 if (wait_status <= 0) {
966 dmaengine_terminate_all(tspi->tx_dma_chan);
967 dev_err(tspi->dev, "TxDma Xfer failed\n");
968 err += 1;
969 }
970 }
971 }
972
973 if (tspi->cur_direction & DATA_DIR_RX) {
974 if (tspi->rx_status) {
975 dmaengine_terminate_all(tspi->rx_dma_chan);
976 err += 2;
977 } else {
978 wait_status = wait_for_completion_interruptible_timeout(
979 &tspi->rx_dma_complete, SPI_DMA_TIMEOUT);
980 if (wait_status <= 0) {
981 dmaengine_terminate_all(tspi->rx_dma_chan);
982 dev_err(tspi->dev, "RxDma Xfer failed\n");
983 err += 2;
984 }
985 }
986 }
987
988 spin_lock_irqsave(&tspi->lock, flags);
989 if (err) {
990 dev_err(tspi->dev, "DmaXfer: ERROR bit set 0x%x\n",
991 tspi->status_reg);
992 dev_err(tspi->dev, "DmaXfer 0x%08x:0x%08x\n",
993 tspi->command1_reg, tspi->dma_control_reg);
Stephen Warrenff2251e2013-11-06 16:31:24 -0700994 reset_control_assert(tspi->rst);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530995 udelay(2);
Stephen Warrenff2251e2013-11-06 16:31:24 -0700996 reset_control_deassert(tspi->rst);
Laxman Dewanganf333a332013-02-22 18:07:39 +0530997 complete(&tspi->xfer_completion);
998 spin_unlock_irqrestore(&tspi->lock, flags);
999 return IRQ_HANDLED;
1000 }
1001
1002 if (tspi->cur_direction & DATA_DIR_RX)
1003 tegra_spi_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
1004
1005 if (tspi->cur_direction & DATA_DIR_TX)
1006 tspi->cur_pos = tspi->cur_tx_pos;
1007 else
1008 tspi->cur_pos = tspi->cur_rx_pos;
1009
1010 if (tspi->cur_pos == t->len) {
1011 complete(&tspi->xfer_completion);
1012 goto exit;
1013 }
1014
1015 /* Continue transfer in current message */
1016 total_fifo_words = tegra_spi_calculate_curr_xfer_param(tspi->cur_spi,
1017 tspi, t);
1018 if (total_fifo_words > SPI_FIFO_DEPTH)
1019 err = tegra_spi_start_dma_based_transfer(tspi, t);
1020 else
1021 err = tegra_spi_start_cpu_based_transfer(tspi, t);
1022
1023exit:
1024 spin_unlock_irqrestore(&tspi->lock, flags);
1025 return IRQ_HANDLED;
1026}
1027
1028static irqreturn_t tegra_spi_isr_thread(int irq, void *context_data)
1029{
1030 struct tegra_spi_data *tspi = context_data;
1031
1032 if (!tspi->is_curr_dma_xfer)
1033 return handle_cpu_based_xfer(tspi);
1034 return handle_dma_based_xfer(tspi);
1035}
1036
1037static irqreturn_t tegra_spi_isr(int irq, void *context_data)
1038{
1039 struct tegra_spi_data *tspi = context_data;
1040
1041 tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
1042 if (tspi->cur_direction & DATA_DIR_TX)
1043 tspi->tx_status = tspi->status_reg &
1044 (SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF);
1045
1046 if (tspi->cur_direction & DATA_DIR_RX)
1047 tspi->rx_status = tspi->status_reg &
1048 (SPI_RX_FIFO_OVF | SPI_RX_FIFO_UNF);
1049 tegra_spi_clear_status(tspi);
1050
1051 return IRQ_WAKE_THREAD;
1052}
1053
1054static void tegra_spi_parse_dt(struct platform_device *pdev,
1055 struct tegra_spi_data *tspi)
1056{
1057 struct device_node *np = pdev->dev.of_node;
1058 u32 of_dma[2];
1059
1060 if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
1061 of_dma, 2) >= 0)
1062 tspi->dma_req_sel = of_dma[1];
1063
1064 if (of_property_read_u32(np, "spi-max-frequency",
1065 &tspi->spi_max_frequency))
1066 tspi->spi_max_frequency = 25000000; /* 25MHz */
1067}
1068
1069static struct of_device_id tegra_spi_of_match[] = {
1070 { .compatible = "nvidia,tegra114-spi", },
1071 {}
1072};
1073MODULE_DEVICE_TABLE(of, tegra_spi_of_match);
1074
1075static int tegra_spi_probe(struct platform_device *pdev)
1076{
1077 struct spi_master *master;
1078 struct tegra_spi_data *tspi;
1079 struct resource *r;
1080 int ret, spi_irq;
1081
1082 master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
1083 if (!master) {
1084 dev_err(&pdev->dev, "master allocation failed\n");
1085 return -ENOMEM;
1086 }
Jingoo Han24b5a822013-05-23 19:20:40 +09001087 platform_set_drvdata(pdev, master);
Laxman Dewanganf333a332013-02-22 18:07:39 +05301088 tspi = spi_master_get_devdata(master);
1089
1090 /* Parse DT */
1091 tegra_spi_parse_dt(pdev, tspi);
1092
1093 /* the spi->mode bits understood by this driver: */
1094 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1095 master->setup = tegra_spi_setup;
1096 master->transfer_one_message = tegra_spi_transfer_one_message;
1097 master->num_chipselect = MAX_CHIP_SELECT;
1098 master->bus_num = -1;
Mark Brown612aa5c2013-07-28 15:37:31 +01001099 master->auto_runtime_pm = true;
Laxman Dewanganf333a332013-02-22 18:07:39 +05301100
1101 tspi->master = master;
1102 tspi->dev = &pdev->dev;
1103 spin_lock_init(&tspi->lock);
1104
1105 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Laxman Dewanganf333a332013-02-22 18:07:39 +05301106 tspi->base = devm_ioremap_resource(&pdev->dev, r);
1107 if (IS_ERR(tspi->base)) {
1108 ret = PTR_ERR(tspi->base);
Laxman Dewanganf333a332013-02-22 18:07:39 +05301109 goto exit_free_master;
1110 }
Laurent Navet5f7f54b2013-05-14 12:07:12 +02001111 tspi->phys = r->start;
Laxman Dewanganf333a332013-02-22 18:07:39 +05301112
1113 spi_irq = platform_get_irq(pdev, 0);
1114 tspi->irq = spi_irq;
1115 ret = request_threaded_irq(tspi->irq, tegra_spi_isr,
1116 tegra_spi_isr_thread, IRQF_ONESHOT,
1117 dev_name(&pdev->dev), tspi);
1118 if (ret < 0) {
1119 dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
1120 tspi->irq);
1121 goto exit_free_master;
1122 }
1123
1124 tspi->clk = devm_clk_get(&pdev->dev, "spi");
1125 if (IS_ERR(tspi->clk)) {
1126 dev_err(&pdev->dev, "can not get clock\n");
1127 ret = PTR_ERR(tspi->clk);
1128 goto exit_free_irq;
1129 }
1130
Stephen Warrenff2251e2013-11-06 16:31:24 -07001131 tspi->rst = devm_reset_control_get(&pdev->dev, "spi");
1132 if (IS_ERR(tspi->rst)) {
1133 dev_err(&pdev->dev, "can not get reset\n");
1134 ret = PTR_ERR(tspi->rst);
1135 goto exit_free_irq;
1136 }
1137
Laxman Dewanganf333a332013-02-22 18:07:39 +05301138 tspi->max_buf_size = SPI_FIFO_DEPTH << 2;
1139 tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
1140
1141 if (tspi->dma_req_sel) {
1142 ret = tegra_spi_init_dma_param(tspi, true);
1143 if (ret < 0) {
1144 dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
1145 goto exit_free_irq;
1146 }
1147
1148 ret = tegra_spi_init_dma_param(tspi, false);
1149 if (ret < 0) {
1150 dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
1151 goto exit_rx_dma_free;
1152 }
1153 tspi->max_buf_size = tspi->dma_buf_size;
1154 init_completion(&tspi->tx_dma_complete);
1155 init_completion(&tspi->rx_dma_complete);
1156 }
1157
1158 init_completion(&tspi->xfer_completion);
1159
1160 pm_runtime_enable(&pdev->dev);
1161 if (!pm_runtime_enabled(&pdev->dev)) {
1162 ret = tegra_spi_runtime_resume(&pdev->dev);
1163 if (ret)
1164 goto exit_pm_disable;
1165 }
1166
1167 ret = pm_runtime_get_sync(&pdev->dev);
1168 if (ret < 0) {
1169 dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
1170 goto exit_pm_disable;
1171 }
1172 tspi->def_command1_reg = SPI_M_S;
1173 tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
1174 pm_runtime_put(&pdev->dev);
1175
1176 master->dev.of_node = pdev->dev.of_node;
Jingoo Han5c809642013-09-24 13:49:24 +09001177 ret = devm_spi_register_master(&pdev->dev, master);
Laxman Dewanganf333a332013-02-22 18:07:39 +05301178 if (ret < 0) {
1179 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
1180 goto exit_pm_disable;
1181 }
1182 return ret;
1183
1184exit_pm_disable:
1185 pm_runtime_disable(&pdev->dev);
1186 if (!pm_runtime_status_suspended(&pdev->dev))
1187 tegra_spi_runtime_suspend(&pdev->dev);
1188 tegra_spi_deinit_dma_param(tspi, false);
1189exit_rx_dma_free:
1190 tegra_spi_deinit_dma_param(tspi, true);
1191exit_free_irq:
1192 free_irq(spi_irq, tspi);
1193exit_free_master:
1194 spi_master_put(master);
1195 return ret;
1196}
1197
1198static int tegra_spi_remove(struct platform_device *pdev)
1199{
Jingoo Han24b5a822013-05-23 19:20:40 +09001200 struct spi_master *master = platform_get_drvdata(pdev);
Laxman Dewanganf333a332013-02-22 18:07:39 +05301201 struct tegra_spi_data *tspi = spi_master_get_devdata(master);
1202
1203 free_irq(tspi->irq, tspi);
Laxman Dewanganf333a332013-02-22 18:07:39 +05301204
1205 if (tspi->tx_dma_chan)
1206 tegra_spi_deinit_dma_param(tspi, false);
1207
1208 if (tspi->rx_dma_chan)
1209 tegra_spi_deinit_dma_param(tspi, true);
1210
1211 pm_runtime_disable(&pdev->dev);
1212 if (!pm_runtime_status_suspended(&pdev->dev))
1213 tegra_spi_runtime_suspend(&pdev->dev);
1214
1215 return 0;
1216}
1217
1218#ifdef CONFIG_PM_SLEEP
1219static int tegra_spi_suspend(struct device *dev)
1220{
1221 struct spi_master *master = dev_get_drvdata(dev);
1222
1223 return spi_master_suspend(master);
1224}
1225
1226static int tegra_spi_resume(struct device *dev)
1227{
1228 struct spi_master *master = dev_get_drvdata(dev);
1229 struct tegra_spi_data *tspi = spi_master_get_devdata(master);
1230 int ret;
1231
1232 ret = pm_runtime_get_sync(dev);
1233 if (ret < 0) {
1234 dev_err(dev, "pm runtime failed, e = %d\n", ret);
1235 return ret;
1236 }
1237 tegra_spi_writel(tspi, tspi->command1_reg, SPI_COMMAND1);
1238 pm_runtime_put(dev);
1239
1240 return spi_master_resume(master);
1241}
1242#endif
1243
1244static int tegra_spi_runtime_suspend(struct device *dev)
1245{
1246 struct spi_master *master = dev_get_drvdata(dev);
1247 struct tegra_spi_data *tspi = spi_master_get_devdata(master);
1248
1249 /* Flush all write which are in PPSB queue by reading back */
1250 tegra_spi_readl(tspi, SPI_COMMAND1);
1251
1252 clk_disable_unprepare(tspi->clk);
1253 return 0;
1254}
1255
1256static int tegra_spi_runtime_resume(struct device *dev)
1257{
1258 struct spi_master *master = dev_get_drvdata(dev);
1259 struct tegra_spi_data *tspi = spi_master_get_devdata(master);
1260 int ret;
1261
1262 ret = clk_prepare_enable(tspi->clk);
1263 if (ret < 0) {
1264 dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
1265 return ret;
1266 }
1267 return 0;
1268}
1269
1270static const struct dev_pm_ops tegra_spi_pm_ops = {
1271 SET_RUNTIME_PM_OPS(tegra_spi_runtime_suspend,
1272 tegra_spi_runtime_resume, NULL)
1273 SET_SYSTEM_SLEEP_PM_OPS(tegra_spi_suspend, tegra_spi_resume)
1274};
1275static struct platform_driver tegra_spi_driver = {
1276 .driver = {
1277 .name = "spi-tegra114",
1278 .owner = THIS_MODULE,
1279 .pm = &tegra_spi_pm_ops,
1280 .of_match_table = tegra_spi_of_match,
1281 },
1282 .probe = tegra_spi_probe,
1283 .remove = tegra_spi_remove,
1284};
1285module_platform_driver(tegra_spi_driver);
1286
1287MODULE_ALIAS("platform:spi-tegra114");
1288MODULE_DESCRIPTION("NVIDIA Tegra114 SPI Controller Driver");
1289MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1290MODULE_LICENSE("GPL v2");