2222#include <linux/gpio/consumer.h>
2323#include <linux/pinctrl/consumer.h>
2424#include <linux/pm_runtime.h>
25+ #include <linux/iopoll.h>
2526#include <trace/events/spi.h>
2627
2728/* SPI register offsets */
233234 */
234235#define DMA_MIN_BYTES 16
235236
236- #define SPI_DMA_MIN_TIMEOUT (msecs_to_jiffies(1000))
237- #define SPI_DMA_TIMEOUT_PER_10K (msecs_to_jiffies(4))
238-
239237#define AUTOSUSPEND_TIMEOUT 2000
240238
241239struct atmel_spi_caps {
@@ -279,6 +277,7 @@ struct atmel_spi {
279277 bool keep_cs ;
280278
281279 u32 fifo_size ;
280+ bool last_polarity ;
282281 u8 native_cs_free ;
283282 u8 native_cs_for_gpio ;
284283};
@@ -291,6 +290,22 @@ struct atmel_spi_device {
291290#define SPI_MAX_DMA_XFER 65535 /* true for both PDC and DMA */
292291#define INVALID_DMA_ADDRESS 0xffffffff
293292
293+ /*
294+ * This frequency can be anything supported by the controller, but to avoid
295+ * unnecessary delay, the highest possible frequency is chosen.
296+ *
297+ * This frequency is the highest possible which is not interfering with other
298+ * chip select registers (see Note for Serial Clock Bit Rate configuration in
299+ * Atmel-11121F-ATARM-SAMA5D3-Series-Datasheet_02-Feb-16, page 1283)
300+ */
301+ #define DUMMY_MSG_FREQUENCY 0x02
302+ /*
303+ * 8 bits is the minimum data the controller is capable of sending.
304+ *
305+ * This message can be anything as it should not be treated by any SPI device.
306+ */
307+ #define DUMMY_MSG 0xAA
308+
294309/*
295310 * Version 2 of the SPI controller has
296311 * - CR.LASTXFER
@@ -304,6 +319,43 @@ static bool atmel_spi_is_v2(struct atmel_spi *as)
304319 return as -> caps .is_spi2 ;
305320}
306321
322+ /*
323+ * Send a dummy message.
324+ *
325+ * This is sometimes needed when using a CS GPIO to force clock transition when
326+ * switching between devices with different polarities.
327+ */
328+ static void atmel_spi_send_dummy (struct atmel_spi * as , struct spi_device * spi , int chip_select )
329+ {
330+ u32 status ;
331+ u32 csr ;
332+
333+ /*
334+ * Set a clock frequency to allow sending message on SPI bus.
335+ * The frequency here can be anything, but is needed for
336+ * the controller to send the data.
337+ */
338+ csr = spi_readl (as , CSR0 + 4 * chip_select );
339+ csr = SPI_BFINS (SCBR , DUMMY_MSG_FREQUENCY , csr );
340+ spi_writel (as , CSR0 + 4 * chip_select , csr );
341+
342+ /*
343+ * Read all data coming from SPI bus, needed to be able to send
344+ * the message.
345+ */
346+ spi_readl (as , RDR );
347+ while (spi_readl (as , SR ) & SPI_BIT (RDRF )) {
348+ spi_readl (as , RDR );
349+ cpu_relax ();
350+ }
351+
352+ spi_writel (as , TDR , DUMMY_MSG );
353+
354+ readl_poll_timeout_atomic (as -> regs + SPI_SR , status ,
355+ (status & SPI_BIT (TXEMPTY )), 1 , 1000 );
356+ }
357+
358+
307359/*
308360 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
309361 * they assume that spi slave device state will not change on deselect, so
@@ -320,11 +372,17 @@ static bool atmel_spi_is_v2(struct atmel_spi *as)
320372 * Master on Chip Select 0.") No workaround exists for that ... so for
321373 * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
322374 * and (c) will trigger that first erratum in some cases.
375+ *
376+ * When changing the clock polarity, the SPI controller waits for the next
377+ * transmission to enforce the default clock state. This may be an issue when
378+ * using a GPIO as Chip Select: the clock level is applied only when the first
379+ * packet is sent, once the CS has already been asserted. The workaround is to
380+ * avoid this by sending a first (dummy) message before toggling the CS state.
323381 */
324-
325382static void cs_activate (struct atmel_spi * as , struct spi_device * spi )
326383{
327384 struct atmel_spi_device * asd = spi -> controller_state ;
385+ bool new_polarity ;
328386 int chip_select ;
329387 u32 mr ;
330388
@@ -353,6 +411,25 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
353411 }
354412
355413 mr = spi_readl (as , MR );
414+
415+ /*
416+ * Ensures the clock polarity is valid before we actually
417+ * assert the CS to avoid spurious clock edges to be
418+ * processed by the spi devices.
419+ */
420+ if (spi_get_csgpiod (spi , 0 )) {
421+ new_polarity = (asd -> csr & SPI_BIT (CPOL )) != 0 ;
422+ if (new_polarity != as -> last_polarity ) {
423+ /*
424+ * Need to disable the GPIO before sending the dummy
425+ * message because it is already set by the spi core.
426+ */
427+ gpiod_set_value_cansleep (spi_get_csgpiod (spi , 0 ), 0 );
428+ atmel_spi_send_dummy (as , spi , chip_select );
429+ as -> last_polarity = new_polarity ;
430+ gpiod_set_value_cansleep (spi_get_csgpiod (spi , 0 ), 1 );
431+ }
432+ }
356433 } else {
357434 u32 cpol = (spi -> mode & SPI_CPOL ) ? SPI_BIT (CPOL ) : 0 ;
358435 int i ;
@@ -1336,12 +1413,10 @@ static int atmel_spi_one_transfer(struct spi_controller *host,
13361413 }
13371414
13381415 dma_timeout = msecs_to_jiffies (spi_controller_xfer_timeout (host , xfer ));
1339- ret_timeout = wait_for_completion_interruptible_timeout (& as -> xfer_completion ,
1340- dma_timeout );
1341- if (ret_timeout <= 0 ) {
1342- dev_err (& spi -> dev , "spi transfer %s\n" ,
1343- !ret_timeout ? "timeout" : "canceled" );
1344- as -> done_status = ret_timeout < 0 ? ret_timeout : - EIO ;
1416+ ret_timeout = wait_for_completion_timeout (& as -> xfer_completion , dma_timeout );
1417+ if (!ret_timeout ) {
1418+ dev_err (& spi -> dev , "spi transfer timeout\n" );
1419+ as -> done_status = - EIO ;
13451420 }
13461421
13471422 if (as -> done_status )
0 commit comments