blob: c02a9982ac9394d026bd7a3f3585b85670c18acb [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
Martin Sperl7e52be02015-05-12 10:32:08 +000023#include <asm/page.h>
Chris Bootf8043872013-03-11 21:38:24 -060024#include <linux/clk.h>
25#include <linux/completion.h>
26#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
Martin Sperl704f32d2015-04-06 17:16:30 +000075#define BCM2835_SPI_POLLING_LIMIT_US 30
Martin Sperla750b122015-04-22 07:33:03 +000076#define BCM2835_SPI_POLLING_JIFFIES 2
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
Lukas Wunneracf0f852018-11-08 08:06:10 +010083/**
84 * struct bcm2835_spi - BCM2835 SPI controller
85 * @regs: base address of register map
86 * @clk: core clock, divided to calculate serial clock
87 * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full
88 * @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
92 * @dma_pending: whether a DMA transfer is in progress
93 */
Chris Bootf8043872013-03-11 21:38:24 -060094struct bcm2835_spi {
95 void __iomem *regs;
96 struct clk *clk;
97 int irq;
Chris Bootf8043872013-03-11 21:38:24 -060098 const u8 *tx_buf;
99 u8 *rx_buf;
Martin Sperle34ff012015-03-26 11:08:36 +0100100 int tx_len;
101 int rx_len;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000102 bool dma_pending;
Chris Bootf8043872013-03-11 21:38:24 -0600103};
104
105static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg)
106{
107 return readl(bs->regs + reg);
108}
109
110static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val)
111{
112 writel(val, bs->regs + reg);
113}
114
Martin Sperl4adf3122015-03-23 15:11:53 +0100115static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs)
Chris Bootf8043872013-03-11 21:38:24 -0600116{
117 u8 byte;
118
Martin Sperle34ff012015-03-26 11:08:36 +0100119 while ((bs->rx_len) &&
120 (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) {
Chris Bootf8043872013-03-11 21:38:24 -0600121 byte = bcm2835_rd(bs, BCM2835_SPI_FIFO);
122 if (bs->rx_buf)
123 *bs->rx_buf++ = byte;
Martin Sperle34ff012015-03-26 11:08:36 +0100124 bs->rx_len--;
Chris Bootf8043872013-03-11 21:38:24 -0600125 }
126}
127
Martin Sperl4adf3122015-03-23 15:11:53 +0100128static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs)
Chris Bootf8043872013-03-11 21:38:24 -0600129{
130 u8 byte;
131
Martin Sperle34ff012015-03-26 11:08:36 +0100132 while ((bs->tx_len) &&
Martin Sperl4adf3122015-03-23 15:11:53 +0100133 (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) {
Chris Bootf8043872013-03-11 21:38:24 -0600134 byte = bs->tx_buf ? *bs->tx_buf++ : 0;
135 bcm2835_wr(bs, BCM2835_SPI_FIFO, byte);
Martin Sperle34ff012015-03-26 11:08:36 +0100136 bs->tx_len--;
Chris Bootf8043872013-03-11 21:38:24 -0600137 }
138}
139
Martin Sperle34ff012015-03-26 11:08:36 +0100140static void bcm2835_spi_reset_hw(struct spi_master *master)
141{
142 struct bcm2835_spi *bs = spi_master_get_devdata(master);
143 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
144
145 /* Disable SPI interrupts and transfer */
146 cs &= ~(BCM2835_SPI_CS_INTR |
147 BCM2835_SPI_CS_INTD |
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000148 BCM2835_SPI_CS_DMAEN |
Martin Sperle34ff012015-03-26 11:08:36 +0100149 BCM2835_SPI_CS_TA);
150 /* and reset RX/TX FIFOS */
151 cs |= BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX;
152
153 /* and reset the SPI_HW */
154 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000155 /* as well as DLEN */
156 bcm2835_wr(bs, BCM2835_SPI_DLEN, 0);
Martin Sperle34ff012015-03-26 11:08:36 +0100157}
158
Chris Bootf8043872013-03-11 21:38:24 -0600159static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id)
160{
161 struct spi_master *master = dev_id;
162 struct bcm2835_spi *bs = spi_master_get_devdata(master);
Chris Bootf8043872013-03-11 21:38:24 -0600163
Martin Sperl4adf3122015-03-23 15:11:53 +0100164 /* Read as many bytes as possible from FIFO */
165 bcm2835_rd_fifo(bs);
Martin Sperle34ff012015-03-26 11:08:36 +0100166 /* Write as many bytes as possible to FIFO */
167 bcm2835_wr_fifo(bs);
Chris Bootf8043872013-03-11 21:38:24 -0600168
Lukas Wunner56c17232018-11-08 08:06:10 +0100169 if (!bs->rx_len) {
Martin Sperle34ff012015-03-26 11:08:36 +0100170 /* Transfer complete - reset SPI HW */
171 bcm2835_spi_reset_hw(master);
172 /* wake up the framework */
173 complete(&master->xfer_completion);
Chris Bootf8043872013-03-11 21:38:24 -0600174 }
175
Martin Sperl4adf3122015-03-23 15:11:53 +0100176 return IRQ_HANDLED;
Chris Bootf8043872013-03-11 21:38:24 -0600177}
178
Martin Sperl704f32d2015-04-06 17:16:30 +0000179static int bcm2835_spi_transfer_one_irq(struct spi_master *master,
180 struct spi_device *spi,
181 struct spi_transfer *tfr,
182 u32 cs)
183{
184 struct bcm2835_spi *bs = spi_master_get_devdata(master);
Chris Bootf8043872013-03-11 21:38:24 -0600185
Chris Bootf8043872013-03-11 21:38:24 -0600186 /*
Lukas Wunner5c09e422018-11-08 08:06:10 +0100187 * Enable HW block, but with interrupts still disabled.
188 * Otherwise the empty TX FIFO would immediately trigger an interrupt.
Chris Bootf8043872013-03-11 21:38:24 -0600189 */
Lukas Wunner5c09e422018-11-08 08:06:10 +0100190 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
191
192 /* fill TX FIFO as much as possible */
193 bcm2835_wr_fifo(bs);
194
195 /* enable interrupts */
Martin Sperle34ff012015-03-26 11:08:36 +0100196 cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA;
Chris Bootf8043872013-03-11 21:38:24 -0600197 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
198
Martin Sperle34ff012015-03-26 11:08:36 +0100199 /* signal that we need to wait for completion */
200 return 1;
Chris Bootf8043872013-03-11 21:38:24 -0600201}
202
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000203/*
204 * DMA support
205 *
206 * this implementation has currently a few issues in so far as it does
207 * not work arrount limitations of the HW.
208 *
209 * the main one being that DMA transfers are limited to 16 bit
210 * (so 0 to 65535 bytes) by the SPI HW due to BCM2835_SPI_DLEN
211 *
212 * also we currently assume that the scatter-gather fragments are
213 * all multiple of 4 (except the last) - otherwise we would need
214 * to reset the FIFO before subsequent transfers...
215 * this also means that tx/rx transfers sg's need to be of equal size!
216 *
217 * there may be a few more border-cases we may need to address as well
218 * but unfortunately this would mean splitting up the scatter-gather
219 * list making it slightly unpractical...
220 */
221static void bcm2835_spi_dma_done(void *data)
222{
223 struct spi_master *master = data;
224 struct bcm2835_spi *bs = spi_master_get_devdata(master);
225
226 /* reset fifo and HW */
227 bcm2835_spi_reset_hw(master);
228
229 /* and terminate tx-dma as we do not have an irq for it
230 * because when the rx dma will terminate and this callback
231 * is called the tx-dma must have finished - can't get to this
232 * situation otherwise...
233 */
Lukas Wunnere82b0b32018-11-08 08:06:10 +0100234 if (cmpxchg(&bs->dma_pending, true, false)) {
235 dmaengine_terminate_all(master->dma_tx);
236 }
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000237
238 /* and mark as completed */;
239 complete(&master->xfer_completion);
240}
241
242static int bcm2835_spi_prepare_sg(struct spi_master *master,
243 struct spi_transfer *tfr,
244 bool is_tx)
245{
246 struct dma_chan *chan;
247 struct scatterlist *sgl;
248 unsigned int nents;
249 enum dma_transfer_direction dir;
250 unsigned long flags;
251
252 struct dma_async_tx_descriptor *desc;
253 dma_cookie_t cookie;
254
255 if (is_tx) {
256 dir = DMA_MEM_TO_DEV;
257 chan = master->dma_tx;
258 nents = tfr->tx_sg.nents;
259 sgl = tfr->tx_sg.sgl;
260 flags = 0 /* no tx interrupt */;
261
262 } else {
263 dir = DMA_DEV_TO_MEM;
264 chan = master->dma_rx;
265 nents = tfr->rx_sg.nents;
266 sgl = tfr->rx_sg.sgl;
267 flags = DMA_PREP_INTERRUPT;
268 }
269 /* prepare the channel */
270 desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
271 if (!desc)
272 return -EINVAL;
273
274 /* set callback for rx */
275 if (!is_tx) {
276 desc->callback = bcm2835_spi_dma_done;
277 desc->callback_param = master;
278 }
279
280 /* submit it to DMA-engine */
281 cookie = dmaengine_submit(desc);
282
283 return dma_submit_error(cookie);
284}
285
286static inline int bcm2835_check_sg_length(struct sg_table *sgt)
287{
288 int i;
289 struct scatterlist *sgl;
290
291 /* check that the sg entries are word-sized (except for last) */
292 for_each_sg(sgt->sgl, sgl, (int)sgt->nents - 1, i) {
293 if (sg_dma_len(sgl) % 4)
294 return -EFAULT;
295 }
296
297 return 0;
298}
299
300static int bcm2835_spi_transfer_one_dma(struct spi_master *master,
301 struct spi_device *spi,
302 struct spi_transfer *tfr,
303 u32 cs)
304{
305 struct bcm2835_spi *bs = spi_master_get_devdata(master);
306 int ret;
307
308 /* check that the scatter gather segments are all a multiple of 4 */
309 if (bcm2835_check_sg_length(&tfr->tx_sg) ||
310 bcm2835_check_sg_length(&tfr->rx_sg)) {
311 dev_warn_once(&spi->dev,
312 "scatter gather segment length is not a multiple of 4 - falling back to interrupt mode\n");
313 return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs);
314 }
315
316 /* setup tx-DMA */
317 ret = bcm2835_spi_prepare_sg(master, tfr, true);
318 if (ret)
319 return ret;
320
321 /* start TX early */
322 dma_async_issue_pending(master->dma_tx);
323
324 /* mark as dma pending */
325 bs->dma_pending = 1;
326
327 /* set the DMA length */
328 bcm2835_wr(bs, BCM2835_SPI_DLEN, tfr->len);
329
330 /* start the HW */
331 bcm2835_wr(bs, BCM2835_SPI_CS,
332 cs | BCM2835_SPI_CS_TA | BCM2835_SPI_CS_DMAEN);
333
334 /* setup rx-DMA late - to run transfers while
335 * mapping of the rx buffers still takes place
336 * this saves 10us or more.
337 */
338 ret = bcm2835_spi_prepare_sg(master, tfr, false);
339 if (ret) {
340 /* need to reset on errors */
341 dmaengine_terminate_all(master->dma_tx);
Lukas Wunnerdbc94412018-11-08 08:06:10 +0100342 bs->dma_pending = false;
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000343 bcm2835_spi_reset_hw(master);
344 return ret;
345 }
346
347 /* start rx dma late */
348 dma_async_issue_pending(master->dma_rx);
349
350 /* wait for wakeup in framework */
351 return 1;
352}
353
354static bool bcm2835_spi_can_dma(struct spi_master *master,
355 struct spi_device *spi,
356 struct spi_transfer *tfr)
357{
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000358 /* we start DMA efforts only on bigger transfers */
359 if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
360 return false;
361
362 /* BCM2835_SPI_DLEN has defined a max transfer size as
363 * 16 bit, so max is 65535
364 * we can revisit this by using an alternative transfer
365 * method - ideally this would get done without any more
366 * interaction...
367 */
368 if (tfr->len > 65535) {
369 dev_warn_once(&spi->dev,
370 "transfer size of %d too big for dma-transfer\n",
371 tfr->len);
372 return false;
373 }
374
375 /* if we run rx/tx_buf with word aligned addresses then we are OK */
Martin Sperl7e52be02015-05-12 10:32:08 +0000376 if ((((size_t)tfr->rx_buf & 3) == 0) &&
377 (((size_t)tfr->tx_buf & 3) == 0))
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000378 return true;
379
380 /* otherwise we only allow transfers within the same page
381 * to avoid wasting time on dma_mapping when it is not practical
382 */
Martin Sperl2a3fffd2015-09-10 09:32:14 +0000383 if (((size_t)tfr->tx_buf & (PAGE_SIZE - 1)) + tfr->len > PAGE_SIZE) {
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000384 dev_warn_once(&spi->dev,
385 "Unaligned spi tx-transfer bridging page\n");
386 return false;
387 }
Martin Sperl2a3fffd2015-09-10 09:32:14 +0000388 if (((size_t)tfr->rx_buf & (PAGE_SIZE - 1)) + tfr->len > PAGE_SIZE) {
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000389 dev_warn_once(&spi->dev,
Martin Sperl2a3fffd2015-09-10 09:32:14 +0000390 "Unaligned spi rx-transfer bridging page\n");
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000391 return false;
392 }
393
394 /* return OK */
395 return true;
396}
397
kbuild test robot29ad1a72015-05-12 19:43:59 +0800398static void bcm2835_dma_release(struct spi_master *master)
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000399{
400 if (master->dma_tx) {
401 dmaengine_terminate_all(master->dma_tx);
402 dma_release_channel(master->dma_tx);
403 master->dma_tx = NULL;
404 }
405 if (master->dma_rx) {
406 dmaengine_terminate_all(master->dma_rx);
407 dma_release_channel(master->dma_rx);
408 master->dma_rx = NULL;
409 }
410}
411
kbuild test robot29ad1a72015-05-12 19:43:59 +0800412static void bcm2835_dma_init(struct spi_master *master, struct device *dev)
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000413{
414 struct dma_slave_config slave_config;
415 const __be32 *addr;
416 dma_addr_t dma_reg_base;
417 int ret;
418
419 /* base address in dma-space */
420 addr = of_get_address(master->dev.of_node, 0, NULL, NULL);
421 if (!addr) {
422 dev_err(dev, "could not get DMA-register address - not using dma mode\n");
423 goto err;
424 }
425 dma_reg_base = be32_to_cpup(addr);
426
427 /* get tx/rx dma */
428 master->dma_tx = dma_request_slave_channel(dev, "tx");
429 if (!master->dma_tx) {
430 dev_err(dev, "no tx-dma configuration found - not using dma mode\n");
431 goto err;
432 }
433 master->dma_rx = dma_request_slave_channel(dev, "rx");
434 if (!master->dma_rx) {
435 dev_err(dev, "no rx-dma configuration found - not using dma mode\n");
436 goto err_release;
437 }
438
439 /* configure DMAs */
440 slave_config.direction = DMA_MEM_TO_DEV;
441 slave_config.dst_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
442 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
443
444 ret = dmaengine_slave_config(master->dma_tx, &slave_config);
445 if (ret)
446 goto err_config;
447
448 slave_config.direction = DMA_DEV_TO_MEM;
449 slave_config.src_addr = (u32)(dma_reg_base + BCM2835_SPI_FIFO);
450 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
451
452 ret = dmaengine_slave_config(master->dma_rx, &slave_config);
453 if (ret)
454 goto err_config;
455
456 /* all went well, so set can_dma */
457 master->can_dma = bcm2835_spi_can_dma;
458 master->max_dma_len = 65535; /* limitation by BCM2835_SPI_DLEN */
459 /* need to do TX AND RX DMA, so we need dummy buffers */
460 master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
461
462 return;
463
464err_config:
465 dev_err(dev, "issue configuring dma: %d - not using DMA mode\n",
466 ret);
467err_release:
468 bcm2835_dma_release(master);
469err:
470 return;
471}
472
Martin Sperla750b122015-04-22 07:33:03 +0000473static int bcm2835_spi_transfer_one_poll(struct spi_master *master,
474 struct spi_device *spi,
475 struct spi_transfer *tfr,
476 u32 cs,
Martin Sperl0122a512015-07-29 07:34:10 +0000477 unsigned long long xfer_time_us)
Martin Sperla750b122015-04-22 07:33:03 +0000478{
479 struct bcm2835_spi *bs = spi_master_get_devdata(master);
480 unsigned long timeout;
481
482 /* enable HW block without interrupts */
483 bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA);
484
485 /* fill in the fifo before timeout calculations
486 * if we are interrupted here, then the data is
487 * getting transferred by the HW while we are interrupted
488 */
489 bcm2835_wr_fifo(bs);
490
491 /* set the timeout */
492 timeout = jiffies + BCM2835_SPI_POLLING_JIFFIES;
493
494 /* loop until finished the transfer */
495 while (bs->rx_len) {
496 /* fill in tx fifo with remaining data */
497 bcm2835_wr_fifo(bs);
498
499 /* read from fifo as much as possible */
500 bcm2835_rd_fifo(bs);
501
502 /* if there is still data pending to read
503 * then check the timeout
504 */
505 if (bs->rx_len && time_after(jiffies, timeout)) {
506 dev_dbg_ratelimited(&spi->dev,
507 "timeout period reached: jiffies: %lu remaining tx/rx: %d/%d - falling back to interrupt mode\n",
508 jiffies - timeout,
509 bs->tx_len, bs->rx_len);
510 /* fall back to interrupt mode */
511 return bcm2835_spi_transfer_one_irq(master, spi,
512 tfr, cs);
513 }
514 }
515
516 /* Transfer complete - reset SPI HW */
517 bcm2835_spi_reset_hw(master);
518 /* and return without waiting for completion */
519 return 0;
520}
521
Martin Sperl704f32d2015-04-06 17:16:30 +0000522static int bcm2835_spi_transfer_one(struct spi_master *master,
523 struct spi_device *spi,
524 struct spi_transfer *tfr)
525{
526 struct bcm2835_spi *bs = spi_master_get_devdata(master);
527 unsigned long spi_hz, clk_hz, cdiv;
Martin Sperl0122a512015-07-29 07:34:10 +0000528 unsigned long spi_used_hz;
529 unsigned long long xfer_time_us;
Martin Sperl704f32d2015-04-06 17:16:30 +0000530 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
531
532 /* set clock */
533 spi_hz = tfr->speed_hz;
534 clk_hz = clk_get_rate(bs->clk);
535
536 if (spi_hz >= clk_hz / 2) {
537 cdiv = 2; /* clk_hz/2 is the fastest we can go */
538 } else if (spi_hz) {
539 /* CDIV must be a multiple of two */
540 cdiv = DIV_ROUND_UP(clk_hz, spi_hz);
541 cdiv += (cdiv % 2);
542
543 if (cdiv >= 65536)
544 cdiv = 0; /* 0 is the slowest we can go */
545 } else {
546 cdiv = 0; /* 0 is the slowest we can go */
547 }
548 spi_used_hz = cdiv ? (clk_hz / cdiv) : (clk_hz / 65536);
549 bcm2835_wr(bs, BCM2835_SPI_CLK, cdiv);
550
Martin Sperlacace732015-07-28 14:03:12 +0000551 /* handle all the 3-wire mode */
Martin Sperl704f32d2015-04-06 17:16:30 +0000552 if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf))
553 cs |= BCM2835_SPI_CS_REN;
Martin Sperlacace732015-07-28 14:03:12 +0000554 else
555 cs &= ~BCM2835_SPI_CS_REN;
Martin Sperl704f32d2015-04-06 17:16:30 +0000556
Lukas Wunner5c09e422018-11-08 08:06:10 +0100557 /*
558 * The driver always uses software-controlled GPIO Chip Select.
559 * Set the hardware-controlled native Chip Select to an invalid
560 * value to prevent it from interfering.
Martin Sperl704f32d2015-04-06 17:16:30 +0000561 */
Lukas Wunner5c09e422018-11-08 08:06:10 +0100562 cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01;
Martin Sperl704f32d2015-04-06 17:16:30 +0000563
564 /* set transmit buffers and length */
565 bs->tx_buf = tfr->tx_buf;
566 bs->rx_buf = tfr->rx_buf;
567 bs->tx_len = tfr->len;
568 bs->rx_len = tfr->len;
569
570 /* calculate the estimated time in us the transfer runs */
Martin Sperl0122a512015-07-29 07:34:10 +0000571 xfer_time_us = (unsigned long long)tfr->len
Martin Sperl704f32d2015-04-06 17:16:30 +0000572 * 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */
Martin Sperl0122a512015-07-29 07:34:10 +0000573 * 1000000;
574 do_div(xfer_time_us, spi_used_hz);
Martin Sperl704f32d2015-04-06 17:16:30 +0000575
576 /* for short requests run polling*/
577 if (xfer_time_us <= BCM2835_SPI_POLLING_LIMIT_US)
578 return bcm2835_spi_transfer_one_poll(master, spi, tfr,
579 cs, xfer_time_us);
580
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000581 /* run in dma mode if conditions are right */
582 if (master->can_dma && bcm2835_spi_can_dma(master, spi, tfr))
583 return bcm2835_spi_transfer_one_dma(master, spi, tfr, cs);
584
585 /* run in interrupt-mode */
Martin Sperl704f32d2015-04-06 17:16:30 +0000586 return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs);
587}
588
Martin Sperlacace732015-07-28 14:03:12 +0000589static int bcm2835_spi_prepare_message(struct spi_master *master,
590 struct spi_message *msg)
591{
592 struct spi_device *spi = msg->spi;
593 struct bcm2835_spi *bs = spi_master_get_devdata(master);
594 u32 cs = bcm2835_rd(bs, BCM2835_SPI_CS);
595
596 cs &= ~(BCM2835_SPI_CS_CPOL | BCM2835_SPI_CS_CPHA);
597
598 if (spi->mode & SPI_CPOL)
599 cs |= BCM2835_SPI_CS_CPOL;
600 if (spi->mode & SPI_CPHA)
601 cs |= BCM2835_SPI_CS_CPHA;
602
603 bcm2835_wr(bs, BCM2835_SPI_CS, cs);
604
605 return 0;
606}
607
Martin Sperle34ff012015-03-26 11:08:36 +0100608static void bcm2835_spi_handle_err(struct spi_master *master,
609 struct spi_message *msg)
Chris Bootf8043872013-03-11 21:38:24 -0600610{
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000611 struct bcm2835_spi *bs = spi_master_get_devdata(master);
612
613 /* if an error occurred and we have an active dma, then terminate */
Lukas Wunnere82b0b32018-11-08 08:06:10 +0100614 if (cmpxchg(&bs->dma_pending, true, false)) {
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000615 dmaengine_terminate_all(master->dma_tx);
616 dmaengine_terminate_all(master->dma_rx);
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000617 }
618 /* and reset */
Martin Sperle34ff012015-03-26 11:08:36 +0100619 bcm2835_spi_reset_hw(master);
Chris Bootf8043872013-03-11 21:38:24 -0600620}
621
Martin Sperla30a5552015-04-06 17:16:31 +0000622static int chip_match_name(struct gpio_chip *chip, void *data)
623{
624 return !strcmp(chip->label, data);
625}
626
Martin Sperle34ff012015-03-26 11:08:36 +0100627static int bcm2835_spi_setup(struct spi_device *spi)
628{
Martin Sperla30a5552015-04-06 17:16:31 +0000629 int err;
630 struct gpio_chip *chip;
Martin Sperle34ff012015-03-26 11:08:36 +0100631 /*
632 * sanity checking the native-chipselects
633 */
634 if (spi->mode & SPI_NO_CS)
635 return 0;
636 if (gpio_is_valid(spi->cs_gpio))
637 return 0;
Martin Sperla30a5552015-04-06 17:16:31 +0000638 if (spi->chip_select > 1) {
639 /* error in the case of native CS requested with CS > 1
640 * officially there is a CS2, but it is not documented
641 * which GPIO is connected with that...
642 */
643 dev_err(&spi->dev,
644 "setup: only two native chip-selects are supported\n");
645 return -EINVAL;
646 }
647 /* now translate native cs to GPIO */
648
649 /* get the gpio chip for the base */
650 chip = gpiochip_find("pinctrl-bcm2835", chip_match_name);
651 if (!chip)
Martin Sperle34ff012015-03-26 11:08:36 +0100652 return 0;
653
Martin Sperla30a5552015-04-06 17:16:31 +0000654 /* and calculate the real CS */
655 spi->cs_gpio = chip->base + 8 - spi->chip_select;
656
657 /* and set up the "mode" and level */
658 dev_info(&spi->dev, "setting up native-CS%i as GPIO %i\n",
659 spi->chip_select, spi->cs_gpio);
660
661 /* set up GPIO as output and pull to the correct level */
662 err = gpio_direction_output(spi->cs_gpio,
663 (spi->mode & SPI_CS_HIGH) ? 0 : 1);
664 if (err) {
665 dev_err(&spi->dev,
666 "could not set CS%i gpio %i as output: %i",
667 spi->chip_select, spi->cs_gpio, err);
668 return err;
669 }
Martin Sperla30a5552015-04-06 17:16:31 +0000670
671 return 0;
Chris Bootf8043872013-03-11 21:38:24 -0600672}
673
674static int bcm2835_spi_probe(struct platform_device *pdev)
675{
676 struct spi_master *master;
677 struct bcm2835_spi *bs;
678 struct resource *res;
679 int err;
680
681 master = spi_alloc_master(&pdev->dev, sizeof(*bs));
682 if (!master) {
683 dev_err(&pdev->dev, "spi_alloc_master() failed\n");
684 return -ENOMEM;
685 }
686
687 platform_set_drvdata(pdev, master);
688
689 master->mode_bits = BCM2835_SPI_MODE_BITS;
Axel Linc2b6a3a2013-08-05 08:43:02 +0800690 master->bits_per_word_mask = SPI_BPW_MASK(8);
Chris Bootf8043872013-03-11 21:38:24 -0600691 master->num_chipselect = 3;
Martin Sperle34ff012015-03-26 11:08:36 +0100692 master->setup = bcm2835_spi_setup;
Martin Sperle34ff012015-03-26 11:08:36 +0100693 master->transfer_one = bcm2835_spi_transfer_one;
694 master->handle_err = bcm2835_spi_handle_err;
Martin Sperlacace732015-07-28 14:03:12 +0000695 master->prepare_message = bcm2835_spi_prepare_message;
Chris Bootf8043872013-03-11 21:38:24 -0600696 master->dev.of_node = pdev->dev.of_node;
697
698 bs = spi_master_get_devdata(master);
699
Chris Bootf8043872013-03-11 21:38:24 -0600700 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Laurent Navet2d6e75e2013-05-02 14:13:30 +0200701 bs->regs = devm_ioremap_resource(&pdev->dev, res);
702 if (IS_ERR(bs->regs)) {
703 err = PTR_ERR(bs->regs);
Chris Bootf8043872013-03-11 21:38:24 -0600704 goto out_master_put;
705 }
706
707 bs->clk = devm_clk_get(&pdev->dev, NULL);
708 if (IS_ERR(bs->clk)) {
709 err = PTR_ERR(bs->clk);
710 dev_err(&pdev->dev, "could not get clk: %d\n", err);
711 goto out_master_put;
712 }
713
Martin Sperlddf0e1c2015-10-15 10:09:11 +0000714 bs->irq = platform_get_irq(pdev, 0);
Chris Bootf8043872013-03-11 21:38:24 -0600715 if (bs->irq <= 0) {
716 dev_err(&pdev->dev, "could not get IRQ: %d\n", bs->irq);
717 err = bs->irq ? bs->irq : -ENODEV;
718 goto out_master_put;
719 }
720
721 clk_prepare_enable(bs->clk);
722
Martin Sperlddf0e1c2015-10-15 10:09:11 +0000723 bcm2835_dma_init(master, &pdev->dev);
724
725 /* initialise the hardware with the default polarities */
726 bcm2835_wr(bs, BCM2835_SPI_CS,
727 BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
728
Jingoo Han08bc0542013-12-09 19:25:00 +0900729 err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0,
Martin Sperl342f9482015-03-20 15:26:11 +0100730 dev_name(&pdev->dev), master);
Chris Bootf8043872013-03-11 21:38:24 -0600731 if (err) {
732 dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
733 goto out_clk_disable;
734 }
735
Jingoo Han247263d2013-09-24 13:23:00 +0900736 err = devm_spi_register_master(&pdev->dev, master);
Chris Bootf8043872013-03-11 21:38:24 -0600737 if (err) {
738 dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
Jingoo Han08bc0542013-12-09 19:25:00 +0900739 goto out_clk_disable;
Chris Bootf8043872013-03-11 21:38:24 -0600740 }
741
742 return 0;
743
Chris Bootf8043872013-03-11 21:38:24 -0600744out_clk_disable:
745 clk_disable_unprepare(bs->clk);
746out_master_put:
747 spi_master_put(master);
748 return err;
749}
750
751static int bcm2835_spi_remove(struct platform_device *pdev)
752{
Wei Yongjune0b35b82013-11-15 15:43:27 +0800753 struct spi_master *master = platform_get_drvdata(pdev);
Chris Bootf8043872013-03-11 21:38:24 -0600754 struct bcm2835_spi *bs = spi_master_get_devdata(master);
755
Chris Bootf8043872013-03-11 21:38:24 -0600756 /* Clear FIFOs, and disable the HW block */
757 bcm2835_wr(bs, BCM2835_SPI_CS,
758 BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);
759
760 clk_disable_unprepare(bs->clk);
Chris Bootf8043872013-03-11 21:38:24 -0600761
Martin Sperl3ecd37e2015-05-10 20:47:28 +0000762 bcm2835_dma_release(master);
763
Chris Bootf8043872013-03-11 21:38:24 -0600764 return 0;
765}
766
767static const struct of_device_id bcm2835_spi_match[] = {
768 { .compatible = "brcm,bcm2835-spi", },
769 {}
770};
771MODULE_DEVICE_TABLE(of, bcm2835_spi_match);
772
773static struct platform_driver bcm2835_spi_driver = {
774 .driver = {
775 .name = DRV_NAME,
Chris Bootf8043872013-03-11 21:38:24 -0600776 .of_match_table = bcm2835_spi_match,
777 },
778 .probe = bcm2835_spi_probe,
779 .remove = bcm2835_spi_remove,
780};
781module_platform_driver(bcm2835_spi_driver);
782
783MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
784MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
Stefan Wahren22bf6cd2018-10-23 13:06:08 +0200785MODULE_LICENSE("GPL");