From 146bea9b9b79f38047f14799c959280cea50c4bf Mon Sep 17 00:00:00 2001 From: Franck Jullien Date: Thu, 24 Nov 2011 21:46:34 +0100 Subject: [PATCH] spi/altera_spi: Add cs_change support The Linux kernel says (spi.h) : * All SPI transfers start with the relevant chipselect active. Normally * it stays selected until after the last transfer in a message. Drivers * can affect the chipselect signal using cs_change. * * (i) If the transfer isn't the last one in the message, this flag is * used to make the chipselect briefly go inactive in the middle of the * message. Toggling chipselect in this way may be needed to terminate * a chip command, letting a single spi_message perform all of group of * chip transactions together. * * (ii) When the transfer is the last one in the message, the chip may * stay selected until the next transfer. On multi-device SPI busses * with nothing blocking messages going to other devices, this is just * a performance hint; starting a message to another device deselects * this one. But in other cases, this can be used to ensure correctness. * Some devices need protocol transactions to be built from a series of * spi_message submissions, where the content of one message is * determined by the results of previous messages and where the whole * transaction * ends when the chipselect goes intactive. In other words, cs_change changes the default chipselect *behavior*. Support of cs_change is necessary to implement the mci spi driver. This patch also fix few things: Passing the bus number to the brand new master device. Disable chipselect during master->setup. Signed-off-by: Franck Jullien Signed-off-by: Sascha Hauer --- arch/nios2/include/asm/spi.h | 1 + drivers/spi/altera_spi.c | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/nios2/include/asm/spi.h b/arch/nios2/include/asm/spi.h index 4e576b992..6c5740c34 100644 --- a/arch/nios2/include/asm/spi.h +++ b/arch/nios2/include/asm/spi.h @@ -8,6 +8,7 @@ struct spi_altera_master { int spi_mode; int databits; int speed; + int bus_num; }; struct altera_spi { diff --git a/drivers/spi/altera_spi.c b/drivers/spi/altera_spi.c index 4fd82fb90..6cf9942d9 100644 --- a/drivers/spi/altera_spi.c +++ b/drivers/spi/altera_spi.c @@ -25,6 +25,9 @@ #include #include #include +#include + +static void altera_spi_cs_inactive(struct spi_device *spi); static int altera_spi_setup(struct spi_device *spi) { @@ -49,6 +52,8 @@ static int altera_spi_setup(struct spi_device *spi) return -1; } + altera_spi_cs_inactive(spi); + dev_dbg(master->dev, " mode 0x%08x, bits_per_word: %d, speed: %d\n", spi->mode, spi->bits_per_word, altera_spi->speed); @@ -167,19 +172,38 @@ static int altera_spi_transfer(struct spi_device *spi, struct spi_message *mesg) struct altera_spi *altera_spi = container_of(spi->master, struct altera_spi, master); struct nios_spi *nios_spi = altera_spi->regs; struct spi_transfer *t; + unsigned int cs_change; + const int nsecs = 50; altera_spi_cs_active(spi); + cs_change = 0; + mesg->actual_length = 0; list_for_each_entry(t, &mesg->transfers, transfer_list) { + + if (cs_change) { + ndelay(nsecs); + altera_spi_cs_inactive(spi); + ndelay(nsecs); + altera_spi_cs_active(spi); + } + + cs_change = t->cs_change; + mesg->actual_length += altera_spi_do_xfer(spi, t); + + if (cs_change) { + altera_spi_cs_active(spi); + } } /* Wait the end of any pending transfer */ while ((readl(&nios_spi->status) & NIOS_SPI_TMT) == 0); - altera_spi_cs_inactive(spi); + if (!cs_change) + altera_spi_cs_inactive(spi); return 0; } @@ -199,6 +223,7 @@ static int altera_spi_probe(struct device_d *dev) master->setup = altera_spi_setup; master->transfer = altera_spi_transfer; master->num_chipselect = pdata->num_chipselect; + master->bus_num = pdata->bus_num; altera_spi->regs = dev_request_mem_region(dev, 0); altera_spi->databits = pdata->databits;