GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / spi / spi-fsl-dspi.c
1 /*
2  * drivers/spi/spi-fsl-dspi.c
3  *
4  * Copyright 2013 Freescale Semiconductor, Inc.
5  *
6  * Freescale DSPI driver
7  * This file contains a driver for the Freescale DSPI
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  */
15
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/dmaengine.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/err.h>
21 #include <linux/errno.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/kernel.h>
25 #include <linux/math64.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/of_device.h>
29 #include <linux/pinctrl/consumer.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/regmap.h>
33 #include <linux/sched.h>
34 #include <linux/spi/spi.h>
35 #include <linux/spi/spi_bitbang.h>
36 #include <linux/time.h>
37
38 #define DRIVER_NAME "fsl-dspi"
39
40 #define TRAN_STATE_RX_VOID              0x01
41 #define TRAN_STATE_TX_VOID              0x02
42 #define TRAN_STATE_WORD_ODD_NUM 0x04
43
44 #define DSPI_FIFO_SIZE                  4
45 #define DSPI_DMA_BUFSIZE                (DSPI_FIFO_SIZE * 1024)
46
47 #define SPI_MCR         0x00
48 #define SPI_MCR_MASTER          (1 << 31)
49 #define SPI_MCR_PCSIS           (0x3F << 16)
50 #define SPI_MCR_CLR_TXF (1 << 11)
51 #define SPI_MCR_CLR_RXF (1 << 10)
52 #define SPI_MCR_DIS_TXF         (1 << 13)
53 #define SPI_MCR_DIS_RXF         (1 << 12)
54 #define SPI_MCR_HALT            (1 << 0)
55
56 #define SPI_TCR                 0x08
57 #define SPI_TCR_GET_TCNT(x)     (((x) & 0xffff0000) >> 16)
58
59 #define SPI_CTAR(x)             (0x0c + (((x) & 0x3) * 4))
60 #define SPI_CTAR_FMSZ(x)        (((x) & 0x0000000f) << 27)
61 #define SPI_CTAR_CPOL(x)        ((x) << 26)
62 #define SPI_CTAR_CPHA(x)        ((x) << 25)
63 #define SPI_CTAR_LSBFE(x)       ((x) << 24)
64 #define SPI_CTAR_PCSSCK(x)      (((x) & 0x00000003) << 22)
65 #define SPI_CTAR_PASC(x)        (((x) & 0x00000003) << 20)
66 #define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
67 #define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
68 #define SPI_CTAR_CSSCK(x)       (((x) & 0x0000000f) << 12)
69 #define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
70 #define SPI_CTAR_DT(x)          (((x) & 0x0000000f) << 4)
71 #define SPI_CTAR_BR(x)          ((x) & 0x0000000f)
72 #define SPI_CTAR_SCALE_BITS     0xf
73
74 #define SPI_CTAR0_SLAVE 0x0c
75
76 #define SPI_SR                  0x2c
77 #define SPI_SR_EOQF             0x10000000
78 #define SPI_SR_TCFQF            0x80000000
79 #define SPI_SR_CLEAR            0x9aaf0000
80
81 #define SPI_RSER_TFFFE          BIT(25)
82 #define SPI_RSER_TFFFD          BIT(24)
83 #define SPI_RSER_RFDFE          BIT(17)
84 #define SPI_RSER_RFDFD          BIT(16)
85
86 #define SPI_RSER                0x30
87 #define SPI_RSER_EOQFE          0x10000000
88 #define SPI_RSER_TCFQE          0x80000000
89
90 #define SPI_PUSHR               0x34
91 #define SPI_PUSHR_CONT          (1 << 31)
92 #define SPI_PUSHR_CTAS(x)       (((x) & 0x00000003) << 28)
93 #define SPI_PUSHR_EOQ           (1 << 27)
94 #define SPI_PUSHR_CTCNT (1 << 26)
95 #define SPI_PUSHR_PCS(x)        (((1 << x) & 0x0000003f) << 16)
96 #define SPI_PUSHR_TXDATA(x)     ((x) & 0x0000ffff)
97
98 #define SPI_PUSHR_SLAVE 0x34
99
100 #define SPI_POPR                0x38
101 #define SPI_POPR_RXDATA(x)      ((x) & 0x0000ffff)
102
103 #define SPI_TXFR0               0x3c
104 #define SPI_TXFR1               0x40
105 #define SPI_TXFR2               0x44
106 #define SPI_TXFR3               0x48
107 #define SPI_RXFR0               0x7c
108 #define SPI_RXFR1               0x80
109 #define SPI_RXFR2               0x84
110 #define SPI_RXFR3               0x88
111
112 #define SPI_FRAME_BITS(bits)    SPI_CTAR_FMSZ((bits) - 1)
113 #define SPI_FRAME_BITS_MASK     SPI_CTAR_FMSZ(0xf)
114 #define SPI_FRAME_BITS_16       SPI_CTAR_FMSZ(0xf)
115 #define SPI_FRAME_BITS_8        SPI_CTAR_FMSZ(0x7)
116
117 #define SPI_CS_INIT             0x01
118 #define SPI_CS_ASSERT           0x02
119 #define SPI_CS_DROP             0x04
120
121 #define SPI_TCR_TCNT_MAX        0x10000
122
123 #define DMA_COMPLETION_TIMEOUT  msecs_to_jiffies(3000)
124
125 struct chip_data {
126         u32 mcr_val;
127         u32 ctar_val;
128         u16 void_write_data;
129 };
130
131 enum dspi_trans_mode {
132         DSPI_EOQ_MODE = 0,
133         DSPI_TCFQ_MODE,
134         DSPI_DMA_MODE,
135 };
136
137 struct fsl_dspi_devtype_data {
138         enum dspi_trans_mode trans_mode;
139         u8 max_clock_factor;
140 };
141
142 static const struct fsl_dspi_devtype_data vf610_data = {
143         .trans_mode = DSPI_DMA_MODE,
144         .max_clock_factor = 2,
145 };
146
147 static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
148         .trans_mode = DSPI_TCFQ_MODE,
149         .max_clock_factor = 8,
150 };
151
152 static const struct fsl_dspi_devtype_data ls2085a_data = {
153         .trans_mode = DSPI_TCFQ_MODE,
154         .max_clock_factor = 8,
155 };
156
157 struct fsl_dspi_dma {
158         /* Length of transfer in words of DSPI_FIFO_SIZE */
159         u32 curr_xfer_len;
160
161         u32 *tx_dma_buf;
162         struct dma_chan *chan_tx;
163         dma_addr_t tx_dma_phys;
164         struct completion cmd_tx_complete;
165         struct dma_async_tx_descriptor *tx_desc;
166
167         u32 *rx_dma_buf;
168         struct dma_chan *chan_rx;
169         dma_addr_t rx_dma_phys;
170         struct completion cmd_rx_complete;
171         struct dma_async_tx_descriptor *rx_desc;
172 };
173
174 struct fsl_dspi {
175         struct spi_master       *master;
176         struct platform_device  *pdev;
177
178         struct regmap           *regmap;
179         int                     irq;
180         struct clk              *clk;
181
182         struct spi_transfer     *cur_transfer;
183         struct spi_message      *cur_msg;
184         struct chip_data        *cur_chip;
185         size_t                  len;
186         void                    *tx;
187         void                    *tx_end;
188         void                    *rx;
189         void                    *rx_end;
190         char                    dataflags;
191         u8                      cs;
192         u16                     void_write_data;
193         u32                     cs_change;
194         const struct fsl_dspi_devtype_data *devtype_data;
195
196         wait_queue_head_t       waitq;
197         u32                     waitflags;
198
199         u32                     spi_tcnt;
200         struct fsl_dspi_dma     *dma;
201 };
202
203 static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word);
204
205 static inline int is_double_byte_mode(struct fsl_dspi *dspi)
206 {
207         unsigned int val;
208
209         regmap_read(dspi->regmap, SPI_CTAR(0), &val);
210
211         return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
212 }
213
214 static void dspi_tx_dma_callback(void *arg)
215 {
216         struct fsl_dspi *dspi = arg;
217         struct fsl_dspi_dma *dma = dspi->dma;
218
219         complete(&dma->cmd_tx_complete);
220 }
221
222 static void dspi_rx_dma_callback(void *arg)
223 {
224         struct fsl_dspi *dspi = arg;
225         struct fsl_dspi_dma *dma = dspi->dma;
226         int rx_word;
227         int i;
228         u16 d;
229
230         rx_word = is_double_byte_mode(dspi);
231
232         if (!(dspi->dataflags & TRAN_STATE_RX_VOID)) {
233                 for (i = 0; i < dma->curr_xfer_len; i++) {
234                         d = dspi->dma->rx_dma_buf[i];
235                         rx_word ? (*(u16 *)dspi->rx = d) :
236                                                 (*(u8 *)dspi->rx = d);
237                         dspi->rx += rx_word + 1;
238                 }
239         }
240
241         complete(&dma->cmd_rx_complete);
242 }
243
244 static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
245 {
246         struct fsl_dspi_dma *dma = dspi->dma;
247         struct device *dev = &dspi->pdev->dev;
248         int time_left;
249         int tx_word;
250         int i;
251
252         tx_word = is_double_byte_mode(dspi);
253
254         for (i = 0; i < dma->curr_xfer_len; i++) {
255                 dspi->dma->tx_dma_buf[i] = dspi_data_to_pushr(dspi, tx_word);
256                 if ((dspi->cs_change) && (!dspi->len))
257                         dspi->dma->tx_dma_buf[i] &= ~SPI_PUSHR_CONT;
258         }
259
260         dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
261                                         dma->tx_dma_phys,
262                                         dma->curr_xfer_len *
263                                         DMA_SLAVE_BUSWIDTH_4_BYTES,
264                                         DMA_MEM_TO_DEV,
265                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
266         if (!dma->tx_desc) {
267                 dev_err(dev, "Not able to get desc for DMA xfer\n");
268                 return -EIO;
269         }
270
271         dma->tx_desc->callback = dspi_tx_dma_callback;
272         dma->tx_desc->callback_param = dspi;
273         if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
274                 dev_err(dev, "DMA submit failed\n");
275                 return -EINVAL;
276         }
277
278         dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
279                                         dma->rx_dma_phys,
280                                         dma->curr_xfer_len *
281                                         DMA_SLAVE_BUSWIDTH_4_BYTES,
282                                         DMA_DEV_TO_MEM,
283                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
284         if (!dma->rx_desc) {
285                 dev_err(dev, "Not able to get desc for DMA xfer\n");
286                 return -EIO;
287         }
288
289         dma->rx_desc->callback = dspi_rx_dma_callback;
290         dma->rx_desc->callback_param = dspi;
291         if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
292                 dev_err(dev, "DMA submit failed\n");
293                 return -EINVAL;
294         }
295
296         reinit_completion(&dspi->dma->cmd_rx_complete);
297         reinit_completion(&dspi->dma->cmd_tx_complete);
298
299         dma_async_issue_pending(dma->chan_rx);
300         dma_async_issue_pending(dma->chan_tx);
301
302         time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
303                                         DMA_COMPLETION_TIMEOUT);
304         if (time_left == 0) {
305                 dev_err(dev, "DMA tx timeout\n");
306                 dmaengine_terminate_all(dma->chan_tx);
307                 dmaengine_terminate_all(dma->chan_rx);
308                 return -ETIMEDOUT;
309         }
310
311         time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
312                                         DMA_COMPLETION_TIMEOUT);
313         if (time_left == 0) {
314                 dev_err(dev, "DMA rx timeout\n");
315                 dmaengine_terminate_all(dma->chan_tx);
316                 dmaengine_terminate_all(dma->chan_rx);
317                 return -ETIMEDOUT;
318         }
319
320         return 0;
321 }
322
323 static int dspi_dma_xfer(struct fsl_dspi *dspi)
324 {
325         struct fsl_dspi_dma *dma = dspi->dma;
326         struct device *dev = &dspi->pdev->dev;
327         int curr_remaining_bytes;
328         int bytes_per_buffer;
329         int word = 1;
330         int ret = 0;
331
332         if (is_double_byte_mode(dspi))
333                 word = 2;
334         curr_remaining_bytes = dspi->len;
335         bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
336         while (curr_remaining_bytes) {
337                 /* Check if current transfer fits the DMA buffer */
338                 dma->curr_xfer_len = curr_remaining_bytes / word;
339                 if (dma->curr_xfer_len > bytes_per_buffer)
340                         dma->curr_xfer_len = bytes_per_buffer;
341
342                 ret = dspi_next_xfer_dma_submit(dspi);
343                 if (ret) {
344                         dev_err(dev, "DMA transfer failed\n");
345                         goto exit;
346
347                 } else {
348                         curr_remaining_bytes -= dma->curr_xfer_len * word;
349                         if (curr_remaining_bytes < 0)
350                                 curr_remaining_bytes = 0;
351                 }
352         }
353
354 exit:
355         return ret;
356 }
357
358 static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
359 {
360         struct fsl_dspi_dma *dma;
361         struct dma_slave_config cfg;
362         struct device *dev = &dspi->pdev->dev;
363         int ret;
364
365         dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
366         if (!dma)
367                 return -ENOMEM;
368
369         dma->chan_rx = dma_request_slave_channel(dev, "rx");
370         if (!dma->chan_rx) {
371                 dev_err(dev, "rx dma channel not available\n");
372                 ret = -ENODEV;
373                 return ret;
374         }
375
376         dma->chan_tx = dma_request_slave_channel(dev, "tx");
377         if (!dma->chan_tx) {
378                 dev_err(dev, "tx dma channel not available\n");
379                 ret = -ENODEV;
380                 goto err_tx_channel;
381         }
382
383         dma->tx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
384                                         &dma->tx_dma_phys, GFP_KERNEL);
385         if (!dma->tx_dma_buf) {
386                 ret = -ENOMEM;
387                 goto err_tx_dma_buf;
388         }
389
390         dma->rx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
391                                         &dma->rx_dma_phys, GFP_KERNEL);
392         if (!dma->rx_dma_buf) {
393                 ret = -ENOMEM;
394                 goto err_rx_dma_buf;
395         }
396
397         memset(&cfg, 0, sizeof(cfg));
398         cfg.src_addr = phy_addr + SPI_POPR;
399         cfg.dst_addr = phy_addr + SPI_PUSHR;
400         cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
401         cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
402         cfg.src_maxburst = 1;
403         cfg.dst_maxburst = 1;
404
405         cfg.direction = DMA_DEV_TO_MEM;
406         ret = dmaengine_slave_config(dma->chan_rx, &cfg);
407         if (ret) {
408                 dev_err(dev, "can't configure rx dma channel\n");
409                 ret = -EINVAL;
410                 goto err_slave_config;
411         }
412
413         cfg.direction = DMA_MEM_TO_DEV;
414         ret = dmaengine_slave_config(dma->chan_tx, &cfg);
415         if (ret) {
416                 dev_err(dev, "can't configure tx dma channel\n");
417                 ret = -EINVAL;
418                 goto err_slave_config;
419         }
420
421         dspi->dma = dma;
422         init_completion(&dma->cmd_tx_complete);
423         init_completion(&dma->cmd_rx_complete);
424
425         return 0;
426
427 err_slave_config:
428         dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
429                         dma->rx_dma_buf, dma->rx_dma_phys);
430 err_rx_dma_buf:
431         dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
432                         dma->tx_dma_buf, dma->tx_dma_phys);
433 err_tx_dma_buf:
434         dma_release_channel(dma->chan_tx);
435 err_tx_channel:
436         dma_release_channel(dma->chan_rx);
437
438         devm_kfree(dev, dma);
439         dspi->dma = NULL;
440
441         return ret;
442 }
443
444 static void dspi_release_dma(struct fsl_dspi *dspi)
445 {
446         struct fsl_dspi_dma *dma = dspi->dma;
447         struct device *dev = &dspi->pdev->dev;
448
449         if (dma) {
450                 if (dma->chan_tx) {
451                         dma_unmap_single(dev, dma->tx_dma_phys,
452                                         DSPI_DMA_BUFSIZE, DMA_TO_DEVICE);
453                         dma_release_channel(dma->chan_tx);
454                 }
455
456                 if (dma->chan_rx) {
457                         dma_unmap_single(dev, dma->rx_dma_phys,
458                                         DSPI_DMA_BUFSIZE, DMA_FROM_DEVICE);
459                         dma_release_channel(dma->chan_rx);
460                 }
461         }
462 }
463
464 static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
465                 unsigned long clkrate)
466 {
467         /* Valid baud rate pre-scaler values */
468         int pbr_tbl[4] = {2, 3, 5, 7};
469         int brs[16] = { 2,      4,      6,      8,
470                 16,     32,     64,     128,
471                 256,    512,    1024,   2048,
472                 4096,   8192,   16384,  32768 };
473         int scale_needed, scale, minscale = INT_MAX;
474         int i, j;
475
476         scale_needed = clkrate / speed_hz;
477         if (clkrate % speed_hz)
478                 scale_needed++;
479
480         for (i = 0; i < ARRAY_SIZE(brs); i++)
481                 for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
482                         scale = brs[i] * pbr_tbl[j];
483                         if (scale >= scale_needed) {
484                                 if (scale < minscale) {
485                                         minscale = scale;
486                                         *br = i;
487                                         *pbr = j;
488                                 }
489                                 break;
490                         }
491                 }
492
493         if (minscale == INT_MAX) {
494                 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
495                         speed_hz, clkrate);
496                 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
497                 *br =  ARRAY_SIZE(brs) - 1;
498         }
499 }
500
501 static void ns_delay_scale(char *psc, char *sc, int delay_ns,
502                 unsigned long clkrate)
503 {
504         int pscale_tbl[4] = {1, 3, 5, 7};
505         int scale_needed, scale, minscale = INT_MAX;
506         int i, j;
507         u32 remainder;
508
509         scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
510                         &remainder);
511         if (remainder)
512                 scale_needed++;
513
514         for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
515                 for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
516                         scale = pscale_tbl[i] * (2 << j);
517                         if (scale >= scale_needed) {
518                                 if (scale < minscale) {
519                                         minscale = scale;
520                                         *psc = i;
521                                         *sc = j;
522                                 }
523                                 break;
524                         }
525                 }
526
527         if (minscale == INT_MAX) {
528                 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
529                         delay_ns, clkrate);
530                 *psc = ARRAY_SIZE(pscale_tbl) - 1;
531                 *sc = SPI_CTAR_SCALE_BITS;
532         }
533 }
534
535 static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word)
536 {
537         u16 d16;
538
539         if (!(dspi->dataflags & TRAN_STATE_TX_VOID))
540                 d16 = tx_word ? *(u16 *)dspi->tx : *(u8 *)dspi->tx;
541         else
542                 d16 = dspi->void_write_data;
543
544         dspi->tx += tx_word + 1;
545         dspi->len -= tx_word + 1;
546
547         return  SPI_PUSHR_TXDATA(d16) |
548                 SPI_PUSHR_PCS(dspi->cs) |
549                 SPI_PUSHR_CTAS(0) |
550                 SPI_PUSHR_CONT;
551 }
552
553 static void dspi_data_from_popr(struct fsl_dspi *dspi, int rx_word)
554 {
555         u16 d;
556         unsigned int val;
557
558         regmap_read(dspi->regmap, SPI_POPR, &val);
559         d = SPI_POPR_RXDATA(val);
560
561         if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
562                 rx_word ? (*(u16 *)dspi->rx = d) : (*(u8 *)dspi->rx = d);
563
564         dspi->rx += rx_word + 1;
565 }
566
567 static int dspi_eoq_write(struct fsl_dspi *dspi)
568 {
569         int tx_count = 0;
570         int tx_word;
571         u32 dspi_pushr = 0;
572
573         tx_word = is_double_byte_mode(dspi);
574
575         while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
576                 /* If we are in word mode, only have a single byte to transfer
577                  * switch to byte mode temporarily.  Will switch back at the
578                  * end of the transfer.
579                  */
580                 if (tx_word && (dspi->len == 1)) {
581                         dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
582                         regmap_update_bits(dspi->regmap, SPI_CTAR(0),
583                                         SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
584                         tx_word = 0;
585                 }
586
587                 dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
588
589                 if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
590                         /* last transfer in the transfer */
591                         dspi_pushr |= SPI_PUSHR_EOQ;
592                         if ((dspi->cs_change) && (!dspi->len))
593                                 dspi_pushr &= ~SPI_PUSHR_CONT;
594                 } else if (tx_word && (dspi->len == 1))
595                         dspi_pushr |= SPI_PUSHR_EOQ;
596
597                 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
598
599                 tx_count++;
600         }
601
602         return tx_count * (tx_word + 1);
603 }
604
605 static int dspi_eoq_read(struct fsl_dspi *dspi)
606 {
607         int rx_count = 0;
608         int rx_word = is_double_byte_mode(dspi);
609
610         while ((dspi->rx < dspi->rx_end)
611                         && (rx_count < DSPI_FIFO_SIZE)) {
612                 if (rx_word && (dspi->rx_end - dspi->rx) == 1)
613                         rx_word = 0;
614
615                 dspi_data_from_popr(dspi, rx_word);
616                 rx_count++;
617         }
618
619         return rx_count;
620 }
621
622 static int dspi_tcfq_write(struct fsl_dspi *dspi)
623 {
624         int tx_word;
625         u32 dspi_pushr = 0;
626
627         tx_word = is_double_byte_mode(dspi);
628
629         if (tx_word && (dspi->len == 1)) {
630                 dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
631                 regmap_update_bits(dspi->regmap, SPI_CTAR(0),
632                                 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
633                 tx_word = 0;
634         }
635
636         dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
637
638         if ((dspi->cs_change) && (!dspi->len))
639                 dspi_pushr &= ~SPI_PUSHR_CONT;
640
641         regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
642
643         return tx_word + 1;
644 }
645
646 static void dspi_tcfq_read(struct fsl_dspi *dspi)
647 {
648         int rx_word = is_double_byte_mode(dspi);
649
650         if (rx_word && (dspi->rx_end - dspi->rx) == 1)
651                 rx_word = 0;
652
653         dspi_data_from_popr(dspi, rx_word);
654 }
655
656 static int dspi_transfer_one_message(struct spi_master *master,
657                 struct spi_message *message)
658 {
659         struct fsl_dspi *dspi = spi_master_get_devdata(master);
660         struct spi_device *spi = message->spi;
661         struct spi_transfer *transfer;
662         int status = 0;
663         enum dspi_trans_mode trans_mode;
664         u32 spi_tcr;
665
666         regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
667         dspi->spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
668
669         message->actual_length = 0;
670
671         list_for_each_entry(transfer, &message->transfers, transfer_list) {
672                 dspi->cur_transfer = transfer;
673                 dspi->cur_msg = message;
674                 dspi->cur_chip = spi_get_ctldata(spi);
675                 dspi->cs = spi->chip_select;
676                 dspi->cs_change = 0;
677                 if (list_is_last(&dspi->cur_transfer->transfer_list,
678                                  &dspi->cur_msg->transfers) || transfer->cs_change)
679                         dspi->cs_change = 1;
680                 dspi->void_write_data = dspi->cur_chip->void_write_data;
681
682                 dspi->dataflags = 0;
683                 dspi->tx = (void *)transfer->tx_buf;
684                 dspi->tx_end = dspi->tx + transfer->len;
685                 dspi->rx = transfer->rx_buf;
686                 dspi->rx_end = dspi->rx + transfer->len;
687                 dspi->len = transfer->len;
688
689                 if (!dspi->rx)
690                         dspi->dataflags |= TRAN_STATE_RX_VOID;
691
692                 if (!dspi->tx)
693                         dspi->dataflags |= TRAN_STATE_TX_VOID;
694
695                 regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
696                 regmap_update_bits(dspi->regmap, SPI_MCR,
697                                 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
698                                 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
699                 regmap_write(dspi->regmap, SPI_CTAR(0),
700                                 dspi->cur_chip->ctar_val);
701
702                 trans_mode = dspi->devtype_data->trans_mode;
703                 switch (trans_mode) {
704                 case DSPI_EOQ_MODE:
705                         regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
706                         dspi_eoq_write(dspi);
707                         break;
708                 case DSPI_TCFQ_MODE:
709                         regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
710                         dspi_tcfq_write(dspi);
711                         break;
712                 case DSPI_DMA_MODE:
713                         regmap_write(dspi->regmap, SPI_RSER,
714                                 SPI_RSER_TFFFE | SPI_RSER_TFFFD |
715                                 SPI_RSER_RFDFE | SPI_RSER_RFDFD);
716                         status = dspi_dma_xfer(dspi);
717                         break;
718                 default:
719                         dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
720                                 trans_mode);
721                         status = -EINVAL;
722                         goto out;
723                 }
724
725                 if (trans_mode != DSPI_DMA_MODE) {
726                         if (wait_event_interruptible(dspi->waitq,
727                                                 dspi->waitflags))
728                                 dev_err(&dspi->pdev->dev,
729                                         "wait transfer complete fail!\n");
730                         dspi->waitflags = 0;
731                 }
732
733                 if (transfer->delay_usecs)
734                         udelay(transfer->delay_usecs);
735         }
736
737 out:
738         message->status = status;
739         spi_finalize_current_message(master);
740
741         return status;
742 }
743
744 static int dspi_setup(struct spi_device *spi)
745 {
746         struct chip_data *chip;
747         struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
748         u32 cs_sck_delay = 0, sck_cs_delay = 0;
749         unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
750         unsigned char pasc = 0, asc = 0, fmsz = 0;
751         unsigned long clkrate;
752
753         if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
754                 fmsz = spi->bits_per_word - 1;
755         } else {
756                 pr_err("Invalid wordsize\n");
757                 return -ENODEV;
758         }
759
760         /* Only alloc on first setup */
761         chip = spi_get_ctldata(spi);
762         if (chip == NULL) {
763                 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
764                 if (!chip)
765                         return -ENOMEM;
766         }
767
768         of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
769                         &cs_sck_delay);
770
771         of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
772                         &sck_cs_delay);
773
774         chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
775                 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
776
777         chip->void_write_data = 0;
778
779         clkrate = clk_get_rate(dspi->clk);
780         hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
781
782         /* Set PCS to SCK delay scale values */
783         ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
784
785         /* Set After SCK delay scale values */
786         ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
787
788         chip->ctar_val =  SPI_CTAR_FMSZ(fmsz)
789                 | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
790                 | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
791                 | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
792                 | SPI_CTAR_PCSSCK(pcssck)
793                 | SPI_CTAR_CSSCK(cssck)
794                 | SPI_CTAR_PASC(pasc)
795                 | SPI_CTAR_ASC(asc)
796                 | SPI_CTAR_PBR(pbr)
797                 | SPI_CTAR_BR(br);
798
799         spi_set_ctldata(spi, chip);
800
801         return 0;
802 }
803
804 static void dspi_cleanup(struct spi_device *spi)
805 {
806         struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
807
808         dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
809                         spi->master->bus_num, spi->chip_select);
810
811         kfree(chip);
812 }
813
814 static irqreturn_t dspi_interrupt(int irq, void *dev_id)
815 {
816         struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
817         struct spi_message *msg = dspi->cur_msg;
818         enum dspi_trans_mode trans_mode;
819         u32 spi_sr, spi_tcr;
820         u32 spi_tcnt, tcnt_diff;
821         int tx_word;
822
823         regmap_read(dspi->regmap, SPI_SR, &spi_sr);
824         regmap_write(dspi->regmap, SPI_SR, spi_sr);
825
826
827         if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
828                 tx_word = is_double_byte_mode(dspi);
829
830                 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
831                 spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
832                 /*
833                  * The width of SPI Transfer Counter in SPI_TCR is 16bits,
834                  * so the max couner is 65535. When the counter reach 65535,
835                  * it will wrap around, counter reset to zero.
836                  * spi_tcnt my be less than dspi->spi_tcnt, it means the
837                  * counter already wrapped around.
838                  * SPI Transfer Counter is a counter of transmitted frames.
839                  * The size of frame maybe two bytes.
840                  */
841                 tcnt_diff = ((spi_tcnt + SPI_TCR_TCNT_MAX) - dspi->spi_tcnt)
842                         % SPI_TCR_TCNT_MAX;
843                 tcnt_diff *= (tx_word + 1);
844                 if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
845                         tcnt_diff--;
846
847                 msg->actual_length += tcnt_diff;
848
849                 dspi->spi_tcnt = spi_tcnt;
850
851                 trans_mode = dspi->devtype_data->trans_mode;
852                 switch (trans_mode) {
853                 case DSPI_EOQ_MODE:
854                         dspi_eoq_read(dspi);
855                         break;
856                 case DSPI_TCFQ_MODE:
857                         dspi_tcfq_read(dspi);
858                         break;
859                 default:
860                         dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
861                                 trans_mode);
862                                 return IRQ_HANDLED;
863                 }
864
865                 if (!dspi->len) {
866                         if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) {
867                                 regmap_update_bits(dspi->regmap,
868                                                    SPI_CTAR(0),
869                                                    SPI_FRAME_BITS_MASK,
870                                                    SPI_FRAME_BITS(16));
871                                 dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM;
872                         }
873
874                         dspi->waitflags = 1;
875                         wake_up_interruptible(&dspi->waitq);
876                 } else {
877                         switch (trans_mode) {
878                         case DSPI_EOQ_MODE:
879                                 dspi_eoq_write(dspi);
880                                 break;
881                         case DSPI_TCFQ_MODE:
882                                 dspi_tcfq_write(dspi);
883                                 break;
884                         default:
885                                 dev_err(&dspi->pdev->dev,
886                                         "unsupported trans_mode %u\n",
887                                         trans_mode);
888                         }
889                 }
890
891                 return IRQ_HANDLED;
892         }
893
894         return IRQ_NONE;
895 }
896
897 static const struct of_device_id fsl_dspi_dt_ids[] = {
898         { .compatible = "fsl,vf610-dspi", .data = (void *)&vf610_data, },
899         { .compatible = "fsl,ls1021a-v1.0-dspi",
900                 .data = (void *)&ls1021a_v1_data, },
901         { .compatible = "fsl,ls2085a-dspi", .data = (void *)&ls2085a_data, },
902         { /* sentinel */ }
903 };
904 MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
905
906 #ifdef CONFIG_PM_SLEEP
907 static int dspi_suspend(struct device *dev)
908 {
909         struct spi_master *master = dev_get_drvdata(dev);
910         struct fsl_dspi *dspi = spi_master_get_devdata(master);
911
912         if (dspi->irq)
913                 disable_irq(dspi->irq);
914         spi_master_suspend(master);
915         clk_disable_unprepare(dspi->clk);
916
917         pinctrl_pm_select_sleep_state(dev);
918
919         return 0;
920 }
921
922 static int dspi_resume(struct device *dev)
923 {
924         struct spi_master *master = dev_get_drvdata(dev);
925         struct fsl_dspi *dspi = spi_master_get_devdata(master);
926         int ret;
927
928         pinctrl_pm_select_default_state(dev);
929
930         ret = clk_prepare_enable(dspi->clk);
931         if (ret)
932                 return ret;
933         spi_master_resume(master);
934         if (dspi->irq)
935                 enable_irq(dspi->irq);
936
937         return 0;
938 }
939 #endif /* CONFIG_PM_SLEEP */
940
941 static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
942
943 static const struct regmap_config dspi_regmap_config = {
944         .reg_bits = 32,
945         .val_bits = 32,
946         .reg_stride = 4,
947         .max_register = 0x88,
948 };
949
950 static void dspi_init(struct fsl_dspi *dspi)
951 {
952         regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
953 }
954
955 static int dspi_probe(struct platform_device *pdev)
956 {
957         struct device_node *np = pdev->dev.of_node;
958         struct spi_master *master;
959         struct fsl_dspi *dspi;
960         struct resource *res;
961         void __iomem *base;
962         int ret = 0, cs_num, bus_num;
963
964         master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
965         if (!master)
966                 return -ENOMEM;
967
968         dspi = spi_master_get_devdata(master);
969         dspi->pdev = pdev;
970         dspi->master = master;
971
972         master->transfer = NULL;
973         master->setup = dspi_setup;
974         master->transfer_one_message = dspi_transfer_one_message;
975         master->dev.of_node = pdev->dev.of_node;
976
977         master->cleanup = dspi_cleanup;
978         master->mode_bits = SPI_CPOL | SPI_CPHA;
979         master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
980                                         SPI_BPW_MASK(16);
981
982         ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
983         if (ret < 0) {
984                 dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
985                 goto out_master_put;
986         }
987         master->num_chipselect = cs_num;
988
989         ret = of_property_read_u32(np, "bus-num", &bus_num);
990         if (ret < 0) {
991                 dev_err(&pdev->dev, "can't get bus-num\n");
992                 goto out_master_put;
993         }
994         master->bus_num = bus_num;
995
996         dspi->devtype_data = of_device_get_match_data(&pdev->dev);
997         if (!dspi->devtype_data) {
998                 dev_err(&pdev->dev, "can't get devtype_data\n");
999                 ret = -EFAULT;
1000                 goto out_master_put;
1001         }
1002
1003         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1004         base = devm_ioremap_resource(&pdev->dev, res);
1005         if (IS_ERR(base)) {
1006                 ret = PTR_ERR(base);
1007                 goto out_master_put;
1008         }
1009
1010         dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
1011                                                 &dspi_regmap_config);
1012         if (IS_ERR(dspi->regmap)) {
1013                 dev_err(&pdev->dev, "failed to init regmap: %ld\n",
1014                                 PTR_ERR(dspi->regmap));
1015                 ret = PTR_ERR(dspi->regmap);
1016                 goto out_master_put;
1017         }
1018
1019         dspi->clk = devm_clk_get(&pdev->dev, "dspi");
1020         if (IS_ERR(dspi->clk)) {
1021                 ret = PTR_ERR(dspi->clk);
1022                 dev_err(&pdev->dev, "unable to get clock\n");
1023                 goto out_master_put;
1024         }
1025         ret = clk_prepare_enable(dspi->clk);
1026         if (ret)
1027                 goto out_master_put;
1028
1029         dspi_init(dspi);
1030         dspi->irq = platform_get_irq(pdev, 0);
1031         if (dspi->irq < 0) {
1032                 dev_err(&pdev->dev, "can't get platform irq\n");
1033                 ret = dspi->irq;
1034                 goto out_clk_put;
1035         }
1036
1037         ret = request_threaded_irq(dspi->irq, dspi_interrupt, NULL,
1038                                    IRQF_SHARED, pdev->name, dspi);
1039         if (ret < 0) {
1040                 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
1041                 goto out_clk_put;
1042         }
1043
1044         if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
1045                 ret = dspi_request_dma(dspi, res->start);
1046                 if (ret < 0) {
1047                         dev_err(&pdev->dev, "can't get dma channels\n");
1048                         goto out_free_irq;
1049                 }
1050         }
1051
1052         master->max_speed_hz =
1053                 clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
1054
1055         init_waitqueue_head(&dspi->waitq);
1056         platform_set_drvdata(pdev, master);
1057
1058         ret = spi_register_master(master);
1059         if (ret != 0) {
1060                 dev_err(&pdev->dev, "Problem registering DSPI master\n");
1061                 goto out_release_dma;
1062         }
1063
1064         return ret;
1065
1066 out_release_dma:
1067         dspi_release_dma(dspi);
1068 out_free_irq:
1069         if (dspi->irq)
1070                 free_irq(dspi->irq, dspi);
1071 out_clk_put:
1072         clk_disable_unprepare(dspi->clk);
1073 out_master_put:
1074         spi_master_put(master);
1075
1076         return ret;
1077 }
1078
1079 static int dspi_remove(struct platform_device *pdev)
1080 {
1081         struct spi_master *master = platform_get_drvdata(pdev);
1082         struct fsl_dspi *dspi = spi_master_get_devdata(master);
1083
1084         /* Disconnect from the SPI framework */
1085         spi_unregister_controller(dspi->master);
1086
1087         /* Disable RX and TX */
1088         regmap_update_bits(dspi->regmap, SPI_MCR,
1089                            SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF,
1090                            SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF);
1091
1092         /* Stop Running */
1093         regmap_update_bits(dspi->regmap, SPI_MCR, SPI_MCR_HALT, SPI_MCR_HALT);
1094
1095         dspi_release_dma(dspi);
1096         if (dspi->irq)
1097                 free_irq(dspi->irq, dspi);
1098         clk_disable_unprepare(dspi->clk);
1099
1100         return 0;
1101 }
1102
1103 static void dspi_shutdown(struct platform_device *pdev)
1104 {
1105         dspi_remove(pdev);
1106 }
1107
1108 static struct platform_driver fsl_dspi_driver = {
1109         .driver.name    = DRIVER_NAME,
1110         .driver.of_match_table = fsl_dspi_dt_ids,
1111         .driver.owner   = THIS_MODULE,
1112         .driver.pm = &dspi_pm,
1113         .probe          = dspi_probe,
1114         .remove         = dspi_remove,
1115         .shutdown       = dspi_shutdown,
1116 };
1117 module_platform_driver(fsl_dspi_driver);
1118
1119 MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
1120 MODULE_LICENSE("GPL");
1121 MODULE_ALIAS("platform:" DRIVER_NAME);