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> |
Martin Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 25 | #include <linux/debugfs.h> |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 26 | #include <linux/delay.h> |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/dmaengine.h> |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 29 | #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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 35 | #include <linux/of_address.h> |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 36 | #include <linux/of_device.h> |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 37 | #include <linux/of_gpio.h> |
| 38 | #include <linux/of_irq.h> |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 39 | #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 Wunner | 2e0733b | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 75 | #define BCM2835_SPI_FIFO_SIZE 64 |
| 76 | #define BCM2835_SPI_FIFO_SIZE_3_4 48 |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 77 | #define BCM2835_SPI_DMA_MIN_LENGTH 96 |
Martin Sperl | 6935224 | 2015-03-19 09:01:53 +0000 | [diff] [blame] | 78 | #define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \ |
| 79 | | SPI_NO_CS | SPI_3WIRE) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 80 | |
| 81 | #define DRV_NAME "spi-bcm2835" |
| 82 | |
Martin Sperl | ff245d9 | 2019-04-23 20:15:11 +0000 | [diff] [blame] | 83 | /* define polling limits */ |
| 84 | unsigned int polling_limit_us = 30; |
| 85 | module_param(polling_limit_us, uint, 0664); |
| 86 | MODULE_PARM_DESC(polling_limit_us, |
| 87 | "time in us to run a transfer in polling mode\n"); |
| 88 | |
Lukas Wunner | acf0f85 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 89 | /** |
| 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 94 | * @tfr: SPI transfer currently processed |
Lukas Wunner | acf0f85 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 95 | * @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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 99 | * @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 Wunner | acf0f85 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 104 | * @dma_pending: whether a DMA transfer is in progress |
Martin Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 105 | * @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 Wunner | acf0f85 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 114 | */ |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 115 | struct bcm2835_spi { |
| 116 | void __iomem *regs; |
| 117 | struct clk *clk; |
| 118 | int irq; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 119 | struct spi_transfer *tfr; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 120 | const u8 *tx_buf; |
| 121 | u8 *rx_buf; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 122 | int tx_len; |
| 123 | int rx_len; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 124 | int tx_prologue; |
| 125 | int rx_prologue; |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 126 | unsigned int tx_spillover; |
Lukas Wunner | 29bdedf | 2018-11-29 15:14:49 +0100 | [diff] [blame] | 127 | unsigned int dma_pending; |
Martin Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 128 | |
| 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 Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 134 | }; |
| 135 | |
Martin Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 136 | #if defined(CONFIG_DEBUG_FS) |
| 137 | static 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 | |
| 161 | static void bcm2835_debugfs_remove(struct bcm2835_spi *bs) |
| 162 | { |
| 163 | debugfs_remove_recursive(bs->debugfs_dir); |
| 164 | bs->debugfs_dir = NULL; |
| 165 | } |
| 166 | #else |
| 167 | static void bcm2835_debugfs_create(struct bcm2835_spi *bs, |
| 168 | const char *dname) |
| 169 | { |
| 170 | } |
| 171 | |
| 172 | static void bcm2835_debugfs_remove(struct bcm2835_spi *bs) |
| 173 | { |
| 174 | } |
| 175 | #endif /* CONFIG_DEBUG_FS */ |
| 176 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 177 | static inline u32 bcm2835_rd(struct bcm2835_spi *bs, unsigned reg) |
| 178 | { |
| 179 | return readl(bs->regs + reg); |
| 180 | } |
| 181 | |
| 182 | static inline void bcm2835_wr(struct bcm2835_spi *bs, unsigned reg, u32 val) |
| 183 | { |
| 184 | writel(val, bs->regs + reg); |
| 185 | } |
| 186 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 187 | static inline void bcm2835_rd_fifo(struct bcm2835_spi *bs) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 188 | { |
| 189 | u8 byte; |
| 190 | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 191 | while ((bs->rx_len) && |
| 192 | (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_RXD)) { |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 193 | byte = bcm2835_rd(bs, BCM2835_SPI_FIFO); |
| 194 | if (bs->rx_buf) |
| 195 | *bs->rx_buf++ = byte; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 196 | bs->rx_len--; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 200 | static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 201 | { |
| 202 | u8 byte; |
| 203 | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 204 | while ((bs->tx_len) && |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 205 | (bcm2835_rd(bs, BCM2835_SPI_CS) & BCM2835_SPI_CS_TXD)) { |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 206 | byte = bs->tx_buf ? *bs->tx_buf++ : 0; |
| 207 | bcm2835_wr(bs, BCM2835_SPI_FIFO, byte); |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 208 | bs->tx_len--; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 212 | /** |
| 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 220 | * 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] | 221 | */ |
| 222 | static inline void bcm2835_rd_fifo_count(struct bcm2835_spi *bs, int count) |
| 223 | { |
| 224 | u32 val; |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 225 | int len; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 226 | |
| 227 | bs->rx_len -= count; |
| 228 | |
| 229 | while (count > 0) { |
| 230 | val = bcm2835_rd(bs, BCM2835_SPI_FIFO); |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 231 | len = min(count, 4); |
| 232 | memcpy(bs->rx_buf, &val, len); |
| 233 | bs->rx_buf += len; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 234 | 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 | */ |
| 248 | static inline void bcm2835_wr_fifo_count(struct bcm2835_spi *bs, int count) |
| 249 | { |
| 250 | u32 val; |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 251 | int len; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 252 | |
| 253 | bs->tx_len -= count; |
| 254 | |
| 255 | while (count > 0) { |
| 256 | if (bs->tx_buf) { |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 257 | len = min(count, 4); |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 258 | 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 271 | * |
| 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 275 | */ |
| 276 | static 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 Wunner | 2e0733b | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 282 | /** |
| 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 | */ |
| 287 | static 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 | */ |
| 307 | static 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 Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 321 | static 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 329 | BCM2835_SPI_CS_DMAEN | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 330 | 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 336 | /* as well as DLEN */ |
| 337 | bcm2835_wr(bs, BCM2835_SPI_DLEN, 0); |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 340 | static 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 Wunner | 2e0733b | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 344 | 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 Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 357 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 358 | /* Read as many bytes as possible from FIFO */ |
| 359 | bcm2835_rd_fifo(bs); |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 360 | /* Write as many bytes as possible to FIFO */ |
| 361 | bcm2835_wr_fifo(bs); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 362 | |
Lukas Wunner | 56c1723 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 363 | if (!bs->rx_len) { |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 364 | /* Transfer complete - reset SPI HW */ |
| 365 | bcm2835_spi_reset_hw(master); |
| 366 | /* wake up the framework */ |
| 367 | complete(&master->xfer_completion); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 368 | } |
| 369 | |
Martin Sperl | 4adf312 | 2015-03-23 15:11:53 +0100 | [diff] [blame] | 370 | return IRQ_HANDLED; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 371 | } |
| 372 | |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 373 | static int bcm2835_spi_transfer_one_irq(struct spi_master *master, |
| 374 | struct spi_device *spi, |
| 375 | struct spi_transfer *tfr, |
Lukas Wunner | 2e0733b | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 376 | u32 cs, bool fifo_empty) |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 377 | { |
| 378 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 379 | |
Martin Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 380 | /* update usage statistics */ |
| 381 | bs->count_transfer_irq++; |
| 382 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 383 | /* |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 384 | * Enable HW block, but with interrupts still disabled. |
| 385 | * Otherwise the empty TX FIFO would immediately trigger an interrupt. |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 386 | */ |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 387 | bcm2835_wr(bs, BCM2835_SPI_CS, cs | BCM2835_SPI_CS_TA); |
| 388 | |
| 389 | /* fill TX FIFO as much as possible */ |
Lukas Wunner | 2e0733b | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 390 | if (fifo_empty) |
| 391 | bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE); |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 392 | bcm2835_wr_fifo(bs); |
| 393 | |
| 394 | /* enable interrupts */ |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 395 | 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] | 396 | bcm2835_wr(bs, BCM2835_SPI_CS, cs); |
| 397 | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 398 | /* signal that we need to wait for completion */ |
| 399 | return 1; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 400 | } |
| 401 | |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 402 | /** |
| 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 | */ |
| 448 | static 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 491 | 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 494 | |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 495 | sg_dma_address(&tfr->rx_sg.sgl[0]) += bs->rx_prologue; |
| 496 | sg_dma_len(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 497 | } |
| 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 514 | sg_dma_address(&tfr->tx_sg.sgl[0]) += bs->tx_prologue; |
| 515 | sg_dma_len(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 516 | } else { |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 517 | 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 520 | } |
| 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 | */ |
| 531 | static 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 Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 539 | sg_dma_address(&tfr->rx_sg.sgl[0]) -= bs->rx_prologue; |
| 540 | sg_dma_len(&tfr->rx_sg.sgl[0]) += bs->rx_prologue; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | if (likely(!bs->tx_spillover)) { |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 544 | sg_dma_address(&tfr->tx_sg.sgl[0]) -= bs->tx_prologue; |
| 545 | sg_dma_len(&tfr->tx_sg.sgl[0]) += bs->tx_prologue; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 546 | } else { |
Lukas Wunner | b31a929 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 547 | 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 553 | static 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 Wunner | e82b0b3 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 566 | if (cmpxchg(&bs->dma_pending, true, false)) { |
Lukas Wunner | 2527704 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 567 | dmaengine_terminate_async(master->dma_tx); |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 568 | bcm2835_spi_undo_prologue(bs); |
Lukas Wunner | e82b0b3 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 569 | } |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 570 | |
| 571 | /* and mark as completed */; |
| 572 | complete(&master->xfer_completion); |
| 573 | } |
| 574 | |
| 575 | static 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 619 | static 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 Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 627 | /* update usage statistics */ |
| 628 | bs->count_transfer_dma++; |
| 629 | |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 630 | /* |
| 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 635 | |
| 636 | /* setup tx-DMA */ |
| 637 | ret = bcm2835_spi_prepare_sg(master, tfr, true); |
| 638 | if (ret) |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 639 | goto err_reset_hw; |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 640 | |
| 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 648 | bcm2835_wr(bs, BCM2835_SPI_DLEN, bs->tx_len); |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 649 | |
| 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 Wunner | 2527704 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 661 | dmaengine_terminate_sync(master->dma_tx); |
Lukas Wunner | dbc9441 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 662 | bs->dma_pending = false; |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 663 | goto err_reset_hw; |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 664 | } |
| 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 Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 671 | |
| 672 | err_reset_hw: |
| 673 | bcm2835_spi_reset_hw(master); |
| 674 | bcm2835_spi_undo_prologue(bs); |
| 675 | return ret; |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static bool bcm2835_spi_can_dma(struct spi_master *master, |
| 679 | struct spi_device *spi, |
| 680 | struct spi_transfer *tfr) |
| 681 | { |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 682 | /* we start DMA efforts only on bigger transfers */ |
| 683 | if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH) |
| 684 | return false; |
| 685 | |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 686 | /* return OK */ |
| 687 | return true; |
| 688 | } |
| 689 | |
kbuild test robot | 29ad1a7 | 2015-05-12 19:43:59 +0800 | [diff] [blame] | 690 | static void bcm2835_dma_release(struct spi_master *master) |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 691 | { |
| 692 | if (master->dma_tx) { |
Lukas Wunner | 2527704 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 693 | dmaengine_terminate_sync(master->dma_tx); |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 694 | dma_release_channel(master->dma_tx); |
| 695 | master->dma_tx = NULL; |
| 696 | } |
| 697 | if (master->dma_rx) { |
Lukas Wunner | 2527704 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 698 | dmaengine_terminate_sync(master->dma_rx); |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 699 | dma_release_channel(master->dma_rx); |
| 700 | master->dma_rx = NULL; |
| 701 | } |
| 702 | } |
| 703 | |
kbuild test robot | 29ad1a7 | 2015-05-12 19:43:59 +0800 | [diff] [blame] | 704 | static void bcm2835_dma_init(struct spi_master *master, struct device *dev) |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 705 | { |
| 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 750 | /* 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 | |
| 755 | err_config: |
| 756 | dev_err(dev, "issue configuring dma: %d - not using DMA mode\n", |
| 757 | ret); |
| 758 | err_release: |
| 759 | bcm2835_dma_release(master); |
| 760 | err: |
| 761 | return; |
| 762 | } |
| 763 | |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 764 | static int bcm2835_spi_transfer_one_poll(struct spi_master *master, |
| 765 | struct spi_device *spi, |
| 766 | struct spi_transfer *tfr, |
Martin Sperl | 9ac3f90 | 2019-04-23 20:15:08 +0000 | [diff] [blame] | 767 | u32 cs) |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 768 | { |
| 769 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
| 770 | unsigned long timeout; |
| 771 | |
Martin Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 772 | /* update usage statistics */ |
| 773 | bs->count_transfer_polling++; |
| 774 | |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 775 | /* 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 Wunner | 2e0733b | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 782 | bcm2835_wr_fifo_blind(bs, BCM2835_SPI_FIFO_SIZE); |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 783 | |
Martin Sperl | ff245d9 | 2019-04-23 20:15:11 +0000 | [diff] [blame] | 784 | /* set the timeout to at least 2 jiffies */ |
| 785 | timeout = jiffies + 2 + HZ * polling_limit_us / 1000000; |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 786 | |
| 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 Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 804 | |
| 805 | /* update usage statistics */ |
| 806 | bs->count_transfer_irq_after_polling++; |
| 807 | |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 808 | return bcm2835_spi_transfer_one_irq(master, spi, |
Lukas Wunner | 2e0733b | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 809 | tfr, cs, false); |
Martin Sperl | a750b12 | 2015-04-22 07:33:03 +0000 | [diff] [blame] | 810 | } |
| 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 Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 819 | static 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 Sperl | ff245d9 | 2019-04-23 20:15:11 +0000 | [diff] [blame] | 824 | unsigned long spi_hz, clk_hz, cdiv, spi_used_hz; |
| 825 | unsigned long hz_per_byte, byte_limit; |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 826 | 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 Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 847 | /* handle all the 3-wire mode */ |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 848 | if ((spi->mode & SPI_3WIRE) && (tfr->rx_buf)) |
| 849 | cs |= BCM2835_SPI_CS_REN; |
Martin Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 850 | else |
| 851 | cs &= ~BCM2835_SPI_CS_REN; |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 852 | |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 853 | /* |
| 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 Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 857 | */ |
Lukas Wunner | 5c09e42 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 858 | cs |= BCM2835_SPI_CS_CS_10 | BCM2835_SPI_CS_CS_01; |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 859 | |
| 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 Sperl | 7f1922e | 2019-04-23 20:15:09 +0000 | [diff] [blame] | 866 | /* 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 Sperl | ff245d9 | 2019-04-23 20:15:11 +0000 | [diff] [blame] | 872 | 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 Sperl | 7f1922e | 2019-04-23 20:15:09 +0000 | [diff] [blame] | 875 | /* run in polling mode for short transfers */ |
Martin Sperl | ff245d9 | 2019-04-23 20:15:11 +0000 | [diff] [blame] | 876 | if (tfr->len < byte_limit) |
Martin Sperl | 9ac3f90 | 2019-04-23 20:15:08 +0000 | [diff] [blame] | 877 | return bcm2835_spi_transfer_one_poll(master, spi, tfr, cs); |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 878 | |
Martin Sperl | c41d62b | 2019-04-23 20:15:10 +0000 | [diff] [blame] | 879 | /* 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 Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 883 | 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 Wunner | 2e0733b | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 887 | return bcm2835_spi_transfer_one_irq(master, spi, tfr, cs, true); |
Martin Sperl | 704f32d | 2015-04-06 17:16:30 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Martin Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 890 | static 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 Madhyastha | 8b7bd10 | 2019-04-13 20:24:14 +0200 | [diff] [blame] | 896 | 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 Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 907 | |
| 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 Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 920 | static void bcm2835_spi_handle_err(struct spi_master *master, |
| 921 | struct spi_message *msg) |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 922 | { |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 923 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
| 924 | |
| 925 | /* if an error occurred and we have an active dma, then terminate */ |
Lukas Wunner | e82b0b3 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 926 | if (cmpxchg(&bs->dma_pending, true, false)) { |
Lukas Wunner | 2527704 | 2018-11-29 16:45:24 +0100 | [diff] [blame] | 927 | dmaengine_terminate_sync(master->dma_tx); |
| 928 | dmaengine_terminate_sync(master->dma_rx); |
Lukas Wunner | 3bd7f65 | 2018-11-08 08:06:10 +0100 | [diff] [blame] | 929 | bcm2835_spi_undo_prologue(bs); |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 930 | } |
| 931 | /* and reset */ |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 932 | bcm2835_spi_reset_hw(master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 933 | } |
| 934 | |
Martin Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 935 | static int chip_match_name(struct gpio_chip *chip, void *data) |
| 936 | { |
| 937 | return !strcmp(chip->label, data); |
| 938 | } |
| 939 | |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 940 | static int bcm2835_spi_setup(struct spi_device *spi) |
| 941 | { |
Martin Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 942 | int err; |
| 943 | struct gpio_chip *chip; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 944 | /* |
| 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 Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 951 | 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 Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 965 | return 0; |
| 966 | |
Martin Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 967 | /* 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 Sperl | a30a555 | 2015-04-06 17:16:31 +0000 | [diff] [blame] | 983 | |
| 984 | return 0; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | static 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 Lin | c2b6a3a | 2013-08-05 08:43:02 +0800 | [diff] [blame] | 1003 | master->bits_per_word_mask = SPI_BPW_MASK(8); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1004 | master->num_chipselect = 3; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 1005 | master->setup = bcm2835_spi_setup; |
Martin Sperl | e34ff01 | 2015-03-26 11:08:36 +0100 | [diff] [blame] | 1006 | master->transfer_one = bcm2835_spi_transfer_one; |
| 1007 | master->handle_err = bcm2835_spi_handle_err; |
Martin Sperl | acace73 | 2015-07-28 14:03:12 +0000 | [diff] [blame] | 1008 | master->prepare_message = bcm2835_spi_prepare_message; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1009 | master->dev.of_node = pdev->dev.of_node; |
| 1010 | |
| 1011 | bs = spi_master_get_devdata(master); |
| 1012 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1013 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Laurent Navet | 2d6e75e | 2013-05-02 14:13:30 +0200 | [diff] [blame] | 1014 | bs->regs = devm_ioremap_resource(&pdev->dev, res); |
| 1015 | if (IS_ERR(bs->regs)) { |
| 1016 | err = PTR_ERR(bs->regs); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1017 | 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 Sperl | ddf0e1c | 2015-10-15 10:09:11 +0000 | [diff] [blame] | 1027 | bs->irq = platform_get_irq(pdev, 0); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1028 | 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 Sperl | ddf0e1c | 2015-10-15 10:09:11 +0000 | [diff] [blame] | 1036 | 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 Han | 08bc054 | 2013-12-09 19:25:00 +0900 | [diff] [blame] | 1042 | err = devm_request_irq(&pdev->dev, bs->irq, bcm2835_spi_interrupt, 0, |
Martin Sperl | 342f948 | 2015-03-20 15:26:11 +0100 | [diff] [blame] | 1043 | dev_name(&pdev->dev), master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1044 | if (err) { |
| 1045 | dev_err(&pdev->dev, "could not request IRQ: %d\n", err); |
| 1046 | goto out_clk_disable; |
| 1047 | } |
| 1048 | |
Jingoo Han | 247263d | 2013-09-24 13:23:00 +0900 | [diff] [blame] | 1049 | err = devm_spi_register_master(&pdev->dev, master); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1050 | if (err) { |
| 1051 | dev_err(&pdev->dev, "could not register SPI master: %d\n", err); |
Jingoo Han | 08bc054 | 2013-12-09 19:25:00 +0900 | [diff] [blame] | 1052 | goto out_clk_disable; |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1053 | } |
| 1054 | |
Martin Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 1055 | bcm2835_debugfs_create(bs, dev_name(&pdev->dev)); |
| 1056 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1057 | return 0; |
| 1058 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1059 | out_clk_disable: |
| 1060 | clk_disable_unprepare(bs->clk); |
| 1061 | out_master_put: |
| 1062 | spi_master_put(master); |
| 1063 | return err; |
| 1064 | } |
| 1065 | |
| 1066 | static int bcm2835_spi_remove(struct platform_device *pdev) |
| 1067 | { |
Wei Yongjun | e0b35b8 | 2013-11-15 15:43:27 +0800 | [diff] [blame] | 1068 | struct spi_master *master = platform_get_drvdata(pdev); |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1069 | struct bcm2835_spi *bs = spi_master_get_devdata(master); |
| 1070 | |
Martin Sperl | 154f7da | 2019-04-23 20:15:13 +0000 | [diff] [blame^] | 1071 | bcm2835_debugfs_remove(bs); |
| 1072 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1073 | /* 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 Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1078 | |
Martin Sperl | 3ecd37e | 2015-05-10 20:47:28 +0000 | [diff] [blame] | 1079 | bcm2835_dma_release(master); |
| 1080 | |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | static const struct of_device_id bcm2835_spi_match[] = { |
| 1085 | { .compatible = "brcm,bcm2835-spi", }, |
| 1086 | {} |
| 1087 | }; |
| 1088 | MODULE_DEVICE_TABLE(of, bcm2835_spi_match); |
| 1089 | |
| 1090 | static struct platform_driver bcm2835_spi_driver = { |
| 1091 | .driver = { |
| 1092 | .name = DRV_NAME, |
Chris Boot | f804387 | 2013-03-11 21:38:24 -0600 | [diff] [blame] | 1093 | .of_match_table = bcm2835_spi_match, |
| 1094 | }, |
| 1095 | .probe = bcm2835_spi_probe, |
| 1096 | .remove = bcm2835_spi_remove, |
| 1097 | }; |
| 1098 | module_platform_driver(bcm2835_spi_driver); |
| 1099 | |
| 1100 | MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2835"); |
| 1101 | MODULE_AUTHOR("Chris Boot <bootc@bootc.net>"); |
Stefan Wahren | 22bf6cd | 2018-10-23 13:06:08 +0200 | [diff] [blame] | 1102 | MODULE_LICENSE("GPL"); |