linux-stable/drivers/spi
Louis Chauvet e21b5fc5b8 spi: atmel: Fix clock issue when using devices with different polarities
[ Upstream commit fc70d643a2 ]

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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-01-05 15:18:38 +01:00
..
atmel-quadspi.c spi: atmel-quadspi: Free resources even if runtime resume failed in .remove() 2023-05-11 23:03:27 +09:00
internals.h
Kconfig spi: spi-zynq-qspi: add spi-mem to driver kconfig dependencies 2023-11-20 11:52:18 +01:00
Makefile spi: microchip-core-qspi: Add support for microchip fpga qspi controllers 2022-08-15 12:17:38 +01:00
spi-altera-core.c
spi-altera-dfl.c spi: spi-altera-dfl: Fix an error handling path 2022-06-06 12:39:16 +01:00
spi-altera-platform.c
spi-amd.c spi: amd: Fix SPI_SPD7 value 2022-11-04 12:25:03 +00:00
spi-ar934x.c spi: ar934x: fix transfer size 2022-01-04 14:59:40 +00:00
spi-armada-3700.c spi: a3700: support BE for AC5 SPI driver 2022-07-26 16:51:07 +01:00
spi-aspeed-smc.c spi: aspeed: Fix window offset of CE1 2022-10-19 14:36:43 +01:00
spi-at91-usart.c
spi-ath79.c spi: ath79: add mem_ops for fast-read 2022-02-08 13:40:18 +00:00
spi-atmel.c spi: atmel: Fix clock issue when using devices with different polarities 2024-01-05 15:18:38 +01:00
spi-au1550.c spi: spi-au1550: replace ternary operator with min() 2022-05-16 17:57:01 +01:00
spi-axi-spi-engine.c
spi-bcm-qspi.c spi: bcm-qspi: return error if neither hif_mspi nor mspi is available 2023-07-19 16:22:03 +02:00
spi-bcm-qspi.h
spi-bcm63xx-hsspi.c spi: bcm63xx-hsspi: Fix multi-bit mode setting 2023-03-10 09:33:22 +01:00
spi-bcm63xx.c spi: bcm63xx: fix max prepend length 2023-07-27 08:50:38 +02:00
spi-bcm2835.c spi: Updates for v5.20 2022-08-02 10:55:04 -07:00
spi-bcm2835aux.c spi: bcm2835aux: Convert to use GPIO descriptors 2022-02-01 16:08:47 +00:00
spi-bitbang-txrx.h spi: bitbang: Fix lsb-first Rx 2022-08-15 01:26:20 +01:00
spi-bitbang.c
spi-brcmstb-qspi.c
spi-butterfly.c
spi-cadence-quadspi.c spi: cadence-quadspi: Add missing check for dma_set_mask 2023-06-21 16:00:58 +02:00
spi-cadence-xspi.c spi: cadence: Fix busy cycles calculation 2023-02-01 08:34:28 +01:00
spi-cadence.c spi: spi-cadence: Fix SPI NO Slave Select macro definition 2022-07-20 18:45:21 +01:00
spi-cavium-octeon.c
spi-cavium-thunderx.c
spi-cavium.c
spi-cavium.h
spi-clps711x.c spi: clps711x: Use syscon_regmap_lookup_by_phandle 2022-04-25 14:01:09 +01:00
spi-coldfire-qspi.c
spi-davinci.c
spi-dln2.c spi: dln2: Propagate firmware node 2021-12-23 13:38:38 +00:00
spi-dw-bt1.c spi: dw: Fix PM disable depth imbalance in dw_spi_bt1_probe 2022-09-26 16:10:36 +01:00
spi-dw-core.c spi: dw: Round of n_bytes to power of 2 2023-07-19 16:21:05 +02:00
spi-dw-dma.c spi: dw-dma: decrease reference count in dw_spi_dma_init_mfld() 2022-11-16 13:43:03 +00:00
spi-dw-mmio.c spi: dw: Remove misleading comment for Mount Evans SoC 2023-07-27 08:50:50 +02:00
spi-dw-pci.c spi: dw: Put the driver entities naming in order 2021-11-16 14:30:05 +00:00
spi-dw.h spi: dw: Add support for Intel Thunder Bay SPI controller 2022-07-13 13:32:32 +01:00
spi-ep93xx.c
spi-falcon.c
spi-fsi.c spi: fsi: Increase timeout and ensure status is checked 2022-06-23 15:48:21 +01:00
spi-fsl-cpm.c spi: fsl-cpm: Use 16 bit mode for large transfers with even size 2023-05-17 11:54:00 +02:00
spi-fsl-cpm.h
spi-fsl-dspi.c spi: fsl-dspi: avoid SCK glitches with continuous transfers 2023-06-21 16:00:58 +02:00
spi-fsl-espi.c
spi-fsl-lib.c
spi-fsl-lib.h
spi-fsl-lpspi.c spi: lpspi: disable lpspi module irq in DMA mode 2023-06-28 11:12:37 +02:00
spi-fsl-qspi.c spi: spi-fsl-qspi: Use devm_platform_ioremap_resource_byname() 2022-09-26 16:10:41 +01:00
spi-fsl-spi.c spi: fsl-cpm: Use 16 bit mode for large transfers with even size 2023-05-17 11:54:00 +02:00
spi-fsl-spi.h
spi-geni-qcom.c spi: spi-geni-qcom: enable SPI_CONTROLLER_MUST_TX for GPI DMA mode 2023-07-19 16:22:01 +02:00
spi-gpio.c spi: spi-gpio: Don't set MOSI as an input if not 3WIRE mode 2022-12-31 13:32:25 +01:00
spi-gxp.c spi: spi-gxp: BUG: Correct spi write return value 2023-10-06 14:56:46 +02:00
spi-hisi-kunpeng.c spi: hisi-kunpeng: Fix the debugfs directory name incorrect 2021-11-17 13:04:54 +00:00
spi-hisi-sfc-v3xx.c
spi-img-spfi.c spi: img-spfi: using pm_runtime_resume_and_get instead of pm_runtime_get_sync 2022-09-23 18:21:48 +01:00
spi-imx.c spi: spi-imx: fix MX51_ECSPI_* macros when cs > 3 2023-05-24 17:32:40 +01:00
spi-ingenic.c spi: ingenic: Add support for new Ingenic SoCs. 2022-04-25 14:00:51 +01:00
spi-intel-pci.c spi: intel-pci: Add support for Granite Rapids SPI serial flash 2023-10-06 14:56:55 +02: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: Check number of chip selects after reading the descriptor 2023-03-17 08:50:20 +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-jcore.c
spi-lantiq-ssc.c spi: Use of_device_get_match_data() 2022-02-21 13:25:19 +00:00
spi-lm70llp.c
spi-loopback-test.c spi: spi-loopback-test: Add test to trigger DMA/PIO mixing 2022-09-19 15:44:19 +01:00
spi-lp8841-rtc.c
spi-mem.c spi: spi-mem: Fix spi_mem_poll_status() 2022-06-06 12:32:27 +01:00
spi-meson-spicc.c spi: meson-spicc: fix do_div build error on non-arm64 2022-11-02 12:27:15 +00:00
spi-meson-spifc.c spi: spi-meson-spifc: Add missing pm_runtime_disable() in meson_spifc_probe 2022-01-07 13:36:38 +00:00
spi-microchip-core-qspi.c spi: microchip-core-qspi: Add support for microchip fpga qspi controllers 2022-08-15 12:17:38 +01:00
spi-microchip-core.c spi: microchip-core: Simplify some error message 2022-08-15 12:18:41 +01:00
spi-mpc52xx-psc.c spi: mpc52xx-psc: Switch to using core message queue 2022-06-27 13:24:36 +01:00
spi-mpc52xx.c spi: mpc52xx: Replace NO_IRQ by 0 2022-10-10 13:01:20 +01:00
spi-mpc512x-psc.c spi: mpc512x-psc: Fix compile errors 2022-02-02 16:09:29 +00:00
spi-mt65xx.c spi: mt65xx: make sure operations completed before unloading 2023-06-14 11:15:15 +02:00
spi-mt7621.c spi: mt7621: Remove 'clk' from 'struct mt7621_spi' 2022-08-29 17:51:39 +01:00
spi-mtk-nor.c spi: spi-mtk-nor: initialize spi controller after resume 2022-04-12 17:18:51 +01:00
spi-mtk-snfi.c spi: mtk-snfi: preserve dma_mapping_error() error codes 2022-05-03 14:00:38 +01:00
spi-mux.c spi: mux: Fix mux interaction with fast path optimisations 2022-09-02 13:27:13 +01:00
spi-mxic.c spi: mxic: Fix an error handling path in mxic_spi_probe() 2022-04-04 08:45:14 +01:00
spi-mxs.c spi: mxs: using pm_runtime_resume_and_get instead of pm_runtime_get_sync 2022-04-25 14:00:59 +01:00
spi-npcm-fiu.c spi: npcm-fiu: Fix UMA reads when dummy.nbytes == 0 2023-11-08 14:10:59 +01:00
spi-npcm-pspi.c spi: npcm-pspi: Add NPCM845 peripheral SPI support 2022-08-15 12:17:29 +01:00
spi-nxp-fspi.c spi: nxp-fspi: use the correct ioremap function 2023-11-20 11:51:56 +01:00
spi-oc-tiny.c
spi-omap-100k.c spi/omap100k:Fix PM disable depth imbalance in omap1_spi100k_probe 2022-09-26 16:10:37 +01:00
spi-omap-uwire.c ARM: omap1: move perseus spi pinconf to board file 2022-04-21 15:01:08 +02:00
spi-omap2-mcspi.c spi: omap2-mcspi: Fix probe so driver works again 2022-09-21 10:28:20 +01:00
spi-orion.c
spi-pic32-sqi.c
spi-pic32.c spi: pic32: Convert to use GPIO descriptors 2022-01-31 15:17:30 +00:00
spi-pl022.c
spi-ppc4xx.c
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: Updates for v6.1 2022-10-04 19:36:53 -07:00
spi-pxa2xx.h spi: pxa2xx: Get rid of unused ->cs_control() 2021-11-29 12:19:59 +00:00
spi-qcom-qspi.c
spi-qup.c spi: qup: Request DMA before enabling clocks 2023-06-14 11:15:15 +02:00
spi-rb4xx.c
spi-realtek-rtl.c
spi-rockchip-sfc.c spi: spi-rockchip: Fix missing unwind goto in rockchip_sfc_probe() 2023-04-26 14:28:34 +02:00
spi-rockchip.c spi: rockchip: Unmask IRQ at the final to avoid preemption 2022-06-20 11:35:43 +01:00
spi-rpc-if.c spi: rpc-if: Fix RPM imbalance in probe error path 2022-04-04 08:38:59 +01:00
spi-rspi.c spi: spi-rspi: Fix PIO fallback on RZ platforms 2022-07-21 17:21:07 +01:00
spi-s3c24xx-regs.h
spi-s3c24xx.c spi: s3c24xx: Switch to use devm_spi_alloc_master() 2022-09-23 18:21:50 +01:00
spi-s3c64xx.c spi: s3c64xx: clear loopback bit after loopback test 2023-07-27 08:50:39 +02:00
spi-sc18is602.c
spi-sh-hspi.c
spi-sh-msiof.c spi: sh-msiof: add generic Gen4 binding 2022-08-29 12:41:36 +01:00
spi-sh-sci.c
spi-sh.c spi: sh: Switch to using core message queue 2022-06-27 13:24:35 +01:00
spi-sifive.c spi: sifive: add PM callbacks to support suspend/resume 2022-06-10 13:32:24 +01:00
spi-slave-mt27xx.c
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-sprd-adi.c
spi-sprd.c spi: spi-sprd: using pm_runtime_resume_and_get instead of pm_runtime_get_sync 2022-04-25 14:00:57 +01:00
spi-st-ssc4.c spi: st-ssc4: Covert to use GPIO descriptors 2022-02-01 16:08:45 +00:00
spi-stm32-qspi.c spi: stm32-qspi: Refactor dual flash mode enable check in ->setup() 2022-08-31 12:13:11 +01:00
spi-stm32.c spi: stm32: add a delay before SPI disable 2023-10-06 14:56:55 +02:00
spi-sun4i.c spi: sun4i: fix typos in comments 2022-03-15 12:03:58 +00:00
spi-sun6i.c spi: sun6i: fix race between DMA RX transfer completion and RX FIFO drain 2023-10-06 14:56:51 +02:00
spi-sunplus-sp7021.c spi: remove spin_lock_irq and variable in the irq procress 2022-04-19 13:13:42 +01:00
spi-synquacer.c spi: synquacer: Fix timeout handling in synquacer_spi_transfer_one() 2023-03-10 09:33:23 +01:00
spi-tegra20-sflash.c spi: tegra20-sflash: fix to check return value of platform_get_irq() in tegra_sflash_probe() 2023-09-13 09:42:31 +02:00
spi-tegra20-slink.c spi: tegra: Fix missing IRQ check in tegra_slink_probe() 2023-11-20 11:51:55 +01:00
spi-tegra114.c spi: spi-tegra114: using pm_runtime_resume_and_get instead of pm_runtime_get_sync 2022-04-19 13:13:44 +01:00
spi-tegra210-quad.c spi: tegra210-quad: Fix iterator outside loop 2023-03-11 13:55:29 +01:00
spi-test.h
spi-ti-qspi.c spi: spi-ti-qspi: Support per-transfer and per-slave speed_hz settings 2022-06-06 12:41:36 +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: topcliff-pch: Use core message validation 2022-06-20 12:43:36 +01:00
spi-uniphier.c spi: uniphier: fix reference count leak in uniphier_spi_probe() 2022-01-26 15:52:05 +00:00
spi-xcomm.c
spi-xilinx.c spi: xilinx: Switch to use devm_spi_alloc_master() 2022-09-23 18:21:49 +01:00
spi-xlp.c spi: xlp: Remove Netlogic XLP variants 2021-11-15 13:27:17 +00:00
spi-xtensa-xtfpga.c spi: xtensa-xtfpga: Switch to use devm_spi_alloc_master() 2022-09-22 14:39:50 +01:00
spi-zynq-qspi.c spi: spi-zynq-qspi: Fix a NULL pointer dereference in zynq_qspi_exec_mem_op() 2022-02-08 13:37:50 +00:00
spi-zynqmp-gqspi.c spi: zynqmp-gqspi: fix clock imbalance on probe failure 2023-10-10 22:00:34 +02:00
spi.c spi: Add APIs in spi core to set/get spi->chip_select and spi->cs_gpiod 2024-01-05 15:18:38 +01:00
spidev.c spi: spidev: remove debug messages that access spidev->spi without locking 2023-02-01 08:34:32 +01:00