spi: spi-imx: Fix out-of-order CS/SCLK operation at low speeds

Problem:
--------
 The problem this patch addresses has the following assumptions about the
 SPI bus setup:
 - The hardware used to find this is Freescale i.MX537 @ 1200MHz
 - The SPI SCLK operate at very low speed, less than 200 kHz
 - There are two SPI devices attached to the bus
   - Each device uses different GPIO for chipselect
   - Each device requires different SCLK signal polarity

 The observation of the SCLK and GPIO chipselect lines with a logic analyzer
 shows, that the SCLK polarity change does sometimes happen after the GPIO
 chipselect is asserted. The SPI slave device reacts on that by counting the
 SCLK polarity change as a clock pulse, which disrupts the communication with
 the SPI slave device.

Explanation:
------------
 We found an interesting correlation, that the maximum delay between the write
 into the ECSPIx_CONFIGREG register and the change of SCLK polarity at each
 SCLK frequency of 10 kHz, 20 kHz, 50 kHz and 100 kHz is 100 uS, 50 uS, 20 uS
 and 10 uS respectively. This lead us to a theory, that at SCLK frequency of
 1 Hz, the delay would be 1 S. Therefore, the time it takes for the write to
 ECSPIx_CONFIGREG to take effect in the hardware is up to the duration of 1
 tick of the SCLK clock.

 During this delay period, if the SCLK frequency is too low, the execution of
 the spi-imx.c driver can advance so much, that the GPIO chipselect will be
 asserted. The GPIO chipselect is asserted almost immediatelly.

Solution:
---------
 The solution this patch presents is simple. We calculate the resulting SCLK
 clock first by dividing the ECSPI block clock by both dividers that are to be
 programmed into the configuration register. Based on the resulting SCLK clock,
 we derive the delay it will take for the changes to get really applied. We are
 extra careful here so we delay twice as long as we should. Note that the patch
 does not create additional overhead at high speeds as the delay will likely be
 close to zero there.

Signed-off-by: Marek Vasut <marex@denx.de>
To: linux-spi@vger.kernel.org
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Huang Shijie <b32955@freescale.com>
Cc: Mark Brown <broonie@kernel.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Shawn Guo <shawn.guo@linaro.org>
Signed-off-by: Mark Brown <broonie@linaro.org>
1 file changed