Skip to content

Commit 5dc47fe

Browse files
Varadarajan Narayananbroonie
authored andcommitted
spi: qup: allow block mode to generate multiple transactions
This let's you write more to the SPI bus than 64K-1 which is important if the block size of a SPI device is >= 64K or some other device wants to do something larger. This has the benefit of completely removing spi_message from the spi-qup transactions Signed-off-by: Matthew McClintock <[email protected]> Signed-off-by: Varadarajan Narayanan <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent 3b5ea2c commit 5dc47fe

File tree

1 file changed

+80
-48
lines changed

1 file changed

+80
-48
lines changed

drivers/spi/spi-qup.c

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120

121121
#define SPI_NUM_CHIPSELECTS 4
122122

123-
#define SPI_MAX_DMA_XFER (SZ_64K - 64)
123+
#define SPI_MAX_XFER (SZ_64K - 64)
124124

125125
/* high speed mode is when bus rate is greater then 26MHz */
126126
#define SPI_HS_MIN_RATE 26000000
@@ -149,6 +149,8 @@ struct spi_qup {
149149
int n_words;
150150
int tx_bytes;
151151
int rx_bytes;
152+
const u8 *tx_buf;
153+
u8 *rx_buf;
152154
int qup_v1;
153155

154156
int mode;
@@ -173,6 +175,12 @@ static inline bool spi_qup_is_dma_xfer(int mode)
173175
return false;
174176
}
175177

178+
/* get's the transaction size length */
179+
static inline unsigned int spi_qup_len(struct spi_qup *controller)
180+
{
181+
return controller->n_words * controller->w_size;
182+
}
183+
176184
static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
177185
{
178186
u32 opstate = readl_relaxed(controller->base + QUP_STATE);
@@ -225,19 +233,19 @@ static int spi_qup_set_state(struct spi_qup *controller, u32 state)
225233
return 0;
226234
}
227235

228-
static void spi_qup_read_from_fifo(struct spi_qup *controller,
229-
struct spi_transfer *xfer, u32 num_words)
236+
static void spi_qup_read_from_fifo(struct spi_qup *controller, u32 num_words)
230237
{
231-
u8 *rx_buf = xfer->rx_buf;
238+
u8 *rx_buf = controller->rx_buf;
232239
int i, shift, num_bytes;
233240
u32 word;
234241

235242
for (; num_words; num_words--) {
236243

237244
word = readl_relaxed(controller->base + QUP_INPUT_FIFO);
238245

239-
num_bytes = min_t(int, xfer->len - controller->rx_bytes,
240-
controller->w_size);
246+
num_bytes = min_t(int, spi_qup_len(controller) -
247+
controller->rx_bytes,
248+
controller->w_size);
241249

242250
if (!rx_buf) {
243251
controller->rx_bytes += num_bytes;
@@ -258,13 +266,12 @@ static void spi_qup_read_from_fifo(struct spi_qup *controller,
258266
}
259267
}
260268

261-
static void spi_qup_read(struct spi_qup *controller,
262-
struct spi_transfer *xfer)
269+
static void spi_qup_read(struct spi_qup *controller)
263270
{
264271
u32 remainder, words_per_block, num_words;
265272
bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
266273

267-
remainder = DIV_ROUND_UP(xfer->len - controller->rx_bytes,
274+
remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->rx_bytes,
268275
controller->w_size);
269276
words_per_block = controller->in_blk_sz >> 2;
270277

@@ -285,7 +292,7 @@ static void spi_qup_read(struct spi_qup *controller,
285292
}
286293

287294
/* read up to the maximum transfer size available */
288-
spi_qup_read_from_fifo(controller, xfer, num_words);
295+
spi_qup_read_from_fifo(controller, num_words);
289296

290297
remainder -= num_words;
291298

@@ -307,18 +314,18 @@ static void spi_qup_read(struct spi_qup *controller,
307314

308315
}
309316

310-
static void spi_qup_write_to_fifo(struct spi_qup *controller,
311-
struct spi_transfer *xfer, u32 num_words)
317+
static void spi_qup_write_to_fifo(struct spi_qup *controller, u32 num_words)
312318
{
313-
const u8 *tx_buf = xfer->tx_buf;
319+
const u8 *tx_buf = controller->tx_buf;
314320
int i, num_bytes;
315321
u32 word, data;
316322

317323
for (; num_words; num_words--) {
318324
word = 0;
319325

320-
num_bytes = min_t(int, xfer->len - controller->tx_bytes,
321-
controller->w_size);
326+
num_bytes = min_t(int, spi_qup_len(controller) -
327+
controller->tx_bytes,
328+
controller->w_size);
322329
if (tx_buf)
323330
for (i = 0; i < num_bytes; i++) {
324331
data = tx_buf[controller->tx_bytes + i];
@@ -338,13 +345,12 @@ static void spi_qup_dma_done(void *data)
338345
complete(&qup->done);
339346
}
340347

341-
static void spi_qup_write(struct spi_qup *controller,
342-
struct spi_transfer *xfer)
348+
static void spi_qup_write(struct spi_qup *controller)
343349
{
344350
bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
345351
u32 remainder, words_per_block, num_words;
346352

347-
remainder = DIV_ROUND_UP(xfer->len - controller->tx_bytes,
353+
remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->tx_bytes,
348354
controller->w_size);
349355
words_per_block = controller->out_blk_sz >> 2;
350356

@@ -364,7 +370,7 @@ static void spi_qup_write(struct spi_qup *controller,
364370
num_words = 1;
365371
}
366372

367-
spi_qup_write_to_fifo(controller, xfer, num_words);
373+
spi_qup_write_to_fifo(controller, num_words);
368374

369375
remainder -= num_words;
370376

@@ -471,44 +477,69 @@ static int spi_qup_do_pio(struct spi_device *spi, struct spi_transfer *xfer,
471477
{
472478
struct spi_master *master = spi->master;
473479
struct spi_qup *qup = spi_master_get_devdata(master);
474-
int ret;
480+
int ret, n_words, iterations, offset = 0;
475481

476-
ret = spi_qup_io_config(spi, xfer);
477-
if (ret)
478-
return ret;
482+
n_words = qup->n_words;
483+
iterations = n_words / SPI_MAX_XFER; /* round down */
484+
qup->rx_buf = xfer->rx_buf;
485+
qup->tx_buf = xfer->tx_buf;
479486

480-
ret = spi_qup_set_state(qup, QUP_STATE_RUN);
481-
if (ret) {
482-
dev_warn(qup->dev, "cannot set RUN state\n");
483-
return ret;
484-
}
487+
do {
488+
if (iterations)
489+
qup->n_words = SPI_MAX_XFER;
490+
else
491+
qup->n_words = n_words % SPI_MAX_XFER;
485492

486-
ret = spi_qup_set_state(qup, QUP_STATE_PAUSE);
487-
if (ret) {
488-
dev_warn(qup->dev, "cannot set PAUSE state\n");
489-
return ret;
490-
}
493+
if (qup->tx_buf && offset)
494+
qup->tx_buf = xfer->tx_buf + offset * SPI_MAX_XFER;
491495

492-
if (qup->mode == QUP_IO_M_MODE_FIFO)
493-
spi_qup_write(qup, xfer);
496+
if (qup->rx_buf && offset)
497+
qup->rx_buf = xfer->rx_buf + offset * SPI_MAX_XFER;
494498

495-
ret = spi_qup_set_state(qup, QUP_STATE_RUN);
496-
if (ret) {
497-
dev_warn(qup->dev, "%s(%d): cannot set RUN state\n",
498-
__func__, __LINE__);
499-
return ret;
500-
}
499+
/*
500+
* if the transaction is small enough, we need
501+
* to fallback to FIFO mode
502+
*/
503+
if (qup->n_words <= (qup->in_fifo_sz / sizeof(u32)))
504+
qup->mode = QUP_IO_M_MODE_FIFO;
501505

502-
if (!wait_for_completion_timeout(&qup->done, timeout))
503-
return -ETIMEDOUT;
506+
ret = spi_qup_io_config(spi, xfer);
507+
if (ret)
508+
return ret;
509+
510+
ret = spi_qup_set_state(qup, QUP_STATE_RUN);
511+
if (ret) {
512+
dev_warn(qup->dev, "cannot set RUN state\n");
513+
return ret;
514+
}
515+
516+
ret = spi_qup_set_state(qup, QUP_STATE_PAUSE);
517+
if (ret) {
518+
dev_warn(qup->dev, "cannot set PAUSE state\n");
519+
return ret;
520+
}
521+
522+
if (qup->mode == QUP_IO_M_MODE_FIFO)
523+
spi_qup_write(qup);
524+
525+
ret = spi_qup_set_state(qup, QUP_STATE_RUN);
526+
if (ret) {
527+
dev_warn(qup->dev, "cannot set RUN state\n");
528+
return ret;
529+
}
530+
531+
if (!wait_for_completion_timeout(&qup->done, timeout))
532+
return -ETIMEDOUT;
533+
534+
offset++;
535+
} while (iterations--);
504536

505537
return 0;
506538
}
507539

508540
static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id)
509541
{
510542
struct spi_qup *controller = dev_id;
511-
struct spi_transfer *xfer = controller->xfer;
512543
u32 opflags, qup_err, spi_err;
513544
int error = 0;
514545

@@ -545,10 +576,10 @@ static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id)
545576
writel_relaxed(opflags, controller->base + QUP_OPERATIONAL);
546577
} else {
547578
if (opflags & QUP_OP_IN_SERVICE_FLAG)
548-
spi_qup_read(controller, xfer);
579+
spi_qup_read(controller);
549580

550581
if (opflags & QUP_OP_OUT_SERVICE_FLAG)
551-
spi_qup_write(controller, xfer);
582+
spi_qup_write(controller);
552583
}
553584

554585
if ((opflags & QUP_OP_MAX_INPUT_DONE_FLAG) || error)
@@ -755,7 +786,8 @@ static int spi_qup_transfer_one(struct spi_master *master,
755786
return ret;
756787

757788
timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC);
758-
timeout = DIV_ROUND_UP(xfer->len * 8, timeout);
789+
timeout = DIV_ROUND_UP(min_t(unsigned long, SPI_MAX_XFER,
790+
xfer->len) * 8, timeout);
759791
timeout = 100 * msecs_to_jiffies(timeout);
760792

761793
reinit_completion(&controller->done);
@@ -969,7 +1001,7 @@ static int spi_qup_probe(struct platform_device *pdev)
9691001
master->dev.of_node = pdev->dev.of_node;
9701002
master->auto_runtime_pm = true;
9711003
master->dma_alignment = dma_get_cache_alignment();
972-
master->max_dma_len = SPI_MAX_DMA_XFER;
1004+
master->max_dma_len = SPI_MAX_XFER;
9731005

9741006
platform_set_drvdata(pdev, master);
9751007

0 commit comments

Comments
 (0)