Skip to content

Commit 4da9d4e

Browse files
vladimirolteangregkh
authored andcommitted
spi: fsl-dspi: avoid SCK glitches with continuous transfers
[ Upstream commit c5c31fb ] The DSPI controller has configurable timing for (a) tCSC: the interval between the assertion of the chip select and the first clock edge (b) tASC: the interval between the last clock edge and the deassertion of the chip select What is a bit surprising, but is documented in the figure "Example of continuous transfer (CPHA=1, CONT=1)" in the datasheet, is that when the chip select stays asserted between multiple TX FIFO writes, the tCSC and tASC times still apply. With CONT=1, chip select remains asserted, but SCK takes a break and goes to the idle state for tASC + tCSC ns. In other words, the default values (of 0 and 0 ns) result in SCK glitches where the SCK transition to the idle state, as well as the SCK transition from the idle state, will have no delay in between, and it may appear that a SCK cycle has simply gone missing. The resulting timing violation might cause data corruption in many peripherals, as their chip select is asserted. The driver has device tree bindings for tCSC ("fsl,spi-cs-sck-delay") and tASC ("fsl,spi-sck-cs-delay"), but these are only specified to apply when the chip select toggles in the first place, and this timing characteristic depends on each peripheral. Many peripherals do not have explicit timing requirements, so many device trees do not have these properties present at all. Nonetheless, the lack of SCK glitches is a common sense requirement, and since the SCK stays in the idle state during transfers for tCSC+tASC ns, and that in itself should look like half a cycle, then let's ensure that tCSC and tASC are at least a quarter of a SCK period, such that their sum is at least half of one. Fixes: 95bf15f ("spi: fsl-dspi: Add ~50ns delay between cs and sck") Reported-by: Lisa Chen (陈敏捷) <[email protected]> Debugged-by: Lisa Chen (陈敏捷) <[email protected]> Tested-by: Lisa Chen (陈敏捷) <[email protected]> Signed-off-by: Vladimir Oltean <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 08acd41 commit 4da9d4e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

drivers/spi/spi-fsl-dspi.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,9 @@ static int dspi_transfer_one_message(struct spi_controller *ctlr,
975975
static int dspi_setup(struct spi_device *spi)
976976
{
977977
struct fsl_dspi *dspi = spi_controller_get_devdata(spi->controller);
978+
u32 period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->max_speed_hz);
978979
unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
980+
u32 quarter_period_ns = DIV_ROUND_UP(period_ns, 4);
979981
u32 cs_sck_delay = 0, sck_cs_delay = 0;
980982
struct fsl_dspi_platform_data *pdata;
981983
unsigned char pasc = 0, asc = 0;
@@ -1003,6 +1005,19 @@ static int dspi_setup(struct spi_device *spi)
10031005
sck_cs_delay = pdata->sck_cs_delay;
10041006
}
10051007

1008+
/* Since tCSC and tASC apply to continuous transfers too, avoid SCK
1009+
* glitches of half a cycle by never allowing tCSC + tASC to go below
1010+
* half a SCK period.
1011+
*/
1012+
if (cs_sck_delay < quarter_period_ns)
1013+
cs_sck_delay = quarter_period_ns;
1014+
if (sck_cs_delay < quarter_period_ns)
1015+
sck_cs_delay = quarter_period_ns;
1016+
1017+
dev_dbg(&spi->dev,
1018+
"DSPI controller timing params: CS-to-SCK delay %u ns, SCK-to-CS delay %u ns\n",
1019+
cs_sck_delay, sck_cs_delay);
1020+
10061021
clkrate = clk_get_rate(dspi->clk);
10071022
hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
10081023

0 commit comments

Comments
 (0)