Skip to content

Commit e34ff01

Browse files
msperlbroonie
authored andcommitted
spi: bcm2835: move to the transfer_one driver model
This also allows for GPIO-CS to get used removing the limitation of 2/3 SPI devises on the SPI bus. Fixes: spi-cs-high with native CS with multiple devices on the spi-bus resetting the chip selects to "normal" polarity after a finished transfer. No other functionality/improvements added. Tested with the following 4 devices on the spi-bus: * mcp2515 with native CS * mcp2515 with gpio CS * fb_st7735r with native CS (plus spi-cs-high via transistor inverting polarity) * enc28j60 with gpio-CS Tested-by: Martin Sperl <[email protected]> Signed-off-by: Martin Sperl <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent 1be38e0 commit e34ff01

File tree

1 file changed

+124
-88
lines changed

1 file changed

+124
-88
lines changed

drivers/spi/spi-bcm2835.c

Lines changed: 124 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Copyright (C) 2012 Chris Boot
55
* Copyright (C) 2013 Stephen Warren
6+
* Copyright (C) 2015 Martin Sperl
67
*
78
* This driver is inspired by:
89
* spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <[email protected]>
@@ -29,6 +30,7 @@
2930
#include <linux/module.h>
3031
#include <linux/of.h>
3132
#include <linux/of_irq.h>
33+
#include <linux/of_gpio.h>
3234
#include <linux/of_device.h>
3335
#include <linux/spi/spi.h>
3436

@@ -76,10 +78,10 @@ struct bcm2835_spi {
7678
void __iomem *regs;
7779
struct clk *clk;
7880
int irq;
79-
struct completion done;
8081
const u8 *tx_buf;
8182
u8 *rx_buf;
82-
int len;
83+
int tx_len;
84+
int rx_len;
8385
};
8486

8587
static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg)
@@ -96,58 +98,73 @@ static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs)
9698
{
9799
u8 byte;
98100

99-
while (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD) {
101+
while ((bs->rx_len) &&
102+
(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
100103
byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
101104
if (bs->rx_buf)
102105
*bs->rx_buf++ = byte;
106+
bs->rx_len--;
103107
}
104108
}
105109

106110
static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
107111
{
108112
u8 byte;
109113

110-
while ((bs->len) &&
114+
while ((bs->tx_len) &&
111115
(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
112116
byte = bs->tx_buf ? *bs->tx_buf++ : 0;
113117
bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
114-
bs->len--;
118+
bs->tx_len--;
115119
}
116120
}
117121

122+
static void bcm2835_spi_reset_hw(struct spi_master *master)
123+
{
124+
struct bcm2835_spi *bs = spi_master_get_devdata(master);
125+
u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
126+
127+
/* Disable SPI interrupts and transfer */
128+
cs &= ~(BCM2835_SPI_CS_INTR |
129+
BCM2835_SPI_CS_INTD |
130+
BCM2835_SPI_CS_TA);
131+
/* and reset RX/TX FIFOS */
132+
cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
133+
134+
/* and reset the SPI_HW */
135+
bcm2835_wr(bs, BCM2835_SPI_CS, cs);
136+
}
137+
118138
static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
119139
{
120140
struct spi_master *master = dev_id;
121141
struct bcm2835_spi *bs = spi_master_get_devdata(master);
122-
u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
123142

124143
/* Read as many bytes as possible from FIFO */
125144
bcm2835_rd_fifo(bs);
126-
127-
if (bs->len) { /* there is more data to transmit */
128-
bcm2835_wr_fifo(bs);
129-
} else { /* Transfer complete */
130-
/* Disable SPI interrupts */
131-
cs &= ~(BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD);
132-
bcm2835_wr(bs, BCM2835_SPI_CS, cs);
133-
134-
/*
135-
* Wake up bcm2835_spi_transfer_one(), which will call
136-
* bcm2835_spi_finish_transfer(), to drain the RX FIFO.
137-
*/
138-
complete(&bs->done);
145+
/* Write as many bytes as possible to FIFO */
146+
bcm2835_wr_fifo(bs);
147+
148+
/* based on flags decide if we can finish the transfer */
149+
if (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE) {
150+
/* Transfer complete - reset SPI HW */
151+
bcm2835_spi_reset_hw(master);
152+
/* wake up the framework */
153+
complete(&master->xfer_completion);
139154
}
140155

141156
return IRQ_HANDLED;
142157
}
143158

144-
static int bcm2835_spi_start_transfer(struct spi_device *spi,
145-
struct spi_transfer *tfr)
159+
static int bcm2835_spi_transfer_one(struct spi_master *master,
160+
struct spi_device *spi,
161+
struct spi_transfer *tfr)
146162
{
147-
struct bcm2835_spi *bs = spi_master_get_devdata(spi->master);
163+
struct bcm2835_spi *bs = spi_master_get_devdata(master);
148164
unsigned long spi_hz, clk_hz, cdiv;
149-
u32 cs = BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
165+
u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
150166

167+
/* set clock */
151168
spi_hz = tfr->speed_hz;
152169
clk_hz = clk_get_rate(bs->clk);
153170

@@ -163,100 +180,118 @@ static int bcm2835_spi_start_transfer(struct spi_device *spi,
163180
} else {
164181
cdiv = 0; /* 0 is the slowest we can go */
165182
}
183+
bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
166184

185+
/* handle all the modes */
167186
if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf))
168187
cs |= BCM2835_SPI_CS_REN;
169-
170188
if (spi->mode & SPI_CPOL)
171189
cs |= BCM2835_SPI_CS_CPOL;
172190
if (spi->mode & SPI_CPHA)
173191
cs |= BCM2835_SPI_CS_CPHA;
174192

175-
if (!(spi->mode & SPI_NO_CS)) {
176-
if (spi->mode & SPI_CS_HIGH) {
177-
cs |= BCM2835_SPI_CS_CSPOL;
178-
cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select;
179-
}
180-
181-
cs |= spi->chip_select;
182-
}
193+
/* for gpio_cs set dummy CS so that no HW-CS get changed
194+
* we can not run this in bcm2835_spi_set_cs, as it does
195+
* not get called for cs_gpio cases, so we need to do it here
196+
*/
197+
if (gpio_is_valid(spi->cs_gpio) || (spi->mode & SPI_NO_CS))
198+
cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
183199

184-
reinit_completion(&bs->done);
200+
/* set transmit buffers and length */
185201
bs->tx_buf = tfr->tx_buf;
186202
bs->rx_buf = tfr->rx_buf;
187-
bs->len = tfr->len;
203+
bs->tx_len = tfr->len;
204+
bs->rx_len = tfr->len;
188205

189-
bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
190206
/*
191207
* Enable the HW block. This will immediately trigger a DONE (TX
192208
* empty) interrupt, upon which we will fill the TX FIFO with the
193209
* first TX bytes. Pre-filling the TX FIFO here to avoid the
194210
* interrupt doesn't work:-(
195211
*/
212+
cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
196213
bcm2835_wr(bs, BCM2835_SPI_CS, cs);
197214

198-
return 0;
215+
/* signal that we need to wait for completion */
216+
return 1;
199217
}
200218

201-
static int bcm2835_spi_finish_transfer(struct spi_device *spi,
202-
struct spi_transfer *tfr,
203-
bool cs_change)
219+
static void bcm2835_spi_handle_err(struct spi_master *master,
220+
struct spi_message *msg)
204221
{
205-
struct bcm2835_spi *bs = spi_master_get_devdata(spi->master);
206-
u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
207-
208-
if (tfr->delay_usecs)
209-
udelay(tfr->delay_usecs);
210-
211-
if (cs_change)
212-
/* Clear TA flag */
213-
bcm2835_wr(bs, BCM2835_SPI_CS, cs & ~BCM2835_SPI_CS_TA);
214-
215-
return 0;
222+
bcm2835_spi_reset_hw(master);
216223
}
217224

218-
static int bcm2835_spi_transfer_one(struct spi_master *master,
219-
struct spi_message *mesg)
225+
static void bcm2835_spi_set_cs(struct spi_device *spi, bool gpio_level)
220226
{
227+
/*
228+
* we can assume that we are "native" as per spi_set_cs
229+
* calling us ONLY when cs_gpio is not set
230+
* we can also assume that we are CS < 3 as per bcm2835_spi_setup
231+
* we would not get called because of error handling there.
232+
* the level passed is the electrical level not enabled/disabled
233+
* so it has to get translated back to enable/disable
234+
* see spi_set_cs in spi.c for the implementation
235+
*/
236+
237+
struct spi_master *master = spi->master;
221238
struct bcm2835_spi *bs = spi_master_get_devdata(master);
222-
struct spi_transfer *tfr;
223-
struct spi_device *spi = mesg->spi;
224-
int err = 0;
225-
unsigned int timeout;
226-
bool cs_change;
227-
228-
list_for_each_entry(tfr, &mesg->transfers, transfer_list) {
229-
err = bcm2835_spi_start_transfer(spi, tfr);
230-
if (err)
231-
goto out;
232-
233-
timeout = wait_for_completion_timeout(
234-
&bs->done,
235-
msecs_to_jiffies(BCM2835_SPI_TIMEOUT_MS)
236-
);
237-
if (!timeout) {
238-
err = -ETIMEDOUT;
239-
goto out;
240-
}
239+
u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
240+
bool enable;
241241

242-
cs_change = tfr->cs_change ||
243-
list_is_last(&tfr->transfer_list, &mesg->transfers);
242+
/* calculate the enable flag from the passed gpio_level */
243+
enable = (spi->mode & SPI_CS_HIGH) ? gpio_level : !gpio_level;
244244

245-
err = bcm2835_spi_finish_transfer(spi, tfr, cs_change);
246-
if (err)
247-
goto out;
245+
/* set flags for "reverse" polarity in the registers */
246+
if (spi->mode & SPI_CS_HIGH) {
247+
/* set the correct CS-bits */
248+
cs |= BCM2835_SPI_CS_CSPOL;
249+
cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select;
250+
} else {
251+
/* clean the CS-bits */
252+
cs &= ~BCM2835_SPI_CS_CSPOL;
253+
cs &= ~(BCM2835_SPI_CS_CSPOL0 << spi->chip_select);
254+
}
248255

249-
mesg->actual_length += (tfr->len - bs->len);
256+
/* select the correct chip_select depending on disabled/enabled */
257+
if (enable) {
258+
/* set cs correctly */
259+
if (spi->mode & SPI_NO_CS) {
260+
/* use the "undefined" chip-select */
261+
cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
262+
} else {
263+
/* set the chip select */
264+
cs &= ~(BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01);
265+
cs |= spi->chip_select;
266+
}
267+
} else {
268+
/* disable CSPOL which puts HW-CS into deselected state */
269+
cs &= ~BCM2835_SPI_CS_CSPOL;
270+
/* use the "undefined" chip-select as precaution */
271+
cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
250272
}
251273

252-
out:
253-
/* Clear FIFOs, and disable the HW block */
254-
bcm2835_wr(bs, BCM2835_SPI_CS,
255-
BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
256-
mesg->status = err;
257-
spi_finalize_current_message(master);
274+
/* finally set the calculated flags in SPI_CS */
275+
bcm2835_wr(bs, BCM2835_SPI_CS, cs);
276+
}
258277

259-
return 0;
278+
static int bcm2835_spi_setup(struct spi_device *spi)
279+
{
280+
/*
281+
* sanity checking the native-chipselects
282+
*/
283+
if (spi->mode & SPI_NO_CS)
284+
return 0;
285+
if (gpio_is_valid(spi->cs_gpio))
286+
return 0;
287+
if (spi->chip_select < 3)
288+
return 0;
289+
290+
/* error in the case of native CS requested with CS-id > 2 */
291+
dev_err(&spi->dev,
292+
"setup: only three native chip-selects are supported\n"
293+
);
294+
return -EINVAL;
260295
}
261296

262297
static int bcm2835_spi_probe(struct platform_device *pdev)
@@ -277,13 +312,14 @@ static int bcm2835_spi_probe(struct platform_device *pdev)
277312
master->mode_bits = BCM2835_SPI_MODE_BITS;
278313
master->bits_per_word_mask = SPI_BPW_MASK(8);
279314
master->num_chipselect = 3;
280-
master->transfer_one_message = bcm2835_spi_transfer_one;
315+
master->setup = bcm2835_spi_setup;
316+
master->set_cs = bcm2835_spi_set_cs;
317+
master->transfer_one = bcm2835_spi_transfer_one;
318+
master->handle_err = bcm2835_spi_handle_err;
281319
master->dev.of_node = pdev->dev.of_node;
282320

283321
bs = spi_master_get_devdata(master);
284322

285-
init_completion(&bs->done);
286-
287323
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
288324
bs->regs = devm_ioremap_resource(&pdev->dev, res);
289325
if (IS_ERR(bs->regs)) {
@@ -314,7 +350,7 @@ static int bcm2835_spi_probe(struct platform_device *pdev)
314350
goto out_clk_disable;
315351
}
316352

317-
/* initialise the hardware */
353+
/* initialise the hardware with the default polarities */
318354
bcm2835_wr(bs, BCM2835_SPI_CS,
319355
BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
320356

0 commit comments

Comments
 (0)