1 From cde4384e1037c15e5dd04c68d19c75798b6281dd Mon Sep 17 00:00:00 2001
2 From: Florian Fainelli <florian@openwrt.org>
3 Date: Fri, 20 Apr 2012 15:37:33 +0200
4 Subject: [PATCH] spi/bcm63xx: convert to the pump message infrastructure
6 This patch converts the bcm63xx SPI driver to use the SPI infrastructure
7 pump message queue. Since we were previously sleeping in the SPI
8 driver's transfer() function (which is not allowed) this is now fixed as well.
10 To complete that conversion a certain number of changes have been made:
11 - the transfer len is split into multiple hardware transfers in case its
12 size is bigger than the hardware FIFO size
13 - the FIFO refill is no longer done in the interrupt context, which was a
14 bad idea leading to quick interrupt handler re-entrancy
16 Tested-by: Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>
17 Signed-off-by: Florian Fainelli <florian@openwrt.org>
18 Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
20 drivers/spi/spi-bcm63xx.c | 149 +++++++++++++++++++++++++++------------------
21 1 file changed, 89 insertions(+), 60 deletions(-)
23 --- a/drivers/spi/spi-bcm63xx.c
24 +++ b/drivers/spi/spi-bcm63xx.c
27 * Broadcom BCM63xx SPI controller support
29 - * Copyright (C) 2009-2011 Florian Fainelli <florian@openwrt.org>
30 + * Copyright (C) 2009-2012 Florian Fainelli <florian@openwrt.org>
31 * Copyright (C) 2010 Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>
33 * This program is free software; you can redistribute it and/or
35 #include <linux/spi/spi.h>
36 #include <linux/completion.h>
37 #include <linux/err.h>
38 +#include <linux/workqueue.h>
39 +#include <linux/pm_runtime.h>
41 #include <bcm63xx_dev_spi.h>
43 @@ -96,17 +98,12 @@ static const unsigned bcm63xx_spi_freq_t
44 { 391000, SPI_CLK_0_391MHZ }
47 -static int bcm63xx_spi_setup_transfer(struct spi_device *spi,
48 - struct spi_transfer *t)
49 +static int bcm63xx_spi_check_transfer(struct spi_device *spi,
50 + struct spi_transfer *t)
52 - struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
58 bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
59 - hz = (t) ? t->speed_hz : spi->max_speed_hz;
60 if (bits_per_word != 8) {
61 dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
62 __func__, bits_per_word);
63 @@ -119,6 +116,19 @@ static int bcm63xx_spi_setup_transfer(st
70 +static void bcm63xx_spi_setup_transfer(struct spi_device *spi,
71 + struct spi_transfer *t)
73 + struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
78 + hz = (t) ? t->speed_hz : spi->max_speed_hz;
80 /* Find the closest clock configuration */
81 for (i = 0; i < SPI_CLK_MASK; i++) {
82 if (hz >= bcm63xx_spi_freq_table[i][0]) {
83 @@ -139,8 +149,6 @@ static int bcm63xx_spi_setup_transfer(st
84 bcm_spi_writeb(bs, reg, SPI_CLK_CFG);
85 dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n",
91 /* the spi->mode bits understood by this driver: */
92 @@ -165,7 +173,7 @@ static int bcm63xx_spi_setup(struct spi_
96 - ret = bcm63xx_spi_setup_transfer(spi, NULL);
97 + ret = bcm63xx_spi_check_transfer(spi, NULL);
99 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
100 spi->mode & ~MODEBITS);
101 @@ -190,28 +198,29 @@ static void bcm63xx_spi_fill_tx_fifo(str
102 bs->remaining_bytes -= size;
105 -static int bcm63xx_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
106 +static unsigned int bcm63xx_txrx_bufs(struct spi_device *spi,
107 + struct spi_transfer *t)
109 struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
113 + /* Disable the CMD_DONE interrupt */
114 + bcm_spi_writeb(bs, 0, SPI_INT_MASK);
116 dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
117 t->tx_buf, t->rx_buf, t->len);
119 /* Transmitter is inhibited */
120 bs->tx_ptr = t->tx_buf;
121 bs->rx_ptr = t->rx_buf;
122 - init_completion(&bs->done);
125 bs->remaining_bytes = t->len;
126 bcm63xx_spi_fill_tx_fifo(bs);
129 - /* Enable the command done interrupt which
130 - * we use to determine completion of a command */
131 - bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
132 + init_completion(&bs->done);
134 /* Fill in the Message control register */
135 msg_ctl = (t->len << SPI_BYTE_CNT_SHIFT);
136 @@ -230,33 +239,76 @@ static int bcm63xx_txrx_bufs(struct spi_
137 cmd |= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
138 cmd |= (spi->chip_select << SPI_CMD_DEVICE_ID_SHIFT);
139 bcm_spi_writew(bs, cmd, SPI_CMD);
140 - wait_for_completion(&bs->done);
142 - /* Disable the CMD_DONE interrupt */
143 - bcm_spi_writeb(bs, 0, SPI_INT_MASK);
144 + /* Enable the CMD_DONE interrupt */
145 + bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
147 return t->len - bs->remaining_bytes;
150 -static int bcm63xx_transfer(struct spi_device *spi, struct spi_message *m)
151 +static int bcm63xx_spi_prepare_transfer(struct spi_master *master)
153 - struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
154 - struct spi_transfer *t;
156 + struct bcm63xx_spi *bs = spi_master_get_devdata(master);
158 - if (unlikely(list_empty(&m->transfers)))
160 + pm_runtime_get_sync(&bs->pdev->dev);
167 +static int bcm63xx_spi_unprepare_transfer(struct spi_master *master)
169 + struct bcm63xx_spi *bs = spi_master_get_devdata(master);
171 + pm_runtime_put(&bs->pdev->dev);
176 +static int bcm63xx_spi_transfer_one(struct spi_master *master,
177 + struct spi_message *m)
179 + struct bcm63xx_spi *bs = spi_master_get_devdata(master);
180 + struct spi_transfer *t;
181 + struct spi_device *spi = m->spi;
183 + unsigned int timeout = 0;
185 list_for_each_entry(t, &m->transfers, transfer_list) {
186 - ret += bcm63xx_txrx_bufs(spi, t);
188 + unsigned int len = t->len;
191 - m->complete(m->context);
192 + status = bcm63xx_spi_check_transfer(spi, t);
196 + /* configure adapter for a new transfer */
197 + bcm63xx_spi_setup_transfer(spi, t);
200 + /* send the data */
201 + len -= bcm63xx_txrx_bufs(spi, t);
203 + timeout = wait_for_completion_timeout(&bs->done, HZ);
205 + status = -ETIMEDOUT;
209 + /* read out all data */
210 + rx_tail = bcm_spi_readb(bs, SPI_RX_TAIL);
212 + /* Read out all the data */
214 + memcpy_fromio(bs->rx_ptr, bs->rx_io, rx_tail);
217 + m->actual_length += t->len;
220 + m->status = status;
221 + spi_finalize_current_message(master);
227 /* This driver supports single master mode only. Hence
228 @@ -267,39 +319,15 @@ static irqreturn_t bcm63xx_spi_interrupt
229 struct spi_master *master = (struct spi_master *)dev_id;
230 struct bcm63xx_spi *bs = spi_master_get_devdata(master);
234 /* Read interupts and clear them immediately */
235 intr = bcm_spi_readb(bs, SPI_INT_STATUS);
236 bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
237 bcm_spi_writeb(bs, 0, SPI_INT_MASK);
239 - /* A tansfer completed */
240 - if (intr & SPI_INTR_CMD_DONE) {
243 - rx_tail = bcm_spi_readb(bs, SPI_RX_TAIL);
245 - /* Read out all the data */
247 - memcpy_fromio(bs->rx_ptr, bs->rx_io, rx_tail);
249 - /* See if there is more data to send */
250 - if (bs->remaining_bytes > 0) {
251 - bcm63xx_spi_fill_tx_fifo(bs);
253 - /* Start the transfer */
254 - bcm_spi_writew(bs, SPI_HD_W << SPI_MSG_TYPE_SHIFT,
256 - cmd = bcm_spi_readw(bs, SPI_CMD);
257 - cmd |= SPI_CMD_START_IMMEDIATE;
258 - cmd |= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
259 - bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
260 - bcm_spi_writew(bs, cmd, SPI_CMD);
262 - complete(&bs->done);
265 + /* A transfer completed */
266 + if (intr & SPI_INTR_CMD_DONE)
267 + complete(&bs->done);
271 @@ -345,7 +373,6 @@ static int __devinit bcm63xx_spi_probe(s
274 bs = spi_master_get_devdata(master);
275 - init_completion(&bs->done);
277 platform_set_drvdata(pdev, master);
279 @@ -379,7 +406,9 @@ static int __devinit bcm63xx_spi_probe(s
280 master->bus_num = pdata->bus_num;
281 master->num_chipselect = pdata->num_chipselect;
282 master->setup = bcm63xx_spi_setup;
283 - master->transfer = bcm63xx_transfer;
284 + master->prepare_transfer_hardware = bcm63xx_spi_prepare_transfer;
285 + master->unprepare_transfer_hardware = bcm63xx_spi_unprepare_transfer;
286 + master->transfer_one_message = bcm63xx_spi_transfer_one;
287 bs->speed_hz = pdata->speed_hz;
289 bs->tx_io = (u8 *)(bs->regs + bcm63xx_spireg(SPI_MSG_DATA));