linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* atmel_spi clock polarity
@ 2008-02-16 13:32 Atsushi Nemoto
       [not found] ` <20080216.223252.25909396.anemo-7JcRY8pycbNHfZP73Gtkiw@public.gmane.org>
  0 siblings, 1 reply; 12+ messages in thread
From: Atsushi Nemoto @ 2008-02-16 13:32 UTC (permalink / raw)
  To: Haavard Skinnemoen; +Cc: David Brownell, spi-devel-general, linux-kernel

I found that atmel_spi driver does not initialize clock polarity
correctly (except for at91rm9200 CS0 channel) in some case.

The atmel_spi driver uses gpio-controlled chipselect.  OTOH spi clock
signal is controlled by CSRn.CPOL bit, but this register controls
clock signal correctly only in 'real transfer' duration.  At the time
of cs_activate() call, CSRn.CPOL will be initialized correctly, but
the controller do not know which channel is to be used next, so clock
signal will stay at the inactive state of last transfer.  If clock
polarity of new transfer and last transfer was differ, new transfer
will start with wrong clock signal state.

For example, if you started SPI MODE 2 or 3 transfer after SPI MODE 0
or 1 transfer, the clock signal state at the assertion of chipselect
will be low.  Of course this will violates SPI transfer.


Here is my quick workaround for this problem.  It makes all CSRn.CPOL
match for the transfer before activating chipselect.  I'm not quite
sure my analysis is correct, and there might be better solution.
Could you give me any comments?


diff --git a/drivers/spi/atmel_spi.c b/drivers/spi/atmel_spi.c
index 293b7ca..85687aa 100644
--- a/drivers/spi/atmel_spi.c
+++ b/drivers/spi/atmel_spi.c
@@ -87,6 +87,16 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
 	unsigned gpio = (unsigned) spi->controller_data;
 	unsigned active = spi->mode & SPI_CS_HIGH;
 	u32 mr;
+	int i;
+	u32 csr;
+	u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
+
+	/* Make sure clock polarity is correct */
+	for (i = 0; i < spi->master->num_chipselect; i++) {
+		csr = spi_readl(as, CSR0 + 4 * i);
+		if ((csr ^ cpol) & SPI_BIT(CPOL))
+			spi_writel(as, CSR0 + 4 * i, csr ^ SPI_BIT(CPOL));
+	}
 
 	mr = spi_readl(as, MR);
 	mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);

---
Atsushi Nemoto

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2008-02-21  1:33 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-16 13:32 atmel_spi clock polarity Atsushi Nemoto
     [not found] ` <20080216.223252.25909396.anemo-7JcRY8pycbNHfZP73Gtkiw@public.gmane.org>
2008-02-18 11:42   ` Haavard Skinnemoen
2008-02-18 14:12     ` Atsushi Nemoto
     [not found]       ` <20080218.231243.41197917.anemo-7JcRY8pycbNHfZP73Gtkiw@public.gmane.org>
2008-02-18 14:31         ` Haavard Skinnemoen
     [not found]           ` <20080218153158.2ff8842a-STKoQgiEtS/cug01aK12UsGzbamoMwWuEvhb3Hwu1Ks@public.gmane.org>
2008-02-20  5:21             ` Atsushi Nemoto
     [not found]               ` <20080220.142109.11595419.nemoto-IGagC74glE2asRnM1LW+pc8NsWr+9BEh@public.gmane.org>
2008-02-20  9:34                 ` Haavard Skinnemoen
     [not found]                   ` <20080220103446.1621c81f-STKoQgiEtS/cug01aK12UsGzbamoMwWuEvhb3Hwu1Ks@public.gmane.org>
2008-02-20  9:50                     ` Atsushi Nemoto
     [not found]                       ` <20080220.185045.08317377.nemoto-IGagC74glE2asRnM1LW+pc8NsWr+9BEh@public.gmane.org>
2008-02-21  1:33                         ` David Brownell
2008-02-21  1:32                     ` David Brownell
2008-02-18 19:57         ` David Brownell
     [not found]           ` <200802181157.57299.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
2008-02-18 22:49             ` Haavard Skinnemoen
2008-02-19 14:51               ` Atsushi Nemoto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).