linux/drivers/spi
Louis Chauvet fc70d643a2
spi: atmel: Fix clock issue when using devices with different polarities
The current Atmel SPI controller driver (v2) behaves incorrectly when
using two SPI devices with different clock polarities and GPIO CS.

When switching from one device to another, the controller driver first
enables the CS and then applies whatever configuration suits the targeted
device (typically, the polarities). The side effect of such order is the
apparition of a spurious clock edge after enabling the CS when the clock
polarity needs to be inverted wrt. the previous configuration of the
controller.

This parasitic clock edge is problematic when the SPI device uses that edge
for internal processing, which is perfectly legitimate given that its CS
was asserted. Indeed, devices such as HVS8080 driven by driver gpio-sr in
the kernel are shift registers and will process this first clock edge to
perform a first register shift. In this case, the first bit gets lost and
the whole data block that will later be read by the kernel is all shifted
by one.

    Current behavior:
      The actual switching of the clock polarity only occurs after the CS
      when the controller sends the first message:

    CLK ------------\   /-\ /-\
                    |   | | | |    . . .
                    \---/ \-/ \
    CS  -----\
             |
             \------------------

             ^      ^   ^
             |      |   |
             |      |   Actual clock of the message sent
             |      |
             |      Change of clock polarity, which occurs with the first
             |      write to the bus. This edge occurs when the CS is
             |      already asserted, and can be interpreted as
             |      the first clock edge by the receiver.
             |
             GPIO CS toggle

This issue is specific to this controller because while the SPI core
performs the operations in the right order, the controller however does
not. In practice, the controller only applies the clock configuration right
before the first transmission.

So this is not a problem when using the controller's dedicated CS, as the
controller does things correctly, but it becomes a problem when you need to
change the clock polarity and use an external GPIO for the CS.

One possible approach to solve this problem is to send a dummy message
before actually activating the CS, so that the controller applies the clock
polarity beforehand.

New behavior:

CLK     ------\      /-\     /-\      /-\     /-\
              |      | | ... | |      | | ... | |
              \------/ \-   -/ \------/ \-   -/ \------

CS      -\/-----------------------\
         ||                       |
         \/                       \---------------------
         ^    ^       ^           ^    ^
         |    |       |           |    |
         |    |       |           |    Expected clock cycles when
         |    |       |           |    sending the message
         |    |       |           |
         |    |       |           Actual GPIO CS activation, occurs inside
         |    |       |           the driver
         |    |       |
         |    |       Dummy message, to trigger clock polarity
         |    |       reconfiguration. This message is not received and
         |    |       processed by the device because CS is low.
         |    |
         |    Change of clock polarity, forced by the dummy message. This
         |    time, the edge is not detected by the receiver.
         |
         This small spike in CS activation is due to the fact that the
         spi-core activates the CS gpio before calling the driver's
         set_cs callback, which deactivates this gpio again until the
         clock polarity is correct.

To avoid having to systematically send a dummy packet, the driver keeps
track of the clock's current polarity. In this way, it only sends the dummy
packet when necessary, ensuring that the clock will have the correct
polarity when the CS is toggled.

There could be two hardware problems with this patch:
1- Maybe the small CS activation peak can confuse SPI devices
2- If on a design, a single wire is used to select two devices depending
on its state, the dummy message may disturb them.

Fixes: 5ee36c9898 ("spi: atmel_spi update chipselect handling")
Cc:  <stable@vger.kernel.org>
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
Link: https://msgid.link/r/20231204154903.11607-1-louis.chauvet@bootlin.com
Signed-off-by: Mark Brown <broonie@kernel.org>
2023-12-14 10:56:37 +00:00
..
atmel-quadspi.c spi: atmel-quadspi: Convert to platform remove callback returning void 2023-03-17 15:47:51 +00:00
internals.h
Kconfig spi: Fixes for v6.7 2023-11-10 11:44:38 -08:00
Makefile spi: Add support for Intel LJCA USB SPI driver 2023-10-11 11:33:38 +02:00
spi-altera-core.c spi: Replace all spi->chip_select and spi->cs_gpiod references with function call 2023-03-11 12:34:01 +00:00
spi-altera-dfl.c spi: spi-altera-dfl: switch to use modern name 2022-12-29 13:22:04 +00:00
spi-altera-platform.c spi: spi-altera-platform: switch to use modern name 2022-12-29 13:22:05 +00:00
spi-amd.c spi: amd: fix Wvoid-pointer-to-enum-cast warning 2023-08-14 13:11:15 +01:00
spi-amlogic-spifc-a1.c spi: amlogic-spifc-a1: switch to use devm_spi_alloc_host() 2023-08-14 13:10:48 +01:00
spi-ar934x.c spi: ar934x: Use helper function devm_clk_get_enabled() 2023-09-11 01:31:52 +01:00
spi-armada-3700.c spi: armada-3700: Use helper function devm_clk_get_prepared() 2023-09-11 01:31:53 +01:00
spi-aspeed-smc.c spi: aspeed: Use helper function devm_clk_get_enabled() 2023-09-11 01:31:54 +01:00
spi-at91-usart.c spi: at91-usart: Remove some dead code 2023-09-25 16:27:48 +02:00
spi-ath79.c spi: ath79: Use helper function devm_clk_get_enabled() 2023-09-11 01:31:55 +01:00
spi-atmel.c spi: atmel: Fix clock issue when using devices with different polarities 2023-12-14 10:56:37 +00:00
spi-au1550.c spi: au1550: switch to use modern name 2023-08-14 13:10:49 +01:00
spi-axi-spi-engine.c spi: spi-axi-spi-engine: Use helper function devm_clk_get_enabled() 2023-09-11 01:31:56 +01:00
spi-bcm63xx-hsspi.c spi: bcm63xx-hsspi: switch to use modern name 2023-08-07 14:38:23 +01:00
spi-bcm63xx.c spi: bcm63xx: switch to use modern name 2023-08-07 14:38:24 +01:00
spi-bcm2835.c spi: bcm2835: add a sentinel at the end of the lookup array 2023-10-10 13:12:11 +01:00
spi-bcm2835aux.c spi: bcm2835aux: Use helper function devm_clk_get_enabled() 2023-09-11 01:31:57 +01:00
spi-bcm-qspi.c spi: bcm-qspi: switch to use modern name 2023-08-07 14:38:20 +01:00
spi-bcm-qspi.h spi: bcm-qspi: Make bcm_qspi_remove() return void 2022-10-18 19:16:53 +01:00
spi-bcmbca-hsspi.c spi: bcmbca-hsspi: switch to use modern name 2023-08-14 13:10:59 +01:00
spi-bitbang-txrx.h spi: Get rid of old SPI_MASTER_NO_TX & SPI_MASTER_NO_RX 2023-07-11 13:41:20 +01:00
spi-bitbang.c spi: Rename SPI_MASTER_GPIO_SS to SPI_CONTROLLER_GPIO_SS 2023-07-11 14:12:56 +01:00
spi-brcmstb-qspi.c spi: brcmstb-qspi: Convert to platform remove callback returning void 2023-03-06 12:31:15 +00:00
spi-butterfly.c spi: butterfly: switch to use modern name 2023-08-07 14:38:25 +01:00
spi-cadence-quadspi.c spi: spi-cadence-quadspi: Fix missing unwind goto warnings 2023-09-26 14:49:24 +02:00
spi-cadence-xspi.c spi: cadence-xspi: switch to use modern name 2023-08-07 14:38:26 +01:00
spi-cadence.c spi: cadence: revert "Add SPI transfer delays" 2023-12-06 16:31:19 +00:00
spi-cavium-octeon.c spi: octeon: switch to use modern name 2023-08-07 14:38:29 +01:00
spi-cavium-thunderx.c spi: spi-cavium-thunderx: Use helper function devm_clk_get_enabled() 2023-09-11 01:31:59 +01:00
spi-cavium.c spi: Replace all spi->chip_select and spi->cs_gpiod references with function call 2023-03-11 12:34:01 +00:00
spi-cavium.h
spi-clps711x.c spi: clps711x: switch to use modern name 2023-08-07 14:38:28 +01:00
spi-coldfire-qspi.c spi: coldfire-qspi: switch to use modern name 2023-08-07 14:38:31 +01:00
spi-cs42l43.c spi: cs42l43: Remove spurious pm_runtime_disable 2023-09-22 15:43:17 +01:00
spi-davinci.c spi: davinci: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:00 +01:00
spi-dln2.c spi: dln2: switch to use modern name 2023-08-07 14:38:32 +01:00
spi-dw-bt1.c spi: dw-bt1: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:01 +01:00
spi-dw-core.c spi: dw: switch to use modern name 2023-08-07 14:38:33 +01:00
spi-dw-dma.c spi: dw: switch to use modern name 2023-08-07 14:38:33 +01:00
spi-dw-mmio.c spi: dw-mmio: Use helper function devm_clk_get_*() 2023-09-11 01:32:02 +01:00
spi-dw-pci.c
spi-dw.h spi: dw: switch to use modern name 2023-08-07 14:38:33 +01:00
spi-ep93xx.c spi: ep93xx: switch to use modern name 2023-08-14 13:10:50 +01:00
spi-falcon.c spi: falcon: switch to use modern name 2023-08-14 13:10:51 +01:00
spi-fsi.c spi: fsi: switch to use spi_alloc_host() 2023-08-14 13:10:52 +01:00
spi-fsl-cpm.c spi: fsl-cpm: Properly define and use IO pointers 2023-08-09 12:52:49 +01:00
spi-fsl-cpm.h
spi-fsl-dspi.c spi: spi-fsl-dspi: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:02 +01:00
spi-fsl-espi.c spi: fsl-espi: switch to use modern name 2023-08-14 13:10:54 +01:00
spi-fsl-lib.c spi: Explicitly include correct DT includes 2023-07-14 20:53:00 +01:00
spi-fsl-lib.h spi: fsl: Remove unused extern declarations 2023-07-25 17:40:28 +01:00
spi-fsl-lpspi.c spi: fsl-lpspi: switch to use modern name 2023-08-14 13:10:54 +01:00
spi-fsl-qspi.c spi: fsl-qspi: switch to use modern name 2023-08-14 13:10:55 +01:00
spi-fsl-spi.c spi: fsl-spi: switch to use modern name 2023-08-14 13:10:56 +01:00
spi-fsl-spi.h
spi-geni-qcom.c spi: spi-geni-qcom: Rename the label unmap_if_dma 2023-10-09 13:14:30 +01:00
spi-gpio.c spi: gpio: switch to use modern name 2023-08-14 13:10:57 +01:00
spi-gxp.c spi: spi-gxp: BUG: Correct spi write return value 2023-09-27 17:06:36 +02:00
spi-hisi-kunpeng.c spi: hisi-kunpeng: switch to use modern name 2023-08-07 14:38:34 +01:00
spi-hisi-sfc-v3xx.c spi: hisi-sfc-v3xx: switch to use modern name 2023-08-14 13:11:00 +01:00
spi-img-spfi.c spi: img-spfi: switch to use modern name 2023-08-14 13:11:01 +01:00
spi-imx.c spi: spi-imx: correctly configure burst length when using dma 2023-12-10 13:45:58 +00:00
spi-ingenic.c spi: ingenic: switch to use devm_spi_alloc_host() 2023-08-14 13:11:03 +01:00
spi-intel-pci.c spi: intel-pci: Add support for Granite Rapids SPI serial flash 2023-09-11 20:33:30 +01:00
spi-intel-platform.c mtd: spi-nor: intel-spi: Convert to SPI MEM 2022-02-14 12:53:09 +00:00
spi-intel.c spi: intel: switch to use modern name 2023-08-14 13:11:04 +01:00
spi-intel.h mtd: spi-nor: intel-spi: Convert to SPI MEM 2022-02-14 12:53:09 +00:00
spi-iproc-qspi.c spi: bcm-qspi: Simplify logic by using devm_platform_ioremap_resource_byname() 2023-08-21 13:10:56 +01:00
spi-jcore.c spi: jcore: switch to use modern name 2023-08-14 13:11:05 +01:00
spi-lantiq-ssc.c spi: lantiq-ssc: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:03 +01:00
spi-ljca.c spi: Add support for Intel LJCA USB SPI driver 2023-10-11 11:33:38 +02:00
spi-lm70llp.c spi: lm70llp: switch to use modern name 2023-09-11 01:31:30 +01:00
spi-loongson-core.c spi: loongson: add bus driver for the loongson spi controller 2023-07-31 15:57:09 +01:00
spi-loongson-pci.c spi: loongson: add bus driver for the loongson spi controller 2023-07-31 15:57:09 +01:00
spi-loongson-plat.c spi: loongson: add bus driver for the loongson spi controller 2023-07-31 15:57:09 +01:00
spi-loongson.h spi: loongson: add bus driver for the loongson spi controller 2023-07-31 15:57:09 +01:00
spi-loopback-test.c spi: Explicitly include correct DT includes 2023-07-14 20:53:00 +01:00
spi-lp8841-rtc.c spi: lp-8841: switch to use modern name 2023-09-11 01:31:31 +01:00
spi-mem.c spi: Replace all spi->chip_select and spi->cs_gpiod references with function call 2023-03-11 12:34:01 +00:00
spi-meson-spicc.c spi: Use devm_clk_get_*() helper function to 2023-09-11 22:43:17 +01:00
spi-meson-spifc.c spi: Use devm_clk_get_*() helper function to 2023-09-11 22:43:17 +01:00
spi-microchip-core-qspi.c spi: Use devm_clk_get_*() helper function to 2023-09-11 22:43:17 +01:00
spi-microchip-core.c spi: Use devm_clk_get_*() helper function to 2023-09-11 22:43:17 +01:00
spi-mpc52xx-psc.c spi: mpc52xx-psc: Make mpc52xx_psc_spi_transfer_one_message() static 2023-10-06 12:34:31 +01:00
spi-mpc52xx.c spi: mpc52xx: switch to use modern name 2023-09-11 01:31:37 +01:00
spi-mpc512x-psc.c spi: mpc512x-psc: switch to use modern name 2023-09-11 01:31:35 +01:00
spi-mt65xx.c spi: mt65xx: switch to use modern name 2023-09-11 01:31:37 +01:00
spi-mt7621.c spi: mt7621: switch to use modern name 2023-09-11 01:31:38 +01:00
spi-mtk-nor.c spi: mtk-nor: switch to use modern name 2023-09-11 01:31:39 +01:00
spi-mtk-snfi.c spi: Use devm_clk_get_*() helper function to 2023-09-11 22:43:17 +01:00
spi-mux.c spi: mux: switch to use spi_alloc_host() 2023-09-11 01:31:41 +01:00
spi-mxic.c spi: mxic: switch to use modern name 2023-09-11 01:31:42 +01:00
spi-mxs.c spi: mxs: switch to use modern name 2023-09-11 01:31:42 +01:00
spi-npcm-fiu.c spi: Merge up fix 2023-10-30 13:20:58 +00:00
spi-npcm-pspi.c spi: npcm-pspi: switch to use modern name 2023-09-11 01:31:43 +01:00
spi-nxp-fspi.c spi: Merge up fix 2023-10-30 13:20:58 +00:00
spi-oc-tiny.c spi: oc-tiny: switch to use modern name 2023-09-11 01:31:45 +01:00
spi-omap2-mcspi.c spi: omap2-mcspi: Add FIFO support without DMA 2023-10-23 13:30:05 +01:00
spi-omap-uwire.c spi: omap-uwire: switch to use modern name 2023-09-11 01:31:46 +01:00
spi-orion.c spi: orion: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:09 +01:00
spi-pci1xxxx.c spi: mchp-pci1xxxx: Annotate struct pci1xxxx_spi with __counted_by 2023-09-22 19:01:40 +01:00
spi-pic32-sqi.c spi: pic32-sqi: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:10 +01:00
spi-pic32.c spi: pic32: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:11 +01:00
spi-pl022.c spi: spl022: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:11 +01:00
spi-ppc4xx.c spi: ppc4xx: switch to use modern name 2023-08-21 14:29:23 +01:00
spi-pxa2xx-dma.c
spi-pxa2xx-pci.c spi: pxa2xx-pci: Constify struct pxa_spi_info variables 2022-02-28 12:10:27 +00:00
spi-pxa2xx.c spi: pxa2xx: switch to use modern name 2023-08-21 14:29:24 +01:00
spi-pxa2xx.h
spi-qcom-qspi.c spi: spi-qcom-qspi: switch to use modern name 2023-08-21 14:29:25 +01:00
spi-qup.c spi: qup: Vote for interconnect bandwidth to DRAM 2023-09-25 14:19:38 +02:00
spi-rb4xx.c spi: rb4xx: switch to use modern name 2023-08-21 14:29:27 +01:00
spi-realtek-rtl.c spi: realtek-rtl: switch to use devm_spi_alloc_host() 2023-08-21 14:29:28 +01:00
spi-rockchip-sfc.c spi: rockchip-sfc: switch to use modern name 2023-08-21 14:29:29 +01:00
spi-rockchip.c spi: rockchip: Use helper function devm_clk_get_enabled() 2023-09-11 01:32:12 +01:00
spi-rpc-if.c spi: rpc-if: switch to use devm_spi_alloc_host() 2023-08-16 12:58:08 +01:00
spi-rspi.c spi: rspi: switch to use spi_alloc_host() 2023-08-21 14:29:30 +01:00
spi-rzv2m-csi.c spi: rzv2m-csi: Add target mode support 2023-10-09 13:14:28 +01:00
spi-s3c64xx.c spi: s3c64xx: switch to use modern name 2023-08-21 14:29:32 +01:00
spi-sc18is602.c spi: sc18is602: switch to use modern name 2023-08-21 14:29:33 +01:00
spi-sh-hspi.c spi: sh-hspi: switch to use modern name 2023-08-21 14:29:34 +01:00
spi-sh-msiof.c spi: sh-msiof: switch to use modern name 2023-08-21 14:29:35 +01:00
spi-sh-sci.c spi: sh-sci: switch to use modern name 2023-08-21 14:29:36 +01:00
spi-sh.c spi: sh: switch to use modern name 2023-08-21 14:29:37 +01:00
spi-sifive.c spi: sifive: switch to use modern name 2023-08-21 14:29:38 +01:00
spi-slave-mt27xx.c spi: Get rid of old SPI_MASTER_MUST_TX & SPI_MASTER_MUST_RX 2023-07-11 13:41:25 +01:00
spi-slave-system-control.c spi: make remove callback a void function 2022-02-09 13:00:45 +00:00
spi-slave-time.c spi: make remove callback a void function 2022-02-09 13:00:45 +00:00
spi-sn-f-ospi.c spi: spi-sn-f-ospi: switch to use modern name 2023-08-21 14:29:39 +01:00
spi-sprd-adi.c spi: Explicitly include correct DT includes 2023-07-14 20:53:00 +01:00
spi-sprd.c spi: Explicitly include correct DT includes 2023-07-14 20:53:00 +01:00
spi-st-ssc4.c spi: Replace all spi->chip_select and spi->cs_gpiod references with function call 2023-03-11 12:34:01 +00:00
spi-stm32-qspi.c spi: Explicitly include correct DT includes 2023-07-14 20:53:00 +01:00
spi-stm32.c spi: Merge up fix 2023-10-30 13:20:58 +00:00
spi-sun4i.c spi: Replace all spi->chip_select and spi->cs_gpiod references with function call 2023-03-11 12:34:01 +00:00
spi-sun6i.c spi: Fixes for v6.6 2023-09-07 15:49:20 -07:00
spi-sunplus-sp7021.c spi: sunplus-sp7021: Convert to platform remove callback returning void 2023-03-06 21:18:18 +00:00
spi-synquacer.c spi: Replace all spi->chip_select and spi->cs_gpiod references with function call 2023-03-11 12:34:01 +00:00
spi-tegra20-sflash.c spi: tegra20-sflash: fix to check return value of platform_get_irq() in tegra_sflash_probe() 2023-07-26 16:30:19 +01:00
spi-tegra20-slink.c spi: tegra: Fix missing IRQ check in tegra_slink_probe() 2023-09-11 01:32:20 +01:00
spi-tegra114.c spi: tegra114: Remove unnecessary NULL-pointer checks 2023-08-15 19:55:00 +01:00
spi-tegra210-quad.c spi: Explicitly include correct DT includes 2023-07-14 20:53:00 +01:00
spi-test.h
spi-ti-qspi.c spi: Explicitly include correct DT includes 2023-07-14 20:53:00 +01:00
spi-tle62x0.c spi: make remove callback a void function 2022-02-09 13:00:45 +00:00
spi-topcliff-pch.c spi: Replace all spi->chip_select and spi->cs_gpiod references with function call 2023-03-11 12:34:01 +00:00
spi-uniphier.c spi: uniphier: Convert to platform remove callback returning void 2023-03-06 21:18:24 +00:00
spi-wpcm-fiu.c spi: Explicitly include correct DT includes 2023-07-14 20:53:00 +01:00
spi-xcomm.c spi: Convert to SPI_CONTROLLER_HALF_DUPLEX 2023-07-11 14:14:31 +01:00
spi-xilinx.c spi: xilinx: Use devm_platform_get_and_ioremap_resource() 2023-03-28 14:49:37 +01:00
spi-xlp.c spi: Replace all spi->chip_select and spi->cs_gpiod references with function call 2023-03-11 12:34:01 +00:00
spi-xtensa-xtfpga.c spi: Get rid of old SPI_MASTER_NO_TX & SPI_MASTER_NO_RX 2023-07-11 13:41:20 +01:00
spi-zynq-qspi.c spi: spi-zynq: Do not check for 0 return after calling platform_get_irq() 2023-08-07 14:38:39 +01:00
spi-zynqmp-gqspi.c spi: Merge up old fix 2023-09-19 13:17:52 +01:00
spi.c spi: Fix null dereference on suspend 2023-11-10 13:54:58 +00:00
spidev.c spi: spidev: make spidev_class constant 2023-10-06 15:44:47 +01:00