blob: 8c121b3374dd14aeaa52778f6c5b82bbe8fb18af [file] [log] [blame]
Chris Bootf8043872013-03-11 21:38:24 -06001/*
2 * Driver for Broadcom BCM2835 SPI Controllers
3 *
4 * Copyright (C) 2012 Chris Boot
5 * Copyright (C) 2013 Stephen Warren
Martin Sperle34ff012015-03-26 11:08:36 +01006 * Copyright (C) 2015 Martin Sperl
Chris Bootf8043872013-03-11 21:38:24 -06007 *
8 * This driver is inspired by:
9 * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
10 * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
Chris Bootf8043872013-03-11 21:38:24 -060021 */
22
23#include <linux/clk.h>
24#include <linux/completion.h>
25#include <linux/delay.h>
Martin Sperl3ecd37e2015-05-10 20:47:28 +000026#include <linux/dma-mapping.h>
27#include <linux/dmaengine.h>
Chris Bootf8043872013-03-11 21:38:24 -060028#include <linux/err.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/of.h>
Martin Sperl3ecd37e2015-05-10 20:47:28 +000034#include <linux/of_address.h>
Chris Bootf8043872013-03-11 21:38:24 -060035#include <linux/of_device.h>
Martin Sperl3ecd37e2015-05-10 20:47:28 +000036#include <linux/of_gpio.h>
37#include <linux/of_irq.h>
Chris Bootf8043872013-03-11 21:38:24 -060038#include <linux/spi/spi.h>
39
40/* SPI register offsets */
41#define BCM2835_SPI_CS 0x00
42#define BCM2835_SPI_FIFO 0x04
43#define BCM2835_SPI_CLK 0x08
44#define BCM2835_SPI_DLEN 0x0c
45#define BCM2835_SPI_LTOH 0x10
46#define BCM2835_SPI_DC 0x14
47
48/* Bitfields in CS */
49#define BCM2835_SPI_CS_LEN_LONG 0x02000000
50#define BCM2835_SPI_CS_DMA_LEN 0x01000000
51#define BCM2835_SPI_CS_CSPOL2 0x00800000
52#define BCM2835_SPI_CS_CSPOL1 0x00400000
53#define BCM2835_SPI_CS_CSPOL0 0x00200000
54#define BCM2835_SPI_CS_RXF 0x00100000
55#define BCM2835_SPI_CS_RXR 0x00080000
56#define BCM2835_SPI_CS_TXD 0x00040000
57#define BCM2835_SPI_CS_RXD 0x00020000
58#define BCM2835_SPI_CS_DONE 0x00010000
59#define BCM2835_SPI_CS_LEN 0x00002000
60#define BCM2835_SPI_CS_REN 0x00001000
61#define BCM2835_SPI_CS_ADCS 0x00000800
62#define BCM2835_SPI_CS_INTR 0x00000400
63#define BCM2835_SPI_CS_INTD 0x00000200
64#define BCM2835_SPI_CS_DMAEN 0x00000100
65#define BCM2835_SPI_CS_TA 0x00000080
66#define BCM2835_SPI_CS_CSPOL 0x00000040
67#define BCM2835_SPI_CS_CLEAR_RX 0x00000020
68#define BCM2835_SPI_CS_CLEAR_TX 0x00000010
69#define BCM2835_SPI_CS_CPOL 0x00000008
70#define BCM2835_SPI_CS_CPHA 0x00000004
71#define BCM2835_SPI_CS_CS_10 0x00000002
72#define BCM2835_SPI_CS_CS_01 0x00000001
73
Martin Sperl704f32d2015-04-06 17:16:30 +000074#define BCM2835_SPI_POLLING_LIMIT_US 30
Martin Sperla750b122015-04-22 07:33:03 +000075#define BCM2835_SPI_POLLING_JIFFIES 2
Martin Sperl3ecd37e2015-05-10 20:47:28 +000076#define BCM2835_SPI_DMA_MIN_LENGTH 96
Martin Sperl69352242015-03-19 09:01:53 +000077#define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
78 | SPI_NO_CS | SPI_3WIRE)
Chris Bootf8043872013-03-11 21:38:24 -060079
80#define DRV_NAME "spi-bcm2835"
81
Lukas Wunneracf0f852018-11-08 08:06:10 +010082/**
83 * struct bcm2835_spi - BCM2835 SPI controller
84 * @regs: base address of register map
85 * @clk: core clock, divided to calculate serial clock
86 * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full
Lukas Wunner3bd7f652018-11-08 08:06:10 +010087 * @tfr: SPI transfer currently processed
Lukas Wunneracf0f852018-11-08 08:06:10 +010088 * @tx_buf: pointer whence next transmitted byte is read
89 * @rx_buf: pointer where next received byte is written
90 * @tx_len: remaining bytes to transmit
91 * @rx_len: remaining bytes to receive
Lukas Wunner3bd7f652018-11-08 08:06:10 +010092 * @tx_prologue: bytes transmitted without DMA if first TX sglist entry's
93 * length is not a multiple of 4 (to overcome hardware limitation)
94 * @rx_prologue: bytes received without DMA if first RX sglist entry's
95 * length is not a multiple of 4 (to overcome hardware limitation)
96 * @tx_spillover: whether @tx_prologue spills over to second TX sglist entry
Lukas Wunneracf0f852018-11-08 08:06:10 +010097 * @dma_pending: whether a DMA transfer is in progress
98 */
Chris Bootf8043872013-03-11 21:38:24 -060099struct bcm2835_spi {
100 void __iomem *regs;
101 struct clk *clk;
102 int irq;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100103 struct spi_transfer *tfr;
Chris Bootf8043872013-03-11 21:38:24 -0600104 const u8 *tx_buf;
105 u8 *rx_buf;
Martin Sperle34ff012015-03-26 11:08:36 +0100106 int tx_len;
107 int rx_len;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100108 int tx_prologue;
109 int rx_prologue;
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100110 unsigned int tx_spillover;
Lukas Wunner29bdedf2018-11-29 15:14:49 +0100111 unsigned int dma_pending;
Chris Bootf8043872013-03-11 21:38:24 -0600112};
113
114static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg)
115{
116 return readl(bs->regs + reg);
117}
118
119static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val)
120{
121 writel(val, bs->regs + reg);
122}
123
Martin Sperl4adf3122015-03-23 15:11:53 +0100124static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs)
Chris Bootf8043872013-03-11 21:38:24 -0600125{
126 u8 byte;
127
Martin Sperle34ff012015-03-26 11:08:36 +0100128 while ((bs->rx_len) &&
129 (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
Chris Bootf8043872013-03-11 21:38:24 -0600130 byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
131 if (bs->rx_buf)
132 *bs->rx_buf++ = byte;
Martin Sperle34ff012015-03-26 11:08:36 +0100133 bs->rx_len--;
Chris Bootf8043872013-03-11 21:38:24 -0600134 }
135}
136
Martin Sperl4adf3122015-03-23 15:11:53 +0100137static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
Chris Bootf8043872013-03-11 21:38:24 -0600138{
139 u8 byte;
140
Martin Sperle34ff012015-03-26 11:08:36 +0100141 while ((bs->tx_len) &&
Martin Sperl4adf3122015-03-23 15:11:53 +0100142 (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
Chris Bootf8043872013-03-11 21:38:24 -0600143 byte = bs->tx_buf ? *bs->tx_buf++ : 0;
144 bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
Martin Sperle34ff012015-03-26 11:08:36 +0100145 bs->tx_len--;
Chris Bootf8043872013-03-11 21:38:24 -0600146 }
147}
148
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100149/**
150 * bcm2835_rd_fifo_count() - blindly read exactly @count bytes from RX FIFO
151 * @bs: BCM2835 SPI controller
152 * @count: bytes to read from RX FIFO
153 *
154 * The caller must ensure that @bs->rx_len is greater than or equal to @count,
155 * that the RX FIFO contains at least @count bytes and that the DMA Enable flag
156 * in the CS register is set (such that a read from the FIFO register receives
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100157 * 32-bit instead of just 8-bit). Moreover @bs->rx_buf must not be %NULL.
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100158 */
159static inline void bcm2835_rd_fifo_count(struct bcm2835_spi *bs, int count)
160{
161 u32 val;
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100162 int len;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100163
164 bs->rx_len -= count;
165
166 while (count > 0) {
167 val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100168 len = min(count, 4);
169 memcpy(bs->rx_buf, &val, len);
170 bs->rx_buf += len;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100171 count -= 4;
172 }
173}
174
175/**
176 * bcm2835_wr_fifo_count() - blindly write exactly @count bytes to TX FIFO
177 * @bs: BCM2835 SPI controller
178 * @count: bytes to write to TX FIFO
179 *
180 * The caller must ensure that @bs->tx_len is greater than or equal to @count,
181 * that the TX FIFO can accommodate @count bytes and that the DMA Enable flag
182 * in the CS register is set (such that a write to the FIFO register transmits
183 * 32-bit instead of just 8-bit).
184 */
185static inline void bcm2835_wr_fifo_count(struct bcm2835_spi *bs, int count)
186{
187 u32 val;
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100188 int len;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100189
190 bs->tx_len -= count;
191
192 while (count > 0) {
193 if (bs->tx_buf) {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100194 len = min(count, 4);
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100195 memcpy(&val, bs->tx_buf, len);
196 bs->tx_buf += len;
197 } else {
198 val = 0;
199 }
200 bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
201 count -= 4;
202 }
203}
204
205/**
206 * bcm2835_wait_tx_fifo_empty() - busy-wait for TX FIFO to empty
207 * @bs: BCM2835 SPI controller
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100208 *
209 * The caller must ensure that the RX FIFO can accommodate as many bytes
210 * as have been written to the TX FIFO: Transmission is halted once the
211 * RX FIFO is full, causing this function to spin forever.
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100212 */
213static inline void bcm2835_wait_tx_fifo_empty(struct bcm2835_spi *bs)
214{
215 while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE))
216 cpu_relax();
217}
218
Martin Sperle34ff012015-03-26 11:08:36 +0100219static void bcm2835_spi_reset_hw(struct spi_master *master)
220{
221 struct bcm2835_spi *bs = spi_master_get_devdata(master);
222 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
223
224 /* Disable SPI interrupts and transfer */
225 cs &= ~(BCM2835_SPI_CS_INTR |
226 BCM2835_SPI_CS_INTD |
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000227 BCM2835_SPI_CS_DMAEN |
Martin Sperle34ff012015-03-26 11:08:36 +0100228 BCM2835_SPI_CS_TA);
229 /* and reset RX/TX FIFOS */
230 cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
231
232 /* and reset the SPI_HW */
233 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000234 /* as well as DLEN */
235 bcm2835_wr(bs, BCM2835_SPI_DLEN, 0);
Martin Sperle34ff012015-03-26 11:08:36 +0100236}
237
Chris Bootf8043872013-03-11 21:38:24 -0600238static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
239{
240 struct spi_master *master = dev_id;
241 struct bcm2835_spi *bs = spi_master_get_devdata(master);
Chris Bootf8043872013-03-11 21:38:24 -0600242
Martin Sperl4adf3122015-03-23 15:11:53 +0100243 /* Read as many bytes as possible from FIFO */
244 bcm2835_rd_fifo(bs);
Martin Sperle34ff012015-03-26 11:08:36 +0100245 /* Write as many bytes as possible to FIFO */
246 bcm2835_wr_fifo(bs);
Chris Bootf8043872013-03-11 21:38:24 -0600247
Lukas Wunner56c17232018-11-08 08:06:10 +0100248 if (!bs->rx_len) {
Martin Sperle34ff012015-03-26 11:08:36 +0100249 /* Transfer complete - reset SPI HW */
250 bcm2835_spi_reset_hw(master);
251 /* wake up the framework */
252 complete(&master->xfer_completion);
Chris Bootf8043872013-03-11 21:38:24 -0600253 }
254
Martin Sperl4adf3122015-03-23 15:11:53 +0100255 return IRQ_HANDLED;
Chris Bootf8043872013-03-11 21:38:24 -0600256}
257
Martin Sperl704f32d2015-04-06 17:16:30 +0000258static int bcm2835_spi_transfer_one_irq(struct spi_master *master,
259 struct spi_device *spi,
260 struct spi_transfer *tfr,
261 u32 cs)
262{
263 struct bcm2835_spi *bs = spi_master_get_devdata(master);
Chris Bootf8043872013-03-11 21:38:24 -0600264
Chris Bootf8043872013-03-11 21:38:24 -0600265 /*
Lukas Wunner5c09e422018-11-08 08:06:10 +0100266 * Enable HW block, but with interrupts still disabled.
267 * Otherwise the empty TX FIFO would immediately trigger an interrupt.
Chris Bootf8043872013-03-11 21:38:24 -0600268 */
Lukas Wunner5c09e422018-11-08 08:06:10 +0100269 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
270
271 /* fill TX FIFO as much as possible */
272 bcm2835_wr_fifo(bs);
273
274 /* enable interrupts */
Martin Sperle34ff012015-03-26 11:08:36 +0100275 cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
Chris Bootf8043872013-03-11 21:38:24 -0600276 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
277
Martin Sperle34ff012015-03-26 11:08:36 +0100278 /* signal that we need to wait for completion */
279 return 1;
Chris Bootf8043872013-03-11 21:38:24 -0600280}
281
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000282/*
283 * DMA support
284 *
285 * this implementation has currently a few issues in so far as it does
286 * not work arrount limitations of the HW.
287 *
288 * the main one being that DMA transfers are limited to 16 bit
289 * (so 0 to 65535 bytes) by the SPI HW due to BCM2835_SPI_DLEN
290 *
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000291 * there may be a few more border-cases we may need to address as well
292 * but unfortunately this would mean splitting up the scatter-gather
293 * list making it slightly unpractical...
294 */
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100295
296/**
297 * bcm2835_spi_transfer_prologue() - transfer first few bytes without DMA
298 * @master: SPI master
299 * @tfr: SPI transfer
300 * @bs: BCM2835 SPI controller
301 * @cs: CS register
302 *
303 * A limitation in DMA mode is that the FIFO must be accessed in 4 byte chunks.
304 * Only the final write access is permitted to transmit less than 4 bytes, the
305 * SPI controller deduces its intended size from the DLEN register.
306 *
307 * If a TX or RX sglist contains multiple entries, one per page, and the first
308 * entry starts in the middle of a page, that first entry's length may not be
309 * a multiple of 4. Subsequent entries are fine because they span an entire
310 * page, hence do have a length that's a multiple of 4.
311 *
312 * This cannot happen with kmalloc'ed buffers (which is what most clients use)
313 * because they are contiguous in physical memory and therefore not split on
314 * page boundaries by spi_map_buf(). But it *can* happen with vmalloc'ed
315 * buffers.
316 *
317 * The DMA engine is incapable of combining sglist entries into a continuous
318 * stream of 4 byte chunks, it treats every entry separately: A TX entry is
319 * rounded up a to a multiple of 4 bytes by transmitting surplus bytes, an RX
320 * entry is rounded up by throwing away received bytes.
321 *
322 * Overcome this limitation by transferring the first few bytes without DMA:
323 * E.g. if the first TX sglist entry's length is 23 and the first RX's is 42,
324 * write 3 bytes to the TX FIFO but read only 2 bytes from the RX FIFO.
325 * The residue of 1 byte in the RX FIFO is picked up by DMA. Together with
326 * the rest of the first RX sglist entry it makes up a multiple of 4 bytes.
327 *
328 * Should the RX prologue be larger, say, 3 vis-à-vis a TX prologue of 1,
329 * write 1 + 4 = 5 bytes to the TX FIFO and read 3 bytes from the RX FIFO.
330 * Caution, the additional 4 bytes spill over to the second TX sglist entry
331 * if the length of the first is *exactly* 1.
332 *
333 * At most 6 bytes are written and at most 3 bytes read. Do we know the
334 * transfer has this many bytes? Yes, see BCM2835_SPI_DMA_MIN_LENGTH.
335 *
336 * The FIFO is normally accessed with 8-bit width by the CPU and 32-bit width
337 * by the DMA engine. Toggling the DMA Enable flag in the CS register switches
338 * the width but also garbles the FIFO's contents. The prologue must therefore
339 * be transmitted in 32-bit width to ensure that the following DMA transfer can
340 * pick up the residue in the RX FIFO in ungarbled form.
341 */
342static void bcm2835_spi_transfer_prologue(struct spi_master *master,
343 struct spi_transfer *tfr,
344 struct bcm2835_spi *bs,
345 u32 cs)
346{
347 int tx_remaining;
348
349 bs->tfr = tfr;
350 bs->tx_prologue = 0;
351 bs->rx_prologue = 0;
352 bs->tx_spillover = false;
353
354 if (!sg_is_last(&tfr->tx_sg.sgl[0]))
355 bs->tx_prologue = sg_dma_len(&tfr->tx_sg.sgl[0]) & 3;
356
357 if (!sg_is_last(&tfr->rx_sg.sgl[0])) {
358 bs->rx_prologue = sg_dma_len(&tfr->rx_sg.sgl[0]) & 3;
359
360 if (bs->rx_prologue > bs->tx_prologue) {
361 if (sg_is_last(&tfr->tx_sg.sgl[0])) {
362 bs->tx_prologue = bs->rx_prologue;
363 } else {
364 bs->tx_prologue += 4;
365 bs->tx_spillover =
366 !(sg_dma_len(&tfr->tx_sg.sgl[0]) & ~3);
367 }
368 }
369 }
370
371 /* rx_prologue > 0 implies tx_prologue > 0, so check only the latter */
372 if (!bs->tx_prologue)
373 return;
374
375 /* Write and read RX prologue. Adjust first entry in RX sglist. */
376 if (bs->rx_prologue) {
377 bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->rx_prologue);
378 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
379 | BCM2835_SPI_CS_DMAEN);
380 bcm2835_wr_fifo_count(bs, bs->rx_prologue);
381 bcm2835_wait_tx_fifo_empty(bs);
382 bcm2835_rd_fifo_count(bs, bs->rx_prologue);
383 bcm2835_spi_reset_hw(master);
384
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100385 dma_sync_single_for_device(master->dma_rx->device->dev,
386 sg_dma_address(&tfr->rx_sg.sgl[0]),
387 bs->rx_prologue, DMA_FROM_DEVICE);
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100388
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100389 sg_dma_address(&tfr->rx_sg.sgl[0]) += bs->rx_prologue;
390 sg_dma_len(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100391 }
392
393 /*
394 * Write remaining TX prologue. Adjust first entry in TX sglist.
395 * Also adjust second entry if prologue spills over to it.
396 */
397 tx_remaining = bs->tx_prologue - bs->rx_prologue;
398 if (tx_remaining) {
399 bcm2835_wr(bs, BCM2835_SPI_DLEN, tx_remaining);
400 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
401 | BCM2835_SPI_CS_DMAEN);
402 bcm2835_wr_fifo_count(bs, tx_remaining);
403 bcm2835_wait_tx_fifo_empty(bs);
404 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_TX);
405 }
406
407 if (likely(!bs->tx_spillover)) {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100408 sg_dma_address(&tfr->tx_sg.sgl[0]) += bs->tx_prologue;
409 sg_dma_len(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100410 } else {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100411 sg_dma_len(&tfr->tx_sg.sgl[0]) = 0;
412 sg_dma_address(&tfr->tx_sg.sgl[1]) += 4;
413 sg_dma_len(&tfr->tx_sg.sgl[1]) -= 4;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100414 }
415}
416
417/**
418 * bcm2835_spi_undo_prologue() - reconstruct original sglist state
419 * @bs: BCM2835 SPI controller
420 *
421 * Undo changes which were made to an SPI transfer's sglist when transmitting
422 * the prologue. This is necessary to ensure the same memory ranges are
423 * unmapped that were originally mapped.
424 */
425static void bcm2835_spi_undo_prologue(struct bcm2835_spi *bs)
426{
427 struct spi_transfer *tfr = bs->tfr;
428
429 if (!bs->tx_prologue)
430 return;
431
432 if (bs->rx_prologue) {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100433 sg_dma_address(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue;
434 sg_dma_len(&tfr->rx_sg.sgl[0]) += bs->rx_prologue;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100435 }
436
437 if (likely(!bs->tx_spillover)) {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100438 sg_dma_address(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue;
439 sg_dma_len(&tfr->tx_sg.sgl[0]) += bs->tx_prologue;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100440 } else {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100441 sg_dma_len(&tfr->tx_sg.sgl[0]) = bs->tx_prologue - 4;
442 sg_dma_address(&tfr->tx_sg.sgl[1]) -= 4;
443 sg_dma_len(&tfr->tx_sg.sgl[1]) += 4;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100444 }
445}
446
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000447static void bcm2835_spi_dma_done(void *data)
448{
449 struct spi_master *master = data;
450 struct bcm2835_spi *bs = spi_master_get_devdata(master);
451
452 /* reset fifo and HW */
453 bcm2835_spi_reset_hw(master);
454
455 /* and terminate tx-dma as we do not have an irq for it
456 * because when the rx dma will terminate and this callback
457 * is called the tx-dma must have finished - can't get to this
458 * situation otherwise...
459 */
Lukas Wunnere82b0b32018-11-08 08:06:10 +0100460 if (cmpxchg(&bs->dma_pending, true, false)) {
461 dmaengine_terminate_all(master->dma_tx);
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100462 bcm2835_spi_undo_prologue(bs);
Lukas Wunnere82b0b32018-11-08 08:06:10 +0100463 }
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000464
465 /* and mark as completed */;
466 complete(&master->xfer_completion);
467}
468
469static int bcm2835_spi_prepare_sg(struct spi_master *master,
470 struct spi_transfer *tfr,
471 bool is_tx)
472{
473 struct dma_chan *chan;
474 struct scatterlist *sgl;
475 unsigned int nents;
476 enum dma_transfer_direction dir;
477 unsigned long flags;
478
479 struct dma_async_tx_descriptor *desc;
480 dma_cookie_t cookie;
481
482 if (is_tx) {
483 dir = DMA_MEM_TO_DEV;
484 chan = master->dma_tx;
485 nents = tfr->tx_sg.nents;
486 sgl = tfr->tx_sg.sgl;
487 flags = 0 /* no tx interrupt */;
488
489 } else {
490 dir = DMA_DEV_TO_MEM;
491 chan = master->dma_rx;
492 nents = tfr->rx_sg.nents;
493 sgl = tfr->rx_sg.sgl;
494 flags = DMA_PREP_INTERRUPT;
495 }
496 /* prepare the channel */
497 desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
498 if (!desc)
499 return -EINVAL;
500
501 /* set callback for rx */
502 if (!is_tx) {
503 desc->callback = bcm2835_spi_dma_done;
504 desc->callback_param = master;
505 }
506
507 /* submit it to DMA-engine */
508 cookie = dmaengine_submit(desc);
509
510 return dma_submit_error(cookie);
511}
512
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000513static int bcm2835_spi_transfer_one_dma(struct spi_master *master,
514 struct spi_device *spi,
515 struct spi_transfer *tfr,
516 u32 cs)
517{
518 struct bcm2835_spi *bs = spi_master_get_devdata(master);
519 int ret;
520
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100521 /*
522 * Transfer first few bytes without DMA if length of first TX or RX
523 * sglist entry is not a multiple of 4 bytes (hardware limitation).
524 */
525 bcm2835_spi_transfer_prologue(master, tfr, bs, cs);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000526
527 /* setup tx-DMA */
528 ret = bcm2835_spi_prepare_sg(master, tfr, true);
529 if (ret)
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100530 goto err_reset_hw;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000531
532 /* start TX early */
533 dma_async_issue_pending(master->dma_tx);
534
535 /* mark as dma pending */
536 bs->dma_pending = 1;
537
538 /* set the DMA length */
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100539 bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->tx_len);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000540
541 /* start the HW */
542 bcm2835_wr(bs, BCM2835_SPI_CS,
543 cs | BCM2835_SPI_CS_TA | BCM2835_SPI_CS_DMAEN);
544
545 /* setup rx-DMA late - to run transfers while
546 * mapping of the rx buffers still takes place
547 * this saves 10us or more.
548 */
549 ret = bcm2835_spi_prepare_sg(master, tfr, false);
550 if (ret) {
551 /* need to reset on errors */
552 dmaengine_terminate_all(master->dma_tx);
Lukas Wunnerdbc94412018-11-08 08:06:10 +0100553 bs->dma_pending = false;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100554 goto err_reset_hw;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000555 }
556
557 /* start rx dma late */
558 dma_async_issue_pending(master->dma_rx);
559
560 /* wait for wakeup in framework */
561 return 1;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100562
563err_reset_hw:
564 bcm2835_spi_reset_hw(master);
565 bcm2835_spi_undo_prologue(bs);
566 return ret;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000567}
568
569static bool bcm2835_spi_can_dma(struct spi_master *master,
570 struct spi_device *spi,
571 struct spi_transfer *tfr)
572{
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000573 /* we start DMA efforts only on bigger transfers */
574 if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
575 return false;
576
577 /* BCM2835_SPI_DLEN has defined a max transfer size as
578 * 16 bit, so max is 65535
579 * we can revisit this by using an alternative transfer
580 * method - ideally this would get done without any more
581 * interaction...
582 */
583 if (tfr->len > 65535) {
584 dev_warn_once(&spi->dev,
585 "transfer size of %d too big for dma-transfer\n",
586 tfr->len);
587 return false;
588 }
589
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000590 /* return OK */
591 return true;
592}
593
kbuild test robot29ad1a72015-05-12 19:43:59 +0800594static void bcm2835_dma_release(struct spi_master *master)
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000595{
596 if (master->dma_tx) {
597 dmaengine_terminate_all(master->dma_tx);
598 dma_release_channel(master->dma_tx);
599 master->dma_tx = NULL;
600 }
601 if (master->dma_rx) {
602 dmaengine_terminate_all(master->dma_rx);
603 dma_release_channel(master->dma_rx);
604 master->dma_rx = NULL;
605 }
606}
607
kbuild test robot29ad1a72015-05-12 19:43:59 +0800608static void bcm2835_dma_init(struct spi_master *master, struct device *dev)
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000609{
610 struct dma_slave_config slave_config;
611 const __be32 *addr;
612 dma_addr_t dma_reg_base;
613 int ret;
614
615 /* base address in dma-space */
616 addr = of_get_address(master->dev.of_node, 0, NULL, NULL);
617 if (!addr) {
618 dev_err(dev, "could not get DMA-register address - not using dma mode\n");
619 goto err;
620 }
621 dma_reg_base = be32_to_cpup(addr);
622
623 /* get tx/rx dma */
624 master->dma_tx = dma_request_slave_channel(dev, "tx");
625 if (!master->dma_tx) {
626 dev_err(dev, "no tx-dma configuration found - not using dma mode\n");
627 goto err;
628 }
629 master->dma_rx = dma_request_slave_channel(dev, "rx");
630 if (!master->dma_rx) {
631 dev_err(dev, "no rx-dma configuration found - not using dma mode\n");
632 goto err_release;
633 }
634
635 /* configure DMAs */
636 slave_config.direction = DMA_MEM_TO_DEV;
637 slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
638 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
639
640 ret = dmaengine_slave_config(master->dma_tx, &slave_config);
641 if (ret)
642 goto err_config;
643
644 slave_config.direction = DMA_DEV_TO_MEM;
645 slave_config.src_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
646 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
647
648 ret = dmaengine_slave_config(master->dma_rx, &slave_config);
649 if (ret)
650 goto err_config;
651
652 /* all went well, so set can_dma */
653 master->can_dma = bcm2835_spi_can_dma;
654 master->max_dma_len = 65535; /* limitation by BCM2835_SPI_DLEN */
655 /* need to do TX AND RX DMA, so we need dummy buffers */
656 master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
657
658 return;
659
660err_config:
661 dev_err(dev, "issue configuring dma: %d - not using DMA mode\n",
662 ret);
663err_release:
664 bcm2835_dma_release(master);
665err:
666 return;
667}
668
Martin Sperla750b122015-04-22 07:33:03 +0000669static int bcm2835_spi_transfer_one_poll(struct spi_master *master,
670 struct spi_device *spi,
671 struct spi_transfer *tfr,
672 u32 cs,
Martin Sperl0122a512015-07-29 07:34:10 +0000673 unsigned long long xfer_time_us)
Martin Sperla750b122015-04-22 07:33:03 +0000674{
675 struct bcm2835_spi *bs = spi_master_get_devdata(master);
676 unsigned long timeout;
677
678 /* enable HW block without interrupts */
679 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
680
681 /* fill in the fifo before timeout calculations
682 * if we are interrupted here, then the data is
683 * getting transferred by the HW while we are interrupted
684 */
685 bcm2835_wr_fifo(bs);
686
687 /* set the timeout */
688 timeout = jiffies + BCM2835_SPI_POLLING_JIFFIES;
689
690 /* loop until finished the transfer */
691 while (bs->rx_len) {
692 /* fill in tx fifo with remaining data */
693 bcm2835_wr_fifo(bs);
694
695 /* read from fifo as much as possible */
696 bcm2835_rd_fifo(bs);
697
698 /* if there is still data pending to read
699 * then check the timeout
700 */
701 if (bs->rx_len && time_after(jiffies, timeout)) {
702 dev_dbg_ratelimited(&spi->dev,
703 "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
704 jiffies - timeout,
705 bs->tx_len, bs->rx_len);
706 /* fall back to interrupt mode */
707 return bcm2835_spi_transfer_one_irq(master, spi,
708 tfr, cs);
709 }
710 }
711
712 /* Transfer complete - reset SPI HW */
713 bcm2835_spi_reset_hw(master);
714 /* and return without waiting for completion */
715 return 0;
716}
717
Martin Sperl704f32d2015-04-06 17:16:30 +0000718static int bcm2835_spi_transfer_one(struct spi_master *master,
719 struct spi_device *spi,
720 struct spi_transfer *tfr)
721{
722 struct bcm2835_spi *bs = spi_master_get_devdata(master);
723 unsigned long spi_hz, clk_hz, cdiv;
Martin Sperl0122a512015-07-29 07:34:10 +0000724 unsigned long spi_used_hz;
725 unsigned long long xfer_time_us;
Martin Sperl704f32d2015-04-06 17:16:30 +0000726 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
727
728 /* set clock */
729 spi_hz = tfr->speed_hz;
730 clk_hz = clk_get_rate(bs->clk);
731
732 if (spi_hz >= clk_hz / 2) {
733 cdiv = 2; /* clk_hz/2 is the fastest we can go */
734 } else if (spi_hz) {
735 /* CDIV must be a multiple of two */
736 cdiv = DIV_ROUND_UP(clk_hz, spi_hz);
737 cdiv += (cdiv % 2);
738
739 if (cdiv >= 65536)
740 cdiv = 0; /* 0 is the slowest we can go */
741 } else {
742 cdiv = 0; /* 0 is the slowest we can go */
743 }
744 spi_used_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536);
745 bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
746
Martin Sperlacace732015-07-28 14:03:12 +0000747 /* handle all the 3-wire mode */
Martin Sperl704f32d2015-04-06 17:16:30 +0000748 if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf))
749 cs |= BCM2835_SPI_CS_REN;
Martin Sperlacace732015-07-28 14:03:12 +0000750 else
751 cs &= ~BCM2835_SPI_CS_REN;
Martin Sperl704f32d2015-04-06 17:16:30 +0000752
Lukas Wunner5c09e422018-11-08 08:06:10 +0100753 /*
754 * The driver always uses software-controlled GPIO Chip Select.
755 * Set the hardware-controlled native Chip Select to an invalid
756 * value to prevent it from interfering.
Martin Sperl704f32d2015-04-06 17:16:30 +0000757 */
Lukas Wunner5c09e422018-11-08 08:06:10 +0100758 cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
Martin Sperl704f32d2015-04-06 17:16:30 +0000759
760 /* set transmit buffers and length */
761 bs->tx_buf = tfr->tx_buf;
762 bs->rx_buf = tfr->rx_buf;
763 bs->tx_len = tfr->len;
764 bs->rx_len = tfr->len;
765
766 /* calculate the estimated time in us the transfer runs */
Martin Sperl0122a512015-07-29 07:34:10 +0000767 xfer_time_us = (unsigned long long)tfr->len
Martin Sperl704f32d2015-04-06 17:16:30 +0000768 * 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */
Martin Sperl0122a512015-07-29 07:34:10 +0000769 * 1000000;
770 do_div(xfer_time_us, spi_used_hz);
Martin Sperl704f32d2015-04-06 17:16:30 +0000771
772 /* for short requests run polling*/
773 if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US)
774 return bcm2835_spi_transfer_one_poll(master, spi, tfr,
775 cs, xfer_time_us);
776
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000777 /* run in dma mode if conditions are right */
778 if (master->can_dma && bcm2835_spi_can_dma(master, spi, tfr))
779 return bcm2835_spi_transfer_one_dma(master, spi, tfr, cs);
780
781 /* run in interrupt-mode */
Martin Sperl704f32d2015-04-06 17:16:30 +0000782 return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs);
783}
784
Martin Sperlacace732015-07-28 14:03:12 +0000785static int bcm2835_spi_prepare_message(struct spi_master *master,
786 struct spi_message *msg)
787{
788 struct spi_device *spi = msg->spi;
789 struct bcm2835_spi *bs = spi_master_get_devdata(master);
790 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
791
792 cs &= ~(BCM2835_SPI_CS_CPOL | BCM2835_SPI_CS_CPHA);
793
794 if (spi->mode & SPI_CPOL)
795 cs |= BCM2835_SPI_CS_CPOL;
796 if (spi->mode & SPI_CPHA)
797 cs |= BCM2835_SPI_CS_CPHA;
798
799 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
800
801 return 0;
802}
803
Martin Sperle34ff012015-03-26 11:08:36 +0100804static void bcm2835_spi_handle_err(struct spi_master *master,
805 struct spi_message *msg)
Chris Bootf8043872013-03-11 21:38:24 -0600806{
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000807 struct bcm2835_spi *bs = spi_master_get_devdata(master);
808
809 /* if an error occurred and we have an active dma, then terminate */
Lukas Wunnere82b0b32018-11-08 08:06:10 +0100810 if (cmpxchg(&bs->dma_pending, true, false)) {
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000811 dmaengine_terminate_all(master->dma_tx);
812 dmaengine_terminate_all(master->dma_rx);
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100813 bcm2835_spi_undo_prologue(bs);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000814 }
815 /* and reset */
Martin Sperle34ff012015-03-26 11:08:36 +0100816 bcm2835_spi_reset_hw(master);
Chris Bootf8043872013-03-11 21:38:24 -0600817}
818
Martin Sperla30a5552015-04-06 17:16:31 +0000819static int chip_match_name(struct gpio_chip *chip, void *data)
820{
821 return !strcmp(chip->label, data);
822}
823
Martin Sperle34ff012015-03-26 11:08:36 +0100824static int bcm2835_spi_setup(struct spi_device *spi)
825{
Martin Sperla30a5552015-04-06 17:16:31 +0000826 int err;
827 struct gpio_chip *chip;
Martin Sperle34ff012015-03-26 11:08:36 +0100828 /*
829 * sanity checking the native-chipselects
830 */
831 if (spi->mode & SPI_NO_CS)
832 return 0;
833 if (gpio_is_valid(spi->cs_gpio))
834 return 0;
Martin Sperla30a5552015-04-06 17:16:31 +0000835 if (spi->chip_select > 1) {
836 /* error in the case of native CS requested with CS > 1
837 * officially there is a CS2, but it is not documented
838 * which GPIO is connected with that...
839 */
840 dev_err(&spi->dev,
841 "setup: only two native chip-selects are supported\n");
842 return -EINVAL;
843 }
844 /* now translate native cs to GPIO */
845
846 /* get the gpio chip for the base */
847 chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
848 if (!chip)
Martin Sperle34ff012015-03-26 11:08:36 +0100849 return 0;
850
Martin Sperla30a5552015-04-06 17:16:31 +0000851 /* and calculate the real CS */
852 spi->cs_gpio = chip->base + 8 - spi->chip_select;
853
854 /* and set up the "mode" and level */
855 dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n",
856 spi->chip_select, spi->cs_gpio);
857
858 /* set up GPIO as output and pull to the correct level */
859 err = gpio_direction_output(spi->cs_gpio,
860 (spi->mode & SPI_CS_HIGH) ? 0 : 1);
861 if (err) {
862 dev_err(&spi->dev,
863 "could not set CS%i gpio %i as output: %i",
864 spi->chip_select, spi->cs_gpio, err);
865 return err;
866 }
Martin Sperla30a5552015-04-06 17:16:31 +0000867
868 return 0;
Chris Bootf8043872013-03-11 21:38:24 -0600869}
870
871static int bcm2835_spi_probe(struct platform_device *pdev)
872{
873 struct spi_master *master;
874 struct bcm2835_spi *bs;
875 struct resource *res;
876 int err;
877
878 master = spi_alloc_master(&pdev->dev, sizeof(*bs));
879 if (!master) {
880 dev_err(&pdev->dev, "spi_alloc_master() failed\n");
881 return -ENOMEM;
882 }
883
884 platform_set_drvdata(pdev, master);
885
886 master->mode_bits = BCM2835_SPI_MODE_BITS;
Axel Linc2b6a3a2013-08-05 08:43:02 +0800887 master->bits_per_word_mask = SPI_BPW_MASK(8);
Chris Bootf8043872013-03-11 21:38:24 -0600888 master->num_chipselect = 3;
Martin Sperle34ff012015-03-26 11:08:36 +0100889 master->setup = bcm2835_spi_setup;
Martin Sperle34ff012015-03-26 11:08:36 +0100890 master->transfer_one = bcm2835_spi_transfer_one;
891 master->handle_err = bcm2835_spi_handle_err;
Martin Sperlacace732015-07-28 14:03:12 +0000892 master->prepare_message = bcm2835_spi_prepare_message;
Chris Bootf8043872013-03-11 21:38:24 -0600893 master->dev.of_node = pdev->dev.of_node;
894
895 bs = spi_master_get_devdata(master);
896
Chris Bootf8043872013-03-11 21:38:24 -0600897 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Laurent Navet2d6e75e2013-05-02 14:13:30 +0200898 bs->regs = devm_ioremap_resource(&pdev->dev, res);
899 if (IS_ERR(bs->regs)) {
900 err = PTR_ERR(bs->regs);
Chris Bootf8043872013-03-11 21:38:24 -0600901 goto out_master_put;
902 }
903
904 bs->clk = devm_clk_get(&pdev->dev, NULL);
905 if (IS_ERR(bs->clk)) {
906 err = PTR_ERR(bs->clk);
907 dev_err(&pdev->dev, "could not get clk: %d\n", err);
908 goto out_master_put;
909 }
910
Martin Sperlddf0e1c2015-10-15 10:09:11 +0000911 bs->irq = platform_get_irq(pdev, 0);
Chris Bootf8043872013-03-11 21:38:24 -0600912 if (bs->irq <= 0) {
913 dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
914 err = bs->irq ? bs->irq : -ENODEV;
915 goto out_master_put;
916 }
917
918 clk_prepare_enable(bs->clk);
919
Martin Sperlddf0e1c2015-10-15 10:09:11 +0000920 bcm2835_dma_init(master, &pdev->dev);
921
922 /* initialise the hardware with the default polarities */
923 bcm2835_wr(bs, BCM2835_SPI_CS,
924 BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
925
Jingoo Han08bc0542013-12-09 19:25:00 +0900926 err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0,
Martin Sperl342f9482015-03-20 15:26:11 +0100927 dev_name(&pdev->dev), master);
Chris Bootf8043872013-03-11 21:38:24 -0600928 if (err) {
929 dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
930 goto out_clk_disable;
931 }
932
Jingoo Han247263d2013-09-24 13:23:00 +0900933 err = devm_spi_register_master(&pdev->dev, master);
Chris Bootf8043872013-03-11 21:38:24 -0600934 if (err) {
935 dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
Jingoo Han08bc0542013-12-09 19:25:00 +0900936 goto out_clk_disable;
Chris Bootf8043872013-03-11 21:38:24 -0600937 }
938
939 return 0;
940
Chris Bootf8043872013-03-11 21:38:24 -0600941out_clk_disable:
942 clk_disable_unprepare(bs->clk);
943out_master_put:
944 spi_master_put(master);
945 return err;
946}
947
948static int bcm2835_spi_remove(struct platform_device *pdev)
949{
Wei Yongjune0b35b82013-11-15 15:43:27 +0800950 struct spi_master *master = platform_get_drvdata(pdev);
Chris Bootf8043872013-03-11 21:38:24 -0600951 struct bcm2835_spi *bs = spi_master_get_devdata(master);
952
Chris Bootf8043872013-03-11 21:38:24 -0600953 /* Clear FIFOs, and disable the HW block */
954 bcm2835_wr(bs, BCM2835_SPI_CS,
955 BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
956
957 clk_disable_unprepare(bs->clk);
Chris Bootf8043872013-03-11 21:38:24 -0600958
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000959 bcm2835_dma_release(master);
960
Chris Bootf8043872013-03-11 21:38:24 -0600961 return 0;
962}
963
964static const struct of_device_id bcm2835_spi_match[] = {
965 { .compatible = "brcm,bcm2835-spi", },
966 {}
967};
968MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
969
970static struct platform_driver bcm2835_spi_driver = {
971 .driver = {
972 .name = DRV_NAME,
Chris Bootf8043872013-03-11 21:38:24 -0600973 .of_match_table = bcm2835_spi_match,
974 },
975 .probe = bcm2835_spi_probe,
976 .remove = bcm2835_spi_remove,
977};
978module_platform_driver(bcm2835_spi_driver);
979
980MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
981MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
Stefan Wahren22bf6cd2018-10-23 13:06:08 +0200982MODULE_LICENSE("GPL");