aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi-fsl-dspi.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2018-08-14 15:01:08 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-08-14 15:01:08 -0400
commit010b0e708e08727d38b82accb21832b63fe2c250 (patch)
tree29f110982a25675c02346320b2529ffd82aef244 /drivers/spi/spi-fsl-dspi.c
parent792adb90fa724ce07c0171cbc96b9215af4b1045 (diff)
parentc1acb21b32a3bb601453764c9eac9fc8fbb3a81d (diff)
Merge tag 'spi-v4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
Pull spi updates from Mark Brown: "Quite an active release for the SPI subsystem, lots of small updates and fixes scattered about with highlights including: - 3-wire support in the GPIO driver. - support for setting a custom memory name in the memory mapped flash drivers. - support for extended mode in the Freescale DSPI controller. - support for the non-standard integration with the Microsemi Ocelot platform in the DesignWare driver. - new driver for the SocioNext UniPhier" * tag 'spi-v4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi: (47 commits) spi: davinci: fix a NULL pointer dereference spi: spi-mem: Constify spi_mem->name mtd: m25p80: Call spi_mem_get_name() to let controller set a custom name spi: spi-mem: Extend the SPI mem interface to set a custom memory name spi: spi-mem: Fix a typo in the documentation of struct spi_mem spi: uniphier: remove unnecessary include headers spi: spi-gpio: add SPI_3WIRE support spi: add flags parameter to txrx_word function pointers spi: add SPI controller driver for UniPhier SoC spi: add DT bindings for UniPhier SPI controller spi: dw: document Microsemi integration spi: img-spfi: Set device select bits for SPFI port state spi: omap2-mcspi: remove several redundant variables spi: dw-mmio: add MSCC Ocelot support spi: dw: export dw_spi_set_cs spi: spi-fsl-espi: Log fifo counters on error spi: imx: Use the longuest possible burst size when in dynamic_burst spi: imx: remove unnecessary check in spi_imx_can_dma spi: imx: Use correct number of bytes per words spi: imx: Use dynamic bursts only when bits_per_word is 8, 16 or 32 ...
Diffstat (limited to 'drivers/spi/spi-fsl-dspi.c')
-rw-r--r--drivers/spi/spi-fsl-dspi.c505
1 files changed, 271 insertions, 234 deletions
diff --git a/drivers/spi/spi-fsl-dspi.c b/drivers/spi/spi-fsl-dspi.c
index 0630962ce442..7cb3ab0a35a0 100644
--- a/drivers/spi/spi-fsl-dspi.c
+++ b/drivers/spi/spi-fsl-dspi.c
@@ -1,17 +1,9 @@
1/* 1// SPDX-License-Identifier: GPL-2.0+
2 * drivers/spi/spi-fsl-dspi.c 2//
3 * 3// Copyright 2013 Freescale Semiconductor, Inc.
4 * Copyright 2013 Freescale Semiconductor, Inc. 4//
5 * 5// Freescale DSPI driver
6 * Freescale DSPI driver 6// This file contains a driver for the Freescale DSPI
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 7
16#include <linux/clk.h> 8#include <linux/clk.h>
17#include <linux/delay.h> 9#include <linux/delay.h>
@@ -38,10 +30,6 @@
38 30
39#define DRIVER_NAME "fsl-dspi" 31#define DRIVER_NAME "fsl-dspi"
40 32
41#define TRAN_STATE_RX_VOID 0x01
42#define TRAN_STATE_TX_VOID 0x02
43#define TRAN_STATE_WORD_ODD_NUM 0x04
44
45#define DSPI_FIFO_SIZE 4 33#define DSPI_FIFO_SIZE 4
46#define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024) 34#define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
47 35
@@ -50,6 +38,7 @@
50#define SPI_MCR_PCSIS (0x3F << 16) 38#define SPI_MCR_PCSIS (0x3F << 16)
51#define SPI_MCR_CLR_TXF (1 << 11) 39#define SPI_MCR_CLR_TXF (1 << 11)
52#define SPI_MCR_CLR_RXF (1 << 10) 40#define SPI_MCR_CLR_RXF (1 << 10)
41#define SPI_MCR_XSPI (1 << 3)
53 42
54#define SPI_TCR 0x08 43#define SPI_TCR 0x08
55#define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16) 44#define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
@@ -86,11 +75,16 @@
86#define SPI_RSER_TCFQE 0x80000000 75#define SPI_RSER_TCFQE 0x80000000
87 76
88#define SPI_PUSHR 0x34 77#define SPI_PUSHR 0x34
89#define SPI_PUSHR_CONT (1 << 31) 78#define SPI_PUSHR_CMD_CONT (1 << 15)
90#define SPI_PUSHR_CTAS(x) (((x) & 0x00000003) << 28) 79#define SPI_PUSHR_CONT (SPI_PUSHR_CMD_CONT << 16)
91#define SPI_PUSHR_EOQ (1 << 27) 80#define SPI_PUSHR_CMD_CTAS(x) (((x) & 0x0003) << 12)
92#define SPI_PUSHR_CTCNT (1 << 26) 81#define SPI_PUSHR_CTAS(x) (SPI_PUSHR_CMD_CTAS(x) << 16)
93#define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16) 82#define SPI_PUSHR_CMD_EOQ (1 << 11)
83#define SPI_PUSHR_EOQ (SPI_PUSHR_CMD_EOQ << 16)
84#define SPI_PUSHR_CMD_CTCNT (1 << 10)
85#define SPI_PUSHR_CTCNT (SPI_PUSHR_CMD_CTCNT << 16)
86#define SPI_PUSHR_CMD_PCS(x) ((1 << x) & 0x003f)
87#define SPI_PUSHR_PCS(x) (SPI_PUSHR_CMD_PCS(x) << 16)
94#define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff) 88#define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
95 89
96#define SPI_PUSHR_SLAVE 0x34 90#define SPI_PUSHR_SLAVE 0x34
@@ -107,21 +101,31 @@
107#define SPI_RXFR2 0x84 101#define SPI_RXFR2 0x84
108#define SPI_RXFR3 0x88 102#define SPI_RXFR3 0x88
109 103
104#define SPI_CTARE(x) (0x11c + (((x) & 0x3) * 4))
105#define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16)
106#define SPI_CTARE_DTCP(x) ((x) & 0x7ff)
107
108#define SPI_SREX 0x13c
109
110#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1) 110#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
111#define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf) 111#define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
112#define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf) 112#define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
113#define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7) 113#define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
114 114
115#define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4)
116#define SPI_FRAME_EBITS_MASK SPI_CTARE_FMSZE(1)
117
118/* Register offsets for regmap_pushr */
119#define PUSHR_CMD 0x0
120#define PUSHR_TX 0x2
121
115#define SPI_CS_INIT 0x01 122#define SPI_CS_INIT 0x01
116#define SPI_CS_ASSERT 0x02 123#define SPI_CS_ASSERT 0x02
117#define SPI_CS_DROP 0x04 124#define SPI_CS_DROP 0x04
118 125
119#define SPI_TCR_TCNT_MAX 0x10000
120
121#define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000) 126#define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
122 127
123struct chip_data { 128struct chip_data {
124 u32 mcr_val;
125 u32 ctar_val; 129 u32 ctar_val;
126 u16 void_write_data; 130 u16 void_write_data;
127}; 131};
@@ -135,6 +139,7 @@ enum dspi_trans_mode {
135struct fsl_dspi_devtype_data { 139struct fsl_dspi_devtype_data {
136 enum dspi_trans_mode trans_mode; 140 enum dspi_trans_mode trans_mode;
137 u8 max_clock_factor; 141 u8 max_clock_factor;
142 bool xspi_mode;
138}; 143};
139 144
140static const struct fsl_dspi_devtype_data vf610_data = { 145static const struct fsl_dspi_devtype_data vf610_data = {
@@ -145,6 +150,7 @@ static const struct fsl_dspi_devtype_data vf610_data = {
145static const struct fsl_dspi_devtype_data ls1021a_v1_data = { 150static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
146 .trans_mode = DSPI_TCFQ_MODE, 151 .trans_mode = DSPI_TCFQ_MODE,
147 .max_clock_factor = 8, 152 .max_clock_factor = 8,
153 .xspi_mode = true,
148}; 154};
149 155
150static const struct fsl_dspi_devtype_data ls2085a_data = { 156static const struct fsl_dspi_devtype_data ls2085a_data = {
@@ -179,6 +185,7 @@ struct fsl_dspi {
179 struct platform_device *pdev; 185 struct platform_device *pdev;
180 186
181 struct regmap *regmap; 187 struct regmap *regmap;
188 struct regmap *regmap_pushr;
182 int irq; 189 int irq;
183 struct clk *clk; 190 struct clk *clk;
184 191
@@ -186,32 +193,62 @@ struct fsl_dspi {
186 struct spi_message *cur_msg; 193 struct spi_message *cur_msg;
187 struct chip_data *cur_chip; 194 struct chip_data *cur_chip;
188 size_t len; 195 size_t len;
189 void *tx; 196 const void *tx;
190 void *tx_end;
191 void *rx; 197 void *rx;
192 void *rx_end; 198 void *rx_end;
193 char dataflags;
194 u8 cs;
195 u16 void_write_data; 199 u16 void_write_data;
196 u32 cs_change; 200 u16 tx_cmd;
201 u8 bits_per_word;
202 u8 bytes_per_word;
197 const struct fsl_dspi_devtype_data *devtype_data; 203 const struct fsl_dspi_devtype_data *devtype_data;
198 204
199 wait_queue_head_t waitq; 205 wait_queue_head_t waitq;
200 u32 waitflags; 206 u32 waitflags;
201 207
202 u32 spi_tcnt;
203 struct fsl_dspi_dma *dma; 208 struct fsl_dspi_dma *dma;
204}; 209};
205 210
206static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word); 211static u32 dspi_pop_tx(struct fsl_dspi *dspi)
212{
213 u32 txdata = 0;
214
215 if (dspi->tx) {
216 if (dspi->bytes_per_word == 1)
217 txdata = *(u8 *)dspi->tx;
218 else if (dspi->bytes_per_word == 2)
219 txdata = *(u16 *)dspi->tx;
220 else /* dspi->bytes_per_word == 4 */
221 txdata = *(u32 *)dspi->tx;
222 dspi->tx += dspi->bytes_per_word;
223 }
224 dspi->len -= dspi->bytes_per_word;
225 return txdata;
226}
207 227
208static inline int is_double_byte_mode(struct fsl_dspi *dspi) 228static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi)
209{ 229{
210 unsigned int val; 230 u16 cmd = dspi->tx_cmd, data = dspi_pop_tx(dspi);
211 231
212 regmap_read(dspi->regmap, SPI_CTAR(0), &val); 232 if (dspi->len > 0)
233 cmd |= SPI_PUSHR_CMD_CONT;
234 return cmd << 16 | data;
235}
213 236
214 return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1; 237static void dspi_push_rx(struct fsl_dspi *dspi, u32 rxdata)
238{
239 if (!dspi->rx)
240 return;
241
242 /* Mask of undefined bits */
243 rxdata &= (1 << dspi->bits_per_word) - 1;
244
245 if (dspi->bytes_per_word == 1)
246 *(u8 *)dspi->rx = rxdata;
247 else if (dspi->bytes_per_word == 2)
248 *(u16 *)dspi->rx = rxdata;
249 else /* dspi->bytes_per_word == 4 */
250 *(u32 *)dspi->rx = rxdata;
251 dspi->rx += dspi->bytes_per_word;
215} 252}
216 253
217static void dspi_tx_dma_callback(void *arg) 254static void dspi_tx_dma_callback(void *arg)
@@ -226,19 +263,11 @@ static void dspi_rx_dma_callback(void *arg)
226{ 263{
227 struct fsl_dspi *dspi = arg; 264 struct fsl_dspi *dspi = arg;
228 struct fsl_dspi_dma *dma = dspi->dma; 265 struct fsl_dspi_dma *dma = dspi->dma;
229 int rx_word;
230 int i; 266 int i;
231 u16 d;
232
233 rx_word = is_double_byte_mode(dspi);
234 267
235 if (!(dspi->dataflags & TRAN_STATE_RX_VOID)) { 268 if (dspi->rx) {
236 for (i = 0; i < dma->curr_xfer_len; i++) { 269 for (i = 0; i < dma->curr_xfer_len; i++)
237 d = dspi->dma->rx_dma_buf[i]; 270 dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
238 rx_word ? (*(u16 *)dspi->rx = d) :
239 (*(u8 *)dspi->rx = d);
240 dspi->rx += rx_word + 1;
241 }
242 } 271 }
243 272
244 complete(&dma->cmd_rx_complete); 273 complete(&dma->cmd_rx_complete);
@@ -249,16 +278,10 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
249 struct fsl_dspi_dma *dma = dspi->dma; 278 struct fsl_dspi_dma *dma = dspi->dma;
250 struct device *dev = &dspi->pdev->dev; 279 struct device *dev = &dspi->pdev->dev;
251 int time_left; 280 int time_left;
252 int tx_word;
253 int i; 281 int i;
254 282
255 tx_word = is_double_byte_mode(dspi); 283 for (i = 0; i < dma->curr_xfer_len; i++)
256 284 dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);
257 for (i = 0; i < dma->curr_xfer_len; i++) {
258 dspi->dma->tx_dma_buf[i] = dspi_data_to_pushr(dspi, tx_word);
259 if ((dspi->cs_change) && (!dspi->len))
260 dspi->dma->tx_dma_buf[i] &= ~SPI_PUSHR_CONT;
261 }
262 285
263 dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx, 286 dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
264 dma->tx_dma_phys, 287 dma->tx_dma_phys,
@@ -327,18 +350,17 @@ static int dspi_dma_xfer(struct fsl_dspi *dspi)
327{ 350{
328 struct fsl_dspi_dma *dma = dspi->dma; 351 struct fsl_dspi_dma *dma = dspi->dma;
329 struct device *dev = &dspi->pdev->dev; 352 struct device *dev = &dspi->pdev->dev;
353 struct spi_message *message = dspi->cur_msg;
330 int curr_remaining_bytes; 354 int curr_remaining_bytes;
331 int bytes_per_buffer; 355 int bytes_per_buffer;
332 int word = 1;
333 int ret = 0; 356 int ret = 0;
334 357
335 if (is_double_byte_mode(dspi))
336 word = 2;
337 curr_remaining_bytes = dspi->len; 358 curr_remaining_bytes = dspi->len;
338 bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE; 359 bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
339 while (curr_remaining_bytes) { 360 while (curr_remaining_bytes) {
340 /* Check if current transfer fits the DMA buffer */ 361 /* Check if current transfer fits the DMA buffer */
341 dma->curr_xfer_len = curr_remaining_bytes / word; 362 dma->curr_xfer_len = curr_remaining_bytes
363 / dspi->bytes_per_word;
342 if (dma->curr_xfer_len > bytes_per_buffer) 364 if (dma->curr_xfer_len > bytes_per_buffer)
343 dma->curr_xfer_len = bytes_per_buffer; 365 dma->curr_xfer_len = bytes_per_buffer;
344 366
@@ -348,7 +370,10 @@ static int dspi_dma_xfer(struct fsl_dspi *dspi)
348 goto exit; 370 goto exit;
349 371
350 } else { 372 } else {
351 curr_remaining_bytes -= dma->curr_xfer_len * word; 373 const int len =
374 dma->curr_xfer_len * dspi->bytes_per_word;
375 curr_remaining_bytes -= len;
376 message->actual_length += len;
352 if (curr_remaining_bytes < 0) 377 if (curr_remaining_bytes < 0)
353 curr_remaining_bytes = 0; 378 curr_remaining_bytes = 0;
354 } 379 }
@@ -534,125 +559,91 @@ static void ns_delay_scale(char *psc, char *sc, int delay_ns,
534 } 559 }
535} 560}
536 561
537static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word) 562static void fifo_write(struct fsl_dspi *dspi)
538{ 563{
539 u16 d16; 564 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pop_tx_pushr(dspi));
540
541 if (!(dspi->dataflags & TRAN_STATE_TX_VOID))
542 d16 = tx_word ? *(u16 *)dspi->tx : *(u8 *)dspi->tx;
543 else
544 d16 = dspi->void_write_data;
545
546 dspi->tx += tx_word + 1;
547 dspi->len -= tx_word + 1;
548
549 return SPI_PUSHR_TXDATA(d16) |
550 SPI_PUSHR_PCS(dspi->cs) |
551 SPI_PUSHR_CTAS(0) |
552 SPI_PUSHR_CONT;
553} 565}
554 566
555static void dspi_data_from_popr(struct fsl_dspi *dspi, int rx_word) 567static void cmd_fifo_write(struct fsl_dspi *dspi)
556{ 568{
557 u16 d; 569 u16 cmd = dspi->tx_cmd;
558 unsigned int val;
559
560 regmap_read(dspi->regmap, SPI_POPR, &val);
561 d = SPI_POPR_RXDATA(val);
562 570
563 if (!(dspi->dataflags & TRAN_STATE_RX_VOID)) 571 if (dspi->len > 0)
564 rx_word ? (*(u16 *)dspi->rx = d) : (*(u8 *)dspi->rx = d); 572 cmd |= SPI_PUSHR_CMD_CONT;
565 573 regmap_write(dspi->regmap_pushr, PUSHR_CMD, cmd);
566 dspi->rx += rx_word + 1;
567} 574}
568 575
569static int dspi_eoq_write(struct fsl_dspi *dspi) 576static void tx_fifo_write(struct fsl_dspi *dspi, u16 txdata)
570{ 577{
571 int tx_count = 0; 578 regmap_write(dspi->regmap_pushr, PUSHR_TX, txdata);
572 int tx_word; 579}
573 u32 dspi_pushr = 0;
574 580
575 tx_word = is_double_byte_mode(dspi); 581static void dspi_tcfq_write(struct fsl_dspi *dspi)
582{
583 /* Clear transfer count */
584 dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
576 585
577 while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) { 586 if (dspi->devtype_data->xspi_mode && dspi->bits_per_word > 16) {
578 /* If we are in word mode, only have a single byte to transfer 587 /* Write two TX FIFO entries first, and then the corresponding
579 * switch to byte mode temporarily. Will switch back at the 588 * CMD FIFO entry.
580 * end of the transfer.
581 */ 589 */
582 if (tx_word && (dspi->len == 1)) { 590 u32 data = dspi_pop_tx(dspi);
583 dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
584 regmap_update_bits(dspi->regmap, SPI_CTAR(0),
585 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
586 tx_word = 0;
587 }
588
589 dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
590
591 if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
592 /* last transfer in the transfer */
593 dspi_pushr |= SPI_PUSHR_EOQ;
594 if ((dspi->cs_change) && (!dspi->len))
595 dspi_pushr &= ~SPI_PUSHR_CONT;
596 } else if (tx_word && (dspi->len == 1))
597 dspi_pushr |= SPI_PUSHR_EOQ;
598 591
599 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr); 592 if (dspi->cur_chip->ctar_val & SPI_CTAR_LSBFE(1)) {
600 593 /* LSB */
601 tx_count++; 594 tx_fifo_write(dspi, data & 0xFFFF);
595 tx_fifo_write(dspi, data >> 16);
596 } else {
597 /* MSB */
598 tx_fifo_write(dspi, data >> 16);
599 tx_fifo_write(dspi, data & 0xFFFF);
600 }
601 cmd_fifo_write(dspi);
602 } else {
603 /* Write one entry to both TX FIFO and CMD FIFO
604 * simultaneously.
605 */
606 fifo_write(dspi);
602 } 607 }
603
604 return tx_count * (tx_word + 1);
605} 608}
606 609
607static int dspi_eoq_read(struct fsl_dspi *dspi) 610static u32 fifo_read(struct fsl_dspi *dspi)
608{ 611{
609 int rx_count = 0; 612 u32 rxdata = 0;
610 int rx_word = is_double_byte_mode(dspi);
611
612 while ((dspi->rx < dspi->rx_end)
613 && (rx_count < DSPI_FIFO_SIZE)) {
614 if (rx_word && (dspi->rx_end - dspi->rx) == 1)
615 rx_word = 0;
616 613
617 dspi_data_from_popr(dspi, rx_word); 614 regmap_read(dspi->regmap, SPI_POPR, &rxdata);
618 rx_count++; 615 return rxdata;
619 }
620
621 return rx_count;
622} 616}
623 617
624static int dspi_tcfq_write(struct fsl_dspi *dspi) 618static void dspi_tcfq_read(struct fsl_dspi *dspi)
625{ 619{
626 int tx_word; 620 dspi_push_rx(dspi, fifo_read(dspi));
627 u32 dspi_pushr = 0; 621}
628
629 tx_word = is_double_byte_mode(dspi);
630 622
631 if (tx_word && (dspi->len == 1)) { 623static void dspi_eoq_write(struct fsl_dspi *dspi)
632 dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM; 624{
633 regmap_update_bits(dspi->regmap, SPI_CTAR(0), 625 int fifo_size = DSPI_FIFO_SIZE;
634 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8)); 626
635 tx_word = 0; 627 /* Fill TX FIFO with as many transfers as possible */
628 while (dspi->len && fifo_size--) {
629 /* Request EOQF for last transfer in FIFO */
630 if (dspi->len == dspi->bytes_per_word || fifo_size == 0)
631 dspi->tx_cmd |= SPI_PUSHR_CMD_EOQ;
632 /* Clear transfer count for first transfer in FIFO */
633 if (fifo_size == (DSPI_FIFO_SIZE - 1))
634 dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
635 /* Write combined TX FIFO and CMD FIFO entry */
636 fifo_write(dspi);
636 } 637 }
637
638 dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
639
640 if ((dspi->cs_change) && (!dspi->len))
641 dspi_pushr &= ~SPI_PUSHR_CONT;
642
643 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
644
645 return tx_word + 1;
646} 638}
647 639
648static void dspi_tcfq_read(struct fsl_dspi *dspi) 640static void dspi_eoq_read(struct fsl_dspi *dspi)
649{ 641{
650 int rx_word = is_double_byte_mode(dspi); 642 int fifo_size = DSPI_FIFO_SIZE;
651
652 if (rx_word && (dspi->rx_end - dspi->rx) == 1)
653 rx_word = 0;
654 643
655 dspi_data_from_popr(dspi, rx_word); 644 /* Read one FIFO entry at and push to rx buffer */
645 while ((dspi->rx < dspi->rx_end) && fifo_size--)
646 dspi_push_rx(dspi, fifo_read(dspi));
656} 647}
657 648
658static int dspi_transfer_one_message(struct spi_master *master, 649static int dspi_transfer_one_message(struct spi_master *master,
@@ -663,10 +654,6 @@ static int dspi_transfer_one_message(struct spi_master *master,
663 struct spi_transfer *transfer; 654 struct spi_transfer *transfer;
664 int status = 0; 655 int status = 0;
665 enum dspi_trans_mode trans_mode; 656 enum dspi_trans_mode trans_mode;
666 u32 spi_tcr;
667
668 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
669 dspi->spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
670 657
671 message->actual_length = 0; 658 message->actual_length = 0;
672 659
@@ -674,32 +661,51 @@ static int dspi_transfer_one_message(struct spi_master *master,
674 dspi->cur_transfer = transfer; 661 dspi->cur_transfer = transfer;
675 dspi->cur_msg = message; 662 dspi->cur_msg = message;
676 dspi->cur_chip = spi_get_ctldata(spi); 663 dspi->cur_chip = spi_get_ctldata(spi);
677 dspi->cs = spi->chip_select; 664 /* Prepare command word for CMD FIFO */
678 dspi->cs_change = 0; 665 dspi->tx_cmd = SPI_PUSHR_CMD_CTAS(0) |
666 SPI_PUSHR_CMD_PCS(spi->chip_select);
679 if (list_is_last(&dspi->cur_transfer->transfer_list, 667 if (list_is_last(&dspi->cur_transfer->transfer_list,
680 &dspi->cur_msg->transfers) || transfer->cs_change) 668 &dspi->cur_msg->transfers)) {
681 dspi->cs_change = 1; 669 /* Leave PCS activated after last transfer when
670 * cs_change is set.
671 */
672 if (transfer->cs_change)
673 dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
674 } else {
675 /* Keep PCS active between transfers in same message
676 * when cs_change is not set, and de-activate PCS
677 * between transfers in the same message when
678 * cs_change is set.
679 */
680 if (!transfer->cs_change)
681 dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
682 }
683
682 dspi->void_write_data = dspi->cur_chip->void_write_data; 684 dspi->void_write_data = dspi->cur_chip->void_write_data;
683 685
684 dspi->dataflags = 0; 686 dspi->tx = transfer->tx_buf;
685 dspi->tx = (void *)transfer->tx_buf;
686 dspi->tx_end = dspi->tx + transfer->len;
687 dspi->rx = transfer->rx_buf; 687 dspi->rx = transfer->rx_buf;
688 dspi->rx_end = dspi->rx + transfer->len; 688 dspi->rx_end = dspi->rx + transfer->len;
689 dspi->len = transfer->len; 689 dspi->len = transfer->len;
690 /* Validated transfer specific frame size (defaults applied) */
691 dspi->bits_per_word = transfer->bits_per_word;
692 if (transfer->bits_per_word <= 8)
693 dspi->bytes_per_word = 1;
694 else if (transfer->bits_per_word <= 16)
695 dspi->bytes_per_word = 2;
696 else
697 dspi->bytes_per_word = 4;
690 698
691 if (!dspi->rx)
692 dspi->dataflags |= TRAN_STATE_RX_VOID;
693
694 if (!dspi->tx)
695 dspi->dataflags |= TRAN_STATE_TX_VOID;
696
697 regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
698 regmap_update_bits(dspi->regmap, SPI_MCR, 699 regmap_update_bits(dspi->regmap, SPI_MCR,
699 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF, 700 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
700 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF); 701 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
701 regmap_write(dspi->regmap, SPI_CTAR(0), 702 regmap_write(dspi->regmap, SPI_CTAR(0),
702 dspi->cur_chip->ctar_val); 703 dspi->cur_chip->ctar_val |
704 SPI_FRAME_BITS(transfer->bits_per_word));
705 if (dspi->devtype_data->xspi_mode)
706 regmap_write(dspi->regmap, SPI_CTARE(0),
707 SPI_FRAME_EBITS(transfer->bits_per_word)
708 | SPI_CTARE_DTCP(1));
703 709
704 trans_mode = dspi->devtype_data->trans_mode; 710 trans_mode = dspi->devtype_data->trans_mode;
705 switch (trans_mode) { 711 switch (trans_mode) {
@@ -750,16 +756,9 @@ static int dspi_setup(struct spi_device *spi)
750 struct fsl_dspi_platform_data *pdata; 756 struct fsl_dspi_platform_data *pdata;
751 u32 cs_sck_delay = 0, sck_cs_delay = 0; 757 u32 cs_sck_delay = 0, sck_cs_delay = 0;
752 unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0; 758 unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
753 unsigned char pasc = 0, asc = 0, fmsz = 0; 759 unsigned char pasc = 0, asc = 0;
754 unsigned long clkrate; 760 unsigned long clkrate;
755 761
756 if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
757 fmsz = spi->bits_per_word - 1;
758 } else {
759 pr_err("Invalid wordsize\n");
760 return -ENODEV;
761 }
762
763 /* Only alloc on first setup */ 762 /* Only alloc on first setup */
764 chip = spi_get_ctldata(spi); 763 chip = spi_get_ctldata(spi);
765 if (chip == NULL) { 764 if (chip == NULL) {
@@ -781,9 +780,6 @@ static int dspi_setup(struct spi_device *spi)
781 sck_cs_delay = pdata->sck_cs_delay; 780 sck_cs_delay = pdata->sck_cs_delay;
782 } 781 }
783 782
784 chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
785 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
786
787 chip->void_write_data = 0; 783 chip->void_write_data = 0;
788 784
789 clkrate = clk_get_rate(dspi->clk); 785 clkrate = clk_get_rate(dspi->clk);
@@ -795,8 +791,7 @@ static int dspi_setup(struct spi_device *spi)
795 /* Set After SCK delay scale values */ 791 /* Set After SCK delay scale values */
796 ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate); 792 ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
797 793
798 chip->ctar_val = SPI_CTAR_FMSZ(fmsz) 794 chip->ctar_val = SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
799 | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
800 | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0) 795 | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
801 | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0) 796 | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
802 | SPI_CTAR_PCSSCK(pcssck) 797 | SPI_CTAR_PCSSCK(pcssck)
@@ -827,36 +822,20 @@ static irqreturn_t dspi_interrupt(int irq, void *dev_id)
827 struct spi_message *msg = dspi->cur_msg; 822 struct spi_message *msg = dspi->cur_msg;
828 enum dspi_trans_mode trans_mode; 823 enum dspi_trans_mode trans_mode;
829 u32 spi_sr, spi_tcr; 824 u32 spi_sr, spi_tcr;
830 u32 spi_tcnt, tcnt_diff; 825 u16 spi_tcnt;
831 int tx_word;
832 826
833 regmap_read(dspi->regmap, SPI_SR, &spi_sr); 827 regmap_read(dspi->regmap, SPI_SR, &spi_sr);
834 regmap_write(dspi->regmap, SPI_SR, spi_sr); 828 regmap_write(dspi->regmap, SPI_SR, spi_sr);
835 829
836 830
837 if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) { 831 if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
838 tx_word = is_double_byte_mode(dspi); 832 /* Get transfer counter (in number of SPI transfers). It was
839 833 * reset to 0 when transfer(s) were started.
834 */
840 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr); 835 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
841 spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr); 836 spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
842 /* 837 /* Update total number of bytes that were transferred */
843 * The width of SPI Transfer Counter in SPI_TCR is 16bits, 838 msg->actual_length += spi_tcnt * dspi->bytes_per_word;
844 * so the max couner is 65535. When the counter reach 65535,
845 * it will wrap around, counter reset to zero.
846 * spi_tcnt my be less than dspi->spi_tcnt, it means the
847 * counter already wrapped around.
848 * SPI Transfer Counter is a counter of transmitted frames.
849 * The size of frame maybe two bytes.
850 */
851 tcnt_diff = ((spi_tcnt + SPI_TCR_TCNT_MAX) - dspi->spi_tcnt)
852 % SPI_TCR_TCNT_MAX;
853 tcnt_diff *= (tx_word + 1);
854 if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
855 tcnt_diff--;
856
857 msg->actual_length += tcnt_diff;
858
859 dspi->spi_tcnt = spi_tcnt;
860 839
861 trans_mode = dspi->devtype_data->trans_mode; 840 trans_mode = dspi->devtype_data->trans_mode;
862 switch (trans_mode) { 841 switch (trans_mode) {
@@ -873,14 +852,6 @@ static irqreturn_t dspi_interrupt(int irq, void *dev_id)
873 } 852 }
874 853
875 if (!dspi->len) { 854 if (!dspi->len) {
876 if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) {
877 regmap_update_bits(dspi->regmap,
878 SPI_CTAR(0),
879 SPI_FRAME_BITS_MASK,
880 SPI_FRAME_BITS(16));
881 dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM;
882 }
883
884 dspi->waitflags = 1; 855 dspi->waitflags = 1;
885 wake_up_interruptible(&dspi->waitq); 856 wake_up_interruptible(&dspi->waitq);
886 } else { 857 } else {
@@ -943,16 +914,62 @@ static int dspi_resume(struct device *dev)
943 914
944static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume); 915static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
945 916
917static const struct regmap_range dspi_volatile_ranges[] = {
918 regmap_reg_range(SPI_MCR, SPI_TCR),
919 regmap_reg_range(SPI_SR, SPI_SR),
920 regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
921};
922
923static const struct regmap_access_table dspi_volatile_table = {
924 .yes_ranges = dspi_volatile_ranges,
925 .n_yes_ranges = ARRAY_SIZE(dspi_volatile_ranges),
926};
927
946static const struct regmap_config dspi_regmap_config = { 928static const struct regmap_config dspi_regmap_config = {
947 .reg_bits = 32, 929 .reg_bits = 32,
948 .val_bits = 32, 930 .val_bits = 32,
949 .reg_stride = 4, 931 .reg_stride = 4,
950 .max_register = 0x88, 932 .max_register = 0x88,
933 .volatile_table = &dspi_volatile_table,
934};
935
936static const struct regmap_range dspi_xspi_volatile_ranges[] = {
937 regmap_reg_range(SPI_MCR, SPI_TCR),
938 regmap_reg_range(SPI_SR, SPI_SR),
939 regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
940 regmap_reg_range(SPI_SREX, SPI_SREX),
941};
942
943static const struct regmap_access_table dspi_xspi_volatile_table = {
944 .yes_ranges = dspi_xspi_volatile_ranges,
945 .n_yes_ranges = ARRAY_SIZE(dspi_xspi_volatile_ranges),
946};
947
948static const struct regmap_config dspi_xspi_regmap_config[] = {
949 {
950 .reg_bits = 32,
951 .val_bits = 32,
952 .reg_stride = 4,
953 .max_register = 0x13c,
954 .volatile_table = &dspi_xspi_volatile_table,
955 },
956 {
957 .name = "pushr",
958 .reg_bits = 16,
959 .val_bits = 16,
960 .reg_stride = 2,
961 .max_register = 0x2,
962 },
951}; 963};
952 964
953static void dspi_init(struct fsl_dspi *dspi) 965static void dspi_init(struct fsl_dspi *dspi)
954{ 966{
967 regmap_write(dspi->regmap, SPI_MCR, SPI_MCR_MASTER | SPI_MCR_PCSIS |
968 (dspi->devtype_data->xspi_mode ? SPI_MCR_XSPI : 0));
955 regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR); 969 regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
970 if (dspi->devtype_data->xspi_mode)
971 regmap_write(dspi->regmap, SPI_CTARE(0),
972 SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1));
956} 973}
957 974
958static int dspi_probe(struct platform_device *pdev) 975static int dspi_probe(struct platform_device *pdev)
@@ -961,6 +978,7 @@ static int dspi_probe(struct platform_device *pdev)
961 struct spi_master *master; 978 struct spi_master *master;
962 struct fsl_dspi *dspi; 979 struct fsl_dspi *dspi;
963 struct resource *res; 980 struct resource *res;
981 const struct regmap_config *regmap_config;
964 void __iomem *base; 982 void __iomem *base;
965 struct fsl_dspi_platform_data *pdata; 983 struct fsl_dspi_platform_data *pdata;
966 int ret = 0, cs_num, bus_num; 984 int ret = 0, cs_num, bus_num;
@@ -980,8 +998,6 @@ static int dspi_probe(struct platform_device *pdev)
980 998
981 master->cleanup = dspi_cleanup; 999 master->cleanup = dspi_cleanup;
982 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST; 1000 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
983 master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
984 SPI_BPW_MASK(16);
985 1001
986 pdata = dev_get_platdata(&pdev->dev); 1002 pdata = dev_get_platdata(&pdev->dev);
987 if (pdata) { 1003 if (pdata) {
@@ -1013,6 +1029,11 @@ static int dspi_probe(struct platform_device *pdev)
1013 } 1029 }
1014 } 1030 }
1015 1031
1032 if (dspi->devtype_data->xspi_mode)
1033 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1034 else
1035 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1036
1016 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1037 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1017 base = devm_ioremap_resource(&pdev->dev, res); 1038 base = devm_ioremap_resource(&pdev->dev, res);
1018 if (IS_ERR(base)) { 1039 if (IS_ERR(base)) {
@@ -1020,8 +1041,11 @@ static int dspi_probe(struct platform_device *pdev)
1020 goto out_master_put; 1041 goto out_master_put;
1021 } 1042 }
1022 1043
1023 dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base, 1044 if (dspi->devtype_data->xspi_mode)
1024 &dspi_regmap_config); 1045 regmap_config = &dspi_xspi_regmap_config[0];
1046 else
1047 regmap_config = &dspi_regmap_config;
1048 dspi->regmap = devm_regmap_init_mmio(&pdev->dev, base, regmap_config);
1025 if (IS_ERR(dspi->regmap)) { 1049 if (IS_ERR(dspi->regmap)) {
1026 dev_err(&pdev->dev, "failed to init regmap: %ld\n", 1050 dev_err(&pdev->dev, "failed to init regmap: %ld\n",
1027 PTR_ERR(dspi->regmap)); 1051 PTR_ERR(dspi->regmap));
@@ -1029,30 +1053,43 @@ static int dspi_probe(struct platform_device *pdev)
1029 goto out_master_put; 1053 goto out_master_put;
1030 } 1054 }
1031 1055
1056 if (dspi->devtype_data->xspi_mode) {
1057 dspi->regmap_pushr = devm_regmap_init_mmio(
1058 &pdev->dev, base + SPI_PUSHR,
1059 &dspi_xspi_regmap_config[1]);
1060 if (IS_ERR(dspi->regmap_pushr)) {
1061 dev_err(&pdev->dev,
1062 "failed to init pushr regmap: %ld\n",
1063 PTR_ERR(dspi->regmap_pushr));
1064 ret = PTR_ERR(dspi->regmap_pushr);
1065 goto out_master_put;
1066 }
1067 }
1068
1069 dspi->clk = devm_clk_get(&pdev->dev, "dspi");
1070 if (IS_ERR(dspi->clk)) {
1071 ret = PTR_ERR(dspi->clk);
1072 dev_err(&pdev->dev, "unable to get clock\n");
1073 goto out_master_put;
1074 }
1075 ret = clk_prepare_enable(dspi->clk);
1076 if (ret)
1077 goto out_master_put;
1078
1032 dspi_init(dspi); 1079 dspi_init(dspi);
1033 dspi->irq = platform_get_irq(pdev, 0); 1080 dspi->irq = platform_get_irq(pdev, 0);
1034 if (dspi->irq < 0) { 1081 if (dspi->irq < 0) {
1035 dev_err(&pdev->dev, "can't get platform irq\n"); 1082 dev_err(&pdev->dev, "can't get platform irq\n");
1036 ret = dspi->irq; 1083 ret = dspi->irq;
1037 goto out_master_put; 1084 goto out_clk_put;
1038 } 1085 }
1039 1086
1040 ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0, 1087 ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
1041 pdev->name, dspi); 1088 pdev->name, dspi);
1042 if (ret < 0) { 1089 if (ret < 0) {
1043 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n"); 1090 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
1044 goto out_master_put; 1091 goto out_clk_put;
1045 }
1046
1047 dspi->clk = devm_clk_get(&pdev->dev, "dspi");
1048 if (IS_ERR(dspi->clk)) {
1049 ret = PTR_ERR(dspi->clk);
1050 dev_err(&pdev->dev, "unable to get clock\n");
1051 goto out_master_put;
1052 } 1092 }
1053 ret = clk_prepare_enable(dspi->clk);
1054 if (ret)
1055 goto out_master_put;
1056 1093
1057 if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) { 1094 if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
1058 ret = dspi_request_dma(dspi, res->start); 1095 ret = dspi_request_dma(dspi, res->start);