2008-11-18 07:57:41

by Bryan Wu

[permalink] [raw]
Subject: [PATCH 0/2] Introduce SPI locking bus interface to SPI framework


Hi Dave,

Yi introduced SPI locking bus interface to SPI framework. This is
required by some SPI devices in bus sharing situation, for example,
the MMC card driver over SPI.

We also use this API in our Blackfin SPI master driver and tested on
our own MMC over SPI driver. Since that MMC over SPI driver is different
with the mainline version, we will try to bring up the MMC_SPI driver by
adding these new SPI locking bus API.

Please kindly review.

Thanks
-Bryan


2008-11-18 07:57:59

by Bryan Wu

[permalink] [raw]
Subject: [PATCH 1/2] spi: Add two new APIs spi_lock_bus() and spi_unlock_bus() to spi framework.

From: Yi Li <[email protected]>

For some MMC card over SPI bus, it needs to lock the SPI bus for its own
use. The SPI transfer can not be interrupted by other SPI devices
which sharing the SPI bus with SPI MMC card.

This patch introduces 2 APIs for SPI bus locking operation.

Signed-off-by: Yi Li <[email protected]>
Signed-off-by: Bryan Wu <[email protected]>
---
drivers/spi/spi.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++
include/linux/spi/spi.h | 7 ++++++
2 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 3734dc9..03b8747 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -591,6 +591,54 @@ static void spi_complete(void *arg)
}

/**
+ * spi_lock_bus - lock SPI bus for exclusive access
+ * @@spi: device which want to lock the bus
+ * Context: any
+ *
+ * Once the caller owns exclusive access to the SPI bus,
+ * only messages for this device will be transferred.
+ * Messages for other devices are queued but not transferred until
+ * the bus owner unlock the bus.
+ *
+ * The caller may call spi_lock_bus() before spi_sync() or spi_async().
+ * So this call may be used in irq and other contexts which can't sleep,
+ * as well as from task contexts which can sleep.
+ *
+ * It returns zero on success, else a negative error code.
+ */
+int spi_lock_bus(struct spi_device *spi)
+{
+ if (spi->master->lock_bus)
+ return spi->master->lock_bus(spi);
+ else
+ return 0;
+}
+EXPORT_SYMBOL_GPL(spi_lock_bus);
+
+/**
+ * spi_unlock_bus - unlock SPI bus
+ * @@spi: device which want to unlock the bus
+ * Context: any
+ *
+ * The caller has called spi_lock_bus() to lock the bus. It calls
+ * spi_unlock_bus() to release the bus so messages for other devices
+ * can be transferred.
+ *
+ * If the caller did not call spi_lock_bus() before, spi_unlock_bus()
+ * should have no effect.
+ *
+ * It returns zero on success, else a negative error code.
+ */
+int spi_unlock_bus(struct spi_device *spi)
+{
+ if (spi->master->unlock_bus)
+ return spi->master->unlock_bus(spi);
+ else
+ return 0;
+}
+EXPORT_SYMBOL_GPL(spi_unlock_bus);
+
+/**
* spi_sync - blocking/synchronous SPI data transfers
* @spi: device with which data will be exchanged
* @message: describes the data transfers
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 4be01bb..c8819e3 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -209,6 +209,8 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
* the device whose settings are being modified.
* @transfer: adds a message to the controller's transfer queue.
* @cleanup: frees controller-specific state
+ * @lock_bus: lock SPI bus for exclusive access
+ * @unlock_bus: unlock SPI bus so other devices can access
*
* Each SPI master controller can communicate with one or more @spi_device
* children. These make a small bus, sharing MOSI, MISO and SCK signals
@@ -264,6 +266,9 @@ struct spi_master {

/* called on release() to free memory provided by spi_master */
void (*cleanup)(struct spi_device *spi);
+
+ int (*lock_bus)(struct spi_device *spi);
+ int (*unlock_bus)(struct spi_device *spi);
};

static inline void *spi_master_get_devdata(struct spi_master *master)
@@ -579,6 +584,8 @@ spi_async(struct spi_device *spi, struct spi_message *message)
*/

extern int spi_sync(struct spi_device *spi, struct spi_message *message);
+extern int spi_lock_bus(struct spi_device *spi);
+extern int spi_unlock_bus(struct spi_device *spi);

/**
* spi_write - SPI synchronous write
--
1.5.6.3

2008-11-18 07:58:26

by Bryan Wu

[permalink] [raw]
Subject: [PATCH 2/2] Blackfin SPI Driver: implement spi_lock_bus(), spi_unlock_bus() in blackfin spi controller driver.

From: Yi Li <[email protected]>

Signed-off-by: Yi Li <[email protected]>
Signed-off-by: Bryan Wu <[email protected]>
---
drivers/spi/spi_bfin5xx.c | 81 +++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/drivers/spi/spi_bfin5xx.c b/drivers/spi/spi_bfin5xx.c
index 0e3102a..28dd0fe 100644
--- a/drivers/spi/spi_bfin5xx.c
+++ b/drivers/spi/spi_bfin5xx.c
@@ -45,6 +45,8 @@ MODULE_LICENSE("GPL");
#define QUEUE_RUNNING 0
#define QUEUE_STOPPED 1

+#define BFIN_SPI_LOCK 1
+
struct driver_data {
/* Driver model hookup */
struct platform_device *pdev;
@@ -68,7 +70,10 @@ struct driver_data {
struct list_head queue;
int busy;
int run;
-
+#ifdef BFIN_SPI_LOCK
+ /* SPI bus is lock by a slave for exclusive access */
+ int locked;
+#endif
/* Message Transfer pump */
struct tasklet_struct pump_transfers;

@@ -932,6 +937,10 @@ static void pump_messages(struct work_struct *work)
{
struct driver_data *drv_data;
unsigned long flags;
+#ifdef BFIN_SPI_LOCK
+ int locked_cs = -1;
+ struct spi_message *next_msg = NULL, *msg = NULL;
+#endif

drv_data = container_of(work, struct driver_data, pump_messages);

@@ -950,10 +959,35 @@ static void pump_messages(struct work_struct *work)
return;
}

+#ifdef BFIN_SPI_LOCK
+ /* Extract head of queue */
+ next_msg = list_entry(drv_data->queue.next,
+ struct spi_message, queue);
+
+ if (drv_data->locked)
+ locked_cs = drv_data->locked;
+
+ /* Someone has locked the bus */
+ if (drv_data->locked && next_msg->spi->chip_select != locked_cs) {
+ list_for_each_entry(msg, &drv_data->queue, queue) {
+ if (msg->spi->chip_select == locked_cs) {
+ next_msg = msg;
+ break;
+ }
+ }
+ /* Do nothing even if there are messages for other devices */
+ if (next_msg->spi->chip_select != locked_cs) {
+ drv_data->busy = 0;
+ spin_unlock_irqrestore(&drv_data->lock, flags);
+ return;
+ }
+ }
+ drv_data->cur_msg = next_msg;
+#else
/* Extract head of queue */
drv_data->cur_msg = list_entry(drv_data->queue.next,
- struct spi_message, queue);
-
+ struct spi_message, queue);
+#endif
/* Setup the SSP using the per chip configuration */
drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
restore_state(drv_data);
@@ -982,6 +1016,39 @@ static void pump_messages(struct work_struct *work)
}

/*
+ * lock the spi bus for exclusive access
+ */
+static int lock_bus(struct spi_device *spi)
+{
+#ifdef BFIN_SPI_LOCK
+ struct driver_data *drv_data = spi_master_get_devdata(spi->master);
+ unsigned long flags;
+
+ spin_lock_irqsave(&drv_data->lock, flags);
+ if (drv_data->locked) {
+ spin_unlock_irqrestore(&drv_data->lock, flags);
+ return -ENOLCK;
+ }
+ drv_data->locked = spi->chip_select;
+ spin_unlock_irqrestore(&drv_data->lock, flags);
+#endif
+ return 0;
+}
+
+static int unlock_bus(struct spi_device *spi)
+{
+#ifdef BFIN_SPI_LOCK
+ struct driver_data *drv_data = spi_master_get_devdata(spi->master);
+ unsigned long flags;
+
+ spin_lock_irqsave(&drv_data->lock, flags);
+ drv_data->locked = 0;
+ spin_unlock_irqrestore(&drv_data->lock, flags);
+#endif
+ return 0;
+}
+
+/*
* got a msg to transfer, queue it in drv_data->queue.
* And kick off message pumper
*/
@@ -1188,6 +1255,9 @@ static inline int init_queue(struct driver_data *drv_data)
INIT_LIST_HEAD(&drv_data->queue);
spin_lock_init(&drv_data->lock);

+#ifdef BFIN_SPI_LOCK
+ drv_data->locked = 0;
+#endif
drv_data->run = QUEUE_STOPPED;
drv_data->busy = 0;

@@ -1235,6 +1305,9 @@ static inline int stop_queue(struct driver_data *drv_data)

spin_lock_irqsave(&drv_data->lock, flags);

+#ifdef BFIN_SPI_LOCK
+ drv_data->locked = 0;
+#endif
/*
* This is a bit lame, but is optimized for the common execution path.
* A wait_queue on the drv_data->busy could be used, but then the common
@@ -1298,6 +1371,8 @@ static int __init bfin5xx_spi_probe(struct platform_device *pdev)
master->cleanup = cleanup;
master->setup = setup;
master->transfer = transfer;
+ master->lock_bus = lock_bus;
+ master->unlock_bus = unlock_bus;

/* Find and map our resources */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
--
1.5.6.3