]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/spi/spi-tegra114.c
spi: tegra: fixed polling mode tranfer timeout
[sojka/nv-tegra/linux-3.10.git] / drivers / spi / spi-tegra114.c
1 /*
2  * SPI driver for NVIDIA's Tegra114 SPI Controller.
3  *
4  * Copyright (c) 2013-2016, 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>
20 #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/gpio.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/io.h>
30 #include <linux/kernel.h>
31 #include <linux/kthread.h>
32 #include <linux/module.h>
33 #include <linux/platform_device.h>
34 #include <linux/pinctrl/consumer.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/of.h>
37 #include <linux/of_device.h>
38 #include <linux/of_gpio.h>
39 #include <linux/spi/spi.h>
40 #include <linux/spi/spi-tegra.h>
41 #include <linux/clk/tegra.h>
42 #include <linux/tegra_prod.h>
43
44 #define SPI_COMMAND1                            0x000
45 #define SPI_BIT_LENGTH(x)                       (((x) & 0x1f) << 0)
46 #define SPI_PACKED                              (1 << 5)
47 #define SPI_TX_EN                               (1 << 11)
48 #define SPI_RX_EN                               (1 << 12)
49 #define SPI_BOTH_EN_BYTE                        (1 << 13)
50 #define SPI_BOTH_EN_BIT                         (1 << 14)
51 #define SPI_LSBYTE_FE                           (1 << 15)
52 #define SPI_LSBIT_FE                            (1 << 16)
53 #define SPI_BIDIROE                             (1 << 17)
54 #define SPI_IDLE_SDA_DRIVE_LOW                  (0 << 18)
55 #define SPI_IDLE_SDA_DRIVE_HIGH                 (1 << 18)
56 #define SPI_IDLE_SDA_PULL_LOW                   (2 << 18)
57 #define SPI_IDLE_SDA_PULL_HIGH                  (3 << 18)
58 #define SPI_IDLE_SDA_MASK                       (3 << 18)
59 #define SPI_CS_SS_VAL                           (1 << 20)
60 #define SPI_CS_SW_HW                            (1 << 21)
61 /* SPI_CS_POL_INACTIVE bits are default high */
62 #define SPI_CS_POL_INACTIVE                     22
63 #define SPI_CS_POL_INACTIVE_0                   (1 << 22)
64 #define SPI_CS_POL_INACTIVE_1                   (1 << 23)
65 #define SPI_CS_POL_INACTIVE_2                   (1 << 24)
66 #define SPI_CS_POL_INACTIVE_3                   (1 << 25)
67 #define SPI_CS_POL_INACTIVE_MASK                (0xF << 22)
68
69 #define SPI_CS_SEL_0                            (0 << 26)
70 #define SPI_CS_SEL_1                            (1 << 26)
71 #define SPI_CS_SEL_2                            (2 << 26)
72 #define SPI_CS_SEL_3                            (3 << 26)
73 #define SPI_CS_SEL_MASK                         (3 << 26)
74 #define SPI_CS_SEL(x)                           (((x) & 0x3) << 26)
75 #define SPI_CONTROL_MODE_0                      (0 << 28)
76 #define SPI_CONTROL_MODE_1                      (1 << 28)
77 #define SPI_CONTROL_MODE_2                      (2 << 28)
78 #define SPI_CONTROL_MODE_3                      (3 << 28)
79 #define SPI_CONTROL_MODE_MASK                   (3 << 28)
80 #define SPI_MODE_SEL(x)                         (((x) & 0x3) << 28)
81 #define SPI_MODE_VAL(x)                         (((x) >> 28) & 0x3)
82 #define SPI_M_S                                 (1 << 30)
83 #define SPI_PIO                                 (1 << 31)
84
85 #define SPI_COMMAND2                            0x004
86 #define SPI_TX_TAP_DELAY(x)                     (((x) & 0x3F) << 6)
87 #define SPI_RX_TAP_DELAY(x)                     (((x) & 0x3F) << 0)
88
89 #define SPI_CS_TIMING1                          0x008
90 #define SPI_SETUP_HOLD(setup, hold)             (((setup) << 4) | (hold))
91 #define SPI_CS_SETUP_HOLD(reg, cs, val)                 \
92                 ((((val) & 0xFFu) << ((cs) * 8)) |      \
93                 ((reg) & ~(0xFFu << ((cs) * 8))))
94
95 #define SPI_CS_TIMING2                          0x00C
96 #define CYCLES_BETWEEN_PACKETS_0(x)             (((x) & 0x1F) << 0)
97 #define CS_ACTIVE_BETWEEN_PACKETS_0             (1 << 5)
98 #define CYCLES_BETWEEN_PACKETS_1(x)             (((x) & 0x1F) << 8)
99 #define CS_ACTIVE_BETWEEN_PACKETS_1             (1 << 13)
100 #define CYCLES_BETWEEN_PACKETS_2(x)             (((x) & 0x1F) << 16)
101 #define CS_ACTIVE_BETWEEN_PACKETS_2             (1 << 21)
102 #define CYCLES_BETWEEN_PACKETS_3(x)             (((x) & 0x1F) << 24)
103 #define CS_ACTIVE_BETWEEN_PACKETS_3             (1 << 29)
104 #define SPI_SET_CS_ACTIVE_BETWEEN_PACKETS(reg, cs, val)         \
105                 (reg = (((val) & 0x1) << ((cs) * 8 + 5)) |      \
106                         ((reg) & ~(1 << ((cs) * 8 + 5))))
107 #define SPI_SET_CYCLES_BETWEEN_PACKETS(reg, cs, val)            \
108                 (reg = (((val) & 0x1F) << ((cs) * 8)) |         \
109                         ((reg) & ~(0x1F << ((cs) * 8))))
110
111 #define SPI_TRANS_STATUS                        0x010
112 #define SPI_BLK_CNT(val)                        (((val) >> 0) & 0xFFFF)
113 #define SPI_SLV_IDLE_COUNT(val)                 (((val) >> 16) & 0xFF)
114 #define SPI_RDY                                 (1 << 30)
115
116 #define SPI_FIFO_STATUS                         0x014
117 #define SPI_RX_FIFO_EMPTY                       (1 << 0)
118 #define SPI_RX_FIFO_FULL                        (1 << 1)
119 #define SPI_TX_FIFO_EMPTY                       (1 << 2)
120 #define SPI_TX_FIFO_FULL                        (1 << 3)
121 #define SPI_RX_FIFO_UNF                         (1 << 4)
122 #define SPI_RX_FIFO_OVF                         (1 << 5)
123 #define SPI_TX_FIFO_UNF                         (1 << 6)
124 #define SPI_TX_FIFO_OVF                         (1 << 7)
125 #define SPI_ERR                                 (1 << 8)
126 #define SPI_TX_FIFO_FLUSH                       (1 << 14)
127 #define SPI_RX_FIFO_FLUSH                       (1 << 15)
128 #define SPI_TX_FIFO_EMPTY_COUNT(val)            (((val) >> 16) & 0x7F)
129 #define SPI_RX_FIFO_FULL_COUNT(val)             (((val) >> 23) & 0x7F)
130 #define SPI_FRAME_END                           (1 << 30)
131 #define SPI_CS_INACTIVE                         (1 << 31)
132
133 #define SPI_FIFO_ERROR                          (SPI_RX_FIFO_UNF | \
134                         SPI_RX_FIFO_OVF | SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF)
135 #define SPI_FIFO_EMPTY                  (SPI_RX_FIFO_EMPTY | SPI_TX_FIFO_EMPTY)
136
137 #define SPI_TX_DATA                             0x018
138 #define SPI_RX_DATA                             0x01C
139
140 #define SPI_DMA_CTL                             0x020
141 #define SPI_TX_TRIG_1                           (0 << 15)
142 #define SPI_TX_TRIG_4                           (1 << 15)
143 #define SPI_TX_TRIG_8                           (2 << 15)
144 #define SPI_TX_TRIG_16                          (3 << 15)
145 #define SPI_TX_TRIG_MASK                        (3 << 15)
146 #define SPI_RX_TRIG_1                           (0 << 19)
147 #define SPI_RX_TRIG_4                           (1 << 19)
148 #define SPI_RX_TRIG_8                           (2 << 19)
149 #define SPI_RX_TRIG_16                          (3 << 19)
150 #define SPI_RX_TRIG_MASK                        (3 << 19)
151 #define SPI_IE_TX                               (1 << 28)
152 #define SPI_IE_RX                               (1 << 29)
153 #define SPI_CONT                                (1 << 30)
154 #define SPI_DMA                                 (1 << 31)
155 #define SPI_DMA_EN                              SPI_DMA
156
157 #define SPI_DMA_BLK                             0x024
158 #define SPI_DMA_BLK_SET(x)                      (((x) & 0xFFFF) << 0)
159
160 #define SPI_TX_FIFO                             0x108
161 #define SPI_RX_FIFO                             0x188
162
163 #define SPI_INTR_MASK           0x18c
164 #define SPI_INTR_RX_FIFO_UNF_MASK               (1 << 25)
165 #define SPI_INTR_RX_FIFO_OVF_MASK               (1 << 26)
166 #define SPI_INTR_TX_FIFO_UNF_MASK               (1 << 27)
167 #define SPI_INTR_TX_FIFO_OVF_MASK               (1 << 28)
168 #define SPI_INTR_RDY_MASK                       (1 << 29)
169 #define SPI_INTR_ALL_MASK                       (0xfe << 25)
170
171 #define MAX_CHIP_SELECT                         4
172 #define SPI_FIFO_DEPTH                          64
173 #define DATA_DIR_TX                             (1 << 0)
174 #define DATA_DIR_RX                             (1 << 1)
175
176 #define SPI_DMA_TIMEOUT                         (msecs_to_jiffies(10000))
177 #define DEFAULT_SPI_DMA_BUF_LEN                 (16*1024)
178 #define TX_FIFO_EMPTY_COUNT_MAX                 (0x40)
179 #define RX_FIFO_FULL_COUNT_ZERO                 (0)
180 #define MAX_HOLD_CYCLES                         16
181 #define SPI_DEFAULT_SPEED                       25000000
182
183 #define MAX_CHIP_SELECT                         4
184 #define SPI_FIFO_DEPTH                          64
185 #define SPI_FIFO_FLUSH_MAX_DELAY                2000
186
187 #define SPI_SPEED_TAP_DELAY_MARGIN 35000000
188 #define SPI_DEFAULT_RX_TAP_DELAY 10
189 #define SPI_POLL_TIMEOUT 10000
190 #define SPI_AUTOSUSPEND_DELAY 100       /* 100ms */
191
192 struct tegra_spi_chip_data {
193         bool intr_mask_reg;
194         bool set_rx_tap_delay;
195 };
196
197 struct tegra_spi_data {
198         struct device                           *dev;
199         struct spi_master                       *master;
200         spinlock_t                              lock;
201
202         struct clk                              *clk;
203         void __iomem                            *base;
204         phys_addr_t                             phys;
205         unsigned                                irq;
206         bool                                    clock_always_on;
207         bool                                    polling_mode;
208         bool                                    boost_reg_access;
209         bool                                    runtime_pm;
210         u32                                     spi_max_frequency;
211         u32                                     cur_speed;
212         unsigned                                min_div;
213
214         struct spi_device                       *cur_spi;
215         unsigned                                cur_pos;
216         unsigned                                cur_len;
217         unsigned                                words_per_32bit;
218         unsigned                                bytes_per_word;
219         unsigned                                curr_dma_words;
220         unsigned                                cur_direction;
221
222         unsigned                                cur_rx_pos;
223         unsigned                                cur_tx_pos;
224
225         unsigned                                dma_buf_size;
226         unsigned                                max_buf_size;
227         bool                                    is_curr_dma_xfer;
228         bool                                    is_hw_based_cs;
229         bool                                    transfer_in_progress;
230
231         struct completion                       rx_dma_complete;
232         struct completion                       tx_dma_complete;
233
234         u32                                     tx_status;
235         u32                                     rx_status;
236         u32                                     status_reg;
237         bool                                    is_packed;
238         unsigned long                           packed_size;
239
240         u32                                     command1_reg;
241         u32                                     dma_control_reg;
242         u32                                     def_command1_reg;
243         u32                                     def_command2_reg;
244         u32                                     spi_cs_timing;
245         u8                                      def_chip_select;
246
247         struct completion                       xfer_completion;
248         struct spi_transfer                     *curr_xfer;
249         struct dma_chan                         *rx_dma_chan;
250         u32                                     *rx_dma_buf;
251         dma_addr_t                              rx_dma_phys;
252         struct dma_async_tx_descriptor          *rx_dma_desc;
253
254         struct dma_chan                         *tx_dma_chan;
255         u32                                     *tx_dma_buf;
256         dma_addr_t                              tx_dma_phys;
257         struct dma_async_tx_descriptor          *tx_dma_desc;
258         const struct tegra_spi_chip_data  *chip_data;
259         struct tegra_spi_device_controller_data cdata[MAX_CHIP_SELECT];
260         bool                                    cs_gpio_reqstd[MAX_CHIP_SELECT];
261         struct tegra_prod_list *prod_list;
262         struct pinctrl *pinctrl;
263         struct pinctrl_state *enable_interface;
264 };
265
266
267 static int tegra_spi_runtime_suspend(struct device *dev);
268 static int tegra_spi_runtime_resume(struct device *dev);
269 static int tegra_spi_status_poll(struct tegra_spi_data *tspi);
270 static int tegra_spi_set_clock_rate(struct tegra_spi_data *tspi, u32 speed);
271
272
273 static inline unsigned long tegra_spi_readl(struct tegra_spi_data *tspi,
274                 unsigned long reg)
275 {
276         return readl(tspi->base + reg);
277 }
278
279 static inline void tegra_spi_writel(struct tegra_spi_data *tspi,
280                 unsigned long val, unsigned long reg)
281 {
282         /* Read back register to make sure that register writes completed */
283         if ((reg == SPI_COMMAND1) && (val & SPI_PIO))
284                 readl(tspi->base + SPI_COMMAND1);
285
286         writel(val, tspi->base + reg);
287 }
288
289 static inline int tegra_spi_runtime_get(struct tegra_spi_data *tspi)
290 {
291         if (!tspi->runtime_pm)
292                 return 0;
293
294         return pm_runtime_get_sync(tspi->dev);
295 }
296
297 static inline int tegra_spi_runtime_put(struct tegra_spi_data *tspi)
298 {
299         if (!tspi->runtime_pm)
300                 return 0;
301
302         pm_runtime_mark_last_busy(tspi->dev);
303         return pm_runtime_put_autosuspend(tspi->dev);
304 }
305
306 static void tegra_spi_clear_status(struct tegra_spi_data *tspi)
307 {
308         unsigned long val;
309
310         /* Write 1 to clear status register */
311         val = tegra_spi_readl(tspi, SPI_TRANS_STATUS);
312         if (val & SPI_RDY)
313                 tegra_spi_writel(tspi, val, SPI_TRANS_STATUS);
314
315         /* Clear fifo status error if any */
316         tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
317         if (tspi->status_reg & SPI_ERR)
318                 tegra_spi_writel(tspi, SPI_ERR | SPI_FIFO_ERROR,
319                                 SPI_FIFO_STATUS);
320 }
321
322 static unsigned tegra_spi_calculate_curr_xfer_param(
323         struct spi_device *spi, struct tegra_spi_data *tspi,
324         struct spi_transfer *t)
325 {
326         unsigned remain_len = t->len - tspi->cur_pos;
327         unsigned max_word;
328         unsigned bits_per_word ;
329         unsigned max_len;
330         unsigned total_fifo_words;
331
332         bits_per_word = t->bits_per_word ? t->bits_per_word :
333                                                 spi->bits_per_word;
334         tspi->bytes_per_word = (bits_per_word - 1) / 8 + 1;
335
336         if ((bits_per_word == 8 || bits_per_word == 16) && (t->len > 3)) {
337                 tspi->is_packed = 1;
338                 tspi->words_per_32bit = 32/bits_per_word;
339         } else {
340                 tspi->is_packed = 0;
341                 tspi->words_per_32bit = 1;
342         }
343
344         if (tspi->is_packed) {
345                 max_len = min(remain_len, tspi->max_buf_size);
346                 tspi->curr_dma_words = max_len/tspi->bytes_per_word;
347                 total_fifo_words = (max_len + 3)/4;
348         } else {
349                 max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
350                 max_word = min(max_word, tspi->max_buf_size/4);
351                 tspi->curr_dma_words = max_word;
352                 total_fifo_words = max_word;
353         }
354         return total_fifo_words;
355 }
356
357 static unsigned tegra_spi_fill_tx_fifo_from_client_txbuf(
358         struct tegra_spi_data *tspi, struct spi_transfer *t)
359 {
360         unsigned nbytes;
361         unsigned tx_empty_count;
362         unsigned max_n_32bit;
363         unsigned i, count;
364         unsigned long x;
365         unsigned int written_words;
366         unsigned fifo_words_left;
367         u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
368
369         tx_empty_count = TX_FIFO_EMPTY_COUNT_MAX;
370
371         if (tspi->is_packed) {
372                 fifo_words_left = tx_empty_count * tspi->words_per_32bit;
373                 written_words = min(fifo_words_left, tspi->curr_dma_words);
374                 nbytes = written_words * tspi->bytes_per_word;
375                 max_n_32bit = DIV_ROUND_UP(nbytes, 4);
376                 for (count = 0; count < max_n_32bit; count++) {
377                         x = 0;
378                         for (i = 0; (i < 4) && nbytes; i++, nbytes--)
379                                 x |= (*tx_buf++) << (i*8);
380                         tegra_spi_writel(tspi, x, SPI_TX_FIFO);
381                 }
382         } else {
383                 max_n_32bit = min(tspi->curr_dma_words,  tx_empty_count);
384                 written_words = max_n_32bit;
385                 nbytes = written_words * tspi->bytes_per_word;
386                 for (count = 0; count < max_n_32bit; count++) {
387                         x = 0;
388                         for (i = 0; nbytes && (i < tspi->bytes_per_word);
389                                                         i++, nbytes--)
390                                 x |= ((*tx_buf++) << i*8);
391                         tegra_spi_writel(tspi, x, SPI_TX_FIFO);
392                 }
393         }
394         tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
395         return written_words;
396 }
397
398 static unsigned int tegra_spi_read_rx_fifo_to_client_rxbuf(
399                 struct tegra_spi_data *tspi, struct spi_transfer *t)
400 {
401         unsigned rx_full_count;
402         unsigned long fifo_status;
403         unsigned i, count;
404         unsigned long x;
405         unsigned int read_words = 0;
406         unsigned len;
407         u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
408
409         fifo_status = tspi->status_reg;
410         rx_full_count = SPI_RX_FIFO_FULL_COUNT(fifo_status);
411         if (tspi->is_packed) {
412                 len = tspi->curr_dma_words * tspi->bytes_per_word;
413                 for (count = 0; count < rx_full_count; count++) {
414                         x = tegra_spi_readl(tspi, SPI_RX_FIFO);
415                         for (i = 0; len && (i < 4); i++, len--)
416                                 *rx_buf++ = (x >> i*8) & 0xFF;
417                 }
418                 tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
419                 read_words += tspi->curr_dma_words;
420         } else {
421                 unsigned int bits_per_word;
422
423                 bits_per_word = t->bits_per_word ? t->bits_per_word :
424                                                 tspi->cur_spi->bits_per_word;
425                 for (count = 0; count < rx_full_count; count++) {
426                         x = tegra_spi_readl(tspi, SPI_RX_FIFO);
427                         for (i = 0; (i < tspi->bytes_per_word); i++)
428                                 *rx_buf++ = (x >> (i*8)) & 0xFF;
429                 }
430                 tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
431                 read_words += rx_full_count;
432         }
433         return read_words;
434 }
435
436 static void tegra_spi_copy_client_txbuf_to_spi_txbuf(
437                 struct tegra_spi_data *tspi, struct spi_transfer *t)
438 {
439         unsigned len;
440
441         /* Make the dma buffer to read by cpu */
442         dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
443                                 tspi->dma_buf_size, DMA_TO_DEVICE);
444
445         if (tspi->is_packed) {
446                 len = tspi->curr_dma_words * tspi->bytes_per_word;
447                 memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
448         } else {
449                 unsigned int i;
450                 unsigned int count;
451                 u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
452                 unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
453                 unsigned int x;
454
455                 for (count = 0; count < tspi->curr_dma_words; count++) {
456                         x = 0;
457                         for (i = 0; consume && (i < tspi->bytes_per_word);
458                                                         i++, consume--)
459                                 x |= ((*tx_buf++) << i * 8);
460                         tspi->tx_dma_buf[count] = x;
461                 }
462         }
463         tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
464
465         /* Make the dma buffer to read by dma */
466         dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
467                                 tspi->dma_buf_size, DMA_TO_DEVICE);
468 }
469
470 static void tegra_spi_copy_spi_rxbuf_to_client_rxbuf(
471                 struct tegra_spi_data *tspi, struct spi_transfer *t)
472 {
473         unsigned len;
474
475         /* Make the dma buffer to read by cpu */
476         dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
477                 tspi->dma_buf_size, DMA_FROM_DEVICE);
478
479         if (tspi->is_packed) {
480                 len = tspi->curr_dma_words * tspi->bytes_per_word;
481                 memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
482         } else {
483                 unsigned int i;
484                 unsigned int count;
485                 unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
486                 unsigned int x;
487                 unsigned int rx_mask, bits_per_word;
488
489                 bits_per_word = t->bits_per_word ? t->bits_per_word :
490                                                 tspi->cur_spi->bits_per_word;
491                 rx_mask = (1ULL << bits_per_word) - 1;
492                 for (count = 0; count < tspi->curr_dma_words; count++) {
493                         x = tspi->rx_dma_buf[count];
494                         x &= rx_mask;
495                         for (i = 0; (i < tspi->bytes_per_word); i++)
496                                 *rx_buf++ = (x >> (i*8)) & 0xFF;
497                 }
498         }
499         tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
500
501         /* Make the dma buffer to read by dma */
502         dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
503                 tspi->dma_buf_size, DMA_FROM_DEVICE);
504 }
505
506 static void tegra_spi_dma_complete(void *args)
507 {
508         struct completion *dma_complete = args;
509
510         complete(dma_complete);
511 }
512
513 static int tegra_spi_start_tx_dma(struct tegra_spi_data *tspi, int len)
514 {
515         INIT_COMPLETION(tspi->tx_dma_complete);
516         tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
517                                 tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
518                                 DMA_PREP_INTERRUPT |  DMA_CTRL_ACK);
519         if (!tspi->tx_dma_desc) {
520                 dev_err(tspi->dev, "Not able to get desc for Tx\n");
521                 return -EIO;
522         }
523
524         tspi->tx_dma_desc->callback = tegra_spi_dma_complete;
525         tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
526
527         dmaengine_submit(tspi->tx_dma_desc);
528         dma_async_issue_pending(tspi->tx_dma_chan);
529         return 0;
530 }
531
532 static int tegra_spi_start_rx_dma(struct tegra_spi_data *tspi, int len)
533 {
534         INIT_COMPLETION(tspi->rx_dma_complete);
535         tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
536                                 tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
537                                 DMA_PREP_INTERRUPT |  DMA_CTRL_ACK);
538         if (!tspi->rx_dma_desc) {
539                 dev_err(tspi->dev, "Not able to get desc for Rx\n");
540                 return -EIO;
541         }
542
543         tspi->rx_dma_desc->callback = tegra_spi_dma_complete;
544         tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
545
546         dmaengine_submit(tspi->rx_dma_desc);
547         dma_async_issue_pending(tspi->rx_dma_chan);
548         return 0;
549 }
550
551 static int check_and_clear_fifo(struct tegra_spi_data *tspi)
552 {
553         unsigned long status = tspi->status_reg;
554         int cnt = SPI_FIFO_FLUSH_MAX_DELAY;
555
556         /* Make sure that Rx and Tx fifo are empty */
557         if ((status & SPI_FIFO_EMPTY) != SPI_FIFO_EMPTY) {
558                 /* flush the fifo */
559                 status |= (SPI_RX_FIFO_FLUSH | SPI_TX_FIFO_FLUSH);
560                 tegra_spi_writel(tspi, status, SPI_FIFO_STATUS);
561                 do {
562                         status = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
563                         if ((status & SPI_FIFO_EMPTY) == SPI_FIFO_EMPTY)
564                                 return 0;
565                         udelay(1);
566                 } while (cnt--);
567                 dev_err(tspi->dev,
568                         "Rx/Tx fifo are not empty status 0x%08lx\n", status);
569                 return -EIO;
570         }
571         return 0;
572 }
573
574 static int tegra_spi_start_dma_based_transfer(
575                 struct tegra_spi_data *tspi, struct spi_transfer *t)
576 {
577         unsigned long val, cmd1;
578         unsigned int len;
579         unsigned long flags;
580         int ret = 0, maxburst;
581         struct dma_slave_config dma_sconfig;
582         u32 speed;
583
584         /* Make sure that Rx and Tx fifo are empty */
585         ret = check_and_clear_fifo(tspi);
586         if (ret != 0)
587                 return ret;
588
589         val = SPI_DMA_BLK_SET(tspi->curr_dma_words - 1);
590         tegra_spi_writel(tspi, val, SPI_DMA_BLK);
591
592         if (tspi->is_packed)
593                 len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
594                                         4) * 4;
595         else
596                 len = tspi->curr_dma_words * 4;
597
598         /* Set attention level based on length of transfer */
599         if (len & 0xF) {
600                 val |= SPI_TX_TRIG_1 | SPI_RX_TRIG_1;
601                 maxburst = 1;
602         } else if (((len) >> 4) & 0x1) {
603                 val |= SPI_TX_TRIG_4 | SPI_RX_TRIG_4;
604                 maxburst = 4;
605         } else {
606                 val |= SPI_TX_TRIG_8 | SPI_RX_TRIG_8;
607                 maxburst = 8;
608         }
609
610         if (!tspi->chip_data->intr_mask_reg) {
611                 if (!tspi->polling_mode) {
612                         if (tspi->cur_direction & DATA_DIR_TX)
613                                 val |= SPI_IE_TX;
614                         if (tspi->cur_direction & DATA_DIR_RX)
615                                 val |= SPI_IE_RX;
616                 }
617         }
618
619         tegra_spi_writel(tspi, val, SPI_DMA_CTL);
620         tspi->dma_control_reg = val;
621
622         if (tspi->cur_direction & DATA_DIR_TX) {
623                 dma_sconfig.dst_addr = tspi->phys + SPI_TX_FIFO;
624                 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
625                 dma_sconfig.dst_maxburst = maxburst;
626                 dmaengine_slave_config(tspi->tx_dma_chan, &dma_sconfig);
627
628                 tegra_spi_copy_client_txbuf_to_spi_txbuf(tspi, t);
629                 ret = tegra_spi_start_tx_dma(tspi, len);
630                 if (ret < 0) {
631                         dev_err(tspi->dev,
632                                 "Starting tx dma failed, err %d\n", ret);
633                         return ret;
634                 }
635         }
636
637         if (tspi->cur_direction & DATA_DIR_RX) {
638                 /* Make the dma buffer to read by dma */
639                 dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
640                                 tspi->dma_buf_size, DMA_FROM_DEVICE);
641                 dma_sconfig.src_addr = tspi->phys + SPI_RX_FIFO;
642                 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
643                 dma_sconfig.src_maxburst = maxburst;
644                 dmaengine_slave_config(tspi->rx_dma_chan, &dma_sconfig);
645
646                 ret = tegra_spi_start_rx_dma(tspi, len);
647                 if (ret < 0) {
648                         dev_err(tspi->dev,
649                                 "Starting rx dma failed, err %d\n", ret);
650                         if (tspi->cur_direction & DATA_DIR_TX)
651                                 dmaengine_terminate_all(tspi->tx_dma_chan);
652                         return ret;
653                 }
654         }
655
656         if (tspi->boost_reg_access) {
657                 speed = t->speed_hz ? t->speed_hz :
658                                 tspi->cur_spi->max_speed_hz;
659                 ret = tegra_spi_set_clock_rate(tspi, speed);
660                 if (ret < 0)
661                         return ret;
662         }
663
664         spin_lock_irqsave(&tspi->lock, flags);
665         cmd1 = tspi->command1_reg;
666         if (tspi->cur_direction & DATA_DIR_TX)
667                 cmd1 |= SPI_TX_EN;
668         if (tspi->cur_direction & DATA_DIR_RX)
669                 cmd1 |= SPI_RX_EN;
670         tegra_spi_writel(tspi, cmd1, SPI_COMMAND1);
671
672         tspi->is_curr_dma_xfer = true;
673         tspi->dma_control_reg = val;
674
675         tspi->transfer_in_progress = true;
676         val |= SPI_DMA_EN;
677         tegra_spi_writel(tspi, val, SPI_DMA_CTL);
678         spin_unlock_irqrestore(&tspi->lock, flags);
679         return ret;
680 }
681
682 static int tegra_spi_start_cpu_based_transfer(
683                 struct tegra_spi_data *tspi, struct spi_transfer *t)
684 {
685         unsigned long val;
686         unsigned long flags;
687         unsigned cur_words;
688         int ret;
689         u32 speed;
690
691         ret = check_and_clear_fifo(tspi);
692         if (ret != 0)
693                 return ret;
694
695         if (tspi->cur_direction & DATA_DIR_TX)
696                 cur_words = tegra_spi_fill_tx_fifo_from_client_txbuf(tspi, t);
697         else
698                 cur_words = tspi->curr_dma_words;
699
700         val = SPI_DMA_BLK_SET(cur_words - 1);
701         tegra_spi_writel(tspi, val, SPI_DMA_BLK);
702
703         val = 0;
704
705         if (!tspi->chip_data->intr_mask_reg) {
706                 if (!tspi->polling_mode) {
707                         if (tspi->cur_direction & DATA_DIR_TX)
708                                 val |= SPI_IE_TX;
709                         if (tspi->cur_direction & DATA_DIR_RX)
710                                 val |= SPI_IE_RX;
711                 }
712                 tegra_spi_writel(tspi, val, SPI_DMA_CTL);
713         }
714
715         tspi->dma_control_reg = val;
716
717         if (tspi->boost_reg_access) {
718                 speed = t->speed_hz ? t->speed_hz :
719                                 tspi->cur_spi->max_speed_hz;
720                 ret = tegra_spi_set_clock_rate(tspi, speed);
721                 if (ret < 0)
722                         return ret;
723         }
724
725         spin_lock_irqsave(&tspi->lock, flags);
726         tspi->is_curr_dma_xfer = false;
727         val = tspi->command1_reg;
728         if (tspi->cur_direction & DATA_DIR_TX)
729                 val |= SPI_TX_EN;
730         if (tspi->cur_direction & DATA_DIR_RX)
731                 val |= SPI_RX_EN;
732
733         tspi->transfer_in_progress = true;
734         val |= SPI_PIO;
735         tegra_spi_writel(tspi, val, SPI_COMMAND1);
736         spin_unlock_irqrestore(&tspi->lock, flags);
737         return 0;
738 }
739
740 static int tegra_spi_init_dma_param(struct tegra_spi_data *tspi,
741                         bool dma_to_memory)
742 {
743         struct dma_chan *dma_chan;
744         u32 *dma_buf;
745         dma_addr_t dma_phys;
746         int ret;
747         struct dma_slave_config dma_sconfig;
748
749         dma_chan = dma_request_slave_channel_reason(tspi->dev,
750                                         dma_to_memory ? "rx" : "tx");
751         if (IS_ERR(dma_chan)) {
752                 ret = PTR_ERR(dma_chan);
753                 if (ret != -EPROBE_DEFER)
754                         dev_err(tspi->dev,
755                                 "Dma channel is not available: %d\n", ret);
756                 return ret;
757         }
758
759         dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
760                                 &dma_phys, GFP_KERNEL);
761         if (!dma_buf) {
762                 dev_err(tspi->dev, "Not able to allocate the dma buffer\n");
763                 dma_release_channel(dma_chan);
764                 return -ENOMEM;
765         }
766
767         if (dma_to_memory) {
768                 dma_sconfig.src_addr = tspi->phys + SPI_RX_FIFO;
769                 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
770                 dma_sconfig.src_maxburst = 0;
771         } else {
772                 dma_sconfig.dst_addr = tspi->phys + SPI_TX_FIFO;
773                 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
774                 dma_sconfig.dst_maxburst = 0;
775         }
776
777         ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
778         if (ret)
779                 goto scrub;
780         if (dma_to_memory) {
781                 tspi->rx_dma_chan = dma_chan;
782                 tspi->rx_dma_buf = dma_buf;
783                 tspi->rx_dma_phys = dma_phys;
784         } else {
785                 tspi->tx_dma_chan = dma_chan;
786                 tspi->tx_dma_buf = dma_buf;
787                 tspi->tx_dma_phys = dma_phys;
788         }
789         return 0;
790
791 scrub:
792         dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
793         dma_release_channel(dma_chan);
794         return ret;
795 }
796
797 static void tegra_spi_deinit_dma_param(struct tegra_spi_data *tspi,
798         bool dma_to_memory)
799 {
800         u32 *dma_buf;
801         dma_addr_t dma_phys;
802         struct dma_chan *dma_chan;
803
804         if (dma_to_memory) {
805                 dma_buf = tspi->rx_dma_buf;
806                 dma_chan = tspi->rx_dma_chan;
807                 dma_phys = tspi->rx_dma_phys;
808                 tspi->rx_dma_chan = NULL;
809                 tspi->rx_dma_buf = NULL;
810         } else {
811                 dma_buf = tspi->tx_dma_buf;
812                 dma_chan = tspi->tx_dma_chan;
813                 dma_phys = tspi->tx_dma_phys;
814                 tspi->tx_dma_buf = NULL;
815                 tspi->tx_dma_chan = NULL;
816         }
817         if (!dma_chan)
818                 return;
819
820         dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
821         dma_release_channel(dma_chan);
822 }
823
824 static void set_best_clk_source(struct tegra_spi_data *tspi,
825                 unsigned long rate)
826 {
827         long new_rate;
828         unsigned long err_rate, crate, prate;
829         unsigned int cdiv, fin_err = rate;
830         int ret;
831         struct clk *pclk, *fpclk = NULL;
832         const char *pclk_name, *fpclk_name;
833         struct device_node *node;
834         struct property *prop;
835
836         node = tspi->master->dev.of_node;
837         if (!of_property_count_strings(node, "nvidia,clk-parents"))
838                 return;
839
840         /* when parent of a clk changes divider is not changed
841          * set a min div with which clk will not cross max rate
842          */
843         if (!tspi->min_div) {
844                 of_property_for_each_string(node, "nvidia,clk-parents",
845                                 prop, pclk_name) {
846                         pclk = clk_get(tspi->dev, pclk_name);
847                         if (IS_ERR(pclk))
848                                 continue;
849                         prate = clk_get_rate(pclk);
850                         crate = tspi->spi_max_frequency;
851                         cdiv = DIV_ROUND_UP(prate, crate);
852                         if (cdiv > tspi->min_div)
853                                 tspi->min_div = cdiv;
854                 }
855         }
856
857         pclk = clk_get_parent(tspi->clk);
858         crate = clk_get_rate(tspi->clk);
859         prate = clk_get_rate(pclk);
860         cdiv = DIV_ROUND_UP(prate, crate);
861         if (cdiv < tspi->min_div) {
862                 crate = DIV_ROUND_UP(prate, tspi->min_div);
863                 clk_set_rate(tspi->clk, crate);
864         }
865
866         of_property_for_each_string(node, "nvidia,clk-parents",
867                                 prop, pclk_name) {
868                 pclk = clk_get(tspi->dev, pclk_name);
869                 if (IS_ERR(pclk))
870                         continue;
871
872                 ret = clk_set_parent(tspi->clk, pclk);
873                 if (ret < 0) {
874                         dev_warn(tspi->dev,
875                                 "Error in setting parent clk src %s\n",
876                                 pclk_name);
877                         continue;
878                 }
879
880                 new_rate = clk_round_rate(tspi->clk, rate);
881                 if (new_rate < 0)
882                         continue;
883
884                 err_rate = abs(new_rate - rate);
885                 if (err_rate < fin_err) {
886                         fpclk = pclk;
887                         fin_err = err_rate;
888                         fpclk_name = pclk_name;
889                 }
890         }
891
892         if (fpclk) {
893                 dev_dbg(tspi->dev, "Setting clk_src %s\n",
894                                 fpclk_name);
895                 clk_set_parent(tspi->clk, fpclk);
896         }
897 }
898
899 static int tegra_spi_set_clock_rate(struct tegra_spi_data *tspi, u32 speed)
900 {
901         int ret;
902
903         if (speed == tspi->cur_speed)
904                 return 0;
905         set_best_clk_source(tspi, speed);
906         ret = clk_set_rate(tspi->clk, speed);
907         if (ret) {
908                 dev_err(tspi->dev, "Failed to set clk freq %d\n", ret);
909                 return -EINVAL;
910         }
911         tspi->cur_speed = speed;
912
913         return 0;
914 }
915 static int tegra_spi_start_transfer_one(struct spi_device *spi,
916                 struct spi_transfer *t, bool is_first_of_msg,
917                 bool is_single_xfer)
918 {
919         struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
920         u32 speed;
921         u32 spi_cs_timing2 = 0;
922         u8 bits_per_word;
923         unsigned total_fifo_words;
924         int ret;
925         struct tegra_spi_device_controller_data *cdata = spi->controller_data;
926         unsigned long command1;
927         int req_mode;
928         char prod_name[15];
929
930         bits_per_word = t->bits_per_word;
931         speed = t->speed_hz ? t->speed_hz : spi->max_speed_hz;
932                 /* set max clock for faster register access */
933         if (tspi->boost_reg_access)
934                 ret = tegra_spi_set_clock_rate(tspi, tspi->spi_max_frequency);
935         else
936                 ret = tegra_spi_set_clock_rate(tspi, speed);
937         if (ret < 0)
938                 return ret;
939
940         tspi->cur_spi = spi;
941         tspi->cur_pos = 0;
942         tspi->cur_rx_pos = 0;
943         tspi->cur_tx_pos = 0;
944         tspi->curr_xfer = t;
945         tspi->tx_status = 0;
946         tspi->rx_status = 0;
947         total_fifo_words = tegra_spi_calculate_curr_xfer_param(spi, tspi, t);
948
949         /* Check that the all words are available */
950         if (t->len % tspi->bytes_per_word != 0)
951                 return -EINVAL;
952
953         if (is_first_of_msg) {
954                 tegra_spi_clear_status(tspi);
955
956                 command1 = tspi->def_command1_reg;
957                 command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
958
959                 command1 &= ~SPI_CONTROL_MODE_MASK;
960                 req_mode = spi->mode & 0x3;
961                 if (req_mode == SPI_MODE_0)
962                         command1 |= SPI_CONTROL_MODE_0;
963                 else if (req_mode == SPI_MODE_1)
964                         command1 |= SPI_CONTROL_MODE_1;
965                 else if (req_mode == SPI_MODE_2)
966                         command1 |= SPI_CONTROL_MODE_2;
967                 else if (req_mode == SPI_MODE_3)
968                         command1 |= SPI_CONTROL_MODE_3;
969
970                 /* Apply mode setting before switching chip select */
971                 if (SPI_MODE_VAL(command1) !=
972                     SPI_MODE_VAL(tspi->def_command1_reg))
973                         tegra_spi_writel(tspi, command1, SPI_COMMAND1);
974
975                 /* possibly use the hw based chip select */
976                 tspi->is_hw_based_cs = false;
977                 if (cdata && cdata->is_hw_based_cs && is_single_xfer &&
978                         ((tspi->curr_dma_words * tspi->bytes_per_word) ==
979                                                 (t->len - tspi->cur_pos))) {
980                         u32 set_count;
981                         u32 hold_count;
982                         u32 spi_cs_timing;
983                         u32 spi_cs_setup;
984
985                         set_count = min(cdata->cs_setup_clk_count, 16);
986                         if (set_count)
987                                 set_count--;
988
989                         hold_count = min(cdata->cs_hold_clk_count, 16);
990                         if (hold_count)
991                                 hold_count--;
992
993                         spi_cs_setup = SPI_SETUP_HOLD(set_count,
994                                         hold_count);
995                         spi_cs_timing = tspi->spi_cs_timing;
996                         spi_cs_timing = SPI_CS_SETUP_HOLD(spi_cs_timing,
997                                                 spi->chip_select,
998                                                 spi_cs_setup);
999                         tspi->spi_cs_timing = spi_cs_timing;
1000                         tegra_spi_writel(tspi, spi_cs_timing,
1001                                                 SPI_CS_TIMING1);
1002                         tspi->is_hw_based_cs = true;
1003                 }
1004
1005                 if (cdata && cdata->cs_inactive_cycles) {
1006                         u32 inactive_cycles;
1007
1008                         SPI_SET_CS_ACTIVE_BETWEEN_PACKETS(spi_cs_timing2,
1009                                                 spi->chip_select,
1010                                                 0);
1011                         inactive_cycles = min(cdata->cs_inactive_cycles, 32);
1012                         SPI_SET_CYCLES_BETWEEN_PACKETS(spi_cs_timing2,
1013                                                 spi->chip_select,
1014                                                 inactive_cycles);
1015                         tegra_spi_writel(tspi, spi_cs_timing2, SPI_CS_TIMING2);
1016                         tspi->is_hw_based_cs = true;
1017                 } else {
1018                         SPI_SET_CS_ACTIVE_BETWEEN_PACKETS(spi_cs_timing2,
1019                                                 spi->chip_select, 1);
1020                         SPI_SET_CYCLES_BETWEEN_PACKETS(spi_cs_timing2,
1021                                                 spi->chip_select, 0);
1022                         tegra_spi_writel(tspi, spi_cs_timing2, SPI_CS_TIMING2);
1023                 }
1024
1025                 if (!tspi->is_hw_based_cs) {
1026                         command1 |= SPI_CS_SW_HW;
1027                         if (spi->mode & SPI_CS_HIGH)
1028                                 command1 |= SPI_CS_SS_VAL;
1029                         else
1030                                 command1 &= ~SPI_CS_SS_VAL;
1031                 } else {
1032                         command1 &= ~SPI_CS_SW_HW;
1033                         command1 &= ~SPI_CS_SS_VAL;
1034                 }
1035
1036                 if (cdata && gpio_is_valid(cdata->cs_gpio)) {
1037                         int gval = 0;
1038                         if (spi->mode & SPI_CS_HIGH)
1039                                 gval = 1;
1040                         gpio_set_value(cdata->cs_gpio, gval);
1041                 }
1042
1043                 if (tspi->prod_list) {
1044                         sprintf(prod_name, "prod_c_cs%d", spi->chip_select);
1045                         tegra_prod_set_by_name(&tspi->base, prod_name,
1046                                 tspi->prod_list);
1047                         tegra_prod_set_by_name(&tspi->base, "prod",
1048                                 tspi->prod_list);
1049                 } else {
1050                         u32 command2_reg;
1051                         command2_reg = tspi->def_command2_reg;
1052                         if (tspi->chip_data->set_rx_tap_delay) {
1053                                 if (speed > SPI_SPEED_TAP_DELAY_MARGIN) {
1054                                         command2_reg = command2_reg &
1055                                                 (~SPI_RX_TAP_DELAY(63));
1056                                         command2_reg = command2_reg |
1057                                                 SPI_RX_TAP_DELAY(
1058                                                 SPI_DEFAULT_RX_TAP_DELAY);
1059                                 }
1060                         }
1061                         if (command2_reg != tspi->def_command2_reg)
1062                                 tegra_spi_writel(tspi, command2_reg,
1063                                                         SPI_COMMAND2);
1064                 }
1065         } else {
1066                 command1 = tspi->command1_reg;
1067                 command1 &= ~SPI_BIT_LENGTH(~0);
1068                 command1 |= SPI_BIT_LENGTH(bits_per_word - 1);
1069         }
1070
1071         if (spi->mode & SPI_LSBYTE_FIRST)
1072                 command1 |= SPI_LSBYTE_FE;
1073         else
1074                 command1 &= ~SPI_LSBYTE_FE;
1075
1076         if (spi->mode & SPI_LSB_FIRST)
1077                 command1 |= SPI_LSBIT_FE;
1078         else
1079                 command1 &= ~SPI_LSBIT_FE;
1080
1081         if (spi->mode & SPI_3WIRE)
1082                 command1 |= SPI_BIDIROE;
1083         else
1084                 command1 &= ~SPI_BIDIROE;
1085
1086         command1 &=  ~SPI_BOTH_EN_BIT;
1087         if ((t->rx_nbits == SPI_NBITS_DUAL) ||
1088                         (t->tx_nbits == SPI_NBITS_DUAL))
1089                 command1 |=  SPI_BOTH_EN_BIT;
1090
1091         if (tspi->is_packed)
1092                 command1 |= SPI_PACKED;
1093
1094         command1 &= ~(SPI_CS_SEL_MASK | SPI_TX_EN | SPI_RX_EN);
1095         tspi->cur_direction = 0;
1096         if (t->rx_buf)
1097                 tspi->cur_direction |= DATA_DIR_RX;
1098         if (t->tx_buf)
1099                 tspi->cur_direction |= DATA_DIR_TX;
1100
1101         command1 |= SPI_CS_SEL(spi->chip_select);
1102         tspi->command1_reg = command1;
1103
1104         dev_dbg(tspi->dev, "The def 0x%x and written 0x%lx\n",
1105                                 tspi->def_command1_reg, command1);
1106
1107         tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
1108
1109         if (total_fifo_words > SPI_FIFO_DEPTH)
1110                 ret = tegra_spi_start_dma_based_transfer(tspi, t);
1111         else
1112                 ret = tegra_spi_start_cpu_based_transfer(tspi, t);
1113         return ret;
1114 }
1115
1116 static struct tegra_spi_device_controller_data
1117         *tegra_spi_get_cdata_dt(struct spi_device *spi,
1118                         struct tegra_spi_data *tspi)
1119 {
1120         struct tegra_spi_device_controller_data *cdata;
1121         struct device_node *slave_np, *data_np;
1122         int ret;
1123
1124         slave_np = spi->dev.of_node;
1125         if (!slave_np) {
1126                 dev_dbg(&spi->dev, "device node not found\n");
1127                 return NULL;
1128         }
1129
1130         data_np = of_get_child_by_name(slave_np, "controller-data");
1131         if (!data_np) {
1132                 dev_dbg(&spi->dev, "child node 'controller-data' not found\n");
1133                 return NULL;
1134         }
1135
1136         cdata = &tspi->cdata[spi->chip_select];
1137         memset(cdata, 0, sizeof(*cdata));
1138
1139         ret = of_property_read_bool(data_np, "nvidia,enable-hw-based-cs");
1140         if (ret)
1141                 cdata->is_hw_based_cs = 1;
1142
1143         of_property_read_u32(data_np, "nvidia,cs-setup-clk-count",
1144                         &cdata->cs_setup_clk_count);
1145         of_property_read_u32(data_np, "nvidia,cs-hold-clk-count",
1146                         &cdata->cs_hold_clk_count);
1147         of_property_read_u32(data_np, "nvidia,rx-clk-tap-delay",
1148                         &cdata->rx_clk_tap_delay);
1149         of_property_read_u32(data_np, "nvidia,tx-clk-tap-delay",
1150                         &cdata->tx_clk_tap_delay);
1151         of_property_read_u32(data_np, "nvidia,cs-inactive-cycles",
1152                         &cdata->cs_inactive_cycles);
1153         of_property_read_u32(data_np, "nvidia,clk-delay-between-packets",
1154                         &cdata->clk_delay_between_packets);
1155
1156         if (cdata->cs_inactive_cycles && cdata->clk_delay_between_packets) {
1157                 dev_err(&spi->dev,
1158                         "CS inactive time and packet delay cannot coexist\n");
1159                 return NULL;
1160         }
1161
1162         if (cdata->clk_delay_between_packets)
1163                 cdata->cs_inactive_cycles = cdata->clk_delay_between_packets;
1164
1165         cdata->cs_gpio = -EINVAL;
1166         if (of_find_property(data_np, "nvidia,chipselect-gpio", NULL))
1167                 cdata->cs_gpio = of_get_named_gpio(data_np,
1168                                         "nvidia,chipselect-gpio", 0);
1169         if ((cdata->cs_gpio < 0) && (cdata->cs_gpio != -EINVAL)) {
1170                 dev_err(&spi->dev,
1171                         "CS GPIO is not found on node %s: %d\n",
1172                         data_np->name, cdata->cs_gpio);
1173                 return NULL;
1174         }
1175         if ((cdata->cs_gpio < 0) && (cdata->clk_delay_between_packets)) {
1176                 dev_err(&spi->dev,
1177                         "CS packet delay requires gpio chip select\n");
1178                 return NULL;
1179         }
1180
1181         of_node_put(data_np);
1182         return cdata;
1183 }
1184
1185 static int tegra_spi_setup(struct spi_device *spi)
1186 {
1187         struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
1188         struct tegra_spi_device_controller_data *cdata = spi->controller_data;
1189         unsigned long val;
1190         unsigned long flags;
1191         unsigned long intr_mask;
1192         int ret;
1193         unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
1194                         SPI_CS_POL_INACTIVE_0,
1195                         SPI_CS_POL_INACTIVE_1,
1196                         SPI_CS_POL_INACTIVE_2,
1197                         SPI_CS_POL_INACTIVE_3,
1198         };
1199
1200         dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
1201                 spi->bits_per_word,
1202                 spi->mode & SPI_CPOL ? "" : "~",
1203                 spi->mode & SPI_CPHA ? "" : "~",
1204                 spi->max_speed_hz);
1205
1206         if (!cdata) {
1207                 cdata = tegra_spi_get_cdata_dt(spi, tspi);
1208                 spi->controller_data = cdata;
1209         }
1210         if (cdata)
1211                 if (cdata->clk_delay_between_packets)
1212                         cdata->cs_inactive_cycles =
1213                                 cdata->clk_delay_between_packets;
1214
1215         /* Set speed to the spi max fequency if spi device has not set */
1216         spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
1217
1218         ret = tegra_spi_runtime_get(tspi);
1219         if (ret < 0) {
1220                 dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
1221                 return ret;
1222         }
1223
1224         if (cdata && gpio_is_valid(cdata->cs_gpio)) {
1225                 if (!tspi->cs_gpio_reqstd[spi->chip_select]) {
1226                         int gpio_flag = GPIOF_OUT_INIT_HIGH;
1227                         if (spi->mode & SPI_CS_HIGH)
1228                                 gpio_flag = GPIOF_OUT_INIT_LOW;
1229
1230                         ret = devm_gpio_request_one(tspi->dev, cdata->cs_gpio,
1231                                         gpio_flag, "cs_gpio");
1232                         if (ret < 0) {
1233                                 dev_err(&spi->dev,
1234                                         "GPIO request failed: %d\n", ret);
1235                                 return ret;
1236                         }
1237                         tspi->cs_gpio_reqstd[spi->chip_select] = true;
1238                 } else {
1239                         int val = (spi->mode & SPI_CS_HIGH) ? 0 : 1;
1240                         gpio_set_value(cdata->cs_gpio, val);
1241                 }
1242         }
1243
1244         if (tspi->chip_data->intr_mask_reg) {
1245                 intr_mask = tegra_spi_readl(tspi, SPI_INTR_MASK);
1246                 if (!tspi->polling_mode)
1247                         intr_mask &= ~(SPI_INTR_ALL_MASK);
1248                 else
1249                         intr_mask |= SPI_INTR_ALL_MASK;
1250                 tegra_spi_writel(tspi, intr_mask, SPI_INTR_MASK);
1251         }
1252         spin_lock_irqsave(&tspi->lock, flags);
1253         val = tspi->def_command1_reg;
1254         if (spi->mode & SPI_CS_HIGH)
1255                 val &= ~cs_pol_bit[spi->chip_select];
1256         else
1257                 val |= cs_pol_bit[spi->chip_select];
1258         if (tspi->def_chip_select == spi->chip_select)
1259                 val |= SPI_MODE_SEL(spi->mode & 0x3);
1260
1261         tspi->def_command1_reg = val;
1262         tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
1263         spin_unlock_irqrestore(&tspi->lock, flags);
1264
1265         tegra_spi_runtime_put(tspi);
1266         return 0;
1267 }
1268
1269 static  int tegra_spi_cs_low(struct spi_device *spi,
1270                 bool state)
1271 {
1272         struct tegra_spi_data *tspi = spi_master_get_devdata(spi->master);
1273         struct tegra_spi_device_controller_data *cdata = spi->controller_data;
1274         int ret;
1275         unsigned long val;
1276         unsigned long flags;
1277         unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
1278                         SPI_CS_POL_INACTIVE_0,
1279                         SPI_CS_POL_INACTIVE_1,
1280                         SPI_CS_POL_INACTIVE_2,
1281                         SPI_CS_POL_INACTIVE_3,
1282         };
1283
1284         ret = tegra_spi_runtime_get(tspi);
1285         if (ret < 0) {
1286                 dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
1287                 return ret;
1288         }
1289
1290         if (cdata && gpio_is_valid(cdata->cs_gpio))
1291                 gpio_set_value(cdata->cs_gpio, 0);
1292
1293         spin_lock_irqsave(&tspi->lock, flags);
1294         if (!(spi->mode & SPI_CS_HIGH)) {
1295                 val = tegra_spi_readl(tspi, SPI_COMMAND1);
1296                 if (state)
1297                         val &= ~cs_pol_bit[spi->chip_select];
1298                 else
1299                         val |= cs_pol_bit[spi->chip_select];
1300                 tegra_spi_writel(tspi, val, SPI_COMMAND1);
1301         }
1302
1303         spin_unlock_irqrestore(&tspi->lock, flags);
1304         tegra_spi_runtime_put(tspi);
1305
1306         return 0;
1307 }
1308
1309 static void tegra_spi_dump_regs(struct tegra_spi_data *tspi)
1310 {
1311         u32 command1_reg;
1312         u32 fifo_status_reg;
1313         u32 dma_ctrl_reg;
1314         u32 trans_status_reg;
1315
1316         command1_reg = tegra_spi_readl(tspi, SPI_COMMAND1);
1317         fifo_status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
1318         dma_ctrl_reg = tegra_spi_readl(tspi, SPI_DMA_CTL);
1319         trans_status_reg = tegra_spi_readl(tspi, SPI_TRANS_STATUS);
1320
1321         dev_err(tspi->dev,
1322                         "SPI_ERR: CMD_0: 0x%08x, FIFO_STS: 0x%08x\n",
1323                         command1_reg, fifo_status_reg);
1324         dev_err(tspi->dev,
1325                         "SPI_ERR: DMA_CTL: 0x%08x, TRANS_STS: 0x%08x\n",
1326                         dma_ctrl_reg, trans_status_reg);
1327 }
1328
1329 static int tegra_spi_wait_on_message_xfer(struct tegra_spi_data *tspi)
1330 {
1331         int ret;
1332
1333         if (tspi->polling_mode)
1334                 ret = tegra_spi_status_poll(tspi);
1335         else
1336                 ret = wait_for_completion_timeout(&tspi->xfer_completion,
1337                                 SPI_DMA_TIMEOUT);
1338         if (WARN_ON(ret == 0)) {
1339                 dev_err(tspi->dev,
1340                                 "spi trasfer timeout, err %d\n", ret);
1341                 tegra_spi_dump_regs(tspi);
1342                 tegra_periph_reset_assert(tspi->clk);
1343                 udelay(2);
1344                 tegra_periph_reset_deassert(tspi->clk);
1345                 if (tspi->is_curr_dma_xfer &&
1346                                 (tspi->cur_direction & DATA_DIR_TX))
1347                         dmaengine_terminate_all(tspi->tx_dma_chan);
1348                 if (tspi->is_curr_dma_xfer &&
1349                                 (tspi->cur_direction & DATA_DIR_RX))
1350                         dmaengine_terminate_all(tspi->rx_dma_chan);
1351                 ret = -EIO;
1352                 return ret;
1353         }
1354         if (tspi->tx_status ||  tspi->rx_status) {
1355                 dev_err(tspi->dev, "Error in Transfer\n");
1356                 tegra_spi_dump_regs(tspi);
1357                 check_and_clear_fifo(tspi);
1358                 ret = -EIO;
1359         }
1360
1361         return 0;
1362 }
1363
1364 static int tegra_spi_wait_remain_message(struct tegra_spi_data *tspi,
1365                 struct spi_transfer *xfer)
1366 {
1367         unsigned total_fifo_words;
1368         int ret = 0;
1369
1370         INIT_COMPLETION(tspi->xfer_completion);
1371
1372         if (tspi->is_curr_dma_xfer) {
1373                 total_fifo_words = tegra_spi_calculate_curr_xfer_param(
1374                                 tspi->cur_spi, tspi, xfer);
1375                 if (total_fifo_words > SPI_FIFO_DEPTH)
1376                         ret = tegra_spi_start_dma_based_transfer(tspi, xfer);
1377                 else
1378                         ret = tegra_spi_start_cpu_based_transfer(tspi, xfer);
1379         } else {
1380                 tegra_spi_calculate_curr_xfer_param(tspi->cur_spi, tspi, xfer);
1381                 tegra_spi_start_cpu_based_transfer(tspi, xfer);
1382         }
1383
1384         ret = tegra_spi_wait_on_message_xfer(tspi);
1385
1386         return ret;
1387 }
1388
1389 static int tegra_spi_handle_message(struct tegra_spi_data *tspi,
1390                 struct spi_transfer *xfer)
1391 {
1392         int ret = 0;
1393         long wait_status;
1394
1395         if (tspi->boost_reg_access) {
1396                 /* set max clock for faster register access */
1397                 ret = tegra_spi_set_clock_rate(tspi, tspi->spi_max_frequency);
1398                 if (ret < 0)
1399                         return ret;
1400         }
1401
1402         if (!tspi->is_curr_dma_xfer) {
1403                 if (tspi->cur_direction & DATA_DIR_RX)
1404                         tegra_spi_read_rx_fifo_to_client_rxbuf(tspi, xfer);
1405                 if (tspi->cur_direction & DATA_DIR_TX)
1406                         tspi->cur_pos = tspi->cur_tx_pos;
1407                 else if (tspi->cur_direction & DATA_DIR_RX)
1408                         tspi->cur_pos = tspi->cur_rx_pos;
1409                 else
1410                         WARN_ON(1);
1411         } else {
1412                 if (tspi->cur_direction & DATA_DIR_TX) {
1413                         wait_status = wait_for_completion_interruptible_timeout(
1414                                         &tspi->tx_dma_complete,
1415                                         SPI_DMA_TIMEOUT);
1416                         if (wait_status <= 0) {
1417                                 dmaengine_terminate_all(tspi->tx_dma_chan);
1418                                 dev_err(tspi->dev, "TxDma Xfer failed, wait_status - %ld\n",
1419                                                 wait_status);
1420                                 tegra_spi_dump_regs(tspi);
1421                                 tegra_periph_reset_assert(tspi->clk);
1422                                 udelay(2);
1423                                 tegra_periph_reset_deassert(tspi->clk);
1424                                 ret = -EIO;
1425                                 return ret;
1426                         }
1427                 }
1428                 if (tspi->cur_direction & DATA_DIR_RX) {
1429                         wait_status = wait_for_completion_interruptible_timeout(
1430                                         &tspi->rx_dma_complete,
1431                                         SPI_DMA_TIMEOUT);
1432                         if (wait_status <= 0) {
1433                                 dmaengine_terminate_all(tspi->rx_dma_chan);
1434                                 dev_err(tspi->dev,
1435                                                 "RxDma Xfer failed, wait_status -  %ld\n",
1436                                                 wait_status);
1437                                 tegra_spi_dump_regs(tspi);
1438                                 tegra_periph_reset_assert(tspi->clk);
1439                                 udelay(2);
1440                                 tegra_periph_reset_deassert(tspi->clk);
1441                                 ret = -EIO;
1442                                 return ret;
1443                         }
1444                 }
1445                 if (tspi->cur_direction & DATA_DIR_RX)
1446                         tegra_spi_copy_spi_rxbuf_to_client_rxbuf(tspi, xfer);
1447
1448                 if (tspi->cur_direction & DATA_DIR_TX)
1449                         tspi->cur_pos = tspi->cur_tx_pos;
1450                 else
1451                         tspi->cur_pos = tspi->cur_rx_pos;
1452
1453         }
1454         return 0;
1455 }
1456
1457 static int tegra_spi_transfer_one_message(struct spi_master *master,
1458                         struct spi_message *msg)
1459 {
1460         bool is_first_msg = true;
1461         bool is_new_msg = true;
1462         int single_xfer;
1463         struct tegra_spi_data *tspi = spi_master_get_devdata(master);
1464         struct spi_transfer *xfer;
1465         struct spi_device *spi = msg->spi;
1466         struct tegra_spi_device_controller_data *cdata = spi->controller_data;
1467         int ret;
1468         int gval = 1;
1469
1470         msg->status = 0;
1471         msg->actual_length = 0;
1472
1473         if (spi->mode & SPI_CS_HIGH)
1474                 gval = 0;
1475
1476         ret = tegra_spi_runtime_get(tspi);
1477         if (ret < 0) {
1478                 dev_err(tspi->dev, "runtime PM get failed: %d\n", ret);
1479                 msg->status = ret;
1480                 spi_finalize_current_message(master);
1481                 return ret;
1482         }
1483
1484         single_xfer = list_is_singular(&msg->transfers);
1485         list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1486                 while (1) {
1487                         if (is_new_msg) {
1488                                 INIT_COMPLETION(tspi->xfer_completion);
1489                                 ret = tegra_spi_start_transfer_one(spi, xfer,
1490                                                 is_first_msg, single_xfer);
1491                                 if (ret < 0) {
1492                                         dev_err(tspi->dev,
1493                                                         "spi cannot start transfer,err %d\n",
1494                                                         ret);
1495                                         goto exit;
1496                                 }
1497                                 is_first_msg = false;
1498                                 is_new_msg = false;
1499                                 ret = tegra_spi_wait_on_message_xfer(tspi);
1500                                 if (ret)
1501                                         goto exit;
1502                                 ret = tegra_spi_handle_message(tspi, xfer);
1503                                 if (ret)
1504                                         goto exit;
1505                                 if (tspi->cur_pos == xfer->len) {
1506                                         is_new_msg = true;
1507                                         break;
1508                                 }
1509                         } else {
1510                                 ret = tegra_spi_wait_remain_message(tspi, xfer);
1511                                 if (ret)
1512                                         goto exit;
1513                                 ret = tegra_spi_handle_message(tspi, xfer);
1514                                 if (ret)
1515                                         goto exit;
1516                                 if (tspi->cur_pos == xfer->len) {
1517                                         is_new_msg = true;
1518                                         break;
1519                                 }
1520                         }
1521                 } /* End of while */
1522                 msg->actual_length += xfer->len;
1523                 if (xfer->cs_change && xfer->delay_usecs) {
1524                         tegra_spi_writel(tspi, tspi->def_command1_reg,
1525                                         SPI_COMMAND1);
1526
1527                         if (cdata && gpio_is_valid(cdata->cs_gpio))
1528                                 gpio_set_value(cdata->cs_gpio, gval);
1529
1530                         udelay(xfer->delay_usecs);
1531
1532                         if (cdata && gpio_is_valid(cdata->cs_gpio))
1533                                 gpio_set_value(cdata->cs_gpio, !gval);
1534                 }
1535         }
1536         ret = 0;
1537 exit:
1538         tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
1539         if (cdata && gpio_is_valid(cdata->cs_gpio))
1540                 gpio_set_value(cdata->cs_gpio, gval);
1541
1542         tegra_spi_runtime_put(tspi);
1543         msg->status = ret;
1544         spi_finalize_current_message(master);
1545         return ret;
1546 }
1547
1548 static void handle_cpu_based_err_xfer(struct tegra_spi_data *tspi)
1549 {
1550         unsigned long flags;
1551
1552         spin_lock_irqsave(&tspi->lock, flags);
1553         if (tspi->tx_status ||  tspi->rx_status) {
1554                 dev_err(tspi->dev, "CpuXfer ERROR bit set 0x%x\n",
1555                         tspi->status_reg);
1556
1557                 if (tspi->status_reg & SPI_TX_FIFO_UNF)
1558                         dev_err(tspi->dev, "CpuXfer: TX FIFO UNDERRUN ERROR\n");
1559
1560                 if (tspi->status_reg & SPI_TX_FIFO_OVF)
1561                         dev_err(tspi->dev, "CpuXfer: TX FIFO OVERFLOW ERROR\n");
1562
1563                 if (tspi->status_reg & SPI_RX_FIFO_UNF)
1564                         dev_err(tspi->dev, "CpuXfer: RX FIFO UNDERRUN ERROR\n");
1565
1566                 if (tspi->status_reg & SPI_RX_FIFO_OVF)
1567                         dev_err(tspi->dev, "CpuXfer: RX FIFO OVERFLOW ERROR\n");
1568
1569                 dev_err(tspi->dev, "CpuXfer 0x%08x:0x%08x\n",
1570                         tspi->command1_reg, tspi->dma_control_reg);
1571                 tegra_spi_dump_regs(tspi);
1572                 tegra_periph_reset_assert(tspi->clk);
1573                 udelay(2);
1574                 tegra_periph_reset_deassert(tspi->clk);
1575         }
1576         spin_unlock_irqrestore(&tspi->lock, flags);
1577 }
1578
1579 static void handle_dma_based_err_xfer(struct tegra_spi_data *tspi)
1580 {
1581         int err = 0;
1582         unsigned long flags;
1583
1584         spin_lock_irqsave(&tspi->lock, flags);
1585         /* Abort dmas if any error */
1586         if (tspi->cur_direction & DATA_DIR_TX) {
1587                 if (tspi->tx_status) {
1588                         dmaengine_terminate_all(tspi->tx_dma_chan);
1589                         err += 1;
1590                 }
1591         }
1592
1593         if (tspi->cur_direction & DATA_DIR_RX) {
1594                 if (tspi->rx_status) {
1595                         dmaengine_terminate_all(tspi->rx_dma_chan);
1596                         err += 2;
1597                 }
1598         }
1599
1600         if (err) {
1601                 dev_err(tspi->dev, "DmaXfer: ERROR bit set 0x%x\n",
1602                         tspi->status_reg);
1603
1604                 if (tspi->status_reg & SPI_TX_FIFO_UNF)
1605                         dev_err(tspi->dev, "DmaXfer: TX FIFO UNDERRUN ERROR\n");
1606
1607                 if (tspi->status_reg & SPI_TX_FIFO_OVF)
1608                         dev_err(tspi->dev, "DmaXfer: TX FIFO OVERFLOW ERROR\n");
1609
1610                 if (tspi->status_reg & SPI_RX_FIFO_UNF)
1611                         dev_err(tspi->dev, "DmaXfer: RX FIFO UNDERRUN ERROR\n");
1612
1613                 if (tspi->status_reg & SPI_RX_FIFO_OVF)
1614                         dev_err(tspi->dev, "DmaXfer: RX FIFO OVERFLOW ERROR\n");
1615
1616                 dev_err(tspi->dev, "DmaXfer 0x%08x:0x%08x\n",
1617                         tspi->command1_reg, tspi->dma_control_reg);
1618                 tegra_spi_dump_regs(tspi);
1619                 tegra_periph_reset_assert(tspi->clk);
1620                 udelay(2);
1621                 tegra_periph_reset_deassert(tspi->clk);
1622         }
1623         spin_unlock_irqrestore(&tspi->lock, flags);
1624 }
1625
1626 static irqreturn_t tegra_spi_isr(int irq, void *context_data)
1627 {
1628         struct tegra_spi_data *tspi = context_data;
1629
1630         if (tspi->polling_mode)
1631                 dev_warn(tspi->dev, "interrupt raised in polling mode\n");
1632
1633         tegra_spi_clear_status(tspi);
1634         if (!tspi->transfer_in_progress) {
1635                 dev_err(tspi->dev, "spurious interrupt, status_reg = 0x%x\n",
1636                                 tspi->status_reg);
1637                 return IRQ_NONE;
1638         }
1639         tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
1640         if (tspi->cur_direction & SPI_TX_EN)
1641                 tspi->tx_status = tspi->status_reg &
1642                                         (SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF);
1643
1644         if (tspi->cur_direction & SPI_RX_EN)
1645                 tspi->rx_status = tspi->status_reg &
1646                                         (SPI_RX_FIFO_OVF | SPI_RX_FIFO_UNF);
1647
1648         if (!tspi->is_curr_dma_xfer)
1649                 handle_cpu_based_err_xfer(tspi);
1650         else
1651                 handle_dma_based_err_xfer(tspi);
1652
1653         tspi->transfer_in_progress = false;
1654         complete(&tspi->xfer_completion);
1655         return IRQ_HANDLED;
1656 }
1657
1658 static int tegra_spi_status_poll(struct tegra_spi_data *tspi)
1659 {
1660         unsigned int status;
1661         unsigned long timeout;
1662
1663         timeout = SPI_POLL_TIMEOUT;
1664         /*
1665          * Read register would take between 1~3us and 1us delay added in loop
1666          * Calculate timeout taking this into consideration
1667          */
1668         do {
1669                 status = tegra_spi_readl(tspi, SPI_TRANS_STATUS);
1670                 if (status & SPI_RDY)
1671                         break;
1672                 timeout--;
1673                 udelay(1);
1674         } while (timeout);
1675
1676         if (!timeout) {
1677                 dev_err(tspi->dev, "transfer timeout (polling)\n");
1678                 return 0;
1679         }
1680
1681         tspi->status_reg = tegra_spi_readl(tspi, SPI_FIFO_STATUS);
1682         if (tspi->cur_direction & DATA_DIR_TX)
1683                 tspi->tx_status = tspi->status_reg &
1684                                         (SPI_TX_FIFO_UNF | SPI_TX_FIFO_OVF);
1685
1686         if (tspi->cur_direction & DATA_DIR_RX)
1687                 tspi->rx_status = tspi->status_reg &
1688                                         (SPI_RX_FIFO_OVF | SPI_RX_FIFO_UNF);
1689
1690         if (!(tspi->cur_direction & DATA_DIR_TX) &&
1691                         !(tspi->cur_direction & DATA_DIR_RX))
1692                 dev_err(tspi->dev, "spurious interrupt, status_reg = 0x%x\n",
1693                                 tspi->status_reg);
1694
1695         tegra_spi_clear_status(tspi);
1696
1697         if (!tspi->is_curr_dma_xfer)
1698                 handle_cpu_based_err_xfer(tspi);
1699         else
1700                 handle_dma_based_err_xfer(tspi);
1701
1702         return timeout;
1703 }
1704
1705 static struct tegra_spi_platform_data *tegra_spi_parse_dt(
1706                 struct platform_device *pdev)
1707 {
1708         struct tegra_spi_platform_data *pdata;
1709         const unsigned int *prop;
1710         struct device_node *np = pdev->dev.of_node;
1711         struct device_node *nc = NULL;
1712         struct device_node *found_nc = NULL;
1713         u32 pval;
1714         int len;
1715         int ret;
1716
1717         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1718         if (!pdata) {
1719                 dev_err(&pdev->dev, "Memory alloc for pdata failed\n");
1720                 return NULL;
1721         }
1722
1723         prop = of_get_property(np, "spi-max-frequency", NULL);
1724         if (prop)
1725                 pdata->spi_max_frequency = be32_to_cpup(prop);
1726
1727         if (of_find_property(np, "nvidia,clock-always-on", NULL))
1728                 pdata->is_clkon_always = true;
1729
1730         if (of_find_property(np, "nvidia,polling-mode", NULL))
1731                 pdata->is_polling_mode = true;
1732
1733         if (of_find_property(np, "nvidia,boost-reg-access", NULL))
1734                 pdata->boost_reg_access = true;
1735
1736         if (of_find_property(np, "nvidia,disable-runtime-pm", NULL))
1737                 pdata->runtime_pm = false;
1738         else
1739                 pdata->runtime_pm = true;
1740
1741         ret = of_property_read_u32(np, "nvidia,maximum-dma-buffer-size", &pval);
1742         if (!ret)
1743                 pdata->max_dma_buffer_size = pval;
1744
1745         /* when no client is defined, default chipselect is zero */
1746         pdata->def_chip_select = 0;
1747
1748         /*
1749          * Last child node or first node which has property as default-cs will
1750          * become the default.
1751          */
1752         for_each_available_child_of_node(np, nc) {
1753                 found_nc = nc;
1754                 ret = of_property_read_bool(nc, "nvidia,default-chipselect");
1755                 if (ret)
1756                         break;
1757         }
1758         if (found_nc) {
1759                 prop = of_get_property(found_nc, "reg", &len);
1760                 if (!prop || len < sizeof(*prop))
1761                         dev_err(&pdev->dev, "%s has no reg property\n",
1762                                         found_nc->full_name);
1763                 else
1764                         pdata->def_chip_select = be32_to_cpup(prop);
1765         }
1766
1767         return pdata;
1768 }
1769
1770 static struct tegra_spi_chip_data tegra114_spi_chip_data = {
1771         .intr_mask_reg = false,
1772         .set_rx_tap_delay = false,
1773 };
1774
1775 static struct tegra_spi_chip_data tegra124_spi_chip_data = {
1776         .intr_mask_reg = false,
1777         .set_rx_tap_delay = true,
1778 };
1779
1780 static struct tegra_spi_chip_data tegra210_spi_chip_data = {
1781         .intr_mask_reg = true,
1782         .set_rx_tap_delay = false,
1783 };
1784
1785 static struct of_device_id tegra_spi_of_match[] = {
1786         {
1787                 .compatible = "nvidia,tegra114-spi",
1788                 .data       = &tegra114_spi_chip_data,
1789         }, {
1790                 .compatible = "nvidia,tegra124-spi",
1791                 .data       = &tegra124_spi_chip_data,
1792         }, {
1793                 .compatible = "nvidia,tegra210-spi",
1794                 .data       = &tegra210_spi_chip_data,
1795         }, {
1796                 .compatible = "nvidia,tegra186-spi",
1797                 .data       = &tegra210_spi_chip_data,
1798         },
1799         {}
1800 };
1801 MODULE_DEVICE_TABLE(of, tegra_spi_of_match);
1802
1803 static int tegra_spi_probe(struct platform_device *pdev)
1804 {
1805         struct spi_master       *master;
1806         struct tegra_spi_data   *tspi;
1807         struct resource         *r;
1808         struct tegra_spi_platform_data *pdata = pdev->dev.platform_data;
1809         const struct of_device_id *match;
1810         const struct tegra_spi_chip_data *chip_data = &tegra114_spi_chip_data;
1811         int ret, spi_irq;
1812         int bus_num;
1813
1814         if (pdev->dev.of_node) {
1815                 bus_num = of_alias_get_id(pdev->dev.of_node, "spi");
1816                 if (bus_num < 0) {
1817                         dev_warn(&pdev->dev,
1818                                 "Dynamic bus number will be registerd\n");
1819                         bus_num = -1;
1820                 }
1821         } else {
1822                 bus_num = pdev->id;
1823         }
1824
1825         if (!pdata && pdev->dev.of_node)
1826                 pdata = tegra_spi_parse_dt(pdev);
1827
1828         if (!pdata) {
1829                 dev_err(&pdev->dev, "No platform data, exiting\n");
1830                 return -ENODEV;
1831         }
1832
1833         if (!pdata->spi_max_frequency)
1834                 pdata->spi_max_frequency = 25000000; /* 25MHz */
1835
1836         master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
1837         if (!master) {
1838                 dev_err(&pdev->dev, "master allocation failed\n");
1839                 return -ENOMEM;
1840         }
1841
1842         /* the spi->mode bits understood by this driver: */
1843         master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST |
1844                 SPI_TX_DUAL | SPI_RX_DUAL;
1845         /* supported bpw 4-32 */
1846         master->bits_per_word_mask = (u32) ~(BIT(0)|BIT(1)|BIT(2));
1847         master->setup = tegra_spi_setup;
1848         master->transfer_one_message = tegra_spi_transfer_one_message;
1849         master->num_chipselect = MAX_CHIP_SELECT;
1850         master->bus_num = bus_num;
1851         master->spi_cs_low  = tegra_spi_cs_low;
1852
1853         dev_set_drvdata(&pdev->dev, master);
1854         tspi = spi_master_get_devdata(master);
1855         tspi->master = master;
1856         tspi->clock_always_on = pdata->is_clkon_always;
1857         tspi->polling_mode = pdata->is_polling_mode;
1858         tspi->boost_reg_access = pdata->boost_reg_access;
1859         tspi->runtime_pm = pdata->runtime_pm;
1860         if (!tspi->runtime_pm)
1861                 tspi->clock_always_on = true;
1862         tspi->def_chip_select = pdata->def_chip_select;
1863         tspi->dev = &pdev->dev;
1864
1865         if (pdev->dev.of_node) {
1866                 match = of_match_device(tegra_spi_of_match,
1867                                 &pdev->dev);
1868                 if (match)
1869                         chip_data = match->data;
1870         }
1871         tspi->chip_data = chip_data;
1872
1873         tspi->prod_list = tegra_prod_get(&pdev->dev, NULL);
1874         if (IS_ERR(tspi->prod_list)) {
1875                 dev_err(&pdev->dev, "Prod settings list not initialized\n");
1876                 tspi->prod_list = NULL;
1877         }
1878
1879         spin_lock_init(&tspi->lock);
1880
1881         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1882         if (!r) {
1883                 dev_err(&pdev->dev, "No IO memory resource\n");
1884                 ret = -ENODEV;
1885                 goto exit_free_master;
1886         }
1887         tspi->phys = r->start;
1888         tspi->base = devm_ioremap_resource(&pdev->dev, r);
1889         if (IS_ERR(tspi->base)) {
1890                 dev_err(&pdev->dev,
1891                         "Cannot request memregion/iomap dma address\n");
1892                 ret = PTR_ERR(tspi->base);
1893                 goto exit_free_master;
1894         }
1895
1896         spi_irq = platform_get_irq(pdev, 0);
1897         tspi->irq = spi_irq;
1898
1899         tspi->clk = devm_clk_get(&pdev->dev, "spi");
1900         if (IS_ERR(tspi->clk)) {
1901                 dev_err(&pdev->dev, "can not get clock\n");
1902                 ret = PTR_ERR(tspi->clk);
1903                 goto exit_free_master;
1904         }
1905
1906         tspi->pinctrl = devm_pinctrl_get(&pdev->dev);
1907         if (IS_ERR(tspi->pinctrl)) {
1908                 dev_info(&pdev->dev, "Pincontrol not found\n");
1909                 tspi->pinctrl = NULL;
1910         }
1911
1912         if (tspi->pinctrl) {
1913                 tspi->enable_interface = pinctrl_lookup_state(tspi->pinctrl,
1914                                                 "interface-enable");
1915                 if (IS_ERR(tspi->enable_interface)) {
1916                         dev_info(&pdev->dev, "Static pin configuration used\n");
1917                         tspi->enable_interface = NULL;
1918                 }
1919         }
1920
1921         tspi->max_buf_size = SPI_FIFO_DEPTH << 2;
1922         tspi->dma_buf_size = (pdata->max_dma_buffer_size) ?
1923                                 pdata->max_dma_buffer_size :
1924                                 DEFAULT_SPI_DMA_BUF_LEN;
1925         tspi->spi_max_frequency = pdata->spi_max_frequency;
1926         tspi->min_div = 0;
1927
1928         ret = tegra_spi_init_dma_param(tspi, true);
1929         if (ret < 0)
1930                 goto exit_free_master;
1931         ret = tegra_spi_init_dma_param(tspi, false);
1932         if (ret < 0)
1933                 goto exit_rx_dma_free;
1934         tspi->max_buf_size = tspi->dma_buf_size;
1935         init_completion(&tspi->tx_dma_complete);
1936         init_completion(&tspi->rx_dma_complete);
1937
1938         init_completion(&tspi->xfer_completion);
1939
1940         if (tspi->clock_always_on) {
1941                 ret = clk_prepare_enable(tspi->clk);
1942                 if (ret < 0) {
1943                         dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
1944                         goto exit_deinit_dma;
1945                 }
1946         }
1947         if (tspi->runtime_pm) {
1948                 pm_runtime_enable(tspi->dev);
1949                 if (!pm_runtime_enabled(tspi->dev)) {
1950                         ret = tegra_spi_runtime_resume(tspi->dev);
1951                         if (ret)
1952                                 goto exit_pm_disable;
1953                 }
1954
1955                 /* set autosuspend delay for the adapter device */
1956                 pm_runtime_set_autosuspend_delay(tspi->dev,
1957                                                  SPI_AUTOSUSPEND_DELAY);
1958                 pm_runtime_use_autosuspend(tspi->dev);
1959         }
1960
1961         ret = tegra_spi_runtime_get(tspi);
1962         if (ret < 0) {
1963                 dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
1964                 goto exit_pm_disable;
1965         }
1966
1967         tegra_periph_reset_assert(tspi->clk);
1968         udelay(2);
1969         tegra_periph_reset_deassert(tspi->clk);
1970
1971         tspi->def_command1_reg  = SPI_M_S | SPI_LSBYTE_FE;
1972         tspi->def_command1_reg |= SPI_CS_SEL(tspi->def_chip_select);
1973         tegra_spi_writel(tspi, tspi->def_command1_reg, SPI_COMMAND1);
1974         tspi->def_command2_reg = tegra_spi_readl(tspi, SPI_COMMAND2);
1975         tegra_spi_runtime_put(tspi);
1976
1977         ret = devm_request_irq(&pdev->dev, tspi->irq, tegra_spi_isr, 0,
1978                         dev_name(&pdev->dev), tspi);
1979         if (ret < 0) {
1980                 dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
1981                                         tspi->irq);
1982                 goto exit_free_master;
1983         }
1984
1985         master->dev.of_node = pdev->dev.of_node;
1986         ret = spi_register_master(master);
1987         if (ret < 0) {
1988                 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
1989                 goto exit_pm_disable;
1990         }
1991
1992         if (tspi->enable_interface) {
1993                 ret = pinctrl_select_state(tspi->pinctrl,
1994                                 tspi->enable_interface);
1995                 if (ret < 0) {
1996                         dev_err(&pdev->dev, "Enable pin interface failed: %d\n",
1997                                 ret);
1998                         goto exit_master_unreg;
1999                 }
2000         }
2001
2002         return ret;
2003
2004 exit_master_unreg:
2005         spi_unregister_master(master);
2006
2007 exit_pm_disable:
2008         pm_runtime_disable(&pdev->dev);
2009         if (!pm_runtime_status_suspended(&pdev->dev))
2010                 tegra_spi_runtime_suspend(&pdev->dev);
2011         if (tspi->clock_always_on)
2012                 clk_disable_unprepare(tspi->clk);
2013 exit_deinit_dma:
2014         tegra_spi_deinit_dma_param(tspi, false);
2015 exit_rx_dma_free:
2016         tegra_spi_deinit_dma_param(tspi, true);
2017 exit_free_master:
2018         spi_master_put(master);
2019         return ret;
2020 }
2021
2022 static int tegra_spi_remove(struct platform_device *pdev)
2023 {
2024         struct spi_master *master = dev_get_drvdata(&pdev->dev);
2025         struct tegra_spi_data   *tspi = spi_master_get_devdata(master);
2026
2027         spi_unregister_master(master);
2028
2029         if (tspi->tx_dma_chan)
2030                 tegra_spi_deinit_dma_param(tspi, false);
2031
2032         if (tspi->rx_dma_chan)
2033                 tegra_spi_deinit_dma_param(tspi, true);
2034
2035         pm_runtime_disable(&pdev->dev);
2036         if (!pm_runtime_status_suspended(&pdev->dev))
2037                 tegra_spi_runtime_suspend(&pdev->dev);
2038
2039         if (tspi->clock_always_on)
2040                 clk_disable_unprepare(tspi->clk);
2041
2042         if (tspi->prod_list)
2043                 tegra_prod_release(&tspi->prod_list);
2044
2045         return 0;
2046 }
2047
2048 #ifdef CONFIG_PM_SLEEP
2049 static int tegra_spi_suspend(struct device *dev)
2050 {
2051         struct spi_master *master = dev_get_drvdata(dev);
2052         struct tegra_spi_data *tspi = spi_master_get_devdata(master);
2053         int ret;
2054
2055         ret = spi_master_suspend(master);
2056
2057         if (tspi->clock_always_on)
2058                 clk_disable_unprepare(tspi->clk);
2059
2060         return ret;
2061 }
2062
2063 static int tegra_spi_resume(struct device *dev)
2064 {
2065         struct spi_master *master = dev_get_drvdata(dev);
2066         struct tegra_spi_data *tspi = spi_master_get_devdata(master);
2067         unsigned long intr_mask;
2068         int ret;
2069
2070         if (tspi->clock_always_on) {
2071                 ret = clk_prepare_enable(tspi->clk);
2072                 if (ret < 0) {
2073                         dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
2074                         return ret;
2075                 }
2076         }
2077
2078         ret = tegra_spi_runtime_get(tspi);
2079         if (ret < 0) {
2080                 dev_err(dev, "pm runtime failed, e = %d\n", ret);
2081                 return ret;
2082         }
2083         tegra_spi_writel(tspi, tspi->command1_reg, SPI_COMMAND1);
2084         tegra_spi_writel(tspi, tspi->def_command2_reg, SPI_COMMAND2);
2085         if (tspi->chip_data->intr_mask_reg) {
2086                 if ((tspi->cur_direction & DATA_DIR_TX) ||
2087                     (tspi->cur_direction & DATA_DIR_RX)) {
2088                         intr_mask = tegra_spi_readl(tspi, SPI_INTR_MASK);
2089                         if (!tspi->polling_mode)
2090                                 intr_mask &= ~(SPI_INTR_ALL_MASK);
2091                         else
2092                                 intr_mask |= SPI_INTR_ALL_MASK;
2093                         tegra_spi_writel(tspi, intr_mask, SPI_INTR_MASK);
2094                 }
2095         }
2096         tegra_spi_runtime_put(tspi);
2097
2098         return spi_master_resume(master);
2099 }
2100 #endif
2101
2102 static int tegra_spi_runtime_suspend(struct device *dev)
2103 {
2104         struct spi_master *master = dev_get_drvdata(dev);
2105         struct tegra_spi_data *tspi = spi_master_get_devdata(master);
2106
2107         /* Flush all write which are in PPSB queue by reading back */
2108         tegra_spi_readl(tspi, SPI_COMMAND1);
2109
2110         clk_disable_unprepare(tspi->clk);
2111         return 0;
2112 }
2113
2114 static int tegra_spi_runtime_resume(struct device *dev)
2115 {
2116         struct spi_master *master = dev_get_drvdata(dev);
2117         struct tegra_spi_data *tspi = spi_master_get_devdata(master);
2118         int ret;
2119
2120         ret = clk_prepare_enable(tspi->clk);
2121         if (ret < 0) {
2122                 dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
2123                 return ret;
2124         }
2125         return 0;
2126 }
2127
2128 static const struct dev_pm_ops tegra_spi_pm_ops = {
2129         SET_RUNTIME_PM_OPS(tegra_spi_runtime_suspend,
2130                 tegra_spi_runtime_resume, NULL)
2131         SET_SYSTEM_SLEEP_PM_OPS(tegra_spi_suspend, tegra_spi_resume)
2132 };
2133 static struct platform_driver tegra_spi_driver = {
2134         .driver = {
2135                 .name           = "spi-tegra114",
2136                 .owner          = THIS_MODULE,
2137                 .pm             = &tegra_spi_pm_ops,
2138                 .of_match_table = of_match_ptr(tegra_spi_of_match),
2139         },
2140         .probe =        tegra_spi_probe,
2141         .remove =       tegra_spi_remove,
2142 };
2143 module_platform_driver(tegra_spi_driver);
2144
2145 MODULE_ALIAS("platform:spi-tegra114");
2146 MODULE_DESCRIPTION("NVIDIA Tegra114/124 SPI Controller Driver");
2147 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
2148 MODULE_LICENSE("GPL v2");