2 * Driver for Broadcom BCM2835 SPI Controllers
4 * Copyright (C) 2012 Chris Boot
5 * Copyright (C) 2013 Stephen Warren
6 * Copyright (C) 2015 Martin Sperl
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
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.
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.
23 #include <linux/clk.h>
24 #include <linux/completion.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
32 #include <linux/of_irq.h>
33 #include <linux/of_gpio.h>
34 #include <linux/of_device.h>
35 #include <linux/spi/spi.h>
37 /* SPI register offsets */
38 #define BCM2835_SPI_CS 0x00
39 #define BCM2835_SPI_FIFO 0x04
40 #define BCM2835_SPI_CLK 0x08
41 #define BCM2835_SPI_DLEN 0x0c
42 #define BCM2835_SPI_LTOH 0x10
43 #define BCM2835_SPI_DC 0x14
46 #define BCM2835_SPI_CS_LEN_LONG 0x02000000
47 #define BCM2835_SPI_CS_DMA_LEN 0x01000000
48 #define BCM2835_SPI_CS_CSPOL2 0x00800000
49 #define BCM2835_SPI_CS_CSPOL1 0x00400000
50 #define BCM2835_SPI_CS_CSPOL0 0x00200000
51 #define BCM2835_SPI_CS_RXF 0x00100000
52 #define BCM2835_SPI_CS_RXR 0x00080000
53 #define BCM2835_SPI_CS_TXD 0x00040000
54 #define BCM2835_SPI_CS_RXD 0x00020000
55 #define BCM2835_SPI_CS_DONE 0x00010000
56 #define BCM2835_SPI_CS_LEN 0x00002000
57 #define BCM2835_SPI_CS_REN 0x00001000
58 #define BCM2835_SPI_CS_ADCS 0x00000800
59 #define BCM2835_SPI_CS_INTR 0x00000400
60 #define BCM2835_SPI_CS_INTD 0x00000200
61 #define BCM2835_SPI_CS_DMAEN 0x00000100
62 #define BCM2835_SPI_CS_TA 0x00000080
63 #define BCM2835_SPI_CS_CSPOL 0x00000040
64 #define BCM2835_SPI_CS_CLEAR_RX 0x00000020
65 #define BCM2835_SPI_CS_CLEAR_TX 0x00000010
66 #define BCM2835_SPI_CS_CPOL 0x00000008
67 #define BCM2835_SPI_CS_CPHA 0x00000004
68 #define BCM2835_SPI_CS_CS_10 0x00000002
69 #define BCM2835_SPI_CS_CS_01 0x00000001
71 #define BCM2835_SPI_TIMEOUT_MS 30000
72 #define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
73 | SPI_NO_CS | SPI_3WIRE)
75 #define DRV_NAME "spi-bcm2835"
87 static inline u32
bcm2835_rd(struct bcm2835_spi
*bs
, unsigned reg
)
89 return readl(bs
->regs
+ reg
);
92 static inline void bcm2835_wr(struct bcm2835_spi
*bs
, unsigned reg
, u32 val
)
94 writel(val
, bs
->regs
+ reg
);
97 static inline void bcm2835_rd_fifo(struct bcm2835_spi
*bs
)
101 while ((bs
->rx_len
) &&
102 (bcm2835_rd(bs
, BCM2835_SPI_CS
) & BCM2835_SPI_CS_RXD
)) {
103 byte
= bcm2835_rd(bs
, BCM2835_SPI_FIFO
);
105 *bs
->rx_buf
++ = byte
;
110 static inline void bcm2835_wr_fifo(struct bcm2835_spi
*bs
)
114 while ((bs
->tx_len
) &&
115 (bcm2835_rd(bs
, BCM2835_SPI_CS
) & BCM2835_SPI_CS_TXD
)) {
116 byte
= bs
->tx_buf
? *bs
->tx_buf
++ : 0;
117 bcm2835_wr(bs
, BCM2835_SPI_FIFO
, byte
);
122 static void bcm2835_spi_reset_hw(struct spi_master
*master
)
124 struct bcm2835_spi
*bs
= spi_master_get_devdata(master
);
125 u32 cs
= bcm2835_rd(bs
, BCM2835_SPI_CS
);
127 /* Disable SPI interrupts and transfer */
128 cs
&= ~(BCM2835_SPI_CS_INTR
|
129 BCM2835_SPI_CS_INTD
|
131 /* and reset RX/TX FIFOS */
132 cs
|= BCM2835_SPI_CS_CLEAR_RX
| BCM2835_SPI_CS_CLEAR_TX
;
134 /* and reset the SPI_HW */
135 bcm2835_wr(bs
, BCM2835_SPI_CS
, cs
);
138 static irqreturn_t
bcm2835_spi_interrupt(int irq
, void *dev_id
)
140 struct spi_master
*master
= dev_id
;
141 struct bcm2835_spi
*bs
= spi_master_get_devdata(master
);
143 /* Read as many bytes as possible from FIFO */
145 /* Write as many bytes as possible to FIFO */
148 /* based on flags decide if we can finish the transfer */
149 if (bcm2835_rd(bs
, BCM2835_SPI_CS
) & BCM2835_SPI_CS_DONE
) {
150 /* Transfer complete - reset SPI HW */
151 bcm2835_spi_reset_hw(master
);
152 /* wake up the framework */
153 complete(&master
->xfer_completion
);
159 static int bcm2835_spi_transfer_one(struct spi_master
*master
,
160 struct spi_device
*spi
,
161 struct spi_transfer
*tfr
)
163 struct bcm2835_spi
*bs
= spi_master_get_devdata(master
);
164 unsigned long spi_hz
, clk_hz
, cdiv
;
165 u32 cs
= bcm2835_rd(bs
, BCM2835_SPI_CS
);
168 spi_hz
= tfr
->speed_hz
;
169 clk_hz
= clk_get_rate(bs
->clk
);
171 if (spi_hz
>= clk_hz
/ 2) {
172 cdiv
= 2; /* clk_hz/2 is the fastest we can go */
174 /* CDIV must be a multiple of two */
175 cdiv
= DIV_ROUND_UP(clk_hz
, spi_hz
);
179 cdiv
= 0; /* 0 is the slowest we can go */
181 cdiv
= 0; /* 0 is the slowest we can go */
183 bcm2835_wr(bs
, BCM2835_SPI_CLK
, cdiv
);
185 /* handle all the modes */
186 if ((spi
->mode
& SPI_3WIRE
) && (tfr
->rx_buf
))
187 cs
|= BCM2835_SPI_CS_REN
;
188 if (spi
->mode
& SPI_CPOL
)
189 cs
|= BCM2835_SPI_CS_CPOL
;
190 if (spi
->mode
& SPI_CPHA
)
191 cs
|= BCM2835_SPI_CS_CPHA
;
193 /* for gpio_cs set dummy CS so that no HW-CS get changed
194 * we can not run this in bcm2835_spi_set_cs, as it does
195 * not get called for cs_gpio cases, so we need to do it here
197 if (gpio_is_valid(spi
->cs_gpio
) || (spi
->mode
& SPI_NO_CS
))
198 cs
|= BCM2835_SPI_CS_CS_10
| BCM2835_SPI_CS_CS_01
;
200 /* set transmit buffers and length */
201 bs
->tx_buf
= tfr
->tx_buf
;
202 bs
->rx_buf
= tfr
->rx_buf
;
203 bs
->tx_len
= tfr
->len
;
204 bs
->rx_len
= tfr
->len
;
207 * Enable the HW block. This will immediately trigger a DONE (TX
208 * empty) interrupt, upon which we will fill the TX FIFO with the
209 * first TX bytes. Pre-filling the TX FIFO here to avoid the
210 * interrupt doesn't work:-(
212 cs
|= BCM2835_SPI_CS_INTR
| BCM2835_SPI_CS_INTD
| BCM2835_SPI_CS_TA
;
213 bcm2835_wr(bs
, BCM2835_SPI_CS
, cs
);
215 /* signal that we need to wait for completion */
219 static void bcm2835_spi_handle_err(struct spi_master
*master
,
220 struct spi_message
*msg
)
222 bcm2835_spi_reset_hw(master
);
225 static void bcm2835_spi_set_cs(struct spi_device
*spi
, bool gpio_level
)
228 * we can assume that we are "native" as per spi_set_cs
229 * calling us ONLY when cs_gpio is not set
230 * we can also assume that we are CS < 3 as per bcm2835_spi_setup
231 * we would not get called because of error handling there.
232 * the level passed is the electrical level not enabled/disabled
233 * so it has to get translated back to enable/disable
234 * see spi_set_cs in spi.c for the implementation
237 struct spi_master
*master
= spi
->master
;
238 struct bcm2835_spi
*bs
= spi_master_get_devdata(master
);
239 u32 cs
= bcm2835_rd(bs
, BCM2835_SPI_CS
);
242 /* calculate the enable flag from the passed gpio_level */
243 enable
= (spi
->mode
& SPI_CS_HIGH
) ? gpio_level
: !gpio_level
;
245 /* set flags for "reverse" polarity in the registers */
246 if (spi
->mode
& SPI_CS_HIGH
) {
247 /* set the correct CS-bits */
248 cs
|= BCM2835_SPI_CS_CSPOL
;
249 cs
|= BCM2835_SPI_CS_CSPOL0
<< spi
->chip_select
;
251 /* clean the CS-bits */
252 cs
&= ~BCM2835_SPI_CS_CSPOL
;
253 cs
&= ~(BCM2835_SPI_CS_CSPOL0
<< spi
->chip_select
);
256 /* select the correct chip_select depending on disabled/enabled */
258 /* set cs correctly */
259 if (spi
->mode
& SPI_NO_CS
) {
260 /* use the "undefined" chip-select */
261 cs
|= BCM2835_SPI_CS_CS_10
| BCM2835_SPI_CS_CS_01
;
263 /* set the chip select */
264 cs
&= ~(BCM2835_SPI_CS_CS_10
| BCM2835_SPI_CS_CS_01
);
265 cs
|= spi
->chip_select
;
268 /* disable CSPOL which puts HW-CS into deselected state */
269 cs
&= ~BCM2835_SPI_CS_CSPOL
;
270 /* use the "undefined" chip-select as precaution */
271 cs
|= BCM2835_SPI_CS_CS_10
| BCM2835_SPI_CS_CS_01
;
274 /* finally set the calculated flags in SPI_CS */
275 bcm2835_wr(bs
, BCM2835_SPI_CS
, cs
);
278 static int bcm2835_spi_setup(struct spi_device
*spi
)
281 * sanity checking the native-chipselects
283 if (spi
->mode
& SPI_NO_CS
)
285 if (gpio_is_valid(spi
->cs_gpio
))
287 if (spi
->chip_select
< 3)
290 /* error in the case of native CS requested with CS-id > 2 */
292 "setup: only three native chip-selects are supported\n"
297 static int bcm2835_spi_probe(struct platform_device
*pdev
)
299 struct spi_master
*master
;
300 struct bcm2835_spi
*bs
;
301 struct resource
*res
;
304 master
= spi_alloc_master(&pdev
->dev
, sizeof(*bs
));
306 dev_err(&pdev
->dev
, "spi_alloc_master() failed\n");
310 platform_set_drvdata(pdev
, master
);
312 master
->mode_bits
= BCM2835_SPI_MODE_BITS
;
313 master
->bits_per_word_mask
= SPI_BPW_MASK(8);
314 master
->num_chipselect
= 3;
315 master
->setup
= bcm2835_spi_setup
;
316 master
->set_cs
= bcm2835_spi_set_cs
;
317 master
->transfer_one
= bcm2835_spi_transfer_one
;
318 master
->handle_err
= bcm2835_spi_handle_err
;
319 master
->dev
.of_node
= pdev
->dev
.of_node
;
321 bs
= spi_master_get_devdata(master
);
323 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
324 bs
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
325 if (IS_ERR(bs
->regs
)) {
326 err
= PTR_ERR(bs
->regs
);
330 bs
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
331 if (IS_ERR(bs
->clk
)) {
332 err
= PTR_ERR(bs
->clk
);
333 dev_err(&pdev
->dev
, "could not get clk: %d\n", err
);
337 bs
->irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
339 dev_err(&pdev
->dev
, "could not get IRQ: %d\n", bs
->irq
);
340 err
= bs
->irq
? bs
->irq
: -ENODEV
;
344 clk_prepare_enable(bs
->clk
);
346 err
= devm_request_irq(&pdev
->dev
, bs
->irq
, bcm2835_spi_interrupt
, 0,
347 dev_name(&pdev
->dev
), master
);
349 dev_err(&pdev
->dev
, "could not request IRQ: %d\n", err
);
350 goto out_clk_disable
;
353 /* initialise the hardware with the default polarities */
354 bcm2835_wr(bs
, BCM2835_SPI_CS
,
355 BCM2835_SPI_CS_CLEAR_RX
| BCM2835_SPI_CS_CLEAR_TX
);
357 err
= devm_spi_register_master(&pdev
->dev
, master
);
359 dev_err(&pdev
->dev
, "could not register SPI master: %d\n", err
);
360 goto out_clk_disable
;
366 clk_disable_unprepare(bs
->clk
);
368 spi_master_put(master
);
372 static int bcm2835_spi_remove(struct platform_device
*pdev
)
374 struct spi_master
*master
= platform_get_drvdata(pdev
);
375 struct bcm2835_spi
*bs
= spi_master_get_devdata(master
);
377 /* Clear FIFOs, and disable the HW block */
378 bcm2835_wr(bs
, BCM2835_SPI_CS
,
379 BCM2835_SPI_CS_CLEAR_RX
| BCM2835_SPI_CS_CLEAR_TX
);
381 clk_disable_unprepare(bs
->clk
);
386 static const struct of_device_id bcm2835_spi_match
[] = {
387 { .compatible
= "brcm,bcm2835-spi", },
390 MODULE_DEVICE_TABLE(of
, bcm2835_spi_match
);
392 static struct platform_driver bcm2835_spi_driver
= {
395 .of_match_table
= bcm2835_spi_match
,
397 .probe
= bcm2835_spi_probe
,
398 .remove
= bcm2835_spi_remove
,
400 module_platform_driver(bcm2835_spi_driver
);
402 MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835");
403 MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
404 MODULE_LICENSE("GPL v2");