Skip to content

Commit 0cb9ca1

Browse files
Andra-Teodora Iliebroonie
authored andcommitted
spi: spi-fsl-dspi: Enable modified transfer protocol on S32G
S32G supports modified transfer protocol where both host and target devices sample later in the SCK period than in Classic SPI mode to allow the logic to tolerate more delays in device pads and board traces. Set MTFE bit in MCR register for frequencies higher than 25MHz. Signed-off-by: Andra-Teodora Ilie <andra.ilie@nxp.com> Signed-off-by: Bogdan-Gabriel Roman <bogdan-gabriel.roman@nxp.com> Signed-off-by: Larisa Grigore <larisa.grigore@nxp.com> Signed-off-by: James Clark <james.clark@linaro.org> Link: https://patch.msgid.link/20250522-james-nxp-spi-v2-11-bea884630cfb@linaro.org Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent c5412ec commit 0cb9ca1

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

drivers/spi/spi-fsl-dspi.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#define SPI_MCR 0x00
2626
#define SPI_MCR_HOST BIT(31)
27+
#define SPI_MCR_MTFE BIT(26)
2728
#define SPI_MCR_PCSIS(x) ((x) << 16)
2829
#define SPI_MCR_CLR_TXF BIT(11)
2930
#define SPI_MCR_CLR_RXF BIT(10)
@@ -37,6 +38,7 @@
3738

3839
#define SPI_CTAR(x) (0x0c + (((x) & GENMASK(2, 0)) * 4))
3940
#define SPI_CTAR_FMSZ(x) (((x) << 27) & GENMASK(30, 27))
41+
#define SPI_CTAR_DBR BIT(31)
4042
#define SPI_CTAR_CPOL BIT(26)
4143
#define SPI_CTAR_CPHA BIT(25)
4244
#define SPI_CTAR_LSBFE BIT(24)
@@ -111,6 +113,8 @@
111113

112114
#define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
113115

116+
#define SPI_25MHZ 25000000
117+
114118
struct chip_data {
115119
u32 ctar_val;
116120
};
@@ -346,6 +350,7 @@ struct fsl_dspi {
346350
const void *tx;
347351
void *rx;
348352
u16 tx_cmd;
353+
bool mtf_enabled;
349354
const struct fsl_dspi_devtype_data *devtype_data;
350355

351356
struct completion xfer_done;
@@ -722,7 +727,7 @@ static void dspi_release_dma(struct fsl_dspi *dspi)
722727
}
723728

724729
static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
725-
unsigned long clkrate)
730+
unsigned long clkrate, bool mtf_enabled)
726731
{
727732
/* Valid baud rate pre-scaler values */
728733
int pbr_tbl[4] = {2, 3, 5, 7};
@@ -739,7 +744,13 @@ static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
739744

740745
for (i = 0; i < ARRAY_SIZE(brs); i++)
741746
for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
742-
scale = brs[i] * pbr_tbl[j];
747+
if (mtf_enabled) {
748+
/* In MTF mode DBR=1 so frequency is doubled */
749+
scale = (brs[i] * pbr_tbl[j]) / 2;
750+
} else {
751+
scale = brs[i] * pbr_tbl[j];
752+
}
753+
743754
if (scale >= scale_needed) {
744755
if (scale < minscale) {
745756
minscale = scale;
@@ -1146,6 +1157,20 @@ static int dspi_transfer_one_message(struct spi_controller *ctlr,
11461157
return status;
11471158
}
11481159

1160+
static int dspi_set_mtf(struct fsl_dspi *dspi)
1161+
{
1162+
if (spi_controller_is_target(dspi->ctlr))
1163+
return 0;
1164+
1165+
if (dspi->mtf_enabled)
1166+
regmap_update_bits(dspi->regmap, SPI_MCR, SPI_MCR_MTFE,
1167+
SPI_MCR_MTFE);
1168+
else
1169+
regmap_update_bits(dspi->regmap, SPI_MCR, SPI_MCR_MTFE, 0);
1170+
1171+
return 0;
1172+
}
1173+
11491174
static int dspi_setup(struct spi_device *spi)
11501175
{
11511176
struct fsl_dspi *dspi = spi_controller_get_devdata(spi->controller);
@@ -1204,7 +1229,16 @@ static int dspi_setup(struct spi_device *spi)
12041229
cs_sck_delay, sck_cs_delay);
12051230

12061231
clkrate = clk_get_rate(dspi->clk);
1207-
hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
1232+
1233+
if (is_s32g_dspi(dspi) && spi->max_speed_hz > SPI_25MHZ)
1234+
dspi->mtf_enabled = true;
1235+
else
1236+
dspi->mtf_enabled = false;
1237+
1238+
dspi_set_mtf(dspi);
1239+
1240+
hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate,
1241+
dspi->mtf_enabled);
12081242

12091243
/* Set PCS to SCK delay scale values */
12101244
ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
@@ -1226,6 +1260,9 @@ static int dspi_setup(struct spi_device *spi)
12261260
SPI_CTAR_PBR(pbr) |
12271261
SPI_CTAR_BR(br);
12281262

1263+
if (dspi->mtf_enabled)
1264+
chip->ctar_val |= SPI_CTAR_DBR;
1265+
12291266
if (spi->mode & SPI_LSB_FIRST)
12301267
chip->ctar_val |= SPI_CTAR_LSBFE;
12311268
}
@@ -1352,6 +1389,8 @@ static int dspi_resume(struct device *dev)
13521389
return ret;
13531390
}
13541391

1392+
dspi_set_mtf(dspi);
1393+
13551394
if (dspi->irq)
13561395
enable_irq(dspi->irq);
13571396

0 commit comments

Comments
 (0)