blob: eb67da697ef52c350d7c02a809d1f4e08c62e28a [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>
Martin Sperl154f7da2019-04-23 20:15:13 +000025#include <linux/debugfs.h>
Chris Bootf8043872013-03-11 21:38:24 -060026#include <linux/delay.h>
Martin Sperl3ecd37e2015-05-10 20:47:28 +000027#include <linux/dma-mapping.h>
28#include <linux/dmaengine.h>
Chris Bootf8043872013-03-11 21:38:24 -060029#include <linux/err.h>
30#include <linux/interrupt.h>
31#include <linux/io.h>
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/of.h>
Martin Sperl3ecd37e2015-05-10 20:47:28 +000035#include <linux/of_address.h>
Chris Bootf8043872013-03-11 21:38:24 -060036#include <linux/of_device.h>
Martin Sperl3ecd37e2015-05-10 20:47:28 +000037#include <linux/of_gpio.h>
38#include <linux/of_irq.h>
Chris Bootf8043872013-03-11 21:38:24 -060039#include <linux/spi/spi.h>
40
41/* SPI register offsets */
42#define BCM2835_SPI_CS 0x00
43#define BCM2835_SPI_FIFO 0x04
44#define BCM2835_SPI_CLK 0x08
45#define BCM2835_SPI_DLEN 0x0c
46#define BCM2835_SPI_LTOH 0x10
47#define BCM2835_SPI_DC 0x14
48
49/* Bitfields in CS */
50#define BCM2835_SPI_CS_LEN_LONG 0x02000000
51#define BCM2835_SPI_CS_DMA_LEN 0x01000000
52#define BCM2835_SPI_CS_CSPOL2 0x00800000
53#define BCM2835_SPI_CS_CSPOL1 0x00400000
54#define BCM2835_SPI_CS_CSPOL0 0x00200000
55#define BCM2835_SPI_CS_RXF 0x00100000
56#define BCM2835_SPI_CS_RXR 0x00080000
57#define BCM2835_SPI_CS_TXD 0x00040000
58#define BCM2835_SPI_CS_RXD 0x00020000
59#define BCM2835_SPI_CS_DONE 0x00010000
60#define BCM2835_SPI_CS_LEN 0x00002000
61#define BCM2835_SPI_CS_REN 0x00001000
62#define BCM2835_SPI_CS_ADCS 0x00000800
63#define BCM2835_SPI_CS_INTR 0x00000400
64#define BCM2835_SPI_CS_INTD 0x00000200
65#define BCM2835_SPI_CS_DMAEN 0x00000100
66#define BCM2835_SPI_CS_TA 0x00000080
67#define BCM2835_SPI_CS_CSPOL 0x00000040
68#define BCM2835_SPI_CS_CLEAR_RX 0x00000020
69#define BCM2835_SPI_CS_CLEAR_TX 0x00000010
70#define BCM2835_SPI_CS_CPOL 0x00000008
71#define BCM2835_SPI_CS_CPHA 0x00000004
72#define BCM2835_SPI_CS_CS_10 0x00000002
73#define BCM2835_SPI_CS_CS_01 0x00000001
74
Lukas Wunner2e0733b2018-11-29 16:45:24 +010075#define BCM2835_SPI_FIFO_SIZE 64
76#define BCM2835_SPI_FIFO_SIZE_3_4 48
Martin Sperl3ecd37e2015-05-10 20:47:28 +000077#define BCM2835_SPI_DMA_MIN_LENGTH 96
Martin Sperl69352242015-03-19 09:01:53 +000078#define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
79 | SPI_NO_CS | SPI_3WIRE)
Chris Bootf8043872013-03-11 21:38:24 -060080
81#define DRV_NAME "spi-bcm2835"
82
Martin Sperlff245d92019-04-23 20:15:11 +000083/* define polling limits */
84unsigned int polling_limit_us = 30;
85module_param(polling_limit_us, uint, 0664);
86MODULE_PARM_DESC(polling_limit_us,
87 "time in us to run a transfer in polling mode\n");
88
Lukas Wunneracf0f852018-11-08 08:06:10 +010089/**
90 * struct bcm2835_spi - BCM2835 SPI controller
91 * @regs: base address of register map
92 * @clk: core clock, divided to calculate serial clock
93 * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full
Lukas Wunner3bd7f652018-11-08 08:06:10 +010094 * @tfr: SPI transfer currently processed
Lukas Wunneracf0f852018-11-08 08:06:10 +010095 * @tx_buf: pointer whence next transmitted byte is read
96 * @rx_buf: pointer where next received byte is written
97 * @tx_len: remaining bytes to transmit
98 * @rx_len: remaining bytes to receive
Lukas Wunner3bd7f652018-11-08 08:06:10 +010099 * @tx_prologue: bytes transmitted without DMA if first TX sglist entry's
100 * length is not a multiple of 4 (to overcome hardware limitation)
101 * @rx_prologue: bytes received without DMA if first RX sglist entry's
102 * length is not a multiple of 4 (to overcome hardware limitation)
103 * @tx_spillover: whether @tx_prologue spills over to second TX sglist entry
Lukas Wunneracf0f852018-11-08 08:06:10 +0100104 * @dma_pending: whether a DMA transfer is in progress
Martin Sperl154f7da2019-04-23 20:15:13 +0000105 * @debugfs_dir: the debugfs directory - neede to remove debugfs when
106 * unloading the module
107 * @count_transfer_polling: count of how often polling mode is used
108 * @count_transfer_irq: count of how often interrupt mode is used
109 * @count_transfer_irq_after_polling: count of how often we fall back to
110 * interrupt mode after starting in polling mode.
111 * These are counted as well in @count_transfer_polling and
112 * @count_transfer_irq
113 * @count_transfer_dma: count how often dma mode is used
Lukas Wunneracf0f852018-11-08 08:06:10 +0100114 */
Chris Bootf8043872013-03-11 21:38:24 -0600115struct bcm2835_spi {
116 void __iomem *regs;
117 struct clk *clk;
118 int irq;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100119 struct spi_transfer *tfr;
Chris Bootf8043872013-03-11 21:38:24 -0600120 const u8 *tx_buf;
121 u8 *rx_buf;
Martin Sperle34ff012015-03-26 11:08:36 +0100122 int tx_len;
123 int rx_len;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100124 int tx_prologue;
125 int rx_prologue;
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100126 unsigned int tx_spillover;
Lukas Wunner29bdedf2018-11-29 15:14:49 +0100127 unsigned int dma_pending;
Martin Sperl154f7da2019-04-23 20:15:13 +0000128
129 struct dentry *debugfs_dir;
130 u64 count_transfer_polling;
131 u64 count_transfer_irq;
132 u64 count_transfer_irq_after_polling;
133 u64 count_transfer_dma;
Chris Bootf8043872013-03-11 21:38:24 -0600134};
135
Martin Sperl154f7da2019-04-23 20:15:13 +0000136#if defined(CONFIG_DEBUG_FS)
137static void bcm2835_debugfs_create(struct bcm2835_spi *bs,
138 const char *dname)
139{
140 char name[64];
141 struct dentry *dir;
142
143 /* get full name */
144 snprintf(name, sizeof(name), "spi-bcm2835-%s", dname);
145
146 /* the base directory */
147 dir = debugfs_create_dir(name, NULL);
148 bs->debugfs_dir = dir;
149
150 /* the counters */
151 debugfs_create_u64("count_transfer_polling", 0444, dir,
152 &bs->count_transfer_polling);
153 debugfs_create_u64("count_transfer_irq", 0444, dir,
154 &bs->count_transfer_irq);
155 debugfs_create_u64("count_transfer_irq_after_polling", 0444, dir,
156 &bs->count_transfer_irq_after_polling);
157 debugfs_create_u64("count_transfer_dma", 0444, dir,
158 &bs->count_transfer_dma);
159}
160
161static void bcm2835_debugfs_remove(struct bcm2835_spi *bs)
162{
163 debugfs_remove_recursive(bs->debugfs_dir);
164 bs->debugfs_dir = NULL;
165}
166#else
167static void bcm2835_debugfs_create(struct bcm2835_spi *bs,
168 const char *dname)
169{
170}
171
172static void bcm2835_debugfs_remove(struct bcm2835_spi *bs)
173{
174}
175#endif /* CONFIG_DEBUG_FS */
176
Chris Bootf8043872013-03-11 21:38:24 -0600177static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg)
178{
179 return readl(bs->regs + reg);
180}
181
182static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val)
183{
184 writel(val, bs->regs + reg);
185}
186
Martin Sperl4adf3122015-03-23 15:11:53 +0100187static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs)
Chris Bootf8043872013-03-11 21:38:24 -0600188{
189 u8 byte;
190
Martin Sperle34ff012015-03-26 11:08:36 +0100191 while ((bs->rx_len) &&
192 (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
Chris Bootf8043872013-03-11 21:38:24 -0600193 byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
194 if (bs->rx_buf)
195 *bs->rx_buf++ = byte;
Martin Sperle34ff012015-03-26 11:08:36 +0100196 bs->rx_len--;
Chris Bootf8043872013-03-11 21:38:24 -0600197 }
198}
199
Martin Sperl4adf3122015-03-23 15:11:53 +0100200static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
Chris Bootf8043872013-03-11 21:38:24 -0600201{
202 u8 byte;
203
Martin Sperle34ff012015-03-26 11:08:36 +0100204 while ((bs->tx_len) &&
Martin Sperl4adf3122015-03-23 15:11:53 +0100205 (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
Chris Bootf8043872013-03-11 21:38:24 -0600206 byte = bs->tx_buf ? *bs->tx_buf++ : 0;
207 bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
Martin Sperle34ff012015-03-26 11:08:36 +0100208 bs->tx_len--;
Chris Bootf8043872013-03-11 21:38:24 -0600209 }
210}
211
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100212/**
213 * bcm2835_rd_fifo_count() - blindly read exactly @count bytes from RX FIFO
214 * @bs: BCM2835 SPI controller
215 * @count: bytes to read from RX FIFO
216 *
217 * The caller must ensure that @bs->rx_len is greater than or equal to @count,
218 * that the RX FIFO contains at least @count bytes and that the DMA Enable flag
219 * in the CS register is set (such that a read from the FIFO register receives
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100220 * 32-bit instead of just 8-bit). Moreover @bs->rx_buf must not be %NULL.
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100221 */
222static inline void bcm2835_rd_fifo_count(struct bcm2835_spi *bs, int count)
223{
224 u32 val;
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100225 int len;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100226
227 bs->rx_len -= count;
228
229 while (count > 0) {
230 val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100231 len = min(count, 4);
232 memcpy(bs->rx_buf, &val, len);
233 bs->rx_buf += len;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100234 count -= 4;
235 }
236}
237
238/**
239 * bcm2835_wr_fifo_count() - blindly write exactly @count bytes to TX FIFO
240 * @bs: BCM2835 SPI controller
241 * @count: bytes to write to TX FIFO
242 *
243 * The caller must ensure that @bs->tx_len is greater than or equal to @count,
244 * that the TX FIFO can accommodate @count bytes and that the DMA Enable flag
245 * in the CS register is set (such that a write to the FIFO register transmits
246 * 32-bit instead of just 8-bit).
247 */
248static inline void bcm2835_wr_fifo_count(struct bcm2835_spi *bs, int count)
249{
250 u32 val;
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100251 int len;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100252
253 bs->tx_len -= count;
254
255 while (count > 0) {
256 if (bs->tx_buf) {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100257 len = min(count, 4);
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100258 memcpy(&val, bs->tx_buf, len);
259 bs->tx_buf += len;
260 } else {
261 val = 0;
262 }
263 bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
264 count -= 4;
265 }
266}
267
268/**
269 * bcm2835_wait_tx_fifo_empty() - busy-wait for TX FIFO to empty
270 * @bs: BCM2835 SPI controller
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100271 *
272 * The caller must ensure that the RX FIFO can accommodate as many bytes
273 * as have been written to the TX FIFO: Transmission is halted once the
274 * RX FIFO is full, causing this function to spin forever.
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100275 */
276static inline void bcm2835_wait_tx_fifo_empty(struct bcm2835_spi *bs)
277{
278 while (!(bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_DONE))
279 cpu_relax();
280}
281
Lukas Wunner2e0733b2018-11-29 16:45:24 +0100282/**
283 * bcm2835_rd_fifo_blind() - blindly read up to @count bytes from RX FIFO
284 * @bs: BCM2835 SPI controller
285 * @count: bytes available for reading in RX FIFO
286 */
287static inline void bcm2835_rd_fifo_blind(struct bcm2835_spi *bs, int count)
288{
289 u8 val;
290
291 count = min(count, bs->rx_len);
292 bs->rx_len -= count;
293
294 while (count) {
295 val = bcm2835_rd(bs, BCM2835_SPI_FIFO);
296 if (bs->rx_buf)
297 *bs->rx_buf++ = val;
298 count--;
299 }
300}
301
302/**
303 * bcm2835_wr_fifo_blind() - blindly write up to @count bytes to TX FIFO
304 * @bs: BCM2835 SPI controller
305 * @count: bytes available for writing in TX FIFO
306 */
307static inline void bcm2835_wr_fifo_blind(struct bcm2835_spi *bs, int count)
308{
309 u8 val;
310
311 count = min(count, bs->tx_len);
312 bs->tx_len -= count;
313
314 while (count) {
315 val = bs->tx_buf ? *bs->tx_buf++ : 0;
316 bcm2835_wr(bs, BCM2835_SPI_FIFO, val);
317 count--;
318 }
319}
320
Martin Sperle34ff012015-03-26 11:08:36 +0100321static void bcm2835_spi_reset_hw(struct spi_master *master)
322{
323 struct bcm2835_spi *bs = spi_master_get_devdata(master);
324 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
325
326 /* Disable SPI interrupts and transfer */
327 cs &= ~(BCM2835_SPI_CS_INTR |
328 BCM2835_SPI_CS_INTD |
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000329 BCM2835_SPI_CS_DMAEN |
Martin Sperle34ff012015-03-26 11:08:36 +0100330 BCM2835_SPI_CS_TA);
331 /* and reset RX/TX FIFOS */
332 cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
333
334 /* and reset the SPI_HW */
335 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000336 /* as well as DLEN */
337 bcm2835_wr(bs, BCM2835_SPI_DLEN, 0);
Martin Sperle34ff012015-03-26 11:08:36 +0100338}
339
Chris Bootf8043872013-03-11 21:38:24 -0600340static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
341{
342 struct spi_master *master = dev_id;
343 struct bcm2835_spi *bs = spi_master_get_devdata(master);
Lukas Wunner2e0733b2018-11-29 16:45:24 +0100344 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
345
346 /*
347 * An interrupt is signaled either if DONE is set (TX FIFO empty)
348 * or if RXR is set (RX FIFO >= ¾ full).
349 */
350 if (cs & BCM2835_SPI_CS_RXF)
351 bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
352 else if (cs & BCM2835_SPI_CS_RXR)
353 bcm2835_rd_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE_3_4);
354
355 if (bs->tx_len && cs & BCM2835_SPI_CS_DONE)
356 bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
Chris Bootf8043872013-03-11 21:38:24 -0600357
Martin Sperl4adf3122015-03-23 15:11:53 +0100358 /* Read as many bytes as possible from FIFO */
359 bcm2835_rd_fifo(bs);
Martin Sperle34ff012015-03-26 11:08:36 +0100360 /* Write as many bytes as possible to FIFO */
361 bcm2835_wr_fifo(bs);
Chris Bootf8043872013-03-11 21:38:24 -0600362
Lukas Wunner56c17232018-11-08 08:06:10 +0100363 if (!bs->rx_len) {
Martin Sperle34ff012015-03-26 11:08:36 +0100364 /* Transfer complete - reset SPI HW */
365 bcm2835_spi_reset_hw(master);
366 /* wake up the framework */
367 complete(&master->xfer_completion);
Chris Bootf8043872013-03-11 21:38:24 -0600368 }
369
Martin Sperl4adf3122015-03-23 15:11:53 +0100370 return IRQ_HANDLED;
Chris Bootf8043872013-03-11 21:38:24 -0600371}
372
Martin Sperl704f32d2015-04-06 17:16:30 +0000373static int bcm2835_spi_transfer_one_irq(struct spi_master *master,
374 struct spi_device *spi,
375 struct spi_transfer *tfr,
Lukas Wunner2e0733b2018-11-29 16:45:24 +0100376 u32 cs, bool fifo_empty)
Martin Sperl704f32d2015-04-06 17:16:30 +0000377{
378 struct bcm2835_spi *bs = spi_master_get_devdata(master);
Chris Bootf8043872013-03-11 21:38:24 -0600379
Martin Sperl154f7da2019-04-23 20:15:13 +0000380 /* update usage statistics */
381 bs->count_transfer_irq++;
382
Chris Bootf8043872013-03-11 21:38:24 -0600383 /*
Lukas Wunner5c09e422018-11-08 08:06:10 +0100384 * Enable HW block, but with interrupts still disabled.
385 * Otherwise the empty TX FIFO would immediately trigger an interrupt.
Chris Bootf8043872013-03-11 21:38:24 -0600386 */
Lukas Wunner5c09e422018-11-08 08:06:10 +0100387 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
388
389 /* fill TX FIFO as much as possible */
Lukas Wunner2e0733b2018-11-29 16:45:24 +0100390 if (fifo_empty)
391 bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
Lukas Wunner5c09e422018-11-08 08:06:10 +0100392 bcm2835_wr_fifo(bs);
393
394 /* enable interrupts */
Martin Sperle34ff012015-03-26 11:08:36 +0100395 cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
Chris Bootf8043872013-03-11 21:38:24 -0600396 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
397
Martin Sperle34ff012015-03-26 11:08:36 +0100398 /* signal that we need to wait for completion */
399 return 1;
Chris Bootf8043872013-03-11 21:38:24 -0600400}
401
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100402/**
403 * bcm2835_spi_transfer_prologue() - transfer first few bytes without DMA
404 * @master: SPI master
405 * @tfr: SPI transfer
406 * @bs: BCM2835 SPI controller
407 * @cs: CS register
408 *
409 * A limitation in DMA mode is that the FIFO must be accessed in 4 byte chunks.
410 * Only the final write access is permitted to transmit less than 4 bytes, the
411 * SPI controller deduces its intended size from the DLEN register.
412 *
413 * If a TX or RX sglist contains multiple entries, one per page, and the first
414 * entry starts in the middle of a page, that first entry's length may not be
415 * a multiple of 4. Subsequent entries are fine because they span an entire
416 * page, hence do have a length that's a multiple of 4.
417 *
418 * This cannot happen with kmalloc'ed buffers (which is what most clients use)
419 * because they are contiguous in physical memory and therefore not split on
420 * page boundaries by spi_map_buf(). But it *can* happen with vmalloc'ed
421 * buffers.
422 *
423 * The DMA engine is incapable of combining sglist entries into a continuous
424 * stream of 4 byte chunks, it treats every entry separately: A TX entry is
425 * rounded up a to a multiple of 4 bytes by transmitting surplus bytes, an RX
426 * entry is rounded up by throwing away received bytes.
427 *
428 * Overcome this limitation by transferring the first few bytes without DMA:
429 * E.g. if the first TX sglist entry's length is 23 and the first RX's is 42,
430 * write 3 bytes to the TX FIFO but read only 2 bytes from the RX FIFO.
431 * The residue of 1 byte in the RX FIFO is picked up by DMA. Together with
432 * the rest of the first RX sglist entry it makes up a multiple of 4 bytes.
433 *
434 * Should the RX prologue be larger, say, 3 vis-à-vis a TX prologue of 1,
435 * write 1 + 4 = 5 bytes to the TX FIFO and read 3 bytes from the RX FIFO.
436 * Caution, the additional 4 bytes spill over to the second TX sglist entry
437 * if the length of the first is *exactly* 1.
438 *
439 * At most 6 bytes are written and at most 3 bytes read. Do we know the
440 * transfer has this many bytes? Yes, see BCM2835_SPI_DMA_MIN_LENGTH.
441 *
442 * The FIFO is normally accessed with 8-bit width by the CPU and 32-bit width
443 * by the DMA engine. Toggling the DMA Enable flag in the CS register switches
444 * the width but also garbles the FIFO's contents. The prologue must therefore
445 * be transmitted in 32-bit width to ensure that the following DMA transfer can
446 * pick up the residue in the RX FIFO in ungarbled form.
447 */
448static void bcm2835_spi_transfer_prologue(struct spi_master *master,
449 struct spi_transfer *tfr,
450 struct bcm2835_spi *bs,
451 u32 cs)
452{
453 int tx_remaining;
454
455 bs->tfr = tfr;
456 bs->tx_prologue = 0;
457 bs->rx_prologue = 0;
458 bs->tx_spillover = false;
459
460 if (!sg_is_last(&tfr->tx_sg.sgl[0]))
461 bs->tx_prologue = sg_dma_len(&tfr->tx_sg.sgl[0]) & 3;
462
463 if (!sg_is_last(&tfr->rx_sg.sgl[0])) {
464 bs->rx_prologue = sg_dma_len(&tfr->rx_sg.sgl[0]) & 3;
465
466 if (bs->rx_prologue > bs->tx_prologue) {
467 if (sg_is_last(&tfr->tx_sg.sgl[0])) {
468 bs->tx_prologue = bs->rx_prologue;
469 } else {
470 bs->tx_prologue += 4;
471 bs->tx_spillover =
472 !(sg_dma_len(&tfr->tx_sg.sgl[0]) & ~3);
473 }
474 }
475 }
476
477 /* rx_prologue > 0 implies tx_prologue > 0, so check only the latter */
478 if (!bs->tx_prologue)
479 return;
480
481 /* Write and read RX prologue. Adjust first entry in RX sglist. */
482 if (bs->rx_prologue) {
483 bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->rx_prologue);
484 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
485 | BCM2835_SPI_CS_DMAEN);
486 bcm2835_wr_fifo_count(bs, bs->rx_prologue);
487 bcm2835_wait_tx_fifo_empty(bs);
488 bcm2835_rd_fifo_count(bs, bs->rx_prologue);
489 bcm2835_spi_reset_hw(master);
490
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100491 dma_sync_single_for_device(master->dma_rx->device->dev,
492 sg_dma_address(&tfr->rx_sg.sgl[0]),
493 bs->rx_prologue, DMA_FROM_DEVICE);
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100494
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100495 sg_dma_address(&tfr->rx_sg.sgl[0]) += bs->rx_prologue;
496 sg_dma_len(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100497 }
498
499 /*
500 * Write remaining TX prologue. Adjust first entry in TX sglist.
501 * Also adjust second entry if prologue spills over to it.
502 */
503 tx_remaining = bs->tx_prologue - bs->rx_prologue;
504 if (tx_remaining) {
505 bcm2835_wr(bs, BCM2835_SPI_DLEN, tx_remaining);
506 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA
507 | BCM2835_SPI_CS_DMAEN);
508 bcm2835_wr_fifo_count(bs, tx_remaining);
509 bcm2835_wait_tx_fifo_empty(bs);
510 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_CLEAR_TX);
511 }
512
513 if (likely(!bs->tx_spillover)) {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100514 sg_dma_address(&tfr->tx_sg.sgl[0]) += bs->tx_prologue;
515 sg_dma_len(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100516 } else {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100517 sg_dma_len(&tfr->tx_sg.sgl[0]) = 0;
518 sg_dma_address(&tfr->tx_sg.sgl[1]) += 4;
519 sg_dma_len(&tfr->tx_sg.sgl[1]) -= 4;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100520 }
521}
522
523/**
524 * bcm2835_spi_undo_prologue() - reconstruct original sglist state
525 * @bs: BCM2835 SPI controller
526 *
527 * Undo changes which were made to an SPI transfer's sglist when transmitting
528 * the prologue. This is necessary to ensure the same memory ranges are
529 * unmapped that were originally mapped.
530 */
531static void bcm2835_spi_undo_prologue(struct bcm2835_spi *bs)
532{
533 struct spi_transfer *tfr = bs->tfr;
534
535 if (!bs->tx_prologue)
536 return;
537
538 if (bs->rx_prologue) {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100539 sg_dma_address(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue;
540 sg_dma_len(&tfr->rx_sg.sgl[0]) += bs->rx_prologue;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100541 }
542
543 if (likely(!bs->tx_spillover)) {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100544 sg_dma_address(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue;
545 sg_dma_len(&tfr->tx_sg.sgl[0]) += bs->tx_prologue;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100546 } else {
Lukas Wunnerb31a9292018-11-29 16:45:24 +0100547 sg_dma_len(&tfr->tx_sg.sgl[0]) = bs->tx_prologue - 4;
548 sg_dma_address(&tfr->tx_sg.sgl[1]) -= 4;
549 sg_dma_len(&tfr->tx_sg.sgl[1]) += 4;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100550 }
551}
552
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000553static void bcm2835_spi_dma_done(void *data)
554{
555 struct spi_master *master = data;
556 struct bcm2835_spi *bs = spi_master_get_devdata(master);
557
558 /* reset fifo and HW */
559 bcm2835_spi_reset_hw(master);
560
561 /* and terminate tx-dma as we do not have an irq for it
562 * because when the rx dma will terminate and this callback
563 * is called the tx-dma must have finished - can't get to this
564 * situation otherwise...
565 */
Lukas Wunnere82b0b32018-11-08 08:06:10 +0100566 if (cmpxchg(&bs->dma_pending, true, false)) {
Lukas Wunner25277042018-11-29 16:45:24 +0100567 dmaengine_terminate_async(master->dma_tx);
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100568 bcm2835_spi_undo_prologue(bs);
Lukas Wunnere82b0b32018-11-08 08:06:10 +0100569 }
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000570
571 /* and mark as completed */;
572 complete(&master->xfer_completion);
573}
574
575static int bcm2835_spi_prepare_sg(struct spi_master *master,
576 struct spi_transfer *tfr,
577 bool is_tx)
578{
579 struct dma_chan *chan;
580 struct scatterlist *sgl;
581 unsigned int nents;
582 enum dma_transfer_direction dir;
583 unsigned long flags;
584
585 struct dma_async_tx_descriptor *desc;
586 dma_cookie_t cookie;
587
588 if (is_tx) {
589 dir = DMA_MEM_TO_DEV;
590 chan = master->dma_tx;
591 nents = tfr->tx_sg.nents;
592 sgl = tfr->tx_sg.sgl;
593 flags = 0 /* no tx interrupt */;
594
595 } else {
596 dir = DMA_DEV_TO_MEM;
597 chan = master->dma_rx;
598 nents = tfr->rx_sg.nents;
599 sgl = tfr->rx_sg.sgl;
600 flags = DMA_PREP_INTERRUPT;
601 }
602 /* prepare the channel */
603 desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
604 if (!desc)
605 return -EINVAL;
606
607 /* set callback for rx */
608 if (!is_tx) {
609 desc->callback = bcm2835_spi_dma_done;
610 desc->callback_param = master;
611 }
612
613 /* submit it to DMA-engine */
614 cookie = dmaengine_submit(desc);
615
616 return dma_submit_error(cookie);
617}
618
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000619static int bcm2835_spi_transfer_one_dma(struct spi_master *master,
620 struct spi_device *spi,
621 struct spi_transfer *tfr,
622 u32 cs)
623{
624 struct bcm2835_spi *bs = spi_master_get_devdata(master);
625 int ret;
626
Martin Sperl154f7da2019-04-23 20:15:13 +0000627 /* update usage statistics */
628 bs->count_transfer_dma++;
629
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100630 /*
631 * Transfer first few bytes without DMA if length of first TX or RX
632 * sglist entry is not a multiple of 4 bytes (hardware limitation).
633 */
634 bcm2835_spi_transfer_prologue(master, tfr, bs, cs);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000635
636 /* setup tx-DMA */
637 ret = bcm2835_spi_prepare_sg(master, tfr, true);
638 if (ret)
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100639 goto err_reset_hw;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000640
641 /* start TX early */
642 dma_async_issue_pending(master->dma_tx);
643
644 /* mark as dma pending */
645 bs->dma_pending = 1;
646
647 /* set the DMA length */
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100648 bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->tx_len);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000649
650 /* start the HW */
651 bcm2835_wr(bs, BCM2835_SPI_CS,
652 cs | BCM2835_SPI_CS_TA | BCM2835_SPI_CS_DMAEN);
653
654 /* setup rx-DMA late - to run transfers while
655 * mapping of the rx buffers still takes place
656 * this saves 10us or more.
657 */
658 ret = bcm2835_spi_prepare_sg(master, tfr, false);
659 if (ret) {
660 /* need to reset on errors */
Lukas Wunner25277042018-11-29 16:45:24 +0100661 dmaengine_terminate_sync(master->dma_tx);
Lukas Wunnerdbc94412018-11-08 08:06:10 +0100662 bs->dma_pending = false;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100663 goto err_reset_hw;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000664 }
665
666 /* start rx dma late */
667 dma_async_issue_pending(master->dma_rx);
668
669 /* wait for wakeup in framework */
670 return 1;
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100671
672err_reset_hw:
673 bcm2835_spi_reset_hw(master);
674 bcm2835_spi_undo_prologue(bs);
675 return ret;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000676}
677
678static bool bcm2835_spi_can_dma(struct spi_master *master,
679 struct spi_device *spi,
680 struct spi_transfer *tfr)
681{
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000682 /* we start DMA efforts only on bigger transfers */
683 if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
684 return false;
685
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000686 /* return OK */
687 return true;
688}
689
kbuild test robot29ad1a72015-05-12 19:43:59 +0800690static void bcm2835_dma_release(struct spi_master *master)
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000691{
692 if (master->dma_tx) {
Lukas Wunner25277042018-11-29 16:45:24 +0100693 dmaengine_terminate_sync(master->dma_tx);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000694 dma_release_channel(master->dma_tx);
695 master->dma_tx = NULL;
696 }
697 if (master->dma_rx) {
Lukas Wunner25277042018-11-29 16:45:24 +0100698 dmaengine_terminate_sync(master->dma_rx);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000699 dma_release_channel(master->dma_rx);
700 master->dma_rx = NULL;
701 }
702}
703
kbuild test robot29ad1a72015-05-12 19:43:59 +0800704static void bcm2835_dma_init(struct spi_master *master, struct device *dev)
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000705{
706 struct dma_slave_config slave_config;
707 const __be32 *addr;
708 dma_addr_t dma_reg_base;
709 int ret;
710
711 /* base address in dma-space */
712 addr = of_get_address(master->dev.of_node, 0, NULL, NULL);
713 if (!addr) {
714 dev_err(dev, "could not get DMA-register address - not using dma mode\n");
715 goto err;
716 }
717 dma_reg_base = be32_to_cpup(addr);
718
719 /* get tx/rx dma */
720 master->dma_tx = dma_request_slave_channel(dev, "tx");
721 if (!master->dma_tx) {
722 dev_err(dev, "no tx-dma configuration found - not using dma mode\n");
723 goto err;
724 }
725 master->dma_rx = dma_request_slave_channel(dev, "rx");
726 if (!master->dma_rx) {
727 dev_err(dev, "no rx-dma configuration found - not using dma mode\n");
728 goto err_release;
729 }
730
731 /* configure DMAs */
732 slave_config.direction = DMA_MEM_TO_DEV;
733 slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
734 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
735
736 ret = dmaengine_slave_config(master->dma_tx, &slave_config);
737 if (ret)
738 goto err_config;
739
740 slave_config.direction = DMA_DEV_TO_MEM;
741 slave_config.src_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
742 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
743
744 ret = dmaengine_slave_config(master->dma_rx, &slave_config);
745 if (ret)
746 goto err_config;
747
748 /* all went well, so set can_dma */
749 master->can_dma = bcm2835_spi_can_dma;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000750 /* need to do TX AND RX DMA, so we need dummy buffers */
751 master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
752
753 return;
754
755err_config:
756 dev_err(dev, "issue configuring dma: %d - not using DMA mode\n",
757 ret);
758err_release:
759 bcm2835_dma_release(master);
760err:
761 return;
762}
763
Martin Sperla750b122015-04-22 07:33:03 +0000764static int bcm2835_spi_transfer_one_poll(struct spi_master *master,
765 struct spi_device *spi,
766 struct spi_transfer *tfr,
Martin Sperl9ac3f902019-04-23 20:15:08 +0000767 u32 cs)
Martin Sperla750b122015-04-22 07:33:03 +0000768{
769 struct bcm2835_spi *bs = spi_master_get_devdata(master);
770 unsigned long timeout;
771
Martin Sperl154f7da2019-04-23 20:15:13 +0000772 /* update usage statistics */
773 bs->count_transfer_polling++;
774
Martin Sperla750b122015-04-22 07:33:03 +0000775 /* enable HW block without interrupts */
776 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
777
778 /* fill in the fifo before timeout calculations
779 * if we are interrupted here, then the data is
780 * getting transferred by the HW while we are interrupted
781 */
Lukas Wunner2e0733b2018-11-29 16:45:24 +0100782 bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE);
Martin Sperla750b122015-04-22 07:33:03 +0000783
Martin Sperlff245d92019-04-23 20:15:11 +0000784 /* set the timeout to at least 2 jiffies */
785 timeout = jiffies + 2 + HZ * polling_limit_us / 1000000;
Martin Sperla750b122015-04-22 07:33:03 +0000786
787 /* loop until finished the transfer */
788 while (bs->rx_len) {
789 /* fill in tx fifo with remaining data */
790 bcm2835_wr_fifo(bs);
791
792 /* read from fifo as much as possible */
793 bcm2835_rd_fifo(bs);
794
795 /* if there is still data pending to read
796 * then check the timeout
797 */
798 if (bs->rx_len && time_after(jiffies, timeout)) {
799 dev_dbg_ratelimited(&spi->dev,
800 "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
801 jiffies - timeout,
802 bs->tx_len, bs->rx_len);
803 /* fall back to interrupt mode */
Martin Sperl154f7da2019-04-23 20:15:13 +0000804
805 /* update usage statistics */
806 bs->count_transfer_irq_after_polling++;
807
Martin Sperla750b122015-04-22 07:33:03 +0000808 return bcm2835_spi_transfer_one_irq(master, spi,
Lukas Wunner2e0733b2018-11-29 16:45:24 +0100809 tfr, cs, false);
Martin Sperla750b122015-04-22 07:33:03 +0000810 }
811 }
812
813 /* Transfer complete - reset SPI HW */
814 bcm2835_spi_reset_hw(master);
815 /* and return without waiting for completion */
816 return 0;
817}
818
Martin Sperl704f32d2015-04-06 17:16:30 +0000819static int bcm2835_spi_transfer_one(struct spi_master *master,
820 struct spi_device *spi,
821 struct spi_transfer *tfr)
822{
823 struct bcm2835_spi *bs = spi_master_get_devdata(master);
Martin Sperlff245d92019-04-23 20:15:11 +0000824 unsigned long spi_hz, clk_hz, cdiv, spi_used_hz;
825 unsigned long hz_per_byte, byte_limit;
Martin Sperl704f32d2015-04-06 17:16:30 +0000826 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
827
828 /* set clock */
829 spi_hz = tfr->speed_hz;
830 clk_hz = clk_get_rate(bs->clk);
831
832 if (spi_hz >= clk_hz / 2) {
833 cdiv = 2; /* clk_hz/2 is the fastest we can go */
834 } else if (spi_hz) {
835 /* CDIV must be a multiple of two */
836 cdiv = DIV_ROUND_UP(clk_hz, spi_hz);
837 cdiv += (cdiv % 2);
838
839 if (cdiv >= 65536)
840 cdiv = 0; /* 0 is the slowest we can go */
841 } else {
842 cdiv = 0; /* 0 is the slowest we can go */
843 }
844 spi_used_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536);
845 bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
846
Martin Sperlacace732015-07-28 14:03:12 +0000847 /* handle all the 3-wire mode */
Martin Sperl704f32d2015-04-06 17:16:30 +0000848 if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf))
849 cs |= BCM2835_SPI_CS_REN;
Martin Sperlacace732015-07-28 14:03:12 +0000850 else
851 cs &= ~BCM2835_SPI_CS_REN;
Martin Sperl704f32d2015-04-06 17:16:30 +0000852
Lukas Wunner5c09e422018-11-08 08:06:10 +0100853 /*
854 * The driver always uses software-controlled GPIO Chip Select.
855 * Set the hardware-controlled native Chip Select to an invalid
856 * value to prevent it from interfering.
Martin Sperl704f32d2015-04-06 17:16:30 +0000857 */
Lukas Wunner5c09e422018-11-08 08:06:10 +0100858 cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
Martin Sperl704f32d2015-04-06 17:16:30 +0000859
860 /* set transmit buffers and length */
861 bs->tx_buf = tfr->tx_buf;
862 bs->rx_buf = tfr->rx_buf;
863 bs->tx_len = tfr->len;
864 bs->rx_len = tfr->len;
865
Martin Sperl7f1922e2019-04-23 20:15:09 +0000866 /* Calculate the estimated time in us the transfer runs. Note that
867 * there is 1 idle clocks cycles after each byte getting transferred
868 * so we have 9 cycles/byte. This is used to find the number of Hz
869 * per byte per polling limit. E.g., we can transfer 1 byte in 30 us
870 * per 300,000 Hz of bus clock.
871 */
Martin Sperlff245d92019-04-23 20:15:11 +0000872 hz_per_byte = polling_limit_us ? (9 * 1000000) / polling_limit_us : 0;
873 byte_limit = hz_per_byte ? spi_used_hz / hz_per_byte : 1;
874
Martin Sperl7f1922e2019-04-23 20:15:09 +0000875 /* run in polling mode for short transfers */
Martin Sperlff245d92019-04-23 20:15:11 +0000876 if (tfr->len < byte_limit)
Martin Sperl9ac3f902019-04-23 20:15:08 +0000877 return bcm2835_spi_transfer_one_poll(master, spi, tfr, cs);
Martin Sperl704f32d2015-04-06 17:16:30 +0000878
Martin Sperlc41d62b2019-04-23 20:15:10 +0000879 /* run in dma mode if conditions are right
880 * Note that unlike poll or interrupt mode DMA mode does not have
881 * this 1 idle clock cycle pattern but runs the spi clock without gaps
882 */
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000883 if (master->can_dma && bcm2835_spi_can_dma(master, spi, tfr))
884 return bcm2835_spi_transfer_one_dma(master, spi, tfr, cs);
885
886 /* run in interrupt-mode */
Lukas Wunner2e0733b2018-11-29 16:45:24 +0100887 return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs, true);
Martin Sperl704f32d2015-04-06 17:16:30 +0000888}
889
Martin Sperlacace732015-07-28 14:03:12 +0000890static int bcm2835_spi_prepare_message(struct spi_master *master,
891 struct spi_message *msg)
892{
893 struct spi_device *spi = msg->spi;
894 struct bcm2835_spi *bs = spi_master_get_devdata(master);
895 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
Meghana Madhyastha8b7bd102019-04-13 20:24:14 +0200896 int ret;
897
898 /*
899 * DMA transfers are limited to 16 bit (0 to 65535 bytes) by the SPI HW
900 * due to DLEN. Split up transfers (32-bit FIFO aligned) if the limit is
901 * exceeded.
902 */
903 ret = spi_split_transfers_maxsize(master, msg, 65532,
904 GFP_KERNEL | GFP_DMA);
905 if (ret)
906 return ret;
Martin Sperlacace732015-07-28 14:03:12 +0000907
908 cs &= ~(BCM2835_SPI_CS_CPOL | BCM2835_SPI_CS_CPHA);
909
910 if (spi->mode & SPI_CPOL)
911 cs |= BCM2835_SPI_CS_CPOL;
912 if (spi->mode & SPI_CPHA)
913 cs |= BCM2835_SPI_CS_CPHA;
914
915 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
916
917 return 0;
918}
919
Martin Sperle34ff012015-03-26 11:08:36 +0100920static void bcm2835_spi_handle_err(struct spi_master *master,
921 struct spi_message *msg)
Chris Bootf8043872013-03-11 21:38:24 -0600922{
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000923 struct bcm2835_spi *bs = spi_master_get_devdata(master);
924
925 /* if an error occurred and we have an active dma, then terminate */
Lukas Wunnere82b0b32018-11-08 08:06:10 +0100926 if (cmpxchg(&bs->dma_pending, true, false)) {
Lukas Wunner25277042018-11-29 16:45:24 +0100927 dmaengine_terminate_sync(master->dma_tx);
928 dmaengine_terminate_sync(master->dma_rx);
Lukas Wunner3bd7f652018-11-08 08:06:10 +0100929 bcm2835_spi_undo_prologue(bs);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000930 }
931 /* and reset */
Martin Sperle34ff012015-03-26 11:08:36 +0100932 bcm2835_spi_reset_hw(master);
Chris Bootf8043872013-03-11 21:38:24 -0600933}
934
Martin Sperla30a5552015-04-06 17:16:31 +0000935static int chip_match_name(struct gpio_chip *chip, void *data)
936{
937 return !strcmp(chip->label, data);
938}
939
Martin Sperle34ff012015-03-26 11:08:36 +0100940static int bcm2835_spi_setup(struct spi_device *spi)
941{
Martin Sperla30a5552015-04-06 17:16:31 +0000942 int err;
943 struct gpio_chip *chip;
Martin Sperle34ff012015-03-26 11:08:36 +0100944 /*
945 * sanity checking the native-chipselects
946 */
947 if (spi->mode & SPI_NO_CS)
948 return 0;
949 if (gpio_is_valid(spi->cs_gpio))
950 return 0;
Martin Sperla30a5552015-04-06 17:16:31 +0000951 if (spi->chip_select > 1) {
952 /* error in the case of native CS requested with CS > 1
953 * officially there is a CS2, but it is not documented
954 * which GPIO is connected with that...
955 */
956 dev_err(&spi->dev,
957 "setup: only two native chip-selects are supported\n");
958 return -EINVAL;
959 }
960 /* now translate native cs to GPIO */
961
962 /* get the gpio chip for the base */
963 chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
964 if (!chip)
Martin Sperle34ff012015-03-26 11:08:36 +0100965 return 0;
966
Martin Sperla30a5552015-04-06 17:16:31 +0000967 /* and calculate the real CS */
968 spi->cs_gpio = chip->base + 8 - spi->chip_select;
969
970 /* and set up the "mode" and level */
971 dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n",
972 spi->chip_select, spi->cs_gpio);
973
974 /* set up GPIO as output and pull to the correct level */
975 err = gpio_direction_output(spi->cs_gpio,
976 (spi->mode & SPI_CS_HIGH) ? 0 : 1);
977 if (err) {
978 dev_err(&spi->dev,
979 "could not set CS%i gpio %i as output: %i",
980 spi->chip_select, spi->cs_gpio, err);
981 return err;
982 }
Martin Sperla30a5552015-04-06 17:16:31 +0000983
984 return 0;
Chris Bootf8043872013-03-11 21:38:24 -0600985}
986
987static int bcm2835_spi_probe(struct platform_device *pdev)
988{
989 struct spi_master *master;
990 struct bcm2835_spi *bs;
991 struct resource *res;
992 int err;
993
994 master = spi_alloc_master(&pdev->dev, sizeof(*bs));
995 if (!master) {
996 dev_err(&pdev->dev, "spi_alloc_master() failed\n");
997 return -ENOMEM;
998 }
999
1000 platform_set_drvdata(pdev, master);
1001
1002 master->mode_bits = BCM2835_SPI_MODE_BITS;
Axel Linc2b6a3a2013-08-05 08:43:02 +08001003 master->bits_per_word_mask = SPI_BPW_MASK(8);
Chris Bootf8043872013-03-11 21:38:24 -06001004 master->num_chipselect = 3;
Martin Sperle34ff012015-03-26 11:08:36 +01001005 master->setup = bcm2835_spi_setup;
Martin Sperle34ff012015-03-26 11:08:36 +01001006 master->transfer_one = bcm2835_spi_transfer_one;
1007 master->handle_err = bcm2835_spi_handle_err;
Martin Sperlacace732015-07-28 14:03:12 +00001008 master->prepare_message = bcm2835_spi_prepare_message;
Chris Bootf8043872013-03-11 21:38:24 -06001009 master->dev.of_node = pdev->dev.of_node;
1010
1011 bs = spi_master_get_devdata(master);
1012
Chris Bootf8043872013-03-11 21:38:24 -06001013 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Laurent Navet2d6e75e2013-05-02 14:13:30 +02001014 bs->regs = devm_ioremap_resource(&pdev->dev, res);
1015 if (IS_ERR(bs->regs)) {
1016 err = PTR_ERR(bs->regs);
Chris Bootf8043872013-03-11 21:38:24 -06001017 goto out_master_put;
1018 }
1019
1020 bs->clk = devm_clk_get(&pdev->dev, NULL);
1021 if (IS_ERR(bs->clk)) {
1022 err = PTR_ERR(bs->clk);
1023 dev_err(&pdev->dev, "could not get clk: %d\n", err);
1024 goto out_master_put;
1025 }
1026
Martin Sperlddf0e1c2015-10-15 10:09:11 +00001027 bs->irq = platform_get_irq(pdev, 0);
Chris Bootf8043872013-03-11 21:38:24 -06001028 if (bs->irq <= 0) {
1029 dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
1030 err = bs->irq ? bs->irq : -ENODEV;
1031 goto out_master_put;
1032 }
1033
1034 clk_prepare_enable(bs->clk);
1035
Martin Sperlddf0e1c2015-10-15 10:09:11 +00001036 bcm2835_dma_init(master, &pdev->dev);
1037
1038 /* initialise the hardware with the default polarities */
1039 bcm2835_wr(bs, BCM2835_SPI_CS,
1040 BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
1041
Jingoo Han08bc0542013-12-09 19:25:00 +09001042 err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0,
Martin Sperl342f9482015-03-20 15:26:11 +01001043 dev_name(&pdev->dev), master);
Chris Bootf8043872013-03-11 21:38:24 -06001044 if (err) {
1045 dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
1046 goto out_clk_disable;
1047 }
1048
Jingoo Han247263d2013-09-24 13:23:00 +09001049 err = devm_spi_register_master(&pdev->dev, master);
Chris Bootf8043872013-03-11 21:38:24 -06001050 if (err) {
1051 dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
Jingoo Han08bc0542013-12-09 19:25:00 +09001052 goto out_clk_disable;
Chris Bootf8043872013-03-11 21:38:24 -06001053 }
1054
Martin Sperl154f7da2019-04-23 20:15:13 +00001055 bcm2835_debugfs_create(bs, dev_name(&pdev->dev));
1056
Chris Bootf8043872013-03-11 21:38:24 -06001057 return 0;
1058
Chris Bootf8043872013-03-11 21:38:24 -06001059out_clk_disable:
1060 clk_disable_unprepare(bs->clk);
1061out_master_put:
1062 spi_master_put(master);
1063 return err;
1064}
1065
1066static int bcm2835_spi_remove(struct platform_device *pdev)
1067{
Wei Yongjune0b35b82013-11-15 15:43:27 +08001068 struct spi_master *master = platform_get_drvdata(pdev);
Chris Bootf8043872013-03-11 21:38:24 -06001069 struct bcm2835_spi *bs = spi_master_get_devdata(master);
1070
Martin Sperl154f7da2019-04-23 20:15:13 +00001071 bcm2835_debugfs_remove(bs);
1072
Chris Bootf8043872013-03-11 21:38:24 -06001073 /* Clear FIFOs, and disable the HW block */
1074 bcm2835_wr(bs, BCM2835_SPI_CS,
1075 BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
1076
1077 clk_disable_unprepare(bs->clk);
Chris Bootf8043872013-03-11 21:38:24 -06001078
Martin Sperl3ecd37e2015-05-10 20:47:28 +00001079 bcm2835_dma_release(master);
1080
Chris Bootf8043872013-03-11 21:38:24 -06001081 return 0;
1082}
1083
1084static const struct of_device_id bcm2835_spi_match[] = {
1085 { .compatible = "brcm,bcm2835-spi", },
1086 {}
1087};
1088MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
1089
1090static struct platform_driver bcm2835_spi_driver = {
1091 .driver = {
1092 .name = DRV_NAME,
Chris Bootf8043872013-03-11 21:38:24 -06001093 .of_match_table = bcm2835_spi_match,
1094 },
1095 .probe = bcm2835_spi_probe,
1096 .remove = bcm2835_spi_remove,
1097};
1098module_platform_driver(bcm2835_spi_driver);
1099
1100MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
1101MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
Stefan Wahren22bf6cd2018-10-23 13:06:08 +02001102MODULE_LICENSE("GPL");