Skip to content

Commit a557fca

Browse files
Patrice Chotardbroonie
authored andcommitted
spi: stm32_qspi: Add transfer_one_message() spi callback
Add transfer_one_message() spi callback in order to use the QSPI interface as a communication channel using up to 8 qspi lines (QSPI configured in dual flash mode). To enable this mode, both spi-rx-bus-width and spi-tx-bus-width must be set to 8 and cs-qpios must be populated. Signed-off-by: Patrice Chotard <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 1d895be commit a557fca

File tree

1 file changed

+109
-9
lines changed

1 file changed

+109
-9
lines changed

drivers/spi/spi-stm32-qspi.c

Lines changed: 109 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/mutex.h>
1616
#include <linux/of.h>
1717
#include <linux/of_device.h>
18+
#include <linux/of_gpio.h>
1819
#include <linux/pinctrl/consumer.h>
1920
#include <linux/pm_runtime.h>
2021
#include <linux/platform_device.h>
@@ -355,10 +356,10 @@ static int stm32_qspi_get_mode(u8 buswidth)
355356
return buswidth;
356357
}
357358

358-
static int stm32_qspi_send(struct spi_mem *mem, const struct spi_mem_op *op)
359+
static int stm32_qspi_send(struct spi_device *spi, const struct spi_mem_op *op)
359360
{
360-
struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master);
361-
struct stm32_qspi_flash *flash = &qspi->flash[mem->spi->chip_select];
361+
struct stm32_qspi *qspi = spi_controller_get_devdata(spi->master);
362+
struct stm32_qspi_flash *flash = &qspi->flash[spi->chip_select];
362363
u32 ccr, cr;
363364
int timeout, err = 0, err_poll_status = 0;
364365

@@ -465,7 +466,7 @@ static int stm32_qspi_poll_status(struct spi_mem *mem, const struct spi_mem_op *
465466
qspi->fmode = CCR_FMODE_APM;
466467
qspi->status_timeout = timeout_ms;
467468

468-
ret = stm32_qspi_send(mem, op);
469+
ret = stm32_qspi_send(mem->spi, op);
469470
mutex_unlock(&qspi->lock);
470471

471472
pm_runtime_mark_last_busy(qspi->dev);
@@ -489,7 +490,7 @@ static int stm32_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
489490
else
490491
qspi->fmode = CCR_FMODE_INDW;
491492

492-
ret = stm32_qspi_send(mem, op);
493+
ret = stm32_qspi_send(mem->spi, op);
493494
mutex_unlock(&qspi->lock);
494495

495496
pm_runtime_mark_last_busy(qspi->dev);
@@ -545,7 +546,7 @@ static ssize_t stm32_qspi_dirmap_read(struct spi_mem_dirmap_desc *desc,
545546
else
546547
qspi->fmode = CCR_FMODE_INDR;
547548

548-
ret = stm32_qspi_send(desc->mem, &op);
549+
ret = stm32_qspi_send(desc->mem->spi, &op);
549550
mutex_unlock(&qspi->lock);
550551

551552
pm_runtime_mark_last_busy(qspi->dev);
@@ -554,12 +555,87 @@ static ssize_t stm32_qspi_dirmap_read(struct spi_mem_dirmap_desc *desc,
554555
return ret ?: len;
555556
}
556557

558+
static int stm32_qspi_transfer_one_message(struct spi_controller *ctrl,
559+
struct spi_message *msg)
560+
{
561+
struct stm32_qspi *qspi = spi_controller_get_devdata(ctrl);
562+
struct spi_transfer *transfer;
563+
struct spi_device *spi = msg->spi;
564+
struct spi_mem_op op;
565+
int ret;
566+
567+
if (!spi->cs_gpiod)
568+
return -EOPNOTSUPP;
569+
570+
mutex_lock(&qspi->lock);
571+
572+
gpiod_set_value_cansleep(spi->cs_gpiod, true);
573+
574+
list_for_each_entry(transfer, &msg->transfers, transfer_list) {
575+
u8 dummy_bytes = 0;
576+
577+
memset(&op, 0, sizeof(op));
578+
579+
dev_dbg(qspi->dev, "tx_buf:%p tx_nbits:%d rx_buf:%p rx_nbits:%d len:%d dummy_data:%d\n",
580+
transfer->tx_buf, transfer->tx_nbits,
581+
transfer->rx_buf, transfer->rx_nbits,
582+
transfer->len, transfer->dummy_data);
583+
584+
/*
585+
* QSPI hardware supports dummy bytes transfer.
586+
* If current transfer is dummy byte, merge it with the next
587+
* transfer in order to take into account QSPI block constraint
588+
*/
589+
if (transfer->dummy_data) {
590+
op.dummy.buswidth = transfer->tx_nbits;
591+
op.dummy.nbytes = transfer->len;
592+
dummy_bytes = transfer->len;
593+
594+
/* if happens, means that message is not correctly built */
595+
if (list_is_last(&transfer->transfer_list, &msg->transfers))
596+
goto end_of_transfer;
597+
598+
transfer = list_next_entry(transfer, transfer_list);
599+
}
600+
601+
op.data.nbytes = transfer->len;
602+
603+
if (transfer->rx_buf) {
604+
qspi->fmode = CCR_FMODE_INDR;
605+
op.data.buswidth = transfer->rx_nbits;
606+
op.data.dir = SPI_MEM_DATA_IN;
607+
op.data.buf.in = transfer->rx_buf;
608+
} else {
609+
qspi->fmode = CCR_FMODE_INDW;
610+
op.data.buswidth = transfer->tx_nbits;
611+
op.data.dir = SPI_MEM_DATA_OUT;
612+
op.data.buf.out = transfer->tx_buf;
613+
}
614+
615+
ret = stm32_qspi_send(spi, &op);
616+
if (ret)
617+
goto end_of_transfer;
618+
619+
msg->actual_length += transfer->len + dummy_bytes;
620+
}
621+
622+
end_of_transfer:
623+
gpiod_set_value_cansleep(spi->cs_gpiod, false);
624+
625+
mutex_unlock(&qspi->lock);
626+
627+
msg->status = ret;
628+
spi_finalize_current_message(ctrl);
629+
630+
return ret;
631+
}
632+
557633
static int stm32_qspi_setup(struct spi_device *spi)
558634
{
559635
struct spi_controller *ctrl = spi->master;
560636
struct stm32_qspi *qspi = spi_controller_get_devdata(ctrl);
561637
struct stm32_qspi_flash *flash;
562-
u32 presc;
638+
u32 presc, mode;
563639
int ret;
564640

565641
if (ctrl->busy)
@@ -568,6 +644,16 @@ static int stm32_qspi_setup(struct spi_device *spi)
568644
if (!spi->max_speed_hz)
569645
return -EINVAL;
570646

647+
mode = spi->mode & (SPI_TX_OCTAL | SPI_RX_OCTAL);
648+
if ((mode == SPI_TX_OCTAL || mode == SPI_RX_OCTAL) ||
649+
((mode == (SPI_TX_OCTAL | SPI_RX_OCTAL)) &&
650+
of_gpio_named_count(qspi->dev->of_node, "cs-gpios") == -ENOENT)) {
651+
dev_err(qspi->dev, "spi-rx-bus-width\\/spi-tx-bus-width\\/cs-gpios\n");
652+
dev_err(qspi->dev, "configuration not supported\n");
653+
654+
return -EINVAL;
655+
}
656+
571657
ret = pm_runtime_resume_and_get(qspi->dev);
572658
if (ret < 0)
573659
return ret;
@@ -580,6 +666,17 @@ static int stm32_qspi_setup(struct spi_device *spi)
580666

581667
mutex_lock(&qspi->lock);
582668
qspi->cr_reg = CR_APMS | 3 << CR_FTHRES_SHIFT | CR_SSHIFT | CR_EN;
669+
670+
/*
671+
* Dual flash mode is only enable in case SPI_TX_OCTAL and SPI_TX_OCTAL
672+
* are both set in spi->mode and "cs-gpios" properties is found in DT
673+
*/
674+
if (((spi->mode & (SPI_TX_OCTAL | SPI_RX_OCTAL)) == (SPI_TX_OCTAL | SPI_RX_OCTAL)) &&
675+
of_gpio_named_count(qspi->dev->of_node, "cs-gpios")) {
676+
qspi->cr_reg |= CR_DFM;
677+
dev_dbg(qspi->dev, "Dual flash mode enable");
678+
}
679+
583680
writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR);
584681

585682
/* set dcr fsize to max address */
@@ -741,11 +838,14 @@ static int stm32_qspi_probe(struct platform_device *pdev)
741838

742839
mutex_init(&qspi->lock);
743840

744-
ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD
745-
| SPI_TX_DUAL | SPI_TX_QUAD;
841+
ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_OCTAL
842+
| SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_OCTAL;
746843
ctrl->setup = stm32_qspi_setup;
747844
ctrl->bus_num = -1;
748845
ctrl->mem_ops = &stm32_qspi_mem_ops;
846+
ctrl->use_gpio_descriptors = true;
847+
ctrl->transfer_one_message = stm32_qspi_transfer_one_message;
848+
ctrl->auto_runtime_pm = true;
749849
ctrl->num_chipselect = STM32_QSPI_MAX_NORCHIP;
750850
ctrl->dev.of_node = dev->of_node;
751851

0 commit comments

Comments
 (0)