Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Broadcom BCM2835 SPI Controllers |
| 3 | * |
| 4 | * Copyright (C) 2012 Chris Boot |
| 5 | * Copyright (C) 2013 Stephen Warren |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 6 | * Copyright (C) 2015 Martin Sperl |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 7 | * |
| 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 Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <linux/clk.h> |
| 24 | #include <linux/completion.h> |
| 25 | #include <linux/delay.h> |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/dmaengine.h> |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 28 | #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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 34 | #include <linux/of_address.h> |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 35 | #include <linux/of_device.h> |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 36 | #include <linux/of_gpio.h> |
| 37 | #include <linux/of_irq.h> |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 38 | #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 Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 74 | #define BCM2835_SPI_POLLING_LIMIT_US 30 |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 75 | #define BCM2835_SPI_POLLING_JIFFIES 2 |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 76 | #define BCM2835_SPI_DMA_MIN_LENGTH 96 |
Martin Sperl | 6935224 | 2015-03-19 09:01:53 +0000 | [diff] [blame] | 77 | #define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \ |
| 78 | | SPI_NO_CS | SPI_3WIRE) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 79 | |
| 80 | #define DRV_NAME "spi-bcm2835" |
| 81 | |
Lukas Wunner | acf0f85 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 82 | /** |
| 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 87 | * @tfr: SPI transfer currently processed |
Lukas Wunner | acf0f85 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 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 |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 92 | * @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 Wunner | acf0f85 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 97 | * @dma_pending: whether a DMA transfer is in progress |
| 98 | */ |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 99 | struct bcm2835_spi { |
| 100 | void __iomem *regs; |
| 101 | struct clk *clk; |
| 102 | int irq; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 103 | struct spi_transfer *tfr; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 104 | const u8 *tx_buf; |
| 105 | u8 *rx_buf; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 106 | int tx_len; |
| 107 | int rx_len; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 108 | int tx_prologue; |
| 109 | int rx_prologue; |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 110 | unsigned int tx_spillover; |
Lukas Wunner | 29bdedf | 2018-11-29 15:14:49 +0100 | [diff] [blame] | 111 | unsigned int dma_pending; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg) |
| 115 | { |
| 116 | return readl(bs->regs + reg); |
| 117 | } |
| 118 | |
| 119 | static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val) |
| 120 | { |
| 121 | writel(val, bs->regs + reg); |
| 122 | } |
| 123 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 124 | static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 125 | { |
| 126 | u8 byte; |
| 127 | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 128 | while ((bs->rx_len) && |
| 129 | (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) { |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 130 | byte = bcm2835_rd(bs, BCM2835_SPI_FIFO); |
| 131 | if (bs->rx_buf) |
| 132 | *bs->rx_buf++ = byte; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 133 | bs->rx_len--; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 137 | static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 138 | { |
| 139 | u8 byte; |
| 140 | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 141 | while ((bs->tx_len) && |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 142 | (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) { |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 143 | byte = bs->tx_buf ? *bs->tx_buf++ : 0; |
| 144 | bcm2835_wr(bs, BCM2835_SPI_FIFO, byte); |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 145 | bs->tx_len--; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 149 | /** |
| 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 157 | * 32-bit instead of just 8-bit). Moreover @bs->rx_buf must not be %NULL. |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 158 | */ |
| 159 | static inline void bcm2835_rd_fifo_count(struct bcm2835_spi *bs, int count) |
| 160 | { |
| 161 | u32 val; |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 162 | int len; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 163 | |
| 164 | bs->rx_len -= count; |
| 165 | |
| 166 | while (count > 0) { |
| 167 | val = bcm2835_rd(bs, BCM2835_SPI_FIFO); |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 168 | len = min(count, 4); |
| 169 | memcpy(bs->rx_buf, &val, len); |
| 170 | bs->rx_buf += len; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 171 | 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 | */ |
| 185 | static inline void bcm2835_wr_fifo_count(struct bcm2835_spi *bs, int count) |
| 186 | { |
| 187 | u32 val; |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 188 | int len; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 189 | |
| 190 | bs->tx_len -= count; |
| 191 | |
| 192 | while (count > 0) { |
| 193 | if (bs->tx_buf) { |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 194 | len = min(count, 4); |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 195 | 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 208 | * |
| 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 212 | */ |
| 213 | static 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 Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 219 | static 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 227 | BCM2835_SPI_CS_DMAEN | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 228 | 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 234 | /* as well as DLEN */ |
| 235 | bcm2835_wr(bs, BCM2835_SPI_DLEN, 0); |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 238 | static 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 Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 242 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 243 | /* Read as many bytes as possible from FIFO */ |
| 244 | bcm2835_rd_fifo(bs); |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 245 | /* Write as many bytes as possible to FIFO */ |
| 246 | bcm2835_wr_fifo(bs); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 247 | |
Lukas Wunner | 56c1723 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 248 | if (!bs->rx_len) { |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 249 | /* Transfer complete - reset SPI HW */ |
| 250 | bcm2835_spi_reset_hw(master); |
| 251 | /* wake up the framework */ |
| 252 | complete(&master->xfer_completion); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 253 | } |
| 254 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 255 | return IRQ_HANDLED; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 256 | } |
| 257 | |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 258 | static 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 Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 264 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 265 | /* |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 266 | * Enable HW block, but with interrupts still disabled. |
| 267 | * Otherwise the empty TX FIFO would immediately trigger an interrupt. |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 268 | */ |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 269 | 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 Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 275 | cs |= BCM2835_SPI_CS_INTR | BCM2835_SPI_CS_INTD | BCM2835_SPI_CS_TA; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 276 | bcm2835_wr(bs, BCM2835_SPI_CS, cs); |
| 277 | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 278 | /* signal that we need to wait for completion */ |
| 279 | return 1; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 280 | } |
| 281 | |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 282 | /* |
| 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 291 | * 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 295 | |
| 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 | */ |
| 342 | static 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 385 | 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 388 | |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 389 | sg_dma_address(&tfr->rx_sg.sgl[0]) += bs->rx_prologue; |
| 390 | sg_dma_len(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 391 | } |
| 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 408 | sg_dma_address(&tfr->tx_sg.sgl[0]) += bs->tx_prologue; |
| 409 | sg_dma_len(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 410 | } else { |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 411 | 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 414 | } |
| 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 | */ |
| 425 | static 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 433 | sg_dma_address(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue; |
| 434 | sg_dma_len(&tfr->rx_sg.sgl[0]) += bs->rx_prologue; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (likely(!bs->tx_spillover)) { |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 438 | sg_dma_address(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue; |
| 439 | sg_dma_len(&tfr->tx_sg.sgl[0]) += bs->tx_prologue; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 440 | } else { |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame^] | 441 | 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 447 | static 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 Wunner | e82b0b3 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 460 | if (cmpxchg(&bs->dma_pending, true, false)) { |
| 461 | dmaengine_terminate_all(master->dma_tx); |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 462 | bcm2835_spi_undo_prologue(bs); |
Lukas Wunner | e82b0b3 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 463 | } |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 464 | |
| 465 | /* and mark as completed */; |
| 466 | complete(&master->xfer_completion); |
| 467 | } |
| 468 | |
| 469 | static 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 513 | static 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 521 | /* |
| 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 526 | |
| 527 | /* setup tx-DMA */ |
| 528 | ret = bcm2835_spi_prepare_sg(master, tfr, true); |
| 529 | if (ret) |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 530 | goto err_reset_hw; |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 531 | |
| 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 539 | bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->tx_len); |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 540 | |
| 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 Wunner | dbc9441 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 553 | bs->dma_pending = false; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 554 | goto err_reset_hw; |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 555 | } |
| 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 562 | |
| 563 | err_reset_hw: |
| 564 | bcm2835_spi_reset_hw(master); |
| 565 | bcm2835_spi_undo_prologue(bs); |
| 566 | return ret; |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static bool bcm2835_spi_can_dma(struct spi_master *master, |
| 570 | struct spi_device *spi, |
| 571 | struct spi_transfer *tfr) |
| 572 | { |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 573 | /* 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 590 | /* return OK */ |
| 591 | return true; |
| 592 | } |
| 593 | |
kbuild test robot | 29ad1a7 | 2015-05-12 19:43:59 +0800 | [diff] [blame] | 594 | static void bcm2835_dma_release(struct spi_master *master) |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 595 | { |
| 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 robot | 29ad1a7 | 2015-05-12 19:43:59 +0800 | [diff] [blame] | 608 | static void bcm2835_dma_init(struct spi_master *master, struct device *dev) |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 609 | { |
| 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 | |
| 660 | err_config: |
| 661 | dev_err(dev, "issue configuring dma: %d - not using DMA mode\n", |
| 662 | ret); |
| 663 | err_release: |
| 664 | bcm2835_dma_release(master); |
| 665 | err: |
| 666 | return; |
| 667 | } |
| 668 | |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 669 | static int bcm2835_spi_transfer_one_poll(struct spi_master *master, |
| 670 | struct spi_device *spi, |
| 671 | struct spi_transfer *tfr, |
| 672 | u32 cs, |
Martin Sperl | 0122a51 | 2015-07-29 07:34:10 +0000 | [diff] [blame] | 673 | unsigned long long xfer_time_us) |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 674 | { |
| 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 Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 718 | static 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 Sperl | 0122a51 | 2015-07-29 07:34:10 +0000 | [diff] [blame] | 724 | unsigned long spi_used_hz; |
| 725 | unsigned long long xfer_time_us; |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 726 | 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 Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 747 | /* handle all the 3-wire mode */ |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 748 | if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf)) |
| 749 | cs |= BCM2835_SPI_CS_REN; |
Martin Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 750 | else |
| 751 | cs &= ~BCM2835_SPI_CS_REN; |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 752 | |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 753 | /* |
| 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 Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 757 | */ |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 758 | cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01; |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 759 | |
| 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 Sperl | 0122a51 | 2015-07-29 07:34:10 +0000 | [diff] [blame] | 767 | xfer_time_us = (unsigned long long)tfr->len |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 768 | * 9 /* clocks/byte - SPI-HW waits 1 clock after each byte */ |
Martin Sperl | 0122a51 | 2015-07-29 07:34:10 +0000 | [diff] [blame] | 769 | * 1000000; |
| 770 | do_div(xfer_time_us, spi_used_hz); |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 771 | |
| 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 777 | /* 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 Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 782 | return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs); |
| 783 | } |
| 784 | |
Martin Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 785 | static 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 Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 804 | static void bcm2835_spi_handle_err(struct spi_master *master, |
| 805 | struct spi_message *msg) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 806 | { |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 807 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
| 808 | |
| 809 | /* if an error occurred and we have an active dma, then terminate */ |
Lukas Wunner | e82b0b3 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 810 | if (cmpxchg(&bs->dma_pending, true, false)) { |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 811 | dmaengine_terminate_all(master->dma_tx); |
| 812 | dmaengine_terminate_all(master->dma_rx); |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 813 | bcm2835_spi_undo_prologue(bs); |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 814 | } |
| 815 | /* and reset */ |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 816 | bcm2835_spi_reset_hw(master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 817 | } |
| 818 | |
Martin Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 819 | static int chip_match_name(struct gpio_chip *chip, void *data) |
| 820 | { |
| 821 | return !strcmp(chip->label, data); |
| 822 | } |
| 823 | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 824 | static int bcm2835_spi_setup(struct spi_device *spi) |
| 825 | { |
Martin Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 826 | int err; |
| 827 | struct gpio_chip *chip; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 828 | /* |
| 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 Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 835 | 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 Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 849 | return 0; |
| 850 | |
Martin Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 851 | /* 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 Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 867 | |
| 868 | return 0; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | static 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 Lin | c2b6a3a | 2013-08-05 08:43:02 +0800 | [diff] [blame] | 887 | master->bits_per_word_mask = SPI_BPW_MASK(8); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 888 | master->num_chipselect = 3; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 889 | master->setup = bcm2835_spi_setup; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 890 | master->transfer_one = bcm2835_spi_transfer_one; |
| 891 | master->handle_err = bcm2835_spi_handle_err; |
Martin Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 892 | master->prepare_message = bcm2835_spi_prepare_message; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 893 | master->dev.of_node = pdev->dev.of_node; |
| 894 | |
| 895 | bs = spi_master_get_devdata(master); |
| 896 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 897 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Laurent Navet | 2d6e75e | 2013-05-02 14:13:30 +0200 | [diff] [blame] | 898 | bs->regs = devm_ioremap_resource(&pdev->dev, res); |
| 899 | if (IS_ERR(bs->regs)) { |
| 900 | err = PTR_ERR(bs->regs); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 901 | 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 Sperl | ddf0e1c | 2015-10-15 10:09:11 +0000 | [diff] [blame] | 911 | bs->irq = platform_get_irq(pdev, 0); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 912 | 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 Sperl | ddf0e1c | 2015-10-15 10:09:11 +0000 | [diff] [blame] | 920 | 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 Han | 08bc054 | 2013-12-09 19:25:00 +0900 | [diff] [blame] | 926 | err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0, |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 927 | dev_name(&pdev->dev), master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 928 | if (err) { |
| 929 | dev_err(&pdev->dev, "could not request IRQ: %d\n", err); |
| 930 | goto out_clk_disable; |
| 931 | } |
| 932 | |
Jingoo Han | 247263d | 2013-09-24 13:23:00 +0900 | [diff] [blame] | 933 | err = devm_spi_register_master(&pdev->dev, master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 934 | if (err) { |
| 935 | dev_err(&pdev->dev, "could not register SPI master: %d\n", err); |
Jingoo Han | 08bc054 | 2013-12-09 19:25:00 +0900 | [diff] [blame] | 936 | goto out_clk_disable; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | return 0; |
| 940 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 941 | out_clk_disable: |
| 942 | clk_disable_unprepare(bs->clk); |
| 943 | out_master_put: |
| 944 | spi_master_put(master); |
| 945 | return err; |
| 946 | } |
| 947 | |
| 948 | static int bcm2835_spi_remove(struct platform_device *pdev) |
| 949 | { |
Wei Yongjun | e0b35b8 | 2013-11-15 15:43:27 +0800 | [diff] [blame] | 950 | struct spi_master *master = platform_get_drvdata(pdev); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 951 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
| 952 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 953 | /* 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 Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 958 | |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 959 | bcm2835_dma_release(master); |
| 960 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | static const struct of_device_id bcm2835_spi_match[] = { |
| 965 | { .compatible = "brcm,bcm2835-spi", }, |
| 966 | {} |
| 967 | }; |
| 968 | MODULE_DEVICE_TABLE(of, bcm2835_spi_match); |
| 969 | |
| 970 | static struct platform_driver bcm2835_spi_driver = { |
| 971 | .driver = { |
| 972 | .name = DRV_NAME, |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 973 | .of_match_table = bcm2835_spi_match, |
| 974 | }, |
| 975 | .probe = bcm2835_spi_probe, |
| 976 | .remove = bcm2835_spi_remove, |
| 977 | }; |
| 978 | module_platform_driver(bcm2835_spi_driver); |
| 979 | |
| 980 | MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835"); |
| 981 | MODULE_AUTHOR("Chris Boot <bootc@bootc.net>"); |
Stefan Wahren | 22bf6cd | 2018-10-23 13:06:08 +0200 | [diff] [blame] | 982 | MODULE_LICENSE("GPL"); |